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