config: make RA retransTime configurable via uci
[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         uint32_t mtu = iface->ra_mtu;
263         int hlim = iface->ra_hoplimit;
264
265         if (mtu == 0)
266                  mtu = odhcpd_get_interface_config(iface->ifname, "mtu");
267
268         if (mtu < 1280)
269                 mtu = 1280;
270
271         if (hlim == 0)
272                 hlim = odhcpd_get_interface_config(iface->ifname, "hop_limit");
273
274         struct {
275                 struct nd_router_advert h;
276                 struct icmpv6_opt lladdr;
277                 struct nd_opt_mtu mtu;
278                 struct nd_opt_prefix_info prefix[sizeof(iface->ia_addr) / sizeof(*iface->ia_addr)];
279         } adv = {
280                 .h = {{.icmp6_type = ND_ROUTER_ADVERT, .icmp6_code = 0}, 0, 0},
281                 .lladdr = {ND_OPT_SOURCE_LINKADDR, 1, {0}},
282                 .mtu = {ND_OPT_MTU, 1, 0, htonl(mtu)},
283         };
284
285         if (hlim > 0)
286                 adv.h.nd_ra_curhoplimit = hlim;
287
288         if (iface->dhcpv6)
289                 adv.h.nd_ra_flags_reserved = ND_RA_FLAG_OTHER;
290
291         if (iface->managed >= RELAYD_MANAGED_MFLAG)
292                 adv.h.nd_ra_flags_reserved |= ND_RA_FLAG_MANAGED;
293
294         if (iface->route_preference < 0)
295                 adv.h.nd_ra_flags_reserved |= ND_RA_PREF_LOW;
296         else if (iface->route_preference > 0)
297                 adv.h.nd_ra_flags_reserved |= ND_RA_PREF_HIGH;
298
299         adv.h.nd_ra_reachable = htonl(iface->ra_reachabletime);
300         adv.h.nd_ra_retransmit = htonl(iface->ra_retranstime);
301
302         odhcpd_get_mac(iface, adv.lladdr.data);
303
304         // If not currently shutting down
305         struct odhcpd_ipaddr addrs[RELAYD_MAX_ADDRS];
306         ssize_t ipcnt = 0;
307         uint32_t minvalid = UINT32_MAX;
308         bool default_route = false;
309         bool valid_prefix = false;
310
311         // If not shutdown
312         if (iface->timer_rs.cb) {
313                 ipcnt = iface->ia_addr_len;
314                 memcpy(addrs, iface->ia_addr, ipcnt * sizeof(*addrs));
315
316                 // Check default route
317                 if (iface->default_router) {
318                         default_route = true;
319
320                         if (iface->default_router > 1)
321                                 valid_prefix = true;
322                 } else if (parse_routes(addrs, ipcnt))
323                         default_route = true;
324         }
325
326         // Construct Prefix Information options
327         size_t cnt = 0;
328
329         struct in6_addr dns_pref, *dns_addr = &dns_pref;
330         size_t dns_cnt = 1;
331
332         odhcpd_get_interface_dns_addr(iface, &dns_pref);
333
334         for (ssize_t i = 0; i < ipcnt; ++i) {
335                 struct odhcpd_ipaddr *addr = &addrs[i];
336                 uint32_t preferred = 0;
337                 uint32_t valid = 0;
338
339                 if (addr->prefix > 96 || addr->valid <= (uint32_t)now) {
340                         char namebuf[INET6_ADDRSTRLEN];
341
342                         inet_ntop(AF_INET6, addr, namebuf, sizeof(namebuf));
343                         syslog(LOG_INFO, "Address %s (prefix %d, valid %u) not suitable as RA prefix on %s",
344                                         namebuf, addr->prefix, addr->valid, iface->ifname);
345                         continue;
346                 }
347
348                 struct nd_opt_prefix_info *p = NULL;
349                 for (size_t i = 0; i < cnt; ++i) {
350                         if (addr->prefix == adv.prefix[i].nd_opt_pi_prefix_len &&
351                                         !odhcpd_bmemcmp(&adv.prefix[i].nd_opt_pi_prefix,
352                                         &addr->addr, addr->prefix))
353                                 p = &adv.prefix[i];
354                 }
355
356                 if (!p) {
357                         if (cnt >= ARRAY_SIZE(adv.prefix))
358                                 break;
359
360                         p = &adv.prefix[cnt++];
361                 }
362
363                 if (addr->preferred > (uint32_t)now) {
364                         preferred = TIME_LEFT(addr->preferred, now);
365
366                         if (iface->ra_useleasetime &&
367                                         preferred > iface->dhcpv4_leasetime)
368                                 preferred = iface->dhcpv4_leasetime;
369                 }
370
371                 valid = TIME_LEFT(addr->valid, now);
372                 if (iface->ra_useleasetime && valid > iface->dhcpv4_leasetime)
373                         valid = iface->dhcpv4_leasetime;
374
375                 if (minvalid > valid)
376                         minvalid = valid;
377
378                 if (!IN6_IS_ADDR_ULA(&addr->addr) || iface->default_router)
379                         valid_prefix = true;
380
381                 odhcpd_bmemcpy(&p->nd_opt_pi_prefix, &addr->addr,
382                                 (iface->ra_advrouter) ? 128 : addr->prefix);
383                 p->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
384                 p->nd_opt_pi_len = 4;
385                 p->nd_opt_pi_prefix_len = (addr->prefix < 64) ? 64 : addr->prefix;
386                 p->nd_opt_pi_flags_reserved = 0;
387                 if (!iface->ra_not_onlink)
388                         p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_ONLINK;
389                 if (iface->managed < RELAYD_MANAGED_NO_AFLAG && addr->prefix <= 64)
390                         p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_AUTO;
391                 if (iface->ra_advrouter)
392                         p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_RADDR;
393                 p->nd_opt_pi_preferred_time = htonl(preferred);
394                 p->nd_opt_pi_valid_time = htonl(valid);
395         }
396
397         // Calculate periodic transmit
398         uint32_t maxival;
399         int msecs = calc_adv_interval(iface, minvalid, &maxival);
400
401         if (default_route) {
402                 if (!valid_prefix) {
403                         syslog(LOG_WARNING, "A default route is present but there is no public prefix "
404                                         "on %s thus we don't announce a default route!", iface->ifname);
405                         adv.h.nd_ra_router_lifetime = 0;
406                 } else
407                         adv.h.nd_ra_router_lifetime = htons(calc_ra_lifetime(iface, maxival));
408
409         } else
410                 adv.h.nd_ra_router_lifetime = 0;
411
412         syslog(LOG_INFO, "Using a RA lifetime of %d seconds on %s", ntohs(adv.h.nd_ra_router_lifetime), iface->ifname);
413
414         // DNS Recursive DNS
415         if (iface->dns_cnt > 0) {
416                 dns_addr = iface->dns;
417                 dns_cnt = iface->dns_cnt;
418         }
419
420         if (!dns_addr || IN6_IS_ADDR_UNSPECIFIED(dns_addr))
421                 dns_cnt = 0;
422
423         struct {
424                 uint8_t type;
425                 uint8_t len;
426                 uint8_t pad;
427                 uint8_t pad2;
428                 uint32_t lifetime;
429         } dns = {ND_OPT_RECURSIVE_DNS, (1 + (2 * dns_cnt)), 0, 0, 0};
430
431
432
433         // DNS Search options
434         uint8_t search_buf[256], *search_domain = iface->search;
435         size_t search_len = iface->search_len, search_padded = 0;
436
437         if (!search_domain && !res_init() && _res.dnsrch[0] && _res.dnsrch[0][0]) {
438                 int len = dn_comp(_res.dnsrch[0], search_buf,
439                                 sizeof(search_buf), NULL, NULL);
440                 if (len > 0) {
441                         search_domain = search_buf;
442                         search_len = len;
443                 }
444         }
445
446         if (search_len > 0)
447                 search_padded = ((search_len + 7) & (~7)) + 8;
448
449         struct {
450                 uint8_t type;
451                 uint8_t len;
452                 uint8_t pad;
453                 uint8_t pad2;
454                 uint32_t lifetime;
455                 uint8_t name[];
456         } *search = alloca(sizeof(*search) + search_padded);
457
458         search->type = ND_OPT_DNS_SEARCH;
459         search->len = search_len ? ((sizeof(*search) + search_padded) / 8) : 0;
460         search->pad = 0;
461         search->pad2 = 0;
462         memcpy(search->name, search_domain, search_len);
463         memset(&search->name[search_len], 0, search_padded - search_len);
464
465
466         size_t routes_cnt = 0;
467         struct {
468                 uint8_t type;
469                 uint8_t len;
470                 uint8_t prefix;
471                 uint8_t flags;
472                 uint32_t lifetime;
473                 uint32_t addr[4];
474         } routes[RELAYD_MAX_PREFIXES];
475
476         for (ssize_t i = 0; i < ipcnt; ++i) {
477                 struct odhcpd_ipaddr *addr = &addrs[i];
478                 if (addr->dprefix > 64 || addr->dprefix == 0 || addr->valid <= (uint32_t)now ||
479                                 (addr->dprefix == 64 && addr->prefix == 64)) {
480                         continue; // Address not suitable
481                 } else if (addr->dprefix > 32) {
482                         addr->addr.s6_addr32[1] &= htonl(~((1U << (64 - addr->dprefix)) - 1));
483                 } else if (addr->dprefix <= 32) {
484                         addr->addr.s6_addr32[0] &= htonl(~((1U << (32 - addr->dprefix)) - 1));
485                         addr->addr.s6_addr32[1] = 0;
486                 }
487
488                 routes[routes_cnt].type = ND_OPT_ROUTE_INFO;
489                 routes[routes_cnt].len = sizeof(*routes) / 8;
490                 routes[routes_cnt].prefix = addr->dprefix;
491                 routes[routes_cnt].flags = 0;
492                 if (iface->route_preference < 0)
493                         routes[routes_cnt].flags |= ND_RA_PREF_LOW;
494                 else if (iface->route_preference > 0)
495                         routes[routes_cnt].flags |= ND_RA_PREF_HIGH;
496                 routes[routes_cnt].lifetime = htonl(TIME_LEFT(addr->valid, now));
497                 routes[routes_cnt].addr[0] = addr->addr.s6_addr32[0];
498                 routes[routes_cnt].addr[1] = addr->addr.s6_addr32[1];
499                 routes[routes_cnt].addr[2] = 0;
500                 routes[routes_cnt].addr[3] = 0;
501
502                 ++routes_cnt;
503         }
504
505         search->lifetime = htonl(maxival*10);
506         dns.lifetime = search->lifetime;
507
508         struct icmpv6_opt adv_interval = {
509                 .type = ND_OPT_RTR_ADV_INTERVAL,
510                 .len = 1,
511                 .data = {0, 0, (maxival*1000) >> 24, (maxival*1000) >> 16, (maxival*1000) >> 8, maxival*1000}
512         };
513
514         struct iovec iov[RA_IOV_LEN] = {
515                         {&adv, (uint8_t*)&adv.prefix[cnt] - (uint8_t*)&adv},
516                         {&routes, routes_cnt * sizeof(*routes)},
517                         {&dns, (dns_cnt) ? sizeof(dns) : 0},
518                         {dns_addr, dns_cnt * sizeof(*dns_addr)},
519                         {search, search->len * 8},
520                         {&adv_interval, adv_interval.len * 8}};
521         struct sockaddr_in6 dest = {AF_INET6, 0, 0, ALL_IPV6_NODES, 0};
522
523         if (from && !IN6_IS_ADDR_UNSPECIFIED(from))
524                 dest.sin6_addr = *from;
525
526         odhcpd_send(router_event.uloop.fd,
527                         &dest, iov, ARRAY_SIZE(iov), iface);
528
529         return msecs;
530 }
531
532
533 static void trigger_router_advert(struct uloop_timeout *event)
534 {
535         struct interface *iface = container_of(event, struct interface, timer_rs);
536         int msecs = send_router_advert(iface, NULL);
537
538         // Rearm timer if not shut down
539         if (event->cb)
540                 uloop_timeout_set(event, msecs);
541 }
542
543
544 // Event handler for incoming ICMPv6 packets
545 static void handle_icmpv6(void *addr, void *data, size_t len,
546                 struct interface *iface, _unused void *dest)
547 {
548         struct icmp6_hdr *hdr = data;
549         struct sockaddr_in6 *from = addr;
550
551         if (!router_icmpv6_valid(addr, data, len))
552                 return;
553
554         if ((iface->ra == RELAYD_SERVER && !iface->master)) { // Server mode
555                 if (hdr->icmp6_type == ND_ROUTER_SOLICIT)
556                         send_router_advert(iface, &from->sin6_addr);
557         } else if (iface->ra == RELAYD_RELAY) { // Relay mode
558                 if (hdr->icmp6_type == ND_ROUTER_ADVERT && iface->master)
559                         forward_router_advertisement(data, len);
560                 else if (hdr->icmp6_type == ND_ROUTER_SOLICIT && !iface->master)
561                         forward_router_solicitation(odhcpd_get_master_interface());
562         }
563 }
564
565
566 // Forward router solicitation
567 static void forward_router_solicitation(const struct interface *iface)
568 {
569         if (!iface)
570                 return;
571
572         struct icmp6_hdr rs = {ND_ROUTER_SOLICIT, 0, 0, {{0}}};
573         struct iovec iov = {&rs, sizeof(rs)};
574         struct sockaddr_in6 all_routers =
575                 {AF_INET6, 0, 0, ALL_IPV6_ROUTERS, iface->ifindex};
576
577         syslog(LOG_NOTICE, "Sending RS to %s", iface->ifname);
578         odhcpd_send(router_event.uloop.fd, &all_routers, &iov, 1, iface);
579 }
580
581
582 // Handler for incoming router solicitations on slave interfaces
583 static void forward_router_advertisement(uint8_t *data, size_t len)
584 {
585         struct nd_router_advert *adv = (struct nd_router_advert *)data;
586
587         // Rewrite options
588         uint8_t *end = data + len;
589         uint8_t *mac_ptr = NULL;
590         struct in6_addr *dns_ptr = NULL;
591         size_t dns_count = 0;
592
593         struct icmpv6_opt *opt;
594         icmpv6_for_each_option(opt, &adv[1], end) {
595                 if (opt->type == ND_OPT_SOURCE_LINKADDR) {
596                         // Store address of source MAC-address
597                         mac_ptr = opt->data;
598                 } else if (opt->type == ND_OPT_RECURSIVE_DNS && opt->len > 1) {
599                         // Check if we have to rewrite DNS
600                         dns_ptr = (struct in6_addr*)&opt->data[6];
601                         dns_count = (opt->len - 1) / 2;
602                 }
603         }
604
605         syslog(LOG_NOTICE, "Got a RA");
606
607         // Indicate a proxy, however we don't follow the rest of RFC 4389 yet
608         adv->nd_ra_flags_reserved |= ND_RA_FLAG_PROXY;
609
610         // Forward advertisement to all slave interfaces
611         struct sockaddr_in6 all_nodes = {AF_INET6, 0, 0, ALL_IPV6_NODES, 0};
612         struct iovec iov = {data, len};
613
614         struct interface *iface;
615         list_for_each_entry(iface, &interfaces, head) {
616                 if (iface->ra != RELAYD_RELAY || iface->master)
617                         continue;
618
619                 // Fixup source hardware address option
620                 if (mac_ptr)
621                         odhcpd_get_mac(iface, mac_ptr);
622
623                 // If we have to rewrite DNS entries
624                 if (iface->always_rewrite_dns && dns_ptr && dns_count > 0) {
625                         const struct in6_addr *rewrite = iface->dns;
626                         struct in6_addr addr;
627                         size_t rewrite_cnt = iface->dns_cnt;
628
629                         if (rewrite_cnt == 0) {
630                                 if (odhcpd_get_interface_dns_addr(iface, &addr))
631                                         continue; // Unable to comply
632
633                                 rewrite = &addr;
634                                 rewrite_cnt = 1;
635                         }
636
637                         // Copy over any other addresses
638                         for (size_t i = 0; i < dns_count; ++i) {
639                                 size_t j = (i < rewrite_cnt) ? i : rewrite_cnt - 1;
640                                 dns_ptr[i] = rewrite[j];
641                         }
642                 }
643
644                 odhcpd_send(router_event.uloop.fd, &all_nodes, &iov, 1, iface);
645         }
646 }