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 int dhcpv6_parse_ia(void *opt
, void *end
);
51 static int dhcpv6_calc_refresh_timers(void);
52 static void dhcpv6_handle_status_code(_unused
const enum dhcpv6_msg orig
,
53 const uint16_t code
, const void *status_msg
, const int len
,
55 static void dhcpv6_handle_ia_status_code(const enum dhcpv6_msg orig
,
56 const struct dhcpv6_ia_hdr
*ia_hdr
, const uint16_t code
,
57 const void *status_msg
, const int len
,
58 bool handled_status_codes
[_DHCPV6_Status_Max
],
61 static reply_handler dhcpv6_handle_reply
;
62 static reply_handler dhcpv6_handle_advert
;
63 static reply_handler dhcpv6_handle_rebind_reply
;
64 static reply_handler dhcpv6_handle_reconfigure
;
65 static int dhcpv6_commit_advert(void);
69 // RFC 3315 - 5.5 Timeout and Delay values
70 static struct dhcpv6_retx dhcpv6_retx
[_DHCPV6_MSG_MAX
] = {
71 [DHCPV6_MSG_UNKNOWN
] = {false, 1, 120, 0, "<POLL>",
72 dhcpv6_handle_reconfigure
, NULL
},
73 [DHCPV6_MSG_SOLICIT
] = {true, 1, 3600, 0, "SOLICIT",
74 dhcpv6_handle_advert
, dhcpv6_commit_advert
},
75 [DHCPV6_MSG_REQUEST
] = {true, 1, 30, 10, "REQUEST",
76 dhcpv6_handle_reply
, NULL
},
77 [DHCPV6_MSG_RENEW
] = {false, 10, 600, 0, "RENEW",
78 dhcpv6_handle_reply
, NULL
},
79 [DHCPV6_MSG_REBIND
] = {false, 10, 600, 0, "REBIND",
80 dhcpv6_handle_rebind_reply
, NULL
},
81 [DHCPV6_MSG_RELEASE
] = {false, 1, 0, 5, "RELEASE", NULL
, NULL
},
82 [DHCPV6_MSG_DECLINE
] = {false, 1, 0, 5, "DECLINE", NULL
, NULL
},
83 [DHCPV6_MSG_INFO_REQ
] = {true, 1, 120, 0, "INFOREQ",
84 dhcpv6_handle_reply
, NULL
},
90 static int ifindex
= -1;
91 static int64_t t1
= 0, t2
= 0, t3
= 0;
94 static int request_prefix
= -1;
95 static enum odhcp6c_ia_mode na_mode
= IA_MODE_NONE
, pd_mode
= IA_MODE_NONE
;
96 static bool accept_reconfig
= false;
99 static uint8_t reconf_key
[16];
103 int init_dhcpv6(const char *ifname
, int request_pd
, int sol_timeout
)
105 request_prefix
= request_pd
;
106 dhcpv6_retx
[DHCPV6_MSG_SOLICIT
].max_timeo
= sol_timeout
;
108 sock
= socket(AF_INET6
, SOCK_DGRAM
| SOCK_CLOEXEC
, IPPROTO_UDP
);
112 strncpy(ifr
.ifr_name
, ifname
, sizeof(ifr
.ifr_name
));
113 if (ioctl(sock
, SIOCGIFINDEX
, &ifr
))
115 ifindex
= ifr
.ifr_ifindex
;
117 // Create client DUID
118 size_t client_id_len
;
119 odhcp6c_get_state(STATE_CLIENT_ID
, &client_id_len
);
120 if (client_id_len
== 0) {
121 ioctl(sock
, SIOCGIFHWADDR
, &ifr
);
122 uint8_t duid
[14] = {0, DHCPV6_OPT_CLIENTID
, 0, 10, 0,
123 DHCPV6_DUID_LLADDR
, 0, 1};
124 memcpy(&duid
[8], ifr
.ifr_hwaddr
.sa_data
, ETHER_ADDR_LEN
);
126 uint8_t zero
[ETHER_ADDR_LEN
] = {0, 0, 0, 0, 0, 0};
127 struct ifreq ifs
[100], *ifp
, *ifend
;
130 ifc
.ifc_len
= sizeof(ifs
);
132 if (!memcmp(&duid
[8], zero
, ETHER_ADDR_LEN
) &&
133 ioctl(sock
, SIOCGIFCONF
, &ifc
) >= 0) {
134 // If our interface doesn't have an address...
135 ifend
= ifs
+ (ifc
.ifc_len
/ sizeof(struct ifreq
));
136 for (ifp
= ifc
.ifc_req
; ifp
< ifend
&&
137 !memcmp(&duid
[8], zero
, 6); ifp
++) {
138 memcpy(ifr
.ifr_name
, ifp
->ifr_name
,
139 sizeof(ifr
.ifr_name
));
140 ioctl(sock
, SIOCGIFHWADDR
, &ifr
);
141 memcpy(&duid
[8], ifr
.ifr_hwaddr
.sa_data
,
146 odhcp6c_add_state(STATE_CLIENT_ID
, duid
, sizeof(duid
));
151 htons(DHCPV6_OPT_SIP_SERVER_D
),
152 htons(DHCPV6_OPT_SIP_SERVER_A
),
153 htons(DHCPV6_OPT_DNS_SERVERS
),
154 htons(DHCPV6_OPT_DNS_DOMAIN
),
155 htons(DHCPV6_OPT_NTP_SERVER
),
156 htons(DHCPV6_OPT_SIP_SERVER_A
),
157 htons(DHCPV6_OPT_AFTR_NAME
),
158 htons(DHCPV6_OPT_PD_EXCLUDE
),
159 #ifdef EXT_PREFIX_CLASS
160 htons(DHCPV6_OPT_PREFIX_CLASS
),
163 odhcp6c_add_state(STATE_ORO
, oro
, sizeof(oro
));
166 // Configure IPv6-options
168 setsockopt(sock
, IPPROTO_IPV6
, IPV6_V6ONLY
, &val
, sizeof(val
));
169 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
171 setsockopt(sock
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
, &val
, sizeof(val
));
172 setsockopt(sock
, SOL_SOCKET
, SO_BINDTODEVICE
, ifname
, strlen(ifname
));
174 struct sockaddr_in6 client_addr
= { .sin6_family
= AF_INET6
,
175 .sin6_port
= htons(DHCPV6_CLIENT_PORT
), .sin6_flowinfo
= 0 };
176 if (bind(sock
, (struct sockaddr
*)&client_addr
, sizeof(client_addr
)))
183 void dhcpv6_set_ia_mode(enum odhcp6c_ia_mode na
, enum odhcp6c_ia_mode pd
)
190 static void dhcpv6_send(enum dhcpv6_msg type
, uint8_t trid
[3], uint32_t ecs
)
194 gethostname(fqdn_buf
, sizeof(fqdn_buf
));
201 size_t fqdn_len
= 5 + dn_comp(fqdn_buf
, fqdn
.data
,
202 sizeof(fqdn
.data
), NULL
, NULL
);
203 fqdn
.type
= htons(DHCPV6_OPT_FQDN
);
204 fqdn
.len
= htons(fqdn_len
- 4);
210 void *cl_id
= odhcp6c_get_state(STATE_CLIENT_ID
, &cl_id_len
);
214 void *srv_id
= odhcp6c_get_state(STATE_SERVER_ID
, &srv_id_len
);
217 size_t ia_pd_entries
, ia_pd_len
= 0;
218 struct odhcp6c_entry
*e
= odhcp6c_get_state(STATE_IA_PD
, &ia_pd_entries
);
219 ia_pd_entries
/= sizeof(*e
);
220 struct dhcpv6_ia_hdr hdr_ia_pd
= {
221 htons(DHCPV6_OPT_IA_PD
),
222 htons(sizeof(hdr_ia_pd
) - 4),
227 uint8_t *ia_pd
= alloca(ia_pd_entries
* (sizeof(struct dhcpv6_ia_prefix
) + 10));
228 for (size_t i
= 0; i
< ia_pd_entries
; ++i
) {
230 if (e
[i
].priority
> 0)
231 ex_len
= ((e
[i
].priority
- e
[i
].length
- 1) / 8) + 6;
233 struct dhcpv6_ia_prefix p
= {
234 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
235 .len
= htons(sizeof(p
) - 4U + ex_len
),
236 .prefix
= e
[i
].length
,
240 memcpy(ia_pd
+ ia_pd_len
, &p
, sizeof(p
));
241 ia_pd_len
+= sizeof(p
);
244 ia_pd
[ia_pd_len
++] = 0;
245 ia_pd
[ia_pd_len
++] = DHCPV6_OPT_PD_EXCLUDE
;
246 ia_pd
[ia_pd_len
++] = 0;
247 ia_pd
[ia_pd_len
++] = ex_len
- 4;
248 ia_pd
[ia_pd_len
++] = e
[i
].priority
;
250 uint32_t excl
= ntohl(e
[i
].router
.s6_addr32
[1]);
251 excl
>>= (64 - e
[i
].priority
);
252 excl
<<= 8 - ((e
[i
].priority
- e
[i
].length
) % 8);
254 for (size_t i
= ex_len
- 5; i
> 0; --i
, excl
>>= 8)
255 ia_pd
[ia_pd_len
+ i
] = excl
& 0xff;
256 ia_pd_len
+= ex_len
- 5;
260 struct dhcpv6_ia_prefix pref
= {
261 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
262 .len
= htons(25), .prefix
= request_prefix
264 if (request_prefix
> 0 && ia_pd_len
== 0 && type
== DHCPV6_MSG_SOLICIT
) {
265 ia_pd
= (uint8_t*)&pref
;
266 ia_pd_len
= sizeof(pref
);
268 hdr_ia_pd
.len
= htons(ntohs(hdr_ia_pd
.len
) + ia_pd_len
);
271 size_t ia_na_entries
, ia_na_len
= 0;
273 e
= odhcp6c_get_state(STATE_IA_NA
, &ia_na_entries
);
274 ia_na_entries
/= sizeof(*e
);
276 struct dhcpv6_ia_hdr hdr_ia_na
= {
277 htons(DHCPV6_OPT_IA_NA
),
278 htons(sizeof(hdr_ia_na
) - 4),
282 struct dhcpv6_ia_addr pa
[ia_na_entries
];
283 for (size_t i
= 0; i
< ia_na_entries
; ++i
) {
284 pa
[i
].type
= htons(DHCPV6_OPT_IA_ADDR
);
285 pa
[i
].len
= htons(sizeof(pa
[i
]) - 4U);
286 pa
[i
].addr
= e
[i
].target
;
292 ia_na_len
= sizeof(pa
);
293 hdr_ia_na
.len
= htons(ntohs(hdr_ia_na
.len
) + ia_na_len
);
295 // Reconfigure Accept
299 } reconf_accept
= {htons(DHCPV6_OPT_RECONF_ACCEPT
), 0};
301 // Request Information Refresh
302 uint16_t oro_refresh
= htons(DHCPV6_OPT_INFO_REFRESH
);
306 void *oro
= odhcp6c_get_state(STATE_ORO
, &oro_len
);
310 uint16_t elapsed_type
;
311 uint16_t elapsed_len
;
312 uint16_t elapsed_value
;
316 type
, {trid
[0], trid
[1], trid
[2]},
317 htons(DHCPV6_OPT_ELAPSED
), htons(2),
318 htons((ecs
> 0xffff) ? 0xffff : ecs
),
319 htons(DHCPV6_OPT_ORO
), htons(oro_len
),
322 struct iovec iov
[] = {
327 {srv_id
, srv_id_len
},
328 {&reconf_accept
, sizeof(reconf_accept
)},
330 {&hdr_ia_na
, sizeof(hdr_ia_na
)},
332 {&hdr_ia_pd
, sizeof(hdr_ia_pd
)},
336 size_t cnt
= ARRAY_SIZE(iov
);
337 if (type
== DHCPV6_MSG_INFO_REQ
) {
339 iov
[2].iov_len
= sizeof(oro_refresh
);
340 hdr
.oro_len
= htons(oro_len
+ sizeof(oro_refresh
));
341 } else if (!request_prefix
) {
345 // Disable IAs if not used
346 if (type
!= DHCPV6_MSG_SOLICIT
) {
354 if (na_mode
== IA_MODE_NONE
)
357 struct sockaddr_in6 srv
= {AF_INET6
, htons(DHCPV6_SERVER_PORT
),
358 0, ALL_DHCPV6_RELAYS
, ifindex
};
359 struct msghdr msg
= {&srv
, sizeof(srv
), iov
, cnt
, NULL
, 0, 0};
361 sendmsg(sock
, &msg
, 0);
365 static int64_t dhcpv6_rand_delay(int64_t time
)
368 odhcp6c_random(&random
, sizeof(random
));
369 return (time
* ((int64_t)random
% 1000LL)) / 10000LL;
373 int dhcpv6_request(enum dhcpv6_msg type
)
375 uint8_t buf
[1536], rc
= 0;
376 uint64_t timeout
= UINT32_MAX
;
377 struct dhcpv6_retx
*retx
= &dhcpv6_retx
[type
];
380 struct timespec ts
= {0, 0};
381 ts
.tv_nsec
= dhcpv6_rand_delay(10 * DHCPV6_REQ_DELAY
);
382 nanosleep(&ts
, NULL
);
385 if (type
== DHCPV6_MSG_UNKNOWN
)
387 else if (type
== DHCPV6_MSG_RENEW
)
388 timeout
= (t2
> t1
) ? t2
- t1
: 0;
389 else if (type
== DHCPV6_MSG_REBIND
)
390 timeout
= (t3
> t2
) ? t3
- t2
: 0;
395 syslog(LOG_NOTICE
, "Starting %s transaction (timeout %llus, max rc %d)",
396 retx
->name
, (unsigned long long)timeout
, retx
->max_rc
);
398 uint64_t start
= odhcp6c_get_milli_time(), round_start
= start
, elapsed
;
400 // Generate transaction ID
401 uint8_t trid
[3] = {0, 0, 0};
402 if (type
!= DHCPV6_MSG_UNKNOWN
)
403 odhcp6c_random(trid
, sizeof(trid
));
409 int64_t delay
= dhcpv6_rand_delay(retx
->init_timeo
* 1000);
411 // First RT MUST be strictly greater than IRT for solicit messages (RFC3313 17.1.2)
412 while (type
== DHCPV6_MSG_SOLICIT
&& delay
<= 0)
413 delay
= dhcpv6_rand_delay(retx
->init_timeo
* 1000);
415 rto
= (retx
->init_timeo
* 1000 + delay
);
418 rto
= (2 * rto
+ dhcpv6_rand_delay(rto
));
420 if (retx
->max_timeo
&& (rto
>= retx
->max_timeo
* 1000))
421 rto
= retx
->max_timeo
* 1000 +
422 dhcpv6_rand_delay(retx
->max_timeo
* 1000);
424 // Calculate end for this round and elapsed time
425 uint64_t round_end
= round_start
+ rto
;
426 elapsed
= round_start
- start
;
428 // Don't wait too long
429 if (round_end
- start
> timeout
* 1000)
430 round_end
= timeout
* 1000 + start
;
432 // Built and send package
433 if (type
!= DHCPV6_MSG_UNKNOWN
) {
434 syslog(LOG_NOTICE
, "Send %s message (elapsed %llums, rc %d)",
435 retx
->name
, (unsigned long long)elapsed
, rc
);
436 dhcpv6_send(type
, trid
, elapsed
/ 10);
441 for (; len
< 0 && round_start
< round_end
;
442 round_start
= odhcp6c_get_milli_time()) {
443 // Check for pending signal
444 if (odhcp6c_signal_process())
447 // Set timeout for receiving
448 uint64_t t
= round_end
- round_start
;
449 struct timeval timeout
= {t
/ 1000, (t
% 1000) * 1000};
450 setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
,
451 &timeout
, sizeof(timeout
));
454 len
= recv(sock
, buf
, sizeof(buf
), 0);
456 if (!dhcpv6_response_is_valid(buf
, len
, trid
, type
))
460 uint8_t *opt
= &buf
[4];
461 uint8_t *opt_end
= opt
+ len
- 4;
463 round_start
= odhcp6c_get_milli_time();
464 elapsed
= round_start
- start
;
465 syslog(LOG_NOTICE
, "Got a valid reply after "
466 "%llums", (unsigned long long)elapsed
);
468 if (retx
->handler_reply
)
469 len
= retx
->handler_reply(
470 type
, rc
, opt
, opt_end
);
472 if (len
> 0 && round_end
- round_start
> 1000)
473 round_end
= 1000 + round_start
;
478 if (retx
->handler_finish
)
479 len
= retx
->handler_finish();
480 } while (len
< 0 && ((elapsed
/ 1000 < timeout
) && (!retx
->max_rc
|| rc
< retx
->max_rc
)));
486 static bool dhcpv6_response_is_valid(const void *buf
, ssize_t len
,
487 const uint8_t transaction
[3], enum dhcpv6_msg type
)
489 const struct dhcpv6_header
*rep
= buf
;
490 if (len
< (ssize_t
)sizeof(*rep
) || memcmp(rep
->tr_id
,
491 transaction
, sizeof(rep
->tr_id
)))
492 return false; // Invalid reply
494 if (type
== DHCPV6_MSG_SOLICIT
) {
495 if (rep
->msg_type
!= DHCPV6_MSG_ADVERT
&&
496 rep
->msg_type
!= DHCPV6_MSG_REPLY
)
498 } else if (type
== DHCPV6_MSG_UNKNOWN
) {
499 if (!accept_reconfig
|| rep
->msg_type
!= DHCPV6_MSG_RECONF
)
501 } else if (rep
->msg_type
!= DHCPV6_MSG_REPLY
) {
505 uint8_t *end
= ((uint8_t*)buf
) + len
, *odata
;
506 uint16_t otype
, olen
;
507 bool clientid_ok
= false, serverid_ok
= false, rcauth_ok
= false;
509 size_t client_id_len
, server_id_len
;
510 void *client_id
= odhcp6c_get_state(STATE_CLIENT_ID
, &client_id_len
);
511 void *server_id
= odhcp6c_get_state(STATE_SERVER_ID
, &server_id_len
);
513 dhcpv6_for_each_option(&rep
[1], end
, otype
, olen
, odata
) {
514 if (otype
== DHCPV6_OPT_CLIENTID
) {
515 clientid_ok
= (olen
+ 4U == client_id_len
) && !memcmp(
516 &odata
[-4], client_id
, client_id_len
);
517 } else if (otype
== DHCPV6_OPT_SERVERID
) {
518 serverid_ok
= (olen
+ 4U == server_id_len
) && !memcmp(
519 &odata
[-4], server_id
, server_id_len
);
520 } else if (otype
== DHCPV6_OPT_AUTH
&& olen
== -4 +
521 sizeof(struct dhcpv6_auth_reconfigure
)) {
522 struct dhcpv6_auth_reconfigure
*r
= (void*)&odata
[-4];
523 if (r
->protocol
!= 3 || r
->algorithm
!= 1 || r
->reconf_type
!= 2)
527 uint8_t serverhash
[16], secretbytes
[16], hash
[16];
528 memcpy(serverhash
, r
->key
, sizeof(serverhash
));
529 memset(r
->key
, 0, sizeof(r
->key
));
530 memcpy(secretbytes
, reconf_key
, sizeof(secretbytes
));
532 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
)
533 secretbytes
[i
] ^= 0x36;
536 md5_append(&md5
, secretbytes
, sizeof(secretbytes
));
537 md5_append(&md5
, buf
, len
);
538 md5_finish(&md5
, hash
);
540 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
) {
541 secretbytes
[i
] ^= 0x36;
542 secretbytes
[i
] ^= 0x5c;
546 md5_append(&md5
, secretbytes
, sizeof(secretbytes
));
547 md5_append(&md5
, hash
, 16);
548 md5_finish(&md5
, hash
);
550 rcauth_ok
= !memcmp(hash
, serverhash
, sizeof(hash
));
554 if (rep
->msg_type
== DHCPV6_MSG_RECONF
&& !rcauth_ok
)
557 return clientid_ok
&& (serverid_ok
|| server_id_len
== 0);
561 int dhcpv6_poll_reconfigure(void)
563 int ret
= dhcpv6_request(DHCPV6_MSG_UNKNOWN
);
565 ret
= dhcpv6_request(ret
);
571 static int dhcpv6_handle_reconfigure(_unused
enum dhcpv6_msg orig
, const int rc
,
572 const void *opt
, const void *end
)
574 // TODO: should verify the reconfigure message
575 uint16_t otype
, olen
;
576 uint8_t *odata
, msg
= DHCPV6_MSG_RENEW
;
577 dhcpv6_for_each_option(opt
, end
, otype
, olen
, odata
)
578 if (otype
== DHCPV6_OPT_RECONF_MESSAGE
&& olen
== 1 && (
579 odata
[0] == DHCPV6_MSG_RENEW
||
580 odata
[0] == DHCPV6_MSG_INFO_REQ
))
583 dhcpv6_handle_reply(DHCPV6_MSG_UNKNOWN
, rc
, NULL
, NULL
);
588 // Collect all advertised servers
589 static int dhcpv6_handle_advert(enum dhcpv6_msg orig
, const int rc
,
590 const void *opt
, const void *end
)
592 uint16_t olen
, otype
;
593 uint8_t *odata
, pref
= 0;
594 struct dhcpv6_server_cand cand
= {false, false, 0, 0, {0}, NULL
, NULL
, 0, 0};
595 bool have_na
= false;
598 dhcpv6_for_each_option(opt
, end
, otype
, olen
, odata
) {
599 if (orig
== DHCPV6_MSG_SOLICIT
&&
600 (otype
== DHCPV6_OPT_IA_PD
|| otype
== DHCPV6_OPT_IA_NA
) &&
601 olen
> sizeof(struct dhcpv6_ia_hdr
)) {
602 struct dhcpv6_ia_hdr
*ia_hdr
= (void*)(&odata
[-4]);
603 dhcpv6_parse_ia(ia_hdr
, odata
+ olen
+ sizeof(*ia_hdr
));
606 if (otype
== DHCPV6_OPT_SERVERID
&& olen
<= 130) {
607 memcpy(cand
.duid
, odata
, olen
);
608 cand
.duid_len
= olen
;
609 } else if (otype
== DHCPV6_OPT_STATUS
&& olen
>= 2) {
610 int error
= ((int)odata
[0] << 8 | (int)odata
[1]);
613 case DHCPV6_NoPrefixAvail
:
614 // Status code on global level
615 if (pd_mode
== IA_MODE_FORCE
)
617 cand
.preference
-= 2000;
620 case DHCPV6_NoAddrsAvail
:
621 // Status code on global level
622 if (na_mode
== IA_MODE_FORCE
)
629 } else if (otype
== DHCPV6_OPT_PREF
&& olen
>= 1 &&
630 cand
.preference
>= 0) {
631 cand
.preference
= pref
= odata
[0];
632 } else if (otype
== DHCPV6_OPT_RECONF_ACCEPT
) {
633 cand
.wants_reconfigure
= true;
634 } else if (otype
== DHCPV6_OPT_IA_PD
&& request_prefix
) {
635 struct dhcpv6_ia_hdr
*h
= (struct dhcpv6_ia_hdr
*)&odata
[-4];
636 uint8_t *oend
= odata
+ olen
, *d
;
637 dhcpv6_for_each_option(&h
[1], oend
, otype
, olen
, d
) {
638 if (otype
== DHCPV6_OPT_IA_PREFIX
&& (olen
+ 4) >=
639 (uint16_t)sizeof(struct dhcpv6_ia_prefix
)) {
640 struct dhcpv6_ia_prefix
*p
= (struct dhcpv6_ia_prefix
*)&d
[-4];
644 } else if (otype
== DHCPV6_OPT_IA_NA
) {
645 struct dhcpv6_ia_hdr
*h
= (struct dhcpv6_ia_hdr
*)&odata
[-4];
646 uint8_t *oend
= odata
+ olen
, *d
;
647 dhcpv6_for_each_option(&h
[1], oend
, otype
, olen
, d
)
648 if (otype
== DHCPV6_OPT_IA_ADDR
)
653 if ((!have_na
&& na_mode
== IA_MODE_FORCE
) ||
654 (!have_pd
&& pd_mode
== IA_MODE_FORCE
))
657 if (na_mode
!= IA_MODE_NONE
&& !have_na
) {
658 cand
.has_noaddravail
= true;
659 cand
.preference
-= 1000;
662 if (pd_mode
!= IA_MODE_NONE
) {
664 cand
.preference
+= 2000 + (128 - have_pd
);
666 cand
.preference
-= 2000;
669 if (cand
.duid_len
> 0) {
670 cand
.ia_na
= odhcp6c_move_state(STATE_IA_NA
, &cand
.ia_na_len
);
671 cand
.ia_pd
= odhcp6c_move_state(STATE_IA_PD
, &cand
.ia_pd_len
);
672 odhcp6c_add_state(STATE_SERVER_CAND
, &cand
, sizeof(cand
));
675 if (orig
== DHCPV6_MSG_SOLICIT
) {
676 odhcp6c_clear_state(STATE_IA_NA
);
677 odhcp6c_clear_state(STATE_IA_PD
);
680 return (rc
> 1 || (pref
== 255 && cand
.preference
> 0)) ? 1 : -1;
684 static int dhcpv6_commit_advert(void)
687 struct dhcpv6_server_cand
*c
= NULL
, *cand
=
688 odhcp6c_get_state(STATE_SERVER_CAND
, &cand_len
);
691 for (size_t i
= 0; i
< cand_len
/ sizeof(*c
); ++i
) {
692 if (cand
[i
].has_noaddravail
)
693 retry
= true; // We want to try again
695 if (!c
|| c
->preference
< cand
[i
].preference
)
699 if (retry
&& na_mode
== IA_MODE_TRY
) {
700 // We give it a second try without the IA_NA
701 na_mode
= IA_MODE_NONE
;
702 return dhcpv6_request(DHCPV6_MSG_SOLICIT
);
706 uint16_t hdr
[2] = {htons(DHCPV6_OPT_SERVERID
),
708 odhcp6c_add_state(STATE_SERVER_ID
, hdr
, sizeof(hdr
));
709 odhcp6c_add_state(STATE_SERVER_ID
, c
->duid
, c
->duid_len
);
710 accept_reconfig
= c
->wants_reconfigure
;
712 odhcp6c_add_state(STATE_IA_NA
, c
->ia_na
, c
->ia_na_len
);
714 odhcp6c_add_state(STATE_IA_PD
, c
->ia_pd
, c
->ia_pd_len
);
717 for (size_t i
= 0; i
< cand_len
/ sizeof(*c
); ++i
) {
721 odhcp6c_clear_state(STATE_SERVER_CAND
);
725 else if ((request_prefix
&& c
->ia_pd_len
) || (na_mode
!= IA_MODE_NONE
&& c
->ia_na_len
))
726 return DHCPV6_STATEFUL
;
728 return DHCPV6_STATELESS
;
732 static int dhcpv6_handle_rebind_reply(enum dhcpv6_msg orig
, const int rc
,
733 const void *opt
, const void *end
)
735 dhcpv6_handle_advert(orig
, rc
, opt
, end
);
736 if (dhcpv6_commit_advert() < 0) {
737 dhcpv6_handle_reply(DHCPV6_MSG_UNKNOWN
, rc
, NULL
, NULL
);
741 return dhcpv6_handle_reply(orig
, rc
, opt
, end
);
745 static int dhcpv6_handle_reply(enum dhcpv6_msg orig
, _unused
const int rc
,
746 const void *opt
, const void *end
)
749 uint16_t otype
, olen
;
750 uint32_t refresh
= UINT32_MAX
;
752 bool handled_status_codes
[_DHCPV6_Status_Max
] = { false, };
756 if (orig
== DHCPV6_MSG_UNKNOWN
) {
757 static time_t last_update
= 0;
758 time_t now
= odhcp6c_get_milli_time() / 1000;
760 uint32_t elapsed
= (last_update
> 0) ? now
- last_update
: 0;
777 if (orig
== DHCPV6_MSG_REQUEST
&& !odhcp6c_is_bound()) {
778 // Delete NA and PD we have in the state from the Advert
779 odhcp6c_clear_state(STATE_IA_NA
);
780 odhcp6c_clear_state(STATE_IA_PD
);
784 odhcp6c_clear_state(STATE_DNS
);
785 odhcp6c_clear_state(STATE_SEARCH
);
786 odhcp6c_clear_state(STATE_SNTP_IP
);
787 odhcp6c_clear_state(STATE_SNTP_FQDN
);
788 odhcp6c_clear_state(STATE_SIP_IP
);
789 odhcp6c_clear_state(STATE_SIP_FQDN
);
790 odhcp6c_clear_state(STATE_AFTR_NAME
);
793 // Parse and find all matching IAs
794 dhcpv6_for_each_option(opt
, end
, otype
, olen
, odata
) {
795 if ((otype
== DHCPV6_OPT_IA_PD
|| otype
== DHCPV6_OPT_IA_NA
)
796 && olen
> sizeof(struct dhcpv6_ia_hdr
)) {
797 struct dhcpv6_ia_hdr
*ia_hdr
= (void*)(&odata
[-4]);
800 if (ia_hdr
->iaid
!= 1)
803 uint16_t code
= DHCPV6_Success
;
804 uint16_t stype
, slen
;
806 // Get and handle status code
807 dhcpv6_for_each_option(&ia_hdr
[1], odata
+ olen
,
808 stype
, slen
, sdata
) {
809 if (stype
== DHCPV6_OPT_STATUS
&& slen
>= 2) {
810 uint8_t *mdata
= (slen
> 2) ? &sdata
[2] : NULL
;
811 uint16_t mlen
= (slen
> 2) ? slen
- 2 : 0;
813 code
= ((int)sdata
[0]) << 8 | ((int)sdata
[1]);
815 if (code
== DHCPV6_Success
)
818 dhcpv6_handle_ia_status_code(orig
, ia_hdr
,
819 code
, mdata
, mlen
, handled_status_codes
, &ret
);
828 if (code
!= DHCPV6_Success
)
831 dhcpv6_parse_ia(ia_hdr
, odata
+ olen
+ sizeof(*ia_hdr
));
832 } else if (otype
== DHCPV6_OPT_STATUS
&& olen
>= 2) {
833 uint8_t *mdata
= (olen
> 2) ? &odata
[2] : NULL
;
834 uint16_t mlen
= (olen
> 2) ? olen
- 2 : 0;
835 uint16_t code
= ((int)odata
[0]) << 8 | ((int)odata
[1]);
837 dhcpv6_handle_status_code(orig
, code
, mdata
, mlen
, &ret
);
839 else if (otype
== DHCPV6_OPT_DNS_SERVERS
) {
841 odhcp6c_add_state(STATE_DNS
, odata
, olen
);
842 } else if (otype
== DHCPV6_OPT_DNS_DOMAIN
) {
843 odhcp6c_add_state(STATE_SEARCH
, odata
, olen
);
844 } else if (otype
== DHCPV6_OPT_NTP_SERVER
) {
845 uint16_t stype
, slen
;
847 // Test status and bail if error
848 dhcpv6_for_each_option(odata
, odata
+ olen
,
849 stype
, slen
, sdata
) {
850 if (slen
== 16 && (stype
== NTP_MC_ADDR
||
851 stype
== NTP_SRV_ADDR
))
852 odhcp6c_add_state(STATE_SNTP_IP
,
854 else if (slen
> 0 && stype
== NTP_SRV_FQDN
)
855 odhcp6c_add_state(STATE_SNTP_FQDN
,
858 } else if (otype
== DHCPV6_OPT_SIP_SERVER_A
) {
860 odhcp6c_add_state(STATE_SIP_IP
, odata
, olen
);
861 } else if (otype
== DHCPV6_OPT_SIP_SERVER_D
) {
862 odhcp6c_add_state(STATE_SIP_FQDN
, odata
, olen
);
863 } else if (otype
== DHCPV6_OPT_INFO_REFRESH
&& olen
>= 4) {
864 refresh
= ntohl(*((uint32_t*)odata
));
865 } else if (otype
== DHCPV6_OPT_AUTH
&& olen
== -4 +
866 sizeof(struct dhcpv6_auth_reconfigure
)) {
867 struct dhcpv6_auth_reconfigure
*r
= (void*)&odata
[-4];
868 if (r
->protocol
== 3 && r
->algorithm
== 1 &&
870 memcpy(reconf_key
, r
->key
, sizeof(r
->key
));
871 } else if (otype
== DHCPV6_OPT_AFTR_NAME
&& olen
> 3) {
873 odhcp6c_get_state(STATE_AFTR_NAME
, &cur_len
);
875 odhcp6c_add_state(STATE_AFTR_NAME
, odata
, olen
);
876 } else if (otype
!= DHCPV6_OPT_CLIENTID
&&
877 otype
!= DHCPV6_OPT_SERVERID
) {
878 odhcp6c_add_state(STATE_CUSTOM_OPTS
,
879 &odata
[-4], olen
+ 4);
883 if (orig
!= DHCPV6_MSG_INFO_REQ
) {
884 // Update refresh timers if no fatal status code was received
885 if ((ret
> 0) && dhcpv6_calc_refresh_timers()) {
887 case DHCPV6_MSG_RENEW
:
888 // Send further renews if T1 is not set
892 case DHCPV6_MSG_REBIND
:
893 // Send further rebinds if T1 and T2 is not set
910 static int dhcpv6_parse_ia(void *opt
, void *end
)
912 struct dhcpv6_ia_hdr
*ia_hdr
= (struct dhcpv6_ia_hdr
*)opt
;
915 uint16_t otype
, olen
;
918 t1
= ntohl(ia_hdr
->t1
);
919 t2
= ntohl(ia_hdr
->t2
);
925 dhcpv6_for_each_option(&ia_hdr
[1], end
, otype
, olen
, odata
) {
926 struct odhcp6c_entry entry
= {IN6ADDR_ANY_INIT
, 0, 0,
927 IN6ADDR_ANY_INIT
, 0, 0, 0, 0, 0};
929 if (otype
== DHCPV6_OPT_IA_PREFIX
) {
930 struct dhcpv6_ia_prefix
*prefix
= (void*)&odata
[-4];
931 if (olen
+ 4U < sizeof(*prefix
))
934 entry
.valid
= ntohl(prefix
->valid
);
935 entry
.preferred
= ntohl(prefix
->preferred
);
937 if (entry
.preferred
> entry
.valid
)
940 entry
.t1
= (t1
? t1
: (entry
.preferred
!= UINT32_MAX
? 0.5 * entry
.preferred
: UINT32_MAX
));
941 entry
.t2
= (t2
? t2
: (entry
.preferred
!= UINT32_MAX
? 0.8 * entry
.preferred
: UINT32_MAX
));
942 if (entry
.t1
> entry
.t2
)
945 entry
.length
= prefix
->prefix
;
946 entry
.target
= prefix
->addr
;
947 uint16_t stype
, slen
;
950 #ifdef EXT_PREFIX_CLASS
951 // Find prefix class, if any
952 dhcpv6_for_each_option(&prefix
[1], odata
+ olen
,
954 if (stype
== DHCPV6_OPT_PREFIX_CLASS
&& slen
== 2)
955 entry
.class = sdata
[0] << 8 | sdata
[1];
960 dhcpv6_for_each_option(odata
+ sizeof(*prefix
) - 4U,
961 odata
+ olen
, stype
, slen
, sdata
) {
962 if (stype
!= DHCPV6_OPT_PD_EXCLUDE
|| slen
< 2)
965 uint8_t elen
= sdata
[0];
969 if (elen
<= 32 || elen
<= entry
.length
) {
975 uint8_t bytes
= ((elen
- entry
.length
- 1) / 8) + 1;
981 uint32_t exclude
= 0;
983 exclude
= exclude
<< 8 | sdata
[bytes
];
986 exclude
>>= 8 - ((elen
- entry
.length
) % 8);
987 exclude
<<= 64 - elen
;
989 // Abusing router & priority fields for exclusion
990 entry
.router
= entry
.target
;
991 entry
.router
.s6_addr32
[1] |= htonl(exclude
);
992 entry
.priority
= elen
;
996 odhcp6c_update_entry(STATE_IA_PD
, &entry
);
1001 memset(&entry
.router
, 0, sizeof(entry
.router
));
1002 } else if (otype
== DHCPV6_OPT_IA_ADDR
) {
1003 struct dhcpv6_ia_addr
*addr
= (void*)&odata
[-4];
1004 if (olen
+ 4U < sizeof(*addr
))
1007 entry
.preferred
= ntohl(addr
->preferred
);
1008 entry
.valid
= ntohl(addr
->valid
);
1010 if (entry
.preferred
> entry
.valid
)
1013 entry
.t1
= (t1
? t1
: (entry
.preferred
!= UINT32_MAX
? 0.5 * entry
.preferred
: UINT32_MAX
));
1014 entry
.t2
= (t2
? t2
: (entry
.preferred
!= UINT32_MAX
? 0.8 * entry
.preferred
: UINT32_MAX
));
1015 if (entry
.t1
> entry
.t2
)
1016 entry
.t1
= entry
.t2
;
1019 entry
.target
= addr
->addr
;
1021 #ifdef EXT_PREFIX_CLASS
1022 uint16_t stype
, slen
;
1024 // Find prefix class, if any
1025 dhcpv6_for_each_option(&addr
[1], odata
+ olen
,
1027 if (stype
== DHCPV6_OPT_PREFIX_CLASS
&& slen
== 2)
1028 entry
.class = sdata
[0] << 8 | sdata
[1];
1031 odhcp6c_update_entry(STATE_IA_NA
, &entry
);
1039 static int dhcpv6_calc_refresh_timers(void)
1041 struct odhcp6c_entry
*e
;
1042 size_t ia_na_entries
, ia_pd_entries
, i
;
1043 int64_t l_t1
= UINT32_MAX
, l_t2
= UINT32_MAX
, l_t3
= 0;
1045 e
= odhcp6c_get_state(STATE_IA_NA
, &ia_na_entries
);
1046 ia_na_entries
/= sizeof(*e
);
1047 for (i
= 0; i
< ia_na_entries
; i
++) {
1054 if (e
[i
].valid
> l_t3
)
1058 e
= odhcp6c_get_state(STATE_IA_PD
, &ia_pd_entries
);
1059 ia_pd_entries
/= sizeof(*e
);
1060 for (i
= 0; i
< ia_pd_entries
; i
++) {
1067 if (e
[i
].valid
> l_t3
)
1071 if (ia_pd_entries
|| ia_na_entries
) {
1077 return (int)(ia_pd_entries
+ ia_na_entries
);
1081 static void dhcpv6_log_status_code(const uint16_t code
, const char *scope
,
1082 const void *status_msg
, const int len
)
1084 uint8_t buf
[len
+ 3];
1086 memset(buf
, 0, sizeof(buf
));
1089 memcpy(&buf
[1], status_msg
, len
);
1093 syslog(LOG_WARNING
, "Server returned %s status %i %s",
1098 static void dhcpv6_handle_status_code(const enum dhcpv6_msg orig
,
1099 const uint16_t code
, const void *status_msg
, const int len
,
1102 dhcpv6_log_status_code(code
, "message", status_msg
, len
);
1105 case DHCPV6_UnspecFail
:
1110 case DHCPV6_UseMulticast
:
1111 // TODO handle multicast status code
1114 case DHCPV6_NoAddrsAvail
:
1115 case DHCPV6_NoPrefixAvail
:
1116 if (orig
== DHCPV6_MSG_REQUEST
)
1117 *ret
= 0; // Failure
1126 static void dhcpv6_handle_ia_status_code(const enum dhcpv6_msg orig
,
1127 const struct dhcpv6_ia_hdr
*ia_hdr
, const uint16_t code
,
1128 const void *status_msg
, const int len
,
1129 bool handled_status_codes
[_DHCPV6_Status_Max
], int *ret
)
1131 dhcpv6_log_status_code(code
, ia_hdr
->type
== DHCPV6_OPT_IA_NA
?
1132 "IA_NA" : "IA_PD", status_msg
, len
);
1135 case DHCPV6_NoBinding
:
1137 case DHCPV6_MSG_RENEW
:
1138 case DHCPV6_MSG_REBIND
:
1139 if ((*ret
> 0) && !handled_status_codes
[code
])
1140 *ret
= dhcpv6_request(DHCPV6_MSG_REQUEST
);
1148 case DHCPV6_NoAddrsAvail
:
1149 case DHCPV6_NoPrefixAvail
:
1151 case DHCPV6_MSG_REQUEST
:
1160 case DHCPV6_NotOnLink
:
1161 // TODO handle not onlink in case of confirm