chiark / gitweb /
networkd: send hostname to dhcp server
[elogind.git] / src / libsystemd-network / dhcp-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) 2013 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 <stdint.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <stdio.h>
26
27 #include "dhcp-internal.h"
28
29 static int option_append(uint8_t options[], size_t size, size_t *offset,
30                          uint8_t code, size_t optlen, const void *optval) {
31         assert(options);
32         assert(offset);
33
34         if (code != DHCP_OPTION_END)
35                 /* always make sure there is space for an END option */
36                 size --;
37
38         switch (code) {
39
40         case DHCP_OPTION_PAD:
41         case DHCP_OPTION_END:
42                 if (size < *offset + 1)
43                         return -ENOBUFS;
44
45                 options[*offset] = code;
46                 *offset += 1;
47                 break;
48
49         default:
50                 if (size < *offset + optlen + 2)
51                         return -ENOBUFS;
52
53                 options[*offset] = code;
54                 options[*offset + 1] = optlen;
55
56                 if (optlen) {
57                         assert(optval);
58
59                         memcpy(&options[*offset + 2], optval, optlen);
60                 }
61
62                 *offset += optlen + 2;
63
64                 break;
65         }
66
67         return 0;
68 }
69
70 int dhcp_option_append(DHCPMessage *message, size_t size, size_t *offset,
71                        uint8_t overload,
72                        uint8_t code, size_t optlen, const void *optval) {
73         size_t file_offset = 0, sname_offset =0;
74         bool file, sname;
75         int r;
76
77         assert(message);
78         assert(offset);
79
80         file = overload & DHCP_OVERLOAD_FILE;
81         sname = overload & DHCP_OVERLOAD_SNAME;
82
83         if (*offset < size) {
84                 /* still space in the options array */
85                 r = option_append(message->options, size, offset, code, optlen, optval);
86                 if (r >= 0)
87                         return 0;
88                 else if (r == -ENOBUFS && (file || sname)) {
89                         /* did not fit, but we have more buffers to try
90                            close the options array and move the offset to its end */
91                         r = option_append(message->options, size, offset, DHCP_OPTION_END, 0, NULL);
92                         if (r < 0)
93                                 return r;
94
95                         *offset = size;
96                 } else
97                         return r;
98         }
99
100         if (overload & DHCP_OVERLOAD_FILE) {
101                 file_offset = *offset - size;
102
103                 if (file_offset < sizeof(message->file)) {
104                         /* still space in the 'file' array */
105                         r = option_append(message->file, sizeof(message->file), &file_offset, code, optlen, optval);
106                         if (r >= 0) {
107                                 *offset = size + file_offset;
108                                 return 0;
109                         } else if (r == -ENOBUFS && sname) {
110                                 /* did not fit, but we have more buffers to try
111                                    close the file array and move the offset to its end */
112                                 r = option_append(message->options, size, offset, DHCP_OPTION_END, 0, NULL);
113                                 if (r < 0)
114                                         return r;
115
116                                 *offset = size + sizeof(message->file);
117                         } else
118                                 return r;
119                 }
120         }
121
122         if (overload & DHCP_OVERLOAD_SNAME) {
123                 sname_offset = *offset - size - (file ? sizeof(message->file) : 0);
124
125                 if (sname_offset < sizeof(message->sname)) {
126                         /* still space in the 'sname' array */
127                         r = option_append(message->sname, sizeof(message->sname), &sname_offset, code, optlen, optval);
128                         if (r >= 0) {
129                                 *offset = size + (file ? sizeof(message->file) : 0) + sname_offset;
130                                 return 0;
131                         } else {
132                                 /* no space, or other error, give up */
133                                 return r;
134                         }
135                 }
136         }
137
138         return -ENOBUFS;
139 }
140
141 static int parse_options(const uint8_t options[], size_t buflen, uint8_t *overload,
142                          uint8_t *message_type, dhcp_option_cb_t cb,
143                          void *user_data) {
144         uint8_t code, len;
145         size_t offset = 0;
146
147         while (offset < buflen) {
148                 switch (options[offset]) {
149                 case DHCP_OPTION_PAD:
150                         offset++;
151
152                         break;
153
154                 case DHCP_OPTION_END:
155                         return 0;
156
157                 case DHCP_OPTION_MESSAGE_TYPE:
158                         if (buflen < offset + 3)
159                                 return -ENOBUFS;
160
161                         len = options[++offset];
162                         if (len != 1)
163                                 return -EINVAL;
164
165                         if (message_type)
166                                 *message_type = options[++offset];
167                         else
168                                 offset++;
169
170                         offset++;
171
172                         break;
173
174                 case DHCP_OPTION_OVERLOAD:
175                         if (buflen < offset + 3)
176                                 return -ENOBUFS;
177
178                         len = options[++offset];
179                         if (len != 1)
180                                 return -EINVAL;
181
182                         if (overload)
183                                 *overload = options[++offset];
184                         else
185                                 offset++;
186
187                         offset++;
188
189                         break;
190
191                 default:
192                         if (buflen < offset + 3)
193                                 return -ENOBUFS;
194
195                         code = options[offset];
196                         len = options[++offset];
197
198                         if (buflen < ++offset + len)
199                                 return -EINVAL;
200
201                         if (cb)
202                                 cb(code, len, &options[offset], user_data);
203
204                         offset += len;
205
206                         break;
207                 }
208         }
209
210         if (offset < buflen)
211                 return -EINVAL;
212
213         return 0;
214 }
215
216 int dhcp_option_parse(DHCPMessage *message, size_t len,
217                       dhcp_option_cb_t cb, void *user_data) {
218         uint8_t overload = 0;
219         uint8_t message_type = 0;
220         int r;
221
222         if (!message)
223                 return -EINVAL;
224
225         if (len < sizeof(DHCPMessage))
226                 return -EINVAL;
227
228         len -= sizeof(DHCPMessage);
229
230         r = parse_options(message->options, len, &overload, &message_type,
231                           cb, user_data);
232         if (r < 0)
233                 return r;
234
235         if (overload & DHCP_OVERLOAD_FILE) {
236                 r = parse_options(message->file, sizeof(message->file),
237                                 NULL, &message_type, cb, user_data);
238                 if (r < 0)
239                         return r;
240         }
241
242         if (overload & DHCP_OVERLOAD_SNAME) {
243                 r = parse_options(message->sname, sizeof(message->sname),
244                                 NULL, &message_type, cb, user_data);
245                 if (r < 0)
246                         return r;
247         }
248
249         if (message_type)
250                 return message_type;
251
252         return -ENOMSG;
253 }