cls_fw.c 10.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * net/sched/cls_fw.c	Classifier mapping ipchains' fwmark to traffic class.
 *
 *		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:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
 *
 * Changes:
 * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_walk off by one
 * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_delete killed all the filter (and kernel).
 * Alex <alex@pilotsoft.com> : 2004xxyy: Added Action extension
 *
 * JHS: We should remove the CONFIG_NET_CLS_IND from here
 * eventually when the meta match extension is made available
 *
 */

#include <linux/module.h>
22
#include <linux/slab.h>
L
Linus Torvalds 已提交
23 24 25 26 27
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/skbuff.h>
28
#include <net/netlink.h>
L
Linus Torvalds 已提交
29 30 31
#include <net/act_api.h>
#include <net/pkt_cls.h>

32
#define HTSIZE 256
33

E
Eric Dumazet 已提交
34
struct fw_head {
35
	u32			mask;
J
John Fastabend 已提交
36 37
	struct fw_filter __rcu	*ht[HTSIZE];
	struct rcu_head		rcu;
L
Linus Torvalds 已提交
38 39
};

E
Eric Dumazet 已提交
40
struct fw_filter {
J
John Fastabend 已提交
41
	struct fw_filter __rcu	*next;
L
Linus Torvalds 已提交
42 43 44
	u32			id;
	struct tcf_result	res;
#ifdef CONFIG_NET_CLS_IND
45
	int			ifindex;
L
Linus Torvalds 已提交
46 47
#endif /* CONFIG_NET_CLS_IND */
	struct tcf_exts		exts;
J
John Fastabend 已提交
48
	struct tcf_proto	*tp;
49 50 51 52
	union {
		struct work_struct	work;
		struct rcu_head		rcu;
	};
L
Linus Torvalds 已提交
53 54
};

55
static u32 fw_hash(u32 handle)
L
Linus Torvalds 已提交
56
{
57 58 59
	handle ^= (handle >> 16);
	handle ^= (handle >> 8);
	return handle % HTSIZE;
L
Linus Torvalds 已提交
60 61
}

62
static int fw_classify(struct sk_buff *skb, const struct tcf_proto *tp,
J
Jamal Hadi Salim 已提交
63
		       struct tcf_result *res)
L
Linus Torvalds 已提交
64
{
J
John Fastabend 已提交
65
	struct fw_head *head = rcu_dereference_bh(tp->root);
L
Linus Torvalds 已提交
66 67
	struct fw_filter *f;
	int r;
68
	u32 id = skb->mark;
L
Linus Torvalds 已提交
69 70

	if (head != NULL) {
71
		id &= head->mask;
J
John Fastabend 已提交
72 73 74

		for (f = rcu_dereference_bh(head->ht[fw_hash(id)]); f;
		     f = rcu_dereference_bh(f->next)) {
L
Linus Torvalds 已提交
75 76 77
			if (f->id == id) {
				*res = f->res;
#ifdef CONFIG_NET_CLS_IND
78
				if (!tcf_match_indev(skb, f->ifindex))
L
Linus Torvalds 已提交
79 80 81 82 83 84 85 86 87 88
					continue;
#endif /* CONFIG_NET_CLS_IND */
				r = tcf_exts_exec(skb, &f->exts, res);
				if (r < 0)
					continue;

				return r;
			}
		}
	} else {
89
		/* Old method: classify the packet using its skb mark. */
E
Eric Dumazet 已提交
90 91
		if (id && (TC_H_MAJ(id) == 0 ||
			   !(TC_H_MAJ(id ^ tp->q->handle)))) {
L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99 100
			res->classid = id;
			res->class = 0;
			return 0;
		}
	}

	return -1;
}

101
static void *fw_get(struct tcf_proto *tp, u32 handle)
L
Linus Torvalds 已提交
102
{
J
John Fastabend 已提交
103
	struct fw_head *head = rtnl_dereference(tp->root);
L
Linus Torvalds 已提交
104 105 106
	struct fw_filter *f;

	if (head == NULL)
107
		return NULL;
L
Linus Torvalds 已提交
108

J
John Fastabend 已提交
109 110
	f = rtnl_dereference(head->ht[fw_hash(handle)]);
	for (; f; f = rtnl_dereference(f->next)) {
L
Linus Torvalds 已提交
111
		if (f->id == handle)
112
			return f;
L
Linus Torvalds 已提交
113
	}
114
	return NULL;
L
Linus Torvalds 已提交
115 116 117 118
}

static int fw_init(struct tcf_proto *tp)
{
119 120 121
	/* We don't allocate fw_head here, because in the old method
	 * we don't need it at all.
	 */
L
Linus Torvalds 已提交
122 123 124
	return 0;
}

125 126 127 128 129 130 131
static void __fw_delete_filter(struct fw_filter *f)
{
	tcf_exts_destroy(&f->exts);
	tcf_exts_put_net(&f->exts);
	kfree(f);
}

132
static void fw_delete_filter_work(struct work_struct *work)
L
Linus Torvalds 已提交
133
{
134
	struct fw_filter *f = container_of(work, struct fw_filter, work);
J
John Fastabend 已提交
135

136
	rtnl_lock();
137
	__fw_delete_filter(f);
138 139 140 141 142 143 144 145 146
	rtnl_unlock();
}

static void fw_delete_filter(struct rcu_head *head)
{
	struct fw_filter *f = container_of(head, struct fw_filter, rcu);

	INIT_WORK(&f->work, fw_delete_filter_work);
	tcf_queue_work(&f->work);
L
Linus Torvalds 已提交
147 148
}

149
static void fw_destroy(struct tcf_proto *tp)
L
Linus Torvalds 已提交
150
{
J
John Fastabend 已提交
151
	struct fw_head *head = rtnl_dereference(tp->root);
L
Linus Torvalds 已提交
152 153 154 155
	struct fw_filter *f;
	int h;

	if (head == NULL)
156
		return;
L
Linus Torvalds 已提交
157

E
Eric Dumazet 已提交
158
	for (h = 0; h < HTSIZE; h++) {
J
John Fastabend 已提交
159 160 161
		while ((f = rtnl_dereference(head->ht[h])) != NULL) {
			RCU_INIT_POINTER(head->ht[h],
					 rtnl_dereference(f->next));
162
			tcf_unbind_filter(tp, &f->res);
163 164 165 166
			if (tcf_exts_get_net(&f->exts))
				call_rcu(&f->rcu, fw_delete_filter);
			else
				__fw_delete_filter(f);
L
Linus Torvalds 已提交
167 168
		}
	}
J
John Fastabend 已提交
169
	kfree_rcu(head, rcu);
L
Linus Torvalds 已提交
170 171
}

172
static int fw_delete(struct tcf_proto *tp, void *arg, bool *last)
L
Linus Torvalds 已提交
173
{
J
John Fastabend 已提交
174
	struct fw_head *head = rtnl_dereference(tp->root);
175
	struct fw_filter *f = arg;
J
John Fastabend 已提交
176 177
	struct fw_filter __rcu **fp;
	struct fw_filter *pfp;
178 179
	int ret = -EINVAL;
	int h;
L
Linus Torvalds 已提交
180 181 182 183

	if (head == NULL || f == NULL)
		goto out;

J
John Fastabend 已提交
184 185 186 187 188 189
	fp = &head->ht[fw_hash(f->id)];

	for (pfp = rtnl_dereference(*fp); pfp;
	     fp = &pfp->next, pfp = rtnl_dereference(*fp)) {
		if (pfp == f) {
			RCU_INIT_POINTER(*fp, rtnl_dereference(f->next));
190
			tcf_unbind_filter(tp, &f->res);
191
			tcf_exts_get_net(&f->exts);
J
John Fastabend 已提交
192
			call_rcu(&f->rcu, fw_delete_filter);
193 194
			ret = 0;
			break;
L
Linus Torvalds 已提交
195 196
		}
	}
197 198 199 200 201 202 203 204 205

	*last = true;
	for (h = 0; h < HTSIZE; h++) {
		if (rcu_access_pointer(head->ht[h])) {
			*last = false;
			break;
		}
	}

L
Linus Torvalds 已提交
206
out:
207
	return ret;
L
Linus Torvalds 已提交
208 209
}

210 211 212 213 214 215
static const struct nla_policy fw_policy[TCA_FW_MAX + 1] = {
	[TCA_FW_CLASSID]	= { .type = NLA_U32 },
	[TCA_FW_INDEV]		= { .type = NLA_STRING, .len = IFNAMSIZ },
	[TCA_FW_MASK]		= { .type = NLA_U32 },
};

216 217 218
static int fw_set_parms(struct net *net, struct tcf_proto *tp,
			struct fw_filter *f, struct nlattr **tb,
			struct nlattr **tca, unsigned long base, bool ovr)
L
Linus Torvalds 已提交
219
{
J
John Fastabend 已提交
220
	struct fw_head *head = rtnl_dereference(tp->root);
221
	u32 mask;
L
Linus Torvalds 已提交
222 223
	int err;

224
	err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &f->exts, ovr);
L
Linus Torvalds 已提交
225 226 227
	if (err < 0)
		return err;

228
	if (tb[TCA_FW_CLASSID]) {
229
		f->res.classid = nla_get_u32(tb[TCA_FW_CLASSID]);
L
Linus Torvalds 已提交
230 231 232 233
		tcf_bind_filter(tp, &f->res, base);
	}

#ifdef CONFIG_NET_CLS_IND
234
	if (tb[TCA_FW_INDEV]) {
235 236
		int ret;
		ret = tcf_change_indev(net, tb[TCA_FW_INDEV]);
237 238
		if (ret < 0)
			return ret;
239
		f->ifindex = ret;
L
Linus Torvalds 已提交
240 241 242
	}
#endif /* CONFIG_NET_CLS_IND */

243
	err = -EINVAL;
244
	if (tb[TCA_FW_MASK]) {
245
		mask = nla_get_u32(tb[TCA_FW_MASK]);
246
		if (mask != head->mask)
247
			return err;
248
	} else if (head->mask != 0xFFFFFFFF)
249
		return err;
L
Linus Torvalds 已提交
250 251 252 253

	return 0;
}

254
static int fw_change(struct net *net, struct sk_buff *in_skb,
255
		     struct tcf_proto *tp, unsigned long base,
256
		     u32 handle, struct nlattr **tca, void **arg,
J
Jamal Hadi Salim 已提交
257
		     bool ovr)
L
Linus Torvalds 已提交
258
{
J
John Fastabend 已提交
259
	struct fw_head *head = rtnl_dereference(tp->root);
260
	struct fw_filter *f = *arg;
261 262
	struct nlattr *opt = tca[TCA_OPTIONS];
	struct nlattr *tb[TCA_FW_MAX + 1];
L
Linus Torvalds 已提交
263 264 265
	int err;

	if (!opt)
266
		return handle ? -EINVAL : 0; /* Succeed if it is old method. */
L
Linus Torvalds 已提交
267

268
	err = nla_parse_nested(tb, TCA_FW_MAX, opt, fw_policy, NULL);
269 270
	if (err < 0)
		return err;
L
Linus Torvalds 已提交
271

J
John Fastabend 已提交
272 273 274 275
	if (f) {
		struct fw_filter *pfp, *fnew;
		struct fw_filter __rcu **fp;

L
Linus Torvalds 已提交
276 277
		if (f->id != handle && handle)
			return -EINVAL;
J
John Fastabend 已提交
278 279 280 281 282 283 284 285 286 287 288 289

		fnew = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
		if (!fnew)
			return -ENOBUFS;

		fnew->id = f->id;
		fnew->res = f->res;
#ifdef CONFIG_NET_CLS_IND
		fnew->ifindex = f->ifindex;
#endif /* CONFIG_NET_CLS_IND */
		fnew->tp = f->tp;

290 291 292 293 294
		err = tcf_exts_init(&fnew->exts, TCA_FW_ACT, TCA_FW_POLICE);
		if (err < 0) {
			kfree(fnew);
			return err;
		}
295

296
		err = fw_set_parms(net, tp, fnew, tb, tca, base, ovr);
J
John Fastabend 已提交
297
		if (err < 0) {
298
			tcf_exts_destroy(&fnew->exts);
J
John Fastabend 已提交
299 300 301 302 303 304 305 306 307 308 309 310
			kfree(fnew);
			return err;
		}

		fp = &head->ht[fw_hash(fnew->id)];
		for (pfp = rtnl_dereference(*fp); pfp;
		     fp = &pfp->next, pfp = rtnl_dereference(*fp))
			if (pfp == f)
				break;

		RCU_INIT_POINTER(fnew->next, rtnl_dereference(pfp->next));
		rcu_assign_pointer(*fp, fnew);
311
		tcf_unbind_filter(tp, &f->res);
312
		tcf_exts_get_net(&f->exts);
J
John Fastabend 已提交
313 314
		call_rcu(&f->rcu, fw_delete_filter);

315
		*arg = fnew;
J
John Fastabend 已提交
316
		return err;
L
Linus Torvalds 已提交
317 318 319 320 321
	}

	if (!handle)
		return -EINVAL;

322 323
	if (!head) {
		u32 mask = 0xFFFFFFFF;
324
		if (tb[TCA_FW_MASK])
325 326 327 328 329 330 331 332
			mask = nla_get_u32(tb[TCA_FW_MASK]);

		head = kzalloc(sizeof(*head), GFP_KERNEL);
		if (!head)
			return -ENOBUFS;
		head->mask = mask;

		rcu_assign_pointer(tp->root, head);
L
Linus Torvalds 已提交
333 334
	}

335
	f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
L
Linus Torvalds 已提交
336 337 338
	if (f == NULL)
		return -ENOBUFS;

339 340 341
	err = tcf_exts_init(&f->exts, TCA_FW_ACT, TCA_FW_POLICE);
	if (err < 0)
		goto errout;
L
Linus Torvalds 已提交
342
	f->id = handle;
J
John Fastabend 已提交
343
	f->tp = tp;
L
Linus Torvalds 已提交
344

345
	err = fw_set_parms(net, tp, f, tb, tca, base, ovr);
L
Linus Torvalds 已提交
346 347 348
	if (err < 0)
		goto errout;

J
John Fastabend 已提交
349 350
	RCU_INIT_POINTER(f->next, head->ht[fw_hash(handle)]);
	rcu_assign_pointer(head->ht[fw_hash(handle)], f);
L
Linus Torvalds 已提交
351

352
	*arg = f;
L
Linus Torvalds 已提交
353 354 355
	return 0;

errout:
356
	tcf_exts_destroy(&f->exts);
J
Jesper Juhl 已提交
357
	kfree(f);
L
Linus Torvalds 已提交
358 359 360 361 362
	return err;
}

static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg)
{
J
John Fastabend 已提交
363
	struct fw_head *head = rtnl_dereference(tp->root);
L
Linus Torvalds 已提交
364 365 366 367 368 369 370 371
	int h;

	if (head == NULL)
		arg->stop = 1;

	if (arg->stop)
		return;

372
	for (h = 0; h < HTSIZE; h++) {
L
Linus Torvalds 已提交
373 374
		struct fw_filter *f;

J
John Fastabend 已提交
375 376
		for (f = rtnl_dereference(head->ht[h]); f;
		     f = rtnl_dereference(f->next)) {
L
Linus Torvalds 已提交
377 378 379 380
			if (arg->count < arg->skip) {
				arg->count++;
				continue;
			}
381
			if (arg->fn(tp, f, arg) < 0) {
L
Linus Torvalds 已提交
382 383 384 385 386 387 388 389
				arg->stop = 1;
				return;
			}
			arg->count++;
		}
	}
}

390
static int fw_dump(struct net *net, struct tcf_proto *tp, void *fh,
L
Linus Torvalds 已提交
391 392
		   struct sk_buff *skb, struct tcmsg *t)
{
J
John Fastabend 已提交
393
	struct fw_head *head = rtnl_dereference(tp->root);
394
	struct fw_filter *f = fh;
395
	struct nlattr *nest;
L
Linus Torvalds 已提交
396 397 398 399 400 401

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

	t->tcm_handle = f->id;

402
	if (!f->res.classid && !tcf_exts_has_actions(&f->exts))
L
Linus Torvalds 已提交
403 404
		return skb->len;

405 406 407
	nest = nla_nest_start(skb, TCA_OPTIONS);
	if (nest == NULL)
		goto nla_put_failure;
L
Linus Torvalds 已提交
408

409 410 411
	if (f->res.classid &&
	    nla_put_u32(skb, TCA_FW_CLASSID, f->res.classid))
		goto nla_put_failure;
L
Linus Torvalds 已提交
412
#ifdef CONFIG_NET_CLS_IND
413 414 415 416 417 418
	if (f->ifindex) {
		struct net_device *dev;
		dev = __dev_get_by_index(net, f->ifindex);
		if (dev && nla_put_string(skb, TCA_FW_INDEV, dev->name))
			goto nla_put_failure;
	}
L
Linus Torvalds 已提交
419
#endif /* CONFIG_NET_CLS_IND */
420 421 422
	if (head->mask != 0xFFFFFFFF &&
	    nla_put_u32(skb, TCA_FW_MASK, head->mask))
		goto nla_put_failure;
L
Linus Torvalds 已提交
423

424
	if (tcf_exts_dump(skb, &f->exts) < 0)
425
		goto nla_put_failure;
L
Linus Torvalds 已提交
426

427
	nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
428

429
	if (tcf_exts_dump_stats(skb, &f->exts) < 0)
430
		goto nla_put_failure;
L
Linus Torvalds 已提交
431 432 433

	return skb->len;

434
nla_put_failure:
435
	nla_nest_cancel(skb, nest);
L
Linus Torvalds 已提交
436 437 438
	return -1;
}

439 440 441 442 443 444 445 446
static void fw_bind_class(void *fh, u32 classid, unsigned long cl)
{
	struct fw_filter *f = fh;

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

447
static struct tcf_proto_ops cls_fw_ops __read_mostly = {
L
Linus Torvalds 已提交
448 449 450 451 452 453 454 455 456
	.kind		=	"fw",
	.classify	=	fw_classify,
	.init		=	fw_init,
	.destroy	=	fw_destroy,
	.get		=	fw_get,
	.change		=	fw_change,
	.delete		=	fw_delete,
	.walk		=	fw_walk,
	.dump		=	fw_dump,
457
	.bind_class	=	fw_bind_class,
L
Linus Torvalds 已提交
458 459 460 461 462 463 464 465
	.owner		=	THIS_MODULE,
};

static int __init init_fw(void)
{
	return register_tcf_proto_ops(&cls_fw_ops);
}

466
static void __exit exit_fw(void)
L
Linus Torvalds 已提交
467 468 469 470 471 472 473
{
	unregister_tcf_proto_ops(&cls_fw_ops);
}

module_init(init_fw)
module_exit(exit_fw)
MODULE_LICENSE("GPL");