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 <arpa/inet.h>
30 #include <netinet/in.h>
33 #include <net/ethernet.h>
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
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
);
51 static int dhcpv6_parse_ia(void *opt
, void *end
);
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
,
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
],
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);
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
},
92 static int ifindex
= -1;
93 static int64_t t1
= 0, t2
= 0, t3
= 0;
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
;
103 static uint8_t reconf_key
[16];
106 int init_dhcpv6(const char *ifname
, int request_pd
, int sol_timeout
)
108 request_prefix
= request_pd
;
109 dhcpv6_retx
[DHCPV6_MSG_SOLICIT
].max_timeo
= sol_timeout
;
111 sock
= socket(AF_INET6
, SOCK_DGRAM
| SOCK_CLOEXEC
, IPPROTO_UDP
);
117 strncpy(ifr
.ifr_name
, ifname
, sizeof(ifr
.ifr_name
));
118 if (ioctl(sock
, SIOCGIFINDEX
, &ifr
) < 0)
120 ifindex
= ifr
.ifr_ifindex
;
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};
129 if (ioctl(sock
, SIOCGIFHWADDR
, &ifr
) >= 0)
130 memcpy(&duid
[8], ifr
.ifr_hwaddr
.sa_data
, ETHER_ADDR_LEN
);
132 uint8_t zero
[ETHER_ADDR_LEN
] = {0, 0, 0, 0, 0, 0};
133 struct ifreq ifs
[100], *ifp
, *ifend
;
136 ifc
.ifc_len
= sizeof(ifs
);
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)
148 memcpy(&duid
[8], ifr
.ifr_hwaddr
.sa_data
,
153 odhcp6c_add_state(STATE_CLIENT_ID
, duid
, sizeof(duid
));
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
),
170 odhcp6c_add_state(STATE_ORO
, oro
, sizeof(oro
));
173 // Configure IPv6-options
175 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_V6ONLY
, &val
, sizeof(val
)) < 0)
177 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
)) < 0)
179 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &val
, sizeof(val
)) < 0)
183 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
, &val
, sizeof(val
)) < 0)
185 if (setsockopt(sock
, SOL_SOCKET
, SO_BINDTODEVICE
, ifname
, strlen(ifname
)) < 0)
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)
200 void dhcpv6_set_ia_mode(enum odhcp6c_ia_mode na
, enum odhcp6c_ia_mode pd
)
207 static void dhcpv6_send(enum dhcpv6_msg type
, uint8_t trid
[3], uint32_t ecs
)
211 gethostname(fqdn_buf
, sizeof(fqdn_buf
));
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);
227 void *cl_id
= odhcp6c_get_state(STATE_CLIENT_ID
, &cl_id_len
);
231 void *srv_id
= odhcp6c_get_state(STATE_SERVER_ID
, &srv_id_len
);
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),
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
) {
247 if (e
[i
].priority
> 0)
248 ex_len
= ((e
[i
].priority
- e
[i
].length
- 1) / 8) + 6;
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
,
257 memcpy(ia_pd
+ ia_pd_len
, &p
, sizeof(p
));
258 ia_pd_len
+= sizeof(p
);
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
;
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);
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;
277 struct dhcpv6_ia_prefix pref
= {
278 .type
= htons(DHCPV6_OPT_IA_PREFIX
),
279 .len
= htons(25), .prefix
= request_prefix
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
);
285 hdr_ia_pd
.len
= htons(ntohs(hdr_ia_pd
.len
) + ia_pd_len
);
288 size_t ia_na_entries
, ia_na_len
= 0;
290 e
= odhcp6c_get_state(STATE_IA_NA
, &ia_na_entries
);
291 ia_na_entries
/= sizeof(*e
);
293 struct dhcpv6_ia_hdr hdr_ia_na
= {
294 htons(DHCPV6_OPT_IA_NA
),
295 htons(sizeof(hdr_ia_na
) - 4),
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
;
309 ia_na_len
= sizeof(pa
);
310 hdr_ia_na
.len
= htons(ntohs(hdr_ia_na
.len
) + ia_na_len
);
312 // Reconfigure Accept
316 } reconf_accept
= {htons(DHCPV6_OPT_RECONF_ACCEPT
), 0};
318 // Request Information Refresh
319 uint16_t oro_refresh
= htons(DHCPV6_OPT_INFO_REFRESH
);
323 void *oro
= odhcp6c_get_state(STATE_ORO
, &oro_len
);
327 uint16_t elapsed_type
;
328 uint16_t elapsed_len
;
329 uint16_t elapsed_value
;
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
),
339 struct iovec iov
[] = {
344 {srv_id
, srv_id_len
},
345 {&reconf_accept
, sizeof(reconf_accept
)},
347 {&hdr_ia_na
, sizeof(hdr_ia_na
)},
349 {&hdr_ia_pd
, sizeof(hdr_ia_pd
)},
353 size_t cnt
= ARRAY_SIZE(iov
);
354 if (type
== DHCPV6_MSG_INFO_REQ
) {
356 iov
[2].iov_len
= sizeof(oro_refresh
);
357 hdr
.oro_len
= htons(oro_len
+ sizeof(oro_refresh
));
358 } else if (!request_prefix
) {
362 // Disable IAs if not used
363 if (type
!= DHCPV6_MSG_SOLICIT
) {
371 if (na_mode
== IA_MODE_NONE
)
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};
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;
394 if (sendmsg(sock
, &msg
, 0) < 0) {
395 char in6_str
[INET6_ADDRSTRLEN
];
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
));
404 static int64_t dhcpv6_rand_delay(int64_t time
)
407 odhcp6c_random(&random
, sizeof(random
));
408 return (time
* ((int64_t)random
% 1000LL)) / 10000LL;
412 int dhcpv6_request(enum dhcpv6_msg type
)
415 uint64_t timeout
= UINT32_MAX
;
416 struct dhcpv6_retx
*retx
= &dhcpv6_retx
[type
];
419 struct timespec ts
= {0, 0};
420 ts
.tv_nsec
= dhcpv6_rand_delay(10 * DHCPV6_REQ_DELAY
);
421 nanosleep(&ts
, NULL
);
424 if (type
== DHCPV6_MSG_UNKNOWN
)
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;
434 syslog(LOG_NOTICE
, "Starting %s transaction (timeout %llus, max rc %d)",
435 retx
->name
, (unsigned long long)timeout
, retx
->max_rc
);
437 uint64_t start
= odhcp6c_get_milli_time(), round_start
= start
, elapsed
;
439 // Generate transaction ID
440 uint8_t trid
[3] = {0, 0, 0};
441 if (type
!= DHCPV6_MSG_UNKNOWN
)
442 odhcp6c_random(trid
, sizeof(trid
));
448 int64_t delay
= dhcpv6_rand_delay(retx
->init_timeo
* 1000);
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);
454 rto
= (retx
->init_timeo
* 1000 + delay
);
457 rto
= (2 * rto
+ dhcpv6_rand_delay(rto
));
459 if (retx
->max_timeo
&& (rto
>= retx
->max_timeo
* 1000))
460 rto
= retx
->max_timeo
* 1000 +
461 dhcpv6_rand_delay(retx
->max_timeo
* 1000);
463 // Calculate end for this round and elapsed time
464 uint64_t round_end
= round_start
+ rto
;
465 elapsed
= round_start
- start
;
467 // Don't wait too long
468 if (round_end
- start
> timeout
* 1000)
469 round_end
= timeout
* 1000 + start
;
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);
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
;
489 // Check for pending signal
490 if (odhcp6c_signal_process())
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
));
500 len
= recvmsg(sock
, &msg
, 0);
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
);
513 if (pktinfo
== NULL
) {
518 if (!dhcpv6_response_is_valid(buf
, len
, trid
,
519 type
, &pktinfo
->ipi6_addr
)) {
524 uint8_t *opt
= &buf
[4];
525 uint8_t *opt_end
= opt
+ len
- 4;
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
);
532 if (retx
->handler_reply
)
533 len
= retx
->handler_reply(type
, rc
, opt
, opt_end
);
535 if (len
> 0 && round_end
- round_start
> 1000)
536 round_end
= 1000 + round_start
;
540 if (retx
->handler_finish
)
541 len
= retx
->handler_finish();
542 } while (len
< 0 && ((elapsed
/ 1000 < timeout
) && (!retx
->max_rc
|| rc
< retx
->max_rc
)));
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
)
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
557 if (type
== DHCPV6_MSG_SOLICIT
) {
558 if (rep
->msg_type
!= DHCPV6_MSG_ADVERT
&&
559 rep
->msg_type
!= DHCPV6_MSG_REPLY
)
561 } else if (type
== DHCPV6_MSG_UNKNOWN
) {
562 if (!accept_reconfig
|| rep
->msg_type
!= DHCPV6_MSG_RECONF
)
564 } else if (rep
->msg_type
!= DHCPV6_MSG_REPLY
) {
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;
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
);
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
) {
584 serverid_ok
= (olen
+ 4U == server_id_len
) && !memcmp(
585 &odata
[-4], server_id
, server_id_len
);
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)
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
));
600 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
)
601 secretbytes
[i
] ^= 0x36;
604 md5_append(&md5
, secretbytes
, sizeof(secretbytes
));
605 md5_append(&md5
, buf
, len
);
606 md5_finish(&md5
, hash
);
608 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
) {
609 secretbytes
[i
] ^= 0x36;
610 secretbytes
[i
] ^= 0x5c;
614 md5_append(&md5
, secretbytes
, sizeof(secretbytes
));
615 md5_append(&md5
, hash
, 16);
616 md5_finish(&md5
, hash
);
618 rcauth_ok
= !memcmp(hash
, serverhash
, sizeof(hash
));
619 } else if (otype
== DHCPV6_OPT_RECONF_MESSAGE
&& olen
== 1) {
621 } else if ((otype
== DHCPV6_OPT_IA_PD
|| otype
== DHCPV6_OPT_IA_NA
)) {
623 if (olen
< sizeof(struct dhcpv6_ia_hdr
))
624 options_valid
= false;
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;
636 if (type
== DHCPV6_MSG_INFO_REQ
&& ia_present
)
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
))
646 return clientid_ok
&& serverid_ok
;
650 int dhcpv6_poll_reconfigure(void)
652 int ret
= dhcpv6_request(DHCPV6_MSG_UNKNOWN
);
654 ret
= dhcpv6_request(ret
);
660 static int dhcpv6_handle_reconfigure(_unused
enum dhcpv6_msg orig
, const int rc
,
661 const void *opt
, const void *end
)
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
))
671 dhcpv6_handle_reply(DHCPV6_MSG_UNKNOWN
, rc
, NULL
, NULL
);
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
)
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;
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
));
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]);
702 case DHCPV6_NoPrefixAvail
:
703 // Status code on global level
704 if (pd_mode
== IA_MODE_FORCE
)
706 cand
.preference
-= 2000;
709 case DHCPV6_NoAddrsAvail
:
710 // Status code on global level
711 if (na_mode
== IA_MODE_FORCE
)
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];
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
)
744 if ((!have_na
&& na_mode
== IA_MODE_FORCE
) ||
745 (!have_pd
&& pd_mode
== IA_MODE_FORCE
))
748 if (na_mode
!= IA_MODE_NONE
&& !have_na
) {
749 cand
.has_noaddravail
= true;
750 cand
.preference
-= 1000;
753 if (pd_mode
!= IA_MODE_NONE
) {
755 cand
.preference
+= 2000 + (128 - have_pd
);
757 cand
.preference
-= 2000;
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
));
766 return (rc
> 1 || (pref
== 255 && cand
.preference
> 0)) ? 1 : -1;
770 static int dhcpv6_commit_advert(void)
773 struct dhcpv6_server_cand
*c
= NULL
, *cand
=
774 odhcp6c_get_state(STATE_SERVER_CAND
, &cand_len
);
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
781 if (!c
|| c
->preference
< cand
[i
].preference
)
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
);
792 uint16_t hdr
[2] = {htons(DHCPV6_OPT_SERVERID
),
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
;
799 odhcp6c_add_state(STATE_IA_NA
, c
->ia_na
, c
->ia_na_len
);
801 odhcp6c_add_state(STATE_IA_PD
, c
->ia_pd
, c
->ia_pd_len
);
804 for (size_t i
= 0; i
< cand_len
/ sizeof(*c
); ++i
) {
808 odhcp6c_clear_state(STATE_SERVER_CAND
);
812 else if ((request_prefix
&& c
->ia_pd_len
) || (na_mode
!= IA_MODE_NONE
&& c
->ia_na_len
))
813 return DHCPV6_STATEFUL
;
815 return DHCPV6_STATELESS
;
819 static int dhcpv6_handle_rebind_reply(enum dhcpv6_msg orig
, const int rc
,
820 const void *opt
, const void *end
)
822 dhcpv6_handle_advert(orig
, rc
, opt
, end
);
823 if (dhcpv6_commit_advert() < 0)
826 return dhcpv6_handle_reply(orig
, rc
, opt
, end
);
830 static int dhcpv6_handle_reply(enum dhcpv6_msg orig
, _unused
const int rc
,
831 const void *opt
, const void *end
)
834 uint16_t otype
, olen
;
835 uint32_t refresh
= UINT32_MAX
;
837 bool handled_status_codes
[_DHCPV6_Status_Max
] = { false, };
841 if (orig
== DHCPV6_MSG_UNKNOWN
) {
842 static time_t last_update
= 0;
843 time_t now
= odhcp6c_get_milli_time() / 1000;
845 uint32_t elapsed
= (last_update
> 0) ? now
- last_update
: 0;
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
);
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
);
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]);
885 if (ia_hdr
->iaid
!= 1)
888 uint16_t code
= DHCPV6_Success
;
889 uint16_t stype
, slen
;
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;
898 code
= ((int)sdata
[0]) << 8 | ((int)sdata
[1]);
900 if (code
== DHCPV6_Success
)
903 dhcpv6_handle_ia_status_code(orig
, ia_hdr
,
904 code
, mdata
, mlen
, handled_status_codes
, &ret
);
913 if (code
!= DHCPV6_Success
)
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]);
924 dhcpv6_handle_status_code(orig
, code
, mdata
, mlen
, &ret
);
926 else if (otype
== DHCPV6_OPT_DNS_SERVERS
) {
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
;
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
,
941 else if (slen
> 0 && stype
== NTP_SRV_FQDN
)
942 odhcp6c_add_state(STATE_SNTP_FQDN
,
945 } else if (otype
== DHCPV6_OPT_SIP_SERVER_A
) {
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 &&
957 memcpy(reconf_key
, r
->key
, sizeof(r
->key
));
958 } else if (otype
== DHCPV6_OPT_AFTR_NAME
&& olen
> 3) {
960 odhcp6c_get_state(STATE_AFTR_NAME
, &cur_len
);
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);
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()) {
974 case DHCPV6_MSG_RENEW
:
975 // Send further renews if T1 is not set
979 case DHCPV6_MSG_REBIND
:
980 // Send further rebinds if T1 and T2 is not set
997 static int dhcpv6_parse_ia(void *opt
, void *end
)
999 struct dhcpv6_ia_hdr
*ia_hdr
= (struct dhcpv6_ia_hdr
*)opt
;
1002 uint16_t otype
, olen
;
1005 t1
= ntohl(ia_hdr
->t1
);
1006 t2
= ntohl(ia_hdr
->t2
);
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};
1016 if (otype
== DHCPV6_OPT_IA_PREFIX
) {
1017 struct dhcpv6_ia_prefix
*prefix
= (void*)&odata
[-4];
1018 if (olen
+ 4U < sizeof(*prefix
))
1021 entry
.valid
= ntohl(prefix
->valid
);
1022 entry
.preferred
= ntohl(prefix
->preferred
);
1024 if (entry
.preferred
> entry
.valid
)
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
;
1032 entry
.length
= prefix
->prefix
;
1033 entry
.target
= prefix
->addr
;
1034 uint16_t stype
, slen
;
1037 #ifdef EXT_PREFIX_CLASS
1038 // Find prefix class, if any
1039 dhcpv6_for_each_option(&prefix
[1], odata
+ olen
,
1041 if (stype
== DHCPV6_OPT_PREFIX_CLASS
&& slen
== 2)
1042 entry
.class = sdata
[0] << 8 | sdata
[1];
1047 dhcpv6_for_each_option(odata
+ sizeof(*prefix
) - 4U,
1048 odata
+ olen
, stype
, slen
, sdata
) {
1049 if (stype
!= DHCPV6_OPT_PD_EXCLUDE
|| slen
< 2)
1052 uint8_t elen
= sdata
[0];
1056 if (elen
<= 32 || elen
<= entry
.length
) {
1062 uint8_t bytes
= ((elen
- entry
.length
- 1) / 8) + 1;
1063 if (slen
<= bytes
) {
1068 uint32_t exclude
= 0;
1070 exclude
= exclude
<< 8 | sdata
[bytes
];
1073 exclude
>>= 8 - ((elen
- entry
.length
) % 8);
1074 exclude
<<= 64 - elen
;
1076 // Abusing router & priority fields for exclusion
1077 entry
.router
= entry
.target
;
1078 entry
.router
.s6_addr32
[1] |= htonl(exclude
);
1079 entry
.priority
= elen
;
1083 odhcp6c_update_entry(STATE_IA_PD
, &entry
);
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
))
1094 entry
.preferred
= ntohl(addr
->preferred
);
1095 entry
.valid
= ntohl(addr
->valid
);
1097 if (entry
.preferred
> entry
.valid
)
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
;
1106 entry
.target
= addr
->addr
;
1108 #ifdef EXT_PREFIX_CLASS
1109 uint16_t stype
, slen
;
1111 // Find prefix class, if any
1112 dhcpv6_for_each_option(&addr
[1], odata
+ olen
,
1114 if (stype
== DHCPV6_OPT_PREFIX_CLASS
&& slen
== 2)
1115 entry
.class = sdata
[0] << 8 | sdata
[1];
1118 odhcp6c_update_entry(STATE_IA_NA
, &entry
);
1126 static int dhcpv6_calc_refresh_timers(void)
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;
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
++) {
1141 if (e
[i
].valid
> l_t3
)
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
++) {
1154 if (e
[i
].valid
> l_t3
)
1158 if (ia_pd_entries
|| ia_na_entries
) {
1164 return (int)(ia_pd_entries
+ ia_na_entries
);
1168 static void dhcpv6_log_status_code(const uint16_t code
, const char *scope
,
1169 const void *status_msg
, const int len
)
1171 uint8_t buf
[len
+ 3];
1173 memset(buf
, 0, sizeof(buf
));
1176 memcpy(&buf
[1], status_msg
, len
);
1180 syslog(LOG_WARNING
, "Server returned %s status %i %s",
1185 static void dhcpv6_handle_status_code(const enum dhcpv6_msg orig
,
1186 const uint16_t code
, const void *status_msg
, const int len
,
1189 dhcpv6_log_status_code(code
, "message", status_msg
, len
);
1192 case DHCPV6_UnspecFail
:
1197 case DHCPV6_UseMulticast
:
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
;
1212 case DHCPV6_NoAddrsAvail
:
1213 case DHCPV6_NoPrefixAvail
:
1214 if (orig
== DHCPV6_MSG_REQUEST
)
1215 *ret
= 0; // Failure
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
)
1229 dhcpv6_log_status_code(code
, ia_hdr
->type
== DHCPV6_OPT_IA_NA
?
1230 "IA_NA" : "IA_PD", status_msg
, len
);
1233 case DHCPV6_NoBinding
:
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
);
1246 case DHCPV6_NoAddrsAvail
:
1247 case DHCPV6_NoPrefixAvail
:
1249 case DHCPV6_MSG_REQUEST
:
1258 case DHCPV6_NotOnLink
:
1259 // TODO handle not onlink in case of confirm