router: use ra_lifetime as lifetime for RA options (FS#2206)
[project/odhcpd.git] / src / router.c
1 /**
2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
3 * Copyright (C) 2018 Hans Dedecker <dedeckeh@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License v2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <signal.h>
19 #include <resolv.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <stdbool.h>
24 #include <arpa/inet.h>
25 #include <net/route.h>
26
27 #include <libubox/utils.h>
28
29 #include "router.h"
30 #include "odhcpd.h"
31
32
33 static void forward_router_solicitation(const struct interface *iface);
34 static void forward_router_advertisement(const struct interface *iface, uint8_t *data, size_t len);
35
36 static void handle_icmpv6(void *addr, void *data, size_t len,
37 struct interface *iface, void *dest);
38 static void trigger_router_advert(struct uloop_timeout *event);
39 static void router_netevent_cb(unsigned long event, struct netevent_handler_info *info);
40
41 static struct netevent_handler router_netevent_handler = { .cb = router_netevent_cb, };
42
43 static FILE *fp_route = NULL;
44
45
46 #define TIME_LEFT(t1, now) ((t1) != UINT32_MAX ? (t1) - (now) : UINT32_MAX)
47
48 int router_init(void)
49 {
50 int ret = 0;
51
52 if (!(fp_route = fopen("/proc/net/ipv6_route", "r"))) {
53 syslog(LOG_ERR, "fopen(/proc/net/ipv6_route): %m");
54 ret = -1;
55 goto out;
56 }
57
58 if (netlink_add_netevent_handler(&router_netevent_handler) < 0) {
59 syslog(LOG_ERR, "Failed to add netevent handler");
60 ret = -1;
61 }
62
63 out:
64 if (ret < 0 && fp_route) {
65 fclose(fp_route);
66 fp_route = NULL;
67 }
68
69 return ret;
70 }
71
72
73 int router_setup_interface(struct interface *iface, bool enable)
74 {
75 int ret = 0;
76
77 if (!fp_route) {
78 ret = -1;
79 goto out;
80 }
81
82
83 if ((!enable || iface->ra == MODE_DISABLED) && iface->router_event.uloop.fd >= 0) {
84 if (!iface->master) {
85 uloop_timeout_cancel(&iface->timer_rs);
86 iface->timer_rs.cb = NULL;
87
88 trigger_router_advert(&iface->timer_rs);
89 }
90
91 uloop_fd_delete(&iface->router_event.uloop);
92 close(iface->router_event.uloop.fd);
93 iface->router_event.uloop.fd = -1;
94 } else if (enable && iface->ra != MODE_DISABLED) {
95 struct icmp6_filter filt;
96 struct ipv6_mreq mreq;
97 int val = 2;
98
99 if (iface->router_event.uloop.fd < 0) {
100 /* Open ICMPv6 socket */
101 iface->router_event.uloop.fd = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC,
102 IPPROTO_ICMPV6);
103 if (iface->router_event.uloop.fd < 0) {
104 syslog(LOG_ERR, "socket(AF_INET6): %m");
105 ret = -1;
106 goto out;
107 }
108
109 if (setsockopt(iface->router_event.uloop.fd, SOL_SOCKET, SO_BINDTODEVICE,
110 iface->ifname, strlen(iface->ifname)) < 0) {
111 syslog(LOG_ERR, "setsockopt(SO_BINDTODEVICE): %m");
112 ret = -1;
113 goto out;
114 }
115
116 /* Let the kernel compute our checksums */
117 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_RAW, IPV6_CHECKSUM,
118 &val, sizeof(val)) < 0) {
119 syslog(LOG_ERR, "setsockopt(IPV6_CHECKSUM): %m");
120 ret = -1;
121 goto out;
122 }
123
124 /* This is required by RFC 4861 */
125 val = 255;
126 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
127 &val, sizeof(val)) < 0) {
128 syslog(LOG_ERR, "setsockopt(IPV6_MULTICAST_HOPS): %m");
129 ret = -1;
130 goto out;
131 }
132
133 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
134 &val, sizeof(val)) < 0) {
135 syslog(LOG_ERR, "setsockopt(IPV6_UNICAST_HOPS): %m");
136 ret = -1;
137 goto out;
138 }
139
140 /* We need to know the source interface */
141 val = 1;
142 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
143 &val, sizeof(val)) < 0) {
144 syslog(LOG_ERR, "setsockopt(IPV6_RECVPKTINFO): %m");
145 ret = -1;
146 goto out;
147 }
148
149 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
150 &val, sizeof(val)) < 0) {
151 syslog(LOG_ERR, "setsockopt(IPV6_RECVHOPLIMIT): %m");
152 ret = -1;
153 goto out;
154 }
155
156 /* Don't loop back */
157 val = 0;
158 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
159 &val, sizeof(val)) < 0) {
160 syslog(LOG_ERR, "setsockopt(IPV6_MULTICAST_LOOP): %m");
161 ret = -1;
162 goto out;
163 }
164
165 /* Filter ICMPv6 package types */
166 ICMP6_FILTER_SETBLOCKALL(&filt);
167 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
168 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filt);
169 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_ICMPV6, ICMP6_FILTER,
170 &filt, sizeof(filt)) < 0) {
171 syslog(LOG_ERR, "setsockopt(ICMP6_FILTER): %m");
172 ret = -1;
173 goto out;
174 }
175
176 iface->router_event.handle_dgram = handle_icmpv6;
177 odhcpd_register(&iface->router_event);
178 } else {
179 uloop_timeout_cancel(&iface->timer_rs);
180 iface->timer_rs.cb = NULL;
181
182 memset(&mreq, 0, sizeof(mreq));
183 mreq.ipv6mr_interface = iface->ifindex;
184 inet_pton(AF_INET6, ALL_IPV6_NODES, &mreq.ipv6mr_multiaddr);
185 setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP,
186 &mreq, sizeof(mreq));
187
188 inet_pton(AF_INET6, ALL_IPV6_ROUTERS, &mreq.ipv6mr_multiaddr);
189 setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP,
190 &mreq, sizeof(mreq));
191 }
192
193 memset(&mreq, 0, sizeof(mreq));
194 mreq.ipv6mr_interface = iface->ifindex;
195 inet_pton(AF_INET6, ALL_IPV6_ROUTERS, &mreq.ipv6mr_multiaddr);
196
197 if (iface->ra == MODE_RELAY && iface->master) {
198 inet_pton(AF_INET6, ALL_IPV6_NODES, &mreq.ipv6mr_multiaddr);
199 forward_router_solicitation(iface);
200 } else if (iface->ra == MODE_SERVER) {
201 iface->timer_rs.cb = trigger_router_advert;
202 uloop_timeout_set(&iface->timer_rs, 1000);
203 }
204
205 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6,
206 IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
207 ret = -1;
208 syslog(LOG_ERR, "setsockopt(IPV6_ADD_MEMBERSHIP): %m");
209 goto out;
210 }
211 }
212 out:
213 if (ret < 0 && iface->router_event.uloop.fd >= 0) {
214 close(iface->router_event.uloop.fd);
215 iface->router_event.uloop.fd = -1;
216 }
217
218 return ret;
219 }
220
221
222 static void router_netevent_cb(unsigned long event, struct netevent_handler_info *info)
223 {
224 struct interface *iface;
225
226 switch (event) {
227 case NETEV_ROUTE6_ADD:
228 case NETEV_ROUTE6_DEL:
229 if (info->rt.dst_len)
230 break;
231
232 avl_for_each_element(&interfaces, iface, avl) {
233 if (iface->ra == MODE_SERVER && !iface->master)
234 uloop_timeout_set(&iface->timer_rs, 1000);
235 }
236 break;
237 case NETEV_ADDR6LIST_CHANGE:
238 iface = info->iface;
239 if (iface && iface->ra == MODE_SERVER && !iface->master)
240 uloop_timeout_set(&iface->timer_rs, 1000);
241 break;
242 default:
243 break;
244 }
245 }
246
247
248 static bool router_icmpv6_valid(struct sockaddr_in6 *source, uint8_t *data, size_t len)
249 {
250 struct icmp6_hdr *hdr = (struct icmp6_hdr *)data;
251 struct icmpv6_opt *opt, *end = (struct icmpv6_opt*)&data[len];
252
253 /* Hoplimit is already checked in odhcpd_receive_packets */
254 if (len < sizeof(*hdr) || hdr->icmp6_code)
255 return false;
256
257 switch (hdr->icmp6_type) {
258 case ND_ROUTER_ADVERT:
259 if (!IN6_IS_ADDR_LINKLOCAL(&source->sin6_addr))
260 return false;
261
262 opt = (struct icmpv6_opt *)((struct nd_router_advert *)data + 1);
263 break;
264
265 case ND_ROUTER_SOLICIT:
266 opt = (struct icmpv6_opt *)((struct nd_router_solicit *)data + 1);
267 break;
268
269 default:
270 return false;
271 }
272
273 icmpv6_for_each_option(opt, opt, end)
274 if (opt->type == ND_OPT_SOURCE_LINKADDR &&
275 IN6_IS_ADDR_UNSPECIFIED(&source->sin6_addr) &&
276 hdr->icmp6_type == ND_ROUTER_SOLICIT)
277 return false;
278
279 /* Check all options parsed successfully */
280 return opt == end;
281 }
282
283
284 /* Detect whether a default route exists, also find the source prefixes */
285 static bool parse_routes(struct odhcpd_ipaddr *n, ssize_t len)
286 {
287 struct odhcpd_ipaddr p = { .addr.in6 = IN6ADDR_ANY_INIT, .prefix = 0,
288 .dprefix = 0, .preferred = 0, .valid = 0};
289 bool found_default = false;
290 char line[512], ifname[16];
291
292 rewind(fp_route);
293
294 while (fgets(line, sizeof(line), fp_route)) {
295 uint32_t rflags;
296 if (sscanf(line, "00000000000000000000000000000000 00 "
297 "%*s %*s %*s %*s %*s %*s %*s %15s", ifname) &&
298 strcmp(ifname, "lo")) {
299 found_default = true;
300 } else if (sscanf(line, "%8" SCNx32 "%8" SCNx32 "%*8" SCNx32 "%*8" SCNx32 " %hhx %*s "
301 "%*s 00000000000000000000000000000000 %*s %*s %*s %" SCNx32 " lo",
302 &p.addr.in6.s6_addr32[0], &p.addr.in6.s6_addr32[1], &p.prefix, &rflags) &&
303 p.prefix > 0 && (rflags & RTF_NONEXTHOP) && (rflags & RTF_REJECT)) {
304 // Find source prefixes by scanning through unreachable-routes
305 p.addr.in6.s6_addr32[0] = htonl(p.addr.in6.s6_addr32[0]);
306 p.addr.in6.s6_addr32[1] = htonl(p.addr.in6.s6_addr32[1]);
307
308 for (ssize_t i = 0; i < len; ++i) {
309 if (n[i].prefix <= 64 && n[i].prefix >= p.prefix &&
310 !odhcpd_bmemcmp(&p.addr.in6, &n[i].addr.in6, p.prefix)) {
311 n[i].dprefix = p.prefix;
312 break;
313 }
314 }
315 }
316 }
317
318 return found_default;
319 }
320
321 static int calc_adv_interval(struct interface *iface, uint32_t minvalid,
322 uint32_t *maxival)
323 {
324 uint32_t minival = iface->ra_mininterval;
325 int msecs;
326
327 *maxival = iface->ra_maxinterval;
328
329 if (*maxival > minvalid/3)
330 *maxival = minvalid/3;
331
332 if (*maxival > MaxRtrAdvInterval)
333 *maxival = MaxRtrAdvInterval;
334 else if (*maxival < 4)
335 *maxival = 4;
336
337 if (minival < MinRtrAdvInterval)
338 minival = MinRtrAdvInterval;
339 else if (minival > (*maxival * 3)/4)
340 minival = (*maxival >= 9 ? *maxival/3 : *maxival);
341
342 odhcpd_urandom(&msecs, sizeof(msecs));
343 msecs = (labs(msecs) % ((*maxival != minival) ? (*maxival - minival)*1000 : 500)) +
344 minival*1000;
345
346 return msecs;
347 }
348
349 static uint32_t calc_ra_lifetime(struct interface *iface, uint32_t maxival)
350 {
351 uint32_t lifetime = 3*maxival;
352
353 if (iface->ra_lifetime >= 0) {
354 lifetime = iface->ra_lifetime;
355 if (lifetime < maxival)
356 lifetime = maxival;
357 else if (lifetime > 9000)
358 lifetime = 9000;
359 }
360
361 return lifetime;
362 }
363
364 enum {
365 IOV_RA_ADV=0,
366 IOV_RA_PFXS,
367 IOV_RA_ROUTES,
368 IOV_RA_DNS,
369 IOV_RA_SEARCH,
370 IOV_RA_ADV_INTERVAL,
371 IOV_RA_TOTAL,
372 };
373
374 struct adv_msg {
375 struct nd_router_advert h;
376 struct icmpv6_opt lladdr;
377 struct nd_opt_mtu mtu;
378 };
379
380 struct nd_opt_dns_server {
381 uint8_t type;
382 uint8_t len;
383 uint8_t pad;
384 uint8_t pad2;
385 uint32_t lifetime;
386 struct in6_addr addr[];
387 };
388
389 struct nd_opt_search_list {
390 uint8_t type;
391 uint8_t len;
392 uint8_t pad;
393 uint8_t pad2;
394 uint32_t lifetime;
395 uint8_t name[];
396 };
397
398 struct nd_opt_route_info {
399 uint8_t type;
400 uint8_t len;
401 uint8_t prefix;
402 uint8_t flags;
403 uint32_t lifetime;
404 uint32_t addr[4];
405 };
406
407 /* Router Advert server mode */
408 static int send_router_advert(struct interface *iface, const struct in6_addr *from)
409 {
410 time_t now = odhcpd_time();
411 struct odhcpd_ipaddr *addrs = NULL;
412 struct adv_msg adv;
413 struct nd_opt_prefix_info *pfxs = NULL;
414 struct nd_opt_dns_server *dns = NULL;
415 struct nd_opt_search_list *search = NULL;
416 struct nd_opt_route_info *routes = NULL;
417 struct nd_opt_adv_interval adv_interval;
418 struct iovec iov[IOV_RA_TOTAL];
419 struct sockaddr_in6 dest;
420 size_t dns_sz = 0, search_sz = 0, pfxs_cnt = 0, routes_cnt = 0;
421 ssize_t addr_cnt = 0;
422 uint32_t minvalid = UINT32_MAX, maxival, lifetime;
423 int msecs, mtu = iface->ra_mtu, hlim = iface->ra_hoplimit;
424 bool default_route = false;
425 bool valid_prefix = false;
426 char buf[INET6_ADDRSTRLEN];
427
428 memset(&adv, 0, sizeof(adv));
429 adv.h.nd_ra_type = ND_ROUTER_ADVERT;
430
431 if (hlim == 0)
432 hlim = odhcpd_get_interface_config(iface->ifname, "hop_limit");
433
434 if (hlim > 0)
435 adv.h.nd_ra_curhoplimit = hlim;
436
437 if (iface->dhcpv6) {
438 adv.h.nd_ra_flags_reserved = ND_RA_FLAG_OTHER;
439
440 if (iface->ra_managed >= RA_MANAGED_MFLAG)
441 adv.h.nd_ra_flags_reserved |= ND_RA_FLAG_MANAGED;
442 }
443
444 if (iface->route_preference < 0)
445 adv.h.nd_ra_flags_reserved |= ND_RA_PREF_LOW;
446 else if (iface->route_preference > 0)
447 adv.h.nd_ra_flags_reserved |= ND_RA_PREF_HIGH;
448
449 adv.h.nd_ra_reachable = htonl(iface->ra_reachabletime);
450 adv.h.nd_ra_retransmit = htonl(iface->ra_retranstime);
451
452 adv.lladdr.type = ND_OPT_SOURCE_LINKADDR;
453 adv.lladdr.len = 1;
454 odhcpd_get_mac(iface, adv.lladdr.data);
455
456 adv.mtu.nd_opt_mtu_type = ND_OPT_MTU;
457 adv.mtu.nd_opt_mtu_len = 1;
458
459 if (mtu == 0)
460 mtu = odhcpd_get_interface_config(iface->ifname, "mtu");
461
462 if (mtu < 1280)
463 mtu = 1280;
464
465 adv.mtu.nd_opt_mtu_mtu = htonl(mtu);
466
467 iov[IOV_RA_ADV].iov_base = (char *)&adv;
468 iov[IOV_RA_ADV].iov_len = sizeof(adv);
469
470 /* If not shutdown */
471 if (iface->timer_rs.cb) {
472 size_t size = sizeof(*addrs) * iface->addr6_len;
473
474 addrs = alloca(size);
475 memcpy(addrs, iface->addr6, size);
476
477 addr_cnt = iface->addr6_len;
478
479 /* Check default route */
480 if (iface->default_router) {
481 default_route = true;
482
483 if (iface->default_router > 1)
484 valid_prefix = true;
485 } else if (parse_routes(addrs, addr_cnt))
486 default_route = true;
487 }
488
489 /* Construct Prefix Information options */
490 for (ssize_t i = 0; i < addr_cnt; ++i) {
491 struct odhcpd_ipaddr *addr = &addrs[i];
492 struct nd_opt_prefix_info *p = NULL;
493 uint32_t preferred = 0;
494 uint32_t valid = 0;
495
496 if (addr->prefix > 96 || addr->valid <= (uint32_t)now) {
497 syslog(LOG_INFO, "Address %s (prefix %d, valid %u) not suitable as RA prefix on %s",
498 inet_ntop(AF_INET6, &addr->addr.in6, buf, sizeof(buf)), addr->prefix,
499 addr->valid, iface->name);
500 continue;
501 }
502
503 if (odhcpd_bmemcmp(&addr->addr, &iface->pio_filter_addr,
504 iface->pio_filter_length) != 0 ||
505 addr->prefix < iface->pio_filter_length) {
506 syslog(LOG_INFO, "Address %s filtered out as RA prefix on %s",
507 inet_ntop(AF_INET6, &addr->addr.in6, buf, sizeof(buf)),
508 iface->name);
509 continue; /* PIO filtered out of this RA */
510 }
511
512 for (size_t i = 0; i < pfxs_cnt; ++i) {
513 if (addr->prefix == pfxs[i].nd_opt_pi_prefix_len &&
514 !odhcpd_bmemcmp(&pfxs[i].nd_opt_pi_prefix,
515 &addr->addr.in6, addr->prefix))
516 p = &pfxs[i];
517 }
518
519 if (!p) {
520 struct nd_opt_prefix_info *tmp;
521
522 tmp = realloc(pfxs, sizeof(*pfxs) * (pfxs_cnt + 1));
523 if (!tmp) {
524 syslog(LOG_ERR, "Realloc failed for RA prefix option on %s", iface->name);
525 continue;
526 }
527
528 pfxs = tmp;
529 p = &pfxs[pfxs_cnt++];
530 memset(p, 0, sizeof(*p));
531 }
532
533 if (addr->preferred > (uint32_t)now) {
534 preferred = TIME_LEFT(addr->preferred, now);
535
536 if (iface->ra_useleasetime &&
537 preferred > iface->dhcpv4_leasetime)
538 preferred = iface->dhcpv4_leasetime;
539 }
540
541 valid = TIME_LEFT(addr->valid, now);
542 if (iface->ra_useleasetime && valid > iface->dhcpv4_leasetime)
543 valid = iface->dhcpv4_leasetime;
544
545 if (minvalid > valid)
546 minvalid = valid;
547
548 if (!IN6_IS_ADDR_ULA(&addr->addr.in6) || iface->default_router)
549 valid_prefix = true;
550
551 odhcpd_bmemcpy(&p->nd_opt_pi_prefix, &addr->addr.in6,
552 (iface->ra_advrouter) ? 128 : addr->prefix);
553 p->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
554 p->nd_opt_pi_len = 4;
555 p->nd_opt_pi_prefix_len = (addr->prefix < 64) ? 64 : addr->prefix;
556 p->nd_opt_pi_flags_reserved = 0;
557 if (!iface->ra_not_onlink)
558 p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_ONLINK;
559 if (iface->ra_managed < RA_MANAGED_NO_AFLAG && addr->prefix <= 64)
560 p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_AUTO;
561 if (iface->ra_advrouter)
562 p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_RADDR;
563 p->nd_opt_pi_preferred_time = htonl(preferred);
564 p->nd_opt_pi_valid_time = htonl(valid);
565 }
566
567 iov[IOV_RA_PFXS].iov_base = (char *)pfxs;
568 iov[IOV_RA_PFXS].iov_len = pfxs_cnt * sizeof(*pfxs);
569
570 /* Calculate periodic transmit */
571 msecs = calc_adv_interval(iface, minvalid, &maxival);
572 lifetime = calc_ra_lifetime(iface, maxival);
573
574 if (default_route) {
575 if (!valid_prefix) {
576 syslog(LOG_WARNING, "A default route is present but there is no public prefix "
577 "on %s thus we don't announce a default route!", iface->name);
578 adv.h.nd_ra_router_lifetime = 0;
579 } else
580 adv.h.nd_ra_router_lifetime = htons(lifetime < UINT16_MAX ? lifetime : UINT16_MAX);
581
582 } else
583 adv.h.nd_ra_router_lifetime = 0;
584
585 syslog(LOG_INFO, "Using a RA lifetime of %d seconds on %s", ntohs(adv.h.nd_ra_router_lifetime), iface->name);
586
587 /* DNS options */
588 if (iface->ra_dns) {
589 struct in6_addr dns_pref, *dns_addr = NULL;
590 size_t dns_cnt = 0, search_padded = 0, search_len = iface->search_len;
591 uint8_t *search_domain = iface->search;
592
593 /* DNS Recursive DNS */
594 if (iface->dns_cnt > 0) {
595 dns_addr = iface->dns;
596 dns_cnt = iface->dns_cnt;
597 } else if (!odhcpd_get_interface_dns_addr(iface, &dns_pref)) {
598 dns_addr = &dns_pref;
599 dns_cnt = 1;
600 }
601
602 if (dns_cnt) {
603 dns_sz = sizeof(*dns) + sizeof(struct in6_addr)*dns_cnt;
604
605 dns = alloca(dns_sz);
606 memset(dns, 0, dns_sz);
607 dns->type = ND_OPT_RECURSIVE_DNS;
608 dns->len = 1 + (2 * dns_cnt);
609 dns->lifetime = htonl(lifetime);
610 memcpy(dns->addr, dns_addr, sizeof(struct in6_addr)*dns_cnt);
611 }
612
613 /* DNS Search options */
614 if (!search_domain && !res_init() && _res.dnsrch[0] && _res.dnsrch[0][0]) {
615 uint8_t search_buf[256];
616
617 int len = dn_comp(_res.dnsrch[0], search_buf,
618 sizeof(search_buf), NULL, NULL);
619 if (len > 0) {
620 search_domain = search_buf;
621 search_len = len;
622 }
623 }
624
625 if (search_len > 0)
626 search_padded = ((search_len + 7) & (~7)) + 8;
627
628 search_sz = sizeof(*search) + search_padded;
629
630 search = alloca(search_sz);
631 memset(search, 0, search_sz);
632 search->type = ND_OPT_DNS_SEARCH;
633 search->len = search_len ? ((sizeof(*search) + search_padded) / 8) : 0;
634 search->lifetime = htonl(lifetime);
635
636 if (search_len > 0) {
637 memcpy(search->name, search_domain, search_len);
638 memset(&search->name[search_len], 0, search_padded - search_len);
639 }
640 }
641
642 iov[IOV_RA_DNS].iov_base = (char *)dns;
643 iov[IOV_RA_DNS].iov_len = dns_sz;
644 iov[IOV_RA_SEARCH].iov_base = (char *)search;
645 iov[IOV_RA_SEARCH].iov_len = search_sz;
646
647 for (ssize_t i = 0; i < addr_cnt; ++i) {
648 struct odhcpd_ipaddr *addr = &addrs[i];
649 struct nd_opt_route_info *tmp;
650 uint32_t valid;
651
652 if (addr->dprefix > 64 || addr->dprefix == 0 || addr->valid <= (uint32_t)now) {
653 syslog(LOG_INFO, "Address %s (dprefix %d, valid %u) not suitable as RA route on %s",
654 inet_ntop(AF_INET6, &addr->addr.in6, buf, sizeof(buf)),
655 addr->dprefix, addr->valid, iface->name);
656
657 continue; /* Address not suitable */
658 }
659
660 if (odhcpd_bmemcmp(&addr->addr, &iface->pio_filter_addr,
661 iface->pio_filter_length) != 0 ||
662 addr->prefix < iface->pio_filter_length) {
663 syslog(LOG_INFO, "Address %s filtered out as RA route on %s",
664 inet_ntop(AF_INET6, &addr->addr.in6, buf, sizeof(buf)),
665 iface->name);
666 continue; /* PIO filtered out of this RA */
667 }
668
669 if (addr->dprefix > 32) {
670 addr->addr.in6.s6_addr32[1] &= htonl(~((1U << (64 - addr->dprefix)) - 1));
671 } else if (addr->dprefix <= 32) {
672 addr->addr.in6.s6_addr32[0] &= htonl(~((1U << (32 - addr->dprefix)) - 1));
673 addr->addr.in6.s6_addr32[1] = 0;
674 }
675
676 tmp = realloc(routes, sizeof(*routes) * (routes_cnt + 1));
677 if (!tmp) {
678 syslog(LOG_ERR, "Realloc failed for RA route option on %s", iface->name);
679 continue;
680 }
681
682 routes = tmp;
683
684 memset(&routes[routes_cnt], 0, sizeof(*routes));
685 routes[routes_cnt].type = ND_OPT_ROUTE_INFO;
686 routes[routes_cnt].len = sizeof(*routes) / 8;
687 routes[routes_cnt].prefix = addr->dprefix;
688 routes[routes_cnt].flags = 0;
689 if (iface->route_preference < 0)
690 routes[routes_cnt].flags |= ND_RA_PREF_LOW;
691 else if (iface->route_preference > 0)
692 routes[routes_cnt].flags |= ND_RA_PREF_HIGH;
693
694 valid = TIME_LEFT(addr->valid, now);
695 routes[routes_cnt].lifetime = htonl(valid < lifetime ? valid : lifetime);
696 routes[routes_cnt].addr[0] = addr->addr.in6.s6_addr32[0];
697 routes[routes_cnt].addr[1] = addr->addr.in6.s6_addr32[1];
698 routes[routes_cnt].addr[2] = 0;
699 routes[routes_cnt].addr[3] = 0;
700
701 ++routes_cnt;
702 }
703
704 iov[IOV_RA_ROUTES].iov_base = (char *)routes;
705 iov[IOV_RA_ROUTES].iov_len = routes_cnt * sizeof(*routes);
706
707 memset(&adv_interval, 0, sizeof(adv_interval));
708 adv_interval.nd_opt_adv_interval_type = ND_OPT_RTR_ADV_INTERVAL;
709 adv_interval.nd_opt_adv_interval_len = 1;
710 adv_interval.nd_opt_adv_interval_ival = htonl(maxival);
711
712 iov[IOV_RA_ADV_INTERVAL].iov_base = (char *)&adv_interval;
713 iov[IOV_RA_ADV_INTERVAL].iov_len = adv_interval.nd_opt_adv_interval_len * 8;
714
715 memset(&dest, 0, sizeof(dest));
716 dest.sin6_family = AF_INET6;
717
718 if (from && !IN6_IS_ADDR_UNSPECIFIED(from))
719 dest.sin6_addr = *from;
720 else
721 inet_pton(AF_INET6, ALL_IPV6_NODES, &dest.sin6_addr);
722
723 syslog(LOG_NOTICE, "Sending a RA on %s", iface->name);
724
725 odhcpd_send(iface->router_event.uloop.fd, &dest, iov, ARRAY_SIZE(iov), iface);
726
727 free(pfxs);
728 free(routes);
729
730 return msecs;
731 }
732
733
734 static void trigger_router_advert(struct uloop_timeout *event)
735 {
736 struct interface *iface = container_of(event, struct interface, timer_rs);
737 int msecs = send_router_advert(iface, NULL);
738
739 /* Rearm timer if not shut down */
740 if (event->cb)
741 uloop_timeout_set(event, msecs);
742 }
743
744
745 /* Event handler for incoming ICMPv6 packets */
746 static void handle_icmpv6(void *addr, void *data, size_t len,
747 struct interface *iface, _unused void *dest)
748 {
749 struct icmp6_hdr *hdr = data;
750 struct sockaddr_in6 *from = addr;
751
752 if (!router_icmpv6_valid(addr, data, len))
753 return;
754
755 if ((iface->ra == MODE_SERVER && !iface->master)) { /* Server mode */
756 if (hdr->icmp6_type == ND_ROUTER_SOLICIT)
757 send_router_advert(iface, &from->sin6_addr);
758 } else if (iface->ra == MODE_RELAY) { /* Relay mode */
759 if (hdr->icmp6_type == ND_ROUTER_SOLICIT && !iface->master) {
760 struct interface *c;
761
762 avl_for_each_element(&interfaces, c, avl) {
763 if (!c->master || c->ra != MODE_RELAY)
764 continue;
765
766 forward_router_solicitation(c);
767 }
768 } else if (hdr->icmp6_type == ND_ROUTER_ADVERT && iface->master)
769 forward_router_advertisement(iface, data, len);
770 }
771 }
772
773
774 /* Forward router solicitation */
775 static void forward_router_solicitation(const struct interface *iface)
776 {
777 struct icmp6_hdr rs = {ND_ROUTER_SOLICIT, 0, 0, {{0}}};
778 struct iovec iov = {&rs, sizeof(rs)};
779 struct sockaddr_in6 all_routers;
780
781 if (!iface)
782 return;
783
784 memset(&all_routers, 0, sizeof(all_routers));
785 all_routers.sin6_family = AF_INET6;
786 inet_pton(AF_INET6, ALL_IPV6_ROUTERS, &all_routers.sin6_addr);
787 all_routers.sin6_scope_id = iface->ifindex;
788
789 syslog(LOG_NOTICE, "Sending RS to %s", iface->name);
790 odhcpd_send(iface->router_event.uloop.fd, &all_routers, &iov, 1, iface);
791 }
792
793
794 /* Handler for incoming router solicitations on slave interfaces */
795 static void forward_router_advertisement(const struct interface *iface, uint8_t *data, size_t len)
796 {
797 struct nd_router_advert *adv = (struct nd_router_advert *)data;
798 struct sockaddr_in6 all_nodes;
799 struct icmpv6_opt *opt;
800 struct interface *c;
801 struct iovec iov = { .iov_base = data, .iov_len = len };
802 /* Rewrite options */
803 uint8_t *end = data + len;
804 uint8_t *mac_ptr = NULL;
805 struct in6_addr *dns_ptr = NULL;
806 size_t dns_count = 0;
807
808 icmpv6_for_each_option(opt, &adv[1], end) {
809 if (opt->type == ND_OPT_SOURCE_LINKADDR) {
810 /* Store address of source MAC-address */
811 mac_ptr = opt->data;
812 } else if (opt->type == ND_OPT_RECURSIVE_DNS && opt->len > 1) {
813 /* Check if we have to rewrite DNS */
814 dns_ptr = (struct in6_addr*)&opt->data[6];
815 dns_count = (opt->len - 1) / 2;
816 }
817 }
818
819 syslog(LOG_NOTICE, "Got a RA on %s", iface->name);
820
821 /* Indicate a proxy, however we don't follow the rest of RFC 4389 yet */
822 adv->nd_ra_flags_reserved |= ND_RA_FLAG_PROXY;
823
824 /* Forward advertisement to all slave interfaces */
825 memset(&all_nodes, 0, sizeof(all_nodes));
826 all_nodes.sin6_family = AF_INET6;
827 inet_pton(AF_INET6, ALL_IPV6_NODES, &all_nodes.sin6_addr);
828
829 avl_for_each_element(&interfaces, c, avl) {
830 if (c->ra != MODE_RELAY || c->master)
831 continue;
832
833 /* Fixup source hardware address option */
834 if (mac_ptr)
835 odhcpd_get_mac(c, mac_ptr);
836
837 /* If we have to rewrite DNS entries */
838 if (c->always_rewrite_dns && dns_ptr && dns_count > 0) {
839 const struct in6_addr *rewrite = c->dns;
840 struct in6_addr addr;
841 size_t rewrite_cnt = c->dns_cnt;
842
843 if (rewrite_cnt == 0) {
844 if (odhcpd_get_interface_dns_addr(c, &addr))
845 continue; /* Unable to comply */
846
847 rewrite = &addr;
848 rewrite_cnt = 1;
849 }
850
851 /* Copy over any other addresses */
852 for (size_t i = 0; i < dns_count; ++i) {
853 size_t j = (i < rewrite_cnt) ? i : rewrite_cnt - 1;
854 dns_ptr[i] = rewrite[j];
855 }
856 }
857
858 syslog(LOG_NOTICE, "Forward a RA on %s", c->name);
859
860 odhcpd_send(c->router_event.uloop.fd, &all_nodes, &iov, 1, c);
861 }
862 }