2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
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.
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.
20 #include <sys/timerfd.h>
21 #include <arpa/inet.h>
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);
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);
37 /* Create socket and register events */
44 int dhcpv6_setup_interface(struct interface *iface, bool enable)
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;
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;
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");
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");
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");
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");
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");
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");
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");
113 if (bind(iface->dhcpv6_event.uloop.fd, (struct sockaddr*)&bind_addr,
114 sizeof(bind_addr)) < 0) {
115 syslog(LOG_ERR, "bind(): %m");
120 memset(&mreq, 0, sizeof(mreq));
121 inet_pton(AF_INET6, ALL_DHCPV6_RELAYS, &mreq.ipv6mr_multiaddr);
122 mreq.ipv6mr_interface = iface->ifindex;
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");
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;
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");
144 iface->dhcpv6_event.handle_dgram = handle_dhcpv6;
145 odhcpd_register(&iface->dhcpv6_event);
148 ret = dhcpv6_setup_ia_interface(iface, enable);
151 if (ret < 0 && iface->dhcpv6_event.uloop.fd > 0) {
152 close(iface->dhcpv6_event.uloop.fd);
153 iface->dhcpv6_event.uloop.fd = -1;
163 #define IOV_STAT IOV_MAXRT
169 #define IOV_REFRESH IOV_PDBUF
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])
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;
185 if (len < sizeof(struct dhcpv6_client_header))
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];
196 uint16_t otype, olen;
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);
210 static void update_nested_message(uint8_t *data, size_t len, ssize_t pdiff)
212 struct dhcpv6_relay_header *hdr = (struct dhcpv6_relay_header*)data;
213 if (hdr->msg_type != DHCPV6_MSG_RELAY_FORW)
216 hdr->msg_type = DHCPV6_MSG_RELAY_REPL;
218 uint16_t otype, olen;
220 dhcpv6_for_each_option(hdr->options, data + len, otype, olen, odata) {
221 if (otype == DHCPV6_OPT_RELAY_MSG) {
223 odata[-2] = (olen >> 8) & 0xff;
224 odata[-1] = olen & 0xff;
225 update_nested_message(odata, olen - pdiff, pdiff);
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)
235 struct dhcpv6_client_header *hdr = data;
237 if (len < sizeof(*hdr))
240 syslog(LOG_NOTICE, "Got DHCPv6 request");
242 /* Construct reply message */
243 struct __attribute__((packed)) {
246 uint16_t serverid_type;
247 uint16_t serverid_length;
249 uint16_t hardware_type;
251 uint16_t clientid_type;
252 uint16_t clientid_length;
253 uint8_t clientid_buf[130];
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),
263 odhcpd_get_mac(iface, dest.mac);
265 struct __attribute__((packed)) {
269 } maxrt = {htons(DHCPV6_OPT_SOL_MAX_RT), htons(sizeof(maxrt) - 4),
272 struct __attribute__((packed)) {
276 } stat = {htons(DHCPV6_OPT_STATUS), htons(sizeof(stat) - 4),
277 htons(DHCPV6_STATUS_USEMULTICAST)};
279 struct __attribute__((packed)) {
283 } refresh = {htons(DHCPV6_OPT_INFO_REFRESH), htons(sizeof(uint32_t)),
286 struct in6_addr dns_addr, *dns_addr_ptr = iface->dns;
287 size_t dns_cnt = iface->dns_cnt;
289 if ((dns_cnt == 0) &&
290 !odhcpd_get_interface_dns_addr(iface, &dns_addr)) {
291 dns_addr_ptr = &dns_addr;
298 } dns = {htons(DHCPV6_OPT_DNS_SERVERS), htons(dns_cnt * sizeof(*dns_addr_ptr))};
302 /* DNS Search options */
303 uint8_t search_buf[256], *search_domain = iface->search;
304 size_t search_len = iface->search_len;
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);
310 search_domain = search_buf;
318 } search = {htons(DHCPV6_OPT_DNS_DOMAIN), htons(search_len)};
321 struct dhcpv6_cer_id cerid = {
323 .type = htons(EXT_CER_ID),
326 .addr = iface->dhcpv6_pd_cer,
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}
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);
349 memcpy(dest.tr_id, &opts[-3], sizeof(dest.tr_id));
351 if (opts[-4] == DHCPV6_MSG_ADVERTISE || opts[-4] == DHCPV6_MSG_REPLY || opts[-4] == DHCPV6_MSG_RELAY_REPL)
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))
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);
365 /* Return inf max rt option in reply to information request */
366 maxrt.type = htons(DHCPV6_OPT_INF_MAX_RT);
369 /* Go through options and find what we need */
370 uint16_t otype, olen;
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 */
388 } else if (otype == DHCPV6_OPT_IA_PD) {
390 iov[IOV_CERID].iov_len = sizeof(cerid);
392 if (IN6_IS_ADDR_UNSPECIFIED(&cerid.addr)) {
393 struct odhcpd_ipaddr *addrs;
394 ssize_t len = netlink_get_interface_addrs(0, true, &addrs);
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;
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);
413 for (ssize_t i = IOV_STAT + 1; i < IOV_TOTAL; ++i)
416 odhcpd_send(iface->dhcpv6_event.uloop.fd, addr, iov, ARRAY_SIZE(iov), iface);
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)))
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));
434 odhcpd_send(iface->dhcpv6_event.uloop.fd, addr, iov, ARRAY_SIZE(iov), iface);
438 /* Central DHCPv6-relay handler */
439 static void handle_dhcpv6(void *addr, void *data, size_t len,
440 struct interface *iface, void *dest_addr)
442 if (iface->dhcpv6 == MODE_SERVER) {
443 handle_client_request(addr, data, len, iface, dest_addr);
444 } else if (iface->dhcpv6 == MODE_RELAY) {
446 relay_server_response(data, len);
448 relay_client_request(addr, data, len, iface);
453 /* Relay server response (regular relay server handling) */
454 static void relay_server_response(uint8_t *data, size_t len)
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};
463 syslog(LOG_NOTICE, "Got a DHCPv6-reply");
466 uint8_t *odata, *end = data + len;
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)
473 memcpy(&target.sin6_addr, &h->peer_address,
474 sizeof(struct in6_addr));
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;
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)
492 bool is_authenticated = false;
493 struct in6_addr *dns_ptr = NULL;
494 size_t dns_count = 0;
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;
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;
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 */
518 const struct in6_addr *rewrite = iface->dns;
519 struct in6_addr addr;
520 size_t rewrite_cnt = iface->dns_cnt;
522 if (rewrite_cnt == 0) {
523 if (odhcpd_get_interface_dns_addr(iface, &addr))
524 return; // Unable to get interface address
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));
537 struct iovec iov = {payload_data, payload_len};
538 odhcpd_send(iface->dhcpv6_event.uloop.fd, &target, &iov, 1, iface);
541 static struct odhcpd_ipaddr *relay_link_address(struct interface *iface)
543 struct odhcpd_ipaddr *addr = NULL;
544 time_t now = odhcpd_time();
546 for (size_t i = 0; i < iface->addr6_len; i++) {
547 if (iface->addr6[i].valid <= (uint32_t)now)
550 if (iface->addr6[i].preferred > (uint32_t)now) {
551 addr = &iface->addr6[i];
555 if (!addr || (iface->addr6[i].valid > addr->valid))
556 addr = &iface->addr6[i];
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)
566 struct interface *master = odhcpd_get_master_interface();
567 const struct dhcpv6_relay_header *h = data;
568 struct sockaddr_in6 s;
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 */
577 syslog(LOG_NOTICE, "Got a DHCPv6-request");
579 /* Construct our forwarding envelope */
580 struct dhcpv6_relay_forward_envelope hdr = {
581 .msg_type = DHCPV6_MSG_RELAY_FORW,
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),
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
593 hdr.hop_count = h->hop_count + 1;
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));
601 /* Detect public IP of slave interface to use as link-address */
602 struct odhcpd_ipaddr *ip = relay_link_address(iface);
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);
611 return; /* Could not obtain a suitable address */
614 memcpy(&hdr.link_address, &ip->addr.in6, sizeof(hdr.link_address));
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);
621 struct iovec iov[2] = {{&hdr, sizeof(hdr)}, {(void*)data, len}};
622 odhcpd_send(master->dhcpv6_event.uloop.fd, &s, iov, 2, master);