^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * net/sched/cls_cgroup.c Control Group Classifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Authors: Thomas Graf <tgraf@suug.ch>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <net/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <net/pkt_cls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <net/cls_cgroup.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct cls_cgroup_head {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) u32 handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct tcf_exts exts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct tcf_ematch_tree ematches;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct tcf_proto *tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct rcu_work rwork;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct tcf_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct cls_cgroup_head *head = rcu_dereference_bh(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) u32 classid = task_get_classid(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (unlikely(!head))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (!classid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (!tcf_em_tree_match(skb, &head->ematches, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) res->classid = classid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) res->class = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return tcf_exts_exec(skb, &head->exts, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static void *cls_cgroup_get(struct tcf_proto *tp, u32 handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static int cls_cgroup_init(struct tcf_proto *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) [TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static void __cls_cgroup_destroy(struct cls_cgroup_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) tcf_exts_destroy(&head->exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) tcf_em_tree_destroy(&head->ematches);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) tcf_exts_put_net(&head->exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) kfree(head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static void cls_cgroup_destroy_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct cls_cgroup_head *head = container_of(to_rcu_work(work),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct cls_cgroup_head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) rwork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) __cls_cgroup_destroy(head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct tcf_proto *tp, unsigned long base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) u32 handle, struct nlattr **tca,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) void **arg, bool ovr, bool rtnl_held,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct nlattr *tb[TCA_CGROUP_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct cls_cgroup_head *head = rtnl_dereference(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct cls_cgroup_head *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (!tca[TCA_OPTIONS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (!head && !handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (head && handle != head->handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) new = kzalloc(sizeof(*head), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (!new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) err = tcf_exts_init(&new->exts, net, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) new->handle = handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) new->tp = tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) err = nla_parse_nested_deprecated(tb, TCA_CGROUP_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) tca[TCA_OPTIONS], cgroup_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &new->exts, ovr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) true, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &new->ematches);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) rcu_assign_pointer(tp->root, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) tcf_exts_get_net(&head->exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) tcf_queue_work(&head->rwork, cls_cgroup_destroy_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) tcf_exts_destroy(&new->exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) kfree(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static void cls_cgroup_destroy(struct tcf_proto *tp, bool rtnl_held,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct cls_cgroup_head *head = rtnl_dereference(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /* Head can still be NULL due to cls_cgroup_init(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (tcf_exts_get_net(&head->exts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) tcf_queue_work(&head->rwork, cls_cgroup_destroy_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) __cls_cgroup_destroy(head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static int cls_cgroup_delete(struct tcf_proto *tp, void *arg, bool *last,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) bool rtnl_held, struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) bool rtnl_held)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct cls_cgroup_head *head = rtnl_dereference(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (arg->count < arg->skip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) goto skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (!head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (arg->fn(tp, head, arg) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) arg->stop = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) skip:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) arg->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static int cls_cgroup_dump(struct net *net, struct tcf_proto *tp, void *fh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct cls_cgroup_head *head = rtnl_dereference(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct nlattr *nest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) t->tcm_handle = head->handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (nest == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (tcf_exts_dump(skb, &head->exts) < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) nla_nest_end(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (tcf_exts_dump_stats(skb, &head->exts) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) nla_nest_cancel(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) .kind = "cgroup",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) .init = cls_cgroup_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) .change = cls_cgroup_change,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) .classify = cls_cgroup_classify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) .destroy = cls_cgroup_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) .get = cls_cgroup_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) .delete = cls_cgroup_delete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) .walk = cls_cgroup_walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) .dump = cls_cgroup_dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static int __init init_cgroup_cls(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return register_tcf_proto_ops(&cls_cgroup_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static void __exit exit_cgroup_cls(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) unregister_tcf_proto_ops(&cls_cgroup_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) module_init(init_cgroup_cls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) module_exit(exit_cgroup_cls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) MODULE_LICENSE("GPL");