dhcpv6: rapid commit support
[oweals/odhcpd.git] / src / dhcpv6.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
17 #include <errno.h>
18 #include <unistd.h>
19 #include <stddef.h>
20 #include <resolv.h>
21 #include <sys/timerfd.h>
22 #include <arpa/inet.h>
23
24 #include <libubox/utils.h>
25
26 #include "odhcpd.h"
27 #include "dhcpv6.h"
28
29
30 static void relay_client_request(struct sockaddr_in6 *source,
31                 const void *data, size_t len, struct interface *iface);
32 static void relay_server_response(uint8_t *data, size_t len);
33
34 static void handle_dhcpv6(void *addr, void *data, size_t len,
35                 struct interface *iface, void *dest);
36 static void handle_client_request(void *addr, void *data, size_t len,
37                 struct interface *iface, void *dest_addr);
38
39
40 /* Create socket and register events */
41 int dhcpv6_init(void)
42 {
43         return dhcpv6_ia_init();
44 }
45
46 int dhcpv6_setup_interface(struct interface *iface, bool enable)
47 {
48         int ret = 0;
49
50         if (iface->dhcpv6_event.uloop.fd > 0) {
51                 uloop_fd_delete(&iface->dhcpv6_event.uloop);
52                 close(iface->dhcpv6_event.uloop.fd);
53                 iface->dhcpv6_event.uloop.fd = -1;
54         }
55
56         /* Configure multicast settings */
57         if (enable && iface->dhcpv6) {
58                 struct sockaddr_in6 bind_addr = {AF_INET6, htons(DHCPV6_SERVER_PORT),
59                                         0, IN6ADDR_ANY_INIT, 0};
60                 struct ipv6_mreq mreq;
61                 int val = 1;
62
63                 iface->dhcpv6_event.uloop.fd = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
64                 if (iface->dhcpv6_event.uloop.fd < 0) {
65                         syslog(LOG_ERR, "socket(AF_INET6): %m");
66                         ret = -1;
67                         goto out;
68                 }
69
70                 /* Basic IPv6 configuration */
71                 if (setsockopt(iface->dhcpv6_event.uloop.fd, SOL_SOCKET, SO_BINDTODEVICE,
72                                         iface->ifname, strlen(iface->ifname)) < 0) {
73                         syslog(LOG_ERR, "setsockopt(SO_BINDTODEVICE): %m");
74                         ret = -1;
75                         goto out;
76                 }
77
78                 if (setsockopt(iface->dhcpv6_event.uloop.fd, IPPROTO_IPV6, IPV6_V6ONLY,
79                                         &val, sizeof(val)) < 0) {
80                         syslog(LOG_ERR, "setsockopt(IPV6_V6ONLY): %m");
81                         ret = -1;
82                         goto out;
83                 }
84
85                 if (setsockopt(iface->dhcpv6_event.uloop.fd, SOL_SOCKET, SO_REUSEADDR,
86                                         &val, sizeof(val)) < 0) {
87                         syslog(LOG_ERR, "setsockopt(SO_REUSEADDR): %m");
88                         ret = -1;
89                         goto out;
90                 }
91
92                 if (setsockopt(iface->dhcpv6_event.uloop.fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
93                                         &val, sizeof(val)) < 0) {
94                         syslog(LOG_ERR, "setsockopt(IPV6_RECVPKTINFO): %m");
95                         ret = -1;
96                         goto out;
97                 }
98
99                 val = DHCPV6_HOP_COUNT_LIMIT;
100                 if (setsockopt(iface->dhcpv6_event.uloop.fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
101                                         &val, sizeof(val)) < 0) {
102                         syslog(LOG_ERR, "setsockopt(IPV6_MULTICAST_HOPS): %m");
103                         ret = -1;
104                         goto out;
105                 }
106
107                 val = 0;
108                 if (setsockopt(iface->dhcpv6_event.uloop.fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
109                                         &val, sizeof(val)) < 0) {
110                         syslog(LOG_ERR, "setsockopt(IPV6_MULTICAST_LOOP): %m");
111                         ret = -1;
112                         goto out;
113                 }
114
115                 if (bind(iface->dhcpv6_event.uloop.fd, (struct sockaddr*)&bind_addr,
116                                         sizeof(bind_addr)) < 0) {
117                         syslog(LOG_ERR, "bind(): %m");
118                         ret = -1;
119                         goto out;
120                 }
121
122                 memset(&mreq, 0, sizeof(mreq));
123                 inet_pton(AF_INET6, ALL_DHCPV6_RELAYS, &mreq.ipv6mr_multiaddr);
124                 mreq.ipv6mr_interface = iface->ifindex;
125
126                 if (setsockopt(iface->dhcpv6_event.uloop.fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
127                                         &mreq, sizeof(mreq)) < 0) {
128                         syslog(LOG_ERR, "setsockopt(IPV6_ADD_MEMBERSHIP): %m");
129                         ret = -1;
130                         goto out;
131                 }
132
133                 if (iface->dhcpv6 == MODE_SERVER) {
134                         memset(&mreq, 0, sizeof(mreq));
135                         inet_pton(AF_INET6, ALL_DHCPV6_SERVERS, &mreq.ipv6mr_multiaddr);
136                         mreq.ipv6mr_interface = iface->ifindex;
137
138                         if (setsockopt(iface->dhcpv6_event.uloop.fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
139                                                 &mreq, sizeof(mreq)) < 0) {
140                                 syslog(LOG_ERR, "setsockopt(IPV6_ADD_MEMBERSHIP): %m");
141                                 ret = -1;
142                                 goto out;
143                         }
144                 }
145
146                 iface->dhcpv6_event.handle_dgram = handle_dhcpv6;
147                 odhcpd_register(&iface->dhcpv6_event);
148         }
149
150         ret = dhcpv6_ia_setup_interface(iface, enable);
151
152 out:
153         if (ret < 0 && iface->dhcpv6_event.uloop.fd > 0) {
154                 close(iface->dhcpv6_event.uloop.fd);
155                 iface->dhcpv6_event.uloop.fd = -1;
156         }
157
158         return ret;
159 }
160
161 enum {
162         IOV_NESTED = 0,
163         IOV_DEST,
164         IOV_MAXRT,
165 #define IOV_STAT IOV_MAXRT
166         IOV_RAPID_COMMIT,
167         IOV_DNS,
168         IOV_DNS_ADDR,
169         IOV_SEARCH,
170         IOV_SEARCH_DOMAIN,
171         IOV_PDBUF,
172 #define IOV_REFRESH IOV_PDBUF
173         IOV_CERID,
174         IOV_DHCPV6_RAW,
175         IOV_RELAY_MSG,
176         IOV_TOTAL
177 };
178
179 static void handle_nested_message(uint8_t *data, size_t len,
180                                   struct dhcpv6_client_header **c_hdr, uint8_t **opts,
181                                   uint8_t **end, struct iovec iov[IOV_TOTAL])
182 {
183         struct dhcpv6_relay_header *r_hdr = (struct dhcpv6_relay_header *)data;
184         uint16_t otype, olen;
185         uint8_t *odata;
186
187         if (iov[IOV_NESTED].iov_base == NULL) {
188                 iov[IOV_NESTED].iov_base = data;
189                 iov[IOV_NESTED].iov_len = len;
190         }
191
192         if (len < sizeof(struct dhcpv6_client_header))
193                 return;
194
195         if (r_hdr->msg_type != DHCPV6_MSG_RELAY_FORW) {
196                 iov[IOV_NESTED].iov_len = data - (uint8_t *)iov[IOV_NESTED].iov_base;
197                 *c_hdr = (void *)data;
198                 *opts = (uint8_t *)&(*c_hdr)[1];
199                 *end = data + len;
200                 return;
201         }
202
203         dhcpv6_for_each_option(r_hdr->options, data + len, otype, olen, odata) {
204                 if (otype == DHCPV6_OPT_RELAY_MSG) {
205                         iov[IOV_RELAY_MSG].iov_base = odata + olen;
206                         iov[IOV_RELAY_MSG].iov_len = (((uint8_t *)iov[IOV_NESTED].iov_base) +
207                                         iov[IOV_NESTED].iov_len) - (odata + olen);
208                         handle_nested_message(odata, olen, c_hdr, opts, end, iov);
209                         return;
210                 }
211         }
212 }
213
214
215 static void update_nested_message(uint8_t *data, size_t len, ssize_t pdiff)
216 {
217         struct dhcpv6_relay_header *hdr = (struct dhcpv6_relay_header*)data;
218         if (hdr->msg_type != DHCPV6_MSG_RELAY_FORW)
219                 return;
220
221         hdr->msg_type = DHCPV6_MSG_RELAY_REPL;
222
223         uint16_t otype, olen;
224         uint8_t *odata;
225         dhcpv6_for_each_option(hdr->options, data + len, otype, olen, odata) {
226                 if (otype == DHCPV6_OPT_RELAY_MSG) {
227                         olen += pdiff;
228                         odata[-2] = (olen >> 8) & 0xff;
229                         odata[-1] = olen & 0xff;
230                         update_nested_message(odata, olen - pdiff, pdiff);
231                         return;
232                 }
233         }
234 }
235
236 /* Simple DHCPv6-server for information requests */
237 static void handle_client_request(void *addr, void *data, size_t len,
238                 struct interface *iface, void *dest_addr)
239 {
240         struct dhcpv6_client_header *hdr = data;
241         uint8_t *opts = (uint8_t *)&hdr[1], *opts_end = (uint8_t *)data + len;
242         bool o_rapid_commit = false;
243
244         if (len < sizeof(*hdr))
245                 return;
246
247         syslog(LOG_NOTICE, "Got DHCPv6 request on %s", iface->name);
248
249         /* Construct reply message */
250         struct __attribute__((packed)) {
251                 uint8_t msg_type;
252                 uint8_t tr_id[3];
253                 uint16_t serverid_type;
254                 uint16_t serverid_length;
255                 uint16_t duid_type;
256                 uint16_t hardware_type;
257                 uint8_t mac[6];
258                 uint16_t clientid_type;
259                 uint16_t clientid_length;
260                 uint8_t clientid_buf[130];
261         } dest = {
262                 .msg_type = DHCPV6_MSG_REPLY,
263                 .serverid_type = htons(DHCPV6_OPT_SERVERID),
264                 .serverid_length = htons(10),
265                 .duid_type = htons(3),
266                 .hardware_type = htons(1),
267                 .clientid_type = htons(DHCPV6_OPT_CLIENTID),
268                 .clientid_buf = {0}
269         };
270         odhcpd_get_mac(iface, dest.mac);
271
272         struct __attribute__((packed)) {
273                 uint16_t type;
274                 uint16_t len;
275                 uint32_t value;
276         } maxrt = {htons(DHCPV6_OPT_SOL_MAX_RT), htons(sizeof(maxrt) - 4),
277                         htonl(60)};
278
279         struct __attribute__((packed)) {
280                 uint16_t type;
281                 uint16_t len;
282         } rapid_commit = {htons(DHCPV6_OPT_RAPID_COMMIT), 0};
283
284         struct __attribute__((packed)) {
285                 uint16_t type;
286                 uint16_t len;
287                 uint16_t value;
288         } stat = {htons(DHCPV6_OPT_STATUS), htons(sizeof(stat) - 4),
289                         htons(DHCPV6_STATUS_USEMULTICAST)};
290
291         struct __attribute__((packed)) {
292                 uint16_t type;
293                 uint16_t len;
294                 uint32_t value;
295         } refresh = {htons(DHCPV6_OPT_INFO_REFRESH), htons(sizeof(uint32_t)),
296                         htonl(600)};
297
298         struct in6_addr dns_addr, *dns_addr_ptr = iface->dns;
299         size_t dns_cnt = iface->dns_cnt;
300
301         if ((dns_cnt == 0) &&
302                 !odhcpd_get_interface_dns_addr(iface, &dns_addr)) {
303                 dns_addr_ptr = &dns_addr;
304                 dns_cnt = 1;
305         }
306
307         struct {
308                 uint16_t type;
309                 uint16_t len;
310         } dns = {htons(DHCPV6_OPT_DNS_SERVERS), htons(dns_cnt * sizeof(*dns_addr_ptr))};
311
312
313
314         /* DNS Search options */
315         uint8_t search_buf[256], *search_domain = iface->search;
316         size_t search_len = iface->search_len;
317
318         if (!search_domain && !res_init() && _res.dnsrch[0] && _res.dnsrch[0][0]) {
319                 int len = dn_comp(_res.dnsrch[0], search_buf,
320                                 sizeof(search_buf), NULL, NULL);
321                 if (len > 0) {
322                         search_domain = search_buf;
323                         search_len = len;
324                 }
325         }
326
327         struct {
328                 uint16_t type;
329                 uint16_t len;
330         } search = {htons(DHCPV6_OPT_DNS_DOMAIN), htons(search_len)};
331
332
333         struct dhcpv6_cer_id cerid = {
334 #ifdef EXT_CER_ID
335                 .type = htons(EXT_CER_ID),
336 #endif
337                 .len = htons(36),
338                 .addr = iface->dhcpv6_pd_cer,
339         };
340
341
342         uint8_t pdbuf[512];
343         struct iovec iov[IOV_TOTAL] = {
344                 [IOV_NESTED] = {NULL, 0},
345                 [IOV_DEST] = {&dest, (uint8_t*)&dest.clientid_type - (uint8_t*)&dest},
346                 [IOV_MAXRT] = {&maxrt, sizeof(maxrt)},
347                 [IOV_RAPID_COMMIT] = {&rapid_commit, 0},
348                 [IOV_DNS] = {&dns, (dns_cnt) ? sizeof(dns) : 0},
349                 [IOV_DNS_ADDR] = {dns_addr_ptr, dns_cnt * sizeof(*dns_addr_ptr)},
350                 [IOV_SEARCH] = {&search, (search_len) ? sizeof(search) : 0},
351                 [IOV_SEARCH_DOMAIN] = {search_domain, search_len},
352                 [IOV_PDBUF] = {pdbuf, 0},
353                 [IOV_CERID] = {&cerid, 0},
354                 [IOV_DHCPV6_RAW] = {iface->dhcpv6_raw, iface->dhcpv6_raw_len},
355                 [IOV_RELAY_MSG] = {NULL, 0}
356         };
357
358         if (hdr->msg_type == DHCPV6_MSG_RELAY_FORW)
359                 handle_nested_message(data, len, &hdr, &opts, &opts_end, iov);
360
361         if (hdr->msg_type == DHCPV6_MSG_ADVERTISE || hdr->msg_type == DHCPV6_MSG_REPLY ||
362             hdr->msg_type == DHCPV6_MSG_RELAY_REPL)
363                 return;
364
365         if (!IN6_IS_ADDR_MULTICAST((struct in6_addr *)dest_addr) && iov[IOV_NESTED].iov_len == 0 &&
366             (hdr->msg_type == DHCPV6_MSG_SOLICIT || hdr->msg_type == DHCPV6_MSG_CONFIRM ||
367              hdr->msg_type == DHCPV6_MSG_REBIND || hdr->msg_type == DHCPV6_MSG_INFORMATION_REQUEST))
368                 return;
369
370         memcpy(dest.tr_id, hdr->transaction_id, sizeof(dest.tr_id));
371
372         /* Go through options and find what we need */
373         uint16_t otype, olen;
374         uint8_t *odata;
375         dhcpv6_for_each_option(opts, opts_end, otype, olen, odata) {
376                 if (otype == DHCPV6_OPT_CLIENTID && olen <= 130) {
377                         dest.clientid_length = htons(olen);
378                         memcpy(dest.clientid_buf, odata, olen);
379                         iov[IOV_DEST].iov_len += 4 + olen;
380                 } else if (otype == DHCPV6_OPT_SERVERID) {
381                         if (olen != ntohs(dest.serverid_length) ||
382                                         memcmp(odata, &dest.duid_type, olen))
383                                 return; /* Not for us */
384                 } else if (iface->filter_class && otype == DHCPV6_OPT_USER_CLASS) {
385                         uint8_t *c = odata, *cend = &odata[olen];
386                         for (; &c[2] <= cend && &c[2 + (c[0] << 8) + c[1]] <= cend; c = &c[2 + (c[0] << 8) + c[1]]) {
387                                 size_t elen = strlen(iface->filter_class);
388                                 if (((((size_t)c[0]) << 8) | c[1]) == elen && !memcmp(&c[2], iface->filter_class, elen))
389                                         return; /* Ignore from homenet */
390                         }
391                 } else if (otype == DHCPV6_OPT_IA_PD) {
392 #ifdef EXT_CER_ID
393                         iov[IOV_CERID].iov_len = sizeof(cerid);
394
395                         if (IN6_IS_ADDR_UNSPECIFIED(&cerid.addr)) {
396                                 struct odhcpd_ipaddr *addrs;
397                                 ssize_t len = netlink_get_interface_addrs(0, true, &addrs);
398
399                                 for (ssize_t i = 0; i < len; ++i)
400                                         if (IN6_IS_ADDR_UNSPECIFIED(&cerid.addr)
401                                                         || memcmp(&addrs[i].addr, &cerid.addr, sizeof(cerid.addr)) < 0)
402                                                 cerid.addr = addrs[i].addr.in6;
403
404                                 free(addrs);
405                         }
406 #endif
407                 } else if (otype == DHCPV6_OPT_RAPID_COMMIT && hdr->msg_type == DHCPV6_MSG_SOLICIT) {
408                         iov[IOV_RAPID_COMMIT].iov_len = sizeof(rapid_commit);
409                         o_rapid_commit = true;
410                 }
411         }
412
413         if (!IN6_IS_ADDR_MULTICAST((struct in6_addr *)dest_addr) && iov[IOV_NESTED].iov_len == 0 &&
414             (hdr->msg_type == DHCPV6_MSG_REQUEST || hdr->msg_type == DHCPV6_MSG_RENEW ||
415              hdr->msg_type == DHCPV6_MSG_RELEASE || hdr->msg_type == DHCPV6_MSG_DECLINE)) {
416                 iov[IOV_STAT].iov_base = &stat;
417                 iov[IOV_STAT].iov_len = sizeof(stat);
418
419                 for (ssize_t i = IOV_STAT + 1; i < IOV_TOTAL; ++i)
420                         iov[i].iov_len = 0;
421
422                 odhcpd_send(iface->dhcpv6_event.uloop.fd, addr, iov, ARRAY_SIZE(iov), iface);
423                 return;
424         }
425
426         if (hdr->msg_type == DHCPV6_MSG_SOLICIT && !o_rapid_commit) {
427                 dest.msg_type = DHCPV6_MSG_ADVERTISE;
428         } else if (hdr->msg_type == DHCPV6_MSG_INFORMATION_REQUEST) {
429                 iov[IOV_REFRESH].iov_base = &refresh;
430                 iov[IOV_REFRESH].iov_len = sizeof(refresh);
431
432                 /* Return inf max rt option in reply to information request */
433                 maxrt.type = htons(DHCPV6_OPT_INF_MAX_RT);
434         }
435
436         if (hdr->msg_type != DHCPV6_MSG_INFORMATION_REQUEST) {
437                 ssize_t ialen = dhcpv6_ia_handle_IAs(pdbuf, sizeof(pdbuf), iface, addr, (const void *)hdr, opts_end);
438
439                 iov[IOV_PDBUF].iov_len = ialen;
440                 if (ialen < 0 ||
441                     (ialen == 0 && (hdr->msg_type == DHCPV6_MSG_REBIND || hdr->msg_type == DHCPV6_MSG_CONFIRM)))
442                         return;
443         }
444
445         if (iov[IOV_NESTED].iov_len > 0) /* Update length */
446                 update_nested_message(data, len, iov[IOV_DEST].iov_len + iov[IOV_MAXRT].iov_len +
447                                       iov[IOV_RAPID_COMMIT].iov_len + iov[IOV_DNS].iov_len +
448                                       iov[IOV_DNS_ADDR].iov_len + iov[IOV_SEARCH].iov_len +
449                                       iov[IOV_SEARCH_DOMAIN].iov_len + iov[IOV_PDBUF].iov_len +
450                                       iov[IOV_CERID].iov_len + iov[IOV_DHCPV6_RAW].iov_len -
451                                       (4 + opts_end - opts));
452
453         odhcpd_send(iface->dhcpv6_event.uloop.fd, addr, iov, ARRAY_SIZE(iov), iface);
454 }
455
456
457 /* Central DHCPv6-relay handler */
458 static void handle_dhcpv6(void *addr, void *data, size_t len,
459                 struct interface *iface, void *dest_addr)
460 {
461         if (iface->dhcpv6 == MODE_SERVER) {
462                 handle_client_request(addr, data, len, iface, dest_addr);
463         } else if (iface->dhcpv6 == MODE_RELAY) {
464                 if (iface->master)
465                         relay_server_response(data, len);
466                 else
467                         relay_client_request(addr, data, len, iface);
468         }
469 }
470
471
472 /* Relay server response (regular relay server handling) */
473 static void relay_server_response(uint8_t *data, size_t len)
474 {
475         /* Information we need to gather */
476         uint8_t *payload_data = NULL;
477         size_t payload_len = 0;
478         int32_t ifaceidx = 0;
479         struct sockaddr_in6 target = {AF_INET6, htons(DHCPV6_CLIENT_PORT),
480                 0, IN6ADDR_ANY_INIT, 0};
481
482         syslog(LOG_NOTICE, "Got a DHCPv6-reply");
483
484         int otype, olen;
485         uint8_t *odata, *end = data + len;
486
487         /* Relay DHCPv6 reply from server to client */
488         struct dhcpv6_relay_header *h = (void*)data;
489         if (len < sizeof(*h) || h->msg_type != DHCPV6_MSG_RELAY_REPL)
490                 return;
491
492         memcpy(&target.sin6_addr, &h->peer_address,
493                         sizeof(struct in6_addr));
494
495         /* Go through options and find what we need */
496         dhcpv6_for_each_option(h->options, end, otype, olen, odata) {
497                 if (otype == DHCPV6_OPT_INTERFACE_ID
498                                 && olen == sizeof(ifaceidx)) {
499                         memcpy(&ifaceidx, odata, sizeof(ifaceidx));
500                 } else if (otype == DHCPV6_OPT_RELAY_MSG) {
501                         payload_data = odata;
502                         payload_len = olen;
503                 }
504         }
505
506         /* Invalid interface-id or basic payload */
507         struct interface *iface = odhcpd_get_interface_by_index(ifaceidx);
508         if (!iface || iface->master || !payload_data || payload_len < 4)
509                 return;
510
511         bool is_authenticated = false;
512         struct in6_addr *dns_ptr = NULL;
513         size_t dns_count = 0;
514
515         /* If the payload is relay-reply we have to send to the server port */
516         if (payload_data[0] == DHCPV6_MSG_RELAY_REPL) {
517                 target.sin6_port = htons(DHCPV6_SERVER_PORT);
518         } else { /* Go through the payload data */
519                 struct dhcpv6_client_header *h = (void*)payload_data;
520                 end = payload_data + payload_len;
521
522                 dhcpv6_for_each_option(&h[1], end, otype, olen, odata) {
523                         if (otype == DHCPV6_OPT_DNS_SERVERS && olen >= 16) {
524                                 dns_ptr = (struct in6_addr*)odata;
525                                 dns_count = olen / 16;
526                         } else if (otype == DHCPV6_OPT_AUTH) {
527                                 is_authenticated = true;
528                         }
529                 }
530         }
531
532         /* Rewrite DNS servers if requested */
533         if (iface->always_rewrite_dns && dns_ptr && dns_count > 0) {
534                 if (is_authenticated)
535                         return; /* Impossible to rewrite */
536
537                 const struct in6_addr *rewrite = iface->dns;
538                 struct in6_addr addr;
539                 size_t rewrite_cnt = iface->dns_cnt;
540
541                 if (rewrite_cnt == 0) {
542                         if (odhcpd_get_interface_dns_addr(iface, &addr))
543                                 return; // Unable to get interface address
544
545                         rewrite = &addr;
546                         rewrite_cnt = 1;
547                 }
548
549                 /* Copy over any other addresses */
550                 for (size_t i = 0; i < dns_count; ++i) {
551                         size_t j = (i < rewrite_cnt) ? i : rewrite_cnt - 1;
552                         memcpy(&dns_ptr[i], &rewrite[j], sizeof(*rewrite));
553                 }
554         }
555
556         struct iovec iov = {payload_data, payload_len};
557         odhcpd_send(iface->dhcpv6_event.uloop.fd, &target, &iov, 1, iface);
558 }
559
560 static struct odhcpd_ipaddr *relay_link_address(struct interface *iface)
561 {
562         struct odhcpd_ipaddr *addr = NULL;
563         time_t now = odhcpd_time();
564
565         for (size_t i = 0; i < iface->addr6_len; i++) {
566                 if (iface->addr6[i].valid <= (uint32_t)now)
567                         continue;
568
569                 if (iface->addr6[i].preferred > (uint32_t)now) {
570                         addr = &iface->addr6[i];
571                         break;
572                 }
573
574                 if (!addr || (iface->addr6[i].valid > addr->valid))
575                         addr = &iface->addr6[i];
576         }
577
578         return addr;
579 }
580
581 /* Relay client request (regular DHCPv6-relay) */
582 static void relay_client_request(struct sockaddr_in6 *source,
583                 const void *data, size_t len, struct interface *iface)
584 {
585         struct interface *master = odhcpd_get_master_interface();
586         const struct dhcpv6_relay_header *h = data;
587         struct sockaddr_in6 s;
588
589         if (!master || master->dhcpv6 != MODE_RELAY ||
590                         h->msg_type == DHCPV6_MSG_RELAY_REPL ||
591                         h->msg_type == DHCPV6_MSG_RECONFIGURE ||
592                         h->msg_type == DHCPV6_MSG_REPLY ||
593                         h->msg_type == DHCPV6_MSG_ADVERTISE)
594                 return; /* Invalid message types for client */
595
596         syslog(LOG_NOTICE, "Got a DHCPv6-request");
597
598         /* Construct our forwarding envelope */
599         struct dhcpv6_relay_forward_envelope hdr = {
600                 .msg_type = DHCPV6_MSG_RELAY_FORW,
601                 .hop_count = 0,
602                 .interface_id_type = htons(DHCPV6_OPT_INTERFACE_ID),
603                 .interface_id_len = htons(sizeof(uint32_t)),
604                 .relay_message_type = htons(DHCPV6_OPT_RELAY_MSG),
605                 .relay_message_len = htons(len),
606         };
607
608         if (h->msg_type == DHCPV6_MSG_RELAY_FORW) { /* handle relay-forward */
609                 if (h->hop_count >= DHCPV6_HOP_COUNT_LIMIT)
610                         return; // Invalid hop count
611                 else
612                         hdr.hop_count = h->hop_count + 1;
613         }
614
615         /* use memcpy here as the destination fields are unaligned */
616         uint32_t ifindex = iface->ifindex;
617         memcpy(&hdr.peer_address, &source->sin6_addr, sizeof(struct in6_addr));
618         memcpy(&hdr.interface_id_data, &ifindex, sizeof(ifindex));
619
620         /* Detect public IP of slave interface to use as link-address */
621         struct odhcpd_ipaddr *ip = relay_link_address(iface);
622         if (!ip) {
623                 /* No suitable address! Is the slave not configured yet?
624                  * Detect public IP of master interface and use it instead
625                  * This is WRONG and probably violates the RFC. However
626                  * otherwise we have a hen and egg problem because the
627                  * slave-interface cannot be auto-configured. */
628                 ip = relay_link_address(master);
629                 if (!ip)
630                         return; /* Could not obtain a suitable address */
631         }
632
633         memcpy(&hdr.link_address, &ip->addr.in6, sizeof(hdr.link_address));
634
635         memset(&s, 0, sizeof(s));
636         s.sin6_family = AF_INET6;
637         s.sin6_port = htons(DHCPV6_SERVER_PORT);
638         inet_pton(AF_INET6, ALL_DHCPV6_SERVERS, &s.sin6_addr);
639
640         struct iovec iov[2] = {{&hdr, sizeof(hdr)}, {(void*)data, len}};
641         odhcpd_send(master->dhcpv6_event.uloop.fd, &s, iov, 2, master);
642 }