OpenVPN
control_msg.h File Reference

TLV framing shared by the TLV-based control messages of the wire protocol. More...

#include "buffer.h"
Include dependency graph for control_msg.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ctrl_msg_tlv_header
 The header every TLV carries: the 15-bit type and optional flag packed into the first 16-bit field, then the length of the value that follows. More...

Macros

#define TLV_TYPE_EARLY_NEG_FLAGS   0x0001 /* early negotiation, in the reset packets */
#define TLV_TYPE_PROBE_REQUEST   0x0002 /* a client probing a server, in P_CONTROL_V2 */
#define TLV_TYPE_PROBE_REPLY   0x0003 /* the server's answer, in P_CONTROL_V2 */
#define CTRL_MSG_TLV_HEADER_LEN   4
#define CTRL_MSG_TLV_OPTIONAL_FLAG   0x8000
#define CTRL_MSG_TLV_TYPE_MASK   0x7fff

Functions

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.
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.
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.
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.
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.

Detailed Description

TLV framing shared by the TLV-based control messages of the wire protocol.

Such a message payload is a sequence of TLV entries. Each TLV starts with a 4-byte header: a 16-bit field whose most significant bit is the "optional" flag and whose remaining 15 bits are the type, followed by a 16-bit length giving the size of the value that follows the header.

All of these messages share one TLV type space, so the TLV types are all defined here. Only the framing is handled here: what a message does with a TLV type it does not know, marked optional or not, is up to its parser.

Definition in file control_msg.h.

Macro Definition Documentation

◆ CTRL_MSG_TLV_HEADER_LEN

◆ CTRL_MSG_TLV_OPTIONAL_FLAG

#define CTRL_MSG_TLV_OPTIONAL_FLAG   0x8000

Definition at line 51 of file control_msg.h.

Referenced by ctrl_msg_tlv_peek_header(), and ctrl_msg_tlv_write_header().

◆ CTRL_MSG_TLV_TYPE_MASK

#define CTRL_MSG_TLV_TYPE_MASK   0x7fff

Definition at line 52 of file control_msg.h.

Referenced by ctrl_msg_tlv_peek_header(), and ctrl_msg_tlv_write_header().

◆ TLV_TYPE_EARLY_NEG_FLAGS

#define TLV_TYPE_EARLY_NEG_FLAGS   0x0001 /* early negotiation, in the reset packets */

◆ TLV_TYPE_PROBE_REPLY

#define TLV_TYPE_PROBE_REPLY   0x0003 /* the server's answer, in P_CONTROL_V2 */

◆ TLV_TYPE_PROBE_REQUEST

Function Documentation

◆ ctrl_msg_find_tlv()

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.

This is for messages that carry exactly one mandatory TLV, which is what the probe messages are. Any other TLV that is not marked optional invalidates the message wherever it appears, as does a second TLV of wanted_type, so the whole sequence is walked even once the wanted TLV has been found. A message carrying several mandatory TLVs walks them with ctrl_msg_tlv_next() instead.

On success value covers exactly the found TLV's value bytes; as in ctrl_msg_tlv_next(), a header claiming more bytes than payload holds is rejected rather than reported as found. payload is consumed as it is read.

Parameters
payloadbuffer positioned at a TLV header
wanted_typethe TLV type to look for
valueset to a buffer covering the found TLV's value; it points into payload and owns no storage
Returns
true if the TLV was found, false if it is absent or present more than once, if a TLV header or value is malformed or truncated, or if a TLV we do not understand is not marked optional.

Definition at line 96 of file control_msg.c.

References BLEN, ctrl_msg_tlv_next(), ctrl_msg_tlv_header::optional, and ctrl_msg_tlv_header::type.

Referenced by test_find_tlv_empty_payload(), test_find_tlv_rejects_duplicate(), test_find_tlv_rejects_unknown_mandatory(), test_find_tlv_skips_optional_after_wanted(), test_find_tlv_trailing_bytes(), test_find_tlv_value_truncated(), test_probe_reply_flags_contradict(), test_probe_reply_roundtrip(), test_probe_request_forward_compat(), and test_probe_request_roundtrip().

◆ ctrl_msg_tlv_next()

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.

Call it while BLEN(buf) > 0 to walk a whole payload.

The length from the header is validated against buf, so on success the whole value is present: a header claiming more bytes than buf holds is rejected. Nothing is consumed unless the whole TLV is there, so a failed read never leaves a partial TLV behind.

Parameters
bufbuffer positioned at a TLV header
hdrfilled with the TLV's type, optional flag and value length
valueset to a buffer covering exactly the TLV's value; it points into buf and owns no storage
Returns
true on success, false if the header or the value is truncated, with buf left untouched.

Definition at line 76 of file control_msg.c.

References ASSERT, BLEN, buf_advance(), buf_read_alloc(), buf_set_read(), CTRL_MSG_TLV_HEADER_LEN, ctrl_msg_tlv_peek_header(), and ctrl_msg_tlv_header::value_len.

Referenced by ctrl_msg_find_tlv(), parse_early_negotiation_tlvs(), test_generate_reset_packet_resend_wkc(), test_tlv_header_truncated(), test_tlv_next_value_truncated(), and test_tlv_next_walks_payload().

◆ ctrl_msg_tlv_peek_header()

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.

Parameters
bufbuffer positioned at the TLV header
hdrfilled with the type, optional flag and value length on success
Returns
true on success, false if there are not enough bytes for a header.

Definition at line 60 of file control_msg.c.

References BLEN, buf_read_u16(), CTRL_MSG_TLV_HEADER_LEN, CTRL_MSG_TLV_OPTIONAL_FLAG, CTRL_MSG_TLV_TYPE_MASK, ctrl_msg_tlv_header::optional, ctrl_msg_tlv_header::type, and ctrl_msg_tlv_header::value_len.

Referenced by ctrl_msg_tlv_next(), test_probe_reply_roundtrip(), test_probe_request_roundtrip(), test_tlv_header_roundtrip(), and test_tlv_header_truncated().

◆ ctrl_msg_tlv_write_header()

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.

type must fit in the 15 bits of the type field.

Nothing is written unless the header and the value_len bytes of value that follow it both fit, so a failed write never leaves a partial TLV behind.

Returns
true on success, false if buf has insufficient space.

Definition at line 32 of file control_msg.c.

References ASSERT, buf_safe(), buf_write_u16(), CTRL_MSG_TLV_HEADER_LEN, CTRL_MSG_TLV_OPTIONAL_FLAG, and CTRL_MSG_TLV_TYPE_MASK.

Referenced by ctrl_msg_tlv_write_u16(), probe_reply_write(), probe_request_write(), test_find_tlv_rejects_duplicate(), test_find_tlv_rejects_unknown_mandatory(), test_find_tlv_skips_optional_after_wanted(), test_find_tlv_value_truncated(), test_probe_request_forward_compat(), test_tlv_header_roundtrip(), test_tlv_next_value_truncated(), test_tlv_next_walks_payload(), and test_tlv_write_no_room().

◆ ctrl_msg_tlv_write_u16()

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.

Returns
true on success, false if buf has insufficient space.

Definition at line 53 of file control_msg.c.

References buf_write_u16(), and ctrl_msg_tlv_write_header().

Referenced by test_tlv_next_walks_payload(), test_tlv_write_no_room(), test_tlv_write_u16_wire_format(), and tls_reset_standalone().