2 * Copyright (C) 2012 Steven Barth <steven@midlink.org>
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.
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.
26 #include <sys/ioctl.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
31 #include <net/ethernet.h>
36 #define ALL_DHCPV6_RELAYS {{{0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
37 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02}}}
38 #define DHCPV6_CLIENT_PORT 546
39 #define DHCPV6_SERVER_PORT 547
40 #define DHCPV6_DUID_LLADDR 3
41 #define DHCPV6_REQ_DELAY 1
44 static bool dhcpv6_response_is_valid(const void *buf
, ssize_t len
,
45 const uint8_t transaction
[3], enum dhcpv6_msg type
);
47 static time_t dhcpv6_parse_ia(void *opt
, void *end
);
49 static reply_handler dhcpv6_handle_reply
;
50 static reply_handler dhcpv6_handle_advert
;
51 static reply_handler dhcpv6_handle_rebind_reply
;
52 static reply_handler dhcpv6_handle_reconfigure
;
53 static int dhcpv6_commit_advert(uint32_t elapsed
);
57 // RFC 3315 - 5.5 Timeout and Delay values
58 static struct dhcpv6_retx dhcpv6_retx
[_DHCPV6_MSG_MAX
] = {
59 [DHCPV6_MSG_UNKNOWN
] = {false, 1, 120, "<POLL>",
60 dhcpv6_handle_reconfigure
, NULL
},
61 [DHCPV6_MSG_SOLICIT
] = {true, 1, 120, "SOLICIT",
62 dhcpv6_handle_advert
, dhcpv6_commit_advert
},
63 [DHCPV6_MSG_REQUEST
] = {true, 30, 10, "REQUEST",
64 dhcpv6_handle_reply
, NULL
},
65 [DHCPV6_MSG_RENEW
] = {false, 10, 600, "RENEW",
66 dhcpv6_handle_reply
, NULL
},
67 [DHCPV6_MSG_REBIND
] = {false, 10, 600, "REBIND",
68 dhcpv6_handle_rebind_reply
, NULL
},
69 [DHCPV6_MSG_RELEASE
] = {false, 1, 600, "RELEASE", NULL
, NULL
},
70 [DHCPV6_MSG_DECLINE
] = {false, 1, 3, "DECLINE", NULL
, NULL
},
71 [DHCPV6_MSG_INFO_REQ
] = {true, 1, 120, "INFOREQ",
72 dhcpv6_handle_reply
, NULL
},
78 static int urandom_fd
= -1;
79 static int ifindex
= -1;
80 static time_t t1
= 0, t2
= 0, t3
= 0;
83 static int request_prefix
= -1;
84 static enum odhcp6c_ia_mode na_mode
= IA_MODE_NONE
;
85 static bool accept_reconfig
= false;
89 int init_dhcpv6(const char *ifname
, int request_pd
)
91 request_prefix
= request_pd
;
92 urandom_fd
= open("/dev/urandom", O_CLOEXEC
| O_RDONLY
);
96 sock
= socket(AF_INET6
, SOCK_DGRAM
| SOCK_CLOEXEC
, IPPROTO_UDP
);
100 strncpy(ifr
.ifr_name
, ifname
, sizeof(ifr
.ifr_name
));
101 if (ioctl(sock
, SIOCGIFINDEX
, &ifr
))
103 ifindex
= ifr
.ifr_ifindex
;
105 // Create client DUID
106 size_t client_id_len
;
107 odhcp6c_get_state(STATE_CLIENT_ID
, &client_id_len
);
108 if (client_id_len
== 0) {
109 ioctl(sock
, SIOCGIFHWADDR
, &ifr
);
110 uint8_t duid
[14] = {0, DHCPV6_OPT_CLIENTID
, 0, 10, 0,
111 DHCPV6_DUID_LLADDR
, 0, 1};
112 memcpy(&duid
[8], ifr
.ifr_hwaddr
.sa_data
, ETHER_ADDR_LEN
);
113 odhcp6c_add_state(STATE_CLIENT_ID
, duid
, sizeof(duid
));
117 uint16_t oro
[] = {htons(DHCPV6_OPT_DNS_SERVERS
),
118 htons(DHCPV6_OPT_DNS_DOMAIN
)};
119 odhcp6c_add_state(STATE_ORO
, oro
, sizeof(oro
));
122 // Configure IPv6-options
124 setsockopt(sock
, IPPROTO_IPV6
, IPV6_V6ONLY
, &val
, sizeof(val
));
125 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
126 setsockopt(sock
, SOL_SOCKET
, SO_BINDTODEVICE
, ifname
, strlen(ifname
));
128 struct sockaddr_in6 client_addr
= { .sin6_family
= AF_INET6
,
129 .sin6_port
= htons(DHCPV6_CLIENT_PORT
), .sin6_flowinfo
= 0 };
130 if (bind(sock
, (struct sockaddr
*)&client_addr
, sizeof(client_addr
)))
137 void dhcpv6_set_ia_na_mode(enum odhcp6c_ia_mode mode
)
143 void dhcpv6_remove_addrs(void)
146 uint8_t *odata
, *ia_na
= odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
147 uint16_t otype
, olen
;
148 dhcpv6_for_each_option(ia_na
, ia_na
+ ia_na_len
, otype
, olen
, odata
) {
149 struct dhcpv6_ia_addr
*addr
= (void*)&odata
[-4];
150 set_rtnetlink_addr(ifindex
, &addr
->addr
, 0, 0);
155 static void dhcpv6_send(enum dhcpv6_msg type
, uint8_t trid
[3], uint32_t ecs
)
159 gethostname(fqdn_buf
, sizeof(fqdn_buf
));
166 size_t fqdn_len
= 5 + dn_comp(fqdn_buf
, fqdn
.data
,
167 sizeof(fqdn
.data
), NULL
, NULL
);
168 fqdn
.type
= htons(DHCPV6_OPT_FQDN
);
169 fqdn
.len
= htons(fqdn_len
- 4);
175 void *cl_id
= odhcp6c_get_state(STATE_CLIENT_ID
, &cl_id_len
);
179 void *srv_id
= odhcp6c_get_state(STATE_SERVER_ID
, &srv_id_len
);
183 void *ia_pd
= odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
184 struct dhcpv6_ia_hdr hdr_ia_pd
= {
185 htons(DHCPV6_OPT_IA_PD
),
186 htons(sizeof(hdr_ia_pd
) - 4 + ia_pd_len
),
190 struct dhcpv6_ia_prefix pref
= {
191 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
192 .len
= htons(25), .prefix
= request_prefix
195 if (ia_pd_len
== 0 && request_prefix
> 0 &&
196 (type
== DHCPV6_MSG_SOLICIT
||
197 type
== DHCPV6_MSG_REQUEST
)) {
199 ia_pd_len
= sizeof(pref
);
204 void *ia_na
= odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
205 struct dhcpv6_ia_hdr hdr_ia_na
= {
206 htons(DHCPV6_OPT_IA_NA
),
207 htons(sizeof(hdr_ia_na
) - 4 + ia_na_len
),
211 // Reconfigure Accept
215 } reconf_accept
= {htons(DHCPV6_OPT_RECONF_ACCEPT
), 0};
217 // Request Information Refresh
218 uint16_t oro_refresh
= htons(DHCPV6_OPT_INFO_REFRESH
);
222 void *oro
= odhcp6c_get_state(STATE_ORO
, &oro_len
);
226 uint16_t elapsed_type
;
227 uint16_t elapsed_len
;
228 uint16_t elapsed_value
;
232 type
, {trid
[0], trid
[1], trid
[2]},
233 htons(DHCPV6_OPT_ELAPSED
), htons(2),
234 htons((ecs
> 0xffff) ? 0xffff : ecs
),
235 htons(DHCPV6_OPT_ORO
), htons(oro_len
),
238 struct iovec iov
[] = {
243 {srv_id
, srv_id_len
},
246 {&hdr_ia_na
, sizeof(hdr_ia_na
)},
248 {&hdr_ia_pd
, sizeof(hdr_ia_pd
)},
252 size_t cnt
= ARRAY_SIZE(iov
);
253 if (type
== DHCPV6_MSG_INFO_REQ
) {
255 iov
[2].iov_len
= sizeof(oro_refresh
);
256 hdr
.oro_len
= htons(oro_len
+ sizeof(oro_refresh
));
257 } else if (!request_prefix
) {
261 // Disable IAs if not used
262 if (type
== DHCPV6_MSG_SOLICIT
) {
263 iov
[5].iov_len
= sizeof(reconf_accept
);
264 } else if (type
!= DHCPV6_MSG_REQUEST
) {
271 if (na_mode
== IA_MODE_NONE
)
274 struct sockaddr_in6 srv
= {AF_INET6
, htons(DHCPV6_SERVER_PORT
),
275 0, ALL_DHCPV6_RELAYS
, ifindex
};
276 struct msghdr msg
= {&srv
, sizeof(srv
), iov
, cnt
, NULL
, 0, 0};
278 sendmsg(sock
, &msg
, 0);
282 static int64_t dhcpv6_rand_delay(int64_t time
)
285 read(urandom_fd
, &random
, sizeof(random
));
286 return (time
* (random
% 1000)) / 10000;
290 int dhcpv6_request(enum dhcpv6_msg type
)
293 uint32_t timeout
= UINT32_MAX
;
294 struct dhcpv6_retx
*retx
= &dhcpv6_retx
[type
];
297 struct timespec ts
= {0, 0};
298 ts
.tv_nsec
= dhcpv6_rand_delay(10 * DHCPV6_REQ_DELAY
);
299 nanosleep(&ts
, NULL
);
302 if (type
== DHCPV6_MSG_RELEASE
|| type
== DHCPV6_MSG_DECLINE
)
304 else if (type
== DHCPV6_MSG_UNKNOWN
)
306 else if (type
== DHCPV6_MSG_RENEW
)
308 else if (type
== DHCPV6_MSG_REBIND
)
314 syslog(LOG_NOTICE
, "Sending %s (timeout %us)", retx
->name
, timeout
);
316 uint64_t start
= adhc6c_get_milli_time(), round_start
= start
, elapsed
;
318 // Generate transaction ID
320 read(urandom_fd
, trid
, sizeof(trid
));
325 rto
= (rto
== 0) ? (retx
->init_timeo
* 1000 +
326 dhcpv6_rand_delay(retx
->init_timeo
* 1000)) :
327 (2 * rto
+ dhcpv6_rand_delay(rto
));
329 if (rto
>= retx
->max_timeo
* 1000)
330 rto
= retx
->max_timeo
* 1000 +
331 dhcpv6_rand_delay(retx
->max_timeo
* 1000);
333 // Calculate end for this round and elapsed time
334 uint64_t round_end
= round_start
+ rto
;
335 elapsed
= round_start
- start
;
337 // Don't wait too long
338 if (round_end
- start
> timeout
* 1000)
339 round_end
= timeout
* 1000 + start
;
341 // Built and send package
342 if (type
!= DHCPV6_MSG_UNKNOWN
)
343 dhcpv6_send(type
, trid
, elapsed
/ 10);
346 for (; len
< 0 && round_start
< round_end
;
347 round_start
= adhc6c_get_milli_time()) {
348 // Check for pending signal
349 if (odhcp6c_signal_is_pending())
352 // Set timeout for receiving
353 uint64_t t
= round_end
- round_start
;
354 struct timeval timeout
= {t
/ 1000, (t
% 1000) * 1000};
355 setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
,
356 &timeout
, sizeof(timeout
));
359 len
= recv(sock
, buf
, sizeof(buf
), 0);
361 if (!dhcpv6_response_is_valid(buf
, len
, trid
, type
))
365 uint8_t *opt
= &buf
[4];
366 uint8_t *opt_end
= opt
+ len
- 4;
368 round_start
= adhc6c_get_milli_time();
369 elapsed
= round_start
- start
;
370 syslog(LOG_NOTICE
, "Got a valid reply after "
371 "%ums", (unsigned)elapsed
);
373 if (retx
->handler_reply
)
374 len
= retx
->handler_reply(type
,
375 opt
, opt_end
, elapsed
/ 1000);
380 if (retx
->handler_finish
)
381 len
= retx
->handler_finish(elapsed
/ 1000);
382 } while (len
< 0 && elapsed
/ 1000 < timeout
);
388 static bool dhcpv6_response_is_valid(const void *buf
, ssize_t len
,
389 const uint8_t transaction
[3], enum dhcpv6_msg type
)
391 const struct dhcpv6_header
*rep
= buf
;
392 if (len
< (ssize_t
)sizeof(*rep
) || memcmp(rep
->tr_id
,
393 transaction
, sizeof(rep
->tr_id
)))
394 return false; // Invalid reply
396 if (type
== DHCPV6_MSG_SOLICIT
) {
397 if (rep
->msg_type
!= DHCPV6_MSG_ADVERT
&&
398 rep
->msg_type
!= DHCPV6_MSG_REPLY
)
400 } else if (type
== DHCPV6_MSG_UNKNOWN
) {
401 if (!accept_reconfig
|| rep
->msg_type
!= DHCPV6_MSG_RECONF
)
403 } else if (rep
->msg_type
!= DHCPV6_MSG_REPLY
) {
407 uint8_t *end
= ((uint8_t*)buf
) + len
, *odata
;
408 uint16_t otype
, olen
;
409 bool clientid_ok
= false, serverid_ok
= false;
411 size_t client_id_len
, server_id_len
;
412 void *client_id
= odhcp6c_get_state(STATE_CLIENT_ID
, &client_id_len
);
413 void *server_id
= odhcp6c_get_state(STATE_SERVER_ID
, &server_id_len
);
415 dhcpv6_for_each_option(&rep
[1], end
, otype
, olen
, odata
)
416 if (otype
== DHCPV6_OPT_CLIENTID
)
417 clientid_ok
= (olen
+ 4U == client_id_len
) && !memcmp(
418 &odata
[-4], client_id
, client_id_len
);
419 else if (otype
== DHCPV6_OPT_SERVERID
)
420 serverid_ok
= (olen
+ 4U == server_id_len
) && !memcmp(
421 &odata
[-4], server_id
, server_id_len
);
423 return clientid_ok
&& (serverid_ok
|| server_id_len
== 0);
427 int dhcpv6_poll_reconfigure(void)
429 int ret
= dhcpv6_request(DHCPV6_MSG_UNKNOWN
);
431 ret
= dhcpv6_request(ret
);
437 static int dhcpv6_handle_reconfigure(_unused
enum dhcpv6_msg orig
,
438 const void *opt
, const void *end
, uint32_t elapsed
)
440 // TODO: should verify the reconfigure message
441 uint16_t otype
, olen
;
442 uint8_t *odata
, msg
= DHCPV6_MSG_RENEW
;
443 dhcpv6_for_each_option(opt
, end
, otype
, olen
, odata
)
444 if (otype
== DHCPV6_OPT_RECONF_MESSAGE
&& olen
== 1 && (
445 odata
[0] == DHCPV6_MSG_RENEW
||
446 odata
[0] == DHCPV6_MSG_INFO_REQ
))
462 dhcpv6_handle_reply(DHCPV6_MSG_UNKNOWN
, NULL
, NULL
, elapsed
);
467 // Collect all advertised servers
468 static int dhcpv6_handle_advert(_unused
enum dhcpv6_msg orig
,
469 const void *opt
, const void *end
, _unused
uint32_t elapsed
)
471 uint16_t olen
, otype
;
473 struct dhcpv6_server_cand cand
= {false, false, 0, 0, {0}};
475 dhcpv6_for_each_option(opt
, end
, otype
, olen
, odata
) {
476 if (otype
== DHCPV6_OPT_SERVERID
&& olen
<= 130) {
477 memcpy(cand
.duid
, odata
, olen
);
478 cand
.duid_len
= olen
;
479 } else if (otype
== DHCPV6_OPT_STATUS
&& olen
>= 2 && !odata
[0]
480 && odata
[1] == DHCPV6_NoAddrsAvail
) {
481 if (na_mode
== IA_MODE_FORCE
) {
484 cand
.has_noaddravail
= true;
485 cand
.preference
-= 1000;
487 } else if (otype
== DHCPV6_OPT_STATUS
&& olen
>= 2 && !odata
[0]
488 && odata
[1] == DHCPV6_NoPrefixAvail
) {
489 cand
.preference
-= 2000;
490 } else if (otype
== DHCPV6_OPT_PREF
&& olen
>= 1 &&
491 cand
.preference
>= 0) {
492 cand
.preference
= odata
[1];
493 } else if (otype
== DHCPV6_OPT_RECONF_ACCEPT
) {
494 cand
.wants_reconfigure
= true;
495 } else if (otype
== DHCPV6_OPT_IA_PD
&& request_prefix
) {
496 struct dhcpv6_ia_hdr
*h
= (void*)odata
;
497 uint8_t *oend
= odata
+ olen
, *d
;
498 dhcpv6_for_each_option(&h
[1], oend
, otype
, olen
, d
) {
499 if (otype
== DHCPV6_OPT_IA_PREFIX
)
500 cand
.preference
+= 2000;
501 else if (otype
== DHCPV6_OPT_STATUS
&&
502 olen
>= 2 && d
[0] == 0 &&
503 d
[1] == DHCPV6_NoPrefixAvail
)
504 cand
.preference
-= 2000;
509 if (cand
.duid_len
> 0)
510 odhcp6c_add_state(STATE_SERVER_CAND
, &cand
, sizeof(cand
));
516 static int dhcpv6_commit_advert(_unused
uint32_t elapsed
)
519 struct dhcpv6_server_cand
*c
= NULL
, *cand
=
520 odhcp6c_get_state(STATE_SERVER_CAND
, &cand_len
);
523 for (size_t i
= 0; i
< cand_len
/ sizeof(*c
); ++i
) {
524 if (cand
[i
].has_noaddravail
)
525 retry
= true; // We want to try again
527 if (!c
|| c
->preference
< cand
[i
].preference
)
531 if (retry
&& na_mode
== IA_MODE_TRY
) {
532 // We give it a second try without the IA_NA
533 na_mode
= IA_MODE_NONE
;
534 return dhcpv6_request(DHCPV6_MSG_SOLICIT
);
538 uint16_t hdr
[2] = {htons(DHCPV6_OPT_SERVERID
),
540 odhcp6c_add_state(STATE_SERVER_ID
, hdr
, sizeof(hdr
));
541 odhcp6c_add_state(STATE_SERVER_ID
, c
->duid
, c
->duid_len
);
542 accept_reconfig
= c
->wants_reconfigure
;
545 odhcp6c_clear_state(STATE_SERVER_CAND
);
549 else if (request_prefix
|| na_mode
!= IA_MODE_NONE
)
550 return DHCPV6_STATEFUL
;
552 return DHCPV6_STATELESS
;
556 static int dhcpv6_handle_rebind_reply(enum dhcpv6_msg orig
,
557 const void *opt
, const void *end
, uint32_t elapsed
)
559 dhcpv6_handle_advert(orig
, opt
, end
, elapsed
);
560 if (dhcpv6_commit_advert(elapsed
) < 0)
563 return dhcpv6_handle_reply(orig
, opt
, end
, elapsed
);
567 static int dhcpv6_handle_reply(_unused
enum dhcpv6_msg orig
,
568 const void *opt
, const void *end
, uint32_t elapsed
)
570 uint16_t otype
, olen
;
572 bool have_update
= false;
574 t1
= t2
= t3
= 86400;
576 size_t ia_na_len
, dns_len
, search_len
;
577 uint8_t *ia_na
= odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
579 odhcp6c_get_state(STATE_DNS
, &dns_len
);
580 odhcp6c_get_state(STATE_SEARCH
, &search_len
);
582 // Decrease valid and preferred lifetime of prefixes
584 uint8_t *ia_pd
= odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
585 dhcpv6_for_each_option(ia_pd
, ia_pd
+ ia_pd_len
, otype
, olen
, odata
) {
586 struct dhcpv6_ia_prefix
*p
= (void*)&odata
[-4];
587 uint32_t valid
= ntohl(p
->valid
);
588 p
->valid
= (valid
< elapsed
) ? 0 : htonl(valid
- elapsed
);
590 uint32_t pref
= ntohl(p
->preferred
);
591 p
->preferred
= (pref
< elapsed
) ? 0 : htonl(pref
- elapsed
);
594 // Decrease valid and preferred lifetime of addresses
595 dhcpv6_for_each_option(ia_na
, ia_na
+ ia_na_len
, otype
, olen
, odata
) {
596 struct dhcpv6_ia_addr
*p
= (void*)&odata
[-4];
597 uint32_t valid
= ntohl(p
->valid
);
598 p
->valid
= (valid
< elapsed
) ? 0 : htonl(valid
- elapsed
);
600 uint32_t pref
= ntohl(p
->preferred
);
601 p
->preferred
= (pref
< elapsed
) ? 0 : htonl(pref
- elapsed
);
604 // Parse and find all matching IAs
605 dhcpv6_for_each_option(opt
, end
, otype
, olen
, odata
) {
606 if ((otype
== DHCPV6_OPT_IA_PD
|| otype
== DHCPV6_OPT_IA_NA
)
607 && olen
> sizeof(struct dhcpv6_ia_hdr
)) {
608 struct dhcpv6_ia_hdr
*ia_hdr
= (void*)(&odata
[-4]);
609 time_t l_t1
= ntohl(ia_hdr
->t1
);
610 time_t l_t2
= ntohl(ia_hdr
->t2
);
612 // Test ID and T1-T2 validity
613 if (ia_hdr
->iaid
!= 1 || l_t2
< l_t1
)
616 uint16_t stype
, slen
;
618 // Test status and bail if error
619 dhcpv6_for_each_option(&ia_hdr
[1], odata
+ olen
,
621 if (stype
== DHCPV6_OPT_STATUS
&& slen
>= 2 &&
622 (sdata
[0] || sdata
[1]))
626 if (l_t1
> 0 && t1
> l_t1
)
629 if (l_t2
> 0 && t2
> l_t2
)
632 // Always report update in case we have IA_PDs so that
633 // the state-script is called with updated times
634 if (otype
== DHCPV6_OPT_IA_PD
&& request_prefix
)
637 time_t n
= dhcpv6_parse_ia(&ia_hdr
[1], odata
+ olen
);
648 } else if (otype
== DHCPV6_OPT_DNS_SERVERS
) {
649 odhcp6c_add_state(STATE_DNS
, odata
, olen
);
650 } else if (otype
== DHCPV6_OPT_DNS_DOMAIN
) {
651 odhcp6c_add_state(STATE_SEARCH
, odata
, olen
);
652 } else if (otype
== DHCPV6_OPT_INFO_REFRESH
&& olen
>= 4) {
653 uint32_t refresh
= ntohl(*((uint32_t*)odata
));
654 if (refresh
< (uint32_t)t1
)
656 } else if (otype
!= DHCPV6_OPT_CLIENTID
&&
657 otype
!= DHCPV6_OPT_SERVERID
) {
658 odhcp6c_add_state(STATE_CUSTOM_OPTS
,
659 &odata
[-4], olen
+ 4);
664 have_update
|= odhcp6c_commit_state(STATE_DNS
, dns_len
);
665 have_update
|= odhcp6c_commit_state(STATE_SEARCH
, search_len
);
666 size_t new_ia_pd_len
, new_ia_na_len
;
667 odhcp6c_get_state(STATE_IA_PD
, &new_ia_pd_len
);
668 odhcp6c_get_state(STATE_IA_NA
, &new_ia_na_len
);
669 have_update
|= (new_ia_pd_len
!= ia_pd_len
) ||
670 (new_ia_na_len
!= ia_na_len
);
673 // Delete prefixes with 0 valid-time
674 ia_pd
= odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
675 ia_end
= ia_pd
+ ia_pd_len
;
676 dhcpv6_for_each_option(ia_pd
, ia_end
, otype
, olen
, odata
) {
677 struct dhcpv6_ia_prefix
*p
= (void*)&odata
[-4];
679 ia_end
= ia_pd
+ odhcp6c_remove_state(STATE_IA_PD
,
680 (uint8_t*)p
- ia_pd
, olen
+ 4);
686 // Delete addresses with 0 valid-time
687 ia_na
= odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
688 ia_end
= ia_na
+ ia_na_len
;
689 dhcpv6_for_each_option(ia_na
, ia_end
, otype
, olen
, odata
) {
690 struct dhcpv6_ia_addr
*p
= (void*)&odata
[-4];
692 ia_end
= ia_na
+ odhcp6c_remove_state(STATE_IA_NA
,
693 (uint8_t*)p
- ia_na
, olen
+ 4);
702 static time_t dhcpv6_parse_ia(void *opt
, void *end
)
704 uint32_t timeout
= UINT32_MAX
; // Minimum timeout
705 uint16_t otype
, olen
, stype
, slen
;
706 uint8_t *odata
, *sdata
;
709 dhcpv6_for_each_option(opt
, end
, otype
, olen
, odata
) {
710 if (otype
== DHCPV6_OPT_IA_PREFIX
) {
711 struct dhcpv6_ia_prefix
*prefix
= (void*)&odata
[-4];
712 if (olen
+ 4U < sizeof(*prefix
))
715 olen
= sizeof(*prefix
); // Normalize length
716 uint32_t valid
= ntohl(prefix
->valid
);
717 uint32_t pref
= ntohl(prefix
->preferred
);
722 // Search matching IA
723 struct dhcpv6_ia_prefix
*local
= NULL
;
725 uint8_t *pd
= odhcp6c_get_state(STATE_IA_PD
, &pd_len
);
726 dhcpv6_for_each_option(pd
, pd
+ pd_len
,
728 if (!memcmp(sdata
+ 8, odata
+ 8,
729 sizeof(local
->addr
) + 1))
730 local
= (void*)&sdata
[-4];
732 if (local
) { // Already know that IA
733 local
->preferred
= prefix
->preferred
;
734 local
->valid
= prefix
->valid
;
736 odhcp6c_add_state(STATE_IA_PD
, prefix
, olen
);
742 if (prefix
->valid
== 0) // We probably lost that prefix
743 odhcp6c_add_state(STATE_IA_PD_LOST
,
745 } else if (otype
== DHCPV6_OPT_IA_ADDR
) {
746 struct dhcpv6_ia_addr
*addr
= (void*)&odata
[-4];
747 if (olen
+ 4U < sizeof(*addr
))
750 olen
= sizeof(*addr
); // Normalize length
751 uint32_t pref
= ntohl(addr
->preferred
);
752 uint32_t valid
= ntohl(addr
->valid
);
757 // Search matching IA
758 struct dhcpv6_ia_addr
*local
= NULL
;
760 uint8_t *na
= odhcp6c_get_state(STATE_IA_NA
, &na_len
);
761 dhcpv6_for_each_option(na
, na
+ na_len
,
763 if (!memcmp(sdata
, odata
, sizeof(local
->addr
)))
764 local
= (void*)&sdata
[-4];
767 if (local
) { // Already know that IA
768 local
->preferred
= addr
->preferred
;
769 local
->valid
= addr
->valid
;
771 odhcp6c_add_state(STATE_IA_NA
, addr
, olen
);
778 if (set_rtnetlink_addr(ifindex
, &addr
->addr
,
779 pref
, valid
) == -EADDRNOTAVAIL
) {
780 dhcpv6_request(DHCPV6_MSG_DECLINE
);