]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/dhcp6-option.c
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
35 #define DHCP6_OPTION_IAADDR_LEN 24
37 static int option_append_hdr(uint8_t **buf
, size_t *buflen
, uint16_t optcode
,
39 assert_return(buf
, -EINVAL
);
40 assert_return(*buf
, -EINVAL
);
41 assert_return(buflen
, -EINVAL
);
43 if (optlen
> 0xffff || *buflen
< optlen
+ DHCP6_OPTION_HDR_LEN
)
46 (*buf
)[0] = optcode
>> 8;
47 (*buf
)[1] = optcode
& 0xff;
48 (*buf
)[2] = optlen
>> 8;
49 (*buf
)[3] = optlen
& 0xff;
51 *buf
+= DHCP6_OPTION_HDR_LEN
;
52 *buflen
-= DHCP6_OPTION_HDR_LEN
;
57 int dhcp6_option_append(uint8_t **buf
, size_t *buflen
, uint16_t code
,
58 size_t optlen
, const void *optval
) {
61 assert_return(optval
, -EINVAL
);
63 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 DHCP6_OPTION_IAADDR_LEN
);
117 memcpy(*buf
, &addr
->address
, DHCP6_OPTION_IAADDR_LEN
);
119 *buf
+= DHCP6_OPTION_IAADDR_LEN
;
120 *buflen
-= DHCP6_OPTION_IAADDR_LEN
;
122 ia_addrlen
+= DHCP6_OPTION_HDR_LEN
+ DHCP6_OPTION_IAADDR_LEN
;
125 r
= option_append_hdr(&ia_hdr
, &ia_buflen
, ia
->type
, len
+ ia_addrlen
);
132 int dhcp6_option_parse(uint8_t **buf
, size_t *buflen
, uint16_t *optcode
,
133 size_t *optlen
, uint8_t **optvalue
) {
134 assert_return(buf
&& buflen
&& optcode
&& optlen
&& optvalue
, -EINVAL
);
139 *optcode
= (*buf
)[0] << 8 | (*buf
)[1];
140 *optlen
= (*buf
)[2] << 8 | (*buf
)[3];
142 if (*optlen
> *buflen
- 4)
145 *optvalue
= &(*buf
)[4];
146 *buflen
-= (*optlen
+ 4);
147 (*buf
) += (*optlen
+ 4);