1
2
3
4
5
6
7
8
9
10
11
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/types.h>
15#include <linux/string.h>
16#include <linux/errno.h>
17#include <linux/skbuff.h>
18#include <linux/cgroup.h>
19#include <linux/rcupdate.h>
20#include <linux/fdtable.h>
21#include <net/rtnetlink.h>
22#include <net/pkt_cls.h>
23#include <net/sock.h>
24#include <net/cls_cgroup.h>
25
26static inline struct cgroup_cls_state *cgrp_cls_state(struct cgroup *cgrp)
27{
28 return container_of(cgroup_subsys_state(cgrp, net_cls_subsys_id),
29 struct cgroup_cls_state, css);
30}
31
32static inline struct cgroup_cls_state *task_cls_state(struct task_struct *p)
33{
34 return container_of(task_subsys_state(p, net_cls_subsys_id),
35 struct cgroup_cls_state, css);
36}
37
38static struct cgroup_subsys_state *cgrp_css_alloc(struct cgroup *cgrp)
39{
40 struct cgroup_cls_state *cs;
41
42 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
43 if (!cs)
44 return ERR_PTR(-ENOMEM);
45 return &cs->css;
46}
47
48static int cgrp_css_online(struct cgroup *cgrp)
49{
50 if (cgrp->parent)
51 cgrp_cls_state(cgrp)->classid =
52 cgrp_cls_state(cgrp->parent)->classid;
53 return 0;
54}
55
56static void cgrp_css_free(struct cgroup *cgrp)
57{
58 kfree(cgrp_cls_state(cgrp));
59}
60
61static int update_classid(const void *v, struct file *file, unsigned n)
62{
63 int err;
64 struct socket *sock = sock_from_file(file, &err);
65 if (sock)
66 sock->sk->sk_classid = (u32)(unsigned long)v;
67 return 0;
68}
69
70static void cgrp_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
71{
72 struct task_struct *p;
73 void *v;
74
75 cgroup_taskset_for_each(p, cgrp, tset) {
76 task_lock(p);
77 v = (void *)(unsigned long)task_cls_classid(p);
78 iterate_fd(p->files, 0, update_classid, v);
79 task_unlock(p);
80 }
81}
82
83static u64 read_classid(struct cgroup *cgrp, struct cftype *cft)
84{
85 return cgrp_cls_state(cgrp)->classid;
86}
87
88static int write_classid(struct cgroup *cgrp, struct cftype *cft, u64 value)
89{
90 cgrp_cls_state(cgrp)->classid = (u32) value;
91 return 0;
92}
93
94static struct cftype ss_files[] = {
95 {
96 .name = "classid",
97 .read_u64 = read_classid,
98 .write_u64 = write_classid,
99 },
100 { }
101};
102
103struct cgroup_subsys net_cls_subsys = {
104 .name = "net_cls",
105 .css_alloc = cgrp_css_alloc,
106 .css_online = cgrp_css_online,
107 .css_free = cgrp_css_free,
108 .attach = cgrp_attach,
109 .subsys_id = net_cls_subsys_id,
110 .base_cftypes = ss_files,
111 .module = THIS_MODULE,
112};
113
114struct cls_cgroup_head {
115 u32 handle;
116 struct tcf_exts exts;
117 struct tcf_ematch_tree ematches;
118 struct tcf_proto *tp;
119 struct rcu_head rcu;
120};
121
122static int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp,
123 struct tcf_result *res)
124{
125 struct cls_cgroup_head *head = rcu_dereference_bh(tp->root);
126 u32 classid;
127
128 classid = task_cls_state(current)->classid;
129
130
131
132
133
134
135
136
137
138
139
140 if (in_serving_softirq()) {
141
142 if (!skb->sk)
143 return -1;
144 classid = skb->sk->sk_classid;
145 }
146
147 if (!classid)
148 return -1;
149
150 if (!tcf_em_tree_match(skb, &head->ematches, NULL))
151 return -1;
152
153 res->classid = classid;
154 res->class = 0;
155 return tcf_exts_exec(skb, &head->exts, res);
156}
157
158static unsigned long cls_cgroup_get(struct tcf_proto *tp, u32 handle)
159{
160 return 0UL;
161}
162
163static int cls_cgroup_init(struct tcf_proto *tp)
164{
165 return 0;
166}
167
168static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
169 [TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED },
170};
171
172static void cls_cgroup_destroy_rcu(struct rcu_head *root)
173{
174 struct cls_cgroup_head *head = container_of(root,
175 struct cls_cgroup_head,
176 rcu);
177
178 tcf_exts_destroy(&head->exts);
179 tcf_em_tree_destroy(&head->ematches);
180 kfree(head);
181}
182
183static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb,
184 struct tcf_proto *tp, unsigned long base,
185 u32 handle, struct nlattr **tca,
186 unsigned long *arg, bool ovr)
187{
188 struct nlattr *tb[TCA_CGROUP_MAX + 1];
189 struct cls_cgroup_head *head = rtnl_dereference(tp->root);
190 struct cls_cgroup_head *new;
191 struct tcf_ematch_tree t;
192 struct tcf_exts e;
193 int err;
194
195 if (!tca[TCA_OPTIONS])
196 return -EINVAL;
197
198 if (!head && !handle)
199 return -EINVAL;
200
201 if (head && handle != head->handle)
202 return -ENOENT;
203
204 new = kzalloc(sizeof(*head), GFP_KERNEL);
205 if (!new)
206 return -ENOBUFS;
207
208 tcf_exts_init(&new->exts, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
209 new->handle = handle;
210 new->tp = tp;
211 err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS],
212 cgroup_policy);
213 if (err < 0)
214 goto errout;
215
216 tcf_exts_init(&e, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
217 err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &e, ovr);
218 if (err < 0)
219 goto errout;
220
221 err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &t);
222 if (err < 0) {
223 tcf_exts_destroy(&e);
224 goto errout;
225 }
226
227 tcf_exts_change(tp, &new->exts, &e);
228 tcf_em_tree_change(tp, &new->ematches, &t);
229
230 rcu_assign_pointer(tp->root, new);
231 if (head)
232 call_rcu(&head->rcu, cls_cgroup_destroy_rcu);
233 return 0;
234errout:
235 kfree(new);
236 return err;
237}
238
239static bool cls_cgroup_destroy(struct tcf_proto *tp, bool force)
240{
241 struct cls_cgroup_head *head = rtnl_dereference(tp->root);
242
243 if (!force)
244 return false;
245
246
247 if (head)
248 call_rcu(&head->rcu, cls_cgroup_destroy_rcu);
249 return true;
250}
251
252static int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg)
253{
254 return -EOPNOTSUPP;
255}
256
257static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg)
258{
259 struct cls_cgroup_head *head = rtnl_dereference(tp->root);
260
261 if (arg->count < arg->skip)
262 goto skip;
263
264 if (arg->fn(tp, (unsigned long) head, arg) < 0) {
265 arg->stop = 1;
266 return;
267 }
268skip:
269 arg->count++;
270}
271
272static int cls_cgroup_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
273 struct sk_buff *skb, struct tcmsg *t)
274{
275 struct cls_cgroup_head *head = rtnl_dereference(tp->root);
276 struct nlattr *nest;
277
278 t->tcm_handle = head->handle;
279
280 nest = nla_nest_start(skb, TCA_OPTIONS);
281 if (nest == NULL)
282 goto nla_put_failure;
283
284 if (tcf_exts_dump(skb, &head->exts) < 0 ||
285 tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
286 goto nla_put_failure;
287
288 nla_nest_end(skb, nest);
289
290 if (tcf_exts_dump_stats(skb, &head->exts) < 0)
291 goto nla_put_failure;
292
293 return skb->len;
294
295nla_put_failure:
296 nla_nest_cancel(skb, nest);
297 return -1;
298}
299
300static struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
301 .kind = "cgroup",
302 .init = cls_cgroup_init,
303 .change = cls_cgroup_change,
304 .classify = cls_cgroup_classify,
305 .destroy = cls_cgroup_destroy,
306 .get = cls_cgroup_get,
307 .delete = cls_cgroup_delete,
308 .walk = cls_cgroup_walk,
309 .dump = cls_cgroup_dump,
310 .owner = THIS_MODULE,
311};
312
313static int __init init_cgroup_cls(void)
314{
315 int ret;
316
317 ret = cgroup_load_subsys(&net_cls_subsys);
318 if (ret)
319 goto out;
320
321 ret = register_tcf_proto_ops(&cls_cgroup_ops);
322 if (ret)
323 cgroup_unload_subsys(&net_cls_subsys);
324
325out:
326 return ret;
327}
328
329static void __exit exit_cgroup_cls(void)
330{
331 unregister_tcf_proto_ops(&cls_cgroup_ops);
332
333 cgroup_unload_subsys(&net_cls_subsys);
334}
335
336module_init(init_cgroup_cls);
337module_exit(exit_cgroup_cls);
338MODULE_LICENSE("GPL");
339