Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * net/sched/cls_fw.c   Classifier mapping ipchains' fwmark to traffic class.
0004  *
0005  * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
0006  *
0007  * Changes:
0008  * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_walk off by one
0009  * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_delete killed all the filter (and kernel).
0010  * Alex <alex@pilotsoft.com> : 2004xxyy: Added Action extension
0011  */
0012 
0013 #include <linux/module.h>
0014 #include <linux/slab.h>
0015 #include <linux/types.h>
0016 #include <linux/kernel.h>
0017 #include <linux/string.h>
0018 #include <linux/errno.h>
0019 #include <linux/skbuff.h>
0020 #include <net/netlink.h>
0021 #include <net/act_api.h>
0022 #include <net/pkt_cls.h>
0023 #include <net/sch_generic.h>
0024 
0025 #define HTSIZE 256
0026 
0027 struct fw_head {
0028     u32         mask;
0029     struct fw_filter __rcu  *ht[HTSIZE];
0030     struct rcu_head     rcu;
0031 };
0032 
0033 struct fw_filter {
0034     struct fw_filter __rcu  *next;
0035     u32         id;
0036     struct tcf_result   res;
0037     int         ifindex;
0038     struct tcf_exts     exts;
0039     struct tcf_proto    *tp;
0040     struct rcu_work     rwork;
0041 };
0042 
0043 static u32 fw_hash(u32 handle)
0044 {
0045     handle ^= (handle >> 16);
0046     handle ^= (handle >> 8);
0047     return handle % HTSIZE;
0048 }
0049 
0050 static int fw_classify(struct sk_buff *skb, const struct tcf_proto *tp,
0051                struct tcf_result *res)
0052 {
0053     struct fw_head *head = rcu_dereference_bh(tp->root);
0054     struct fw_filter *f;
0055     int r;
0056     u32 id = skb->mark;
0057 
0058     if (head != NULL) {
0059         id &= head->mask;
0060 
0061         for (f = rcu_dereference_bh(head->ht[fw_hash(id)]); f;
0062              f = rcu_dereference_bh(f->next)) {
0063             if (f->id == id) {
0064                 *res = f->res;
0065                 if (!tcf_match_indev(skb, f->ifindex))
0066                     continue;
0067                 r = tcf_exts_exec(skb, &f->exts, res);
0068                 if (r < 0)
0069                     continue;
0070 
0071                 return r;
0072             }
0073         }
0074     } else {
0075         struct Qdisc *q = tcf_block_q(tp->chain->block);
0076 
0077         /* Old method: classify the packet using its skb mark. */
0078         if (id && (TC_H_MAJ(id) == 0 ||
0079                !(TC_H_MAJ(id ^ q->handle)))) {
0080             res->classid = id;
0081             res->class = 0;
0082             return 0;
0083         }
0084     }
0085 
0086     return -1;
0087 }
0088 
0089 static void *fw_get(struct tcf_proto *tp, u32 handle)
0090 {
0091     struct fw_head *head = rtnl_dereference(tp->root);
0092     struct fw_filter *f;
0093 
0094     if (head == NULL)
0095         return NULL;
0096 
0097     f = rtnl_dereference(head->ht[fw_hash(handle)]);
0098     for (; f; f = rtnl_dereference(f->next)) {
0099         if (f->id == handle)
0100             return f;
0101     }
0102     return NULL;
0103 }
0104 
0105 static int fw_init(struct tcf_proto *tp)
0106 {
0107     /* We don't allocate fw_head here, because in the old method
0108      * we don't need it at all.
0109      */
0110     return 0;
0111 }
0112 
0113 static void __fw_delete_filter(struct fw_filter *f)
0114 {
0115     tcf_exts_destroy(&f->exts);
0116     tcf_exts_put_net(&f->exts);
0117     kfree(f);
0118 }
0119 
0120 static void fw_delete_filter_work(struct work_struct *work)
0121 {
0122     struct fw_filter *f = container_of(to_rcu_work(work),
0123                        struct fw_filter,
0124                        rwork);
0125     rtnl_lock();
0126     __fw_delete_filter(f);
0127     rtnl_unlock();
0128 }
0129 
0130 static void fw_destroy(struct tcf_proto *tp, bool rtnl_held,
0131                struct netlink_ext_ack *extack)
0132 {
0133     struct fw_head *head = rtnl_dereference(tp->root);
0134     struct fw_filter *f;
0135     int h;
0136 
0137     if (head == NULL)
0138         return;
0139 
0140     for (h = 0; h < HTSIZE; h++) {
0141         while ((f = rtnl_dereference(head->ht[h])) != NULL) {
0142             RCU_INIT_POINTER(head->ht[h],
0143                      rtnl_dereference(f->next));
0144             tcf_unbind_filter(tp, &f->res);
0145             if (tcf_exts_get_net(&f->exts))
0146                 tcf_queue_work(&f->rwork, fw_delete_filter_work);
0147             else
0148                 __fw_delete_filter(f);
0149         }
0150     }
0151     kfree_rcu(head, rcu);
0152 }
0153 
0154 static int fw_delete(struct tcf_proto *tp, void *arg, bool *last,
0155              bool rtnl_held, struct netlink_ext_ack *extack)
0156 {
0157     struct fw_head *head = rtnl_dereference(tp->root);
0158     struct fw_filter *f = arg;
0159     struct fw_filter __rcu **fp;
0160     struct fw_filter *pfp;
0161     int ret = -EINVAL;
0162     int h;
0163 
0164     if (head == NULL || f == NULL)
0165         goto out;
0166 
0167     fp = &head->ht[fw_hash(f->id)];
0168 
0169     for (pfp = rtnl_dereference(*fp); pfp;
0170          fp = &pfp->next, pfp = rtnl_dereference(*fp)) {
0171         if (pfp == f) {
0172             RCU_INIT_POINTER(*fp, rtnl_dereference(f->next));
0173             tcf_unbind_filter(tp, &f->res);
0174             tcf_exts_get_net(&f->exts);
0175             tcf_queue_work(&f->rwork, fw_delete_filter_work);
0176             ret = 0;
0177             break;
0178         }
0179     }
0180 
0181     *last = true;
0182     for (h = 0; h < HTSIZE; h++) {
0183         if (rcu_access_pointer(head->ht[h])) {
0184             *last = false;
0185             break;
0186         }
0187     }
0188 
0189 out:
0190     return ret;
0191 }
0192 
0193 static const struct nla_policy fw_policy[TCA_FW_MAX + 1] = {
0194     [TCA_FW_CLASSID]    = { .type = NLA_U32 },
0195     [TCA_FW_INDEV]      = { .type = NLA_STRING, .len = IFNAMSIZ },
0196     [TCA_FW_MASK]       = { .type = NLA_U32 },
0197 };
0198 
0199 static int fw_set_parms(struct net *net, struct tcf_proto *tp,
0200             struct fw_filter *f, struct nlattr **tb,
0201             struct nlattr **tca, unsigned long base, u32 flags,
0202             struct netlink_ext_ack *extack)
0203 {
0204     struct fw_head *head = rtnl_dereference(tp->root);
0205     u32 mask;
0206     int err;
0207 
0208     err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &f->exts, flags,
0209                 extack);
0210     if (err < 0)
0211         return err;
0212 
0213     if (tb[TCA_FW_CLASSID]) {
0214         f->res.classid = nla_get_u32(tb[TCA_FW_CLASSID]);
0215         tcf_bind_filter(tp, &f->res, base);
0216     }
0217 
0218     if (tb[TCA_FW_INDEV]) {
0219         int ret;
0220         ret = tcf_change_indev(net, tb[TCA_FW_INDEV], extack);
0221         if (ret < 0)
0222             return ret;
0223         f->ifindex = ret;
0224     }
0225 
0226     err = -EINVAL;
0227     if (tb[TCA_FW_MASK]) {
0228         mask = nla_get_u32(tb[TCA_FW_MASK]);
0229         if (mask != head->mask)
0230             return err;
0231     } else if (head->mask != 0xFFFFFFFF)
0232         return err;
0233 
0234     return 0;
0235 }
0236 
0237 static int fw_change(struct net *net, struct sk_buff *in_skb,
0238              struct tcf_proto *tp, unsigned long base,
0239              u32 handle, struct nlattr **tca, void **arg,
0240              u32 flags, struct netlink_ext_ack *extack)
0241 {
0242     struct fw_head *head = rtnl_dereference(tp->root);
0243     struct fw_filter *f = *arg;
0244     struct nlattr *opt = tca[TCA_OPTIONS];
0245     struct nlattr *tb[TCA_FW_MAX + 1];
0246     int err;
0247 
0248     if (!opt)
0249         return handle ? -EINVAL : 0; /* Succeed if it is old method. */
0250 
0251     err = nla_parse_nested_deprecated(tb, TCA_FW_MAX, opt, fw_policy,
0252                       NULL);
0253     if (err < 0)
0254         return err;
0255 
0256     if (f) {
0257         struct fw_filter *pfp, *fnew;
0258         struct fw_filter __rcu **fp;
0259 
0260         if (f->id != handle && handle)
0261             return -EINVAL;
0262 
0263         fnew = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
0264         if (!fnew)
0265             return -ENOBUFS;
0266 
0267         fnew->id = f->id;
0268         fnew->res = f->res;
0269         fnew->ifindex = f->ifindex;
0270         fnew->tp = f->tp;
0271 
0272         err = tcf_exts_init(&fnew->exts, net, TCA_FW_ACT,
0273                     TCA_FW_POLICE);
0274         if (err < 0) {
0275             kfree(fnew);
0276             return err;
0277         }
0278 
0279         err = fw_set_parms(net, tp, fnew, tb, tca, base, flags, extack);
0280         if (err < 0) {
0281             tcf_exts_destroy(&fnew->exts);
0282             kfree(fnew);
0283             return err;
0284         }
0285 
0286         fp = &head->ht[fw_hash(fnew->id)];
0287         for (pfp = rtnl_dereference(*fp); pfp;
0288              fp = &pfp->next, pfp = rtnl_dereference(*fp))
0289             if (pfp == f)
0290                 break;
0291 
0292         RCU_INIT_POINTER(fnew->next, rtnl_dereference(pfp->next));
0293         rcu_assign_pointer(*fp, fnew);
0294         tcf_unbind_filter(tp, &f->res);
0295         tcf_exts_get_net(&f->exts);
0296         tcf_queue_work(&f->rwork, fw_delete_filter_work);
0297 
0298         *arg = fnew;
0299         return err;
0300     }
0301 
0302     if (!handle)
0303         return -EINVAL;
0304 
0305     if (!head) {
0306         u32 mask = 0xFFFFFFFF;
0307         if (tb[TCA_FW_MASK])
0308             mask = nla_get_u32(tb[TCA_FW_MASK]);
0309 
0310         head = kzalloc(sizeof(*head), GFP_KERNEL);
0311         if (!head)
0312             return -ENOBUFS;
0313         head->mask = mask;
0314 
0315         rcu_assign_pointer(tp->root, head);
0316     }
0317 
0318     f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
0319     if (f == NULL)
0320         return -ENOBUFS;
0321 
0322     err = tcf_exts_init(&f->exts, net, TCA_FW_ACT, TCA_FW_POLICE);
0323     if (err < 0)
0324         goto errout;
0325     f->id = handle;
0326     f->tp = tp;
0327 
0328     err = fw_set_parms(net, tp, f, tb, tca, base, flags, extack);
0329     if (err < 0)
0330         goto errout;
0331 
0332     RCU_INIT_POINTER(f->next, head->ht[fw_hash(handle)]);
0333     rcu_assign_pointer(head->ht[fw_hash(handle)], f);
0334 
0335     *arg = f;
0336     return 0;
0337 
0338 errout:
0339     tcf_exts_destroy(&f->exts);
0340     kfree(f);
0341     return err;
0342 }
0343 
0344 static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg,
0345             bool rtnl_held)
0346 {
0347     struct fw_head *head = rtnl_dereference(tp->root);
0348     int h;
0349 
0350     if (head == NULL)
0351         arg->stop = 1;
0352 
0353     if (arg->stop)
0354         return;
0355 
0356     for (h = 0; h < HTSIZE; h++) {
0357         struct fw_filter *f;
0358 
0359         for (f = rtnl_dereference(head->ht[h]); f;
0360              f = rtnl_dereference(f->next)) {
0361             if (arg->count < arg->skip) {
0362                 arg->count++;
0363                 continue;
0364             }
0365             if (arg->fn(tp, f, arg) < 0) {
0366                 arg->stop = 1;
0367                 return;
0368             }
0369             arg->count++;
0370         }
0371     }
0372 }
0373 
0374 static int fw_dump(struct net *net, struct tcf_proto *tp, void *fh,
0375            struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
0376 {
0377     struct fw_head *head = rtnl_dereference(tp->root);
0378     struct fw_filter *f = fh;
0379     struct nlattr *nest;
0380 
0381     if (f == NULL)
0382         return skb->len;
0383 
0384     t->tcm_handle = f->id;
0385 
0386     if (!f->res.classid && !tcf_exts_has_actions(&f->exts))
0387         return skb->len;
0388 
0389     nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
0390     if (nest == NULL)
0391         goto nla_put_failure;
0392 
0393     if (f->res.classid &&
0394         nla_put_u32(skb, TCA_FW_CLASSID, f->res.classid))
0395         goto nla_put_failure;
0396     if (f->ifindex) {
0397         struct net_device *dev;
0398         dev = __dev_get_by_index(net, f->ifindex);
0399         if (dev && nla_put_string(skb, TCA_FW_INDEV, dev->name))
0400             goto nla_put_failure;
0401     }
0402     if (head->mask != 0xFFFFFFFF &&
0403         nla_put_u32(skb, TCA_FW_MASK, head->mask))
0404         goto nla_put_failure;
0405 
0406     if (tcf_exts_dump(skb, &f->exts) < 0)
0407         goto nla_put_failure;
0408 
0409     nla_nest_end(skb, nest);
0410 
0411     if (tcf_exts_dump_stats(skb, &f->exts) < 0)
0412         goto nla_put_failure;
0413 
0414     return skb->len;
0415 
0416 nla_put_failure:
0417     nla_nest_cancel(skb, nest);
0418     return -1;
0419 }
0420 
0421 static void fw_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
0422               unsigned long base)
0423 {
0424     struct fw_filter *f = fh;
0425 
0426     if (f && f->res.classid == classid) {
0427         if (cl)
0428             __tcf_bind_filter(q, &f->res, base);
0429         else
0430             __tcf_unbind_filter(q, &f->res);
0431     }
0432 }
0433 
0434 static struct tcf_proto_ops cls_fw_ops __read_mostly = {
0435     .kind       =   "fw",
0436     .classify   =   fw_classify,
0437     .init       =   fw_init,
0438     .destroy    =   fw_destroy,
0439     .get        =   fw_get,
0440     .change     =   fw_change,
0441     .delete     =   fw_delete,
0442     .walk       =   fw_walk,
0443     .dump       =   fw_dump,
0444     .bind_class =   fw_bind_class,
0445     .owner      =   THIS_MODULE,
0446 };
0447 
0448 static int __init init_fw(void)
0449 {
0450     return register_tcf_proto_ops(&cls_fw_ops);
0451 }
0452 
0453 static void __exit exit_fw(void)
0454 {
0455     unregister_tcf_proto_ops(&cls_fw_ops);
0456 }
0457 
0458 module_init(init_fw)
0459 module_exit(exit_fw)
0460 MODULE_LICENSE("GPL");