]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/dhcp6-option.c
sd-dhcp6-client: Add basic DHCPv6 option handling
[thirdparty/systemd.git] / src / libsystemd-network / dhcp6-option.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright (C) 2014 Intel Corporation. All rights reserved.
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <netinet/in.h>
23 #include <errno.h>
24 #include <string.h>
25
26 #include "sparse-endian.h"
27 #include "util.h"
28
29 #include "dhcp6-internal.h"
30 #include "dhcp6-protocol.h"
31
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
36
37 static int option_append_hdr(uint8_t **buf, size_t *buflen, uint16_t optcode,
38 size_t optlen) {
39 assert_return(buf, -EINVAL);
40 assert_return(*buf, -EINVAL);
41 assert_return(buflen, -EINVAL);
42
43 if (optlen > 0xffff || *buflen < optlen + DHCP6_OPTION_HDR_LEN)
44 return -ENOBUFS;
45
46 (*buf)[0] = optcode >> 8;
47 (*buf)[1] = optcode & 0xff;
48 (*buf)[2] = optlen >> 8;
49 (*buf)[3] = optlen & 0xff;
50
51 *buf += DHCP6_OPTION_HDR_LEN;
52 *buflen -= DHCP6_OPTION_HDR_LEN;
53
54 return 0;
55 }
56
57 int dhcp6_option_append(uint8_t **buf, size_t *buflen, uint16_t code,
58 size_t optlen, const void *optval) {
59 int r;
60
61 assert_return(optval, -EINVAL);
62
63 r = option_append_hdr(buf, buflen, code, optlen);
64 if (r < 0)
65 return r;
66
67 memcpy(*buf, optval, optlen);
68
69 *buf += optlen;
70 *buflen -= optlen;
71
72 return 0;
73 }
74
75 int dhcp6_option_append_ia(uint8_t **buf, size_t *buflen, DHCP6IA *ia) {
76 uint16_t len;
77 uint8_t *ia_hdr;
78 size_t ia_buflen, ia_addrlen = 0;
79 DHCP6Address *addr;
80 int r;
81
82 assert_return(buf && *buf && buflen && ia, -EINVAL);
83
84 switch (ia->type) {
85 case DHCP6_OPTION_IA_NA:
86 len = DHCP6_OPTION_IA_NA_LEN;
87 break;
88
89 case DHCP6_OPTION_IA_TA:
90 len = DHCP6_OPTION_IA_TA_LEN;
91 break;
92
93 default:
94 return -EINVAL;
95 }
96
97 if (*buflen < len)
98 return -ENOBUFS;
99
100 ia_hdr = *buf;
101 ia_buflen = *buflen;
102
103 *buf += DHCP6_OPTION_HDR_LEN;
104 *buflen -= DHCP6_OPTION_HDR_LEN;
105
106 memcpy(*buf, &ia->id, len);
107
108 *buf += len;
109 *buflen -= len;
110
111 LIST_FOREACH(addresses, addr, ia->addresses) {
112 r = option_append_hdr(buf, buflen, DHCP6_OPTION_IAADDR,
113 DHCP6_OPTION_IAADDR_LEN);
114 if (r < 0)
115 return r;
116
117 memcpy(*buf, &addr->address, DHCP6_OPTION_IAADDR_LEN);
118
119 *buf += DHCP6_OPTION_IAADDR_LEN;
120 *buflen -= DHCP6_OPTION_IAADDR_LEN;
121
122 ia_addrlen += DHCP6_OPTION_HDR_LEN + DHCP6_OPTION_IAADDR_LEN;
123 }
124
125 r = option_append_hdr(&ia_hdr, &ia_buflen, ia->type, len + ia_addrlen);
126 if (r < 0)
127 return r;
128
129 return 0;
130 }
131
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);
135
136 if (*buflen == 0)
137 return -ENOMSG;
138
139 *optcode = (*buf)[0] << 8 | (*buf)[1];
140 *optlen = (*buf)[2] << 8 | (*buf)[3];
141
142 if (*optlen > *buflen - 4)
143 return -ENOBUFS;
144
145 *optvalue = &(*buf)[4];
146 *buflen -= (*optlen + 4);
147 (*buf) += (*optlen + 4);
148
149 return 0;
150 }