0285923a0190dd56d1e783067f3c005c22d18cdf
[project/odhcp6c.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 #include <time.h>
16 #include <fcntl.h>
17 #include <errno.h>
18 #include <stdlib.h>
19 #include <signal.h>
20 #include <limits.h>
21 #include <resolv.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <syslog.h>
25 #include <stdbool.h>
26 #include <sys/time.h>
27 #include <sys/ioctl.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30
31 #include <net/if.h>
32 #include <net/ethernet.h>
33
34 #include "odhcp6c.h"
35 #include "md5.h"
36
37
38 #define ALL_DHCPV6_RELAYS {{{0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
39 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02}}}
40 #define DHCPV6_CLIENT_PORT 546
41 #define DHCPV6_SERVER_PORT 547
42 #define DHCPV6_DUID_LLADDR 3
43 #define DHCPV6_REQ_DELAY 1
44
45
46 static bool dhcpv6_response_is_valid(const void *buf, ssize_t len,
47 const uint8_t transaction[3], enum dhcpv6_msg type);
48
49 static uint32_t dhcpv6_parse_ia(void *opt, void *end);
50
51 static reply_handler dhcpv6_handle_reply;
52 static reply_handler dhcpv6_handle_advert;
53 static reply_handler dhcpv6_handle_rebind_reply;
54 static reply_handler dhcpv6_handle_reconfigure;
55 static int dhcpv6_commit_advert(void);
56
57
58
59 // RFC 3315 - 5.5 Timeout and Delay values
60 static struct dhcpv6_retx dhcpv6_retx[_DHCPV6_MSG_MAX] = {
61 [DHCPV6_MSG_UNKNOWN] = {false, 1, 120, "<POLL>",
62 dhcpv6_handle_reconfigure, NULL},
63 [DHCPV6_MSG_SOLICIT] = {true, 1, 3600, "SOLICIT",
64 dhcpv6_handle_advert, dhcpv6_commit_advert},
65 [DHCPV6_MSG_REQUEST] = {true, 30, 10, "REQUEST",
66 dhcpv6_handle_reply, NULL},
67 [DHCPV6_MSG_RENEW] = {false, 10, 600, "RENEW",
68 dhcpv6_handle_reply, NULL},
69 [DHCPV6_MSG_REBIND] = {false, 10, 600, "REBIND",
70 dhcpv6_handle_rebind_reply, NULL},
71 [DHCPV6_MSG_RELEASE] = {false, 1, 600, "RELEASE", NULL, NULL},
72 [DHCPV6_MSG_DECLINE] = {false, 1, 3, "DECLINE", NULL, NULL},
73 [DHCPV6_MSG_INFO_REQ] = {true, 1, 120, "INFOREQ",
74 dhcpv6_handle_reply, NULL},
75 };
76
77
78 // Sockets
79 static int sock = -1;
80 static int ifindex = -1;
81 static int64_t t1 = 0, t2 = 0, t3 = 0;
82
83 // IA states
84 static int request_prefix = -1;
85 static enum odhcp6c_ia_mode na_mode = IA_MODE_NONE, pd_mode = IA_MODE_NONE;
86 static bool accept_reconfig = false;
87
88 // Reconfigure key
89 static uint8_t reconf_key[16];
90
91
92
93 int init_dhcpv6(const char *ifname, int request_pd)
94 {
95 request_prefix = request_pd;
96
97 sock = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
98
99 // Detect interface
100 struct ifreq ifr;
101 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
102 if (ioctl(sock, SIOCGIFINDEX, &ifr))
103 return -1;
104 ifindex = ifr.ifr_ifindex;
105
106 // Create client DUID
107 size_t client_id_len;
108 odhcp6c_get_state(STATE_CLIENT_ID, &client_id_len);
109 if (client_id_len == 0) {
110 ioctl(sock, SIOCGIFHWADDR, &ifr);
111 uint8_t duid[14] = {0, DHCPV6_OPT_CLIENTID, 0, 10, 0,
112 DHCPV6_DUID_LLADDR, 0, 1};
113 memcpy(&duid[8], ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
114
115 uint8_t zero[ETHER_ADDR_LEN] = {0, 0, 0, 0, 0, 0};
116 struct ifreq ifs[100], *ifp, *ifend;
117 struct ifconf ifc;
118 ifc.ifc_req = ifs;
119 ifc.ifc_len = sizeof(ifs);
120
121 if (!memcmp(&duid[8], zero, ETHER_ADDR_LEN) &&
122 ioctl(sock, SIOCGIFCONF, &ifc) >= 0) {
123 // If our interface doesn't have an address...
124 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
125 for (ifp = ifc.ifc_req; ifp < ifend &&
126 !memcmp(&duid[8], zero, 6); ifp++) {
127 memcpy(ifr.ifr_name, ifp->ifr_name,
128 sizeof(ifr.ifr_name));
129 ioctl(sock, SIOCGIFHWADDR, &ifr);
130 memcpy(&duid[8], ifr.ifr_hwaddr.sa_data,
131 ETHER_ADDR_LEN);
132 }
133 }
134
135 odhcp6c_add_state(STATE_CLIENT_ID, duid, sizeof(duid));
136 }
137
138 // Create ORO
139 uint16_t oro[] = {
140 htons(DHCPV6_OPT_SIP_SERVER_D),
141 htons(DHCPV6_OPT_SIP_SERVER_A),
142 htons(DHCPV6_OPT_DNS_SERVERS),
143 htons(DHCPV6_OPT_DNS_DOMAIN),
144 htons(DHCPV6_OPT_NTP_SERVER),
145 htons(DHCPV6_OPT_SIP_SERVER_A),
146 htons(DHCPV6_OPT_AFTR_NAME),
147 htons(DHCPV6_OPT_PD_EXCLUDE),
148 #ifdef EXT_PREFIX_CLASS
149 htons(DHCPV6_OPT_PREFIX_CLASS),
150 #endif
151 };
152 odhcp6c_add_state(STATE_ORO, oro, sizeof(oro));
153
154
155 // Configure IPv6-options
156 int val = 1;
157 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val));
158 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
159 setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname));
160
161 struct sockaddr_in6 client_addr = { .sin6_family = AF_INET6,
162 .sin6_port = htons(DHCPV6_CLIENT_PORT), .sin6_flowinfo = 0 };
163 if (bind(sock, (struct sockaddr*)&client_addr, sizeof(client_addr)))
164 return -1;
165
166 return 0;
167 }
168
169
170 void dhcpv6_set_ia_mode(enum odhcp6c_ia_mode na, enum odhcp6c_ia_mode pd)
171 {
172 na_mode = na;
173 pd_mode = pd;
174 }
175
176
177 static void dhcpv6_send(enum dhcpv6_msg type, uint8_t trid[3], uint32_t ecs)
178 {
179 // Build FQDN
180 char fqdn_buf[256];
181 gethostname(fqdn_buf, sizeof(fqdn_buf));
182 struct {
183 uint16_t type;
184 uint16_t len;
185 uint8_t flags;
186 uint8_t data[256];
187 } fqdn;
188 size_t fqdn_len = 5 + dn_comp(fqdn_buf, fqdn.data,
189 sizeof(fqdn.data), NULL, NULL);
190 fqdn.type = htons(DHCPV6_OPT_FQDN);
191 fqdn.len = htons(fqdn_len - 4);
192 fqdn.flags = 0;
193
194
195 // Build Client ID
196 size_t cl_id_len;
197 void *cl_id = odhcp6c_get_state(STATE_CLIENT_ID, &cl_id_len);
198
199 // Get Server ID
200 size_t srv_id_len;
201 void *srv_id = odhcp6c_get_state(STATE_SERVER_ID, &srv_id_len);
202
203 // Build IA_PDs
204 size_t ia_pd_entries, ia_pd_len = 0;
205 struct odhcp6c_entry *e = odhcp6c_get_state(STATE_IA_PD, &ia_pd_entries);
206 ia_pd_entries /= sizeof(*e);
207 struct dhcpv6_ia_hdr hdr_ia_pd = {
208 htons(DHCPV6_OPT_IA_PD),
209 htons(sizeof(hdr_ia_pd) - 4),
210 1, 0, 0
211 };
212
213
214 uint8_t *ia_pd = alloca(ia_pd_entries * (sizeof(struct dhcpv6_ia_prefix) + 10));
215 for (size_t i = 0; i < ia_pd_entries; ++i) {
216 uint8_t ex_len = 0;
217 if (e[i].priority > 0)
218 ex_len = ((e[i].priority - e[i].length - 1) / 8) + 6;
219
220 struct dhcpv6_ia_prefix p = {
221 .type = htons(DHCPV6_OPT_IA_PREFIX),
222 .len = htons(sizeof(p) - 4U + ex_len),
223 .prefix = e[i].length,
224 .addr = e[i].target
225 };
226
227 memcpy(ia_pd + ia_pd_len, &p, sizeof(p));
228 ia_pd_len += sizeof(p);
229
230 if (ex_len) {
231 ia_pd[ia_pd_len++] = 0;
232 ia_pd[ia_pd_len++] = DHCPV6_OPT_PD_EXCLUDE;
233 ia_pd[ia_pd_len++] = 0;
234 ia_pd[ia_pd_len++] = ex_len - 4;
235 ia_pd[ia_pd_len++] = e[i].priority;
236
237 uint32_t excl = ntohl(e[i].router.s6_addr32[1]);
238 excl >>= (64 - e[i].priority);
239 excl <<= 8 - ((e[i].priority - e[i].length) % 8);
240
241 for (size_t i = ex_len - 5; i > 0; --i, excl >>= 8)
242 ia_pd[ia_pd_len + i] = excl & 0xff;
243 ia_pd_len += ex_len - 5;
244 }
245 }
246
247 struct dhcpv6_ia_prefix pref = {
248 .type = htons(DHCPV6_OPT_IA_PREFIX),
249 .len = htons(25), .prefix = request_prefix
250 };
251 if (request_prefix > 0 && ia_pd_len == 0 && type == DHCPV6_MSG_SOLICIT) {
252 ia_pd = (uint8_t*)&pref;
253 ia_pd_len = sizeof(pref);
254 }
255 hdr_ia_pd.len = htons(ntohs(hdr_ia_pd.len) + ia_pd_len);
256
257 // Build IA_NAs
258 size_t ia_na_entries, ia_na_len = 0;
259 void *ia_na = NULL;
260 e = odhcp6c_get_state(STATE_IA_NA, &ia_na_entries);
261 ia_na_entries /= sizeof(*e);
262
263 struct dhcpv6_ia_hdr hdr_ia_na = {
264 htons(DHCPV6_OPT_IA_NA),
265 htons(sizeof(hdr_ia_na) - 4),
266 1, 0, 0
267 };
268
269 struct dhcpv6_ia_addr pa[ia_na_entries];
270 for (size_t i = 0; i < ia_na_entries; ++i) {
271 pa[i].type = htons(DHCPV6_OPT_IA_ADDR);
272 pa[i].len = htons(sizeof(pa[i]) - 4U);
273 pa[i].addr = e[i].target;
274 pa[i].preferred = 0;
275 pa[i].valid = 0;
276 }
277
278 ia_na = pa;
279 ia_na_len = sizeof(pa);
280 hdr_ia_na.len = htons(ntohs(hdr_ia_na.len) + ia_na_len);
281
282 // Reconfigure Accept
283 struct {
284 uint16_t type;
285 uint16_t length;
286 } reconf_accept = {htons(DHCPV6_OPT_RECONF_ACCEPT), 0};
287
288 // Request Information Refresh
289 uint16_t oro_refresh = htons(DHCPV6_OPT_INFO_REFRESH);
290
291 // Prepare Header
292 size_t oro_len;
293 void *oro = odhcp6c_get_state(STATE_ORO, &oro_len);
294 struct {
295 uint8_t type;
296 uint8_t trid[3];
297 uint16_t elapsed_type;
298 uint16_t elapsed_len;
299 uint16_t elapsed_value;
300 uint16_t oro_type;
301 uint16_t oro_len;
302 } hdr = {
303 type, {trid[0], trid[1], trid[2]},
304 htons(DHCPV6_OPT_ELAPSED), htons(2),
305 htons((ecs > 0xffff) ? 0xffff : ecs),
306 htons(DHCPV6_OPT_ORO), htons(oro_len),
307 };
308
309 struct iovec iov[] = {
310 {&hdr, sizeof(hdr)},
311 {oro, oro_len},
312 {&oro_refresh, 0},
313 {cl_id, cl_id_len},
314 {srv_id, srv_id_len},
315 {&reconf_accept, sizeof(reconf_accept)},
316 {&fqdn, fqdn_len},
317 {&hdr_ia_na, sizeof(hdr_ia_na)},
318 {ia_na, ia_na_len},
319 {&hdr_ia_pd, sizeof(hdr_ia_pd)},
320 {ia_pd, ia_pd_len},
321 };
322
323 size_t cnt = ARRAY_SIZE(iov);
324 if (type == DHCPV6_MSG_INFO_REQ) {
325 cnt = 5;
326 iov[2].iov_len = sizeof(oro_refresh);
327 hdr.oro_len = htons(oro_len + sizeof(oro_refresh));
328 } else if (!request_prefix) {
329 cnt = 9;
330 }
331
332 // Disable IAs if not used
333 if (type != DHCPV6_MSG_SOLICIT) {
334 iov[5].iov_len = 0;
335 if (ia_na_len == 0)
336 iov[7].iov_len = 0;
337 if (ia_pd_len == 0)
338 iov[9].iov_len = 0;
339 }
340
341 if (na_mode == IA_MODE_NONE)
342 iov[7].iov_len = 0;
343
344 struct sockaddr_in6 srv = {AF_INET6, htons(DHCPV6_SERVER_PORT),
345 0, ALL_DHCPV6_RELAYS, ifindex};
346 struct msghdr msg = {&srv, sizeof(srv), iov, cnt, NULL, 0, 0};
347
348 sendmsg(sock, &msg, 0);
349 }
350
351
352 static int64_t dhcpv6_rand_delay(int64_t time)
353 {
354 int random;
355 odhcp6c_random(&random, sizeof(random));
356 return (time * ((int64_t)random % 1000LL)) / 10000LL;
357 }
358
359
360 int dhcpv6_request(enum dhcpv6_msg type)
361 {
362 uint8_t buf[1536];
363 uint64_t timeout = UINT32_MAX;
364 struct dhcpv6_retx *retx = &dhcpv6_retx[type];
365
366 if (retx->delay) {
367 struct timespec ts = {0, 0};
368 ts.tv_nsec = dhcpv6_rand_delay(10 * DHCPV6_REQ_DELAY);
369 nanosleep(&ts, NULL);
370 }
371
372 if (type == DHCPV6_MSG_RELEASE || type == DHCPV6_MSG_DECLINE)
373 timeout = 3;
374 else if (type == DHCPV6_MSG_UNKNOWN)
375 timeout = t1;
376 else if (type == DHCPV6_MSG_RENEW)
377 timeout = (t2 > t1) ? t2 - t1 : 0;
378 else if (type == DHCPV6_MSG_REBIND)
379 timeout = (t3 > t2) ? t3 - t2 : 0;
380
381 if (timeout == 0)
382 return -1;
383
384 syslog(LOG_NOTICE, "Sending %s (timeout %us)", retx->name, (unsigned)timeout);
385
386 uint64_t start = odhcp6c_get_milli_time(), round_start = start, elapsed;
387
388 // Generate transaction ID
389 uint8_t trid[3] = {0, 0, 0};
390 if (type != DHCPV6_MSG_UNKNOWN)
391 odhcp6c_random(trid, sizeof(trid));
392 ssize_t len = -1;
393 int64_t rto = 0;
394
395 do {
396 rto = (rto == 0) ? (retx->init_timeo * 1000 +
397 dhcpv6_rand_delay(retx->init_timeo * 1000)) :
398 (2 * rto + dhcpv6_rand_delay(rto));
399
400 if (rto >= retx->max_timeo * 1000)
401 rto = retx->max_timeo * 1000 +
402 dhcpv6_rand_delay(retx->max_timeo * 1000);
403
404 // Calculate end for this round and elapsed time
405 uint64_t round_end = round_start + rto;
406 elapsed = round_start - start;
407
408 // Don't wait too long
409 if (round_end - start > timeout * 1000)
410 round_end = timeout * 1000 + start;
411
412 // Built and send package
413 if (type != DHCPV6_MSG_UNKNOWN)
414 dhcpv6_send(type, trid, elapsed / 10);
415
416 // Receive rounds
417 for (; len < 0 && round_start < round_end;
418 round_start = odhcp6c_get_milli_time()) {
419 // Check for pending signal
420 if (odhcp6c_signal_process())
421 return -1;
422
423 // Set timeout for receiving
424 uint64_t t = round_end - round_start;
425 struct timeval timeout = {t / 1000, (t % 1000) * 1000};
426 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
427 &timeout, sizeof(timeout));
428
429 // Receive cycle
430 len = recv(sock, buf, sizeof(buf), 0);
431
432 if (!dhcpv6_response_is_valid(buf, len, trid, type))
433 len = -1;
434
435 if (len > 0) {
436 uint8_t *opt = &buf[4];
437 uint8_t *opt_end = opt + len - 4;
438
439 round_start = odhcp6c_get_milli_time();
440 elapsed = round_start - start;
441 syslog(LOG_NOTICE, "Got a valid reply after "
442 "%ums", (unsigned)elapsed);
443
444 if (retx->handler_reply)
445 len = retx->handler_reply(
446 type, opt, opt_end);
447
448 if (len > 0 && round_end - round_start > 1000)
449 round_end = 1000 + round_start;
450 }
451 }
452
453 // Allow
454 if (retx->handler_finish)
455 len = retx->handler_finish();
456 } while (len < 0 && elapsed / 1000 < timeout);
457
458 return len;
459 }
460
461
462 static bool dhcpv6_response_is_valid(const void *buf, ssize_t len,
463 const uint8_t transaction[3], enum dhcpv6_msg type)
464 {
465 const struct dhcpv6_header *rep = buf;
466 if (len < (ssize_t)sizeof(*rep) || memcmp(rep->tr_id,
467 transaction, sizeof(rep->tr_id)))
468 return false; // Invalid reply
469
470 if (type == DHCPV6_MSG_SOLICIT) {
471 if (rep->msg_type != DHCPV6_MSG_ADVERT &&
472 rep->msg_type != DHCPV6_MSG_REPLY)
473 return false;
474 } else if (type == DHCPV6_MSG_UNKNOWN) {
475 if (!accept_reconfig || rep->msg_type != DHCPV6_MSG_RECONF)
476 return false;
477 } else if (rep->msg_type != DHCPV6_MSG_REPLY) {
478 return false;
479 }
480
481 uint8_t *end = ((uint8_t*)buf) + len, *odata;
482 uint16_t otype, olen;
483 bool clientid_ok = false, serverid_ok = false, rcauth_ok = false;
484
485 size_t client_id_len, server_id_len;
486 void *client_id = odhcp6c_get_state(STATE_CLIENT_ID, &client_id_len);
487 void *server_id = odhcp6c_get_state(STATE_SERVER_ID, &server_id_len);
488
489 dhcpv6_for_each_option(&rep[1], end, otype, olen, odata) {
490 if (otype == DHCPV6_OPT_CLIENTID) {
491 clientid_ok = (olen + 4U == client_id_len) && !memcmp(
492 &odata[-4], client_id, client_id_len);
493 } else if (otype == DHCPV6_OPT_SERVERID) {
494 serverid_ok = (olen + 4U == server_id_len) && !memcmp(
495 &odata[-4], server_id, server_id_len);
496 } else if (otype == DHCPV6_OPT_AUTH && olen == -4 +
497 sizeof(struct dhcpv6_auth_reconfigure)) {
498 struct dhcpv6_auth_reconfigure *r = (void*)&odata[-4];
499 if (r->protocol != 3 || r->algorithm != 1 || r->reconf_type != 2)
500 continue;
501
502 md5_state_t md5;
503 uint8_t serverhash[16], secretbytes[16], hash[16];
504 memcpy(serverhash, r->key, sizeof(serverhash));
505 memset(r->key, 0, sizeof(r->key));
506 memcpy(secretbytes, reconf_key, sizeof(secretbytes));
507
508 for (size_t i = 0; i < sizeof(secretbytes); ++i)
509 secretbytes[i] ^= 0x36;
510
511 md5_init(&md5);
512 md5_append(&md5, secretbytes, sizeof(secretbytes));
513 md5_append(&md5, buf, len);
514 md5_finish(&md5, hash);
515
516 for (size_t i = 0; i < sizeof(secretbytes); ++i) {
517 secretbytes[i] ^= 0x36;
518 secretbytes[i] ^= 0x5c;
519 }
520
521 md5_init(&md5);
522 md5_append(&md5, secretbytes, sizeof(secretbytes));
523 md5_append(&md5, hash, 16);
524 md5_finish(&md5, hash);
525
526 rcauth_ok = !memcmp(hash, serverhash, sizeof(hash));
527 }
528 }
529
530 if (rep->msg_type == DHCPV6_MSG_RECONF && !rcauth_ok)
531 return false;
532
533 return clientid_ok && (serverid_ok || server_id_len == 0);
534 }
535
536
537 int dhcpv6_poll_reconfigure(void)
538 {
539 int ret = dhcpv6_request(DHCPV6_MSG_UNKNOWN);
540 if (ret != -1)
541 ret = dhcpv6_request(ret);
542
543 return ret;
544 }
545
546
547 static int dhcpv6_handle_reconfigure(_unused enum dhcpv6_msg orig,
548 const void *opt, const void *end)
549 {
550 // TODO: should verify the reconfigure message
551 uint16_t otype, olen;
552 uint8_t *odata, msg = DHCPV6_MSG_RENEW;
553 dhcpv6_for_each_option(opt, end, otype, olen, odata)
554 if (otype == DHCPV6_OPT_RECONF_MESSAGE && olen == 1 && (
555 odata[0] == DHCPV6_MSG_RENEW ||
556 odata[0] == DHCPV6_MSG_INFO_REQ))
557 msg = odata[0];
558
559 dhcpv6_handle_reply(DHCPV6_MSG_UNKNOWN, NULL, NULL);
560 return msg;
561 }
562
563
564 // Collect all advertised servers
565 static int dhcpv6_handle_advert(enum dhcpv6_msg orig,
566 const void *opt, const void *end)
567 {
568 uint16_t olen, otype;
569 uint8_t *odata;
570 struct dhcpv6_server_cand cand = {false, false, 0, 0, {0}, NULL, NULL, 0, 0};
571 bool have_na = false, have_pd = false;
572
573 dhcpv6_for_each_option(opt, end, otype, olen, odata) {
574 if (orig == DHCPV6_MSG_SOLICIT &&
575 (otype == DHCPV6_OPT_IA_PD || otype == DHCPV6_OPT_IA_NA) &&
576 olen > sizeof(struct dhcpv6_ia_hdr)) {
577 struct dhcpv6_ia_hdr *ia_hdr = (void*)(&odata[-4]);
578 dhcpv6_parse_ia(&ia_hdr[1], odata + olen);
579 }
580
581 if (otype == DHCPV6_OPT_SERVERID && olen <= 130) {
582 memcpy(cand.duid, odata, olen);
583 cand.duid_len = olen;
584 } else if (otype == DHCPV6_OPT_STATUS && olen >= 2 && !odata[0]
585 && odata[1] == DHCPV6_NoPrefixAvail) {
586 cand.preference -= 2000;
587 } else if (otype == DHCPV6_OPT_PREF && olen >= 1 &&
588 cand.preference >= 0) {
589 cand.preference = odata[0];
590 } else if (otype == DHCPV6_OPT_RECONF_ACCEPT) {
591 cand.wants_reconfigure = true;
592 } else if (otype == DHCPV6_OPT_IA_PD && request_prefix) {
593 struct dhcpv6_ia_hdr *h = (struct dhcpv6_ia_hdr*)&odata[-4];
594 uint8_t *oend = odata + olen, *d;
595 dhcpv6_for_each_option(&h[1], oend, otype, olen, d)
596 if (otype == DHCPV6_OPT_IA_PREFIX)
597 have_pd = true;
598 } else if (otype == DHCPV6_OPT_IA_NA) {
599 struct dhcpv6_ia_hdr *h = (struct dhcpv6_ia_hdr*)&odata[-4];
600 uint8_t *oend = odata + olen, *d;
601 dhcpv6_for_each_option(&h[1], oend, otype, olen, d)
602 if (otype == DHCPV6_OPT_IA_ADDR)
603 have_na = true;
604 }
605 }
606
607 if ((!have_na && na_mode == IA_MODE_FORCE) ||
608 (!have_pd && pd_mode == IA_MODE_FORCE))
609 return -1;
610
611 if (na_mode != IA_MODE_NONE && !have_na) {
612 cand.has_noaddravail = true;
613 cand.preference -= 1000;
614 }
615
616 if (pd_mode != IA_MODE_NONE) {
617 if (have_pd)
618 cand.preference += 2000;
619 else
620 cand.preference -= 2000;
621 }
622
623 if (cand.duid_len > 0) {
624 cand.ia_na = odhcp6c_move_state(STATE_IA_NA, &cand.ia_na_len);
625 cand.ia_pd = odhcp6c_move_state(STATE_IA_PD, &cand.ia_pd_len);
626 odhcp6c_add_state(STATE_SERVER_CAND, &cand, sizeof(cand));
627 }
628
629 if (orig == DHCPV6_MSG_SOLICIT) {
630 odhcp6c_clear_state(STATE_IA_NA);
631 odhcp6c_clear_state(STATE_IA_PD);
632 }
633
634 return -1;
635 }
636
637
638 static int dhcpv6_commit_advert(void)
639 {
640 size_t cand_len;
641 struct dhcpv6_server_cand *c = NULL, *cand =
642 odhcp6c_get_state(STATE_SERVER_CAND, &cand_len);
643
644 bool retry = false;
645 for (size_t i = 0; i < cand_len / sizeof(*c); ++i) {
646 if (cand[i].has_noaddravail)
647 retry = true; // We want to try again
648
649 if (!c || c->preference < cand[i].preference)
650 c = &cand[i];
651 }
652
653 if (retry && na_mode == IA_MODE_TRY) {
654 // We give it a second try without the IA_NA
655 na_mode = IA_MODE_NONE;
656 return dhcpv6_request(DHCPV6_MSG_SOLICIT);
657 }
658
659 if (c) {
660 uint16_t hdr[2] = {htons(DHCPV6_OPT_SERVERID),
661 htons(c->duid_len)};
662 odhcp6c_add_state(STATE_SERVER_ID, hdr, sizeof(hdr));
663 odhcp6c_add_state(STATE_SERVER_ID, c->duid, c->duid_len);
664 accept_reconfig = c->wants_reconfigure;
665 odhcp6c_add_state(STATE_IA_NA, c->ia_na, c->ia_na_len);
666 odhcp6c_add_state(STATE_IA_PD, c->ia_pd, c->ia_pd_len);
667 }
668
669 for (size_t i = 0; i < cand_len / sizeof(*c); ++i) {
670 free(cand[i].ia_na);
671 free(cand[i].ia_pd);
672 }
673 odhcp6c_clear_state(STATE_SERVER_CAND);
674
675 if (!c)
676 return -1;
677 else if (request_prefix || na_mode != IA_MODE_NONE)
678 return DHCPV6_STATEFUL;
679 else
680 return DHCPV6_STATELESS;
681 }
682
683
684 static int dhcpv6_handle_rebind_reply(enum dhcpv6_msg orig,
685 const void *opt, const void *end)
686 {
687 dhcpv6_handle_advert(orig, opt, end);
688 if (dhcpv6_commit_advert() < 0) {
689 dhcpv6_handle_reply(DHCPV6_MSG_UNKNOWN, NULL, NULL);
690 return -1;
691 }
692
693 return dhcpv6_handle_reply(orig, opt, end);
694 }
695
696
697 static int dhcpv6_handle_reply(enum dhcpv6_msg orig,
698 const void *opt, const void *end)
699 {
700 uint8_t *odata;
701 uint16_t otype, olen;
702
703 odhcp6c_expire();
704
705 if (orig == DHCPV6_MSG_UNKNOWN) {
706 static time_t last_update = 0;
707 time_t now = odhcp6c_get_milli_time() / 1000;
708
709 uint32_t elapsed = (last_update > 0) ? now - last_update : 0;
710 last_update = now;
711
712 t1 -= elapsed;
713 t2 -= elapsed;
714 t3 -= elapsed;
715
716 if (t1 < 0)
717 t1 = 0;
718
719 if (t2 < 0)
720 t2 = 0;
721
722 if (t3 < 0)
723 t3 = 0;
724 } else {
725 t1 = t2 = t3 = UINT32_MAX;
726 }
727
728 if (orig == DHCPV6_MSG_REQUEST) {
729 // Delete NA and PD we have in the state from the Advert
730 odhcp6c_clear_state(STATE_IA_NA);
731 odhcp6c_clear_state(STATE_IA_PD);
732 }
733
734 if (opt) {
735 odhcp6c_clear_state(STATE_DNS);
736 odhcp6c_clear_state(STATE_SEARCH);
737 odhcp6c_clear_state(STATE_SNTP_IP);
738 odhcp6c_clear_state(STATE_SNTP_FQDN);
739 odhcp6c_clear_state(STATE_SIP_IP);
740 odhcp6c_clear_state(STATE_SIP_FQDN);
741 odhcp6c_clear_state(STATE_AFTR_NAME);
742 }
743
744 // Parse and find all matching IAs
745 dhcpv6_for_each_option(opt, end, otype, olen, odata) {
746 if ((otype == DHCPV6_OPT_IA_PD || otype == DHCPV6_OPT_IA_NA)
747 && olen > sizeof(struct dhcpv6_ia_hdr)) {
748 struct dhcpv6_ia_hdr *ia_hdr = (void*)(&odata[-4]);
749 uint32_t l_t1 = ntohl(ia_hdr->t1);
750 uint32_t l_t2 = ntohl(ia_hdr->t2);
751
752 // Test ID and T1-T2 validity
753 if (ia_hdr->iaid != 1 || l_t2 < l_t1)
754 continue;
755
756 int error = 0;
757 uint16_t stype, slen;
758 uint8_t *sdata;
759 // Test status and bail if error
760 dhcpv6_for_each_option(&ia_hdr[1], odata + olen,
761 stype, slen, sdata)
762 if (stype == DHCPV6_OPT_STATUS && slen >= 2)
763 error = ((int)sdata[0]) << 8 | ((int)sdata[1]);
764
765 if (error) {
766 syslog(LOG_WARNING, "Server returned IAID status %i!", error);
767 if (error != 2)
768 raise(SIGUSR2);
769 break;
770 }
771
772 uint32_t n = dhcpv6_parse_ia(&ia_hdr[1], odata + olen);
773
774 if (!l_t1)
775 l_t1 = 300;
776
777 if (!l_t2)
778 l_t2 = 600;
779
780 if (n < t3)
781 t3 = n;
782
783 // Update times
784 if (l_t1 > 0 && t1 > l_t1)
785 t1 = l_t1;
786
787 if (l_t2 > 0 && t2 > l_t2)
788 t2 = l_t2;
789
790 } else if (otype == DHCPV6_OPT_DNS_SERVERS) {
791 if (olen % 16 == 0)
792 odhcp6c_add_state(STATE_DNS, odata, olen);
793 } else if (otype == DHCPV6_OPT_DNS_DOMAIN) {
794 odhcp6c_add_state(STATE_SEARCH, odata, olen);
795 } else if (otype == DHCPV6_OPT_NTP_SERVER) {
796 uint16_t stype, slen;
797 uint8_t *sdata;
798 // Test status and bail if error
799 dhcpv6_for_each_option(odata, odata + olen,
800 stype, slen, sdata) {
801 if (slen == 16 && (stype == NTP_MC_ADDR ||
802 stype == NTP_SRV_ADDR))
803 odhcp6c_add_state(STATE_SNTP_IP,
804 sdata, slen);
805 else if (slen > 0 && stype == NTP_SRV_FQDN)
806 odhcp6c_add_state(STATE_SNTP_FQDN,
807 sdata, slen);
808 }
809 } else if (otype == DHCPV6_OPT_SIP_SERVER_A) {
810 if (olen == 16)
811 odhcp6c_add_state(STATE_SIP_IP, odata, olen);
812 } else if (otype == DHCPV6_OPT_SIP_SERVER_D) {
813 odhcp6c_add_state(STATE_SIP_FQDN, odata, olen);
814 } else if (otype == DHCPV6_OPT_INFO_REFRESH && olen >= 4) {
815 uint32_t refresh = ntohl(*((uint32_t*)odata));
816 if (refresh < (uint32_t)t1)
817 t1 = refresh;
818 } else if (otype == DHCPV6_OPT_AUTH && olen == -4 +
819 sizeof(struct dhcpv6_auth_reconfigure)) {
820 struct dhcpv6_auth_reconfigure *r = (void*)&odata[-4];
821 if (r->protocol == 3 && r->algorithm == 1 &&
822 r->reconf_type == 1)
823 memcpy(reconf_key, r->key, sizeof(r->key));
824 } else if (otype == DHCPV6_OPT_AFTR_NAME && olen > 3) {
825 size_t cur_len;
826 odhcp6c_get_state(STATE_AFTR_NAME, &cur_len);
827 if (cur_len == 0)
828 odhcp6c_add_state(STATE_AFTR_NAME, odata, olen);
829 } else if (otype != DHCPV6_OPT_CLIENTID &&
830 otype != DHCPV6_OPT_SERVERID) {
831 odhcp6c_add_state(STATE_CUSTOM_OPTS,
832 &odata[-4], olen + 4);
833 }
834 }
835
836 if (t1 == UINT32_MAX)
837 t1 = 300;
838
839 if (t2 == UINT32_MAX)
840 t2 = 600;
841
842 if (t3 == UINT32_MAX)
843 t3 = 3600;
844
845 return true;
846 }
847
848
849 static uint32_t dhcpv6_parse_ia(void *opt, void *end)
850 {
851 uint32_t timeout = UINT32_MAX; // Minimum timeout
852 uint16_t otype, olen;
853 uint8_t *odata;
854
855 // Update address IA
856 dhcpv6_for_each_option(opt, end, otype, olen, odata) {
857 struct odhcp6c_entry entry = {IN6ADDR_ANY_INIT, 0, 0,
858 IN6ADDR_ANY_INIT, 0, 0, 0};
859
860 if (otype == DHCPV6_OPT_IA_PREFIX) {
861 struct dhcpv6_ia_prefix *prefix = (void*)&odata[-4];
862 if (olen + 4U < sizeof(*prefix))
863 continue;
864
865 entry.valid = ntohl(prefix->valid);
866 entry.preferred = ntohl(prefix->preferred);
867
868 if (entry.preferred > entry.valid)
869 continue;
870
871 entry.length = prefix->prefix;
872 entry.target = prefix->addr;
873 uint16_t stype, slen;
874 uint8_t *sdata;
875
876 #ifdef EXT_PREFIX_CLASS
877 // Find prefix class, if any
878 dhcpv6_for_each_option(&prefix[1], odata + olen,
879 stype, slen, sdata)
880 if (stype == DHCPV6_OPT_PREFIX_CLASS && slen == 2)
881 entry.class = sdata[0] << 8 | sdata[1];
882 #endif
883
884 // Parse PD-exclude
885 bool ok = true;
886 dhcpv6_for_each_option(odata + sizeof(*prefix) - 4U,
887 odata + olen, stype, slen, sdata) {
888 if (stype != DHCPV6_OPT_PD_EXCLUDE || slen < 2)
889 continue;
890
891 uint8_t elen = sdata[0];
892 if (elen > 64)
893 elen = 64;
894
895 if (elen <= 32 || elen <= entry.length) {
896 ok = false;
897 continue;
898 }
899
900
901 uint8_t bytes = ((elen - entry.length - 1) / 8) + 1;
902 if (slen <= bytes) {
903 ok = false;
904 continue;
905 }
906
907 uint32_t exclude = 0;
908 do {
909 exclude = exclude << 8 | sdata[bytes];
910 } while (--bytes);
911
912 exclude >>= 8 - ((elen - entry.length) % 8);
913 exclude <<= 64 - elen;
914
915 // Abusing router & priority fields for exclusion
916 entry.router = entry.target;
917 entry.router.s6_addr32[1] |= htonl(exclude);
918 entry.priority = elen;
919 }
920
921 if (ok)
922 odhcp6c_update_entry(STATE_IA_PD, &entry);
923
924 entry.priority = 0;
925 memset(&entry.router, 0, sizeof(entry.router));
926 } else if (otype == DHCPV6_OPT_IA_ADDR) {
927 struct dhcpv6_ia_addr *addr = (void*)&odata[-4];
928 if (olen + 4U < sizeof(*addr))
929 continue;
930
931 entry.preferred = ntohl(addr->preferred);
932 entry.valid = ntohl(addr->valid);
933
934 if (entry.preferred > entry.valid)
935 continue;
936
937 entry.length = 128;
938 entry.target = addr->addr;
939
940 #ifdef EXT_PREFIX_CLASS
941 uint16_t stype, slen;
942 uint8_t *sdata;
943 // Find prefix class, if any
944 dhcpv6_for_each_option(&addr[1], odata + olen,
945 stype, slen, sdata)
946 if (stype == DHCPV6_OPT_PREFIX_CLASS && slen == 2)
947 entry.class = sdata[0] << 8 | sdata[1];
948 #endif
949
950 odhcp6c_update_entry(STATE_IA_NA, &entry);
951 }
952 if (entry.valid > 0 && timeout > entry.valid)
953 timeout = entry.valid;
954 }
955
956 return timeout;
957 }