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