cls_basic.c 6.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * net/sched/cls_basic.c	Basic Packet Classifier.
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 *
 * Authors:	Thomas Graf <tgraf@suug.ch>
 */

#include <linux/module.h>
13
#include <linux/slab.h>
L
Linus Torvalds 已提交
14 15 16 17 18 19
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/rtnetlink.h>
#include <linux/skbuff.h>
20
#include <net/netlink.h>
L
Linus Torvalds 已提交
21 22 23
#include <net/act_api.h>
#include <net/pkt_cls.h>

E
Eric Dumazet 已提交
24
struct basic_head {
L
Linus Torvalds 已提交
25 26
	u32			hgenerator;
	struct list_head	flist;
J
John Fastabend 已提交
27
	struct rcu_head		rcu;
L
Linus Torvalds 已提交
28 29
};

E
Eric Dumazet 已提交
30
struct basic_filter {
L
Linus Torvalds 已提交
31 32 33 34
	u32			handle;
	struct tcf_exts		exts;
	struct tcf_ematch_tree	ematches;
	struct tcf_result	res;
J
John Fastabend 已提交
35
	struct tcf_proto	*tp;
L
Linus Torvalds 已提交
36
	struct list_head	link;
J
John Fastabend 已提交
37
	struct rcu_head		rcu;
L
Linus Torvalds 已提交
38 39
};

40
static int basic_classify(struct sk_buff *skb, const struct tcf_proto *tp,
L
Linus Torvalds 已提交
41 42 43
			  struct tcf_result *res)
{
	int r;
J
John Fastabend 已提交
44
	struct basic_head *head = rcu_dereference_bh(tp->root);
L
Linus Torvalds 已提交
45 46
	struct basic_filter *f;

J
John Fastabend 已提交
47
	list_for_each_entry_rcu(f, &head->flist, link) {
L
Linus Torvalds 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61
		if (!tcf_em_tree_match(skb, &f->ematches, NULL))
			continue;
		*res = f->res;
		r = tcf_exts_exec(skb, &f->exts, res);
		if (r < 0)
			continue;
		return r;
	}
	return -1;
}

static unsigned long basic_get(struct tcf_proto *tp, u32 handle)
{
	unsigned long l = 0UL;
J
John Fastabend 已提交
62
	struct basic_head *head = rtnl_dereference(tp->root);
L
Linus Torvalds 已提交
63 64
	struct basic_filter *f;

65 66
	list_for_each_entry(f, &head->flist, link) {
		if (f->handle == handle) {
L
Linus Torvalds 已提交
67
			l = (unsigned long) f;
68 69 70
			break;
		}
	}
L
Linus Torvalds 已提交
71 72 73 74 75 76

	return l;
}

static int basic_init(struct tcf_proto *tp)
{
77 78 79 80 81 82
	struct basic_head *head;

	head = kzalloc(sizeof(*head), GFP_KERNEL);
	if (head == NULL)
		return -ENOBUFS;
	INIT_LIST_HEAD(&head->flist);
J
John Fastabend 已提交
83
	rcu_assign_pointer(tp->root, head);
L
Linus Torvalds 已提交
84 85 86
	return 0;
}

J
John Fastabend 已提交
87
static void basic_delete_filter(struct rcu_head *head)
L
Linus Torvalds 已提交
88
{
J
John Fastabend 已提交
89 90
	struct basic_filter *f = container_of(head, struct basic_filter, rcu);

91
	tcf_exts_destroy(&f->exts);
92
	tcf_em_tree_destroy(&f->ematches);
L
Linus Torvalds 已提交
93 94 95
	kfree(f);
}

96
static void basic_destroy(struct tcf_proto *tp)
L
Linus Torvalds 已提交
97
{
J
John Fastabend 已提交
98
	struct basic_head *head = rtnl_dereference(tp->root);
L
Linus Torvalds 已提交
99
	struct basic_filter *f, *n;
100

L
Linus Torvalds 已提交
101
	list_for_each_entry_safe(f, n, &head->flist, link) {
J
John Fastabend 已提交
102
		list_del_rcu(&f->link);
103
		tcf_unbind_filter(tp, &f->res);
J
John Fastabend 已提交
104
		call_rcu(&f->rcu, basic_delete_filter);
L
Linus Torvalds 已提交
105
	}
J
John Fastabend 已提交
106
	kfree_rcu(head, rcu);
L
Linus Torvalds 已提交
107 108
}

109
static int basic_delete(struct tcf_proto *tp, unsigned long arg, bool *last)
L
Linus Torvalds 已提交
110
{
111
	struct basic_head *head = rtnl_dereference(tp->root);
112
	struct basic_filter *f = (struct basic_filter *) arg;
L
Linus Torvalds 已提交
113

114 115 116
	list_del_rcu(&f->link);
	tcf_unbind_filter(tp, &f->res);
	call_rcu(&f->rcu, basic_delete_filter);
117
	*last = list_empty(&head->flist);
118
	return 0;
L
Linus Torvalds 已提交
119 120
}

121 122 123 124 125
static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
	[TCA_BASIC_CLASSID]	= { .type = NLA_U32 },
	[TCA_BASIC_EMATCHES]	= { .type = NLA_NESTED },
};

126 127 128
static int basic_set_parms(struct net *net, struct tcf_proto *tp,
			   struct basic_filter *f, unsigned long base,
			   struct nlattr **tb,
129
			   struct nlattr *est, bool ovr)
L
Linus Torvalds 已提交
130
{
131
	int err;
L
Linus Torvalds 已提交
132 133 134
	struct tcf_exts e;
	struct tcf_ematch_tree t;

135
	err = tcf_exts_init(&e, TCA_BASIC_ACT, TCA_BASIC_POLICE);
L
Linus Torvalds 已提交
136 137
	if (err < 0)
		return err;
138 139 140
	err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
	if (err < 0)
		goto errout;
L
Linus Torvalds 已提交
141

142
	err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &t);
L
Linus Torvalds 已提交
143 144 145
	if (err < 0)
		goto errout;

146
	if (tb[TCA_BASIC_CLASSID]) {
147
		f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
L
Linus Torvalds 已提交
148 149 150 151 152
		tcf_bind_filter(tp, &f->res, base);
	}

	tcf_exts_change(tp, &f->exts, &e);
	tcf_em_tree_change(tp, &f->ematches, &t);
J
John Fastabend 已提交
153
	f->tp = tp;
L
Linus Torvalds 已提交
154 155 156

	return 0;
errout:
157
	tcf_exts_destroy(&e);
L
Linus Torvalds 已提交
158 159 160
	return err;
}

161
static int basic_change(struct net *net, struct sk_buff *in_skb,
162
			struct tcf_proto *tp, unsigned long base, u32 handle,
163
			struct nlattr **tca, unsigned long *arg, bool ovr)
L
Linus Torvalds 已提交
164
{
165
	int err;
J
John Fastabend 已提交
166
	struct basic_head *head = rtnl_dereference(tp->root);
167
	struct nlattr *tb[TCA_BASIC_MAX + 1];
J
John Fastabend 已提交
168 169
	struct basic_filter *fold = (struct basic_filter *) *arg;
	struct basic_filter *fnew;
L
Linus Torvalds 已提交
170

171
	if (tca[TCA_OPTIONS] == NULL)
L
Linus Torvalds 已提交
172 173
		return -EINVAL;

174
	err = nla_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
175
			       basic_policy, NULL);
176 177
	if (err < 0)
		return err;
L
Linus Torvalds 已提交
178

J
John Fastabend 已提交
179 180
	if (fold != NULL) {
		if (handle && fold->handle != handle)
L
Linus Torvalds 已提交
181 182 183
			return -EINVAL;
	}

J
John Fastabend 已提交
184
	fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
185 186
	if (!fnew)
		return -ENOBUFS;
L
Linus Torvalds 已提交
187

188 189 190 191
	err = tcf_exts_init(&fnew->exts, TCA_BASIC_ACT, TCA_BASIC_POLICE);
	if (err < 0)
		goto errout;

L
Linus Torvalds 已提交
192
	err = -EINVAL;
J
John Fastabend 已提交
193 194 195 196 197
	if (handle) {
		fnew->handle = handle;
	} else if (fold) {
		fnew->handle = fold->handle;
	} else {
198
		unsigned int i = 0x80000000;
L
Linus Torvalds 已提交
199 200 201 202 203 204
		do {
			if (++head->hgenerator == 0x7FFFFFFF)
				head->hgenerator = 1;
		} while (--i > 0 && basic_get(tp, head->hgenerator));

		if (i <= 0) {
E
Eric Dumazet 已提交
205
			pr_err("Insufficient number of handles\n");
L
Linus Torvalds 已提交
206 207 208
			goto errout;
		}

J
John Fastabend 已提交
209
		fnew->handle = head->hgenerator;
L
Linus Torvalds 已提交
210 211
	}

J
John Fastabend 已提交
212
	err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr);
L
Linus Torvalds 已提交
213 214 215
	if (err < 0)
		goto errout;

J
John Fastabend 已提交
216 217 218 219
	*arg = (unsigned long)fnew;

	if (fold) {
		list_replace_rcu(&fold->link, &fnew->link);
220
		tcf_unbind_filter(tp, &fold->res);
J
John Fastabend 已提交
221 222 223 224
		call_rcu(&fold->rcu, basic_delete_filter);
	} else {
		list_add_rcu(&fnew->link, &head->flist);
	}
L
Linus Torvalds 已提交
225 226 227

	return 0;
errout:
228
	tcf_exts_destroy(&fnew->exts);
J
John Fastabend 已提交
229
	kfree(fnew);
L
Linus Torvalds 已提交
230 231 232 233 234
	return err;
}

static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg)
{
J
John Fastabend 已提交
235
	struct basic_head *head = rtnl_dereference(tp->root);
L
Linus Torvalds 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
	struct basic_filter *f;

	list_for_each_entry(f, &head->flist, link) {
		if (arg->count < arg->skip)
			goto skip;

		if (arg->fn(tp, (unsigned long) f, arg) < 0) {
			arg->stop = 1;
			break;
		}
skip:
		arg->count++;
	}
}

251
static int basic_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
L
Linus Torvalds 已提交
252 253 254
		      struct sk_buff *skb, struct tcmsg *t)
{
	struct basic_filter *f = (struct basic_filter *) fh;
255
	struct nlattr *nest;
L
Linus Torvalds 已提交
256 257 258 259 260 261

	if (f == NULL)
		return skb->len;

	t->tcm_handle = f->handle;

262 263 264
	nest = nla_nest_start(skb, TCA_OPTIONS);
	if (nest == NULL)
		goto nla_put_failure;
L
Linus Torvalds 已提交
265

266 267 268
	if (f->res.classid &&
	    nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
		goto nla_put_failure;
269

270
	if (tcf_exts_dump(skb, &f->exts) < 0 ||
L
Linus Torvalds 已提交
271
	    tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
272
		goto nla_put_failure;
L
Linus Torvalds 已提交
273

274
	nla_nest_end(skb, nest);
275

276
	if (tcf_exts_dump_stats(skb, &f->exts) < 0)
277 278
		goto nla_put_failure;

L
Linus Torvalds 已提交
279 280
	return skb->len;

281
nla_put_failure:
282
	nla_nest_cancel(skb, nest);
L
Linus Torvalds 已提交
283 284 285
	return -1;
}

286
static struct tcf_proto_ops cls_basic_ops __read_mostly = {
L
Linus Torvalds 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
	.kind		=	"basic",
	.classify	=	basic_classify,
	.init		=	basic_init,
	.destroy	=	basic_destroy,
	.get		=	basic_get,
	.change		=	basic_change,
	.delete		=	basic_delete,
	.walk		=	basic_walk,
	.dump		=	basic_dump,
	.owner		=	THIS_MODULE,
};

static int __init init_basic(void)
{
	return register_tcf_proto_ops(&cls_basic_ops);
}

304
static void __exit exit_basic(void)
L
Linus Torvalds 已提交
305 306 307 308 309 310 311 312
{
	unregister_tcf_proto_ops(&cls_basic_ops);
}

module_init(init_basic)
module_exit(exit_basic)
MODULE_LICENSE("GPL");