cls_basic.c 7.6 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 <linux/idr.h>
21
#include <net/netlink.h>
L
Linus Torvalds 已提交
22 23 24
#include <net/act_api.h>
#include <net/pkt_cls.h>

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

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

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

J
John Fastabend 已提交
51
	list_for_each_entry_rcu(f, &head->flist, link) {
L
Linus Torvalds 已提交
52 53 54 55 56 57 58 59 60 61 62
		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;
}

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

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

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

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

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

90 91 92 93 94 95 96 97
static void __basic_delete_filter(struct basic_filter *f)
{
	tcf_exts_destroy(&f->exts);
	tcf_em_tree_destroy(&f->ematches);
	tcf_exts_put_net(&f->exts);
	kfree(f);
}

98
static void basic_delete_filter_work(struct work_struct *work)
L
Linus Torvalds 已提交
99
{
100
	struct basic_filter *f = container_of(work, struct basic_filter, work);
J
John Fastabend 已提交
101

102
	rtnl_lock();
103
	__basic_delete_filter(f);
104
	rtnl_unlock();
L
Linus Torvalds 已提交
105 106
}

107 108 109 110 111 112 113 114
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);
}

115
static void basic_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
L
Linus Torvalds 已提交
116
{
J
John Fastabend 已提交
117
	struct basic_head *head = rtnl_dereference(tp->root);
L
Linus Torvalds 已提交
118
	struct basic_filter *f, *n;
119

L
Linus Torvalds 已提交
120
	list_for_each_entry_safe(f, n, &head->flist, link) {
J
John Fastabend 已提交
121
		list_del_rcu(&f->link);
122
		tcf_unbind_filter(tp, &f->res);
123
		idr_remove_ext(&head->handle_idr, f->handle);
124 125 126 127
		if (tcf_exts_get_net(&f->exts))
			call_rcu(&f->rcu, basic_delete_filter);
		else
			__basic_delete_filter(f);
L
Linus Torvalds 已提交
128
	}
129
	idr_destroy(&head->handle_idr);
J
John Fastabend 已提交
130
	kfree_rcu(head, rcu);
L
Linus Torvalds 已提交
131 132
}

133 134
static int basic_delete(struct tcf_proto *tp, void *arg, bool *last,
			struct netlink_ext_ack *extack)
L
Linus Torvalds 已提交
135
{
136
	struct basic_head *head = rtnl_dereference(tp->root);
137
	struct basic_filter *f = arg;
L
Linus Torvalds 已提交
138

139 140
	list_del_rcu(&f->link);
	tcf_unbind_filter(tp, &f->res);
141
	idr_remove_ext(&head->handle_idr, f->handle);
142
	tcf_exts_get_net(&f->exts);
143
	call_rcu(&f->rcu, basic_delete_filter);
144
	*last = list_empty(&head->flist);
145
	return 0;
L
Linus Torvalds 已提交
146 147
}

148 149 150 151 152
static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
	[TCA_BASIC_CLASSID]	= { .type = NLA_U32 },
	[TCA_BASIC_EMATCHES]	= { .type = NLA_NESTED },
};

153 154 155
static int basic_set_parms(struct net *net, struct tcf_proto *tp,
			   struct basic_filter *f, unsigned long base,
			   struct nlattr **tb,
156 157
			   struct nlattr *est, bool ovr,
			   struct netlink_ext_ack *extack)
L
Linus Torvalds 已提交
158
{
159
	int err;
L
Linus Torvalds 已提交
160

161
	err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, extack);
L
Linus Torvalds 已提交
162 163 164
	if (err < 0)
		return err;

165
	err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &f->ematches);
L
Linus Torvalds 已提交
166
	if (err < 0)
167
		return err;
L
Linus Torvalds 已提交
168

169
	if (tb[TCA_BASIC_CLASSID]) {
170
		f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
L
Linus Torvalds 已提交
171 172 173
		tcf_bind_filter(tp, &f->res, base);
	}

J
John Fastabend 已提交
174
	f->tp = tp;
L
Linus Torvalds 已提交
175 176 177
	return 0;
}

178
static int basic_change(struct net *net, struct sk_buff *in_skb,
179
			struct tcf_proto *tp, unsigned long base, u32 handle,
180 181
			struct nlattr **tca, void **arg, bool ovr,
			struct netlink_ext_ack *extack)
L
Linus Torvalds 已提交
182
{
183
	int err;
J
John Fastabend 已提交
184
	struct basic_head *head = rtnl_dereference(tp->root);
185
	struct nlattr *tb[TCA_BASIC_MAX + 1];
J
John Fastabend 已提交
186 187
	struct basic_filter *fold = (struct basic_filter *) *arg;
	struct basic_filter *fnew;
188
	unsigned long idr_index;
L
Linus Torvalds 已提交
189

190
	if (tca[TCA_OPTIONS] == NULL)
L
Linus Torvalds 已提交
191 192
		return -EINVAL;

193
	err = nla_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
194
			       basic_policy, NULL);
195 196
	if (err < 0)
		return err;
L
Linus Torvalds 已提交
197

J
John Fastabend 已提交
198 199
	if (fold != NULL) {
		if (handle && fold->handle != handle)
L
Linus Torvalds 已提交
200 201 202
			return -EINVAL;
	}

J
John Fastabend 已提交
203
	fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
204 205
	if (!fnew)
		return -ENOBUFS;
L
Linus Torvalds 已提交
206

207 208 209 210
	err = tcf_exts_init(&fnew->exts, TCA_BASIC_ACT, TCA_BASIC_POLICE);
	if (err < 0)
		goto errout;

J
John Fastabend 已提交
211 212
	if (handle) {
		fnew->handle = handle;
213 214 215 216 217 218
		if (!fold) {
			err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index,
					    handle, handle + 1, GFP_KERNEL);
			if (err)
				goto errout;
		}
J
John Fastabend 已提交
219
	} else {
220 221 222
		err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index,
				    1, 0x7FFFFFFF, GFP_KERNEL);
		if (err)
L
Linus Torvalds 已提交
223
			goto errout;
224
		fnew->handle = idr_index;
L
Linus Torvalds 已提交
225 226
	}

227 228
	err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr,
			      extack);
229 230 231
	if (err < 0) {
		if (!fold)
			idr_remove_ext(&head->handle_idr, fnew->handle);
L
Linus Torvalds 已提交
232
		goto errout;
233
	}
L
Linus Torvalds 已提交
234

235
	*arg = fnew;
J
John Fastabend 已提交
236 237

	if (fold) {
238
		idr_replace_ext(&head->handle_idr, fnew, fnew->handle);
J
John Fastabend 已提交
239
		list_replace_rcu(&fold->link, &fnew->link);
240
		tcf_unbind_filter(tp, &fold->res);
241
		tcf_exts_get_net(&fold->exts);
J
John Fastabend 已提交
242 243 244 245
		call_rcu(&fold->rcu, basic_delete_filter);
	} else {
		list_add_rcu(&fnew->link, &head->flist);
	}
L
Linus Torvalds 已提交
246 247 248

	return 0;
errout:
249
	tcf_exts_destroy(&fnew->exts);
J
John Fastabend 已提交
250
	kfree(fnew);
L
Linus Torvalds 已提交
251 252 253 254 255
	return err;
}

static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg)
{
J
John Fastabend 已提交
256
	struct basic_head *head = rtnl_dereference(tp->root);
L
Linus Torvalds 已提交
257 258 259 260 261 262
	struct basic_filter *f;

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

263
		if (arg->fn(tp, f, arg) < 0) {
L
Linus Torvalds 已提交
264 265 266 267 268 269 270 271
			arg->stop = 1;
			break;
		}
skip:
		arg->count++;
	}
}

272 273 274 275 276 277 278 279
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;
}

280
static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh,
L
Linus Torvalds 已提交
281 282
		      struct sk_buff *skb, struct tcmsg *t)
{
283
	struct basic_filter *f = fh;
284
	struct nlattr *nest;
L
Linus Torvalds 已提交
285 286 287 288 289 290

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

	t->tcm_handle = f->handle;

291 292 293
	nest = nla_nest_start(skb, TCA_OPTIONS);
	if (nest == NULL)
		goto nla_put_failure;
L
Linus Torvalds 已提交
294

295 296 297
	if (f->res.classid &&
	    nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
		goto nla_put_failure;
298

299
	if (tcf_exts_dump(skb, &f->exts) < 0 ||
L
Linus Torvalds 已提交
300
	    tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
301
		goto nla_put_failure;
L
Linus Torvalds 已提交
302

303
	nla_nest_end(skb, nest);
304

305
	if (tcf_exts_dump_stats(skb, &f->exts) < 0)
306 307
		goto nla_put_failure;

L
Linus Torvalds 已提交
308 309
	return skb->len;

310
nla_put_failure:
311
	nla_nest_cancel(skb, nest);
L
Linus Torvalds 已提交
312 313 314
	return -1;
}

315
static struct tcf_proto_ops cls_basic_ops __read_mostly = {
L
Linus Torvalds 已提交
316 317 318 319 320 321 322 323 324
	.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,
325
	.bind_class	=	basic_bind_class,
L
Linus Torvalds 已提交
326 327 328 329 330 331 332 333
	.owner		=	THIS_MODULE,
};

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

334
static void __exit exit_basic(void)
L
Linus Torvalds 已提交
335 336 337 338 339 340 341 342
{
	unregister_tcf_proto_ops(&cls_basic_ops);
}

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