cls_fw.c 9.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13
/*
 * net/sched/cls_fw.c	Classifier mapping ipchains' fwmark to traffic class.
 *
 * 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
 */

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

25
#define HTSIZE 256
26

E
Eric Dumazet 已提交
27
struct fw_head {
28
	u32			mask;
J
John Fastabend 已提交
29 30
	struct fw_filter __rcu	*ht[HTSIZE];
	struct rcu_head		rcu;
L
Linus Torvalds 已提交
31 32
};

E
Eric Dumazet 已提交
33
struct fw_filter {
J
John Fastabend 已提交
34
	struct fw_filter __rcu	*next;
L
Linus Torvalds 已提交
35 36
	u32			id;
	struct tcf_result	res;
37
	int			ifindex;
L
Linus Torvalds 已提交
38
	struct tcf_exts		exts;
J
John Fastabend 已提交
39
	struct tcf_proto	*tp;
C
Cong Wang 已提交
40
	struct rcu_work		rwork;
L
Linus Torvalds 已提交
41 42
};

43
static u32 fw_hash(u32 handle)
L
Linus Torvalds 已提交
44
{
45 46 47
	handle ^= (handle >> 16);
	handle ^= (handle >> 8);
	return handle % HTSIZE;
L
Linus Torvalds 已提交
48 49
}

50
static int fw_classify(struct sk_buff *skb, const struct tcf_proto *tp,
J
Jamal Hadi Salim 已提交
51
		       struct tcf_result *res)
L
Linus Torvalds 已提交
52
{
J
John Fastabend 已提交
53
	struct fw_head *head = rcu_dereference_bh(tp->root);
L
Linus Torvalds 已提交
54 55
	struct fw_filter *f;
	int r;
56
	u32 id = skb->mark;
L
Linus Torvalds 已提交
57 58

	if (head != NULL) {
59
		id &= head->mask;
J
John Fastabend 已提交
60 61 62

		for (f = rcu_dereference_bh(head->ht[fw_hash(id)]); f;
		     f = rcu_dereference_bh(f->next)) {
L
Linus Torvalds 已提交
63 64
			if (f->id == id) {
				*res = f->res;
65
				if (!tcf_match_indev(skb, f->ifindex))
L
Linus Torvalds 已提交
66 67 68 69 70 71 72 73 74
					continue;
				r = tcf_exts_exec(skb, &f->exts, res);
				if (r < 0)
					continue;

				return r;
			}
		}
	} else {
75 76
		struct Qdisc *q = tcf_block_q(tp->chain->block);

77
		/* Old method: classify the packet using its skb mark. */
E
Eric Dumazet 已提交
78
		if (id && (TC_H_MAJ(id) == 0 ||
79
			   !(TC_H_MAJ(id ^ q->handle)))) {
L
Linus Torvalds 已提交
80 81 82 83 84 85 86 87 88
			res->classid = id;
			res->class = 0;
			return 0;
		}
	}

	return -1;
}

89
static void *fw_get(struct tcf_proto *tp, u32 handle)
L
Linus Torvalds 已提交
90
{
J
John Fastabend 已提交
91
	struct fw_head *head = rtnl_dereference(tp->root);
L
Linus Torvalds 已提交
92 93 94
	struct fw_filter *f;

	if (head == NULL)
95
		return NULL;
L
Linus Torvalds 已提交
96

J
John Fastabend 已提交
97 98
	f = rtnl_dereference(head->ht[fw_hash(handle)]);
	for (; f; f = rtnl_dereference(f->next)) {
L
Linus Torvalds 已提交
99
		if (f->id == handle)
100
			return f;
L
Linus Torvalds 已提交
101
	}
102
	return NULL;
L
Linus Torvalds 已提交
103 104 105 106
}

static int fw_init(struct tcf_proto *tp)
{
107 108 109
	/* We don't allocate fw_head here, because in the old method
	 * we don't need it at all.
	 */
L
Linus Torvalds 已提交
110 111 112
	return 0;
}

113 114 115 116 117 118 119
static void __fw_delete_filter(struct fw_filter *f)
{
	tcf_exts_destroy(&f->exts);
	tcf_exts_put_net(&f->exts);
	kfree(f);
}

120
static void fw_delete_filter_work(struct work_struct *work)
L
Linus Torvalds 已提交
121
{
C
Cong Wang 已提交
122 123 124
	struct fw_filter *f = container_of(to_rcu_work(work),
					   struct fw_filter,
					   rwork);
125
	rtnl_lock();
126
	__fw_delete_filter(f);
127 128 129
	rtnl_unlock();
}

130 131
static void fw_destroy(struct tcf_proto *tp, bool rtnl_held,
		       struct netlink_ext_ack *extack)
L
Linus Torvalds 已提交
132
{
J
John Fastabend 已提交
133
	struct fw_head *head = rtnl_dereference(tp->root);
L
Linus Torvalds 已提交
134 135 136 137
	struct fw_filter *f;
	int h;

	if (head == NULL)
138
		return;
L
Linus Torvalds 已提交
139

E
Eric Dumazet 已提交
140
	for (h = 0; h < HTSIZE; h++) {
J
John Fastabend 已提交
141 142 143
		while ((f = rtnl_dereference(head->ht[h])) != NULL) {
			RCU_INIT_POINTER(head->ht[h],
					 rtnl_dereference(f->next));
144
			tcf_unbind_filter(tp, &f->res);
145
			if (tcf_exts_get_net(&f->exts))
C
Cong Wang 已提交
146
				tcf_queue_work(&f->rwork, fw_delete_filter_work);
147 148
			else
				__fw_delete_filter(f);
L
Linus Torvalds 已提交
149 150
		}
	}
J
John Fastabend 已提交
151
	kfree_rcu(head, rcu);
L
Linus Torvalds 已提交
152 153
}

154
static int fw_delete(struct tcf_proto *tp, void *arg, bool *last,
155
		     bool rtnl_held, struct netlink_ext_ack *extack)
L
Linus Torvalds 已提交
156
{
J
John Fastabend 已提交
157
	struct fw_head *head = rtnl_dereference(tp->root);
158
	struct fw_filter *f = arg;
J
John Fastabend 已提交
159 160
	struct fw_filter __rcu **fp;
	struct fw_filter *pfp;
161 162
	int ret = -EINVAL;
	int h;
L
Linus Torvalds 已提交
163 164 165 166

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

J
John Fastabend 已提交
167 168 169 170 171 172
	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));
173
			tcf_unbind_filter(tp, &f->res);
174
			tcf_exts_get_net(&f->exts);
C
Cong Wang 已提交
175
			tcf_queue_work(&f->rwork, fw_delete_filter_work);
176 177
			ret = 0;
			break;
L
Linus Torvalds 已提交
178 179
		}
	}
180 181 182 183 184 185 186 187 188

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

L
Linus Torvalds 已提交
189
out:
190
	return ret;
L
Linus Torvalds 已提交
191 192
}

193 194 195 196 197 198
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 },
};

199 200
static int fw_set_parms(struct net *net, struct tcf_proto *tp,
			struct fw_filter *f, struct nlattr **tb,
201 202
			struct nlattr **tca, unsigned long base, bool ovr,
			struct netlink_ext_ack *extack)
L
Linus Torvalds 已提交
203
{
J
John Fastabend 已提交
204
	struct fw_head *head = rtnl_dereference(tp->root);
205
	u32 mask;
L
Linus Torvalds 已提交
206 207
	int err;

208
	err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &f->exts, ovr,
209
				true, extack);
L
Linus Torvalds 已提交
210 211 212
	if (err < 0)
		return err;

213
	if (tb[TCA_FW_INDEV]) {
214
		int ret;
215
		ret = tcf_change_indev(net, tb[TCA_FW_INDEV], extack);
216 217
		if (ret < 0)
			return ret;
218
		f->ifindex = ret;
L
Linus Torvalds 已提交
219 220
	}

221
	err = -EINVAL;
222
	if (tb[TCA_FW_MASK]) {
223
		mask = nla_get_u32(tb[TCA_FW_MASK]);
224
		if (mask != head->mask)
225
			return err;
226
	} else if (head->mask != 0xFFFFFFFF)
227
		return err;
L
Linus Torvalds 已提交
228

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

L
Linus Torvalds 已提交
234 235 236
	return 0;
}

237
static int fw_change(struct net *net, struct sk_buff *in_skb,
238
		     struct tcf_proto *tp, unsigned long base,
239
		     u32 handle, struct nlattr **tca, void **arg,
240 241
		     bool ovr, bool rtnl_held,
		     struct netlink_ext_ack *extack)
L
Linus Torvalds 已提交
242
{
J
John Fastabend 已提交
243
	struct fw_head *head = rtnl_dereference(tp->root);
244
	struct fw_filter *f = *arg;
245 246
	struct nlattr *opt = tca[TCA_OPTIONS];
	struct nlattr *tb[TCA_FW_MAX + 1];
L
Linus Torvalds 已提交
247 248 249
	int err;

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

252 253
	err = nla_parse_nested_deprecated(tb, TCA_FW_MAX, opt, fw_policy,
					  NULL);
254 255
	if (err < 0)
		return err;
L
Linus Torvalds 已提交
256

J
John Fastabend 已提交
257 258 259 260
	if (f) {
		struct fw_filter *pfp, *fnew;
		struct fw_filter __rcu **fp;

L
Linus Torvalds 已提交
261 262
		if (f->id != handle && handle)
			return -EINVAL;
J
John Fastabend 已提交
263 264 265 266 267 268 269 270 271

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

		fnew->id = f->id;
		fnew->ifindex = f->ifindex;
		fnew->tp = f->tp;

272 273
		err = tcf_exts_init(&fnew->exts, net, TCA_FW_ACT,
				    TCA_FW_POLICE);
274 275 276 277
		if (err < 0) {
			kfree(fnew);
			return err;
		}
278

279
		err = fw_set_parms(net, tp, fnew, tb, tca, base, ovr, extack);
J
John Fastabend 已提交
280
		if (err < 0) {
281
			tcf_exts_destroy(&fnew->exts);
J
John Fastabend 已提交
282 283 284 285 286 287 288 289 290 291 292 293
			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);
294
		tcf_unbind_filter(tp, &f->res);
295
		tcf_exts_get_net(&f->exts);
C
Cong Wang 已提交
296
		tcf_queue_work(&f->rwork, fw_delete_filter_work);
J
John Fastabend 已提交
297

298
		*arg = fnew;
J
John Fastabend 已提交
299
		return err;
L
Linus Torvalds 已提交
300 301 302 303 304
	}

	if (!handle)
		return -EINVAL;

305 306
	if (!head) {
		u32 mask = 0xFFFFFFFF;
307
		if (tb[TCA_FW_MASK])
308 309 310 311 312 313 314 315
			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 已提交
316 317
	}

318
	f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
L
Linus Torvalds 已提交
319 320 321
	if (f == NULL)
		return -ENOBUFS;

322
	err = tcf_exts_init(&f->exts, net, TCA_FW_ACT, TCA_FW_POLICE);
323 324
	if (err < 0)
		goto errout;
L
Linus Torvalds 已提交
325
	f->id = handle;
J
John Fastabend 已提交
326
	f->tp = tp;
L
Linus Torvalds 已提交
327

328
	err = fw_set_parms(net, tp, f, tb, tca, base, ovr, extack);
L
Linus Torvalds 已提交
329 330 331
	if (err < 0)
		goto errout;

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

335
	*arg = f;
L
Linus Torvalds 已提交
336 337 338
	return 0;

errout:
339
	tcf_exts_destroy(&f->exts);
J
Jesper Juhl 已提交
340
	kfree(f);
L
Linus Torvalds 已提交
341 342 343
	return err;
}

344 345
static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg,
		    bool rtnl_held)
L
Linus Torvalds 已提交
346
{
J
John Fastabend 已提交
347
	struct fw_head *head = rtnl_dereference(tp->root);
L
Linus Torvalds 已提交
348 349
	int h;

350 351 352 353
	if (head == NULL)
		arg->stop = 1;

	if (arg->stop)
L
Linus Torvalds 已提交
354 355
		return;

356
	for (h = 0; h < HTSIZE; h++) {
L
Linus Torvalds 已提交
357 358
		struct fw_filter *f;

J
John Fastabend 已提交
359 360
		for (f = rtnl_dereference(head->ht[h]); f;
		     f = rtnl_dereference(f->next)) {
L
Linus Torvalds 已提交
361 362 363 364
			if (arg->count < arg->skip) {
				arg->count++;
				continue;
			}
365
			if (arg->fn(tp, f, arg) < 0) {
L
Linus Torvalds 已提交
366 367 368 369 370 371 372 373
				arg->stop = 1;
				return;
			}
			arg->count++;
		}
	}
}

374
static int fw_dump(struct net *net, struct tcf_proto *tp, void *fh,
375
		   struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
L
Linus Torvalds 已提交
376
{
J
John Fastabend 已提交
377
	struct fw_head *head = rtnl_dereference(tp->root);
378
	struct fw_filter *f = fh;
379
	struct nlattr *nest;
L
Linus Torvalds 已提交
380 381 382 383 384 385

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

	t->tcm_handle = f->id;

386
	if (!f->res.classid && !tcf_exts_has_actions(&f->exts))
L
Linus Torvalds 已提交
387 388
		return skb->len;

389
	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
390 391
	if (nest == NULL)
		goto nla_put_failure;
L
Linus Torvalds 已提交
392

393 394 395
	if (f->res.classid &&
	    nla_put_u32(skb, TCA_FW_CLASSID, f->res.classid))
		goto nla_put_failure;
396 397 398 399 400 401
	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;
	}
402 403 404
	if (head->mask != 0xFFFFFFFF &&
	    nla_put_u32(skb, TCA_FW_MASK, head->mask))
		goto nla_put_failure;
L
Linus Torvalds 已提交
405

406
	if (tcf_exts_dump(skb, &f->exts) < 0)
407
		goto nla_put_failure;
L
Linus Torvalds 已提交
408

409
	nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
410

411
	if (tcf_exts_dump_stats(skb, &f->exts) < 0)
412
		goto nla_put_failure;
L
Linus Torvalds 已提交
413 414 415

	return skb->len;

416
nla_put_failure:
417
	nla_nest_cancel(skb, nest);
L
Linus Torvalds 已提交
418 419 420
	return -1;
}

421 422
static void fw_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
			  unsigned long base)
423 424 425
{
	struct fw_filter *f = fh;

426 427 428 429 430 431
	if (f && f->res.classid == classid) {
		if (cl)
			__tcf_bind_filter(q, &f->res, base);
		else
			__tcf_unbind_filter(q, &f->res);
	}
432 433
}

434
static struct tcf_proto_ops cls_fw_ops __read_mostly = {
L
Linus Torvalds 已提交
435 436 437 438 439 440 441 442 443
	.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,
444
	.bind_class	=	fw_bind_class,
L
Linus Torvalds 已提交
445 446 447 448 449 450 451 452
	.owner		=	THIS_MODULE,
};

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

453
static void __exit exit_fw(void)
L
Linus Torvalds 已提交
454 455 456 457 458 459 460
{
	unregister_tcf_proto_ops(&cls_fw_ops);
}

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