34d43b0af6bc95f51c5f8bbc4e198a0785c0a9d0
[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 if (iface->router_event.uloop.registered)
217 uloop_fd_delete(&iface->router_event.uloop);
218
219 close(iface->router_event.uloop.fd);
220 iface->router_event.uloop.fd = -1;
221 }
222
223 return ret;
224 }
225
226
227 static void router_netevent_cb(unsigned long event, struct netevent_handler_info *info)
228 {
229 struct interface *iface;
230
231 switch (event) {
232 case NETEV_IFINDEX_CHANGE:
233 iface = info->iface;
234 if (iface && iface->router_event.uloop.fd >= 0) {
235 if (iface->router_event.uloop.registered)
236 uloop_fd_delete(&iface->router_event.uloop);
237
238 close(iface->router_event.uloop.fd);
239 iface->router_event.uloop.fd = -1;
240 }
241 break;
242 case NETEV_ROUTE6_ADD:
243 case NETEV_ROUTE6_DEL:
244 if (info->rt.dst_len)
245 break;
246
247 avl_for_each_element(&interfaces, iface, avl) {
248 if (iface->ra == MODE_SERVER && !iface->master)
249 uloop_timeout_set(&iface->timer_rs, 1000);
250 }
251 break;
252 case NETEV_ADDR6LIST_CHANGE:
253 iface = info->iface;
254 if (iface && iface->ra == MODE_SERVER && !iface->master)
255 uloop_timeout_set(&iface->timer_rs, 1000);
256 break;
257 default:
258 break;
259 }
260 }
261
262
263 static bool router_icmpv6_valid(struct sockaddr_in6 *source, uint8_t *data, size_t len)
264 {
265 struct icmp6_hdr *hdr = (struct icmp6_hdr *)data;
266 struct icmpv6_opt *opt, *end = (struct icmpv6_opt*)&data[len];
267
268 /* Hoplimit is already checked in odhcpd_receive_packets */
269 if (len < sizeof(*hdr) || hdr->icmp6_code)
270 return false;
271
272 switch (hdr->icmp6_type) {
273 case ND_ROUTER_ADVERT:
274 if (!IN6_IS_ADDR_LINKLOCAL(&source->sin6_addr))
275 return false;
276
277 opt = (struct icmpv6_opt *)((struct nd_router_advert *)data + 1);
278 break;
279
280 case ND_ROUTER_SOLICIT:
281 opt = (struct icmpv6_opt *)((struct nd_router_solicit *)data + 1);
282 break;
283
284 default:
285 return false;
286 }
287
288 icmpv6_for_each_option(opt, opt, end)
289 if (opt->type == ND_OPT_SOURCE_LINKADDR &&
290 IN6_IS_ADDR_UNSPECIFIED(&source->sin6_addr) &&
291 hdr->icmp6_type == ND_ROUTER_SOLICIT)
292 return false;
293
294 /* Check all options parsed successfully */
295 return opt == end;
296 }
297
298
299 /* Detect whether a default route exists, also find the source prefixes */
300 static bool parse_routes(struct odhcpd_ipaddr *n, ssize_t len)
301 {
302 struct odhcpd_ipaddr p = { .addr.in6 = IN6ADDR_ANY_INIT, .prefix = 0,
303 .dprefix = 0, .preferred = 0, .valid = 0};
304 bool found_default = false;
305 char line[512], ifname[16];
306
307 rewind(fp_route);
308
309 while (fgets(line, sizeof(line), fp_route)) {
310 uint32_t rflags;
311 if (sscanf(line, "00000000000000000000000000000000 00 "
312 "%*s %*s %*s %*s %*s %*s %*s %15s", ifname) &&
313 strcmp(ifname, "lo")) {
314 found_default = true;
315 } else if (sscanf(line, "%8" SCNx32 "%8" SCNx32 "%*8" SCNx32 "%*8" SCNx32 " %hhx %*s "
316 "%*s 00000000000000000000000000000000 %*s %*s %*s %" SCNx32 " lo",
317 &p.addr.in6.s6_addr32[0], &p.addr.in6.s6_addr32[1], &p.prefix, &rflags) &&
318 p.prefix > 0 && (rflags & RTF_NONEXTHOP) && (rflags & RTF_REJECT)) {
319 // Find source prefixes by scanning through unreachable-routes
320 p.addr.in6.s6_addr32[0] = htonl(p.addr.in6.s6_addr32[0]);
321 p.addr.in6.s6_addr32[1] = htonl(p.addr.in6.s6_addr32[1]);
322
323 for (ssize_t i = 0; i < len; ++i) {
324 if (n[i].prefix <= 64 && n[i].prefix >= p.prefix &&
325 !odhcpd_bmemcmp(&p.addr.in6, &n[i].addr.in6, p.prefix)) {
326 n[i].dprefix = p.prefix;
327 break;
328 }
329 }
330 }
331 }
332
333 return found_default;
334 }
335
336 static int calc_adv_interval(struct interface *iface, uint32_t minvalid,
337 uint32_t *maxival)
338 {
339 uint32_t minival = iface->ra_mininterval;
340 int msecs;
341
342 *maxival = iface->ra_maxinterval;
343
344 if (*maxival > minvalid/3)
345 *maxival = minvalid/3;
346
347 if (*maxival > MaxRtrAdvInterval)
348 *maxival = MaxRtrAdvInterval;
349 else if (*maxival < 4)
350 *maxival = 4;
351
352 if (minival < MinRtrAdvInterval)
353 minival = MinRtrAdvInterval;
354 else if (minival > (*maxival * 3)/4)
355 minival = (*maxival >= 9 ? *maxival/3 : *maxival);
356
357 odhcpd_urandom(&msecs, sizeof(msecs));
358 msecs = (labs(msecs) % ((*maxival != minival) ? (*maxival - minival)*1000 : 500)) +
359 minival*1000;
360
361 return msecs;
362 }
363
364 static uint32_t calc_ra_lifetime(struct interface *iface, uint32_t maxival)
365 {
366 uint32_t lifetime = 3*maxival;
367
368 if (iface->ra_lifetime >= 0) {
369 lifetime = iface->ra_lifetime;
370 if (lifetime > 0 && lifetime < maxival)
371 lifetime = maxival;
372 else if (lifetime > 9000)
373 lifetime = 9000;
374 }
375
376 return lifetime;
377 }
378
379 enum {
380 IOV_RA_ADV=0,
381 IOV_RA_PFXS,
382 IOV_RA_ROUTES,
383 IOV_RA_DNS,
384 IOV_RA_SEARCH,
385 IOV_RA_ADV_INTERVAL,
386 IOV_RA_TOTAL,
387 };
388
389 struct adv_msg {
390 struct nd_router_advert h;
391 struct icmpv6_opt lladdr;
392 struct nd_opt_mtu mtu;
393 };
394
395 struct nd_opt_dns_server {
396 uint8_t type;
397 uint8_t len;
398 uint8_t pad;
399 uint8_t pad2;
400 uint32_t lifetime;
401 struct in6_addr addr[];
402 };
403
404 struct nd_opt_search_list {
405 uint8_t type;
406 uint8_t len;
407 uint8_t pad;
408 uint8_t pad2;
409 uint32_t lifetime;
410 uint8_t name[];
411 };
412
413 struct nd_opt_route_info {
414 uint8_t type;
415 uint8_t len;
416 uint8_t prefix;
417 uint8_t flags;
418 uint32_t lifetime;
419 uint32_t addr[4];
420 };
421
422 /* Router Advert server mode */
423 static int send_router_advert(struct interface *iface, const struct in6_addr *from)
424 {
425 time_t now = odhcpd_time();
426 struct odhcpd_ipaddr *addrs = NULL;
427 struct adv_msg adv;
428 struct nd_opt_prefix_info *pfxs = NULL;
429 struct nd_opt_dns_server *dns = NULL;
430 struct nd_opt_search_list *search = NULL;
431 struct nd_opt_route_info *routes = NULL;
432 struct nd_opt_adv_interval adv_interval;
433 struct iovec iov[IOV_RA_TOTAL];
434 struct sockaddr_in6 dest;
435 size_t dns_sz = 0, search_sz = 0, pfxs_cnt = 0, routes_cnt = 0;
436 ssize_t addr_cnt = 0;
437 uint32_t minvalid = UINT32_MAX, maxival, lifetime;
438 int msecs, mtu = iface->ra_mtu, hlim = iface->ra_hoplimit;
439 bool default_route = false;
440 bool valid_prefix = false;
441 char buf[INET6_ADDRSTRLEN];
442
443 memset(&adv, 0, sizeof(adv));
444 adv.h.nd_ra_type = ND_ROUTER_ADVERT;
445
446 if (hlim == 0)
447 hlim = odhcpd_get_interface_config(iface->ifname, "hop_limit");
448
449 if (hlim > 0)
450 adv.h.nd_ra_curhoplimit = hlim;
451
452 adv.h.nd_ra_flags_reserved = iface->ra_flags;
453
454 if (iface->route_preference < 0)
455 adv.h.nd_ra_flags_reserved |= ND_RA_PREF_LOW;
456 else if (iface->route_preference > 0)
457 adv.h.nd_ra_flags_reserved |= ND_RA_PREF_HIGH;
458
459 adv.h.nd_ra_reachable = htonl(iface->ra_reachabletime);
460 adv.h.nd_ra_retransmit = htonl(iface->ra_retranstime);
461
462 adv.lladdr.type = ND_OPT_SOURCE_LINKADDR;
463 adv.lladdr.len = 1;
464 odhcpd_get_mac(iface, adv.lladdr.data);
465
466 adv.mtu.nd_opt_mtu_type = ND_OPT_MTU;
467 adv.mtu.nd_opt_mtu_len = 1;
468
469 if (mtu == 0)
470 mtu = odhcpd_get_interface_config(iface->ifname, "mtu");
471
472 if (mtu < 1280)
473 mtu = 1280;
474
475 adv.mtu.nd_opt_mtu_mtu = htonl(mtu);
476
477 iov[IOV_RA_ADV].iov_base = (char *)&adv;
478 iov[IOV_RA_ADV].iov_len = sizeof(adv);
479
480 /* If not shutdown */
481 if (iface->timer_rs.cb) {
482 size_t size = sizeof(*addrs) * iface->addr6_len;
483
484 addrs = alloca(size);
485 memcpy(addrs, iface->addr6, size);
486
487 addr_cnt = iface->addr6_len;
488
489 /* Check default route */
490 if (iface->default_router) {
491 default_route = true;
492
493 if (iface->default_router > 1)
494 valid_prefix = true;
495 } else if (parse_routes(addrs, addr_cnt))
496 default_route = true;
497 }
498
499 /* Construct Prefix Information options */
500 for (ssize_t i = 0; i < addr_cnt; ++i) {
501 struct odhcpd_ipaddr *addr = &addrs[i];
502 struct nd_opt_prefix_info *p = NULL;
503 uint32_t preferred = 0;
504 uint32_t valid = 0;
505
506 if (addr->prefix > 96 || addr->valid <= (uint32_t)now) {
507 syslog(LOG_INFO, "Address %s (prefix %d, valid %u) not suitable as RA prefix on %s",
508 inet_ntop(AF_INET6, &addr->addr.in6, buf, sizeof(buf)), addr->prefix,
509 addr->valid, iface->name);
510 continue;
511 }
512
513 if (odhcpd_bmemcmp(&addr->addr, &iface->pio_filter_addr,
514 iface->pio_filter_length) != 0 ||
515 addr->prefix < iface->pio_filter_length) {
516 syslog(LOG_INFO, "Address %s filtered out as RA prefix on %s",
517 inet_ntop(AF_INET6, &addr->addr.in6, buf, sizeof(buf)),
518 iface->name);
519 continue; /* PIO filtered out of this RA */
520 }
521
522 for (size_t i = 0; i < pfxs_cnt; ++i) {
523 if (addr->prefix == pfxs[i].nd_opt_pi_prefix_len &&
524 !odhcpd_bmemcmp(&pfxs[i].nd_opt_pi_prefix,
525 &addr->addr.in6, addr->prefix))
526 p = &pfxs[i];
527 }
528
529 if (!p) {
530 struct nd_opt_prefix_info *tmp;
531
532 tmp = realloc(pfxs, sizeof(*pfxs) * (pfxs_cnt + 1));
533 if (!tmp) {
534 syslog(LOG_ERR, "Realloc failed for RA prefix option on %s", iface->name);
535 continue;
536 }
537
538 pfxs = tmp;
539 p = &pfxs[pfxs_cnt++];
540 memset(p, 0, sizeof(*p));
541 }
542
543 if (addr->preferred > (uint32_t)now) {
544 preferred = TIME_LEFT(addr->preferred, now);
545
546 if (iface->ra_useleasetime &&
547 preferred > iface->dhcpv4_leasetime)
548 preferred = iface->dhcpv4_leasetime;
549 }
550
551 valid = TIME_LEFT(addr->valid, now);
552 if (iface->ra_useleasetime && valid > iface->dhcpv4_leasetime)
553 valid = iface->dhcpv4_leasetime;
554
555 if (minvalid > valid)
556 minvalid = valid;
557
558 if (!IN6_IS_ADDR_ULA(&addr->addr.in6) || iface->default_router)
559 valid_prefix = true;
560
561 odhcpd_bmemcpy(&p->nd_opt_pi_prefix, &addr->addr.in6,
562 (iface->ra_advrouter) ? 128 : addr->prefix);
563 p->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
564 p->nd_opt_pi_len = 4;
565 p->nd_opt_pi_prefix_len = (addr->prefix < 64) ? 64 : addr->prefix;
566 p->nd_opt_pi_flags_reserved = 0;
567 if (!iface->ra_not_onlink)
568 p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_ONLINK;
569 if (iface->ra_slaac && addr->prefix <= 64)
570 p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_AUTO;
571 if (iface->ra_advrouter)
572 p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_RADDR;
573 p->nd_opt_pi_preferred_time = htonl(preferred);
574 p->nd_opt_pi_valid_time = htonl(valid);
575 }
576
577 iov[IOV_RA_PFXS].iov_base = (char *)pfxs;
578 iov[IOV_RA_PFXS].iov_len = pfxs_cnt * sizeof(*pfxs);
579
580 /* Calculate periodic transmit */
581 msecs = calc_adv_interval(iface, minvalid, &maxival);
582 lifetime = calc_ra_lifetime(iface, maxival);
583
584 if (default_route) {
585 if (!valid_prefix) {
586 syslog(LOG_WARNING, "A default route is present but there is no public prefix "
587 "on %s thus we don't announce a default route!", iface->name);
588 adv.h.nd_ra_router_lifetime = 0;
589 } else
590 adv.h.nd_ra_router_lifetime = htons(lifetime < UINT16_MAX ? lifetime : UINT16_MAX);
591
592 } else
593 adv.h.nd_ra_router_lifetime = 0;
594
595 syslog(LOG_INFO, "Using a RA lifetime of %d seconds on %s", ntohs(adv.h.nd_ra_router_lifetime), iface->name);
596
597 /* DNS options */
598 if (iface->ra_dns) {
599 struct in6_addr dns_pref, *dns_addr = NULL;
600 size_t dns_cnt = 0, search_len = iface->search_len;
601 uint8_t *search_domain = iface->search;
602
603 /* DNS Recursive DNS */
604 if (iface->dns_cnt > 0) {
605 dns_addr = iface->dns;
606 dns_cnt = iface->dns_cnt;
607 } else if (!odhcpd_get_interface_dns_addr(iface, &dns_pref)) {
608 dns_addr = &dns_pref;
609 dns_cnt = 1;
610 }
611
612 if (dns_cnt) {
613 dns_sz = sizeof(*dns) + sizeof(struct in6_addr)*dns_cnt;
614
615 dns = alloca(dns_sz);
616 memset(dns, 0, dns_sz);
617 dns->type = ND_OPT_RECURSIVE_DNS;
618 dns->len = 1 + (2 * dns_cnt);
619 dns->lifetime = htonl(lifetime);
620 memcpy(dns->addr, dns_addr, sizeof(struct in6_addr)*dns_cnt);
621 }
622
623 /* DNS Search options */
624 if (!search_domain && !res_init() && _res.dnsrch[0] && _res.dnsrch[0][0]) {
625 uint8_t search_buf[256];
626
627 int len = dn_comp(_res.dnsrch[0], search_buf,
628 sizeof(search_buf), NULL, NULL);
629 if (len > 0) {
630 search_domain = search_buf;
631 search_len = len;
632 }
633 }
634
635 if (search_len > 0) {
636 size_t search_padded = ((search_len + 7) & (~7)) + 8;
637
638 search_sz = sizeof(*search) + search_padded;
639
640 search = alloca(search_sz);
641 memset(search, 0, search_sz);
642 search->type = ND_OPT_DNS_SEARCH;
643 search->len = search_len ? ((sizeof(*search) + search_padded) / 8) : 0;
644 search->lifetime = htonl(lifetime);
645 memcpy(search->name, search_domain, search_len);
646 memset(&search->name[search_len], 0, search_padded - search_len);
647 }
648 }
649
650 iov[IOV_RA_DNS].iov_base = (char *)dns;
651 iov[IOV_RA_DNS].iov_len = dns_sz;
652 iov[IOV_RA_SEARCH].iov_base = (char *)search;
653 iov[IOV_RA_SEARCH].iov_len = search_sz;
654
655 for (ssize_t i = 0; i < addr_cnt; ++i) {
656 struct odhcpd_ipaddr *addr = &addrs[i];
657 struct nd_opt_route_info *tmp;
658 uint32_t valid;
659
660 if (addr->dprefix > 64 || addr->dprefix == 0 || addr->valid <= (uint32_t)now) {
661 syslog(LOG_INFO, "Address %s (dprefix %d, valid %u) not suitable as RA route on %s",
662 inet_ntop(AF_INET6, &addr->addr.in6, buf, sizeof(buf)),
663 addr->dprefix, addr->valid, iface->name);
664
665 continue; /* Address not suitable */
666 }
667
668 if (odhcpd_bmemcmp(&addr->addr, &iface->pio_filter_addr,
669 iface->pio_filter_length) != 0 ||
670 addr->prefix < iface->pio_filter_length) {
671 syslog(LOG_INFO, "Address %s filtered out as RA route on %s",
672 inet_ntop(AF_INET6, &addr->addr.in6, buf, sizeof(buf)),
673 iface->name);
674 continue; /* PIO filtered out of this RA */
675 }
676
677 if (addr->dprefix > 32) {
678 addr->addr.in6.s6_addr32[1] &= htonl(~((1U << (64 - addr->dprefix)) - 1));
679 } else if (addr->dprefix <= 32) {
680 addr->addr.in6.s6_addr32[0] &= htonl(~((1U << (32 - addr->dprefix)) - 1));
681 addr->addr.in6.s6_addr32[1] = 0;
682 }
683
684 tmp = realloc(routes, sizeof(*routes) * (routes_cnt + 1));
685 if (!tmp) {
686 syslog(LOG_ERR, "Realloc failed for RA route option on %s", iface->name);
687 continue;
688 }
689
690 routes = tmp;
691
692 memset(&routes[routes_cnt], 0, sizeof(*routes));
693 routes[routes_cnt].type = ND_OPT_ROUTE_INFO;
694 routes[routes_cnt].len = sizeof(*routes) / 8;
695 routes[routes_cnt].prefix = addr->dprefix;
696 routes[routes_cnt].flags = 0;
697 if (iface->route_preference < 0)
698 routes[routes_cnt].flags |= ND_RA_PREF_LOW;
699 else if (iface->route_preference > 0)
700 routes[routes_cnt].flags |= ND_RA_PREF_HIGH;
701
702 valid = TIME_LEFT(addr->valid, now);
703 routes[routes_cnt].lifetime = htonl(valid < lifetime ? valid : lifetime);
704 routes[routes_cnt].addr[0] = addr->addr.in6.s6_addr32[0];
705 routes[routes_cnt].addr[1] = addr->addr.in6.s6_addr32[1];
706 routes[routes_cnt].addr[2] = 0;
707 routes[routes_cnt].addr[3] = 0;
708
709 ++routes_cnt;
710 }
711
712 iov[IOV_RA_ROUTES].iov_base = (char *)routes;
713 iov[IOV_RA_ROUTES].iov_len = routes_cnt * sizeof(*routes);
714
715 memset(&adv_interval, 0, sizeof(adv_interval));
716 adv_interval.nd_opt_adv_interval_type = ND_OPT_RTR_ADV_INTERVAL;
717 adv_interval.nd_opt_adv_interval_len = 1;
718 adv_interval.nd_opt_adv_interval_ival = htonl(maxival);
719
720 iov[IOV_RA_ADV_INTERVAL].iov_base = (char *)&adv_interval;
721 iov[IOV_RA_ADV_INTERVAL].iov_len = adv_interval.nd_opt_adv_interval_len * 8;
722
723 memset(&dest, 0, sizeof(dest));
724 dest.sin6_family = AF_INET6;
725
726 if (from && !IN6_IS_ADDR_UNSPECIFIED(from))
727 dest.sin6_addr = *from;
728 else
729 inet_pton(AF_INET6, ALL_IPV6_NODES, &dest.sin6_addr);
730
731 syslog(LOG_NOTICE, "Sending a RA on %s", iface->name);
732
733 odhcpd_send(iface->router_event.uloop.fd, &dest, iov, ARRAY_SIZE(iov), iface);
734
735 free(pfxs);
736 free(routes);
737
738 return msecs;
739 }
740
741
742 static void trigger_router_advert(struct uloop_timeout *event)
743 {
744 struct interface *iface = container_of(event, struct interface, timer_rs);
745 int msecs = send_router_advert(iface, NULL);
746
747 /* Rearm timer if not shut down */
748 if (event->cb)
749 uloop_timeout_set(event, msecs);
750 }
751
752
753 /* Event handler for incoming ICMPv6 packets */
754 static void handle_icmpv6(void *addr, void *data, size_t len,
755 struct interface *iface, _unused void *dest)
756 {
757 struct icmp6_hdr *hdr = data;
758 struct sockaddr_in6 *from = addr;
759
760 if (!router_icmpv6_valid(addr, data, len))
761 return;
762
763 if ((iface->ra == MODE_SERVER && !iface->master)) { /* Server mode */
764 if (hdr->icmp6_type == ND_ROUTER_SOLICIT)
765 send_router_advert(iface, &from->sin6_addr);
766 } else if (iface->ra == MODE_RELAY) { /* Relay mode */
767 if (hdr->icmp6_type == ND_ROUTER_SOLICIT && !iface->master) {
768 struct interface *c;
769
770 avl_for_each_element(&interfaces, c, avl) {
771 if (!c->master || c->ra != MODE_RELAY)
772 continue;
773
774 forward_router_solicitation(c);
775 }
776 } else if (hdr->icmp6_type == ND_ROUTER_ADVERT && iface->master)
777 forward_router_advertisement(iface, data, len);
778 }
779 }
780
781
782 /* Forward router solicitation */
783 static void forward_router_solicitation(const struct interface *iface)
784 {
785 struct icmp6_hdr rs = {ND_ROUTER_SOLICIT, 0, 0, {{0}}};
786 struct iovec iov = {&rs, sizeof(rs)};
787 struct sockaddr_in6 all_routers;
788
789 if (!iface)
790 return;
791
792 memset(&all_routers, 0, sizeof(all_routers));
793 all_routers.sin6_family = AF_INET6;
794 inet_pton(AF_INET6, ALL_IPV6_ROUTERS, &all_routers.sin6_addr);
795 all_routers.sin6_scope_id = iface->ifindex;
796
797 syslog(LOG_NOTICE, "Sending RS to %s", iface->name);
798 odhcpd_send(iface->router_event.uloop.fd, &all_routers, &iov, 1, iface);
799 }
800
801
802 /* Handler for incoming router solicitations on slave interfaces */
803 static void forward_router_advertisement(const struct interface *iface, uint8_t *data, size_t len)
804 {
805 struct nd_router_advert *adv = (struct nd_router_advert *)data;
806 struct sockaddr_in6 all_nodes;
807 struct icmpv6_opt *opt;
808 struct interface *c;
809 struct iovec iov = { .iov_base = data, .iov_len = len };
810 /* Rewrite options */
811 uint8_t *end = data + len;
812 uint8_t *mac_ptr = NULL;
813 struct in6_addr *dns_ptr = NULL;
814 size_t dns_count = 0;
815
816 icmpv6_for_each_option(opt, &adv[1], end) {
817 if (opt->type == ND_OPT_SOURCE_LINKADDR) {
818 /* Store address of source MAC-address */
819 mac_ptr = opt->data;
820 } else if (opt->type == ND_OPT_RECURSIVE_DNS && opt->len > 1) {
821 /* Check if we have to rewrite DNS */
822 dns_ptr = (struct in6_addr*)&opt->data[6];
823 dns_count = (opt->len - 1) / 2;
824 }
825 }
826
827 syslog(LOG_NOTICE, "Got a RA on %s", iface->name);
828
829 /* Indicate a proxy, however we don't follow the rest of RFC 4389 yet */
830 adv->nd_ra_flags_reserved |= ND_RA_FLAG_PROXY;
831
832 /* Forward advertisement to all slave interfaces */
833 memset(&all_nodes, 0, sizeof(all_nodes));
834 all_nodes.sin6_family = AF_INET6;
835 inet_pton(AF_INET6, ALL_IPV6_NODES, &all_nodes.sin6_addr);
836
837 avl_for_each_element(&interfaces, c, avl) {
838 if (c->ra != MODE_RELAY || c->master)
839 continue;
840
841 /* Fixup source hardware address option */
842 if (mac_ptr)
843 odhcpd_get_mac(c, mac_ptr);
844
845 /* If we have to rewrite DNS entries */
846 if (c->always_rewrite_dns && dns_ptr && dns_count > 0) {
847 const struct in6_addr *rewrite = c->dns;
848 struct in6_addr addr;
849 size_t rewrite_cnt = c->dns_cnt;
850
851 if (rewrite_cnt == 0) {
852 if (odhcpd_get_interface_dns_addr(c, &addr))
853 continue; /* Unable to comply */
854
855 rewrite = &addr;
856 rewrite_cnt = 1;
857 }
858
859 /* Copy over any other addresses */
860 for (size_t i = 0; i < dns_count; ++i) {
861 size_t j = (i < rewrite_cnt) ? i : rewrite_cnt - 1;
862 dns_ptr[i] = rewrite[j];
863 }
864 }
865
866 syslog(LOG_NOTICE, "Forward a RA on %s", c->name);
867
868 odhcpd_send(c->router_event.uloop.fd, &all_nodes, &iov, 1, c);
869 }
870 }