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