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>
25 static const char *excluded_class = "HOMENET";
28 static void relay_client_request(struct sockaddr_in6 *source,
29 const void *data, size_t len, struct interface *iface);
30 static void relay_server_response(uint8_t *data, size_t len);
32 static void handle_dhcpv6(void *addr, void *data, size_t len,
33 struct interface *iface);
34 static void handle_client_request(void *addr, void *data, size_t len,
35 struct interface *iface);
39 // Create socket and register events
47 int setup_dhcpv6_interface(struct interface *iface, bool enable)
49 if (iface->dhcpv6_event.uloop.fd > 0) {
50 uloop_fd_delete(&iface->dhcpv6_event.uloop);
51 close(iface->dhcpv6_event.uloop.fd);
52 iface->dhcpv6_event.uloop.fd = -1;
55 // Configure multicast settings
56 if (enable && iface->dhcpv6 && !iface->master) {
57 int sock = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
59 syslog(LOG_ERR, "Failed to create DHCPv6 server socket: %s",
64 // Basic IPv6 configuration
65 setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, iface->ifname, strlen(iface->ifname));
68 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val));
69 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
70 setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &val, sizeof(val));
72 val = DHCPV6_HOP_COUNT_LIMIT;
73 setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val));
76 setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val, sizeof(val));
78 struct sockaddr_in6 bind_addr = {AF_INET6, htons(DHCPV6_SERVER_PORT),
79 0, IN6ADDR_ANY_INIT, 0};
81 if (bind(sock, (struct sockaddr*)&bind_addr, sizeof(bind_addr))) {
82 syslog(LOG_ERR, "Failed to open DHCPv6 server socket: %s",
87 struct ipv6_mreq relay = {ALL_DHCPV6_RELAYS, iface->ifindex};
88 struct ipv6_mreq server = {ALL_DHCPV6_SERVERS, iface->ifindex};
89 setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &relay, sizeof(relay));
91 if (iface->dhcpv6 == RELAYD_SERVER)
92 setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &server, sizeof(server));
94 iface->dhcpv6_event.uloop.fd = sock;
95 iface->dhcpv6_event.handle_dgram = handle_dhcpv6;
96 odhcpd_register(&iface->dhcpv6_event);
99 return setup_dhcpv6_ia_interface(iface, enable);
103 static void handle_nested_message(uint8_t *data, size_t len,
104 uint8_t **opts, uint8_t **end, struct iovec iov[9])
106 struct dhcpv6_relay_header *hdr = (struct dhcpv6_relay_header*)data;
107 if (iov[0].iov_base == NULL) {
108 iov[0].iov_base = data;
109 iov[0].iov_len = len;
112 if (len < sizeof(struct dhcpv6_client_header))
115 if (hdr->msg_type != DHCPV6_MSG_RELAY_FORW) {
116 iov[0].iov_len = data - (uint8_t*)iov[0].iov_base;
117 struct dhcpv6_client_header *hdr = (void*)data;
118 *opts = (uint8_t*)&hdr[1];
123 uint16_t otype, olen;
125 dhcpv6_for_each_option(hdr->options, data + len, otype, olen, odata) {
126 if (otype == DHCPV6_OPT_RELAY_MSG) {
127 iov[8].iov_base = odata + olen;
128 iov[8].iov_len = (((uint8_t*)iov[0].iov_base) + iov[0].iov_len)
130 handle_nested_message(odata, olen, opts, end, iov);
137 static void update_nested_message(uint8_t *data, size_t len, ssize_t pdiff)
139 struct dhcpv6_relay_header *hdr = (struct dhcpv6_relay_header*)data;
140 if (hdr->msg_type != DHCPV6_MSG_RELAY_FORW)
143 hdr->msg_type = DHCPV6_MSG_RELAY_REPL;
145 uint16_t otype, olen;
147 dhcpv6_for_each_option(hdr->options, data + len, otype, olen, odata) {
148 if (otype == DHCPV6_OPT_RELAY_MSG) {
150 odata[-2] = (olen >> 8) & 0xff;
151 odata[-1] = olen & 0xff;
152 update_nested_message(odata, olen - pdiff, pdiff);
159 // Simple DHCPv6-server for information requests
160 static void handle_client_request(void *addr, void *data, size_t len,
161 struct interface *iface)
163 struct dhcpv6_client_header *hdr = data;
164 if (len < sizeof(*hdr))
167 syslog(LOG_NOTICE, "Got DHCPv6 request");
169 // Construct reply message
170 struct __attribute__((packed)) {
173 uint16_t serverid_type;
174 uint16_t serverid_length;
176 uint16_t hardware_type;
178 uint16_t solmaxrt_type;
179 uint16_t solmaxrt_length;
180 uint32_t solmaxrt_value;
181 uint16_t clientid_type;
182 uint16_t clientid_length;
183 uint8_t clientid_buf[130];
185 .msg_type = DHCPV6_MSG_REPLY,
186 .serverid_type = htons(DHCPV6_OPT_SERVERID),
187 .serverid_length = htons(10),
188 .duid_type = htons(3),
189 .hardware_type = htons(1),
190 .solmaxrt_type = htons(DHCPV6_OPT_SOL_MAX_RT),
191 .solmaxrt_length = htons(4),
192 .solmaxrt_value = htonl(60),
193 .clientid_type = htons(DHCPV6_OPT_CLIENTID),
196 odhcpd_get_mac(iface, dest.mac);
198 struct __attribute__((packed)) {
202 } stat = {htons(DHCPV6_OPT_STATUS), htons(sizeof(stat) - 4),
203 htons(DHCPV6_STATUS_NOADDRSAVAIL)};
205 struct __attribute__((packed)) {
209 } refresh = {htons(DHCPV6_OPT_INFO_REFRESH), htons(sizeof(uint32_t)),
212 struct odhcpd_ipaddr ipaddr;
213 struct in6_addr *dns_addr = iface->dns;
214 size_t dns_cnt = iface->dns_cnt;
216 if (dns_cnt == 0 && odhcpd_get_interface_addresses(iface->ifindex, &ipaddr, 1) == 1) {
217 dns_addr = &ipaddr.addr;
224 } dns = {htons(DHCPV6_OPT_DNS_SERVERS), htons(dns_cnt * sizeof(*dns_addr))};
228 // DNS Search options
229 uint8_t search_buf[256], *search_domain = iface->search;
230 size_t search_len = iface->search_len;
232 if (!search_domain && !res_init() && _res.dnsrch[0] && _res.dnsrch[0][0]) {
233 int len = dn_comp(_res.dnsrch[0], search_buf,
234 sizeof(search_buf), NULL, NULL);
236 search_domain = search_buf;
244 } search = {htons(DHCPV6_OPT_DNS_DOMAIN), htons(search_len)};
247 struct dhcpv6_cer_id cerid = {
249 .type = htons(EXT_CER_ID),
252 .addr = iface->dhcpv6_pd_cer,
257 struct iovec iov[] = {{NULL, 0},
258 {&dest, (uint8_t*)&dest.clientid_type - (uint8_t*)&dest},
259 {&dns, (dns_cnt) ? sizeof(dns) : 0},
260 {dns_addr, dns_cnt * sizeof(*dns_addr)},
261 {&search, (search_len) ? sizeof(search) : 0},
262 {search_domain, search_len},
267 uint8_t *opts = (uint8_t*)&hdr[1], *opts_end = (uint8_t*)data + len;
268 if (hdr->msg_type == DHCPV6_MSG_RELAY_FORW)
269 handle_nested_message(data, len, &opts, &opts_end, iov);
271 memcpy(dest.tr_id, &opts[-3], sizeof(dest.tr_id));
273 if (opts[-4] == DHCPV6_MSG_ADVERTISE || opts[-4] == DHCPV6_MSG_REPLY || opts[-4] == DHCPV6_MSG_RELAY_REPL)
276 if (opts[-4] == DHCPV6_MSG_SOLICIT) {
277 dest.msg_type = DHCPV6_MSG_ADVERTISE;
278 } else if (opts[-4] == DHCPV6_MSG_INFORMATION_REQUEST) {
279 iov[6].iov_base = &refresh;
280 iov[6].iov_len = sizeof(refresh);
283 // Go through options and find what we need
284 uint16_t otype, olen;
286 dhcpv6_for_each_option(opts, opts_end, otype, olen, odata) {
287 if (otype == DHCPV6_OPT_CLIENTID && olen <= 130) {
288 dest.clientid_length = htons(olen);
289 memcpy(dest.clientid_buf, odata, olen);
290 iov[1].iov_len += 4 + olen;
291 } else if (otype == DHCPV6_OPT_SERVERID) {
292 if (olen != ntohs(dest.serverid_length) ||
293 memcmp(odata, &dest.duid_type, olen))
294 return; // Not for us
295 } else if (otype == DHCPV6_OPT_USER_CLASS) {
296 uint8_t *c = odata, *cend = &odata[olen];
297 for (; &c[2] <= cend && &c[2 + (c[0] << 8) + c[1]] <= cend; c = &c[2 + (c[0] << 8) + c[1]]) {
298 size_t elen = strlen(excluded_class);
299 if (((((size_t)c[0]) << 8) | c[1]) == elen && !memcmp(&c[2], excluded_class, elen))
300 return; // Ignore from homenet
302 } else if (otype == DHCPV6_OPT_IA_PD) {
304 iov[7].iov_len = sizeof(cerid);
306 if (IN6_IS_ADDR_UNSPECIFIED(&cerid.addr)) {
307 struct odhcpd_ipaddr addrs[32];
308 ssize_t len = odhcpd_get_interface_addresses(0, addrs,
309 sizeof(addrs) / sizeof(*addrs));
311 for (ssize_t i = 0; i < len; ++i)
312 if (IN6_IS_ADDR_UNSPECIFIED(&cerid.addr)
313 || memcmp(&addrs[i].addr, &cerid.addr, sizeof(cerid.addr)) < 0)
314 cerid.addr = addrs[i].addr;
320 if (opts[-4] != DHCPV6_MSG_INFORMATION_REQUEST) {
321 ssize_t ialen = dhcpv6_handle_ia(pdbuf, sizeof(pdbuf), iface, addr, &opts[-4], opts_end);
322 iov[6].iov_len = ialen;
323 if (ialen < 0 || (ialen == 0 && (opts[-4] == DHCPV6_MSG_REBIND || opts[-4] == DHCPV6_MSG_CONFIRM)))
327 if (iov[0].iov_len > 0) // Update length
328 update_nested_message(data, len, iov[1].iov_len + iov[2].iov_len +
329 iov[3].iov_len + iov[4].iov_len + iov[5].iov_len +
330 iov[6].iov_len + iov[7].iov_len - (4 + opts_end - opts));
332 odhcpd_send(iface->dhcpv6_event.uloop.fd, addr, iov, ARRAY_SIZE(iov), iface);
336 // Central DHCPv6-relay handler
337 static void handle_dhcpv6(void *addr, void *data, size_t len,
338 struct interface *iface)
340 if (iface->dhcpv6 == RELAYD_SERVER) {
341 handle_client_request(addr, data, len, iface);
342 } else if (iface->dhcpv6 == RELAYD_RELAY) {
344 relay_server_response(data, len);
346 relay_client_request(addr, data, len, iface);
351 // Relay server response (regular relay server handling)
352 static void relay_server_response(uint8_t *data, size_t len)
354 // Information we need to gather
355 uint8_t *payload_data = NULL;
356 size_t payload_len = 0;
357 int32_t ifaceidx = 0;
358 struct sockaddr_in6 target = {AF_INET6, htons(DHCPV6_CLIENT_PORT),
359 0, IN6ADDR_ANY_INIT, 0};
361 syslog(LOG_NOTICE, "Got a DHCPv6-reply");
364 uint8_t *odata, *end = data + len;
366 // Relay DHCPv6 reply from server to client
367 struct dhcpv6_relay_header *h = (void*)data;
368 if (len < sizeof(*h) || h->msg_type != DHCPV6_MSG_RELAY_REPL)
371 memcpy(&target.sin6_addr, &h->peer_address,
372 sizeof(struct in6_addr));
374 // Go through options and find what we need
375 dhcpv6_for_each_option(h->options, end, otype, olen, odata) {
376 if (otype == DHCPV6_OPT_INTERFACE_ID
377 && olen == sizeof(ifaceidx)) {
378 memcpy(&ifaceidx, odata, sizeof(ifaceidx));
379 } else if (otype == DHCPV6_OPT_RELAY_MSG) {
380 payload_data = odata;
385 // Invalid interface-id or basic payload
386 struct interface *iface = odhcpd_get_interface_by_index(ifaceidx);
387 if (!iface || iface->master || !payload_data || payload_len < 4)
390 bool is_authenticated = false;
391 struct in6_addr *dns_ptr = NULL;
392 size_t dns_count = 0;
394 // If the payload is relay-reply we have to send to the server port
395 if (payload_data[0] == DHCPV6_MSG_RELAY_REPL) {
396 target.sin6_port = htons(DHCPV6_SERVER_PORT);
397 } else { // Go through the payload data
398 struct dhcpv6_client_header *h = (void*)payload_data;
399 end = payload_data + payload_len;
401 dhcpv6_for_each_option(&h[1], end, otype, olen, odata) {
402 if (otype == DHCPV6_OPT_DNS_SERVERS && olen >= 16) {
403 dns_ptr = (struct in6_addr*)odata;
404 dns_count = olen / 16;
405 } else if (otype == DHCPV6_OPT_AUTH) {
406 is_authenticated = true;
411 // Rewrite DNS servers if requested
412 if (iface->always_rewrite_dns && dns_ptr && dns_count > 0) {
413 if (is_authenticated)
414 return; // Impossible to rewrite
416 struct odhcpd_ipaddr ip;
417 const struct in6_addr *rewrite = iface->dns;
418 size_t rewrite_cnt = iface->dns_cnt;
420 if (rewrite_cnt == 0) {
421 if (odhcpd_get_interface_addresses(iface->ifindex, &ip, 1) < 1)
422 return; // Unable to get interface address
428 // Copy over any other addresses
429 for (size_t i = 0; i < dns_count; ++i) {
430 size_t j = (i < rewrite_cnt) ? i : rewrite_cnt - 1;
431 memcpy(&dns_ptr[i], &rewrite[j], sizeof(*rewrite));
435 struct iovec iov = {payload_data, payload_len};
436 odhcpd_send(iface->dhcpv6_event.uloop.fd, &target, &iov, 1, iface);
440 // Relay client request (regular DHCPv6-relay)
441 static void relay_client_request(struct sockaddr_in6 *source,
442 const void *data, size_t len, struct interface *iface)
444 struct interface *master = odhcpd_get_master_interface();
445 const struct dhcpv6_relay_header *h = data;
446 if (!master || master->dhcpv6 != RELAYD_RELAY ||
447 h->msg_type == DHCPV6_MSG_RELAY_REPL ||
448 h->msg_type == DHCPV6_MSG_RECONFIGURE ||
449 h->msg_type == DHCPV6_MSG_REPLY ||
450 h->msg_type == DHCPV6_MSG_ADVERTISE)
451 return; // Invalid message types for client
453 syslog(LOG_NOTICE, "Got a DHCPv6-request");
455 // Construct our forwarding envelope
456 struct dhcpv6_relay_forward_envelope hdr = {
457 .msg_type = DHCPV6_MSG_RELAY_FORW,
459 .interface_id_type = htons(DHCPV6_OPT_INTERFACE_ID),
460 .interface_id_len = htons(sizeof(uint32_t)),
461 .relay_message_type = htons(DHCPV6_OPT_RELAY_MSG),
462 .relay_message_len = htons(len),
465 if (h->msg_type == DHCPV6_MSG_RELAY_FORW) { // handle relay-forward
466 if (h->hop_count >= DHCPV6_HOP_COUNT_LIMIT)
467 return; // Invalid hop count
469 hdr.hop_count = h->hop_count + 1;
472 // use memcpy here as the destination fields are unaligned
473 uint32_t ifindex = iface->ifindex;
474 memcpy(&hdr.peer_address, &source->sin6_addr, sizeof(struct in6_addr));
475 memcpy(&hdr.interface_id_data, &ifindex, sizeof(ifindex));
477 // Detect public IP of slave interface to use as link-address
478 struct odhcpd_ipaddr ip;
479 if (odhcpd_get_interface_addresses(iface->ifindex, &ip, 1) < 1) {
480 // No suitable address! Is the slave not configured yet?
481 // Detect public IP of master interface and use it instead
482 // This is WRONG and probably violates the RFC. However
483 // otherwise we have a hen and egg problem because the
484 // slave-interface cannot be auto-configured.
485 if (odhcpd_get_interface_addresses(master->ifindex, &ip, 1) < 1)
486 return; // Could not obtain a suitable address
488 memcpy(&hdr.link_address, &ip.addr, sizeof(hdr.link_address));
490 struct sockaddr_in6 dhcpv6_servers = {AF_INET6,
491 htons(DHCPV6_SERVER_PORT), 0, ALL_DHCPV6_SERVERS, 0};
492 struct iovec iov[2] = {{&hdr, sizeof(hdr)}, {(void*)data, len}};
493 odhcpd_send(iface->dhcpv6_event.uloop.fd, &dhcpv6_servers, iov, 2, master);