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 uint32_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(void);
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 ifindex
= -1;
79 static int64_t t1
= 0, t2
= 0, t3
= 0;
82 static int request_prefix
= -1;
83 static enum odhcp6c_ia_mode na_mode
= IA_MODE_NONE
;
84 static bool accept_reconfig
= false;
88 int init_dhcpv6(const char *ifname
, int request_pd
)
90 request_prefix
= request_pd
;
92 sock
= socket(AF_INET6
, SOCK_DGRAM
| SOCK_CLOEXEC
, IPPROTO_UDP
);
96 strncpy(ifr
.ifr_name
, ifname
, sizeof(ifr
.ifr_name
));
97 if (ioctl(sock
, SIOCGIFINDEX
, &ifr
))
99 ifindex
= ifr
.ifr_ifindex
;
101 // Create client DUID
102 size_t client_id_len
;
103 odhcp6c_get_state(STATE_CLIENT_ID
, &client_id_len
);
104 if (client_id_len
== 0) {
105 ioctl(sock
, SIOCGIFHWADDR
, &ifr
);
106 uint8_t duid
[14] = {0, DHCPV6_OPT_CLIENTID
, 0, 10, 0,
107 DHCPV6_DUID_LLADDR
, 0, 1};
108 memcpy(&duid
[8], ifr
.ifr_hwaddr
.sa_data
, ETHER_ADDR_LEN
);
110 uint8_t zero
[ETHER_ADDR_LEN
] = {0, 0, 0, 0, 0, 0};
111 struct ifreq ifs
[100], *ifp
, *ifend
;
114 ifc
.ifc_len
= sizeof(ifs
);
116 if (!memcmp(&duid
[8], zero
, ETHER_ADDR_LEN
) &&
117 ioctl(sock
, SIOCGIFCONF
, &ifc
) >= 0) {
118 // If our interface doesn't have an address...
119 ifend
= ifs
+ (ifc
.ifc_len
/ sizeof(struct ifreq
));
120 for (ifp
= ifc
.ifc_req
; ifp
< ifend
&&
121 !memcmp(&duid
[8], zero
, 6); ifp
++) {
122 memcpy(ifr
.ifr_name
, ifp
->ifr_name
,
123 sizeof(ifr
.ifr_name
));
124 ioctl(sock
, SIOCGIFHWADDR
, &ifr
);
125 memcpy(&duid
[8], ifr
.ifr_hwaddr
.sa_data
,
130 odhcp6c_add_state(STATE_CLIENT_ID
, duid
, sizeof(duid
));
134 uint16_t oro
[] = {htons(DHCPV6_OPT_DNS_SERVERS
),
135 htons(DHCPV6_OPT_DNS_DOMAIN
),
136 htons(DHCPV6_OPT_NTP_SERVER
),
137 htons(DHCPV6_OPT_SIP_SERVER_A
),
138 htons(DHCPV6_OPT_PREFIX_CLASS
)
140 odhcp6c_add_state(STATE_ORO
, oro
, sizeof(oro
));
143 // Configure IPv6-options
145 setsockopt(sock
, IPPROTO_IPV6
, IPV6_V6ONLY
, &val
, sizeof(val
));
146 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
147 setsockopt(sock
, SOL_SOCKET
, SO_BINDTODEVICE
, ifname
, strlen(ifname
));
149 struct sockaddr_in6 client_addr
= { .sin6_family
= AF_INET6
,
150 .sin6_port
= htons(DHCPV6_CLIENT_PORT
), .sin6_flowinfo
= 0 };
151 if (bind(sock
, (struct sockaddr
*)&client_addr
, sizeof(client_addr
)))
158 void dhcpv6_set_ia_na_mode(enum odhcp6c_ia_mode mode
)
164 static void dhcpv6_send(enum dhcpv6_msg type
, uint8_t trid
[3], uint32_t ecs
)
168 gethostname(fqdn_buf
, sizeof(fqdn_buf
));
175 size_t fqdn_len
= 5 + dn_comp(fqdn_buf
, fqdn
.data
,
176 sizeof(fqdn
.data
), NULL
, NULL
);
177 fqdn
.type
= htons(DHCPV6_OPT_FQDN
);
178 fqdn
.len
= htons(fqdn_len
- 4);
184 void *cl_id
= odhcp6c_get_state(STATE_CLIENT_ID
, &cl_id_len
);
188 void *srv_id
= odhcp6c_get_state(STATE_SERVER_ID
, &srv_id_len
);
191 size_t ia_pd_entries
, ia_pd_len
= 0;
193 struct odhcp6c_entry
*e
= odhcp6c_get_state(STATE_IA_PD
, &ia_pd_entries
);
194 ia_pd_entries
/= sizeof(*e
);
195 struct dhcpv6_ia_hdr hdr_ia_pd
= {
196 htons(DHCPV6_OPT_IA_PD
),
197 htons(sizeof(hdr_ia_pd
) - 4),
201 struct dhcpv6_ia_prefix pref
= {
202 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
203 .len
= htons(25), .prefix
= request_prefix
207 struct dhcpv6_ia_prefix p
[ia_pd_entries
];
208 for (size_t i
= 0; i
< ia_pd_entries
; ++i
) {
209 p
[i
].type
= htons(DHCPV6_OPT_IA_PREFIX
);
210 p
[i
].len
= htons(sizeof(p
[i
]) - 4U);
213 p
[i
].prefix
= e
[i
].length
;
214 p
[i
].addr
= e
[i
].target
;
217 ia_pd_len
= sizeof(p
);
218 hdr_ia_pd
.len
= htons(ntohs(hdr_ia_pd
.len
) + ia_pd_len
);
220 if (request_prefix
> 0 &&
221 (type
== DHCPV6_MSG_SOLICIT
||
222 type
== DHCPV6_MSG_REQUEST
)) {
224 ia_pd_len
= sizeof(pref
);
225 hdr_ia_pd
.len
= htons(ntohs(hdr_ia_pd
.len
) + ia_pd_len
);
229 size_t ia_na_entries
, ia_na_len
= 0;
231 e
= odhcp6c_get_state(STATE_IA_NA
, &ia_na_entries
);
232 ia_na_entries
/= sizeof(*e
);
234 struct dhcpv6_ia_hdr hdr_ia_na
= {
235 htons(DHCPV6_OPT_IA_NA
),
236 htons(sizeof(hdr_ia_na
) - 4),
240 struct dhcpv6_ia_addr pa
[ia_na_entries
];
241 for (size_t i
= 0; i
< ia_na_entries
; ++i
) {
242 pa
[i
].type
= htons(DHCPV6_OPT_IA_ADDR
);
243 pa
[i
].len
= htons(sizeof(pa
[i
]) - 4U);
244 pa
[i
].addr
= e
[i
].target
;
250 ia_na_len
= sizeof(pa
);
251 hdr_ia_na
.len
= htons(ntohs(hdr_ia_na
.len
) + ia_na_len
);
253 // Reconfigure Accept
257 } reconf_accept
= {htons(DHCPV6_OPT_RECONF_ACCEPT
), 0};
259 // Request Information Refresh
260 uint16_t oro_refresh
= htons(DHCPV6_OPT_INFO_REFRESH
);
264 void *oro
= odhcp6c_get_state(STATE_ORO
, &oro_len
);
268 uint16_t elapsed_type
;
269 uint16_t elapsed_len
;
270 uint16_t elapsed_value
;
274 type
, {trid
[0], trid
[1], trid
[2]},
275 htons(DHCPV6_OPT_ELAPSED
), htons(2),
276 htons((ecs
> 0xffff) ? 0xffff : ecs
),
277 htons(DHCPV6_OPT_ORO
), htons(oro_len
),
280 struct iovec iov
[] = {
285 {srv_id
, srv_id_len
},
288 {&hdr_ia_na
, sizeof(hdr_ia_na
)},
290 {&hdr_ia_pd
, sizeof(hdr_ia_pd
)},
294 size_t cnt
= ARRAY_SIZE(iov
);
295 if (type
== DHCPV6_MSG_INFO_REQ
) {
297 iov
[2].iov_len
= sizeof(oro_refresh
);
298 hdr
.oro_len
= htons(oro_len
+ sizeof(oro_refresh
));
299 } else if (!request_prefix
) {
303 // Disable IAs if not used
304 if (type
== DHCPV6_MSG_SOLICIT
) {
305 iov
[5].iov_len
= sizeof(reconf_accept
);
306 } else if (type
!= DHCPV6_MSG_REQUEST
) {
313 if (na_mode
== IA_MODE_NONE
)
316 struct sockaddr_in6 srv
= {AF_INET6
, htons(DHCPV6_SERVER_PORT
),
317 0, ALL_DHCPV6_RELAYS
, ifindex
};
318 struct msghdr msg
= {&srv
, sizeof(srv
), iov
, cnt
, NULL
, 0, 0};
320 sendmsg(sock
, &msg
, 0);
324 static int64_t dhcpv6_rand_delay(int64_t time
)
327 odhcp6c_random(&random
, sizeof(random
));
328 return (time
* (random
% 1000)) / 10000;
332 int dhcpv6_request(enum dhcpv6_msg type
)
335 uint32_t timeout
= UINT32_MAX
;
336 struct dhcpv6_retx
*retx
= &dhcpv6_retx
[type
];
339 struct timespec ts
= {0, 0};
340 ts
.tv_nsec
= dhcpv6_rand_delay(10 * DHCPV6_REQ_DELAY
);
341 nanosleep(&ts
, NULL
);
344 if (type
== DHCPV6_MSG_RELEASE
|| type
== DHCPV6_MSG_DECLINE
)
346 else if (type
== DHCPV6_MSG_UNKNOWN
)
348 else if (type
== DHCPV6_MSG_RENEW
)
350 else if (type
== DHCPV6_MSG_REBIND
)
356 syslog(LOG_NOTICE
, "Sending %s (timeout %us)", retx
->name
, timeout
);
358 uint64_t start
= odhcp6c_get_milli_time(), round_start
= start
, elapsed
;
360 // Generate transaction ID
362 odhcp6c_random(trid
, sizeof(trid
));
367 rto
= (rto
== 0) ? (retx
->init_timeo
* 1000 +
368 dhcpv6_rand_delay(retx
->init_timeo
* 1000)) :
369 (2 * rto
+ dhcpv6_rand_delay(rto
));
371 if (rto
>= retx
->max_timeo
* 1000)
372 rto
= retx
->max_timeo
* 1000 +
373 dhcpv6_rand_delay(retx
->max_timeo
* 1000);
375 // Calculate end for this round and elapsed time
376 uint64_t round_end
= round_start
+ rto
;
377 elapsed
= round_start
- start
;
379 // Don't wait too long
380 if (round_end
- start
> timeout
* 1000)
381 round_end
= timeout
* 1000 + start
;
383 // Built and send package
384 if (type
!= DHCPV6_MSG_UNKNOWN
)
385 dhcpv6_send(type
, trid
, elapsed
/ 10);
388 for (; len
< 0 && round_start
< round_end
;
389 round_start
= odhcp6c_get_milli_time()) {
390 // Check for pending signal
391 if (odhcp6c_signal_process())
394 // Set timeout for receiving
395 uint64_t t
= round_end
- round_start
;
396 struct timeval timeout
= {t
/ 1000, (t
% 1000) * 1000};
397 setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
,
398 &timeout
, sizeof(timeout
));
401 len
= recv(sock
, buf
, sizeof(buf
), 0);
403 if (!dhcpv6_response_is_valid(buf
, len
, trid
, type
))
407 uint8_t *opt
= &buf
[4];
408 uint8_t *opt_end
= opt
+ len
- 4;
410 round_start
= odhcp6c_get_milli_time();
411 elapsed
= round_start
- start
;
412 syslog(LOG_NOTICE
, "Got a valid reply after "
413 "%ums", (unsigned)elapsed
);
415 if (retx
->handler_reply
)
416 len
= retx
->handler_reply(
419 if (round_end
- round_start
> 1000)
420 round_end
= 1000 + round_start
;
425 if (retx
->handler_finish
)
426 len
= retx
->handler_finish();
427 } while (len
< 0 && elapsed
/ 1000 < timeout
);
433 static bool dhcpv6_response_is_valid(const void *buf
, ssize_t len
,
434 const uint8_t transaction
[3], enum dhcpv6_msg type
)
436 const struct dhcpv6_header
*rep
= buf
;
437 if (len
< (ssize_t
)sizeof(*rep
) || memcmp(rep
->tr_id
,
438 transaction
, sizeof(rep
->tr_id
)))
439 return false; // Invalid reply
441 if (type
== DHCPV6_MSG_SOLICIT
) {
442 if (rep
->msg_type
!= DHCPV6_MSG_ADVERT
&&
443 rep
->msg_type
!= DHCPV6_MSG_REPLY
)
445 } else if (type
== DHCPV6_MSG_UNKNOWN
) {
446 if (!accept_reconfig
|| rep
->msg_type
!= DHCPV6_MSG_RECONF
)
448 } else if (rep
->msg_type
!= DHCPV6_MSG_REPLY
) {
452 uint8_t *end
= ((uint8_t*)buf
) + len
, *odata
;
453 uint16_t otype
, olen
;
454 bool clientid_ok
= false, serverid_ok
= false;
456 size_t client_id_len
, server_id_len
;
457 void *client_id
= odhcp6c_get_state(STATE_CLIENT_ID
, &client_id_len
);
458 void *server_id
= odhcp6c_get_state(STATE_SERVER_ID
, &server_id_len
);
460 dhcpv6_for_each_option(&rep
[1], end
, otype
, olen
, odata
)
461 if (otype
== DHCPV6_OPT_CLIENTID
)
462 clientid_ok
= (olen
+ 4U == client_id_len
) && !memcmp(
463 &odata
[-4], client_id
, client_id_len
);
464 else if (otype
== DHCPV6_OPT_SERVERID
)
465 serverid_ok
= (olen
+ 4U == server_id_len
) && !memcmp(
466 &odata
[-4], server_id
, server_id_len
);
468 return clientid_ok
&& (serverid_ok
|| server_id_len
== 0);
472 int dhcpv6_poll_reconfigure(void)
474 int ret
= dhcpv6_request(DHCPV6_MSG_UNKNOWN
);
476 ret
= dhcpv6_request(ret
);
482 static int dhcpv6_handle_reconfigure(_unused
enum dhcpv6_msg orig
,
483 const void *opt
, const void *end
)
485 // TODO: should verify the reconfigure message
486 uint16_t otype
, olen
;
487 uint8_t *odata
, msg
= DHCPV6_MSG_RENEW
;
488 dhcpv6_for_each_option(opt
, end
, otype
, olen
, odata
)
489 if (otype
== DHCPV6_OPT_RECONF_MESSAGE
&& olen
== 1 && (
490 odata
[0] == DHCPV6_MSG_RENEW
||
491 odata
[0] == DHCPV6_MSG_INFO_REQ
))
494 dhcpv6_handle_reply(DHCPV6_MSG_UNKNOWN
, NULL
, NULL
);
499 // Collect all advertised servers
500 static int dhcpv6_handle_advert(_unused
enum dhcpv6_msg orig
,
501 const void *opt
, const void *end
)
503 uint16_t olen
, otype
;
505 struct dhcpv6_server_cand cand
= {false, false, 0, 0, {0}};
507 dhcpv6_for_each_option(opt
, end
, otype
, olen
, odata
) {
508 if (otype
== DHCPV6_OPT_SERVERID
&& olen
<= 130) {
509 memcpy(cand
.duid
, odata
, olen
);
510 cand
.duid_len
= olen
;
511 } else if (otype
== DHCPV6_OPT_STATUS
&& olen
>= 2 && !odata
[0]
512 && odata
[1] == DHCPV6_NoAddrsAvail
) {
513 if (na_mode
== IA_MODE_FORCE
) {
516 cand
.has_noaddravail
= true;
517 cand
.preference
-= 1000;
519 } else if (otype
== DHCPV6_OPT_STATUS
&& olen
>= 2 && !odata
[0]
520 && odata
[1] == DHCPV6_NoPrefixAvail
) {
521 cand
.preference
-= 2000;
522 } else if (otype
== DHCPV6_OPT_PREF
&& olen
>= 1 &&
523 cand
.preference
>= 0) {
524 cand
.preference
= odata
[0];
525 } else if (otype
== DHCPV6_OPT_RECONF_ACCEPT
) {
526 cand
.wants_reconfigure
= true;
527 } else if (otype
== DHCPV6_OPT_IA_PD
&& request_prefix
) {
528 struct dhcpv6_ia_hdr
*h
= (void*)odata
;
529 uint8_t *oend
= odata
+ olen
, *d
;
530 dhcpv6_for_each_option(&h
[1], oend
, otype
, olen
, d
) {
531 if (otype
== DHCPV6_OPT_IA_PREFIX
)
532 cand
.preference
+= 2000;
533 else if (otype
== DHCPV6_OPT_STATUS
&&
534 olen
>= 2 && d
[0] == 0 &&
535 d
[1] == DHCPV6_NoPrefixAvail
)
536 cand
.preference
-= 2000;
541 if (cand
.duid_len
> 0)
542 odhcp6c_add_state(STATE_SERVER_CAND
, &cand
, sizeof(cand
));
548 static int dhcpv6_commit_advert(void)
551 struct dhcpv6_server_cand
*c
= NULL
, *cand
=
552 odhcp6c_get_state(STATE_SERVER_CAND
, &cand_len
);
555 for (size_t i
= 0; i
< cand_len
/ sizeof(*c
); ++i
) {
556 if (cand
[i
].has_noaddravail
)
557 retry
= true; // We want to try again
559 if (!c
|| c
->preference
< cand
[i
].preference
)
563 if (retry
&& na_mode
== IA_MODE_TRY
) {
564 // We give it a second try without the IA_NA
565 na_mode
= IA_MODE_NONE
;
566 return dhcpv6_request(DHCPV6_MSG_SOLICIT
);
570 uint16_t hdr
[2] = {htons(DHCPV6_OPT_SERVERID
),
572 odhcp6c_add_state(STATE_SERVER_ID
, hdr
, sizeof(hdr
));
573 odhcp6c_add_state(STATE_SERVER_ID
, c
->duid
, c
->duid_len
);
574 accept_reconfig
= c
->wants_reconfigure
;
577 odhcp6c_clear_state(STATE_SERVER_CAND
);
581 else if (request_prefix
|| na_mode
!= IA_MODE_NONE
)
582 return DHCPV6_STATEFUL
;
584 return DHCPV6_STATELESS
;
588 static int dhcpv6_handle_rebind_reply(enum dhcpv6_msg orig
,
589 const void *opt
, const void *end
)
591 dhcpv6_handle_advert(orig
, opt
, end
);
592 if (dhcpv6_commit_advert() < 0) {
593 dhcpv6_handle_reply(DHCPV6_MSG_UNKNOWN
, NULL
, NULL
);
597 return dhcpv6_handle_reply(orig
, opt
, end
);
601 static int dhcpv6_handle_reply(enum dhcpv6_msg orig
,
602 const void *opt
, const void *end
)
605 uint16_t otype
, olen
;
607 static time_t last_update
= 0;
608 time_t now
= odhcp6c_get_milli_time() / 1000;
610 uint32_t elapsed
= now
- last_update
;
613 if (orig
== DHCPV6_MSG_UNKNOWN
) {
627 t1
= t2
= t3
= UINT32_MAX
;
631 odhcp6c_clear_state(STATE_DNS
);
632 odhcp6c_clear_state(STATE_SEARCH
);
633 odhcp6c_clear_state(STATE_SNTP_IP
);
634 odhcp6c_clear_state(STATE_SNTP_FQDN
);
635 odhcp6c_clear_state(STATE_SIP_IP
);
636 odhcp6c_clear_state(STATE_SIP_FQDN
);
639 // Parse and find all matching IAs
640 dhcpv6_for_each_option(opt
, end
, otype
, olen
, odata
) {
641 if ((otype
== DHCPV6_OPT_IA_PD
|| otype
== DHCPV6_OPT_IA_NA
)
642 && olen
> sizeof(struct dhcpv6_ia_hdr
)) {
643 struct dhcpv6_ia_hdr
*ia_hdr
= (void*)(&odata
[-4]);
644 uint32_t l_t1
= ntohl(ia_hdr
->t1
);
645 uint32_t l_t2
= ntohl(ia_hdr
->t2
);
647 // Test ID and T1-T2 validity
648 if (ia_hdr
->iaid
!= 1 || l_t2
< l_t1
)
651 uint16_t stype
, slen
;
653 // Test status and bail if error
654 dhcpv6_for_each_option(&ia_hdr
[1], odata
+ olen
,
656 if (stype
== DHCPV6_OPT_STATUS
&& slen
>= 2 &&
657 (sdata
[0] || sdata
[1]))
661 if (l_t1
> 0 && t1
> l_t1
)
664 if (l_t2
> 0 && t2
> l_t2
)
667 uint32_t n
= dhcpv6_parse_ia(&ia_hdr
[1], odata
+ olen
);
684 } else if (otype
== DHCPV6_OPT_DNS_SERVERS
) {
686 odhcp6c_add_state(STATE_DNS
, odata
, olen
);
687 } else if (otype
== DHCPV6_OPT_DNS_DOMAIN
) {
688 odhcp6c_add_state(STATE_SEARCH
, odata
, olen
);
689 } else if (otype
== DHCPV6_OPT_NTP_SERVER
) {
690 uint16_t stype
, slen
;
692 // Test status and bail if error
693 dhcpv6_for_each_option(odata
, odata
+ olen
,
694 stype
, slen
, sdata
) {
695 if (slen
== 16 && (stype
== NTP_MC_ADDR
||
696 stype
== NTP_SRV_ADDR
))
697 odhcp6c_add_state(STATE_SNTP_IP
,
699 else if (slen
> 0 && stype
== NTP_SRV_FQDN
)
700 odhcp6c_add_state(STATE_SNTP_FQDN
,
703 } else if (otype
== DHCPV6_OPT_SIP_SERVER_A
) {
705 odhcp6c_add_state(STATE_SIP_IP
, odata
, olen
);
706 } else if (otype
== DHCPV6_OPT_SIP_SERVER_D
) {
707 odhcp6c_add_state(STATE_SIP_FQDN
, odata
, olen
);
708 } else if (otype
== DHCPV6_OPT_INFO_REFRESH
&& olen
>= 4) {
709 uint32_t refresh
= ntohl(*((uint32_t*)odata
));
710 if (refresh
< (uint32_t)t1
)
712 } else if (otype
!= DHCPV6_OPT_CLIENTID
&&
713 otype
!= DHCPV6_OPT_SERVERID
) {
714 odhcp6c_add_state(STATE_CUSTOM_OPTS
,
715 &odata
[-4], olen
+ 4);
723 static uint32_t dhcpv6_parse_ia(void *opt
, void *end
)
725 uint32_t timeout
= UINT32_MAX
; // Minimum timeout
726 uint16_t otype
, olen
;
730 dhcpv6_for_each_option(opt
, end
, otype
, olen
, odata
) {
731 struct odhcp6c_entry entry
= {IN6ADDR_ANY_INIT
,
732 0, 0, IN6ADDR_ANY_INIT
, 0, 0, 0};
734 if (otype
== DHCPV6_OPT_IA_PREFIX
) {
735 struct dhcpv6_ia_prefix
*prefix
= (void*)&odata
[-4];
736 if (olen
+ 4U < sizeof(*prefix
))
739 entry
.valid
= ntohl(prefix
->valid
);
740 entry
.preferred
= ntohl(prefix
->preferred
);
742 if (entry
.preferred
> entry
.valid
)
745 entry
.length
= prefix
->prefix
;
746 entry
.target
= prefix
->addr
;
747 uint16_t stype
, slen
;
750 // Find prefix class, if any
751 dhcpv6_for_each_option(odata
, odata
+ olen
,
753 if (stype
== DHCPV6_OPT_PREFIX_CLASS
&& slen
== 2)
754 entry
.prefix_class
= ntohs(*((uint16_t*)sdata
));
756 odhcp6c_update_entry(STATE_IA_PD
, &entry
);
757 } else if (otype
== DHCPV6_OPT_IA_ADDR
) {
758 struct dhcpv6_ia_addr
*addr
= (void*)&odata
[-4];
759 if (olen
+ 4U < sizeof(*addr
))
762 entry
.preferred
= ntohl(addr
->preferred
);
763 entry
.valid
= ntohl(addr
->valid
);
765 if (entry
.preferred
> entry
.valid
)
769 entry
.target
= addr
->addr
;
771 odhcp6c_update_entry(STATE_IA_NA
, &entry
);
774 if (entry
.valid
> 0 && timeout
> entry
.valid
)
775 timeout
= entry
.valid
;