cls_api.c 13.9 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 84
	int rc = -ENOENT;

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

97 98 99
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 已提交
100 101 102 103


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

104
static inline u32 tcf_auto_prio(struct tcf_proto *tp)
L
Linus Torvalds 已提交
105
{
106
	u32 first = TC_H_MAKE(0xC0000000U, 0U);
L
Linus Torvalds 已提交
107 108

	if (tp)
E
Eric Dumazet 已提交
109
		first = tp->prio - 1;
L
Linus Torvalds 已提交
110 111 112 113 114 115

	return first;
}

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

116
static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n)
L
Linus Torvalds 已提交
117
{
118
	struct net *net = sock_net(skb->sk);
119
	struct nlattr *tca[TCA_MAX + 1];
120
	spinlock_t *root_lock;
L
Linus Torvalds 已提交
121 122 123 124 125 126 127 128 129
	struct tcmsg *t;
	u32 protocol;
	u32 prio;
	u32 nprio;
	u32 parent;
	struct net_device *dev;
	struct Qdisc  *q;
	struct tcf_proto **back, **chain;
	struct tcf_proto *tp;
130
	const struct tcf_proto_ops *tp_ops;
131
	const struct Qdisc_class_ops *cops;
L
Linus Torvalds 已提交
132 133 134
	unsigned long cl;
	unsigned long fh;
	int err;
135
	int tp_created = 0;
L
Linus Torvalds 已提交
136

137 138
	if ((n->nlmsg_type != RTM_GETTFILTER) && !capable(CAP_NET_ADMIN))
		return -EPERM;
139

L
Linus Torvalds 已提交
140
replay:
141 142 143 144
	err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL);
	if (err < 0)
		return err;

145
	t = nlmsg_data(n);
L
Linus Torvalds 已提交
146 147 148 149 150 151 152 153
	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) {
		/* If no priority is given, user wants we allocated it. */
E
Eric Dumazet 已提交
154 155
		if (n->nlmsg_type != RTM_NEWTFILTER ||
		    !(n->nlmsg_flags & NLM_F_CREATE))
L
Linus Torvalds 已提交
156
			return -ENOENT;
157
		prio = TC_H_MAKE(0x80000000U, 0U);
L
Linus Torvalds 已提交
158 159 160 161 162
	}

	/* Find head of filter chain. */

	/* Find link */
163
	dev = __dev_get_by_index(net, t->tcm_ifindex);
164
	if (dev == NULL)
L
Linus Torvalds 已提交
165 166 167 168
		return -ENODEV;

	/* Find qdisc */
	if (!parent) {
169
		q = dev->qdisc;
L
Linus Torvalds 已提交
170
		parent = q->handle;
171 172 173 174 175
	} else {
		q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
		if (q == NULL)
			return -EINVAL;
	}
L
Linus Torvalds 已提交
176 177

	/* Is it classful? */
E
Eric Dumazet 已提交
178 179
	cops = q->ops->cl_ops;
	if (!cops)
L
Linus Torvalds 已提交
180 181
		return -EINVAL;

182 183 184
	if (cops->tcf_chain == NULL)
		return -EOPNOTSUPP;

L
Linus Torvalds 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198
	/* 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;

	/* Check the chain for existence of proto-tcf with this priority */
E
Eric Dumazet 已提交
199
	for (back = chain; (tp = *back) != NULL; back = &tp->next) {
L
Linus Torvalds 已提交
200 201
		if (tp->prio >= prio) {
			if (tp->prio == prio) {
E
Eric Dumazet 已提交
202 203
				if (!nprio ||
				    (tp->protocol != protocol && protocol))
L
Linus Torvalds 已提交
204 205 206 207 208 209 210
					goto errout;
			} else
				tp = NULL;
			break;
		}
	}

211
	root_lock = qdisc_root_sleeping_lock(q);
212

L
Linus Torvalds 已提交
213 214 215
	if (tp == NULL) {
		/* Proto-tcf does not exist, create new one */

216
		if (tca[TCA_KIND] == NULL || !protocol)
L
Linus Torvalds 已提交
217 218 219
			goto errout;

		err = -ENOENT;
E
Eric Dumazet 已提交
220 221
		if (n->nlmsg_type != RTM_NEWTFILTER ||
		    !(n->nlmsg_flags & NLM_F_CREATE))
L
Linus Torvalds 已提交
222 223 224 225 226 227
			goto errout;


		/* Create new proto tcf */

		err = -ENOBUFS;
228 229
		tp = kzalloc(sizeof(*tp), GFP_KERNEL);
		if (tp == NULL)
L
Linus Torvalds 已提交
230
			goto errout;
231
		err = -ENOENT;
232
		tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND]);
L
Linus Torvalds 已提交
233
		if (tp_ops == NULL) {
234
#ifdef CONFIG_MODULES
235
			struct nlattr *kind = tca[TCA_KIND];
L
Linus Torvalds 已提交
236 237 238
			char name[IFNAMSIZ];

			if (kind != NULL &&
239
			    nla_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
L
Linus Torvalds 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
				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;
261
		tp->prio = nprio ? : TC_H_MAJ(tcf_auto_prio(*back));
L
Linus Torvalds 已提交
262 263 264
		tp->q = q;
		tp->classify = tp_ops->classify;
		tp->classid = parent;
265 266 267

		err = tp_ops->init(tp);
		if (err != 0) {
L
Linus Torvalds 已提交
268 269 270 271 272
			module_put(tp_ops->owner);
			kfree(tp);
			goto errout;
		}

273
		tp_created = 1;
L
Linus Torvalds 已提交
274

275
	} else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind))
L
Linus Torvalds 已提交
276 277 278 279 280 281
		goto errout;

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

	if (fh == 0) {
		if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
282
			spin_lock_bh(root_lock);
L
Linus Torvalds 已提交
283
			*back = tp->next;
284
			spin_unlock_bh(root_lock);
L
Linus Torvalds 已提交
285

286
			tfilter_notify(net, skb, n, tp, fh, RTM_DELTFILTER);
L
Linus Torvalds 已提交
287 288 289 290 291 292
			tcf_destroy(tp);
			err = 0;
			goto errout;
		}

		err = -ENOENT;
293 294
		if (n->nlmsg_type != RTM_NEWTFILTER ||
		    !(n->nlmsg_flags & NLM_F_CREATE))
L
Linus Torvalds 已提交
295 296 297
			goto errout;
	} else {
		switch (n->nlmsg_type) {
298
		case RTM_NEWTFILTER:
L
Linus Torvalds 已提交
299
			err = -EEXIST;
300 301 302
			if (n->nlmsg_flags & NLM_F_EXCL) {
				if (tp_created)
					tcf_destroy(tp);
L
Linus Torvalds 已提交
303
				goto errout;
304
			}
L
Linus Torvalds 已提交
305 306 307 308
			break;
		case RTM_DELTFILTER:
			err = tp->ops->delete(tp, fh);
			if (err == 0)
309
				tfilter_notify(net, skb, n, tp, fh, RTM_DELTFILTER);
L
Linus Torvalds 已提交
310 311
			goto errout;
		case RTM_GETTFILTER:
312
			err = tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER);
L
Linus Torvalds 已提交
313 314 315 316 317 318 319
			goto errout;
		default:
			err = -EINVAL;
			goto errout;
		}
	}

320
	err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh);
321 322 323 324 325 326 327
	if (err == 0) {
		if (tp_created) {
			spin_lock_bh(root_lock);
			tp->next = *back;
			*back = tp;
			spin_unlock_bh(root_lock);
		}
328
		tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER);
329 330 331 332
	} else {
		if (tp_created)
			tcf_destroy(tp);
	}
L
Linus Torvalds 已提交
333 334 335 336 337 338 339 340 341 342

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

343
static int tcf_fill_node(struct sk_buff *skb, struct tcf_proto *tp,
344
			 unsigned long fh, u32 portid, u32 seq, u16 flags, int event)
L
Linus Torvalds 已提交
345 346 347
{
	struct tcmsg *tcm;
	struct nlmsghdr  *nlh;
348
	unsigned char *b = skb_tail_pointer(skb);
L
Linus Torvalds 已提交
349

350
	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
351 352 353
	if (!nlh)
		goto out_nlmsg_trim;
	tcm = nlmsg_data(nlh);
L
Linus Torvalds 已提交
354
	tcm->tcm_family = AF_UNSPEC;
355
	tcm->tcm__pad1 = 0;
J
Jiri Pirko 已提交
356
	tcm->tcm__pad2 = 0;
357
	tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex;
L
Linus Torvalds 已提交
358 359
	tcm->tcm_parent = tp->classid;
	tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
360 361
	if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
		goto nla_put_failure;
L
Linus Torvalds 已提交
362 363 364 365
	tcm->tcm_handle = fh;
	if (RTM_DELTFILTER != event) {
		tcm->tcm_handle = 0;
		if (tp->ops->dump && tp->ops->dump(tp, fh, skb, tcm) < 0)
366
			goto nla_put_failure;
L
Linus Torvalds 已提交
367
	}
368
	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
L
Linus Torvalds 已提交
369 370
	return skb->len;

371
out_nlmsg_trim:
372
nla_put_failure:
373
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
374 375 376
	return -1;
}

377 378 379
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 已提交
380 381
{
	struct sk_buff *skb;
382
	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
L
Linus Torvalds 已提交
383 384 385 386 387

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

388
	if (tcf_fill_node(skb, tp, fh, portid, n->nlmsg_seq, 0, event) <= 0) {
L
Linus Torvalds 已提交
389 390 391 392
		kfree_skb(skb);
		return -EINVAL;
	}

393
	return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
394
			      n->nlmsg_flags & NLM_F_ECHO);
L
Linus Torvalds 已提交
395 396
}

397
struct tcf_dump_args {
L
Linus Torvalds 已提交
398 399 400 401 402
	struct tcf_walker w;
	struct sk_buff *skb;
	struct netlink_callback *cb;
};

403 404
static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
			 struct tcf_walker *arg)
L
Linus Torvalds 已提交
405
{
406
	struct tcf_dump_args *a = (void *)arg;
L
Linus Torvalds 已提交
407

408
	return tcf_fill_node(a->skb, tp, n, NETLINK_CB(a->cb->skb).portid,
L
Linus Torvalds 已提交
409 410 411
			     a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER);
}

E
Eric Dumazet 已提交
412
/* called with RTNL */
L
Linus Torvalds 已提交
413 414
static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
{
415
	struct net *net = sock_net(skb->sk);
L
Linus Torvalds 已提交
416 417 418 419 420
	int t;
	int s_t;
	struct net_device *dev;
	struct Qdisc *q;
	struct tcf_proto *tp, **chain;
421
	struct tcmsg *tcm = nlmsg_data(cb->nlh);
L
Linus Torvalds 已提交
422
	unsigned long cl = 0;
423
	const struct Qdisc_class_ops *cops;
L
Linus Torvalds 已提交
424 425
	struct tcf_dump_args arg;

426
	if (nlmsg_len(cb->nlh) < sizeof(*tcm))
L
Linus Torvalds 已提交
427
		return skb->len;
E
Eric Dumazet 已提交
428 429
	dev = __dev_get_by_index(net, tcm->tcm_ifindex);
	if (!dev)
L
Linus Torvalds 已提交
430 431 432
		return skb->len;

	if (!tcm->tcm_parent)
433
		q = dev->qdisc;
L
Linus Torvalds 已提交
434 435 436 437
	else
		q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
	if (!q)
		goto out;
E
Eric Dumazet 已提交
438 439
	cops = q->ops->cl_ops;
	if (!cops)
L
Linus Torvalds 已提交
440
		goto errout;
441 442
	if (cops->tcf_chain == NULL)
		goto errout;
L
Linus Torvalds 已提交
443 444 445 446 447 448 449 450 451 452 453
	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];

E
Eric Dumazet 已提交
454 455 456
	for (tp = *chain, t = 0; tp; tp = tp->next, t++) {
		if (t < s_t)
			continue;
L
Linus Torvalds 已提交
457 458 459 460 461 462 463 464 465
		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)
			memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
		if (cb->args[1] == 0) {
466
			if (tcf_fill_node(skb, tp, 0, NETLINK_CB(cb->skb).portid,
467 468
					  cb->nlh->nlmsg_seq, NLM_F_MULTI,
					  RTM_NEWTFILTER) <= 0)
L
Linus Torvalds 已提交
469
				break;
470

L
Linus Torvalds 已提交
471 472 473 474 475 476 477 478
			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 已提交
479
		arg.w.skip = cb->args[1] - 1;
L
Linus Torvalds 已提交
480 481
		arg.w.count = 0;
		tp->ops->walk(tp, &arg.w);
E
Eric Dumazet 已提交
482
		cb->args[1] = arg.w.count + 1;
L
Linus Torvalds 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495
		if (arg.w.stop)
			break;
	}

	cb->args[0] = t;

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

496
void tcf_exts_destroy(struct tcf_proto *tp, struct tcf_exts *exts)
L
Linus Torvalds 已提交
497 498
{
#ifdef CONFIG_NET_CLS_ACT
499 500
	tcf_action_destroy(&exts->actions, TCA_ACT_UNBIND);
	INIT_LIST_HEAD(&exts->actions);
L
Linus Torvalds 已提交
501 502
#endif
}
503
EXPORT_SYMBOL(tcf_exts_destroy);
L
Linus Torvalds 已提交
504

505
int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
506
		  struct nlattr *rate_tlv, struct tcf_exts *exts)
L
Linus Torvalds 已提交
507 508 509 510 511
{
#ifdef CONFIG_NET_CLS_ACT
	{
		struct tc_action *act;

512
		INIT_LIST_HEAD(&exts->actions);
513 514
		if (exts->police && tb[exts->police]) {
			act = tcf_action_init_1(net, tb[exts->police], rate_tlv,
515
						"police", TCA_ACT_NOREPLACE,
516 517 518
						TCA_ACT_BIND);
			if (IS_ERR(act))
				return PTR_ERR(act);
L
Linus Torvalds 已提交
519

520 521
			act->type = exts->type = TCA_OLD_COMPAT;
			list_add(&act->list, &exts->actions);
522
		} else if (exts->action && tb[exts->action]) {
523
			int err;
524
			err = tcf_action_init(net, tb[exts->action], rate_tlv,
525
					      NULL, TCA_ACT_NOREPLACE,
526 527 528
					      TCA_ACT_BIND, &exts->actions);
			if (err)
				return err;
L
Linus Torvalds 已提交
529 530 531
		}
	}
#else
532 533
	if ((exts->action && tb[exts->action]) ||
	    (exts->police && tb[exts->police]))
L
Linus Torvalds 已提交
534 535 536 537 538
		return -EOPNOTSUPP;
#endif

	return 0;
}
539
EXPORT_SYMBOL(tcf_exts_validate);
L
Linus Torvalds 已提交
540

541 542
void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
		     struct tcf_exts *src)
L
Linus Torvalds 已提交
543 544
{
#ifdef CONFIG_NET_CLS_ACT
545 546
	if (!list_empty(&src->actions)) {
		LIST_HEAD(tmp);
L
Linus Torvalds 已提交
547
		tcf_tree_lock(tp);
548 549
		list_splice_init(&dst->actions, &tmp);
		list_splice(&src->actions, &dst->actions);
L
Linus Torvalds 已提交
550
		tcf_tree_unlock(tp);
551
		tcf_action_destroy(&tmp, TCA_ACT_UNBIND);
L
Linus Torvalds 已提交
552 553 554
	}
#endif
}
555
EXPORT_SYMBOL(tcf_exts_change);
L
Linus Torvalds 已提交
556

557 558 559
#define tcf_exts_first_act(ext) \
		list_first_entry(&(exts)->actions, struct tc_action, list)

560
int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
L
Linus Torvalds 已提交
561 562
{
#ifdef CONFIG_NET_CLS_ACT
563
	if (exts->action && !list_empty(&exts->actions)) {
L
Linus Torvalds 已提交
564 565 566 567 568
		/*
		 * 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
		 */
569
		struct nlattr *nest;
570
		if (exts->type != TCA_OLD_COMPAT) {
571
			nest = nla_nest_start(skb, exts->action);
572 573
			if (nest == NULL)
				goto nla_put_failure;
574
			if (tcf_action_dump(skb, &exts->actions, 0, 0) < 0)
575
				goto nla_put_failure;
576
			nla_nest_end(skb, nest);
577
		} else if (exts->police) {
578
			struct tc_action *act = tcf_exts_first_act(exts);
579
			nest = nla_nest_start(skb, exts->police);
580 581
			if (nest == NULL)
				goto nla_put_failure;
582
			if (tcf_action_dump_old(skb, act, 0, 0) < 0)
583
				goto nla_put_failure;
584
			nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
585 586 587 588
		}
	}
#endif
	return 0;
589
nla_put_failure: __attribute__ ((unused))
L
Linus Torvalds 已提交
590 591
	return -1;
}
592
EXPORT_SYMBOL(tcf_exts_dump);
L
Linus Torvalds 已提交
593

594

595
int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
L
Linus Torvalds 已提交
596 597
{
#ifdef CONFIG_NET_CLS_ACT
598 599 600
	struct tc_action *a = tcf_exts_first_act(exts);
	if (tcf_action_copy_stats(skb, a, 1) < 0)
		return -1;
L
Linus Torvalds 已提交
601 602 603
#endif
	return 0;
}
604
EXPORT_SYMBOL(tcf_exts_dump_stats);
L
Linus Torvalds 已提交
605 606 607

static int __init tc_filter_init(void)
{
608 609
	rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, NULL);
	rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, NULL);
610
	rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
611
		      tc_dump_tfilter, NULL);
L
Linus Torvalds 已提交
612 613 614 615 616

	return 0;
}

subsys_initcall(tc_filter_init);