cls_api.c 14.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


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

109
static inline u32 tcf_auto_prio(struct tcf_proto *tp)
L
Linus Torvalds 已提交
110
{
111
	u32 first = TC_H_MAKE(0xC0000000U, 0U);
L
Linus Torvalds 已提交
112 113

	if (tp)
E
Eric Dumazet 已提交
114
		first = tp->prio - 1;
L
Linus Torvalds 已提交
115 116 117 118 119 120

	return first;
}

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

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

142
	if ((n->nlmsg_type != RTM_GETTFILTER) &&
143
	    !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
144
		return -EPERM;
145

L
Linus Torvalds 已提交
146
replay:
147 148 149 150
	err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL);
	if (err < 0)
		return err;

151
	t = nlmsg_data(n);
L
Linus Torvalds 已提交
152 153 154 155 156 157 158 159
	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 已提交
160 161
		if (n->nlmsg_type != RTM_NEWTFILTER ||
		    !(n->nlmsg_flags & NLM_F_CREATE))
L
Linus Torvalds 已提交
162
			return -ENOENT;
163
		prio = TC_H_MAKE(0x80000000U, 0U);
L
Linus Torvalds 已提交
164 165 166 167 168
	}

	/* Find head of filter chain. */

	/* Find link */
169
	dev = __dev_get_by_index(net, t->tcm_ifindex);
170
	if (dev == NULL)
L
Linus Torvalds 已提交
171 172 173 174
		return -ENODEV;

	/* Find qdisc */
	if (!parent) {
175
		q = dev->qdisc;
L
Linus Torvalds 已提交
176
		parent = q->handle;
177 178 179 180 181
	} else {
		q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
		if (q == NULL)
			return -EINVAL;
	}
L
Linus Torvalds 已提交
182 183

	/* Is it classful? */
E
Eric Dumazet 已提交
184 185
	cops = q->ops->cl_ops;
	if (!cops)
L
Linus Torvalds 已提交
186 187
		return -EINVAL;

188 189 190
	if (cops->tcf_chain == NULL)
		return -EOPNOTSUPP;

L
Linus Torvalds 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204
	/* 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 */
J
John Fastabend 已提交
205 206 207
	for (back = chain;
	     (tp = rtnl_dereference(*back)) != NULL;
	     back = &tp->next) {
L
Linus Torvalds 已提交
208 209
		if (tp->prio >= prio) {
			if (tp->prio == prio) {
E
Eric Dumazet 已提交
210 211
				if (!nprio ||
				    (tp->protocol != protocol && protocol))
L
Linus Torvalds 已提交
212 213 214 215 216 217 218 219 220 221
					goto errout;
			} else
				tp = NULL;
			break;
		}
	}

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

222
		if (tca[TCA_KIND] == NULL || !protocol)
L
Linus Torvalds 已提交
223 224 225
			goto errout;

		err = -ENOENT;
E
Eric Dumazet 已提交
226 227
		if (n->nlmsg_type != RTM_NEWTFILTER ||
		    !(n->nlmsg_flags & NLM_F_CREATE))
L
Linus Torvalds 已提交
228 229 230 231 232 233
			goto errout;


		/* Create new proto tcf */

		err = -ENOBUFS;
234 235
		tp = kzalloc(sizeof(*tp), GFP_KERNEL);
		if (tp == NULL)
L
Linus Torvalds 已提交
236
			goto errout;
237
		err = -ENOENT;
238
		tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND]);
L
Linus Torvalds 已提交
239
		if (tp_ops == NULL) {
240
#ifdef CONFIG_MODULES
241
			struct nlattr *kind = tca[TCA_KIND];
L
Linus Torvalds 已提交
242 243 244
			char name[IFNAMSIZ];

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

		err = tp_ops->init(tp);
		if (err != 0) {
L
Linus Torvalds 已提交
275 276 277 278 279
			module_put(tp_ops->owner);
			kfree(tp);
			goto errout;
		}

280
		tp_created = 1;
L
Linus Torvalds 已提交
281

282
	} else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind))
L
Linus Torvalds 已提交
283 284 285 286 287 288
		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 已提交
289 290 291
			struct tcf_proto *next = rtnl_dereference(tp->next);

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

293
			tfilter_notify(net, skb, n, tp, fh, RTM_DELTFILTER);
294
			tcf_destroy(tp, true);
L
Linus Torvalds 已提交
295 296 297 298 299
			err = 0;
			goto errout;
		}

		err = -ENOENT;
300 301
		if (n->nlmsg_type != RTM_NEWTFILTER ||
		    !(n->nlmsg_flags & NLM_F_CREATE))
L
Linus Torvalds 已提交
302 303 304
			goto errout;
	} else {
		switch (n->nlmsg_type) {
305
		case RTM_NEWTFILTER:
L
Linus Torvalds 已提交
306
			err = -EEXIST;
307 308
			if (n->nlmsg_flags & NLM_F_EXCL) {
				if (tp_created)
309
					tcf_destroy(tp, true);
L
Linus Torvalds 已提交
310
				goto errout;
311
			}
L
Linus Torvalds 已提交
312 313 314
			break;
		case RTM_DELTFILTER:
			err = tp->ops->delete(tp, fh);
315
			if (err == 0) {
316
				struct tcf_proto *next = rtnl_dereference(tp->next);
317

318 319
				tfilter_notify(net, skb, n, tp, fh, RTM_DELTFILTER);
				if (tcf_destroy(tp, false))
320 321
					RCU_INIT_POINTER(*back, next);
			}
L
Linus Torvalds 已提交
322 323
			goto errout;
		case RTM_GETTFILTER:
324
			err = tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER);
L
Linus Torvalds 已提交
325 326 327 328 329 330 331
			goto errout;
		default:
			err = -EINVAL;
			goto errout;
		}
	}

332 333
	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);
334 335
	if (err == 0) {
		if (tp_created) {
J
John Fastabend 已提交
336 337
			RCU_INIT_POINTER(tp->next, rtnl_dereference(*back));
			rcu_assign_pointer(*back, tp);
338
		}
339
		tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER);
340 341
	} else {
		if (tp_created)
342
			tcf_destroy(tp, true);
343
	}
L
Linus Torvalds 已提交
344 345 346 347 348 349 350 351 352 353

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

354
static int tcf_fill_node(struct net *net, struct sk_buff *skb, struct tcf_proto *tp,
355
			 unsigned long fh, u32 portid, u32 seq, u16 flags, int event)
L
Linus Torvalds 已提交
356 357 358
{
	struct tcmsg *tcm;
	struct nlmsghdr  *nlh;
359
	unsigned char *b = skb_tail_pointer(skb);
L
Linus Torvalds 已提交
360

361
	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
362 363 364
	if (!nlh)
		goto out_nlmsg_trim;
	tcm = nlmsg_data(nlh);
L
Linus Torvalds 已提交
365
	tcm->tcm_family = AF_UNSPEC;
366
	tcm->tcm__pad1 = 0;
J
Jiri Pirko 已提交
367
	tcm->tcm__pad2 = 0;
368
	tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex;
L
Linus Torvalds 已提交
369 370
	tcm->tcm_parent = tp->classid;
	tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
371 372
	if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
		goto nla_put_failure;
L
Linus Torvalds 已提交
373 374 375
	tcm->tcm_handle = fh;
	if (RTM_DELTFILTER != event) {
		tcm->tcm_handle = 0;
376
		if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
377
			goto nla_put_failure;
L
Linus Torvalds 已提交
378
	}
379
	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
L
Linus Torvalds 已提交
380 381
	return skb->len;

382
out_nlmsg_trim:
383
nla_put_failure:
384
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
385 386 387
	return -1;
}

388 389 390
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 已提交
391 392
{
	struct sk_buff *skb;
393
	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
L
Linus Torvalds 已提交
394 395 396 397 398

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

399
	if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq, 0, event) <= 0) {
L
Linus Torvalds 已提交
400 401 402 403
		kfree_skb(skb);
		return -EINVAL;
	}

404
	return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
405
			      n->nlmsg_flags & NLM_F_ECHO);
L
Linus Torvalds 已提交
406 407
}

408
struct tcf_dump_args {
L
Linus Torvalds 已提交
409 410 411 412 413
	struct tcf_walker w;
	struct sk_buff *skb;
	struct netlink_callback *cb;
};

414 415
static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
			 struct tcf_walker *arg)
L
Linus Torvalds 已提交
416
{
417
	struct tcf_dump_args *a = (void *)arg;
418
	struct net *net = sock_net(a->skb->sk);
L
Linus Torvalds 已提交
419

420
	return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid,
L
Linus Torvalds 已提交
421 422 423
			     a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER);
}

E
Eric Dumazet 已提交
424
/* called with RTNL */
L
Linus Torvalds 已提交
425 426
static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
{
427
	struct net *net = sock_net(skb->sk);
L
Linus Torvalds 已提交
428 429 430 431
	int t;
	int s_t;
	struct net_device *dev;
	struct Qdisc *q;
J
John Fastabend 已提交
432
	struct tcf_proto *tp, __rcu **chain;
433
	struct tcmsg *tcm = nlmsg_data(cb->nlh);
L
Linus Torvalds 已提交
434
	unsigned long cl = 0;
435
	const struct Qdisc_class_ops *cops;
L
Linus Torvalds 已提交
436 437
	struct tcf_dump_args arg;

438
	if (nlmsg_len(cb->nlh) < sizeof(*tcm))
L
Linus Torvalds 已提交
439
		return skb->len;
E
Eric Dumazet 已提交
440 441
	dev = __dev_get_by_index(net, tcm->tcm_ifindex);
	if (!dev)
L
Linus Torvalds 已提交
442 443 444
		return skb->len;

	if (!tcm->tcm_parent)
445
		q = dev->qdisc;
L
Linus Torvalds 已提交
446 447 448 449
	else
		q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
	if (!q)
		goto out;
E
Eric Dumazet 已提交
450 451
	cops = q->ops->cl_ops;
	if (!cops)
L
Linus Torvalds 已提交
452
		goto errout;
453 454
	if (cops->tcf_chain == NULL)
		goto errout;
L
Linus Torvalds 已提交
455 456 457 458 459 460 461 462 463 464 465
	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 已提交
466 467
	for (tp = rtnl_dereference(*chain), t = 0;
	     tp; tp = rtnl_dereference(tp->next), t++) {
E
Eric Dumazet 已提交
468 469
		if (t < s_t)
			continue;
L
Linus Torvalds 已提交
470 471 472 473 474 475 476 477 478
		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) {
479
			if (tcf_fill_node(net, skb, tp, 0, NETLINK_CB(cb->skb).portid,
480 481
					  cb->nlh->nlmsg_seq, NLM_F_MULTI,
					  RTM_NEWTFILTER) <= 0)
L
Linus Torvalds 已提交
482
				break;
483

L
Linus Torvalds 已提交
484 485 486 487 488 489 490 491
			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 已提交
492
		arg.w.skip = cb->args[1] - 1;
L
Linus Torvalds 已提交
493 494
		arg.w.count = 0;
		tp->ops->walk(tp, &arg.w);
E
Eric Dumazet 已提交
495
		cb->args[1] = arg.w.count + 1;
L
Linus Torvalds 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508
		if (arg.w.stop)
			break;
	}

	cb->args[0] = t;

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

509
void tcf_exts_destroy(struct tcf_exts *exts)
L
Linus Torvalds 已提交
510 511
{
#ifdef CONFIG_NET_CLS_ACT
512 513
	tcf_action_destroy(&exts->actions, TCA_ACT_UNBIND);
	INIT_LIST_HEAD(&exts->actions);
L
Linus Torvalds 已提交
514 515
#endif
}
516
EXPORT_SYMBOL(tcf_exts_destroy);
L
Linus Torvalds 已提交
517

518
int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
519
		  struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
L
Linus Torvalds 已提交
520 521 522 523 524
{
#ifdef CONFIG_NET_CLS_ACT
	{
		struct tc_action *act;

525
		INIT_LIST_HEAD(&exts->actions);
526 527
		if (exts->police && tb[exts->police]) {
			act = tcf_action_init_1(net, tb[exts->police], rate_tlv,
528
						"police", ovr,
529 530 531
						TCA_ACT_BIND);
			if (IS_ERR(act))
				return PTR_ERR(act);
L
Linus Torvalds 已提交
532

533 534
			act->type = exts->type = TCA_OLD_COMPAT;
			list_add(&act->list, &exts->actions);
535
		} else if (exts->action && tb[exts->action]) {
536
			int err;
537
			err = tcf_action_init(net, tb[exts->action], rate_tlv,
538
					      NULL, ovr,
539 540 541
					      TCA_ACT_BIND, &exts->actions);
			if (err)
				return err;
L
Linus Torvalds 已提交
542 543 544
		}
	}
#else
545 546
	if ((exts->action && tb[exts->action]) ||
	    (exts->police && tb[exts->police]))
L
Linus Torvalds 已提交
547 548 549 550 551
		return -EOPNOTSUPP;
#endif

	return 0;
}
552
EXPORT_SYMBOL(tcf_exts_validate);
L
Linus Torvalds 已提交
553

554 555
void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
		     struct tcf_exts *src)
L
Linus Torvalds 已提交
556 557
{
#ifdef CONFIG_NET_CLS_ACT
558 559 560 561
	LIST_HEAD(tmp);
	tcf_tree_lock(tp);
	list_splice_init(&dst->actions, &tmp);
	list_splice(&src->actions, &dst->actions);
562
	dst->type = src->type;
563 564
	tcf_tree_unlock(tp);
	tcf_action_destroy(&tmp, TCA_ACT_UNBIND);
L
Linus Torvalds 已提交
565 566
#endif
}
567
EXPORT_SYMBOL(tcf_exts_change);
L
Linus Torvalds 已提交
568

569 570 571
#define tcf_exts_first_act(ext)					\
	list_first_entry_or_null(&(exts)->actions,		\
				 struct tc_action, list)
572

573
int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
L
Linus Torvalds 已提交
574 575
{
#ifdef CONFIG_NET_CLS_ACT
576 577
	struct nlattr *nest;

578
	if (exts->action && !list_empty(&exts->actions)) {
L
Linus Torvalds 已提交
579 580 581 582 583
		/*
		 * 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
		 */
584
		if (exts->type != TCA_OLD_COMPAT) {
585
			nest = nla_nest_start(skb, exts->action);
586 587
			if (nest == NULL)
				goto nla_put_failure;
588
			if (tcf_action_dump(skb, &exts->actions, 0, 0) < 0)
589
				goto nla_put_failure;
590
			nla_nest_end(skb, nest);
591
		} else if (exts->police) {
592
			struct tc_action *act = tcf_exts_first_act(exts);
593
			nest = nla_nest_start(skb, exts->police);
594
			if (nest == NULL || !act)
595
				goto nla_put_failure;
596
			if (tcf_action_dump_old(skb, act, 0, 0) < 0)
597
				goto nla_put_failure;
598
			nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
599 600 601
		}
	}
	return 0;
602 603 604

nla_put_failure:
	nla_nest_cancel(skb, nest);
L
Linus Torvalds 已提交
605
	return -1;
606 607 608
#else
	return 0;
#endif
L
Linus Torvalds 已提交
609
}
610
EXPORT_SYMBOL(tcf_exts_dump);
L
Linus Torvalds 已提交
611

612

613
int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
L
Linus Torvalds 已提交
614 615
{
#ifdef CONFIG_NET_CLS_ACT
616
	struct tc_action *a = tcf_exts_first_act(exts);
617
	if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
618
		return -1;
L
Linus Torvalds 已提交
619 620 621
#endif
	return 0;
}
622
EXPORT_SYMBOL(tcf_exts_dump_stats);
L
Linus Torvalds 已提交
623 624 625

static int __init tc_filter_init(void)
{
626 627
	rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, NULL);
	rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, NULL);
628
	rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
629
		      tc_dump_tfilter, NULL);
L
Linus Torvalds 已提交
630 631 632 633 634

	return 0;
}

subsys_initcall(tc_filter_init);