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