]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/sched/cls_tcindex.c
cls_flower: Fix incorrect idr release when failing to modify rule
[mirror_ubuntu-bionic-kernel.git] / net / sched / cls_tcindex.c
CommitLineData
1da177e4
LT
1/*
2 * net/sched/cls_tcindex.c Packet classifier for skb->tc_index
3 *
4 * Written 1998,1999 by Werner Almesberger, EPFL ICA
5 */
6
1da177e4
LT
7#include <linux/module.h>
8#include <linux/types.h>
9#include <linux/kernel.h>
10#include <linux/skbuff.h>
11#include <linux/errno.h>
5a0e3ad6 12#include <linux/slab.h>
1da177e4 13#include <net/act_api.h>
dc5fc579 14#include <net/netlink.h>
1da177e4 15#include <net/pkt_cls.h>
1abf2720 16#include <net/sch_generic.h>
1da177e4 17
1da177e4
LT
18/*
19 * Passing parameters to the root seems to be done more awkwardly than really
20 * necessary. At least, u32 doesn't seem to use such dirty hacks. To be
21 * verified. FIXME.
22 */
23
24#define PERFECT_HASH_THRESHOLD 64 /* use perfect hash if not bigger */
25#define DEFAULT_HASH_SIZE 64 /* optimized for diffserv */
26
27
1da177e4
LT
28struct tcindex_filter_result {
29 struct tcf_exts exts;
30 struct tcf_result res;
27ce4f05
CW
31 union {
32 struct work_struct work;
33 struct rcu_head rcu;
34 };
1da177e4
LT
35};
36
37struct tcindex_filter {
38 u16 key;
39 struct tcindex_filter_result result;
331b7292 40 struct tcindex_filter __rcu *next;
27ce4f05
CW
41 union {
42 struct work_struct work;
43 struct rcu_head rcu;
44 };
1da177e4
LT
45};
46
47
48struct tcindex_data {
49 struct tcindex_filter_result *perfect; /* perfect hash; NULL if none */
331b7292
JF
50 struct tcindex_filter __rcu **h; /* imperfect hash; */
51 struct tcf_proto *tp;
1da177e4 52 u16 mask; /* AND key with mask */
331b7292
JF
53 u32 shift; /* shift ANDed key to the right */
54 u32 hash; /* hash table size; 0 if undefined */
55 u32 alloc_hash; /* allocated size */
56 u32 fall_through; /* 0: only classify if explicit match */
57 struct rcu_head rcu;
1da177e4
LT
58};
59
5a7a5555 60static inline int tcindex_filter_is_set(struct tcindex_filter_result *r)
1da177e4 61{
6fc6d06e 62 return tcf_exts_has_actions(&r->exts) || r->res.classid;
1da177e4
LT
63}
64
5a7a5555
JHS
65static struct tcindex_filter_result *tcindex_lookup(struct tcindex_data *p,
66 u16 key)
1da177e4 67{
331b7292
JF
68 if (p->perfect) {
69 struct tcindex_filter_result *f = p->perfect + key;
70
71 return tcindex_filter_is_set(f) ? f : NULL;
72 } else if (p->h) {
73 struct tcindex_filter __rcu **fp;
74 struct tcindex_filter *f;
1da177e4 75
331b7292
JF
76 fp = &p->h[key % p->hash];
77 for (f = rcu_dereference_bh_rtnl(*fp);
78 f;
79 fp = &f->next, f = rcu_dereference_bh_rtnl(*fp))
1da177e4
LT
80 if (f->key == key)
81 return &f->result;
82 }
83
84 return NULL;
85}
86
87
dc7f9f6e 88static int tcindex_classify(struct sk_buff *skb, const struct tcf_proto *tp,
1da177e4
LT
89 struct tcf_result *res)
90{
2f9a220e 91 struct tcindex_data *p = rcu_dereference_bh(tp->root);
1da177e4
LT
92 struct tcindex_filter_result *f;
93 int key = (skb->tc_index & p->mask) >> p->shift;
94
aa767bfe
SH
95 pr_debug("tcindex_classify(skb %p,tp %p,res %p),p %p\n",
96 skb, tp, res, p);
1da177e4
LT
97
98 f = tcindex_lookup(p, key);
99 if (!f) {
1abf2720
JP
100 struct Qdisc *q = tcf_block_q(tp->chain->block);
101
1da177e4
LT
102 if (!p->fall_through)
103 return -1;
1abf2720 104 res->classid = TC_H_MAKE(TC_H_MAJ(q->handle), key);
1da177e4 105 res->class = 0;
aa767bfe 106 pr_debug("alg 0x%x\n", res->classid);
1da177e4
LT
107 return 0;
108 }
109 *res = f->res;
aa767bfe 110 pr_debug("map 0x%x\n", res->classid);
1da177e4
LT
111
112 return tcf_exts_exec(skb, &f->exts, res);
113}
114
115
8113c095 116static void *tcindex_get(struct tcf_proto *tp, u32 handle)
1da177e4 117{
331b7292 118 struct tcindex_data *p = rtnl_dereference(tp->root);
1da177e4
LT
119 struct tcindex_filter_result *r;
120
aa767bfe 121 pr_debug("tcindex_get(tp %p,handle 0x%08x)\n", tp, handle);
1da177e4 122 if (p->perfect && handle >= p->alloc_hash)
8113c095 123 return NULL;
1da177e4 124 r = tcindex_lookup(p, handle);
8113c095 125 return r && tcindex_filter_is_set(r) ? r : NULL;
1da177e4
LT
126}
127
1da177e4
LT
128static int tcindex_init(struct tcf_proto *tp)
129{
130 struct tcindex_data *p;
131
aa767bfe
SH
132 pr_debug("tcindex_init(tp %p)\n", tp);
133 p = kzalloc(sizeof(struct tcindex_data), GFP_KERNEL);
1da177e4
LT
134 if (!p)
135 return -ENOMEM;
136
1da177e4
LT
137 p->mask = 0xffff;
138 p->hash = DEFAULT_HASH_SIZE;
139 p->fall_through = 1;
140
331b7292 141 rcu_assign_pointer(tp->root, p);
1da177e4
LT
142 return 0;
143}
144
f2b75105
CW
145static void __tcindex_destroy_rexts(struct tcindex_filter_result *r)
146{
147 tcf_exts_destroy(&r->exts);
148 tcf_exts_put_net(&r->exts);
149}
150
27ce4f05
CW
151static void tcindex_destroy_rexts_work(struct work_struct *work)
152{
153 struct tcindex_filter_result *r;
154
155 r = container_of(work, struct tcindex_filter_result, work);
156 rtnl_lock();
f2b75105 157 __tcindex_destroy_rexts(r);
27ce4f05
CW
158 rtnl_unlock();
159}
160
ed7aa879
AS
161static void tcindex_destroy_rexts(struct rcu_head *head)
162{
163 struct tcindex_filter_result *r;
164
165 r = container_of(head, struct tcindex_filter_result, rcu);
27ce4f05
CW
166 INIT_WORK(&r->work, tcindex_destroy_rexts_work);
167 tcf_queue_work(&r->work);
168}
169
f2b75105
CW
170static void __tcindex_destroy_fexts(struct tcindex_filter *f)
171{
172 tcf_exts_destroy(&f->result.exts);
173 tcf_exts_put_net(&f->result.exts);
174 kfree(f);
175}
176
27ce4f05
CW
177static void tcindex_destroy_fexts_work(struct work_struct *work)
178{
179 struct tcindex_filter *f = container_of(work, struct tcindex_filter,
180 work);
181
182 rtnl_lock();
f2b75105 183 __tcindex_destroy_fexts(f);
27ce4f05 184 rtnl_unlock();
ed7aa879
AS
185}
186
187static void tcindex_destroy_fexts(struct rcu_head *head)
188{
5a7a5555
JHS
189 struct tcindex_filter *f = container_of(head, struct tcindex_filter,
190 rcu);
ed7aa879 191
27ce4f05
CW
192 INIT_WORK(&f->work, tcindex_destroy_fexts_work);
193 tcf_queue_work(&f->work);
ed7aa879
AS
194}
195
8113c095 196static int tcindex_delete(struct tcf_proto *tp, void *arg, bool *last)
1da177e4 197{
331b7292 198 struct tcindex_data *p = rtnl_dereference(tp->root);
8113c095 199 struct tcindex_filter_result *r = arg;
331b7292 200 struct tcindex_filter __rcu **walk;
1da177e4
LT
201 struct tcindex_filter *f = NULL;
202
8113c095 203 pr_debug("tcindex_delete(tp %p,arg %p),p %p\n", tp, arg, p);
1da177e4
LT
204 if (p->perfect) {
205 if (!r->res.class)
206 return -ENOENT;
207 } else {
208 int i;
1da177e4 209
331b7292
JF
210 for (i = 0; i < p->hash; i++) {
211 walk = p->h + i;
212 for (f = rtnl_dereference(*walk); f;
213 walk = &f->next, f = rtnl_dereference(*walk)) {
214 if (&f->result == r)
1da177e4 215 goto found;
331b7292
JF
216 }
217 }
1da177e4
LT
218 return -ENOENT;
219
220found:
331b7292 221 rcu_assign_pointer(*walk, rtnl_dereference(f->next));
1da177e4
LT
222 }
223 tcf_unbind_filter(tp, &r->res);
ed7aa879
AS
224 /* all classifiers are required to call tcf_exts_destroy() after rcu
225 * grace period, since converted-to-rcu actions are relying on that
226 * in cleanup() callback
227 */
f2b75105
CW
228 if (f) {
229 if (tcf_exts_get_net(&f->result.exts))
230 call_rcu(&f->rcu, tcindex_destroy_fexts);
231 else
232 __tcindex_destroy_fexts(f);
233 } else {
234 if (tcf_exts_get_net(&r->exts))
235 call_rcu(&r->rcu, tcindex_destroy_rexts);
236 else
237 __tcindex_destroy_rexts(r);
238 }
763dbf63
WC
239
240 *last = false;
1da177e4
LT
241 return 0;
242}
243
331b7292 244static int tcindex_destroy_element(struct tcf_proto *tp,
8113c095 245 void *arg, struct tcf_walker *walker)
1da177e4 246{
763dbf63
WC
247 bool last;
248
249 return tcindex_delete(tp, arg, &last);
331b7292
JF
250}
251
252static void __tcindex_destroy(struct rcu_head *head)
253{
254 struct tcindex_data *p = container_of(head, struct tcindex_data, rcu);
255
256 kfree(p->perfect);
257 kfree(p->h);
258 kfree(p);
1da177e4
LT
259}
260
261static inline int
262valid_perfect_hash(struct tcindex_data *p)
263{
264 return p->hash > (p->mask >> p->shift);
265}
266
6fa8c014
PM
267static const struct nla_policy tcindex_policy[TCA_TCINDEX_MAX + 1] = {
268 [TCA_TCINDEX_HASH] = { .type = NLA_U32 },
269 [TCA_TCINDEX_MASK] = { .type = NLA_U16 },
270 [TCA_TCINDEX_SHIFT] = { .type = NLA_U32 },
271 [TCA_TCINDEX_FALL_THROUGH] = { .type = NLA_U32 },
272 [TCA_TCINDEX_CLASSID] = { .type = NLA_U32 },
273};
274
b9a24bb7 275static int tcindex_filter_result_init(struct tcindex_filter_result *r)
bf63ac73
CW
276{
277 memset(r, 0, sizeof(*r));
b9a24bb7 278 return tcf_exts_init(&r->exts, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
bf63ac73
CW
279}
280
331b7292
JF
281static void __tcindex_partial_destroy(struct rcu_head *head)
282{
283 struct tcindex_data *p = container_of(head, struct tcindex_data, rcu);
284
285 kfree(p->perfect);
286 kfree(p);
287}
288
b9a24bb7
WC
289static void tcindex_free_perfect_hash(struct tcindex_data *cp)
290{
291 int i;
292
293 for (i = 0; i < cp->hash; i++)
294 tcf_exts_destroy(&cp->perfect[i].exts);
295 kfree(cp->perfect);
296}
297
298static int tcindex_alloc_perfect_hash(struct tcindex_data *cp)
299{
300 int i, err = 0;
301
302 cp->perfect = kcalloc(cp->hash, sizeof(struct tcindex_filter_result),
303 GFP_KERNEL);
304 if (!cp->perfect)
305 return -ENOMEM;
306
307 for (i = 0; i < cp->hash; i++) {
308 err = tcf_exts_init(&cp->perfect[i].exts,
309 TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
310 if (err < 0)
311 goto errout;
312 }
313
314 return 0;
315
316errout:
317 tcindex_free_perfect_hash(cp);
318 return err;
319}
320
1da177e4 321static int
c1b52739
BL
322tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
323 u32 handle, struct tcindex_data *p,
324 struct tcindex_filter_result *r, struct nlattr **tb,
2f7ef2f8 325 struct nlattr *est, bool ovr)
1da177e4 326{
1da177e4
LT
327 struct tcindex_filter_result new_filter_result, *old_r = r;
328 struct tcindex_filter_result cr;
b9a24bb7 329 struct tcindex_data *cp = NULL, *oldp;
1da177e4 330 struct tcindex_filter *f = NULL; /* make gcc behave */
b9a24bb7 331 int err, balloc = 0;
1da177e4
LT
332 struct tcf_exts e;
333
b9a24bb7 334 err = tcf_exts_init(&e, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
1da177e4
LT
335 if (err < 0)
336 return err;
b9a24bb7
WC
337 err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
338 if (err < 0)
339 goto errout;
10297b99 340
02c5e844 341 err = -ENOMEM;
331b7292
JF
342 /* tcindex_data attributes must look atomic to classifier/lookup so
343 * allocate new tcindex data and RCU assign it onto root. Keeping
344 * perfect hash and hash pointers from old data.
345 */
a57a65ba 346 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
02c5e844 347 if (!cp)
44b75e43 348 goto errout;
331b7292
JF
349
350 cp->mask = p->mask;
351 cp->shift = p->shift;
352 cp->hash = p->hash;
353 cp->alloc_hash = p->alloc_hash;
354 cp->fall_through = p->fall_through;
355 cp->tp = tp;
356
357 if (p->perfect) {
6e056569
WC
358 int i;
359
b9a24bb7 360 if (tcindex_alloc_perfect_hash(cp) < 0)
331b7292 361 goto errout;
6e056569 362 for (i = 0; i < cp->hash; i++)
b9a24bb7 363 cp->perfect[i].res = p->perfect[i].res;
44b75e43 364 balloc = 1;
331b7292
JF
365 }
366 cp->h = p->h;
367
b9a24bb7
WC
368 err = tcindex_filter_result_init(&new_filter_result);
369 if (err < 0)
370 goto errout1;
371 err = tcindex_filter_result_init(&cr);
372 if (err < 0)
373 goto errout1;
1da177e4 374 if (old_r)
bf63ac73 375 cr.res = r->res;
1da177e4 376
6fa8c014 377 if (tb[TCA_TCINDEX_HASH])
331b7292 378 cp->hash = nla_get_u32(tb[TCA_TCINDEX_HASH]);
1da177e4 379
6fa8c014 380 if (tb[TCA_TCINDEX_MASK])
331b7292 381 cp->mask = nla_get_u16(tb[TCA_TCINDEX_MASK]);
1da177e4 382
6fa8c014 383 if (tb[TCA_TCINDEX_SHIFT])
331b7292 384 cp->shift = nla_get_u32(tb[TCA_TCINDEX_SHIFT]);
1da177e4
LT
385
386 err = -EBUSY;
331b7292 387
1da177e4
LT
388 /* Hash already allocated, make sure that we still meet the
389 * requirements for the allocated hash.
390 */
331b7292
JF
391 if (cp->perfect) {
392 if (!valid_perfect_hash(cp) ||
393 cp->hash > cp->alloc_hash)
44b75e43 394 goto errout_alloc;
331b7292 395 } else if (cp->h && cp->hash != cp->alloc_hash) {
44b75e43 396 goto errout_alloc;
331b7292 397 }
1da177e4
LT
398
399 err = -EINVAL;
6fa8c014 400 if (tb[TCA_TCINDEX_FALL_THROUGH])
331b7292 401 cp->fall_through = nla_get_u32(tb[TCA_TCINDEX_FALL_THROUGH]);
1da177e4 402
331b7292 403 if (!cp->hash) {
1da177e4
LT
404 /* Hash not specified, use perfect hash if the upper limit
405 * of the hashing index is below the threshold.
406 */
331b7292
JF
407 if ((cp->mask >> cp->shift) < PERFECT_HASH_THRESHOLD)
408 cp->hash = (cp->mask >> cp->shift) + 1;
1da177e4 409 else
331b7292 410 cp->hash = DEFAULT_HASH_SIZE;
1da177e4
LT
411 }
412
68f6a7c6 413 if (!cp->perfect && !cp->h)
331b7292 414 cp->alloc_hash = cp->hash;
1da177e4
LT
415
416 /* Note: this could be as restrictive as if (handle & ~(mask >> shift))
417 * but then, we'd fail handles that may become valid after some future
418 * mask change. While this is extremely unlikely to ever matter,
419 * the check below is safer (and also more backwards-compatible).
420 */
331b7292
JF
421 if (cp->perfect || valid_perfect_hash(cp))
422 if (handle >= cp->alloc_hash)
44b75e43 423 goto errout_alloc;
1da177e4
LT
424
425
426 err = -ENOMEM;
331b7292
JF
427 if (!cp->perfect && !cp->h) {
428 if (valid_perfect_hash(cp)) {
b9a24bb7 429 if (tcindex_alloc_perfect_hash(cp) < 0)
44b75e43 430 goto errout_alloc;
1da177e4
LT
431 balloc = 1;
432 } else {
331b7292
JF
433 struct tcindex_filter __rcu **hash;
434
435 hash = kcalloc(cp->hash,
436 sizeof(struct tcindex_filter *),
437 GFP_KERNEL);
438
439 if (!hash)
44b75e43 440 goto errout_alloc;
331b7292
JF
441
442 cp->h = hash;
1da177e4
LT
443 balloc = 2;
444 }
445 }
446
331b7292
JF
447 if (cp->perfect)
448 r = cp->perfect + handle;
1da177e4 449 else
331b7292 450 r = tcindex_lookup(cp, handle) ? : &new_filter_result;
1da177e4
LT
451
452 if (r == &new_filter_result) {
0da974f4 453 f = kzalloc(sizeof(*f), GFP_KERNEL);
1da177e4
LT
454 if (!f)
455 goto errout_alloc;
6e056569 456 f->key = handle;
6e056569 457 f->next = NULL;
b9a24bb7
WC
458 err = tcindex_filter_result_init(&f->result);
459 if (err < 0) {
460 kfree(f);
461 goto errout_alloc;
462 }
10297b99 463 }
1da177e4 464
add93b61 465 if (tb[TCA_TCINDEX_CLASSID]) {
1587bac4 466 cr.res.classid = nla_get_u32(tb[TCA_TCINDEX_CLASSID]);
1da177e4 467 tcf_bind_filter(tp, &cr.res, base);
10297b99 468 }
1da177e4 469
bf63ac73 470 if (old_r)
9b0d4446 471 tcf_exts_change(&r->exts, &e);
bf63ac73 472 else
9b0d4446 473 tcf_exts_change(&cr.exts, &e);
1da177e4 474
b9a24bb7
WC
475 if (old_r && old_r != r) {
476 err = tcindex_filter_result_init(old_r);
477 if (err < 0) {
478 kfree(f);
479 goto errout_alloc;
480 }
481 }
1da177e4 482
331b7292 483 oldp = p;
bf63ac73 484 r->res = cr.res;
331b7292 485 rcu_assign_pointer(tp->root, cp);
1da177e4
LT
486
487 if (r == &new_filter_result) {
331b7292
JF
488 struct tcindex_filter *nfp;
489 struct tcindex_filter __rcu **fp;
1da177e4 490
9b0d4446 491 tcf_exts_change(&f->result.exts, &r->exts);
331b7292 492
69301eaa 493 fp = cp->h + (handle % cp->hash);
331b7292
JF
494 for (nfp = rtnl_dereference(*fp);
495 nfp;
496 fp = &nfp->next, nfp = rtnl_dereference(*fp))
497 ; /* nothing */
498
499 rcu_assign_pointer(*fp, f);
10297b99 500 }
1da177e4 501
331b7292
JF
502 if (oldp)
503 call_rcu(&oldp->rcu, __tcindex_partial_destroy);
1da177e4
LT
504 return 0;
505
506errout_alloc:
507 if (balloc == 1)
b9a24bb7 508 tcindex_free_perfect_hash(cp);
1da177e4 509 else if (balloc == 2)
331b7292 510 kfree(cp->h);
b9a24bb7
WC
511errout1:
512 tcf_exts_destroy(&cr.exts);
513 tcf_exts_destroy(&new_filter_result.exts);
1da177e4 514errout:
331b7292 515 kfree(cp);
18d0264f 516 tcf_exts_destroy(&e);
1da177e4
LT
517 return err;
518}
519
520static int
c1b52739 521tcindex_change(struct net *net, struct sk_buff *in_skb,
af4c6641 522 struct tcf_proto *tp, unsigned long base, u32 handle,
8113c095 523 struct nlattr **tca, void **arg, bool ovr)
1da177e4 524{
add93b61
PM
525 struct nlattr *opt = tca[TCA_OPTIONS];
526 struct nlattr *tb[TCA_TCINDEX_MAX + 1];
331b7292 527 struct tcindex_data *p = rtnl_dereference(tp->root);
8113c095 528 struct tcindex_filter_result *r = *arg;
cee63723 529 int err;
1da177e4 530
aa767bfe 531 pr_debug("tcindex_change(tp %p,handle 0x%08x,tca %p,arg %p),opt %p,"
8113c095
WC
532 "p %p,r %p,*arg %p\n",
533 tp, handle, tca, arg, opt, p, r, arg ? *arg : NULL);
1da177e4
LT
534
535 if (!opt)
536 return 0;
537
fceb6435 538 err = nla_parse_nested(tb, TCA_TCINDEX_MAX, opt, tcindex_policy, NULL);
cee63723
PM
539 if (err < 0)
540 return err;
1da177e4 541
c1b52739 542 return tcindex_set_parms(net, tp, base, handle, p, r, tb,
2f7ef2f8 543 tca[TCA_RATE], ovr);
1da177e4
LT
544}
545
1da177e4
LT
546static void tcindex_walk(struct tcf_proto *tp, struct tcf_walker *walker)
547{
331b7292 548 struct tcindex_data *p = rtnl_dereference(tp->root);
aa767bfe 549 struct tcindex_filter *f, *next;
1da177e4
LT
550 int i;
551
aa767bfe 552 pr_debug("tcindex_walk(tp %p,walker %p),p %p\n", tp, walker, p);
1da177e4
LT
553 if (p->perfect) {
554 for (i = 0; i < p->hash; i++) {
555 if (!p->perfect[i].res.class)
556 continue;
557 if (walker->count >= walker->skip) {
8113c095 558 if (walker->fn(tp, p->perfect + i, walker) < 0) {
1da177e4
LT
559 walker->stop = 1;
560 return;
561 }
562 }
563 walker->count++;
564 }
565 }
566 if (!p->h)
567 return;
568 for (i = 0; i < p->hash; i++) {
331b7292
JF
569 for (f = rtnl_dereference(p->h[i]); f; f = next) {
570 next = rtnl_dereference(f->next);
1da177e4 571 if (walker->count >= walker->skip) {
8113c095 572 if (walker->fn(tp, &f->result, walker) < 0) {
1da177e4
LT
573 walker->stop = 1;
574 return;
575 }
576 }
577 walker->count++;
578 }
579 }
580}
581
763dbf63 582static void tcindex_destroy(struct tcf_proto *tp)
1da177e4 583{
331b7292 584 struct tcindex_data *p = rtnl_dereference(tp->root);
1da177e4
LT
585 struct tcf_walker walker;
586
aa767bfe 587 pr_debug("tcindex_destroy(tp %p),p %p\n", tp, p);
1da177e4
LT
588 walker.count = 0;
589 walker.skip = 0;
e40f5c72 590 walker.fn = tcindex_destroy_element;
aa767bfe 591 tcindex_walk(tp, &walker);
331b7292 592
331b7292 593 call_rcu(&p->rcu, __tcindex_destroy);
1da177e4
LT
594}
595
596
8113c095 597static int tcindex_dump(struct net *net, struct tcf_proto *tp, void *fh,
5a7a5555 598 struct sk_buff *skb, struct tcmsg *t)
1da177e4 599{
331b7292 600 struct tcindex_data *p = rtnl_dereference(tp->root);
8113c095 601 struct tcindex_filter_result *r = fh;
4b3550ef 602 struct nlattr *nest;
1da177e4 603
8113c095 604 pr_debug("tcindex_dump(tp %p,fh %p,skb %p,t %p),p %p,r %p\n",
6ea3b446 605 tp, fh, skb, t, p, r);
aa767bfe 606 pr_debug("p->perfect %p p->h %p\n", p->perfect, p->h);
4b3550ef
PM
607
608 nest = nla_nest_start(skb, TCA_OPTIONS);
609 if (nest == NULL)
610 goto nla_put_failure;
611
1da177e4
LT
612 if (!fh) {
613 t->tcm_handle = ~0; /* whatever ... */
1b34ec43
DM
614 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;
4b3550ef 619 nla_nest_end(skb, nest);
1da177e4
LT
620 } else {
621 if (p->perfect) {
331b7292 622 t->tcm_handle = r - p->perfect;
1da177e4
LT
623 } else {
624 struct tcindex_filter *f;
331b7292 625 struct tcindex_filter __rcu **fp;
1da177e4
LT
626 int i;
627
628 t->tcm_handle = 0;
629 for (i = 0; !t->tcm_handle && i < p->hash; i++) {
331b7292
JF
630 fp = &p->h[i];
631 for (f = rtnl_dereference(*fp);
632 !t->tcm_handle && f;
633 fp = &f->next, f = rtnl_dereference(*fp)) {
1da177e4
LT
634 if (&f->result == r)
635 t->tcm_handle = f->key;
636 }
637 }
638 }
aa767bfe 639 pr_debug("handle = %d\n", t->tcm_handle);
1b34ec43
DM
640 if (r->res.class &&
641 nla_put_u32(skb, TCA_TCINDEX_CLASSID, r->res.classid))
642 goto nla_put_failure;
1da177e4 643
5da57f42 644 if (tcf_exts_dump(skb, &r->exts) < 0)
add93b61 645 goto nla_put_failure;
4b3550ef 646 nla_nest_end(skb, nest);
1da177e4 647
5da57f42 648 if (tcf_exts_dump_stats(skb, &r->exts) < 0)
add93b61 649 goto nla_put_failure;
1da177e4 650 }
10297b99 651
1da177e4
LT
652 return skb->len;
653
add93b61 654nla_put_failure:
6ea3b446 655 nla_nest_cancel(skb, nest);
1da177e4
LT
656 return -1;
657}
658
07d79fc7
CW
659static void tcindex_bind_class(void *fh, u32 classid, unsigned long cl)
660{
661 struct tcindex_filter_result *r = fh;
662
663 if (r && r->res.classid == classid)
664 r->res.class = cl;
665}
666
2eb9d75c 667static struct tcf_proto_ops cls_tcindex_ops __read_mostly = {
1da177e4
LT
668 .kind = "tcindex",
669 .classify = tcindex_classify,
670 .init = tcindex_init,
671 .destroy = tcindex_destroy,
672 .get = tcindex_get,
1da177e4
LT
673 .change = tcindex_change,
674 .delete = tcindex_delete,
675 .walk = tcindex_walk,
676 .dump = tcindex_dump,
07d79fc7 677 .bind_class = tcindex_bind_class,
1da177e4
LT
678 .owner = THIS_MODULE,
679};
680
681static int __init init_tcindex(void)
682{
683 return register_tcf_proto_ops(&cls_tcindex_ops);
684}
685
10297b99 686static void __exit exit_tcindex(void)
1da177e4
LT
687{
688 unregister_tcf_proto_ops(&cls_tcindex_ops);
689}
690
691module_init(init_tcindex)
692module_exit(exit_tcindex)
693MODULE_LICENSE("GPL");