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-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * net/sched/cls_tcindex.c	Packet classifier for skb->tc_index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Written 1998,1999 by Werner Almesberger, EPFL ICA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/refcount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <net/act_api.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <net/pkt_cls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <net/sch_generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * Passing parameters to the root seems to be done more awkwardly than really
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * necessary. At least, u32 doesn't seem to use such dirty hacks. To be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * verified. FIXME.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define PERFECT_HASH_THRESHOLD	64	/* use perfect hash if not bigger */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define DEFAULT_HASH_SIZE	64	/* optimized for diffserv */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) struct tcindex_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) struct tcindex_filter_result {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct tcf_exts		exts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct tcf_result	res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct tcindex_data	*p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct rcu_work		rwork;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) struct tcindex_filter {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	u16 key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct tcindex_filter_result result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct tcindex_filter __rcu *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	struct rcu_work rwork;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) struct tcindex_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	struct tcindex_filter_result *perfect; /* perfect hash; NULL if none */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	struct tcindex_filter __rcu **h; /* imperfect hash; */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	struct tcf_proto *tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	u16 mask;		/* AND key with mask */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	u32 shift;		/* shift ANDed key to the right */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	u32 hash;		/* hash table size; 0 if undefined */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	u32 alloc_hash;		/* allocated size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	u32 fall_through;	/* 0: only classify if explicit match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	refcount_t refcnt;	/* a temporary refcnt for perfect hash */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct rcu_work rwork;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static inline int tcindex_filter_is_set(struct tcindex_filter_result *r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	return tcf_exts_has_actions(&r->exts) || r->res.classid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static void tcindex_data_get(struct tcindex_data *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	refcount_inc(&p->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static void tcindex_data_put(struct tcindex_data *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (refcount_dec_and_test(&p->refcnt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		kfree(p->perfect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		kfree(p->h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) static struct tcindex_filter_result *tcindex_lookup(struct tcindex_data *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 						    u16 key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	if (p->perfect) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		struct tcindex_filter_result *f = p->perfect + key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		return tcindex_filter_is_set(f) ? f : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	} else if (p->h) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		struct tcindex_filter __rcu **fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		struct tcindex_filter *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		fp = &p->h[key % p->hash];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		for (f = rcu_dereference_bh_rtnl(*fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		     f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		     fp = &f->next, f = rcu_dereference_bh_rtnl(*fp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			if (f->key == key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 				return &f->result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static int tcindex_classify(struct sk_buff *skb, const struct tcf_proto *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			    struct tcf_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	struct tcindex_data *p = rcu_dereference_bh(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	struct tcindex_filter_result *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	int key = (skb->tc_index & p->mask) >> p->shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	pr_debug("tcindex_classify(skb %p,tp %p,res %p),p %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		 skb, tp, res, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	f = tcindex_lookup(p, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (!f) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		struct Qdisc *q = tcf_block_q(tp->chain->block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		if (!p->fall_through)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		res->classid = TC_H_MAKE(TC_H_MAJ(q->handle), key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		res->class = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		pr_debug("alg 0x%x\n", res->classid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	*res = f->res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	pr_debug("map 0x%x\n", res->classid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	return tcf_exts_exec(skb, &f->exts, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static void *tcindex_get(struct tcf_proto *tp, u32 handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	struct tcindex_data *p = rtnl_dereference(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	struct tcindex_filter_result *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	pr_debug("tcindex_get(tp %p,handle 0x%08x)\n", tp, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (p->perfect && handle >= p->alloc_hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	r = tcindex_lookup(p, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	return r && tcindex_filter_is_set(r) ? r : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static int tcindex_init(struct tcf_proto *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	struct tcindex_data *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	pr_debug("tcindex_init(tp %p)\n", tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	p = kzalloc(sizeof(struct tcindex_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	p->mask = 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	p->hash = DEFAULT_HASH_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	p->fall_through = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	refcount_set(&p->refcnt, 1); /* Paired with tcindex_destroy_work() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	rcu_assign_pointer(tp->root, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static void __tcindex_destroy_rexts(struct tcindex_filter_result *r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	tcf_exts_destroy(&r->exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	tcf_exts_put_net(&r->exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	tcindex_data_put(r->p);
^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 tcindex_destroy_rexts_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	struct tcindex_filter_result *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	r = container_of(to_rcu_work(work),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			 struct tcindex_filter_result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			 rwork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	__tcindex_destroy_rexts(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	rtnl_unlock();
^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 __tcindex_destroy_fexts(struct tcindex_filter *f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	tcf_exts_destroy(&f->result.exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	tcf_exts_put_net(&f->result.exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	kfree(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static void tcindex_destroy_fexts_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	struct tcindex_filter *f = container_of(to_rcu_work(work),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 						struct tcindex_filter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 						rwork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	__tcindex_destroy_fexts(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static int tcindex_delete(struct tcf_proto *tp, void *arg, bool *last,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			  bool rtnl_held, struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	struct tcindex_data *p = rtnl_dereference(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	struct tcindex_filter_result *r = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	struct tcindex_filter __rcu **walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	struct tcindex_filter *f = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	pr_debug("tcindex_delete(tp %p,arg %p),p %p\n", tp, arg, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	if (p->perfect) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		if (!r->res.class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 			return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		for (i = 0; i < p->hash; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			walk = p->h + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			for (f = rtnl_dereference(*walk); f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			     walk = &f->next, f = rtnl_dereference(*walk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 				if (&f->result == r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 					goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		rcu_assign_pointer(*walk, rtnl_dereference(f->next));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	tcf_unbind_filter(tp, &r->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	/* all classifiers are required to call tcf_exts_destroy() after rcu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	 * grace period, since converted-to-rcu actions are relying on that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	 * in cleanup() callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	if (f) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		if (tcf_exts_get_net(&f->result.exts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			tcf_queue_work(&f->rwork, tcindex_destroy_fexts_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			__tcindex_destroy_fexts(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		tcindex_data_get(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		if (tcf_exts_get_net(&r->exts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			tcf_queue_work(&r->rwork, tcindex_destroy_rexts_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			__tcindex_destroy_rexts(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	*last = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static void tcindex_destroy_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	struct tcindex_data *p = container_of(to_rcu_work(work),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 					      struct tcindex_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 					      rwork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	tcindex_data_put(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) valid_perfect_hash(struct tcindex_data *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	return  p->hash > (p->mask >> p->shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static const struct nla_policy tcindex_policy[TCA_TCINDEX_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	[TCA_TCINDEX_HASH]		= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	[TCA_TCINDEX_MASK]		= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	[TCA_TCINDEX_SHIFT]		= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	[TCA_TCINDEX_FALL_THROUGH]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	[TCA_TCINDEX_CLASSID]		= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static int tcindex_filter_result_init(struct tcindex_filter_result *r,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 				      struct tcindex_data *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 				      struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	memset(r, 0, sizeof(*r));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	r->p = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	return tcf_exts_init(&r->exts, net, TCA_TCINDEX_ACT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			     TCA_TCINDEX_POLICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static void tcindex_free_perfect_hash(struct tcindex_data *cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static void tcindex_partial_destroy_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	struct tcindex_data *p = container_of(to_rcu_work(work),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 					      struct tcindex_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 					      rwork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	if (p->perfect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		tcindex_free_perfect_hash(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) static void tcindex_free_perfect_hash(struct tcindex_data *cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	for (i = 0; i < cp->hash; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		tcf_exts_destroy(&cp->perfect[i].exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	kfree(cp->perfect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static int tcindex_alloc_perfect_hash(struct net *net, struct tcindex_data *cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	int i, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	cp->perfect = kcalloc(cp->hash, sizeof(struct tcindex_filter_result),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			      GFP_KERNEL | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	if (!cp->perfect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	for (i = 0; i < cp->hash; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		err = tcf_exts_init(&cp->perfect[i].exts, net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 				    TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		cp->perfect[i].p = cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	tcindex_free_perfect_hash(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		  u32 handle, struct tcindex_data *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		  struct tcindex_filter_result *r, struct nlattr **tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		  struct nlattr *est, bool ovr, struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	struct tcindex_filter_result new_filter_result, *old_r = r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	struct tcindex_data *cp = NULL, *oldp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	struct tcindex_filter *f = NULL; /* make gcc behave */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	struct tcf_result cr = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	int err, balloc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	struct tcf_exts e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	err = tcf_exts_init(&e, net, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	err = tcf_exts_validate(net, tp, tb, est, &e, ovr, true, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	/* tcindex_data attributes must look atomic to classifier/lookup so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	 * allocate new tcindex data and RCU assign it onto root. Keeping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	 * perfect hash and hash pointers from old data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	cp = kzalloc(sizeof(*cp), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	if (!cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	cp->mask = p->mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	cp->shift = p->shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	cp->hash = p->hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	cp->alloc_hash = p->alloc_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	cp->fall_through = p->fall_through;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	cp->tp = tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	refcount_set(&cp->refcnt, 1); /* Paired with tcindex_destroy_work() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	if (tb[TCA_TCINDEX_HASH])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		cp->hash = nla_get_u32(tb[TCA_TCINDEX_HASH]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	if (tb[TCA_TCINDEX_MASK])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		cp->mask = nla_get_u16(tb[TCA_TCINDEX_MASK]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	if (tb[TCA_TCINDEX_SHIFT]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		cp->shift = nla_get_u32(tb[TCA_TCINDEX_SHIFT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		if (cp->shift > 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 			goto errout;
^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) 	if (!cp->hash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		/* Hash not specified, use perfect hash if the upper limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		 * of the hashing index is below the threshold.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		if ((cp->mask >> cp->shift) < PERFECT_HASH_THRESHOLD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 			cp->hash = (cp->mask >> cp->shift) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 			cp->hash = DEFAULT_HASH_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	if (p->perfect) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		if (tcindex_alloc_perfect_hash(net, cp) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		cp->alloc_hash = cp->hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		for (i = 0; i < min(cp->hash, p->hash); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 			cp->perfect[i].res = p->perfect[i].res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		balloc = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	cp->h = p->h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	err = tcindex_filter_result_init(&new_filter_result, cp, net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		goto errout_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	if (old_r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		cr = r->res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	/* Hash already allocated, make sure that we still meet the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	 * requirements for the allocated hash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	if (cp->perfect) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		if (!valid_perfect_hash(cp) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		    cp->hash > cp->alloc_hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 			goto errout_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	} else if (cp->h && cp->hash != cp->alloc_hash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		goto errout_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	if (tb[TCA_TCINDEX_FALL_THROUGH])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		cp->fall_through = nla_get_u32(tb[TCA_TCINDEX_FALL_THROUGH]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	if (!cp->perfect && !cp->h)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		cp->alloc_hash = cp->hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	/* Note: this could be as restrictive as if (handle & ~(mask >> shift))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	 * but then, we'd fail handles that may become valid after some future
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	 * mask change. While this is extremely unlikely to ever matter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	 * the check below is safer (and also more backwards-compatible).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	if (cp->perfect || valid_perfect_hash(cp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		if (handle >= cp->alloc_hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 			goto errout_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	if (!cp->perfect && !cp->h) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		if (valid_perfect_hash(cp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 			if (tcindex_alloc_perfect_hash(net, cp) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 				goto errout_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 			balloc = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 			struct tcindex_filter __rcu **hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 			hash = kcalloc(cp->hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 				       sizeof(struct tcindex_filter *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 				       GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 			if (!hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 				goto errout_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 			cp->h = hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 			balloc = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	if (cp->perfect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		r = cp->perfect + handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		r = tcindex_lookup(cp, handle) ? : &new_filter_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	if (r == &new_filter_result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		f = kzalloc(sizeof(*f), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		if (!f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 			goto errout_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		f->key = handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		f->next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		err = tcindex_filter_result_init(&f->result, cp, net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 			kfree(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 			goto errout_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	if (tb[TCA_TCINDEX_CLASSID]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		cr.classid = nla_get_u32(tb[TCA_TCINDEX_CLASSID]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		tcf_bind_filter(tp, &cr, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	if (old_r && old_r != r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		err = tcindex_filter_result_init(old_r, cp, net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 			kfree(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 			goto errout_alloc;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	oldp = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	r->res = cr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	tcf_exts_change(&r->exts, &e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	rcu_assign_pointer(tp->root, cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	if (r == &new_filter_result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		struct tcindex_filter *nfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		struct tcindex_filter __rcu **fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		f->result.res = r->res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		tcf_exts_change(&f->result.exts, &r->exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		fp = cp->h + (handle % cp->hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		for (nfp = rtnl_dereference(*fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		     nfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		     fp = &nfp->next, nfp = rtnl_dereference(*fp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 				; /* nothing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		rcu_assign_pointer(*fp, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		tcf_exts_destroy(&new_filter_result.exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	if (oldp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		tcf_queue_work(&oldp->rwork, tcindex_partial_destroy_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) errout_alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	if (balloc == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		tcindex_free_perfect_hash(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	else if (balloc == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		kfree(cp->h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	tcf_exts_destroy(&new_filter_result.exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	kfree(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	tcf_exts_destroy(&e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) tcindex_change(struct net *net, struct sk_buff *in_skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	       struct tcf_proto *tp, unsigned long base, u32 handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	       struct nlattr **tca, void **arg, bool ovr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	       bool rtnl_held, struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	struct nlattr *opt = tca[TCA_OPTIONS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	struct nlattr *tb[TCA_TCINDEX_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	struct tcindex_data *p = rtnl_dereference(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	struct tcindex_filter_result *r = *arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	pr_debug("tcindex_change(tp %p,handle 0x%08x,tca %p,arg %p),opt %p,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	    "p %p,r %p,*arg %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	    tp, handle, tca, arg, opt, p, r, *arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	if (!opt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	err = nla_parse_nested_deprecated(tb, TCA_TCINDEX_MAX, opt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 					  tcindex_policy, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	return tcindex_set_parms(net, tp, base, handle, p, r, tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 				 tca[TCA_RATE], ovr, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) static void tcindex_walk(struct tcf_proto *tp, struct tcf_walker *walker,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 			 bool rtnl_held)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	struct tcindex_data *p = rtnl_dereference(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	struct tcindex_filter *f, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	pr_debug("tcindex_walk(tp %p,walker %p),p %p\n", tp, walker, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	if (p->perfect) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		for (i = 0; i < p->hash; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 			if (!p->perfect[i].res.class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 			if (walker->count >= walker->skip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 				if (walker->fn(tp, p->perfect + i, walker) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 					walker->stop = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 					return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 			walker->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	if (!p->h)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	for (i = 0; i < p->hash; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		for (f = rtnl_dereference(p->h[i]); f; f = next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 			next = rtnl_dereference(f->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 			if (walker->count >= walker->skip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 				if (walker->fn(tp, &f->result, walker) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 					walker->stop = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 					return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 			walker->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) static void tcindex_destroy(struct tcf_proto *tp, bool rtnl_held,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 			    struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	struct tcindex_data *p = rtnl_dereference(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	pr_debug("tcindex_destroy(tp %p),p %p\n", tp, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	if (p->perfect) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 		for (i = 0; i < p->hash; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 			struct tcindex_filter_result *r = p->perfect + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 			/* tcf_queue_work() does not guarantee the ordering we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 			 * want, so we have to take this refcnt temporarily to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 			 * ensure 'p' is freed after all tcindex_filter_result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 			 * here. Imperfect hash does not need this, because it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 			 * uses linked lists rather than an array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 			tcindex_data_get(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 			tcf_unbind_filter(tp, &r->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 			if (tcf_exts_get_net(&r->exts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 				tcf_queue_work(&r->rwork,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 					       tcindex_destroy_rexts_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 				__tcindex_destroy_rexts(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	for (i = 0; p->h && i < p->hash; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 		struct tcindex_filter *f, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 		bool last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 		for (f = rtnl_dereference(p->h[i]); f; f = next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 			next = rtnl_dereference(f->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 			tcindex_delete(tp, &f->result, &last, rtnl_held, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	tcf_queue_work(&p->rwork, tcindex_destroy_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) static int tcindex_dump(struct net *net, struct tcf_proto *tp, void *fh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 			struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	struct tcindex_data *p = rtnl_dereference(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	struct tcindex_filter_result *r = fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	struct nlattr *nest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	pr_debug("tcindex_dump(tp %p,fh %p,skb %p,t %p),p %p,r %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 		 tp, fh, skb, t, p, r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	pr_debug("p->perfect %p p->h %p\n", p->perfect, p->h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	if (nest == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	if (!fh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 		t->tcm_handle = ~0; /* whatever ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 		if (nla_put_u32(skb, TCA_TCINDEX_HASH, p->hash) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 		    nla_put_u16(skb, TCA_TCINDEX_MASK, p->mask) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 		    nla_put_u32(skb, TCA_TCINDEX_SHIFT, p->shift) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 		    nla_put_u32(skb, TCA_TCINDEX_FALL_THROUGH, p->fall_through))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 			goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 		nla_nest_end(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 		if (p->perfect) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 			t->tcm_handle = r - p->perfect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 			struct tcindex_filter *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 			struct tcindex_filter __rcu **fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 			int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 			t->tcm_handle = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 			for (i = 0; !t->tcm_handle && i < p->hash; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 				fp = &p->h[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 				for (f = rtnl_dereference(*fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 				     !t->tcm_handle && f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 				     fp = &f->next, f = rtnl_dereference(*fp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 					if (&f->result == r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 						t->tcm_handle = f->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 		pr_debug("handle = %d\n", t->tcm_handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 		if (r->res.class &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 		    nla_put_u32(skb, TCA_TCINDEX_CLASSID, r->res.classid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 			goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 		if (tcf_exts_dump(skb, &r->exts) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 			goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 		nla_nest_end(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 		if (tcf_exts_dump_stats(skb, &r->exts) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 			goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 	nla_nest_cancel(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) static void tcindex_bind_class(void *fh, u32 classid, unsigned long cl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 			       void *q, unsigned long base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	struct tcindex_filter_result *r = fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	if (r && r->res.classid == classid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 		if (cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 			__tcf_bind_filter(q, &r->res, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 			__tcf_unbind_filter(q, &r->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) static struct tcf_proto_ops cls_tcindex_ops __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	.kind		=	"tcindex",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 	.classify	=	tcindex_classify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	.init		=	tcindex_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	.destroy	=	tcindex_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	.get		=	tcindex_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	.change		=	tcindex_change,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	.delete		=	tcindex_delete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	.walk		=	tcindex_walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	.dump		=	tcindex_dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	.bind_class	=	tcindex_bind_class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	.owner		=	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) static int __init init_tcindex(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	return register_tcf_proto_ops(&cls_tcindex_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) static void __exit exit_tcindex(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	unregister_tcf_proto_ops(&cls_tcindex_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) module_init(init_tcindex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) module_exit(exit_tcindex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) MODULE_LICENSE("GPL");