1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright (C) 2014-2015 Intel Corporation. All rights reserved.
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <netinet/in.h>
26 #include "sd-dhcp6-client.h"
28 #include "alloc-util.h"
29 #include "dhcp6-internal.h"
30 #include "dhcp6-protocol.h"
31 #include "dns-domain.h"
32 #include "sparse-endian.h"
34 #include "unaligned.h"
37 #define DHCP6_OPTION_IA_NA_LEN 12
38 #define DHCP6_OPTION_IA_TA_LEN 4
40 typedef struct DHCP6Option
{
44 } _packed_ DHCP6Option
;
46 static int option_append_hdr(uint8_t **buf
, size_t *buflen
, uint16_t optcode
,
48 DHCP6Option
*option
= (DHCP6Option
*) *buf
;
50 assert_return(buf
, -EINVAL
);
51 assert_return(*buf
, -EINVAL
);
52 assert_return(buflen
, -EINVAL
);
54 if (optlen
> 0xffff || *buflen
< optlen
+ sizeof(DHCP6Option
))
57 option
->code
= htobe16(optcode
);
58 option
->len
= htobe16(optlen
);
60 *buf
+= sizeof(DHCP6Option
);
61 *buflen
-= sizeof(DHCP6Option
);
66 int dhcp6_option_append(uint8_t **buf
, size_t *buflen
, uint16_t code
,
67 size_t optlen
, const void *optval
) {
70 assert_return(optval
|| optlen
== 0, -EINVAL
);
72 r
= option_append_hdr(buf
, buflen
, code
, optlen
);
77 memcpy(*buf
, optval
, optlen
);
85 int dhcp6_option_append_ia(uint8_t **buf
, size_t *buflen
, DHCP6IA
*ia
) {
88 size_t ia_buflen
, ia_addrlen
= 0;
92 assert_return(buf
&& *buf
&& buflen
&& ia
, -EINVAL
);
95 case SD_DHCP6_OPTION_IA_NA
:
96 len
= DHCP6_OPTION_IA_NA_LEN
;
99 case SD_DHCP6_OPTION_IA_TA
:
100 len
= DHCP6_OPTION_IA_TA_LEN
;
113 *buf
+= sizeof(DHCP6Option
);
114 *buflen
-= sizeof(DHCP6Option
);
116 memcpy(*buf
, &ia
->id
, len
);
121 LIST_FOREACH(addresses
, addr
, ia
->addresses
) {
122 r
= option_append_hdr(buf
, buflen
, SD_DHCP6_OPTION_IAADDR
,
123 sizeof(addr
->iaaddr
));
127 memcpy(*buf
, &addr
->iaaddr
, sizeof(addr
->iaaddr
));
129 *buf
+= sizeof(addr
->iaaddr
);
130 *buflen
-= sizeof(addr
->iaaddr
);
132 ia_addrlen
+= sizeof(DHCP6Option
) + sizeof(addr
->iaaddr
);
135 r
= option_append_hdr(&ia_hdr
, &ia_buflen
, ia
->type
, len
+ ia_addrlen
);
143 static int option_parse_hdr(uint8_t **buf
, size_t *buflen
, uint16_t *optcode
, size_t *optlen
) {
144 DHCP6Option
*option
= (DHCP6Option
*) *buf
;
147 assert_return(buf
, -EINVAL
);
148 assert_return(optcode
, -EINVAL
);
149 assert_return(optlen
, -EINVAL
);
151 if (*buflen
< sizeof(DHCP6Option
))
154 len
= be16toh(option
->len
);
159 *optcode
= be16toh(option
->code
);
168 int dhcp6_option_parse(uint8_t **buf
, size_t *buflen
, uint16_t *optcode
,
169 size_t *optlen
, uint8_t **optvalue
) {
172 assert_return(buf
&& buflen
&& optcode
&& optlen
&& optvalue
, -EINVAL
);
174 r
= option_parse_hdr(buf
, buflen
, optcode
, optlen
);
178 if (*optlen
> *buflen
)
188 int dhcp6_option_parse_ia(uint8_t **buf
, size_t *buflen
, uint16_t iatype
,
191 uint16_t opt
, status
;
193 size_t iaaddr_offset
;
195 uint32_t lt_t1
, lt_t2
, lt_valid
, lt_pref
, lt_min
= ~0;
197 assert_return(ia
, -EINVAL
);
198 assert_return(!ia
->addresses
, -EINVAL
);
201 case SD_DHCP6_OPTION_IA_NA
:
203 if (*buflen
< DHCP6_OPTION_IA_NA_LEN
+ sizeof(DHCP6Option
) +
204 sizeof(addr
->iaaddr
)) {
209 iaaddr_offset
= DHCP6_OPTION_IA_NA_LEN
;
210 memcpy(&ia
->id
, *buf
, iaaddr_offset
);
212 lt_t1
= be32toh(ia
->lifetime_t1
);
213 lt_t2
= be32toh(ia
->lifetime_t2
);
215 if (lt_t1
&& lt_t2
&& lt_t1
> lt_t2
) {
216 log_dhcp6_client(client
, "IA T1 %ds > T2 %ds",
224 case SD_DHCP6_OPTION_IA_TA
:
225 if (*buflen
< DHCP6_OPTION_IA_TA_LEN
+ sizeof(DHCP6Option
) +
226 sizeof(addr
->iaaddr
)) {
231 iaaddr_offset
= DHCP6_OPTION_IA_TA_LEN
;
232 memcpy(&ia
->id
, *buf
, iaaddr_offset
);
246 *buflen
-= iaaddr_offset
;
247 *buf
+= iaaddr_offset
;
249 while ((r
= option_parse_hdr(buf
, buflen
, &opt
, &optlen
)) >= 0) {
252 case SD_DHCP6_OPTION_IAADDR
:
254 addr
= new0(DHCP6Address
, 1);
260 LIST_INIT(addresses
, addr
);
262 memcpy(&addr
->iaaddr
, *buf
, sizeof(addr
->iaaddr
));
264 lt_valid
= be32toh(addr
->iaaddr
.lifetime_valid
);
265 lt_pref
= be32toh(addr
->iaaddr
.lifetime_valid
);
267 if (!lt_valid
|| lt_pref
> lt_valid
) {
268 log_dhcp6_client(client
, "IA preferred %ds > valid %ds",
272 LIST_PREPEND(addresses
, ia
->addresses
, addr
);
273 if (lt_valid
< lt_min
)
279 case SD_DHCP6_OPTION_STATUS_CODE
:
280 if (optlen
< sizeof(status
))
283 status
= (*buf
)[0] << 8 | (*buf
)[1];
285 log_dhcp6_client(client
, "IA status %d",
294 log_dhcp6_client(client
, "Unknown IA option %d", opt
);
305 if (!ia
->lifetime_t1
&& !ia
->lifetime_t2
) {
307 lt_t2
= lt_min
/ 10 * 8;
308 ia
->lifetime_t1
= htobe32(lt_t1
);
309 ia
->lifetime_t2
= htobe32(lt_t2
);
311 log_dhcp6_client(client
, "Computed IA T1 %ds and T2 %ds as both were zero",
325 int dhcp6_option_parse_ip6addrs(uint8_t *optval
, uint16_t optlen
,
326 struct in6_addr
**addrs
, size_t count
,
329 if (optlen
== 0 || optlen
% sizeof(struct in6_addr
) != 0)
332 if (!GREEDY_REALLOC(*addrs
, *allocated
,
333 count
* sizeof(struct in6_addr
) + optlen
))
336 memcpy(*addrs
+ count
, optval
, optlen
);
338 count
+= optlen
/ sizeof(struct in6_addr
);
343 int dhcp6_option_parse_domainname(const uint8_t *optval
, uint16_t optlen
, char ***str_arr
) {
344 size_t pos
= 0, idx
= 0;
345 _cleanup_free_
char **names
= NULL
;
348 assert_return(optlen
> 1, -ENODATA
);
349 assert_return(optval
[optlen
- 1] == '\0', -EINVAL
);
351 while (pos
< optlen
) {
352 _cleanup_free_
char *ret
= NULL
;
353 size_t n
= 0, allocated
= 0;
368 label
= (const char *)&optval
[pos
];
373 if (!GREEDY_REALLOC(ret
, allocated
, n
+ !first
+ DNS_LABEL_ESCAPED_MAX
)) {
383 r
= dns_label_escape(label
, c
, ret
+ n
, DNS_LABEL_ESCAPED_MAX
);
395 if (!GREEDY_REALLOC(ret
, allocated
, n
+ 1)) {
402 r
= strv_extend(&names
, ret
);