blob: 4dab833f66cb64a5443865b4ee424a3b5067e750 [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
11 *
12 * JHS: We should remove the CONFIG_NET_CLS_IND from here
13 * eventually when the meta match extension is made available
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/types.h>
19#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/skbuff.h>
Patrick McHardy0ba48052007-07-02 22:49:07 -070023#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <net/act_api.h>
25#include <net/pkt_cls.h>
Jiri Pirko1abf2722017-10-13 14:01:03 +020026#include <net/sch_generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Eric Dumazetd37d8ac2014-03-17 20:20:49 -070028#define HTSIZE 256
Thomas Grafc5c13fa2005-04-24 20:19:54 -070029
Eric Dumazetcc7ec452011-01-19 19:26:56 +000030struct fw_head {
Eric Dumazetd37d8ac2014-03-17 20:20:49 -070031 u32 mask;
John Fastabende35a8ee2014-09-12 20:07:22 -070032 struct fw_filter __rcu *ht[HTSIZE];
33 struct rcu_head rcu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034};
35
Eric Dumazetcc7ec452011-01-19 19:26:56 +000036struct fw_filter {
John Fastabende35a8ee2014-09-12 20:07:22 -070037 struct fw_filter __rcu *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 u32 id;
39 struct tcf_result res;
40#ifdef CONFIG_NET_CLS_IND
WANG Cong2519a602014-01-09 16:14:02 -080041 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#endif /* CONFIG_NET_CLS_IND */
43 struct tcf_exts exts;
John Fastabende35a8ee2014-09-12 20:07:22 -070044 struct tcf_proto *tp;
Cong Wangaaa908f2018-05-23 15:26:53 -070045 struct rcu_work rwork;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
Eric Dumazetd37d8ac2014-03-17 20:20:49 -070048static u32 fw_hash(u32 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Eric Dumazetd37d8ac2014-03-17 20:20:49 -070050 handle ^= (handle >> 16);
51 handle ^= (handle >> 8);
52 return handle % HTSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
Eric Dumazetdc7f9f62011-07-05 23:25:42 +000055static int fw_classify(struct sk_buff *skb, const struct tcf_proto *tp,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -040056 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
John Fastabende35a8ee2014-09-12 20:07:22 -070058 struct fw_head *head = rcu_dereference_bh(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 struct fw_filter *f;
60 int r;
Patrick McHardy5c804bf2006-12-05 13:46:13 -080061 u32 id = skb->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 if (head != NULL) {
Patrick McHardy5c804bf2006-12-05 13:46:13 -080064 id &= head->mask;
John Fastabende35a8ee2014-09-12 20:07:22 -070065
66 for (f = rcu_dereference_bh(head->ht[fw_hash(id)]); f;
67 f = rcu_dereference_bh(f->next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (f->id == id) {
69 *res = f->res;
70#ifdef CONFIG_NET_CLS_IND
WANG Cong2519a602014-01-09 16:14:02 -080071 if (!tcf_match_indev(skb, f->ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 continue;
73#endif /* CONFIG_NET_CLS_IND */
74 r = tcf_exts_exec(skb, &f->exts, res);
75 if (r < 0)
76 continue;
77
78 return r;
79 }
80 }
81 } else {
Jiri Pirko1abf2722017-10-13 14:01:03 +020082 struct Qdisc *q = tcf_block_q(tp->chain->block);
83
WANG Congd8aecb12015-09-22 17:01:11 -070084 /* Old method: classify the packet using its skb mark. */
Eric Dumazetcc7ec452011-01-19 19:26:56 +000085 if (id && (TC_H_MAJ(id) == 0 ||
Jiri Pirko1abf2722017-10-13 14:01:03 +020086 !(TC_H_MAJ(id ^ q->handle)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 res->classid = id;
88 res->class = 0;
89 return 0;
90 }
91 }
92
93 return -1;
94}
95
WANG Cong8113c092017-08-04 21:31:43 -070096static void *fw_get(struct tcf_proto *tp, u32 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
John Fastabende35a8ee2014-09-12 20:07:22 -070098 struct fw_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 struct fw_filter *f;
100
101 if (head == NULL)
WANG Cong8113c092017-08-04 21:31:43 -0700102 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
John Fastabende35a8ee2014-09-12 20:07:22 -0700104 f = rtnl_dereference(head->ht[fw_hash(handle)]);
105 for (; f; f = rtnl_dereference(f->next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (f->id == handle)
WANG Cong8113c092017-08-04 21:31:43 -0700107 return f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
WANG Cong8113c092017-08-04 21:31:43 -0700109 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static int fw_init(struct tcf_proto *tp)
113{
WANG Congd8aecb12015-09-22 17:01:11 -0700114 /* We don't allocate fw_head here, because in the old method
115 * we don't need it at all.
116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return 0;
118}
119
Cong Wangd5f984f2017-11-06 13:47:25 -0800120static void __fw_delete_filter(struct fw_filter *f)
121{
122 tcf_exts_destroy(&f->exts);
123 tcf_exts_put_net(&f->exts);
124 kfree(f);
125}
126
Cong Wange071dff2017-10-26 18:24:34 -0700127static void fw_delete_filter_work(struct work_struct *work)
128{
Cong Wangaaa908f2018-05-23 15:26:53 -0700129 struct fw_filter *f = container_of(to_rcu_work(work),
130 struct fw_filter,
131 rwork);
Cong Wange071dff2017-10-26 18:24:34 -0700132 rtnl_lock();
Cong Wangd5f984f2017-11-06 13:47:25 -0800133 __fw_delete_filter(f);
Cong Wange071dff2017-10-26 18:24:34 -0700134 rtnl_unlock();
135}
136
Vlad Buslov12db03b2019-02-11 10:55:45 +0200137static void fw_destroy(struct tcf_proto *tp, bool rtnl_held,
138 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
John Fastabende35a8ee2014-09-12 20:07:22 -0700140 struct fw_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 struct fw_filter *f;
142 int h;
143
144 if (head == NULL)
WANG Cong763dbf62017-04-19 14:21:21 -0700145 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000147 for (h = 0; h < HTSIZE; h++) {
John Fastabende35a8ee2014-09-12 20:07:22 -0700148 while ((f = rtnl_dereference(head->ht[h])) != NULL) {
149 RCU_INIT_POINTER(head->ht[h],
150 rtnl_dereference(f->next));
John Fastabend18cdb372014-10-05 21:28:52 -0700151 tcf_unbind_filter(tp, &f->res);
Cong Wangd5f984f2017-11-06 13:47:25 -0800152 if (tcf_exts_get_net(&f->exts))
Cong Wangaaa908f2018-05-23 15:26:53 -0700153 tcf_queue_work(&f->rwork, fw_delete_filter_work);
Cong Wangd5f984f2017-11-06 13:47:25 -0800154 else
155 __fw_delete_filter(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157 }
John Fastabende35a8ee2014-09-12 20:07:22 -0700158 kfree_rcu(head, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Alexander Aring571acf22018-01-18 11:20:53 -0500161static int fw_delete(struct tcf_proto *tp, void *arg, bool *last,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200162 bool rtnl_held, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
John Fastabende35a8ee2014-09-12 20:07:22 -0700164 struct fw_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700165 struct fw_filter *f = arg;
John Fastabende35a8ee2014-09-12 20:07:22 -0700166 struct fw_filter __rcu **fp;
167 struct fw_filter *pfp;
WANG Cong763dbf62017-04-19 14:21:21 -0700168 int ret = -EINVAL;
169 int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 if (head == NULL || f == NULL)
172 goto out;
173
John Fastabende35a8ee2014-09-12 20:07:22 -0700174 fp = &head->ht[fw_hash(f->id)];
175
176 for (pfp = rtnl_dereference(*fp); pfp;
177 fp = &pfp->next, pfp = rtnl_dereference(*fp)) {
178 if (pfp == f) {
179 RCU_INIT_POINTER(*fp, rtnl_dereference(f->next));
John Fastabend18cdb372014-10-05 21:28:52 -0700180 tcf_unbind_filter(tp, &f->res);
Cong Wangd5f984f2017-11-06 13:47:25 -0800181 tcf_exts_get_net(&f->exts);
Cong Wangaaa908f2018-05-23 15:26:53 -0700182 tcf_queue_work(&f->rwork, fw_delete_filter_work);
WANG Cong763dbf62017-04-19 14:21:21 -0700183 ret = 0;
184 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186 }
WANG Cong763dbf62017-04-19 14:21:21 -0700187
188 *last = true;
189 for (h = 0; h < HTSIZE; h++) {
190 if (rcu_access_pointer(head->ht[h])) {
191 *last = false;
192 break;
193 }
194 }
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196out:
WANG Cong763dbf62017-04-19 14:21:21 -0700197 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800200static const struct nla_policy fw_policy[TCA_FW_MAX + 1] = {
201 [TCA_FW_CLASSID] = { .type = NLA_U32 },
202 [TCA_FW_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
203 [TCA_FW_MASK] = { .type = NLA_U32 },
204};
205
Jiri Pirko1e5003a2017-08-04 14:29:05 +0200206static int fw_set_parms(struct net *net, struct tcf_proto *tp,
207 struct fw_filter *f, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -0500208 struct nlattr **tca, unsigned long base, bool ovr,
209 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
John Fastabende35a8ee2014-09-12 20:07:22 -0700211 struct fw_head *head = rtnl_dereference(tp->root);
Patrick McHardyb4e9b522006-08-25 16:11:42 -0700212 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 int err;
214
Alexander Aring50a56192018-01-18 11:20:52 -0500215 err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &f->exts, ovr,
Vlad Buslovec6743a2019-02-11 10:55:43 +0200216 true, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (err < 0)
218 return err;
219
Patrick McHardyadd93b62008-01-22 22:11:33 -0800220 if (tb[TCA_FW_CLASSID]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800221 f->res.classid = nla_get_u32(tb[TCA_FW_CLASSID]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 tcf_bind_filter(tp, &f->res, base);
223 }
224
225#ifdef CONFIG_NET_CLS_IND
Patrick McHardyadd93b62008-01-22 22:11:33 -0800226 if (tb[TCA_FW_INDEV]) {
WANG Cong2519a602014-01-09 16:14:02 -0800227 int ret;
Alexander Aring1057c552018-01-18 11:20:54 -0500228 ret = tcf_change_indev(net, tb[TCA_FW_INDEV], extack);
Jiri Pirko94611bf2017-08-04 14:29:07 +0200229 if (ret < 0)
230 return ret;
WANG Cong2519a602014-01-09 16:14:02 -0800231 f->ifindex = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233#endif /* CONFIG_NET_CLS_IND */
234
Wei Yongjuncb95ec62013-04-17 16:49:10 +0000235 err = -EINVAL;
Patrick McHardyadd93b62008-01-22 22:11:33 -0800236 if (tb[TCA_FW_MASK]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800237 mask = nla_get_u32(tb[TCA_FW_MASK]);
Patrick McHardyb4e9b522006-08-25 16:11:42 -0700238 if (mask != head->mask)
Jiri Pirko94611bf2017-08-04 14:29:07 +0200239 return err;
Patrick McHardyb4e9b522006-08-25 16:11:42 -0700240 } else if (head->mask != 0xFFFFFFFF)
Jiri Pirko94611bf2017-08-04 14:29:07 +0200241 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000246static int fw_change(struct net *net, struct sk_buff *in_skb,
Eric W. Biedermanaf4c6642012-05-25 13:42:45 -0600247 struct tcf_proto *tp, unsigned long base,
WANG Cong8113c092017-08-04 21:31:43 -0700248 u32 handle, struct nlattr **tca, void **arg,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200249 bool ovr, bool rtnl_held,
250 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
John Fastabende35a8ee2014-09-12 20:07:22 -0700252 struct fw_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700253 struct fw_filter *f = *arg;
Patrick McHardyadd93b62008-01-22 22:11:33 -0800254 struct nlattr *opt = tca[TCA_OPTIONS];
255 struct nlattr *tb[TCA_FW_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 int err;
257
258 if (!opt)
WANG Congd8aecb12015-09-22 17:01:11 -0700259 return handle ? -EINVAL : 0; /* Succeed if it is old method. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Johannes Berg8cb08172019-04-26 14:07:28 +0200261 err = nla_parse_nested_deprecated(tb, TCA_FW_MAX, opt, fw_policy,
262 NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800263 if (err < 0)
264 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
John Fastabende35a8ee2014-09-12 20:07:22 -0700266 if (f) {
267 struct fw_filter *pfp, *fnew;
268 struct fw_filter __rcu **fp;
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (f->id != handle && handle)
271 return -EINVAL;
John Fastabende35a8ee2014-09-12 20:07:22 -0700272
273 fnew = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
274 if (!fnew)
275 return -ENOBUFS;
276
277 fnew->id = f->id;
278 fnew->res = f->res;
279#ifdef CONFIG_NET_CLS_IND
280 fnew->ifindex = f->ifindex;
281#endif /* CONFIG_NET_CLS_IND */
282 fnew->tp = f->tp;
283
Cong Wang14215102019-02-20 21:37:42 -0800284 err = tcf_exts_init(&fnew->exts, net, TCA_FW_ACT,
285 TCA_FW_POLICE);
WANG Congb9a24bb2016-08-19 12:36:54 -0700286 if (err < 0) {
287 kfree(fnew);
288 return err;
289 }
John Fastabende1f93eb2014-09-15 23:31:42 -0700290
Alexander Aring50a56192018-01-18 11:20:52 -0500291 err = fw_set_parms(net, tp, fnew, tb, tca, base, ovr, extack);
John Fastabende35a8ee2014-09-12 20:07:22 -0700292 if (err < 0) {
WANG Congb9a24bb2016-08-19 12:36:54 -0700293 tcf_exts_destroy(&fnew->exts);
John Fastabende35a8ee2014-09-12 20:07:22 -0700294 kfree(fnew);
295 return err;
296 }
297
298 fp = &head->ht[fw_hash(fnew->id)];
299 for (pfp = rtnl_dereference(*fp); pfp;
300 fp = &pfp->next, pfp = rtnl_dereference(*fp))
301 if (pfp == f)
302 break;
303
304 RCU_INIT_POINTER(fnew->next, rtnl_dereference(pfp->next));
305 rcu_assign_pointer(*fp, fnew);
John Fastabend18cdb372014-10-05 21:28:52 -0700306 tcf_unbind_filter(tp, &f->res);
Cong Wangd5f984f2017-11-06 13:47:25 -0800307 tcf_exts_get_net(&f->exts);
Cong Wangaaa908f2018-05-23 15:26:53 -0700308 tcf_queue_work(&f->rwork, fw_delete_filter_work);
John Fastabende35a8ee2014-09-12 20:07:22 -0700309
WANG Cong8113c092017-08-04 21:31:43 -0700310 *arg = fnew;
John Fastabende35a8ee2014-09-12 20:07:22 -0700311 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313
314 if (!handle)
315 return -EINVAL;
316
WANG Congd8aecb12015-09-22 17:01:11 -0700317 if (!head) {
318 u32 mask = 0xFFFFFFFF;
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800319 if (tb[TCA_FW_MASK])
WANG Congd8aecb12015-09-22 17:01:11 -0700320 mask = nla_get_u32(tb[TCA_FW_MASK]);
321
322 head = kzalloc(sizeof(*head), GFP_KERNEL);
323 if (!head)
324 return -ENOBUFS;
325 head->mask = mask;
326
327 rcu_assign_pointer(tp->root, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700330 f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (f == NULL)
332 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Cong Wang14215102019-02-20 21:37:42 -0800334 err = tcf_exts_init(&f->exts, net, TCA_FW_ACT, TCA_FW_POLICE);
WANG Congb9a24bb2016-08-19 12:36:54 -0700335 if (err < 0)
336 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 f->id = handle;
John Fastabende35a8ee2014-09-12 20:07:22 -0700338 f->tp = tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Alexander Aring50a56192018-01-18 11:20:52 -0500340 err = fw_set_parms(net, tp, f, tb, tca, base, ovr, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (err < 0)
342 goto errout;
343
John Fastabende35a8ee2014-09-12 20:07:22 -0700344 RCU_INIT_POINTER(f->next, head->ht[fw_hash(handle)]);
345 rcu_assign_pointer(head->ht[fw_hash(handle)], f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
WANG Cong8113c092017-08-04 21:31:43 -0700347 *arg = f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return 0;
349
350errout:
WANG Congb9a24bb2016-08-19 12:36:54 -0700351 tcf_exts_destroy(&f->exts);
Jesper Juhla51482b2005-11-08 09:41:34 -0800352 kfree(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return err;
354}
355
Vlad Buslov12db03b2019-02-11 10:55:45 +0200356static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg,
357 bool rtnl_held)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
John Fastabende35a8ee2014-09-12 20:07:22 -0700359 struct fw_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 int h;
361
Vlad Buslov1d997872019-02-27 15:49:17 +0200362 if (head == NULL)
363 arg->stop = 1;
364
365 if (arg->stop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return;
367
Thomas Grafc5c13fa2005-04-24 20:19:54 -0700368 for (h = 0; h < HTSIZE; h++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 struct fw_filter *f;
370
John Fastabende35a8ee2014-09-12 20:07:22 -0700371 for (f = rtnl_dereference(head->ht[h]); f;
372 f = rtnl_dereference(f->next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (arg->count < arg->skip) {
374 arg->count++;
375 continue;
376 }
WANG Cong8113c092017-08-04 21:31:43 -0700377 if (arg->fn(tp, f, arg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 arg->stop = 1;
379 return;
380 }
381 arg->count++;
382 }
383 }
384}
385
WANG Cong8113c092017-08-04 21:31:43 -0700386static int fw_dump(struct net *net, struct tcf_proto *tp, void *fh,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200387 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
John Fastabende35a8ee2014-09-12 20:07:22 -0700389 struct fw_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700390 struct fw_filter *f = fh;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800391 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 if (f == NULL)
394 return skb->len;
395
396 t->tcm_handle = f->id;
397
Jiri Pirko6fc6d062017-08-04 14:29:00 +0200398 if (!f->res.classid && !tcf_exts_has_actions(&f->exts))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return skb->len;
400
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200401 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800402 if (nest == NULL)
403 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
David S. Miller1b34ec42012-03-29 05:11:39 -0400405 if (f->res.classid &&
406 nla_put_u32(skb, TCA_FW_CLASSID, f->res.classid))
407 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408#ifdef CONFIG_NET_CLS_IND
WANG Cong2519a602014-01-09 16:14:02 -0800409 if (f->ifindex) {
410 struct net_device *dev;
411 dev = __dev_get_by_index(net, f->ifindex);
412 if (dev && nla_put_string(skb, TCA_FW_INDEV, dev->name))
413 goto nla_put_failure;
414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415#endif /* CONFIG_NET_CLS_IND */
David S. Miller1b34ec42012-03-29 05:11:39 -0400416 if (head->mask != 0xFFFFFFFF &&
417 nla_put_u32(skb, TCA_FW_MASK, head->mask))
418 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
WANG Cong5da57f42013-12-15 20:15:07 -0800420 if (tcf_exts_dump(skb, &f->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800421 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800423 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
WANG Cong5da57f42013-12-15 20:15:07 -0800425 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800426 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 return skb->len;
429
Patrick McHardyadd93b62008-01-22 22:11:33 -0800430nla_put_failure:
Jiri Pirko6ea3b442014-12-09 22:23:29 +0100431 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return -1;
433}
434
Cong Wang07d79fc2017-08-30 14:30:36 -0700435static void fw_bind_class(void *fh, u32 classid, unsigned long cl)
436{
437 struct fw_filter *f = fh;
438
439 if (f && f->res.classid == classid)
440 f->res.class = cl;
441}
442
Patrick McHardy2eb9d752008-01-22 22:10:42 -0800443static struct tcf_proto_ops cls_fw_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 .kind = "fw",
445 .classify = fw_classify,
446 .init = fw_init,
447 .destroy = fw_destroy,
448 .get = fw_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 .change = fw_change,
450 .delete = fw_delete,
451 .walk = fw_walk,
452 .dump = fw_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -0700453 .bind_class = fw_bind_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 .owner = THIS_MODULE,
455};
456
457static int __init init_fw(void)
458{
459 return register_tcf_proto_ops(&cls_fw_ops);
460}
461
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900462static void __exit exit_fw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 unregister_tcf_proto_ops(&cls_fw_ops);
465}
466
467module_init(init_fw)
468module_exit(exit_fw)
469MODULE_LICENSE("GPL");