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-2018 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 struct mroute_addr {
76  uint8_t len; /* length of address */
77  uint8_t unused;
78  uint8_t type; /* MR_ADDR/MR_WITH flags */
79  uint8_t netbits; /* number of bits in network part of address,
80  * valid if MR_WITH_NETBITS is set */
81  union {
82  uint8_t raw_addr[MR_MAX_ADDR_LEN]; /* actual address */
83  struct {
85  uint16_t vid;
86  } ether;
87  struct {
88  in_addr_t addr; /* _network order_ IPv4 address */
89  in_port_t port; /* _network order_ TCP/UDP port */
90  } v4;
91  struct {
92  struct in6_addr addr;
93  in_port_t port; /* _network order_ TCP/UDP port */
94  } v6;
95  struct {
96  uint8_t prefix[12];
97  in_addr_t addr; /* _network order_ IPv4 address */
98  } v4mappedv6;
99  }
100 #ifndef HAVE_ANONYMOUS_UNION_SUPPORT
101 /* Wrappers to support compilers that do not grok anonymous unions */
102  mroute_union
103 #define raw_addr mroute_union.raw_addr
104 #define ether mroute_union.ether
105 #define v4 mroute_union.v4
106 #define v6 mroute_union.v6
107 #define v4mappedv6 mroute_union.v4mappedv6
108 #endif
109  ;
110 };
111 
112 /* Double-check that struct packing works as expected */
113 static_assert(offsetof(struct mroute_addr, v4.port) ==
114  offsetof(struct mroute_addr, v4) + 4,
115  "Unexpected struct packing of v4");
116 static_assert(offsetof(struct mroute_addr, v6.port) ==
117  offsetof(struct mroute_addr, v6) + 16,
118  "Unexpected struct packing of v6");
119 static_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  unsigned int cache_generation; /* incremented when route added */
133  int ageable_ttl_secs; /* host route cache entry time-to-live*/
134  int n_net_len; /* length of net_len array */
135  uint8_t net_len[MR_HELPER_NET_LEN]; /* CIDR netlengths in descending order */
136  int net_len_refcount[MR_HELPER_NET_LEN]; /* refcount of each netlength */
137 };
138 
139 struct openvpn_sockaddr;
140 
142  const struct openvpn_sockaddr *osaddr,
143  bool use_port);
144 
145 bool mroute_learnable_address(const struct mroute_addr *addr,
146  struct gc_arena *gc);
147 
148 uint32_t mroute_addr_hash_function(const void *key, uint32_t iv);
149 
150 bool mroute_addr_compare_function(const void *key1, const void *key2);
151 
152 void mroute_addr_init(struct mroute_addr *addr);
153 
154 const char *mroute_addr_print(const struct mroute_addr *ma,
155  struct gc_arena *gc);
156 
157 #define MAPF_SUBNET (1<<0)
158 #define MAPF_IA_EMPTY_IF_UNDEF (1<<1)
159 #define MAPF_SHOW_ARP (1<<2)
160 const char *mroute_addr_print_ex(const struct mroute_addr *ma,
161  const unsigned int flags,
162  struct gc_arena *gc);
163 
164 void mroute_addr_mask_host_bits(struct mroute_addr *ma);
165 
167 
168 void mroute_helper_free(struct mroute_helper *mh);
169 
170 void mroute_helper_add_iroute46(struct mroute_helper *mh, int netbits);
171 
172 void mroute_helper_del_iroute46(struct mroute_helper *mh, int netbits);
173 
174 unsigned int mroute_extract_addr_ip(struct mroute_addr *src,
175  struct mroute_addr *dest,
176  const struct buffer *buf);
177 
178 unsigned int mroute_extract_addr_ether(struct mroute_addr *src,
179  struct mroute_addr *dest,
180  struct mroute_addr *esrc,
181  struct mroute_addr *edest,
182  uint16_t vid,
183  const struct buffer *buf);
184 
185 /*
186  * Given a raw packet in buf, return the src and dest
187  * addresses of the packet.
188  */
189 static inline unsigned int
191  struct mroute_addr *dest,
192  struct mroute_addr *esrc,
193  struct mroute_addr *edest,
194  uint16_t vid,
195  const struct buffer *buf,
196  int tunnel_type)
197 {
198  unsigned int ret = 0;
199  verify_align_4(buf);
200  if (tunnel_type == DEV_TYPE_TUN)
201  {
202  ret = mroute_extract_addr_ip(src, dest, buf);
203  }
204  else if (tunnel_type == DEV_TYPE_TAP)
205  {
206  ret = mroute_extract_addr_ether(src, dest, esrc, edest, vid, buf);
207  }
208  return ret;
209 }
210 
211 static inline bool
212 mroute_addr_equal(const struct mroute_addr *a1, const struct mroute_addr *a2)
213 {
214  if (a1->type != a2->type)
215  {
216  return false;
217  }
218  if (a1->netbits != a2->netbits)
219  {
220  return false;
221  }
222  if (a1->len != a2->len)
223  {
224  return false;
225  }
226  return memcmp(a1->raw_addr, a2->raw_addr, a1->len) == 0;
227 }
228 
229 static inline const uint8_t *
231 {
232  /* NOTE: depends on ordering of struct mroute_addr */
233  return (uint8_t *) &a->type;
234 }
235 
236 static inline uint32_t
238 {
239  return (uint32_t) a->len + 2;
240 }
241 
242 static inline void
244 {
245  dest->type = MR_ADDR_IPV4;
246  dest->netbits = 0;
247  dest->len = 4;
248  dest->v4.addr = htonl(src);
249 }
250 
251 static inline in_addr_t
253 {
254  if ((addr->type & MR_ADDR_MASK) == MR_ADDR_IPV4 && addr->netbits == 0 && addr->len == 4)
255  {
256  return ntohl(addr->v4.addr);
257  }
258  else
259  {
260  return 0;
261  }
262 }
263 
264 static inline void
266 {
267  ma->len = 0;
268  ma->type = MR_ADDR_NONE;
269 }
270 
271 #endif /* MROUTE_H */
uint8_t type
Definition: mroute.h:78
const char * mroute_addr_print(const struct mroute_addr *ma, struct gc_arena *gc)
Definition: mroute.c:448
struct mroute_addr::@0::@1 ether
struct mroute_addr::@0::@3 v6
void mroute_helper_free(struct mroute_helper *mh)
Definition: mroute.c:612
in_port_t port
Definition: mroute.h:89
#define MR_MAX_ADDR_LEN
Definition: mroute.h:55
#define static_assert(expr, diagnostic)
Definition: error.h:211
static uint32_t mroute_addr_hash_len(const struct mroute_addr *a)
Definition: mroute.h:237
#define MR_ADDR_MASK
Definition: mroute.h:64
#define in_addr_t
Definition: config-msvc.h:103
unsigned int cache_generation
Definition: mroute.h:132
unsigned int mroute_extract_addr_ip(struct mroute_addr *src, struct mroute_addr *dest, const struct buffer *buf)
Definition: mroute.c:185
in_addr_t addr
Definition: mroute.h:88
list flags
void mroute_helper_add_iroute46(struct mroute_helper *mh, int netbits)
Definition: mroute.c:581
uint8_t addr[OPENVPN_ETH_ALEN]
Definition: mroute.h:84
uint8_t prefix[12]
Definition: mroute.h:96
bool mroute_learnable_address(const struct mroute_addr *addr, struct gc_arena *gc)
Definition: mroute.c:67
struct mroute_addr::@0::@4 v4mappedv6
int ageable_ttl_secs
Definition: mroute.h:133
const char * mroute_addr_print_ex(const struct mroute_addr *ma, const unsigned int flags, struct gc_arena *gc)
Definition: mroute.c:455
static unsigned int mroute_extract_addr_from_packet(struct mroute_addr *src, struct mroute_addr *dest, struct mroute_addr *esrc, struct mroute_addr *edest, uint16_t vid, const struct buffer *buf, int tunnel_type)
Definition: mroute.h:190
#define DEV_TYPE_TUN
Definition: proto.h:37
void mroute_addr_mask_host_bits(struct mroute_addr *ma)
Definition: mroute.c:393
uint8_t len
Definition: mroute.h:76
unsigned int mroute_extract_addr_ether(struct mroute_addr *src, struct mroute_addr *dest, struct mroute_addr *esrc, struct mroute_addr *edest, uint16_t vid, const struct buffer *buf)
Definition: mroute.c:263
uint16_t vid
Definition: mroute.h:85
struct mroute_addr::@0::@2 v4
#define MR_ADDR_NONE
Definition: mroute.h:60
#define MR_ADDR_IPV4
Definition: mroute.h:62
Container for bidirectional cipher and HMAC key material.
Definition: crypto.h:181
uint16_t in_port_t
Definition: config-msvc.h:120
uint8_t raw_addr[MR_MAX_ADDR_LEN]
Definition: mroute.h:82
static const uint8_t * mroute_addr_hash_ptr(const struct mroute_addr *a)
Definition: mroute.h:230
uint8_t unused
Definition: mroute.h:77
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
void mroute_helper_del_iroute46(struct mroute_helper *mh, int netbits)
Definition: mroute.c:596
#define verify_align_4(ptr)
Definition: buffer.h:981
int n_net_len
Definition: mroute.h:134
static void mroute_extract_in_addr_t(struct mroute_addr *dest, const in_addr_t src)
Definition: mroute.h:243
static in_addr_t in_addr_t_from_mroute_addr(const struct mroute_addr *addr)
Definition: mroute.h:252
#define OPENVPN_ETH_ALEN
Definition: proto.h:54
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
static bool mroute_addr_equal(const struct mroute_addr *a1, const struct mroute_addr *a2)
Definition: mroute.h:212
bool mroute_extract_openvpn_sockaddr(struct mroute_addr *addr, const struct openvpn_sockaddr *osaddr, bool use_port)
Definition: mroute.c:336
void mroute_addr_init(struct mroute_addr *addr)
Definition: mroute.c:41
uint8_t netbits
Definition: mroute.h:79
bool mroute_addr_compare_function(const void *key1, const void *key2)
Definition: mroute.c:441
static void mroute_addr_reset(struct mroute_addr *ma)
Definition: mroute.h:265
struct mroute_helper * mroute_helper_init(int ageable_ttl_secs)
Definition: mroute.c:543
uint32_t mroute_addr_hash_function(const void *key, uint32_t iv)
Definition: mroute.c:433
#define MR_HELPER_NET_LEN
Definition: mroute.h:126
Container for unidirectional cipher and HMAC key material.
Definition: crypto.h:151
#define DEV_TYPE_TAP
Definition: proto.h:38