aboutsummaryrefslogtreecommitdiff
path: root/networking/rptra6.c
blob: 9499125e289857862752c998b388bbc67c75e3f3 (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
/*
 * 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	"P:"

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

#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);
}

int rptra6_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE;
int rptra6_main(int argc, char *argv[])
{
	int i, r, first, sock, on, nrecv, rcvd_ttl, olen;
	uint8_t flags_reserved;
	size_t o;
	char *new_name, *out_name;
	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 icmp6_hdr * icmp;
	struct cmsghdr *cmsgptr;
	struct sockaddr_in6 *sin6p;
	FILE *of;
	char *str_pidfile;
	struct stat sb;
	struct sockaddr_in6 remote;          /* responding internet address */
	struct sockaddr_in6 loc_sin6;
	struct msghdr msg;
	struct iovec iov[1];
	char namebuf[NI_MAXHOST];
	char cmsgbuf[256];
	char packet[4096];

	str_pidfile= NULL;
	(void) getopt32(argv, OPT_STRING, &str_pidfile);
	
	if (argc != optind+2)
		usage();

	new_name= argv[optind];
	out_name= argv[optind+1];

	if (str_pidfile)
	{
		of= fopen(str_pidfile, "w");
		if (of)
		{
			fprintf(of, "%d\n", getpid());
			fclose(of);
		}
	}
	
	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;

	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;
		}

		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 *)&remote, msg.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(&msg); cmsgptr; 
			cmsgptr= CMSG_NXTHDR(&msg, 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)
			continue;
		if (errno == ENOENT)
		{
			rename(new_name, out_name);
			continue;
		}
		fprintf(stderr, "stat '%s' failed: %s\n", out_name, strerror(errno));
		exit(1);
	}

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