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