config: remove 'ignore' config option
[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 enable = enable && (iface->ra != MODE_DISABLED);
78
79 if (!fp_route) {
80 ret = -1;
81 goto out;
82 }
83
84
85 if (!enable && iface->router_event.uloop.fd >= 0) {
86 if (!iface->master) {
87 uloop_timeout_cancel(&iface->timer_rs);
88 iface->timer_rs.cb = NULL;
89
90 trigger_router_advert(&iface->timer_rs);
91 }
92
93 uloop_fd_delete(&iface->router_event.uloop);
94 close(iface->router_event.uloop.fd);
95 iface->router_event.uloop.fd = -1;
96 } else if (enable) {
97 struct icmp6_filter filt;
98 struct ipv6_mreq mreq;
99 int val = 2;
100
101 if (iface->router_event.uloop.fd < 0) {
102 /* Open ICMPv6 socket */
103 iface->router_event.uloop.fd = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC,
104 IPPROTO_ICMPV6);
105 if (iface->router_event.uloop.fd < 0) {
106 syslog(LOG_ERR, "socket(AF_INET6): %m");
107 ret = -1;
108 goto out;
109 }
110
111 if (setsockopt(iface->router_event.uloop.fd, SOL_SOCKET, SO_BINDTODEVICE,
112 iface->ifname, strlen(iface->ifname)) < 0) {
113 syslog(LOG_ERR, "setsockopt(SO_BINDTODEVICE): %m");
114 ret = -1;
115 goto out;
116 }
117
118 /* Let the kernel compute our checksums */
119 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_RAW, IPV6_CHECKSUM,
120 &val, sizeof(val)) < 0) {
121 syslog(LOG_ERR, "setsockopt(IPV6_CHECKSUM): %m");
122 ret = -1;
123 goto out;
124 }
125
126 /* This is required by RFC 4861 */
127 val = 255;
128 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
129 &val, sizeof(val)) < 0) {
130 syslog(LOG_ERR, "setsockopt(IPV6_MULTICAST_HOPS): %m");
131 ret = -1;
132 goto out;
133 }
134
135 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
136 &val, sizeof(val)) < 0) {
137 syslog(LOG_ERR, "setsockopt(IPV6_UNICAST_HOPS): %m");
138 ret = -1;
139 goto out;
140 }
141
142 /* We need to know the source interface */
143 val = 1;
144 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
145 &val, sizeof(val)) < 0) {
146 syslog(LOG_ERR, "setsockopt(IPV6_RECVPKTINFO): %m");
147 ret = -1;
148 goto out;
149 }
150
151 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
152 &val, sizeof(val)) < 0) {
153 syslog(LOG_ERR, "setsockopt(IPV6_RECVHOPLIMIT): %m");
154 ret = -1;
155 goto out;
156 }
157
158 /* Don't loop back */
159 val = 0;
160 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
161 &val, sizeof(val)) < 0) {
162 syslog(LOG_ERR, "setsockopt(IPV6_MULTICAST_LOOP): %m");
163 ret = -1;
164 goto out;
165 }
166
167 /* Filter ICMPv6 package types */
168 ICMP6_FILTER_SETBLOCKALL(&filt);
169 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
170 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filt);
171 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_ICMPV6, ICMP6_FILTER,
172 &filt, sizeof(filt)) < 0) {
173 syslog(LOG_ERR, "setsockopt(ICMP6_FILTER): %m");
174 ret = -1;
175 goto out;
176 }
177
178 iface->router_event.handle_dgram = handle_icmpv6;
179 odhcpd_register(&iface->router_event);
180 } else {
181 uloop_timeout_cancel(&iface->timer_rs);
182 iface->timer_rs.cb = NULL;
183
184 memset(&mreq, 0, sizeof(mreq));
185 mreq.ipv6mr_interface = iface->ifindex;
186 inet_pton(AF_INET6, ALL_IPV6_NODES, &mreq.ipv6mr_multiaddr);
187 setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP,
188 &mreq, sizeof(mreq));
189
190 inet_pton(AF_INET6, ALL_IPV6_ROUTERS, &mreq.ipv6mr_multiaddr);
191 setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP,
192 &mreq, sizeof(mreq));
193 }
194
195 memset(&mreq, 0, sizeof(mreq));
196 mreq.ipv6mr_interface = iface->ifindex;
197 inet_pton(AF_INET6, ALL_IPV6_ROUTERS, &mreq.ipv6mr_multiaddr);
198
199 if (iface->ra == MODE_RELAY && iface->master) {
200 inet_pton(AF_INET6, ALL_IPV6_NODES, &mreq.ipv6mr_multiaddr);
201 forward_router_solicitation(iface);
202 } else if (iface->ra == MODE_SERVER) {
203 iface->timer_rs.cb = trigger_router_advert;
204 uloop_timeout_set(&iface->timer_rs, 1000);
205 }
206
207 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6,
208 IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
209 ret = -1;
210 syslog(LOG_ERR, "setsockopt(IPV6_ADD_MEMBERSHIP): %m");
211 goto out;
212 }
213 }
214 out:
215 if (ret < 0 && iface->router_event.uloop.fd >= 0) {
216 close(iface->router_event.uloop.fd);
217 iface->router_event.uloop.fd = -1;
218 }
219
220 return ret;
221 }
222
223
224 static void router_netevent_cb(unsigned long event, struct netevent_handler_info *info)
225 {
226 struct interface *iface;
227
228 switch (event) {
229 case NETEV_ROUTE6_ADD:
230 case NETEV_ROUTE6_DEL:
231 if (info->rt.dst_len)
232 break;
233
234 avl_for_each_element(&interfaces, iface, avl) {
235 if (iface->ra == MODE_SERVER && !iface->master)
236 uloop_timeout_set(&iface->timer_rs, 1000);
237 }
238 break;
239 case NETEV_ADDR6LIST_CHANGE:
240 iface = info->iface;
241 if (iface && iface->ra == MODE_SERVER && !iface->master)
242 uloop_timeout_set(&iface->timer_rs, 1000);
243 break;
244 default:
245 break;
246 }
247 }
248
249
250 static bool router_icmpv6_valid(struct sockaddr_in6 *source, uint8_t *data, size_t len)
251 {
252 struct icmp6_hdr *hdr = (struct icmp6_hdr *)data;
253 struct icmpv6_opt *opt, *end = (struct icmpv6_opt*)&data[len];
254
255 /* Hoplimit is already checked in odhcpd_receive_packets */
256 if (len < sizeof(*hdr) || hdr->icmp6_code)
257 return false;
258
259 switch (hdr->icmp6_type) {
260 case ND_ROUTER_ADVERT:
261 if (!IN6_IS_ADDR_LINKLOCAL(&source->sin6_addr))
262 return false;
263
264 opt = (struct icmpv6_opt *)((struct nd_router_advert *)data + 1);
265 break;
266
267 case ND_ROUTER_SOLICIT:
268 opt = (struct icmpv6_opt *)((struct nd_router_solicit *)data + 1);
269 break;
270
271 default:
272 return false;
273 }
274
275 icmpv6_for_each_option(opt, opt, end)
276 if (opt->type == ND_OPT_SOURCE_LINKADDR &&
277 IN6_IS_ADDR_UNSPECIFIED(&source->sin6_addr) &&
278 hdr->icmp6_type == ND_ROUTER_SOLICIT)
279 return false;
280
281 /* Check all options parsed successfully */
282 return opt == end;
283 }
284
285
286 /* Detect whether a default route exists, also find the source prefixes */
287 static bool parse_routes(struct odhcpd_ipaddr *n, ssize_t len)
288 {
289 struct odhcpd_ipaddr p = { .addr.in6 = IN6ADDR_ANY_INIT, .prefix = 0,
290 .dprefix = 0, .preferred = 0, .valid = 0};
291 bool found_default = false;
292 char line[512], ifname[16];
293
294 rewind(fp_route);
295
296 while (fgets(line, sizeof(line), fp_route)) {
297 uint32_t rflags;
298 if (sscanf(line, "00000000000000000000000000000000 00 "
299 "%*s %*s %*s %*s %*s %*s %*s %15s", ifname) &&
300 strcmp(ifname, "lo")) {
301 found_default = true;
302 } else if (sscanf(line, "%8" SCNx32 "%8" SCNx32 "%*8" SCNx32 "%*8" SCNx32 " %hhx %*s "
303 "%*s 00000000000000000000000000000000 %*s %*s %*s %" SCNx32 " lo",
304 &p.addr.in6.s6_addr32[0], &p.addr.in6.s6_addr32[1], &p.prefix, &rflags) &&
305 p.prefix > 0 && (rflags & RTF_NONEXTHOP) && (rflags & RTF_REJECT)) {
306 // Find source prefixes by scanning through unreachable-routes
307 p.addr.in6.s6_addr32[0] = htonl(p.addr.in6.s6_addr32[0]);
308 p.addr.in6.s6_addr32[1] = htonl(p.addr.in6.s6_addr32[1]);
309
310 for (ssize_t i = 0; i < len; ++i) {
311 if (n[i].prefix <= 64 && n[i].prefix >= p.prefix &&
312 !odhcpd_bmemcmp(&p.addr.in6, &n[i].addr.in6, p.prefix)) {
313 n[i].dprefix = p.prefix;
314 break;
315 }
316 }
317 }
318 }
319
320 return found_default;
321 }
322
323 static int calc_adv_interval(struct interface *iface, uint32_t minvalid,
324 uint32_t *maxival)
325 {
326 uint32_t minival = iface->ra_mininterval;
327 int msecs;
328
329 *maxival = iface->ra_maxinterval;
330
331 if (*maxival > minvalid/3)
332 *maxival = minvalid/3;
333
334 if (*maxival > MaxRtrAdvInterval)
335 *maxival = MaxRtrAdvInterval;
336 else if (*maxival < 4)
337 *maxival = 4;
338
339 if (minival < MinRtrAdvInterval)
340 minival = MinRtrAdvInterval;
341 else if (minival > (*maxival * 3)/4)
342 minival = (*maxival >= 9 ? *maxival/3 : *maxival);
343
344 odhcpd_urandom(&msecs, sizeof(msecs));
345 msecs = (labs(msecs) % ((*maxival != minival) ? (*maxival - minival)*1000 : 500)) +
346 minival*1000;
347
348 return msecs;
349 }
350
351 static uint32_t calc_ra_lifetime(struct interface *iface, uint32_t maxival)
352 {
353 uint32_t lifetime = 3*maxival;
354
355 if (iface->ra_lifetime >= 0) {
356 lifetime = iface->ra_lifetime;
357 if (lifetime < maxival)
358 lifetime = maxival;
359 else if (lifetime > 9000)
360 lifetime = 9000;
361 }
362
363 return lifetime;
364 }
365
366 enum {
367 IOV_RA_ADV=0,
368 IOV_RA_PFXS,
369 IOV_RA_ROUTES,
370 IOV_RA_DNS,
371 IOV_RA_SEARCH,
372 IOV_RA_ADV_INTERVAL,
373 IOV_RA_TOTAL,
374 };
375
376 struct adv_msg {
377 struct nd_router_advert h;
378 struct icmpv6_opt lladdr;
379 struct nd_opt_mtu mtu;
380 };
381
382 struct nd_opt_dns_server {
383 uint8_t type;
384 uint8_t len;
385 uint8_t pad;
386 uint8_t pad2;
387 uint32_t lifetime;
388 struct in6_addr addr[];
389 };
390
391 struct nd_opt_search_list {
392 uint8_t type;
393 uint8_t len;
394 uint8_t pad;
395 uint8_t pad2;
396 uint32_t lifetime;
397 uint8_t name[];
398 };
399
400 struct nd_opt_route_info {
401 uint8_t type;
402 uint8_t len;
403 uint8_t prefix;
404 uint8_t flags;
405 uint32_t lifetime;
406 uint32_t addr[4];
407 };
408
409 /* Router Advert server mode */
410 static int send_router_advert(struct interface *iface, const struct in6_addr *from)
411 {
412 time_t now = odhcpd_time();
413 struct odhcpd_ipaddr *addrs = NULL;
414 struct adv_msg adv;
415 struct nd_opt_prefix_info *pfxs = NULL;
416 struct nd_opt_dns_server *dns = NULL;
417 struct nd_opt_search_list *search = NULL;
418 struct nd_opt_route_info *routes = NULL;
419 struct nd_opt_adv_interval adv_interval;
420 struct iovec iov[IOV_RA_TOTAL];
421 struct sockaddr_in6 dest;
422 size_t dns_sz = 0, search_sz = 0, pfxs_cnt = 0, routes_cnt = 0;
423 ssize_t addr_cnt = 0;
424 uint32_t minvalid = UINT32_MAX, maxival, lifetime;
425 int msecs, mtu = iface->ra_mtu, hlim = iface->ra_hoplimit;
426 bool default_route = false;
427 bool valid_prefix = false;
428 char buf[INET6_ADDRSTRLEN];
429
430 memset(&adv, 0, sizeof(adv));
431 adv.h.nd_ra_type = ND_ROUTER_ADVERT;
432
433 if (hlim == 0)
434 hlim = odhcpd_get_interface_config(iface->ifname, "hop_limit");
435
436 if (hlim > 0)
437 adv.h.nd_ra_curhoplimit = hlim;
438
439 if (iface->dhcpv6 != MODE_DISABLED) {
440 adv.h.nd_ra_flags_reserved = ND_RA_FLAG_OTHER;
441
442 if (iface->ra_managed >= RA_MANAGED_MFLAG)
443 adv.h.nd_ra_flags_reserved |= ND_RA_FLAG_MANAGED;
444 }
445
446 if (iface->route_preference < 0)
447 adv.h.nd_ra_flags_reserved |= ND_RA_PREF_LOW;
448 else if (iface->route_preference > 0)
449 adv.h.nd_ra_flags_reserved |= ND_RA_PREF_HIGH;
450
451 adv.h.nd_ra_reachable = htonl(iface->ra_reachabletime);
452 adv.h.nd_ra_retransmit = htonl(iface->ra_retranstime);
453
454 adv.lladdr.type = ND_OPT_SOURCE_LINKADDR;
455 adv.lladdr.len = 1;
456 odhcpd_get_mac(iface, adv.lladdr.data);
457
458 adv.mtu.nd_opt_mtu_type = ND_OPT_MTU;
459 adv.mtu.nd_opt_mtu_len = 1;
460
461 if (mtu == 0)
462 mtu = odhcpd_get_interface_config(iface->ifname, "mtu");
463
464 if (mtu < 1280)
465 mtu = 1280;
466
467 adv.mtu.nd_opt_mtu_mtu = htonl(mtu);
468
469 iov[IOV_RA_ADV].iov_base = (char *)&adv;
470 iov[IOV_RA_ADV].iov_len = sizeof(adv);
471
472 /* If not shutdown */
473 if (iface->timer_rs.cb) {
474 size_t size = sizeof(*addrs) * iface->addr6_len;
475
476 addrs = alloca(size);
477 memcpy(addrs, iface->addr6, size);
478
479 addr_cnt = iface->addr6_len;
480
481 /* Check default route */
482 if (iface->default_router) {
483 default_route = true;
484
485 if (iface->default_router > 1)
486 valid_prefix = true;
487 } else if (parse_routes(addrs, addr_cnt))
488 default_route = true;
489 }
490
491 /* Construct Prefix Information options */
492 for (ssize_t i = 0; i < addr_cnt; ++i) {
493 struct odhcpd_ipaddr *addr = &addrs[i];
494 struct nd_opt_prefix_info *p = NULL;
495 uint32_t preferred = 0;
496 uint32_t valid = 0;
497
498 if (addr->prefix > 96 || addr->valid <= (uint32_t)now) {
499 syslog(LOG_INFO, "Address %s (prefix %d, valid %u) not suitable as RA prefix on %s",
500 inet_ntop(AF_INET6, &addr->addr.in6, buf, sizeof(buf)), addr->prefix,
501 addr->valid, iface->name);
502 continue;
503 }
504
505 if (odhcpd_bmemcmp(&addr->addr, &iface->pio_filter_addr,
506 iface->pio_filter_length) != 0 ||
507 addr->prefix < iface->pio_filter_length) {
508 syslog(LOG_INFO, "Address %s filtered out as RA prefix on %s",
509 inet_ntop(AF_INET6, &addr->addr.in6, buf, sizeof(buf)),
510 iface->name);
511 continue; /* PIO filtered out of this RA */
512 }
513
514 for (size_t i = 0; i < pfxs_cnt; ++i) {
515 if (addr->prefix == pfxs[i].nd_opt_pi_prefix_len &&
516 !odhcpd_bmemcmp(&pfxs[i].nd_opt_pi_prefix,
517 &addr->addr.in6, addr->prefix))
518 p = &pfxs[i];
519 }
520
521 if (!p) {
522 struct nd_opt_prefix_info *tmp;
523
524 tmp = realloc(pfxs, sizeof(*pfxs) * (pfxs_cnt + 1));
525 if (!tmp) {
526 syslog(LOG_ERR, "Realloc failed for RA prefix option on %s", iface->name);
527 continue;
528 }
529
530 pfxs = tmp;
531 p = &pfxs[pfxs_cnt++];
532 memset(p, 0, sizeof(*p));
533 }
534
535 if (addr->preferred > (uint32_t)now) {
536 preferred = TIME_LEFT(addr->preferred, now);
537
538 if (iface->ra_useleasetime &&
539 preferred > iface->dhcpv4_leasetime)
540 preferred = iface->dhcpv4_leasetime;
541 }
542
543 valid = TIME_LEFT(addr->valid, now);
544 if (iface->ra_useleasetime && valid > iface->dhcpv4_leasetime)
545 valid = iface->dhcpv4_leasetime;
546
547 if (minvalid > valid)
548 minvalid = valid;
549
550 if (!IN6_IS_ADDR_ULA(&addr->addr.in6) || iface->default_router)
551 valid_prefix = true;
552
553 odhcpd_bmemcpy(&p->nd_opt_pi_prefix, &addr->addr.in6,
554 (iface->ra_advrouter) ? 128 : addr->prefix);
555 p->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
556 p->nd_opt_pi_len = 4;
557 p->nd_opt_pi_prefix_len = (addr->prefix < 64) ? 64 : addr->prefix;
558 p->nd_opt_pi_flags_reserved = 0;
559 if (!iface->ra_not_onlink)
560 p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_ONLINK;
561 if (iface->ra_managed < RA_MANAGED_NO_AFLAG && addr->prefix <= 64)
562 p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_AUTO;
563 if (iface->ra_advrouter)
564 p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_RADDR;
565 p->nd_opt_pi_preferred_time = htonl(preferred);
566 p->nd_opt_pi_valid_time = htonl(valid);
567 }
568
569 iov[IOV_RA_PFXS].iov_base = (char *)pfxs;
570 iov[IOV_RA_PFXS].iov_len = pfxs_cnt * sizeof(*pfxs);
571
572 /* Calculate periodic transmit */
573 msecs = calc_adv_interval(iface, minvalid, &maxival);
574 lifetime = calc_ra_lifetime(iface, maxival);
575
576 if (default_route) {
577 if (!valid_prefix) {
578 syslog(LOG_WARNING, "A default route is present but there is no public prefix "
579 "on %s thus we don't announce a default route!", iface->name);
580 adv.h.nd_ra_router_lifetime = 0;
581 } else
582 adv.h.nd_ra_router_lifetime = htons(lifetime < UINT16_MAX ? lifetime : UINT16_MAX);
583
584 } else
585 adv.h.nd_ra_router_lifetime = 0;
586
587 syslog(LOG_INFO, "Using a RA lifetime of %d seconds on %s", ntohs(adv.h.nd_ra_router_lifetime), iface->name);
588
589 /* DNS options */
590 if (iface->ra_dns) {
591 struct in6_addr dns_pref, *dns_addr = NULL;
592 size_t dns_cnt = 0, search_len = iface->search_len;
593 uint8_t *search_domain = iface->search;
594
595 /* DNS Recursive DNS */
596 if (iface->dns_cnt > 0) {
597 dns_addr = iface->dns;
598 dns_cnt = iface->dns_cnt;
599 } else if (!odhcpd_get_interface_dns_addr(iface, &dns_pref)) {
600 dns_addr = &dns_pref;
601 dns_cnt = 1;
602 }
603
604 if (dns_cnt) {
605 dns_sz = sizeof(*dns) + sizeof(struct in6_addr)*dns_cnt;
606
607 dns = alloca(dns_sz);
608 memset(dns, 0, dns_sz);
609 dns->type = ND_OPT_RECURSIVE_DNS;
610 dns->len = 1 + (2 * dns_cnt);
611 dns->lifetime = htonl(lifetime);
612 memcpy(dns->addr, dns_addr, sizeof(struct in6_addr)*dns_cnt);
613 }
614
615 /* DNS Search options */
616 if (!search_domain && !res_init() && _res.dnsrch[0] && _res.dnsrch[0][0]) {
617 uint8_t search_buf[256];
618
619 int len = dn_comp(_res.dnsrch[0], search_buf,
620 sizeof(search_buf), NULL, NULL);
621 if (len > 0) {
622 search_domain = search_buf;
623 search_len = len;
624 }
625 }
626
627 if (search_len > 0) {
628 size_t search_padded = ((search_len + 7) & (~7)) + 8;
629
630 search_sz = sizeof(*search) + search_padded;
631
632 search = alloca(search_sz);
633 memset(search, 0, search_sz);
634 search->type = ND_OPT_DNS_SEARCH;
635 search->len = search_len ? ((sizeof(*search) + search_padded) / 8) : 0;
636 search->lifetime = htonl(lifetime);
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 }