blob: 9904299424a1ce729cfc0fe996ad1c9771e176dc [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <net/act_api.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070015#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <net/pkt_cls.h>
Jiri Pirko1abf2722017-10-13 14:01:03 +020017#include <net/sch_generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019/*
20 * Passing parameters to the root seems to be done more awkwardly than really
21 * necessary. At least, u32 doesn't seem to use such dirty hacks. To be
22 * verified. FIXME.
23 */
24
25#define PERFECT_HASH_THRESHOLD 64 /* use perfect hash if not bigger */
26#define DEFAULT_HASH_SIZE 64 /* optimized for diffserv */
27
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029struct tcindex_filter_result {
30 struct tcf_exts exts;
31 struct tcf_result res;
Cong Wangaaa908f2018-05-23 15:26:53 -070032 struct rcu_work rwork;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033};
34
35struct tcindex_filter {
36 u16 key;
37 struct tcindex_filter_result result;
John Fastabend331b7292014-09-12 20:08:20 -070038 struct tcindex_filter __rcu *next;
Cong Wangaaa908f2018-05-23 15:26:53 -070039 struct rcu_work rwork;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040};
41
42
43struct tcindex_data {
44 struct tcindex_filter_result *perfect; /* perfect hash; NULL if none */
John Fastabend331b7292014-09-12 20:08:20 -070045 struct tcindex_filter __rcu **h; /* imperfect hash; */
46 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 u16 mask; /* AND key with mask */
John Fastabend331b7292014-09-12 20:08:20 -070048 u32 shift; /* shift ANDed key to the right */
49 u32 hash; /* hash table size; 0 if undefined */
50 u32 alloc_hash; /* allocated size */
51 u32 fall_through; /* 0: only classify if explicit match */
Cong Wang3d210532019-02-16 10:58:26 -080052 struct rcu_work rwork;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053};
54
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -040055static inline int tcindex_filter_is_set(struct tcindex_filter_result *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Jiri Pirko6fc6d062017-08-04 14:29:00 +020057 return tcf_exts_has_actions(&r->exts) || r->res.classid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -040060static struct tcindex_filter_result *tcindex_lookup(struct tcindex_data *p,
61 u16 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
John Fastabend331b7292014-09-12 20:08:20 -070063 if (p->perfect) {
64 struct tcindex_filter_result *f = p->perfect + key;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
John Fastabend331b7292014-09-12 20:08:20 -070066 return tcindex_filter_is_set(f) ? f : NULL;
67 } else if (p->h) {
68 struct tcindex_filter __rcu **fp;
69 struct tcindex_filter *f;
70
71 fp = &p->h[key % p->hash];
72 for (f = rcu_dereference_bh_rtnl(*fp);
73 f;
74 fp = &f->next, f = rcu_dereference_bh_rtnl(*fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (f->key == key)
76 return &f->result;
77 }
78
79 return NULL;
80}
81
82
Eric Dumazetdc7f9f62011-07-05 23:25:42 +000083static int tcindex_classify(struct sk_buff *skb, const struct tcf_proto *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 struct tcf_result *res)
85{
WANG Cong2f9a2202014-09-15 14:06:48 -070086 struct tcindex_data *p = rcu_dereference_bh(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 struct tcindex_filter_result *f;
88 int key = (skb->tc_index & p->mask) >> p->shift;
89
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -080090 pr_debug("tcindex_classify(skb %p,tp %p,res %p),p %p\n",
91 skb, tp, res, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 f = tcindex_lookup(p, key);
94 if (!f) {
Jiri Pirko1abf2722017-10-13 14:01:03 +020095 struct Qdisc *q = tcf_block_q(tp->chain->block);
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (!p->fall_through)
98 return -1;
Jiri Pirko1abf2722017-10-13 14:01:03 +020099 res->classid = TC_H_MAKE(TC_H_MAJ(q->handle), key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 res->class = 0;
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800101 pr_debug("alg 0x%x\n", res->classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return 0;
103 }
104 *res = f->res;
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800105 pr_debug("map 0x%x\n", res->classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 return tcf_exts_exec(skb, &f->exts, res);
108}
109
110
WANG Cong8113c092017-08-04 21:31:43 -0700111static void *tcindex_get(struct tcf_proto *tp, u32 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
John Fastabend331b7292014-09-12 20:08:20 -0700113 struct tcindex_data *p = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 struct tcindex_filter_result *r;
115
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800116 pr_debug("tcindex_get(tp %p,handle 0x%08x)\n", tp, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (p->perfect && handle >= p->alloc_hash)
WANG Cong8113c092017-08-04 21:31:43 -0700118 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 r = tcindex_lookup(p, handle);
WANG Cong8113c092017-08-04 21:31:43 -0700120 return r && tcindex_filter_is_set(r) ? r : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static int tcindex_init(struct tcf_proto *tp)
124{
125 struct tcindex_data *p;
126
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800127 pr_debug("tcindex_init(tp %p)\n", tp);
128 p = kzalloc(sizeof(struct tcindex_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (!p)
130 return -ENOMEM;
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 p->mask = 0xffff;
133 p->hash = DEFAULT_HASH_SIZE;
134 p->fall_through = 1;
135
John Fastabend331b7292014-09-12 20:08:20 -0700136 rcu_assign_pointer(tp->root, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return 0;
138}
139
Cong Wangf2b75102017-11-06 13:47:29 -0800140static void __tcindex_destroy_rexts(struct tcindex_filter_result *r)
141{
142 tcf_exts_destroy(&r->exts);
143 tcf_exts_put_net(&r->exts);
144}
145
Cong Wang27ce4f02017-10-26 18:24:39 -0700146static void tcindex_destroy_rexts_work(struct work_struct *work)
147{
148 struct tcindex_filter_result *r;
149
Cong Wangaaa908f2018-05-23 15:26:53 -0700150 r = container_of(to_rcu_work(work),
151 struct tcindex_filter_result,
152 rwork);
Cong Wang27ce4f02017-10-26 18:24:39 -0700153 rtnl_lock();
Cong Wangf2b75102017-11-06 13:47:29 -0800154 __tcindex_destroy_rexts(r);
Cong Wang27ce4f02017-10-26 18:24:39 -0700155 rtnl_unlock();
156}
157
Cong Wangf2b75102017-11-06 13:47:29 -0800158static void __tcindex_destroy_fexts(struct tcindex_filter *f)
159{
160 tcf_exts_destroy(&f->result.exts);
161 tcf_exts_put_net(&f->result.exts);
162 kfree(f);
163}
164
Cong Wang27ce4f02017-10-26 18:24:39 -0700165static void tcindex_destroy_fexts_work(struct work_struct *work)
166{
Cong Wangaaa908f2018-05-23 15:26:53 -0700167 struct tcindex_filter *f = container_of(to_rcu_work(work),
168 struct tcindex_filter,
169 rwork);
Cong Wang27ce4f02017-10-26 18:24:39 -0700170
171 rtnl_lock();
Cong Wangf2b75102017-11-06 13:47:29 -0800172 __tcindex_destroy_fexts(f);
Cong Wang27ce4f02017-10-26 18:24:39 -0700173 rtnl_unlock();
Alexei Starovoitoved7aa872015-08-25 20:06:33 -0700174}
175
Alexander Aring571acf22018-01-18 11:20:53 -0500176static int tcindex_delete(struct tcf_proto *tp, void *arg, bool *last,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200177 bool rtnl_held, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
John Fastabend331b7292014-09-12 20:08:20 -0700179 struct tcindex_data *p = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700180 struct tcindex_filter_result *r = arg;
John Fastabend331b7292014-09-12 20:08:20 -0700181 struct tcindex_filter __rcu **walk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 struct tcindex_filter *f = NULL;
183
WANG Cong8113c092017-08-04 21:31:43 -0700184 pr_debug("tcindex_delete(tp %p,arg %p),p %p\n", tp, arg, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (p->perfect) {
186 if (!r->res.class)
187 return -ENOENT;
188 } else {
189 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
John Fastabend331b7292014-09-12 20:08:20 -0700191 for (i = 0; i < p->hash; i++) {
192 walk = p->h + i;
193 for (f = rtnl_dereference(*walk); f;
194 walk = &f->next, f = rtnl_dereference(*walk)) {
195 if (&f->result == r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 goto found;
John Fastabend331b7292014-09-12 20:08:20 -0700197 }
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return -ENOENT;
200
201found:
John Fastabend331b7292014-09-12 20:08:20 -0700202 rcu_assign_pointer(*walk, rtnl_dereference(f->next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204 tcf_unbind_filter(tp, &r->res);
Alexei Starovoitoved7aa872015-08-25 20:06:33 -0700205 /* all classifiers are required to call tcf_exts_destroy() after rcu
206 * grace period, since converted-to-rcu actions are relying on that
207 * in cleanup() callback
208 */
Cong Wangf2b75102017-11-06 13:47:29 -0800209 if (f) {
210 if (tcf_exts_get_net(&f->result.exts))
Cong Wangaaa908f2018-05-23 15:26:53 -0700211 tcf_queue_work(&f->rwork, tcindex_destroy_fexts_work);
Cong Wangf2b75102017-11-06 13:47:29 -0800212 else
213 __tcindex_destroy_fexts(f);
214 } else {
215 if (tcf_exts_get_net(&r->exts))
Cong Wangaaa908f2018-05-23 15:26:53 -0700216 tcf_queue_work(&r->rwork, tcindex_destroy_rexts_work);
Cong Wangf2b75102017-11-06 13:47:29 -0800217 else
218 __tcindex_destroy_rexts(r);
219 }
WANG Cong763dbf62017-04-19 14:21:21 -0700220
221 *last = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return 0;
223}
224
Cong Wang3d210532019-02-16 10:58:26 -0800225static void tcindex_destroy_work(struct work_struct *work)
John Fastabend331b7292014-09-12 20:08:20 -0700226{
Cong Wang3d210532019-02-16 10:58:26 -0800227 struct tcindex_data *p = container_of(to_rcu_work(work),
228 struct tcindex_data,
229 rwork);
John Fastabend331b7292014-09-12 20:08:20 -0700230
231 kfree(p->perfect);
232 kfree(p->h);
233 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236static inline int
237valid_perfect_hash(struct tcindex_data *p)
238{
239 return p->hash > (p->mask >> p->shift);
240}
241
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800242static const struct nla_policy tcindex_policy[TCA_TCINDEX_MAX + 1] = {
243 [TCA_TCINDEX_HASH] = { .type = NLA_U32 },
244 [TCA_TCINDEX_MASK] = { .type = NLA_U16 },
245 [TCA_TCINDEX_SHIFT] = { .type = NLA_U32 },
246 [TCA_TCINDEX_FALL_THROUGH] = { .type = NLA_U32 },
247 [TCA_TCINDEX_CLASSID] = { .type = NLA_U32 },
248};
249
Cong Wang14215102019-02-20 21:37:42 -0800250static int tcindex_filter_result_init(struct tcindex_filter_result *r,
251 struct net *net)
Cong Wangbf63ac72014-05-19 12:15:49 -0700252{
253 memset(r, 0, sizeof(*r));
Cong Wang14215102019-02-20 21:37:42 -0800254 return tcf_exts_init(&r->exts, net, TCA_TCINDEX_ACT,
255 TCA_TCINDEX_POLICE);
Cong Wangbf63ac72014-05-19 12:15:49 -0700256}
257
Cong Wang3d210532019-02-16 10:58:26 -0800258static void tcindex_partial_destroy_work(struct work_struct *work)
John Fastabend331b7292014-09-12 20:08:20 -0700259{
Cong Wang3d210532019-02-16 10:58:26 -0800260 struct tcindex_data *p = container_of(to_rcu_work(work),
261 struct tcindex_data,
262 rwork);
John Fastabend331b7292014-09-12 20:08:20 -0700263
Cong Wangb1be2e82020-03-11 22:42:27 -0700264 rtnl_lock();
John Fastabend331b7292014-09-12 20:08:20 -0700265 kfree(p->perfect);
266 kfree(p);
Cong Wangb1be2e82020-03-11 22:42:27 -0700267 rtnl_unlock();
John Fastabend331b7292014-09-12 20:08:20 -0700268}
269
WANG Congb9a24bb2016-08-19 12:36:54 -0700270static void tcindex_free_perfect_hash(struct tcindex_data *cp)
271{
272 int i;
273
274 for (i = 0; i < cp->hash; i++)
275 tcf_exts_destroy(&cp->perfect[i].exts);
276 kfree(cp->perfect);
277}
278
Cong Wang033b2282019-02-11 13:06:15 -0800279static int tcindex_alloc_perfect_hash(struct net *net, struct tcindex_data *cp)
WANG Congb9a24bb2016-08-19 12:36:54 -0700280{
281 int i, err = 0;
282
283 cp->perfect = kcalloc(cp->hash, sizeof(struct tcindex_filter_result),
284 GFP_KERNEL);
285 if (!cp->perfect)
286 return -ENOMEM;
287
288 for (i = 0; i < cp->hash; i++) {
Cong Wang14215102019-02-20 21:37:42 -0800289 err = tcf_exts_init(&cp->perfect[i].exts, net,
WANG Congb9a24bb2016-08-19 12:36:54 -0700290 TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
291 if (err < 0)
292 goto errout;
293 }
294
295 return 0;
296
297errout:
298 tcindex_free_perfect_hash(cp);
299 return err;
300}
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302static int
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000303tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
304 u32 handle, struct tcindex_data *p,
305 struct tcindex_filter_result *r, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -0500306 struct nlattr *est, bool ovr, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct tcindex_filter_result new_filter_result, *old_r = r;
WANG Congb9a24bb2016-08-19 12:36:54 -0700309 struct tcindex_data *cp = NULL, *oldp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 struct tcindex_filter *f = NULL; /* make gcc behave */
Cong Wang1db817e72019-02-11 13:06:16 -0800311 struct tcf_result cr = {};
WANG Congb9a24bb2016-08-19 12:36:54 -0700312 int err, balloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 struct tcf_exts e;
314
Cong Wang14215102019-02-20 21:37:42 -0800315 err = tcf_exts_init(&e, net, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (err < 0)
317 return err;
Vlad Buslovec6743a2019-02-11 10:55:43 +0200318 err = tcf_exts_validate(net, tp, tb, est, &e, ovr, true, extack);
WANG Congb9a24bb2016-08-19 12:36:54 -0700319 if (err < 0)
320 goto errout;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900321
WANG Cong02c5e842014-09-25 12:06:04 -0700322 err = -ENOMEM;
John Fastabend331b7292014-09-12 20:08:20 -0700323 /* tcindex_data attributes must look atomic to classifier/lookup so
324 * allocate new tcindex data and RCU assign it onto root. Keeping
325 * perfect hash and hash pointers from old data.
326 */
WANG Conga57a65b2014-09-15 14:06:46 -0700327 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
WANG Cong02c5e842014-09-25 12:06:04 -0700328 if (!cp)
WANG Cong44b75e42014-09-15 16:43:42 -0700329 goto errout;
John Fastabend331b7292014-09-12 20:08:20 -0700330
331 cp->mask = p->mask;
332 cp->shift = p->shift;
333 cp->hash = p->hash;
334 cp->alloc_hash = p->alloc_hash;
335 cp->fall_through = p->fall_through;
336 cp->tp = tp;
337
Cong Wang599be012020-02-02 21:14:35 -0800338 if (tb[TCA_TCINDEX_HASH])
339 cp->hash = nla_get_u32(tb[TCA_TCINDEX_HASH]);
340
341 if (tb[TCA_TCINDEX_MASK])
342 cp->mask = nla_get_u16(tb[TCA_TCINDEX_MASK]);
343
344 if (tb[TCA_TCINDEX_SHIFT])
345 cp->shift = nla_get_u32(tb[TCA_TCINDEX_SHIFT]);
346
347 if (!cp->hash) {
348 /* Hash not specified, use perfect hash if the upper limit
349 * of the hashing index is below the threshold.
350 */
351 if ((cp->mask >> cp->shift) < PERFECT_HASH_THRESHOLD)
352 cp->hash = (cp->mask >> cp->shift) + 1;
353 else
354 cp->hash = DEFAULT_HASH_SIZE;
355 }
356
John Fastabend331b7292014-09-12 20:08:20 -0700357 if (p->perfect) {
WANG Cong6e056562014-09-30 16:07:23 -0700358 int i;
359
Cong Wang033b2282019-02-11 13:06:15 -0800360 if (tcindex_alloc_perfect_hash(net, cp) < 0)
John Fastabend331b7292014-09-12 20:08:20 -0700361 goto errout;
Cong Wang0d1c3532020-03-11 22:42:28 -0700362 cp->alloc_hash = cp->hash;
Cong Wang599be012020-02-02 21:14:35 -0800363 for (i = 0; i < min(cp->hash, p->hash); i++)
WANG Congb9a24bb2016-08-19 12:36:54 -0700364 cp->perfect[i].res = p->perfect[i].res;
WANG Cong44b75e42014-09-15 16:43:42 -0700365 balloc = 1;
John Fastabend331b7292014-09-12 20:08:20 -0700366 }
367 cp->h = p->h;
368
Cong Wang14215102019-02-20 21:37:42 -0800369 err = tcindex_filter_result_init(&new_filter_result, net);
WANG Congb9a24bb2016-08-19 12:36:54 -0700370 if (err < 0)
Cong Wang52b5ae52020-02-04 11:10:12 -0800371 goto errout_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (old_r)
Cong Wang1db817e72019-02-11 13:06:16 -0800373 cr = r->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 err = -EBUSY;
John Fastabend331b7292014-09-12 20:08:20 -0700376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /* Hash already allocated, make sure that we still meet the
378 * requirements for the allocated hash.
379 */
John Fastabend331b7292014-09-12 20:08:20 -0700380 if (cp->perfect) {
381 if (!valid_perfect_hash(cp) ||
382 cp->hash > cp->alloc_hash)
WANG Cong44b75e42014-09-15 16:43:42 -0700383 goto errout_alloc;
John Fastabend331b7292014-09-12 20:08:20 -0700384 } else if (cp->h && cp->hash != cp->alloc_hash) {
WANG Cong44b75e42014-09-15 16:43:42 -0700385 goto errout_alloc;
John Fastabend331b7292014-09-12 20:08:20 -0700386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 err = -EINVAL;
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800389 if (tb[TCA_TCINDEX_FALL_THROUGH])
John Fastabend331b7292014-09-12 20:08:20 -0700390 cp->fall_through = nla_get_u32(tb[TCA_TCINDEX_FALL_THROUGH]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
WANG Cong68f6a7c2014-09-25 12:06:05 -0700392 if (!cp->perfect && !cp->h)
John Fastabend331b7292014-09-12 20:08:20 -0700393 cp->alloc_hash = cp->hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 /* Note: this could be as restrictive as if (handle & ~(mask >> shift))
396 * but then, we'd fail handles that may become valid after some future
397 * mask change. While this is extremely unlikely to ever matter,
398 * the check below is safer (and also more backwards-compatible).
399 */
John Fastabend331b7292014-09-12 20:08:20 -0700400 if (cp->perfect || valid_perfect_hash(cp))
401 if (handle >= cp->alloc_hash)
WANG Cong44b75e42014-09-15 16:43:42 -0700402 goto errout_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404
405 err = -ENOMEM;
John Fastabend331b7292014-09-12 20:08:20 -0700406 if (!cp->perfect && !cp->h) {
407 if (valid_perfect_hash(cp)) {
Cong Wang033b2282019-02-11 13:06:15 -0800408 if (tcindex_alloc_perfect_hash(net, cp) < 0)
WANG Cong44b75e42014-09-15 16:43:42 -0700409 goto errout_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 balloc = 1;
411 } else {
John Fastabend331b7292014-09-12 20:08:20 -0700412 struct tcindex_filter __rcu **hash;
413
414 hash = kcalloc(cp->hash,
415 sizeof(struct tcindex_filter *),
416 GFP_KERNEL);
417
418 if (!hash)
WANG Cong44b75e42014-09-15 16:43:42 -0700419 goto errout_alloc;
John Fastabend331b7292014-09-12 20:08:20 -0700420
421 cp->h = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 balloc = 2;
423 }
424 }
425
John Fastabend331b7292014-09-12 20:08:20 -0700426 if (cp->perfect)
427 r = cp->perfect + handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 else
John Fastabend331b7292014-09-12 20:08:20 -0700429 r = tcindex_lookup(cp, handle) ? : &new_filter_result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 if (r == &new_filter_result) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700432 f = kzalloc(sizeof(*f), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (!f)
434 goto errout_alloc;
WANG Cong6e056562014-09-30 16:07:23 -0700435 f->key = handle;
WANG Cong6e056562014-09-30 16:07:23 -0700436 f->next = NULL;
Cong Wang14215102019-02-20 21:37:42 -0800437 err = tcindex_filter_result_init(&f->result, net);
WANG Congb9a24bb2016-08-19 12:36:54 -0700438 if (err < 0) {
439 kfree(f);
440 goto errout_alloc;
441 }
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Patrick McHardyadd93b62008-01-22 22:11:33 -0800444 if (tb[TCA_TCINDEX_CLASSID]) {
Cong Wang1db817e72019-02-11 13:06:16 -0800445 cr.classid = nla_get_u32(tb[TCA_TCINDEX_CLASSID]);
446 tcf_bind_filter(tp, &cr, base);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
WANG Congb9a24bb2016-08-19 12:36:54 -0700449 if (old_r && old_r != r) {
Cong Wang14215102019-02-20 21:37:42 -0800450 err = tcindex_filter_result_init(old_r, net);
WANG Congb9a24bb2016-08-19 12:36:54 -0700451 if (err < 0) {
452 kfree(f);
453 goto errout_alloc;
454 }
455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
John Fastabend331b7292014-09-12 20:08:20 -0700457 oldp = p;
Cong Wang1db817e72019-02-11 13:06:16 -0800458 r->res = cr;
Hangbin Liu2df8bee2018-08-13 18:44:03 +0800459 tcf_exts_change(&r->exts, &e);
460
John Fastabend331b7292014-09-12 20:08:20 -0700461 rcu_assign_pointer(tp->root, cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 if (r == &new_filter_result) {
John Fastabend331b7292014-09-12 20:08:20 -0700464 struct tcindex_filter *nfp;
465 struct tcindex_filter __rcu **fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Hangbin Liu008369d2018-08-13 18:44:04 +0800467 f->result.res = r->res;
Jiri Pirko9b0d4442017-08-04 14:29:15 +0200468 tcf_exts_change(&f->result.exts, &r->exts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
WANG Cong69301ea2014-09-15 16:43:43 -0700470 fp = cp->h + (handle % cp->hash);
John Fastabend331b7292014-09-12 20:08:20 -0700471 for (nfp = rtnl_dereference(*fp);
472 nfp;
473 fp = &nfp->next, nfp = rtnl_dereference(*fp))
474 ; /* nothing */
475
476 rcu_assign_pointer(*fp, f);
Cong Wang1db817e72019-02-11 13:06:16 -0800477 } else {
478 tcf_exts_destroy(&new_filter_result.exts);
John Fastabend331b7292014-09-12 20:08:20 -0700479 }
480
481 if (oldp)
Cong Wang3d210532019-02-16 10:58:26 -0800482 tcf_queue_work(&oldp->rwork, tcindex_partial_destroy_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return 0;
484
485errout_alloc:
486 if (balloc == 1)
WANG Congb9a24bb2016-08-19 12:36:54 -0700487 tcindex_free_perfect_hash(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 else if (balloc == 2)
John Fastabend331b7292014-09-12 20:08:20 -0700489 kfree(cp->h);
WANG Congb9a24bb2016-08-19 12:36:54 -0700490 tcf_exts_destroy(&new_filter_result.exts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491errout:
John Fastabend331b7292014-09-12 20:08:20 -0700492 kfree(cp);
WANG Cong18d02642014-09-25 10:26:37 -0700493 tcf_exts_destroy(&e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return err;
495}
496
497static int
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000498tcindex_change(struct net *net, struct sk_buff *in_skb,
Eric W. Biedermanaf4c6642012-05-25 13:42:45 -0600499 struct tcf_proto *tp, unsigned long base, u32 handle,
Alexander Aring7306db32018-01-18 11:20:51 -0500500 struct nlattr **tca, void **arg, bool ovr,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200501 bool rtnl_held, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Patrick McHardyadd93b62008-01-22 22:11:33 -0800503 struct nlattr *opt = tca[TCA_OPTIONS];
504 struct nlattr *tb[TCA_TCINDEX_MAX + 1];
John Fastabend331b7292014-09-12 20:08:20 -0700505 struct tcindex_data *p = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700506 struct tcindex_filter_result *r = *arg;
Patrick McHardycee63722008-01-23 20:33:32 -0800507 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800509 pr_debug("tcindex_change(tp %p,handle 0x%08x,tca %p,arg %p),opt %p,"
WANG Cong8113c092017-08-04 21:31:43 -0700510 "p %p,r %p,*arg %p\n",
511 tp, handle, tca, arg, opt, p, r, arg ? *arg : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 if (!opt)
514 return 0;
515
Johannes Berg8cb08172019-04-26 14:07:28 +0200516 err = nla_parse_nested_deprecated(tb, TCA_TCINDEX_MAX, opt,
517 tcindex_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800518 if (err < 0)
519 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000521 return tcindex_set_parms(net, tp, base, handle, p, r, tb,
Alexander Aring50a56192018-01-18 11:20:52 -0500522 tca[TCA_RATE], ovr, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
Vlad Buslov12db03b2019-02-11 10:55:45 +0200525static void tcindex_walk(struct tcf_proto *tp, struct tcf_walker *walker,
526 bool rtnl_held)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
John Fastabend331b7292014-09-12 20:08:20 -0700528 struct tcindex_data *p = rtnl_dereference(tp->root);
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800529 struct tcindex_filter *f, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 int i;
531
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800532 pr_debug("tcindex_walk(tp %p,walker %p),p %p\n", tp, walker, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (p->perfect) {
534 for (i = 0; i < p->hash; i++) {
535 if (!p->perfect[i].res.class)
536 continue;
537 if (walker->count >= walker->skip) {
WANG Cong8113c092017-08-04 21:31:43 -0700538 if (walker->fn(tp, p->perfect + i, walker) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 walker->stop = 1;
540 return;
541 }
542 }
543 walker->count++;
544 }
545 }
546 if (!p->h)
547 return;
548 for (i = 0; i < p->hash; i++) {
John Fastabend331b7292014-09-12 20:08:20 -0700549 for (f = rtnl_dereference(p->h[i]); f; f = next) {
550 next = rtnl_dereference(f->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (walker->count >= walker->skip) {
WANG Cong8113c092017-08-04 21:31:43 -0700552 if (walker->fn(tp, &f->result, walker) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 walker->stop = 1;
554 return;
555 }
556 }
557 walker->count++;
558 }
559 }
560}
561
Vlad Buslov12db03b2019-02-11 10:55:45 +0200562static void tcindex_destroy(struct tcf_proto *tp, bool rtnl_held,
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800563 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
John Fastabend331b7292014-09-12 20:08:20 -0700565 struct tcindex_data *p = rtnl_dereference(tp->root);
Cong Wang51dcb692019-02-16 10:58:27 -0800566 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800568 pr_debug("tcindex_destroy(tp %p),p %p\n", tp, p);
Cong Wang51dcb692019-02-16 10:58:27 -0800569
570 if (p->perfect) {
571 for (i = 0; i < p->hash; i++) {
572 struct tcindex_filter_result *r = p->perfect + i;
573
574 tcf_unbind_filter(tp, &r->res);
575 if (tcf_exts_get_net(&r->exts))
576 tcf_queue_work(&r->rwork,
577 tcindex_destroy_rexts_work);
578 else
579 __tcindex_destroy_rexts(r);
580 }
581 }
582
583 for (i = 0; p->h && i < p->hash; i++) {
584 struct tcindex_filter *f, *next;
585 bool last;
586
587 for (f = rtnl_dereference(p->h[i]); f; f = next) {
588 next = rtnl_dereference(f->next);
589 tcindex_delete(tp, &f->result, &last, rtnl_held, NULL);
590 }
591 }
John Fastabend331b7292014-09-12 20:08:20 -0700592
Cong Wang3d210532019-02-16 10:58:26 -0800593 tcf_queue_work(&p->rwork, tcindex_destroy_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
596
WANG Cong8113c092017-08-04 21:31:43 -0700597static int tcindex_dump(struct net *net, struct tcf_proto *tp, void *fh,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200598 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
John Fastabend331b7292014-09-12 20:08:20 -0700600 struct tcindex_data *p = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700601 struct tcindex_filter_result *r = fh;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800602 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
WANG Cong8113c092017-08-04 21:31:43 -0700604 pr_debug("tcindex_dump(tp %p,fh %p,skb %p,t %p),p %p,r %p\n",
Jiri Pirko6ea3b442014-12-09 22:23:29 +0100605 tp, fh, skb, t, p, r);
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800606 pr_debug("p->perfect %p p->h %p\n", p->perfect, p->h);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800607
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200608 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800609 if (nest == NULL)
610 goto nla_put_failure;
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (!fh) {
613 t->tcm_handle = ~0; /* whatever ... */
David S. Miller1b34ec42012-03-29 05:11:39 -0400614 if (nla_put_u32(skb, TCA_TCINDEX_HASH, p->hash) ||
615 nla_put_u16(skb, TCA_TCINDEX_MASK, p->mask) ||
616 nla_put_u32(skb, TCA_TCINDEX_SHIFT, p->shift) ||
617 nla_put_u32(skb, TCA_TCINDEX_FALL_THROUGH, p->fall_through))
618 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800619 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 } else {
621 if (p->perfect) {
John Fastabend331b7292014-09-12 20:08:20 -0700622 t->tcm_handle = r - p->perfect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 } else {
624 struct tcindex_filter *f;
John Fastabend331b7292014-09-12 20:08:20 -0700625 struct tcindex_filter __rcu **fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 int i;
627
628 t->tcm_handle = 0;
629 for (i = 0; !t->tcm_handle && i < p->hash; i++) {
John Fastabend331b7292014-09-12 20:08:20 -0700630 fp = &p->h[i];
631 for (f = rtnl_dereference(*fp);
632 !t->tcm_handle && f;
633 fp = &f->next, f = rtnl_dereference(*fp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (&f->result == r)
635 t->tcm_handle = f->key;
636 }
637 }
638 }
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800639 pr_debug("handle = %d\n", t->tcm_handle);
David S. Miller1b34ec42012-03-29 05:11:39 -0400640 if (r->res.class &&
641 nla_put_u32(skb, TCA_TCINDEX_CLASSID, r->res.classid))
642 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
WANG Cong5da57f42013-12-15 20:15:07 -0800644 if (tcf_exts_dump(skb, &r->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800645 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800646 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
WANG Cong5da57f42013-12-15 20:15:07 -0800648 if (tcf_exts_dump_stats(skb, &r->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800649 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return skb->len;
653
Patrick McHardyadd93b62008-01-22 22:11:33 -0800654nla_put_failure:
Jiri Pirko6ea3b442014-12-09 22:23:29 +0100655 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return -1;
657}
658
Cong Wang2e24cd72020-01-23 16:26:18 -0800659static void tcindex_bind_class(void *fh, u32 classid, unsigned long cl,
660 void *q, unsigned long base)
Cong Wang07d79fc2017-08-30 14:30:36 -0700661{
662 struct tcindex_filter_result *r = fh;
663
Cong Wang2e24cd72020-01-23 16:26:18 -0800664 if (r && r->res.classid == classid) {
665 if (cl)
666 __tcf_bind_filter(q, &r->res, base);
667 else
668 __tcf_unbind_filter(q, &r->res);
669 }
Cong Wang07d79fc2017-08-30 14:30:36 -0700670}
671
Patrick McHardy2eb9d752008-01-22 22:10:42 -0800672static struct tcf_proto_ops cls_tcindex_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 .kind = "tcindex",
674 .classify = tcindex_classify,
675 .init = tcindex_init,
676 .destroy = tcindex_destroy,
677 .get = tcindex_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 .change = tcindex_change,
679 .delete = tcindex_delete,
680 .walk = tcindex_walk,
681 .dump = tcindex_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -0700682 .bind_class = tcindex_bind_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 .owner = THIS_MODULE,
684};
685
686static int __init init_tcindex(void)
687{
688 return register_tcf_proto_ops(&cls_tcindex_ops);
689}
690
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900691static void __exit exit_tcindex(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 unregister_tcf_proto_ops(&cls_tcindex_ops);
694}
695
696module_init(init_tcindex)
697module_exit(exit_tcindex)
698MODULE_LICENSE("GPL");