OpenVPN
control_msg.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 "control_msg.h"
30
31bool
32ctrl_msg_tlv_write_header(struct buffer *buf, uint16_t type, bool optional, uint16_t value_len)
33{
34 /* the type has 15 bits; the top bit is the optional flag */
36
37 /* Write nothing unless the whole TLV fits. The header promises that
38 * value_len bytes follow it, so a header written without room for the
39 * value would leave a partial TLV in buf. */
40 if (!buf_safe(buf, (size_t)CTRL_MSG_TLV_HEADER_LEN + value_len))
41 {
42 return false;
43 }
44 uint16_t field = type;
45 if (optional)
46 {
48 }
49 return buf_write_u16(buf, field) && buf_write_u16(buf, value_len);
50}
51
52bool
53ctrl_msg_tlv_write_u16(struct buffer *buf, uint16_t type, bool optional, uint16_t value)
54{
55 return ctrl_msg_tlv_write_header(buf, type, optional, sizeof(uint16_t))
56 && buf_write_u16(buf, value);
57}
58
59bool
61{
63 {
64 return false; /* fewer than 4 bytes left: truncated header */
65 }
66 /* read from a copy, so that buf is left where it was */
67 struct buffer tmp = *buf;
68 uint16_t field = (uint16_t)buf_read_u16(&tmp);
69 hdr->value_len = (uint16_t)buf_read_u16(&tmp);
70 hdr->type = field & CTRL_MSG_TLV_TYPE_MASK;
71 hdr->optional = (field & CTRL_MSG_TLV_OPTIONAL_FLAG) != 0;
72 return true;
73}
74
75bool
76ctrl_msg_tlv_next(struct buffer *buf, struct ctrl_msg_tlv_header *hdr, struct buffer *value)
77{
78 if (!ctrl_msg_tlv_peek_header(buf, hdr))
79 {
80 return false;
81 }
82 /* Consume nothing unless the whole TLV is there, so that a value shorter
83 * than the header claims leaves buf untouched. */
84 if (BLEN(buf) < CTRL_MSG_TLV_HEADER_LEN + hdr->value_len)
85 {
86 return false;
87 }
89 const uint8_t *v = buf_read_alloc(buf, hdr->value_len);
90 ASSERT(v);
91 buf_set_read(value, v, hdr->value_len);
92 return true;
93}
94
95bool
96ctrl_msg_find_tlv(struct buffer *payload, uint16_t wanted_type, struct buffer *value)
97{
98 bool found = false;
99 while (BLEN(payload) > 0)
100 {
101 struct ctrl_msg_tlv_header hdr;
102 struct buffer v;
103 if (!ctrl_msg_tlv_next(payload, &hdr, &v))
104 {
105 return false;
106 }
107 if (hdr.type == wanted_type)
108 {
109 if (found)
110 {
111 return false; /* the TLV may occur only once */
112 }
113 *value = v;
114 found = true;
115 }
116 else if (!hdr.optional)
117 {
118 return false; /* a mandatory TLV we do not understand, wherever it sits */
119 }
120 }
121 return found;
122}
static bool buf_write_u16(struct buffer *dest, uint16_t data)
Append a uint16_t to a buffer in network byte order.
Definition buffer.h:1302
static int buf_read_u16(struct buffer *buf)
Read and consume a uint16_t from the front of a buffer.
Definition buffer.h:1516
static uint8_t * buf_read_alloc(struct buffer *buf, int size)
Consume bytes from the front of a buffer for reading.
Definition buffer.h:1214
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 void buf_set_read(struct buffer *buf, const uint8_t *data, size_t size)
Initialise a buffer with an externally provided read-only memory region.
Definition buffer.h:687
static bool buf_advance(struct buffer *buf, ssize_t size)
Advance the content start of a buffer, consuming bytes from the front.
Definition buffer.h:1165
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:151
bool ctrl_msg_tlv_write_u16(struct buffer *buf, uint16_t type, bool optional, uint16_t value)
Write a complete TLV whose value is a single 16-bit integer to buf.
Definition control_msg.c:53
bool ctrl_msg_tlv_peek_header(const struct buffer *buf, struct ctrl_msg_tlv_header *hdr)
Read a TLV header from buf without consuming it.
Definition control_msg.c:60
bool ctrl_msg_tlv_write_header(struct buffer *buf, uint16_t type, bool optional, uint16_t value_len)
Write a TLV header (type + optional flag + value length) to buf.
Definition control_msg.c:32
bool ctrl_msg_find_tlv(struct buffer *payload, uint16_t wanted_type, struct buffer *value)
Scan payload for the TLV of type wanted_type, skipping any other TLV type that is marked optional.
Definition control_msg.c:96
bool ctrl_msg_tlv_next(struct buffer *buf, struct ctrl_msg_tlv_header *hdr, struct buffer *value)
Read the next TLV from buf, advancing past its header and value.
Definition control_msg.c:76
TLV framing shared by the TLV-based control messages of the wire protocol.
#define CTRL_MSG_TLV_HEADER_LEN
Definition control_msg.h:48
#define CTRL_MSG_TLV_TYPE_MASK
Definition control_msg.h:52
#define CTRL_MSG_TLV_OPTIONAL_FLAG
Definition control_msg.h:51
#define ASSERT(x)
Definition error.h:221
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
The header every TLV carries: the 15-bit type and optional flag packed into the first 16-bit field,...
Definition control_msg.h:57
bool optional
value of the optional flag
Definition control_msg.h:59
uint16_t value_len
length of the value following the header
Definition control_msg.h:60
uint16_t type
the 15-bit TLV type
Definition control_msg.h:58