blob: 2cc38cd719383ba461d2a6c86aad70fab8a902d8 [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,
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700155 struct nlattr *est, bool ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
stephen hemminger64590822013-09-26 17:42:16 -0700157 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Jiri Pirkoff1f8ca2017-08-04 14:29:09 +0200159 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (err < 0)
161 return err;
162
Jiri Pirko4ebc1e32017-08-04 14:28:57 +0200163 err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &f->ematches);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (err < 0)
Jiri Pirkoff1f8ca2017-08-04 14:29:09 +0200165 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Patrick McHardyadd93b62008-01-22 22:11:33 -0800167 if (tb[TCA_BASIC_CLASSID]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800168 f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 tcf_bind_filter(tp, &f->res, base);
170 }
171
John Fastabend9888faef2014-09-12 20:05:59 -0700172 f->tp = tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000176static int basic_change(struct net *net, struct sk_buff *in_skb,
Eric W. Biedermanaf4c6642012-05-25 13:42:45 -0600177 struct tcf_proto *tp, unsigned long base, u32 handle,
Alexander Aring7306db32018-01-18 11:20:51 -0500178 struct nlattr **tca, void **arg, bool ovr,
179 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Patrick McHardycee63722008-01-23 20:33:32 -0800181 int err;
John Fastabend9888faef2014-09-12 20:05:59 -0700182 struct basic_head *head = rtnl_dereference(tp->root);
Patrick McHardyadd93b62008-01-22 22:11:33 -0800183 struct nlattr *tb[TCA_BASIC_MAX + 1];
John Fastabend9888faef2014-09-12 20:05:59 -0700184 struct basic_filter *fold = (struct basic_filter *) *arg;
185 struct basic_filter *fnew;
Cong Wang1d8134f2017-09-25 10:13:50 -0700186 unsigned long idr_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Patrick McHardyadd93b62008-01-22 22:11:33 -0800188 if (tca[TCA_OPTIONS] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return -EINVAL;
190
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800191 err = nla_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
Johannes Bergfceb6432017-04-12 14:34:07 +0200192 basic_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800193 if (err < 0)
194 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
John Fastabend9888faef2014-09-12 20:05:59 -0700196 if (fold != NULL) {
197 if (handle && fold->handle != handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200
John Fastabend9888faef2014-09-12 20:05:59 -0700201 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
Jiri Pirkobd42b782014-12-05 15:50:22 +0100202 if (!fnew)
203 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
WANG Congb9a24bb2016-08-19 12:36:54 -0700205 err = tcf_exts_init(&fnew->exts, TCA_BASIC_ACT, TCA_BASIC_POLICE);
206 if (err < 0)
207 goto errout;
208
John Fastabend9888faef2014-09-12 20:05:59 -0700209 if (handle) {
210 fnew->handle = handle;
Cong Wang1d8134f2017-09-25 10:13:50 -0700211 if (!fold) {
212 err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index,
213 handle, handle + 1, GFP_KERNEL);
214 if (err)
215 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
Cong Wang1d8134f2017-09-25 10:13:50 -0700217 } else {
218 err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index,
219 1, 0x7FFFFFFF, GFP_KERNEL);
220 if (err)
221 goto errout;
222 fnew->handle = idr_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224
John Fastabend9888faef2014-09-12 20:05:59 -0700225 err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr);
Cong Wang1d8134f2017-09-25 10:13:50 -0700226 if (err < 0) {
227 if (!fold)
228 idr_remove_ext(&head->handle_idr, fnew->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 goto errout;
Cong Wang1d8134f2017-09-25 10:13:50 -0700230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
WANG Cong8113c092017-08-04 21:31:43 -0700232 *arg = fnew;
John Fastabend9888faef2014-09-12 20:05:59 -0700233
234 if (fold) {
Cong Wang1d8134f2017-09-25 10:13:50 -0700235 idr_replace_ext(&head->handle_idr, fnew, fnew->handle);
John Fastabend9888faef2014-09-12 20:05:59 -0700236 list_replace_rcu(&fold->link, &fnew->link);
John Fastabend18cdb372014-10-05 21:28:52 -0700237 tcf_unbind_filter(tp, &fold->res);
Cong Wang0b2a5982017-11-06 13:47:20 -0800238 tcf_exts_get_net(&fold->exts);
John Fastabend9888faef2014-09-12 20:05:59 -0700239 call_rcu(&fold->rcu, basic_delete_filter);
240 } else {
241 list_add_rcu(&fnew->link, &head->flist);
242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 return 0;
245errout:
WANG Congb9a24bb2016-08-19 12:36:54 -0700246 tcf_exts_destroy(&fnew->exts);
John Fastabend9888faef2014-09-12 20:05:59 -0700247 kfree(fnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return err;
249}
250
251static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg)
252{
John Fastabend9888faef2014-09-12 20:05:59 -0700253 struct basic_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 struct basic_filter *f;
255
256 list_for_each_entry(f, &head->flist, link) {
257 if (arg->count < arg->skip)
258 goto skip;
259
WANG Cong8113c092017-08-04 21:31:43 -0700260 if (arg->fn(tp, f, arg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 arg->stop = 1;
262 break;
263 }
264skip:
265 arg->count++;
266 }
267}
268
Cong Wang07d79fc2017-08-30 14:30:36 -0700269static void basic_bind_class(void *fh, u32 classid, unsigned long cl)
270{
271 struct basic_filter *f = fh;
272
273 if (f && f->res.classid == classid)
274 f->res.class = cl;
275}
276
WANG Cong8113c092017-08-04 21:31:43 -0700277static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct sk_buff *skb, struct tcmsg *t)
279{
WANG Cong8113c092017-08-04 21:31:43 -0700280 struct basic_filter *f = fh;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800281 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 if (f == NULL)
284 return skb->len;
285
286 t->tcm_handle = f->handle;
287
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800288 nest = nla_nest_start(skb, TCA_OPTIONS);
289 if (nest == NULL)
290 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
David S. Miller1b34ec42012-03-29 05:11:39 -0400292 if (f->res.classid &&
293 nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
294 goto nla_put_failure;
Thomas Grafe1e284a2005-06-08 15:11:02 -0700295
WANG Cong5da57f42013-12-15 20:15:07 -0800296 if (tcf_exts_dump(skb, &f->exts) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800298 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800300 nla_nest_end(skb, nest);
stephen hemminger4c46ee52010-11-04 11:47:04 +0000301
WANG Cong5da57f42013-12-15 20:15:07 -0800302 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
stephen hemminger4c46ee52010-11-04 11:47:04 +0000303 goto nla_put_failure;
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return skb->len;
306
Patrick McHardyadd93b62008-01-22 22:11:33 -0800307nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800308 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return -1;
310}
311
Patrick McHardy2eb9d752008-01-22 22:10:42 -0800312static struct tcf_proto_ops cls_basic_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 .kind = "basic",
314 .classify = basic_classify,
315 .init = basic_init,
316 .destroy = basic_destroy,
317 .get = basic_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 .change = basic_change,
319 .delete = basic_delete,
320 .walk = basic_walk,
321 .dump = basic_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -0700322 .bind_class = basic_bind_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 .owner = THIS_MODULE,
324};
325
326static int __init init_basic(void)
327{
328 return register_tcf_proto_ops(&cls_basic_ops);
329}
330
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900331static void __exit exit_basic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 unregister_tcf_proto_ops(&cls_basic_ops);
334}
335
336module_init(init_basic)
337module_exit(exit_basic)
338MODULE_LICENSE("GPL");
339