blob: dd5fdb62c6df51009c640dd467718e8184f2632d [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>
Cong Wang59548942019-01-17 17:14:01 -080021#include <linux/percpu.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070022#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <net/act_api.h>
24#include <net/pkt_cls.h>
25
Eric Dumazetcc7ec452011-01-19 19:26:56 +000026struct basic_head {
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 struct list_head flist;
Cong Wang1d8134f2017-09-25 10:13:50 -070028 struct idr handle_idr;
John Fastabend9888faef2014-09-12 20:05:59 -070029 struct rcu_head rcu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030};
31
Eric Dumazetcc7ec452011-01-19 19:26:56 +000032struct basic_filter {
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 u32 handle;
34 struct tcf_exts exts;
35 struct tcf_ematch_tree ematches;
36 struct tcf_result res;
John Fastabend9888faef2014-09-12 20:05:59 -070037 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct list_head link;
Cong Wang59548942019-01-17 17:14:01 -080039 struct tc_basic_pcnt __percpu *pf;
Cong Wangaaa908f2018-05-23 15:26:53 -070040 struct rcu_work rwork;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041};
42
Eric Dumazetdc7f9f62011-07-05 23:25:42 +000043static int basic_classify(struct sk_buff *skb, const struct tcf_proto *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 struct tcf_result *res)
45{
46 int r;
John Fastabend9888faef2014-09-12 20:05:59 -070047 struct basic_head *head = rcu_dereference_bh(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct basic_filter *f;
49
John Fastabend9888faef2014-09-12 20:05:59 -070050 list_for_each_entry_rcu(f, &head->flist, link) {
Cong Wang59548942019-01-17 17:14:01 -080051 __this_cpu_inc(f->pf->rcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if (!tcf_em_tree_match(skb, &f->ematches, NULL))
53 continue;
Cong Wang59548942019-01-17 17:14:01 -080054 __this_cpu_inc(f->pf->rhit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 *res = f->res;
56 r = tcf_exts_exec(skb, &f->exts, res);
57 if (r < 0)
58 continue;
59 return r;
60 }
61 return -1;
62}
63
WANG Cong8113c092017-08-04 21:31:43 -070064static void *basic_get(struct tcf_proto *tp, u32 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
John Fastabend9888faef2014-09-12 20:05:59 -070066 struct basic_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 struct basic_filter *f;
68
Daniel Borkmann1c1bc6b2015-01-22 10:58:18 +010069 list_for_each_entry(f, &head->flist, link) {
70 if (f->handle == handle) {
WANG Cong8113c092017-08-04 21:31:43 -070071 return f;
Daniel Borkmann1c1bc6b2015-01-22 10:58:18 +010072 }
73 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
WANG Cong8113c092017-08-04 21:31:43 -070075 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int basic_init(struct tcf_proto *tp)
79{
Patrick McHardyd3fa76e2007-03-24 22:13:06 -070080 struct basic_head *head;
81
82 head = kzalloc(sizeof(*head), GFP_KERNEL);
83 if (head == NULL)
84 return -ENOBUFS;
85 INIT_LIST_HEAD(&head->flist);
Cong Wang1d8134f2017-09-25 10:13:50 -070086 idr_init(&head->handle_idr);
John Fastabend9888faef2014-09-12 20:05:59 -070087 rcu_assign_pointer(tp->root, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return 0;
89}
90
Cong Wang0b2a5982017-11-06 13:47:20 -080091static void __basic_delete_filter(struct basic_filter *f)
92{
93 tcf_exts_destroy(&f->exts);
94 tcf_em_tree_destroy(&f->ematches);
95 tcf_exts_put_net(&f->exts);
Cong Wang59548942019-01-17 17:14:01 -080096 free_percpu(f->pf);
Cong Wang0b2a5982017-11-06 13:47:20 -080097 kfree(f);
98}
99
Cong Wangc96a4832017-10-26 18:24:29 -0700100static void basic_delete_filter_work(struct work_struct *work)
101{
Cong Wangaaa908f2018-05-23 15:26:53 -0700102 struct basic_filter *f = container_of(to_rcu_work(work),
103 struct basic_filter,
104 rwork);
Cong Wangc96a4832017-10-26 18:24:29 -0700105 rtnl_lock();
Cong Wang0b2a5982017-11-06 13:47:20 -0800106 __basic_delete_filter(f);
Cong Wangc96a4832017-10-26 18:24:29 -0700107 rtnl_unlock();
Cong Wangc96a4832017-10-26 18:24:29 -0700108}
109
Vlad Buslov12db03b2019-02-11 10:55:45 +0200110static void basic_destroy(struct tcf_proto *tp, bool rtnl_held,
111 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
John Fastabend9888faef2014-09-12 20:05:59 -0700113 struct basic_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 struct basic_filter *f, *n;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 list_for_each_entry_safe(f, n, &head->flist, link) {
John Fastabend9888faef2014-09-12 20:05:59 -0700117 list_del_rcu(&f->link);
John Fastabend18cdb372014-10-05 21:28:52 -0700118 tcf_unbind_filter(tp, &f->res);
Matthew Wilcox9c160942017-11-28 09:48:43 -0500119 idr_remove(&head->handle_idr, f->handle);
Cong Wang0b2a5982017-11-06 13:47:20 -0800120 if (tcf_exts_get_net(&f->exts))
Cong Wangaaa908f2018-05-23 15:26:53 -0700121 tcf_queue_work(&f->rwork, basic_delete_filter_work);
Cong Wang0b2a5982017-11-06 13:47:20 -0800122 else
123 __basic_delete_filter(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Cong Wang1d8134f2017-09-25 10:13:50 -0700125 idr_destroy(&head->handle_idr);
John Fastabend9888faef2014-09-12 20:05:59 -0700126 kfree_rcu(head, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
Alexander Aring571acf22018-01-18 11:20:53 -0500129static int basic_delete(struct tcf_proto *tp, void *arg, bool *last,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200130 bool rtnl_held, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
WANG Cong763dbf62017-04-19 14:21:21 -0700132 struct basic_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700133 struct basic_filter *f = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Jiri Pirkoe4386452014-12-02 18:00:31 +0100135 list_del_rcu(&f->link);
136 tcf_unbind_filter(tp, &f->res);
Matthew Wilcox9c160942017-11-28 09:48:43 -0500137 idr_remove(&head->handle_idr, f->handle);
Cong Wang0b2a5982017-11-06 13:47:20 -0800138 tcf_exts_get_net(&f->exts);
Cong Wangaaa908f2018-05-23 15:26:53 -0700139 tcf_queue_work(&f->rwork, basic_delete_filter_work);
WANG Cong763dbf62017-04-19 14:21:21 -0700140 *last = list_empty(&head->flist);
Jiri Pirkoe4386452014-12-02 18:00:31 +0100141 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800144static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
145 [TCA_BASIC_CLASSID] = { .type = NLA_U32 },
146 [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
147};
148
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000149static int basic_set_parms(struct net *net, struct tcf_proto *tp,
150 struct basic_filter *f, unsigned long base,
151 struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -0500152 struct nlattr *est, bool ovr,
153 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
stephen hemminger64590822013-09-26 17:42:16 -0700155 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Vlad Buslovec6743a2019-02-11 10:55:43 +0200157 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, true, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (err < 0)
159 return err;
160
Jiri Pirko4ebc1e32017-08-04 14:28:57 +0200161 err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &f->ematches);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (err < 0)
Jiri Pirkoff1f8ca2017-08-04 14:29:09 +0200163 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Patrick McHardyadd93b62008-01-22 22:11:33 -0800165 if (tb[TCA_BASIC_CLASSID]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800166 f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 tcf_bind_filter(tp, &f->res, base);
168 }
169
John Fastabend9888faef2014-09-12 20:05:59 -0700170 f->tp = tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000174static int basic_change(struct net *net, struct sk_buff *in_skb,
Eric W. Biedermanaf4c6642012-05-25 13:42:45 -0600175 struct tcf_proto *tp, unsigned long base, u32 handle,
Alexander Aring7306db32018-01-18 11:20:51 -0500176 struct nlattr **tca, void **arg, bool ovr,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200177 bool rtnl_held, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Patrick McHardycee63722008-01-23 20:33:32 -0800179 int err;
John Fastabend9888faef2014-09-12 20:05:59 -0700180 struct basic_head *head = rtnl_dereference(tp->root);
Patrick McHardyadd93b62008-01-22 22:11:33 -0800181 struct nlattr *tb[TCA_BASIC_MAX + 1];
John Fastabend9888faef2014-09-12 20:05:59 -0700182 struct basic_filter *fold = (struct basic_filter *) *arg;
183 struct basic_filter *fnew;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Patrick McHardyadd93b62008-01-22 22:11:33 -0800185 if (tca[TCA_OPTIONS] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return -EINVAL;
187
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800188 err = nla_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
Johannes Bergfceb6432017-04-12 14:34:07 +0200189 basic_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800190 if (err < 0)
191 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
John Fastabend9888faef2014-09-12 20:05:59 -0700193 if (fold != NULL) {
194 if (handle && fold->handle != handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197
John Fastabend9888faef2014-09-12 20:05:59 -0700198 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
Jiri Pirkobd42b782014-12-05 15:50:22 +0100199 if (!fnew)
200 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Cong Wang14215102019-02-20 21:37:42 -0800202 err = tcf_exts_init(&fnew->exts, net, TCA_BASIC_ACT, TCA_BASIC_POLICE);
WANG Congb9a24bb2016-08-19 12:36:54 -0700203 if (err < 0)
204 goto errout;
205
Matthew Wilcox05af0eb2017-11-28 10:36:09 -0500206 if (!handle) {
207 handle = 1;
208 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
209 INT_MAX, GFP_KERNEL);
210 } else if (!fold) {
211 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
212 handle, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
Matthew Wilcox05af0eb2017-11-28 10:36:09 -0500214 if (err)
215 goto errout;
216 fnew->handle = handle;
Cong Wang59548942019-01-17 17:14:01 -0800217 fnew->pf = alloc_percpu(struct tc_basic_pcnt);
218 if (!fnew->pf) {
219 err = -ENOMEM;
220 goto errout;
221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Alexander Aring50a56192018-01-18 11:20:52 -0500223 err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr,
224 extack);
Cong Wang1d8134f2017-09-25 10:13:50 -0700225 if (err < 0) {
226 if (!fold)
Matthew Wilcox9c160942017-11-28 09:48:43 -0500227 idr_remove(&head->handle_idr, fnew->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 goto errout;
Cong Wang1d8134f2017-09-25 10:13:50 -0700229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
WANG Cong8113c092017-08-04 21:31:43 -0700231 *arg = fnew;
John Fastabend9888faef2014-09-12 20:05:59 -0700232
233 if (fold) {
Matthew Wilcox234a4622017-11-28 09:56:36 -0500234 idr_replace(&head->handle_idr, fnew, fnew->handle);
John Fastabend9888faef2014-09-12 20:05:59 -0700235 list_replace_rcu(&fold->link, &fnew->link);
John Fastabend18cdb372014-10-05 21:28:52 -0700236 tcf_unbind_filter(tp, &fold->res);
Cong Wang0b2a5982017-11-06 13:47:20 -0800237 tcf_exts_get_net(&fold->exts);
Cong Wangaaa908f2018-05-23 15:26:53 -0700238 tcf_queue_work(&fold->rwork, basic_delete_filter_work);
John Fastabend9888faef2014-09-12 20:05:59 -0700239 } else {
240 list_add_rcu(&fnew->link, &head->flist);
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 return 0;
244errout:
Cong Wang59548942019-01-17 17:14:01 -0800245 free_percpu(fnew->pf);
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
Vlad Buslov12db03b2019-02-11 10:55:45 +0200251static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg,
252 bool rtnl_held)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
John Fastabend9888faef2014-09-12 20:05:59 -0700254 struct basic_head *head = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 struct basic_filter *f;
256
257 list_for_each_entry(f, &head->flist, link) {
258 if (arg->count < arg->skip)
259 goto skip;
260
WANG Cong8113c092017-08-04 21:31:43 -0700261 if (arg->fn(tp, f, arg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 arg->stop = 1;
263 break;
264 }
265skip:
266 arg->count++;
267 }
268}
269
Cong Wang07d79fc2017-08-30 14:30:36 -0700270static void basic_bind_class(void *fh, u32 classid, unsigned long cl)
271{
272 struct basic_filter *f = fh;
273
274 if (f && f->res.classid == classid)
275 f->res.class = cl;
276}
277
WANG Cong8113c092017-08-04 21:31:43 -0700278static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200279 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Cong Wang59548942019-01-17 17:14:01 -0800281 struct tc_basic_pcnt gpf = {};
WANG Cong8113c092017-08-04 21:31:43 -0700282 struct basic_filter *f = fh;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800283 struct nlattr *nest;
Cong Wang59548942019-01-17 17:14:01 -0800284 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 if (f == NULL)
287 return skb->len;
288
289 t->tcm_handle = f->handle;
290
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200291 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800292 if (nest == NULL)
293 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
David S. Miller1b34ec42012-03-29 05:11:39 -0400295 if (f->res.classid &&
296 nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
297 goto nla_put_failure;
Thomas Grafe1e284a2005-06-08 15:11:02 -0700298
Cong Wang59548942019-01-17 17:14:01 -0800299 for_each_possible_cpu(cpu) {
300 struct tc_basic_pcnt *pf = per_cpu_ptr(f->pf, cpu);
301
302 gpf.rcnt += pf->rcnt;
303 gpf.rhit += pf->rhit;
304 }
305
306 if (nla_put_64bit(skb, TCA_BASIC_PCNT,
307 sizeof(struct tc_basic_pcnt),
308 &gpf, TCA_BASIC_PAD))
309 goto nla_put_failure;
310
WANG Cong5da57f42013-12-15 20:15:07 -0800311 if (tcf_exts_dump(skb, &f->exts) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800313 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800315 nla_nest_end(skb, nest);
stephen hemminger4c46ee52010-11-04 11:47:04 +0000316
WANG Cong5da57f42013-12-15 20:15:07 -0800317 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
stephen hemminger4c46ee52010-11-04 11:47:04 +0000318 goto nla_put_failure;
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return skb->len;
321
Patrick McHardyadd93b62008-01-22 22:11:33 -0800322nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800323 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return -1;
325}
326
Patrick McHardy2eb9d752008-01-22 22:10:42 -0800327static struct tcf_proto_ops cls_basic_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 .kind = "basic",
329 .classify = basic_classify,
330 .init = basic_init,
331 .destroy = basic_destroy,
332 .get = basic_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 .change = basic_change,
334 .delete = basic_delete,
335 .walk = basic_walk,
336 .dump = basic_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -0700337 .bind_class = basic_bind_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 .owner = THIS_MODULE,
339};
340
341static int __init init_basic(void)
342{
343 return register_tcf_proto_ops(&cls_basic_ops);
344}
345
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900346static void __exit exit_basic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 unregister_tcf_proto_ops(&cls_basic_ops);
349}
350
351module_init(init_basic)
352module_exit(exit_basic)
353MODULE_LICENSE("GPL");