xref: /openbmc/linux/net/sched/cls_fw.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * net/sched/cls_fw.c	Classifier mapping ipchains' fwmark to traffic class.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Changes:
81da177e4SLinus Torvalds  * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_walk off by one
91da177e4SLinus Torvalds  * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_delete killed all the filter (and kernel).
101da177e4SLinus Torvalds  * Alex <alex@pilotsoft.com> : 2004xxyy: Added Action extension
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #include <linux/module.h>
145a0e3ad6STejun Heo #include <linux/slab.h>
151da177e4SLinus Torvalds #include <linux/types.h>
161da177e4SLinus Torvalds #include <linux/kernel.h>
171da177e4SLinus Torvalds #include <linux/string.h>
181da177e4SLinus Torvalds #include <linux/errno.h>
191da177e4SLinus Torvalds #include <linux/skbuff.h>
200ba48053SPatrick McHardy #include <net/netlink.h>
211da177e4SLinus Torvalds #include <net/act_api.h>
221da177e4SLinus Torvalds #include <net/pkt_cls.h>
231abf2720SJiri Pirko #include <net/sch_generic.h>
249f3101dcSPedro Tammela #include <net/tc_wrapper.h>
251da177e4SLinus Torvalds 
26d37d8ac1SEric Dumazet #define HTSIZE 256
27c5c13fafSThomas Graf 
28cc7ec456SEric Dumazet struct fw_head {
29b4e9b520SPatrick McHardy 	u32			mask;
30e35a8ee5SJohn Fastabend 	struct fw_filter __rcu	*ht[HTSIZE];
31e35a8ee5SJohn Fastabend 	struct rcu_head		rcu;
321da177e4SLinus Torvalds };
331da177e4SLinus Torvalds 
34cc7ec456SEric Dumazet struct fw_filter {
35e35a8ee5SJohn Fastabend 	struct fw_filter __rcu	*next;
361da177e4SLinus Torvalds 	u32			id;
371da177e4SLinus Torvalds 	struct tcf_result	res;
382519a602SWANG Cong 	int			ifindex;
391da177e4SLinus Torvalds 	struct tcf_exts		exts;
40e35a8ee5SJohn Fastabend 	struct tcf_proto	*tp;
41aaa908ffSCong Wang 	struct rcu_work		rwork;
42e071dff2SCong Wang };
431da177e4SLinus Torvalds 
fw_hash(u32 handle)44d37d8ac1SEric Dumazet static u32 fw_hash(u32 handle)
451da177e4SLinus Torvalds {
46d37d8ac1SEric Dumazet 	handle ^= (handle >> 16);
47d37d8ac1SEric Dumazet 	handle ^= (handle >> 8);
48d37d8ac1SEric Dumazet 	return handle % HTSIZE;
491da177e4SLinus Torvalds }
501da177e4SLinus Torvalds 
fw_classify(struct sk_buff * skb,const struct tcf_proto * tp,struct tcf_result * res)519f3101dcSPedro Tammela TC_INDIRECT_SCOPE int fw_classify(struct sk_buff *skb,
529f3101dcSPedro Tammela 				  const struct tcf_proto *tp,
531da177e4SLinus Torvalds 				  struct tcf_result *res)
541da177e4SLinus Torvalds {
55e35a8ee5SJohn Fastabend 	struct fw_head *head = rcu_dereference_bh(tp->root);
561da177e4SLinus Torvalds 	struct fw_filter *f;
571da177e4SLinus Torvalds 	int r;
585c804bfdSPatrick McHardy 	u32 id = skb->mark;
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds 	if (head != NULL) {
615c804bfdSPatrick McHardy 		id &= head->mask;
62e35a8ee5SJohn Fastabend 
63e35a8ee5SJohn Fastabend 		for (f = rcu_dereference_bh(head->ht[fw_hash(id)]); f;
64e35a8ee5SJohn Fastabend 		     f = rcu_dereference_bh(f->next)) {
651da177e4SLinus Torvalds 			if (f->id == id) {
661da177e4SLinus Torvalds 				*res = f->res;
672519a602SWANG Cong 				if (!tcf_match_indev(skb, f->ifindex))
681da177e4SLinus Torvalds 					continue;
691da177e4SLinus Torvalds 				r = tcf_exts_exec(skb, &f->exts, res);
701da177e4SLinus Torvalds 				if (r < 0)
711da177e4SLinus Torvalds 					continue;
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 				return r;
741da177e4SLinus Torvalds 			}
751da177e4SLinus Torvalds 		}
761da177e4SLinus Torvalds 	} else {
771abf2720SJiri Pirko 		struct Qdisc *q = tcf_block_q(tp->chain->block);
781abf2720SJiri Pirko 
79d8aecb10SWANG Cong 		/* Old method: classify the packet using its skb mark. */
80cc7ec456SEric Dumazet 		if (id && (TC_H_MAJ(id) == 0 ||
811abf2720SJiri Pirko 			   !(TC_H_MAJ(id ^ q->handle)))) {
821da177e4SLinus Torvalds 			res->classid = id;
831da177e4SLinus Torvalds 			res->class = 0;
841da177e4SLinus Torvalds 			return 0;
851da177e4SLinus Torvalds 		}
861da177e4SLinus Torvalds 	}
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	return -1;
891da177e4SLinus Torvalds }
901da177e4SLinus Torvalds 
fw_get(struct tcf_proto * tp,u32 handle)918113c095SWANG Cong static void *fw_get(struct tcf_proto *tp, u32 handle)
921da177e4SLinus Torvalds {
93e35a8ee5SJohn Fastabend 	struct fw_head *head = rtnl_dereference(tp->root);
941da177e4SLinus Torvalds 	struct fw_filter *f;
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	if (head == NULL)
978113c095SWANG Cong 		return NULL;
981da177e4SLinus Torvalds 
99e35a8ee5SJohn Fastabend 	f = rtnl_dereference(head->ht[fw_hash(handle)]);
100e35a8ee5SJohn Fastabend 	for (; f; f = rtnl_dereference(f->next)) {
1011da177e4SLinus Torvalds 		if (f->id == handle)
1028113c095SWANG Cong 			return f;
1031da177e4SLinus Torvalds 	}
1048113c095SWANG Cong 	return NULL;
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds 
fw_init(struct tcf_proto * tp)1071da177e4SLinus Torvalds static int fw_init(struct tcf_proto *tp)
1081da177e4SLinus Torvalds {
109d8aecb10SWANG Cong 	/* We don't allocate fw_head here, because in the old method
110d8aecb10SWANG Cong 	 * we don't need it at all.
111d8aecb10SWANG Cong 	 */
1121da177e4SLinus Torvalds 	return 0;
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
__fw_delete_filter(struct fw_filter * f)115d5f984f5SCong Wang static void __fw_delete_filter(struct fw_filter *f)
116d5f984f5SCong Wang {
117d5f984f5SCong Wang 	tcf_exts_destroy(&f->exts);
118d5f984f5SCong Wang 	tcf_exts_put_net(&f->exts);
119d5f984f5SCong Wang 	kfree(f);
120d5f984f5SCong Wang }
121d5f984f5SCong Wang 
fw_delete_filter_work(struct work_struct * work)122e071dff2SCong Wang static void fw_delete_filter_work(struct work_struct *work)
123e071dff2SCong Wang {
124aaa908ffSCong Wang 	struct fw_filter *f = container_of(to_rcu_work(work),
125aaa908ffSCong Wang 					   struct fw_filter,
126aaa908ffSCong Wang 					   rwork);
127e071dff2SCong Wang 	rtnl_lock();
128d5f984f5SCong Wang 	__fw_delete_filter(f);
129e071dff2SCong Wang 	rtnl_unlock();
130e071dff2SCong Wang }
131e071dff2SCong Wang 
fw_destroy(struct tcf_proto * tp,bool rtnl_held,struct netlink_ext_ack * extack)13212db03b6SVlad Buslov static void fw_destroy(struct tcf_proto *tp, bool rtnl_held,
13312db03b6SVlad Buslov 		       struct netlink_ext_ack *extack)
1341da177e4SLinus Torvalds {
135e35a8ee5SJohn Fastabend 	struct fw_head *head = rtnl_dereference(tp->root);
1361da177e4SLinus Torvalds 	struct fw_filter *f;
1371da177e4SLinus Torvalds 	int h;
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	if (head == NULL)
140763dbf63SWANG Cong 		return;
1411da177e4SLinus Torvalds 
142c5c13fafSThomas Graf 	for (h = 0; h < HTSIZE; h++) {
143e35a8ee5SJohn Fastabend 		while ((f = rtnl_dereference(head->ht[h])) != NULL) {
144e35a8ee5SJohn Fastabend 			RCU_INIT_POINTER(head->ht[h],
145e35a8ee5SJohn Fastabend 					 rtnl_dereference(f->next));
14618cdb37eSJohn Fastabend 			tcf_unbind_filter(tp, &f->res);
147d5f984f5SCong Wang 			if (tcf_exts_get_net(&f->exts))
148aaa908ffSCong Wang 				tcf_queue_work(&f->rwork, fw_delete_filter_work);
149d5f984f5SCong Wang 			else
150d5f984f5SCong Wang 				__fw_delete_filter(f);
1511da177e4SLinus Torvalds 		}
1521da177e4SLinus Torvalds 	}
153e35a8ee5SJohn Fastabend 	kfree_rcu(head, rcu);
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
fw_delete(struct tcf_proto * tp,void * arg,bool * last,bool rtnl_held,struct netlink_ext_ack * extack)156571acf21SAlexander Aring static int fw_delete(struct tcf_proto *tp, void *arg, bool *last,
15712db03b6SVlad Buslov 		     bool rtnl_held, struct netlink_ext_ack *extack)
1581da177e4SLinus Torvalds {
159e35a8ee5SJohn Fastabend 	struct fw_head *head = rtnl_dereference(tp->root);
1608113c095SWANG Cong 	struct fw_filter *f = arg;
161e35a8ee5SJohn Fastabend 	struct fw_filter __rcu **fp;
162e35a8ee5SJohn Fastabend 	struct fw_filter *pfp;
163763dbf63SWANG Cong 	int ret = -EINVAL;
164763dbf63SWANG Cong 	int h;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	if (head == NULL || f == NULL)
1671da177e4SLinus Torvalds 		goto out;
1681da177e4SLinus Torvalds 
169e35a8ee5SJohn Fastabend 	fp = &head->ht[fw_hash(f->id)];
170e35a8ee5SJohn Fastabend 
171e35a8ee5SJohn Fastabend 	for (pfp = rtnl_dereference(*fp); pfp;
172e35a8ee5SJohn Fastabend 	     fp = &pfp->next, pfp = rtnl_dereference(*fp)) {
173e35a8ee5SJohn Fastabend 		if (pfp == f) {
174e35a8ee5SJohn Fastabend 			RCU_INIT_POINTER(*fp, rtnl_dereference(f->next));
17518cdb37eSJohn Fastabend 			tcf_unbind_filter(tp, &f->res);
176d5f984f5SCong Wang 			tcf_exts_get_net(&f->exts);
177aaa908ffSCong Wang 			tcf_queue_work(&f->rwork, fw_delete_filter_work);
178763dbf63SWANG Cong 			ret = 0;
179763dbf63SWANG Cong 			break;
1801da177e4SLinus Torvalds 		}
1811da177e4SLinus Torvalds 	}
182763dbf63SWANG Cong 
183763dbf63SWANG Cong 	*last = true;
184763dbf63SWANG Cong 	for (h = 0; h < HTSIZE; h++) {
185763dbf63SWANG Cong 		if (rcu_access_pointer(head->ht[h])) {
186763dbf63SWANG Cong 			*last = false;
187763dbf63SWANG Cong 			break;
188763dbf63SWANG Cong 		}
189763dbf63SWANG Cong 	}
190763dbf63SWANG Cong 
1911da177e4SLinus Torvalds out:
192763dbf63SWANG Cong 	return ret;
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds 
1956fa8c014SPatrick McHardy static const struct nla_policy fw_policy[TCA_FW_MAX + 1] = {
1966fa8c014SPatrick McHardy 	[TCA_FW_CLASSID]	= { .type = NLA_U32 },
1976fa8c014SPatrick McHardy 	[TCA_FW_INDEV]		= { .type = NLA_STRING, .len = IFNAMSIZ },
1986fa8c014SPatrick McHardy 	[TCA_FW_MASK]		= { .type = NLA_U32 },
1996fa8c014SPatrick McHardy };
2006fa8c014SPatrick McHardy 
fw_set_parms(struct net * net,struct tcf_proto * tp,struct fw_filter * f,struct nlattr ** tb,struct nlattr ** tca,unsigned long base,u32 flags,struct netlink_ext_ack * extack)2011e5003afSJiri Pirko static int fw_set_parms(struct net *net, struct tcf_proto *tp,
2021e5003afSJiri Pirko 			struct fw_filter *f, struct nlattr **tb,
203695176bfSCong Wang 			struct nlattr **tca, unsigned long base, u32 flags,
20450a56190SAlexander Aring 			struct netlink_ext_ack *extack)
2051da177e4SLinus Torvalds {
206e35a8ee5SJohn Fastabend 	struct fw_head *head = rtnl_dereference(tp->root);
207b4e9b520SPatrick McHardy 	u32 mask;
2081da177e4SLinus Torvalds 	int err;
2091da177e4SLinus Torvalds 
210695176bfSCong Wang 	err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &f->exts, flags,
211695176bfSCong Wang 				extack);
2121da177e4SLinus Torvalds 	if (err < 0)
2131da177e4SLinus Torvalds 		return err;
2141da177e4SLinus Torvalds 
215add93b61SPatrick McHardy 	if (tb[TCA_FW_INDEV]) {
2162519a602SWANG Cong 		int ret;
2171057c55fSAlexander Aring 		ret = tcf_change_indev(net, tb[TCA_FW_INDEV], extack);
21894611bffSJiri Pirko 		if (ret < 0)
21994611bffSJiri Pirko 			return ret;
2202519a602SWANG Cong 		f->ifindex = ret;
2211da177e4SLinus Torvalds 	}
2221da177e4SLinus Torvalds 
223cb95ec62SWei Yongjun 	err = -EINVAL;
224add93b61SPatrick McHardy 	if (tb[TCA_FW_MASK]) {
2251587bac4SPatrick McHardy 		mask = nla_get_u32(tb[TCA_FW_MASK]);
226b4e9b520SPatrick McHardy 		if (mask != head->mask)
22794611bffSJiri Pirko 			return err;
228b4e9b520SPatrick McHardy 	} else if (head->mask != 0xFFFFFFFF)
22994611bffSJiri Pirko 		return err;
2301da177e4SLinus Torvalds 
231*0323bce5SM A Ramdhan 	if (tb[TCA_FW_CLASSID]) {
232*0323bce5SM A Ramdhan 		f->res.classid = nla_get_u32(tb[TCA_FW_CLASSID]);
233*0323bce5SM A Ramdhan 		tcf_bind_filter(tp, &f->res, base);
234*0323bce5SM A Ramdhan 	}
235*0323bce5SM A Ramdhan 
2361da177e4SLinus Torvalds 	return 0;
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds 
fw_change(struct net * net,struct sk_buff * in_skb,struct tcf_proto * tp,unsigned long base,u32 handle,struct nlattr ** tca,void ** arg,u32 flags,struct netlink_ext_ack * extack)239c1b52739SBenjamin LaHaise static int fw_change(struct net *net, struct sk_buff *in_skb,
240af4c6641SEric W. Biederman 		     struct tcf_proto *tp, unsigned long base,
2418113c095SWANG Cong 		     u32 handle, struct nlattr **tca, void **arg,
242695176bfSCong Wang 		     u32 flags, struct netlink_ext_ack *extack)
2431da177e4SLinus Torvalds {
244e35a8ee5SJohn Fastabend 	struct fw_head *head = rtnl_dereference(tp->root);
2458113c095SWANG Cong 	struct fw_filter *f = *arg;
246add93b61SPatrick McHardy 	struct nlattr *opt = tca[TCA_OPTIONS];
247add93b61SPatrick McHardy 	struct nlattr *tb[TCA_FW_MAX + 1];
2481da177e4SLinus Torvalds 	int err;
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	if (!opt)
251d8aecb10SWANG Cong 		return handle ? -EINVAL : 0; /* Succeed if it is old method. */
2521da177e4SLinus Torvalds 
2538cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(tb, TCA_FW_MAX, opt, fw_policy,
2548cb08174SJohannes Berg 					  NULL);
255cee63723SPatrick McHardy 	if (err < 0)
256cee63723SPatrick McHardy 		return err;
2571da177e4SLinus Torvalds 
258e35a8ee5SJohn Fastabend 	if (f) {
259e35a8ee5SJohn Fastabend 		struct fw_filter *pfp, *fnew;
260e35a8ee5SJohn Fastabend 		struct fw_filter __rcu **fp;
261e35a8ee5SJohn Fastabend 
2621da177e4SLinus Torvalds 		if (f->id != handle && handle)
2631da177e4SLinus Torvalds 			return -EINVAL;
264e35a8ee5SJohn Fastabend 
265e35a8ee5SJohn Fastabend 		fnew = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
266e35a8ee5SJohn Fastabend 		if (!fnew)
267e35a8ee5SJohn Fastabend 			return -ENOBUFS;
268e35a8ee5SJohn Fastabend 
269e35a8ee5SJohn Fastabend 		fnew->id = f->id;
270e35a8ee5SJohn Fastabend 		fnew->ifindex = f->ifindex;
271e35a8ee5SJohn Fastabend 		fnew->tp = f->tp;
272e35a8ee5SJohn Fastabend 
27314215108SCong Wang 		err = tcf_exts_init(&fnew->exts, net, TCA_FW_ACT,
27414215108SCong Wang 				    TCA_FW_POLICE);
275b9a24bb7SWANG Cong 		if (err < 0) {
276b9a24bb7SWANG Cong 			kfree(fnew);
277b9a24bb7SWANG Cong 			return err;
278b9a24bb7SWANG Cong 		}
279e1f93eb0SJohn Fastabend 
280695176bfSCong Wang 		err = fw_set_parms(net, tp, fnew, tb, tca, base, flags, extack);
281e35a8ee5SJohn Fastabend 		if (err < 0) {
282b9a24bb7SWANG Cong 			tcf_exts_destroy(&fnew->exts);
283e35a8ee5SJohn Fastabend 			kfree(fnew);
284e35a8ee5SJohn Fastabend 			return err;
285e35a8ee5SJohn Fastabend 		}
286e35a8ee5SJohn Fastabend 
287e35a8ee5SJohn Fastabend 		fp = &head->ht[fw_hash(fnew->id)];
288e35a8ee5SJohn Fastabend 		for (pfp = rtnl_dereference(*fp); pfp;
289e35a8ee5SJohn Fastabend 		     fp = &pfp->next, pfp = rtnl_dereference(*fp))
290e35a8ee5SJohn Fastabend 			if (pfp == f)
291e35a8ee5SJohn Fastabend 				break;
292e35a8ee5SJohn Fastabend 
293e35a8ee5SJohn Fastabend 		RCU_INIT_POINTER(fnew->next, rtnl_dereference(pfp->next));
294e35a8ee5SJohn Fastabend 		rcu_assign_pointer(*fp, fnew);
29518cdb37eSJohn Fastabend 		tcf_unbind_filter(tp, &f->res);
296d5f984f5SCong Wang 		tcf_exts_get_net(&f->exts);
297aaa908ffSCong Wang 		tcf_queue_work(&f->rwork, fw_delete_filter_work);
298e35a8ee5SJohn Fastabend 
2998113c095SWANG Cong 		*arg = fnew;
300e35a8ee5SJohn Fastabend 		return err;
3011da177e4SLinus Torvalds 	}
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	if (!handle)
3041da177e4SLinus Torvalds 		return -EINVAL;
3051da177e4SLinus Torvalds 
306d8aecb10SWANG Cong 	if (!head) {
307d8aecb10SWANG Cong 		u32 mask = 0xFFFFFFFF;
3086fa8c014SPatrick McHardy 		if (tb[TCA_FW_MASK])
309d8aecb10SWANG Cong 			mask = nla_get_u32(tb[TCA_FW_MASK]);
310d8aecb10SWANG Cong 
311d8aecb10SWANG Cong 		head = kzalloc(sizeof(*head), GFP_KERNEL);
312d8aecb10SWANG Cong 		if (!head)
313d8aecb10SWANG Cong 			return -ENOBUFS;
314d8aecb10SWANG Cong 		head->mask = mask;
315d8aecb10SWANG Cong 
316d8aecb10SWANG Cong 		rcu_assign_pointer(tp->root, head);
3171da177e4SLinus Torvalds 	}
3181da177e4SLinus Torvalds 
3190da974f4SPanagiotis Issaris 	f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
3201da177e4SLinus Torvalds 	if (f == NULL)
3211da177e4SLinus Torvalds 		return -ENOBUFS;
3221da177e4SLinus Torvalds 
32314215108SCong Wang 	err = tcf_exts_init(&f->exts, net, TCA_FW_ACT, TCA_FW_POLICE);
324b9a24bb7SWANG Cong 	if (err < 0)
325b9a24bb7SWANG Cong 		goto errout;
3261da177e4SLinus Torvalds 	f->id = handle;
327e35a8ee5SJohn Fastabend 	f->tp = tp;
3281da177e4SLinus Torvalds 
329695176bfSCong Wang 	err = fw_set_parms(net, tp, f, tb, tca, base, flags, extack);
3301da177e4SLinus Torvalds 	if (err < 0)
3311da177e4SLinus Torvalds 		goto errout;
3321da177e4SLinus Torvalds 
333e35a8ee5SJohn Fastabend 	RCU_INIT_POINTER(f->next, head->ht[fw_hash(handle)]);
334e35a8ee5SJohn Fastabend 	rcu_assign_pointer(head->ht[fw_hash(handle)], f);
3351da177e4SLinus Torvalds 
3368113c095SWANG Cong 	*arg = f;
3371da177e4SLinus Torvalds 	return 0;
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds errout:
340b9a24bb7SWANG Cong 	tcf_exts_destroy(&f->exts);
3411da177e4SLinus Torvalds 	kfree(f);
3421da177e4SLinus Torvalds 	return err;
3431da177e4SLinus Torvalds }
3441da177e4SLinus Torvalds 
fw_walk(struct tcf_proto * tp,struct tcf_walker * arg,bool rtnl_held)34512db03b6SVlad Buslov static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg,
34612db03b6SVlad Buslov 		    bool rtnl_held)
3471da177e4SLinus Torvalds {
348e35a8ee5SJohn Fastabend 	struct fw_head *head = rtnl_dereference(tp->root);
3491da177e4SLinus Torvalds 	int h;
3501da177e4SLinus Torvalds 
3511d997875SVlad Buslov 	if (head == NULL)
3521d997875SVlad Buslov 		arg->stop = 1;
3531d997875SVlad Buslov 
3541d997875SVlad Buslov 	if (arg->stop)
3551da177e4SLinus Torvalds 		return;
3561da177e4SLinus Torvalds 
357c5c13fafSThomas Graf 	for (h = 0; h < HTSIZE; h++) {
3581da177e4SLinus Torvalds 		struct fw_filter *f;
3591da177e4SLinus Torvalds 
360e35a8ee5SJohn Fastabend 		for (f = rtnl_dereference(head->ht[h]); f;
361e35a8ee5SJohn Fastabend 		     f = rtnl_dereference(f->next)) {
3625508ff7cSZhengchao Shao 			if (!tc_cls_stats_dump(tp, arg, f))
3631da177e4SLinus Torvalds 				return;
3641da177e4SLinus Torvalds 		}
3651da177e4SLinus Torvalds 	}
3661da177e4SLinus Torvalds }
3671da177e4SLinus Torvalds 
fw_dump(struct net * net,struct tcf_proto * tp,void * fh,struct sk_buff * skb,struct tcmsg * t,bool rtnl_held)3688113c095SWANG Cong static int fw_dump(struct net *net, struct tcf_proto *tp, void *fh,
36912db03b6SVlad Buslov 		   struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
3701da177e4SLinus Torvalds {
371e35a8ee5SJohn Fastabend 	struct fw_head *head = rtnl_dereference(tp->root);
3728113c095SWANG Cong 	struct fw_filter *f = fh;
3734b3550efSPatrick McHardy 	struct nlattr *nest;
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	if (f == NULL)
3761da177e4SLinus Torvalds 		return skb->len;
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	t->tcm_handle = f->id;
3791da177e4SLinus Torvalds 
3806fc6d06eSJiri Pirko 	if (!f->res.classid && !tcf_exts_has_actions(&f->exts))
3811da177e4SLinus Torvalds 		return skb->len;
3821da177e4SLinus Torvalds 
383ae0be8deSMichal Kubecek 	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
3844b3550efSPatrick McHardy 	if (nest == NULL)
3854b3550efSPatrick McHardy 		goto nla_put_failure;
3861da177e4SLinus Torvalds 
3871b34ec43SDavid S. Miller 	if (f->res.classid &&
3881b34ec43SDavid S. Miller 	    nla_put_u32(skb, TCA_FW_CLASSID, f->res.classid))
3891b34ec43SDavid S. Miller 		goto nla_put_failure;
3902519a602SWANG Cong 	if (f->ifindex) {
3912519a602SWANG Cong 		struct net_device *dev;
3922519a602SWANG Cong 		dev = __dev_get_by_index(net, f->ifindex);
3932519a602SWANG Cong 		if (dev && nla_put_string(skb, TCA_FW_INDEV, dev->name))
3941b34ec43SDavid S. Miller 			goto nla_put_failure;
3952519a602SWANG Cong 	}
3961b34ec43SDavid S. Miller 	if (head->mask != 0xFFFFFFFF &&
3971b34ec43SDavid S. Miller 	    nla_put_u32(skb, TCA_FW_MASK, head->mask))
3981b34ec43SDavid S. Miller 		goto nla_put_failure;
3991da177e4SLinus Torvalds 
4005da57f42SWANG Cong 	if (tcf_exts_dump(skb, &f->exts) < 0)
401add93b61SPatrick McHardy 		goto nla_put_failure;
4021da177e4SLinus Torvalds 
4034b3550efSPatrick McHardy 	nla_nest_end(skb, nest);
4041da177e4SLinus Torvalds 
4055da57f42SWANG Cong 	if (tcf_exts_dump_stats(skb, &f->exts) < 0)
406add93b61SPatrick McHardy 		goto nla_put_failure;
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	return skb->len;
4091da177e4SLinus Torvalds 
410add93b61SPatrick McHardy nla_put_failure:
4116ea3b446SJiri Pirko 	nla_nest_cancel(skb, nest);
4121da177e4SLinus Torvalds 	return -1;
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
fw_bind_class(void * fh,u32 classid,unsigned long cl,void * q,unsigned long base)4152e24cd75SCong Wang static void fw_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
4162e24cd75SCong Wang 			  unsigned long base)
41707d79fc7SCong Wang {
41807d79fc7SCong Wang 	struct fw_filter *f = fh;
41907d79fc7SCong Wang 
420cc9039a1SZhengchao Shao 	tc_cls_bind_class(classid, cl, q, &f->res, base);
42107d79fc7SCong Wang }
42207d79fc7SCong Wang 
4232eb9d75cSPatrick McHardy static struct tcf_proto_ops cls_fw_ops __read_mostly = {
4241da177e4SLinus Torvalds 	.kind		=	"fw",
4251da177e4SLinus Torvalds 	.classify	=	fw_classify,
4261da177e4SLinus Torvalds 	.init		=	fw_init,
4271da177e4SLinus Torvalds 	.destroy	=	fw_destroy,
4281da177e4SLinus Torvalds 	.get		=	fw_get,
4291da177e4SLinus Torvalds 	.change		=	fw_change,
4301da177e4SLinus Torvalds 	.delete		=	fw_delete,
4311da177e4SLinus Torvalds 	.walk		=	fw_walk,
4321da177e4SLinus Torvalds 	.dump		=	fw_dump,
43307d79fc7SCong Wang 	.bind_class	=	fw_bind_class,
4341da177e4SLinus Torvalds 	.owner		=	THIS_MODULE,
4351da177e4SLinus Torvalds };
4361da177e4SLinus Torvalds 
init_fw(void)4371da177e4SLinus Torvalds static int __init init_fw(void)
4381da177e4SLinus Torvalds {
4391da177e4SLinus Torvalds 	return register_tcf_proto_ops(&cls_fw_ops);
4401da177e4SLinus Torvalds }
4411da177e4SLinus Torvalds 
exit_fw(void)4421da177e4SLinus Torvalds static void __exit exit_fw(void)
4431da177e4SLinus Torvalds {
4441da177e4SLinus Torvalds 	unregister_tcf_proto_ops(&cls_fw_ops);
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds module_init(init_fw)
4481da177e4SLinus Torvalds module_exit(exit_fw)
4491da177e4SLinus Torvalds MODULE_LICENSE("GPL");
450