| 1 |
/* SPDX-License-Identifier: GPL-2.0 */ |
| 2 |
/* |
| 3 |
* Copyright (C) 2015-2019 Jason A. Donenfeld <[email protected]>. All Rights Reserved. |
| 4 |
*/ |
| 5 |
|
| 6 |
#ifndef _WG_ALLOWEDIPS_H |
| 7 |
#define _WG_ALLOWEDIPS_H |
| 8 |
|
| 9 |
#include <linux/mutex.h> |
| 10 |
#include <linux/ip.h> |
| 11 |
#include <linux/ipv6.h> |
| 12 |
|
| 13 |
struct wg_peer; |
| 14 |
|
| 15 |
struct allowedips_node { |
| 16 |
struct wg_peer __rcu *peer; |
| 17 |
struct allowedips_node __rcu *bit[2]; |
| 18 |
u8 cidr, bit_at_a, bit_at_b, bitlen; |
| 19 |
u8 bits[16] __aligned(__alignof(u64)); |
| 20 |
|
| 21 |
/* Keep rarely used members at bottom to be beyond cache line. */ |
| 22 |
unsigned long parent_bit_packed; |
| 23 |
union { |
| 24 |
struct list_head peer_list; |
| 25 |
struct rcu_head rcu; |
| 26 |
}; |
| 27 |
}; |
| 28 |
|
| 29 |
struct allowedips { |
| 30 |
struct allowedips_node __rcu *root4; |
| 31 |
struct allowedips_node __rcu *root6; |
| 32 |
u64 seq; |
| 33 |
} __aligned(4); /* We pack the lower 2 bits of &root, but m68k only gives 16-bit alignment. */ |
| 34 |
|
| 35 |
void wg_allowedips_init(struct allowedips *table); |
| 36 |
void wg_allowedips_free(struct allowedips *table, struct mutex *mutex); |
| 37 |
int wg_allowedips_insert_v4(struct allowedips *table, const struct in_addr *ip, |
| 38 |
u8 cidr, struct wg_peer *peer, struct mutex *lock); |
| 39 |
int wg_allowedips_insert_v6(struct allowedips *table, const struct in6_addr *ip, |
| 40 |
u8 cidr, struct wg_peer *peer, struct mutex *lock); |
| 41 |
int wg_allowedips_remove_v4(struct allowedips *table, const struct in_addr *ip, |
| 42 |
u8 cidr, struct wg_peer *peer, struct mutex *lock); |
| 43 |
int wg_allowedips_remove_v6(struct allowedips *table, const struct in6_addr *ip, |
| 44 |
u8 cidr, struct wg_peer *peer, struct mutex *lock); |
| 45 |
void wg_allowedips_remove_by_peer(struct allowedips *table, |
| 46 |
struct wg_peer *peer, struct mutex *lock); |
| 47 |
/* The ip input pointer should be __aligned(__alignof(u64))) */ |
| 48 |
int wg_allowedips_read_node(struct allowedips_node *node, u8 ip[16], u8 *cidr); |
| 49 |
|
| 50 |
/* These return a strong reference to a peer: */ |
| 51 |
struct wg_peer *wg_allowedips_lookup_dst(struct allowedips *table, |
| 52 |
struct sk_buff *skb); |
| 53 |
struct wg_peer *wg_allowedips_lookup_src(struct allowedips *table, |
| 54 |
struct sk_buff *skb); |
| 55 |
|
| 56 |
#ifdef DEBUG |
| 57 |
bool wg_allowedips_selftest(void); |
| 58 |
#endif |
| 59 |
|
| 60 |
int wg_allowedips_slab_init(void); |
| 61 |
void wg_allowedips_slab_uninit(void); |
| 62 |
|
| 63 |
#endif /* _WG_ALLOWEDIPS_H */ |