cls_basic.c 6.9 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;
37 38 39 40
	union {
		struct work_struct	work;
		struct rcu_head		rcu;
	};
L
Linus Torvalds 已提交
41 42
};

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

J
John Fastabend 已提交
50
	list_for_each_entry_rcu(f, &head->flist, link) {
L
Linus Torvalds 已提交
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;
}

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

67 68
	list_for_each_entry(f, &head->flist, link) {
		if (f->handle == handle) {
69
			return f;
70 71
		}
	}
L
Linus Torvalds 已提交
72

73
	return NULL;
L
Linus Torvalds 已提交
74 75 76 77
}

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

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

88
static void basic_delete_filter_work(struct work_struct *work)
L
Linus Torvalds 已提交
89
{
90
	struct basic_filter *f = container_of(work, struct basic_filter, work);
J
John Fastabend 已提交
91

92
	rtnl_lock();
93
	tcf_exts_destroy(&f->exts);
94
	tcf_em_tree_destroy(&f->ematches);
95 96
	rtnl_unlock();

L
Linus Torvalds 已提交
97 98 99
	kfree(f);
}

100 101 102 103 104 105 106 107
static void basic_delete_filter(struct rcu_head *head)
{
	struct basic_filter *f = container_of(head, struct basic_filter, rcu);

	INIT_WORK(&f->work, basic_delete_filter_work);
	tcf_queue_work(&f->work);
}

108
static void basic_destroy(struct tcf_proto *tp)
L
Linus Torvalds 已提交
109
{
J
John Fastabend 已提交
110
	struct basic_head *head = rtnl_dereference(tp->root);
L
Linus Torvalds 已提交
111
	struct basic_filter *f, *n;
112

L
Linus Torvalds 已提交
113
	list_for_each_entry_safe(f, n, &head->flist, link) {
J
John Fastabend 已提交
114
		list_del_rcu(&f->link);
115
		tcf_unbind_filter(tp, &f->res);
J
John Fastabend 已提交
116
		call_rcu(&f->rcu, basic_delete_filter);
L
Linus Torvalds 已提交
117
	}
J
John Fastabend 已提交
118
	kfree_rcu(head, rcu);
L
Linus Torvalds 已提交
119 120
}

121
static int basic_delete(struct tcf_proto *tp, void *arg, bool *last)
L
Linus Torvalds 已提交
122
{
123
	struct basic_head *head = rtnl_dereference(tp->root);
124
	struct basic_filter *f = arg;
L
Linus Torvalds 已提交
125

126 127 128
	list_del_rcu(&f->link);
	tcf_unbind_filter(tp, &f->res);
	call_rcu(&f->rcu, basic_delete_filter);
129
	*last = list_empty(&head->flist);
130
	return 0;
L
Linus Torvalds 已提交
131 132
}

133 134 135 136 137
static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
	[TCA_BASIC_CLASSID]	= { .type = NLA_U32 },
	[TCA_BASIC_EMATCHES]	= { .type = NLA_NESTED },
};

138 139 140
static int basic_set_parms(struct net *net, struct tcf_proto *tp,
			   struct basic_filter *f, unsigned long base,
			   struct nlattr **tb,
141
			   struct nlattr *est, bool ovr)
L
Linus Torvalds 已提交
142
{
143
	int err;
L
Linus Torvalds 已提交
144

145
	err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr);
L
Linus Torvalds 已提交
146 147 148
	if (err < 0)
		return err;

149
	err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &f->ematches);
L
Linus Torvalds 已提交
150
	if (err < 0)
151
		return err;
L
Linus Torvalds 已提交
152

153
	if (tb[TCA_BASIC_CLASSID]) {
154
		f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
L
Linus Torvalds 已提交
155 156 157
		tcf_bind_filter(tp, &f->res, base);
	}

J
John Fastabend 已提交
158
	f->tp = tp;
L
Linus Torvalds 已提交
159 160 161
	return 0;
}

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

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

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

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

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

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

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

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

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

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

217
	*arg = fnew;
J
John Fastabend 已提交
218 219 220

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

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

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

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

243
		if (arg->fn(tp, f, arg) < 0) {
L
Linus Torvalds 已提交
244 245 246 247 248 249 250 251
			arg->stop = 1;
			break;
		}
skip:
		arg->count++;
	}
}

252 253 254 255 256 257 258 259
static void basic_bind_class(void *fh, u32 classid, unsigned long cl)
{
	struct basic_filter *f = fh;

	if (f && f->res.classid == classid)
		f->res.class = cl;
}

260
static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh,
L
Linus Torvalds 已提交
261 262
		      struct sk_buff *skb, struct tcmsg *t)
{
263
	struct basic_filter *f = fh;
264
	struct nlattr *nest;
L
Linus Torvalds 已提交
265 266 267 268 269 270

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

	t->tcm_handle = f->handle;

271 272 273
	nest = nla_nest_start(skb, TCA_OPTIONS);
	if (nest == NULL)
		goto nla_put_failure;
L
Linus Torvalds 已提交
274

275 276 277
	if (f->res.classid &&
	    nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
		goto nla_put_failure;
278

279
	if (tcf_exts_dump(skb, &f->exts) < 0 ||
L
Linus Torvalds 已提交
280
	    tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
281
		goto nla_put_failure;
L
Linus Torvalds 已提交
282

283
	nla_nest_end(skb, nest);
284

285
	if (tcf_exts_dump_stats(skb, &f->exts) < 0)
286 287
		goto nla_put_failure;

L
Linus Torvalds 已提交
288 289
	return skb->len;

290
nla_put_failure:
291
	nla_nest_cancel(skb, nest);
L
Linus Torvalds 已提交
292 293 294
	return -1;
}

295
static struct tcf_proto_ops cls_basic_ops __read_mostly = {
L
Linus Torvalds 已提交
296 297 298 299 300 301 302 303 304
	.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,
305
	.bind_class	=	basic_bind_class,
L
Linus Torvalds 已提交
306 307 308 309 310 311 312 313
	.owner		=	THIS_MODULE,
};

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

314
static void __exit exit_basic(void)
L
Linus Torvalds 已提交
315 316 317 318 319 320 321 322
{
	unregister_tcf_proto_ops(&cls_basic_ops);
}

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