blob: b7bcf67641bf2c86975c8b0d6d3f0707cd7727af [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
WANG Cong763dbf62017-04-19 14:21:21 -0700115static void basic_destroy(struct tcf_proto *tp)
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);
Cong Wang1d8134f2017-09-25 10:13:50 -0700123 idr_remove_ext(&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
WANG Cong8113c092017-08-04 21:31:43 -0700133static int basic_delete(struct tcf_proto *tp, void *arg, bool *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
WANG Cong763dbf62017-04-19 14:21:21 -0700135 struct basic_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700136 struct basic_filter *f = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Jiri Pirkoe4386452014-12-02 18:00:31 +0100138 list_del_rcu(&f->link);
139 tcf_unbind_filter(tp, &f->res);
Cong Wang1d8134f2017-09-25 10:13:50 -0700140 idr_remove_ext(&head->handle_idr, f->handle);
Cong Wang0b2a5982017-11-06 13:47:20 -0800141 tcf_exts_get_net(&f->exts);
Jiri Pirkoe4386452014-12-02 18:00:31 +0100142 call_rcu(&f->rcu, basic_delete_filter);
WANG Cong763dbf62017-04-19 14:21:21 -0700143 *last = list_empty(&head->flist);
Jiri Pirkoe4386452014-12-02 18:00:31 +0100144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800147static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
148 [TCA_BASIC_CLASSID] = { .type = NLA_U32 },
149 [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
150};
151
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000152static int basic_set_parms(struct net *net, struct tcf_proto *tp,
153 struct basic_filter *f, unsigned long base,
154 struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -0500155 struct nlattr *est, bool ovr,
156 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
stephen hemminger64590822013-09-26 17:42:16 -0700158 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Alexander Aring50a56192018-01-18 11:20:52 -0500160 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (err < 0)
162 return err;
163
Jiri Pirko4ebc1e32017-08-04 14:28:57 +0200164 err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &f->ematches);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (err < 0)
Jiri Pirkoff1f8ca2017-08-04 14:29:09 +0200166 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Patrick McHardyadd93b62008-01-22 22:11:33 -0800168 if (tb[TCA_BASIC_CLASSID]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800169 f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 tcf_bind_filter(tp, &f->res, base);
171 }
172
John Fastabend9888faef2014-09-12 20:05:59 -0700173 f->tp = tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000177static int basic_change(struct net *net, struct sk_buff *in_skb,
Eric W. Biedermanaf4c6642012-05-25 13:42:45 -0600178 struct tcf_proto *tp, unsigned long base, u32 handle,
Alexander Aring7306db32018-01-18 11:20:51 -0500179 struct nlattr **tca, void **arg, bool ovr,
180 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Patrick McHardycee63722008-01-23 20:33:32 -0800182 int err;
John Fastabend9888faef2014-09-12 20:05:59 -0700183 struct basic_head *head = rtnl_dereference(tp->root);
Patrick McHardyadd93b62008-01-22 22:11:33 -0800184 struct nlattr *tb[TCA_BASIC_MAX + 1];
John Fastabend9888faef2014-09-12 20:05:59 -0700185 struct basic_filter *fold = (struct basic_filter *) *arg;
186 struct basic_filter *fnew;
Cong Wang1d8134f2017-09-25 10:13:50 -0700187 unsigned long idr_index;
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
John Fastabend9888faef2014-09-12 20:05:59 -0700210 if (handle) {
211 fnew->handle = handle;
Cong Wang1d8134f2017-09-25 10:13:50 -0700212 if (!fold) {
213 err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index,
214 handle, handle + 1, GFP_KERNEL);
215 if (err)
216 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
Cong Wang1d8134f2017-09-25 10:13:50 -0700218 } else {
219 err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index,
220 1, 0x7FFFFFFF, GFP_KERNEL);
221 if (err)
222 goto errout;
223 fnew->handle = idr_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
Alexander Aring50a56192018-01-18 11:20:52 -0500226 err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr,
227 extack);
Cong Wang1d8134f2017-09-25 10:13:50 -0700228 if (err < 0) {
229 if (!fold)
230 idr_remove_ext(&head->handle_idr, fnew->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 goto errout;
Cong Wang1d8134f2017-09-25 10:13:50 -0700232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
WANG Cong8113c092017-08-04 21:31:43 -0700234 *arg = fnew;
John Fastabend9888faef2014-09-12 20:05:59 -0700235
236 if (fold) {
Cong Wang1d8134f2017-09-25 10:13:50 -0700237 idr_replace_ext(&head->handle_idr, fnew, fnew->handle);
John Fastabend9888faef2014-09-12 20:05:59 -0700238 list_replace_rcu(&fold->link, &fnew->link);
John Fastabend18cdb372014-10-05 21:28:52 -0700239 tcf_unbind_filter(tp, &fold->res);
Cong Wang0b2a5982017-11-06 13:47:20 -0800240 tcf_exts_get_net(&fold->exts);
John Fastabend9888faef2014-09-12 20:05:59 -0700241 call_rcu(&fold->rcu, basic_delete_filter);
242 } else {
243 list_add_rcu(&fnew->link, &head->flist);
244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 return 0;
247errout:
WANG Congb9a24bb2016-08-19 12:36:54 -0700248 tcf_exts_destroy(&fnew->exts);
John Fastabend9888faef2014-09-12 20:05:59 -0700249 kfree(fnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return err;
251}
252
253static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg)
254{
John Fastabend9888faef2014-09-12 20:05:59 -0700255 struct basic_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 struct basic_filter *f;
257
258 list_for_each_entry(f, &head->flist, link) {
259 if (arg->count < arg->skip)
260 goto skip;
261
WANG Cong8113c092017-08-04 21:31:43 -0700262 if (arg->fn(tp, f, arg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 arg->stop = 1;
264 break;
265 }
266skip:
267 arg->count++;
268 }
269}
270
Cong Wang07d79fc2017-08-30 14:30:36 -0700271static void basic_bind_class(void *fh, u32 classid, unsigned long cl)
272{
273 struct basic_filter *f = fh;
274
275 if (f && f->res.classid == classid)
276 f->res.class = cl;
277}
278
WANG Cong8113c092017-08-04 21:31:43 -0700279static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 struct sk_buff *skb, struct tcmsg *t)
281{
WANG Cong8113c092017-08-04 21:31:43 -0700282 struct basic_filter *f = fh;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800283 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 if (f == NULL)
286 return skb->len;
287
288 t->tcm_handle = f->handle;
289
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800290 nest = nla_nest_start(skb, TCA_OPTIONS);
291 if (nest == NULL)
292 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
David S. Miller1b34ec42012-03-29 05:11:39 -0400294 if (f->res.classid &&
295 nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
296 goto nla_put_failure;
Thomas Grafe1e284a2005-06-08 15:11:02 -0700297
WANG Cong5da57f42013-12-15 20:15:07 -0800298 if (tcf_exts_dump(skb, &f->exts) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800300 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800302 nla_nest_end(skb, nest);
stephen hemminger4c46ee52010-11-04 11:47:04 +0000303
WANG Cong5da57f42013-12-15 20:15:07 -0800304 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
stephen hemminger4c46ee52010-11-04 11:47:04 +0000305 goto nla_put_failure;
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return skb->len;
308
Patrick McHardyadd93b62008-01-22 22:11:33 -0800309nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800310 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return -1;
312}
313
Patrick McHardy2eb9d752008-01-22 22:10:42 -0800314static struct tcf_proto_ops cls_basic_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 .kind = "basic",
316 .classify = basic_classify,
317 .init = basic_init,
318 .destroy = basic_destroy,
319 .get = basic_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 .change = basic_change,
321 .delete = basic_delete,
322 .walk = basic_walk,
323 .dump = basic_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -0700324 .bind_class = basic_bind_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 .owner = THIS_MODULE,
326};
327
328static int __init init_basic(void)
329{
330 return register_tcf_proto_ops(&cls_basic_ops);
331}
332
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900333static void __exit exit_basic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 unregister_tcf_proto_ops(&cls_basic_ops);
336}
337
338module_init(init_basic)
339module_exit(exit_basic)
340MODULE_LICENSE("GPL");
341