blob: c4007b9cd16d6a200d943e3e0536d6b20022ba77 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * net/sched/cls_tcindex.c Packet classifier for skb->tc_index
4 *
5 * Written 1998,1999 by Werner Almesberger, EPFL ICA
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/module.h>
9#include <linux/types.h>
10#include <linux/kernel.h>
11#include <linux/skbuff.h>
12#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Cong Wang304e0242020-03-28 12:12:59 -070014#include <linux/refcount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <net/act_api.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070016#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <net/pkt_cls.h>
Jiri Pirko1abf2722017-10-13 14:01:03 +020018#include <net/sch_generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020/*
21 * Passing parameters to the root seems to be done more awkwardly than really
22 * necessary. At least, u32 doesn't seem to use such dirty hacks. To be
23 * verified. FIXME.
24 */
25
26#define PERFECT_HASH_THRESHOLD 64 /* use perfect hash if not bigger */
27#define DEFAULT_HASH_SIZE 64 /* optimized for diffserv */
28
29
Cong Wang304e0242020-03-28 12:12:59 -070030struct tcindex_data;
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032struct tcindex_filter_result {
33 struct tcf_exts exts;
34 struct tcf_result res;
Cong Wang304e0242020-03-28 12:12:59 -070035 struct tcindex_data *p;
Cong Wangaaa908f2018-05-23 15:26:53 -070036 struct rcu_work rwork;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037};
38
39struct tcindex_filter {
40 u16 key;
41 struct tcindex_filter_result result;
John Fastabend331b7292014-09-12 20:08:20 -070042 struct tcindex_filter __rcu *next;
Cong Wangaaa908f2018-05-23 15:26:53 -070043 struct rcu_work rwork;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044};
45
46
47struct tcindex_data {
48 struct tcindex_filter_result *perfect; /* perfect hash; NULL if none */
John Fastabend331b7292014-09-12 20:08:20 -070049 struct tcindex_filter __rcu **h; /* imperfect hash; */
50 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 u16 mask; /* AND key with mask */
John Fastabend331b7292014-09-12 20:08:20 -070052 u32 shift; /* shift ANDed key to the right */
53 u32 hash; /* hash table size; 0 if undefined */
54 u32 alloc_hash; /* allocated size */
55 u32 fall_through; /* 0: only classify if explicit match */
Cong Wang304e0242020-03-28 12:12:59 -070056 refcount_t refcnt; /* a temporary refcnt for perfect hash */
Cong Wang3d210532019-02-16 10:58:26 -080057 struct rcu_work rwork;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058};
59
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -040060static inline int tcindex_filter_is_set(struct tcindex_filter_result *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Jiri Pirko6fc6d062017-08-04 14:29:00 +020062 return tcf_exts_has_actions(&r->exts) || r->res.classid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
Cong Wang304e0242020-03-28 12:12:59 -070065static void tcindex_data_get(struct tcindex_data *p)
66{
67 refcount_inc(&p->refcnt);
68}
69
70static void tcindex_data_put(struct tcindex_data *p)
71{
72 if (refcount_dec_and_test(&p->refcnt)) {
73 kfree(p->perfect);
74 kfree(p->h);
75 kfree(p);
76 }
77}
78
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -040079static struct tcindex_filter_result *tcindex_lookup(struct tcindex_data *p,
80 u16 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
John Fastabend331b7292014-09-12 20:08:20 -070082 if (p->perfect) {
83 struct tcindex_filter_result *f = p->perfect + key;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
John Fastabend331b7292014-09-12 20:08:20 -070085 return tcindex_filter_is_set(f) ? f : NULL;
86 } else if (p->h) {
87 struct tcindex_filter __rcu **fp;
88 struct tcindex_filter *f;
89
90 fp = &p->h[key % p->hash];
91 for (f = rcu_dereference_bh_rtnl(*fp);
92 f;
93 fp = &f->next, f = rcu_dereference_bh_rtnl(*fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (f->key == key)
95 return &f->result;
96 }
97
98 return NULL;
99}
100
101
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000102static int tcindex_classify(struct sk_buff *skb, const struct tcf_proto *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 struct tcf_result *res)
104{
WANG Cong2f9a2202014-09-15 14:06:48 -0700105 struct tcindex_data *p = rcu_dereference_bh(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct tcindex_filter_result *f;
107 int key = (skb->tc_index & p->mask) >> p->shift;
108
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800109 pr_debug("tcindex_classify(skb %p,tp %p,res %p),p %p\n",
110 skb, tp, res, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 f = tcindex_lookup(p, key);
113 if (!f) {
Jiri Pirko1abf2722017-10-13 14:01:03 +0200114 struct Qdisc *q = tcf_block_q(tp->chain->block);
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (!p->fall_through)
117 return -1;
Jiri Pirko1abf2722017-10-13 14:01:03 +0200118 res->classid = TC_H_MAKE(TC_H_MAJ(q->handle), key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 res->class = 0;
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800120 pr_debug("alg 0x%x\n", res->classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return 0;
122 }
123 *res = f->res;
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800124 pr_debug("map 0x%x\n", res->classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 return tcf_exts_exec(skb, &f->exts, res);
127}
128
129
WANG Cong8113c092017-08-04 21:31:43 -0700130static void *tcindex_get(struct tcf_proto *tp, u32 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
John Fastabend331b7292014-09-12 20:08:20 -0700132 struct tcindex_data *p = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 struct tcindex_filter_result *r;
134
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800135 pr_debug("tcindex_get(tp %p,handle 0x%08x)\n", tp, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (p->perfect && handle >= p->alloc_hash)
WANG Cong8113c092017-08-04 21:31:43 -0700137 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 r = tcindex_lookup(p, handle);
WANG Cong8113c092017-08-04 21:31:43 -0700139 return r && tcindex_filter_is_set(r) ? r : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static int tcindex_init(struct tcf_proto *tp)
143{
144 struct tcindex_data *p;
145
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800146 pr_debug("tcindex_init(tp %p)\n", tp);
147 p = kzalloc(sizeof(struct tcindex_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (!p)
149 return -ENOMEM;
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 p->mask = 0xffff;
152 p->hash = DEFAULT_HASH_SIZE;
153 p->fall_through = 1;
Cong Wanga8eab6d2020-04-02 20:58:51 -0700154 refcount_set(&p->refcnt, 1); /* Paired with tcindex_destroy_work() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
John Fastabend331b7292014-09-12 20:08:20 -0700156 rcu_assign_pointer(tp->root, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return 0;
158}
159
Cong Wangf2b75102017-11-06 13:47:29 -0800160static void __tcindex_destroy_rexts(struct tcindex_filter_result *r)
161{
162 tcf_exts_destroy(&r->exts);
163 tcf_exts_put_net(&r->exts);
Cong Wang304e0242020-03-28 12:12:59 -0700164 tcindex_data_put(r->p);
Cong Wangf2b75102017-11-06 13:47:29 -0800165}
166
Cong Wang27ce4f02017-10-26 18:24:39 -0700167static void tcindex_destroy_rexts_work(struct work_struct *work)
168{
169 struct tcindex_filter_result *r;
170
Cong Wangaaa908f2018-05-23 15:26:53 -0700171 r = container_of(to_rcu_work(work),
172 struct tcindex_filter_result,
173 rwork);
Cong Wang27ce4f02017-10-26 18:24:39 -0700174 rtnl_lock();
Cong Wangf2b75102017-11-06 13:47:29 -0800175 __tcindex_destroy_rexts(r);
Cong Wang27ce4f02017-10-26 18:24:39 -0700176 rtnl_unlock();
177}
178
Cong Wangf2b75102017-11-06 13:47:29 -0800179static void __tcindex_destroy_fexts(struct tcindex_filter *f)
180{
181 tcf_exts_destroy(&f->result.exts);
182 tcf_exts_put_net(&f->result.exts);
183 kfree(f);
184}
185
Cong Wang27ce4f02017-10-26 18:24:39 -0700186static void tcindex_destroy_fexts_work(struct work_struct *work)
187{
Cong Wangaaa908f2018-05-23 15:26:53 -0700188 struct tcindex_filter *f = container_of(to_rcu_work(work),
189 struct tcindex_filter,
190 rwork);
Cong Wang27ce4f02017-10-26 18:24:39 -0700191
192 rtnl_lock();
Cong Wangf2b75102017-11-06 13:47:29 -0800193 __tcindex_destroy_fexts(f);
Cong Wang27ce4f02017-10-26 18:24:39 -0700194 rtnl_unlock();
Alexei Starovoitoved7aa872015-08-25 20:06:33 -0700195}
196
Alexander Aring571acf22018-01-18 11:20:53 -0500197static int tcindex_delete(struct tcf_proto *tp, void *arg, bool *last,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200198 bool rtnl_held, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
John Fastabend331b7292014-09-12 20:08:20 -0700200 struct tcindex_data *p = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700201 struct tcindex_filter_result *r = arg;
John Fastabend331b7292014-09-12 20:08:20 -0700202 struct tcindex_filter __rcu **walk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 struct tcindex_filter *f = NULL;
204
WANG Cong8113c092017-08-04 21:31:43 -0700205 pr_debug("tcindex_delete(tp %p,arg %p),p %p\n", tp, arg, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (p->perfect) {
207 if (!r->res.class)
208 return -ENOENT;
209 } else {
210 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
John Fastabend331b7292014-09-12 20:08:20 -0700212 for (i = 0; i < p->hash; i++) {
213 walk = p->h + i;
214 for (f = rtnl_dereference(*walk); f;
215 walk = &f->next, f = rtnl_dereference(*walk)) {
216 if (&f->result == r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 goto found;
John Fastabend331b7292014-09-12 20:08:20 -0700218 }
219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return -ENOENT;
221
222found:
John Fastabend331b7292014-09-12 20:08:20 -0700223 rcu_assign_pointer(*walk, rtnl_dereference(f->next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225 tcf_unbind_filter(tp, &r->res);
Alexei Starovoitoved7aa872015-08-25 20:06:33 -0700226 /* all classifiers are required to call tcf_exts_destroy() after rcu
227 * grace period, since converted-to-rcu actions are relying on that
228 * in cleanup() callback
229 */
Cong Wangf2b75102017-11-06 13:47:29 -0800230 if (f) {
231 if (tcf_exts_get_net(&f->result.exts))
Cong Wangaaa908f2018-05-23 15:26:53 -0700232 tcf_queue_work(&f->rwork, tcindex_destroy_fexts_work);
Cong Wangf2b75102017-11-06 13:47:29 -0800233 else
234 __tcindex_destroy_fexts(f);
235 } else {
Cong Wang304e0242020-03-28 12:12:59 -0700236 tcindex_data_get(p);
237
Cong Wangf2b75102017-11-06 13:47:29 -0800238 if (tcf_exts_get_net(&r->exts))
Cong Wangaaa908f2018-05-23 15:26:53 -0700239 tcf_queue_work(&r->rwork, tcindex_destroy_rexts_work);
Cong Wangf2b75102017-11-06 13:47:29 -0800240 else
241 __tcindex_destroy_rexts(r);
242 }
WANG Cong763dbf62017-04-19 14:21:21 -0700243
244 *last = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return 0;
246}
247
Cong Wang3d210532019-02-16 10:58:26 -0800248static void tcindex_destroy_work(struct work_struct *work)
John Fastabend331b7292014-09-12 20:08:20 -0700249{
Cong Wang3d210532019-02-16 10:58:26 -0800250 struct tcindex_data *p = container_of(to_rcu_work(work),
251 struct tcindex_data,
252 rwork);
John Fastabend331b7292014-09-12 20:08:20 -0700253
Cong Wang304e0242020-03-28 12:12:59 -0700254 tcindex_data_put(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257static inline int
258valid_perfect_hash(struct tcindex_data *p)
259{
260 return p->hash > (p->mask >> p->shift);
261}
262
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800263static const struct nla_policy tcindex_policy[TCA_TCINDEX_MAX + 1] = {
264 [TCA_TCINDEX_HASH] = { .type = NLA_U32 },
265 [TCA_TCINDEX_MASK] = { .type = NLA_U16 },
266 [TCA_TCINDEX_SHIFT] = { .type = NLA_U32 },
267 [TCA_TCINDEX_FALL_THROUGH] = { .type = NLA_U32 },
268 [TCA_TCINDEX_CLASSID] = { .type = NLA_U32 },
269};
270
Cong Wang14215102019-02-20 21:37:42 -0800271static int tcindex_filter_result_init(struct tcindex_filter_result *r,
Cong Wang304e0242020-03-28 12:12:59 -0700272 struct tcindex_data *p,
Cong Wang14215102019-02-20 21:37:42 -0800273 struct net *net)
Cong Wangbf63ac72014-05-19 12:15:49 -0700274{
275 memset(r, 0, sizeof(*r));
Cong Wang304e0242020-03-28 12:12:59 -0700276 r->p = p;
Cong Wang14215102019-02-20 21:37:42 -0800277 return tcf_exts_init(&r->exts, net, TCA_TCINDEX_ACT,
278 TCA_TCINDEX_POLICE);
Cong Wangbf63ac72014-05-19 12:15:49 -0700279}
280
Cong Wang3d210532019-02-16 10:58:26 -0800281static void tcindex_partial_destroy_work(struct work_struct *work)
John Fastabend331b7292014-09-12 20:08:20 -0700282{
Cong Wang3d210532019-02-16 10:58:26 -0800283 struct tcindex_data *p = container_of(to_rcu_work(work),
284 struct tcindex_data,
285 rwork);
John Fastabend331b7292014-09-12 20:08:20 -0700286
Cong Wangb1be2e82020-03-11 22:42:27 -0700287 rtnl_lock();
John Fastabend331b7292014-09-12 20:08:20 -0700288 kfree(p->perfect);
289 kfree(p);
Cong Wangb1be2e82020-03-11 22:42:27 -0700290 rtnl_unlock();
John Fastabend331b7292014-09-12 20:08:20 -0700291}
292
WANG Congb9a24bb2016-08-19 12:36:54 -0700293static void tcindex_free_perfect_hash(struct tcindex_data *cp)
294{
295 int i;
296
297 for (i = 0; i < cp->hash; i++)
298 tcf_exts_destroy(&cp->perfect[i].exts);
299 kfree(cp->perfect);
300}
301
Cong Wang033b2282019-02-11 13:06:15 -0800302static int tcindex_alloc_perfect_hash(struct net *net, struct tcindex_data *cp)
WANG Congb9a24bb2016-08-19 12:36:54 -0700303{
304 int i, err = 0;
305
306 cp->perfect = kcalloc(cp->hash, sizeof(struct tcindex_filter_result),
307 GFP_KERNEL);
308 if (!cp->perfect)
309 return -ENOMEM;
310
311 for (i = 0; i < cp->hash; i++) {
Cong Wang14215102019-02-20 21:37:42 -0800312 err = tcf_exts_init(&cp->perfect[i].exts, net,
WANG Congb9a24bb2016-08-19 12:36:54 -0700313 TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
314 if (err < 0)
315 goto errout;
Cong Wang304e0242020-03-28 12:12:59 -0700316 cp->perfect[i].p = cp;
WANG Congb9a24bb2016-08-19 12:36:54 -0700317 }
318
319 return 0;
320
321errout:
322 tcindex_free_perfect_hash(cp);
323 return err;
324}
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326static int
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000327tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
328 u32 handle, struct tcindex_data *p,
329 struct tcindex_filter_result *r, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -0500330 struct nlattr *est, bool ovr, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 struct tcindex_filter_result new_filter_result, *old_r = r;
WANG Congb9a24bb2016-08-19 12:36:54 -0700333 struct tcindex_data *cp = NULL, *oldp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 struct tcindex_filter *f = NULL; /* make gcc behave */
Cong Wang1db817e72019-02-11 13:06:16 -0800335 struct tcf_result cr = {};
WANG Congb9a24bb2016-08-19 12:36:54 -0700336 int err, balloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 struct tcf_exts e;
338
Cong Wang14215102019-02-20 21:37:42 -0800339 err = tcf_exts_init(&e, net, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (err < 0)
341 return err;
Vlad Buslovec6743a2019-02-11 10:55:43 +0200342 err = tcf_exts_validate(net, tp, tb, est, &e, ovr, true, extack);
WANG Congb9a24bb2016-08-19 12:36:54 -0700343 if (err < 0)
344 goto errout;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900345
WANG Cong02c5e842014-09-25 12:06:04 -0700346 err = -ENOMEM;
John Fastabend331b7292014-09-12 20:08:20 -0700347 /* tcindex_data attributes must look atomic to classifier/lookup so
348 * allocate new tcindex data and RCU assign it onto root. Keeping
349 * perfect hash and hash pointers from old data.
350 */
WANG Conga57a65b2014-09-15 14:06:46 -0700351 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
WANG Cong02c5e842014-09-25 12:06:04 -0700352 if (!cp)
WANG Cong44b75e42014-09-15 16:43:42 -0700353 goto errout;
John Fastabend331b7292014-09-12 20:08:20 -0700354
355 cp->mask = p->mask;
356 cp->shift = p->shift;
357 cp->hash = p->hash;
358 cp->alloc_hash = p->alloc_hash;
359 cp->fall_through = p->fall_through;
360 cp->tp = tp;
Cong Wang304e0242020-03-28 12:12:59 -0700361 refcount_set(&cp->refcnt, 1); /* Paired with tcindex_destroy_work() */
John Fastabend331b7292014-09-12 20:08:20 -0700362
Cong Wang599be012020-02-02 21:14:35 -0800363 if (tb[TCA_TCINDEX_HASH])
364 cp->hash = nla_get_u32(tb[TCA_TCINDEX_HASH]);
365
366 if (tb[TCA_TCINDEX_MASK])
367 cp->mask = nla_get_u16(tb[TCA_TCINDEX_MASK]);
368
Eric Dumazetbcd0cf12021-01-14 10:52:29 -0800369 if (tb[TCA_TCINDEX_SHIFT]) {
Cong Wang599be012020-02-02 21:14:35 -0800370 cp->shift = nla_get_u32(tb[TCA_TCINDEX_SHIFT]);
Eric Dumazetbcd0cf12021-01-14 10:52:29 -0800371 if (cp->shift > 16) {
372 err = -EINVAL;
373 goto errout;
374 }
375 }
Cong Wang599be012020-02-02 21:14:35 -0800376 if (!cp->hash) {
377 /* Hash not specified, use perfect hash if the upper limit
378 * of the hashing index is below the threshold.
379 */
380 if ((cp->mask >> cp->shift) < PERFECT_HASH_THRESHOLD)
381 cp->hash = (cp->mask >> cp->shift) + 1;
382 else
383 cp->hash = DEFAULT_HASH_SIZE;
384 }
385
John Fastabend331b7292014-09-12 20:08:20 -0700386 if (p->perfect) {
WANG Cong6e056562014-09-30 16:07:23 -0700387 int i;
388
Cong Wang033b2282019-02-11 13:06:15 -0800389 if (tcindex_alloc_perfect_hash(net, cp) < 0)
John Fastabend331b7292014-09-12 20:08:20 -0700390 goto errout;
Cong Wang0d1c3532020-03-11 22:42:28 -0700391 cp->alloc_hash = cp->hash;
Cong Wang599be012020-02-02 21:14:35 -0800392 for (i = 0; i < min(cp->hash, p->hash); i++)
WANG Congb9a24bb2016-08-19 12:36:54 -0700393 cp->perfect[i].res = p->perfect[i].res;
WANG Cong44b75e42014-09-15 16:43:42 -0700394 balloc = 1;
John Fastabend331b7292014-09-12 20:08:20 -0700395 }
396 cp->h = p->h;
397
Cong Wang304e0242020-03-28 12:12:59 -0700398 err = tcindex_filter_result_init(&new_filter_result, cp, net);
WANG Congb9a24bb2016-08-19 12:36:54 -0700399 if (err < 0)
Cong Wang52b5ae52020-02-04 11:10:12 -0800400 goto errout_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (old_r)
Cong Wang1db817e72019-02-11 13:06:16 -0800402 cr = r->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 err = -EBUSY;
John Fastabend331b7292014-09-12 20:08:20 -0700405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 /* Hash already allocated, make sure that we still meet the
407 * requirements for the allocated hash.
408 */
John Fastabend331b7292014-09-12 20:08:20 -0700409 if (cp->perfect) {
410 if (!valid_perfect_hash(cp) ||
411 cp->hash > cp->alloc_hash)
WANG Cong44b75e42014-09-15 16:43:42 -0700412 goto errout_alloc;
John Fastabend331b7292014-09-12 20:08:20 -0700413 } else if (cp->h && cp->hash != cp->alloc_hash) {
WANG Cong44b75e42014-09-15 16:43:42 -0700414 goto errout_alloc;
John Fastabend331b7292014-09-12 20:08:20 -0700415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 err = -EINVAL;
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800418 if (tb[TCA_TCINDEX_FALL_THROUGH])
John Fastabend331b7292014-09-12 20:08:20 -0700419 cp->fall_through = nla_get_u32(tb[TCA_TCINDEX_FALL_THROUGH]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
WANG Cong68f6a7c2014-09-25 12:06:05 -0700421 if (!cp->perfect && !cp->h)
John Fastabend331b7292014-09-12 20:08:20 -0700422 cp->alloc_hash = cp->hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 /* Note: this could be as restrictive as if (handle & ~(mask >> shift))
425 * but then, we'd fail handles that may become valid after some future
426 * mask change. While this is extremely unlikely to ever matter,
427 * the check below is safer (and also more backwards-compatible).
428 */
John Fastabend331b7292014-09-12 20:08:20 -0700429 if (cp->perfect || valid_perfect_hash(cp))
430 if (handle >= cp->alloc_hash)
WANG Cong44b75e42014-09-15 16:43:42 -0700431 goto errout_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433
434 err = -ENOMEM;
John Fastabend331b7292014-09-12 20:08:20 -0700435 if (!cp->perfect && !cp->h) {
436 if (valid_perfect_hash(cp)) {
Cong Wang033b2282019-02-11 13:06:15 -0800437 if (tcindex_alloc_perfect_hash(net, cp) < 0)
WANG Cong44b75e42014-09-15 16:43:42 -0700438 goto errout_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 balloc = 1;
440 } else {
John Fastabend331b7292014-09-12 20:08:20 -0700441 struct tcindex_filter __rcu **hash;
442
443 hash = kcalloc(cp->hash,
444 sizeof(struct tcindex_filter *),
445 GFP_KERNEL);
446
447 if (!hash)
WANG Cong44b75e42014-09-15 16:43:42 -0700448 goto errout_alloc;
John Fastabend331b7292014-09-12 20:08:20 -0700449
450 cp->h = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 balloc = 2;
452 }
453 }
454
John Fastabend331b7292014-09-12 20:08:20 -0700455 if (cp->perfect)
456 r = cp->perfect + handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 else
John Fastabend331b7292014-09-12 20:08:20 -0700458 r = tcindex_lookup(cp, handle) ? : &new_filter_result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 if (r == &new_filter_result) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700461 f = kzalloc(sizeof(*f), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (!f)
463 goto errout_alloc;
WANG Cong6e056562014-09-30 16:07:23 -0700464 f->key = handle;
WANG Cong6e056562014-09-30 16:07:23 -0700465 f->next = NULL;
Cong Wang304e0242020-03-28 12:12:59 -0700466 err = tcindex_filter_result_init(&f->result, cp, net);
WANG Congb9a24bb2016-08-19 12:36:54 -0700467 if (err < 0) {
468 kfree(f);
469 goto errout_alloc;
470 }
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Patrick McHardyadd93b62008-01-22 22:11:33 -0800473 if (tb[TCA_TCINDEX_CLASSID]) {
Cong Wang1db817e72019-02-11 13:06:16 -0800474 cr.classid = nla_get_u32(tb[TCA_TCINDEX_CLASSID]);
475 tcf_bind_filter(tp, &cr, base);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
WANG Congb9a24bb2016-08-19 12:36:54 -0700478 if (old_r && old_r != r) {
Cong Wang304e0242020-03-28 12:12:59 -0700479 err = tcindex_filter_result_init(old_r, cp, net);
WANG Congb9a24bb2016-08-19 12:36:54 -0700480 if (err < 0) {
481 kfree(f);
482 goto errout_alloc;
483 }
484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
John Fastabend331b7292014-09-12 20:08:20 -0700486 oldp = p;
Cong Wang1db817e72019-02-11 13:06:16 -0800487 r->res = cr;
Hangbin Liu2df8bee2018-08-13 18:44:03 +0800488 tcf_exts_change(&r->exts, &e);
489
John Fastabend331b7292014-09-12 20:08:20 -0700490 rcu_assign_pointer(tp->root, cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 if (r == &new_filter_result) {
John Fastabend331b7292014-09-12 20:08:20 -0700493 struct tcindex_filter *nfp;
494 struct tcindex_filter __rcu **fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Hangbin Liu008369d2018-08-13 18:44:04 +0800496 f->result.res = r->res;
Jiri Pirko9b0d4442017-08-04 14:29:15 +0200497 tcf_exts_change(&f->result.exts, &r->exts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
WANG Cong69301ea2014-09-15 16:43:43 -0700499 fp = cp->h + (handle % cp->hash);
John Fastabend331b7292014-09-12 20:08:20 -0700500 for (nfp = rtnl_dereference(*fp);
501 nfp;
502 fp = &nfp->next, nfp = rtnl_dereference(*fp))
503 ; /* nothing */
504
505 rcu_assign_pointer(*fp, f);
Cong Wang1db817e72019-02-11 13:06:16 -0800506 } else {
507 tcf_exts_destroy(&new_filter_result.exts);
John Fastabend331b7292014-09-12 20:08:20 -0700508 }
509
510 if (oldp)
Cong Wang3d210532019-02-16 10:58:26 -0800511 tcf_queue_work(&oldp->rwork, tcindex_partial_destroy_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return 0;
513
514errout_alloc:
515 if (balloc == 1)
WANG Congb9a24bb2016-08-19 12:36:54 -0700516 tcindex_free_perfect_hash(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 else if (balloc == 2)
John Fastabend331b7292014-09-12 20:08:20 -0700518 kfree(cp->h);
WANG Congb9a24bb2016-08-19 12:36:54 -0700519 tcf_exts_destroy(&new_filter_result.exts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520errout:
John Fastabend331b7292014-09-12 20:08:20 -0700521 kfree(cp);
WANG Cong18d02642014-09-25 10:26:37 -0700522 tcf_exts_destroy(&e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return err;
524}
525
526static int
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000527tcindex_change(struct net *net, struct sk_buff *in_skb,
Eric W. Biedermanaf4c6642012-05-25 13:42:45 -0600528 struct tcf_proto *tp, unsigned long base, u32 handle,
Alexander Aring7306db32018-01-18 11:20:51 -0500529 struct nlattr **tca, void **arg, bool ovr,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200530 bool rtnl_held, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Patrick McHardyadd93b62008-01-22 22:11:33 -0800532 struct nlattr *opt = tca[TCA_OPTIONS];
533 struct nlattr *tb[TCA_TCINDEX_MAX + 1];
John Fastabend331b7292014-09-12 20:08:20 -0700534 struct tcindex_data *p = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700535 struct tcindex_filter_result *r = *arg;
Patrick McHardycee63722008-01-23 20:33:32 -0800536 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800538 pr_debug("tcindex_change(tp %p,handle 0x%08x,tca %p,arg %p),opt %p,"
WANG Cong8113c092017-08-04 21:31:43 -0700539 "p %p,r %p,*arg %p\n",
Gaurav Singhc5efcf12020-06-21 22:24:30 -0400540 tp, handle, tca, arg, opt, p, r, *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 if (!opt)
543 return 0;
544
Johannes Berg8cb08172019-04-26 14:07:28 +0200545 err = nla_parse_nested_deprecated(tb, TCA_TCINDEX_MAX, opt,
546 tcindex_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800547 if (err < 0)
548 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000550 return tcindex_set_parms(net, tp, base, handle, p, r, tb,
Alexander Aring50a56192018-01-18 11:20:52 -0500551 tca[TCA_RATE], ovr, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Vlad Buslov12db03b2019-02-11 10:55:45 +0200554static void tcindex_walk(struct tcf_proto *tp, struct tcf_walker *walker,
555 bool rtnl_held)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
John Fastabend331b7292014-09-12 20:08:20 -0700557 struct tcindex_data *p = rtnl_dereference(tp->root);
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800558 struct tcindex_filter *f, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 int i;
560
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800561 pr_debug("tcindex_walk(tp %p,walker %p),p %p\n", tp, walker, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (p->perfect) {
563 for (i = 0; i < p->hash; i++) {
564 if (!p->perfect[i].res.class)
565 continue;
566 if (walker->count >= walker->skip) {
WANG Cong8113c092017-08-04 21:31:43 -0700567 if (walker->fn(tp, p->perfect + i, walker) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 walker->stop = 1;
569 return;
570 }
571 }
572 walker->count++;
573 }
574 }
575 if (!p->h)
576 return;
577 for (i = 0; i < p->hash; i++) {
John Fastabend331b7292014-09-12 20:08:20 -0700578 for (f = rtnl_dereference(p->h[i]); f; f = next) {
579 next = rtnl_dereference(f->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (walker->count >= walker->skip) {
WANG Cong8113c092017-08-04 21:31:43 -0700581 if (walker->fn(tp, &f->result, walker) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 walker->stop = 1;
583 return;
584 }
585 }
586 walker->count++;
587 }
588 }
589}
590
Vlad Buslov12db03b2019-02-11 10:55:45 +0200591static void tcindex_destroy(struct tcf_proto *tp, bool rtnl_held,
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800592 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
John Fastabend331b7292014-09-12 20:08:20 -0700594 struct tcindex_data *p = rtnl_dereference(tp->root);
Cong Wang51dcb692019-02-16 10:58:27 -0800595 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800597 pr_debug("tcindex_destroy(tp %p),p %p\n", tp, p);
Cong Wang51dcb692019-02-16 10:58:27 -0800598
599 if (p->perfect) {
600 for (i = 0; i < p->hash; i++) {
601 struct tcindex_filter_result *r = p->perfect + i;
602
Cong Wang304e0242020-03-28 12:12:59 -0700603 /* tcf_queue_work() does not guarantee the ordering we
604 * want, so we have to take this refcnt temporarily to
605 * ensure 'p' is freed after all tcindex_filter_result
606 * here. Imperfect hash does not need this, because it
607 * uses linked lists rather than an array.
608 */
609 tcindex_data_get(p);
610
Cong Wang51dcb692019-02-16 10:58:27 -0800611 tcf_unbind_filter(tp, &r->res);
612 if (tcf_exts_get_net(&r->exts))
613 tcf_queue_work(&r->rwork,
614 tcindex_destroy_rexts_work);
615 else
616 __tcindex_destroy_rexts(r);
617 }
618 }
619
620 for (i = 0; p->h && i < p->hash; i++) {
621 struct tcindex_filter *f, *next;
622 bool last;
623
624 for (f = rtnl_dereference(p->h[i]); f; f = next) {
625 next = rtnl_dereference(f->next);
626 tcindex_delete(tp, &f->result, &last, rtnl_held, NULL);
627 }
628 }
John Fastabend331b7292014-09-12 20:08:20 -0700629
Cong Wang3d210532019-02-16 10:58:26 -0800630 tcf_queue_work(&p->rwork, tcindex_destroy_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633
WANG Cong8113c092017-08-04 21:31:43 -0700634static int tcindex_dump(struct net *net, struct tcf_proto *tp, void *fh,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200635 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
John Fastabend331b7292014-09-12 20:08:20 -0700637 struct tcindex_data *p = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700638 struct tcindex_filter_result *r = fh;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800639 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
WANG Cong8113c092017-08-04 21:31:43 -0700641 pr_debug("tcindex_dump(tp %p,fh %p,skb %p,t %p),p %p,r %p\n",
Jiri Pirko6ea3b442014-12-09 22:23:29 +0100642 tp, fh, skb, t, p, r);
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800643 pr_debug("p->perfect %p p->h %p\n", p->perfect, p->h);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800644
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200645 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800646 if (nest == NULL)
647 goto nla_put_failure;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (!fh) {
650 t->tcm_handle = ~0; /* whatever ... */
David S. Miller1b34ec42012-03-29 05:11:39 -0400651 if (nla_put_u32(skb, TCA_TCINDEX_HASH, p->hash) ||
652 nla_put_u16(skb, TCA_TCINDEX_MASK, p->mask) ||
653 nla_put_u32(skb, TCA_TCINDEX_SHIFT, p->shift) ||
654 nla_put_u32(skb, TCA_TCINDEX_FALL_THROUGH, p->fall_through))
655 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800656 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 } else {
658 if (p->perfect) {
John Fastabend331b7292014-09-12 20:08:20 -0700659 t->tcm_handle = r - p->perfect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 } else {
661 struct tcindex_filter *f;
John Fastabend331b7292014-09-12 20:08:20 -0700662 struct tcindex_filter __rcu **fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 int i;
664
665 t->tcm_handle = 0;
666 for (i = 0; !t->tcm_handle && i < p->hash; i++) {
John Fastabend331b7292014-09-12 20:08:20 -0700667 fp = &p->h[i];
668 for (f = rtnl_dereference(*fp);
669 !t->tcm_handle && f;
670 fp = &f->next, f = rtnl_dereference(*fp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (&f->result == r)
672 t->tcm_handle = f->key;
673 }
674 }
675 }
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800676 pr_debug("handle = %d\n", t->tcm_handle);
David S. Miller1b34ec42012-03-29 05:11:39 -0400677 if (r->res.class &&
678 nla_put_u32(skb, TCA_TCINDEX_CLASSID, r->res.classid))
679 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
WANG Cong5da57f42013-12-15 20:15:07 -0800681 if (tcf_exts_dump(skb, &r->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800682 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800683 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
WANG Cong5da57f42013-12-15 20:15:07 -0800685 if (tcf_exts_dump_stats(skb, &r->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800686 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return skb->len;
690
Patrick McHardyadd93b62008-01-22 22:11:33 -0800691nla_put_failure:
Jiri Pirko6ea3b442014-12-09 22:23:29 +0100692 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return -1;
694}
695
Cong Wang2e24cd72020-01-23 16:26:18 -0800696static void tcindex_bind_class(void *fh, u32 classid, unsigned long cl,
697 void *q, unsigned long base)
Cong Wang07d79fc2017-08-30 14:30:36 -0700698{
699 struct tcindex_filter_result *r = fh;
700
Cong Wang2e24cd72020-01-23 16:26:18 -0800701 if (r && r->res.classid == classid) {
702 if (cl)
703 __tcf_bind_filter(q, &r->res, base);
704 else
705 __tcf_unbind_filter(q, &r->res);
706 }
Cong Wang07d79fc2017-08-30 14:30:36 -0700707}
708
Patrick McHardy2eb9d752008-01-22 22:10:42 -0800709static struct tcf_proto_ops cls_tcindex_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 .kind = "tcindex",
711 .classify = tcindex_classify,
712 .init = tcindex_init,
713 .destroy = tcindex_destroy,
714 .get = tcindex_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 .change = tcindex_change,
716 .delete = tcindex_delete,
717 .walk = tcindex_walk,
718 .dump = tcindex_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -0700719 .bind_class = tcindex_bind_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 .owner = THIS_MODULE,
721};
722
723static int __init init_tcindex(void)
724{
725 return register_tcf_proto_ops(&cls_tcindex_ops);
726}
727
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900728static void __exit exit_tcindex(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 unregister_tcf_proto_ops(&cls_tcindex_ops);
731}
732
733module_init(init_tcindex)
734module_exit(exit_tcindex)
735MODULE_LICENSE("GPL");