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