Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^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) #include <linux/jhash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <linux/netfilter.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/rhashtable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <net/genetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <net/ila.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <net/netns/generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <uapi/linux/genetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "ila.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) struct ila_xlat_params {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 	struct ila_params ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	int ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) struct ila_map {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	struct ila_xlat_params xp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	struct rhash_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	struct ila_map __rcu *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	struct rcu_head rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define MAX_LOCKS 1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define	LOCKS_PER_CPU 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static int alloc_ila_locks(struct ila_net *ilan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	return alloc_bucket_spinlocks(&ilan->xlat.locks, &ilan->xlat.locks_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 				      MAX_LOCKS, LOCKS_PER_CPU,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 				      GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) static u32 hashrnd __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static __always_inline void __ila_hash_secret_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	net_get_random_once(&hashrnd, sizeof(hashrnd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static inline u32 ila_locator_hash(struct ila_locator loc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	u32 *v = (u32 *)loc.v32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	__ila_hash_secret_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	return jhash_2words(v[0], v[1], hashrnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static inline spinlock_t *ila_get_lock(struct ila_net *ilan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 				       struct ila_locator loc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	return &ilan->xlat.locks[ila_locator_hash(loc) & ilan->xlat.locks_mask];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static inline int ila_cmp_wildcards(struct ila_map *ila,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 				    struct ila_addr *iaddr, int ifindex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	return (ila->xp.ifindex && ila->xp.ifindex != ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) static inline int ila_cmp_params(struct ila_map *ila,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 				 struct ila_xlat_params *xp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	return (ila->xp.ifindex != xp->ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static int ila_cmpfn(struct rhashtable_compare_arg *arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		     const void *obj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	const struct ila_map *ila = obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	return (ila->xp.ip.locator_match.v64 != *(__be64 *)arg->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) static inline int ila_order(struct ila_map *ila)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	int score = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	if (ila->xp.ifindex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		score += 1 << 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	return score;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) static const struct rhashtable_params rht_params = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	.nelem_hint = 1024,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	.head_offset = offsetof(struct ila_map, node),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	.key_offset = offsetof(struct ila_map, xp.ip.locator_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	.key_len = sizeof(u64), /* identifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	.max_size = 1048576,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	.min_size = 256,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	.automatic_shrinking = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	.obj_cmpfn = ila_cmpfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) static int parse_nl_config(struct genl_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			   struct ila_xlat_params *xp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	memset(xp, 0, sizeof(*xp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (info->attrs[ILA_ATTR_LOCATOR])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		xp->ip.locator.v64 = (__force __be64)nla_get_u64(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			info->attrs[ILA_ATTR_LOCATOR]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (info->attrs[ILA_ATTR_LOCATOR_MATCH])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		xp->ip.locator_match.v64 = (__force __be64)nla_get_u64(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			info->attrs[ILA_ATTR_LOCATOR_MATCH]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (info->attrs[ILA_ATTR_CSUM_MODE])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		xp->ip.csum_mode = nla_get_u8(info->attrs[ILA_ATTR_CSUM_MODE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		xp->ip.csum_mode = ILA_CSUM_NO_ACTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	if (info->attrs[ILA_ATTR_IDENT_TYPE])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		xp->ip.ident_type = nla_get_u8(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 				info->attrs[ILA_ATTR_IDENT_TYPE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		xp->ip.ident_type = ILA_ATYPE_USE_FORMAT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (info->attrs[ILA_ATTR_IFINDEX])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		xp->ifindex = nla_get_s32(info->attrs[ILA_ATTR_IFINDEX]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /* Must be called with rcu readlock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static inline struct ila_map *ila_lookup_wildcards(struct ila_addr *iaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 						   int ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 						   struct ila_net *ilan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	struct ila_map *ila;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	ila = rhashtable_lookup_fast(&ilan->xlat.rhash_table, &iaddr->loc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 				     rht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	while (ila) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		if (!ila_cmp_wildcards(ila, iaddr, ifindex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			return ila;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		ila = rcu_access_pointer(ila->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /* Must be called with rcu readlock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static inline struct ila_map *ila_lookup_by_params(struct ila_xlat_params *xp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 						   struct ila_net *ilan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	struct ila_map *ila;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	ila = rhashtable_lookup_fast(&ilan->xlat.rhash_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 				     &xp->ip.locator_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 				     rht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	while (ila) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		if (!ila_cmp_params(ila, xp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			return ila;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		ila = rcu_access_pointer(ila->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static inline void ila_release(struct ila_map *ila)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	kfree_rcu(ila, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static void ila_free_node(struct ila_map *ila)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	struct ila_map *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	/* Assume rcu_readlock held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	while (ila) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		next = rcu_access_pointer(ila->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		ila_release(ila);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		ila = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static void ila_free_cb(void *ptr, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	ila_free_node((struct ila_map *)ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static int ila_xlat_addr(struct sk_buff *skb, bool sir2ila);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static unsigned int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) ila_nf_input(void *priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	     struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	     const struct nf_hook_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	ila_xlat_addr(skb, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	return NF_ACCEPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static const struct nf_hook_ops ila_nf_hook_ops[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		.hook = ila_nf_input,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		.pf = NFPROTO_IPV6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		.hooknum = NF_INET_PRE_ROUTING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		.priority = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static int ila_add_mapping(struct net *net, struct ila_xlat_params *xp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	struct ila_net *ilan = net_generic(net, ila_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	struct ila_map *ila, *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	spinlock_t *lock = ila_get_lock(ilan, xp->ip.locator_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	int err = 0, order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (!ilan->xlat.hooks_registered) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		/* We defer registering net hooks in the namespace until the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		 * first mapping is added.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		err = nf_register_net_hooks(net, ila_nf_hook_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 					    ARRAY_SIZE(ila_nf_hook_ops));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		ilan->xlat.hooks_registered = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	ila = kzalloc(sizeof(*ila), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	if (!ila)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	ila_init_saved_csum(&xp->ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	ila->xp = *xp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	order = ila_order(ila);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	spin_lock(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	head = rhashtable_lookup_fast(&ilan->xlat.rhash_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 				      &xp->ip.locator_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 				      rht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	if (!head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		/* New entry for the rhash_table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		err = rhashtable_lookup_insert_fast(&ilan->xlat.rhash_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 						    &ila->node, rht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		struct ila_map *tila = head, *prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			if (!ila_cmp_params(tila, xp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 				err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			if (order > ila_order(tila))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			prev = tila;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			tila = rcu_dereference_protected(tila->next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 				lockdep_is_held(lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		} while (tila);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		if (prev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			/* Insert in sub list of head */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 			RCU_INIT_POINTER(ila->next, tila);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			rcu_assign_pointer(prev->next, ila);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			/* Make this ila new head */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			RCU_INIT_POINTER(ila->next, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			err = rhashtable_replace_fast(&ilan->xlat.rhash_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 						      &head->node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 						      &ila->node, rht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	spin_unlock(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		kfree(ila);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static int ila_del_mapping(struct net *net, struct ila_xlat_params *xp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	struct ila_net *ilan = net_generic(net, ila_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	struct ila_map *ila, *head, *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	spinlock_t *lock = ila_get_lock(ilan, xp->ip.locator_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	int err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	spin_lock(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	head = rhashtable_lookup_fast(&ilan->xlat.rhash_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 				      &xp->ip.locator_match, rht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	ila = head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	while (ila) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		if (ila_cmp_params(ila, xp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 			prev = ila;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 			ila = rcu_dereference_protected(ila->next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 							lockdep_is_held(lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		if (prev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			/* Not head, just delete from list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			rcu_assign_pointer(prev->next, ila->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			/* It is the head. If there is something in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 			 * sublist we need to make a new head.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 			head = rcu_dereference_protected(ila->next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 							 lockdep_is_held(lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 			if (head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 				/* Put first entry in the sublist into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 				 * table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 				err = rhashtable_replace_fast(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 					&ilan->xlat.rhash_table, &ila->node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 					&head->node, rht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 				if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 				/* Entry no longer used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 				err = rhashtable_remove_fast(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 						&ilan->xlat.rhash_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 						&ila->node, rht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		ila_release(ila);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	spin_unlock(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) int ila_xlat_nl_cmd_add_mapping(struct sk_buff *skb, struct genl_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	struct net *net = genl_info_net(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	struct ila_xlat_params p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	err = parse_nl_config(info, &p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	return ila_add_mapping(net, &p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) int ila_xlat_nl_cmd_del_mapping(struct sk_buff *skb, struct genl_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	struct net *net = genl_info_net(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	struct ila_xlat_params xp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	err = parse_nl_config(info, &xp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	ila_del_mapping(net, &xp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) static inline spinlock_t *lock_from_ila_map(struct ila_net *ilan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 					    struct ila_map *ila)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	return ila_get_lock(ilan, ila->xp.ip.locator_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) int ila_xlat_nl_cmd_flush(struct sk_buff *skb, struct genl_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	struct net *net = genl_info_net(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	struct ila_net *ilan = net_generic(net, ila_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	struct rhashtable_iter iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	struct ila_map *ila;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	spinlock_t *lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	rhashtable_walk_enter(&ilan->xlat.rhash_table, &iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	rhashtable_walk_start(&iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		ila = rhashtable_walk_next(&iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		if (IS_ERR(ila)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 			if (PTR_ERR(ila) == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 			ret = PTR_ERR(ila);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		} else if (!ila) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		lock = lock_from_ila_map(ilan, ila);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		spin_lock(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		ret = rhashtable_remove_fast(&ilan->xlat.rhash_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 					     &ila->node, rht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 			ila_free_node(ila);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		spin_unlock(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	rhashtable_walk_stop(&iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	rhashtable_walk_exit(&iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) static int ila_fill_info(struct ila_map *ila, struct sk_buff *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	if (nla_put_u64_64bit(msg, ILA_ATTR_LOCATOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 			      (__force u64)ila->xp.ip.locator.v64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 			      ILA_ATTR_PAD) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	    nla_put_u64_64bit(msg, ILA_ATTR_LOCATOR_MATCH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 			      (__force u64)ila->xp.ip.locator_match.v64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 			      ILA_ATTR_PAD) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	    nla_put_s32(msg, ILA_ATTR_IFINDEX, ila->xp.ifindex) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	    nla_put_u8(msg, ILA_ATTR_CSUM_MODE, ila->xp.ip.csum_mode) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	    nla_put_u8(msg, ILA_ATTR_IDENT_TYPE, ila->xp.ip.ident_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) static int ila_dump_info(struct ila_map *ila,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 			 u32 portid, u32 seq, u32 flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 			 struct sk_buff *skb, u8 cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	void *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	hdr = genlmsg_put(skb, portid, seq, &ila_nl_family, flags, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	if (!hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	if (ila_fill_info(ila, skb) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	genlmsg_end(skb, hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	genlmsg_cancel(skb, hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) int ila_xlat_nl_cmd_get_mapping(struct sk_buff *skb, struct genl_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	struct net *net = genl_info_net(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	struct ila_net *ilan = net_generic(net, ila_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	struct sk_buff *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	struct ila_xlat_params xp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	struct ila_map *ila;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	ret = parse_nl_config(info, &xp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	if (!msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	ila = ila_lookup_by_params(&xp, ilan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	if (ila) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		ret = ila_dump_info(ila,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 				    info->snd_portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 				    info->snd_seq, 0, msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 				    info->genlhdr->cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	return genlmsg_reply(msg, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	nlmsg_free(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) struct ila_dump_iter {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	struct rhashtable_iter rhiter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	int skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) int ila_xlat_nl_dump_start(struct netlink_callback *cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	struct net *net = sock_net(cb->skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	struct ila_net *ilan = net_generic(net, ila_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	struct ila_dump_iter *iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	iter = kmalloc(sizeof(*iter), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	if (!iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	rhashtable_walk_enter(&ilan->xlat.rhash_table, &iter->rhiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	iter->skip = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	cb->args[0] = (long)iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) int ila_xlat_nl_dump_done(struct netlink_callback *cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	rhashtable_walk_exit(&iter->rhiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	kfree(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) int ila_xlat_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	struct rhashtable_iter *rhiter = &iter->rhiter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	int skip = iter->skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	struct ila_map *ila;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	rhashtable_walk_start(rhiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	/* Get first entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	ila = rhashtable_walk_peek(rhiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	if (ila && !IS_ERR(ila) && skip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		/* Skip over visited entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 		while (ila && skip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 			/* Skip over any ila entries in this list that we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 			 * have already dumped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 			ila = rcu_access_pointer(ila->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 			skip--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	skip = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 		if (IS_ERR(ila)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 			ret = PTR_ERR(ila);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 			if (ret == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 				/* Table has changed and iter has reset. Return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 				 * -EAGAIN to the application even if we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 				 * written data to the skb. The application
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 				 * needs to deal with this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 				goto out_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 		} else if (!ila) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		while (ila) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 			ret =  ila_dump_info(ila, NETLINK_CB(cb->skb).portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 					     cb->nlh->nlmsg_seq, NLM_F_MULTI,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 					     skb, ILA_CMD_GET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 			skip++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 			ila = rcu_access_pointer(ila->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		skip = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 		ila = rhashtable_walk_next(rhiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	iter->skip = skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	ret = (skb->len ? : ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) out_ret:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	rhashtable_walk_stop(rhiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) int ila_xlat_init_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	struct ila_net *ilan = net_generic(net, ila_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	err = alloc_ila_locks(ilan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	rhashtable_init(&ilan->xlat.rhash_table, &rht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) void ila_xlat_exit_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	struct ila_net *ilan = net_generic(net, ila_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	rhashtable_free_and_destroy(&ilan->xlat.rhash_table, ila_free_cb, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	free_bucket_spinlocks(ilan->xlat.locks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	if (ilan->xlat.hooks_registered)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 		nf_unregister_net_hooks(net, ila_nf_hook_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 					ARRAY_SIZE(ila_nf_hook_ops));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) static int ila_xlat_addr(struct sk_buff *skb, bool sir2ila)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	struct ila_map *ila;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	struct ipv6hdr *ip6h = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	struct net *net = dev_net(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	struct ila_net *ilan = net_generic(net, ila_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	struct ila_addr *iaddr = ila_a2i(&ip6h->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	/* Assumes skb contains a valid IPv6 header that is pulled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	/* No check here that ILA type in the mapping matches what is in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	 * address. We assume that whatever sender gaves us can be translated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	 * The checksum mode however is relevant.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	ila = ila_lookup_wildcards(iaddr, skb->dev->ifindex, ilan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	if (ila)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 		ila_update_ipv6_locator(skb, &ila->xp.ip, sir2ila);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) }