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