]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/dhcp-option.c
Merge pull request #2603 from poettering/drop-compat-libs
[thirdparty/systemd.git] / src / libsystemd-network / dhcp-option.c
1 /***
2 This file is part of systemd.
3
4 Copyright (C) 2013 Intel Corporation. All rights reserved.
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "alloc-util.h"
26 #include "utf8.h"
27
28 #include "dhcp-internal.h"
29
30 static int option_append(uint8_t options[], size_t size, size_t *offset,
31 uint8_t code, size_t optlen, const void *optval) {
32 assert(options);
33 assert(offset);
34
35 if (code != SD_DHCP_OPTION_END)
36 /* always make sure there is space for an END option */
37 size --;
38
39 switch (code) {
40
41 case SD_DHCP_OPTION_PAD:
42 case SD_DHCP_OPTION_END:
43 if (size < *offset + 1)
44 return -ENOBUFS;
45
46 options[*offset] = code;
47 *offset += 1;
48 break;
49
50 default:
51 if (size < *offset + optlen + 2)
52 return -ENOBUFS;
53
54 options[*offset] = code;
55 options[*offset + 1] = optlen;
56
57 memcpy_safe(&options[*offset + 2], optval, optlen);
58 *offset += optlen + 2;
59
60 break;
61 }
62
63 return 0;
64 }
65
66 int dhcp_option_append(DHCPMessage *message, size_t size, size_t *offset,
67 uint8_t overload,
68 uint8_t code, size_t optlen, const void *optval) {
69 size_t file_offset = 0, sname_offset =0;
70 bool file, sname;
71 int r;
72
73 assert(message);
74 assert(offset);
75
76 file = overload & DHCP_OVERLOAD_FILE;
77 sname = overload & DHCP_OVERLOAD_SNAME;
78
79 if (*offset < size) {
80 /* still space in the options array */
81 r = option_append(message->options, size, offset, code, optlen, optval);
82 if (r >= 0)
83 return 0;
84 else if (r == -ENOBUFS && (file || sname)) {
85 /* did not fit, but we have more buffers to try
86 close the options array and move the offset to its end */
87 r = option_append(message->options, size, offset, SD_DHCP_OPTION_END, 0, NULL);
88 if (r < 0)
89 return r;
90
91 *offset = size;
92 } else
93 return r;
94 }
95
96 if (overload & DHCP_OVERLOAD_FILE) {
97 file_offset = *offset - size;
98
99 if (file_offset < sizeof(message->file)) {
100 /* still space in the 'file' array */
101 r = option_append(message->file, sizeof(message->file), &file_offset, code, optlen, optval);
102 if (r >= 0) {
103 *offset = size + file_offset;
104 return 0;
105 } else if (r == -ENOBUFS && sname) {
106 /* did not fit, but we have more buffers to try
107 close the file array and move the offset to its end */
108 r = option_append(message->options, size, offset, SD_DHCP_OPTION_END, 0, NULL);
109 if (r < 0)
110 return r;
111
112 *offset = size + sizeof(message->file);
113 } else
114 return r;
115 }
116 }
117
118 if (overload & DHCP_OVERLOAD_SNAME) {
119 sname_offset = *offset - size - (file ? sizeof(message->file) : 0);
120
121 if (sname_offset < sizeof(message->sname)) {
122 /* still space in the 'sname' array */
123 r = option_append(message->sname, sizeof(message->sname), &sname_offset, code, optlen, optval);
124 if (r >= 0) {
125 *offset = size + (file ? sizeof(message->file) : 0) + sname_offset;
126 return 0;
127 } else {
128 /* no space, or other error, give up */
129 return r;
130 }
131 }
132 }
133
134 return -ENOBUFS;
135 }
136
137 static int parse_options(const uint8_t options[], size_t buflen, uint8_t *overload,
138 uint8_t *message_type, char **error_message, dhcp_option_cb_t cb,
139 void *userdata) {
140 uint8_t code, len;
141 const uint8_t *option;
142 size_t offset = 0;
143
144 while (offset < buflen) {
145 code = options[offset ++];
146
147 switch (code) {
148 case SD_DHCP_OPTION_PAD:
149 continue;
150
151 case SD_DHCP_OPTION_END:
152 return 0;
153 }
154
155 if (buflen < offset + 1)
156 return -ENOBUFS;
157
158 len = options[offset ++];
159
160 if (buflen < offset + len)
161 return -EINVAL;
162
163 option = &options[offset];
164
165 switch (code) {
166 case SD_DHCP_OPTION_MESSAGE_TYPE:
167 if (len != 1)
168 return -EINVAL;
169
170 if (message_type)
171 *message_type = *option;
172
173 break;
174
175 case SD_DHCP_OPTION_ERROR_MESSAGE:
176 if (len == 0)
177 return -EINVAL;
178
179 if (error_message) {
180 _cleanup_free_ char *string = NULL;
181
182 /* Accept a trailing NUL byte */
183 if (memchr(option, 0, len - 1))
184 return -EINVAL;
185
186 string = strndup((const char *) option, len);
187 if (!string)
188 return -ENOMEM;
189
190 if (!ascii_is_valid(string))
191 return -EINVAL;
192
193 free(*error_message);
194 *error_message = string;
195 string = NULL;
196 }
197
198 break;
199 case SD_DHCP_OPTION_OVERLOAD:
200 if (len != 1)
201 return -EINVAL;
202
203 if (overload)
204 *overload = *option;
205
206 break;
207
208 default:
209 if (cb)
210 cb(code, len, option, userdata);
211
212 break;
213 }
214
215 offset += len;
216 }
217
218 if (offset < buflen)
219 return -EINVAL;
220
221 return 0;
222 }
223
224 int dhcp_option_parse(DHCPMessage *message, size_t len, dhcp_option_cb_t cb, void *userdata, char **_error_message) {
225 _cleanup_free_ char *error_message = NULL;
226 uint8_t overload = 0;
227 uint8_t message_type = 0;
228 int r;
229
230 if (!message)
231 return -EINVAL;
232
233 if (len < sizeof(DHCPMessage))
234 return -EINVAL;
235
236 len -= sizeof(DHCPMessage);
237
238 r = parse_options(message->options, len, &overload, &message_type, &error_message, cb, userdata);
239 if (r < 0)
240 return r;
241
242 if (overload & DHCP_OVERLOAD_FILE) {
243 r = parse_options(message->file, sizeof(message->file), NULL, &message_type, &error_message, cb, userdata);
244 if (r < 0)
245 return r;
246 }
247
248 if (overload & DHCP_OVERLOAD_SNAME) {
249 r = parse_options(message->sname, sizeof(message->sname), NULL, &message_type, &error_message, cb, userdata);
250 if (r < 0)
251 return r;
252 }
253
254 if (message_type == 0)
255 return -ENOMSG;
256
257 if (_error_message && IN_SET(message_type, DHCP_NAK, DHCP_DECLINE)) {
258 *_error_message = error_message;
259 error_message = NULL;
260 }
261
262 return message_type;
263 }