ndp: code cleanup
[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 = {{.fd = -1}, handle_icmpv6, NULL};
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                 }
124
125                 if (iface->ra == RELAYD_RELAY || (iface->ra == RELAYD_SERVER && !iface->master))
126                         setsockopt(router_event.uloop.fd, IPPROTO_IPV6,
127                                         IPV6_ADD_MEMBERSHIP, mreq, sizeof(all_nodes));
128         }
129         return 0;
130 }
131
132
133 // Signal handler to resend all RDs
134 static void sigusr1_refresh(_unused int signal)
135 {
136         struct interface *iface;
137         list_for_each_entry(iface, &interfaces, head)
138                 if (iface->ra == RELAYD_SERVER && !iface->master)
139                         uloop_timeout_set(&iface->timer_rs, 1000);
140 }
141
142 static bool router_icmpv6_valid(struct sockaddr_in6 *source, uint8_t *data, size_t len)
143 {
144         struct icmp6_hdr *hdr = (struct icmp6_hdr *)data;
145         struct icmpv6_opt *opt, *end = (struct icmpv6_opt*)&data[len];
146
147         /* Hoplimit is already checked in odhcpd_receive_packets */
148         if (len < sizeof(*hdr) || hdr->icmp6_code)
149                 return false;
150
151         switch (hdr->icmp6_type) {
152         case ND_ROUTER_ADVERT:
153                 if (!IN6_IS_ADDR_LINKLOCAL(&source->sin6_addr))
154                         return false;
155
156                 opt = (struct icmpv6_opt *)((struct nd_router_advert *)data + 1);
157                 break;
158
159         case ND_ROUTER_SOLICIT:
160                 opt = (struct icmpv6_opt *)((struct nd_router_solicit *)data + 1);
161                 break;
162
163         default:
164                 return false;
165         }
166
167         icmpv6_for_each_option(opt, opt, end)
168                 if (opt->type == ND_OPT_SOURCE_LINKADDR &&
169                                 IN6_IS_ADDR_UNSPECIFIED(&source->sin6_addr) &&
170                                 hdr->icmp6_type == ND_ROUTER_SOLICIT)
171                         return false;
172
173         // Check all options parsed successfully
174         return opt == end;
175 }
176
177
178 // Detect whether a default route exists, also find the source prefixes
179 static bool parse_routes(struct odhcpd_ipaddr *n, ssize_t len)
180 {
181         rewind(fp_route);
182
183         char line[512], ifname[16];
184         bool found_default = false;
185         struct odhcpd_ipaddr p = {IN6ADDR_ANY_INIT, 0, 0, 0, 0};
186         while (fgets(line, sizeof(line), fp_route)) {
187                 uint32_t rflags;
188                 if (sscanf(line, "00000000000000000000000000000000 00 "
189                                 "%*s %*s %*s %*s %*s %*s %*s %15s", ifname) &&
190                                 strcmp(ifname, "lo")) {
191                         found_default = true;
192                 } else if (sscanf(line, "%8" SCNx32 "%8" SCNx32 "%*8" SCNx32 "%*8" SCNx32 " %hhx %*s "
193                                 "%*s 00000000000000000000000000000000 %*s %*s %*s %" SCNx32 " lo",
194                                 &p.addr.s6_addr32[0], &p.addr.s6_addr32[1], &p.prefix, &rflags) &&
195                                 p.prefix > 0 && (rflags & RTF_NONEXTHOP) && (rflags & RTF_REJECT)) {
196                         // Find source prefixes by scanning through unreachable-routes
197                         p.addr.s6_addr32[0] = htonl(p.addr.s6_addr32[0]);
198                         p.addr.s6_addr32[1] = htonl(p.addr.s6_addr32[1]);
199
200                         for (ssize_t i = 0; i < len; ++i) {
201                                 if (n[i].prefix <= 64 && n[i].prefix >= p.prefix &&
202                                                 !odhcpd_bmemcmp(&p.addr, &n[i].addr, p.prefix)) {
203                                         n[i].dprefix = p.prefix;
204                                         break;
205                                 }
206                         }
207
208                 }
209         }
210
211         return found_default;
212 }
213
214 // Router Advert server mode
215 static uint64_t send_router_advert(struct interface *iface, const struct in6_addr *from)
216 {
217         time_t now = odhcpd_time();
218         int mtu = odhcpd_get_interface_config(iface->ifname, "mtu");
219         int hlim = odhcpd_get_interface_config(iface->ifname, "hop_limit");
220
221         if (mtu < 1280)
222                 mtu = 1280;
223
224         struct {
225                 struct nd_router_advert h;
226                 struct icmpv6_opt lladdr;
227                 struct nd_opt_mtu mtu;
228                 struct nd_opt_prefix_info prefix[sizeof(iface->ia_addr) / sizeof(*iface->ia_addr)];
229         } adv = {
230                 .h = {{.icmp6_type = ND_ROUTER_ADVERT, .icmp6_code = 0}, 0, 0},
231                 .lladdr = {ND_OPT_SOURCE_LINKADDR, 1, {0}},
232                 .mtu = {ND_OPT_MTU, 1, 0, htonl(mtu)},
233         };
234
235         if (hlim > 0)
236                 adv.h.nd_ra_curhoplimit = hlim;
237
238         if (iface->dhcpv6)
239                 adv.h.nd_ra_flags_reserved = ND_RA_FLAG_OTHER;
240
241         if (iface->managed >= RELAYD_MANAGED_MFLAG)
242                 adv.h.nd_ra_flags_reserved |= ND_RA_FLAG_MANAGED;
243
244         if (iface->route_preference < 0)
245                 adv.h.nd_ra_flags_reserved |= ND_RA_PREF_LOW;
246         else if (iface->route_preference > 0)
247                 adv.h.nd_ra_flags_reserved |= ND_RA_PREF_HIGH;
248         odhcpd_get_mac(iface, adv.lladdr.data);
249
250         // If not currently shutting down
251         struct odhcpd_ipaddr addrs[RELAYD_MAX_ADDRS];
252         ssize_t ipcnt = 0;
253         int64_t minvalid = INT64_MAX;
254
255         // If not shutdown
256         if (iface->timer_rs.cb) {
257                 ipcnt = iface->ia_addr_len;
258                 memcpy(addrs, iface->ia_addr, ipcnt * sizeof(*addrs));
259
260                 // Check default route
261                 if (iface->default_router > 1)
262                         adv.h.nd_ra_router_lifetime = htons(iface->default_router);
263                 else if (parse_routes(addrs, ipcnt))
264                         adv.h.nd_ra_router_lifetime = htons(1);
265
266                 syslog(LOG_DEBUG, "Initial router lifetime %d, %d address(es) available",
267                                 ntohs(adv.h.nd_ra_router_lifetime), ipcnt);
268         }
269
270         // Construct Prefix Information options
271         size_t cnt = 0;
272
273         struct in6_addr dns_pref, *dns_addr = &dns_pref;
274         size_t dns_cnt = 1;
275
276         odhcpd_get_linklocal_interface_address(iface->ifindex, &dns_pref);
277
278         for (ssize_t i = 0; i < ipcnt; ++i) {
279                 struct odhcpd_ipaddr *addr = &addrs[i];
280
281                 if (addr->prefix > 96 || addr->valid <= (uint32_t)now) {
282                         char namebuf[INET6_ADDRSTRLEN];
283
284                         inet_ntop(AF_INET6, addr, namebuf, sizeof(namebuf));
285                         syslog(LOG_DEBUG, "Address %s (prefix %d, valid %u) not suitable",
286                                         namebuf, addr->prefix, addr->valid);
287                         continue;
288                 }
289
290                 struct nd_opt_prefix_info *p = NULL;
291                 for (size_t i = 0; i < cnt; ++i) {
292                         if (addr->prefix == adv.prefix[i].nd_opt_pi_prefix_len &&
293                                         !odhcpd_bmemcmp(&adv.prefix[i].nd_opt_pi_prefix,
294                                         &addr->addr, addr->prefix))
295                                 p = &adv.prefix[i];
296                 }
297
298                 if (!p) {
299                         if (cnt >= ARRAY_SIZE(adv.prefix))
300                                 break;
301
302                         p = &adv.prefix[cnt++];
303                 }
304
305                 if (addr->preferred > (uint32_t)now &&
306                                 minvalid > 1000LL * TIME_LEFT(addr->valid, now))
307                         minvalid = 1000LL * TIME_LEFT(addr->valid, now);
308
309                 uint32_t this_lifetime = TIME_LEFT(addr->valid, now);
310                 if (this_lifetime > UINT16_MAX)
311                         this_lifetime = UINT16_MAX;
312                 if (((addr->addr.s6_addr[0] & 0xfe) != 0xfc || iface->default_router)
313                                 && adv.h.nd_ra_router_lifetime
314                                 && ntohs(adv.h.nd_ra_router_lifetime) < this_lifetime) {
315                         adv.h.nd_ra_router_lifetime = htons(this_lifetime);
316
317                         syslog(LOG_DEBUG, "Updating router lifetime to %d", this_lifetime);
318                 }
319
320                 odhcpd_bmemcpy(&p->nd_opt_pi_prefix, &addr->addr,
321                                 (iface->ra_advrouter) ? 128 : addr->prefix);
322                 p->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
323                 p->nd_opt_pi_len = 4;
324                 p->nd_opt_pi_prefix_len = (addr->prefix < 64) ? 64 : addr->prefix;
325                 p->nd_opt_pi_flags_reserved = 0;
326                 if (!iface->ra_not_onlink)
327                         p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_ONLINK;
328                 if (iface->managed < RELAYD_MANAGED_NO_AFLAG && addr->prefix <= 64)
329                         p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_AUTO;
330                 if (iface->ra_advrouter)
331                         p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_RADDR;
332                 p->nd_opt_pi_valid_time = htonl(TIME_LEFT(addr->valid, now));
333                 if (addr->preferred > (uint32_t)now)
334                         p->nd_opt_pi_preferred_time = htonl(TIME_LEFT(addr->preferred, now));
335                 else if (addr->valid - now < 7200)
336                         p->nd_opt_pi_valid_time = 0;
337         }
338
339         if (!iface->default_router && adv.h.nd_ra_router_lifetime == htons(1)) {
340                 syslog(LOG_WARNING, "A default route is present but there is no public prefix "
341                                 "on %s thus we don't announce a default route!", iface->ifname);
342                 adv.h.nd_ra_router_lifetime = 0;
343         }
344
345         // DNS Recursive DNS
346         if (iface->dns_cnt > 0) {
347                 dns_addr = iface->dns;
348                 dns_cnt = iface->dns_cnt;
349         }
350
351         if (!dns_addr || IN6_IS_ADDR_UNSPECIFIED(dns_addr))
352                 dns_cnt = 0;
353
354         struct {
355                 uint8_t type;
356                 uint8_t len;
357                 uint8_t pad;
358                 uint8_t pad2;
359                 uint32_t lifetime;
360         } dns = {ND_OPT_RECURSIVE_DNS, (1 + (2 * dns_cnt)), 0, 0, 0};
361
362
363
364         // DNS Search options
365         uint8_t search_buf[256], *search_domain = iface->search;
366         size_t search_len = iface->search_len, search_padded = 0;
367
368         if (!search_domain && !res_init() && _res.dnsrch[0] && _res.dnsrch[0][0]) {
369                 int len = dn_comp(_res.dnsrch[0], search_buf,
370                                 sizeof(search_buf), NULL, NULL);
371                 if (len > 0) {
372                         search_domain = search_buf;
373                         search_len = len;
374                 }
375         }
376
377         if (search_len > 0)
378                 search_padded = ((search_len + 7) & (~7)) + 8;
379
380         struct {
381                 uint8_t type;
382                 uint8_t len;
383                 uint8_t pad;
384                 uint8_t pad2;
385                 uint32_t lifetime;
386                 uint8_t name[];
387         } *search = alloca(sizeof(*search) + search_padded);
388
389         search->type = ND_OPT_DNS_SEARCH;
390         search->len = search_len ? ((sizeof(*search) + search_padded) / 8) : 0;
391         search->pad = 0;
392         search->pad2 = 0;
393         memcpy(search->name, search_domain, search_len);
394         memset(&search->name[search_len], 0, search_padded - search_len);
395
396
397         size_t routes_cnt = 0;
398         struct {
399                 uint8_t type;
400                 uint8_t len;
401                 uint8_t prefix;
402                 uint8_t flags;
403                 uint32_t lifetime;
404                 uint32_t addr[4];
405         } routes[RELAYD_MAX_PREFIXES];
406
407         for (ssize_t i = 0; i < ipcnt; ++i) {
408                 struct odhcpd_ipaddr *addr = &addrs[i];
409                 if (addr->dprefix > 64 || addr->dprefix == 0 || addr->valid <= (uint32_t)now ||
410                                 (addr->dprefix == 64 && addr->prefix == 64)) {
411                         continue; // Address not suitable
412                 } else if (addr->dprefix > 32) {
413                         addr->addr.s6_addr32[1] &= htonl(~((1U << (64 - addr->dprefix)) - 1));
414                 } else if (addr->dprefix <= 32) {
415                         addr->addr.s6_addr32[0] &= htonl(~((1U << (32 - addr->dprefix)) - 1));
416                         addr->addr.s6_addr32[1] = 0;
417                 }
418
419                 routes[routes_cnt].type = ND_OPT_ROUTE_INFO;
420                 routes[routes_cnt].len = sizeof(*routes) / 8;
421                 routes[routes_cnt].prefix = addr->dprefix;
422                 routes[routes_cnt].flags = 0;
423                 if (iface->route_preference < 0)
424                         routes[routes_cnt].flags |= ND_RA_PREF_LOW;
425                 else if (iface->route_preference > 0)
426                         routes[routes_cnt].flags |= ND_RA_PREF_HIGH;
427                 routes[routes_cnt].lifetime = htonl(TIME_LEFT(addr->valid, now));
428                 routes[routes_cnt].addr[0] = addr->addr.s6_addr32[0];
429                 routes[routes_cnt].addr[1] = addr->addr.s6_addr32[1];
430                 routes[routes_cnt].addr[2] = 0;
431                 routes[routes_cnt].addr[3] = 0;
432
433                 ++routes_cnt;
434         }
435
436         // Calculate periodic transmit
437         int msecs = 0;
438         uint32_t maxival = iface->ra_maxinterval * 1000;
439         uint32_t minival;
440
441         if (maxival < 4000 || maxival > MaxRtrAdvInterval * 1000)
442                 maxival = MaxRtrAdvInterval * 1000;
443
444         if (maxival > minvalid / 3) {
445                 maxival = minvalid / 3;
446
447                 if (maxival < 4000)
448                         maxival = 4000;
449         }
450
451         minival = (maxival * 3) / 4;
452
453         search->lifetime = htonl(maxival / 100);
454         dns.lifetime = search->lifetime;
455
456         odhcpd_urandom(&msecs, sizeof(msecs));
457         msecs = (labs(msecs) % (maxival - minival)) + minival;
458
459         struct icmpv6_opt adv_interval = {
460                 .type = ND_OPT_RTR_ADV_INTERVAL,
461                 .len = 1,
462                 .data = {0, 0, maxival >> 24, maxival >> 16, maxival >> 8, maxival}
463         };
464
465         struct iovec iov[RA_IOV_LEN] = {
466                         {&adv, (uint8_t*)&adv.prefix[cnt] - (uint8_t*)&adv},
467                         {&routes, routes_cnt * sizeof(*routes)},
468                         {&dns, (dns_cnt) ? sizeof(dns) : 0},
469                         {dns_addr, dns_cnt * sizeof(*dns_addr)},
470                         {search, search->len * 8},
471                         {&adv_interval, adv_interval.len * 8}};
472         struct sockaddr_in6 dest = {AF_INET6, 0, 0, ALL_IPV6_NODES, 0};
473
474         if (from && !IN6_IS_ADDR_UNSPECIFIED(from))
475                 dest.sin6_addr = *from;
476
477         odhcpd_send(router_event.uloop.fd,
478                         &dest, iov, ARRAY_SIZE(iov), iface);
479
480         return msecs;
481 }
482
483
484 static void trigger_router_advert(struct uloop_timeout *event)
485 {
486         struct interface *iface = container_of(event, struct interface, timer_rs);
487         int msecs = send_router_advert(iface, NULL);
488
489         // Rearm timer if not shut down
490         if (event->cb)
491                 uloop_timeout_set(event, msecs);
492 }
493
494
495 // Event handler for incoming ICMPv6 packets
496 static void handle_icmpv6(void *addr, void *data, size_t len,
497                 struct interface *iface, _unused void *dest)
498 {
499         struct icmp6_hdr *hdr = data;
500         struct sockaddr_in6 *from = addr;
501
502         if (!router_icmpv6_valid(addr, data, len))
503                 return;
504
505         if ((iface->ra == RELAYD_SERVER && !iface->master)) { // Server mode
506                 if (hdr->icmp6_type == ND_ROUTER_SOLICIT)
507                         send_router_advert(iface, &from->sin6_addr);
508         } else if (iface->ra == RELAYD_RELAY) { // Relay mode
509                 if (hdr->icmp6_type == ND_ROUTER_ADVERT && iface->master)
510                         forward_router_advertisement(data, len);
511                 else if (hdr->icmp6_type == ND_ROUTER_SOLICIT && !iface->master)
512                         forward_router_solicitation(odhcpd_get_master_interface());
513         }
514 }
515
516
517 // Forward router solicitation
518 static void forward_router_solicitation(const struct interface *iface)
519 {
520         if (!iface)
521                 return;
522
523         struct icmp6_hdr rs = {ND_ROUTER_SOLICIT, 0, 0, {{0}}};
524         struct iovec iov = {&rs, sizeof(rs)};
525         struct sockaddr_in6 all_routers =
526                 {AF_INET6, 0, 0, ALL_IPV6_ROUTERS, iface->ifindex};
527
528         syslog(LOG_NOTICE, "Sending RS to %s", iface->ifname);
529         odhcpd_send(router_event.uloop.fd, &all_routers, &iov, 1, iface);
530 }
531
532
533 // Handler for incoming router solicitations on slave interfaces
534 static void forward_router_advertisement(uint8_t *data, size_t len)
535 {
536         struct nd_router_advert *adv = (struct nd_router_advert *)data;
537
538         // Rewrite options
539         uint8_t *end = data + len;
540         uint8_t *mac_ptr = NULL;
541         struct in6_addr *dns_ptr = NULL;
542         size_t dns_count = 0;
543
544         struct icmpv6_opt *opt;
545         icmpv6_for_each_option(opt, &adv[1], end) {
546                 if (opt->type == ND_OPT_SOURCE_LINKADDR) {
547                         // Store address of source MAC-address
548                         mac_ptr = opt->data;
549                 } else if (opt->type == ND_OPT_RECURSIVE_DNS && opt->len > 1) {
550                         // Check if we have to rewrite DNS
551                         dns_ptr = (struct in6_addr*)&opt->data[6];
552                         dns_count = (opt->len - 1) / 2;
553                 }
554         }
555
556         syslog(LOG_NOTICE, "Got a RA");
557
558         // Indicate a proxy, however we don't follow the rest of RFC 4389 yet
559         adv->nd_ra_flags_reserved |= ND_RA_FLAG_PROXY;
560
561         // Forward advertisement to all slave interfaces
562         struct sockaddr_in6 all_nodes = {AF_INET6, 0, 0, ALL_IPV6_NODES, 0};
563         struct iovec iov = {data, len};
564
565         struct interface *iface;
566         list_for_each_entry(iface, &interfaces, head) {
567                 if (iface->ra != RELAYD_RELAY || iface->master)
568                         continue;
569
570                 // Fixup source hardware address option
571                 if (mac_ptr)
572                         odhcpd_get_mac(iface, mac_ptr);
573
574                 // If we have to rewrite DNS entries
575                 if (iface->always_rewrite_dns && dns_ptr && dns_count > 0) {
576                         const struct in6_addr *rewrite = iface->dns;
577                         struct in6_addr addr;
578                         size_t rewrite_cnt = iface->dns_cnt;
579
580                         if (rewrite_cnt == 0) {
581                                 if (odhcpd_get_linklocal_interface_address(iface->ifindex, &addr))
582                                         continue; // Unable to comply
583
584                                 rewrite = &addr;
585                                 rewrite_cnt = 1;
586                         }
587
588                         // Copy over any other addresses
589                         for (size_t i = 0; i < dns_count; ++i) {
590                                 size_t j = (i < rewrite_cnt) ? i : rewrite_cnt - 1;
591                                 dns_ptr[i] = rewrite[j];
592                         }
593                 }
594
595                 odhcpd_send(router_event.uloop.fd, &all_nodes, &iov, 1, iface);
596         }
597 }