]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/sched/cls_basic.c
Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-jammy-kernel.git] / net / sched / cls_basic.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * net/sched/cls_basic.c Basic Packet Classifier.
4 *
1da177e4
LT
5 * Authors: Thomas Graf <tgraf@suug.ch>
6 */
7
1da177e4 8#include <linux/module.h>
5a0e3ad6 9#include <linux/slab.h>
1da177e4
LT
10#include <linux/types.h>
11#include <linux/kernel.h>
1da177e4 12#include <linux/string.h>
1da177e4
LT
13#include <linux/errno.h>
14#include <linux/rtnetlink.h>
15#include <linux/skbuff.h>
1d8134fe 16#include <linux/idr.h>
5954894b 17#include <linux/percpu.h>
dc5fc579 18#include <net/netlink.h>
1da177e4
LT
19#include <net/act_api.h>
20#include <net/pkt_cls.h>
21
cc7ec456 22struct basic_head {
1da177e4 23 struct list_head flist;
1d8134fe 24 struct idr handle_idr;
9888faef 25 struct rcu_head rcu;
1da177e4
LT
26};
27
cc7ec456 28struct basic_filter {
1da177e4
LT
29 u32 handle;
30 struct tcf_exts exts;
31 struct tcf_ematch_tree ematches;
32 struct tcf_result res;
9888faef 33 struct tcf_proto *tp;
1da177e4 34 struct list_head link;
5954894b 35 struct tc_basic_pcnt __percpu *pf;
aaa908ff 36 struct rcu_work rwork;
1da177e4
LT
37};
38
dc7f9f6e 39static int basic_classify(struct sk_buff *skb, const struct tcf_proto *tp,
1da177e4
LT
40 struct tcf_result *res)
41{
42 int r;
9888faef 43 struct basic_head *head = rcu_dereference_bh(tp->root);
1da177e4
LT
44 struct basic_filter *f;
45
9888faef 46 list_for_each_entry_rcu(f, &head->flist, link) {
5954894b 47 __this_cpu_inc(f->pf->rcnt);
1da177e4
LT
48 if (!tcf_em_tree_match(skb, &f->ematches, NULL))
49 continue;
5954894b 50 __this_cpu_inc(f->pf->rhit);
1da177e4
LT
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
8113c095 60static void *basic_get(struct tcf_proto *tp, u32 handle)
1da177e4 61{
9888faef 62 struct basic_head *head = rtnl_dereference(tp->root);
1da177e4
LT
63 struct basic_filter *f;
64
1c1bc6bd
DB
65 list_for_each_entry(f, &head->flist, link) {
66 if (f->handle == handle) {
8113c095 67 return f;
1c1bc6bd
DB
68 }
69 }
1da177e4 70
8113c095 71 return NULL;
1da177e4
LT
72}
73
1da177e4
LT
74static int basic_init(struct tcf_proto *tp)
75{
d3fa76ee
PM
76 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);
1d8134fe 82 idr_init(&head->handle_idr);
9888faef 83 rcu_assign_pointer(tp->root, head);
1da177e4
LT
84 return 0;
85}
86
0b2a5989
CW
87static 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);
5954894b 92 free_percpu(f->pf);
0b2a5989
CW
93 kfree(f);
94}
95
c96a4838 96static void basic_delete_filter_work(struct work_struct *work)
1da177e4 97{
aaa908ff
CW
98 struct basic_filter *f = container_of(to_rcu_work(work),
99 struct basic_filter,
100 rwork);
c96a4838 101 rtnl_lock();
0b2a5989 102 __basic_delete_filter(f);
c96a4838 103 rtnl_unlock();
1da177e4
LT
104}
105
12db03b6
VB
106static void basic_destroy(struct tcf_proto *tp, bool rtnl_held,
107 struct netlink_ext_ack *extack)
1da177e4 108{
9888faef 109 struct basic_head *head = rtnl_dereference(tp->root);
1da177e4 110 struct basic_filter *f, *n;
10297b99 111
1da177e4 112 list_for_each_entry_safe(f, n, &head->flist, link) {
9888faef 113 list_del_rcu(&f->link);
18cdb37e 114 tcf_unbind_filter(tp, &f->res);
9c160941 115 idr_remove(&head->handle_idr, f->handle);
0b2a5989 116 if (tcf_exts_get_net(&f->exts))
aaa908ff 117 tcf_queue_work(&f->rwork, basic_delete_filter_work);
0b2a5989
CW
118 else
119 __basic_delete_filter(f);
1da177e4 120 }
1d8134fe 121 idr_destroy(&head->handle_idr);
9888faef 122 kfree_rcu(head, rcu);
1da177e4
LT
123}
124
571acf21 125static int basic_delete(struct tcf_proto *tp, void *arg, bool *last,
12db03b6 126 bool rtnl_held, struct netlink_ext_ack *extack)
1da177e4 127{
763dbf63 128 struct basic_head *head = rtnl_dereference(tp->root);
8113c095 129 struct basic_filter *f = arg;
1da177e4 130
e4386456
JP
131 list_del_rcu(&f->link);
132 tcf_unbind_filter(tp, &f->res);
9c160941 133 idr_remove(&head->handle_idr, f->handle);
0b2a5989 134 tcf_exts_get_net(&f->exts);
aaa908ff 135 tcf_queue_work(&f->rwork, basic_delete_filter_work);
763dbf63 136 *last = list_empty(&head->flist);
e4386456 137 return 0;
1da177e4
LT
138}
139
6fa8c014
PM
140static 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
c1b52739
BL
145static int basic_set_parms(struct net *net, struct tcf_proto *tp,
146 struct basic_filter *f, unsigned long base,
147 struct nlattr **tb,
50a56190
AA
148 struct nlattr *est, bool ovr,
149 struct netlink_ext_ack *extack)
1da177e4 150{
6459082a 151 int err;
1da177e4 152
ec6743a1 153 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, true, extack);
1da177e4
LT
154 if (err < 0)
155 return err;
156
4ebc1e3c 157 err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &f->ematches);
1da177e4 158 if (err < 0)
ff1f8ca0 159 return err;
1da177e4 160
add93b61 161 if (tb[TCA_BASIC_CLASSID]) {
1587bac4 162 f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
1da177e4
LT
163 tcf_bind_filter(tp, &f->res, base);
164 }
165
9888faef 166 f->tp = tp;
1da177e4 167 return 0;
1da177e4
LT
168}
169
c1b52739 170static int basic_change(struct net *net, struct sk_buff *in_skb,
af4c6641 171 struct tcf_proto *tp, unsigned long base, u32 handle,
7306db38 172 struct nlattr **tca, void **arg, bool ovr,
12db03b6 173 bool rtnl_held, struct netlink_ext_ack *extack)
1da177e4 174{
cee63723 175 int err;
9888faef 176 struct basic_head *head = rtnl_dereference(tp->root);
add93b61 177 struct nlattr *tb[TCA_BASIC_MAX + 1];
9888faef
JF
178 struct basic_filter *fold = (struct basic_filter *) *arg;
179 struct basic_filter *fnew;
1da177e4 180
add93b61 181 if (tca[TCA_OPTIONS] == NULL)
1da177e4
LT
182 return -EINVAL;
183
8cb08174
JB
184 err = nla_parse_nested_deprecated(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
185 basic_policy, NULL);
cee63723
PM
186 if (err < 0)
187 return err;
1da177e4 188
9888faef
JF
189 if (fold != NULL) {
190 if (handle && fold->handle != handle)
1da177e4 191 return -EINVAL;
1da177e4
LT
192 }
193
9888faef 194 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
bd42b788
JP
195 if (!fnew)
196 return -ENOBUFS;
1da177e4 197
14215108 198 err = tcf_exts_init(&fnew->exts, net, TCA_BASIC_ACT, TCA_BASIC_POLICE);
b9a24bb7
WC
199 if (err < 0)
200 goto errout;
201
05af0ebb
MW
202 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);
1da177e4 209 }
05af0ebb
MW
210 if (err)
211 goto errout;
212 fnew->handle = handle;
5954894b
CW
213 fnew->pf = alloc_percpu(struct tc_basic_pcnt);
214 if (!fnew->pf) {
215 err = -ENOMEM;
216 goto errout;
217 }
1da177e4 218
50a56190
AA
219 err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr,
220 extack);
1d8134fe
CW
221 if (err < 0) {
222 if (!fold)
9c160941 223 idr_remove(&head->handle_idr, fnew->handle);
1da177e4 224 goto errout;
1d8134fe 225 }
1da177e4 226
8113c095 227 *arg = fnew;
9888faef
JF
228
229 if (fold) {
234a4624 230 idr_replace(&head->handle_idr, fnew, fnew->handle);
9888faef 231 list_replace_rcu(&fold->link, &fnew->link);
18cdb37e 232 tcf_unbind_filter(tp, &fold->res);
0b2a5989 233 tcf_exts_get_net(&fold->exts);
aaa908ff 234 tcf_queue_work(&fold->rwork, basic_delete_filter_work);
9888faef
JF
235 } else {
236 list_add_rcu(&fnew->link, &head->flist);
237 }
1da177e4
LT
238
239 return 0;
240errout:
5954894b 241 free_percpu(fnew->pf);
b9a24bb7 242 tcf_exts_destroy(&fnew->exts);
9888faef 243 kfree(fnew);
1da177e4
LT
244 return err;
245}
246
12db03b6
VB
247static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg,
248 bool rtnl_held)
1da177e4 249{
9888faef 250 struct basic_head *head = rtnl_dereference(tp->root);
1da177e4
LT
251 struct basic_filter *f;
252
253 list_for_each_entry(f, &head->flist, link) {
254 if (arg->count < arg->skip)
255 goto skip;
256
8113c095 257 if (arg->fn(tp, f, arg) < 0) {
1da177e4
LT
258 arg->stop = 1;
259 break;
260 }
261skip:
262 arg->count++;
263 }
264}
265
07d79fc7
CW
266static void basic_bind_class(void *fh, u32 classid, unsigned long cl)
267{
268 struct basic_filter *f = fh;
269
270 if (f && f->res.classid == classid)
271 f->res.class = cl;
272}
273
8113c095 274static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh,
12db03b6 275 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
1da177e4 276{
5954894b 277 struct tc_basic_pcnt gpf = {};
8113c095 278 struct basic_filter *f = fh;
4b3550ef 279 struct nlattr *nest;
5954894b 280 int cpu;
1da177e4
LT
281
282 if (f == NULL)
283 return skb->len;
284
285 t->tcm_handle = f->handle;
286
ae0be8de 287 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
4b3550ef
PM
288 if (nest == NULL)
289 goto nla_put_failure;
1da177e4 290
1b34ec43
DM
291 if (f->res.classid &&
292 nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
293 goto nla_put_failure;
e1e284a4 294
5954894b
CW
295 for_each_possible_cpu(cpu) {
296 struct tc_basic_pcnt *pf = per_cpu_ptr(f->pf, cpu);
297
298 gpf.rcnt += pf->rcnt;
299 gpf.rhit += pf->rhit;
300 }
301
302 if (nla_put_64bit(skb, TCA_BASIC_PCNT,
303 sizeof(struct tc_basic_pcnt),
304 &gpf, TCA_BASIC_PAD))
305 goto nla_put_failure;
306
5da57f42 307 if (tcf_exts_dump(skb, &f->exts) < 0 ||
1da177e4 308 tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
add93b61 309 goto nla_put_failure;
1da177e4 310
4b3550ef 311 nla_nest_end(skb, nest);
4c46ee52 312
5da57f42 313 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
4c46ee52 314 goto nla_put_failure;
315
1da177e4
LT
316 return skb->len;
317
add93b61 318nla_put_failure:
4b3550ef 319 nla_nest_cancel(skb, nest);
1da177e4
LT
320 return -1;
321}
322
2eb9d75c 323static struct tcf_proto_ops cls_basic_ops __read_mostly = {
1da177e4
LT
324 .kind = "basic",
325 .classify = basic_classify,
326 .init = basic_init,
327 .destroy = basic_destroy,
328 .get = basic_get,
1da177e4
LT
329 .change = basic_change,
330 .delete = basic_delete,
331 .walk = basic_walk,
332 .dump = basic_dump,
07d79fc7 333 .bind_class = basic_bind_class,
1da177e4
LT
334 .owner = THIS_MODULE,
335};
336
337static int __init init_basic(void)
338{
339 return register_tcf_proto_ops(&cls_basic_ops);
340}
341
10297b99 342static void __exit exit_basic(void)
1da177e4
LT
343{
344 unregister_tcf_proto_ops(&cls_basic_ops);
345}
346
347module_init(init_basic)
348module_exit(exit_basic)
349MODULE_LICENSE("GPL");