OpenVPN
dhcp.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2026 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "syshead.h"
28
29#include "dhcp.h"
30#include "socket_util.h"
31#include "error.h"
32
33#include "memdbg.h"
34
35static int
36get_dhcp_message_type(const struct dhcp *dhcp, const int optlen)
37{
38 const uint8_t *p = (uint8_t *)(dhcp + 1);
39 int i;
40
41 for (i = 0; i < optlen; ++i)
42 {
43 const uint8_t type = p[i];
44 const int room = optlen - i;
45 if (type == DHCP_END) /* didn't find what we were looking for */
46 {
47 return -1;
48 }
49 else if (type == DHCP_PAD) /* no-operation */
50 {
51 }
52 else if (type == DHCP_MSG_TYPE) /* what we are looking for */
53 {
54 if (room >= 3)
55 {
56 if (p[i + 1] == 1) /* option length should be 1 */
57 {
58 return p[i + 2]; /* return message type */
59 }
60 }
61 return -1;
62 }
63 else /* some other option */
64 {
65 if (room >= 2)
66 {
67 const int len = p[i + 1]; /* get option length */
68 i += (len + 1); /* advance to next option */
69 }
70 }
71 }
72 return -1;
73}
74
75#if defined(__GNUC__) || defined(__clang__)
76#pragma GCC diagnostic push
77#pragma GCC diagnostic ignored "-Wsign-conversion"
78#endif
79
80static in_addr_t
81do_extract(struct dhcp *dhcp, int optlen)
82{
83 uint8_t *p = (uint8_t *)(dhcp + 1);
84 int i;
85 in_addr_t ret = 0;
86
87 for (i = 0; i < optlen;)
88 {
89 const uint8_t type = p[i];
90 const int room = optlen - i;
91 if (type == DHCP_END)
92 {
93 break;
94 }
95 else if (type == DHCP_PAD)
96 {
97 ++i;
98 }
99 else if (type == DHCP_ROUTER)
100 {
101 if (room >= 2)
102 {
103 const int len = p[i + 1]; /* get option length */
104 if (len <= (room - 2))
105 {
106 /* get router IP address */
107 if (!ret && len >= 4 && (len & 3) == 0)
108 {
109 memcpy(&ret, p + i + 2, 4);
110 ret = ntohl(ret);
111 }
112 {
113 /* delete the router option */
114 uint8_t *dest = p + i;
115 const int owlen = len + 2; /* len of data to overwrite */
116 uint8_t *src = dest + owlen;
117 uint8_t *end = p + optlen;
118 const intptr_t movlen = end - src;
119 if (movlen > 0)
120 {
121 memmove(dest, src, movlen); /* overwrite router option */
122 }
123 memset(end - owlen, DHCP_PAD, owlen); /* pad tail */
124 }
125 }
126 else
127 {
128 break;
129 }
130 }
131 else
132 {
133 break;
134 }
135 }
136 else /* some other option */
137 {
138 if (room >= 2)
139 {
140 const int len = p[i + 1]; /* get option length */
141 i += (len + 2); /* advance to next option */
142 }
143 else
144 {
145 break;
146 }
147 }
148 }
149 return ret;
150}
151
154{
155 struct dhcp_full *df = (struct dhcp_full *)BPTR(ipbuf);
156 const int optlen =
157 BLEN(ipbuf)
158 - (int)(sizeof(struct openvpn_iphdr) + sizeof(struct openvpn_udphdr) + sizeof(struct dhcp));
159
160 if (optlen >= 0 && df->ip.protocol == OPENVPN_IPPROTO_UDP
161 && df->udp.source == htons(BOOTPS_PORT) && df->udp.dest == htons(BOOTPC_PORT)
162 && df->dhcp.op == BOOTREPLY)
163 {
164 const int message_type = get_dhcp_message_type(&df->dhcp, optlen);
165 if (message_type == DHCPACK || message_type == DHCPOFFER)
166 {
167 /* get the router IP address while padding out all DHCP router options */
168 const in_addr_t ret = do_extract(&df->dhcp, optlen);
169
170 /* recompute the UDP checksum */
171 df->udp.check = 0;
172 df->udp.check = htons(ip_checksum(
173 AF_INET, (uint8_t *)&df->udp,
174 sizeof(struct openvpn_udphdr) + sizeof(struct dhcp) + optlen,
175 (uint8_t *)&df->ip.saddr, (uint8_t *)&df->ip.daddr, OPENVPN_IPPROTO_UDP));
176
177 /* only return the extracted Router address if DHCPACK */
178 if (message_type == DHCPACK)
179 {
180 if (ret)
181 {
182 struct gc_arena gc = gc_new();
183 msg(D_ROUTE, "Extracted DHCP router address: %s", print_in_addr_t(ret, 0, &gc));
184 gc_free(&gc);
185 }
186
187 return ret;
188 }
189 }
190 }
191 return 0;
192}
193
194#if defined(__GNUC__) || defined(__clang__)
195#pragma GCC diagnostic pop
196#endif
197
198#if defined(_WIN32) || defined(DHCP_UNIT_TEST)
199
200/*
201 * Convert DHCP options from the command line / config file
202 * into a raw DHCP-format options string.
203 */
204
205static void
206write_dhcp_u8(struct buffer *buf, const uint8_t type, const uint8_t data, bool *error)
207{
208 if (!buf_safe(buf, 3))
209 {
210 *error = true;
211 msg(M_WARN, "write_dhcp_u8: buffer overflow building DHCP options");
212 return;
213 }
214 buf_write_u8(buf, type);
215 buf_write_u8(buf, 1);
216 buf_write_u8(buf, data);
217}
218
219static void
220write_dhcp_u32_array(struct buffer *buf, const uint8_t type, const uint32_t *data,
221 const unsigned int len, bool *error)
222{
223 if (len > 0)
224 {
225 const size_t size = len * sizeof(uint32_t);
226
227 if (!buf_safe(buf, 2 + size))
228 {
229 *error = true;
230 msg(M_WARN, "write_dhcp_u32_array: buffer overflow building DHCP options");
231 return;
232 }
233 if (size < 1 || size > 255)
234 {
235 *error = true;
236 msg(M_WARN, "write_dhcp_u32_array: size (%zu) must be > 0 and <= 255", size);
237 return;
238 }
239 buf_write_u8(buf, type);
240 buf_write_u8(buf, (uint8_t)size);
241 for (unsigned int i = 0; i < len; ++i)
242 {
243 buf_write_u32(buf, data[i]);
244 }
245 }
246}
247
248static void
249write_dhcp_str(struct buffer *buf, const uint8_t type, const char *str, bool *error)
250{
251 const size_t len = strlen(str);
252 if (!buf_safe(buf, 2 + len))
253 {
254 *error = true;
255 msg(M_WARN, "write_dhcp_str: buffer overflow building DHCP options");
256 return;
257 }
258 if (len < 1 || len > 255)
259 {
260 *error = true;
261 msg(M_WARN, "write_dhcp_str: string '%s' must be > 0 bytes and <= 255 bytes", str);
262 return;
263 }
264 buf_write_u8(buf, type);
265 buf_write_u8(buf, (uint8_t)len);
266 buf_write(buf, str, len);
267}
268
269/*
270 * RFC3397 states that multiple searchdomains are encoded as follows:
271 * - at start the length of the entire option is given
272 * - each subdomain is preceded by its length
273 * - each searchdomain is separated by a NUL character
274 * e.g. if you want "openvpn.net" and "duckduckgo.com" then you end up with
275 * 0x1D 0x7 openvpn 0x3 net 0x00 0x0A duckduckgo 0x3 com 0x00
276 */
277static void
278write_dhcp_search_str(struct buffer *buf, const uint8_t type, const char *const *str_array,
279 int array_len, bool *error)
280{
281 char tmp_buf[256];
282 size_t len = 0;
283
284 for (int i = 0; i < array_len; i++)
285 {
286 const char *ptr = str_array[i];
287
288 /* Each entry consumes strlen(ptr) + 2 bytes: one leading label length
289 * byte and one trailing NUL. */
290 if (strlen(ptr) + len + 2 > sizeof(tmp_buf))
291 {
292 *error = true;
293 msg(M_WARN, "write_dhcp_search_str: temp buffer overflow building DHCP options");
294 return;
295 }
296 /* Loop over all subdomains separated by a dot and replace the dot
297 * with the length of the subdomain */
298
299 /* label_length_pos points to the byte to be replaced by the length
300 * of the following domain label */
301 size_t label_length_pos = len++;
302
303 while (true)
304 {
305 if (*ptr == '.' || *ptr == '\0')
306 {
307 /* cast is protected by sizeof(tmp_buf) */
308 tmp_buf[label_length_pos] = (char)(len - label_length_pos - 1);
309 label_length_pos = len;
310 if (*ptr == '\0')
311 {
312 break;
313 }
314 }
315 tmp_buf[len++] = *ptr++;
316 }
317 /* And close off with an extra NUL char */
318 tmp_buf[len++] = 0;
319 }
320
321 if (!buf_safe(buf, 2 + len))
322 {
323 *error = true;
324 msg(M_WARN, "write_search_dhcp_str: buffer overflow building DHCP options");
325 return;
326 }
327 if (len > 255)
328 {
329 *error = true;
330 msg(M_WARN, "write_dhcp_search_str: search domain string must be <= 255 bytes");
331 return;
332 }
333
334 buf_write_u8(buf, type);
335 buf_write_u8(buf, (uint8_t)len);
336 buf_write(buf, tmp_buf, len);
337}
338
339#if defined(__GNUC__) || defined(__clang__)
340#pragma GCC diagnostic push
341#pragma GCC diagnostic ignored "-Wsign-conversion"
342#endif
343
344bool
346{
347 bool error = false;
348 if (o->domain)
349 {
350 write_dhcp_str(buf, DHCP_DOMAIN_NAME, o->domain, &error);
351 }
352
353 if (o->netbios_scope)
354 {
356 }
357
358 if (o->netbios_node_type)
359 {
361 }
362
363 write_dhcp_u32_array(buf, DHCP_DOMAIN_SERVER, (uint32_t *)o->dns, o->dns_len, &error);
364 write_dhcp_u32_array(buf, DHCP_NETBIOS_DOMAIN_SERVER, (uint32_t *)o->wins, o->wins_len, &error);
365 write_dhcp_u32_array(buf, DHCP_NTP_SERVER, (uint32_t *)o->ntp, o->ntp_len, &error);
366 write_dhcp_u32_array(buf, DHCP_NETBIOS_DIST_SERVER, (uint32_t *)o->nbdd, o->nbdd_len, &error);
367
368 if (o->domain_search_list_len > 0)
369 {
371 }
372
373 /* the MS DHCP server option 'Disable Netbios-over-TCP/IP
374 * is implemented as vendor option 001, value 002.
375 * A value of 001 means 'leave NBT alone' which is the default */
376 if (o->disable_nbt)
377 {
378 if (!buf_safe(buf, 8))
379 {
380 msg(M_WARN, "build_dhcp_options_string: buffer overflow building DHCP options");
381 return false;
382 }
384 buf_write_u8(buf, 6); /* total length field */
385 buf_write_u8(buf, 0x001);
386 buf_write_u8(buf, 4); /* length of the vendor specified field */
387 buf_write_u32(buf, 0x002);
388 }
389 return !error;
390}
391
392#if defined(__GNUC__) || defined(__clang__)
393#pragma GCC diagnostic pop
394#endif
395
396#endif /* defined(_WIN32) */
#define BPTR(buf)
Return a pointer to the start of the buffer content.
Definition buffer.h:139
static bool buf_write_u32(struct buffer *dest, uint32_t data)
Append a uint32_t to a buffer in network byte order.
Definition buffer.h:1317
static bool buf_safe(const struct buffer *buf, size_t len)
Check whether len bytes can be appended to a buffer.
Definition buffer.h:994
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Append data to a buffer.
Definition buffer.h:1239
static bool buf_write_u8(struct buffer *dest, uint8_t data)
Append a uint8_t to a buffer.
Definition buffer.h:1288
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:151
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1974
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1958
in_addr_t dhcp_extract_router_msg(struct buffer *ipbuf)
Definition dhcp.c:153
static in_addr_t do_extract(struct dhcp *dhcp, int optlen)
Definition dhcp.c:81
static void write_dhcp_search_str(struct buffer *buf, const uint8_t type, const char *const *str_array, int array_len, bool *error)
Definition dhcp.c:278
static void write_dhcp_u8(struct buffer *buf, const uint8_t type, const uint8_t data, bool *error)
Definition dhcp.c:206
static void write_dhcp_u32_array(struct buffer *buf, const uint8_t type, const uint32_t *data, const unsigned int len, bool *error)
Definition dhcp.c:220
static int get_dhcp_message_type(const struct dhcp *dhcp, const int optlen)
Definition dhcp.c:36
static void write_dhcp_str(struct buffer *buf, const uint8_t type, const char *str, bool *error)
Definition dhcp.c:249
bool build_dhcp_options_string(struct buffer *buf, const struct tuntap_options *o)
Definition dhcp.c:345
#define DHCP_NETBIOS_DOMAIN_SERVER
Definition dhcp.h:39
#define DHCP_DOMAIN_NAME
Definition dhcp.h:36
#define DHCP_PAD
Definition dhcp.h:33
#define DHCP_ROUTER
Definition dhcp.h:34
#define DHCP_MSG_TYPE
Definition dhcp.h:43
#define DHCPACK
Definition dhcp.h:52
#define BOOTPS_PORT
Definition dhcp.h:58
#define BOOTPC_PORT
Definition dhcp.h:59
#define BOOTREPLY
Definition dhcp.h:64
#define DHCP_NETBIOS_DIST_SERVER
Definition dhcp.h:40
#define DHCPOFFER
Definition dhcp.h:49
#define DHCP_END
Definition dhcp.h:45
#define DHCP_VENDOR
Definition dhcp.h:38
#define DHCP_NTP_SERVER
Definition dhcp.h:37
#define DHCP_NETBIOS_SCOPE
Definition dhcp.h:42
#define DHCP_DOMAIN_SEARCH
Definition dhcp.h:44
#define DHCP_NETBIOS_NODE_TYPE
Definition dhcp.h:41
#define DHCP_DOMAIN_SERVER
Definition dhcp.h:35
#define D_ROUTE
Definition errlevel.h:78
#define msg(flags,...)
Definition error.h:152
#define M_WARN
Definition error.h:92
uint16_t ip_checksum(const sa_family_t af, const uint8_t *payload, const int len_payload, const uint8_t *src_addr, const uint8_t *dest_addr, const int proto)
Calculates an IP or IPv6 checksum with a pseudo header as required by TCP, UDP and ICMPv6.
Definition proto.c:119
#define OPENVPN_IPPROTO_UDP
Definition proto.h:106
const char * print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc)
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
struct openvpn_udphdr udp
Definition dhcp.h:86
struct dhcp dhcp
Definition dhcp.h:87
struct openvpn_iphdr ip
Definition dhcp.h:85
Definition dhcp.h:62
uint8_t op
Definition dhcp.h:65
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
uint32_t saddr
Definition proto.h:111
uint32_t daddr
Definition proto.h:112
uint8_t protocol
Definition proto.h:108
uint16_t check
Definition proto.h:159
uint16_t source
Definition proto.h:156
uint16_t dest
Definition proto.h:157
in_addr_t nbdd[N_DHCP_ADDR]
Definition tun.h:123
in_addr_t ntp[N_DHCP_ADDR]
Definition tun.h:119
unsigned int ntp_len
Definition tun.h:120
in_addr_t wins[N_DHCP_ADDR]
Definition tun.h:115
uint8_t netbios_node_type
Definition tun.h:105
unsigned int dns_len
Definition tun.h:112
in_addr_t dns[N_DHCP_ADDR]
Definition tun.h:111
const char * netbios_scope
Definition tun.h:103
unsigned int domain_search_list_len
Definition tun.h:130
const char * domain
Definition tun.h:101
const char * domain_search_list[N_SEARCH_LIST_LEN]
Definition tun.h:129
unsigned int nbdd_len
Definition tun.h:124
bool disable_nbt
Definition tun.h:133
unsigned int wins_len
Definition tun.h:116
uint32_t in_addr_t
Definition syshead.h:52
struct gc_arena gc
Definition test_ssl.c:138