blob: 6b7ab3512f5bb8136162b2275c5d789a5dceb084 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sched/cls_basic.c Basic Packet Classifier.
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: Thomas Graf <tgraf@suug.ch>
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/types.h>
15#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/rtnetlink.h>
19#include <linux/skbuff.h>
Cong Wang1d8134f2017-09-25 10:13:50 -070020#include <linux/idr.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070021#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <net/act_api.h>
23#include <net/pkt_cls.h>
24
Eric Dumazetcc7ec452011-01-19 19:26:56 +000025struct basic_head {
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 struct list_head flist;
Cong Wang1d8134f2017-09-25 10:13:50 -070027 struct idr handle_idr;
John Fastabend9888faef2014-09-12 20:05:59 -070028 struct rcu_head rcu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029};
30
Eric Dumazetcc7ec452011-01-19 19:26:56 +000031struct basic_filter {
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 u32 handle;
33 struct tcf_exts exts;
34 struct tcf_ematch_tree ematches;
35 struct tcf_result res;
John Fastabend9888faef2014-09-12 20:05:59 -070036 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 struct list_head link;
Cong Wangc96a4832017-10-26 18:24:29 -070038 union {
39 struct work_struct work;
40 struct rcu_head rcu;
41 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070042};
43
Eric Dumazetdc7f9f62011-07-05 23:25:42 +000044static int basic_classify(struct sk_buff *skb, const struct tcf_proto *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 struct tcf_result *res)
46{
47 int r;
John Fastabend9888faef2014-09-12 20:05:59 -070048 struct basic_head *head = rcu_dereference_bh(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 struct basic_filter *f;
50
John Fastabend9888faef2014-09-12 20:05:59 -070051 list_for_each_entry_rcu(f, &head->flist, link) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if (!tcf_em_tree_match(skb, &f->ematches, NULL))
53 continue;
54 *res = f->res;
55 r = tcf_exts_exec(skb, &f->exts, res);
56 if (r < 0)
57 continue;
58 return r;
59 }
60 return -1;
61}
62
WANG Cong8113c092017-08-04 21:31:43 -070063static void *basic_get(struct tcf_proto *tp, u32 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
John Fastabend9888faef2014-09-12 20:05:59 -070065 struct basic_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 struct basic_filter *f;
67
Daniel Borkmann1c1bc6b2015-01-22 10:58:18 +010068 list_for_each_entry(f, &head->flist, link) {
69 if (f->handle == handle) {
WANG Cong8113c092017-08-04 21:31:43 -070070 return f;
Daniel Borkmann1c1bc6b2015-01-22 10:58:18 +010071 }
72 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
WANG Cong8113c092017-08-04 21:31:43 -070074 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int basic_init(struct tcf_proto *tp)
78{
Patrick McHardyd3fa76e2007-03-24 22:13:06 -070079 struct basic_head *head;
80
81 head = kzalloc(sizeof(*head), GFP_KERNEL);
82 if (head == NULL)
83 return -ENOBUFS;
84 INIT_LIST_HEAD(&head->flist);
Cong Wang1d8134f2017-09-25 10:13:50 -070085 idr_init(&head->handle_idr);
John Fastabend9888faef2014-09-12 20:05:59 -070086 rcu_assign_pointer(tp->root, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return 0;
88}
89
Cong Wang0b2a5982017-11-06 13:47:20 -080090static void __basic_delete_filter(struct basic_filter *f)
91{
92 tcf_exts_destroy(&f->exts);
93 tcf_em_tree_destroy(&f->ematches);
94 tcf_exts_put_net(&f->exts);
95 kfree(f);
96}
97
Cong Wangc96a4832017-10-26 18:24:29 -070098static void basic_delete_filter_work(struct work_struct *work)
99{
100 struct basic_filter *f = container_of(work, struct basic_filter, work);
101
102 rtnl_lock();
Cong Wang0b2a5982017-11-06 13:47:20 -0800103 __basic_delete_filter(f);
Cong Wangc96a4832017-10-26 18:24:29 -0700104 rtnl_unlock();
Cong Wangc96a4832017-10-26 18:24:29 -0700105}
106
John Fastabend9888faef2014-09-12 20:05:59 -0700107static void basic_delete_filter(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
John Fastabend9888faef2014-09-12 20:05:59 -0700109 struct basic_filter *f = container_of(head, struct basic_filter, rcu);
John Fastabend9888faef2014-09-12 20:05:59 -0700110
Cong Wangc96a4832017-10-26 18:24:29 -0700111 INIT_WORK(&f->work, basic_delete_filter_work);
112 tcf_queue_work(&f->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800115static void basic_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
John Fastabend9888faef2014-09-12 20:05:59 -0700117 struct basic_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 struct basic_filter *f, *n;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 list_for_each_entry_safe(f, n, &head->flist, link) {
John Fastabend9888faef2014-09-12 20:05:59 -0700121 list_del_rcu(&f->link);
John Fastabend18cdb372014-10-05 21:28:52 -0700122 tcf_unbind_filter(tp, &f->res);
Matthew Wilcox9c160942017-11-28 09:48:43 -0500123 idr_remove(&head->handle_idr, f->handle);
Cong Wang0b2a5982017-11-06 13:47:20 -0800124 if (tcf_exts_get_net(&f->exts))
125 call_rcu(&f->rcu, basic_delete_filter);
126 else
127 __basic_delete_filter(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
Cong Wang1d8134f2017-09-25 10:13:50 -0700129 idr_destroy(&head->handle_idr);
John Fastabend9888faef2014-09-12 20:05:59 -0700130 kfree_rcu(head, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Alexander Aring571acf22018-01-18 11:20:53 -0500133static int basic_delete(struct tcf_proto *tp, void *arg, bool *last,
134 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
WANG Cong763dbf62017-04-19 14:21:21 -0700136 struct basic_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700137 struct basic_filter *f = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Jiri Pirkoe4386452014-12-02 18:00:31 +0100139 list_del_rcu(&f->link);
140 tcf_unbind_filter(tp, &f->res);
Matthew Wilcox9c160942017-11-28 09:48:43 -0500141 idr_remove(&head->handle_idr, f->handle);
Cong Wang0b2a5982017-11-06 13:47:20 -0800142 tcf_exts_get_net(&f->exts);
Jiri Pirkoe4386452014-12-02 18:00:31 +0100143 call_rcu(&f->rcu, basic_delete_filter);
WANG Cong763dbf62017-04-19 14:21:21 -0700144 *last = list_empty(&head->flist);
Jiri Pirkoe4386452014-12-02 18:00:31 +0100145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800148static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
149 [TCA_BASIC_CLASSID] = { .type = NLA_U32 },
150 [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
151};
152
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000153static int basic_set_parms(struct net *net, struct tcf_proto *tp,
154 struct basic_filter *f, unsigned long base,
155 struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -0500156 struct nlattr *est, bool ovr,
157 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
stephen hemminger64590822013-09-26 17:42:16 -0700159 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Alexander Aring50a56192018-01-18 11:20:52 -0500161 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (err < 0)
163 return err;
164
Jiri Pirko4ebc1e32017-08-04 14:28:57 +0200165 err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &f->ematches);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (err < 0)
Jiri Pirkoff1f8ca2017-08-04 14:29:09 +0200167 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Patrick McHardyadd93b62008-01-22 22:11:33 -0800169 if (tb[TCA_BASIC_CLASSID]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800170 f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 tcf_bind_filter(tp, &f->res, base);
172 }
173
John Fastabend9888faef2014-09-12 20:05:59 -0700174 f->tp = tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000178static int basic_change(struct net *net, struct sk_buff *in_skb,
Eric W. Biedermanaf4c6642012-05-25 13:42:45 -0600179 struct tcf_proto *tp, unsigned long base, u32 handle,
Alexander Aring7306db32018-01-18 11:20:51 -0500180 struct nlattr **tca, void **arg, bool ovr,
181 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Patrick McHardycee63722008-01-23 20:33:32 -0800183 int err;
John Fastabend9888faef2014-09-12 20:05:59 -0700184 struct basic_head *head = rtnl_dereference(tp->root);
Patrick McHardyadd93b62008-01-22 22:11:33 -0800185 struct nlattr *tb[TCA_BASIC_MAX + 1];
John Fastabend9888faef2014-09-12 20:05:59 -0700186 struct basic_filter *fold = (struct basic_filter *) *arg;
187 struct basic_filter *fnew;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Patrick McHardyadd93b62008-01-22 22:11:33 -0800189 if (tca[TCA_OPTIONS] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return -EINVAL;
191
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800192 err = nla_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
Johannes Bergfceb6432017-04-12 14:34:07 +0200193 basic_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800194 if (err < 0)
195 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
John Fastabend9888faef2014-09-12 20:05:59 -0700197 if (fold != NULL) {
198 if (handle && fold->handle != handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201
John Fastabend9888faef2014-09-12 20:05:59 -0700202 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
Jiri Pirkobd42b782014-12-05 15:50:22 +0100203 if (!fnew)
204 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
WANG Congb9a24bb2016-08-19 12:36:54 -0700206 err = tcf_exts_init(&fnew->exts, TCA_BASIC_ACT, TCA_BASIC_POLICE);
207 if (err < 0)
208 goto errout;
209
Matthew Wilcox05af0eb2017-11-28 10:36:09 -0500210 if (!handle) {
211 handle = 1;
212 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
213 INT_MAX, GFP_KERNEL);
214 } else if (!fold) {
215 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
216 handle, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
Matthew Wilcox05af0eb2017-11-28 10:36:09 -0500218 if (err)
219 goto errout;
220 fnew->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Alexander Aring50a56192018-01-18 11:20:52 -0500222 err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr,
223 extack);
Cong Wang1d8134f2017-09-25 10:13:50 -0700224 if (err < 0) {
225 if (!fold)
Matthew Wilcox9c160942017-11-28 09:48:43 -0500226 idr_remove(&head->handle_idr, fnew->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 goto errout;
Cong Wang1d8134f2017-09-25 10:13:50 -0700228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
WANG Cong8113c092017-08-04 21:31:43 -0700230 *arg = fnew;
John Fastabend9888faef2014-09-12 20:05:59 -0700231
232 if (fold) {
Matthew Wilcox234a4622017-11-28 09:56:36 -0500233 idr_replace(&head->handle_idr, fnew, fnew->handle);
John Fastabend9888faef2014-09-12 20:05:59 -0700234 list_replace_rcu(&fold->link, &fnew->link);
John Fastabend18cdb372014-10-05 21:28:52 -0700235 tcf_unbind_filter(tp, &fold->res);
Cong Wang0b2a5982017-11-06 13:47:20 -0800236 tcf_exts_get_net(&fold->exts);
John Fastabend9888faef2014-09-12 20:05:59 -0700237 call_rcu(&fold->rcu, basic_delete_filter);
238 } else {
239 list_add_rcu(&fnew->link, &head->flist);
240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 return 0;
243errout:
WANG Congb9a24bb2016-08-19 12:36:54 -0700244 tcf_exts_destroy(&fnew->exts);
John Fastabend9888faef2014-09-12 20:05:59 -0700245 kfree(fnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return err;
247}
248
249static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg)
250{
John Fastabend9888faef2014-09-12 20:05:59 -0700251 struct basic_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 struct basic_filter *f;
253
254 list_for_each_entry(f, &head->flist, link) {
255 if (arg->count < arg->skip)
256 goto skip;
257
WANG Cong8113c092017-08-04 21:31:43 -0700258 if (arg->fn(tp, f, arg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 arg->stop = 1;
260 break;
261 }
262skip:
263 arg->count++;
264 }
265}
266
Cong Wang07d79fc2017-08-30 14:30:36 -0700267static void basic_bind_class(void *fh, u32 classid, unsigned long cl)
268{
269 struct basic_filter *f = fh;
270
271 if (f && f->res.classid == classid)
272 f->res.class = cl;
273}
274
WANG Cong8113c092017-08-04 21:31:43 -0700275static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 struct sk_buff *skb, struct tcmsg *t)
277{
WANG Cong8113c092017-08-04 21:31:43 -0700278 struct basic_filter *f = fh;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800279 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 if (f == NULL)
282 return skb->len;
283
284 t->tcm_handle = f->handle;
285
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800286 nest = nla_nest_start(skb, TCA_OPTIONS);
287 if (nest == NULL)
288 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
David S. Miller1b34ec42012-03-29 05:11:39 -0400290 if (f->res.classid &&
291 nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
292 goto nla_put_failure;
Thomas Grafe1e284a2005-06-08 15:11:02 -0700293
WANG Cong5da57f42013-12-15 20:15:07 -0800294 if (tcf_exts_dump(skb, &f->exts) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800296 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800298 nla_nest_end(skb, nest);
stephen hemminger4c46ee52010-11-04 11:47:04 +0000299
WANG Cong5da57f42013-12-15 20:15:07 -0800300 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
stephen hemminger4c46ee52010-11-04 11:47:04 +0000301 goto nla_put_failure;
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return skb->len;
304
Patrick McHardyadd93b62008-01-22 22:11:33 -0800305nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800306 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return -1;
308}
309
Patrick McHardy2eb9d752008-01-22 22:10:42 -0800310static struct tcf_proto_ops cls_basic_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 .kind = "basic",
312 .classify = basic_classify,
313 .init = basic_init,
314 .destroy = basic_destroy,
315 .get = basic_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 .change = basic_change,
317 .delete = basic_delete,
318 .walk = basic_walk,
319 .dump = basic_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -0700320 .bind_class = basic_bind_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 .owner = THIS_MODULE,
322};
323
324static int __init init_basic(void)
325{
326 return register_tcf_proto_ops(&cls_basic_ops);
327}
328
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900329static void __exit exit_basic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 unregister_tcf_proto_ops(&cls_basic_ops);
332}
333
334module_init(init_basic)
335module_exit(exit_basic)
336MODULE_LICENSE("GPL");
337