blob: 8654b0ce997c1cb525222f51a8531e99a3799efb [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * net/sched/cls_fw.c Classifier mapping ipchains' fwmark to traffic class.
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6 *
7 * Changes:
8 * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_walk off by one
9 * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_delete killed all the filter (and kernel).
10 * Alex <alex@pilotsoft.com> : 2004xxyy: Added Action extension
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/types.h>
16#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/skbuff.h>
Patrick McHardy0ba48052007-07-02 22:49:07 -070020#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <net/act_api.h>
22#include <net/pkt_cls.h>
Jiri Pirko1abf2722017-10-13 14:01:03 +020023#include <net/sch_generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Eric Dumazetd37d8ac2014-03-17 20:20:49 -070025#define HTSIZE 256
Thomas Grafc5c13fa2005-04-24 20:19:54 -070026
Eric Dumazetcc7ec452011-01-19 19:26:56 +000027struct fw_head {
Eric Dumazetd37d8ac2014-03-17 20:20:49 -070028 u32 mask;
John Fastabende35a8ee2014-09-12 20:07:22 -070029 struct fw_filter __rcu *ht[HTSIZE];
30 struct rcu_head rcu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031};
32
Eric Dumazetcc7ec452011-01-19 19:26:56 +000033struct fw_filter {
John Fastabende35a8ee2014-09-12 20:07:22 -070034 struct fw_filter __rcu *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 u32 id;
36 struct tcf_result res;
WANG Cong2519a602014-01-09 16:14:02 -080037 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct tcf_exts exts;
John Fastabende35a8ee2014-09-12 20:07:22 -070039 struct tcf_proto *tp;
Cong Wangaaa908f2018-05-23 15:26:53 -070040 struct rcu_work rwork;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041};
42
Eric Dumazetd37d8ac2014-03-17 20:20:49 -070043static u32 fw_hash(u32 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Eric Dumazetd37d8ac2014-03-17 20:20:49 -070045 handle ^= (handle >> 16);
46 handle ^= (handle >> 8);
47 return handle % HTSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
Eric Dumazetdc7f9f62011-07-05 23:25:42 +000050static int fw_classify(struct sk_buff *skb, const struct tcf_proto *tp,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -040051 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
John Fastabende35a8ee2014-09-12 20:07:22 -070053 struct fw_head *head = rcu_dereference_bh(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 struct fw_filter *f;
55 int r;
Patrick McHardy5c804bf2006-12-05 13:46:13 -080056 u32 id = skb->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 if (head != NULL) {
Patrick McHardy5c804bf2006-12-05 13:46:13 -080059 id &= head->mask;
John Fastabende35a8ee2014-09-12 20:07:22 -070060
61 for (f = rcu_dereference_bh(head->ht[fw_hash(id)]); f;
62 f = rcu_dereference_bh(f->next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (f->id == id) {
64 *res = f->res;
WANG Cong2519a602014-01-09 16:14:02 -080065 if (!tcf_match_indev(skb, f->ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 r = tcf_exts_exec(skb, &f->exts, res);
68 if (r < 0)
69 continue;
70
71 return r;
72 }
73 }
74 } else {
Jiri Pirko1abf2722017-10-13 14:01:03 +020075 struct Qdisc *q = tcf_block_q(tp->chain->block);
76
WANG Congd8aecb12015-09-22 17:01:11 -070077 /* Old method: classify the packet using its skb mark. */
Eric Dumazetcc7ec452011-01-19 19:26:56 +000078 if (id && (TC_H_MAJ(id) == 0 ||
Jiri Pirko1abf2722017-10-13 14:01:03 +020079 !(TC_H_MAJ(id ^ q->handle)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 res->classid = id;
81 res->class = 0;
82 return 0;
83 }
84 }
85
86 return -1;
87}
88
WANG Cong8113c092017-08-04 21:31:43 -070089static void *fw_get(struct tcf_proto *tp, u32 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
John Fastabende35a8ee2014-09-12 20:07:22 -070091 struct fw_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 struct fw_filter *f;
93
94 if (head == NULL)
WANG Cong8113c092017-08-04 21:31:43 -070095 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
John Fastabende35a8ee2014-09-12 20:07:22 -070097 f = rtnl_dereference(head->ht[fw_hash(handle)]);
98 for (; f; f = rtnl_dereference(f->next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (f->id == handle)
WANG Cong8113c092017-08-04 21:31:43 -0700100 return f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
WANG Cong8113c092017-08-04 21:31:43 -0700102 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static int fw_init(struct tcf_proto *tp)
106{
WANG Congd8aecb12015-09-22 17:01:11 -0700107 /* We don't allocate fw_head here, because in the old method
108 * we don't need it at all.
109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return 0;
111}
112
Cong Wangd5f984f2017-11-06 13:47:25 -0800113static void __fw_delete_filter(struct fw_filter *f)
114{
115 tcf_exts_destroy(&f->exts);
116 tcf_exts_put_net(&f->exts);
117 kfree(f);
118}
119
Cong Wange071dff2017-10-26 18:24:34 -0700120static void fw_delete_filter_work(struct work_struct *work)
121{
Cong Wangaaa908f2018-05-23 15:26:53 -0700122 struct fw_filter *f = container_of(to_rcu_work(work),
123 struct fw_filter,
124 rwork);
Cong Wange071dff2017-10-26 18:24:34 -0700125 rtnl_lock();
Cong Wangd5f984f2017-11-06 13:47:25 -0800126 __fw_delete_filter(f);
Cong Wange071dff2017-10-26 18:24:34 -0700127 rtnl_unlock();
128}
129
Vlad Buslov12db03b2019-02-11 10:55:45 +0200130static void fw_destroy(struct tcf_proto *tp, bool rtnl_held,
131 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
John Fastabende35a8ee2014-09-12 20:07:22 -0700133 struct fw_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 struct fw_filter *f;
135 int h;
136
137 if (head == NULL)
WANG Cong763dbf62017-04-19 14:21:21 -0700138 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000140 for (h = 0; h < HTSIZE; h++) {
John Fastabende35a8ee2014-09-12 20:07:22 -0700141 while ((f = rtnl_dereference(head->ht[h])) != NULL) {
142 RCU_INIT_POINTER(head->ht[h],
143 rtnl_dereference(f->next));
John Fastabend18cdb372014-10-05 21:28:52 -0700144 tcf_unbind_filter(tp, &f->res);
Cong Wangd5f984f2017-11-06 13:47:25 -0800145 if (tcf_exts_get_net(&f->exts))
Cong Wangaaa908f2018-05-23 15:26:53 -0700146 tcf_queue_work(&f->rwork, fw_delete_filter_work);
Cong Wangd5f984f2017-11-06 13:47:25 -0800147 else
148 __fw_delete_filter(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150 }
John Fastabende35a8ee2014-09-12 20:07:22 -0700151 kfree_rcu(head, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Alexander Aring571acf22018-01-18 11:20:53 -0500154static int fw_delete(struct tcf_proto *tp, void *arg, bool *last,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200155 bool rtnl_held, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
John Fastabende35a8ee2014-09-12 20:07:22 -0700157 struct fw_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700158 struct fw_filter *f = arg;
John Fastabende35a8ee2014-09-12 20:07:22 -0700159 struct fw_filter __rcu **fp;
160 struct fw_filter *pfp;
WANG Cong763dbf62017-04-19 14:21:21 -0700161 int ret = -EINVAL;
162 int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 if (head == NULL || f == NULL)
165 goto out;
166
John Fastabende35a8ee2014-09-12 20:07:22 -0700167 fp = &head->ht[fw_hash(f->id)];
168
169 for (pfp = rtnl_dereference(*fp); pfp;
170 fp = &pfp->next, pfp = rtnl_dereference(*fp)) {
171 if (pfp == f) {
172 RCU_INIT_POINTER(*fp, rtnl_dereference(f->next));
John Fastabend18cdb372014-10-05 21:28:52 -0700173 tcf_unbind_filter(tp, &f->res);
Cong Wangd5f984f2017-11-06 13:47:25 -0800174 tcf_exts_get_net(&f->exts);
Cong Wangaaa908f2018-05-23 15:26:53 -0700175 tcf_queue_work(&f->rwork, fw_delete_filter_work);
WANG Cong763dbf62017-04-19 14:21:21 -0700176 ret = 0;
177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179 }
WANG Cong763dbf62017-04-19 14:21:21 -0700180
181 *last = true;
182 for (h = 0; h < HTSIZE; h++) {
183 if (rcu_access_pointer(head->ht[h])) {
184 *last = false;
185 break;
186 }
187 }
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189out:
WANG Cong763dbf62017-04-19 14:21:21 -0700190 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800193static const struct nla_policy fw_policy[TCA_FW_MAX + 1] = {
194 [TCA_FW_CLASSID] = { .type = NLA_U32 },
195 [TCA_FW_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
196 [TCA_FW_MASK] = { .type = NLA_U32 },
197};
198
Jiri Pirko1e5003a2017-08-04 14:29:05 +0200199static int fw_set_parms(struct net *net, struct tcf_proto *tp,
200 struct fw_filter *f, struct nlattr **tb,
Cong Wang695176b2021-07-29 16:12:14 -0700201 struct nlattr **tca, unsigned long base, u32 flags,
Alexander Aring50a56192018-01-18 11:20:52 -0500202 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
John Fastabende35a8ee2014-09-12 20:07:22 -0700204 struct fw_head *head = rtnl_dereference(tp->root);
Patrick McHardyb4e9b522006-08-25 16:11:42 -0700205 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 int err;
207
Cong Wang695176b2021-07-29 16:12:14 -0700208 err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &f->exts, flags,
209 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (err < 0)
211 return err;
212
Patrick McHardyadd93b62008-01-22 22:11:33 -0800213 if (tb[TCA_FW_CLASSID]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800214 f->res.classid = nla_get_u32(tb[TCA_FW_CLASSID]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 tcf_bind_filter(tp, &f->res, base);
216 }
217
Patrick McHardyadd93b62008-01-22 22:11:33 -0800218 if (tb[TCA_FW_INDEV]) {
WANG Cong2519a602014-01-09 16:14:02 -0800219 int ret;
Alexander Aring1057c552018-01-18 11:20:54 -0500220 ret = tcf_change_indev(net, tb[TCA_FW_INDEV], extack);
Jiri Pirko94611bf2017-08-04 14:29:07 +0200221 if (ret < 0)
222 return ret;
WANG Cong2519a602014-01-09 16:14:02 -0800223 f->ifindex = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Wei Yongjuncb95ec62013-04-17 16:49:10 +0000226 err = -EINVAL;
Patrick McHardyadd93b62008-01-22 22:11:33 -0800227 if (tb[TCA_FW_MASK]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800228 mask = nla_get_u32(tb[TCA_FW_MASK]);
Patrick McHardyb4e9b522006-08-25 16:11:42 -0700229 if (mask != head->mask)
Jiri Pirko94611bf2017-08-04 14:29:07 +0200230 return err;
Patrick McHardyb4e9b522006-08-25 16:11:42 -0700231 } else if (head->mask != 0xFFFFFFFF)
Jiri Pirko94611bf2017-08-04 14:29:07 +0200232 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000237static int fw_change(struct net *net, struct sk_buff *in_skb,
Eric W. Biedermanaf4c6642012-05-25 13:42:45 -0600238 struct tcf_proto *tp, unsigned long base,
WANG Cong8113c092017-08-04 21:31:43 -0700239 u32 handle, struct nlattr **tca, void **arg,
Cong Wang695176b2021-07-29 16:12:14 -0700240 u32 flags, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
John Fastabende35a8ee2014-09-12 20:07:22 -0700242 struct fw_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700243 struct fw_filter *f = *arg;
Patrick McHardyadd93b62008-01-22 22:11:33 -0800244 struct nlattr *opt = tca[TCA_OPTIONS];
245 struct nlattr *tb[TCA_FW_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 int err;
247
248 if (!opt)
WANG Congd8aecb12015-09-22 17:01:11 -0700249 return handle ? -EINVAL : 0; /* Succeed if it is old method. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Johannes Berg8cb08172019-04-26 14:07:28 +0200251 err = nla_parse_nested_deprecated(tb, TCA_FW_MAX, opt, fw_policy,
252 NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800253 if (err < 0)
254 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
John Fastabende35a8ee2014-09-12 20:07:22 -0700256 if (f) {
257 struct fw_filter *pfp, *fnew;
258 struct fw_filter __rcu **fp;
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (f->id != handle && handle)
261 return -EINVAL;
John Fastabende35a8ee2014-09-12 20:07:22 -0700262
263 fnew = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
264 if (!fnew)
265 return -ENOBUFS;
266
267 fnew->id = f->id;
268 fnew->res = f->res;
John Fastabende35a8ee2014-09-12 20:07:22 -0700269 fnew->ifindex = f->ifindex;
John Fastabende35a8ee2014-09-12 20:07:22 -0700270 fnew->tp = f->tp;
271
Cong Wang14215102019-02-20 21:37:42 -0800272 err = tcf_exts_init(&fnew->exts, net, TCA_FW_ACT,
273 TCA_FW_POLICE);
WANG Congb9a24bb2016-08-19 12:36:54 -0700274 if (err < 0) {
275 kfree(fnew);
276 return err;
277 }
John Fastabende1f93eb2014-09-15 23:31:42 -0700278
Cong Wang695176b2021-07-29 16:12:14 -0700279 err = fw_set_parms(net, tp, fnew, tb, tca, base, flags, extack);
John Fastabende35a8ee2014-09-12 20:07:22 -0700280 if (err < 0) {
WANG Congb9a24bb2016-08-19 12:36:54 -0700281 tcf_exts_destroy(&fnew->exts);
John Fastabende35a8ee2014-09-12 20:07:22 -0700282 kfree(fnew);
283 return err;
284 }
285
286 fp = &head->ht[fw_hash(fnew->id)];
287 for (pfp = rtnl_dereference(*fp); pfp;
288 fp = &pfp->next, pfp = rtnl_dereference(*fp))
289 if (pfp == f)
290 break;
291
292 RCU_INIT_POINTER(fnew->next, rtnl_dereference(pfp->next));
293 rcu_assign_pointer(*fp, fnew);
John Fastabend18cdb372014-10-05 21:28:52 -0700294 tcf_unbind_filter(tp, &f->res);
Cong Wangd5f984f2017-11-06 13:47:25 -0800295 tcf_exts_get_net(&f->exts);
Cong Wangaaa908f2018-05-23 15:26:53 -0700296 tcf_queue_work(&f->rwork, fw_delete_filter_work);
John Fastabende35a8ee2014-09-12 20:07:22 -0700297
WANG Cong8113c092017-08-04 21:31:43 -0700298 *arg = fnew;
John Fastabende35a8ee2014-09-12 20:07:22 -0700299 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301
302 if (!handle)
303 return -EINVAL;
304
WANG Congd8aecb12015-09-22 17:01:11 -0700305 if (!head) {
306 u32 mask = 0xFFFFFFFF;
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800307 if (tb[TCA_FW_MASK])
WANG Congd8aecb12015-09-22 17:01:11 -0700308 mask = nla_get_u32(tb[TCA_FW_MASK]);
309
310 head = kzalloc(sizeof(*head), GFP_KERNEL);
311 if (!head)
312 return -ENOBUFS;
313 head->mask = mask;
314
315 rcu_assign_pointer(tp->root, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700318 f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (f == NULL)
320 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Cong Wang14215102019-02-20 21:37:42 -0800322 err = tcf_exts_init(&f->exts, net, TCA_FW_ACT, TCA_FW_POLICE);
WANG Congb9a24bb2016-08-19 12:36:54 -0700323 if (err < 0)
324 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 f->id = handle;
John Fastabende35a8ee2014-09-12 20:07:22 -0700326 f->tp = tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Cong Wang695176b2021-07-29 16:12:14 -0700328 err = fw_set_parms(net, tp, f, tb, tca, base, flags, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (err < 0)
330 goto errout;
331
John Fastabende35a8ee2014-09-12 20:07:22 -0700332 RCU_INIT_POINTER(f->next, head->ht[fw_hash(handle)]);
333 rcu_assign_pointer(head->ht[fw_hash(handle)], f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
WANG Cong8113c092017-08-04 21:31:43 -0700335 *arg = f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return 0;
337
338errout:
WANG Congb9a24bb2016-08-19 12:36:54 -0700339 tcf_exts_destroy(&f->exts);
Jesper Juhla51482b2005-11-08 09:41:34 -0800340 kfree(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return err;
342}
343
Vlad Buslov12db03b2019-02-11 10:55:45 +0200344static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg,
345 bool rtnl_held)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
John Fastabende35a8ee2014-09-12 20:07:22 -0700347 struct fw_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 int h;
349
Vlad Buslov1d997872019-02-27 15:49:17 +0200350 if (head == NULL)
351 arg->stop = 1;
352
353 if (arg->stop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return;
355
Thomas Grafc5c13fa2005-04-24 20:19:54 -0700356 for (h = 0; h < HTSIZE; h++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 struct fw_filter *f;
358
John Fastabende35a8ee2014-09-12 20:07:22 -0700359 for (f = rtnl_dereference(head->ht[h]); f;
360 f = rtnl_dereference(f->next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (arg->count < arg->skip) {
362 arg->count++;
363 continue;
364 }
WANG Cong8113c092017-08-04 21:31:43 -0700365 if (arg->fn(tp, f, arg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 arg->stop = 1;
367 return;
368 }
369 arg->count++;
370 }
371 }
372}
373
WANG Cong8113c092017-08-04 21:31:43 -0700374static int fw_dump(struct net *net, struct tcf_proto *tp, void *fh,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200375 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
John Fastabende35a8ee2014-09-12 20:07:22 -0700377 struct fw_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700378 struct fw_filter *f = fh;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800379 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 if (f == NULL)
382 return skb->len;
383
384 t->tcm_handle = f->id;
385
Jiri Pirko6fc6d062017-08-04 14:29:00 +0200386 if (!f->res.classid && !tcf_exts_has_actions(&f->exts))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return skb->len;
388
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200389 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800390 if (nest == NULL)
391 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
David S. Miller1b34ec42012-03-29 05:11:39 -0400393 if (f->res.classid &&
394 nla_put_u32(skb, TCA_FW_CLASSID, f->res.classid))
395 goto nla_put_failure;
WANG Cong2519a602014-01-09 16:14:02 -0800396 if (f->ifindex) {
397 struct net_device *dev;
398 dev = __dev_get_by_index(net, f->ifindex);
399 if (dev && nla_put_string(skb, TCA_FW_INDEV, dev->name))
400 goto nla_put_failure;
401 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400402 if (head->mask != 0xFFFFFFFF &&
403 nla_put_u32(skb, TCA_FW_MASK, head->mask))
404 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
WANG Cong5da57f42013-12-15 20:15:07 -0800406 if (tcf_exts_dump(skb, &f->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800407 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800409 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
WANG Cong5da57f42013-12-15 20:15:07 -0800411 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800412 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 return skb->len;
415
Patrick McHardyadd93b62008-01-22 22:11:33 -0800416nla_put_failure:
Jiri Pirko6ea3b442014-12-09 22:23:29 +0100417 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return -1;
419}
420
Cong Wang2e24cd72020-01-23 16:26:18 -0800421static void fw_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
422 unsigned long base)
Cong Wang07d79fc2017-08-30 14:30:36 -0700423{
424 struct fw_filter *f = fh;
425
Cong Wang2e24cd72020-01-23 16:26:18 -0800426 if (f && f->res.classid == classid) {
427 if (cl)
428 __tcf_bind_filter(q, &f->res, base);
429 else
430 __tcf_unbind_filter(q, &f->res);
431 }
Cong Wang07d79fc2017-08-30 14:30:36 -0700432}
433
Patrick McHardy2eb9d752008-01-22 22:10:42 -0800434static struct tcf_proto_ops cls_fw_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 .kind = "fw",
436 .classify = fw_classify,
437 .init = fw_init,
438 .destroy = fw_destroy,
439 .get = fw_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 .change = fw_change,
441 .delete = fw_delete,
442 .walk = fw_walk,
443 .dump = fw_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -0700444 .bind_class = fw_bind_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 .owner = THIS_MODULE,
446};
447
448static int __init init_fw(void)
449{
450 return register_tcf_proto_ops(&cls_fw_ops);
451}
452
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900453static void __exit exit_fw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 unregister_tcf_proto_ops(&cls_fw_ops);
456}
457
458module_init(init_fw)
459module_exit(exit_fw)
460MODULE_LICENSE("GPL");