blob: 99183b8621ecb0543af91020167d13c193cad214 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sched/cls_fw.c Classifier mapping ipchains' fwmark to traffic class.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 * Changes:
12 * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_walk off by one
13 * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_delete killed all the filter (and kernel).
14 * Alex <alex@pilotsoft.com> : 2004xxyy: Added Action extension
15 *
16 * JHS: We should remove the CONFIG_NET_CLS_IND from here
17 * eventually when the meta match extension is made available
18 *
19 */
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/types.h>
24#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/skbuff.h>
Patrick McHardy0ba48052007-07-02 22:49:07 -070028#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <net/act_api.h>
30#include <net/pkt_cls.h>
31
Eric Dumazetd37d8ac2014-03-17 20:20:49 -070032#define HTSIZE 256
Thomas Grafc5c13fa2005-04-24 20:19:54 -070033
Eric Dumazetcc7ec452011-01-19 19:26:56 +000034struct fw_head {
Eric Dumazetd37d8ac2014-03-17 20:20:49 -070035 u32 mask;
John Fastabende35a8ee2014-09-12 20:07:22 -070036 struct fw_filter __rcu *ht[HTSIZE];
37 struct rcu_head rcu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038};
39
Eric Dumazetcc7ec452011-01-19 19:26:56 +000040struct fw_filter {
John Fastabende35a8ee2014-09-12 20:07:22 -070041 struct fw_filter __rcu *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 u32 id;
43 struct tcf_result res;
44#ifdef CONFIG_NET_CLS_IND
WANG Cong2519a602014-01-09 16:14:02 -080045 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#endif /* CONFIG_NET_CLS_IND */
47 struct tcf_exts exts;
John Fastabende35a8ee2014-09-12 20:07:22 -070048 struct tcf_proto *tp;
Cong Wange071dff2017-10-26 18:24:34 -070049 union {
50 struct work_struct work;
51 struct rcu_head rcu;
52 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070053};
54
Eric Dumazetd37d8ac2014-03-17 20:20:49 -070055static u32 fw_hash(u32 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Eric Dumazetd37d8ac2014-03-17 20:20:49 -070057 handle ^= (handle >> 16);
58 handle ^= (handle >> 8);
59 return handle % HTSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
Eric Dumazetdc7f9f62011-07-05 23:25:42 +000062static int fw_classify(struct sk_buff *skb, const struct tcf_proto *tp,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -040063 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
John Fastabende35a8ee2014-09-12 20:07:22 -070065 struct fw_head *head = rcu_dereference_bh(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 struct fw_filter *f;
67 int r;
Patrick McHardy5c804bf2006-12-05 13:46:13 -080068 u32 id = skb->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 if (head != NULL) {
Patrick McHardy5c804bf2006-12-05 13:46:13 -080071 id &= head->mask;
John Fastabende35a8ee2014-09-12 20:07:22 -070072
73 for (f = rcu_dereference_bh(head->ht[fw_hash(id)]); f;
74 f = rcu_dereference_bh(f->next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (f->id == id) {
76 *res = f->res;
77#ifdef CONFIG_NET_CLS_IND
WANG Cong2519a602014-01-09 16:14:02 -080078 if (!tcf_match_indev(skb, f->ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 continue;
80#endif /* CONFIG_NET_CLS_IND */
81 r = tcf_exts_exec(skb, &f->exts, res);
82 if (r < 0)
83 continue;
84
85 return r;
86 }
87 }
88 } else {
WANG Congd8aecb12015-09-22 17:01:11 -070089 /* Old method: classify the packet using its skb mark. */
Eric Dumazetcc7ec452011-01-19 19:26:56 +000090 if (id && (TC_H_MAJ(id) == 0 ||
91 !(TC_H_MAJ(id ^ tp->q->handle)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 res->classid = id;
93 res->class = 0;
94 return 0;
95 }
96 }
97
98 return -1;
99}
100
WANG Cong8113c092017-08-04 21:31:43 -0700101static void *fw_get(struct tcf_proto *tp, u32 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
John Fastabende35a8ee2014-09-12 20:07:22 -0700103 struct fw_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct fw_filter *f;
105
106 if (head == NULL)
WANG Cong8113c092017-08-04 21:31:43 -0700107 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
John Fastabende35a8ee2014-09-12 20:07:22 -0700109 f = rtnl_dereference(head->ht[fw_hash(handle)]);
110 for (; f; f = rtnl_dereference(f->next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (f->id == handle)
WANG Cong8113c092017-08-04 21:31:43 -0700112 return f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
WANG Cong8113c092017-08-04 21:31:43 -0700114 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static int fw_init(struct tcf_proto *tp)
118{
WANG Congd8aecb12015-09-22 17:01:11 -0700119 /* We don't allocate fw_head here, because in the old method
120 * we don't need it at all.
121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return 0;
123}
124
Cong Wange071dff2017-10-26 18:24:34 -0700125static void fw_delete_filter_work(struct work_struct *work)
126{
127 struct fw_filter *f = container_of(work, struct fw_filter, work);
128
129 rtnl_lock();
130 tcf_exts_destroy(&f->exts);
131 kfree(f);
132 rtnl_unlock();
133}
134
John Fastabende35a8ee2014-09-12 20:07:22 -0700135static void fw_delete_filter(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
John Fastabende35a8ee2014-09-12 20:07:22 -0700137 struct fw_filter *f = container_of(head, struct fw_filter, rcu);
John Fastabende35a8ee2014-09-12 20:07:22 -0700138
Cong Wange071dff2017-10-26 18:24:34 -0700139 INIT_WORK(&f->work, fw_delete_filter_work);
140 tcf_queue_work(&f->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
WANG Cong763dbf62017-04-19 14:21:21 -0700143static void fw_destroy(struct tcf_proto *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
John Fastabende35a8ee2014-09-12 20:07:22 -0700145 struct fw_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct fw_filter *f;
147 int h;
148
149 if (head == NULL)
WANG Cong763dbf62017-04-19 14:21:21 -0700150 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000152 for (h = 0; h < HTSIZE; h++) {
John Fastabende35a8ee2014-09-12 20:07:22 -0700153 while ((f = rtnl_dereference(head->ht[h])) != NULL) {
154 RCU_INIT_POINTER(head->ht[h],
155 rtnl_dereference(f->next));
John Fastabend18cdb372014-10-05 21:28:52 -0700156 tcf_unbind_filter(tp, &f->res);
John Fastabende35a8ee2014-09-12 20:07:22 -0700157 call_rcu(&f->rcu, fw_delete_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159 }
John Fastabende35a8ee2014-09-12 20:07:22 -0700160 kfree_rcu(head, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
WANG Cong8113c092017-08-04 21:31:43 -0700163static int fw_delete(struct tcf_proto *tp, void *arg, bool *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
John Fastabende35a8ee2014-09-12 20:07:22 -0700165 struct fw_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700166 struct fw_filter *f = arg;
John Fastabende35a8ee2014-09-12 20:07:22 -0700167 struct fw_filter __rcu **fp;
168 struct fw_filter *pfp;
WANG Cong763dbf62017-04-19 14:21:21 -0700169 int ret = -EINVAL;
170 int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 if (head == NULL || f == NULL)
173 goto out;
174
John Fastabende35a8ee2014-09-12 20:07:22 -0700175 fp = &head->ht[fw_hash(f->id)];
176
177 for (pfp = rtnl_dereference(*fp); pfp;
178 fp = &pfp->next, pfp = rtnl_dereference(*fp)) {
179 if (pfp == f) {
180 RCU_INIT_POINTER(*fp, rtnl_dereference(f->next));
John Fastabend18cdb372014-10-05 21:28:52 -0700181 tcf_unbind_filter(tp, &f->res);
John Fastabende35a8ee2014-09-12 20:07:22 -0700182 call_rcu(&f->rcu, fw_delete_filter);
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,
208 struct nlattr **tca, unsigned long base, bool ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
John Fastabende35a8ee2014-09-12 20:07:22 -0700210 struct fw_head *head = rtnl_dereference(tp->root);
Patrick McHardyb4e9b522006-08-25 16:11:42 -0700211 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 int err;
213
Jiri Pirko94611bf2017-08-04 14:29:07 +0200214 err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &f->exts, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (err < 0)
216 return err;
217
Patrick McHardyadd93b62008-01-22 22:11:33 -0800218 if (tb[TCA_FW_CLASSID]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800219 f->res.classid = nla_get_u32(tb[TCA_FW_CLASSID]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 tcf_bind_filter(tp, &f->res, base);
221 }
222
223#ifdef CONFIG_NET_CLS_IND
Patrick McHardyadd93b62008-01-22 22:11:33 -0800224 if (tb[TCA_FW_INDEV]) {
WANG Cong2519a602014-01-09 16:14:02 -0800225 int ret;
226 ret = tcf_change_indev(net, tb[TCA_FW_INDEV]);
Jiri Pirko94611bf2017-08-04 14:29:07 +0200227 if (ret < 0)
228 return ret;
WANG Cong2519a602014-01-09 16:14:02 -0800229 f->ifindex = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231#endif /* CONFIG_NET_CLS_IND */
232
Wei Yongjuncb95ec62013-04-17 16:49:10 +0000233 err = -EINVAL;
Patrick McHardyadd93b62008-01-22 22:11:33 -0800234 if (tb[TCA_FW_MASK]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800235 mask = nla_get_u32(tb[TCA_FW_MASK]);
Patrick McHardyb4e9b522006-08-25 16:11:42 -0700236 if (mask != head->mask)
Jiri Pirko94611bf2017-08-04 14:29:07 +0200237 return err;
Patrick McHardyb4e9b522006-08-25 16:11:42 -0700238 } else if (head->mask != 0xFFFFFFFF)
Jiri Pirko94611bf2017-08-04 14:29:07 +0200239 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000244static int fw_change(struct net *net, struct sk_buff *in_skb,
Eric W. Biedermanaf4c6642012-05-25 13:42:45 -0600245 struct tcf_proto *tp, unsigned long base,
WANG Cong8113c092017-08-04 21:31:43 -0700246 u32 handle, struct nlattr **tca, void **arg,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400247 bool ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
John Fastabende35a8ee2014-09-12 20:07:22 -0700249 struct fw_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700250 struct fw_filter *f = *arg;
Patrick McHardyadd93b62008-01-22 22:11:33 -0800251 struct nlattr *opt = tca[TCA_OPTIONS];
252 struct nlattr *tb[TCA_FW_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 int err;
254
255 if (!opt)
WANG Congd8aecb12015-09-22 17:01:11 -0700256 return handle ? -EINVAL : 0; /* Succeed if it is old method. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Johannes Bergfceb6432017-04-12 14:34:07 +0200258 err = nla_parse_nested(tb, TCA_FW_MAX, opt, fw_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800259 if (err < 0)
260 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
John Fastabende35a8ee2014-09-12 20:07:22 -0700262 if (f) {
263 struct fw_filter *pfp, *fnew;
264 struct fw_filter __rcu **fp;
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (f->id != handle && handle)
267 return -EINVAL;
John Fastabende35a8ee2014-09-12 20:07:22 -0700268
269 fnew = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
270 if (!fnew)
271 return -ENOBUFS;
272
273 fnew->id = f->id;
274 fnew->res = f->res;
275#ifdef CONFIG_NET_CLS_IND
276 fnew->ifindex = f->ifindex;
277#endif /* CONFIG_NET_CLS_IND */
278 fnew->tp = f->tp;
279
WANG Congb9a24bb2016-08-19 12:36:54 -0700280 err = tcf_exts_init(&fnew->exts, TCA_FW_ACT, TCA_FW_POLICE);
281 if (err < 0) {
282 kfree(fnew);
283 return err;
284 }
John Fastabende1f93eb2014-09-15 23:31:42 -0700285
Jiri Pirko1e5003a2017-08-04 14:29:05 +0200286 err = fw_set_parms(net, tp, fnew, tb, tca, base, ovr);
John Fastabende35a8ee2014-09-12 20:07:22 -0700287 if (err < 0) {
WANG Congb9a24bb2016-08-19 12:36:54 -0700288 tcf_exts_destroy(&fnew->exts);
John Fastabende35a8ee2014-09-12 20:07:22 -0700289 kfree(fnew);
290 return err;
291 }
292
293 fp = &head->ht[fw_hash(fnew->id)];
294 for (pfp = rtnl_dereference(*fp); pfp;
295 fp = &pfp->next, pfp = rtnl_dereference(*fp))
296 if (pfp == f)
297 break;
298
299 RCU_INIT_POINTER(fnew->next, rtnl_dereference(pfp->next));
300 rcu_assign_pointer(*fp, fnew);
John Fastabend18cdb372014-10-05 21:28:52 -0700301 tcf_unbind_filter(tp, &f->res);
John Fastabende35a8ee2014-09-12 20:07:22 -0700302 call_rcu(&f->rcu, fw_delete_filter);
303
WANG Cong8113c092017-08-04 21:31:43 -0700304 *arg = fnew;
John Fastabende35a8ee2014-09-12 20:07:22 -0700305 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307
308 if (!handle)
309 return -EINVAL;
310
WANG Congd8aecb12015-09-22 17:01:11 -0700311 if (!head) {
312 u32 mask = 0xFFFFFFFF;
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800313 if (tb[TCA_FW_MASK])
WANG Congd8aecb12015-09-22 17:01:11 -0700314 mask = nla_get_u32(tb[TCA_FW_MASK]);
315
316 head = kzalloc(sizeof(*head), GFP_KERNEL);
317 if (!head)
318 return -ENOBUFS;
319 head->mask = mask;
320
321 rcu_assign_pointer(tp->root, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700324 f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (f == NULL)
326 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
WANG Congb9a24bb2016-08-19 12:36:54 -0700328 err = tcf_exts_init(&f->exts, TCA_FW_ACT, TCA_FW_POLICE);
329 if (err < 0)
330 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 f->id = handle;
John Fastabende35a8ee2014-09-12 20:07:22 -0700332 f->tp = tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Jiri Pirko1e5003a2017-08-04 14:29:05 +0200334 err = fw_set_parms(net, tp, f, tb, tca, base, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (err < 0)
336 goto errout;
337
John Fastabende35a8ee2014-09-12 20:07:22 -0700338 RCU_INIT_POINTER(f->next, head->ht[fw_hash(handle)]);
339 rcu_assign_pointer(head->ht[fw_hash(handle)], f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
WANG Cong8113c092017-08-04 21:31:43 -0700341 *arg = f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return 0;
343
344errout:
WANG Congb9a24bb2016-08-19 12:36:54 -0700345 tcf_exts_destroy(&f->exts);
Jesper Juhla51482b2005-11-08 09:41:34 -0800346 kfree(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return err;
348}
349
350static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg)
351{
John Fastabende35a8ee2014-09-12 20:07:22 -0700352 struct fw_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 int h;
354
355 if (head == NULL)
356 arg->stop = 1;
357
358 if (arg->stop)
359 return;
360
Thomas Grafc5c13fa2005-04-24 20:19:54 -0700361 for (h = 0; h < HTSIZE; h++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct fw_filter *f;
363
John Fastabende35a8ee2014-09-12 20:07:22 -0700364 for (f = rtnl_dereference(head->ht[h]); f;
365 f = rtnl_dereference(f->next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (arg->count < arg->skip) {
367 arg->count++;
368 continue;
369 }
WANG Cong8113c092017-08-04 21:31:43 -0700370 if (arg->fn(tp, f, arg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 arg->stop = 1;
372 return;
373 }
374 arg->count++;
375 }
376 }
377}
378
WANG Cong8113c092017-08-04 21:31:43 -0700379static int fw_dump(struct net *net, struct tcf_proto *tp, void *fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 struct sk_buff *skb, struct tcmsg *t)
381{
John Fastabende35a8ee2014-09-12 20:07:22 -0700382 struct fw_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700383 struct fw_filter *f = fh;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800384 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 if (f == NULL)
387 return skb->len;
388
389 t->tcm_handle = f->id;
390
Jiri Pirko6fc6d062017-08-04 14:29:00 +0200391 if (!f->res.classid && !tcf_exts_has_actions(&f->exts))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return skb->len;
393
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800394 nest = nla_nest_start(skb, TCA_OPTIONS);
395 if (nest == NULL)
396 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
David S. Miller1b34ec42012-03-29 05:11:39 -0400398 if (f->res.classid &&
399 nla_put_u32(skb, TCA_FW_CLASSID, f->res.classid))
400 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401#ifdef CONFIG_NET_CLS_IND
WANG Cong2519a602014-01-09 16:14:02 -0800402 if (f->ifindex) {
403 struct net_device *dev;
404 dev = __dev_get_by_index(net, f->ifindex);
405 if (dev && nla_put_string(skb, TCA_FW_INDEV, dev->name))
406 goto nla_put_failure;
407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408#endif /* CONFIG_NET_CLS_IND */
David S. Miller1b34ec42012-03-29 05:11:39 -0400409 if (head->mask != 0xFFFFFFFF &&
410 nla_put_u32(skb, TCA_FW_MASK, head->mask))
411 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
WANG Cong5da57f42013-12-15 20:15:07 -0800413 if (tcf_exts_dump(skb, &f->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800414 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800416 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
WANG Cong5da57f42013-12-15 20:15:07 -0800418 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800419 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 return skb->len;
422
Patrick McHardyadd93b62008-01-22 22:11:33 -0800423nla_put_failure:
Jiri Pirko6ea3b442014-12-09 22:23:29 +0100424 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return -1;
426}
427
Cong Wang07d79fc2017-08-30 14:30:36 -0700428static void fw_bind_class(void *fh, u32 classid, unsigned long cl)
429{
430 struct fw_filter *f = fh;
431
432 if (f && f->res.classid == classid)
433 f->res.class = cl;
434}
435
Patrick McHardy2eb9d752008-01-22 22:10:42 -0800436static struct tcf_proto_ops cls_fw_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 .kind = "fw",
438 .classify = fw_classify,
439 .init = fw_init,
440 .destroy = fw_destroy,
441 .get = fw_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 .change = fw_change,
443 .delete = fw_delete,
444 .walk = fw_walk,
445 .dump = fw_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -0700446 .bind_class = fw_bind_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 .owner = THIS_MODULE,
448};
449
450static int __init init_fw(void)
451{
452 return register_tcf_proto_ops(&cls_fw_ops);
453}
454
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900455static void __exit exit_fw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 unregister_tcf_proto_ops(&cls_fw_ops);
458}
459
460module_init(init_fw)
461module_exit(exit_fw)
462MODULE_LICENSE("GPL");