OpenVPN
mroute.h
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-2025 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
24#ifndef MROUTE_H
25#define MROUTE_H
26
27#include "buffer.h"
28#include "list.h"
29#include "route.h"
30
31#include <stddef.h>
32
33#define IP_MCAST_SUBNET_MASK ((in_addr_t)240 << 24)
34#define IP_MCAST_NETWORK ((in_addr_t)224 << 24)
35
36/* Return status values for mroute_extract_addr_from_packet */
37
38#define MROUTE_EXTRACT_SUCCEEDED (1 << 0)
39#define MROUTE_EXTRACT_BCAST (1 << 1)
40#define MROUTE_EXTRACT_MCAST (1 << 2)
41#define MROUTE_EXTRACT_IGMP (1 << 3)
42
43#define MROUTE_SEC_EXTRACT_SUCCEEDED (1 << (0 + MROUTE_SEC_SHIFT))
44#define MROUTE_SEC_EXTRACT_BCAST (1 << (1 + MROUTE_SEC_SHIFT))
45#define MROUTE_SEC_EXTRACT_MCAST (1 << (2 + MROUTE_SEC_SHIFT))
46#define MROUTE_SEC_EXTRACT_IGMP (1 << (3 + MROUTE_SEC_SHIFT))
47
48#define MROUTE_SEC_SHIFT 4
49
50/*
51 * Choose the largest address possible with
52 * any of our supported types, which is IPv6
53 * with port number.
54 */
55#define MR_MAX_ADDR_LEN 20
56
57/*
58 * Address Types
59 */
60#define MR_ADDR_NONE 0
61#define MR_ADDR_ETHER 1
62#define MR_ADDR_IPV4 2
63#define MR_ADDR_IPV6 3
64#define MR_ADDR_MASK 3
65
66/* Address type mask indicating that port # is part of address */
67#define MR_WITH_PORT 4
68
69/* Address type mask indicating that netbits is part of address */
70#define MR_WITH_NETBITS 8
71
72/* Indicates than IPv4 addr was extracted from ARP packet */
73#define MR_ARP 16
74
75/* Address type mask indicating that proto # is part of address */
76#define MR_WITH_PROTO 32
77
78/* MRoute is an on link/scope address needed for DCO on Unix platforms */
79#define MR_ONLINK_DCO_ADDR 64
80
82{
83 uint8_t len; /* length of address */
85 uint8_t type; /* MR_ADDR/MR_WITH flags */
86 uint8_t netbits; /* number of bits in network part of address,
87 * valid if MR_WITH_NETBITS is set */
88 union
89 {
90 uint8_t raw_addr[MR_MAX_ADDR_LEN]; /* actual address */
91 struct
92 {
96 struct
97 {
98 in_addr_t addr; /* _network order_ IPv4 address */
99 in_port_t port; /* _network order_ TCP/UDP port */
100 } v4;
101 struct
102 {
104 in_port_t port; /* _network order_ TCP/UDP port */
105 } v6;
106 struct
107 {
109 in_addr_t addr; /* _network order_ IPv4 address */
111 };
112};
113
114/* Double-check that struct packing works as expected */
115static_assert(offsetof(struct mroute_addr, v4.port) == offsetof(struct mroute_addr, v4) + 4,
116 "Unexpected struct packing of v4");
117static_assert(offsetof(struct mroute_addr, v6.port) == offsetof(struct mroute_addr, v6) + 16,
118 "Unexpected struct packing of v6");
119static_assert(offsetof(struct mroute_addr, v4mappedv6.addr)
120 == offsetof(struct mroute_addr, v4mappedv6) + 12,
121 "Unexpected struct packing of v4mappedv6");
122
123/*
124 * Number of bits in an address. Should be raised for IPv6.
125 */
126#define MR_HELPER_NET_LEN 129
127
128/*
129 * Used to help maintain CIDR routing table.
130 */
132{
133 unsigned int cache_generation; /* incremented when route added */
134 int ageable_ttl_secs; /* host route cache entry time-to-live*/
135 int n_net_len; /* length of net_len array */
136 uint8_t net_len[MR_HELPER_NET_LEN]; /* CIDR netlengths in descending order */
137 int net_len_refcount[MR_HELPER_NET_LEN]; /* refcount of each netlength */
138};
139
140struct openvpn_sockaddr;
141
143 const struct openvpn_sockaddr *osaddr, bool use_port);
144
145bool mroute_learnable_address(const struct mroute_addr *addr, struct gc_arena *gc);
146
147uint32_t mroute_addr_hash_function(const void *key, uint32_t iv);
148
149bool mroute_addr_compare_function(const void *key1, const void *key2);
150
151void mroute_addr_init(struct mroute_addr *addr);
152
153const char *mroute_addr_print(const struct mroute_addr *ma, struct gc_arena *gc);
154
155#define MAPF_SUBNET (1 << 0)
156#define MAPF_IA_EMPTY_IF_UNDEF (1 << 1)
157#define MAPF_SHOW_ARP (1 << 2)
158#define MAPF_SHOW_FAMILY (1 << 3)
159const char *mroute_addr_print_ex(const struct mroute_addr *ma, const unsigned int flags,
160 struct gc_arena *gc);
161
163
165
166void mroute_helper_free(struct mroute_helper *mh);
167
168void mroute_helper_add_iroute46(struct mroute_helper *mh, int netbits);
169
170void mroute_helper_del_iroute46(struct mroute_helper *mh, int netbits);
171
172unsigned int mroute_extract_addr_ip(struct mroute_addr *src, struct mroute_addr *dest,
173 const struct buffer *buf);
174
175unsigned int mroute_extract_addr_ether(struct mroute_addr *src, struct mroute_addr *dest,
176 uint16_t vid, const struct buffer *buf);
177
178/*
179 * Given a raw packet in buf, return the src and dest
180 * addresses of the packet.
181 */
182static inline unsigned int
183mroute_extract_addr_from_packet(struct mroute_addr *src, struct mroute_addr *dest, uint16_t vid,
184 const struct buffer *buf, int tunnel_type)
185{
186 unsigned int ret = 0;
187 verify_align_4(buf);
188
189 /*
190 * Since we don't really need the protocol on vaddresses for internal VPN
191 * payload packets, make sure we have the same value to avoid hashing insert
192 * and search issues.
193 */
194 src->proto = 0;
195 dest->proto = src->proto;
196
197 if (tunnel_type == DEV_TYPE_TUN)
198 {
199 ret = mroute_extract_addr_ip(src, dest, buf);
200 }
201 else if (tunnel_type == DEV_TYPE_TAP)
202 {
203 ret = mroute_extract_addr_ether(src, dest, vid, buf);
204 }
205 return ret;
206}
207
208static inline bool
209mroute_addr_equal(const struct mroute_addr *a1, const struct mroute_addr *a2)
210{
211 if (a1->type != a2->type)
212 {
213 return false;
214 }
215 if (a1->proto != a2->proto)
216 {
217 return false;
218 }
219 if (a1->netbits != a2->netbits)
220 {
221 return false;
222 }
223 if (a1->len != a2->len)
224 {
225 return false;
226 }
227 return memcmp(a1->raw_addr, a2->raw_addr, a1->len) == 0;
228}
229
230static inline const uint8_t *
232{
233 /* NOTE: depends on ordering of struct mroute_addr */
234 return (uint8_t *)&a->proto;
235}
236
237static inline uint32_t
239{
240 return (uint32_t)a->len + 3;
241}
242
243static inline void
244mroute_extract_in_addr_t(struct mroute_addr *dest, const in_addr_t src)
245{
246 dest->type = MR_ADDR_IPV4;
247 dest->netbits = 0;
248 dest->len = 4;
249 dest->v4.addr = htonl(src);
250}
251
252static inline in_addr_t
254{
255 if ((addr->type & MR_ADDR_MASK) == MR_ADDR_IPV4 && addr->netbits == 0 && addr->len == 4)
256 {
257 return ntohl(addr->v4.addr);
258 }
259 else
260 {
261 return 0;
262 }
263}
264
265static inline void
267{
268 ma->len = 0;
269 ma->type = MR_ADDR_NONE;
270}
271
272#endif /* MROUTE_H */
#define verify_align_4(ptr)
Definition buffer.h:983
static const char *const key1
Definition cert_data.h:55
static uint32_t mroute_addr_hash_len(const struct mroute_addr *a)
Definition mroute.h:238
#define MR_MAX_ADDR_LEN
Definition mroute.h:55
void mroute_addr_mask_host_bits(struct mroute_addr *ma)
Definition mroute.c:318
void mroute_helper_add_iroute46(struct mroute_helper *mh, int netbits)
Definition mroute.c:520
unsigned int mroute_extract_addr_ip(struct mroute_addr *src, struct mroute_addr *dest, const struct buffer *buf)
Definition mroute.c:147
static unsigned int mroute_extract_addr_from_packet(struct mroute_addr *src, struct mroute_addr *dest, uint16_t vid, const struct buffer *buf, int tunnel_type)
Definition mroute.h:183
static in_addr_t in_addr_t_from_mroute_addr(const struct mroute_addr *addr)
Definition mroute.h:253
const char * mroute_addr_print_ex(const struct mroute_addr *ma, const unsigned int flags, struct gc_arena *gc)
Definition mroute.c:377
bool mroute_extract_openvpn_sockaddr(struct mroute_addr *addr, const struct openvpn_sockaddr *osaddr, bool use_port)
Definition mroute.c:254
const char * mroute_addr_print(const struct mroute_addr *ma, struct gc_arena *gc)
Definition mroute.c:371
static void mroute_extract_in_addr_t(struct mroute_addr *dest, const in_addr_t src)
Definition mroute.h:244
uint32_t mroute_addr_hash_function(const void *key, uint32_t iv)
Definition mroute.c:358
#define MR_ADDR_IPV4
Definition mroute.h:62
void mroute_helper_del_iroute46(struct mroute_helper *mh, int netbits)
Definition mroute.c:535
bool mroute_learnable_address(const struct mroute_addr *addr, struct gc_arena *gc)
Definition mroute.c:63
static const uint8_t * mroute_addr_hash_ptr(const struct mroute_addr *a)
Definition mroute.h:231
bool mroute_addr_compare_function(const void *key1, const void *key2)
Definition mroute.c:365
unsigned int mroute_extract_addr_ether(struct mroute_addr *src, struct mroute_addr *dest, uint16_t vid, const struct buffer *buf)
Definition mroute.c:222
struct mroute_helper * mroute_helper_init(int ageable_ttl_secs)
Definition mroute.c:482
#define MR_HELPER_NET_LEN
Definition mroute.h:126
void mroute_addr_init(struct mroute_addr *addr)
Definition mroute.c:38
static bool mroute_addr_equal(const struct mroute_addr *a1, const struct mroute_addr *a2)
Definition mroute.h:209
#define MR_ADDR_MASK
Definition mroute.h:64
static void mroute_addr_reset(struct mroute_addr *ma)
Definition mroute.h:266
#define MR_ADDR_NONE
Definition mroute.h:60
void mroute_helper_free(struct mroute_helper *mh)
Definition mroute.c:551
#define DEV_TYPE_TAP
Definition proto.h:36
#define OPENVPN_ETH_ALEN
Definition proto.h:52
#define DEV_TYPE_TUN
Definition proto.h:35
Wrapper structure for dynamically allocated memory.
Definition buffer.h:60
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:116
Container for bidirectional cipher and HMAC key material.
Definition crypto.h:240
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:152
uint8_t raw_addr[MR_MAX_ADDR_LEN]
Definition mroute.h:90
struct mroute_addr::@2::@7 v4mappedv6
uint16_t vid
Definition mroute.h:94
in_addr_t addr
Definition mroute.h:98
uint8_t prefix[12]
Definition mroute.h:108
struct mroute_addr::@2::@4 ether
struct mroute_addr::@2::@6 v6
uint8_t addr[OPENVPN_ETH_ALEN]
Definition mroute.h:93
struct mroute_addr::@2::@5 v4
uint8_t proto
Definition mroute.h:84
uint8_t type
Definition mroute.h:85
in_port_t port
Definition mroute.h:99
uint8_t len
Definition mroute.h:83
uint8_t netbits
Definition mroute.h:86
int ageable_ttl_secs
Definition mroute.h:134
int net_len_refcount[MR_HELPER_NET_LEN]
Definition mroute.h:137
unsigned int cache_generation
Definition mroute.h:133
uint8_t net_len[MR_HELPER_NET_LEN]
Definition mroute.h:136
int n_net_len
Definition mroute.h:135
union openvpn_sockaddr::@27 addr
struct gc_arena gc
Definition test_ssl.c:131