1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright (C) 2014 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/>.
22 #include <netinet/in.h>
26 #include "sparse-endian.h"
29 #include "dhcp6-internal.h"
30 #include "dhcp6-protocol.h"
32 #define DHCP6_OPTION_HDR_LEN 4
33 #define DHCP6_OPTION_IA_NA_LEN 12
34 #define DHCP6_OPTION_IA_TA_LEN 4
36 static int option_append_hdr(uint8_t **buf, size_t *buflen, uint16_t optcode,
38 assert_return(buf, -EINVAL);
39 assert_return(*buf, -EINVAL);
40 assert_return(buflen, -EINVAL);
42 if (optlen > 0xffff || *buflen < optlen + DHCP6_OPTION_HDR_LEN)
45 (*buf)[0] = optcode >> 8;
46 (*buf)[1] = optcode & 0xff;
47 (*buf)[2] = optlen >> 8;
48 (*buf)[3] = optlen & 0xff;
50 *buf += DHCP6_OPTION_HDR_LEN;
51 *buflen -= DHCP6_OPTION_HDR_LEN;
56 int dhcp6_option_append(uint8_t **buf, size_t *buflen, uint16_t code,
57 size_t optlen, const void *optval) {
60 assert_return(optval || optlen == 0, -EINVAL);
62 r = option_append_hdr(buf, buflen, code, optlen);
67 memcpy(*buf, optval, optlen);
75 int dhcp6_option_append_ia(uint8_t **buf, size_t *buflen, DHCP6IA *ia) {
78 size_t ia_buflen, ia_addrlen = 0;
82 assert_return(buf && *buf && buflen && ia, -EINVAL);
85 case DHCP6_OPTION_IA_NA:
86 len = DHCP6_OPTION_IA_NA_LEN;
89 case DHCP6_OPTION_IA_TA:
90 len = DHCP6_OPTION_IA_TA_LEN;
103 *buf += DHCP6_OPTION_HDR_LEN;
104 *buflen -= DHCP6_OPTION_HDR_LEN;
106 memcpy(*buf, &ia->id, len);
111 LIST_FOREACH(addresses, addr, ia->addresses) {
112 r = option_append_hdr(buf, buflen, DHCP6_OPTION_IAADDR,
113 sizeof(addr->iaaddr));
117 memcpy(*buf, &addr->iaaddr, sizeof(addr->iaaddr));
119 *buf += sizeof(addr->iaaddr);
120 *buflen -= sizeof(addr->iaaddr);
122 ia_addrlen += DHCP6_OPTION_HDR_LEN + sizeof(addr->iaaddr);
125 r = option_append_hdr(&ia_hdr, &ia_buflen, ia->type, len + ia_addrlen);
133 static int option_parse_hdr(uint8_t **buf, size_t *buflen, uint16_t *opt,
137 assert_return(buf, -EINVAL);
138 assert_return(opt, -EINVAL);
139 assert_return(optlen, -EINVAL);
144 len = (*buf)[2] << 8 | (*buf)[3];
149 *opt = (*buf)[0] << 8 | (*buf)[1];
158 int dhcp6_option_parse(uint8_t **buf, size_t *buflen, uint16_t *optcode,
159 size_t *optlen, uint8_t **optvalue) {
162 assert_return(buf && buflen && optcode && optlen && optvalue, -EINVAL);
164 r = option_parse_hdr(buf, buflen, optcode, optlen);
168 if (*optlen > *buflen)
178 int dhcp6_option_parse_ia(uint8_t **buf, size_t *buflen, uint16_t iatype,
181 uint16_t opt, status;
183 size_t iaaddr_offset;
185 uint32_t lt_t1, lt_t2, lt_valid, lt_pref, lt_min = ~0;
187 assert_return(ia, -EINVAL);
188 assert_return(!ia->addresses, -EINVAL);
191 case DHCP6_OPTION_IA_NA:
193 if (*buflen < DHCP6_OPTION_IA_NA_LEN + DHCP6_OPTION_HDR_LEN +
194 sizeof(addr->iaaddr)) {
199 iaaddr_offset = DHCP6_OPTION_IA_NA_LEN;
200 memcpy(&ia->id, *buf, iaaddr_offset);
202 lt_t1 = be32toh(ia->lifetime_t1);
203 lt_t2 = be32toh(ia->lifetime_t2);
205 if (lt_t1 && lt_t2 && lt_t1 > lt_t2) {
206 log_dhcp6_client(client, "IA T1 %ds > T2 %ds",
214 case DHCP6_OPTION_IA_TA:
215 if (*buflen < DHCP6_OPTION_IA_TA_LEN + DHCP6_OPTION_HDR_LEN +
216 sizeof(addr->iaaddr)) {
221 iaaddr_offset = DHCP6_OPTION_IA_TA_LEN;
222 memcpy(&ia->id, *buf, iaaddr_offset);
236 *buflen -= iaaddr_offset;
237 *buf += iaaddr_offset;
239 while ((r = option_parse_hdr(buf, buflen, &opt, &optlen)) >= 0) {
242 case DHCP6_OPTION_IAADDR:
244 addr = new0(DHCP6Address, 1);
250 LIST_INIT(addresses, addr);
252 memcpy(&addr->iaaddr, *buf, sizeof(addr->iaaddr));
254 lt_valid = be32toh(addr->iaaddr.lifetime_valid);
255 lt_pref = be32toh(addr->iaaddr.lifetime_valid);
257 if (!lt_valid || lt_pref > lt_valid) {
258 log_dhcp6_client(client, "IA preferred %ds > valid %ds",
262 LIST_PREPEND(addresses, ia->addresses, addr);
263 if (lt_valid < lt_min)
269 case DHCP6_OPTION_STATUS_CODE:
270 if (optlen < sizeof(status))
273 status = (*buf)[0] << 8 | (*buf)[1];
275 log_dhcp6_client(client, "IA status %d",
284 log_dhcp6_client(client, "Unknown IA option %d", opt);
295 if (!ia->lifetime_t1 && !ia->lifetime_t2) {
297 lt_t2 = lt_min / 10 * 8;
298 ia->lifetime_t1 = htobe32(lt_t1);
299 ia->lifetime_t2 = htobe32(lt_t2);
301 log_dhcp6_client(client, "Computed IA T1 %ds and T2 %ds as both were zero",