cls_api.c 16.1 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 22 23 24
/*
 * net/sched/cls_api.c	Packet classifier API.
 *
 *		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:
 *
 * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
 *
 */

#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/skbuff.h>
#include <linux/init.h>
#include <linux/kmod.h>
25
#include <linux/err.h>
26
#include <linux/slab.h>
27 28
#include <net/net_namespace.h>
#include <net/sock.h>
29
#include <net/netlink.h>
L
Linus Torvalds 已提交
30 31 32 33
#include <net/pkt_sched.h>
#include <net/pkt_cls.h>

/* The list of all installed classifier types */
34
static LIST_HEAD(tcf_proto_base);
L
Linus Torvalds 已提交
35 36 37 38 39 40

/* Protects list of registered TC modules. It is pure SMP lock. */
static DEFINE_RWLOCK(cls_mod_lock);

/* Find classifier type by string name */

41
static const struct tcf_proto_ops *tcf_proto_lookup_ops(struct nlattr *kind)
L
Linus Torvalds 已提交
42
{
43
	const struct tcf_proto_ops *t, *res = NULL;
L
Linus Torvalds 已提交
44 45 46

	if (kind) {
		read_lock(&cls_mod_lock);
47
		list_for_each_entry(t, &tcf_proto_base, head) {
48
			if (nla_strcmp(kind, t->kind) == 0) {
49 50
				if (try_module_get(t->owner))
					res = t;
L
Linus Torvalds 已提交
51 52 53 54 55
				break;
			}
		}
		read_unlock(&cls_mod_lock);
	}
56
	return res;
L
Linus Torvalds 已提交
57 58 59 60 61 62
}

/* Register(unregister) new classifier type */

int register_tcf_proto_ops(struct tcf_proto_ops *ops)
{
63
	struct tcf_proto_ops *t;
L
Linus Torvalds 已提交
64 65 66
	int rc = -EEXIST;

	write_lock(&cls_mod_lock);
67
	list_for_each_entry(t, &tcf_proto_base, head)
L
Linus Torvalds 已提交
68 69 70
		if (!strcmp(ops->kind, t->kind))
			goto out;

71
	list_add_tail(&ops->head, &tcf_proto_base);
L
Linus Torvalds 已提交
72 73 74 75 76
	rc = 0;
out:
	write_unlock(&cls_mod_lock);
	return rc;
}
77
EXPORT_SYMBOL(register_tcf_proto_ops);
L
Linus Torvalds 已提交
78 79 80

int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
{
81
	struct tcf_proto_ops *t;
L
Linus Torvalds 已提交
82 83
	int rc = -ENOENT;

84 85 86 87 88
	/* Wait for outstanding call_rcu()s, if any, from a
	 * tcf_proto_ops's destroy() handler.
	 */
	rcu_barrier();

L
Linus Torvalds 已提交
89
	write_lock(&cls_mod_lock);
90 91 92 93
	list_for_each_entry(t, &tcf_proto_base, head) {
		if (t == ops) {
			list_del(&t->head);
			rc = 0;
L
Linus Torvalds 已提交
94
			break;
95 96
		}
	}
L
Linus Torvalds 已提交
97 98 99
	write_unlock(&cls_mod_lock);
	return rc;
}
100
EXPORT_SYMBOL(unregister_tcf_proto_ops);
L
Linus Torvalds 已提交
101

102 103
static int tfilter_notify(struct net *net, struct sk_buff *oskb,
			  struct nlmsghdr *n, struct tcf_proto *tp,
104
			  unsigned long fh, int event, bool unicast);
L
Linus Torvalds 已提交
105

106 107 108 109 110 111 112 113 114
static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
				 struct nlmsghdr *n,
				 struct tcf_proto __rcu **chain, int event)
{
	struct tcf_proto __rcu **it_chain;
	struct tcf_proto *tp;

	for (it_chain = chain; (tp = rtnl_dereference(*it_chain)) != NULL;
	     it_chain = &tp->next)
115
		tfilter_notify(net, oskb, n, tp, 0, event, false);
116
}
L
Linus Torvalds 已提交
117 118 119

/* Select new prio value from the range, managed by kernel. */

120
static inline u32 tcf_auto_prio(struct tcf_proto *tp)
L
Linus Torvalds 已提交
121
{
122
	u32 first = TC_H_MAKE(0xC0000000U, 0U);
L
Linus Torvalds 已提交
123 124

	if (tp)
E
Eric Dumazet 已提交
125
		first = tp->prio - 1;
L
Linus Torvalds 已提交
126 127 128 129 130 131

	return first;
}

/* Add/change/delete/get a filter node */

132
static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n)
L
Linus Torvalds 已提交
133
{
134
	struct net *net = sock_net(skb->sk);
135
	struct nlattr *tca[TCA_MAX + 1];
L
Linus Torvalds 已提交
136 137 138 139 140 141 142
	struct tcmsg *t;
	u32 protocol;
	u32 prio;
	u32 nprio;
	u32 parent;
	struct net_device *dev;
	struct Qdisc  *q;
J
John Fastabend 已提交
143 144
	struct tcf_proto __rcu **back;
	struct tcf_proto __rcu **chain;
L
Linus Torvalds 已提交
145
	struct tcf_proto *tp;
146
	const struct tcf_proto_ops *tp_ops;
147
	const struct Qdisc_class_ops *cops;
L
Linus Torvalds 已提交
148 149 150
	unsigned long cl;
	unsigned long fh;
	int err;
151
	int tp_created;
L
Linus Torvalds 已提交
152

153
	if ((n->nlmsg_type != RTM_GETTFILTER) &&
154
	    !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
155
		return -EPERM;
156

L
Linus Torvalds 已提交
157
replay:
158 159
	tp_created = 0;

160 161 162 163
	err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL);
	if (err < 0)
		return err;

164
	t = nlmsg_data(n);
L
Linus Torvalds 已提交
165 166 167 168 169 170 171
	protocol = TC_H_MIN(t->tcm_info);
	prio = TC_H_MAJ(t->tcm_info);
	nprio = prio;
	parent = t->tcm_parent;
	cl = 0;

	if (prio == 0) {
172 173
		switch (n->nlmsg_type) {
		case RTM_DELTFILTER:
174
			if (protocol || t->tcm_handle || tca[TCA_KIND])
175 176 177 178 179 180 181 182 183 184 185 186
				return -ENOENT;
			break;
		case RTM_NEWTFILTER:
			/* If no priority is provided by the user,
			 * we allocate one.
			 */
			if (n->nlmsg_flags & NLM_F_CREATE) {
				prio = TC_H_MAKE(0x80000000U, 0U);
				break;
			}
			/* fall-through */
		default:
L
Linus Torvalds 已提交
187
			return -ENOENT;
188
		}
L
Linus Torvalds 已提交
189 190 191 192 193
	}

	/* Find head of filter chain. */

	/* Find link */
194
	dev = __dev_get_by_index(net, t->tcm_ifindex);
195
	if (dev == NULL)
L
Linus Torvalds 已提交
196 197 198 199
		return -ENODEV;

	/* Find qdisc */
	if (!parent) {
200
		q = dev->qdisc;
L
Linus Torvalds 已提交
201
		parent = q->handle;
202 203 204 205 206
	} else {
		q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
		if (q == NULL)
			return -EINVAL;
	}
L
Linus Torvalds 已提交
207 208

	/* Is it classful? */
E
Eric Dumazet 已提交
209 210
	cops = q->ops->cl_ops;
	if (!cops)
L
Linus Torvalds 已提交
211 212
		return -EINVAL;

213 214 215
	if (cops->tcf_chain == NULL)
		return -EOPNOTSUPP;

L
Linus Torvalds 已提交
216 217 218 219 220 221 222 223 224 225 226 227
	/* Do we search for filter, attached to class? */
	if (TC_H_MIN(parent)) {
		cl = cops->get(q, parent);
		if (cl == 0)
			return -ENOENT;
	}

	/* And the last stroke */
	chain = cops->tcf_chain(q, cl);
	err = -EINVAL;
	if (chain == NULL)
		goto errout;
228 229 230 231 232 233
	if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
		tfilter_notify_chain(net, skb, n, chain, RTM_DELTFILTER);
		tcf_destroy_chain(chain);
		err = 0;
		goto errout;
	}
L
Linus Torvalds 已提交
234 235

	/* Check the chain for existence of proto-tcf with this priority */
J
John Fastabend 已提交
236 237 238
	for (back = chain;
	     (tp = rtnl_dereference(*back)) != NULL;
	     back = &tp->next) {
L
Linus Torvalds 已提交
239 240
		if (tp->prio >= prio) {
			if (tp->prio == prio) {
E
Eric Dumazet 已提交
241 242
				if (!nprio ||
				    (tp->protocol != protocol && protocol))
L
Linus Torvalds 已提交
243 244 245 246 247 248 249 250 251 252
					goto errout;
			} else
				tp = NULL;
			break;
		}
	}

	if (tp == NULL) {
		/* Proto-tcf does not exist, create new one */

253
		if (tca[TCA_KIND] == NULL || !protocol)
L
Linus Torvalds 已提交
254 255 256
			goto errout;

		err = -ENOENT;
E
Eric Dumazet 已提交
257 258
		if (n->nlmsg_type != RTM_NEWTFILTER ||
		    !(n->nlmsg_flags & NLM_F_CREATE))
L
Linus Torvalds 已提交
259 260 261 262 263 264
			goto errout;


		/* Create new proto tcf */

		err = -ENOBUFS;
265 266
		tp = kzalloc(sizeof(*tp), GFP_KERNEL);
		if (tp == NULL)
L
Linus Torvalds 已提交
267
			goto errout;
268
		err = -ENOENT;
269
		tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND]);
L
Linus Torvalds 已提交
270
		if (tp_ops == NULL) {
271
#ifdef CONFIG_MODULES
272
			struct nlattr *kind = tca[TCA_KIND];
L
Linus Torvalds 已提交
273 274 275
			char name[IFNAMSIZ];

			if (kind != NULL &&
276
			    nla_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
L
Linus Torvalds 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
				rtnl_unlock();
				request_module("cls_%s", name);
				rtnl_lock();
				tp_ops = tcf_proto_lookup_ops(kind);
				/* We dropped the RTNL semaphore in order to
				 * perform the module load.  So, even if we
				 * succeeded in loading the module we have to
				 * replay the request.  We indicate this using
				 * -EAGAIN.
				 */
				if (tp_ops != NULL) {
					module_put(tp_ops->owner);
					err = -EAGAIN;
				}
			}
#endif
			kfree(tp);
			goto errout;
		}
		tp->ops = tp_ops;
		tp->protocol = protocol;
J
John Fastabend 已提交
298 299
		tp->prio = nprio ? :
			       TC_H_MAJ(tcf_auto_prio(rtnl_dereference(*back)));
L
Linus Torvalds 已提交
300 301 302
		tp->q = q;
		tp->classify = tp_ops->classify;
		tp->classid = parent;
303 304 305

		err = tp_ops->init(tp);
		if (err != 0) {
L
Linus Torvalds 已提交
306 307 308 309 310
			module_put(tp_ops->owner);
			kfree(tp);
			goto errout;
		}

311
		tp_created = 1;
L
Linus Torvalds 已提交
312

313
	} else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind))
L
Linus Torvalds 已提交
314 315 316 317 318 319
		goto errout;

	fh = tp->ops->get(tp, t->tcm_handle);

	if (fh == 0) {
		if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
J
John Fastabend 已提交
320 321 322
			struct tcf_proto *next = rtnl_dereference(tp->next);

			RCU_INIT_POINTER(*back, next);
L
Linus Torvalds 已提交
323

324 325
			tfilter_notify(net, skb, n, tp, fh,
				       RTM_DELTFILTER, false);
326
			tcf_destroy(tp, true);
L
Linus Torvalds 已提交
327 328 329 330 331
			err = 0;
			goto errout;
		}

		err = -ENOENT;
332 333
		if (n->nlmsg_type != RTM_NEWTFILTER ||
		    !(n->nlmsg_flags & NLM_F_CREATE))
L
Linus Torvalds 已提交
334 335 336
			goto errout;
	} else {
		switch (n->nlmsg_type) {
337
		case RTM_NEWTFILTER:
L
Linus Torvalds 已提交
338
			err = -EEXIST;
339 340
			if (n->nlmsg_flags & NLM_F_EXCL) {
				if (tp_created)
341
					tcf_destroy(tp, true);
L
Linus Torvalds 已提交
342
				goto errout;
343
			}
L
Linus Torvalds 已提交
344 345 346
			break;
		case RTM_DELTFILTER:
			err = tp->ops->delete(tp, fh);
347
			if (err == 0) {
348
				struct tcf_proto *next = rtnl_dereference(tp->next);
349

350 351
				tfilter_notify(net, skb, n, tp,
					       t->tcm_handle,
352
					       RTM_DELTFILTER, false);
353
				if (tcf_destroy(tp, false))
354 355
					RCU_INIT_POINTER(*back, next);
			}
L
Linus Torvalds 已提交
356 357
			goto errout;
		case RTM_GETTFILTER:
J
Jamal Hadi Salim 已提交
358
			err = tfilter_notify(net, skb, n, tp, fh,
359
					     RTM_NEWTFILTER, true);
L
Linus Torvalds 已提交
360 361 362 363 364 365 366
			goto errout;
		default:
			err = -EINVAL;
			goto errout;
		}
	}

367 368
	err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
			      n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
369 370
	if (err == 0) {
		if (tp_created) {
J
John Fastabend 已提交
371 372
			RCU_INIT_POINTER(tp->next, rtnl_dereference(*back));
			rcu_assign_pointer(*back, tp);
373
		}
374
		tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER, false);
375 376
	} else {
		if (tp_created)
377
			tcf_destroy(tp, true);
378
	}
L
Linus Torvalds 已提交
379 380 381 382 383 384 385 386 387 388

errout:
	if (cl)
		cops->put(q, cl);
	if (err == -EAGAIN)
		/* Replay the request. */
		goto replay;
	return err;
}

389 390 391
static int tcf_fill_node(struct net *net, struct sk_buff *skb,
			 struct tcf_proto *tp, unsigned long fh, u32 portid,
			 u32 seq, u16 flags, int event)
L
Linus Torvalds 已提交
392 393 394
{
	struct tcmsg *tcm;
	struct nlmsghdr  *nlh;
395
	unsigned char *b = skb_tail_pointer(skb);
L
Linus Torvalds 已提交
396

397
	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
398 399 400
	if (!nlh)
		goto out_nlmsg_trim;
	tcm = nlmsg_data(nlh);
L
Linus Torvalds 已提交
401
	tcm->tcm_family = AF_UNSPEC;
402
	tcm->tcm__pad1 = 0;
J
Jiri Pirko 已提交
403
	tcm->tcm__pad2 = 0;
404
	tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex;
L
Linus Torvalds 已提交
405 406
	tcm->tcm_parent = tp->classid;
	tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
407 408
	if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
		goto nla_put_failure;
L
Linus Torvalds 已提交
409 410 411
	tcm->tcm_handle = fh;
	if (RTM_DELTFILTER != event) {
		tcm->tcm_handle = 0;
412
		if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
413
			goto nla_put_failure;
L
Linus Torvalds 已提交
414
	}
415
	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
L
Linus Torvalds 已提交
416 417
	return skb->len;

418
out_nlmsg_trim:
419
nla_put_failure:
420
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
421 422 423
	return -1;
}

424 425
static int tfilter_notify(struct net *net, struct sk_buff *oskb,
			  struct nlmsghdr *n, struct tcf_proto *tp,
426
			  unsigned long fh, int event, bool unicast)
L
Linus Torvalds 已提交
427 428
{
	struct sk_buff *skb;
429
	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
L
Linus Torvalds 已提交
430 431 432 433 434

	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
	if (!skb)
		return -ENOBUFS;

435 436
	if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq,
			  n->nlmsg_flags, event) <= 0) {
L
Linus Torvalds 已提交
437 438 439 440
		kfree_skb(skb);
		return -EINVAL;
	}

441 442 443
	if (unicast)
		return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);

444
	return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
445
			      n->nlmsg_flags & NLM_F_ECHO);
L
Linus Torvalds 已提交
446 447
}

448
struct tcf_dump_args {
L
Linus Torvalds 已提交
449 450 451 452 453
	struct tcf_walker w;
	struct sk_buff *skb;
	struct netlink_callback *cb;
};

454 455
static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
			 struct tcf_walker *arg)
L
Linus Torvalds 已提交
456
{
457
	struct tcf_dump_args *a = (void *)arg;
458
	struct net *net = sock_net(a->skb->sk);
L
Linus Torvalds 已提交
459

460
	return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid,
J
Jamal Hadi Salim 已提交
461 462
			     a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
			     RTM_NEWTFILTER);
L
Linus Torvalds 已提交
463 464
}

E
Eric Dumazet 已提交
465
/* called with RTNL */
L
Linus Torvalds 已提交
466 467
static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
{
468
	struct net *net = sock_net(skb->sk);
L
Linus Torvalds 已提交
469 470 471 472
	int t;
	int s_t;
	struct net_device *dev;
	struct Qdisc *q;
J
John Fastabend 已提交
473
	struct tcf_proto *tp, __rcu **chain;
474
	struct tcmsg *tcm = nlmsg_data(cb->nlh);
L
Linus Torvalds 已提交
475
	unsigned long cl = 0;
476
	const struct Qdisc_class_ops *cops;
L
Linus Torvalds 已提交
477 478
	struct tcf_dump_args arg;

479
	if (nlmsg_len(cb->nlh) < sizeof(*tcm))
L
Linus Torvalds 已提交
480
		return skb->len;
E
Eric Dumazet 已提交
481 482
	dev = __dev_get_by_index(net, tcm->tcm_ifindex);
	if (!dev)
L
Linus Torvalds 已提交
483 484 485
		return skb->len;

	if (!tcm->tcm_parent)
486
		q = dev->qdisc;
L
Linus Torvalds 已提交
487 488 489 490
	else
		q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
	if (!q)
		goto out;
E
Eric Dumazet 已提交
491 492
	cops = q->ops->cl_ops;
	if (!cops)
L
Linus Torvalds 已提交
493
		goto errout;
494 495
	if (cops->tcf_chain == NULL)
		goto errout;
L
Linus Torvalds 已提交
496 497 498 499 500 501 502 503 504 505 506
	if (TC_H_MIN(tcm->tcm_parent)) {
		cl = cops->get(q, tcm->tcm_parent);
		if (cl == 0)
			goto errout;
	}
	chain = cops->tcf_chain(q, cl);
	if (chain == NULL)
		goto errout;

	s_t = cb->args[0];

J
John Fastabend 已提交
507 508
	for (tp = rtnl_dereference(*chain), t = 0;
	     tp; tp = rtnl_dereference(tp->next), t++) {
E
Eric Dumazet 已提交
509 510
		if (t < s_t)
			continue;
L
Linus Torvalds 已提交
511 512 513 514 515 516 517
		if (TC_H_MAJ(tcm->tcm_info) &&
		    TC_H_MAJ(tcm->tcm_info) != tp->prio)
			continue;
		if (TC_H_MIN(tcm->tcm_info) &&
		    TC_H_MIN(tcm->tcm_info) != tp->protocol)
			continue;
		if (t > s_t)
518 519
			memset(&cb->args[1], 0,
			       sizeof(cb->args)-sizeof(cb->args[0]));
L
Linus Torvalds 已提交
520
		if (cb->args[1] == 0) {
521 522
			if (tcf_fill_node(net, skb, tp, 0,
					  NETLINK_CB(cb->skb).portid,
523 524
					  cb->nlh->nlmsg_seq, NLM_F_MULTI,
					  RTM_NEWTFILTER) <= 0)
L
Linus Torvalds 已提交
525
				break;
526

L
Linus Torvalds 已提交
527 528 529 530 531 532 533 534
			cb->args[1] = 1;
		}
		if (tp->ops->walk == NULL)
			continue;
		arg.w.fn = tcf_node_dump;
		arg.skb = skb;
		arg.cb = cb;
		arg.w.stop = 0;
E
Eric Dumazet 已提交
535
		arg.w.skip = cb->args[1] - 1;
L
Linus Torvalds 已提交
536 537
		arg.w.count = 0;
		tp->ops->walk(tp, &arg.w);
E
Eric Dumazet 已提交
538
		cb->args[1] = arg.w.count + 1;
L
Linus Torvalds 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551
		if (arg.w.stop)
			break;
	}

	cb->args[0] = t;

errout:
	if (cl)
		cops->put(q, cl);
out:
	return skb->len;
}

552
void tcf_exts_destroy(struct tcf_exts *exts)
L
Linus Torvalds 已提交
553 554
{
#ifdef CONFIG_NET_CLS_ACT
555 556 557 558 559 560
	LIST_HEAD(actions);

	tcf_exts_to_list(exts, &actions);
	tcf_action_destroy(&actions, TCA_ACT_UNBIND);
	kfree(exts->actions);
	exts->nr_actions = 0;
L
Linus Torvalds 已提交
561 562
#endif
}
563
EXPORT_SYMBOL(tcf_exts_destroy);
L
Linus Torvalds 已提交
564

565
int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
J
Jamal Hadi Salim 已提交
566
		      struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
L
Linus Torvalds 已提交
567 568 569 570 571
{
#ifdef CONFIG_NET_CLS_ACT
	{
		struct tc_action *act;

572 573
		if (exts->police && tb[exts->police]) {
			act = tcf_action_init_1(net, tb[exts->police], rate_tlv,
J
Jamal Hadi Salim 已提交
574
						"police", ovr, TCA_ACT_BIND);
575 576
			if (IS_ERR(act))
				return PTR_ERR(act);
L
Linus Torvalds 已提交
577

578
			act->type = exts->type = TCA_OLD_COMPAT;
579 580
			exts->actions[0] = act;
			exts->nr_actions = 1;
581
		} else if (exts->action && tb[exts->action]) {
582 583 584
			LIST_HEAD(actions);
			int err, i = 0;

585
			err = tcf_action_init(net, tb[exts->action], rate_tlv,
J
Jamal Hadi Salim 已提交
586 587
					      NULL, ovr, TCA_ACT_BIND,
					      &actions);
588 589
			if (err)
				return err;
590 591 592
			list_for_each_entry(act, &actions, list)
				exts->actions[i++] = act;
			exts->nr_actions = i;
L
Linus Torvalds 已提交
593 594 595
		}
	}
#else
596 597
	if ((exts->action && tb[exts->action]) ||
	    (exts->police && tb[exts->police]))
L
Linus Torvalds 已提交
598 599 600 601 602
		return -EOPNOTSUPP;
#endif

	return 0;
}
603
EXPORT_SYMBOL(tcf_exts_validate);
L
Linus Torvalds 已提交
604

605 606
void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
		     struct tcf_exts *src)
L
Linus Torvalds 已提交
607 608
{
#ifdef CONFIG_NET_CLS_ACT
609 610
	struct tcf_exts old = *dst;

611
	tcf_tree_lock(tp);
612 613
	dst->nr_actions = src->nr_actions;
	dst->actions = src->actions;
614
	dst->type = src->type;
615
	tcf_tree_unlock(tp);
616 617

	tcf_exts_destroy(&old);
L
Linus Torvalds 已提交
618 619
#endif
}
620
EXPORT_SYMBOL(tcf_exts_change);
L
Linus Torvalds 已提交
621

622 623 624 625 626 627 628 629 630
#ifdef CONFIG_NET_CLS_ACT
static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
{
	if (exts->nr_actions == 0)
		return NULL;
	else
		return exts->actions[0];
}
#endif
631

632
int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
L
Linus Torvalds 已提交
633 634
{
#ifdef CONFIG_NET_CLS_ACT
635 636
	struct nlattr *nest;

637
	if (exts->action && exts->nr_actions) {
L
Linus Torvalds 已提交
638 639 640 641 642
		/*
		 * again for backward compatible mode - we want
		 * to work with both old and new modes of entering
		 * tc data even if iproute2  was newer - jhs
		 */
643
		if (exts->type != TCA_OLD_COMPAT) {
644 645
			LIST_HEAD(actions);

646
			nest = nla_nest_start(skb, exts->action);
647 648
			if (nest == NULL)
				goto nla_put_failure;
649 650 651

			tcf_exts_to_list(exts, &actions);
			if (tcf_action_dump(skb, &actions, 0, 0) < 0)
652
				goto nla_put_failure;
653
			nla_nest_end(skb, nest);
654
		} else if (exts->police) {
655
			struct tc_action *act = tcf_exts_first_act(exts);
656
			nest = nla_nest_start(skb, exts->police);
657
			if (nest == NULL || !act)
658
				goto nla_put_failure;
659
			if (tcf_action_dump_old(skb, act, 0, 0) < 0)
660
				goto nla_put_failure;
661
			nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
662 663 664
		}
	}
	return 0;
665 666 667

nla_put_failure:
	nla_nest_cancel(skb, nest);
L
Linus Torvalds 已提交
668
	return -1;
669 670 671
#else
	return 0;
#endif
L
Linus Torvalds 已提交
672
}
673
EXPORT_SYMBOL(tcf_exts_dump);
L
Linus Torvalds 已提交
674

675

676
int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
L
Linus Torvalds 已提交
677 678
{
#ifdef CONFIG_NET_CLS_ACT
679
	struct tc_action *a = tcf_exts_first_act(exts);
680
	if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
681
		return -1;
L
Linus Torvalds 已提交
682 683 684
#endif
	return 0;
}
685
EXPORT_SYMBOL(tcf_exts_dump_stats);
L
Linus Torvalds 已提交
686

687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
		     struct net_device **hw_dev)
{
#ifdef CONFIG_NET_CLS_ACT
	const struct tc_action *a;
	LIST_HEAD(actions);

	if (tc_no_actions(exts))
		return -EINVAL;

	tcf_exts_to_list(exts, &actions);
	list_for_each_entry(a, &actions, list) {
		if (a->ops->get_dev) {
			a->ops->get_dev(a, dev_net(dev), hw_dev);
			break;
		}
	}
	if (*hw_dev)
		return 0;
#endif
	return -EOPNOTSUPP;
}
EXPORT_SYMBOL(tcf_exts_get_dev);

L
Linus Torvalds 已提交
711 712
static int __init tc_filter_init(void)
{
713 714
	rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, NULL);
	rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, NULL);
715
	rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
716
		      tc_dump_tfilter, NULL);
L
Linus Torvalds 已提交
717 718 719 720 721

	return 0;
}

subsys_initcall(tc_filter_init);