Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * net/sched/cls_tcindex.c  Packet classifier for skb->tc_index
0004  *
0005  * Written 1998,1999 by Werner Almesberger, EPFL ICA
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/types.h>
0010 #include <linux/kernel.h>
0011 #include <linux/skbuff.h>
0012 #include <linux/errno.h>
0013 #include <linux/slab.h>
0014 #include <linux/refcount.h>
0015 #include <net/act_api.h>
0016 #include <net/netlink.h>
0017 #include <net/pkt_cls.h>
0018 #include <net/sch_generic.h>
0019 
0020 /*
0021  * Passing parameters to the root seems to be done more awkwardly than really
0022  * necessary. At least, u32 doesn't seem to use such dirty hacks. To be
0023  * verified. FIXME.
0024  */
0025 
0026 #define PERFECT_HASH_THRESHOLD  64  /* use perfect hash if not bigger */
0027 #define DEFAULT_HASH_SIZE   64  /* optimized for diffserv */
0028 
0029 
0030 struct tcindex_data;
0031 
0032 struct tcindex_filter_result {
0033     struct tcf_exts     exts;
0034     struct tcf_result   res;
0035     struct tcindex_data *p;
0036     struct rcu_work     rwork;
0037 };
0038 
0039 struct tcindex_filter {
0040     u16 key;
0041     struct tcindex_filter_result result;
0042     struct tcindex_filter __rcu *next;
0043     struct rcu_work rwork;
0044 };
0045 
0046 
0047 struct tcindex_data {
0048     struct tcindex_filter_result *perfect; /* perfect hash; NULL if none */
0049     struct tcindex_filter __rcu **h; /* imperfect hash; */
0050     struct tcf_proto *tp;
0051     u16 mask;       /* AND key with mask */
0052     u32 shift;      /* shift ANDed key to the right */
0053     u32 hash;       /* hash table size; 0 if undefined */
0054     u32 alloc_hash;     /* allocated size */
0055     u32 fall_through;   /* 0: only classify if explicit match */
0056     refcount_t refcnt;  /* a temporary refcnt for perfect hash */
0057     struct rcu_work rwork;
0058 };
0059 
0060 static inline int tcindex_filter_is_set(struct tcindex_filter_result *r)
0061 {
0062     return tcf_exts_has_actions(&r->exts) || r->res.classid;
0063 }
0064 
0065 static void tcindex_data_get(struct tcindex_data *p)
0066 {
0067     refcount_inc(&p->refcnt);
0068 }
0069 
0070 static void tcindex_data_put(struct tcindex_data *p)
0071 {
0072     if (refcount_dec_and_test(&p->refcnt)) {
0073         kfree(p->perfect);
0074         kfree(p->h);
0075         kfree(p);
0076     }
0077 }
0078 
0079 static struct tcindex_filter_result *tcindex_lookup(struct tcindex_data *p,
0080                             u16 key)
0081 {
0082     if (p->perfect) {
0083         struct tcindex_filter_result *f = p->perfect + key;
0084 
0085         return tcindex_filter_is_set(f) ? f : NULL;
0086     } else if (p->h) {
0087         struct tcindex_filter __rcu **fp;
0088         struct tcindex_filter *f;
0089 
0090         fp = &p->h[key % p->hash];
0091         for (f = rcu_dereference_bh_rtnl(*fp);
0092              f;
0093              fp = &f->next, f = rcu_dereference_bh_rtnl(*fp))
0094             if (f->key == key)
0095                 return &f->result;
0096     }
0097 
0098     return NULL;
0099 }
0100 
0101 
0102 static int tcindex_classify(struct sk_buff *skb, const struct tcf_proto *tp,
0103                 struct tcf_result *res)
0104 {
0105     struct tcindex_data *p = rcu_dereference_bh(tp->root);
0106     struct tcindex_filter_result *f;
0107     int key = (skb->tc_index & p->mask) >> p->shift;
0108 
0109     pr_debug("tcindex_classify(skb %p,tp %p,res %p),p %p\n",
0110          skb, tp, res, p);
0111 
0112     f = tcindex_lookup(p, key);
0113     if (!f) {
0114         struct Qdisc *q = tcf_block_q(tp->chain->block);
0115 
0116         if (!p->fall_through)
0117             return -1;
0118         res->classid = TC_H_MAKE(TC_H_MAJ(q->handle), key);
0119         res->class = 0;
0120         pr_debug("alg 0x%x\n", res->classid);
0121         return 0;
0122     }
0123     *res = f->res;
0124     pr_debug("map 0x%x\n", res->classid);
0125 
0126     return tcf_exts_exec(skb, &f->exts, res);
0127 }
0128 
0129 
0130 static void *tcindex_get(struct tcf_proto *tp, u32 handle)
0131 {
0132     struct tcindex_data *p = rtnl_dereference(tp->root);
0133     struct tcindex_filter_result *r;
0134 
0135     pr_debug("tcindex_get(tp %p,handle 0x%08x)\n", tp, handle);
0136     if (p->perfect && handle >= p->alloc_hash)
0137         return NULL;
0138     r = tcindex_lookup(p, handle);
0139     return r && tcindex_filter_is_set(r) ? r : NULL;
0140 }
0141 
0142 static int tcindex_init(struct tcf_proto *tp)
0143 {
0144     struct tcindex_data *p;
0145 
0146     pr_debug("tcindex_init(tp %p)\n", tp);
0147     p = kzalloc(sizeof(struct tcindex_data), GFP_KERNEL);
0148     if (!p)
0149         return -ENOMEM;
0150 
0151     p->mask = 0xffff;
0152     p->hash = DEFAULT_HASH_SIZE;
0153     p->fall_through = 1;
0154     refcount_set(&p->refcnt, 1); /* Paired with tcindex_destroy_work() */
0155 
0156     rcu_assign_pointer(tp->root, p);
0157     return 0;
0158 }
0159 
0160 static void __tcindex_destroy_rexts(struct tcindex_filter_result *r)
0161 {
0162     tcf_exts_destroy(&r->exts);
0163     tcf_exts_put_net(&r->exts);
0164     tcindex_data_put(r->p);
0165 }
0166 
0167 static void tcindex_destroy_rexts_work(struct work_struct *work)
0168 {
0169     struct tcindex_filter_result *r;
0170 
0171     r = container_of(to_rcu_work(work),
0172              struct tcindex_filter_result,
0173              rwork);
0174     rtnl_lock();
0175     __tcindex_destroy_rexts(r);
0176     rtnl_unlock();
0177 }
0178 
0179 static void __tcindex_destroy_fexts(struct tcindex_filter *f)
0180 {
0181     tcf_exts_destroy(&f->result.exts);
0182     tcf_exts_put_net(&f->result.exts);
0183     kfree(f);
0184 }
0185 
0186 static void tcindex_destroy_fexts_work(struct work_struct *work)
0187 {
0188     struct tcindex_filter *f = container_of(to_rcu_work(work),
0189                         struct tcindex_filter,
0190                         rwork);
0191 
0192     rtnl_lock();
0193     __tcindex_destroy_fexts(f);
0194     rtnl_unlock();
0195 }
0196 
0197 static int tcindex_delete(struct tcf_proto *tp, void *arg, bool *last,
0198               bool rtnl_held, struct netlink_ext_ack *extack)
0199 {
0200     struct tcindex_data *p = rtnl_dereference(tp->root);
0201     struct tcindex_filter_result *r = arg;
0202     struct tcindex_filter __rcu **walk;
0203     struct tcindex_filter *f = NULL;
0204 
0205     pr_debug("tcindex_delete(tp %p,arg %p),p %p\n", tp, arg, p);
0206     if (p->perfect) {
0207         if (!r->res.class)
0208             return -ENOENT;
0209     } else {
0210         int i;
0211 
0212         for (i = 0; i < p->hash; i++) {
0213             walk = p->h + i;
0214             for (f = rtnl_dereference(*walk); f;
0215                  walk = &f->next, f = rtnl_dereference(*walk)) {
0216                 if (&f->result == r)
0217                     goto found;
0218             }
0219         }
0220         return -ENOENT;
0221 
0222 found:
0223         rcu_assign_pointer(*walk, rtnl_dereference(f->next));
0224     }
0225     tcf_unbind_filter(tp, &r->res);
0226     /* all classifiers are required to call tcf_exts_destroy() after rcu
0227      * grace period, since converted-to-rcu actions are relying on that
0228      * in cleanup() callback
0229      */
0230     if (f) {
0231         if (tcf_exts_get_net(&f->result.exts))
0232             tcf_queue_work(&f->rwork, tcindex_destroy_fexts_work);
0233         else
0234             __tcindex_destroy_fexts(f);
0235     } else {
0236         tcindex_data_get(p);
0237 
0238         if (tcf_exts_get_net(&r->exts))
0239             tcf_queue_work(&r->rwork, tcindex_destroy_rexts_work);
0240         else
0241             __tcindex_destroy_rexts(r);
0242     }
0243 
0244     *last = false;
0245     return 0;
0246 }
0247 
0248 static void tcindex_destroy_work(struct work_struct *work)
0249 {
0250     struct tcindex_data *p = container_of(to_rcu_work(work),
0251                           struct tcindex_data,
0252                           rwork);
0253 
0254     tcindex_data_put(p);
0255 }
0256 
0257 static inline int
0258 valid_perfect_hash(struct tcindex_data *p)
0259 {
0260     return  p->hash > (p->mask >> p->shift);
0261 }
0262 
0263 static const struct nla_policy tcindex_policy[TCA_TCINDEX_MAX + 1] = {
0264     [TCA_TCINDEX_HASH]      = { .type = NLA_U32 },
0265     [TCA_TCINDEX_MASK]      = { .type = NLA_U16 },
0266     [TCA_TCINDEX_SHIFT]     = { .type = NLA_U32 },
0267     [TCA_TCINDEX_FALL_THROUGH]  = { .type = NLA_U32 },
0268     [TCA_TCINDEX_CLASSID]       = { .type = NLA_U32 },
0269 };
0270 
0271 static int tcindex_filter_result_init(struct tcindex_filter_result *r,
0272                       struct tcindex_data *p,
0273                       struct net *net)
0274 {
0275     memset(r, 0, sizeof(*r));
0276     r->p = p;
0277     return tcf_exts_init(&r->exts, net, TCA_TCINDEX_ACT,
0278                  TCA_TCINDEX_POLICE);
0279 }
0280 
0281 static void tcindex_free_perfect_hash(struct tcindex_data *cp);
0282 
0283 static void tcindex_partial_destroy_work(struct work_struct *work)
0284 {
0285     struct tcindex_data *p = container_of(to_rcu_work(work),
0286                           struct tcindex_data,
0287                           rwork);
0288 
0289     rtnl_lock();
0290     if (p->perfect)
0291         tcindex_free_perfect_hash(p);
0292     kfree(p);
0293     rtnl_unlock();
0294 }
0295 
0296 static void tcindex_free_perfect_hash(struct tcindex_data *cp)
0297 {
0298     int i;
0299 
0300     for (i = 0; i < cp->hash; i++)
0301         tcf_exts_destroy(&cp->perfect[i].exts);
0302     kfree(cp->perfect);
0303 }
0304 
0305 static int tcindex_alloc_perfect_hash(struct net *net, struct tcindex_data *cp)
0306 {
0307     int i, err = 0;
0308 
0309     cp->perfect = kcalloc(cp->hash, sizeof(struct tcindex_filter_result),
0310                   GFP_KERNEL | __GFP_NOWARN);
0311     if (!cp->perfect)
0312         return -ENOMEM;
0313 
0314     for (i = 0; i < cp->hash; i++) {
0315         err = tcf_exts_init(&cp->perfect[i].exts, net,
0316                     TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
0317         if (err < 0)
0318             goto errout;
0319         cp->perfect[i].p = cp;
0320     }
0321 
0322     return 0;
0323 
0324 errout:
0325     tcindex_free_perfect_hash(cp);
0326     return err;
0327 }
0328 
0329 static int
0330 tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
0331           u32 handle, struct tcindex_data *p,
0332           struct tcindex_filter_result *r, struct nlattr **tb,
0333           struct nlattr *est, u32 flags, struct netlink_ext_ack *extack)
0334 {
0335     struct tcindex_filter_result new_filter_result, *old_r = r;
0336     struct tcindex_data *cp = NULL, *oldp;
0337     struct tcindex_filter *f = NULL; /* make gcc behave */
0338     struct tcf_result cr = {};
0339     int err, balloc = 0;
0340     struct tcf_exts e;
0341 
0342     err = tcf_exts_init(&e, net, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
0343     if (err < 0)
0344         return err;
0345     err = tcf_exts_validate(net, tp, tb, est, &e, flags, extack);
0346     if (err < 0)
0347         goto errout;
0348 
0349     err = -ENOMEM;
0350     /* tcindex_data attributes must look atomic to classifier/lookup so
0351      * allocate new tcindex data and RCU assign it onto root. Keeping
0352      * perfect hash and hash pointers from old data.
0353      */
0354     cp = kzalloc(sizeof(*cp), GFP_KERNEL);
0355     if (!cp)
0356         goto errout;
0357 
0358     cp->mask = p->mask;
0359     cp->shift = p->shift;
0360     cp->hash = p->hash;
0361     cp->alloc_hash = p->alloc_hash;
0362     cp->fall_through = p->fall_through;
0363     cp->tp = tp;
0364     refcount_set(&cp->refcnt, 1); /* Paired with tcindex_destroy_work() */
0365 
0366     if (tb[TCA_TCINDEX_HASH])
0367         cp->hash = nla_get_u32(tb[TCA_TCINDEX_HASH]);
0368 
0369     if (tb[TCA_TCINDEX_MASK])
0370         cp->mask = nla_get_u16(tb[TCA_TCINDEX_MASK]);
0371 
0372     if (tb[TCA_TCINDEX_SHIFT]) {
0373         cp->shift = nla_get_u32(tb[TCA_TCINDEX_SHIFT]);
0374         if (cp->shift > 16) {
0375             err = -EINVAL;
0376             goto errout;
0377         }
0378     }
0379     if (!cp->hash) {
0380         /* Hash not specified, use perfect hash if the upper limit
0381          * of the hashing index is below the threshold.
0382          */
0383         if ((cp->mask >> cp->shift) < PERFECT_HASH_THRESHOLD)
0384             cp->hash = (cp->mask >> cp->shift) + 1;
0385         else
0386             cp->hash = DEFAULT_HASH_SIZE;
0387     }
0388 
0389     if (p->perfect) {
0390         int i;
0391 
0392         if (tcindex_alloc_perfect_hash(net, cp) < 0)
0393             goto errout;
0394         cp->alloc_hash = cp->hash;
0395         for (i = 0; i < min(cp->hash, p->hash); i++)
0396             cp->perfect[i].res = p->perfect[i].res;
0397         balloc = 1;
0398     }
0399     cp->h = p->h;
0400 
0401     err = tcindex_filter_result_init(&new_filter_result, cp, net);
0402     if (err < 0)
0403         goto errout_alloc;
0404     if (old_r)
0405         cr = r->res;
0406 
0407     err = -EBUSY;
0408 
0409     /* Hash already allocated, make sure that we still meet the
0410      * requirements for the allocated hash.
0411      */
0412     if (cp->perfect) {
0413         if (!valid_perfect_hash(cp) ||
0414             cp->hash > cp->alloc_hash)
0415             goto errout_alloc;
0416     } else if (cp->h && cp->hash != cp->alloc_hash) {
0417         goto errout_alloc;
0418     }
0419 
0420     err = -EINVAL;
0421     if (tb[TCA_TCINDEX_FALL_THROUGH])
0422         cp->fall_through = nla_get_u32(tb[TCA_TCINDEX_FALL_THROUGH]);
0423 
0424     if (!cp->perfect && !cp->h)
0425         cp->alloc_hash = cp->hash;
0426 
0427     /* Note: this could be as restrictive as if (handle & ~(mask >> shift))
0428      * but then, we'd fail handles that may become valid after some future
0429      * mask change. While this is extremely unlikely to ever matter,
0430      * the check below is safer (and also more backwards-compatible).
0431      */
0432     if (cp->perfect || valid_perfect_hash(cp))
0433         if (handle >= cp->alloc_hash)
0434             goto errout_alloc;
0435 
0436 
0437     err = -ENOMEM;
0438     if (!cp->perfect && !cp->h) {
0439         if (valid_perfect_hash(cp)) {
0440             if (tcindex_alloc_perfect_hash(net, cp) < 0)
0441                 goto errout_alloc;
0442             balloc = 1;
0443         } else {
0444             struct tcindex_filter __rcu **hash;
0445 
0446             hash = kcalloc(cp->hash,
0447                        sizeof(struct tcindex_filter *),
0448                        GFP_KERNEL);
0449 
0450             if (!hash)
0451                 goto errout_alloc;
0452 
0453             cp->h = hash;
0454             balloc = 2;
0455         }
0456     }
0457 
0458     if (cp->perfect)
0459         r = cp->perfect + handle;
0460     else
0461         r = tcindex_lookup(cp, handle) ? : &new_filter_result;
0462 
0463     if (r == &new_filter_result) {
0464         f = kzalloc(sizeof(*f), GFP_KERNEL);
0465         if (!f)
0466             goto errout_alloc;
0467         f->key = handle;
0468         f->next = NULL;
0469         err = tcindex_filter_result_init(&f->result, cp, net);
0470         if (err < 0) {
0471             kfree(f);
0472             goto errout_alloc;
0473         }
0474     }
0475 
0476     if (tb[TCA_TCINDEX_CLASSID]) {
0477         cr.classid = nla_get_u32(tb[TCA_TCINDEX_CLASSID]);
0478         tcf_bind_filter(tp, &cr, base);
0479     }
0480 
0481     if (old_r && old_r != r) {
0482         err = tcindex_filter_result_init(old_r, cp, net);
0483         if (err < 0) {
0484             kfree(f);
0485             goto errout_alloc;
0486         }
0487     }
0488 
0489     oldp = p;
0490     r->res = cr;
0491     tcf_exts_change(&r->exts, &e);
0492 
0493     rcu_assign_pointer(tp->root, cp);
0494 
0495     if (r == &new_filter_result) {
0496         struct tcindex_filter *nfp;
0497         struct tcindex_filter __rcu **fp;
0498 
0499         f->result.res = r->res;
0500         tcf_exts_change(&f->result.exts, &r->exts);
0501 
0502         fp = cp->h + (handle % cp->hash);
0503         for (nfp = rtnl_dereference(*fp);
0504              nfp;
0505              fp = &nfp->next, nfp = rtnl_dereference(*fp))
0506                 ; /* nothing */
0507 
0508         rcu_assign_pointer(*fp, f);
0509     } else {
0510         tcf_exts_destroy(&new_filter_result.exts);
0511     }
0512 
0513     if (oldp)
0514         tcf_queue_work(&oldp->rwork, tcindex_partial_destroy_work);
0515     return 0;
0516 
0517 errout_alloc:
0518     if (balloc == 1)
0519         tcindex_free_perfect_hash(cp);
0520     else if (balloc == 2)
0521         kfree(cp->h);
0522     tcf_exts_destroy(&new_filter_result.exts);
0523 errout:
0524     kfree(cp);
0525     tcf_exts_destroy(&e);
0526     return err;
0527 }
0528 
0529 static int
0530 tcindex_change(struct net *net, struct sk_buff *in_skb,
0531            struct tcf_proto *tp, unsigned long base, u32 handle,
0532            struct nlattr **tca, void **arg, u32 flags,
0533            struct netlink_ext_ack *extack)
0534 {
0535     struct nlattr *opt = tca[TCA_OPTIONS];
0536     struct nlattr *tb[TCA_TCINDEX_MAX + 1];
0537     struct tcindex_data *p = rtnl_dereference(tp->root);
0538     struct tcindex_filter_result *r = *arg;
0539     int err;
0540 
0541     pr_debug("tcindex_change(tp %p,handle 0x%08x,tca %p,arg %p),opt %p,"
0542         "p %p,r %p,*arg %p\n",
0543         tp, handle, tca, arg, opt, p, r, *arg);
0544 
0545     if (!opt)
0546         return 0;
0547 
0548     err = nla_parse_nested_deprecated(tb, TCA_TCINDEX_MAX, opt,
0549                       tcindex_policy, NULL);
0550     if (err < 0)
0551         return err;
0552 
0553     return tcindex_set_parms(net, tp, base, handle, p, r, tb,
0554                  tca[TCA_RATE], flags, extack);
0555 }
0556 
0557 static void tcindex_walk(struct tcf_proto *tp, struct tcf_walker *walker,
0558              bool rtnl_held)
0559 {
0560     struct tcindex_data *p = rtnl_dereference(tp->root);
0561     struct tcindex_filter *f, *next;
0562     int i;
0563 
0564     pr_debug("tcindex_walk(tp %p,walker %p),p %p\n", tp, walker, p);
0565     if (p->perfect) {
0566         for (i = 0; i < p->hash; i++) {
0567             if (!p->perfect[i].res.class)
0568                 continue;
0569             if (walker->count >= walker->skip) {
0570                 if (walker->fn(tp, p->perfect + i, walker) < 0) {
0571                     walker->stop = 1;
0572                     return;
0573                 }
0574             }
0575             walker->count++;
0576         }
0577     }
0578     if (!p->h)
0579         return;
0580     for (i = 0; i < p->hash; i++) {
0581         for (f = rtnl_dereference(p->h[i]); f; f = next) {
0582             next = rtnl_dereference(f->next);
0583             if (walker->count >= walker->skip) {
0584                 if (walker->fn(tp, &f->result, walker) < 0) {
0585                     walker->stop = 1;
0586                     return;
0587                 }
0588             }
0589             walker->count++;
0590         }
0591     }
0592 }
0593 
0594 static void tcindex_destroy(struct tcf_proto *tp, bool rtnl_held,
0595                 struct netlink_ext_ack *extack)
0596 {
0597     struct tcindex_data *p = rtnl_dereference(tp->root);
0598     int i;
0599 
0600     pr_debug("tcindex_destroy(tp %p),p %p\n", tp, p);
0601 
0602     if (p->perfect) {
0603         for (i = 0; i < p->hash; i++) {
0604             struct tcindex_filter_result *r = p->perfect + i;
0605 
0606             /* tcf_queue_work() does not guarantee the ordering we
0607              * want, so we have to take this refcnt temporarily to
0608              * ensure 'p' is freed after all tcindex_filter_result
0609              * here. Imperfect hash does not need this, because it
0610              * uses linked lists rather than an array.
0611              */
0612             tcindex_data_get(p);
0613 
0614             tcf_unbind_filter(tp, &r->res);
0615             if (tcf_exts_get_net(&r->exts))
0616                 tcf_queue_work(&r->rwork,
0617                            tcindex_destroy_rexts_work);
0618             else
0619                 __tcindex_destroy_rexts(r);
0620         }
0621     }
0622 
0623     for (i = 0; p->h && i < p->hash; i++) {
0624         struct tcindex_filter *f, *next;
0625         bool last;
0626 
0627         for (f = rtnl_dereference(p->h[i]); f; f = next) {
0628             next = rtnl_dereference(f->next);
0629             tcindex_delete(tp, &f->result, &last, rtnl_held, NULL);
0630         }
0631     }
0632 
0633     tcf_queue_work(&p->rwork, tcindex_destroy_work);
0634 }
0635 
0636 
0637 static int tcindex_dump(struct net *net, struct tcf_proto *tp, void *fh,
0638             struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
0639 {
0640     struct tcindex_data *p = rtnl_dereference(tp->root);
0641     struct tcindex_filter_result *r = fh;
0642     struct nlattr *nest;
0643 
0644     pr_debug("tcindex_dump(tp %p,fh %p,skb %p,t %p),p %p,r %p\n",
0645          tp, fh, skb, t, p, r);
0646     pr_debug("p->perfect %p p->h %p\n", p->perfect, p->h);
0647 
0648     nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
0649     if (nest == NULL)
0650         goto nla_put_failure;
0651 
0652     if (!fh) {
0653         t->tcm_handle = ~0; /* whatever ... */
0654         if (nla_put_u32(skb, TCA_TCINDEX_HASH, p->hash) ||
0655             nla_put_u16(skb, TCA_TCINDEX_MASK, p->mask) ||
0656             nla_put_u32(skb, TCA_TCINDEX_SHIFT, p->shift) ||
0657             nla_put_u32(skb, TCA_TCINDEX_FALL_THROUGH, p->fall_through))
0658             goto nla_put_failure;
0659         nla_nest_end(skb, nest);
0660     } else {
0661         if (p->perfect) {
0662             t->tcm_handle = r - p->perfect;
0663         } else {
0664             struct tcindex_filter *f;
0665             struct tcindex_filter __rcu **fp;
0666             int i;
0667 
0668             t->tcm_handle = 0;
0669             for (i = 0; !t->tcm_handle && i < p->hash; i++) {
0670                 fp = &p->h[i];
0671                 for (f = rtnl_dereference(*fp);
0672                      !t->tcm_handle && f;
0673                      fp = &f->next, f = rtnl_dereference(*fp)) {
0674                     if (&f->result == r)
0675                         t->tcm_handle = f->key;
0676                 }
0677             }
0678         }
0679         pr_debug("handle = %d\n", t->tcm_handle);
0680         if (r->res.class &&
0681             nla_put_u32(skb, TCA_TCINDEX_CLASSID, r->res.classid))
0682             goto nla_put_failure;
0683 
0684         if (tcf_exts_dump(skb, &r->exts) < 0)
0685             goto nla_put_failure;
0686         nla_nest_end(skb, nest);
0687 
0688         if (tcf_exts_dump_stats(skb, &r->exts) < 0)
0689             goto nla_put_failure;
0690     }
0691 
0692     return skb->len;
0693 
0694 nla_put_failure:
0695     nla_nest_cancel(skb, nest);
0696     return -1;
0697 }
0698 
0699 static void tcindex_bind_class(void *fh, u32 classid, unsigned long cl,
0700                    void *q, unsigned long base)
0701 {
0702     struct tcindex_filter_result *r = fh;
0703 
0704     if (r && r->res.classid == classid) {
0705         if (cl)
0706             __tcf_bind_filter(q, &r->res, base);
0707         else
0708             __tcf_unbind_filter(q, &r->res);
0709     }
0710 }
0711 
0712 static struct tcf_proto_ops cls_tcindex_ops __read_mostly = {
0713     .kind       =   "tcindex",
0714     .classify   =   tcindex_classify,
0715     .init       =   tcindex_init,
0716     .destroy    =   tcindex_destroy,
0717     .get        =   tcindex_get,
0718     .change     =   tcindex_change,
0719     .delete     =   tcindex_delete,
0720     .walk       =   tcindex_walk,
0721     .dump       =   tcindex_dump,
0722     .bind_class =   tcindex_bind_class,
0723     .owner      =   THIS_MODULE,
0724 };
0725 
0726 static int __init init_tcindex(void)
0727 {
0728     return register_tcf_proto_ops(&cls_tcindex_ops);
0729 }
0730 
0731 static void __exit exit_tcindex(void)
0732 {
0733     unregister_tcf_proto_ops(&cls_tcindex_ops);
0734 }
0735 
0736 module_init(init_tcindex)
0737 module_exit(exit_tcindex)
0738 MODULE_LICENSE("GPL");