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