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>
206a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagner#include <linux/fdtable.h>
21f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf#include <net/rtnetlink.h>
22f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf#include <net/pkt_cls.h>
23f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#include <net/sock.h>
24f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#include <net/cls_cgroup.h>
25f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
268e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefanstatic inline struct cgroup_cls_state *cgrp_cls_state(struct cgroup *cgrp)
27f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
288e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan	return container_of(cgroup_subsys_state(cgrp, net_cls_subsys_id),
298e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan			    struct cgroup_cls_state, css);
308e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan}
318e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan
328e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefanstatic inline struct cgroup_cls_state *task_cls_state(struct task_struct *p)
338e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan{
348e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan	return container_of(task_subsys_state(p, net_cls_subsys_id),
358e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan			    struct cgroup_cls_state, css);
36f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
37f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
3892fb97487a7e41b222c1417cabd1d1ab7cc3a48cTejun Heostatic struct cgroup_subsys_state *cgrp_css_alloc(struct cgroup *cgrp)
39f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
40f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cgroup_cls_state *cs;
41f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
42cc7ec456f82da7f89a5b376e613b3ac4311b3e9aEric Dumazet	cs = kzalloc(sizeof(*cs), GFP_KERNEL);
43cc7ec456f82da7f89a5b376e613b3ac4311b3e9aEric Dumazet	if (!cs)
44f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return ERR_PTR(-ENOMEM);
450ba18f7a5e268e095f79e32b7b47e8ce4fbabbe2Tejun Heo	return &cs->css;
460ba18f7a5e268e095f79e32b7b47e8ce4fbabbe2Tejun Heo}
47f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
480ba18f7a5e268e095f79e32b7b47e8ce4fbabbe2Tejun Heostatic int cgrp_css_online(struct cgroup *cgrp)
490ba18f7a5e268e095f79e32b7b47e8ce4fbabbe2Tejun Heo{
50f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (cgrp->parent)
510ba18f7a5e268e095f79e32b7b47e8ce4fbabbe2Tejun Heo		cgrp_cls_state(cgrp)->classid =
520ba18f7a5e268e095f79e32b7b47e8ce4fbabbe2Tejun Heo			cgrp_cls_state(cgrp->parent)->classid;
530ba18f7a5e268e095f79e32b7b47e8ce4fbabbe2Tejun Heo	return 0;
54f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
55f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
5692fb97487a7e41b222c1417cabd1d1ab7cc3a48cTejun Heostatic void cgrp_css_free(struct cgroup *cgrp)
57f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
588e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan	kfree(cgrp_cls_state(cgrp));
59f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
60f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
616a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagnerstatic int update_classid(const void *v, struct file *file, unsigned n)
626a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagner{
636a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagner	int err;
646a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagner	struct socket *sock = sock_from_file(file, &err);
656a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagner	if (sock)
666a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagner		sock->sk->sk_classid = (u32)(unsigned long)v;
676a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagner	return 0;
686a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagner}
696a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagner
706a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagnerstatic void cgrp_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
716a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagner{
726a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagner	struct task_struct *p;
736a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagner	void *v;
746a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagner
756a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagner	cgroup_taskset_for_each(p, cgrp, tset) {
766a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagner		task_lock(p);
776a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagner		v = (void *)(unsigned long)task_cls_classid(p);
786a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagner		iterate_fd(p->files, 0, update_classid, v);
796a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagner		task_unlock(p);
806a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagner	}
816a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagner}
826a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagner
83f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic u64 read_classid(struct cgroup *cgrp, struct cftype *cft)
84f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
858e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan	return cgrp_cls_state(cgrp)->classid;
86f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
87f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
88f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int write_classid(struct cgroup *cgrp, struct cftype *cft, u64 value)
89f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
908e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan	cgrp_cls_state(cgrp)->classid = (u32) value;
91f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return 0;
92f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
93f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
94f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic struct cftype ss_files[] = {
95f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	{
96f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		.name = "classid",
97f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		.read_u64 = read_classid,
98f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		.write_u64 = write_classid,
99f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	},
1004baf6e33251b37f111e21289f8ee71fe4cce236eTejun Heo	{ }	/* terminate */
101f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
102f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
103676f7c8f84d15e94065841529016da5ab92e901bTejun Heostruct cgroup_subsys net_cls_subsys = {
104676f7c8f84d15e94065841529016da5ab92e901bTejun Heo	.name		= "net_cls",
10592fb97487a7e41b222c1417cabd1d1ab7cc3a48cTejun Heo	.css_alloc	= cgrp_css_alloc,
1060ba18f7a5e268e095f79e32b7b47e8ce4fbabbe2Tejun Heo	.css_online	= cgrp_css_online,
10792fb97487a7e41b222c1417cabd1d1ab7cc3a48cTejun Heo	.css_free	= cgrp_css_free,
1086a328d8c6f03501657ad580f6f98bf9a42583ff7Daniel Wagner	.attach		= cgrp_attach,
109676f7c8f84d15e94065841529016da5ab92e901bTejun Heo	.subsys_id	= net_cls_subsys_id,
1104baf6e33251b37f111e21289f8ee71fe4cce236eTejun Heo	.base_cftypes	= ss_files,
111676f7c8f84d15e94065841529016da5ab92e901bTejun Heo	.module		= THIS_MODULE,
112676f7c8f84d15e94065841529016da5ab92e901bTejun Heo};
113676f7c8f84d15e94065841529016da5ab92e901bTejun Heo
114cc7ec456f82da7f89a5b376e613b3ac4311b3e9aEric Dumazetstruct cls_cgroup_head {
115f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	u32			handle;
116f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct tcf_exts		exts;
117f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct tcf_ematch_tree	ematches;
118f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
119f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
120dc7f9f6e8838556f226c2ebd1da7bb305cb25654Eric Dumazetstatic int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp,
121f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			       struct tcf_result *res)
122f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
123f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cls_cgroup_head *head = tp->root;
124e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	u32 classid;
125f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
126f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	rcu_read_lock();
127f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	classid = task_cls_state(current)->classid;
128f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	rcu_read_unlock();
129f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
130f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	/*
131f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * Due to the nature of the classifier it is required to ignore all
132f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * packets originating from softirq context as accessing `current'
133f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * would lead to false results.
134f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 *
135f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * This test assumes that all callers of dev_queue_xmit() explicitely
136f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * disable bh. Knowing this, it is possible to detect softirq based
137f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * calls by looking at the number of nested bh disable calls because
138f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * softirqs always disables bh.
139f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 */
14075e1056f5c57050415b64cb761a3acc35d91f013Venkatesh Pallipadi	if (in_serving_softirq()) {
141f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		/* If there is an sk_classid we'll use that. */
142f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		if (!skb->sk)
143f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu			return -1;
144f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		classid = skb->sk->sk_classid;
145f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	}
146f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
147e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	if (!classid)
148e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage		return -1;
149e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage
150e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	if (!tcf_em_tree_match(skb, &head->ematches, NULL))
151e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage		return -1;
152e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage
153e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	res->classid = classid;
154e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	res->class = 0;
155e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	return tcf_exts_exec(skb, &head->exts, res);
156f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
157f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
158f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic unsigned long cls_cgroup_get(struct tcf_proto *tp, u32 handle)
159f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
160f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return 0UL;
161f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
162f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
163f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic void cls_cgroup_put(struct tcf_proto *tp, unsigned long f)
164f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
165f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
166f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
167f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int cls_cgroup_init(struct tcf_proto *tp)
168f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
169f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return 0;
170f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
171f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
172f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic const struct tcf_ext_map cgroup_ext_map = {
173f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.action = TCA_CGROUP_ACT,
174f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.police = TCA_CGROUP_POLICE,
175f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
176f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
177f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
178f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	[TCA_CGROUP_EMATCHES]	= { .type = NLA_NESTED },
179f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
180f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
181c1b52739e45f5969b208ebc377f52468280af11eBenjamin LaHaisestatic int cls_cgroup_change(struct net *net, struct sk_buff *in_skb,
182af4c6641f5ad445fe6d0832da42406dbd9a37ce4Eric W. Biederman			     struct tcf_proto *tp, unsigned long base,
183f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			     u32 handle, struct nlattr **tca,
184f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			     unsigned long *arg)
185f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
186cc7ec456f82da7f89a5b376e613b3ac4311b3e9aEric Dumazet	struct nlattr *tb[TCA_CGROUP_MAX + 1];
187f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cls_cgroup_head *head = tp->root;
188f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct tcf_ematch_tree t;
189f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct tcf_exts e;
190f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	int err;
191f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
19252ea3a56a3268bc2a5a7c75e98c81463004e38efMinoru Usui	if (!tca[TCA_OPTIONS])
19352ea3a56a3268bc2a5a7c75e98c81463004e38efMinoru Usui		return -EINVAL;
19452ea3a56a3268bc2a5a7c75e98c81463004e38efMinoru Usui
195f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (head == NULL) {
196f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		if (!handle)
197f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			return -EINVAL;
198f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
199f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		head = kzalloc(sizeof(*head), GFP_KERNEL);
200f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		if (head == NULL)
201f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			return -ENOBUFS;
202f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
203f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		head->handle = handle;
204f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
205f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tcf_tree_lock(tp);
206f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tp->root = head;
207f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tcf_tree_unlock(tp);
208f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	}
209f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
210f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (handle != head->handle)
211f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return -ENOENT;
212f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
213f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS],
214f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			       cgroup_policy);
215f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (err < 0)
216f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return err;
217f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
218c1b52739e45f5969b208ebc377f52468280af11eBenjamin LaHaise	err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &e,
219c1b52739e45f5969b208ebc377f52468280af11eBenjamin LaHaise				&cgroup_ext_map);
220f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (err < 0)
221f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return err;
222f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
223f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &t);
224f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (err < 0)
225f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return err;
226f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
227f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	tcf_exts_change(tp, &head->exts, &e);
228f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	tcf_em_tree_change(tp, &head->ematches, &t);
229f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
230f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return 0;
231f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
232f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
233f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic void cls_cgroup_destroy(struct tcf_proto *tp)
234f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
23547a1a1d4be2910b13a8e90f75c17e253c39531ffPatrick McHardy	struct cls_cgroup_head *head = tp->root;
236f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
237f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (head) {
238f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tcf_exts_destroy(tp, &head->exts);
239f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tcf_em_tree_destroy(tp, &head->ematches);
240f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		kfree(head);
241f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	}
242f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
243f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
244f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg)
245f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
246f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return -EOPNOTSUPP;
247f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
248f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
249f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg)
250f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
251f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cls_cgroup_head *head = tp->root;
252f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
253f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (arg->count < arg->skip)
254f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		goto skip;
255f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
256f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (arg->fn(tp, (unsigned long) head, arg) < 0) {
257f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		arg->stop = 1;
258f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return;
259f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	}
260f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafskip:
261f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	arg->count++;
262f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
263f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
264f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int cls_cgroup_dump(struct tcf_proto *tp, unsigned long fh,
265f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			   struct sk_buff *skb, struct tcmsg *t)
266f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
267f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cls_cgroup_head *head = tp->root;
268f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	unsigned char *b = skb_tail_pointer(skb);
269f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct nlattr *nest;
270f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
271f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	t->tcm_handle = head->handle;
272f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
273f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	nest = nla_nest_start(skb, TCA_OPTIONS);
274f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (nest == NULL)
275f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		goto nla_put_failure;
276f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
277f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (tcf_exts_dump(skb, &head->exts, &cgroup_ext_map) < 0 ||
278f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	    tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
279f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		goto nla_put_failure;
280f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
281f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	nla_nest_end(skb, nest);
282f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
283f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (tcf_exts_dump_stats(skb, &head->exts, &cgroup_ext_map) < 0)
284f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		goto nla_put_failure;
285f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
286f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return skb->len;
287f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
288f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafnla_put_failure:
289f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	nlmsg_trim(skb, b);
290f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return -1;
291f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
292f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
293f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
294f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.kind		=	"cgroup",
295f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.init		=	cls_cgroup_init,
296f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.change		=	cls_cgroup_change,
297f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.classify	=	cls_cgroup_classify,
298f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.destroy	=	cls_cgroup_destroy,
299f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.get		=	cls_cgroup_get,
300f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.put		=	cls_cgroup_put,
301f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.delete		=	cls_cgroup_delete,
302f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.walk		=	cls_cgroup_walk,
303f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.dump		=	cls_cgroup_dump,
304f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.owner		=	THIS_MODULE,
305f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
306f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
307f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int __init init_cgroup_cls(void)
308f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
309f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	int ret;
310f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
3118e039d84b323c4503c4d56863faa47c783660826Ben Blum	ret = cgroup_load_subsys(&net_cls_subsys);
3128e039d84b323c4503c4d56863faa47c783660826Ben Blum	if (ret)
313f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		goto out;
314f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
315f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	ret = register_tcf_proto_ops(&cls_cgroup_ops);
316f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	if (ret)
317f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		cgroup_unload_subsys(&net_cls_subsys);
318f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
319f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xuout:
3208e039d84b323c4503c4d56863faa47c783660826Ben Blum	return ret;
321f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
322f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
323f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic void __exit exit_cgroup_cls(void)
324f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
325f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	unregister_tcf_proto_ops(&cls_cgroup_ops);
326f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
3278e039d84b323c4503c4d56863faa47c783660826Ben Blum	cgroup_unload_subsys(&net_cls_subsys);
328f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
329f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
330f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafmodule_init(init_cgroup_cls);
331f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafmodule_exit(exit_cgroup_cls);
332f400923735ecbb67cbe4a3606c9479f694754f51Thomas GrafMODULE_LICENSE("GPL");
333