aboutsummaryrefslogtreecommitdiff
path: root/networking/rptra6.c
blob: c015134f013bff8e17967d08202fbec92d4f6099 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
 * Copyright (c) 2013-2014 RIPE NCC <atlas@ripe.net>
 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
 */
#include "libbb.h"

#include <netinet/in.h>
#include <netinet/ip_icmp.h>
#include <netinet/icmp6.h>

#define OPT_STRING	"lP:r:"

enum {
        OPT_l = (1 << 0),
};

#define DBQ(str) "\"" #str "\""

#define N_DNS	3	/* Number of DNS resolvers to keep track of */

#define IN6ADDR_ALL_NODES_INIT { { { 0xff,0x02,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
struct in6_addr in6addr_all_nodes = IN6ADDR_ALL_NODES_INIT;        /* ff02::1 */

#define OPT_RDNSS	25

#define RA_PREF_MASK	0x18
#define RA_PREF_HIGH	0x08
#define RA_PREF_LOW	0x18

struct opt_rdnss             /* RDNSS option */
{
	uint8_t   nd_opt_rdnss_type;
	uint8_t   nd_opt_rdnss_len;
	uint16_t  nd_opt_rdnss_reserved;
	uint32_t  nd_opt_rdnss_lifetime;
};

static void usage(void)
{
	fprintf(stderr, "Usage: rptra6 <new> <out>\n");
	exit(1);
}

static void do_resolv(char *str_resolv, char *str_resolv_new,
	char *packet, ssize_t nrecv,
	char dnscurr[N_DNS][INET6_ADDRSTRLEN], 
	time_t *dnsexpires)
{
	int i, olen, n_dns;
	size_t o;
	uint32_t lifetime;
	struct nd_router_advert *ra;
	struct nd_opt_hdr *oh;
	struct opt_rdnss *rdnss;
	FILE *f;
	char namebuf[NI_MAXHOST];
	char dnsnext[N_DNS][INET6_ADDRSTRLEN];

	ra= (struct nd_router_advert *)packet;

	/* Clear resolver list */
	for (n_dns= 0; n_dns < N_DNS; n_dns++)
		strcpy(dnsnext[n_dns], "");
			
	for (o= sizeof(*ra); o<nrecv;)
	{
		if (o+sizeof(*oh) > nrecv)
		{
			printf("partial option\n");
			break;
		}

		oh= (struct nd_opt_hdr *)&packet[o];
		if (oh->nd_opt_len == 0)
		{
			printf("bad option length (0) at %ld\n",
				(long)o);
			break;
		}
		olen= oh->nd_opt_len * 8;

		switch(oh->nd_opt_type)
		{
		case OPT_RDNSS:	/* 25 */

			rdnss= (struct opt_rdnss *)oh;
			lifetime= ntohl(rdnss->nd_opt_rdnss_lifetime);
			/* Assume one year is infinite enough */
			if (lifetime == (uint32_t)-1)
				lifetime= 365*24*3600;

			n_dns= 0;

			for (i= 8; i+16 <= olen; i+= 16)
			{
				if (lifetime == 0)
				{
					/* zero lifetime implies empty list */
					break;
				}
				inet_ntop(AF_INET6, ((char *)oh)+i,
					namebuf, sizeof(namebuf));
				if (n_dns < N_DNS)
				{
					strcpy(dnsnext[n_dns], namebuf);
					n_dns++;
				}
			}

			/* Check if the list of resolvers changed */
			for (n_dns= 0; n_dns < N_DNS; n_dns++)
			{
				if (strcmp(dnscurr[n_dns],
					dnsnext[n_dns]) != 0)
				{
					break;
				}
			}
			if (str_resolv && n_dns < N_DNS)
			{
				memcpy(dnscurr, dnsnext,
					sizeof(dnsnext));

				/* Ignore errors */
				f= fopen(str_resolv_new, "w");
				for (n_dns= 0; n_dns<N_DNS; n_dns++)
				{
					if (strlen(dnscurr[n_dns]) == 0)
						break;
					fprintf(f, "nameserver %s\n",
						dnscurr[n_dns]);
				}
				fclose(f);
				rename(str_resolv_new, str_resolv);
			}
			if (lifetime)
				*dnsexpires= time(NULL) + lifetime;
			else
				*dnsexpires= 0;
		
			break;
		}


		o += olen;
	}

	/* Check if we have to expire DNS entries */
	if (*dnsexpires && *dnsexpires < time(NULL))
	{
		*dnsexpires= 0;
		for (n_dns= 0; n_dns<N_DNS; n_dns++)
			strcpy(dnscurr[n_dns], "!");
		if (str_resolv)
		{
			/* Ignore errors */
			f= fopen(str_resolv_new, "w");
			fclose(f);
			rename(str_resolv_new, str_resolv);
		}
	}
}

static void log_ra(char *out_name, char *new_name,
	struct sockaddr_in6 *remotep,
	struct msghdr *msgp, char *packet, ssize_t nrecv)
{
	int i, r, first, rcvd_ttl, olen;
	uint8_t flags_reserved;
	size_t o;
	FILE *of;
	struct cmsghdr *cmsgptr;
	struct sockaddr_in6 *sin6p;
	struct sockaddr_in6 loc_sin6;
	struct nd_router_advert *ra;
	struct nd_opt_hdr *oh;
	struct nd_opt_prefix_info *pi;
	struct nd_opt_mtu *mtup;
	struct opt_rdnss *rdnssp;
	struct stat sb;
	char namebuf[NI_MAXHOST];

	of= fopen(new_name, "a");
	if (of == NULL)
	{
		fprintf(stderr, "unable to open '%s': %s\n", new_name, strerror(errno));
		exit(1);
	}

	fprintf(of, "RESULT { " DBQ(id) ": " DBQ(9019) ", " DBQ(time) ": %ld",
		(long)time(NULL));
	getnameinfo((struct sockaddr *)remotep, msgp->msg_namelen,
		namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST);
	fprintf(of, ", " DBQ(src) ": " DBQ(%s), namebuf);

	/* Set destination address of packet as local address */
	memset(&loc_sin6, '\0', sizeof(loc_sin6));
	for (cmsgptr= CMSG_FIRSTHDR(msgp); cmsgptr; 
		cmsgptr= CMSG_NXTHDR(msgp, cmsgptr))
	{
		if (cmsgptr->cmsg_len == 0)
			break;	/* Can this happen? */
		if (cmsgptr->cmsg_level == IPPROTO_IPV6 &&
			cmsgptr->cmsg_type == IPV6_PKTINFO)
		{
			sin6p= &loc_sin6;
			sin6p->sin6_family= AF_INET6;
			sin6p->sin6_addr= ((struct in6_pktinfo *)
				CMSG_DATA(cmsgptr))->ipi6_addr;
		}
		if (cmsgptr->cmsg_level == IPPROTO_IPV6 &&
			cmsgptr->cmsg_type == IPV6_HOPLIMIT)
		{
			rcvd_ttl= *(int *)CMSG_DATA(cmsgptr);
		}
	}

	if (memcmp(&loc_sin6.sin6_addr, &in6addr_all_nodes,
		sizeof(loc_sin6.sin6_addr)) != 0)
	{
		getnameinfo((struct sockaddr *)&loc_sin6, sizeof(loc_sin6),
			namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST);
		fprintf(of, ", " DBQ(dst) ": " DBQ(%s), namebuf);
	}
	if (rcvd_ttl != 255)
		fprintf(of, ", " DBQ(ttl) ": %d", rcvd_ttl);

	ra= (struct nd_router_advert *)packet;
	fprintf(of, ", " DBQ(hop_limit) ": %d", ra->nd_ra_curhoplimit);
	flags_reserved= ra->nd_ra_flags_reserved;
	if (flags_reserved & ND_RA_FLAG_OTHER)
	{
		fprintf(of, ", " DBQ(other_conf) ": true");
		flags_reserved &= ~ND_RA_FLAG_OTHER;
	}
	switch(flags_reserved & RA_PREF_MASK)
	{
	case RA_PREF_HIGH:
		fprintf(of, ", " DBQ(preference) ": " DBQ(high));
		flags_reserved &= ~RA_PREF_MASK;
		break;
	case RA_PREF_LOW:
		fprintf(of, ", " DBQ(preference) ": " DBQ(low));
		flags_reserved &= ~RA_PREF_MASK;
		break;
	}
	if (flags_reserved)
		fprintf(of, ", " DBQ(reserved) ": 0x%x", flags_reserved);
	fprintf(of, ", " DBQ(lifetime) ": %d", ntohs(ra->nd_ra_router_lifetime));
	if (ra->nd_ra_reachable)
		fprintf(of, ", " DBQ(reachable_time) ": %d", ntohl(ra->nd_ra_reachable));
	if (ra->nd_ra_retransmit)
		fprintf(of, ", " DBQ(retransmit_time) ": %d", ntohl(ra->nd_ra_retransmit));

	fprintf(of, ", " DBQ(options) ": [ ");
	first= 1;
	for (o= sizeof(*ra); o<nrecv;)
	{
		if (!first)
			fprintf(of, ", ");
		else
			first= 0;

		if (o+sizeof(*oh) > nrecv)
		{
			printf("partial option\n");
			break;
		}

		oh= (struct nd_opt_hdr *)&packet[o];
		if (oh->nd_opt_len == 0)
		{
			printf("bad option length (0) at %ld\n",
				(long)o);
			break;
		}
		olen= oh->nd_opt_len * 8;

		switch(oh->nd_opt_type)
		{
		case ND_OPT_SOURCE_LINKADDR:	/* 1 */
			fprintf(of, "{ " DBQ(type) ": " DBQ(link layer address) ", "
				DBQ(addr) ": \"");
			for (i= 2; i<olen; i++)
			{
				fprintf(of, "%s%02x", i == 2 ? "" : ":",
					((uint8_t *)oh)[i]);
			}
			fprintf(of, "\" }");
			break;
		case ND_OPT_PREFIX_INFORMATION:	/* 3 */
			if (olen < sizeof(*pi))
			{
				printf(
			"bad option length (%d) for prefix info\n",
					oh->nd_opt_len);
				break;
			}
			pi= (struct nd_opt_prefix_info *)oh;
			fprintf(of, "{ " DBQ(prefix_len) ": %d", 
				pi->nd_opt_pi_prefix_len);
			flags_reserved= pi->nd_opt_pi_flags_reserved;
			if (flags_reserved & ND_OPT_PI_FLAG_ONLINK)
			{
				fprintf(of, ", " DBQ(onlink) ": true");
				flags_reserved &= ~ND_OPT_PI_FLAG_ONLINK;
			}
			if (flags_reserved & ND_OPT_PI_FLAG_AUTO)
			{
				fprintf(of, ", " DBQ(auto) ": true");
				flags_reserved &= ~ND_OPT_PI_FLAG_AUTO;
			}
		
			if (flags_reserved)
			{
				fprintf(of, ", " DBQ(reserved1) ": 0x%x", flags_reserved);
			}
			fprintf(of, ", " DBQ(valid_time) ": %d",
				ntohl(pi-> nd_opt_pi_valid_time));
			fprintf(of, ", " DBQ(preferred_time) ": %d",
				ntohl(pi-> nd_opt_pi_preferred_time));
			if (pi-> nd_opt_pi_reserved2)
			{
				fprintf(of, ", " DBQ(reserved2) ": %d",
					ntohl(pi-> nd_opt_pi_reserved2));
			}

			fprintf(of, ", " DBQ(prefix) ": " DBQ(%s) " }",
				inet_ntop(AF_INET6, &pi->nd_opt_pi_prefix,
				namebuf, sizeof(namebuf)));
			break;

		case ND_OPT_MTU:	/* 5 */
			fprintf(of, "{ " DBQ(type) ": " DBQ(mtu));
			mtup= (struct nd_opt_mtu *)oh;
			if (mtup->nd_opt_mtu_reserved)
			{
				fprintf(of, ", " DBQ(reserved) ": 0x%x",
				ntohs(mtup->nd_opt_mtu_reserved));
			}
			fprintf(of, ", " DBQ(mtu) ": %d }",
				ntohl(mtup->nd_opt_mtu_mtu));
			break;

		case OPT_RDNSS:	/* 25 */
			fprintf(of, "{ " DBQ(type) ": " DBQ(rdnss));
			rdnssp= (struct opt_rdnss *)oh;
			if (rdnssp->nd_opt_rdnss_reserved)
			{
				fprintf(of, ", " DBQ(reserved) ": %d",
				ntohs(rdnssp->nd_opt_rdnss_reserved));
			}
			fprintf(of, ", " DBQ(lifetime) ": %d",
				ntohl(rdnssp->nd_opt_rdnss_lifetime));

			fprintf(of, ", " DBQ(addrs) ": [ ");
			for (i= 8; i+16 <= olen; i+= 16)
			{
				inet_ntop(AF_INET6, ((char *)oh)+i,
					namebuf, sizeof(namebuf));
				fprintf(of, "%s" DBQ(%s),
					i == 8 ? "" : ", ",
					namebuf);
			}
			fprintf(of, " ] }");

			break;

		default:
			fprintf(of, "{ " DBQ(type_no) ": %d }", oh->nd_opt_type);
			break;
		}


		o += olen;
	}
	fprintf(of, " ] }\n");

	fclose(of);

	r= stat(out_name, &sb);
	if (r == 0)
		return;
	if (errno == ENOENT)
	{
		rename(new_name, out_name);
		return;
	}
	fprintf(stderr, "stat '%s' failed: %s\n", out_name, strerror(errno));
	exit(1);
}

int rptra6_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE;
int rptra6_main(int argc, char *argv[])
{
	int i, sock, on, nrecv, do_log;
	unsigned opts;
	size_t len;
	time_t dnsexpires;
	char *new_name, *out_name, *str_resolv, *str_resolv_new;
	struct icmp6_hdr * icmp;
	FILE *of;
	char *str_pidfile;
	struct sockaddr_in6 remote;          /* responding internet address */
	struct msghdr msg;
	struct iovec iov[1];
	char dnscurr[N_DNS][INET6_ADDRSTRLEN];
	char cmsgbuf[256];
	char packet[4096];

	str_pidfile= NULL;
	str_resolv= NULL;
	opts= getopt32(argv, OPT_STRING, &str_pidfile, &str_resolv);

	do_log= !!(opts & OPT_l);
	
	if (do_log)
	{
		if (argc != optind+2)
			usage();

		new_name= argv[optind];
		out_name= argv[optind+1];
	}
	else
	{
		if (argc != optind)
			usage();
		new_name= NULL;
		out_name= NULL;
	}

	if (str_pidfile)
	{
		of= fopen(str_pidfile, "w");
		if (of)
		{
			fprintf(of, "%d\n", getpid());
			fclose(of);
		}
	}

	str_resolv_new= NULL;
	if (str_resolv)
	{
		len= strlen(str_resolv) + 4 + 1;
		str_resolv_new= malloc(len);
		snprintf(str_resolv_new, len, "%s.new", str_resolv);
	}
	
	of= NULL;

	sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
	if (sock == -1)
	{
		printf("socket failed: %s\n", strerror(errno));
		return 1;
	}

	on = 1;
	setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof(on));

	on = 1;
	setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on, sizeof(on));


	icmp = (struct icmp6_hdr *) packet;

	/* Put something weird in the current list of DNS resolvers to 
	 * trigger an update.
	 */
	for (i= 0; i<N_DNS; i++)
		strcpy(dnscurr[i], "!");
	dnsexpires= 0;	/* Currently, there is no DNS info */

	for(;;)
	{
		iov[0].iov_base= packet;
		iov[0].iov_len= sizeof(packet);
		msg.msg_name= &remote;
		msg.msg_namelen= sizeof(remote);
		msg.msg_iov= iov;
		msg.msg_iovlen= 1;
		msg.msg_control= cmsgbuf;
		msg.msg_controllen= sizeof(cmsgbuf);
		msg.msg_flags= 0;			/* Not really needed */

		/* Receive data from the network */
		nrecv= recvmsg(sock, &msg, 0);
		if (nrecv < 0)
		{
			printf("recvmsg failed: %s\n", strerror(errno));
			break;
		}

		/* Check for Destination Host Unreachable */
		if (icmp->icmp6_type != ND_ROUTER_ADVERT)
		{
			switch(icmp->icmp6_type)
			{
			case ICMP6_DST_UNREACH:		/*   1 */
			case ICMP6_PACKET_TOO_BIG:	/*   2 */
			case ICMP6_TIME_EXCEEDED:	/*   3 */
			case ICMP6_ECHO_REQUEST:	/* 128 */
			case ICMP6_ECHO_REPLY:		/* 129 */
			case ND_NEIGHBOR_SOLICIT:	/* 135 */
			case ND_NEIGHBOR_ADVERT:	/* 136 */
			case ND_REDIRECT:		/* 137 */
				break;	/* Ignore */
			default:
				printf("icmp6_type %d\n", icmp->icmp6_type);
				break;
			}
			continue;
		}

		if (do_log)
		{
			log_ra(out_name, new_name, &remote, &msg,
				packet, nrecv);
		}
		if (str_resolv)
		{
			do_resolv(str_resolv, str_resolv_new, packet, nrecv,
				dnscurr, &dnsexpires);
		}
	}

	fprintf(stderr, "end of main\n");
	exit(1);
}