Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * net/sched/cls_cgroup.c   Control Group Classifier
0004  *
0005  * Authors: Thomas Graf <tgraf@suug.ch>
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/slab.h>
0010 #include <linux/skbuff.h>
0011 #include <linux/rcupdate.h>
0012 #include <net/rtnetlink.h>
0013 #include <net/pkt_cls.h>
0014 #include <net/sock.h>
0015 #include <net/cls_cgroup.h>
0016 
0017 struct cls_cgroup_head {
0018     u32         handle;
0019     struct tcf_exts     exts;
0020     struct tcf_ematch_tree  ematches;
0021     struct tcf_proto    *tp;
0022     struct rcu_work     rwork;
0023 };
0024 
0025 static int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp,
0026                    struct tcf_result *res)
0027 {
0028     struct cls_cgroup_head *head = rcu_dereference_bh(tp->root);
0029     u32 classid = task_get_classid(skb);
0030 
0031     if (unlikely(!head))
0032         return -1;
0033     if (!classid)
0034         return -1;
0035     if (!tcf_em_tree_match(skb, &head->ematches, NULL))
0036         return -1;
0037 
0038     res->classid = classid;
0039     res->class = 0;
0040 
0041     return tcf_exts_exec(skb, &head->exts, res);
0042 }
0043 
0044 static void *cls_cgroup_get(struct tcf_proto *tp, u32 handle)
0045 {
0046     return NULL;
0047 }
0048 
0049 static int cls_cgroup_init(struct tcf_proto *tp)
0050 {
0051     return 0;
0052 }
0053 
0054 static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
0055     [TCA_CGROUP_EMATCHES]   = { .type = NLA_NESTED },
0056 };
0057 
0058 static void __cls_cgroup_destroy(struct cls_cgroup_head *head)
0059 {
0060     tcf_exts_destroy(&head->exts);
0061     tcf_em_tree_destroy(&head->ematches);
0062     tcf_exts_put_net(&head->exts);
0063     kfree(head);
0064 }
0065 
0066 static void cls_cgroup_destroy_work(struct work_struct *work)
0067 {
0068     struct cls_cgroup_head *head = container_of(to_rcu_work(work),
0069                             struct cls_cgroup_head,
0070                             rwork);
0071     rtnl_lock();
0072     __cls_cgroup_destroy(head);
0073     rtnl_unlock();
0074 }
0075 
0076 static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb,
0077                  struct tcf_proto *tp, unsigned long base,
0078                  u32 handle, struct nlattr **tca,
0079                  void **arg, u32 flags,
0080                  struct netlink_ext_ack *extack)
0081 {
0082     struct nlattr *tb[TCA_CGROUP_MAX + 1];
0083     struct cls_cgroup_head *head = rtnl_dereference(tp->root);
0084     struct cls_cgroup_head *new;
0085     int err;
0086 
0087     if (!tca[TCA_OPTIONS])
0088         return -EINVAL;
0089 
0090     if (!head && !handle)
0091         return -EINVAL;
0092 
0093     if (head && handle != head->handle)
0094         return -ENOENT;
0095 
0096     new = kzalloc(sizeof(*head), GFP_KERNEL);
0097     if (!new)
0098         return -ENOBUFS;
0099 
0100     err = tcf_exts_init(&new->exts, net, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
0101     if (err < 0)
0102         goto errout;
0103     new->handle = handle;
0104     new->tp = tp;
0105     err = nla_parse_nested_deprecated(tb, TCA_CGROUP_MAX,
0106                       tca[TCA_OPTIONS], cgroup_policy,
0107                       NULL);
0108     if (err < 0)
0109         goto errout;
0110 
0111     err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &new->exts, flags,
0112                 extack);
0113     if (err < 0)
0114         goto errout;
0115 
0116     err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &new->ematches);
0117     if (err < 0)
0118         goto errout;
0119 
0120     rcu_assign_pointer(tp->root, new);
0121     if (head) {
0122         tcf_exts_get_net(&head->exts);
0123         tcf_queue_work(&head->rwork, cls_cgroup_destroy_work);
0124     }
0125     return 0;
0126 errout:
0127     tcf_exts_destroy(&new->exts);
0128     kfree(new);
0129     return err;
0130 }
0131 
0132 static void cls_cgroup_destroy(struct tcf_proto *tp, bool rtnl_held,
0133                    struct netlink_ext_ack *extack)
0134 {
0135     struct cls_cgroup_head *head = rtnl_dereference(tp->root);
0136 
0137     /* Head can still be NULL due to cls_cgroup_init(). */
0138     if (head) {
0139         if (tcf_exts_get_net(&head->exts))
0140             tcf_queue_work(&head->rwork, cls_cgroup_destroy_work);
0141         else
0142             __cls_cgroup_destroy(head);
0143     }
0144 }
0145 
0146 static int cls_cgroup_delete(struct tcf_proto *tp, void *arg, bool *last,
0147                  bool rtnl_held, struct netlink_ext_ack *extack)
0148 {
0149     return -EOPNOTSUPP;
0150 }
0151 
0152 static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg,
0153                 bool rtnl_held)
0154 {
0155     struct cls_cgroup_head *head = rtnl_dereference(tp->root);
0156 
0157     if (arg->count < arg->skip)
0158         goto skip;
0159 
0160     if (!head)
0161         return;
0162     if (arg->fn(tp, head, arg) < 0) {
0163         arg->stop = 1;
0164         return;
0165     }
0166 skip:
0167     arg->count++;
0168 }
0169 
0170 static int cls_cgroup_dump(struct net *net, struct tcf_proto *tp, void *fh,
0171                struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
0172 {
0173     struct cls_cgroup_head *head = rtnl_dereference(tp->root);
0174     struct nlattr *nest;
0175 
0176     t->tcm_handle = head->handle;
0177 
0178     nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
0179     if (nest == NULL)
0180         goto nla_put_failure;
0181 
0182     if (tcf_exts_dump(skb, &head->exts) < 0 ||
0183         tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
0184         goto nla_put_failure;
0185 
0186     nla_nest_end(skb, nest);
0187 
0188     if (tcf_exts_dump_stats(skb, &head->exts) < 0)
0189         goto nla_put_failure;
0190 
0191     return skb->len;
0192 
0193 nla_put_failure:
0194     nla_nest_cancel(skb, nest);
0195     return -1;
0196 }
0197 
0198 static struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
0199     .kind       =   "cgroup",
0200     .init       =   cls_cgroup_init,
0201     .change     =   cls_cgroup_change,
0202     .classify   =   cls_cgroup_classify,
0203     .destroy    =   cls_cgroup_destroy,
0204     .get        =   cls_cgroup_get,
0205     .delete     =   cls_cgroup_delete,
0206     .walk       =   cls_cgroup_walk,
0207     .dump       =   cls_cgroup_dump,
0208     .owner      =   THIS_MODULE,
0209 };
0210 
0211 static int __init init_cgroup_cls(void)
0212 {
0213     return register_tcf_proto_ops(&cls_cgroup_ops);
0214 }
0215 
0216 static void __exit exit_cgroup_cls(void)
0217 {
0218     unregister_tcf_proto_ops(&cls_cgroup_ops);
0219 }
0220 
0221 module_init(init_cgroup_cls);
0222 module_exit(exit_cgroup_cls);
0223 MODULE_LICENSE("GPL");