b727818139a1aba2f5fffd9032a9a26856a6be26
[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 <arpa/inet.h>
30 #include <netinet/in.h>
31
32 #include <net/if.h>
33 #include <net/ethernet.h>
34
35 #include "odhcp6c.h"
36 #include "md5.h"
37
38
39 #define ALL_DHCPV6_RELAYS {{{0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
40 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02}}}
41 #define DHCPV6_CLIENT_PORT 546
42 #define DHCPV6_SERVER_PORT 547
43 #define DHCPV6_DUID_LLADDR 3
44 #define DHCPV6_REQ_DELAY 1
45
46
47 static bool dhcpv6_response_is_valid(const void *buf, ssize_t len,
48 const uint8_t transaction[3], enum dhcpv6_msg type,
49 const struct in6_addr *daddr);
50
51 static int dhcpv6_parse_ia(void *opt, void *end);
52
53 static int dhcpv6_calc_refresh_timers(void);
54 static void dhcpv6_handle_status_code(_unused const enum dhcpv6_msg orig,
55 const uint16_t code, const void *status_msg, const int len,
56 int *ret);
57 static void dhcpv6_handle_ia_status_code(const enum dhcpv6_msg orig,
58 const struct dhcpv6_ia_hdr *ia_hdr, const uint16_t code,
59 const void *status_msg, const int len,
60 bool handled_status_codes[_DHCPV6_Status_Max],
61 int *ret);
62
63 static reply_handler dhcpv6_handle_reply;
64 static reply_handler dhcpv6_handle_advert;
65 static reply_handler dhcpv6_handle_rebind_reply;
66 static reply_handler dhcpv6_handle_reconfigure;
67 static int dhcpv6_commit_advert(void);
68
69
70
71 // RFC 3315 - 5.5 Timeout and Delay values
72 static struct dhcpv6_retx dhcpv6_retx[_DHCPV6_MSG_MAX] = {
73 [DHCPV6_MSG_UNKNOWN] = {false, 1, 120, 0, "<POLL>",
74 dhcpv6_handle_reconfigure, NULL},
75 [DHCPV6_MSG_SOLICIT] = {true, 1, 3600, 0, "SOLICIT",
76 dhcpv6_handle_advert, dhcpv6_commit_advert},
77 [DHCPV6_MSG_REQUEST] = {true, 1, 30, 10, "REQUEST",
78 dhcpv6_handle_reply, NULL},
79 [DHCPV6_MSG_RENEW] = {false, 10, 600, 0, "RENEW",
80 dhcpv6_handle_reply, NULL},
81 [DHCPV6_MSG_REBIND] = {false, 10, 600, 0, "REBIND",
82 dhcpv6_handle_rebind_reply, NULL},
83 [DHCPV6_MSG_RELEASE] = {false, 1, 0, 5, "RELEASE", NULL, NULL},
84 [DHCPV6_MSG_DECLINE] = {false, 1, 0, 5, "DECLINE", NULL, NULL},
85 [DHCPV6_MSG_INFO_REQ] = {true, 1, 120, 0, "INFOREQ",
86 dhcpv6_handle_reply, NULL},
87 };
88
89
90 // Sockets
91 static int sock = -1;
92 static int ifindex = -1;
93 static int64_t t1 = 0, t2 = 0, t3 = 0;
94
95 // IA states
96 static int request_prefix = -1;
97 static enum odhcp6c_ia_mode na_mode = IA_MODE_NONE, pd_mode = IA_MODE_NONE;
98 static bool accept_reconfig = false;
99 // Server unicast address
100 static struct in6_addr server_addr = IN6ADDR_ANY_INIT;
101
102 // Reconfigure key
103 static uint8_t reconf_key[16];
104
105
106 int init_dhcpv6(const char *ifname, int request_pd, int sol_timeout)
107 {
108 request_prefix = request_pd;
109 dhcpv6_retx[DHCPV6_MSG_SOLICIT].max_timeo = sol_timeout;
110
111 sock = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
112 if (sock < 0)
113 return -1;
114
115 // Detect interface
116 struct ifreq ifr;
117 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
118 if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0)
119 return -1;
120 ifindex = ifr.ifr_ifindex;
121
122 // Create client DUID
123 size_t client_id_len;
124 odhcp6c_get_state(STATE_CLIENT_ID, &client_id_len);
125 if (client_id_len == 0) {
126 uint8_t duid[14] = {0, DHCPV6_OPT_CLIENTID, 0, 10, 0,
127 DHCPV6_DUID_LLADDR, 0, 1};
128
129 if (ioctl(sock, SIOCGIFHWADDR, &ifr) >= 0)
130 memcpy(&duid[8], ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
131
132 uint8_t zero[ETHER_ADDR_LEN] = {0, 0, 0, 0, 0, 0};
133 struct ifreq ifs[100], *ifp, *ifend;
134 struct ifconf ifc;
135 ifc.ifc_req = ifs;
136 ifc.ifc_len = sizeof(ifs);
137
138 if (!memcmp(&duid[8], zero, ETHER_ADDR_LEN) &&
139 ioctl(sock, SIOCGIFCONF, &ifc) >= 0) {
140 // If our interface doesn't have an address...
141 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
142 for (ifp = ifc.ifc_req; ifp < ifend &&
143 !memcmp(&duid[8], zero, ETHER_ADDR_LEN); ifp++) {
144 memcpy(ifr.ifr_name, ifp->ifr_name,
145 sizeof(ifr.ifr_name));
146 if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0)
147 continue;
148 memcpy(&duid[8], ifr.ifr_hwaddr.sa_data,
149 ETHER_ADDR_LEN);
150 }
151 }
152
153 odhcp6c_add_state(STATE_CLIENT_ID, duid, sizeof(duid));
154 }
155
156 // Create ORO
157 uint16_t oro[] = {
158 htons(DHCPV6_OPT_SIP_SERVER_D),
159 htons(DHCPV6_OPT_SIP_SERVER_A),
160 htons(DHCPV6_OPT_DNS_SERVERS),
161 htons(DHCPV6_OPT_DNS_DOMAIN),
162 htons(DHCPV6_OPT_UNICAST),
163 htons(DHCPV6_OPT_NTP_SERVER),
164 htons(DHCPV6_OPT_AFTR_NAME),
165 htons(DHCPV6_OPT_PD_EXCLUDE),
166 #ifdef EXT_PREFIX_CLASS
167 htons(DHCPV6_OPT_PREFIX_CLASS),
168 #endif
169 };
170 odhcp6c_add_state(STATE_ORO, oro, sizeof(oro));
171
172 do {
173 // Configure IPv6-options
174 int val = 1;
175 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val)) < 0)
176 break;
177 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0)
178 break;
179 if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &val, sizeof(val)) < 0)
180 break;
181
182 val = 0;
183 if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val, sizeof(val)) < 0)
184 break;
185 if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname)) < 0)
186 break;
187
188 struct sockaddr_in6 client_addr = { .sin6_family = AF_INET6,
189 .sin6_port = htons(DHCPV6_CLIENT_PORT), .sin6_flowinfo = 0 };
190 if (bind(sock, (struct sockaddr*)&client_addr, sizeof(client_addr)) < 0)
191 break;
192
193 return 0;
194 } while (0);
195
196 return -1;
197 }
198
199
200 void dhcpv6_set_ia_mode(enum odhcp6c_ia_mode na, enum odhcp6c_ia_mode pd)
201 {
202 na_mode = na;
203 pd_mode = pd;
204 }
205
206
207 static void dhcpv6_send(enum dhcpv6_msg type, uint8_t trid[3], uint32_t ecs)
208 {
209 // Build FQDN
210 char fqdn_buf[256];
211 gethostname(fqdn_buf, sizeof(fqdn_buf));
212 struct {
213 uint16_t type;
214 uint16_t len;
215 uint8_t flags;
216 uint8_t data[256];
217 } fqdn;
218 size_t fqdn_len = 5 + dn_comp(fqdn_buf, fqdn.data,
219 sizeof(fqdn.data), NULL, NULL);
220 fqdn.type = htons(DHCPV6_OPT_FQDN);
221 fqdn.len = htons(fqdn_len - 4);
222 fqdn.flags = 0;
223
224
225 // Build Client ID
226 size_t cl_id_len;
227 void *cl_id = odhcp6c_get_state(STATE_CLIENT_ID, &cl_id_len);
228
229 // Get Server ID
230 size_t srv_id_len;
231 void *srv_id = odhcp6c_get_state(STATE_SERVER_ID, &srv_id_len);
232
233 // Build IA_PDs
234 size_t ia_pd_entries, ia_pd_len = 0;
235 struct odhcp6c_entry *e = odhcp6c_get_state(STATE_IA_PD, &ia_pd_entries);
236 ia_pd_entries /= sizeof(*e);
237 struct dhcpv6_ia_hdr hdr_ia_pd = {
238 htons(DHCPV6_OPT_IA_PD),
239 htons(sizeof(hdr_ia_pd) - 4),
240 1, 0, 0
241 };
242
243
244 uint8_t *ia_pd = alloca(ia_pd_entries * (sizeof(struct dhcpv6_ia_prefix) + 10));
245 for (size_t i = 0; i < ia_pd_entries; ++i) {
246 uint8_t ex_len = 0;
247 if (e[i].priority > 0)
248 ex_len = ((e[i].priority - e[i].length - 1) / 8) + 6;
249
250 struct dhcpv6_ia_prefix p = {
251 .type = htons(DHCPV6_OPT_IA_PREFIX),
252 .len = htons(sizeof(p) - 4U + ex_len),
253 .prefix = e[i].length,
254 .addr = e[i].target
255 };
256
257 memcpy(ia_pd + ia_pd_len, &p, sizeof(p));
258 ia_pd_len += sizeof(p);
259
260 if (ex_len) {
261 ia_pd[ia_pd_len++] = 0;
262 ia_pd[ia_pd_len++] = DHCPV6_OPT_PD_EXCLUDE;
263 ia_pd[ia_pd_len++] = 0;
264 ia_pd[ia_pd_len++] = ex_len - 4;
265 ia_pd[ia_pd_len++] = e[i].priority;
266
267 uint32_t excl = ntohl(e[i].router.s6_addr32[1]);
268 excl >>= (64 - e[i].priority);
269 excl <<= 8 - ((e[i].priority - e[i].length) % 8);
270
271 for (size_t i = ex_len - 5; i > 0; --i, excl >>= 8)
272 ia_pd[ia_pd_len + i] = excl & 0xff;
273 ia_pd_len += ex_len - 5;
274 }
275 }
276
277 struct dhcpv6_ia_prefix pref = {
278 .type = htons(DHCPV6_OPT_IA_PREFIX),
279 .len = htons(25), .prefix = request_prefix
280 };
281 if (request_prefix > 0 && ia_pd_len == 0 && type == DHCPV6_MSG_SOLICIT) {
282 ia_pd = (uint8_t*)&pref;
283 ia_pd_len = sizeof(pref);
284 }
285 hdr_ia_pd.len = htons(ntohs(hdr_ia_pd.len) + ia_pd_len);
286
287 // Build IA_NAs
288 size_t ia_na_entries, ia_na_len = 0;
289 void *ia_na = NULL;
290 e = odhcp6c_get_state(STATE_IA_NA, &ia_na_entries);
291 ia_na_entries /= sizeof(*e);
292
293 struct dhcpv6_ia_hdr hdr_ia_na = {
294 htons(DHCPV6_OPT_IA_NA),
295 htons(sizeof(hdr_ia_na) - 4),
296 1, 0, 0
297 };
298
299 struct dhcpv6_ia_addr pa[ia_na_entries];
300 for (size_t i = 0; i < ia_na_entries; ++i) {
301 pa[i].type = htons(DHCPV6_OPT_IA_ADDR);
302 pa[i].len = htons(sizeof(pa[i]) - 4U);
303 pa[i].addr = e[i].target;
304 pa[i].preferred = 0;
305 pa[i].valid = 0;
306 }
307
308 ia_na = pa;
309 ia_na_len = sizeof(pa);
310 hdr_ia_na.len = htons(ntohs(hdr_ia_na.len) + ia_na_len);
311
312 // Reconfigure Accept
313 struct {
314 uint16_t type;
315 uint16_t length;
316 } reconf_accept = {htons(DHCPV6_OPT_RECONF_ACCEPT), 0};
317
318 // Request Information Refresh
319 uint16_t oro_refresh = htons(DHCPV6_OPT_INFO_REFRESH);
320
321 // Prepare Header
322 size_t oro_len;
323 void *oro = odhcp6c_get_state(STATE_ORO, &oro_len);
324 struct {
325 uint8_t type;
326 uint8_t trid[3];
327 uint16_t elapsed_type;
328 uint16_t elapsed_len;
329 uint16_t elapsed_value;
330 uint16_t oro_type;
331 uint16_t oro_len;
332 } hdr = {
333 type, {trid[0], trid[1], trid[2]},
334 htons(DHCPV6_OPT_ELAPSED), htons(2),
335 htons((ecs > 0xffff) ? 0xffff : ecs),
336 htons(DHCPV6_OPT_ORO), htons(oro_len),
337 };
338
339 struct iovec iov[] = {
340 {&hdr, sizeof(hdr)},
341 {oro, oro_len},
342 {&oro_refresh, 0},
343 {cl_id, cl_id_len},
344 {srv_id, srv_id_len},
345 {&reconf_accept, sizeof(reconf_accept)},
346 {&fqdn, fqdn_len},
347 {&hdr_ia_na, sizeof(hdr_ia_na)},
348 {ia_na, ia_na_len},
349 {&hdr_ia_pd, sizeof(hdr_ia_pd)},
350 {ia_pd, ia_pd_len},
351 };
352
353 size_t cnt = ARRAY_SIZE(iov);
354 if (type == DHCPV6_MSG_INFO_REQ) {
355 cnt = 5;
356 iov[2].iov_len = sizeof(oro_refresh);
357 hdr.oro_len = htons(oro_len + sizeof(oro_refresh));
358 } else if (!request_prefix) {
359 cnt = 9;
360 }
361
362 // Disable IAs if not used
363 if (type != DHCPV6_MSG_SOLICIT) {
364 iov[5].iov_len = 0;
365 if (ia_na_len == 0)
366 iov[7].iov_len = 0;
367 if (ia_pd_len == 0)
368 iov[9].iov_len = 0;
369 }
370
371 if (na_mode == IA_MODE_NONE)
372 iov[7].iov_len = 0;
373
374 struct sockaddr_in6 srv = {AF_INET6, htons(DHCPV6_SERVER_PORT),
375 0, ALL_DHCPV6_RELAYS, ifindex};
376 struct msghdr msg = {&srv, sizeof(srv), iov, cnt, NULL, 0, 0};
377
378 switch (type) {
379 case DHCPV6_MSG_REQUEST:
380 case DHCPV6_MSG_RENEW:
381 case DHCPV6_MSG_RELEASE:
382 case DHCPV6_MSG_DECLINE:
383 if (!IN6_IS_ADDR_UNSPECIFIED(&server_addr) &&
384 odhcp6c_addr_in_scope(&server_addr)) {
385 srv.sin6_addr = server_addr;
386 if (!IN6_IS_ADDR_LINKLOCAL(&server_addr))
387 srv.sin6_scope_id = 0;
388 }
389 break;
390 default:
391 break;
392 }
393
394 if (sendmsg(sock, &msg, 0) < 0) {
395 char in6_str[INET6_ADDRSTRLEN];
396
397 syslog(LOG_ERR, "Failed to send DHCPV6 message to %s (%s)",
398 inet_ntop(AF_INET6, (const void *)&srv.sin6_addr,
399 in6_str, sizeof(in6_str)), strerror(errno));
400 }
401 }
402
403
404 static int64_t dhcpv6_rand_delay(int64_t time)
405 {
406 int random;
407 odhcp6c_random(&random, sizeof(random));
408 return (time * ((int64_t)random % 1000LL)) / 10000LL;
409 }
410
411
412 int dhcpv6_request(enum dhcpv6_msg type)
413 {
414 uint8_t rc = 0;
415 uint64_t timeout = UINT32_MAX;
416 struct dhcpv6_retx *retx = &dhcpv6_retx[type];
417
418 if (retx->delay) {
419 struct timespec ts = {0, 0};
420 ts.tv_nsec = dhcpv6_rand_delay(10 * DHCPV6_REQ_DELAY);
421 nanosleep(&ts, NULL);
422 }
423
424 if (type == DHCPV6_MSG_UNKNOWN)
425 timeout = t1;
426 else if (type == DHCPV6_MSG_RENEW)
427 timeout = (t2 > t1) ? t2 - t1 : 0;
428 else if (type == DHCPV6_MSG_REBIND)
429 timeout = (t3 > t2) ? t3 - t2 : 0;
430
431 if (timeout == 0)
432 return -1;
433
434 syslog(LOG_NOTICE, "Starting %s transaction (timeout %llus, max rc %d)",
435 retx->name, (unsigned long long)timeout, retx->max_rc);
436
437 uint64_t start = odhcp6c_get_milli_time(), round_start = start, elapsed;
438
439 // Generate transaction ID
440 uint8_t trid[3] = {0, 0, 0};
441 if (type != DHCPV6_MSG_UNKNOWN)
442 odhcp6c_random(trid, sizeof(trid));
443 ssize_t len = -1;
444 int64_t rto = 0;
445
446 do {
447 if (rto == 0) {
448 int64_t delay = dhcpv6_rand_delay(retx->init_timeo * 1000);
449
450 // First RT MUST be strictly greater than IRT for solicit messages (RFC3313 17.1.2)
451 while (type == DHCPV6_MSG_SOLICIT && delay <= 0)
452 delay = dhcpv6_rand_delay(retx->init_timeo * 1000);
453
454 rto = (retx->init_timeo * 1000 + delay);
455 }
456 else
457 rto = (2 * rto + dhcpv6_rand_delay(rto));
458
459 if (retx->max_timeo && (rto >= retx->max_timeo * 1000))
460 rto = retx->max_timeo * 1000 +
461 dhcpv6_rand_delay(retx->max_timeo * 1000);
462
463 // Calculate end for this round and elapsed time
464 uint64_t round_end = round_start + rto;
465 elapsed = round_start - start;
466
467 // Don't wait too long
468 if (round_end - start > timeout * 1000)
469 round_end = timeout * 1000 + start;
470
471 // Built and send package
472 if (type != DHCPV6_MSG_UNKNOWN) {
473 if (type != DHCPV6_MSG_SOLICIT)
474 syslog(LOG_NOTICE, "Send %s message (elapsed %llums, rc %d)",
475 retx->name, (unsigned long long)elapsed, rc);
476 dhcpv6_send(type, trid, elapsed / 10);
477 rc++;
478 }
479
480 // Receive rounds
481 for (; len < 0 && round_start < round_end;
482 round_start = odhcp6c_get_milli_time()) {
483 uint8_t buf[1536], cmsg_buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
484 struct iovec iov = {buf, sizeof(buf)};
485 struct msghdr msg = {NULL, 0, &iov, 1,
486 cmsg_buf, sizeof(cmsg_buf), 0};
487 struct in6_pktinfo *pktinfo = NULL;
488
489 // Check for pending signal
490 if (odhcp6c_signal_process())
491 return -1;
492
493 // Set timeout for receiving
494 uint64_t t = round_end - round_start;
495 struct timeval timeout = {t / 1000, (t % 1000) * 1000};
496 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
497 &timeout, sizeof(timeout));
498
499 // Receive cycle
500 len = recvmsg(sock, &msg, 0);
501 if (len < 0)
502 continue;
503
504 for (struct cmsghdr *ch = CMSG_FIRSTHDR(&msg); ch != NULL;
505 ch = CMSG_NXTHDR(&msg, ch)) {
506 if (ch->cmsg_level == SOL_IPV6 &&
507 ch->cmsg_type == IPV6_PKTINFO) {
508 pktinfo = (struct in6_pktinfo *)CMSG_DATA(ch);
509 break;
510 }
511 }
512
513 if (pktinfo == NULL) {
514 len = -1;
515 continue;
516 }
517
518 if (!dhcpv6_response_is_valid(buf, len, trid,
519 type, &pktinfo->ipi6_addr)) {
520 len = -1;
521 continue;
522 }
523
524 uint8_t *opt = &buf[4];
525 uint8_t *opt_end = opt + len - 4;
526
527 round_start = odhcp6c_get_milli_time();
528 elapsed = round_start - start;
529 syslog(LOG_NOTICE, "Got a valid reply after "
530 "%llums", (unsigned long long)elapsed);
531
532 if (retx->handler_reply)
533 len = retx->handler_reply(type, rc, opt, opt_end);
534
535 if (len > 0 && round_end - round_start > 1000)
536 round_end = 1000 + round_start;
537 }
538
539 // Allow
540 if (retx->handler_finish)
541 len = retx->handler_finish();
542 } while (len < 0 && ((elapsed / 1000 < timeout) && (!retx->max_rc || rc < retx->max_rc)));
543
544 return len;
545 }
546
547 // Message validation checks according to RFC3315 chapter 15
548 static bool dhcpv6_response_is_valid(const void *buf, ssize_t len,
549 const uint8_t transaction[3], enum dhcpv6_msg type,
550 const struct in6_addr *daddr)
551 {
552 const struct dhcpv6_header *rep = buf;
553 if (len < (ssize_t)sizeof(*rep) || memcmp(rep->tr_id,
554 transaction, sizeof(rep->tr_id)))
555 return false; // Invalid reply
556
557 if (type == DHCPV6_MSG_SOLICIT) {
558 if (rep->msg_type != DHCPV6_MSG_ADVERT &&
559 rep->msg_type != DHCPV6_MSG_REPLY)
560 return false;
561 } else if (type == DHCPV6_MSG_UNKNOWN) {
562 if (!accept_reconfig || rep->msg_type != DHCPV6_MSG_RECONF)
563 return false;
564 } else if (rep->msg_type != DHCPV6_MSG_REPLY) {
565 return false;
566 }
567
568 uint8_t *end = ((uint8_t*)buf) + len, *odata,
569 rcmsg = DHCPV6_MSG_UNKNOWN;
570 uint16_t otype, olen;
571 bool clientid_ok = false, serverid_ok = false, rcauth_ok = false,
572 ia_present = false, options_valid = true;
573
574 size_t client_id_len, server_id_len;
575 void *client_id = odhcp6c_get_state(STATE_CLIENT_ID, &client_id_len);
576 void *server_id = odhcp6c_get_state(STATE_SERVER_ID, &server_id_len);
577
578 dhcpv6_for_each_option(&rep[1], end, otype, olen, odata) {
579 if (otype == DHCPV6_OPT_CLIENTID) {
580 clientid_ok = (olen + 4U == client_id_len) && !memcmp(
581 &odata[-4], client_id, client_id_len);
582 } else if (otype == DHCPV6_OPT_SERVERID) {
583 if (server_id_len)
584 serverid_ok = (olen + 4U == server_id_len) && !memcmp(
585 &odata[-4], server_id, server_id_len);
586 else
587 serverid_ok = true;
588 } else if (otype == DHCPV6_OPT_AUTH && olen == -4 +
589 sizeof(struct dhcpv6_auth_reconfigure)) {
590 struct dhcpv6_auth_reconfigure *r = (void*)&odata[-4];
591 if (r->protocol != 3 || r->algorithm != 1 || r->reconf_type != 2)
592 continue;
593
594 md5_state_t md5;
595 uint8_t serverhash[16], secretbytes[16], hash[16];
596 memcpy(serverhash, r->key, sizeof(serverhash));
597 memset(r->key, 0, sizeof(r->key));
598 memcpy(secretbytes, reconf_key, sizeof(secretbytes));
599
600 for (size_t i = 0; i < sizeof(secretbytes); ++i)
601 secretbytes[i] ^= 0x36;
602
603 md5_init(&md5);
604 md5_append(&md5, secretbytes, sizeof(secretbytes));
605 md5_append(&md5, buf, len);
606 md5_finish(&md5, hash);
607
608 for (size_t i = 0; i < sizeof(secretbytes); ++i) {
609 secretbytes[i] ^= 0x36;
610 secretbytes[i] ^= 0x5c;
611 }
612
613 md5_init(&md5);
614 md5_append(&md5, secretbytes, sizeof(secretbytes));
615 md5_append(&md5, hash, 16);
616 md5_finish(&md5, hash);
617
618 rcauth_ok = !memcmp(hash, serverhash, sizeof(hash));
619 } else if (otype == DHCPV6_OPT_RECONF_MESSAGE && olen == 1) {
620 rcmsg = odata[0];
621 } else if ((otype == DHCPV6_OPT_IA_PD || otype == DHCPV6_OPT_IA_NA)) {
622 ia_present = true;
623 if (olen < sizeof(struct dhcpv6_ia_hdr))
624 options_valid = false;
625 }
626 else if ((otype == DHCPV6_OPT_IA_ADDR) || (otype == DHCPV6_OPT_IA_PREFIX) ||
627 (otype == DHCPV6_OPT_PD_EXCLUDE)) {
628 // Options are not allowed on global level
629 options_valid = false;
630 }
631 }
632
633 if (!options_valid)
634 return false;
635
636 if (type == DHCPV6_MSG_INFO_REQ && ia_present)
637 return false;
638
639 if (rep->msg_type == DHCPV6_MSG_RECONF) {
640 if ((rcmsg != DHCPV6_MSG_RENEW && rcmsg != DHCPV6_MSG_INFO_REQ) ||
641 (rcmsg == DHCPV6_MSG_INFO_REQ && ia_present) ||
642 !rcauth_ok || IN6_IS_ADDR_MULTICAST(daddr))
643 return false;
644 }
645
646 return clientid_ok && serverid_ok;
647 }
648
649
650 int dhcpv6_poll_reconfigure(void)
651 {
652 int ret = dhcpv6_request(DHCPV6_MSG_UNKNOWN);
653 if (ret != -1)
654 ret = dhcpv6_request(ret);
655
656 return ret;
657 }
658
659
660 static int dhcpv6_handle_reconfigure(_unused enum dhcpv6_msg orig, const int rc,
661 const void *opt, const void *end)
662 {
663 uint16_t otype, olen;
664 uint8_t *odata, msg = DHCPV6_MSG_RENEW;
665 dhcpv6_for_each_option(opt, end, otype, olen, odata)
666 if (otype == DHCPV6_OPT_RECONF_MESSAGE && olen == 1 && (
667 odata[0] == DHCPV6_MSG_RENEW ||
668 odata[0] == DHCPV6_MSG_INFO_REQ))
669 msg = odata[0];
670
671 dhcpv6_handle_reply(DHCPV6_MSG_UNKNOWN, rc, NULL, NULL);
672 return msg;
673 }
674
675
676 // Collect all advertised servers
677 static int dhcpv6_handle_advert(enum dhcpv6_msg orig, const int rc,
678 const void *opt, const void *end)
679 {
680 uint16_t olen, otype;
681 uint8_t *odata, pref = 0;
682 struct dhcpv6_server_cand cand = {false, false, 0, 0, {0},
683 IN6ADDR_ANY_INIT, NULL, NULL, 0, 0};
684 bool have_na = false;
685 int have_pd = 0;
686
687 dhcpv6_for_each_option(opt, end, otype, olen, odata) {
688 if (orig == DHCPV6_MSG_SOLICIT &&
689 (otype == DHCPV6_OPT_IA_PD || otype == DHCPV6_OPT_IA_NA) &&
690 olen > sizeof(struct dhcpv6_ia_hdr)) {
691 struct dhcpv6_ia_hdr *ia_hdr = (void*)(&odata[-4]);
692 dhcpv6_parse_ia(ia_hdr, odata + olen + sizeof(*ia_hdr));
693 }
694
695 if (otype == DHCPV6_OPT_SERVERID && olen <= 130) {
696 memcpy(cand.duid, odata, olen);
697 cand.duid_len = olen;
698 } else if (otype == DHCPV6_OPT_STATUS && olen >= 2) {
699 int error = ((int)odata[0] << 8 | (int)odata[1]);
700
701 switch (error) {
702 case DHCPV6_NoPrefixAvail:
703 // Status code on global level
704 if (pd_mode == IA_MODE_FORCE)
705 return -1;
706 cand.preference -= 2000;
707 break;
708
709 case DHCPV6_NoAddrsAvail:
710 // Status code on global level
711 if (na_mode == IA_MODE_FORCE)
712 return -1;
713 break;
714
715 default :
716 break;
717 }
718 } else if (otype == DHCPV6_OPT_PREF && olen >= 1 &&
719 cand.preference >= 0) {
720 cand.preference = pref = odata[0];
721 } else if (otype == DHCPV6_OPT_UNICAST && olen == sizeof(cand.server_addr)) {
722 cand.server_addr = *(struct in6_addr *)odata;
723 } else if (otype == DHCPV6_OPT_RECONF_ACCEPT) {
724 cand.wants_reconfigure = true;
725 } else if (otype == DHCPV6_OPT_IA_PD && request_prefix) {
726 struct dhcpv6_ia_hdr *h = (struct dhcpv6_ia_hdr*)&odata[-4];
727 uint8_t *oend = odata + olen, *d;
728 dhcpv6_for_each_option(&h[1], oend, otype, olen, d) {
729 if (otype == DHCPV6_OPT_IA_PREFIX && (olen + 4) >=
730 (uint16_t)sizeof(struct dhcpv6_ia_prefix)) {
731 struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix*)&d[-4];
732 have_pd = p->prefix;
733 }
734 }
735 } else if (otype == DHCPV6_OPT_IA_NA) {
736 struct dhcpv6_ia_hdr *h = (struct dhcpv6_ia_hdr*)&odata[-4];
737 uint8_t *oend = odata + olen, *d;
738 dhcpv6_for_each_option(&h[1], oend, otype, olen, d)
739 if (otype == DHCPV6_OPT_IA_ADDR)
740 have_na = true;
741 }
742 }
743
744 if ((!have_na && na_mode == IA_MODE_FORCE) ||
745 (!have_pd && pd_mode == IA_MODE_FORCE))
746 return -1;
747
748 if (na_mode != IA_MODE_NONE && !have_na) {
749 cand.has_noaddravail = true;
750 cand.preference -= 1000;
751 }
752
753 if (pd_mode != IA_MODE_NONE) {
754 if (have_pd)
755 cand.preference += 2000 + (128 - have_pd);
756 else
757 cand.preference -= 2000;
758 }
759
760 if (cand.duid_len > 0) {
761 cand.ia_na = odhcp6c_move_state(STATE_IA_NA, &cand.ia_na_len);
762 cand.ia_pd = odhcp6c_move_state(STATE_IA_PD, &cand.ia_pd_len);
763 odhcp6c_add_state(STATE_SERVER_CAND, &cand, sizeof(cand));
764 }
765
766 return (rc > 1 || (pref == 255 && cand.preference > 0)) ? 1 : -1;
767 }
768
769
770 static int dhcpv6_commit_advert(void)
771 {
772 size_t cand_len;
773 struct dhcpv6_server_cand *c = NULL, *cand =
774 odhcp6c_get_state(STATE_SERVER_CAND, &cand_len);
775
776 bool retry = false;
777 for (size_t i = 0; i < cand_len / sizeof(*c); ++i) {
778 if (cand[i].has_noaddravail)
779 retry = true; // We want to try again
780
781 if (!c || c->preference < cand[i].preference)
782 c = &cand[i];
783 }
784
785 if (retry && na_mode == IA_MODE_TRY) {
786 // We give it a second try without the IA_NA
787 na_mode = IA_MODE_NONE;
788 return dhcpv6_request(DHCPV6_MSG_SOLICIT);
789 }
790
791 if (c) {
792 uint16_t hdr[2] = {htons(DHCPV6_OPT_SERVERID),
793 htons(c->duid_len)};
794 odhcp6c_add_state(STATE_SERVER_ID, hdr, sizeof(hdr));
795 odhcp6c_add_state(STATE_SERVER_ID, c->duid, c->duid_len);
796 accept_reconfig = c->wants_reconfigure;
797 server_addr = c->server_addr;
798 if (c->ia_na_len)
799 odhcp6c_add_state(STATE_IA_NA, c->ia_na, c->ia_na_len);
800 if (c->ia_pd_len)
801 odhcp6c_add_state(STATE_IA_PD, c->ia_pd, c->ia_pd_len);
802 }
803
804 for (size_t i = 0; i < cand_len / sizeof(*c); ++i) {
805 free(cand[i].ia_na);
806 free(cand[i].ia_pd);
807 }
808 odhcp6c_clear_state(STATE_SERVER_CAND);
809
810 if (!c)
811 return -1;
812 else if ((request_prefix && c->ia_pd_len) || (na_mode != IA_MODE_NONE && c->ia_na_len))
813 return DHCPV6_STATEFUL;
814 else
815 return DHCPV6_STATELESS;
816 }
817
818
819 static int dhcpv6_handle_rebind_reply(enum dhcpv6_msg orig, const int rc,
820 const void *opt, const void *end)
821 {
822 dhcpv6_handle_advert(orig, rc, opt, end);
823 if (dhcpv6_commit_advert() < 0)
824 return -1;
825
826 return dhcpv6_handle_reply(orig, rc, opt, end);
827 }
828
829
830 static int dhcpv6_handle_reply(enum dhcpv6_msg orig, _unused const int rc,
831 const void *opt, const void *end)
832 {
833 uint8_t *odata;
834 uint16_t otype, olen;
835 uint32_t refresh = UINT32_MAX;
836 int ret = 1;
837 bool handled_status_codes[_DHCPV6_Status_Max] = { false, };
838
839 odhcp6c_expire();
840
841 if (orig == DHCPV6_MSG_UNKNOWN) {
842 static time_t last_update = 0;
843 time_t now = odhcp6c_get_milli_time() / 1000;
844
845 uint32_t elapsed = (last_update > 0) ? now - last_update : 0;
846 last_update = now;
847
848 t1 -= elapsed;
849 t2 -= elapsed;
850 t3 -= elapsed;
851
852 if (t1 < 0)
853 t1 = 0;
854
855 if (t2 < 0)
856 t2 = 0;
857
858 if (t3 < 0)
859 t3 = 0;
860 }
861
862 if (orig == DHCPV6_MSG_REQUEST && !odhcp6c_is_bound()) {
863 // Delete NA and PD we have in the state from the Advert
864 odhcp6c_clear_state(STATE_IA_NA);
865 odhcp6c_clear_state(STATE_IA_PD);
866 }
867
868 if (opt) {
869 odhcp6c_clear_state(STATE_DNS);
870 odhcp6c_clear_state(STATE_SEARCH);
871 odhcp6c_clear_state(STATE_SNTP_IP);
872 odhcp6c_clear_state(STATE_SNTP_FQDN);
873 odhcp6c_clear_state(STATE_SIP_IP);
874 odhcp6c_clear_state(STATE_SIP_FQDN);
875 odhcp6c_clear_state(STATE_AFTR_NAME);
876 }
877
878 // Parse and find all matching IAs
879 dhcpv6_for_each_option(opt, end, otype, olen, odata) {
880 if ((otype == DHCPV6_OPT_IA_PD || otype == DHCPV6_OPT_IA_NA)
881 && olen > sizeof(struct dhcpv6_ia_hdr)) {
882 struct dhcpv6_ia_hdr *ia_hdr = (void*)(&odata[-4]);
883
884 // Test ID
885 if (ia_hdr->iaid != 1)
886 continue;
887
888 uint16_t code = DHCPV6_Success;
889 uint16_t stype, slen;
890 uint8_t *sdata;
891 // Get and handle status code
892 dhcpv6_for_each_option(&ia_hdr[1], odata + olen,
893 stype, slen, sdata) {
894 if (stype == DHCPV6_OPT_STATUS && slen >= 2) {
895 uint8_t *mdata = (slen > 2) ? &sdata[2] : NULL;
896 uint16_t mlen = (slen > 2) ? slen - 2 : 0;
897
898 code = ((int)sdata[0]) << 8 | ((int)sdata[1]);
899
900 if (code == DHCPV6_Success)
901 continue;
902
903 dhcpv6_handle_ia_status_code(orig, ia_hdr,
904 code, mdata, mlen, handled_status_codes, &ret);
905
906
907 if (ret > 0)
908 return ret;
909 break;
910 }
911 }
912
913 if (code != DHCPV6_Success)
914 continue;
915
916 dhcpv6_parse_ia(ia_hdr, odata + olen + sizeof(*ia_hdr));
917 } else if (otype == DHCPV6_OPT_UNICAST && olen == sizeof(server_addr)) {
918 server_addr = *(struct in6_addr *)odata;
919 } else if (otype == DHCPV6_OPT_STATUS && olen >= 2) {
920 uint8_t *mdata = (olen > 2) ? &odata[2] : NULL;
921 uint16_t mlen = (olen > 2) ? olen - 2 : 0;
922 uint16_t code = ((int)odata[0]) << 8 | ((int)odata[1]);
923
924 dhcpv6_handle_status_code(orig, code, mdata, mlen, &ret);
925 }
926 else if (otype == DHCPV6_OPT_DNS_SERVERS) {
927 if (olen % 16 == 0)
928 odhcp6c_add_state(STATE_DNS, odata, olen);
929 } else if (otype == DHCPV6_OPT_DNS_DOMAIN) {
930 odhcp6c_add_state(STATE_SEARCH, odata, olen);
931 } else if (otype == DHCPV6_OPT_NTP_SERVER) {
932 uint16_t stype, slen;
933 uint8_t *sdata;
934 // Test status and bail if error
935 dhcpv6_for_each_option(odata, odata + olen,
936 stype, slen, sdata) {
937 if (slen == 16 && (stype == NTP_MC_ADDR ||
938 stype == NTP_SRV_ADDR))
939 odhcp6c_add_state(STATE_SNTP_IP,
940 sdata, slen);
941 else if (slen > 0 && stype == NTP_SRV_FQDN)
942 odhcp6c_add_state(STATE_SNTP_FQDN,
943 sdata, slen);
944 }
945 } else if (otype == DHCPV6_OPT_SIP_SERVER_A) {
946 if (olen == 16)
947 odhcp6c_add_state(STATE_SIP_IP, odata, olen);
948 } else if (otype == DHCPV6_OPT_SIP_SERVER_D) {
949 odhcp6c_add_state(STATE_SIP_FQDN, odata, olen);
950 } else if (otype == DHCPV6_OPT_INFO_REFRESH && olen >= 4) {
951 refresh = ntohl(*((uint32_t*)odata));
952 } else if (otype == DHCPV6_OPT_AUTH && olen == -4 +
953 sizeof(struct dhcpv6_auth_reconfigure)) {
954 struct dhcpv6_auth_reconfigure *r = (void*)&odata[-4];
955 if (r->protocol == 3 && r->algorithm == 1 &&
956 r->reconf_type == 1)
957 memcpy(reconf_key, r->key, sizeof(r->key));
958 } else if (otype == DHCPV6_OPT_AFTR_NAME && olen > 3) {
959 size_t cur_len;
960 odhcp6c_get_state(STATE_AFTR_NAME, &cur_len);
961 if (cur_len == 0)
962 odhcp6c_add_state(STATE_AFTR_NAME, odata, olen);
963 } else if (otype != DHCPV6_OPT_CLIENTID &&
964 otype != DHCPV6_OPT_SERVERID) {
965 odhcp6c_add_state(STATE_CUSTOM_OPTS,
966 &odata[-4], olen + 4);
967 }
968 }
969
970 if (orig != DHCPV6_MSG_INFO_REQ) {
971 // Update refresh timers if no fatal status code was received
972 if ((ret > 0) && dhcpv6_calc_refresh_timers()) {
973 switch (orig) {
974 case DHCPV6_MSG_RENEW:
975 // Send further renews if T1 is not set
976 if (!t1)
977 ret = -1;
978 break;
979 case DHCPV6_MSG_REBIND:
980 // Send further rebinds if T1 and T2 is not set
981 if (!t1 && !t2)
982 ret = -1;
983 break;
984
985 default :
986 break;
987 }
988 }
989 }
990 else if (ret > 0)
991 t1 = refresh;
992
993 return ret;
994 }
995
996
997 static int dhcpv6_parse_ia(void *opt, void *end)
998 {
999 struct dhcpv6_ia_hdr *ia_hdr = (struct dhcpv6_ia_hdr *)opt;
1000 int parsed_ia = 0;
1001 uint32_t t1, t2;
1002 uint16_t otype, olen;
1003 uint8_t *odata;
1004
1005 t1 = ntohl(ia_hdr->t1);
1006 t2 = ntohl(ia_hdr->t2);
1007
1008 if (t1 > t2)
1009 return 0;
1010
1011 // Update address IA
1012 dhcpv6_for_each_option(&ia_hdr[1], end, otype, olen, odata) {
1013 struct odhcp6c_entry entry = {IN6ADDR_ANY_INIT, 0, 0,
1014 IN6ADDR_ANY_INIT, 0, 0, 0, 0, 0};
1015
1016 if (otype == DHCPV6_OPT_IA_PREFIX) {
1017 struct dhcpv6_ia_prefix *prefix = (void*)&odata[-4];
1018 if (olen + 4U < sizeof(*prefix))
1019 continue;
1020
1021 entry.valid = ntohl(prefix->valid);
1022 entry.preferred = ntohl(prefix->preferred);
1023
1024 if (entry.preferred > entry.valid)
1025 continue;
1026
1027 entry.t1 = (t1 ? t1 : (entry.preferred != UINT32_MAX ? 0.5 * entry.preferred : UINT32_MAX));
1028 entry.t2 = (t2 ? t2 : (entry.preferred != UINT32_MAX ? 0.8 * entry.preferred : UINT32_MAX));
1029 if (entry.t1 > entry.t2)
1030 entry.t1 = entry.t2;
1031
1032 entry.length = prefix->prefix;
1033 entry.target = prefix->addr;
1034 uint16_t stype, slen;
1035 uint8_t *sdata;
1036
1037 #ifdef EXT_PREFIX_CLASS
1038 // Find prefix class, if any
1039 dhcpv6_for_each_option(&prefix[1], odata + olen,
1040 stype, slen, sdata)
1041 if (stype == DHCPV6_OPT_PREFIX_CLASS && slen == 2)
1042 entry.class = sdata[0] << 8 | sdata[1];
1043 #endif
1044
1045 // Parse PD-exclude
1046 bool ok = true;
1047 dhcpv6_for_each_option(odata + sizeof(*prefix) - 4U,
1048 odata + olen, stype, slen, sdata) {
1049 if (stype != DHCPV6_OPT_PD_EXCLUDE || slen < 2)
1050 continue;
1051
1052 uint8_t elen = sdata[0];
1053 if (elen > 64)
1054 elen = 64;
1055
1056 if (elen <= 32 || elen <= entry.length) {
1057 ok = false;
1058 continue;
1059 }
1060
1061
1062 uint8_t bytes = ((elen - entry.length - 1) / 8) + 1;
1063 if (slen <= bytes) {
1064 ok = false;
1065 continue;
1066 }
1067
1068 uint32_t exclude = 0;
1069 do {
1070 exclude = exclude << 8 | sdata[bytes];
1071 } while (--bytes);
1072
1073 exclude >>= 8 - ((elen - entry.length) % 8);
1074 exclude <<= 64 - elen;
1075
1076 // Abusing router & priority fields for exclusion
1077 entry.router = entry.target;
1078 entry.router.s6_addr32[1] |= htonl(exclude);
1079 entry.priority = elen;
1080 }
1081
1082 if (ok) {
1083 odhcp6c_update_entry(STATE_IA_PD, &entry);
1084 parsed_ia++;
1085 }
1086
1087 entry.priority = 0;
1088 memset(&entry.router, 0, sizeof(entry.router));
1089 } else if (otype == DHCPV6_OPT_IA_ADDR) {
1090 struct dhcpv6_ia_addr *addr = (void*)&odata[-4];
1091 if (olen + 4U < sizeof(*addr))
1092 continue;
1093
1094 entry.preferred = ntohl(addr->preferred);
1095 entry.valid = ntohl(addr->valid);
1096
1097 if (entry.preferred > entry.valid)
1098 continue;
1099
1100 entry.t1 = (t1 ? t1 : (entry.preferred != UINT32_MAX ? 0.5 * entry.preferred : UINT32_MAX));
1101 entry.t2 = (t2 ? t2 : (entry.preferred != UINT32_MAX ? 0.8 * entry.preferred : UINT32_MAX));
1102 if (entry.t1 > entry.t2)
1103 entry.t1 = entry.t2;
1104
1105 entry.length = 128;
1106 entry.target = addr->addr;
1107
1108 #ifdef EXT_PREFIX_CLASS
1109 uint16_t stype, slen;
1110 uint8_t *sdata;
1111 // Find prefix class, if any
1112 dhcpv6_for_each_option(&addr[1], odata + olen,
1113 stype, slen, sdata)
1114 if (stype == DHCPV6_OPT_PREFIX_CLASS && slen == 2)
1115 entry.class = sdata[0] << 8 | sdata[1];
1116 #endif
1117
1118 odhcp6c_update_entry(STATE_IA_NA, &entry);
1119 parsed_ia++;
1120 }
1121 }
1122 return parsed_ia;
1123 }
1124
1125
1126 static int dhcpv6_calc_refresh_timers(void)
1127 {
1128 struct odhcp6c_entry *e;
1129 size_t ia_na_entries, ia_pd_entries, i;
1130 int64_t l_t1 = UINT32_MAX, l_t2 = UINT32_MAX, l_t3 = 0;
1131
1132 e = odhcp6c_get_state(STATE_IA_NA, &ia_na_entries);
1133 ia_na_entries /= sizeof(*e);
1134 for (i = 0; i < ia_na_entries; i++) {
1135 if (e[i].t1 < l_t1)
1136 l_t1 = e[i].t1;
1137
1138 if (e[i].t2 < l_t2)
1139 l_t2 = e[i].t2;
1140
1141 if (e[i].valid > l_t3)
1142 l_t3 = e[i].valid;
1143 }
1144
1145 e = odhcp6c_get_state(STATE_IA_PD, &ia_pd_entries);
1146 ia_pd_entries /= sizeof(*e);
1147 for (i = 0; i < ia_pd_entries; i++) {
1148 if (e[i].t1 < l_t1)
1149 l_t1 = e[i].t1;
1150
1151 if (e[i].t2 < l_t2)
1152 l_t2 = e[i].t2;
1153
1154 if (e[i].valid > l_t3)
1155 l_t3 = e[i].valid;
1156 }
1157
1158 if (ia_pd_entries || ia_na_entries) {
1159 t1 = l_t1;
1160 t2 = l_t2;
1161 t3 = l_t3;
1162 }
1163
1164 return (int)(ia_pd_entries + ia_na_entries);
1165 }
1166
1167
1168 static void dhcpv6_log_status_code(const uint16_t code, const char *scope,
1169 const void *status_msg, const int len)
1170 {
1171 uint8_t buf[len + 3];
1172
1173 memset(buf, 0, sizeof(buf));
1174 if (len) {
1175 buf[0] = '(';
1176 memcpy(&buf[1], status_msg, len);
1177 buf[len + 1] = ')';
1178 }
1179
1180 syslog(LOG_WARNING, "Server returned %s status %i %s",
1181 scope, code, buf);
1182 }
1183
1184
1185 static void dhcpv6_handle_status_code(const enum dhcpv6_msg orig,
1186 const uint16_t code, const void *status_msg, const int len,
1187 int *ret)
1188 {
1189 dhcpv6_log_status_code(code, "message", status_msg, len);
1190
1191 switch (code) {
1192 case DHCPV6_UnspecFail:
1193 // Generic failure
1194 *ret = 0;
1195 break;
1196
1197 case DHCPV6_UseMulticast:
1198 switch(orig) {
1199 case DHCPV6_MSG_REQUEST:
1200 case DHCPV6_MSG_RENEW:
1201 case DHCPV6_MSG_RELEASE:
1202 case DHCPV6_MSG_DECLINE:
1203 // Message needs to be retransmitted according to RFC3315 chapter 18.1.8
1204 server_addr = in6addr_any;
1205 *ret = 0;
1206 break;
1207 default:
1208 break;
1209 }
1210 break;
1211
1212 case DHCPV6_NoAddrsAvail:
1213 case DHCPV6_NoPrefixAvail:
1214 if (orig == DHCPV6_MSG_REQUEST)
1215 *ret = 0; // Failure
1216 break;
1217
1218 default:
1219 break;
1220 }
1221 }
1222
1223
1224 static void dhcpv6_handle_ia_status_code(const enum dhcpv6_msg orig,
1225 const struct dhcpv6_ia_hdr *ia_hdr, const uint16_t code,
1226 const void *status_msg, const int len,
1227 bool handled_status_codes[_DHCPV6_Status_Max], int *ret)
1228 {
1229 dhcpv6_log_status_code(code, ia_hdr->type == DHCPV6_OPT_IA_NA ?
1230 "IA_NA" : "IA_PD", status_msg, len);
1231
1232 switch (code) {
1233 case DHCPV6_NoBinding:
1234 switch (orig) {
1235 case DHCPV6_MSG_RENEW:
1236 case DHCPV6_MSG_REBIND:
1237 if ((*ret > 0) && !handled_status_codes[code])
1238 *ret = dhcpv6_request(DHCPV6_MSG_REQUEST);
1239 break;
1240
1241 default:
1242 break;
1243 }
1244 break;
1245
1246 case DHCPV6_NoAddrsAvail:
1247 case DHCPV6_NoPrefixAvail:
1248 switch (orig) {
1249 case DHCPV6_MSG_REQUEST:
1250 if (*ret != 0)
1251 *ret = 0;
1252 break;
1253 default:
1254 break;
1255 }
1256 break;
1257
1258 case DHCPV6_NotOnLink:
1259 // TODO handle not onlink in case of confirm
1260 break;
1261
1262 default:
1263 break;
1264 }
1265 }