-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.c
507 lines (433 loc) · 12.9 KB
/
process.c
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
/*
*
* Authors:
* Pedro Roque <roque@di.fc.ul.pt>
* Lars Fenneberg <lf@elemental.net>
*
* This software is Copyright 1996,1997 by the above mentioned author(s),
* All Rights Reserved.
*
* The license which is distributed with this software in the file COPYRIGHT
* applies to this software. If your distribution is missing this file, you
* may request it from <pekkas@netcore.fi>.
*
*/
#include "config.h"
#include "includes.h"
#include "radvd.h"
static void process_rs(struct Interface *, unsigned char *msg,
int len, struct sockaddr_in6 *);
static void process_ra(struct Interface *, unsigned char *msg, int len,
struct sockaddr_in6 *);
static int addr_match(struct in6_addr *a1, struct in6_addr *a2,
int prefixlen);
void
process(struct Interface *ifacel, unsigned char *msg, int len,
struct sockaddr_in6 *addr, struct in6_pktinfo *pkt_info, int hoplimit)
{
struct Interface *iface;
struct icmp6_hdr *icmph;
char addr_str[INET6_ADDRSTRLEN];
print_addr(&addr->sin6_addr, addr_str);
if ( ! pkt_info )
{
flog(LOG_WARNING, "received packet with no pkt_info from %s!", addr_str );
return;
}
/*
* can this happen?
*/
if (len < (int)sizeof(struct icmp6_hdr))
{
flog(LOG_WARNING, "received icmpv6 packet with invalid length (%d) from %s",
len, addr_str);
return;
}
icmph = (struct icmp6_hdr *) msg;
if (icmph->icmp6_type != ND_ROUTER_SOLICIT &&
icmph->icmp6_type != ND_ROUTER_ADVERT)
{
/*
* We just want to listen to RSs and RAs
*/
flog(LOG_ERR, "icmpv6 filter failed");
return;
}
if (icmph->icmp6_type == ND_ROUTER_ADVERT)
{
if (len < (int)sizeof(struct nd_router_advert)) {
flog(LOG_WARNING, "received icmpv6 RA packet with invalid length (%d) from %s",
len, addr_str);
return;
}
if (!IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
flog(LOG_WARNING, "received icmpv6 RA packet with non-linklocal source address from %s", addr_str);
return;
}
}
if (icmph->icmp6_type == ND_ROUTER_SOLICIT)
{
if (len < (int)sizeof(struct nd_router_solicit)) {
flog(LOG_WARNING, "received icmpv6 RS packet with invalid length (%d) from %s",
len, addr_str);
return;
}
}
if (icmph->icmp6_code != 0)
{
flog(LOG_WARNING, "received icmpv6 RS/RA packet with invalid code (%d) from %s",
icmph->icmp6_code, addr_str);
return;
}
dlog(LOG_DEBUG, 4, "if_index %u", pkt_info->ipi6_ifindex);
/* get iface by received if_index */
for (iface = ifacel; iface; iface=iface->next)
{
if ((int)iface->if_index == pkt_info->ipi6_ifindex)
{
break;
}
}
if (iface == NULL)
{
dlog(LOG_DEBUG, 2, "received packet from unknown interface: %d",
pkt_info->ipi6_ifindex);
return;
}
if (hoplimit != 255)
{
print_addr(&addr->sin6_addr, addr_str);
flog(LOG_WARNING, "received RS or RA with invalid hoplimit %d from %s",
hoplimit, addr_str);
return;
}
if (!iface->AdvSendAdvert)
{
dlog(LOG_DEBUG, 2, "AdvSendAdvert is off for %s", iface->Name);
return;
}
dlog(LOG_DEBUG, 4, "found Interface: %s", iface->Name);
if (icmph->icmp6_type == ND_ROUTER_SOLICIT)
{
dlog(LOG_DEBUG, 4, "received RS from %s", addr_str);
process_rs(iface, msg, len, addr);
}
else if (icmph->icmp6_type == ND_ROUTER_ADVERT)
{
dlog(LOG_DEBUG, 4, "received RA from %s", addr_str);
process_ra(iface, msg, len, addr);
}
}
static void
process_rs(struct Interface *iface, unsigned char *msg, int len,
struct sockaddr_in6 *addr)
{
double delay;
double next;
struct timeval tv;
uint8_t *opt_str;
/* validation */
len -= sizeof(struct nd_router_solicit);
opt_str = (uint8_t *)(msg + sizeof(struct nd_router_solicit));
while (len > 0)
{
int optlen;
if (len < 2)
{
flog(LOG_WARNING, "trailing garbage in RS");
return;
}
optlen = (opt_str[1] << 3);
if (optlen == 0)
{
flog(LOG_WARNING, "zero length option in RS");
return;
}
else if (optlen > len)
{
flog(LOG_WARNING, "option length greater than total length in RS");
return;
}
if (*opt_str == ND_OPT_SOURCE_LINKADDR &&
IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr)) {
flog(LOG_WARNING, "received icmpv6 RS packet with unspecified source address and there is a lladdr option");
return;
}
len -= optlen;
opt_str += optlen;
}
gettimeofday(&tv, NULL);
delay = MAX_RA_DELAY_TIME * rand() / (RAND_MAX +1.0);
if (iface->UnicastOnly) {
send_ra_forall(iface, &addr->sin6_addr);
}
else if ( timevaldiff(&tv, &iface->last_multicast) / 1000.0 < iface->MinDelayBetweenRAs ) {
/* last RA was sent only a few moments ago, don't send another immediately. */
next = iface->MinDelayBetweenRAs - (tv.tv_sec + tv.tv_usec / 1000000.0) + (iface->last_multicast.tv_sec + iface->last_multicast.tv_usec / 1000000.0) + delay/1000.0;
iface->next_multicast = next_timeval(next);
}
else {
/* no RA sent in a while, send a multicast reply */
send_ra_forall(iface, NULL);
next = rand_between(iface->MinRtrAdvInterval, iface->MaxRtrAdvInterval);
iface->next_multicast = next_timeval(next);
}
}
/*
* check router advertisements according to RFC 4861, 6.2.7
*/
static void
process_ra(struct Interface *iface, unsigned char *msg, int len,
struct sockaddr_in6 *addr)
{
struct nd_router_advert *radvert;
char addr_str[INET6_ADDRSTRLEN];
uint8_t *opt_str;
print_addr(&addr->sin6_addr, addr_str);
radvert = (struct nd_router_advert *) msg;
if ((radvert->nd_ra_curhoplimit && iface->AdvCurHopLimit) &&
(radvert->nd_ra_curhoplimit != iface->AdvCurHopLimit))
{
flog(LOG_WARNING, "our AdvCurHopLimit on %s doesn't agree with %s",
iface->Name, addr_str);
}
if ((radvert->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED) && !iface->AdvManagedFlag)
{
flog(LOG_WARNING, "our AdvManagedFlag on %s doesn't agree with %s",
iface->Name, addr_str);
}
if ((radvert->nd_ra_flags_reserved & ND_RA_FLAG_OTHER) && !iface->AdvOtherConfigFlag)
{
flog(LOG_WARNING, "our AdvOtherConfigFlag on %s doesn't agree with %s",
iface->Name, addr_str);
}
/* note: we don't check the default router preference here, because they're likely different */
if ((radvert->nd_ra_reachable && iface->AdvReachableTime) &&
(ntohl(radvert->nd_ra_reachable) != iface->AdvReachableTime))
{
flog(LOG_WARNING, "our AdvReachableTime on %s doesn't agree with %s",
iface->Name, addr_str);
}
if ((radvert->nd_ra_retransmit && iface->AdvRetransTimer) &&
(ntohl(radvert->nd_ra_retransmit) != iface->AdvRetransTimer))
{
flog(LOG_WARNING, "our AdvRetransTimer on %s doesn't agree with %s",
iface->Name, addr_str);
}
len -= sizeof(struct nd_router_advert);
if (len == 0)
return;
opt_str = (uint8_t *)(msg + sizeof(struct nd_router_advert));
while (len > 0)
{
int optlen;
struct nd_opt_prefix_info *pinfo;
struct nd_opt_rdnss_info_local *rdnssinfo;
struct nd_opt_dnssl_info_local *dnsslinfo;
struct nd_opt_mtu *mtu;
struct AdvPrefix *prefix;
struct AdvRDNSS *rdnss;
char prefix_str[INET6_ADDRSTRLEN];
char rdnss_str[INET6_ADDRSTRLEN];
char suffix[256];
unsigned int offset, label_len;
uint32_t preferred, valid, count;
if (len < 2)
{
flog(LOG_ERR, "trailing garbage in RA on %s from %s",
iface->Name, addr_str);
break;
}
optlen = (opt_str[1] << 3);
if (optlen == 0)
{
flog(LOG_ERR, "zero length option in RA on %s from %s",
iface->Name, addr_str);
break;
}
else if (optlen > len)
{
flog(LOG_ERR, "option length (%d) greater than total"
" length (%d) in RA on %s from %s",
optlen, len,
iface->Name, addr_str);
break;
}
switch (*opt_str)
{
case ND_OPT_MTU:
mtu = (struct nd_opt_mtu *)opt_str;
if (len < (int)sizeof(*mtu))
return;
if (iface->AdvLinkMTU && (ntohl(mtu->nd_opt_mtu_mtu) != iface->AdvLinkMTU))
{
flog(LOG_WARNING, "our AdvLinkMTU on %s doesn't agree with %s",
iface->Name, addr_str);
}
break;
case ND_OPT_PREFIX_INFORMATION:
pinfo = (struct nd_opt_prefix_info *) opt_str;
if (len < (int)sizeof(*pinfo))
return;
preferred = ntohl(pinfo->nd_opt_pi_preferred_time);
valid = ntohl(pinfo->nd_opt_pi_valid_time);
prefix = iface->AdvPrefixList;
while (prefix)
{
if (prefix->enabled &&
(prefix->PrefixLen == pinfo->nd_opt_pi_prefix_len) &&
addr_match(&prefix->Prefix, &pinfo->nd_opt_pi_prefix,
prefix->PrefixLen))
{
print_addr(&prefix->Prefix, prefix_str);
if (!prefix->DecrementLifetimesFlag && valid != prefix->AdvValidLifetime)
{
flog(LOG_WARNING, "our AdvValidLifetime on"
" %s for %s doesn't agree with %s",
iface->Name,
prefix_str,
addr_str
);
}
if (!prefix->DecrementLifetimesFlag && preferred != prefix->AdvPreferredLifetime)
{
flog(LOG_WARNING, "our AdvPreferredLifetime on"
" %s for %s doesn't agree with %s",
iface->Name,
prefix_str,
addr_str
);
}
}
prefix = prefix->next;
}
break;
case ND_OPT_ROUTE_INFORMATION:
/* not checked: these will very likely vary a lot */
break;
case ND_OPT_SOURCE_LINKADDR:
/* not checked */
break;
case ND_OPT_TARGET_LINKADDR:
case ND_OPT_REDIRECTED_HEADER:
flog(LOG_ERR, "invalid option %d in RA on %s from %s",
(int)*opt_str, iface->Name, addr_str);
break;
/* Mobile IPv6 extensions */
case ND_OPT_RTR_ADV_INTERVAL:
case ND_OPT_HOME_AGENT_INFO:
/* not checked */
break;
case ND_OPT_RDNSS_INFORMATION:
rdnssinfo = (struct nd_opt_rdnss_info_local *) opt_str;
if (len < (int)sizeof(*rdnssinfo))
return;
count = rdnssinfo->nd_opt_rdnssi_len;
/* Check the RNDSS addresses received */
switch (count) {
case 7:
rdnss = iface->AdvRDNSSList;
if (!check_rdnss_presence(rdnss, &rdnssinfo->nd_opt_rdnssi_addr3 )) {
/* no match found in iface->AdvRDNSSList */
print_addr(&rdnssinfo->nd_opt_rdnssi_addr3, rdnss_str);
flog(LOG_WARNING, "RDNSS address %s received on %s from %s is not advertised by us",
rdnss_str, iface->Name, addr_str);
}
/* FALLTHROUGH */
case 5:
rdnss = iface->AdvRDNSSList;
if (!check_rdnss_presence(rdnss, &rdnssinfo->nd_opt_rdnssi_addr2 )) {
/* no match found in iface->AdvRDNSSList */
print_addr(&rdnssinfo->nd_opt_rdnssi_addr2, rdnss_str);
flog(LOG_WARNING, "RDNSS address %s received on %s from %s is not advertised by us",
rdnss_str, iface->Name, addr_str);
}
/* FALLTHROUGH */
case 3:
rdnss = iface->AdvRDNSSList;
if (!check_rdnss_presence(rdnss, &rdnssinfo->nd_opt_rdnssi_addr1 )) {
/* no match found in iface->AdvRDNSSList */
print_addr(&rdnssinfo->nd_opt_rdnssi_addr1, rdnss_str);
flog(LOG_WARNING, "RDNSS address %s received on %s from %s is not advertised by us",
rdnss_str, iface->Name, addr_str);
}
break;
default:
flog(LOG_ERR, "invalid len %i in RDNSS option on %s from %s",
count, iface->Name, addr_str);
}
break;
case ND_OPT_DNSSL_INFORMATION:
dnsslinfo = (struct nd_opt_dnssl_info_local *) opt_str;
if (len < (int)sizeof(*dnsslinfo))
return;
suffix[0] = '\0';
for (offset = 0; (int)offset < (dnsslinfo->nd_opt_dnssli_len-1)*8;) {
if (&dnsslinfo->nd_opt_dnssli_suffixes[offset] - opt_str >= len)
return;
label_len = dnsslinfo->nd_opt_dnssli_suffixes[offset++];
if (label_len == 0) {
/*
* Ignore empty suffixes. They're
* probably just padding...
*/
if (suffix[0] == '\0')
continue;
if (!check_dnssl_presence(iface->AdvDNSSLList, suffix)) {
flog(LOG_WARNING, "DNSSL suffix %s received on %s from %s is not advertised by us",
suffix, iface->Name, addr_str);
}
suffix[0] = '\0';
continue;
}
/*
* 1) must not overflow int: label + 2, offset + label_len
* 2) last byte of dnssli_suffix must not overflow opt_str + len
*/
if ((sizeof(suffix) - strlen(suffix)) < (label_len + 2) ||
label_len > label_len + 2 ||
&dnsslinfo->nd_opt_dnssli_suffixes[offset+label_len] - opt_str >= len ||
offset + label_len < offset) {
flog(LOG_ERR, "oversized suffix in DNSSL option on %s from %s",
iface->Name, addr_str);
break;
}
if (suffix[0] != '\0')
strcat(suffix, ".");
strncat(suffix, (char*)&dnsslinfo->nd_opt_dnssli_suffixes[offset], label_len);
offset += label_len;
}
break;
default:
dlog(LOG_DEBUG, 1, "unknown option %d in RA on %s from %s",
(int)*opt_str, iface->Name, addr_str);
break;
}
len -= optlen;
opt_str += optlen;
}
}
static int
addr_match(struct in6_addr *a1, struct in6_addr *a2, int prefixlen)
{
unsigned int pdw;
unsigned int pbi;
pdw = prefixlen >> 0x05; /* num of whole uint32_t in prefix */
pbi = prefixlen & 0x1f; /* num of bits in incomplete uint32_t in prefix */
if (pdw)
{
if (memcmp(a1, a2, pdw << 2))
return 0;
}
if (pbi)
{
uint32_t w1, w2;
uint32_t mask;
w1 = *((uint32_t *)a1 + pdw);
w2 = *((uint32_t *)a2 + pdw);
mask = htonl(((uint32_t) 0xffffffff) << (0x20 - pbi));
if ((w1 ^ w2) & mask)
return 0;
}
return 1;
}