OpenVPN
mroute.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-2021 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 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #elif defined(_MSC_VER)
27 #include "config-msvc.h"
28 #endif
29 
30 #include "syshead.h"
31 
32 
33 #include "mroute.h"
34 #include "proto.h"
35 #include "error.h"
36 #include "socket.h"
37 
38 #include "memdbg.h"
39 
40 void
42 {
43  CLEAR(*addr);
44 }
45 
46 /*
47  * Ethernet multicast addresses.
48  */
49 
50 static inline bool
51 is_mac_mcast_addr(const uint8_t *mac)
52 {
53  return (bool) (mac[0] & 1);
54 }
55 
56 static inline bool
57 is_mac_mcast_maddr(const struct mroute_addr *addr)
58 {
59  return (addr->type & MR_ADDR_MASK) == MR_ADDR_ETHER
60  && is_mac_mcast_addr(addr->ether.addr);
61 }
62 
63 /*
64  * Don't learn certain addresses.
65  */
66 bool
67 mroute_learnable_address(const struct mroute_addr *addr, struct gc_arena *gc)
68 {
69  int i;
70  bool all_zeros = true;
71  bool all_ones = true;
72 
73  for (i = 0; i < addr->len; ++i)
74  {
75  int b = addr->raw_addr[i];
76  if (b != 0x00)
77  {
78  all_zeros = false;
79  }
80  if (b != 0xFF)
81  {
82  all_ones = false;
83  }
84  }
85 
86  /* only networkss shorter than 8 bits are allowed to be all 0s. */
87  if (all_zeros
88  && !((addr->type & MR_WITH_NETBITS) && (addr->netbits < 8)))
89  {
90  msg(D_MULTI_LOW, "Can't learn %s: network is all 0s, but netbits >= 8",
91  mroute_addr_print(addr, gc));
92  return false;
93  }
94 
95  if (all_ones)
96  {
97  msg(D_MULTI_LOW, "Can't learn %s: network is all 1s",
98  mroute_addr_print(addr, gc));
99  return false;
100  }
101 
102  if (is_mac_mcast_maddr(addr))
103  {
104  msg(D_MULTI_LOW, "Can't learn %s: network is a multicast address",
105  mroute_addr_print(addr, gc));
106  return false;
107  }
108 
109  return true;
110 }
111 
112 static inline void
113 mroute_get_in_addr_t(struct mroute_addr *ma, const in_addr_t src, unsigned int mask)
114 {
115  if (ma)
116  {
117  ma->type = MR_ADDR_IPV4 | mask;
118  ma->netbits = 0;
119  ma->len = 4;
120  ma->v4.addr = src;
121  }
122 }
123 
124 static inline void
125 mroute_get_in6_addr(struct mroute_addr *ma, const struct in6_addr src, unsigned int mask)
126 {
127  if (ma)
128  {
129  ma->type = MR_ADDR_IPV6 | mask;
130  ma->netbits = 0;
131  ma->len = 16;
132  ma->v6.addr = src;
133  }
134 }
135 
136 static inline bool
138 {
139  return ((addr & htonl(IP_MCAST_SUBNET_MASK)) == htonl(IP_MCAST_NETWORK));
140 }
141 
142 /* RFC 4291, 2.7, "binary 11111111 at the start of an address identifies
143  * the address as being a multicast address"
144  */
145 static inline bool
146 mroute_is_mcast_ipv6(const struct in6_addr addr)
147 {
148  return (addr.s6_addr[0] == 0xff);
149 }
150 
151 
152 unsigned int
154  const struct buffer *buf)
155 {
156  unsigned int ret = 0;
157  if (BLEN(buf) >= 1)
158  {
159  switch (OPENVPN_IPH_GET_VER(*BPTR(buf)))
160  {
161  case 4:
162  if (BLEN(buf) >= (int) sizeof(struct openvpn_iphdr))
163  {
164  const struct openvpn_iphdr *ip = (const struct openvpn_iphdr *) BPTR(buf);
165 
166  mroute_get_in_addr_t(src, ip->saddr, 0);
167  mroute_get_in_addr_t(dest, ip->daddr, 0);
168 
169  /* multicast packet? */
170  if (mroute_is_mcast(ip->daddr))
171  {
172  ret |= MROUTE_EXTRACT_MCAST;
173  }
174 
175  /* IGMP message? */
176  if (ip->protocol == OPENVPN_IPPROTO_IGMP)
177  {
178  ret |= MROUTE_EXTRACT_IGMP;
179  }
180 
182  }
183  break;
184 
185  case 6:
186  if (BLEN(buf) >= (int) sizeof(struct openvpn_ipv6hdr))
187  {
188  const struct openvpn_ipv6hdr *ipv6 = (const struct openvpn_ipv6hdr *) BPTR(buf);
189 #if 0 /* very basic debug */
190  struct gc_arena gc = gc_new();
191  msg( M_INFO, "IPv6 packet! src=%s, dst=%s",
192  print_in6_addr( ipv6->saddr, 0, &gc ),
193  print_in6_addr( ipv6->daddr, 0, &gc ));
194  gc_free(&gc);
195 #endif
196 
197  mroute_get_in6_addr(src, ipv6->saddr, 0);
198  mroute_get_in6_addr(dest, ipv6->daddr, 0);
199 
200  if (mroute_is_mcast_ipv6(ipv6->daddr))
201  {
202  ret |= MROUTE_EXTRACT_MCAST;
203  }
204 
206  }
207  break;
208 
209  default:
210  msg(M_WARN, "IP packet with unknown IP version=%d seen",
211  OPENVPN_IPH_GET_VER(*BPTR(buf)));
212  }
213  }
214  return ret;
215 }
216 
217 static void
219  const uint8_t *ether_addr,
220  uint16_t vid)
221 {
222  maddr->type = MR_ADDR_ETHER;
223  maddr->netbits = 0;
224  maddr->len = OPENVPN_ETH_ALEN;
225  memcpy(maddr->ether.addr, ether_addr, OPENVPN_ETH_ALEN);
226  maddr->len += sizeof(vid);
227  maddr->ether.vid = vid;
228 }
229 
230 unsigned int
232  struct mroute_addr *dest,
233  uint16_t vid,
234  const struct buffer *buf)
235 {
236  unsigned int ret = 0;
237  if (BLEN(buf) >= (int) sizeof(struct openvpn_ethhdr))
238  {
239  const struct openvpn_ethhdr *eth = (const struct openvpn_ethhdr *) BPTR(buf);
240  if (src)
241  {
242  mroute_copy_ether_to_addr(src, eth->source, vid);
243  }
244  if (dest)
245  {
246  mroute_copy_ether_to_addr(dest, eth->dest, vid);
247 
248  /* ethernet broadcast/multicast packet? */
249  if (is_mac_mcast_addr(eth->dest))
250  {
251  ret |= MROUTE_EXTRACT_BCAST;
252  }
253  }
254 
256 
257  }
258  return ret;
259 }
260 
261 /*
262  * Translate a struct openvpn_sockaddr (osaddr)
263  * to a struct mroute_addr (addr).
264  */
265 bool
267  const struct openvpn_sockaddr *osaddr,
268  bool use_port)
269 {
270  switch (osaddr->addr.sa.sa_family)
271  {
272  case AF_INET:
273  {
274  if (use_port)
275  {
276  addr->type = MR_ADDR_IPV4 | MR_WITH_PORT;
277  addr->netbits = 0;
278  addr->len = 6;
279  addr->v4.addr = osaddr->addr.in4.sin_addr.s_addr;
280  addr->v4.port = osaddr->addr.in4.sin_port;
281  }
282  else
283  {
284  addr->type = MR_ADDR_IPV4;
285  addr->netbits = 0;
286  addr->len = 4;
287  addr->v4.addr = osaddr->addr.in4.sin_addr.s_addr;
288  }
289  return true;
290  }
291 
292  case AF_INET6:
293  if (use_port)
294  {
295  addr->type = MR_ADDR_IPV6 | MR_WITH_PORT;
296  addr->netbits = 0;
297  addr->len = 18;
298  addr->v6.addr = osaddr->addr.in6.sin6_addr;
299  addr->v6.port = osaddr->addr.in6.sin6_port;
300  }
301  else
302  {
303  addr->type = MR_ADDR_IPV6;
304  addr->netbits = 0;
305  addr->len = 16;
306  addr->v6.addr = osaddr->addr.in6.sin6_addr;
307  }
308  return true;
309  }
310  return false;
311 }
312 
313 /*
314  * Zero off the host bits in an address, leaving
315  * only the network bits, using the netbits member of
316  * struct mroute_addr as the controlling parameter.
317  *
318  * TODO: this is called for route-lookup for every yet-unhashed
319  * destination address, so for lots of active net-iroutes, this
320  * might benefit from some "zeroize 32 bit at a time" improvements
321  */
322 void
324 {
325  if ((ma->type & MR_ADDR_MASK) == MR_ADDR_IPV4)
326  {
327  in_addr_t addr = ntohl(ma->v4.addr);
328  addr &= netbits_to_netmask(ma->netbits);
329  ma->v4.addr = htonl(addr);
330  }
331  else if ((ma->type & MR_ADDR_MASK) == MR_ADDR_IPV6)
332  {
333  int byte = sizeof(ma->v6.addr) - 1; /* rightmost byte in address */
334  int bits_to_clear = 128 - ma->netbits;
335 
336  while (byte >= 0 && bits_to_clear > 0)
337  {
338  if (bits_to_clear >= 8)
339  {
340  ma->v6.addr.s6_addr[byte--] = 0;
341  bits_to_clear -= 8;
342  }
343  else
344  {
345  ma->v6.addr.s6_addr[byte--] &= (IPV4_NETMASK_HOST << bits_to_clear);
346  bits_to_clear = 0;
347  }
348  }
349  ASSERT( bits_to_clear == 0 );
350  }
351  else
352  {
353  ASSERT(0);
354  }
355 }
356 
357 /*
358  * The mroute_addr hash function takes into account the
359  * address type, number of bits in the network address,
360  * and the actual address.
361  */
362 uint32_t
363 mroute_addr_hash_function(const void *key, uint32_t iv)
364 {
365  return hash_func(mroute_addr_hash_ptr((const struct mroute_addr *) key),
366  mroute_addr_hash_len((const struct mroute_addr *) key),
367  iv);
368 }
369 
370 bool
371 mroute_addr_compare_function(const void *key1, const void *key2)
372 {
373  return mroute_addr_equal((const struct mroute_addr *) key1,
374  (const struct mroute_addr *) key2);
375 }
376 
377 const char *
379  struct gc_arena *gc)
380 {
382 }
383 
384 const char *
386  const unsigned int flags,
387  struct gc_arena *gc)
388 {
389  struct buffer out = alloc_buf_gc(64, gc);
390  if (ma)
391  {
392  struct mroute_addr maddr = *ma;
393 
394  switch (maddr.type & MR_ADDR_MASK)
395  {
396  case MR_ADDR_ETHER:
397  buf_printf(&out, "%s", format_hex_ex(ma->ether.addr,
398  sizeof(ma->ether.addr), 0, 1, ":", gc));
399  buf_printf(&out, "@%hu", ma->ether.vid);
400  break;
401 
402  case MR_ADDR_IPV4:
403  {
404  if ((flags & MAPF_SHOW_ARP) && (maddr.type & MR_ARP))
405  {
406  buf_printf(&out, "ARP/");
407  }
408  buf_printf(&out, "%s", print_in_addr_t(ntohl(maddr.v4.addr),
409  (flags & MAPF_IA_EMPTY_IF_UNDEF) ? IA_EMPTY_IF_UNDEF : 0, gc));
410  if (maddr.type & MR_WITH_NETBITS)
411  {
412  if (flags & MAPF_SUBNET)
413  {
414  const in_addr_t netmask = netbits_to_netmask(maddr.netbits);
415  buf_printf(&out, "/%s", print_in_addr_t(netmask, 0, gc));
416  }
417  else
418  {
419  buf_printf(&out, "/%d", maddr.netbits);
420  }
421  }
422  if (maddr.type & MR_WITH_PORT)
423  {
424  buf_printf(&out, ":%d", ntohs(maddr.v4.port));
425  }
426  }
427  break;
428 
429  case MR_ADDR_IPV6:
430  {
431  if (IN6_IS_ADDR_V4MAPPED( &maddr.v6.addr ) )
432  {
433  buf_printf(&out, "%s", print_in_addr_t(maddr.v4mappedv6.addr,
434  IA_NET_ORDER, gc));
435  /* we only print port numbers for v4mapped v6 as of
436  * today, because "v6addr:port" is too ambiguous
437  */
438  if (maddr.type & MR_WITH_PORT)
439  {
440  buf_printf(&out, ":%d", ntohs(maddr.v6.port));
441  }
442  }
443  else
444  {
445  buf_printf(&out, "%s", print_in6_addr(maddr.v6.addr, 0, gc));
446  }
447  if (maddr.type & MR_WITH_NETBITS)
448  {
449  buf_printf(&out, "/%d", maddr.netbits);
450  }
451  }
452  break;
453 
454  default:
455  buf_printf(&out, "UNKNOWN");
456  break;
457  }
458  return BSTR(&out);
459  }
460  else
461  {
462  return "[NULL]";
463  }
464 }
465 
466 /*
467  * mroute_helper's main job is keeping track of
468  * currently used CIDR netlengths, so we don't
469  * have to cycle through all 33.
470  */
471 
472 struct mroute_helper *
474 {
475  struct mroute_helper *mh;
476  ALLOC_OBJ_CLEAR(mh, struct mroute_helper);
478  return mh;
479 }
480 
481 static void
483 {
484  int i, j = 0;
485  for (i = MR_HELPER_NET_LEN - 1; i >= 0; --i)
486  {
487  if (mh->net_len_refcount[i] > 0)
488  {
489  mh->net_len[j++] = (uint8_t) i;
490  }
491  }
492  mh->n_net_len = j;
493 
494 #ifdef ENABLE_DEBUG
496  {
497  struct gc_arena gc = gc_new();
498  struct buffer out = alloc_buf_gc(256, &gc);
499  buf_printf(&out, "MROUTE CIDR netlen:");
500  for (i = 0; i < mh->n_net_len; ++i)
501  {
502  buf_printf(&out, " /%d", mh->net_len[i]);
503  }
504  dmsg(D_MULTI_DEBUG, "%s", BSTR(&out));
505  gc_free(&gc);
506  }
507 #endif
508 }
509 
510 void
512 {
513  if (netbits >= 0)
514  {
515  ASSERT(netbits < MR_HELPER_NET_LEN);
516  ++mh->cache_generation;
517  ++mh->net_len_refcount[netbits];
518  if (mh->net_len_refcount[netbits] == 1)
519  {
521  }
522  }
523 }
524 
525 void
527 {
528  if (netbits >= 0)
529  {
530  ASSERT(netbits < MR_HELPER_NET_LEN);
531  ++mh->cache_generation;
532  --mh->net_len_refcount[netbits];
533  ASSERT(mh->net_len_refcount[netbits] >= 0);
534  if (!mh->net_len_refcount[netbits])
535  {
537  }
538  }
539 }
540 
541 void
543 {
544  free(mh);
545 }
uint8_t type
Definition: mroute.h:78
#define MR_WITH_NETBITS
Definition: mroute.h:70
uint32_t daddr
Definition: proto.h:128
static bool mroute_is_mcast_ipv6(const struct in6_addr addr)
Definition: mroute.c:146
uint8_t protocol
Definition: proto.h:124
void mroute_addr_mask_host_bits(struct mroute_addr *ma)
Definition: mroute.c:323
const char * print_in6_addr(struct in6_addr a6, unsigned int flags, struct gc_arena *gc)
Definition: socket.c:2896
#define M_INFO
Definition: errlevel.h:55
struct mroute_addr::@0::@1 ether
#define MR_ADDR_ETHER
Definition: mroute.h:61
struct mroute_addr::@0::@3 v6
static void gc_free(struct gc_arena *a)
Definition: buffer.h:1023
static void mroute_get_in6_addr(struct mroute_addr *ma, const struct in6_addr src, unsigned int mask)
Definition: mroute.c:125
#define MROUTE_EXTRACT_MCAST
Definition: mroute.h:40
bool mroute_addr_compare_function(const void *key1, const void *key2)
Definition: mroute.c:371
static uint32_t mroute_addr_hash_len(const struct mroute_addr *a)
Definition: mroute.h:233
void mroute_helper_del_iroute46(struct mroute_helper *mh, int netbits)
Definition: mroute.c:526
#define MR_ADDR_MASK
Definition: mroute.h:64
static in_addr_t netbits_to_netmask(const int netbits)
Definition: route.h:377
uint32_t mroute_addr_hash_function(const void *key, uint32_t iv)
Definition: mroute.c:363
bool buf_printf(struct buffer *buf, const char *format,...)
Definition: buffer.c:242
const char * mroute_addr_print_ex(const struct mroute_addr *ma, const unsigned int flags, struct gc_arena *gc)
Definition: mroute.c:385
#define in_addr_t
Definition: config-msvc.h:103
#define MR_WITH_PORT
Definition: mroute.h:67
#define dmsg(flags,...)
Definition: error.h:157
static bool mroute_is_mcast(const in_addr_t addr)
Definition: mroute.c:137
unsigned int cache_generation
Definition: mroute.h:132
#define ASSERT(x)
Definition: error.h:204
#define IA_NET_ORDER
Definition: socket.h:358
static bool check_debug_level(unsigned int level)
Definition: error.h:234
#define OPENVPN_IPH_GET_VER(v)
Definition: proto.h:107
#define IP_MCAST_SUBNET_MASK
Definition: mroute.h:33
struct in6_addr daddr
Definition: proto.h:143
void mroute_addr_init(struct mroute_addr *addr)
Definition: mroute.c:41
#define MROUTE_EXTRACT_IGMP
Definition: mroute.h:41
struct sockaddr_in6 in6
Definition: socket.h:70
list flags
#define CLEAR(x)
Definition: basic.h:33
#define BPTR(buf)
Definition: buffer.h:124
unsigned int mroute_extract_addr_ether(struct mroute_addr *src, struct mroute_addr *dest, uint16_t vid, const struct buffer *buf)
Definition: mroute.c:231
struct mroute_addr::@0::@4 v4mappedv6
#define IA_EMPTY_IF_UNDEF
Definition: socket.h:357
int net_len_refcount[MR_HELPER_NET_LEN]
Definition: mroute.h:136
#define MAPF_SHOW_ARP
Definition: mroute.h:159
const char * mroute_addr_print(const struct mroute_addr *ma, struct gc_arena *gc)
Definition: mroute.c:378
int ageable_ttl_secs
Definition: mroute.h:133
static struct gc_arena gc_new(void)
Definition: buffer.h:1015
#define ALLOC_OBJ_CLEAR(dptr, type)
Definition: buffer.h:1050
const char * print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc)
Definition: socket.c:2876
bool mroute_extract_openvpn_sockaddr(struct mroute_addr *addr, const struct openvpn_sockaddr *osaddr, bool use_port)
Definition: mroute.c:266
#define MR_ARP
Definition: mroute.h:73
struct sockaddr_in in4
Definition: socket.h:69
#define D_MULTI_DEBUG
Definition: errlevel.h:123
uint8_t len
Definition: mroute.h:76
void mroute_helper_free(struct mroute_helper *mh)
Definition: mroute.c:542
struct in6_addr saddr
Definition: proto.h:142
union openvpn_sockaddr::@10 addr
#define MROUTE_EXTRACT_BCAST
Definition: mroute.h:39
struct mroute_addr::@0::@2 v4
char * format_hex_ex(const uint8_t *data, int size, int maxoutput, unsigned int space_break_flags, const char *separator, struct gc_arena *gc)
Definition: buffer.c:519
#define IP_MCAST_NETWORK
Definition: mroute.h:34
static bool is_mac_mcast_maddr(const struct mroute_addr *addr)
Definition: mroute.c:57
#define MAPF_IA_EMPTY_IF_UNDEF
Definition: mroute.h:158
static void mroute_get_in_addr_t(struct mroute_addr *ma, const in_addr_t src, unsigned int mask)
Definition: mroute.c:113
#define MR_ADDR_IPV4
Definition: mroute.h:62
uint8_t source[OPENVPN_ETH_ALEN]
Definition: proto.h:58
Container for bidirectional cipher and HMAC key material.
Definition: crypto.h:181
uint8_t dest[OPENVPN_ETH_ALEN]
Definition: proto.h:57
#define BLEN(buf)
Definition: buffer.h:127
#define msg(flags,...)
Definition: error.h:153
uint8_t raw_addr[MR_MAX_ADDR_LEN]
Definition: mroute.h:82
uint32_t hash_func(const uint8_t *k, uint32_t length, uint32_t initval)
Definition: list.c:589
uint32_t saddr
Definition: proto.h:127
#define MROUTE_EXTRACT_SUCCEEDED
Definition: mroute.h:38
struct mroute_helper * mroute_helper_init(int ageable_ttl_secs)
Definition: mroute.c:473
static const uint8_t * mroute_addr_hash_ptr(const struct mroute_addr *a)
Definition: mroute.h:226
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
void mroute_helper_add_iroute46(struct mroute_helper *mh, int netbits)
Definition: mroute.c:511
#define OPENVPN_IPPROTO_IGMP
Definition: proto.h:120
int n_net_len
Definition: mroute.h:134
#define MR_ADDR_IPV6
Definition: mroute.h:63
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Definition: buffer.c:90
static bool is_mac_mcast_addr(const uint8_t *mac)
Definition: mroute.c:51
#define M_WARN
Definition: error.h:100
#define free
Definition: cmocka.c:1850
#define OPENVPN_ETH_ALEN
Definition: proto.h:54
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
static void mroute_helper_regenerate(struct mroute_helper *mh)
Definition: mroute.c:482
static bool mroute_addr_equal(const struct mroute_addr *a1, const struct mroute_addr *a2)
Definition: mroute.h:208
#define BSTR(buf)
Definition: buffer.h:129
unsigned int mroute_extract_addr_ip(struct mroute_addr *src, struct mroute_addr *dest, const struct buffer *buf)
Definition: mroute.c:153
#define MAPF_SUBNET
Definition: mroute.h:157
uint8_t net_len[MR_HELPER_NET_LEN]
Definition: mroute.h:135
uint8_t netbits
Definition: mroute.h:79
struct sockaddr sa
Definition: socket.h:68
#define IPV4_NETMASK_HOST
Definition: basic.h:35
#define D_MULTI_LOW
Definition: errlevel.h:86
static void mroute_copy_ether_to_addr(struct mroute_addr *maddr, const uint8_t *ether_addr, uint16_t vid)
Definition: mroute.c:218
#define MR_HELPER_NET_LEN
Definition: mroute.h:126
bool mroute_learnable_address(const struct mroute_addr *addr, struct gc_arena *gc)
Definition: mroute.c:67
Container for unidirectional cipher and HMAC key material.
Definition: crypto.h:151