dhcpv6-ia: simplify logic to write statefile and dhcpv6 logging
[project/odhcpd.git] / src / router.c
1 /**
2  * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License v2 as published by
6  * the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  */
14
15 #include <errno.h>
16 #include <fcntl.h>
17 #include <signal.h>
18 #include <resolv.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <stdbool.h>
23 #include <arpa/inet.h>
24 #include <net/route.h>
25
26 #include "router.h"
27 #include "odhcpd.h"
28
29
30 static void forward_router_solicitation(const struct interface *iface);
31 static void forward_router_advertisement(uint8_t *data, size_t len);
32
33 static void handle_icmpv6(void *addr, void *data, size_t len,
34                 struct interface *iface, void *dest);
35 static void trigger_router_advert(struct uloop_timeout *event);
36 static void sigusr1_refresh(int signal);
37
38 static struct odhcpd_event router_event = {.uloop = {.fd = -1}, .handle_dgram = handle_icmpv6, };
39
40 static FILE *fp_route = NULL;
41 #define RA_IOV_LEN 6
42
43 #define TIME_LEFT(t1, now) ((t1) != UINT32_MAX ? (t1) - (now) : UINT32_MAX)
44
45 int init_router(void)
46 {
47         // Open ICMPv6 socket
48         int sock = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_ICMPV6);
49         if (sock < 0 && errno != EAFNOSUPPORT) {
50                 syslog(LOG_ERR, "Failed to open RAW-socket: %s", strerror(errno));
51                 return -1;
52         }
53
54         // Let the kernel compute our checksums
55         int val = 2;
56         setsockopt(sock, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val));
57
58         // This is required by RFC 4861
59         val = 255;
60         setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val));
61         setsockopt(sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val, sizeof(val));
62
63         // We need to know the source interface
64         val = 1;
65         setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &val, sizeof(val));
66         setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &val, sizeof(val));
67
68         // Don't loop back
69         val = 0;
70         setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val, sizeof(val));
71
72         // Filter ICMPv6 package types
73         struct icmp6_filter filt;
74         ICMP6_FILTER_SETBLOCKALL(&filt);
75         ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
76         ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filt);
77         setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, sizeof(filt));
78
79         // Register socket
80         router_event.uloop.fd = sock;
81         odhcpd_register(&router_event);
82
83         if (!(fp_route = fopen("/proc/net/ipv6_route", "r")))
84                 syslog(LOG_ERR, "Failed to open routing table: %s",
85                                 strerror(errno));
86
87         signal(SIGUSR1, sigusr1_refresh);
88         return 0;
89 }
90
91
92 int setup_router_interface(struct interface *iface, bool enable)
93 {
94         if (!fp_route || router_event.uloop.fd < 0)
95                 return -1;
96
97         struct ipv6_mreq all_nodes = {ALL_IPV6_NODES, iface->ifindex};
98         struct ipv6_mreq all_routers = {ALL_IPV6_ROUTERS, iface->ifindex};
99
100         uloop_timeout_cancel(&iface->timer_rs);
101         iface->timer_rs.cb = NULL;
102
103         if (iface->ifindex <= 0)
104                 return -1;
105
106         setsockopt(router_event.uloop.fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP,
107                         &all_nodes, sizeof(all_nodes));
108         setsockopt(router_event.uloop.fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP,
109                         &all_routers, sizeof(all_routers));
110
111         if (!enable) {
112                 if (iface->ra)
113                         trigger_router_advert(&iface->timer_rs);
114         } else {
115                 void *mreq = &all_routers;
116
117                 if (iface->ra == RELAYD_RELAY && iface->master) {
118                         mreq = &all_nodes;
119                         forward_router_solicitation(iface);
120                 } else if (iface->ra == RELAYD_SERVER && !iface->master) {
121                         iface->timer_rs.cb = trigger_router_advert;
122                         uloop_timeout_set(&iface->timer_rs, 1000);
123                         ndp_rqs_addr6_dump();
124                 }
125
126                 if (iface->ra == RELAYD_RELAY || (iface->ra == RELAYD_SERVER && !iface->master))
127                         setsockopt(router_event.uloop.fd, IPPROTO_IPV6,
128                                         IPV6_ADD_MEMBERSHIP, mreq, sizeof(all_nodes));
129         }
130         return 0;
131 }
132
133
134 // Signal handler to resend all RDs
135 static void sigusr1_refresh(_unused int signal)
136 {
137         struct interface *iface;
138         list_for_each_entry(iface, &interfaces, head)
139                 if (iface->ra == RELAYD_SERVER && !iface->master)
140                         uloop_timeout_set(&iface->timer_rs, 1000);
141 }
142
143 static bool router_icmpv6_valid(struct sockaddr_in6 *source, uint8_t *data, size_t len)
144 {
145         struct icmp6_hdr *hdr = (struct icmp6_hdr *)data;
146         struct icmpv6_opt *opt, *end = (struct icmpv6_opt*)&data[len];
147
148         /* Hoplimit is already checked in odhcpd_receive_packets */
149         if (len < sizeof(*hdr) || hdr->icmp6_code)
150                 return false;
151
152         switch (hdr->icmp6_type) {
153         case ND_ROUTER_ADVERT:
154                 if (!IN6_IS_ADDR_LINKLOCAL(&source->sin6_addr))
155                         return false;
156
157                 opt = (struct icmpv6_opt *)((struct nd_router_advert *)data + 1);
158                 break;
159
160         case ND_ROUTER_SOLICIT:
161                 opt = (struct icmpv6_opt *)((struct nd_router_solicit *)data + 1);
162                 break;
163
164         default:
165                 return false;
166         }
167
168         icmpv6_for_each_option(opt, opt, end)
169                 if (opt->type == ND_OPT_SOURCE_LINKADDR &&
170                                 IN6_IS_ADDR_UNSPECIFIED(&source->sin6_addr) &&
171                                 hdr->icmp6_type == ND_ROUTER_SOLICIT)
172                         return false;
173
174         // Check all options parsed successfully
175         return opt == end;
176 }
177
178
179 // Detect whether a default route exists, also find the source prefixes
180 static bool parse_routes(struct odhcpd_ipaddr *n, ssize_t len)
181 {
182         rewind(fp_route);
183
184         char line[512], ifname[16];
185         bool found_default = false;
186         struct odhcpd_ipaddr p = {IN6ADDR_ANY_INIT, 0, 0, 0, 0};
187         while (fgets(line, sizeof(line), fp_route)) {
188                 uint32_t rflags;
189                 if (sscanf(line, "00000000000000000000000000000000 00 "
190                                 "%*s %*s %*s %*s %*s %*s %*s %15s", ifname) &&
191                                 strcmp(ifname, "lo")) {
192                         found_default = true;
193                 } else if (sscanf(line, "%8" SCNx32 "%8" SCNx32 "%*8" SCNx32 "%*8" SCNx32 " %hhx %*s "
194                                 "%*s 00000000000000000000000000000000 %*s %*s %*s %" SCNx32 " lo",
195                                 &p.addr.s6_addr32[0], &p.addr.s6_addr32[1], &p.prefix, &rflags) &&
196                                 p.prefix > 0 && (rflags & RTF_NONEXTHOP) && (rflags & RTF_REJECT)) {
197                         // Find source prefixes by scanning through unreachable-routes
198                         p.addr.s6_addr32[0] = htonl(p.addr.s6_addr32[0]);
199                         p.addr.s6_addr32[1] = htonl(p.addr.s6_addr32[1]);
200
201                         for (ssize_t i = 0; i < len; ++i) {
202                                 if (n[i].prefix <= 64 && n[i].prefix >= p.prefix &&
203                                                 !odhcpd_bmemcmp(&p.addr, &n[i].addr, p.prefix)) {
204                                         n[i].dprefix = p.prefix;
205                                         break;
206                                 }
207                         }
208
209                 }
210         }
211
212         return found_default;
213 }
214
215 static int calc_adv_interval(struct interface *iface, uint32_t minvalid,
216                 uint32_t *maxival)
217 {
218         uint32_t minival = iface->ra_mininterval;
219         int msecs;
220
221         *maxival = iface->ra_maxinterval;
222
223         if (*maxival > minvalid/3)
224                 *maxival = minvalid/3;
225
226         if (*maxival > MaxRtrAdvInterval)
227                 *maxival = MaxRtrAdvInterval;
228         else if (*maxival < 4)
229                 *maxival = 4;
230
231         if (minival < MinRtrAdvInterval)
232                 minival = MinRtrAdvInterval;
233         else if (minival > (*maxival * 3)/4)
234                 minival = (*maxival >= 9 ? *maxival/3 : *maxival);
235
236         odhcpd_urandom(&msecs, sizeof(msecs));
237         msecs = (labs(msecs) % ((*maxival != minival) ? (*maxival - minival)*1000 : 500)) +
238                         minival*1000;
239
240         return msecs;
241 }
242
243 static uint16_t calc_ra_lifetime(struct interface *iface, uint32_t maxival)
244 {
245         uint16_t lifetime = 3*maxival;
246
247         if (iface->ra_lifetime >= 0) {
248                 lifetime = iface->ra_lifetime;
249                 if (lifetime < maxival)
250                         lifetime = maxival;
251                 else if (lifetime > 9000)
252                         lifetime = 9000;
253         }
254
255         return lifetime;
256 }
257
258 // Router Advert server mode
259 static uint64_t send_router_advert(struct interface *iface, const struct in6_addr *from)
260 {
261         time_t now = odhcpd_time();
262         int mtu = odhcpd_get_interface_config(iface->ifname, "mtu");
263         int hlim = odhcpd_get_interface_config(iface->ifname, "hop_limit");
264
265         if (mtu < 1280)
266                 mtu = 1280;
267
268         struct {
269                 struct nd_router_advert h;
270                 struct icmpv6_opt lladdr;
271                 struct nd_opt_mtu mtu;
272                 struct nd_opt_prefix_info prefix[sizeof(iface->ia_addr) / sizeof(*iface->ia_addr)];
273         } adv = {
274                 .h = {{.icmp6_type = ND_ROUTER_ADVERT, .icmp6_code = 0}, 0, 0},
275                 .lladdr = {ND_OPT_SOURCE_LINKADDR, 1, {0}},
276                 .mtu = {ND_OPT_MTU, 1, 0, htonl(mtu)},
277         };
278
279         if (hlim > 0)
280                 adv.h.nd_ra_curhoplimit = hlim;
281
282         if (iface->dhcpv6)
283                 adv.h.nd_ra_flags_reserved = ND_RA_FLAG_OTHER;
284
285         if (iface->managed >= RELAYD_MANAGED_MFLAG)
286                 adv.h.nd_ra_flags_reserved |= ND_RA_FLAG_MANAGED;
287
288         if (iface->route_preference < 0)
289                 adv.h.nd_ra_flags_reserved |= ND_RA_PREF_LOW;
290         else if (iface->route_preference > 0)
291                 adv.h.nd_ra_flags_reserved |= ND_RA_PREF_HIGH;
292         odhcpd_get_mac(iface, adv.lladdr.data);
293
294         // If not currently shutting down
295         struct odhcpd_ipaddr addrs[RELAYD_MAX_ADDRS];
296         ssize_t ipcnt = 0;
297         uint32_t minvalid = UINT32_MAX;
298         bool default_route = false;
299         bool valid_prefix = false;
300
301         // If not shutdown
302         if (iface->timer_rs.cb) {
303                 ipcnt = iface->ia_addr_len;
304                 memcpy(addrs, iface->ia_addr, ipcnt * sizeof(*addrs));
305
306                 // Check default route
307                 if (iface->default_router) {
308                         default_route = true;
309
310                         if (iface->default_router > 1)
311                                 valid_prefix = true;
312                 } else if (parse_routes(addrs, ipcnt))
313                         default_route = true;
314         }
315
316         // Construct Prefix Information options
317         size_t cnt = 0;
318
319         struct in6_addr dns_pref, *dns_addr = &dns_pref;
320         size_t dns_cnt = 1;
321
322         odhcpd_get_interface_dns_addr(iface, &dns_pref);
323
324         for (ssize_t i = 0; i < ipcnt; ++i) {
325                 struct odhcpd_ipaddr *addr = &addrs[i];
326                 uint32_t preferred = 0;
327                 uint32_t valid = 0;
328
329                 if (addr->prefix > 96 || addr->valid <= (uint32_t)now) {
330                         char namebuf[INET6_ADDRSTRLEN];
331
332                         inet_ntop(AF_INET6, addr, namebuf, sizeof(namebuf));
333                         syslog(LOG_INFO, "Address %s (prefix %d, valid %u) not suitable as RA prefix on %s",
334                                         namebuf, addr->prefix, addr->valid, iface->ifname);
335                         continue;
336                 }
337
338                 struct nd_opt_prefix_info *p = NULL;
339                 for (size_t i = 0; i < cnt; ++i) {
340                         if (addr->prefix == adv.prefix[i].nd_opt_pi_prefix_len &&
341                                         !odhcpd_bmemcmp(&adv.prefix[i].nd_opt_pi_prefix,
342                                         &addr->addr, addr->prefix))
343                                 p = &adv.prefix[i];
344                 }
345
346                 if (!p) {
347                         if (cnt >= ARRAY_SIZE(adv.prefix))
348                                 break;
349
350                         p = &adv.prefix[cnt++];
351                 }
352
353                 if (addr->preferred > (uint32_t)now) {
354                         preferred = TIME_LEFT(addr->preferred, now);
355
356                         if (iface->ra_useleasetime &&
357                                         preferred > iface->dhcpv4_leasetime)
358                                 preferred = iface->dhcpv4_leasetime;
359                 }
360
361                 valid = TIME_LEFT(addr->valid, now);
362                 if (iface->ra_useleasetime && valid > iface->dhcpv4_leasetime)
363                         valid = iface->dhcpv4_leasetime;
364
365                 if (minvalid > valid)
366                         minvalid = valid;
367
368                 if (!IN6_IS_ADDR_ULA(&addr->addr) || iface->default_router)
369                         valid_prefix = true;
370
371                 odhcpd_bmemcpy(&p->nd_opt_pi_prefix, &addr->addr,
372                                 (iface->ra_advrouter) ? 128 : addr->prefix);
373                 p->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
374                 p->nd_opt_pi_len = 4;
375                 p->nd_opt_pi_prefix_len = (addr->prefix < 64) ? 64 : addr->prefix;
376                 p->nd_opt_pi_flags_reserved = 0;
377                 if (!iface->ra_not_onlink)
378                         p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_ONLINK;
379                 if (iface->managed < RELAYD_MANAGED_NO_AFLAG && addr->prefix <= 64)
380                         p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_AUTO;
381                 if (iface->ra_advrouter)
382                         p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_RADDR;
383                 p->nd_opt_pi_preferred_time = htonl(preferred);
384                 p->nd_opt_pi_valid_time = htonl(valid);
385         }
386
387         // Calculate periodic transmit
388         uint32_t maxival;
389         int msecs = calc_adv_interval(iface, minvalid, &maxival);
390
391         if (default_route) {
392                 if (!valid_prefix) {
393                         syslog(LOG_WARNING, "A default route is present but there is no public prefix "
394                                         "on %s thus we don't announce a default route!", iface->ifname);
395                         adv.h.nd_ra_router_lifetime = 0;
396                 } else
397                         adv.h.nd_ra_router_lifetime = htons(calc_ra_lifetime(iface, maxival));
398
399         } else
400                 adv.h.nd_ra_router_lifetime = 0;
401
402         syslog(LOG_INFO, "Using a RA lifetime of %d seconds on %s", ntohs(adv.h.nd_ra_router_lifetime), iface->ifname);
403
404         // DNS Recursive DNS
405         if (iface->dns_cnt > 0) {
406                 dns_addr = iface->dns;
407                 dns_cnt = iface->dns_cnt;
408         }
409
410         if (!dns_addr || IN6_IS_ADDR_UNSPECIFIED(dns_addr))
411                 dns_cnt = 0;
412
413         struct {
414                 uint8_t type;
415                 uint8_t len;
416                 uint8_t pad;
417                 uint8_t pad2;
418                 uint32_t lifetime;
419         } dns = {ND_OPT_RECURSIVE_DNS, (1 + (2 * dns_cnt)), 0, 0, 0};
420
421
422
423         // DNS Search options
424         uint8_t search_buf[256], *search_domain = iface->search;
425         size_t search_len = iface->search_len, search_padded = 0;
426
427         if (!search_domain && !res_init() && _res.dnsrch[0] && _res.dnsrch[0][0]) {
428                 int len = dn_comp(_res.dnsrch[0], search_buf,
429                                 sizeof(search_buf), NULL, NULL);
430                 if (len > 0) {
431                         search_domain = search_buf;
432                         search_len = len;
433                 }
434         }
435
436         if (search_len > 0)
437                 search_padded = ((search_len + 7) & (~7)) + 8;
438
439         struct {
440                 uint8_t type;
441                 uint8_t len;
442                 uint8_t pad;
443                 uint8_t pad2;
444                 uint32_t lifetime;
445                 uint8_t name[];
446         } *search = alloca(sizeof(*search) + search_padded);
447
448         search->type = ND_OPT_DNS_SEARCH;
449         search->len = search_len ? ((sizeof(*search) + search_padded) / 8) : 0;
450         search->pad = 0;
451         search->pad2 = 0;
452         memcpy(search->name, search_domain, search_len);
453         memset(&search->name[search_len], 0, search_padded - search_len);
454
455
456         size_t routes_cnt = 0;
457         struct {
458                 uint8_t type;
459                 uint8_t len;
460                 uint8_t prefix;
461                 uint8_t flags;
462                 uint32_t lifetime;
463                 uint32_t addr[4];
464         } routes[RELAYD_MAX_PREFIXES];
465
466         for (ssize_t i = 0; i < ipcnt; ++i) {
467                 struct odhcpd_ipaddr *addr = &addrs[i];
468                 if (addr->dprefix > 64 || addr->dprefix == 0 || addr->valid <= (uint32_t)now ||
469                                 (addr->dprefix == 64 && addr->prefix == 64)) {
470                         continue; // Address not suitable
471                 } else if (addr->dprefix > 32) {
472                         addr->addr.s6_addr32[1] &= htonl(~((1U << (64 - addr->dprefix)) - 1));
473                 } else if (addr->dprefix <= 32) {
474                         addr->addr.s6_addr32[0] &= htonl(~((1U << (32 - addr->dprefix)) - 1));
475                         addr->addr.s6_addr32[1] = 0;
476                 }
477
478                 routes[routes_cnt].type = ND_OPT_ROUTE_INFO;
479                 routes[routes_cnt].len = sizeof(*routes) / 8;
480                 routes[routes_cnt].prefix = addr->dprefix;
481                 routes[routes_cnt].flags = 0;
482                 if (iface->route_preference < 0)
483                         routes[routes_cnt].flags |= ND_RA_PREF_LOW;
484                 else if (iface->route_preference > 0)
485                         routes[routes_cnt].flags |= ND_RA_PREF_HIGH;
486                 routes[routes_cnt].lifetime = htonl(TIME_LEFT(addr->valid, now));
487                 routes[routes_cnt].addr[0] = addr->addr.s6_addr32[0];
488                 routes[routes_cnt].addr[1] = addr->addr.s6_addr32[1];
489                 routes[routes_cnt].addr[2] = 0;
490                 routes[routes_cnt].addr[3] = 0;
491
492                 ++routes_cnt;
493         }
494
495         search->lifetime = htonl(maxival*10);
496         dns.lifetime = search->lifetime;
497
498         struct icmpv6_opt adv_interval = {
499                 .type = ND_OPT_RTR_ADV_INTERVAL,
500                 .len = 1,
501                 .data = {0, 0, (maxival*1000) >> 24, (maxival*1000) >> 16, (maxival*1000) >> 8, maxival*1000}
502         };
503
504         struct iovec iov[RA_IOV_LEN] = {
505                         {&adv, (uint8_t*)&adv.prefix[cnt] - (uint8_t*)&adv},
506                         {&routes, routes_cnt * sizeof(*routes)},
507                         {&dns, (dns_cnt) ? sizeof(dns) : 0},
508                         {dns_addr, dns_cnt * sizeof(*dns_addr)},
509                         {search, search->len * 8},
510                         {&adv_interval, adv_interval.len * 8}};
511         struct sockaddr_in6 dest = {AF_INET6, 0, 0, ALL_IPV6_NODES, 0};
512
513         if (from && !IN6_IS_ADDR_UNSPECIFIED(from))
514                 dest.sin6_addr = *from;
515
516         odhcpd_send(router_event.uloop.fd,
517                         &dest, iov, ARRAY_SIZE(iov), iface);
518
519         return msecs;
520 }
521
522
523 static void trigger_router_advert(struct uloop_timeout *event)
524 {
525         struct interface *iface = container_of(event, struct interface, timer_rs);
526         int msecs = send_router_advert(iface, NULL);
527
528         // Rearm timer if not shut down
529         if (event->cb)
530                 uloop_timeout_set(event, msecs);
531 }
532
533
534 // Event handler for incoming ICMPv6 packets
535 static void handle_icmpv6(void *addr, void *data, size_t len,
536                 struct interface *iface, _unused void *dest)
537 {
538         struct icmp6_hdr *hdr = data;
539         struct sockaddr_in6 *from = addr;
540
541         if (!router_icmpv6_valid(addr, data, len))
542                 return;
543
544         if ((iface->ra == RELAYD_SERVER && !iface->master)) { // Server mode
545                 if (hdr->icmp6_type == ND_ROUTER_SOLICIT)
546                         send_router_advert(iface, &from->sin6_addr);
547         } else if (iface->ra == RELAYD_RELAY) { // Relay mode
548                 if (hdr->icmp6_type == ND_ROUTER_ADVERT && iface->master)
549                         forward_router_advertisement(data, len);
550                 else if (hdr->icmp6_type == ND_ROUTER_SOLICIT && !iface->master)
551                         forward_router_solicitation(odhcpd_get_master_interface());
552         }
553 }
554
555
556 // Forward router solicitation
557 static void forward_router_solicitation(const struct interface *iface)
558 {
559         if (!iface)
560                 return;
561
562         struct icmp6_hdr rs = {ND_ROUTER_SOLICIT, 0, 0, {{0}}};
563         struct iovec iov = {&rs, sizeof(rs)};
564         struct sockaddr_in6 all_routers =
565                 {AF_INET6, 0, 0, ALL_IPV6_ROUTERS, iface->ifindex};
566
567         syslog(LOG_NOTICE, "Sending RS to %s", iface->ifname);
568         odhcpd_send(router_event.uloop.fd, &all_routers, &iov, 1, iface);
569 }
570
571
572 // Handler for incoming router solicitations on slave interfaces
573 static void forward_router_advertisement(uint8_t *data, size_t len)
574 {
575         struct nd_router_advert *adv = (struct nd_router_advert *)data;
576
577         // Rewrite options
578         uint8_t *end = data + len;
579         uint8_t *mac_ptr = NULL;
580         struct in6_addr *dns_ptr = NULL;
581         size_t dns_count = 0;
582
583         struct icmpv6_opt *opt;
584         icmpv6_for_each_option(opt, &adv[1], end) {
585                 if (opt->type == ND_OPT_SOURCE_LINKADDR) {
586                         // Store address of source MAC-address
587                         mac_ptr = opt->data;
588                 } else if (opt->type == ND_OPT_RECURSIVE_DNS && opt->len > 1) {
589                         // Check if we have to rewrite DNS
590                         dns_ptr = (struct in6_addr*)&opt->data[6];
591                         dns_count = (opt->len - 1) / 2;
592                 }
593         }
594
595         syslog(LOG_NOTICE, "Got a RA");
596
597         // Indicate a proxy, however we don't follow the rest of RFC 4389 yet
598         adv->nd_ra_flags_reserved |= ND_RA_FLAG_PROXY;
599
600         // Forward advertisement to all slave interfaces
601         struct sockaddr_in6 all_nodes = {AF_INET6, 0, 0, ALL_IPV6_NODES, 0};
602         struct iovec iov = {data, len};
603
604         struct interface *iface;
605         list_for_each_entry(iface, &interfaces, head) {
606                 if (iface->ra != RELAYD_RELAY || iface->master)
607                         continue;
608
609                 // Fixup source hardware address option
610                 if (mac_ptr)
611                         odhcpd_get_mac(iface, mac_ptr);
612
613                 // If we have to rewrite DNS entries
614                 if (iface->always_rewrite_dns && dns_ptr && dns_count > 0) {
615                         const struct in6_addr *rewrite = iface->dns;
616                         struct in6_addr addr;
617                         size_t rewrite_cnt = iface->dns_cnt;
618
619                         if (rewrite_cnt == 0) {
620                                 if (odhcpd_get_interface_dns_addr(iface, &addr))
621                                         continue; // Unable to comply
622
623                                 rewrite = &addr;
624                                 rewrite_cnt = 1;
625                         }
626
627                         // Copy over any other addresses
628                         for (size_t i = 0; i < dns_count; ++i) {
629                                 size_t j = (i < rewrite_cnt) ? i : rewrite_cnt - 1;
630                                 dns_ptr[i] = rewrite[j];
631                         }
632                 }
633
634                 odhcpd_send(router_event.uloop.fd, &all_nodes, &iov, 1, iface);
635         }
636 }