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-2024 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, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 struct mroute_addr {
79  uint8_t len; /* length of address */
80  uint8_t proto;
81  uint8_t type; /* MR_ADDR/MR_WITH flags */
82  uint8_t netbits; /* number of bits in network part of address,
83  * valid if MR_WITH_NETBITS is set */
84  union {
85  uint8_t raw_addr[MR_MAX_ADDR_LEN]; /* actual address */
86  struct {
88  uint16_t vid;
89  } ether;
90  struct {
91  in_addr_t addr; /* _network order_ IPv4 address */
92  in_port_t port; /* _network order_ TCP/UDP port */
93  } v4;
94  struct {
95  struct in6_addr addr;
96  in_port_t port; /* _network order_ TCP/UDP port */
97  } v6;
98  struct {
99  uint8_t prefix[12];
100  in_addr_t addr; /* _network order_ IPv4 address */
101  } v4mappedv6;
102  };
103 };
104 
105 /* Double-check that struct packing works as expected */
106 static_assert(offsetof(struct mroute_addr, v4.port) ==
107  offsetof(struct mroute_addr, v4) + 4,
108  "Unexpected struct packing of v4");
109 static_assert(offsetof(struct mroute_addr, v6.port) ==
110  offsetof(struct mroute_addr, v6) + 16,
111  "Unexpected struct packing of v6");
112 static_assert(offsetof(struct mroute_addr, v4mappedv6.addr) ==
113  offsetof(struct mroute_addr, v4mappedv6) + 12,
114  "Unexpected struct packing of v4mappedv6");
115 
116 /*
117  * Number of bits in an address. Should be raised for IPv6.
118  */
119 #define MR_HELPER_NET_LEN 129
120 
121 /*
122  * Used to help maintain CIDR routing table.
123  */
125  unsigned int cache_generation; /* incremented when route added */
126  int ageable_ttl_secs; /* host route cache entry time-to-live*/
127  int n_net_len; /* length of net_len array */
128  uint8_t net_len[MR_HELPER_NET_LEN]; /* CIDR netlengths in descending order */
129  int net_len_refcount[MR_HELPER_NET_LEN]; /* refcount of each netlength */
130 };
131 
132 struct openvpn_sockaddr;
133 
135  const struct openvpn_sockaddr *osaddr,
136  bool use_port);
137 
138 bool mroute_learnable_address(const struct mroute_addr *addr,
139  struct gc_arena *gc);
140 
141 uint32_t mroute_addr_hash_function(const void *key, uint32_t iv);
142 
143 bool mroute_addr_compare_function(const void *key1, const void *key2);
144 
145 void mroute_addr_init(struct mroute_addr *addr);
146 
147 const char *mroute_addr_print(const struct mroute_addr *ma,
148  struct gc_arena *gc);
149 
150 #define MAPF_SUBNET (1<<0)
151 #define MAPF_IA_EMPTY_IF_UNDEF (1<<1)
152 #define MAPF_SHOW_ARP (1<<2)
153 const char *mroute_addr_print_ex(const struct mroute_addr *ma,
154  const unsigned int flags,
155  struct gc_arena *gc);
156 
157 void mroute_addr_mask_host_bits(struct mroute_addr *ma);
158 
160 
161 void mroute_helper_free(struct mroute_helper *mh);
162 
163 void mroute_helper_add_iroute46(struct mroute_helper *mh, int netbits);
164 
165 void mroute_helper_del_iroute46(struct mroute_helper *mh, int netbits);
166 
167 unsigned int mroute_extract_addr_ip(struct mroute_addr *src,
168  struct mroute_addr *dest,
169  const struct buffer *buf);
170 
171 unsigned int mroute_extract_addr_ether(struct mroute_addr *src,
172  struct mroute_addr *dest,
173  uint16_t vid,
174  const struct buffer *buf);
175 
176 /*
177  * Given a raw packet in buf, return the src and dest
178  * addresses of the packet.
179  */
180 static inline unsigned int
182  struct mroute_addr *dest,
183  uint16_t vid,
184  const struct buffer *buf,
185  int tunnel_type)
186 {
187  unsigned int ret = 0;
188  verify_align_4(buf);
189 
190  /*
191  * Since we don't really need the protocol on vaddresses for internal VPN
192  * payload packets, make sure we have the same value to avoid hashing insert
193  * and search issues.
194  */
195  src->proto = 0;
196  dest->proto = src->proto;
197 
198  if (tunnel_type == DEV_TYPE_TUN)
199  {
200  ret = mroute_extract_addr_ip(src, dest, buf);
201  }
202  else if (tunnel_type == DEV_TYPE_TAP)
203  {
204  ret = mroute_extract_addr_ether(src, dest, vid, buf);
205  }
206  return ret;
207 }
208 
209 static inline bool
210 mroute_addr_equal(const struct mroute_addr *a1, const struct mroute_addr *a2)
211 {
212  if (a1->type != a2->type)
213  {
214  return false;
215  }
216  if (a1->proto != a2->proto)
217  {
218  return false;
219  }
220  if (a1->netbits != a2->netbits)
221  {
222  return false;
223  }
224  if (a1->len != a2->len)
225  {
226  return false;
227  }
228  return memcmp(a1->raw_addr, a2->raw_addr, a1->len) == 0;
229 }
230 
231 static inline const uint8_t *
233 {
234  /* NOTE: depends on ordering of struct mroute_addr */
235  return (uint8_t *) &a->proto;
236 }
237 
238 static inline uint32_t
240 {
241  return (uint32_t) a->len + 3;
242 }
243 
244 static inline void
245 mroute_extract_in_addr_t(struct mroute_addr *dest, const in_addr_t src)
246 {
247  dest->type = MR_ADDR_IPV4;
248  dest->netbits = 0;
249  dest->len = 4;
250  dest->v4.addr = htonl(src);
251 }
252 
253 static inline in_addr_t
255 {
256  if ((addr->type & MR_ADDR_MASK) == MR_ADDR_IPV4 && addr->netbits == 0 && addr->len == 4)
257  {
258  return ntohl(addr->v4.addr);
259  }
260  else
261  {
262  return 0;
263  }
264 }
265 
266 static inline void
268 {
269  ma->len = 0;
270  ma->type = MR_ADDR_NONE;
271 }
272 
273 #endif /* MROUTE_H */
MR_ADDR_MASK
#define MR_ADDR_MASK
Definition: mroute.h:64
mroute_addr_init
void mroute_addr_init(struct mroute_addr *addr)
Definition: mroute.c:39
mroute_addr_hash_function
uint32_t mroute_addr_hash_function(const void *key, uint32_t iv)
Definition: mroute.c:369
DEV_TYPE_TUN
#define DEV_TYPE_TUN
Definition: proto.h:36
mroute_addr::ether
struct mroute_addr::@2::@4 ether
mroute_addr_reset
static void mroute_addr_reset(struct mroute_addr *ma)
Definition: mroute.h:267
static_assert
#define static_assert(expr, diagnostic)
Definition: error.h:212
key1
static const char *const key1
Definition: cert_data.h:56
mroute_helper::net_len_refcount
int net_len_refcount[MR_HELPER_NET_LEN]
Definition: mroute.h:129
mroute_extract_addr_ip
unsigned int mroute_extract_addr_ip(struct mroute_addr *src, struct mroute_addr *dest, const struct buffer *buf)
Definition: mroute.c:151
MR_MAX_ADDR_LEN
#define MR_MAX_ADDR_LEN
Definition: mroute.h:55
mroute_learnable_address
bool mroute_learnable_address(const struct mroute_addr *addr, struct gc_arena *gc)
Definition: mroute.c:65
openvpn_sockaddr
Definition: socket.h:65
mroute_helper_init
struct mroute_helper * mroute_helper_init(int ageable_ttl_secs)
Definition: mroute.c:488
mroute_addr_print
const char * mroute_addr_print(const struct mroute_addr *ma, struct gc_arena *gc)
Definition: mroute.c:384
mroute_addr::netbits
uint8_t netbits
Definition: mroute.h:82
mroute_helper::net_len
uint8_t net_len[MR_HELPER_NET_LEN]
Definition: mroute.h:128
mroute_addr_mask_host_bits
void mroute_addr_mask_host_bits(struct mroute_addr *ma)
Definition: mroute.c:329
key
Container for unidirectional cipher and HMAC key material.
Definition: crypto.h:151
mroute_helper_add_iroute46
void mroute_helper_add_iroute46(struct mroute_helper *mh, int netbits)
Definition: mroute.c:526
MR_HELPER_NET_LEN
#define MR_HELPER_NET_LEN
Definition: mroute.h:119
MR_ADDR_NONE
#define MR_ADDR_NONE
Definition: mroute.h:60
mroute_addr_hash_len
static uint32_t mroute_addr_hash_len(const struct mroute_addr *a)
Definition: mroute.h:239
mroute_addr::addr
uint8_t addr[OPENVPN_ETH_ALEN]
Definition: mroute.h:87
mroute_addr::v6
struct mroute_addr::@2::@6 v6
mroute_addr::prefix
uint8_t prefix[12]
Definition: mroute.h:99
route.h
mroute_addr::port
in_port_t port
Definition: mroute.h:92
MR_ADDR_IPV4
#define MR_ADDR_IPV4
Definition: mroute.h:62
mroute_addr::len
uint8_t len
Definition: mroute.h:79
mroute_helper::cache_generation
unsigned int cache_generation
Definition: mroute.h:125
verify_align_4
#define verify_align_4(ptr)
Definition: buffer.h:991
DEV_TYPE_TAP
#define DEV_TYPE_TAP
Definition: proto.h:37
mroute_addr_compare_function
bool mroute_addr_compare_function(const void *key1, const void *key2)
Definition: mroute.c:377
buffer
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
mroute_addr::v4
struct mroute_addr::@2::@5 v4
mroute_addr::vid
uint16_t vid
Definition: mroute.h:88
buffer.h
mroute_addr
Definition: mroute.h:78
mroute_helper::ageable_ttl_secs
int ageable_ttl_secs
Definition: mroute.h:126
mroute_addr::proto
uint8_t proto
Definition: mroute.h:80
gc_arena
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
mroute_extract_addr_ether
unsigned int mroute_extract_addr_ether(struct mroute_addr *src, struct mroute_addr *dest, uint16_t vid, const struct buffer *buf)
Definition: mroute.c:229
mroute_addr::v4mappedv6
struct mroute_addr::@2::@7 v4mappedv6
in_addr_t_from_mroute_addr
static in_addr_t in_addr_t_from_mroute_addr(const struct mroute_addr *addr)
Definition: mroute.h:254
mroute_addr::addr
in_addr_t addr
Definition: mroute.h:91
OPENVPN_ETH_ALEN
#define OPENVPN_ETH_ALEN
Definition: proto.h:53
mroute_helper
Definition: mroute.h:124
mroute_addr::type
uint8_t type
Definition: mroute.h:81
key2
Container for bidirectional cipher and HMAC key material.
Definition: crypto.h:238
mroute_extract_addr_from_packet
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:181
list.h
mroute_addr::raw_addr
uint8_t raw_addr[MR_MAX_ADDR_LEN]
Definition: mroute.h:85
mroute_addr_print_ex
const char * mroute_addr_print_ex(const struct mroute_addr *ma, const unsigned int flags, struct gc_arena *gc)
Definition: mroute.c:391
mroute_helper_free
void mroute_helper_free(struct mroute_helper *mh)
Definition: mroute.c:557
mroute_helper_del_iroute46
void mroute_helper_del_iroute46(struct mroute_helper *mh, int netbits)
Definition: mroute.c:541
mroute_addr_hash_ptr
static const uint8_t * mroute_addr_hash_ptr(const struct mroute_addr *a)
Definition: mroute.h:232
mroute_extract_openvpn_sockaddr
bool mroute_extract_openvpn_sockaddr(struct mroute_addr *addr, const struct openvpn_sockaddr *osaddr, bool use_port)
Definition: mroute.c:264
mroute_extract_in_addr_t
static void mroute_extract_in_addr_t(struct mroute_addr *dest, const in_addr_t src)
Definition: mroute.h:245
mroute_helper::n_net_len
int n_net_len
Definition: mroute.h:127
openvpn_sockaddr::addr
union openvpn_sockaddr::@20 addr
mroute_addr_equal
static bool mroute_addr_equal(const struct mroute_addr *a1, const struct mroute_addr *a2)
Definition: mroute.h:210
gc
struct gc_arena gc
Definition: test_ssl.c:155