blob: 95367f37098de53ee4b133ff6cd356ccb1e43e55 [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 Wangaaa908f2018-05-23 15:26:53 -070038 struct rcu_work rwork;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
40
Eric Dumazetdc7f9f62011-07-05 23:25:42 +000041static int basic_classify(struct sk_buff *skb, const struct tcf_proto *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 struct tcf_result *res)
43{
44 int r;
John Fastabend9888faef2014-09-12 20:05:59 -070045 struct basic_head *head = rcu_dereference_bh(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 struct basic_filter *f;
47
John Fastabend9888faef2014-09-12 20:05:59 -070048 list_for_each_entry_rcu(f, &head->flist, link) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 if (!tcf_em_tree_match(skb, &f->ematches, NULL))
50 continue;
51 *res = f->res;
52 r = tcf_exts_exec(skb, &f->exts, res);
53 if (r < 0)
54 continue;
55 return r;
56 }
57 return -1;
58}
59
WANG Cong8113c092017-08-04 21:31:43 -070060static void *basic_get(struct tcf_proto *tp, u32 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
John Fastabend9888faef2014-09-12 20:05:59 -070062 struct basic_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 struct basic_filter *f;
64
Daniel Borkmann1c1bc6b2015-01-22 10:58:18 +010065 list_for_each_entry(f, &head->flist, link) {
66 if (f->handle == handle) {
WANG Cong8113c092017-08-04 21:31:43 -070067 return f;
Daniel Borkmann1c1bc6b2015-01-22 10:58:18 +010068 }
69 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
WANG Cong8113c092017-08-04 21:31:43 -070071 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static int basic_init(struct tcf_proto *tp)
75{
Patrick McHardyd3fa76e2007-03-24 22:13:06 -070076 struct basic_head *head;
77
78 head = kzalloc(sizeof(*head), GFP_KERNEL);
79 if (head == NULL)
80 return -ENOBUFS;
81 INIT_LIST_HEAD(&head->flist);
Cong Wang1d8134f2017-09-25 10:13:50 -070082 idr_init(&head->handle_idr);
John Fastabend9888faef2014-09-12 20:05:59 -070083 rcu_assign_pointer(tp->root, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return 0;
85}
86
Cong Wang0b2a5982017-11-06 13:47:20 -080087static void __basic_delete_filter(struct basic_filter *f)
88{
89 tcf_exts_destroy(&f->exts);
90 tcf_em_tree_destroy(&f->ematches);
91 tcf_exts_put_net(&f->exts);
92 kfree(f);
93}
94
Cong Wangc96a4832017-10-26 18:24:29 -070095static void basic_delete_filter_work(struct work_struct *work)
96{
Cong Wangaaa908f2018-05-23 15:26:53 -070097 struct basic_filter *f = container_of(to_rcu_work(work),
98 struct basic_filter,
99 rwork);
Cong Wangc96a4832017-10-26 18:24:29 -0700100 rtnl_lock();
Cong Wang0b2a5982017-11-06 13:47:20 -0800101 __basic_delete_filter(f);
Cong Wangc96a4832017-10-26 18:24:29 -0700102 rtnl_unlock();
Cong Wangc96a4832017-10-26 18:24:29 -0700103}
104
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800105static void basic_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
John Fastabend9888faef2014-09-12 20:05:59 -0700107 struct basic_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 struct basic_filter *f, *n;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 list_for_each_entry_safe(f, n, &head->flist, link) {
John Fastabend9888faef2014-09-12 20:05:59 -0700111 list_del_rcu(&f->link);
John Fastabend18cdb372014-10-05 21:28:52 -0700112 tcf_unbind_filter(tp, &f->res);
Matthew Wilcox9c160942017-11-28 09:48:43 -0500113 idr_remove(&head->handle_idr, f->handle);
Cong Wang0b2a5982017-11-06 13:47:20 -0800114 if (tcf_exts_get_net(&f->exts))
Cong Wangaaa908f2018-05-23 15:26:53 -0700115 tcf_queue_work(&f->rwork, basic_delete_filter_work);
Cong Wang0b2a5982017-11-06 13:47:20 -0800116 else
117 __basic_delete_filter(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
Cong Wang1d8134f2017-09-25 10:13:50 -0700119 idr_destroy(&head->handle_idr);
John Fastabend9888faef2014-09-12 20:05:59 -0700120 kfree_rcu(head, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Alexander Aring571acf22018-01-18 11:20:53 -0500123static int basic_delete(struct tcf_proto *tp, void *arg, bool *last,
124 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
WANG Cong763dbf62017-04-19 14:21:21 -0700126 struct basic_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700127 struct basic_filter *f = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Jiri Pirkoe4386452014-12-02 18:00:31 +0100129 list_del_rcu(&f->link);
130 tcf_unbind_filter(tp, &f->res);
Matthew Wilcox9c160942017-11-28 09:48:43 -0500131 idr_remove(&head->handle_idr, f->handle);
Cong Wang0b2a5982017-11-06 13:47:20 -0800132 tcf_exts_get_net(&f->exts);
Cong Wangaaa908f2018-05-23 15:26:53 -0700133 tcf_queue_work(&f->rwork, basic_delete_filter_work);
WANG Cong763dbf62017-04-19 14:21:21 -0700134 *last = list_empty(&head->flist);
Jiri Pirkoe4386452014-12-02 18:00:31 +0100135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800138static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
139 [TCA_BASIC_CLASSID] = { .type = NLA_U32 },
140 [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
141};
142
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000143static int basic_set_parms(struct net *net, struct tcf_proto *tp,
144 struct basic_filter *f, unsigned long base,
145 struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -0500146 struct nlattr *est, bool ovr,
147 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
stephen hemminger64590822013-09-26 17:42:16 -0700149 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Alexander Aring50a56192018-01-18 11:20:52 -0500151 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (err < 0)
153 return err;
154
Jiri Pirko4ebc1e32017-08-04 14:28:57 +0200155 err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &f->ematches);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (err < 0)
Jiri Pirkoff1f8ca2017-08-04 14:29:09 +0200157 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Patrick McHardyadd93b62008-01-22 22:11:33 -0800159 if (tb[TCA_BASIC_CLASSID]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800160 f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 tcf_bind_filter(tp, &f->res, base);
162 }
163
John Fastabend9888faef2014-09-12 20:05:59 -0700164 f->tp = tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000168static int basic_change(struct net *net, struct sk_buff *in_skb,
Eric W. Biedermanaf4c6642012-05-25 13:42:45 -0600169 struct tcf_proto *tp, unsigned long base, u32 handle,
Alexander Aring7306db32018-01-18 11:20:51 -0500170 struct nlattr **tca, void **arg, bool ovr,
171 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Patrick McHardycee63722008-01-23 20:33:32 -0800173 int err;
John Fastabend9888faef2014-09-12 20:05:59 -0700174 struct basic_head *head = rtnl_dereference(tp->root);
Patrick McHardyadd93b62008-01-22 22:11:33 -0800175 struct nlattr *tb[TCA_BASIC_MAX + 1];
John Fastabend9888faef2014-09-12 20:05:59 -0700176 struct basic_filter *fold = (struct basic_filter *) *arg;
177 struct basic_filter *fnew;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Patrick McHardyadd93b62008-01-22 22:11:33 -0800179 if (tca[TCA_OPTIONS] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return -EINVAL;
181
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800182 err = nla_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
Johannes Bergfceb6432017-04-12 14:34:07 +0200183 basic_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800184 if (err < 0)
185 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
John Fastabend9888faef2014-09-12 20:05:59 -0700187 if (fold != NULL) {
188 if (handle && fold->handle != handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191
John Fastabend9888faef2014-09-12 20:05:59 -0700192 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
Jiri Pirkobd42b782014-12-05 15:50:22 +0100193 if (!fnew)
194 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
WANG Congb9a24bb2016-08-19 12:36:54 -0700196 err = tcf_exts_init(&fnew->exts, TCA_BASIC_ACT, TCA_BASIC_POLICE);
197 if (err < 0)
198 goto errout;
199
Matthew Wilcox05af0eb2017-11-28 10:36:09 -0500200 if (!handle) {
201 handle = 1;
202 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
203 INT_MAX, GFP_KERNEL);
204 } else if (!fold) {
205 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
206 handle, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
Matthew Wilcox05af0eb2017-11-28 10:36:09 -0500208 if (err)
209 goto errout;
210 fnew->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Alexander Aring50a56192018-01-18 11:20:52 -0500212 err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr,
213 extack);
Cong Wang1d8134f2017-09-25 10:13:50 -0700214 if (err < 0) {
215 if (!fold)
Matthew Wilcox9c160942017-11-28 09:48:43 -0500216 idr_remove(&head->handle_idr, fnew->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 goto errout;
Cong Wang1d8134f2017-09-25 10:13:50 -0700218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
WANG Cong8113c092017-08-04 21:31:43 -0700220 *arg = fnew;
John Fastabend9888faef2014-09-12 20:05:59 -0700221
222 if (fold) {
Matthew Wilcox234a4622017-11-28 09:56:36 -0500223 idr_replace(&head->handle_idr, fnew, fnew->handle);
John Fastabend9888faef2014-09-12 20:05:59 -0700224 list_replace_rcu(&fold->link, &fnew->link);
John Fastabend18cdb372014-10-05 21:28:52 -0700225 tcf_unbind_filter(tp, &fold->res);
Cong Wang0b2a5982017-11-06 13:47:20 -0800226 tcf_exts_get_net(&fold->exts);
Cong Wangaaa908f2018-05-23 15:26:53 -0700227 tcf_queue_work(&fold->rwork, basic_delete_filter_work);
John Fastabend9888faef2014-09-12 20:05:59 -0700228 } else {
229 list_add_rcu(&fnew->link, &head->flist);
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 return 0;
233errout:
WANG Congb9a24bb2016-08-19 12:36:54 -0700234 tcf_exts_destroy(&fnew->exts);
John Fastabend9888faef2014-09-12 20:05:59 -0700235 kfree(fnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return err;
237}
238
239static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg)
240{
John Fastabend9888faef2014-09-12 20:05:59 -0700241 struct basic_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 struct basic_filter *f;
243
244 list_for_each_entry(f, &head->flist, link) {
245 if (arg->count < arg->skip)
246 goto skip;
247
WANG Cong8113c092017-08-04 21:31:43 -0700248 if (arg->fn(tp, f, arg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 arg->stop = 1;
250 break;
251 }
252skip:
253 arg->count++;
254 }
255}
256
Cong Wang07d79fc2017-08-30 14:30:36 -0700257static void basic_bind_class(void *fh, u32 classid, unsigned long cl)
258{
259 struct basic_filter *f = fh;
260
261 if (f && f->res.classid == classid)
262 f->res.class = cl;
263}
264
WANG Cong8113c092017-08-04 21:31:43 -0700265static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 struct sk_buff *skb, struct tcmsg *t)
267{
WANG Cong8113c092017-08-04 21:31:43 -0700268 struct basic_filter *f = fh;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800269 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 if (f == NULL)
272 return skb->len;
273
274 t->tcm_handle = f->handle;
275
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800276 nest = nla_nest_start(skb, TCA_OPTIONS);
277 if (nest == NULL)
278 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
David S. Miller1b34ec42012-03-29 05:11:39 -0400280 if (f->res.classid &&
281 nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
282 goto nla_put_failure;
Thomas Grafe1e284a2005-06-08 15:11:02 -0700283
WANG Cong5da57f42013-12-15 20:15:07 -0800284 if (tcf_exts_dump(skb, &f->exts) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800286 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800288 nla_nest_end(skb, nest);
stephen hemminger4c46ee52010-11-04 11:47:04 +0000289
WANG Cong5da57f42013-12-15 20:15:07 -0800290 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
stephen hemminger4c46ee52010-11-04 11:47:04 +0000291 goto nla_put_failure;
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return skb->len;
294
Patrick McHardyadd93b62008-01-22 22:11:33 -0800295nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800296 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return -1;
298}
299
Patrick McHardy2eb9d752008-01-22 22:10:42 -0800300static struct tcf_proto_ops cls_basic_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 .kind = "basic",
302 .classify = basic_classify,
303 .init = basic_init,
304 .destroy = basic_destroy,
305 .get = basic_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 .change = basic_change,
307 .delete = basic_delete,
308 .walk = basic_walk,
309 .dump = basic_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -0700310 .bind_class = basic_bind_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 .owner = THIS_MODULE,
312};
313
314static int __init init_basic(void)
315{
316 return register_tcf_proto_ops(&cls_basic_ops);
317}
318
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900319static void __exit exit_basic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 unregister_tcf_proto_ops(&cls_basic_ops);
322}
323
324module_init(init_basic)
325module_exit(exit_basic)
326MODULE_LICENSE("GPL");
327