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-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * net/sched/cls_basic.c	Basic Packet Classifier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Authors:	Thomas Graf <tgraf@suug.ch>
^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/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/percpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <net/act_api.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <net/pkt_cls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) struct basic_head {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	struct list_head	flist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	struct idr		handle_idr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct rcu_head		rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) struct basic_filter {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	u32			handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct tcf_exts		exts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	struct tcf_ematch_tree	ematches;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	struct tcf_result	res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct tcf_proto	*tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct list_head	link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct tc_basic_pcnt __percpu *pf;
^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) static int basic_classify(struct sk_buff *skb, const struct tcf_proto *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 			  struct tcf_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	struct basic_head *head = rcu_dereference_bh(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct basic_filter *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	list_for_each_entry_rcu(f, &head->flist, link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		__this_cpu_inc(f->pf->rcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		if (!tcf_em_tree_match(skb, &f->ematches, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		__this_cpu_inc(f->pf->rhit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		*res = f->res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		r = tcf_exts_exec(skb, &f->exts, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	return -1;
^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 void *basic_get(struct tcf_proto *tp, u32 handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	struct basic_head *head = rtnl_dereference(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	struct basic_filter *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	list_for_each_entry(f, &head->flist, link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		if (f->handle == handle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			return f;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) static int basic_init(struct tcf_proto *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct basic_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	head = kzalloc(sizeof(*head), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	if (head == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	INIT_LIST_HEAD(&head->flist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	idr_init(&head->handle_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	rcu_assign_pointer(tp->root, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) static void __basic_delete_filter(struct basic_filter *f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	tcf_exts_destroy(&f->exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	tcf_em_tree_destroy(&f->ematches);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	tcf_exts_put_net(&f->exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	free_percpu(f->pf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	kfree(f);
^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 void basic_delete_filter_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	struct basic_filter *f = container_of(to_rcu_work(work),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 					      struct basic_filter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 					      rwork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	__basic_delete_filter(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static void basic_destroy(struct tcf_proto *tp, bool rtnl_held,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			  struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct basic_head *head = rtnl_dereference(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct basic_filter *f, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	list_for_each_entry_safe(f, n, &head->flist, link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		list_del_rcu(&f->link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		tcf_unbind_filter(tp, &f->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		idr_remove(&head->handle_idr, f->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		if (tcf_exts_get_net(&f->exts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			tcf_queue_work(&f->rwork, basic_delete_filter_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 			__basic_delete_filter(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	idr_destroy(&head->handle_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	kfree_rcu(head, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static int basic_delete(struct tcf_proto *tp, void *arg, bool *last,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			bool rtnl_held, struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	struct basic_head *head = rtnl_dereference(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	struct basic_filter *f = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	list_del_rcu(&f->link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	tcf_unbind_filter(tp, &f->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	idr_remove(&head->handle_idr, f->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	tcf_exts_get_net(&f->exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	tcf_queue_work(&f->rwork, basic_delete_filter_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	*last = list_empty(&head->flist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	[TCA_BASIC_CLASSID]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	[TCA_BASIC_EMATCHES]	= { .type = NLA_NESTED },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static int basic_set_parms(struct net *net, struct tcf_proto *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			   struct basic_filter *f, unsigned long base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			   struct nlattr **tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			   struct nlattr *est, bool ovr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			   struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, true, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &f->ematches);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	if (tb[TCA_BASIC_CLASSID]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		tcf_bind_filter(tp, &f->res, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	f->tp = tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static int basic_change(struct net *net, struct sk_buff *in_skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			struct tcf_proto *tp, unsigned long base, u32 handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			struct nlattr **tca, void **arg, bool ovr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			bool rtnl_held, struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	struct basic_head *head = rtnl_dereference(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	struct nlattr *tb[TCA_BASIC_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	struct basic_filter *fold = (struct basic_filter *) *arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	struct basic_filter *fnew;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (tca[TCA_OPTIONS] == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	err = nla_parse_nested_deprecated(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 					  basic_policy, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (fold != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		if (handle && fold->handle != handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	if (!fnew)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	err = tcf_exts_init(&fnew->exts, net, TCA_BASIC_ACT, TCA_BASIC_POLICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	if (!handle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		handle = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 				    INT_MAX, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	} else if (!fold) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 				    handle, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	fnew->handle = handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	fnew->pf = alloc_percpu(struct tc_basic_pcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	if (!fnew->pf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			      extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		if (!fold)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 			idr_remove(&head->handle_idr, fnew->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	*arg = fnew;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	if (fold) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		idr_replace(&head->handle_idr, fnew, fnew->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		list_replace_rcu(&fold->link, &fnew->link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		tcf_unbind_filter(tp, &fold->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		tcf_exts_get_net(&fold->exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		tcf_queue_work(&fold->rwork, basic_delete_filter_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		list_add_rcu(&fnew->link, &head->flist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	free_percpu(fnew->pf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	tcf_exts_destroy(&fnew->exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	kfree(fnew);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		       bool rtnl_held)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	struct basic_head *head = rtnl_dereference(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	struct basic_filter *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	list_for_each_entry(f, &head->flist, link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		if (arg->count < arg->skip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			goto skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		if (arg->fn(tp, f, arg) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			arg->stop = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) skip:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		arg->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) static void basic_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			     unsigned long base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	struct basic_filter *f = fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	if (f && f->res.classid == classid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		if (cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 			__tcf_bind_filter(q, &f->res, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			__tcf_unbind_filter(q, &f->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		      struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	struct tc_basic_pcnt gpf = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	struct basic_filter *f = fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	struct nlattr *nest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	if (f == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	t->tcm_handle = f->handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	if (nest == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	if (f->res.classid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	    nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		struct tc_basic_pcnt *pf = per_cpu_ptr(f->pf, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		gpf.rcnt += pf->rcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		gpf.rhit += pf->rhit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	if (nla_put_64bit(skb, TCA_BASIC_PCNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			  sizeof(struct tc_basic_pcnt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			  &gpf, TCA_BASIC_PAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	if (tcf_exts_dump(skb, &f->exts) < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	    tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	nla_nest_end(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	if (tcf_exts_dump_stats(skb, &f->exts) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	nla_nest_cancel(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static struct tcf_proto_ops cls_basic_ops __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	.kind		=	"basic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	.classify	=	basic_classify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	.init		=	basic_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	.destroy	=	basic_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	.get		=	basic_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	.change		=	basic_change,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	.delete		=	basic_delete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	.walk		=	basic_walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	.dump		=	basic_dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	.bind_class	=	basic_bind_class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	.owner		=	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) static int __init init_basic(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	return register_tcf_proto_ops(&cls_basic_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) static void __exit exit_basic(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	unregister_tcf_proto_ops(&cls_basic_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) module_init(init_basic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) module_exit(exit_basic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) MODULE_LICENSE("GPL");