cls_api.c 15.4 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 104
static int tfilter_notify(struct net *net, struct sk_buff *oskb,
			  struct nlmsghdr *n, struct tcf_proto *tp,
			  unsigned long fh, int event);
L
Linus Torvalds 已提交
105

106 107 108 109 110 111 112 113 114 115 116
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)
		tfilter_notify(net, oskb, n, tp, 0, event);
}
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 = 0;
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 160 161
	err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL);
	if (err < 0)
		return err;

162
	t = nlmsg_data(n);
L
Linus Torvalds 已提交
163 164 165 166 167 168 169
	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) {
170 171
		switch (n->nlmsg_type) {
		case RTM_DELTFILTER:
172
			if (protocol || t->tcm_handle || tca[TCA_KIND])
173 174 175 176 177 178 179 180 181 182 183 184
				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 已提交
185
			return -ENOENT;
186
		}
L
Linus Torvalds 已提交
187 188 189 190 191
	}

	/* Find head of filter chain. */

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

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

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

211 212 213
	if (cops->tcf_chain == NULL)
		return -EOPNOTSUPP;

L
Linus Torvalds 已提交
214 215 216 217 218 219 220 221 222 223 224 225
	/* 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;
226 227 228 229 230 231
	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 已提交
232 233

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

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

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

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


		/* Create new proto tcf */

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

			if (kind != NULL &&
274
			    nla_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
L
Linus Torvalds 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
				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 已提交
296 297
		tp->prio = nprio ? :
			       TC_H_MAJ(tcf_auto_prio(rtnl_dereference(*back)));
L
Linus Torvalds 已提交
298 299 300
		tp->q = q;
		tp->classify = tp_ops->classify;
		tp->classid = parent;
301 302 303

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

309
		tp_created = 1;
L
Linus Torvalds 已提交
310

311
	} else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind))
L
Linus Torvalds 已提交
312 313 314 315 316 317
		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 已提交
318 319 320
			struct tcf_proto *next = rtnl_dereference(tp->next);

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

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

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

347 348
				tfilter_notify(net, skb, n, tp, fh, RTM_DELTFILTER);
				if (tcf_destroy(tp, false))
349 350
					RCU_INIT_POINTER(*back, next);
			}
L
Linus Torvalds 已提交
351 352
			goto errout;
		case RTM_GETTFILTER:
353
			err = tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER);
L
Linus Torvalds 已提交
354 355 356 357 358 359 360
			goto errout;
		default:
			err = -EINVAL;
			goto errout;
		}
	}

361 362
	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);
363 364
	if (err == 0) {
		if (tp_created) {
J
John Fastabend 已提交
365 366
			RCU_INIT_POINTER(tp->next, rtnl_dereference(*back));
			rcu_assign_pointer(*back, tp);
367
		}
368
		tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER);
369 370
	} else {
		if (tp_created)
371
			tcf_destroy(tp, true);
372
	}
L
Linus Torvalds 已提交
373 374 375 376 377 378 379 380 381 382

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

383 384 385
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 已提交
386 387 388
{
	struct tcmsg *tcm;
	struct nlmsghdr  *nlh;
389
	unsigned char *b = skb_tail_pointer(skb);
L
Linus Torvalds 已提交
390

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

412
out_nlmsg_trim:
413
nla_put_failure:
414
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
415 416 417
	return -1;
}

418 419 420
static int tfilter_notify(struct net *net, struct sk_buff *oskb,
			  struct nlmsghdr *n, struct tcf_proto *tp,
			  unsigned long fh, int event)
L
Linus Torvalds 已提交
421 422
{
	struct sk_buff *skb;
423
	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
L
Linus Torvalds 已提交
424 425 426 427 428

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

429
	if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq, 0, event) <= 0) {
L
Linus Torvalds 已提交
430 431 432 433
		kfree_skb(skb);
		return -EINVAL;
	}

434
	return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
435
			      n->nlmsg_flags & NLM_F_ECHO);
L
Linus Torvalds 已提交
436 437
}

438
struct tcf_dump_args {
L
Linus Torvalds 已提交
439 440 441 442 443
	struct tcf_walker w;
	struct sk_buff *skb;
	struct netlink_callback *cb;
};

444 445
static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
			 struct tcf_walker *arg)
L
Linus Torvalds 已提交
446
{
447
	struct tcf_dump_args *a = (void *)arg;
448
	struct net *net = sock_net(a->skb->sk);
L
Linus Torvalds 已提交
449

450
	return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid,
L
Linus Torvalds 已提交
451 452 453
			     a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER);
}

E
Eric Dumazet 已提交
454
/* called with RTNL */
L
Linus Torvalds 已提交
455 456
static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
{
457
	struct net *net = sock_net(skb->sk);
L
Linus Torvalds 已提交
458 459 460 461
	int t;
	int s_t;
	struct net_device *dev;
	struct Qdisc *q;
J
John Fastabend 已提交
462
	struct tcf_proto *tp, __rcu **chain;
463
	struct tcmsg *tcm = nlmsg_data(cb->nlh);
L
Linus Torvalds 已提交
464
	unsigned long cl = 0;
465
	const struct Qdisc_class_ops *cops;
L
Linus Torvalds 已提交
466 467
	struct tcf_dump_args arg;

468
	if (nlmsg_len(cb->nlh) < sizeof(*tcm))
L
Linus Torvalds 已提交
469
		return skb->len;
E
Eric Dumazet 已提交
470 471
	dev = __dev_get_by_index(net, tcm->tcm_ifindex);
	if (!dev)
L
Linus Torvalds 已提交
472 473 474
		return skb->len;

	if (!tcm->tcm_parent)
475
		q = dev->qdisc;
L
Linus Torvalds 已提交
476 477 478 479
	else
		q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
	if (!q)
		goto out;
E
Eric Dumazet 已提交
480 481
	cops = q->ops->cl_ops;
	if (!cops)
L
Linus Torvalds 已提交
482
		goto errout;
483 484
	if (cops->tcf_chain == NULL)
		goto errout;
L
Linus Torvalds 已提交
485 486 487 488 489 490 491 492 493 494 495
	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 已提交
496 497
	for (tp = rtnl_dereference(*chain), t = 0;
	     tp; tp = rtnl_dereference(tp->next), t++) {
E
Eric Dumazet 已提交
498 499
		if (t < s_t)
			continue;
L
Linus Torvalds 已提交
500 501 502 503 504 505 506
		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)
507 508
			memset(&cb->args[1], 0,
			       sizeof(cb->args)-sizeof(cb->args[0]));
L
Linus Torvalds 已提交
509
		if (cb->args[1] == 0) {
510 511
			if (tcf_fill_node(net, skb, tp, 0,
					  NETLINK_CB(cb->skb).portid,
512 513
					  cb->nlh->nlmsg_seq, NLM_F_MULTI,
					  RTM_NEWTFILTER) <= 0)
L
Linus Torvalds 已提交
514
				break;
515

L
Linus Torvalds 已提交
516 517 518 519 520 521 522 523
			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 已提交
524
		arg.w.skip = cb->args[1] - 1;
L
Linus Torvalds 已提交
525 526
		arg.w.count = 0;
		tp->ops->walk(tp, &arg.w);
E
Eric Dumazet 已提交
527
		cb->args[1] = arg.w.count + 1;
L
Linus Torvalds 已提交
528 529 530 531 532 533 534 535 536 537 538 539 540
		if (arg.w.stop)
			break;
	}

	cb->args[0] = t;

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

541
void tcf_exts_destroy(struct tcf_exts *exts)
L
Linus Torvalds 已提交
542 543
{
#ifdef CONFIG_NET_CLS_ACT
544 545 546 547 548 549
	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 已提交
550 551
#endif
}
552
EXPORT_SYMBOL(tcf_exts_destroy);
L
Linus Torvalds 已提交
553

554
int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
555
		  struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
L
Linus Torvalds 已提交
556 557 558 559 560
{
#ifdef CONFIG_NET_CLS_ACT
	{
		struct tc_action *act;

561 562
		if (exts->police && tb[exts->police]) {
			act = tcf_action_init_1(net, tb[exts->police], rate_tlv,
563
						"police", ovr,
564 565 566
						TCA_ACT_BIND);
			if (IS_ERR(act))
				return PTR_ERR(act);
L
Linus Torvalds 已提交
567

568
			act->type = exts->type = TCA_OLD_COMPAT;
569 570
			exts->actions[0] = act;
			exts->nr_actions = 1;
571
		} else if (exts->action && tb[exts->action]) {
572 573 574
			LIST_HEAD(actions);
			int err, i = 0;

575
			err = tcf_action_init(net, tb[exts->action], rate_tlv,
576
					      NULL, ovr,
577
					      TCA_ACT_BIND, &actions);
578 579
			if (err)
				return err;
580 581 582
			list_for_each_entry(act, &actions, list)
				exts->actions[i++] = act;
			exts->nr_actions = i;
L
Linus Torvalds 已提交
583 584 585
		}
	}
#else
586 587
	if ((exts->action && tb[exts->action]) ||
	    (exts->police && tb[exts->police]))
L
Linus Torvalds 已提交
588 589 590 591 592
		return -EOPNOTSUPP;
#endif

	return 0;
}
593
EXPORT_SYMBOL(tcf_exts_validate);
L
Linus Torvalds 已提交
594

595 596
void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
		     struct tcf_exts *src)
L
Linus Torvalds 已提交
597 598
{
#ifdef CONFIG_NET_CLS_ACT
599 600
	struct tcf_exts old = *dst;

601
	tcf_tree_lock(tp);
602 603
	dst->nr_actions = src->nr_actions;
	dst->actions = src->actions;
604
	dst->type = src->type;
605
	tcf_tree_unlock(tp);
606 607

	tcf_exts_destroy(&old);
L
Linus Torvalds 已提交
608 609
#endif
}
610
EXPORT_SYMBOL(tcf_exts_change);
L
Linus Torvalds 已提交
611

612 613 614 615 616 617 618 619 620
#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
621

622
int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
L
Linus Torvalds 已提交
623 624
{
#ifdef CONFIG_NET_CLS_ACT
625 626
	struct nlattr *nest;

627
	if (exts->action && exts->nr_actions) {
L
Linus Torvalds 已提交
628 629 630 631 632
		/*
		 * 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
		 */
633
		if (exts->type != TCA_OLD_COMPAT) {
634 635
			LIST_HEAD(actions);

636
			nest = nla_nest_start(skb, exts->action);
637 638
			if (nest == NULL)
				goto nla_put_failure;
639 640 641

			tcf_exts_to_list(exts, &actions);
			if (tcf_action_dump(skb, &actions, 0, 0) < 0)
642
				goto nla_put_failure;
643
			nla_nest_end(skb, nest);
644
		} else if (exts->police) {
645
			struct tc_action *act = tcf_exts_first_act(exts);
646
			nest = nla_nest_start(skb, exts->police);
647
			if (nest == NULL || !act)
648
				goto nla_put_failure;
649
			if (tcf_action_dump_old(skb, act, 0, 0) < 0)
650
				goto nla_put_failure;
651
			nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
652 653 654
		}
	}
	return 0;
655 656 657

nla_put_failure:
	nla_nest_cancel(skb, nest);
L
Linus Torvalds 已提交
658
	return -1;
659 660 661
#else
	return 0;
#endif
L
Linus Torvalds 已提交
662
}
663
EXPORT_SYMBOL(tcf_exts_dump);
L
Linus Torvalds 已提交
664

665

666
int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
L
Linus Torvalds 已提交
667 668
{
#ifdef CONFIG_NET_CLS_ACT
669
	struct tc_action *a = tcf_exts_first_act(exts);
670
	if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
671
		return -1;
L
Linus Torvalds 已提交
672 673 674
#endif
	return 0;
}
675
EXPORT_SYMBOL(tcf_exts_dump_stats);
L
Linus Torvalds 已提交
676 677 678

static int __init tc_filter_init(void)
{
679 680
	rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, NULL);
	rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, NULL);
681
	rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
682
		      tc_dump_tfilter, NULL);
L
Linus Torvalds 已提交
683 684 685 686 687

	return 0;
}

subsys_initcall(tc_filter_init);