blob: 8158fc9ee1ab089e78de825a1767c660ac24757c [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * net/sched/cls_basic.c Basic Packet Classifier.
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Authors: Thomas Graf <tgraf@suug.ch>
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/types.h>
11#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/errno.h>
14#include <linux/rtnetlink.h>
15#include <linux/skbuff.h>
Cong Wang1d8134f2017-09-25 10:13:50 -070016#include <linux/idr.h>
Cong Wang59548942019-01-17 17:14:01 -080017#include <linux/percpu.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070018#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <net/act_api.h>
20#include <net/pkt_cls.h>
21
Eric Dumazetcc7ec452011-01-19 19:26:56 +000022struct basic_head {
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 struct list_head flist;
Cong Wang1d8134f2017-09-25 10:13:50 -070024 struct idr handle_idr;
John Fastabend9888faef2014-09-12 20:05:59 -070025 struct rcu_head rcu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026};
27
Eric Dumazetcc7ec452011-01-19 19:26:56 +000028struct basic_filter {
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 u32 handle;
30 struct tcf_exts exts;
31 struct tcf_ematch_tree ematches;
32 struct tcf_result res;
John Fastabend9888faef2014-09-12 20:05:59 -070033 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 struct list_head link;
Cong Wang59548942019-01-17 17:14:01 -080035 struct tc_basic_pcnt __percpu *pf;
Cong Wangaaa908f2018-05-23 15:26:53 -070036 struct rcu_work rwork;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037};
38
Eric Dumazetdc7f9f62011-07-05 23:25:42 +000039static int basic_classify(struct sk_buff *skb, const struct tcf_proto *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 struct tcf_result *res)
41{
42 int r;
John Fastabend9888faef2014-09-12 20:05:59 -070043 struct basic_head *head = rcu_dereference_bh(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 struct basic_filter *f;
45
John Fastabend9888faef2014-09-12 20:05:59 -070046 list_for_each_entry_rcu(f, &head->flist, link) {
Cong Wang59548942019-01-17 17:14:01 -080047 __this_cpu_inc(f->pf->rcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 if (!tcf_em_tree_match(skb, &f->ematches, NULL))
49 continue;
Cong Wang59548942019-01-17 17:14:01 -080050 __this_cpu_inc(f->pf->rhit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 *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);
Cong Wang59548942019-01-17 17:14:01 -080092 free_percpu(f->pf);
Cong Wang0b2a5982017-11-06 13:47:20 -080093 kfree(f);
94}
95
Cong Wangc96a4832017-10-26 18:24:29 -070096static void basic_delete_filter_work(struct work_struct *work)
97{
Cong Wangaaa908f2018-05-23 15:26:53 -070098 struct basic_filter *f = container_of(to_rcu_work(work),
99 struct basic_filter,
100 rwork);
Cong Wangc96a4832017-10-26 18:24:29 -0700101 rtnl_lock();
Cong Wang0b2a5982017-11-06 13:47:20 -0800102 __basic_delete_filter(f);
Cong Wangc96a4832017-10-26 18:24:29 -0700103 rtnl_unlock();
Cong Wangc96a4832017-10-26 18:24:29 -0700104}
105
Vlad Buslov12db03b2019-02-11 10:55:45 +0200106static void basic_destroy(struct tcf_proto *tp, bool rtnl_held,
107 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
John Fastabend9888faef2014-09-12 20:05:59 -0700109 struct basic_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 struct basic_filter *f, *n;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 list_for_each_entry_safe(f, n, &head->flist, link) {
John Fastabend9888faef2014-09-12 20:05:59 -0700113 list_del_rcu(&f->link);
John Fastabend18cdb372014-10-05 21:28:52 -0700114 tcf_unbind_filter(tp, &f->res);
Matthew Wilcox9c160942017-11-28 09:48:43 -0500115 idr_remove(&head->handle_idr, f->handle);
Cong Wang0b2a5982017-11-06 13:47:20 -0800116 if (tcf_exts_get_net(&f->exts))
Cong Wangaaa908f2018-05-23 15:26:53 -0700117 tcf_queue_work(&f->rwork, basic_delete_filter_work);
Cong Wang0b2a5982017-11-06 13:47:20 -0800118 else
119 __basic_delete_filter(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
Cong Wang1d8134f2017-09-25 10:13:50 -0700121 idr_destroy(&head->handle_idr);
John Fastabend9888faef2014-09-12 20:05:59 -0700122 kfree_rcu(head, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Alexander Aring571acf22018-01-18 11:20:53 -0500125static int basic_delete(struct tcf_proto *tp, void *arg, bool *last,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200126 bool rtnl_held, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
WANG Cong763dbf62017-04-19 14:21:21 -0700128 struct basic_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700129 struct basic_filter *f = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Jiri Pirkoe4386452014-12-02 18:00:31 +0100131 list_del_rcu(&f->link);
132 tcf_unbind_filter(tp, &f->res);
Matthew Wilcox9c160942017-11-28 09:48:43 -0500133 idr_remove(&head->handle_idr, f->handle);
Cong Wang0b2a5982017-11-06 13:47:20 -0800134 tcf_exts_get_net(&f->exts);
Cong Wangaaa908f2018-05-23 15:26:53 -0700135 tcf_queue_work(&f->rwork, basic_delete_filter_work);
WANG Cong763dbf62017-04-19 14:21:21 -0700136 *last = list_empty(&head->flist);
Jiri Pirkoe4386452014-12-02 18:00:31 +0100137 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800140static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
141 [TCA_BASIC_CLASSID] = { .type = NLA_U32 },
142 [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
143};
144
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000145static int basic_set_parms(struct net *net, struct tcf_proto *tp,
146 struct basic_filter *f, unsigned long base,
147 struct nlattr **tb,
Cong Wang695176b2021-07-29 16:12:14 -0700148 struct nlattr *est, u32 flags,
Alexander Aring50a56192018-01-18 11:20:52 -0500149 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
stephen hemminger64590822013-09-26 17:42:16 -0700151 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Cong Wang695176b2021-07-29 16:12:14 -0700153 err = tcf_exts_validate(net, tp, tb, est, &f->exts, flags, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (err < 0)
155 return err;
156
Jiri Pirko4ebc1e32017-08-04 14:28:57 +0200157 err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &f->ematches);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (err < 0)
Jiri Pirkoff1f8ca2017-08-04 14:29:09 +0200159 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Patrick McHardyadd93b62008-01-22 22:11:33 -0800161 if (tb[TCA_BASIC_CLASSID]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800162 f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 tcf_bind_filter(tp, &f->res, base);
164 }
165
John Fastabend9888faef2014-09-12 20:05:59 -0700166 f->tp = tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000170static int basic_change(struct net *net, struct sk_buff *in_skb,
Eric W. Biedermanaf4c6642012-05-25 13:42:45 -0600171 struct tcf_proto *tp, unsigned long base, u32 handle,
Cong Wang695176b2021-07-29 16:12:14 -0700172 struct nlattr **tca, void **arg,
173 u32 flags, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Patrick McHardycee63722008-01-23 20:33:32 -0800175 int err;
John Fastabend9888faef2014-09-12 20:05:59 -0700176 struct basic_head *head = rtnl_dereference(tp->root);
Patrick McHardyadd93b62008-01-22 22:11:33 -0800177 struct nlattr *tb[TCA_BASIC_MAX + 1];
John Fastabend9888faef2014-09-12 20:05:59 -0700178 struct basic_filter *fold = (struct basic_filter *) *arg;
179 struct basic_filter *fnew;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Patrick McHardyadd93b62008-01-22 22:11:33 -0800181 if (tca[TCA_OPTIONS] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return -EINVAL;
183
Johannes Berg8cb08172019-04-26 14:07:28 +0200184 err = nla_parse_nested_deprecated(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
185 basic_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800186 if (err < 0)
187 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
John Fastabend9888faef2014-09-12 20:05:59 -0700189 if (fold != NULL) {
190 if (handle && fold->handle != handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193
John Fastabend9888faef2014-09-12 20:05:59 -0700194 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
Jiri Pirkobd42b782014-12-05 15:50:22 +0100195 if (!fnew)
196 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Cong Wang14215102019-02-20 21:37:42 -0800198 err = tcf_exts_init(&fnew->exts, net, TCA_BASIC_ACT, TCA_BASIC_POLICE);
WANG Congb9a24bb2016-08-19 12:36:54 -0700199 if (err < 0)
200 goto errout;
201
Matthew Wilcox05af0eb2017-11-28 10:36:09 -0500202 if (!handle) {
203 handle = 1;
204 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
205 INT_MAX, GFP_KERNEL);
206 } else if (!fold) {
207 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
208 handle, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
Matthew Wilcox05af0eb2017-11-28 10:36:09 -0500210 if (err)
211 goto errout;
212 fnew->handle = handle;
Cong Wang59548942019-01-17 17:14:01 -0800213 fnew->pf = alloc_percpu(struct tc_basic_pcnt);
214 if (!fnew->pf) {
215 err = -ENOMEM;
216 goto errout;
217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Cong Wang695176b2021-07-29 16:12:14 -0700219 err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], flags,
Alexander Aring50a56192018-01-18 11:20:52 -0500220 extack);
Cong Wang1d8134f2017-09-25 10:13:50 -0700221 if (err < 0) {
222 if (!fold)
Matthew Wilcox9c160942017-11-28 09:48:43 -0500223 idr_remove(&head->handle_idr, fnew->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 goto errout;
Cong Wang1d8134f2017-09-25 10:13:50 -0700225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
WANG Cong8113c092017-08-04 21:31:43 -0700227 *arg = fnew;
John Fastabend9888faef2014-09-12 20:05:59 -0700228
229 if (fold) {
Matthew Wilcox234a4622017-11-28 09:56:36 -0500230 idr_replace(&head->handle_idr, fnew, fnew->handle);
John Fastabend9888faef2014-09-12 20:05:59 -0700231 list_replace_rcu(&fold->link, &fnew->link);
John Fastabend18cdb372014-10-05 21:28:52 -0700232 tcf_unbind_filter(tp, &fold->res);
Cong Wang0b2a5982017-11-06 13:47:20 -0800233 tcf_exts_get_net(&fold->exts);
Cong Wangaaa908f2018-05-23 15:26:53 -0700234 tcf_queue_work(&fold->rwork, basic_delete_filter_work);
John Fastabend9888faef2014-09-12 20:05:59 -0700235 } else {
236 list_add_rcu(&fnew->link, &head->flist);
237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 return 0;
240errout:
Cong Wang59548942019-01-17 17:14:01 -0800241 free_percpu(fnew->pf);
WANG Congb9a24bb2016-08-19 12:36:54 -0700242 tcf_exts_destroy(&fnew->exts);
John Fastabend9888faef2014-09-12 20:05:59 -0700243 kfree(fnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return err;
245}
246
Vlad Buslov12db03b2019-02-11 10:55:45 +0200247static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg,
248 bool rtnl_held)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
John Fastabend9888faef2014-09-12 20:05:59 -0700250 struct basic_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 struct basic_filter *f;
252
253 list_for_each_entry(f, &head->flist, link) {
254 if (arg->count < arg->skip)
255 goto skip;
256
WANG Cong8113c092017-08-04 21:31:43 -0700257 if (arg->fn(tp, f, arg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 arg->stop = 1;
259 break;
260 }
261skip:
262 arg->count++;
263 }
264}
265
Cong Wang2e24cd72020-01-23 16:26:18 -0800266static void basic_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
267 unsigned long base)
Cong Wang07d79fc2017-08-30 14:30:36 -0700268{
269 struct basic_filter *f = fh;
270
Cong Wang2e24cd72020-01-23 16:26:18 -0800271 if (f && f->res.classid == classid) {
272 if (cl)
273 __tcf_bind_filter(q, &f->res, base);
274 else
275 __tcf_unbind_filter(q, &f->res);
276 }
Cong Wang07d79fc2017-08-30 14:30:36 -0700277}
278
WANG Cong8113c092017-08-04 21:31:43 -0700279static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200280 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Cong Wang59548942019-01-17 17:14:01 -0800282 struct tc_basic_pcnt gpf = {};
WANG Cong8113c092017-08-04 21:31:43 -0700283 struct basic_filter *f = fh;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800284 struct nlattr *nest;
Cong Wang59548942019-01-17 17:14:01 -0800285 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 if (f == NULL)
288 return skb->len;
289
290 t->tcm_handle = f->handle;
291
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200292 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800293 if (nest == NULL)
294 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
David S. Miller1b34ec42012-03-29 05:11:39 -0400296 if (f->res.classid &&
297 nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
298 goto nla_put_failure;
Thomas Grafe1e284a2005-06-08 15:11:02 -0700299
Cong Wang59548942019-01-17 17:14:01 -0800300 for_each_possible_cpu(cpu) {
301 struct tc_basic_pcnt *pf = per_cpu_ptr(f->pf, cpu);
302
303 gpf.rcnt += pf->rcnt;
304 gpf.rhit += pf->rhit;
305 }
306
307 if (nla_put_64bit(skb, TCA_BASIC_PCNT,
308 sizeof(struct tc_basic_pcnt),
309 &gpf, TCA_BASIC_PAD))
310 goto nla_put_failure;
311
WANG Cong5da57f42013-12-15 20:15:07 -0800312 if (tcf_exts_dump(skb, &f->exts) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800314 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800316 nla_nest_end(skb, nest);
stephen hemminger4c46ee52010-11-04 11:47:04 +0000317
WANG Cong5da57f42013-12-15 20:15:07 -0800318 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
stephen hemminger4c46ee52010-11-04 11:47:04 +0000319 goto nla_put_failure;
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return skb->len;
322
Patrick McHardyadd93b62008-01-22 22:11:33 -0800323nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800324 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return -1;
326}
327
Patrick McHardy2eb9d752008-01-22 22:10:42 -0800328static struct tcf_proto_ops cls_basic_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 .kind = "basic",
330 .classify = basic_classify,
331 .init = basic_init,
332 .destroy = basic_destroy,
333 .get = basic_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 .change = basic_change,
335 .delete = basic_delete,
336 .walk = basic_walk,
337 .dump = basic_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -0700338 .bind_class = basic_bind_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 .owner = THIS_MODULE,
340};
341
342static int __init init_basic(void)
343{
344 return register_tcf_proto_ops(&cls_basic_ops);
345}
346
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900347static void __exit exit_basic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 unregister_tcf_proto_ops(&cls_basic_ops);
350}
351
352module_init(init_basic)
353module_exit(exit_basic)
354MODULE_LICENSE("GPL");