1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/sched/cls_fw.c Classifier mapping ipchains' fwmark to traffic class.
5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
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
12 * JHS: We should remove the CONFIG_NET_CLS_IND from here
13 * eventually when the meta match extension is made available
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/skbuff.h>
23 #include <net/netlink.h>
24 #include <net/act_api.h>
25 #include <net/pkt_cls.h>
26 #include <net/sch_generic.h>
32 struct fw_filter __rcu *ht[HTSIZE];
37 struct fw_filter __rcu *next;
39 struct tcf_result res;
40 #ifdef CONFIG_NET_CLS_IND
42 #endif /* CONFIG_NET_CLS_IND */
45 struct rcu_work rwork;
48 static u32 fw_hash(u32 handle)
50 handle ^= (handle >> 16);
51 handle ^= (handle >> 8);
52 return handle % HTSIZE;
55 static int fw_classify(struct sk_buff *skb, const struct tcf_proto *tp,
56 struct tcf_result *res)
58 struct fw_head *head = rcu_dereference_bh(tp->root);
66 for (f = rcu_dereference_bh(head->ht[fw_hash(id)]); f;
67 f = rcu_dereference_bh(f->next)) {
70 #ifdef CONFIG_NET_CLS_IND
71 if (!tcf_match_indev(skb, f->ifindex))
73 #endif /* CONFIG_NET_CLS_IND */
74 r = tcf_exts_exec(skb, &f->exts, res);
82 struct Qdisc *q = tcf_block_q(tp->chain->block);
84 /* Old method: classify the packet using its skb mark. */
85 if (id && (TC_H_MAJ(id) == 0 ||
86 !(TC_H_MAJ(id ^ q->handle)))) {
96 static void *fw_get(struct tcf_proto *tp, u32 handle)
98 struct fw_head *head = rtnl_dereference(tp->root);
104 f = rtnl_dereference(head->ht[fw_hash(handle)]);
105 for (; f; f = rtnl_dereference(f->next)) {
112 static int fw_init(struct tcf_proto *tp)
114 /* We don't allocate fw_head here, because in the old method
115 * we don't need it at all.
120 static void __fw_delete_filter(struct fw_filter *f)
122 tcf_exts_destroy(&f->exts);
123 tcf_exts_put_net(&f->exts);
127 static void fw_delete_filter_work(struct work_struct *work)
129 struct fw_filter *f = container_of(to_rcu_work(work),
133 __fw_delete_filter(f);
137 static void fw_destroy(struct tcf_proto *tp, bool rtnl_held,
138 struct netlink_ext_ack *extack)
140 struct fw_head *head = rtnl_dereference(tp->root);
147 for (h = 0; h < HTSIZE; h++) {
148 while ((f = rtnl_dereference(head->ht[h])) != NULL) {
149 RCU_INIT_POINTER(head->ht[h],
150 rtnl_dereference(f->next));
151 tcf_unbind_filter(tp, &f->res);
152 if (tcf_exts_get_net(&f->exts))
153 tcf_queue_work(&f->rwork, fw_delete_filter_work);
155 __fw_delete_filter(f);
158 kfree_rcu(head, rcu);
161 static int fw_delete(struct tcf_proto *tp, void *arg, bool *last,
162 bool rtnl_held, struct netlink_ext_ack *extack)
164 struct fw_head *head = rtnl_dereference(tp->root);
165 struct fw_filter *f = arg;
166 struct fw_filter __rcu **fp;
167 struct fw_filter *pfp;
171 if (head == NULL || f == NULL)
174 fp = &head->ht[fw_hash(f->id)];
176 for (pfp = rtnl_dereference(*fp); pfp;
177 fp = &pfp->next, pfp = rtnl_dereference(*fp)) {
179 RCU_INIT_POINTER(*fp, rtnl_dereference(f->next));
180 tcf_unbind_filter(tp, &f->res);
181 tcf_exts_get_net(&f->exts);
182 tcf_queue_work(&f->rwork, fw_delete_filter_work);
189 for (h = 0; h < HTSIZE; h++) {
190 if (rcu_access_pointer(head->ht[h])) {
200 static 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 },
206 static 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,
209 struct netlink_ext_ack *extack)
211 struct fw_head *head = rtnl_dereference(tp->root);
215 err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &f->exts, ovr,
220 if (tb[TCA_FW_CLASSID]) {
221 f->res.classid = nla_get_u32(tb[TCA_FW_CLASSID]);
222 tcf_bind_filter(tp, &f->res, base);
225 #ifdef CONFIG_NET_CLS_IND
226 if (tb[TCA_FW_INDEV]) {
228 ret = tcf_change_indev(net, tb[TCA_FW_INDEV], extack);
233 #endif /* CONFIG_NET_CLS_IND */
236 if (tb[TCA_FW_MASK]) {
237 mask = nla_get_u32(tb[TCA_FW_MASK]);
238 if (mask != head->mask)
240 } else if (head->mask != 0xFFFFFFFF)
246 static int fw_change(struct net *net, struct sk_buff *in_skb,
247 struct tcf_proto *tp, unsigned long base,
248 u32 handle, struct nlattr **tca, void **arg,
249 bool ovr, bool rtnl_held,
250 struct netlink_ext_ack *extack)
252 struct fw_head *head = rtnl_dereference(tp->root);
253 struct fw_filter *f = *arg;
254 struct nlattr *opt = tca[TCA_OPTIONS];
255 struct nlattr *tb[TCA_FW_MAX + 1];
259 return handle ? -EINVAL : 0; /* Succeed if it is old method. */
261 err = nla_parse_nested_deprecated(tb, TCA_FW_MAX, opt, fw_policy,
267 struct fw_filter *pfp, *fnew;
268 struct fw_filter __rcu **fp;
270 if (f->id != handle && handle)
273 fnew = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
279 #ifdef CONFIG_NET_CLS_IND
280 fnew->ifindex = f->ifindex;
281 #endif /* CONFIG_NET_CLS_IND */
284 err = tcf_exts_init(&fnew->exts, net, TCA_FW_ACT,
291 err = fw_set_parms(net, tp, fnew, tb, tca, base, ovr, extack);
293 tcf_exts_destroy(&fnew->exts);
298 fp = &head->ht[fw_hash(fnew->id)];
299 for (pfp = rtnl_dereference(*fp); pfp;
300 fp = &pfp->next, pfp = rtnl_dereference(*fp))
304 RCU_INIT_POINTER(fnew->next, rtnl_dereference(pfp->next));
305 rcu_assign_pointer(*fp, fnew);
306 tcf_unbind_filter(tp, &f->res);
307 tcf_exts_get_net(&f->exts);
308 tcf_queue_work(&f->rwork, fw_delete_filter_work);
318 u32 mask = 0xFFFFFFFF;
320 mask = nla_get_u32(tb[TCA_FW_MASK]);
322 head = kzalloc(sizeof(*head), GFP_KERNEL);
327 rcu_assign_pointer(tp->root, head);
330 f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
334 err = tcf_exts_init(&f->exts, net, TCA_FW_ACT, TCA_FW_POLICE);
340 err = fw_set_parms(net, tp, f, tb, tca, base, ovr, extack);
344 RCU_INIT_POINTER(f->next, head->ht[fw_hash(handle)]);
345 rcu_assign_pointer(head->ht[fw_hash(handle)], f);
351 tcf_exts_destroy(&f->exts);
356 static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg,
359 struct fw_head *head = rtnl_dereference(tp->root);
368 for (h = 0; h < HTSIZE; h++) {
371 for (f = rtnl_dereference(head->ht[h]); f;
372 f = rtnl_dereference(f->next)) {
373 if (arg->count < arg->skip) {
377 if (arg->fn(tp, f, arg) < 0) {
386 static int fw_dump(struct net *net, struct tcf_proto *tp, void *fh,
387 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
389 struct fw_head *head = rtnl_dereference(tp->root);
390 struct fw_filter *f = fh;
396 t->tcm_handle = f->id;
398 if (!f->res.classid && !tcf_exts_has_actions(&f->exts))
401 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
403 goto nla_put_failure;
405 if (f->res.classid &&
406 nla_put_u32(skb, TCA_FW_CLASSID, f->res.classid))
407 goto nla_put_failure;
408 #ifdef CONFIG_NET_CLS_IND
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;
415 #endif /* CONFIG_NET_CLS_IND */
416 if (head->mask != 0xFFFFFFFF &&
417 nla_put_u32(skb, TCA_FW_MASK, head->mask))
418 goto nla_put_failure;
420 if (tcf_exts_dump(skb, &f->exts) < 0)
421 goto nla_put_failure;
423 nla_nest_end(skb, nest);
425 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
426 goto nla_put_failure;
431 nla_nest_cancel(skb, nest);
435 static void fw_bind_class(void *fh, u32 classid, unsigned long cl)
437 struct fw_filter *f = fh;
439 if (f && f->res.classid == classid)
443 static struct tcf_proto_ops cls_fw_ops __read_mostly = {
445 .classify = fw_classify,
447 .destroy = fw_destroy,
453 .bind_class = fw_bind_class,
454 .owner = THIS_MODULE,
457 static int __init init_fw(void)
459 return register_tcf_proto_ops(&cls_fw_ops);
462 static void __exit exit_fw(void)
464 unregister_tcf_proto_ops(&cls_fw_ops);
469 MODULE_LICENSE("GPL");