1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * net/sched/cls_tcindex.c Packet classifier for skb->tc_index
4 *
5 * Written 1998,1999 by Werner Almesberger, EPFL ICA
6 */
7
8 #include <linux/module.h>
9 #include <linux/types.h>
10 #include <linux/kernel.h>
11 #include <linux/skbuff.h>
12 #include <linux/errno.h>
13 #include <linux/slab.h>
14 #include <linux/refcount.h>
15 #include <net/act_api.h>
16 #include <net/netlink.h>
17 #include <net/pkt_cls.h>
18 #include <net/sch_generic.h>
19
20 /*
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
30 struct tcindex_data;
31
32 struct tcindex_filter_result {
33 struct tcf_exts exts;
34 struct tcf_result res;
35 struct tcindex_data *p;
36 struct rcu_work rwork;
37 };
38
39 struct tcindex_filter {
40 u16 key;
41 struct tcindex_filter_result result;
42 struct tcindex_filter __rcu *next;
43 struct rcu_work rwork;
44 };
45
46
47 struct tcindex_data {
48 struct tcindex_filter_result *perfect; /* perfect hash; NULL if none */
49 struct tcindex_filter __rcu **h; /* imperfect hash; */
50 struct tcf_proto *tp;
51 u16 mask; /* AND key with mask */
52 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 */
56 refcount_t refcnt; /* a temporary refcnt for perfect hash */
57 struct rcu_work rwork;
58 };
59
tcindex_filter_is_set(struct tcindex_filter_result * r)60 static inline int tcindex_filter_is_set(struct tcindex_filter_result *r)
61 {
62 return tcf_exts_has_actions(&r->exts) || r->res.classid;
63 }
64
tcindex_data_get(struct tcindex_data * p)65 static void tcindex_data_get(struct tcindex_data *p)
66 {
67 refcount_inc(&p->refcnt);
68 }
69
tcindex_data_put(struct tcindex_data * p)70 static 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
tcindex_lookup(struct tcindex_data * p,u16 key)79 static struct tcindex_filter_result *tcindex_lookup(struct tcindex_data *p,
80 u16 key)
81 {
82 if (p->perfect) {
83 struct tcindex_filter_result *f = p->perfect + key;
84
85 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))
94 if (f->key == key)
95 return &f->result;
96 }
97
98 return NULL;
99 }
100
101
tcindex_classify(struct sk_buff * skb,const struct tcf_proto * tp,struct tcf_result * res)102 static int tcindex_classify(struct sk_buff *skb, const struct tcf_proto *tp,
103 struct tcf_result *res)
104 {
105 struct tcindex_data *p = rcu_dereference_bh(tp->root);
106 struct tcindex_filter_result *f;
107 int key = (skb->tc_index & p->mask) >> p->shift;
108
109 pr_debug("tcindex_classify(skb %p,tp %p,res %p),p %p\n",
110 skb, tp, res, p);
111
112 f = tcindex_lookup(p, key);
113 if (!f) {
114 struct Qdisc *q = tcf_block_q(tp->chain->block);
115
116 if (!p->fall_through)
117 return -1;
118 res->classid = TC_H_MAKE(TC_H_MAJ(q->handle), key);
119 res->class = 0;
120 pr_debug("alg 0x%x\n", res->classid);
121 return 0;
122 }
123 *res = f->res;
124 pr_debug("map 0x%x\n", res->classid);
125
126 return tcf_exts_exec(skb, &f->exts, res);
127 }
128
129
tcindex_get(struct tcf_proto * tp,u32 handle)130 static void *tcindex_get(struct tcf_proto *tp, u32 handle)
131 {
132 struct tcindex_data *p = rtnl_dereference(tp->root);
133 struct tcindex_filter_result *r;
134
135 pr_debug("tcindex_get(tp %p,handle 0x%08x)\n", tp, handle);
136 if (p->perfect && handle >= p->alloc_hash)
137 return NULL;
138 r = tcindex_lookup(p, handle);
139 return r && tcindex_filter_is_set(r) ? r : NULL;
140 }
141
tcindex_init(struct tcf_proto * tp)142 static int tcindex_init(struct tcf_proto *tp)
143 {
144 struct tcindex_data *p;
145
146 pr_debug("tcindex_init(tp %p)\n", tp);
147 p = kzalloc(sizeof(struct tcindex_data), GFP_KERNEL);
148 if (!p)
149 return -ENOMEM;
150
151 p->mask = 0xffff;
152 p->hash = DEFAULT_HASH_SIZE;
153 p->fall_through = 1;
154 refcount_set(&p->refcnt, 1); /* Paired with tcindex_destroy_work() */
155
156 rcu_assign_pointer(tp->root, p);
157 return 0;
158 }
159
__tcindex_destroy_rexts(struct tcindex_filter_result * r)160 static void __tcindex_destroy_rexts(struct tcindex_filter_result *r)
161 {
162 tcf_exts_destroy(&r->exts);
163 tcf_exts_put_net(&r->exts);
164 tcindex_data_put(r->p);
165 }
166
tcindex_destroy_rexts_work(struct work_struct * work)167 static void tcindex_destroy_rexts_work(struct work_struct *work)
168 {
169 struct tcindex_filter_result *r;
170
171 r = container_of(to_rcu_work(work),
172 struct tcindex_filter_result,
173 rwork);
174 rtnl_lock();
175 __tcindex_destroy_rexts(r);
176 rtnl_unlock();
177 }
178
__tcindex_destroy_fexts(struct tcindex_filter * f)179 static 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
tcindex_destroy_fexts_work(struct work_struct * work)186 static void tcindex_destroy_fexts_work(struct work_struct *work)
187 {
188 struct tcindex_filter *f = container_of(to_rcu_work(work),
189 struct tcindex_filter,
190 rwork);
191
192 rtnl_lock();
193 __tcindex_destroy_fexts(f);
194 rtnl_unlock();
195 }
196
tcindex_delete(struct tcf_proto * tp,void * arg,bool * last,bool rtnl_held,struct netlink_ext_ack * extack)197 static int tcindex_delete(struct tcf_proto *tp, void *arg, bool *last,
198 bool rtnl_held, struct netlink_ext_ack *extack)
199 {
200 struct tcindex_data *p = rtnl_dereference(tp->root);
201 struct tcindex_filter_result *r = arg;
202 struct tcindex_filter __rcu **walk;
203 struct tcindex_filter *f = NULL;
204
205 pr_debug("tcindex_delete(tp %p,arg %p),p %p\n", tp, arg, p);
206 if (p->perfect) {
207 if (!r->res.class)
208 return -ENOENT;
209 } else {
210 int i;
211
212 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)
217 goto found;
218 }
219 }
220 return -ENOENT;
221
222 found:
223 rcu_assign_pointer(*walk, rtnl_dereference(f->next));
224 }
225 tcf_unbind_filter(tp, &r->res);
226 /* 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 */
230 if (f) {
231 if (tcf_exts_get_net(&f->result.exts))
232 tcf_queue_work(&f->rwork, tcindex_destroy_fexts_work);
233 else
234 __tcindex_destroy_fexts(f);
235 } else {
236 tcindex_data_get(p);
237
238 if (tcf_exts_get_net(&r->exts))
239 tcf_queue_work(&r->rwork, tcindex_destroy_rexts_work);
240 else
241 __tcindex_destroy_rexts(r);
242 }
243
244 *last = false;
245 return 0;
246 }
247
tcindex_destroy_work(struct work_struct * work)248 static void tcindex_destroy_work(struct work_struct *work)
249 {
250 struct tcindex_data *p = container_of(to_rcu_work(work),
251 struct tcindex_data,
252 rwork);
253
254 tcindex_data_put(p);
255 }
256
257 static inline int
valid_perfect_hash(struct tcindex_data * p)258 valid_perfect_hash(struct tcindex_data *p)
259 {
260 return p->hash > (p->mask >> p->shift);
261 }
262
263 static 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
tcindex_filter_result_init(struct tcindex_filter_result * r,struct tcindex_data * p,struct net * net)271 static int tcindex_filter_result_init(struct tcindex_filter_result *r,
272 struct tcindex_data *p,
273 struct net *net)
274 {
275 memset(r, 0, sizeof(*r));
276 r->p = p;
277 return tcf_exts_init(&r->exts, net, TCA_TCINDEX_ACT,
278 TCA_TCINDEX_POLICE);
279 }
280
tcindex_partial_destroy_work(struct work_struct * work)281 static void tcindex_partial_destroy_work(struct work_struct *work)
282 {
283 struct tcindex_data *p = container_of(to_rcu_work(work),
284 struct tcindex_data,
285 rwork);
286
287 rtnl_lock();
288 kfree(p->perfect);
289 kfree(p);
290 rtnl_unlock();
291 }
292
tcindex_free_perfect_hash(struct tcindex_data * cp)293 static 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
tcindex_alloc_perfect_hash(struct net * net,struct tcindex_data * cp)302 static int tcindex_alloc_perfect_hash(struct net *net, struct tcindex_data *cp)
303 {
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++) {
312 err = tcf_exts_init(&cp->perfect[i].exts, net,
313 TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
314 if (err < 0)
315 goto errout;
316 cp->perfect[i].p = cp;
317 }
318
319 return 0;
320
321 errout:
322 tcindex_free_perfect_hash(cp);
323 return err;
324 }
325
326 static int
tcindex_set_parms(struct net * net,struct tcf_proto * tp,unsigned long base,u32 handle,struct tcindex_data * p,struct tcindex_filter_result * r,struct nlattr ** tb,struct nlattr * est,bool ovr,struct netlink_ext_ack * extack)327 tcindex_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,
330 struct nlattr *est, bool ovr, struct netlink_ext_ack *extack)
331 {
332 struct tcindex_filter_result new_filter_result, *old_r = r;
333 struct tcindex_data *cp = NULL, *oldp;
334 struct tcindex_filter *f = NULL; /* make gcc behave */
335 struct tcf_result cr = {};
336 int err, balloc = 0;
337 struct tcf_exts e;
338
339 err = tcf_exts_init(&e, net, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
340 if (err < 0)
341 return err;
342 err = tcf_exts_validate(net, tp, tb, est, &e, ovr, true, extack);
343 if (err < 0)
344 goto errout;
345
346 err = -ENOMEM;
347 /* 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 */
351 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
352 if (!cp)
353 goto errout;
354
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;
361 refcount_set(&cp->refcnt, 1); /* Paired with tcindex_destroy_work() */
362
363 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
369 if (tb[TCA_TCINDEX_SHIFT]) {
370 cp->shift = nla_get_u32(tb[TCA_TCINDEX_SHIFT]);
371 if (cp->shift > 16) {
372 err = -EINVAL;
373 goto errout;
374 }
375 }
376 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
386 if (p->perfect) {
387 int i;
388
389 if (tcindex_alloc_perfect_hash(net, cp) < 0)
390 goto errout;
391 cp->alloc_hash = cp->hash;
392 for (i = 0; i < min(cp->hash, p->hash); i++)
393 cp->perfect[i].res = p->perfect[i].res;
394 balloc = 1;
395 }
396 cp->h = p->h;
397
398 err = tcindex_filter_result_init(&new_filter_result, cp, net);
399 if (err < 0)
400 goto errout_alloc;
401 if (old_r)
402 cr = r->res;
403
404 err = -EBUSY;
405
406 /* Hash already allocated, make sure that we still meet the
407 * requirements for the allocated hash.
408 */
409 if (cp->perfect) {
410 if (!valid_perfect_hash(cp) ||
411 cp->hash > cp->alloc_hash)
412 goto errout_alloc;
413 } else if (cp->h && cp->hash != cp->alloc_hash) {
414 goto errout_alloc;
415 }
416
417 err = -EINVAL;
418 if (tb[TCA_TCINDEX_FALL_THROUGH])
419 cp->fall_through = nla_get_u32(tb[TCA_TCINDEX_FALL_THROUGH]);
420
421 if (!cp->perfect && !cp->h)
422 cp->alloc_hash = cp->hash;
423
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 */
429 if (cp->perfect || valid_perfect_hash(cp))
430 if (handle >= cp->alloc_hash)
431 goto errout_alloc;
432
433
434 err = -ENOMEM;
435 if (!cp->perfect && !cp->h) {
436 if (valid_perfect_hash(cp)) {
437 if (tcindex_alloc_perfect_hash(net, cp) < 0)
438 goto errout_alloc;
439 balloc = 1;
440 } else {
441 struct tcindex_filter __rcu **hash;
442
443 hash = kcalloc(cp->hash,
444 sizeof(struct tcindex_filter *),
445 GFP_KERNEL);
446
447 if (!hash)
448 goto errout_alloc;
449
450 cp->h = hash;
451 balloc = 2;
452 }
453 }
454
455 if (cp->perfect)
456 r = cp->perfect + handle;
457 else
458 r = tcindex_lookup(cp, handle) ? : &new_filter_result;
459
460 if (r == &new_filter_result) {
461 f = kzalloc(sizeof(*f), GFP_KERNEL);
462 if (!f)
463 goto errout_alloc;
464 f->key = handle;
465 f->next = NULL;
466 err = tcindex_filter_result_init(&f->result, cp, net);
467 if (err < 0) {
468 kfree(f);
469 goto errout_alloc;
470 }
471 }
472
473 if (tb[TCA_TCINDEX_CLASSID]) {
474 cr.classid = nla_get_u32(tb[TCA_TCINDEX_CLASSID]);
475 tcf_bind_filter(tp, &cr, base);
476 }
477
478 if (old_r && old_r != r) {
479 err = tcindex_filter_result_init(old_r, cp, net);
480 if (err < 0) {
481 kfree(f);
482 goto errout_alloc;
483 }
484 }
485
486 oldp = p;
487 r->res = cr;
488 tcf_exts_change(&r->exts, &e);
489
490 rcu_assign_pointer(tp->root, cp);
491
492 if (r == &new_filter_result) {
493 struct tcindex_filter *nfp;
494 struct tcindex_filter __rcu **fp;
495
496 f->result.res = r->res;
497 tcf_exts_change(&f->result.exts, &r->exts);
498
499 fp = cp->h + (handle % cp->hash);
500 for (nfp = rtnl_dereference(*fp);
501 nfp;
502 fp = &nfp->next, nfp = rtnl_dereference(*fp))
503 ; /* nothing */
504
505 rcu_assign_pointer(*fp, f);
506 } else {
507 tcf_exts_destroy(&new_filter_result.exts);
508 }
509
510 if (oldp)
511 tcf_queue_work(&oldp->rwork, tcindex_partial_destroy_work);
512 return 0;
513
514 errout_alloc:
515 if (balloc == 1)
516 tcindex_free_perfect_hash(cp);
517 else if (balloc == 2)
518 kfree(cp->h);
519 tcf_exts_destroy(&new_filter_result.exts);
520 errout:
521 kfree(cp);
522 tcf_exts_destroy(&e);
523 return err;
524 }
525
526 static int
tcindex_change(struct net * net,struct sk_buff * in_skb,struct tcf_proto * tp,unsigned long base,u32 handle,struct nlattr ** tca,void ** arg,bool ovr,bool rtnl_held,struct netlink_ext_ack * extack)527 tcindex_change(struct net *net, struct sk_buff *in_skb,
528 struct tcf_proto *tp, unsigned long base, u32 handle,
529 struct nlattr **tca, void **arg, bool ovr,
530 bool rtnl_held, struct netlink_ext_ack *extack)
531 {
532 struct nlattr *opt = tca[TCA_OPTIONS];
533 struct nlattr *tb[TCA_TCINDEX_MAX + 1];
534 struct tcindex_data *p = rtnl_dereference(tp->root);
535 struct tcindex_filter_result *r = *arg;
536 int err;
537
538 pr_debug("tcindex_change(tp %p,handle 0x%08x,tca %p,arg %p),opt %p,"
539 "p %p,r %p,*arg %p\n",
540 tp, handle, tca, arg, opt, p, r, *arg);
541
542 if (!opt)
543 return 0;
544
545 err = nla_parse_nested_deprecated(tb, TCA_TCINDEX_MAX, opt,
546 tcindex_policy, NULL);
547 if (err < 0)
548 return err;
549
550 return tcindex_set_parms(net, tp, base, handle, p, r, tb,
551 tca[TCA_RATE], ovr, extack);
552 }
553
tcindex_walk(struct tcf_proto * tp,struct tcf_walker * walker,bool rtnl_held)554 static void tcindex_walk(struct tcf_proto *tp, struct tcf_walker *walker,
555 bool rtnl_held)
556 {
557 struct tcindex_data *p = rtnl_dereference(tp->root);
558 struct tcindex_filter *f, *next;
559 int i;
560
561 pr_debug("tcindex_walk(tp %p,walker %p),p %p\n", tp, walker, p);
562 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) {
567 if (walker->fn(tp, p->perfect + i, walker) < 0) {
568 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++) {
578 for (f = rtnl_dereference(p->h[i]); f; f = next) {
579 next = rtnl_dereference(f->next);
580 if (walker->count >= walker->skip) {
581 if (walker->fn(tp, &f->result, walker) < 0) {
582 walker->stop = 1;
583 return;
584 }
585 }
586 walker->count++;
587 }
588 }
589 }
590
tcindex_destroy(struct tcf_proto * tp,bool rtnl_held,struct netlink_ext_ack * extack)591 static void tcindex_destroy(struct tcf_proto *tp, bool rtnl_held,
592 struct netlink_ext_ack *extack)
593 {
594 struct tcindex_data *p = rtnl_dereference(tp->root);
595 int i;
596
597 pr_debug("tcindex_destroy(tp %p),p %p\n", tp, p);
598
599 if (p->perfect) {
600 for (i = 0; i < p->hash; i++) {
601 struct tcindex_filter_result *r = p->perfect + i;
602
603 /* 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
611 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 }
629
630 tcf_queue_work(&p->rwork, tcindex_destroy_work);
631 }
632
633
tcindex_dump(struct net * net,struct tcf_proto * tp,void * fh,struct sk_buff * skb,struct tcmsg * t,bool rtnl_held)634 static int tcindex_dump(struct net *net, struct tcf_proto *tp, void *fh,
635 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
636 {
637 struct tcindex_data *p = rtnl_dereference(tp->root);
638 struct tcindex_filter_result *r = fh;
639 struct nlattr *nest;
640
641 pr_debug("tcindex_dump(tp %p,fh %p,skb %p,t %p),p %p,r %p\n",
642 tp, fh, skb, t, p, r);
643 pr_debug("p->perfect %p p->h %p\n", p->perfect, p->h);
644
645 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
646 if (nest == NULL)
647 goto nla_put_failure;
648
649 if (!fh) {
650 t->tcm_handle = ~0; /* whatever ... */
651 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;
656 nla_nest_end(skb, nest);
657 } else {
658 if (p->perfect) {
659 t->tcm_handle = r - p->perfect;
660 } else {
661 struct tcindex_filter *f;
662 struct tcindex_filter __rcu **fp;
663 int i;
664
665 t->tcm_handle = 0;
666 for (i = 0; !t->tcm_handle && i < p->hash; i++) {
667 fp = &p->h[i];
668 for (f = rtnl_dereference(*fp);
669 !t->tcm_handle && f;
670 fp = &f->next, f = rtnl_dereference(*fp)) {
671 if (&f->result == r)
672 t->tcm_handle = f->key;
673 }
674 }
675 }
676 pr_debug("handle = %d\n", t->tcm_handle);
677 if (r->res.class &&
678 nla_put_u32(skb, TCA_TCINDEX_CLASSID, r->res.classid))
679 goto nla_put_failure;
680
681 if (tcf_exts_dump(skb, &r->exts) < 0)
682 goto nla_put_failure;
683 nla_nest_end(skb, nest);
684
685 if (tcf_exts_dump_stats(skb, &r->exts) < 0)
686 goto nla_put_failure;
687 }
688
689 return skb->len;
690
691 nla_put_failure:
692 nla_nest_cancel(skb, nest);
693 return -1;
694 }
695
tcindex_bind_class(void * fh,u32 classid,unsigned long cl,void * q,unsigned long base)696 static void tcindex_bind_class(void *fh, u32 classid, unsigned long cl,
697 void *q, unsigned long base)
698 {
699 struct tcindex_filter_result *r = fh;
700
701 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 }
707 }
708
709 static struct tcf_proto_ops cls_tcindex_ops __read_mostly = {
710 .kind = "tcindex",
711 .classify = tcindex_classify,
712 .init = tcindex_init,
713 .destroy = tcindex_destroy,
714 .get = tcindex_get,
715 .change = tcindex_change,
716 .delete = tcindex_delete,
717 .walk = tcindex_walk,
718 .dump = tcindex_dump,
719 .bind_class = tcindex_bind_class,
720 .owner = THIS_MODULE,
721 };
722
init_tcindex(void)723 static int __init init_tcindex(void)
724 {
725 return register_tcf_proto_ops(&cls_tcindex_ops);
726 }
727
exit_tcindex(void)728 static void __exit exit_tcindex(void)
729 {
730 unregister_tcf_proto_ops(&cls_tcindex_ops);
731 }
732
733 module_init(init_tcindex)
734 module_exit(exit_tcindex)
735 MODULE_LICENSE("GPL");
736