2 * Copyright (C) 2012-2013 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.
27 #include <sys/ioctl.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
32 #include <net/ethernet.h>
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
46 static bool dhcpv6_response_is_valid(const void *buf
, ssize_t len
,
47 const uint8_t transaction
[3], enum dhcpv6_msg type
);
49 static uint32_t dhcpv6_parse_ia(void *opt
, void *end
);
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);
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, 1, 30, "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
},
80 static int ifindex
= -1;
81 static int64_t t1
= 0, t2
= 0, t3
= 0;
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;
89 static uint8_t reconf_key
[16];
93 int init_dhcpv6(const char *ifname
, int request_pd
)
95 request_prefix
= request_pd
;
97 sock
= socket(AF_INET6
, SOCK_DGRAM
| SOCK_CLOEXEC
, IPPROTO_UDP
);
101 strncpy(ifr
.ifr_name
, ifname
, sizeof(ifr
.ifr_name
));
102 if (ioctl(sock
, SIOCGIFINDEX
, &ifr
))
104 ifindex
= ifr
.ifr_ifindex
;
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
);
115 uint8_t zero
[ETHER_ADDR_LEN
] = {0, 0, 0, 0, 0, 0};
116 struct ifreq ifs
[100], *ifp
, *ifend
;
119 ifc
.ifc_len
= sizeof(ifs
);
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
,
135 odhcp6c_add_state(STATE_CLIENT_ID
, duid
, sizeof(duid
));
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
),
152 odhcp6c_add_state(STATE_ORO
, oro
, sizeof(oro
));
155 // Configure IPv6-options
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
));
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
)))
170 void dhcpv6_set_ia_mode(enum odhcp6c_ia_mode na
, enum odhcp6c_ia_mode pd
)
177 static void dhcpv6_send(enum dhcpv6_msg type
, uint8_t trid
[3], uint32_t ecs
)
181 gethostname(fqdn_buf
, sizeof(fqdn_buf
));
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);
197 void *cl_id
= odhcp6c_get_state(STATE_CLIENT_ID
, &cl_id_len
);
201 void *srv_id
= odhcp6c_get_state(STATE_SERVER_ID
, &srv_id_len
);
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),
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
) {
217 if (e
[i
].priority
> 0)
218 ex_len
= ((e
[i
].priority
- e
[i
].length
- 1) / 8) + 6;
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
,
227 memcpy(ia_pd
+ ia_pd_len
, &p
, sizeof(p
));
228 ia_pd_len
+= sizeof(p
);
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
;
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);
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;
247 struct dhcpv6_ia_prefix pref
= {
248 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
249 .len
= htons(25), .prefix
= request_prefix
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
);
255 hdr_ia_pd
.len
= htons(ntohs(hdr_ia_pd
.len
) + ia_pd_len
);
258 size_t ia_na_entries
, ia_na_len
= 0;
260 e
= odhcp6c_get_state(STATE_IA_NA
, &ia_na_entries
);
261 ia_na_entries
/= sizeof(*e
);
263 struct dhcpv6_ia_hdr hdr_ia_na
= {
264 htons(DHCPV6_OPT_IA_NA
),
265 htons(sizeof(hdr_ia_na
) - 4),
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
;
279 ia_na_len
= sizeof(pa
);
280 hdr_ia_na
.len
= htons(ntohs(hdr_ia_na
.len
) + ia_na_len
);
282 // Reconfigure Accept
286 } reconf_accept
= {htons(DHCPV6_OPT_RECONF_ACCEPT
), 0};
288 // Request Information Refresh
289 uint16_t oro_refresh
= htons(DHCPV6_OPT_INFO_REFRESH
);
293 void *oro
= odhcp6c_get_state(STATE_ORO
, &oro_len
);
297 uint16_t elapsed_type
;
298 uint16_t elapsed_len
;
299 uint16_t elapsed_value
;
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
),
309 struct iovec iov
[] = {
314 {srv_id
, srv_id_len
},
315 {&reconf_accept
, sizeof(reconf_accept
)},
317 {&hdr_ia_na
, sizeof(hdr_ia_na
)},
319 {&hdr_ia_pd
, sizeof(hdr_ia_pd
)},
323 size_t cnt
= ARRAY_SIZE(iov
);
324 if (type
== DHCPV6_MSG_INFO_REQ
) {
326 iov
[2].iov_len
= sizeof(oro_refresh
);
327 hdr
.oro_len
= htons(oro_len
+ sizeof(oro_refresh
));
328 } else if (!request_prefix
) {
332 // Disable IAs if not used
333 if (type
!= DHCPV6_MSG_SOLICIT
) {
341 if (na_mode
== IA_MODE_NONE
)
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};
348 sendmsg(sock
, &msg
, 0);
352 static int64_t dhcpv6_rand_delay(int64_t time
)
355 odhcp6c_random(&random
, sizeof(random
));
356 return (time
* ((int64_t)random
% 1000LL)) / 10000LL;
360 int dhcpv6_request(enum dhcpv6_msg type
)
363 uint64_t timeout
= UINT32_MAX
;
364 struct dhcpv6_retx
*retx
= &dhcpv6_retx
[type
];
367 struct timespec ts
= {0, 0};
368 ts
.tv_nsec
= dhcpv6_rand_delay(10 * DHCPV6_REQ_DELAY
);
369 nanosleep(&ts
, NULL
);
372 if (type
== DHCPV6_MSG_REQUEST
)
374 else if (type
== DHCPV6_MSG_RELEASE
|| type
== DHCPV6_MSG_DECLINE
)
376 else if (type
== DHCPV6_MSG_UNKNOWN
)
378 else if (type
== DHCPV6_MSG_RENEW
)
379 timeout
= (t2
> t1
) ? t2
- t1
: 0;
380 else if (type
== DHCPV6_MSG_REBIND
)
381 timeout
= (t3
> t2
) ? t3
- t2
: 0;
386 syslog(LOG_NOTICE
, "Sending %s (timeout %us)", retx
->name
, (unsigned)timeout
);
388 uint64_t start
= odhcp6c_get_milli_time(), round_start
= start
, elapsed
;
390 // Generate transaction ID
391 uint8_t trid
[3] = {0, 0, 0};
392 if (type
!= DHCPV6_MSG_UNKNOWN
)
393 odhcp6c_random(trid
, sizeof(trid
));
398 rto
= (rto
== 0) ? (retx
->init_timeo
* 1000 +
399 dhcpv6_rand_delay(retx
->init_timeo
* 1000)) :
400 (2 * rto
+ dhcpv6_rand_delay(rto
));
402 if (rto
>= retx
->max_timeo
* 1000)
403 rto
= retx
->max_timeo
* 1000 +
404 dhcpv6_rand_delay(retx
->max_timeo
* 1000);
406 // Calculate end for this round and elapsed time
407 uint64_t round_end
= round_start
+ rto
;
408 elapsed
= round_start
- start
;
410 // Don't wait too long
411 if (round_end
- start
> timeout
* 1000)
412 round_end
= timeout
* 1000 + start
;
414 // Built and send package
415 if (type
!= DHCPV6_MSG_UNKNOWN
)
416 dhcpv6_send(type
, trid
, elapsed
/ 10);
419 for (; len
< 0 && round_start
< round_end
;
420 round_start
= odhcp6c_get_milli_time()) {
421 // Check for pending signal
422 if (odhcp6c_signal_process())
425 // Set timeout for receiving
426 uint64_t t
= round_end
- round_start
;
427 struct timeval timeout
= {t
/ 1000, (t
% 1000) * 1000};
428 setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
,
429 &timeout
, sizeof(timeout
));
432 len
= recv(sock
, buf
, sizeof(buf
), 0);
434 if (!dhcpv6_response_is_valid(buf
, len
, trid
, type
))
438 uint8_t *opt
= &buf
[4];
439 uint8_t *opt_end
= opt
+ len
- 4;
441 round_start
= odhcp6c_get_milli_time();
442 elapsed
= round_start
- start
;
443 syslog(LOG_NOTICE
, "Got a valid reply after "
444 "%ums", (unsigned)elapsed
);
446 if (retx
->handler_reply
)
447 len
= retx
->handler_reply(
450 if (len
> 0 && round_end
- round_start
> 1000)
451 round_end
= 1000 + round_start
;
456 if (retx
->handler_finish
)
457 len
= retx
->handler_finish();
458 } while (len
< 0 && elapsed
/ 1000 < timeout
);
464 static bool dhcpv6_response_is_valid(const void *buf
, ssize_t len
,
465 const uint8_t transaction
[3], enum dhcpv6_msg type
)
467 const struct dhcpv6_header
*rep
= buf
;
468 if (len
< (ssize_t
)sizeof(*rep
) || memcmp(rep
->tr_id
,
469 transaction
, sizeof(rep
->tr_id
)))
470 return false; // Invalid reply
472 if (type
== DHCPV6_MSG_SOLICIT
) {
473 if (rep
->msg_type
!= DHCPV6_MSG_ADVERT
&&
474 rep
->msg_type
!= DHCPV6_MSG_REPLY
)
476 } else if (type
== DHCPV6_MSG_UNKNOWN
) {
477 if (!accept_reconfig
|| rep
->msg_type
!= DHCPV6_MSG_RECONF
)
479 } else if (rep
->msg_type
!= DHCPV6_MSG_REPLY
) {
483 uint8_t *end
= ((uint8_t*)buf
) + len
, *odata
;
484 uint16_t otype
, olen
;
485 bool clientid_ok
= false, serverid_ok
= false, rcauth_ok
= false;
487 size_t client_id_len
, server_id_len
;
488 void *client_id
= odhcp6c_get_state(STATE_CLIENT_ID
, &client_id_len
);
489 void *server_id
= odhcp6c_get_state(STATE_SERVER_ID
, &server_id_len
);
491 dhcpv6_for_each_option(&rep
[1], end
, otype
, olen
, odata
) {
492 if (otype
== DHCPV6_OPT_CLIENTID
) {
493 clientid_ok
= (olen
+ 4U == client_id_len
) && !memcmp(
494 &odata
[-4], client_id
, client_id_len
);
495 } else if (otype
== DHCPV6_OPT_SERVERID
) {
496 serverid_ok
= (olen
+ 4U == server_id_len
) && !memcmp(
497 &odata
[-4], server_id
, server_id_len
);
498 } else if (otype
== DHCPV6_OPT_AUTH
&& olen
== -4 +
499 sizeof(struct dhcpv6_auth_reconfigure
)) {
500 struct dhcpv6_auth_reconfigure
*r
= (void*)&odata
[-4];
501 if (r
->protocol
!= 3 || r
->algorithm
!= 1 || r
->reconf_type
!= 2)
505 uint8_t serverhash
[16], secretbytes
[16], hash
[16];
506 memcpy(serverhash
, r
->key
, sizeof(serverhash
));
507 memset(r
->key
, 0, sizeof(r
->key
));
508 memcpy(secretbytes
, reconf_key
, sizeof(secretbytes
));
510 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
)
511 secretbytes
[i
] ^= 0x36;
514 md5_append(&md5
, secretbytes
, sizeof(secretbytes
));
515 md5_append(&md5
, buf
, len
);
516 md5_finish(&md5
, hash
);
518 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
) {
519 secretbytes
[i
] ^= 0x36;
520 secretbytes
[i
] ^= 0x5c;
524 md5_append(&md5
, secretbytes
, sizeof(secretbytes
));
525 md5_append(&md5
, hash
, 16);
526 md5_finish(&md5
, hash
);
528 rcauth_ok
= !memcmp(hash
, serverhash
, sizeof(hash
));
532 if (rep
->msg_type
== DHCPV6_MSG_RECONF
&& !rcauth_ok
)
535 return clientid_ok
&& (serverid_ok
|| server_id_len
== 0);
539 int dhcpv6_poll_reconfigure(void)
541 int ret
= dhcpv6_request(DHCPV6_MSG_UNKNOWN
);
543 ret
= dhcpv6_request(ret
);
549 static int dhcpv6_handle_reconfigure(_unused
enum dhcpv6_msg orig
,
550 const void *opt
, const void *end
)
552 // TODO: should verify the reconfigure message
553 uint16_t otype
, olen
;
554 uint8_t *odata
, msg
= DHCPV6_MSG_RENEW
;
555 dhcpv6_for_each_option(opt
, end
, otype
, olen
, odata
)
556 if (otype
== DHCPV6_OPT_RECONF_MESSAGE
&& olen
== 1 && (
557 odata
[0] == DHCPV6_MSG_RENEW
||
558 odata
[0] == DHCPV6_MSG_INFO_REQ
))
561 dhcpv6_handle_reply(DHCPV6_MSG_UNKNOWN
, NULL
, NULL
);
566 // Collect all advertised servers
567 static int dhcpv6_handle_advert(enum dhcpv6_msg orig
,
568 const void *opt
, const void *end
)
570 uint16_t olen
, otype
;
572 struct dhcpv6_server_cand cand
= {false, false, 0, 0, {0}, NULL
, NULL
, 0, 0};
573 bool have_na
= false, have_pd
= false;
575 dhcpv6_for_each_option(opt
, end
, otype
, olen
, odata
) {
576 if (orig
== DHCPV6_MSG_SOLICIT
&&
577 (otype
== DHCPV6_OPT_IA_PD
|| otype
== DHCPV6_OPT_IA_NA
) &&
578 olen
> sizeof(struct dhcpv6_ia_hdr
)) {
579 struct dhcpv6_ia_hdr
*ia_hdr
= (void*)(&odata
[-4]);
580 dhcpv6_parse_ia(&ia_hdr
[1], odata
+ olen
);
583 if (otype
== DHCPV6_OPT_SERVERID
&& olen
<= 130) {
584 memcpy(cand
.duid
, odata
, olen
);
585 cand
.duid_len
= olen
;
586 } else if (otype
== DHCPV6_OPT_STATUS
&& olen
>= 2 && !odata
[0]
587 && odata
[1] == DHCPV6_NoPrefixAvail
) {
588 cand
.preference
-= 2000;
589 } else if (otype
== DHCPV6_OPT_PREF
&& olen
>= 1 &&
590 cand
.preference
>= 0) {
591 cand
.preference
= odata
[0];
592 } else if (otype
== DHCPV6_OPT_RECONF_ACCEPT
) {
593 cand
.wants_reconfigure
= true;
594 } else if (otype
== DHCPV6_OPT_IA_PD
&& request_prefix
) {
595 struct dhcpv6_ia_hdr
*h
= (struct dhcpv6_ia_hdr
*)&odata
[-4];
596 uint8_t *oend
= odata
+ olen
, *d
;
597 dhcpv6_for_each_option(&h
[1], oend
, otype
, olen
, d
)
598 if (otype
== DHCPV6_OPT_IA_PREFIX
)
600 } else if (otype
== DHCPV6_OPT_IA_NA
) {
601 struct dhcpv6_ia_hdr
*h
= (struct dhcpv6_ia_hdr
*)&odata
[-4];
602 uint8_t *oend
= odata
+ olen
, *d
;
603 dhcpv6_for_each_option(&h
[1], oend
, otype
, olen
, d
)
604 if (otype
== DHCPV6_OPT_IA_ADDR
)
609 if ((!have_na
&& na_mode
== IA_MODE_FORCE
) ||
610 (!have_pd
&& pd_mode
== IA_MODE_FORCE
))
613 if (na_mode
!= IA_MODE_NONE
&& !have_na
) {
614 cand
.has_noaddravail
= true;
615 cand
.preference
-= 1000;
618 if (pd_mode
!= IA_MODE_NONE
) {
620 cand
.preference
+= 2000;
622 cand
.preference
-= 2000;
625 if (cand
.duid_len
> 0) {
626 cand
.ia_na
= odhcp6c_move_state(STATE_IA_NA
, &cand
.ia_na_len
);
627 cand
.ia_pd
= odhcp6c_move_state(STATE_IA_PD
, &cand
.ia_pd_len
);
628 odhcp6c_add_state(STATE_SERVER_CAND
, &cand
, sizeof(cand
));
631 if (orig
== DHCPV6_MSG_SOLICIT
) {
632 odhcp6c_clear_state(STATE_IA_NA
);
633 odhcp6c_clear_state(STATE_IA_PD
);
640 static int dhcpv6_commit_advert(void)
643 struct dhcpv6_server_cand
*c
= NULL
, *cand
=
644 odhcp6c_get_state(STATE_SERVER_CAND
, &cand_len
);
647 for (size_t i
= 0; i
< cand_len
/ sizeof(*c
); ++i
) {
648 if (cand
[i
].has_noaddravail
)
649 retry
= true; // We want to try again
651 if (!c
|| c
->preference
< cand
[i
].preference
)
655 if (retry
&& na_mode
== IA_MODE_TRY
) {
656 // We give it a second try without the IA_NA
657 na_mode
= IA_MODE_NONE
;
658 return dhcpv6_request(DHCPV6_MSG_SOLICIT
);
662 uint16_t hdr
[2] = {htons(DHCPV6_OPT_SERVERID
),
664 odhcp6c_add_state(STATE_SERVER_ID
, hdr
, sizeof(hdr
));
665 odhcp6c_add_state(STATE_SERVER_ID
, c
->duid
, c
->duid_len
);
666 accept_reconfig
= c
->wants_reconfigure
;
667 odhcp6c_add_state(STATE_IA_NA
, c
->ia_na
, c
->ia_na_len
);
668 odhcp6c_add_state(STATE_IA_PD
, c
->ia_pd
, c
->ia_pd_len
);
671 for (size_t i
= 0; i
< cand_len
/ sizeof(*c
); ++i
) {
675 odhcp6c_clear_state(STATE_SERVER_CAND
);
679 else if (request_prefix
|| na_mode
!= IA_MODE_NONE
)
680 return DHCPV6_STATEFUL
;
682 return DHCPV6_STATELESS
;
686 static int dhcpv6_handle_rebind_reply(enum dhcpv6_msg orig
,
687 const void *opt
, const void *end
)
689 dhcpv6_handle_advert(orig
, opt
, end
);
690 if (dhcpv6_commit_advert() < 0) {
691 dhcpv6_handle_reply(DHCPV6_MSG_UNKNOWN
, NULL
, NULL
);
695 return dhcpv6_handle_reply(orig
, opt
, end
);
699 static int dhcpv6_handle_reply(enum dhcpv6_msg orig
,
700 const void *opt
, const void *end
)
703 uint16_t otype
, olen
;
707 if (orig
== DHCPV6_MSG_UNKNOWN
) {
708 static time_t last_update
= 0;
709 time_t now
= odhcp6c_get_milli_time() / 1000;
711 uint32_t elapsed
= (last_update
> 0) ? now
- last_update
: 0;
727 t1
= t2
= t3
= UINT32_MAX
;
730 if (orig
== DHCPV6_MSG_REQUEST
) {
731 // Delete NA and PD we have in the state from the Advert
732 odhcp6c_clear_state(STATE_IA_NA
);
733 odhcp6c_clear_state(STATE_IA_PD
);
737 odhcp6c_clear_state(STATE_DNS
);
738 odhcp6c_clear_state(STATE_SEARCH
);
739 odhcp6c_clear_state(STATE_SNTP_IP
);
740 odhcp6c_clear_state(STATE_SNTP_FQDN
);
741 odhcp6c_clear_state(STATE_SIP_IP
);
742 odhcp6c_clear_state(STATE_SIP_FQDN
);
743 odhcp6c_clear_state(STATE_AFTR_NAME
);
746 // Parse and find all matching IAs
747 dhcpv6_for_each_option(opt
, end
, otype
, olen
, odata
) {
748 if ((otype
== DHCPV6_OPT_IA_PD
|| otype
== DHCPV6_OPT_IA_NA
)
749 && olen
> sizeof(struct dhcpv6_ia_hdr
)) {
750 struct dhcpv6_ia_hdr
*ia_hdr
= (void*)(&odata
[-4]);
751 uint32_t l_t1
= ntohl(ia_hdr
->t1
);
752 uint32_t l_t2
= ntohl(ia_hdr
->t2
);
754 // Test ID and T1-T2 validity
755 if (ia_hdr
->iaid
!= 1 || l_t2
< l_t1
)
759 uint16_t stype
, slen
;
761 // Test status and bail if error
762 dhcpv6_for_each_option(&ia_hdr
[1], odata
+ olen
,
764 if (stype
== DHCPV6_OPT_STATUS
&& slen
>= 2)
765 error
= ((int)sdata
[0]) << 8 | ((int)sdata
[1]);
768 syslog(LOG_WARNING
, "Server returned IAID status %i!", error
);
774 uint32_t n
= dhcpv6_parse_ia(&ia_hdr
[1], odata
+ olen
);
786 if (l_t1
> 0 && t1
> l_t1
)
789 if (l_t2
> 0 && t2
> l_t2
)
792 } else if (otype
== DHCPV6_OPT_DNS_SERVERS
) {
794 odhcp6c_add_state(STATE_DNS
, odata
, olen
);
795 } else if (otype
== DHCPV6_OPT_DNS_DOMAIN
) {
796 odhcp6c_add_state(STATE_SEARCH
, odata
, olen
);
797 } else if (otype
== DHCPV6_OPT_NTP_SERVER
) {
798 uint16_t stype
, slen
;
800 // Test status and bail if error
801 dhcpv6_for_each_option(odata
, odata
+ olen
,
802 stype
, slen
, sdata
) {
803 if (slen
== 16 && (stype
== NTP_MC_ADDR
||
804 stype
== NTP_SRV_ADDR
))
805 odhcp6c_add_state(STATE_SNTP_IP
,
807 else if (slen
> 0 && stype
== NTP_SRV_FQDN
)
808 odhcp6c_add_state(STATE_SNTP_FQDN
,
811 } else if (otype
== DHCPV6_OPT_SIP_SERVER_A
) {
813 odhcp6c_add_state(STATE_SIP_IP
, odata
, olen
);
814 } else if (otype
== DHCPV6_OPT_SIP_SERVER_D
) {
815 odhcp6c_add_state(STATE_SIP_FQDN
, odata
, olen
);
816 } else if (otype
== DHCPV6_OPT_INFO_REFRESH
&& olen
>= 4) {
817 uint32_t refresh
= ntohl(*((uint32_t*)odata
));
818 if (refresh
< (uint32_t)t1
)
820 } else if (otype
== DHCPV6_OPT_AUTH
&& olen
== -4 +
821 sizeof(struct dhcpv6_auth_reconfigure
)) {
822 struct dhcpv6_auth_reconfigure
*r
= (void*)&odata
[-4];
823 if (r
->protocol
== 3 && r
->algorithm
== 1 &&
825 memcpy(reconf_key
, r
->key
, sizeof(r
->key
));
826 } else if (otype
== DHCPV6_OPT_AFTR_NAME
&& olen
> 3) {
828 odhcp6c_get_state(STATE_AFTR_NAME
, &cur_len
);
830 odhcp6c_add_state(STATE_AFTR_NAME
, odata
, olen
);
831 } else if (otype
!= DHCPV6_OPT_CLIENTID
&&
832 otype
!= DHCPV6_OPT_SERVERID
) {
833 odhcp6c_add_state(STATE_CUSTOM_OPTS
,
834 &odata
[-4], olen
+ 4);
838 if (t1
== UINT32_MAX
)
841 if (t2
== UINT32_MAX
)
844 if (t3
== UINT32_MAX
)
851 static uint32_t dhcpv6_parse_ia(void *opt
, void *end
)
853 uint32_t timeout
= UINT32_MAX
; // Minimum timeout
854 uint16_t otype
, olen
;
858 dhcpv6_for_each_option(opt
, end
, otype
, olen
, odata
) {
859 struct odhcp6c_entry entry
= {IN6ADDR_ANY_INIT
, 0, 0,
860 IN6ADDR_ANY_INIT
, 0, 0, 0};
862 if (otype
== DHCPV6_OPT_IA_PREFIX
) {
863 struct dhcpv6_ia_prefix
*prefix
= (void*)&odata
[-4];
864 if (olen
+ 4U < sizeof(*prefix
))
867 entry
.valid
= ntohl(prefix
->valid
);
868 entry
.preferred
= ntohl(prefix
->preferred
);
870 if (entry
.preferred
> entry
.valid
)
873 entry
.length
= prefix
->prefix
;
874 entry
.target
= prefix
->addr
;
875 uint16_t stype
, slen
;
878 #ifdef EXT_PREFIX_CLASS
879 // Find prefix class, if any
880 dhcpv6_for_each_option(&prefix
[1], odata
+ olen
,
882 if (stype
== DHCPV6_OPT_PREFIX_CLASS
&& slen
== 2)
883 entry
.class = sdata
[0] << 8 | sdata
[1];
888 dhcpv6_for_each_option(odata
+ sizeof(*prefix
) - 4U,
889 odata
+ olen
, stype
, slen
, sdata
) {
890 if (stype
!= DHCPV6_OPT_PD_EXCLUDE
|| slen
< 2)
893 uint8_t elen
= sdata
[0];
897 if (elen
<= 32 || elen
<= entry
.length
) {
903 uint8_t bytes
= ((elen
- entry
.length
- 1) / 8) + 1;
909 uint32_t exclude
= 0;
911 exclude
= exclude
<< 8 | sdata
[bytes
];
914 exclude
>>= 8 - ((elen
- entry
.length
) % 8);
915 exclude
<<= 64 - elen
;
917 // Abusing router & priority fields for exclusion
918 entry
.router
= entry
.target
;
919 entry
.router
.s6_addr32
[1] |= htonl(exclude
);
920 entry
.priority
= elen
;
924 odhcp6c_update_entry(STATE_IA_PD
, &entry
);
927 memset(&entry
.router
, 0, sizeof(entry
.router
));
928 } else if (otype
== DHCPV6_OPT_IA_ADDR
) {
929 struct dhcpv6_ia_addr
*addr
= (void*)&odata
[-4];
930 if (olen
+ 4U < sizeof(*addr
))
933 entry
.preferred
= ntohl(addr
->preferred
);
934 entry
.valid
= ntohl(addr
->valid
);
936 if (entry
.preferred
> entry
.valid
)
940 entry
.target
= addr
->addr
;
942 #ifdef EXT_PREFIX_CLASS
943 uint16_t stype
, slen
;
945 // Find prefix class, if any
946 dhcpv6_for_each_option(&addr
[1], odata
+ olen
,
948 if (stype
== DHCPV6_OPT_PREFIX_CLASS
&& slen
== 2)
949 entry
.class = sdata
[0] << 8 | sdata
[1];
952 odhcp6c_update_entry(STATE_IA_NA
, &entry
);
954 if (entry
.valid
> 0 && timeout
> entry
.valid
)
955 timeout
= entry
.valid
;