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