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