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