blob: ec945294626a8b3d1cd761b6a2636290fc47d659 [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,
Alexander Aring50a56192018-01-18 11:20:52 -0500201 struct nlattr **tca, unsigned long base, bool ovr,
202 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
Alexander Aring50a56192018-01-18 11:20:52 -0500208 err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &f->exts, ovr,
Vlad Buslovec6743a2019-02-11 10:55:43 +0200209 true, 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,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200240 bool ovr, bool rtnl_held,
241 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
John Fastabende35a8ee2014-09-12 20:07:22 -0700243 struct fw_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700244 struct fw_filter *f = *arg;
Patrick McHardyadd93b62008-01-22 22:11:33 -0800245 struct nlattr *opt = tca[TCA_OPTIONS];
246 struct nlattr *tb[TCA_FW_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 int err;
248
249 if (!opt)
WANG Congd8aecb12015-09-22 17:01:11 -0700250 return handle ? -EINVAL : 0; /* Succeed if it is old method. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Johannes Berg8cb08172019-04-26 14:07:28 +0200252 err = nla_parse_nested_deprecated(tb, TCA_FW_MAX, opt, fw_policy,
253 NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800254 if (err < 0)
255 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
John Fastabende35a8ee2014-09-12 20:07:22 -0700257 if (f) {
258 struct fw_filter *pfp, *fnew;
259 struct fw_filter __rcu **fp;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (f->id != handle && handle)
262 return -EINVAL;
John Fastabende35a8ee2014-09-12 20:07:22 -0700263
264 fnew = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
265 if (!fnew)
266 return -ENOBUFS;
267
268 fnew->id = f->id;
269 fnew->res = f->res;
John Fastabende35a8ee2014-09-12 20:07:22 -0700270 fnew->ifindex = f->ifindex;
John Fastabende35a8ee2014-09-12 20:07:22 -0700271 fnew->tp = f->tp;
272
Cong Wang14215102019-02-20 21:37:42 -0800273 err = tcf_exts_init(&fnew->exts, net, TCA_FW_ACT,
274 TCA_FW_POLICE);
WANG Congb9a24bb2016-08-19 12:36:54 -0700275 if (err < 0) {
276 kfree(fnew);
277 return err;
278 }
John Fastabende1f93eb2014-09-15 23:31:42 -0700279
Alexander Aring50a56192018-01-18 11:20:52 -0500280 err = fw_set_parms(net, tp, fnew, tb, tca, base, ovr, extack);
John Fastabende35a8ee2014-09-12 20:07:22 -0700281 if (err < 0) {
WANG Congb9a24bb2016-08-19 12:36:54 -0700282 tcf_exts_destroy(&fnew->exts);
John Fastabende35a8ee2014-09-12 20:07:22 -0700283 kfree(fnew);
284 return err;
285 }
286
287 fp = &head->ht[fw_hash(fnew->id)];
288 for (pfp = rtnl_dereference(*fp); pfp;
289 fp = &pfp->next, pfp = rtnl_dereference(*fp))
290 if (pfp == f)
291 break;
292
293 RCU_INIT_POINTER(fnew->next, rtnl_dereference(pfp->next));
294 rcu_assign_pointer(*fp, fnew);
John Fastabend18cdb372014-10-05 21:28:52 -0700295 tcf_unbind_filter(tp, &f->res);
Cong Wangd5f984f2017-11-06 13:47:25 -0800296 tcf_exts_get_net(&f->exts);
Cong Wangaaa908f2018-05-23 15:26:53 -0700297 tcf_queue_work(&f->rwork, fw_delete_filter_work);
John Fastabende35a8ee2014-09-12 20:07:22 -0700298
WANG Cong8113c092017-08-04 21:31:43 -0700299 *arg = fnew;
John Fastabende35a8ee2014-09-12 20:07:22 -0700300 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
303 if (!handle)
304 return -EINVAL;
305
WANG Congd8aecb12015-09-22 17:01:11 -0700306 if (!head) {
307 u32 mask = 0xFFFFFFFF;
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800308 if (tb[TCA_FW_MASK])
WANG Congd8aecb12015-09-22 17:01:11 -0700309 mask = nla_get_u32(tb[TCA_FW_MASK]);
310
311 head = kzalloc(sizeof(*head), GFP_KERNEL);
312 if (!head)
313 return -ENOBUFS;
314 head->mask = mask;
315
316 rcu_assign_pointer(tp->root, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700319 f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (f == NULL)
321 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Cong Wang14215102019-02-20 21:37:42 -0800323 err = tcf_exts_init(&f->exts, net, TCA_FW_ACT, TCA_FW_POLICE);
WANG Congb9a24bb2016-08-19 12:36:54 -0700324 if (err < 0)
325 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 f->id = handle;
John Fastabende35a8ee2014-09-12 20:07:22 -0700327 f->tp = tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Alexander Aring50a56192018-01-18 11:20:52 -0500329 err = fw_set_parms(net, tp, f, tb, tca, base, ovr, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (err < 0)
331 goto errout;
332
John Fastabende35a8ee2014-09-12 20:07:22 -0700333 RCU_INIT_POINTER(f->next, head->ht[fw_hash(handle)]);
334 rcu_assign_pointer(head->ht[fw_hash(handle)], f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
WANG Cong8113c092017-08-04 21:31:43 -0700336 *arg = f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return 0;
338
339errout:
WANG Congb9a24bb2016-08-19 12:36:54 -0700340 tcf_exts_destroy(&f->exts);
Jesper Juhla51482b2005-11-08 09:41:34 -0800341 kfree(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return err;
343}
344
Vlad Buslov12db03b2019-02-11 10:55:45 +0200345static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg,
346 bool rtnl_held)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
John Fastabende35a8ee2014-09-12 20:07:22 -0700348 struct fw_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 int h;
350
Vlad Buslov1d997872019-02-27 15:49:17 +0200351 if (head == NULL)
352 arg->stop = 1;
353
354 if (arg->stop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return;
356
Thomas Grafc5c13fa2005-04-24 20:19:54 -0700357 for (h = 0; h < HTSIZE; h++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 struct fw_filter *f;
359
John Fastabende35a8ee2014-09-12 20:07:22 -0700360 for (f = rtnl_dereference(head->ht[h]); f;
361 f = rtnl_dereference(f->next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (arg->count < arg->skip) {
363 arg->count++;
364 continue;
365 }
WANG Cong8113c092017-08-04 21:31:43 -0700366 if (arg->fn(tp, f, arg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 arg->stop = 1;
368 return;
369 }
370 arg->count++;
371 }
372 }
373}
374
WANG Cong8113c092017-08-04 21:31:43 -0700375static int fw_dump(struct net *net, struct tcf_proto *tp, void *fh,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200376 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
John Fastabende35a8ee2014-09-12 20:07:22 -0700378 struct fw_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700379 struct fw_filter *f = fh;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800380 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 if (f == NULL)
383 return skb->len;
384
385 t->tcm_handle = f->id;
386
Jiri Pirko6fc6d062017-08-04 14:29:00 +0200387 if (!f->res.classid && !tcf_exts_has_actions(&f->exts))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return skb->len;
389
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200390 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800391 if (nest == NULL)
392 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
David S. Miller1b34ec42012-03-29 05:11:39 -0400394 if (f->res.classid &&
395 nla_put_u32(skb, TCA_FW_CLASSID, f->res.classid))
396 goto nla_put_failure;
WANG Cong2519a602014-01-09 16:14:02 -0800397 if (f->ifindex) {
398 struct net_device *dev;
399 dev = __dev_get_by_index(net, f->ifindex);
400 if (dev && nla_put_string(skb, TCA_FW_INDEV, dev->name))
401 goto nla_put_failure;
402 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400403 if (head->mask != 0xFFFFFFFF &&
404 nla_put_u32(skb, TCA_FW_MASK, head->mask))
405 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
WANG Cong5da57f42013-12-15 20:15:07 -0800407 if (tcf_exts_dump(skb, &f->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800408 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800410 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
WANG Cong5da57f42013-12-15 20:15:07 -0800412 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800413 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 return skb->len;
416
Patrick McHardyadd93b62008-01-22 22:11:33 -0800417nla_put_failure:
Jiri Pirko6ea3b442014-12-09 22:23:29 +0100418 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return -1;
420}
421
Cong Wang2e24cd72020-01-23 16:26:18 -0800422static void fw_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
423 unsigned long base)
Cong Wang07d79fc2017-08-30 14:30:36 -0700424{
425 struct fw_filter *f = fh;
426
Cong Wang2e24cd72020-01-23 16:26:18 -0800427 if (f && f->res.classid == classid) {
428 if (cl)
429 __tcf_bind_filter(q, &f->res, base);
430 else
431 __tcf_unbind_filter(q, &f->res);
432 }
Cong Wang07d79fc2017-08-30 14:30:36 -0700433}
434
Patrick McHardy2eb9d752008-01-22 22:10:42 -0800435static struct tcf_proto_ops cls_fw_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 .kind = "fw",
437 .classify = fw_classify,
438 .init = fw_init,
439 .destroy = fw_destroy,
440 .get = fw_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 .change = fw_change,
442 .delete = fw_delete,
443 .walk = fw_walk,
444 .dump = fw_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -0700445 .bind_class = fw_bind_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 .owner = THIS_MODULE,
447};
448
449static int __init init_fw(void)
450{
451 return register_tcf_proto_ops(&cls_fw_ops);
452}
453
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900454static void __exit exit_fw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 unregister_tcf_proto_ops(&cls_fw_ops);
457}
458
459module_init(init_fw)
460module_exit(exit_fw)
461MODULE_LICENSE("GPL");