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