cls_cgroup.c revision f845172531fb7410c7fb7780b1a6e51ee6df7d52
1f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf/*
2f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf * net/sched/cls_cgroup.c	Control Group Classifier
3f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf *
4f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf *		This program is free software; you can redistribute it and/or
5f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf *		modify it under the terms of the GNU General Public License
6f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf *		as published by the Free Software Foundation; either version
7f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf *		2 of the License, or (at your option) any later version.
8f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf *
9f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf * Authors:	Thomas Graf <tgraf@suug.ch>
10f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf */
11f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
12f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf#include <linux/module.h>
135a0e3ad6af8660be21ca98a971cd00f331318c05Tejun Heo#include <linux/slab.h>
14f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf#include <linux/types.h>
15f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf#include <linux/string.h>
16f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf#include <linux/errno.h>
17f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf#include <linux/skbuff.h>
18f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf#include <linux/cgroup.h>
19f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#include <linux/rcupdate.h>
20f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf#include <net/rtnetlink.h>
21f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf#include <net/pkt_cls.h>
22f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#include <net/sock.h>
23f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#include <net/cls_cgroup.h>
24f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
258e039d84b323c4503c4d56863faa47c783660826Ben Blumstatic struct cgroup_subsys_state *cgrp_create(struct cgroup_subsys *ss,
268e039d84b323c4503c4d56863faa47c783660826Ben Blum					       struct cgroup *cgrp);
278e039d84b323c4503c4d56863faa47c783660826Ben Blumstatic void cgrp_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp);
288e039d84b323c4503c4d56863faa47c783660826Ben Blumstatic int cgrp_populate(struct cgroup_subsys *ss, struct cgroup *cgrp);
298e039d84b323c4503c4d56863faa47c783660826Ben Blum
308e039d84b323c4503c4d56863faa47c783660826Ben Blumstruct cgroup_subsys net_cls_subsys = {
318e039d84b323c4503c4d56863faa47c783660826Ben Blum	.name		= "net_cls",
328e039d84b323c4503c4d56863faa47c783660826Ben Blum	.create		= cgrp_create,
338e039d84b323c4503c4d56863faa47c783660826Ben Blum	.destroy	= cgrp_destroy,
348e039d84b323c4503c4d56863faa47c783660826Ben Blum	.populate	= cgrp_populate,
358e039d84b323c4503c4d56863faa47c783660826Ben Blum#ifdef CONFIG_NET_CLS_CGROUP
368e039d84b323c4503c4d56863faa47c783660826Ben Blum	.subsys_id	= net_cls_subsys_id,
378e039d84b323c4503c4d56863faa47c783660826Ben Blum#else
388e039d84b323c4503c4d56863faa47c783660826Ben Blum#define net_cls_subsys_id net_cls_subsys.subsys_id
398e039d84b323c4503c4d56863faa47c783660826Ben Blum#endif
408e039d84b323c4503c4d56863faa47c783660826Ben Blum	.module		= THIS_MODULE,
418e039d84b323c4503c4d56863faa47c783660826Ben Blum};
428e039d84b323c4503c4d56863faa47c783660826Ben Blum
438e039d84b323c4503c4d56863faa47c783660826Ben Blum
448e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefanstatic inline struct cgroup_cls_state *cgrp_cls_state(struct cgroup *cgrp)
45f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
468e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan	return container_of(cgroup_subsys_state(cgrp, net_cls_subsys_id),
478e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan			    struct cgroup_cls_state, css);
488e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan}
498e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan
508e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefanstatic inline struct cgroup_cls_state *task_cls_state(struct task_struct *p)
518e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan{
528e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan	return container_of(task_subsys_state(p, net_cls_subsys_id),
538e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan			    struct cgroup_cls_state, css);
54f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
55f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
56f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic struct cgroup_subsys_state *cgrp_create(struct cgroup_subsys *ss,
57f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf						 struct cgroup *cgrp)
58f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
59f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cgroup_cls_state *cs;
60f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
61f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (!(cs = kzalloc(sizeof(*cs), GFP_KERNEL)))
62f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return ERR_PTR(-ENOMEM);
63f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
64f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (cgrp->parent)
658e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan		cs->classid = cgrp_cls_state(cgrp->parent)->classid;
66f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
67f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return &cs->css;
68f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
69f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
70f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic void cgrp_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
71f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
728e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan	kfree(cgrp_cls_state(cgrp));
73f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
74f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
75f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic u64 read_classid(struct cgroup *cgrp, struct cftype *cft)
76f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
778e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan	return cgrp_cls_state(cgrp)->classid;
78f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
79f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
80f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int write_classid(struct cgroup *cgrp, struct cftype *cft, u64 value)
81f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
828e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan	cgrp_cls_state(cgrp)->classid = (u32) value;
83f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return 0;
84f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
85f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
86f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic struct cftype ss_files[] = {
87f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	{
88f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		.name = "classid",
89f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		.read_u64 = read_classid,
90f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		.write_u64 = write_classid,
91f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	},
92f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
93f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
94f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int cgrp_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
95f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
96f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return cgroup_add_files(cgrp, ss, ss_files, ARRAY_SIZE(ss_files));
97f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
98f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
99f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstruct cls_cgroup_head
100f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
101f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	u32			handle;
102f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct tcf_exts		exts;
103f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct tcf_ematch_tree	ematches;
104f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
105f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
106f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int cls_cgroup_classify(struct sk_buff *skb, struct tcf_proto *tp,
107f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			       struct tcf_result *res)
108f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
109f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cls_cgroup_head *head = tp->root;
110e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	u32 classid;
111f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
112f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	rcu_read_lock();
113f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	classid = task_cls_state(current)->classid;
114f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	rcu_read_unlock();
115f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
116f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	/*
117f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * Due to the nature of the classifier it is required to ignore all
118f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * packets originating from softirq context as accessing `current'
119f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * would lead to false results.
120f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 *
121f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * This test assumes that all callers of dev_queue_xmit() explicitely
122f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * disable bh. Knowing this, it is possible to detect softirq based
123f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * calls by looking at the number of nested bh disable calls because
124f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * softirqs always disables bh.
125f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 */
126f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	if (softirq_count() != SOFTIRQ_OFFSET) {
127f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		/* If there is an sk_classid we'll use that. */
128f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		if (!skb->sk)
129f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu			return -1;
130f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		classid = skb->sk->sk_classid;
131f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	}
132f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
133e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	if (!classid)
134e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage		return -1;
135e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage
136e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	if (!tcf_em_tree_match(skb, &head->ematches, NULL))
137e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage		return -1;
138e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage
139e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	res->classid = classid;
140e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	res->class = 0;
141e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	return tcf_exts_exec(skb, &head->exts, res);
142f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
143f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
144f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic unsigned long cls_cgroup_get(struct tcf_proto *tp, u32 handle)
145f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
146f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return 0UL;
147f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
148f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
149f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic void cls_cgroup_put(struct tcf_proto *tp, unsigned long f)
150f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
151f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
152f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
153f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int cls_cgroup_init(struct tcf_proto *tp)
154f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
155f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return 0;
156f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
157f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
158f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic const struct tcf_ext_map cgroup_ext_map = {
159f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.action = TCA_CGROUP_ACT,
160f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.police = TCA_CGROUP_POLICE,
161f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
162f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
163f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
164f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	[TCA_CGROUP_EMATCHES]	= { .type = NLA_NESTED },
165f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
166f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
167f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int cls_cgroup_change(struct tcf_proto *tp, unsigned long base,
168f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			     u32 handle, struct nlattr **tca,
169f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			     unsigned long *arg)
170f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
171f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct nlattr *tb[TCA_CGROUP_MAX+1];
172f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cls_cgroup_head *head = tp->root;
173f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct tcf_ematch_tree t;
174f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct tcf_exts e;
175f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	int err;
176f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
17752ea3a56a3268bc2a5a7c75e98c81463004e38efMinoru Usui	if (!tca[TCA_OPTIONS])
17852ea3a56a3268bc2a5a7c75e98c81463004e38efMinoru Usui		return -EINVAL;
17952ea3a56a3268bc2a5a7c75e98c81463004e38efMinoru Usui
180f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (head == NULL) {
181f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		if (!handle)
182f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			return -EINVAL;
183f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
184f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		head = kzalloc(sizeof(*head), GFP_KERNEL);
185f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		if (head == NULL)
186f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			return -ENOBUFS;
187f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
188f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		head->handle = handle;
189f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
190f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tcf_tree_lock(tp);
191f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tp->root = head;
192f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tcf_tree_unlock(tp);
193f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	}
194f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
195f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (handle != head->handle)
196f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return -ENOENT;
197f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
198f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS],
199f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			       cgroup_policy);
200f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (err < 0)
201f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return err;
202f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
203f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	err = tcf_exts_validate(tp, tb, tca[TCA_RATE], &e, &cgroup_ext_map);
204f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (err < 0)
205f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return err;
206f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
207f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &t);
208f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (err < 0)
209f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return err;
210f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
211f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	tcf_exts_change(tp, &head->exts, &e);
212f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	tcf_em_tree_change(tp, &head->ematches, &t);
213f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
214f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return 0;
215f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
216f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
217f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic void cls_cgroup_destroy(struct tcf_proto *tp)
218f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
21947a1a1d4be2910b13a8e90f75c17e253c39531ffPatrick McHardy	struct cls_cgroup_head *head = tp->root;
220f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
221f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (head) {
222f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tcf_exts_destroy(tp, &head->exts);
223f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tcf_em_tree_destroy(tp, &head->ematches);
224f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		kfree(head);
225f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	}
226f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
227f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
228f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg)
229f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
230f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return -EOPNOTSUPP;
231f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
232f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
233f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg)
234f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
235f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cls_cgroup_head *head = tp->root;
236f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
237f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (arg->count < arg->skip)
238f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		goto skip;
239f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
240f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (arg->fn(tp, (unsigned long) head, arg) < 0) {
241f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		arg->stop = 1;
242f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return;
243f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	}
244f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafskip:
245f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	arg->count++;
246f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
247f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
248f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int cls_cgroup_dump(struct tcf_proto *tp, unsigned long fh,
249f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			   struct sk_buff *skb, struct tcmsg *t)
250f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
251f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cls_cgroup_head *head = tp->root;
252f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	unsigned char *b = skb_tail_pointer(skb);
253f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct nlattr *nest;
254f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
255f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	t->tcm_handle = head->handle;
256f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
257f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	nest = nla_nest_start(skb, TCA_OPTIONS);
258f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (nest == NULL)
259f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		goto nla_put_failure;
260f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
261f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (tcf_exts_dump(skb, &head->exts, &cgroup_ext_map) < 0 ||
262f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	    tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
263f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		goto nla_put_failure;
264f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
265f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	nla_nest_end(skb, nest);
266f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
267f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (tcf_exts_dump_stats(skb, &head->exts, &cgroup_ext_map) < 0)
268f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		goto nla_put_failure;
269f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
270f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return skb->len;
271f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
272f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafnla_put_failure:
273f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	nlmsg_trim(skb, b);
274f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return -1;
275f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
276f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
277f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
278f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.kind		=	"cgroup",
279f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.init		=	cls_cgroup_init,
280f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.change		=	cls_cgroup_change,
281f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.classify	=	cls_cgroup_classify,
282f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.destroy	=	cls_cgroup_destroy,
283f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.get		=	cls_cgroup_get,
284f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.put		=	cls_cgroup_put,
285f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.delete		=	cls_cgroup_delete,
286f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.walk		=	cls_cgroup_walk,
287f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.dump		=	cls_cgroup_dump,
288f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.owner		=	THIS_MODULE,
289f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
290f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
291f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int __init init_cgroup_cls(void)
292f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
293f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	int ret;
294f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
2958e039d84b323c4503c4d56863faa47c783660826Ben Blum	ret = cgroup_load_subsys(&net_cls_subsys);
2968e039d84b323c4503c4d56863faa47c783660826Ben Blum	if (ret)
297f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		goto out;
298f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
299f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#ifndef CONFIG_NET_CLS_CGROUP
300f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	/* We can't use rcu_assign_pointer because this is an int. */
301f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	smp_wmb();
302f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	net_cls_subsys_id = net_cls_subsys.subsys_id;
303f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#endif
304f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
305f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	ret = register_tcf_proto_ops(&cls_cgroup_ops);
306f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	if (ret)
307f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		cgroup_unload_subsys(&net_cls_subsys);
308f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
309f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xuout:
3108e039d84b323c4503c4d56863faa47c783660826Ben Blum	return ret;
311f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
312f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
313f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic void __exit exit_cgroup_cls(void)
314f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
315f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	unregister_tcf_proto_ops(&cls_cgroup_ops);
316f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
317f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#ifndef CONFIG_NET_CLS_CGROUP
318f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	net_cls_subsys_id = -1;
319f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	synchronize_rcu();
320f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#endif
321f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
3228e039d84b323c4503c4d56863faa47c783660826Ben Blum	cgroup_unload_subsys(&net_cls_subsys);
323f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
324f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
325f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafmodule_init(init_cgroup_cls);
326f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafmodule_exit(exit_cgroup_cls);
327f400923735ecbb67cbe4a3606c9479f694754f51Thomas GrafMODULE_LICENSE("GPL");
328