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

	return first;
}

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
static bool tcf_proto_destroy(struct tcf_proto *tp, bool force)
{
	if (tp->ops->destroy(tp, force)) {
		module_put(tp->ops->owner);
		kfree_rcu(tp, rcu);
		return true;
	}
	return false;
}

void tcf_destroy_chain(struct tcf_proto __rcu **fl)
{
	struct tcf_proto *tp;

	while ((tp = rtnl_dereference(*fl)) != NULL) {
		RCU_INIT_POINTER(*fl, tp->next);
		tcf_proto_destroy(tp, true);
	}
}
EXPORT_SYMBOL(tcf_destroy_chain);

L
Linus Torvalds 已提交
151 152
/* Add/change/delete/get a filter node */

153
static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n)
L
Linus Torvalds 已提交
154
{
155
	struct net *net = sock_net(skb->sk);
156
	struct nlattr *tca[TCA_MAX + 1];
L
Linus Torvalds 已提交
157 158 159 160 161 162 163
	struct tcmsg *t;
	u32 protocol;
	u32 prio;
	u32 nprio;
	u32 parent;
	struct net_device *dev;
	struct Qdisc  *q;
J
John Fastabend 已提交
164 165
	struct tcf_proto __rcu **back;
	struct tcf_proto __rcu **chain;
L
Linus Torvalds 已提交
166
	struct tcf_proto *tp;
167
	const struct tcf_proto_ops *tp_ops;
168
	const struct Qdisc_class_ops *cops;
L
Linus Torvalds 已提交
169 170 171
	unsigned long cl;
	unsigned long fh;
	int err;
172
	int tp_created;
L
Linus Torvalds 已提交
173

174
	if ((n->nlmsg_type != RTM_GETTFILTER) &&
175
	    !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
176
		return -EPERM;
177

L
Linus Torvalds 已提交
178
replay:
179 180
	tp_created = 0;

181 182 183 184
	err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL);
	if (err < 0)
		return err;

185
	t = nlmsg_data(n);
L
Linus Torvalds 已提交
186 187 188 189 190 191 192
	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) {
193 194
		switch (n->nlmsg_type) {
		case RTM_DELTFILTER:
195
			if (protocol || t->tcm_handle || tca[TCA_KIND])
196 197 198 199 200 201 202 203 204 205 206 207
				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 已提交
208
			return -ENOENT;
209
		}
L
Linus Torvalds 已提交
210 211 212 213 214
	}

	/* Find head of filter chain. */

	/* Find link */
215
	dev = __dev_get_by_index(net, t->tcm_ifindex);
216
	if (dev == NULL)
L
Linus Torvalds 已提交
217 218 219 220
		return -ENODEV;

	/* Find qdisc */
	if (!parent) {
221
		q = dev->qdisc;
L
Linus Torvalds 已提交
222
		parent = q->handle;
223 224 225 226 227
	} else {
		q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
		if (q == NULL)
			return -EINVAL;
	}
L
Linus Torvalds 已提交
228 229

	/* Is it classful? */
E
Eric Dumazet 已提交
230 231
	cops = q->ops->cl_ops;
	if (!cops)
L
Linus Torvalds 已提交
232 233
		return -EINVAL;

234 235 236
	if (cops->tcf_chain == NULL)
		return -EOPNOTSUPP;

L
Linus Torvalds 已提交
237 238 239 240 241 242 243 244 245 246 247 248
	/* 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;
249 250 251 252 253 254
	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 已提交
255 256

	/* Check the chain for existence of proto-tcf with this priority */
J
John Fastabend 已提交
257 258 259
	for (back = chain;
	     (tp = rtnl_dereference(*back)) != NULL;
	     back = &tp->next) {
L
Linus Torvalds 已提交
260 261
		if (tp->prio >= prio) {
			if (tp->prio == prio) {
E
Eric Dumazet 已提交
262 263
				if (!nprio ||
				    (tp->protocol != protocol && protocol))
L
Linus Torvalds 已提交
264 265 266 267 268 269 270 271 272 273
					goto errout;
			} else
				tp = NULL;
			break;
		}
	}

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

274
		if (tca[TCA_KIND] == NULL || !protocol)
L
Linus Torvalds 已提交
275 276 277
			goto errout;

		err = -ENOENT;
E
Eric Dumazet 已提交
278 279
		if (n->nlmsg_type != RTM_NEWTFILTER ||
		    !(n->nlmsg_flags & NLM_F_CREATE))
L
Linus Torvalds 已提交
280 281 282 283 284 285
			goto errout;


		/* Create new proto tcf */

		err = -ENOBUFS;
286 287
		tp = kzalloc(sizeof(*tp), GFP_KERNEL);
		if (tp == NULL)
L
Linus Torvalds 已提交
288
			goto errout;
289
		err = -ENOENT;
290
		tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND]);
L
Linus Torvalds 已提交
291
		if (tp_ops == NULL) {
292
#ifdef CONFIG_MODULES
293
			struct nlattr *kind = tca[TCA_KIND];
L
Linus Torvalds 已提交
294 295 296
			char name[IFNAMSIZ];

			if (kind != NULL &&
297
			    nla_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
L
Linus Torvalds 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
				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 已提交
319 320
		tp->prio = nprio ? :
			       TC_H_MAJ(tcf_auto_prio(rtnl_dereference(*back)));
L
Linus Torvalds 已提交
321 322 323
		tp->q = q;
		tp->classify = tp_ops->classify;
		tp->classid = parent;
324 325 326

		err = tp_ops->init(tp);
		if (err != 0) {
L
Linus Torvalds 已提交
327 328 329 330 331
			module_put(tp_ops->owner);
			kfree(tp);
			goto errout;
		}

332
		tp_created = 1;
L
Linus Torvalds 已提交
333

334
	} else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind))
L
Linus Torvalds 已提交
335 336 337 338 339 340
		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 已提交
341 342 343
			struct tcf_proto *next = rtnl_dereference(tp->next);

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

345 346
			tfilter_notify(net, skb, n, tp, fh,
				       RTM_DELTFILTER, false);
347
			tcf_proto_destroy(tp, true);
L
Linus Torvalds 已提交
348 349 350 351 352
			err = 0;
			goto errout;
		}

		err = -ENOENT;
353 354
		if (n->nlmsg_type != RTM_NEWTFILTER ||
		    !(n->nlmsg_flags & NLM_F_CREATE))
L
Linus Torvalds 已提交
355 356 357
			goto errout;
	} else {
		switch (n->nlmsg_type) {
358
		case RTM_NEWTFILTER:
L
Linus Torvalds 已提交
359
			err = -EEXIST;
360 361
			if (n->nlmsg_flags & NLM_F_EXCL) {
				if (tp_created)
362
					tcf_proto_destroy(tp, true);
L
Linus Torvalds 已提交
363
				goto errout;
364
			}
L
Linus Torvalds 已提交
365 366 367
			break;
		case RTM_DELTFILTER:
			err = tp->ops->delete(tp, fh);
368
			if (err == 0) {
369
				struct tcf_proto *next = rtnl_dereference(tp->next);
370

371 372
				tfilter_notify(net, skb, n, tp,
					       t->tcm_handle,
373
					       RTM_DELTFILTER, false);
374
				if (tcf_proto_destroy(tp, false))
375 376
					RCU_INIT_POINTER(*back, next);
			}
L
Linus Torvalds 已提交
377 378
			goto errout;
		case RTM_GETTFILTER:
J
Jamal Hadi Salim 已提交
379
			err = tfilter_notify(net, skb, n, tp, fh,
380
					     RTM_NEWTFILTER, true);
L
Linus Torvalds 已提交
381 382 383 384 385 386 387
			goto errout;
		default:
			err = -EINVAL;
			goto errout;
		}
	}

388 389
	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);
390 391
	if (err == 0) {
		if (tp_created) {
J
John Fastabend 已提交
392 393
			RCU_INIT_POINTER(tp->next, rtnl_dereference(*back));
			rcu_assign_pointer(*back, tp);
394
		}
395
		tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER, false);
396 397
	} else {
		if (tp_created)
398
			tcf_proto_destroy(tp, true);
399
	}
L
Linus Torvalds 已提交
400 401 402 403 404 405 406 407 408 409

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

410 411 412
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 已提交
413 414 415
{
	struct tcmsg *tcm;
	struct nlmsghdr  *nlh;
416
	unsigned char *b = skb_tail_pointer(skb);
L
Linus Torvalds 已提交
417

418
	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
419 420 421
	if (!nlh)
		goto out_nlmsg_trim;
	tcm = nlmsg_data(nlh);
L
Linus Torvalds 已提交
422
	tcm->tcm_family = AF_UNSPEC;
423
	tcm->tcm__pad1 = 0;
J
Jiri Pirko 已提交
424
	tcm->tcm__pad2 = 0;
425
	tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex;
L
Linus Torvalds 已提交
426 427
	tcm->tcm_parent = tp->classid;
	tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
428 429
	if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
		goto nla_put_failure;
L
Linus Torvalds 已提交
430 431 432
	tcm->tcm_handle = fh;
	if (RTM_DELTFILTER != event) {
		tcm->tcm_handle = 0;
433
		if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
434
			goto nla_put_failure;
L
Linus Torvalds 已提交
435
	}
436
	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
L
Linus Torvalds 已提交
437 438
	return skb->len;

439
out_nlmsg_trim:
440
nla_put_failure:
441
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
442 443 444
	return -1;
}

445 446
static int tfilter_notify(struct net *net, struct sk_buff *oskb,
			  struct nlmsghdr *n, struct tcf_proto *tp,
447
			  unsigned long fh, int event, bool unicast)
L
Linus Torvalds 已提交
448 449
{
	struct sk_buff *skb;
450
	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
L
Linus Torvalds 已提交
451 452 453 454 455

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

456 457
	if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq,
			  n->nlmsg_flags, event) <= 0) {
L
Linus Torvalds 已提交
458 459 460 461
		kfree_skb(skb);
		return -EINVAL;
	}

462 463 464
	if (unicast)
		return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);

465
	return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
466
			      n->nlmsg_flags & NLM_F_ECHO);
L
Linus Torvalds 已提交
467 468
}

469
struct tcf_dump_args {
L
Linus Torvalds 已提交
470 471 472 473 474
	struct tcf_walker w;
	struct sk_buff *skb;
	struct netlink_callback *cb;
};

475 476
static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
			 struct tcf_walker *arg)
L
Linus Torvalds 已提交
477
{
478
	struct tcf_dump_args *a = (void *)arg;
479
	struct net *net = sock_net(a->skb->sk);
L
Linus Torvalds 已提交
480

481
	return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid,
J
Jamal Hadi Salim 已提交
482 483
			     a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
			     RTM_NEWTFILTER);
L
Linus Torvalds 已提交
484 485
}

E
Eric Dumazet 已提交
486
/* called with RTNL */
L
Linus Torvalds 已提交
487 488
static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
{
489
	struct net *net = sock_net(skb->sk);
L
Linus Torvalds 已提交
490 491 492 493
	int t;
	int s_t;
	struct net_device *dev;
	struct Qdisc *q;
J
John Fastabend 已提交
494
	struct tcf_proto *tp, __rcu **chain;
495
	struct tcmsg *tcm = nlmsg_data(cb->nlh);
L
Linus Torvalds 已提交
496
	unsigned long cl = 0;
497
	const struct Qdisc_class_ops *cops;
L
Linus Torvalds 已提交
498 499
	struct tcf_dump_args arg;

500
	if (nlmsg_len(cb->nlh) < sizeof(*tcm))
L
Linus Torvalds 已提交
501
		return skb->len;
E
Eric Dumazet 已提交
502 503
	dev = __dev_get_by_index(net, tcm->tcm_ifindex);
	if (!dev)
L
Linus Torvalds 已提交
504 505 506
		return skb->len;

	if (!tcm->tcm_parent)
507
		q = dev->qdisc;
L
Linus Torvalds 已提交
508 509 510 511
	else
		q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
	if (!q)
		goto out;
E
Eric Dumazet 已提交
512 513
	cops = q->ops->cl_ops;
	if (!cops)
L
Linus Torvalds 已提交
514
		goto errout;
515 516
	if (cops->tcf_chain == NULL)
		goto errout;
L
Linus Torvalds 已提交
517 518 519 520 521 522 523 524 525 526 527
	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 已提交
528 529
	for (tp = rtnl_dereference(*chain), t = 0;
	     tp; tp = rtnl_dereference(tp->next), t++) {
E
Eric Dumazet 已提交
530 531
		if (t < s_t)
			continue;
L
Linus Torvalds 已提交
532 533 534 535 536 537 538
		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)
539 540
			memset(&cb->args[1], 0,
			       sizeof(cb->args)-sizeof(cb->args[0]));
L
Linus Torvalds 已提交
541
		if (cb->args[1] == 0) {
542 543
			if (tcf_fill_node(net, skb, tp, 0,
					  NETLINK_CB(cb->skb).portid,
544 545
					  cb->nlh->nlmsg_seq, NLM_F_MULTI,
					  RTM_NEWTFILTER) <= 0)
L
Linus Torvalds 已提交
546
				break;
547

L
Linus Torvalds 已提交
548 549 550 551 552 553 554 555
			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 已提交
556
		arg.w.skip = cb->args[1] - 1;
L
Linus Torvalds 已提交
557 558
		arg.w.count = 0;
		tp->ops->walk(tp, &arg.w);
E
Eric Dumazet 已提交
559
		cb->args[1] = arg.w.count + 1;
L
Linus Torvalds 已提交
560 561 562 563 564 565 566 567 568 569 570 571 572
		if (arg.w.stop)
			break;
	}

	cb->args[0] = t;

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

573
void tcf_exts_destroy(struct tcf_exts *exts)
L
Linus Torvalds 已提交
574 575
{
#ifdef CONFIG_NET_CLS_ACT
576 577 578 579 580 581
	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 已提交
582 583
#endif
}
584
EXPORT_SYMBOL(tcf_exts_destroy);
L
Linus Torvalds 已提交
585

586
int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
J
Jamal Hadi Salim 已提交
587
		      struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
L
Linus Torvalds 已提交
588 589 590 591 592
{
#ifdef CONFIG_NET_CLS_ACT
	{
		struct tc_action *act;

593 594
		if (exts->police && tb[exts->police]) {
			act = tcf_action_init_1(net, tb[exts->police], rate_tlv,
J
Jamal Hadi Salim 已提交
595
						"police", ovr, TCA_ACT_BIND);
596 597
			if (IS_ERR(act))
				return PTR_ERR(act);
L
Linus Torvalds 已提交
598

599
			act->type = exts->type = TCA_OLD_COMPAT;
600 601
			exts->actions[0] = act;
			exts->nr_actions = 1;
602
		} else if (exts->action && tb[exts->action]) {
603 604 605
			LIST_HEAD(actions);
			int err, i = 0;

606
			err = tcf_action_init(net, tb[exts->action], rate_tlv,
J
Jamal Hadi Salim 已提交
607 608
					      NULL, ovr, TCA_ACT_BIND,
					      &actions);
609 610
			if (err)
				return err;
611 612 613
			list_for_each_entry(act, &actions, list)
				exts->actions[i++] = act;
			exts->nr_actions = i;
L
Linus Torvalds 已提交
614 615 616
		}
	}
#else
617 618
	if ((exts->action && tb[exts->action]) ||
	    (exts->police && tb[exts->police]))
L
Linus Torvalds 已提交
619 620 621 622 623
		return -EOPNOTSUPP;
#endif

	return 0;
}
624
EXPORT_SYMBOL(tcf_exts_validate);
L
Linus Torvalds 已提交
625

626 627
void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
		     struct tcf_exts *src)
L
Linus Torvalds 已提交
628 629
{
#ifdef CONFIG_NET_CLS_ACT
630 631
	struct tcf_exts old = *dst;

632
	tcf_tree_lock(tp);
633 634
	dst->nr_actions = src->nr_actions;
	dst->actions = src->actions;
635
	dst->type = src->type;
636
	tcf_tree_unlock(tp);
637 638

	tcf_exts_destroy(&old);
L
Linus Torvalds 已提交
639 640
#endif
}
641
EXPORT_SYMBOL(tcf_exts_change);
L
Linus Torvalds 已提交
642

643 644 645 646 647 648 649 650 651
#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
652

653
int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
L
Linus Torvalds 已提交
654 655
{
#ifdef CONFIG_NET_CLS_ACT
656 657
	struct nlattr *nest;

658
	if (exts->action && exts->nr_actions) {
L
Linus Torvalds 已提交
659 660 661 662 663
		/*
		 * 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
		 */
664
		if (exts->type != TCA_OLD_COMPAT) {
665 666
			LIST_HEAD(actions);

667
			nest = nla_nest_start(skb, exts->action);
668 669
			if (nest == NULL)
				goto nla_put_failure;
670 671 672

			tcf_exts_to_list(exts, &actions);
			if (tcf_action_dump(skb, &actions, 0, 0) < 0)
673
				goto nla_put_failure;
674
			nla_nest_end(skb, nest);
675
		} else if (exts->police) {
676
			struct tc_action *act = tcf_exts_first_act(exts);
677
			nest = nla_nest_start(skb, exts->police);
678
			if (nest == NULL || !act)
679
				goto nla_put_failure;
680
			if (tcf_action_dump_old(skb, act, 0, 0) < 0)
681
				goto nla_put_failure;
682
			nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
683 684 685
		}
	}
	return 0;
686 687 688

nla_put_failure:
	nla_nest_cancel(skb, nest);
L
Linus Torvalds 已提交
689
	return -1;
690 691 692
#else
	return 0;
#endif
L
Linus Torvalds 已提交
693
}
694
EXPORT_SYMBOL(tcf_exts_dump);
L
Linus Torvalds 已提交
695

696

697
int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
L
Linus Torvalds 已提交
698 699
{
#ifdef CONFIG_NET_CLS_ACT
700
	struct tc_action *a = tcf_exts_first_act(exts);
701
	if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
702
		return -1;
L
Linus Torvalds 已提交
703 704 705
#endif
	return 0;
}
706
EXPORT_SYMBOL(tcf_exts_dump_stats);
L
Linus Torvalds 已提交
707

708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
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 已提交
732 733
static int __init tc_filter_init(void)
{
734 735
	rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, NULL);
	rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, NULL);
736
	rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
737
		      tc_dump_tfilter, NULL);
L
Linus Torvalds 已提交
738 739 740 741 742

	return 0;
}

subsys_initcall(tc_filter_init);