cls_api.c 91.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * net/sched/cls_api.c	Packet classifier API.
 *
 * 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>
17
#include <linux/err.h>
L
Linus Torvalds 已提交
18 19 20
#include <linux/skbuff.h>
#include <linux/init.h>
#include <linux/kmod.h>
21
#include <linux/slab.h>
22
#include <linux/idr.h>
23
#include <linux/jhash.h>
24
#include <linux/rculist.h>
25 26
#include <net/net_namespace.h>
#include <net/sock.h>
27
#include <net/netlink.h>
L
Linus Torvalds 已提交
28 29
#include <net/pkt_sched.h>
#include <net/pkt_cls.h>
30
#include <net/tc_act/tc_pedit.h>
31 32 33 34 35
#include <net/tc_act/tc_mirred.h>
#include <net/tc_act/tc_vlan.h>
#include <net/tc_act/tc_tunnel_key.h>
#include <net/tc_act/tc_csum.h>
#include <net/tc_act/tc_gact.h>
36
#include <net/tc_act/tc_police.h>
37
#include <net/tc_act/tc_sample.h>
38
#include <net/tc_act/tc_skbedit.h>
P
Paul Blakey 已提交
39
#include <net/tc_act/tc_ct.h>
40
#include <net/tc_act/tc_mpls.h>
41
#include <net/tc_act/tc_gate.h>
42
#include <net/flow_offload.h>
L
Linus Torvalds 已提交
43

44 45
extern const struct nla_policy rtm_tca_policy[TCA_MAX + 1];

L
Linus Torvalds 已提交
46
/* The list of all installed classifier types */
47
static LIST_HEAD(tcf_proto_base);
L
Linus Torvalds 已提交
48 49 50 51

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

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#ifdef CONFIG_NET_CLS_ACT
DEFINE_STATIC_KEY_FALSE(tc_skb_ext_tc);
EXPORT_SYMBOL(tc_skb_ext_tc);

void tc_skb_ext_tc_enable(void)
{
	static_branch_inc(&tc_skb_ext_tc);
}
EXPORT_SYMBOL(tc_skb_ext_tc_enable);

void tc_skb_ext_tc_disable(void)
{
	static_branch_dec(&tc_skb_ext_tc);
}
EXPORT_SYMBOL(tc_skb_ext_tc_disable);
#endif

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
static u32 destroy_obj_hashfn(const struct tcf_proto *tp)
{
	return jhash_3words(tp->chain->index, tp->prio,
			    (__force __u32)tp->protocol, 0);
}

static void tcf_proto_signal_destroying(struct tcf_chain *chain,
					struct tcf_proto *tp)
{
	struct tcf_block *block = chain->block;

	mutex_lock(&block->proto_destroy_lock);
	hash_add_rcu(block->proto_destroy_ht, &tp->destroy_ht_node,
		     destroy_obj_hashfn(tp));
	mutex_unlock(&block->proto_destroy_lock);
}

static bool tcf_proto_cmp(const struct tcf_proto *tp1,
			  const struct tcf_proto *tp2)
{
	return tp1->chain->index == tp2->chain->index &&
	       tp1->prio == tp2->prio &&
	       tp1->protocol == tp2->protocol;
}

static bool tcf_proto_exists_destroying(struct tcf_chain *chain,
					struct tcf_proto *tp)
{
	u32 hash = destroy_obj_hashfn(tp);
	struct tcf_proto *iter;
	bool found = false;

	rcu_read_lock();
	hash_for_each_possible_rcu(chain->block->proto_destroy_ht, iter,
				   destroy_ht_node, hash) {
		if (tcf_proto_cmp(tp, iter)) {
			found = true;
			break;
		}
	}
	rcu_read_unlock();

	return found;
}

static void
tcf_proto_signal_destroyed(struct tcf_chain *chain, struct tcf_proto *tp)
{
	struct tcf_block *block = chain->block;

	mutex_lock(&block->proto_destroy_lock);
	if (hash_hashed(&tp->destroy_ht_node))
		hash_del_rcu(&tp->destroy_ht_node);
	mutex_unlock(&block->proto_destroy_lock);
}

L
Linus Torvalds 已提交
125 126
/* Find classifier type by string name */

127
static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind)
L
Linus Torvalds 已提交
128
{
129
	const struct tcf_proto_ops *t, *res = NULL;
L
Linus Torvalds 已提交
130 131 132

	if (kind) {
		read_lock(&cls_mod_lock);
133
		list_for_each_entry(t, &tcf_proto_base, head) {
134
			if (strcmp(kind, t->kind) == 0) {
135 136
				if (try_module_get(t->owner))
					res = t;
L
Linus Torvalds 已提交
137 138 139 140 141
				break;
			}
		}
		read_unlock(&cls_mod_lock);
	}
142
	return res;
L
Linus Torvalds 已提交
143 144
}

145
static const struct tcf_proto_ops *
146 147
tcf_proto_lookup_ops(const char *kind, bool rtnl_held,
		     struct netlink_ext_ack *extack)
148 149 150 151 152 153 154
{
	const struct tcf_proto_ops *ops;

	ops = __tcf_proto_lookup_ops(kind);
	if (ops)
		return ops;
#ifdef CONFIG_MODULES
155 156
	if (rtnl_held)
		rtnl_unlock();
157
	request_module("cls_%s", kind);
158 159
	if (rtnl_held)
		rtnl_lock();
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	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 (ops) {
		module_put(ops->owner);
		return ERR_PTR(-EAGAIN);
	}
#endif
	NL_SET_ERR_MSG(extack, "TC classifier not found");
	return ERR_PTR(-ENOENT);
}

L
Linus Torvalds 已提交
175 176 177 178
/* Register(unregister) new classifier type */

int register_tcf_proto_ops(struct tcf_proto_ops *ops)
{
179
	struct tcf_proto_ops *t;
L
Linus Torvalds 已提交
180 181 182
	int rc = -EEXIST;

	write_lock(&cls_mod_lock);
183
	list_for_each_entry(t, &tcf_proto_base, head)
L
Linus Torvalds 已提交
184 185 186
		if (!strcmp(ops->kind, t->kind))
			goto out;

187
	list_add_tail(&ops->head, &tcf_proto_base);
L
Linus Torvalds 已提交
188 189 190 191 192
	rc = 0;
out:
	write_unlock(&cls_mod_lock);
	return rc;
}
193
EXPORT_SYMBOL(register_tcf_proto_ops);
L
Linus Torvalds 已提交
194

195 196
static struct workqueue_struct *tc_filter_wq;

L
Linus Torvalds 已提交
197 198
int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
{
199
	struct tcf_proto_ops *t;
L
Linus Torvalds 已提交
200 201
	int rc = -ENOENT;

202 203 204 205
	/* Wait for outstanding call_rcu()s, if any, from a
	 * tcf_proto_ops's destroy() handler.
	 */
	rcu_barrier();
206
	flush_workqueue(tc_filter_wq);
207

L
Linus Torvalds 已提交
208
	write_lock(&cls_mod_lock);
209 210 211 212
	list_for_each_entry(t, &tcf_proto_base, head) {
		if (t == ops) {
			list_del(&t->head);
			rc = 0;
L
Linus Torvalds 已提交
213
			break;
214 215
		}
	}
L
Linus Torvalds 已提交
216 217 218
	write_unlock(&cls_mod_lock);
	return rc;
}
219
EXPORT_SYMBOL(unregister_tcf_proto_ops);
L
Linus Torvalds 已提交
220

C
Cong Wang 已提交
221
bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
222
{
C
Cong Wang 已提交
223 224
	INIT_RCU_WORK(rwork, func);
	return queue_rcu_work(tc_filter_wq, rwork);
225 226 227
}
EXPORT_SYMBOL(tcf_queue_work);

L
Linus Torvalds 已提交
228 229
/* Select new prio value from the range, managed by kernel. */

230
static inline u32 tcf_auto_prio(struct tcf_proto *tp)
L
Linus Torvalds 已提交
231
{
232
	u32 first = TC_H_MAKE(0xC0000000U, 0U);
L
Linus Torvalds 已提交
233 234

	if (tp)
E
Eric Dumazet 已提交
235
		first = tp->prio - 1;
L
Linus Torvalds 已提交
236

237
	return TC_H_MAJ(first);
L
Linus Torvalds 已提交
238 239
}

240 241 242
static bool tcf_proto_check_kind(struct nlattr *kind, char *name)
{
	if (kind)
243
		return nla_strscpy(name, kind, IFNAMSIZ) < 0;
244 245 246 247
	memset(name, 0, IFNAMSIZ);
	return false;
}

248 249 250 251 252
static bool tcf_proto_is_unlocked(const char *kind)
{
	const struct tcf_proto_ops *ops;
	bool ret;

253 254 255
	if (strlen(kind) == 0)
		return false;

256 257 258 259 260 261 262 263 264 265 266 267
	ops = tcf_proto_lookup_ops(kind, false, NULL);
	/* On error return false to take rtnl lock. Proto lookup/create
	 * functions will perform lookup again and properly handle errors.
	 */
	if (IS_ERR(ops))
		return false;

	ret = !!(ops->flags & TCF_PROTO_OPS_DOIT_UNLOCKED);
	module_put(ops->owner);
	return ret;
}

268
static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
269
					  u32 prio, struct tcf_chain *chain,
270
					  bool rtnl_held,
271
					  struct netlink_ext_ack *extack)
272 273 274 275 276 277 278 279
{
	struct tcf_proto *tp;
	int err;

	tp = kzalloc(sizeof(*tp), GFP_KERNEL);
	if (!tp)
		return ERR_PTR(-ENOBUFS);

280
	tp->ops = tcf_proto_lookup_ops(kind, rtnl_held, extack);
281 282
	if (IS_ERR(tp->ops)) {
		err = PTR_ERR(tp->ops);
283
		goto errout;
284 285 286 287
	}
	tp->classify = tp->ops->classify;
	tp->protocol = protocol;
	tp->prio = prio;
288
	tp->chain = chain;
289
	spin_lock_init(&tp->lock);
290
	refcount_set(&tp->refcnt, 1);
291 292 293 294 295 296 297 298 299 300 301 302 303

	err = tp->ops->init(tp);
	if (err) {
		module_put(tp->ops->owner);
		goto errout;
	}
	return tp;

errout:
	kfree(tp);
	return ERR_PTR(err);
}

304 305 306 307 308 309 310
static void tcf_proto_get(struct tcf_proto *tp)
{
	refcount_inc(&tp->refcnt);
}

static void tcf_chain_put(struct tcf_chain *chain);

311
static void tcf_proto_destroy(struct tcf_proto *tp, bool rtnl_held,
312
			      bool sig_destroy, struct netlink_ext_ack *extack)
313
{
314
	tp->ops->destroy(tp, rtnl_held, extack);
315 316
	if (sig_destroy)
		tcf_proto_signal_destroyed(tp->chain, tp);
317
	tcf_chain_put(tp->chain);
318 319
	module_put(tp->ops->owner);
	kfree_rcu(tp, rcu);
320 321
}

322
static void tcf_proto_put(struct tcf_proto *tp, bool rtnl_held,
323 324 325
			  struct netlink_ext_ack *extack)
{
	if (refcount_dec_and_test(&tp->refcnt))
326
		tcf_proto_destroy(tp, rtnl_held, true, extack);
327 328
}

329
static bool tcf_proto_check_delete(struct tcf_proto *tp)
330
{
331 332
	if (tp->ops->delete_empty)
		return tp->ops->delete_empty(tp);
333

334
	tp->deleting = true;
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	return tp->deleting;
}

static void tcf_proto_mark_delete(struct tcf_proto *tp)
{
	spin_lock(&tp->lock);
	tp->deleting = true;
	spin_unlock(&tp->lock);
}

static bool tcf_proto_is_deleting(struct tcf_proto *tp)
{
	bool deleting;

	spin_lock(&tp->lock);
	deleting = tp->deleting;
	spin_unlock(&tp->lock);

	return deleting;
}

356 357 358
#define ASSERT_BLOCK_LOCKED(block)					\
	lockdep_assert_held(&(block)->lock)

359 360 361 362 363 364
struct tcf_filter_chain_list_item {
	struct list_head list;
	tcf_chain_head_change_t *chain_head_change;
	void *chain_head_change_priv;
};

365 366
static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
					  u32 chain_index)
367
{
368 369
	struct tcf_chain *chain;

370 371
	ASSERT_BLOCK_LOCKED(block);

372 373 374
	chain = kzalloc(sizeof(*chain), GFP_KERNEL);
	if (!chain)
		return NULL;
375
	list_add_tail_rcu(&chain->list, &block->chain_list);
376
	mutex_init(&chain->filter_chain_lock);
377 378
	chain->block = block;
	chain->index = chain_index;
379
	chain->refcnt = 1;
380 381
	if (!chain->index)
		block->chain0.chain = chain;
382
	return chain;
383 384
}

385 386 387 388 389 390
static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
				       struct tcf_proto *tp_head)
{
	if (item->chain_head_change)
		item->chain_head_change(tp_head, item->chain_head_change_priv);
}
391 392 393

static void tcf_chain0_head_change(struct tcf_chain *chain,
				   struct tcf_proto *tp_head)
394
{
395
	struct tcf_filter_chain_list_item *item;
396
	struct tcf_block *block = chain->block;
397

398 399
	if (chain->index)
		return;
400 401

	mutex_lock(&block->lock);
402
	list_for_each_entry(item, &block->chain0.filter_chain_list, list)
403
		tcf_chain_head_change_item(item, tp_head);
404
	mutex_unlock(&block->lock);
405 406
}

407 408 409
/* Returns true if block can be safely freed. */

static bool tcf_chain_detach(struct tcf_chain *chain)
J
Jiri Pirko 已提交
410
{
411 412
	struct tcf_block *block = chain->block;

413 414
	ASSERT_BLOCK_LOCKED(block);

415
	list_del_rcu(&chain->list);
416 417
	if (!chain->index)
		block->chain0.chain = NULL;
418 419 420 421 422 423 424 425 426 427 428

	if (list_empty(&block->chain_list) &&
	    refcount_read(&block->refcnt) == 0)
		return true;

	return false;
}

static void tcf_block_destroy(struct tcf_block *block)
{
	mutex_destroy(&block->lock);
429
	mutex_destroy(&block->proto_destroy_lock);
430 431 432 433 434 435 436
	kfree_rcu(block, rcu);
}

static void tcf_chain_destroy(struct tcf_chain *chain, bool free_block)
{
	struct tcf_block *block = chain->block;

437
	mutex_destroy(&chain->filter_chain_lock);
438
	kfree_rcu(chain, rcu);
439 440
	if (free_block)
		tcf_block_destroy(block);
441
}
442

443 444
static void tcf_chain_hold(struct tcf_chain *chain)
{
445 446
	ASSERT_BLOCK_LOCKED(chain->block);

447
	++chain->refcnt;
448 449
}

450
static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
451
{
452 453
	ASSERT_BLOCK_LOCKED(chain->block);

454
	/* In case all the references are action references, this
455
	 * chain should not be shown to the user.
456 457 458 459
	 */
	return chain->refcnt == chain->action_refcnt;
}

460 461
static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
					  u32 chain_index)
462 463 464
{
	struct tcf_chain *chain;

465 466
	ASSERT_BLOCK_LOCKED(block);

467
	list_for_each_entry(chain, &block->chain_list, list) {
468
		if (chain->index == chain_index)
469
			return chain;
470 471 472 473
	}
	return NULL;
}

474 475 476 477 478 479 480 481 482 483 484 485 486 487
#if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
static struct tcf_chain *tcf_chain_lookup_rcu(const struct tcf_block *block,
					      u32 chain_index)
{
	struct tcf_chain *chain;

	list_for_each_entry_rcu(chain, &block->chain_list, list) {
		if (chain->index == chain_index)
			return chain;
	}
	return NULL;
}
#endif

488 489 490
static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
			   u32 seq, u16 flags, int event, bool unicast);

491 492 493
static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
					 u32 chain_index, bool create,
					 bool by_act)
494
{
495 496
	struct tcf_chain *chain = NULL;
	bool is_first_reference;
497

498 499
	mutex_lock(&block->lock);
	chain = tcf_chain_lookup(block, chain_index);
500 501
	if (chain) {
		tcf_chain_hold(chain);
502 503
	} else {
		if (!create)
504
			goto errout;
505 506
		chain = tcf_chain_create(block, chain_index);
		if (!chain)
507
			goto errout;
508
	}
509

510 511
	if (by_act)
		++chain->action_refcnt;
512 513
	is_first_reference = chain->refcnt - chain->action_refcnt == 1;
	mutex_unlock(&block->lock);
514 515 516 517 518 519

	/* Send notification only in case we got the first
	 * non-action reference. Until then, the chain acts only as
	 * a placeholder for actions pointing to it and user ought
	 * not know about them.
	 */
520
	if (is_first_reference && !by_act)
521 522 523
		tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
				RTM_NEWCHAIN, false);

524
	return chain;
525 526 527 528

errout:
	mutex_unlock(&block->lock);
	return chain;
529
}
530

531 532
static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
				       bool create)
533 534 535
{
	return __tcf_chain_get(block, chain_index, create, false);
}
536

537 538
struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
{
539
	return __tcf_chain_get(block, chain_index, true, true);
540 541 542
}
EXPORT_SYMBOL(tcf_chain_get_by_act);

543 544 545 546 547 548
static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
			       void *tmplt_priv);
static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
				  void *tmplt_priv, u32 chain_index,
				  struct tcf_block *block, struct sk_buff *oskb,
				  u32 seq, u16 flags, bool unicast);
549

550 551
static void __tcf_chain_put(struct tcf_chain *chain, bool by_act,
			    bool explicitly_created)
552
{
553
	struct tcf_block *block = chain->block;
554
	const struct tcf_proto_ops *tmplt_ops;
555
	bool free_block = false;
556
	unsigned int refcnt;
557
	void *tmplt_priv;
558 559

	mutex_lock(&block->lock);
560 561 562 563 564 565 566 567
	if (explicitly_created) {
		if (!chain->explicitly_created) {
			mutex_unlock(&block->lock);
			return;
		}
		chain->explicitly_created = false;
	}

568 569
	if (by_act)
		chain->action_refcnt--;
570 571 572 573 574 575

	/* tc_chain_notify_delete can't be called while holding block lock.
	 * However, when block is unlocked chain can be changed concurrently, so
	 * save these to temporary variables.
	 */
	refcnt = --chain->refcnt;
576 577
	tmplt_ops = chain->tmplt_ops;
	tmplt_priv = chain->tmplt_priv;
578 579

	/* The last dropped non-action reference will trigger notification. */
580 581
	if (refcnt - chain->action_refcnt == 0 && !by_act) {
		tc_chain_notify_delete(tmplt_ops, tmplt_priv, chain->index,
582
				       block, NULL, 0, 0, false);
583 584 585
		/* Last reference to chain, no need to lock. */
		chain->flushing = false;
	}
586

587 588 589 590
	if (refcnt == 0)
		free_block = tcf_chain_detach(chain);
	mutex_unlock(&block->lock);

591
	if (refcnt == 0) {
592
		tc_chain_tmplt_del(tmplt_ops, tmplt_priv);
593
		tcf_chain_destroy(chain, free_block);
594
	}
595
}
596

597
static void tcf_chain_put(struct tcf_chain *chain)
598
{
599
	__tcf_chain_put(chain, false, false);
600
}
601

602 603
void tcf_chain_put_by_act(struct tcf_chain *chain)
{
604
	__tcf_chain_put(chain, true, false);
605 606 607
}
EXPORT_SYMBOL(tcf_chain_put_by_act);

608 609
static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
{
610
	__tcf_chain_put(chain, false, true);
611 612
}

613
static void tcf_chain_flush(struct tcf_chain *chain, bool rtnl_held)
614
{
615
	struct tcf_proto *tp, *tp_next;
616

617 618
	mutex_lock(&chain->filter_chain_lock);
	tp = tcf_chain_dereference(chain->filter_chain, chain);
619 620 621 622 623 624
	while (tp) {
		tp_next = rcu_dereference_protected(tp->next, 1);
		tcf_proto_signal_destroying(chain, tp);
		tp = tp_next;
	}
	tp = tcf_chain_dereference(chain->filter_chain, chain);
625
	RCU_INIT_POINTER(chain->filter_chain, NULL);
626
	tcf_chain0_head_change(chain, NULL);
627
	chain->flushing = true;
628 629
	mutex_unlock(&chain->filter_chain_lock);

630
	while (tp) {
631
		tp_next = rcu_dereference_protected(tp->next, 1);
632
		tcf_proto_put(tp, rtnl_held, NULL);
633
		tp = tp_next;
634 635 636
	}
}

637 638 639
static int tcf_block_setup(struct tcf_block *block,
			   struct flow_block_offload *bo);

640
static void tcf_block_offload_init(struct flow_block_offload *bo,
641
				   struct net_device *dev, struct Qdisc *sch,
642 643 644 645 646 647 648 649 650 651 652
				   enum flow_block_command command,
				   enum flow_block_binder_type binder_type,
				   struct flow_block *flow_block,
				   bool shared, struct netlink_ext_ack *extack)
{
	bo->net = dev_net(dev);
	bo->command = command;
	bo->binder_type = binder_type;
	bo->block = flow_block;
	bo->block_shared = shared;
	bo->extack = extack;
653
	bo->sch = sch;
654
	bo->cb_list_head = &flow_block->cb_list;
655 656 657
	INIT_LIST_HEAD(&bo->cb_list);
}

658 659 660 661
static void tcf_block_unbind(struct tcf_block *block,
			     struct flow_block_offload *bo);

static void tc_block_indr_cleanup(struct flow_block_cb *block_cb)
662
{
663 664
	struct tcf_block *block = block_cb->indr.data;
	struct net_device *dev = block_cb->indr.dev;
665
	struct Qdisc *sch = block_cb->indr.sch;
666
	struct netlink_ext_ack extack = {};
667
	struct flow_block_offload bo = {};
668

669
	tcf_block_offload_init(&bo, dev, sch, FLOW_BLOCK_UNBIND,
670 671 672
			       block_cb->indr.binder_type,
			       &block->flow_block, tcf_block_shared(block),
			       &extack);
673
	rtnl_lock();
674
	down_write(&block->cb_lock);
675
	list_del(&block_cb->driver_list);
676 677
	list_move(&block_cb->list, &bo.cb_list);
	tcf_block_unbind(block, &bo);
678
	up_write(&block->cb_lock);
679
	rtnl_unlock();
680 681
}

682 683
static bool tcf_block_offload_in_use(struct tcf_block *block)
{
684
	return atomic_read(&block->offloadcnt);
685 686 687
}

static int tcf_block_offload_cmd(struct tcf_block *block,
688
				 struct net_device *dev, struct Qdisc *sch,
689
				 struct tcf_block_ext_info *ei,
690
				 enum flow_block_command command,
691
				 struct netlink_ext_ack *extack)
692
{
693
	struct flow_block_offload bo = {};
694

695
	tcf_block_offload_init(&bo, dev, sch, command, ei->binder_type,
696 697
			       &block->flow_block, tcf_block_shared(block),
			       extack);
698

699 700 701
	if (dev->netdev_ops->ndo_setup_tc) {
		int err;

702
		err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
703 704 705 706 707 708 709
		if (err < 0) {
			if (err != -EOPNOTSUPP)
				NL_SET_ERR_MSG(extack, "Driver ndo_setup_tc failed");
			return err;
		}

		return tcf_block_setup(block, &bo);
710
	}
711

712
	flow_indr_dev_setup_offload(dev, sch, TC_SETUP_BLOCK, block, &bo,
713 714 715 716
				    tc_block_indr_cleanup);
	tcf_block_setup(block, &bo);

	return -EOPNOTSUPP;
717 718
}

719
static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
720 721
				  struct tcf_block_ext_info *ei,
				  struct netlink_ext_ack *extack)
722
{
723 724 725
	struct net_device *dev = q->dev_queue->dev;
	int err;

726
	down_write(&block->cb_lock);
727 728 729 730

	/* If tc offload feature is disabled and the block we try to bind
	 * to already has some offloaded filters, forbid to bind.
	 */
731 732 733
	if (dev->netdev_ops->ndo_setup_tc &&
	    !tc_can_offload(dev) &&
	    tcf_block_offload_in_use(block)) {
734
		NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
735 736
		err = -EOPNOTSUPP;
		goto err_unlock;
737
	}
738

739
	err = tcf_block_offload_cmd(block, dev, q, ei, FLOW_BLOCK_BIND, extack);
740 741
	if (err == -EOPNOTSUPP)
		goto no_offload_dev_inc;
742
	if (err)
743
		goto err_unlock;
744

745
	up_write(&block->cb_lock);
746
	return 0;
747 748

no_offload_dev_inc:
749
	if (tcf_block_offload_in_use(block))
750
		goto err_unlock;
751

752
	err = 0;
753
	block->nooffloaddevcnt++;
754 755 756
err_unlock:
	up_write(&block->cb_lock);
	return err;
757 758 759 760 761
}

static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
				     struct tcf_block_ext_info *ei)
{
762 763 764
	struct net_device *dev = q->dev_queue->dev;
	int err;

765
	down_write(&block->cb_lock);
766
	err = tcf_block_offload_cmd(block, dev, q, ei, FLOW_BLOCK_UNBIND, NULL);
767 768
	if (err == -EOPNOTSUPP)
		goto no_offload_dev_dec;
769
	up_write(&block->cb_lock);
770 771 772 773
	return;

no_offload_dev_dec:
	WARN_ON(block->nooffloaddevcnt-- == 0);
774
	up_write(&block->cb_lock);
775 776
}

777
static int
778 779 780
tcf_chain0_head_change_cb_add(struct tcf_block *block,
			      struct tcf_block_ext_info *ei,
			      struct netlink_ext_ack *extack)
781 782
{
	struct tcf_filter_chain_list_item *item;
783
	struct tcf_chain *chain0;
784 785 786 787 788 789 790 791

	item = kmalloc(sizeof(*item), GFP_KERNEL);
	if (!item) {
		NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
		return -ENOMEM;
	}
	item->chain_head_change = ei->chain_head_change;
	item->chain_head_change_priv = ei->chain_head_change_priv;
792 793 794

	mutex_lock(&block->lock);
	chain0 = block->chain0.chain;
795 796 797 798
	if (chain0)
		tcf_chain_hold(chain0);
	else
		list_add(&item->list, &block->chain0.filter_chain_list);
799 800
	mutex_unlock(&block->lock);

801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
	if (chain0) {
		struct tcf_proto *tp_head;

		mutex_lock(&chain0->filter_chain_lock);

		tp_head = tcf_chain_dereference(chain0->filter_chain, chain0);
		if (tp_head)
			tcf_chain_head_change_item(item, tp_head);

		mutex_lock(&block->lock);
		list_add(&item->list, &block->chain0.filter_chain_list);
		mutex_unlock(&block->lock);

		mutex_unlock(&chain0->filter_chain_lock);
		tcf_chain_put(chain0);
	}

818 819 820 821
	return 0;
}

static void
822 823
tcf_chain0_head_change_cb_del(struct tcf_block *block,
			      struct tcf_block_ext_info *ei)
824 825 826
{
	struct tcf_filter_chain_list_item *item;

827
	mutex_lock(&block->lock);
828
	list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
829 830 831
		if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
		    (item->chain_head_change == ei->chain_head_change &&
		     item->chain_head_change_priv == ei->chain_head_change_priv)) {
832
			if (block->chain0.chain)
833
				tcf_chain_head_change_item(item, NULL);
834
			list_del(&item->list);
835 836
			mutex_unlock(&block->lock);

837 838 839 840
			kfree(item);
			return;
		}
	}
841
	mutex_unlock(&block->lock);
842 843 844
	WARN_ON(1);
}

845
struct tcf_net {
846
	spinlock_t idr_lock; /* Protects idr */
847 848 849 850 851 852
	struct idr idr;
};

static unsigned int tcf_net_id;

static int tcf_block_insert(struct tcf_block *block, struct net *net,
853
			    struct netlink_ext_ack *extack)
854
{
855
	struct tcf_net *tn = net_generic(net, tcf_net_id);
856 857 858 859 860 861 862 863
	int err;

	idr_preload(GFP_KERNEL);
	spin_lock(&tn->idr_lock);
	err = idr_alloc_u32(&tn->idr, block, &block->index, block->index,
			    GFP_NOWAIT);
	spin_unlock(&tn->idr_lock);
	idr_preload_end();
864

865
	return err;
866 867
}

868 869 870 871
static void tcf_block_remove(struct tcf_block *block, struct net *net)
{
	struct tcf_net *tn = net_generic(net, tcf_net_id);

872
	spin_lock(&tn->idr_lock);
873
	idr_remove(&tn->idr, block->index);
874
	spin_unlock(&tn->idr_lock);
875 876 877
}

static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
878
					  u32 block_index,
879
					  struct netlink_ext_ack *extack)
880
{
881
	struct tcf_block *block;
882

883
	block = kzalloc(sizeof(*block), GFP_KERNEL);
884 885
	if (!block) {
		NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
886
		return ERR_PTR(-ENOMEM);
887
	}
888
	mutex_init(&block->lock);
889
	mutex_init(&block->proto_destroy_lock);
890
	init_rwsem(&block->cb_lock);
891
	flow_block_init(&block->flow_block);
892
	INIT_LIST_HEAD(&block->chain_list);
893
	INIT_LIST_HEAD(&block->owner_list);
894
	INIT_LIST_HEAD(&block->chain0.filter_chain_list);
895

896
	refcount_set(&block->refcnt, 1);
897
	block->net = net;
898 899 900 901 902
	block->index = block_index;

	/* Don't store q pointer for blocks which are shared */
	if (!tcf_block_shared(block))
		block->q = q;
903 904 905 906 907 908 909
	return block;
}

static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
{
	struct tcf_net *tn = net_generic(net, tcf_net_id);

910
	return idr_find(&tn->idr, block_index);
911 912
}

913 914 915 916 917 918 919 920 921 922 923 924 925
static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index)
{
	struct tcf_block *block;

	rcu_read_lock();
	block = tcf_block_lookup(net, block_index);
	if (block && !refcount_inc_not_zero(&block->refcnt))
		block = NULL;
	rcu_read_unlock();

	return block;
}

926 927
static struct tcf_chain *
__tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
928
{
929 930 931 932 933 934 935
	mutex_lock(&block->lock);
	if (chain)
		chain = list_is_last(&chain->list, &block->chain_list) ?
			NULL : list_next_entry(chain, list);
	else
		chain = list_first_entry_or_null(&block->chain_list,
						 struct tcf_chain, list);
936

937 938 939 940 941 942
	/* skip all action-only chains */
	while (chain && tcf_chain_held_by_acts_only(chain))
		chain = list_is_last(&chain->list, &block->chain_list) ?
			NULL : list_next_entry(chain, list);

	if (chain)
943
		tcf_chain_hold(chain);
944
	mutex_unlock(&block->lock);
945

946
	return chain;
947 948
}

949 950 951 952 953 954 955 956 957 958 959
/* Function to be used by all clients that want to iterate over all chains on
 * block. It properly obtains block->lock and takes reference to chain before
 * returning it. Users of this function must be tolerant to concurrent chain
 * insertion/deletion or ensure that no concurrent chain modification is
 * possible. Note that all netlink dump callbacks cannot guarantee to provide
 * consistent dump because rtnl lock is released each time skb is filled with
 * data and sent to user-space.
 */

struct tcf_chain *
tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
960
{
961
	struct tcf_chain *chain_next = __tcf_get_next_chain(block, chain);
962

963
	if (chain)
964
		tcf_chain_put(chain);
965 966 967 968 969

	return chain_next;
}
EXPORT_SYMBOL(tcf_get_next_chain);

970 971 972
static struct tcf_proto *
__tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
{
973 974
	u32 prio = 0;

975 976 977
	ASSERT_RTNL();
	mutex_lock(&chain->filter_chain_lock);

978
	if (!tp) {
979
		tp = tcf_chain_dereference(chain->filter_chain, chain);
980 981 982 983 984 985 986 987 988 989 990 991
	} else if (tcf_proto_is_deleting(tp)) {
		/* 'deleting' flag is set and chain->filter_chain_lock was
		 * unlocked, which means next pointer could be invalid. Restart
		 * search.
		 */
		prio = tp->prio + 1;
		tp = tcf_chain_dereference(chain->filter_chain, chain);

		for (; tp; tp = tcf_chain_dereference(tp->next, chain))
			if (!tp->deleting && tp->prio >= prio)
				break;
	} else {
992
		tp = tcf_chain_dereference(tp->next, chain);
993
	}
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011

	if (tp)
		tcf_proto_get(tp);

	mutex_unlock(&chain->filter_chain_lock);

	return tp;
}

/* Function to be used by all clients that want to iterate over all tp's on
 * chain. Users of this function must be tolerant to concurrent tp
 * insertion/deletion or ensure that no concurrent chain modification is
 * possible. Note that all netlink dump callbacks cannot guarantee to provide
 * consistent dump because rtnl lock is released each time skb is filled with
 * data and sent to user-space.
 */

struct tcf_proto *
1012
tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
1013 1014 1015 1016
{
	struct tcf_proto *tp_next = __tcf_get_next_proto(chain, tp);

	if (tp)
1017
		tcf_proto_put(tp, true, NULL);
1018 1019 1020 1021 1022

	return tp_next;
}
EXPORT_SYMBOL(tcf_get_next_proto);

1023
static void tcf_block_flush_all_chains(struct tcf_block *block, bool rtnl_held)
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
{
	struct tcf_chain *chain;

	/* Last reference to block. At this point chains cannot be added or
	 * removed concurrently.
	 */
	for (chain = tcf_get_next_chain(block, NULL);
	     chain;
	     chain = tcf_get_next_chain(block, chain)) {
		tcf_chain_put_explicitly_created(chain);
1034
		tcf_chain_flush(chain, rtnl_held);
1035 1036 1037
	}
}

1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
/* Lookup Qdisc and increments its reference counter.
 * Set parent, if necessary.
 */

static int __tcf_qdisc_find(struct net *net, struct Qdisc **q,
			    u32 *parent, int ifindex, bool rtnl_held,
			    struct netlink_ext_ack *extack)
{
	const struct Qdisc_class_ops *cops;
	struct net_device *dev;
	int err = 0;

	if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
		return 0;

	rcu_read_lock();

	/* Find link */
	dev = dev_get_by_index_rcu(net, ifindex);
	if (!dev) {
		rcu_read_unlock();
		return -ENODEV;
	}

	/* Find qdisc */
	if (!*parent) {
1064
		*q = rcu_dereference(dev->qdisc);
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
		*parent = (*q)->handle;
	} else {
		*q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent));
		if (!*q) {
			NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
			err = -EINVAL;
			goto errout_rcu;
		}
	}

	*q = qdisc_refcount_inc_nz(*q);
	if (!*q) {
		NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
		err = -EINVAL;
		goto errout_rcu;
	}

	/* Is it classful? */
	cops = (*q)->ops->cl_ops;
	if (!cops) {
		NL_SET_ERR_MSG(extack, "Qdisc not classful");
		err = -EINVAL;
		goto errout_qdisc;
	}

	if (!cops->tcf_block) {
		NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
		err = -EOPNOTSUPP;
		goto errout_qdisc;
	}

errout_rcu:
	/* At this point we know that qdisc is not noop_qdisc,
	 * which means that qdisc holds a reference to net_device
	 * and we hold a reference to qdisc, so it is safe to release
	 * rcu read lock.
	 */
	rcu_read_unlock();
	return err;

errout_qdisc:
	rcu_read_unlock();

	if (rtnl_held)
		qdisc_put(*q);
	else
		qdisc_put_unlocked(*q);
	*q = NULL;

	return err;
}

static int __tcf_qdisc_cl_find(struct Qdisc *q, u32 parent, unsigned long *cl,
			       int ifindex, struct netlink_ext_ack *extack)
{
	if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
		return 0;

	/* Do we search for filter, attached to class? */
	if (TC_H_MIN(parent)) {
		const struct Qdisc_class_ops *cops = q->ops->cl_ops;

		*cl = cops->find(q, parent);
		if (*cl == 0) {
			NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
			return -ENOENT;
		}
	}

	return 0;
}

static struct tcf_block *__tcf_block_find(struct net *net, struct Qdisc *q,
					  unsigned long cl, int ifindex,
					  u32 block_index,
					  struct netlink_ext_ack *extack)
{
	struct tcf_block *block;

	if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
		block = tcf_block_refcnt_get(net, block_index);
		if (!block) {
			NL_SET_ERR_MSG(extack, "Block of given index was not found");
			return ERR_PTR(-EINVAL);
		}
	} else {
		const struct Qdisc_class_ops *cops = q->ops->cl_ops;

		block = cops->tcf_block(q, cl, extack);
		if (!block)
			return ERR_PTR(-EINVAL);

		if (tcf_block_shared(block)) {
			NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
			return ERR_PTR(-EOPNOTSUPP);
		}

		/* Always take reference to block in order to support execution
		 * of rules update path of cls API without rtnl lock. Caller
		 * must release block when it is finished using it. 'if' block
		 * of this conditional obtain reference to block by calling
		 * tcf_block_refcnt_get().
		 */
		refcount_inc(&block->refcnt);
	}

	return block;
}

1174
static void __tcf_block_put(struct tcf_block *block, struct Qdisc *q,
1175
			    struct tcf_block_ext_info *ei, bool rtnl_held)
1176
{
1177
	if (refcount_dec_and_mutex_lock(&block->refcnt, &block->lock)) {
1178 1179 1180 1181 1182 1183 1184 1185
		/* Flushing/putting all chains will cause the block to be
		 * deallocated when last chain is freed. However, if chain_list
		 * is empty, block has to be manually deallocated. After block
		 * reference counter reached 0, it is no longer possible to
		 * increment it or add new chains to block.
		 */
		bool free_block = list_empty(&block->chain_list);

1186
		mutex_unlock(&block->lock);
1187 1188 1189 1190 1191 1192 1193
		if (tcf_block_shared(block))
			tcf_block_remove(block, block->net);

		if (q)
			tcf_block_offload_unbind(block, q, ei);

		if (free_block)
1194
			tcf_block_destroy(block);
1195
		else
1196
			tcf_block_flush_all_chains(block, rtnl_held);
1197 1198 1199 1200 1201
	} else if (q) {
		tcf_block_offload_unbind(block, q, ei);
	}
}

1202
static void tcf_block_refcnt_put(struct tcf_block *block, bool rtnl_held)
1203
{
1204
	__tcf_block_put(block, NULL, NULL, rtnl_held);
1205 1206
}

1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
/* Find tcf block.
 * Set q, parent, cl when appropriate.
 */

static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
					u32 *parent, unsigned long *cl,
					int ifindex, u32 block_index,
					struct netlink_ext_ack *extack)
{
	struct tcf_block *block;
1217
	int err = 0;
1218

1219
	ASSERT_RTNL();
1220

1221 1222 1223
	err = __tcf_qdisc_find(net, q, parent, ifindex, true, extack);
	if (err)
		goto errout;
1224

1225 1226 1227
	err = __tcf_qdisc_cl_find(*q, *parent, cl, ifindex, extack);
	if (err)
		goto errout_qdisc;
1228

1229
	block = __tcf_block_find(net, *q, *cl, ifindex, block_index, extack);
1230 1231
	if (IS_ERR(block)) {
		err = PTR_ERR(block);
1232
		goto errout_qdisc;
1233
	}
1234 1235

	return block;
1236 1237

errout_qdisc:
1238
	if (*q)
1239
		qdisc_put(*q);
1240 1241
errout:
	*q = NULL;
1242 1243 1244
	return ERR_PTR(err);
}

1245 1246
static void tcf_block_release(struct Qdisc *q, struct tcf_block *block,
			      bool rtnl_held)
1247
{
1248
	if (!IS_ERR_OR_NULL(block))
1249
		tcf_block_refcnt_put(block, rtnl_held);
1250

1251 1252 1253 1254 1255 1256
	if (q) {
		if (rtnl_held)
			qdisc_put(q);
		else
			qdisc_put_unlocked(q);
	}
1257 1258
}

1259 1260 1261
struct tcf_block_owner_item {
	struct list_head list;
	struct Qdisc *q;
1262
	enum flow_block_binder_type binder_type;
1263 1264 1265 1266 1267
};

static void
tcf_block_owner_netif_keep_dst(struct tcf_block *block,
			       struct Qdisc *q,
1268
			       enum flow_block_binder_type binder_type)
1269 1270
{
	if (block->keep_dst &&
1271 1272
	    binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
	    binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
		netif_keep_dst(qdisc_dev(q));
}

void tcf_block_netif_keep_dst(struct tcf_block *block)
{
	struct tcf_block_owner_item *item;

	block->keep_dst = true;
	list_for_each_entry(item, &block->owner_list, list)
		tcf_block_owner_netif_keep_dst(block, item->q,
					       item->binder_type);
}
EXPORT_SYMBOL(tcf_block_netif_keep_dst);

static int tcf_block_owner_add(struct tcf_block *block,
			       struct Qdisc *q,
1289
			       enum flow_block_binder_type binder_type)
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
{
	struct tcf_block_owner_item *item;

	item = kmalloc(sizeof(*item), GFP_KERNEL);
	if (!item)
		return -ENOMEM;
	item->q = q;
	item->binder_type = binder_type;
	list_add(&item->list, &block->owner_list);
	return 0;
}

static void tcf_block_owner_del(struct tcf_block *block,
				struct Qdisc *q,
1304
				enum flow_block_binder_type binder_type)
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
{
	struct tcf_block_owner_item *item;

	list_for_each_entry(item, &block->owner_list, list) {
		if (item->q == q && item->binder_type == binder_type) {
			list_del(&item->list);
			kfree(item);
			return;
		}
	}
	WARN_ON(1);
}

1318 1319 1320 1321 1322 1323 1324 1325
int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
		      struct tcf_block_ext_info *ei,
		      struct netlink_ext_ack *extack)
{
	struct net *net = qdisc_net(q);
	struct tcf_block *block = NULL;
	int err;

1326
	if (ei->block_index)
1327
		/* block_index not 0 means the shared block is requested */
1328
		block = tcf_block_refcnt_get(net, ei->block_index);
1329 1330

	if (!block) {
1331
		block = tcf_block_create(net, q, ei->block_index, extack);
1332 1333
		if (IS_ERR(block))
			return PTR_ERR(block);
1334 1335
		if (tcf_block_shared(block)) {
			err = tcf_block_insert(block, net, extack);
1336 1337 1338 1339 1340
			if (err)
				goto err_block_insert;
		}
	}

1341 1342 1343 1344 1345 1346
	err = tcf_block_owner_add(block, q, ei->binder_type);
	if (err)
		goto err_block_owner_add;

	tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);

1347
	err = tcf_chain0_head_change_cb_add(block, ei, extack);
1348
	if (err)
1349
		goto err_chain0_head_change_cb_add;
1350

1351
	err = tcf_block_offload_bind(block, q, ei, extack);
1352 1353 1354
	if (err)
		goto err_block_offload_bind;

1355 1356
	*p_block = block;
	return 0;
1357

1358
err_block_offload_bind:
1359 1360
	tcf_chain0_head_change_cb_del(block, ei);
err_chain0_head_change_cb_add:
1361 1362
	tcf_block_owner_del(block, q, ei->binder_type);
err_block_owner_add:
1363
err_block_insert:
1364
	tcf_block_refcnt_put(block, true);
1365
	return err;
1366
}
1367 1368
EXPORT_SYMBOL(tcf_block_get_ext);

1369 1370 1371 1372 1373 1374 1375
static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
{
	struct tcf_proto __rcu **p_filter_chain = priv;

	rcu_assign_pointer(*p_filter_chain, tp_head);
}

1376
int tcf_block_get(struct tcf_block **p_block,
1377 1378
		  struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
		  struct netlink_ext_ack *extack)
1379
{
1380 1381 1382 1383
	struct tcf_block_ext_info ei = {
		.chain_head_change = tcf_chain_head_change_dflt,
		.chain_head_change_priv = p_filter_chain,
	};
1384

1385
	WARN_ON(!p_filter_chain);
1386
	return tcf_block_get_ext(p_block, q, &ei, extack);
1387
}
1388 1389
EXPORT_SYMBOL(tcf_block_get);

1390
/* XXX: Standalone actions are not allowed to jump to any chain, and bound
1391
 * actions should be all removed after flushing.
1392
 */
1393
void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
1394
		       struct tcf_block_ext_info *ei)
1395
{
1396 1397
	if (!block)
		return;
1398
	tcf_chain0_head_change_cb_del(block, ei);
1399
	tcf_block_owner_del(block, q, ei->binder_type);
1400

1401
	__tcf_block_put(block, q, ei, true);
1402
}
1403 1404 1405 1406 1407 1408
EXPORT_SYMBOL(tcf_block_put_ext);

void tcf_block_put(struct tcf_block *block)
{
	struct tcf_block_ext_info ei = {0, };

1409 1410
	if (!block)
		return;
1411
	tcf_block_put_ext(block, block->q, &ei);
1412
}
1413

1414
EXPORT_SYMBOL(tcf_block_put);
1415

1416
static int
1417
tcf_block_playback_offloads(struct tcf_block *block, flow_setup_cb_t *cb,
1418 1419 1420
			    void *cb_priv, bool add, bool offload_in_use,
			    struct netlink_ext_ack *extack)
{
1421
	struct tcf_chain *chain, *chain_prev;
1422
	struct tcf_proto *tp, *tp_prev;
1423 1424
	int err;

1425 1426
	lockdep_assert_held(&block->cb_lock);

1427 1428 1429 1430 1431
	for (chain = __tcf_get_next_chain(block, NULL);
	     chain;
	     chain_prev = chain,
		     chain = __tcf_get_next_chain(block, chain),
		     tcf_chain_put(chain_prev)) {
1432 1433 1434
		for (tp = __tcf_get_next_proto(chain, NULL); tp;
		     tp_prev = tp,
			     tp = __tcf_get_next_proto(chain, tp),
1435
			     tcf_proto_put(tp_prev, true, NULL)) {
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
			if (tp->ops->reoffload) {
				err = tp->ops->reoffload(tp, add, cb, cb_priv,
							 extack);
				if (err && add)
					goto err_playback_remove;
			} else if (add && offload_in_use) {
				err = -EOPNOTSUPP;
				NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
				goto err_playback_remove;
			}
		}
	}

	return 0;

err_playback_remove:
1452
	tcf_proto_put(tp, true, NULL);
1453
	tcf_chain_put(chain);
1454 1455 1456 1457 1458
	tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
				    extack);
	return err;
}

1459 1460 1461 1462 1463 1464
static int tcf_block_bind(struct tcf_block *block,
			  struct flow_block_offload *bo)
{
	struct flow_block_cb *block_cb, *next;
	int err, i = 0;

1465 1466
	lockdep_assert_held(&block->cb_lock);

1467 1468 1469 1470 1471 1472 1473
	list_for_each_entry(block_cb, &bo->cb_list, list) {
		err = tcf_block_playback_offloads(block, block_cb->cb,
						  block_cb->cb_priv, true,
						  tcf_block_offload_in_use(block),
						  bo->extack);
		if (err)
			goto err_unroll;
1474 1475
		if (!bo->unlocked_driver_cb)
			block->lockeddevcnt++;
1476 1477 1478

		i++;
	}
1479
	list_splice(&bo->cb_list, &block->flow_block.cb_list);
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490

	return 0;

err_unroll:
	list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
		if (i-- > 0) {
			list_del(&block_cb->list);
			tcf_block_playback_offloads(block, block_cb->cb,
						    block_cb->cb_priv, false,
						    tcf_block_offload_in_use(block),
						    NULL);
1491 1492
			if (!bo->unlocked_driver_cb)
				block->lockeddevcnt--;
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
		}
		flow_block_cb_free(block_cb);
	}

	return err;
}

static void tcf_block_unbind(struct tcf_block *block,
			     struct flow_block_offload *bo)
{
	struct flow_block_cb *block_cb, *next;

1505 1506
	lockdep_assert_held(&block->cb_lock);

1507 1508 1509 1510 1511 1512 1513
	list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
		tcf_block_playback_offloads(block, block_cb->cb,
					    block_cb->cb_priv, false,
					    tcf_block_offload_in_use(block),
					    NULL);
		list_del(&block_cb->list);
		flow_block_cb_free(block_cb);
1514 1515
		if (!bo->unlocked_driver_cb)
			block->lockeddevcnt--;
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
	}
}

static int tcf_block_setup(struct tcf_block *block,
			   struct flow_block_offload *bo)
{
	int err;

	switch (bo->command) {
	case FLOW_BLOCK_BIND:
		err = tcf_block_bind(block, bo);
		break;
	case FLOW_BLOCK_UNBIND:
		err = 0;
		tcf_block_unbind(block, bo);
		break;
	default:
		WARN_ON_ONCE(1);
		err = -EOPNOTSUPP;
	}

	return err;
}

1540 1541 1542 1543
/* Main classifier routine: scans classifier chain attached
 * to this qdisc, (optionally) tests for protocol and asks
 * specific classifiers.
 */
1544 1545
static inline int __tcf_classify(struct sk_buff *skb,
				 const struct tcf_proto *tp,
1546
				 const struct tcf_proto *orig_tp,
1547 1548 1549
				 struct tcf_result *res,
				 bool compat_mode,
				 u32 *last_executed_chain)
1550 1551
{
#ifdef CONFIG_NET_CLS_ACT
1552
	const int max_reclassify_loop = 16;
1553
	const struct tcf_proto *first_tp;
1554 1555 1556 1557 1558
	int limit = 0;

reclassify:
#endif
	for (; tp; tp = rcu_dereference_bh(tp->next)) {
1559
		__be16 protocol = skb_protocol(skb, false);
1560 1561 1562 1563 1564 1565 1566 1567
		int err;

		if (tp->protocol != protocol &&
		    tp->protocol != htons(ETH_P_ALL))
			continue;

		err = tp->classify(skb, tp, res);
#ifdef CONFIG_NET_CLS_ACT
1568
		if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
1569
			first_tp = orig_tp;
1570
			*last_executed_chain = first_tp->chain->index;
1571
			goto reset;
1572
		} else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
1573
			first_tp = res->goto_tp;
1574
			*last_executed_chain = err & TC_ACT_EXT_VAL_MASK;
1575 1576
			goto reset;
		}
1577 1578 1579 1580 1581 1582 1583 1584 1585
#endif
		if (err >= 0)
			return err;
	}

	return TC_ACT_UNSPEC; /* signal: continue lookup */
#ifdef CONFIG_NET_CLS_ACT
reset:
	if (unlikely(limit++ >= max_reclassify_loop)) {
1586 1587 1588
		net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
				       tp->chain->block->index,
				       tp->prio & 0xffff,
1589 1590 1591 1592
				       ntohs(tp->protocol));
		return TC_ACT_SHOT;
	}

1593
	tp = first_tp;
1594 1595 1596
	goto reclassify;
#endif
}
1597

1598 1599 1600
int tcf_classify(struct sk_buff *skb,
		 const struct tcf_block *block,
		 const struct tcf_proto *tp,
1601 1602 1603 1604 1605
		 struct tcf_result *res, bool compat_mode)
{
#if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
	u32 last_executed_chain = 0;

1606
	return __tcf_classify(skb, tp, tp, res, compat_mode,
1607 1608 1609
			      &last_executed_chain);
#else
	u32 last_executed_chain = tp ? tp->chain->index : 0;
1610
	const struct tcf_proto *orig_tp = tp;
1611 1612 1613
	struct tc_skb_ext *ext;
	int ret;

1614 1615
	if (block) {
		ext = skb_ext_find(skb, TC_SKB_EXT);
1616

1617 1618
		if (ext && ext->chain) {
			struct tcf_chain *fchain;
1619

1620 1621 1622
			fchain = tcf_chain_lookup_rcu(block, ext->chain);
			if (!fchain)
				return TC_ACT_SHOT;
1623

1624 1625
			/* Consume, so cloned/redirect skbs won't inherit ext */
			skb_ext_del(skb, TC_SKB_EXT);
1626

1627 1628 1629
			tp = rcu_dereference_bh(fchain->filter_chain);
			last_executed_chain = fchain->index;
		}
1630 1631 1632 1633
	}

	ret = __tcf_classify(skb, tp, orig_tp, res, compat_mode,
			     &last_executed_chain);
1634

1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
	if (tc_skb_ext_tc_enabled()) {
		/* If we missed on some chain */
		if (ret == TC_ACT_UNSPEC && last_executed_chain) {
			struct tc_skb_cb *cb = tc_skb_cb(skb);

			ext = tc_skb_ext_alloc(skb);
			if (WARN_ON_ONCE(!ext))
				return TC_ACT_SHOT;
			ext->chain = last_executed_chain;
			ext->mru = cb->mru;
			ext->post_ct = cb->post_ct;
			ext->post_ct_snat = cb->post_ct_snat;
			ext->post_ct_dnat = cb->post_ct_dnat;
			ext->zone = cb->zone;
		}
1650 1651 1652 1653 1654
	}

	return ret;
#endif
}
1655
EXPORT_SYMBOL(tcf_classify);
1656

1657 1658 1659 1660 1661
struct tcf_chain_info {
	struct tcf_proto __rcu **pprev;
	struct tcf_proto __rcu *next;
};

1662 1663
static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain *chain,
					   struct tcf_chain_info *chain_info)
1664
{
1665
	return tcf_chain_dereference(*chain_info->pprev, chain);
1666 1667
}

1668 1669 1670
static int tcf_chain_tp_insert(struct tcf_chain *chain,
			       struct tcf_chain_info *chain_info,
			       struct tcf_proto *tp)
1671
{
1672 1673 1674
	if (chain->flushing)
		return -EAGAIN;

1675
	RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info));
1676
	if (*chain_info->pprev == chain->filter_chain)
1677
		tcf_chain0_head_change(chain, tp);
1678
	tcf_proto_get(tp);
1679
	rcu_assign_pointer(*chain_info->pprev, tp);
1680 1681

	return 0;
1682 1683 1684 1685 1686 1687
}

static void tcf_chain_tp_remove(struct tcf_chain *chain,
				struct tcf_chain_info *chain_info,
				struct tcf_proto *tp)
{
1688
	struct tcf_proto *next = tcf_chain_dereference(chain_info->next, chain);
1689

1690
	tcf_proto_mark_delete(tp);
1691
	if (tp == chain->filter_chain)
1692
		tcf_chain0_head_change(chain, next);
1693 1694 1695
	RCU_INIT_POINTER(*chain_info->pprev, next);
}

1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707
static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
					   struct tcf_chain_info *chain_info,
					   u32 protocol, u32 prio,
					   bool prio_allocate);

/* Try to insert new proto.
 * If proto with specified priority already exists, free new proto
 * and return existing one.
 */

static struct tcf_proto *tcf_chain_tp_insert_unique(struct tcf_chain *chain,
						    struct tcf_proto *tp_new,
1708 1709
						    u32 protocol, u32 prio,
						    bool rtnl_held)
1710 1711 1712
{
	struct tcf_chain_info chain_info;
	struct tcf_proto *tp;
1713
	int err = 0;
1714 1715 1716

	mutex_lock(&chain->filter_chain_lock);

1717 1718 1719 1720 1721 1722
	if (tcf_proto_exists_destroying(chain, tp_new)) {
		mutex_unlock(&chain->filter_chain_lock);
		tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
		return ERR_PTR(-EAGAIN);
	}

1723 1724 1725
	tp = tcf_chain_tp_find(chain, &chain_info,
			       protocol, prio, false);
	if (!tp)
1726
		err = tcf_chain_tp_insert(chain, &chain_info, tp_new);
1727 1728 1729
	mutex_unlock(&chain->filter_chain_lock);

	if (tp) {
1730
		tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1731
		tp_new = tp;
1732
	} else if (err) {
1733
		tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1734
		tp_new = ERR_PTR(err);
1735 1736 1737 1738 1739 1740
	}

	return tp_new;
}

static void tcf_chain_tp_delete_empty(struct tcf_chain *chain,
1741
				      struct tcf_proto *tp, bool rtnl_held,
1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
				      struct netlink_ext_ack *extack)
{
	struct tcf_chain_info chain_info;
	struct tcf_proto *tp_iter;
	struct tcf_proto **pprev;
	struct tcf_proto *next;

	mutex_lock(&chain->filter_chain_lock);

	/* Atomically find and remove tp from chain. */
	for (pprev = &chain->filter_chain;
	     (tp_iter = tcf_chain_dereference(*pprev, chain));
	     pprev = &tp_iter->next) {
		if (tp_iter == tp) {
			chain_info.pprev = pprev;
			chain_info.next = tp_iter->next;
			WARN_ON(tp_iter->deleting);
			break;
		}
	}
	/* Verify that tp still exists and no new filters were inserted
	 * concurrently.
	 * Mark tp for deletion if it is empty.
	 */
1766
	if (!tp_iter || !tcf_proto_check_delete(tp)) {
1767 1768 1769 1770
		mutex_unlock(&chain->filter_chain_lock);
		return;
	}

1771
	tcf_proto_signal_destroying(chain, tp);
1772 1773 1774 1775 1776 1777
	next = tcf_chain_dereference(chain_info.next, chain);
	if (tp == chain->filter_chain)
		tcf_chain0_head_change(chain, next);
	RCU_INIT_POINTER(*chain_info.pprev, next);
	mutex_unlock(&chain->filter_chain_lock);

1778
	tcf_proto_put(tp, rtnl_held, extack);
1779 1780
}

1781 1782 1783 1784 1785 1786 1787 1788 1789 1790
static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
					   struct tcf_chain_info *chain_info,
					   u32 protocol, u32 prio,
					   bool prio_allocate)
{
	struct tcf_proto **pprev;
	struct tcf_proto *tp;

	/* Check the chain for existence of proto-tcf with this priority */
	for (pprev = &chain->filter_chain;
1791 1792
	     (tp = tcf_chain_dereference(*pprev, chain));
	     pprev = &tp->next) {
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804
		if (tp->prio >= prio) {
			if (tp->prio == prio) {
				if (prio_allocate ||
				    (tp->protocol != protocol && protocol))
					return ERR_PTR(-EINVAL);
			} else {
				tp = NULL;
			}
			break;
		}
	}
	chain_info->pprev = pprev;
1805 1806 1807 1808 1809 1810
	if (tp) {
		chain_info->next = tp->next;
		tcf_proto_get(tp);
	} else {
		chain_info->next = NULL;
	}
1811 1812 1813
	return tp;
}

1814
static int tcf_fill_node(struct net *net, struct sk_buff *skb,
1815 1816
			 struct tcf_proto *tp, struct tcf_block *block,
			 struct Qdisc *q, u32 parent, void *fh,
1817
			 u32 portid, u32 seq, u16 flags, int event,
1818
			 bool terse_dump, bool rtnl_held)
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
{
	struct tcmsg *tcm;
	struct nlmsghdr  *nlh;
	unsigned char *b = skb_tail_pointer(skb);

	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
	if (!nlh)
		goto out_nlmsg_trim;
	tcm = nlmsg_data(nlh);
	tcm->tcm_family = AF_UNSPEC;
	tcm->tcm__pad1 = 0;
	tcm->tcm__pad2 = 0;
1831 1832 1833 1834 1835 1836 1837
	if (q) {
		tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
		tcm->tcm_parent = parent;
	} else {
		tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
		tcm->tcm_block_index = block->index;
	}
1838 1839 1840 1841 1842 1843 1844
	tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
	if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
		goto nla_put_failure;
	if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
		goto nla_put_failure;
	if (!fh) {
		tcm->tcm_handle = 0;
1845 1846 1847 1848 1849 1850 1851 1852
	} else if (terse_dump) {
		if (tp->ops->terse_dump) {
			if (tp->ops->terse_dump(net, tp, fh, skb, tcm,
						rtnl_held) < 0)
				goto nla_put_failure;
		} else {
			goto cls_op_not_supp;
		}
1853
	} else {
1854 1855
		if (tp->ops->dump &&
		    tp->ops->dump(net, tp, fh, skb, tcm, rtnl_held) < 0)
1856 1857 1858 1859 1860 1861 1862
			goto nla_put_failure;
	}
	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
	return skb->len;

out_nlmsg_trim:
nla_put_failure:
1863
cls_op_not_supp:
1864 1865 1866 1867 1868 1869
	nlmsg_trim(skb, b);
	return -1;
}

static int tfilter_notify(struct net *net, struct sk_buff *oskb,
			  struct nlmsghdr *n, struct tcf_proto *tp,
1870
			  struct tcf_block *block, struct Qdisc *q,
1871 1872
			  u32 parent, void *fh, int event, bool unicast,
			  bool rtnl_held)
1873 1874 1875
{
	struct sk_buff *skb;
	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1876
	int err = 0;
1877 1878 1879 1880 1881

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

1882
	if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1883
			  n->nlmsg_seq, n->nlmsg_flags, event,
1884
			  false, rtnl_held) <= 0) {
1885 1886 1887 1888 1889
		kfree_skb(skb);
		return -EINVAL;
	}

	if (unicast)
1890
		err = rtnl_unicast(skb, net, portid);
1891 1892 1893 1894
	else
		err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
				     n->nlmsg_flags & NLM_F_ECHO);
	return err;
1895 1896 1897 1898
}

static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
			      struct nlmsghdr *n, struct tcf_proto *tp,
1899
			      struct tcf_block *block, struct Qdisc *q,
1900
			      u32 parent, void *fh, bool unicast, bool *last,
1901
			      bool rtnl_held, struct netlink_ext_ack *extack)
1902 1903 1904 1905 1906 1907 1908 1909 1910
{
	struct sk_buff *skb;
	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
	int err;

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

1911
	if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1912
			  n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER,
1913
			  false, rtnl_held) <= 0) {
1914
		NL_SET_ERR_MSG(extack, "Failed to build del event notification");
1915 1916 1917 1918
		kfree_skb(skb);
		return -EINVAL;
	}

1919
	err = tp->ops->delete(tp, fh, last, rtnl_held, extack);
1920 1921 1922 1923 1924 1925
	if (err) {
		kfree_skb(skb);
		return err;
	}

	if (unicast)
1926
		err = rtnl_unicast(skb, net, portid);
1927 1928 1929
	else
		err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
				     n->nlmsg_flags & NLM_F_ECHO);
1930 1931
	if (err < 0)
		NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1932

1933
	return err;
1934 1935 1936
}

static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
1937 1938
				 struct tcf_block *block, struct Qdisc *q,
				 u32 parent, struct nlmsghdr *n,
1939
				 struct tcf_chain *chain, int event)
1940 1941 1942
{
	struct tcf_proto *tp;

1943 1944
	for (tp = tcf_get_next_proto(chain, NULL);
	     tp; tp = tcf_get_next_proto(chain, tp))
1945
		tfilter_notify(net, oskb, n, tp, block,
1946
			       q, parent, NULL, event, false, true);
1947 1948
}

1949 1950 1951 1952 1953 1954
static void tfilter_put(struct tcf_proto *tp, void *fh)
{
	if (tp->ops->put && fh)
		tp->ops->put(tp, fh);
}

1955
static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1956
			  struct netlink_ext_ack *extack)
L
Linus Torvalds 已提交
1957
{
1958
	struct net *net = sock_net(skb->sk);
1959
	struct nlattr *tca[TCA_MAX + 1];
1960
	char name[IFNAMSIZ];
L
Linus Torvalds 已提交
1961 1962 1963
	struct tcmsg *t;
	u32 protocol;
	u32 prio;
1964
	bool prio_allocate;
L
Linus Torvalds 已提交
1965
	u32 parent;
1966
	u32 chain_index;
1967
	struct Qdisc *q;
1968
	struct tcf_chain_info chain_info;
1969
	struct tcf_chain *chain;
1970
	struct tcf_block *block;
L
Linus Torvalds 已提交
1971 1972
	struct tcf_proto *tp;
	unsigned long cl;
1973
	void *fh;
L
Linus Torvalds 已提交
1974
	int err;
1975
	int tp_created;
1976
	bool rtnl_held = false;
1977
	u32 flags;
L
Linus Torvalds 已提交
1978

1979
	if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1980
		return -EPERM;
1981

L
Linus Torvalds 已提交
1982
replay:
1983 1984
	tp_created = 0;

1985 1986
	err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
				     rtm_tca_policy, extack);
1987 1988 1989
	if (err < 0)
		return err;

1990
	t = nlmsg_data(n);
L
Linus Torvalds 已提交
1991 1992
	protocol = TC_H_MIN(t->tcm_info);
	prio = TC_H_MAJ(t->tcm_info);
1993
	prio_allocate = false;
L
Linus Torvalds 已提交
1994
	parent = t->tcm_parent;
1995
	tp = NULL;
L
Linus Torvalds 已提交
1996
	cl = 0;
1997
	block = NULL;
1998 1999
	q = NULL;
	chain = NULL;
2000
	flags = 0;
L
Linus Torvalds 已提交
2001 2002

	if (prio == 0) {
2003 2004 2005 2006 2007 2008 2009
		/* If no priority is provided by the user,
		 * we allocate one.
		 */
		if (n->nlmsg_flags & NLM_F_CREATE) {
			prio = TC_H_MAKE(0x80000000U, 0U);
			prio_allocate = true;
		} else {
2010
			NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
L
Linus Torvalds 已提交
2011
			return -ENOENT;
2012
		}
L
Linus Torvalds 已提交
2013 2014 2015 2016
	}

	/* Find head of filter chain. */

2017 2018 2019 2020
	err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
	if (err)
		return err;

2021 2022 2023 2024 2025 2026
	if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
		NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
		err = -EINVAL;
		goto errout;
	}

2027 2028 2029 2030 2031 2032
	/* Take rtnl mutex if rtnl_held was set to true on previous iteration,
	 * block is shared (no qdisc found), qdisc is not unlocked, classifier
	 * type is not specified, classifier is not unlocked.
	 */
	if (rtnl_held ||
	    (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2033
	    !tcf_proto_is_unlocked(name)) {
2034 2035 2036 2037 2038 2039 2040 2041 2042 2043
		rtnl_held = true;
		rtnl_lock();
	}

	err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
	if (err)
		goto errout;

	block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
				 extack);
2044 2045 2046
	if (IS_ERR(block)) {
		err = PTR_ERR(block);
		goto errout;
2047
	}
2048
	block->classid = parent;
2049 2050 2051

	chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
	if (chain_index > TC_ACT_EXT_VAL_MASK) {
2052
		NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2053 2054 2055
		err = -EINVAL;
		goto errout;
	}
2056
	chain = tcf_chain_get(block, chain_index, true);
2057
	if (!chain) {
2058
		NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
2059
		err = -ENOMEM;
2060 2061
		goto errout;
	}
L
Linus Torvalds 已提交
2062

2063
	mutex_lock(&chain->filter_chain_lock);
2064 2065 2066
	tp = tcf_chain_tp_find(chain, &chain_info, protocol,
			       prio, prio_allocate);
	if (IS_ERR(tp)) {
2067
		NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2068
		err = PTR_ERR(tp);
2069
		goto errout_locked;
L
Linus Torvalds 已提交
2070 2071 2072
	}

	if (tp == NULL) {
2073 2074
		struct tcf_proto *tp_new = NULL;

2075 2076 2077 2078 2079
		if (chain->flushing) {
			err = -EAGAIN;
			goto errout_locked;
		}

L
Linus Torvalds 已提交
2080 2081
		/* Proto-tcf does not exist, create new one */

2082
		if (tca[TCA_KIND] == NULL || !protocol) {
2083
			NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
2084
			err = -EINVAL;
2085
			goto errout_locked;
2086
		}
L
Linus Torvalds 已提交
2087

2088
		if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2089
			NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2090
			err = -ENOENT;
2091
			goto errout_locked;
2092
		}
L
Linus Torvalds 已提交
2093

2094
		if (prio_allocate)
2095 2096
			prio = tcf_auto_prio(tcf_chain_tp_prev(chain,
							       &chain_info));
L
Linus Torvalds 已提交
2097

2098
		mutex_unlock(&chain->filter_chain_lock);
2099 2100
		tp_new = tcf_proto_create(name, protocol, prio, chain,
					  rtnl_held, extack);
2101 2102
		if (IS_ERR(tp_new)) {
			err = PTR_ERR(tp_new);
2103
			goto errout_tp;
L
Linus Torvalds 已提交
2104
		}
2105

2106
		tp_created = 1;
2107 2108
		tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio,
						rtnl_held);
2109 2110 2111 2112
		if (IS_ERR(tp)) {
			err = PTR_ERR(tp);
			goto errout_tp;
		}
2113 2114
	} else {
		mutex_unlock(&chain->filter_chain_lock);
2115
	}
L
Linus Torvalds 已提交
2116

2117 2118 2119 2120 2121 2122
	if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
		NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
		err = -EINVAL;
		goto errout;
	}

L
Linus Torvalds 已提交
2123 2124
	fh = tp->ops->get(tp, t->tcm_handle);

2125
	if (!fh) {
2126
		if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2127
			NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2128
			err = -ENOENT;
L
Linus Torvalds 已提交
2129
			goto errout;
2130
		}
2131
	} else if (n->nlmsg_flags & NLM_F_EXCL) {
2132
		tfilter_put(tp, fh);
2133 2134 2135
		NL_SET_ERR_MSG(extack, "Filter already exists");
		err = -EEXIST;
		goto errout;
L
Linus Torvalds 已提交
2136 2137
	}

2138 2139 2140 2141 2142 2143
	if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
		NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
		err = -EINVAL;
		goto errout;
	}

2144 2145 2146 2147
	if (!(n->nlmsg_flags & NLM_F_CREATE))
		flags |= TCA_ACT_FLAGS_REPLACE;
	if (!rtnl_held)
		flags |= TCA_ACT_FLAGS_NO_RTNL;
2148
	err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
2149
			      flags, extack);
2150
	if (err == 0) {
2151
		tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2152
			       RTM_NEWTFILTER, false, rtnl_held);
2153
		tfilter_put(tp, fh);
2154 2155 2156
		/* q pointer is NULL for shared blocks */
		if (q)
			q->flags &= ~TCQ_F_CAN_BYPASS;
2157
	}
L
Linus Torvalds 已提交
2158 2159

errout:
2160
	if (err && tp_created)
2161
		tcf_chain_tp_delete_empty(chain, tp, rtnl_held, NULL);
2162
errout_tp:
2163 2164
	if (chain) {
		if (tp && !IS_ERR(tp))
2165
			tcf_proto_put(tp, rtnl_held, NULL);
2166 2167 2168
		if (!tp_created)
			tcf_chain_put(chain);
	}
2169
	tcf_block_release(q, block, rtnl_held);
2170 2171 2172 2173 2174 2175 2176 2177 2178

	if (rtnl_held)
		rtnl_unlock();

	if (err == -EAGAIN) {
		/* Take rtnl lock in case EAGAIN is caused by concurrent flush
		 * of target chain.
		 */
		rtnl_held = true;
L
Linus Torvalds 已提交
2179 2180
		/* Replay the request. */
		goto replay;
2181
	}
L
Linus Torvalds 已提交
2182
	return err;
2183 2184 2185 2186

errout_locked:
	mutex_unlock(&chain->filter_chain_lock);
	goto errout;
L
Linus Torvalds 已提交
2187 2188
}

2189 2190 2191 2192 2193
static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
			  struct netlink_ext_ack *extack)
{
	struct net *net = sock_net(skb->sk);
	struct nlattr *tca[TCA_MAX + 1];
2194
	char name[IFNAMSIZ];
2195 2196 2197 2198 2199 2200 2201 2202
	struct tcmsg *t;
	u32 protocol;
	u32 prio;
	u32 parent;
	u32 chain_index;
	struct Qdisc *q = NULL;
	struct tcf_chain_info chain_info;
	struct tcf_chain *chain = NULL;
2203
	struct tcf_block *block = NULL;
2204 2205 2206 2207
	struct tcf_proto *tp = NULL;
	unsigned long cl = 0;
	void *fh = NULL;
	int err;
2208
	bool rtnl_held = false;
2209 2210 2211 2212

	if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
		return -EPERM;

2213 2214
	err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
				     rtm_tca_policy, extack);
2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229
	if (err < 0)
		return err;

	t = nlmsg_data(n);
	protocol = TC_H_MIN(t->tcm_info);
	prio = TC_H_MAJ(t->tcm_info);
	parent = t->tcm_parent;

	if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
		NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
		return -ENOENT;
	}

	/* Find head of filter chain. */

2230 2231 2232 2233
	err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
	if (err)
		return err;

2234 2235 2236 2237 2238
	if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
		NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
		err = -EINVAL;
		goto errout;
	}
2239 2240 2241 2242 2243 2244
	/* Take rtnl mutex if flushing whole chain, block is shared (no qdisc
	 * found), qdisc is not unlocked, classifier type is not specified,
	 * classifier is not unlocked.
	 */
	if (!prio ||
	    (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2245
	    !tcf_proto_is_unlocked(name)) {
2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
		rtnl_held = true;
		rtnl_lock();
	}

	err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
	if (err)
		goto errout;

	block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
				 extack);
2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268
	if (IS_ERR(block)) {
		err = PTR_ERR(block);
		goto errout;
	}

	chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
	if (chain_index > TC_ACT_EXT_VAL_MASK) {
		NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
		err = -EINVAL;
		goto errout;
	}
	chain = tcf_chain_get(block, chain_index, false);
	if (!chain) {
2269 2270 2271 2272 2273 2274 2275
		/* User requested flush on non-existent chain. Nothing to do,
		 * so just return success.
		 */
		if (prio == 0) {
			err = 0;
			goto errout;
		}
2276
		NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2277
		err = -ENOENT;
2278 2279 2280 2281 2282
		goto errout;
	}

	if (prio == 0) {
		tfilter_notify_chain(net, skb, block, q, parent, n,
2283
				     chain, RTM_DELTFILTER);
2284
		tcf_chain_flush(chain, rtnl_held);
2285 2286 2287 2288
		err = 0;
		goto errout;
	}

2289
	mutex_lock(&chain->filter_chain_lock);
2290 2291 2292 2293
	tp = tcf_chain_tp_find(chain, &chain_info, protocol,
			       prio, false);
	if (!tp || IS_ERR(tp)) {
		NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2294
		err = tp ? PTR_ERR(tp) : -ENOENT;
2295
		goto errout_locked;
2296 2297 2298
	} else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
		NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
		err = -EINVAL;
2299 2300
		goto errout_locked;
	} else if (t->tcm_handle == 0) {
2301
		tcf_proto_signal_destroying(chain, tp);
2302 2303 2304
		tcf_chain_tp_remove(chain, &chain_info, tp);
		mutex_unlock(&chain->filter_chain_lock);

2305
		tcf_proto_put(tp, rtnl_held, NULL);
2306
		tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2307
			       RTM_DELTFILTER, false, rtnl_held);
2308
		err = 0;
2309 2310
		goto errout;
	}
2311
	mutex_unlock(&chain->filter_chain_lock);
2312 2313 2314 2315

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

	if (!fh) {
2316 2317
		NL_SET_ERR_MSG(extack, "Specified filter handle not found");
		err = -ENOENT;
2318 2319 2320 2321 2322
	} else {
		bool last;

		err = tfilter_del_notify(net, skb, n, tp, block,
					 q, parent, fh, false, &last,
2323 2324
					 rtnl_held, extack);

2325 2326
		if (err)
			goto errout;
2327
		if (last)
2328
			tcf_chain_tp_delete_empty(chain, tp, rtnl_held, extack);
2329 2330 2331
	}

errout:
2332 2333
	if (chain) {
		if (tp && !IS_ERR(tp))
2334
			tcf_proto_put(tp, rtnl_held, NULL);
2335
		tcf_chain_put(chain);
2336
	}
2337
	tcf_block_release(q, block, rtnl_held);
2338 2339 2340 2341

	if (rtnl_held)
		rtnl_unlock();

2342
	return err;
2343 2344 2345 2346

errout_locked:
	mutex_unlock(&chain->filter_chain_lock);
	goto errout;
2347 2348 2349 2350 2351 2352 2353
}

static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
			  struct netlink_ext_ack *extack)
{
	struct net *net = sock_net(skb->sk);
	struct nlattr *tca[TCA_MAX + 1];
2354
	char name[IFNAMSIZ];
2355 2356 2357 2358 2359 2360 2361 2362
	struct tcmsg *t;
	u32 protocol;
	u32 prio;
	u32 parent;
	u32 chain_index;
	struct Qdisc *q = NULL;
	struct tcf_chain_info chain_info;
	struct tcf_chain *chain = NULL;
2363
	struct tcf_block *block = NULL;
2364 2365 2366 2367
	struct tcf_proto *tp = NULL;
	unsigned long cl = 0;
	void *fh = NULL;
	int err;
2368
	bool rtnl_held = false;
2369

2370 2371
	err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
				     rtm_tca_policy, extack);
2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386
	if (err < 0)
		return err;

	t = nlmsg_data(n);
	protocol = TC_H_MIN(t->tcm_info);
	prio = TC_H_MAJ(t->tcm_info);
	parent = t->tcm_parent;

	if (prio == 0) {
		NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
		return -ENOENT;
	}

	/* Find head of filter chain. */

2387 2388 2389 2390
	err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
	if (err)
		return err;

2391 2392 2393 2394 2395
	if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
		NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
		err = -EINVAL;
		goto errout;
	}
2396 2397 2398 2399 2400
	/* Take rtnl mutex if block is shared (no qdisc found), qdisc is not
	 * unlocked, classifier type is not specified, classifier is not
	 * unlocked.
	 */
	if ((q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2401
	    !tcf_proto_is_unlocked(name)) {
2402 2403 2404 2405 2406 2407 2408 2409 2410 2411
		rtnl_held = true;
		rtnl_lock();
	}

	err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
	if (err)
		goto errout;

	block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
				 extack);
2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429
	if (IS_ERR(block)) {
		err = PTR_ERR(block);
		goto errout;
	}

	chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
	if (chain_index > TC_ACT_EXT_VAL_MASK) {
		NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
		err = -EINVAL;
		goto errout;
	}
	chain = tcf_chain_get(block, chain_index, false);
	if (!chain) {
		NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
		err = -EINVAL;
		goto errout;
	}

2430
	mutex_lock(&chain->filter_chain_lock);
2431 2432
	tp = tcf_chain_tp_find(chain, &chain_info, protocol,
			       prio, false);
2433
	mutex_unlock(&chain->filter_chain_lock);
2434 2435
	if (!tp || IS_ERR(tp)) {
		NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2436
		err = tp ? PTR_ERR(tp) : -ENOENT;
2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450
		goto errout;
	} else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
		NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
		err = -EINVAL;
		goto errout;
	}

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

	if (!fh) {
		NL_SET_ERR_MSG(extack, "Specified filter handle not found");
		err = -ENOENT;
	} else {
		err = tfilter_notify(net, skb, n, tp, block, q, parent,
2451
				     fh, RTM_NEWTFILTER, true, rtnl_held);
2452 2453 2454 2455
		if (err < 0)
			NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
	}

2456
	tfilter_put(tp, fh);
2457
errout:
2458 2459
	if (chain) {
		if (tp && !IS_ERR(tp))
2460
			tcf_proto_put(tp, rtnl_held, NULL);
2461
		tcf_chain_put(chain);
2462
	}
2463
	tcf_block_release(q, block, rtnl_held);
2464 2465 2466 2467

	if (rtnl_held)
		rtnl_unlock();

2468 2469 2470
	return err;
}

2471
struct tcf_dump_args {
L
Linus Torvalds 已提交
2472 2473 2474
	struct tcf_walker w;
	struct sk_buff *skb;
	struct netlink_callback *cb;
2475
	struct tcf_block *block;
2476 2477
	struct Qdisc *q;
	u32 parent;
2478
	bool terse_dump;
L
Linus Torvalds 已提交
2479 2480
};

2481
static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
L
Linus Torvalds 已提交
2482
{
2483
	struct tcf_dump_args *a = (void *)arg;
2484
	struct net *net = sock_net(a->skb->sk);
L
Linus Torvalds 已提交
2485

2486
	return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
2487
			     n, NETLINK_CB(a->cb->skb).portid,
J
Jamal Hadi Salim 已提交
2488
			     a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
2489
			     RTM_NEWTFILTER, a->terse_dump, true);
L
Linus Torvalds 已提交
2490 2491
}

2492 2493
static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
			   struct sk_buff *skb, struct netlink_callback *cb,
2494
			   long index_start, long *p_index, bool terse)
2495 2496
{
	struct net *net = sock_net(skb->sk);
2497
	struct tcf_block *block = chain->block;
2498
	struct tcmsg *tcm = nlmsg_data(cb->nlh);
2499
	struct tcf_proto *tp, *tp_prev;
2500 2501
	struct tcf_dump_args arg;

2502 2503 2504 2505
	for (tp = __tcf_get_next_proto(chain, NULL);
	     tp;
	     tp_prev = tp,
		     tp = __tcf_get_next_proto(chain, tp),
2506
		     tcf_proto_put(tp_prev, true, NULL),
2507
		     (*p_index)++) {
2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519
		if (*p_index < index_start)
			continue;
		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 (*p_index > index_start)
			memset(&cb->args[1], 0,
			       sizeof(cb->args) - sizeof(cb->args[0]));
		if (cb->args[1] == 0) {
2520
			if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
2521 2522
					  NETLINK_CB(cb->skb).portid,
					  cb->nlh->nlmsg_seq, NLM_F_MULTI,
2523
					  RTM_NEWTFILTER, false, true) <= 0)
2524
				goto errout;
2525 2526 2527 2528 2529 2530 2531
			cb->args[1] = 1;
		}
		if (!tp->ops->walk)
			continue;
		arg.w.fn = tcf_node_dump;
		arg.skb = skb;
		arg.cb = cb;
2532
		arg.block = block;
2533 2534
		arg.q = q;
		arg.parent = parent;
2535 2536 2537
		arg.w.stop = 0;
		arg.w.skip = cb->args[1] - 1;
		arg.w.count = 0;
2538
		arg.w.cookie = cb->args[2];
2539
		arg.terse_dump = terse;
2540
		tp->ops->walk(tp, &arg.w, true);
2541
		cb->args[2] = arg.w.cookie;
2542 2543
		cb->args[1] = arg.w.count + 1;
		if (arg.w.stop)
2544
			goto errout;
2545
	}
2546
	return true;
2547 2548

errout:
2549
	tcf_proto_put(tp, true, NULL);
2550
	return false;
2551 2552
}

2553 2554 2555 2556
static const struct nla_policy tcf_tfilter_dump_policy[TCA_MAX + 1] = {
	[TCA_DUMP_FLAGS] = NLA_POLICY_BITFIELD32(TCA_DUMP_FLAGS_TERSE),
};

E
Eric Dumazet 已提交
2557
/* called with RTNL */
L
Linus Torvalds 已提交
2558 2559
static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
{
2560
	struct tcf_chain *chain, *chain_prev;
2561
	struct net *net = sock_net(skb->sk);
2562
	struct nlattr *tca[TCA_MAX + 1];
2563
	struct Qdisc *q = NULL;
2564
	struct tcf_block *block;
2565
	struct tcmsg *tcm = nlmsg_data(cb->nlh);
2566
	bool terse_dump = false;
2567 2568
	long index_start;
	long index;
2569
	u32 parent;
2570
	int err;
L
Linus Torvalds 已提交
2571

2572
	if (nlmsg_len(cb->nlh) < sizeof(*tcm))
L
Linus Torvalds 已提交
2573
		return skb->len;
2574

2575
	err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
2576
				     tcf_tfilter_dump_policy, cb->extack);
2577 2578 2579
	if (err)
		return err;

2580 2581 2582 2583 2584 2585 2586
	if (tca[TCA_DUMP_FLAGS]) {
		struct nla_bitfield32 flags =
			nla_get_bitfield32(tca[TCA_DUMP_FLAGS]);

		terse_dump = flags.value & TCA_DUMP_FLAGS_TERSE;
	}

2587
	if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2588
		block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2589 2590
		if (!block)
			goto out;
2591 2592 2593 2594 2595 2596 2597
		/* If we work with block index, q is NULL and parent value
		 * will never be used in the following code. The check
		 * in tcf_fill_node prevents it. However, compiler does not
		 * see that far, so set parent to zero to silence the warning
		 * about parent being uninitialized.
		 */
		parent = 0;
2598
	} else {
2599 2600 2601 2602 2603 2604 2605 2606 2607
		const struct Qdisc_class_ops *cops;
		struct net_device *dev;
		unsigned long cl = 0;

		dev = __dev_get_by_index(net, tcm->tcm_ifindex);
		if (!dev)
			return skb->len;

		parent = tcm->tcm_parent;
2608
		if (!parent)
2609
			q = rtnl_dereference(dev->qdisc);
2610
		else
2611 2612 2613 2614 2615
			q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
		if (!q)
			goto out;
		cops = q->ops->cl_ops;
		if (!cops)
2616
			goto out;
2617 2618 2619 2620 2621 2622 2623 2624 2625 2626
		if (!cops->tcf_block)
			goto out;
		if (TC_H_MIN(tcm->tcm_parent)) {
			cl = cops->find(q, tcm->tcm_parent);
			if (cl == 0)
				goto out;
		}
		block = cops->tcf_block(q, cl, NULL);
		if (!block)
			goto out;
2627
		parent = block->classid;
2628 2629
		if (tcf_block_shared(block))
			q = NULL;
L
Linus Torvalds 已提交
2630 2631
	}

2632 2633
	index_start = cb->args[0];
	index = 0;
2634

2635 2636 2637 2638 2639
	for (chain = __tcf_get_next_chain(block, NULL);
	     chain;
	     chain_prev = chain,
		     chain = __tcf_get_next_chain(block, chain),
		     tcf_chain_put(chain_prev)) {
2640 2641 2642
		if (tca[TCA_CHAIN] &&
		    nla_get_u32(tca[TCA_CHAIN]) != chain->index)
			continue;
2643
		if (!tcf_chain_dump(chain, q, parent, skb, cb,
2644
				    index_start, &index, terse_dump)) {
2645
			tcf_chain_put(chain);
2646
			err = -EMSGSIZE;
2647
			break;
2648
		}
2649 2650
	}

2651
	if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
2652
		tcf_block_refcnt_put(block, true);
2653
	cb->args[0] = index;
L
Linus Torvalds 已提交
2654 2655

out:
2656 2657 2658
	/* If we did no progress, the error (EMSGSIZE) is real */
	if (skb->len == 0 && err)
		return err;
L
Linus Torvalds 已提交
2659 2660 2661
	return skb->len;
}

2662 2663 2664 2665
static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops,
			      void *tmplt_priv, u32 chain_index,
			      struct net *net, struct sk_buff *skb,
			      struct tcf_block *block,
2666 2667 2668
			      u32 portid, u32 seq, u16 flags, int event)
{
	unsigned char *b = skb_tail_pointer(skb);
2669
	const struct tcf_proto_ops *ops;
2670 2671
	struct nlmsghdr *nlh;
	struct tcmsg *tcm;
2672 2673
	void *priv;

2674 2675
	ops = tmplt_ops;
	priv = tmplt_priv;
2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692

	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
	if (!nlh)
		goto out_nlmsg_trim;
	tcm = nlmsg_data(nlh);
	tcm->tcm_family = AF_UNSPEC;
	tcm->tcm__pad1 = 0;
	tcm->tcm__pad2 = 0;
	tcm->tcm_handle = 0;
	if (block->q) {
		tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
		tcm->tcm_parent = block->q->handle;
	} else {
		tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
		tcm->tcm_block_index = block->index;
	}

2693
	if (nla_put_u32(skb, TCA_CHAIN, chain_index))
2694 2695
		goto nla_put_failure;

2696 2697 2698 2699 2700 2701 2702
	if (ops) {
		if (nla_put_string(skb, TCA_KIND, ops->kind))
			goto nla_put_failure;
		if (ops->tmplt_dump(skb, net, priv) < 0)
			goto nla_put_failure;
	}

2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718
	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
	return skb->len;

out_nlmsg_trim:
nla_put_failure:
	nlmsg_trim(skb, b);
	return -EMSGSIZE;
}

static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
			   u32 seq, u16 flags, int event, bool unicast)
{
	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
	struct tcf_block *block = chain->block;
	struct net *net = block->net;
	struct sk_buff *skb;
2719
	int err = 0;
2720 2721 2722 2723 2724

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

2725 2726
	if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
			       chain->index, net, skb, block, portid,
2727 2728 2729 2730 2731 2732
			       seq, flags, event) <= 0) {
		kfree_skb(skb);
		return -EINVAL;
	}

	if (unicast)
2733
		err = rtnl_unicast(skb, net, portid);
2734 2735 2736
	else
		err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
				     flags & NLM_F_ECHO);
2737

2738
	return err;
2739 2740
}

2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760
static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
				  void *tmplt_priv, u32 chain_index,
				  struct tcf_block *block, struct sk_buff *oskb,
				  u32 seq, u16 flags, bool unicast)
{
	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
	struct net *net = block->net;
	struct sk_buff *skb;

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

	if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb,
			       block, portid, seq, flags, RTM_DELCHAIN) <= 0) {
		kfree_skb(skb);
		return -EINVAL;
	}

	if (unicast)
2761
		return rtnl_unicast(skb, net, portid);
2762 2763 2764 2765

	return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
}

2766 2767 2768 2769 2770
static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
			      struct nlattr **tca,
			      struct netlink_ext_ack *extack)
{
	const struct tcf_proto_ops *ops;
2771
	char name[IFNAMSIZ];
2772 2773 2774 2775 2776 2777
	void *tmplt_priv;

	/* If kind is not set, user did not specify template. */
	if (!tca[TCA_KIND])
		return 0;

2778 2779 2780 2781 2782 2783
	if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
		NL_SET_ERR_MSG(extack, "Specified TC chain template name too long");
		return -EINVAL;
	}

	ops = tcf_proto_lookup_ops(name, true, extack);
2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800
	if (IS_ERR(ops))
		return PTR_ERR(ops);
	if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
		NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
		return -EOPNOTSUPP;
	}

	tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
	if (IS_ERR(tmplt_priv)) {
		module_put(ops->owner);
		return PTR_ERR(tmplt_priv);
	}
	chain->tmplt_ops = ops;
	chain->tmplt_priv = tmplt_priv;
	return 0;
}

2801 2802
static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
			       void *tmplt_priv)
2803 2804
{
	/* If template ops are set, no work to do for us. */
2805
	if (!tmplt_ops)
2806 2807
		return;

2808 2809
	tmplt_ops->tmplt_destroy(tmplt_priv);
	module_put(tmplt_ops->owner);
2810 2811
}

2812 2813 2814 2815 2816 2817 2818 2819 2820 2821
/* Add/delete/get a chain */

static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
			struct netlink_ext_ack *extack)
{
	struct net *net = sock_net(skb->sk);
	struct nlattr *tca[TCA_MAX + 1];
	struct tcmsg *t;
	u32 parent;
	u32 chain_index;
2822 2823
	struct Qdisc *q;
	struct tcf_chain *chain;
2824 2825 2826 2827 2828 2829 2830 2831 2832
	struct tcf_block *block;
	unsigned long cl;
	int err;

	if (n->nlmsg_type != RTM_GETCHAIN &&
	    !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
		return -EPERM;

replay:
2833
	q = NULL;
2834 2835
	err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
				     rtm_tca_policy, extack);
2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850
	if (err < 0)
		return err;

	t = nlmsg_data(n);
	parent = t->tcm_parent;
	cl = 0;

	block = tcf_block_find(net, &q, &parent, &cl,
			       t->tcm_ifindex, t->tcm_block_index, extack);
	if (IS_ERR(block))
		return PTR_ERR(block);

	chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
	if (chain_index > TC_ACT_EXT_VAL_MASK) {
		NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2851 2852
		err = -EINVAL;
		goto errout_block;
2853
	}
2854 2855

	mutex_lock(&block->lock);
2856 2857 2858
	chain = tcf_chain_lookup(block, chain_index);
	if (n->nlmsg_type == RTM_NEWCHAIN) {
		if (chain) {
2859
			if (tcf_chain_held_by_acts_only(chain)) {
2860
				/* The chain exists only because there is
2861
				 * some action referencing it.
2862 2863 2864 2865
				 */
				tcf_chain_hold(chain);
			} else {
				NL_SET_ERR_MSG(extack, "Filter chain already exists");
2866
				err = -EEXIST;
2867
				goto errout_block_locked;
2868 2869 2870 2871
			}
		} else {
			if (!(n->nlmsg_flags & NLM_F_CREATE)) {
				NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
2872
				err = -ENOENT;
2873
				goto errout_block_locked;
2874 2875 2876 2877
			}
			chain = tcf_chain_create(block, chain_index);
			if (!chain) {
				NL_SET_ERR_MSG(extack, "Failed to create filter chain");
2878
				err = -ENOMEM;
2879
				goto errout_block_locked;
2880
			}
2881 2882
		}
	} else {
2883
		if (!chain || tcf_chain_held_by_acts_only(chain)) {
2884
			NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2885
			err = -EINVAL;
2886
			goto errout_block_locked;
2887 2888 2889 2890
		}
		tcf_chain_hold(chain);
	}

2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901
	if (n->nlmsg_type == RTM_NEWCHAIN) {
		/* Modifying chain requires holding parent block lock. In case
		 * the chain was successfully added, take a reference to the
		 * chain. This ensures that an empty chain does not disappear at
		 * the end of this function.
		 */
		tcf_chain_hold(chain);
		chain->explicitly_created = true;
	}
	mutex_unlock(&block->lock);

2902 2903
	switch (n->nlmsg_type) {
	case RTM_NEWCHAIN:
2904
		err = tc_chain_tmplt_add(chain, net, tca, extack);
2905 2906
		if (err) {
			tcf_chain_put_explicitly_created(chain);
2907
			goto errout;
2908 2909
		}

2910 2911 2912 2913
		tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
				RTM_NEWCHAIN, false);
		break;
	case RTM_DELCHAIN:
2914
		tfilter_notify_chain(net, skb, block, q, parent, n,
2915
				     chain, RTM_DELTFILTER);
2916
		/* Flush the chain first as the user requested chain removal. */
2917
		tcf_chain_flush(chain, true);
2918 2919 2920 2921 2922 2923 2924
		/* In case the chain was successfully deleted, put a reference
		 * to the chain previously taken during addition.
		 */
		tcf_chain_put_explicitly_created(chain);
		break;
	case RTM_GETCHAIN:
		err = tc_chain_notify(chain, skb, n->nlmsg_seq,
2925
				      n->nlmsg_flags, n->nlmsg_type, true);
2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936
		if (err < 0)
			NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
		break;
	default:
		err = -EOPNOTSUPP;
		NL_SET_ERR_MSG(extack, "Unsupported message type");
		goto errout;
	}

errout:
	tcf_chain_put(chain);
2937
errout_block:
2938
	tcf_block_release(q, block, true);
2939 2940 2941 2942
	if (err == -EAGAIN)
		/* Replay the request. */
		goto replay;
	return err;
2943 2944 2945 2946

errout_block_locked:
	mutex_unlock(&block->lock);
	goto errout_block;
2947 2948 2949 2950 2951 2952 2953 2954 2955 2956
}

/* called with RTNL */
static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
{
	struct net *net = sock_net(skb->sk);
	struct nlattr *tca[TCA_MAX + 1];
	struct Qdisc *q = NULL;
	struct tcf_block *block;
	struct tcmsg *tcm = nlmsg_data(cb->nlh);
2957
	struct tcf_chain *chain;
2958 2959 2960 2961 2962 2963 2964
	long index_start;
	long index;
	int err;

	if (nlmsg_len(cb->nlh) < sizeof(*tcm))
		return skb->len;

2965 2966
	err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
				     rtm_tca_policy, cb->extack);
2967 2968 2969 2970
	if (err)
		return err;

	if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2971
		block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982
		if (!block)
			goto out;
	} else {
		const struct Qdisc_class_ops *cops;
		struct net_device *dev;
		unsigned long cl = 0;

		dev = __dev_get_by_index(net, tcm->tcm_ifindex);
		if (!dev)
			return skb->len;

2983
		if (!tcm->tcm_parent)
2984
			q = rtnl_dereference(dev->qdisc);
2985
		else
2986
			q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2987

2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009
		if (!q)
			goto out;
		cops = q->ops->cl_ops;
		if (!cops)
			goto out;
		if (!cops->tcf_block)
			goto out;
		if (TC_H_MIN(tcm->tcm_parent)) {
			cl = cops->find(q, tcm->tcm_parent);
			if (cl == 0)
				goto out;
		}
		block = cops->tcf_block(q, cl, NULL);
		if (!block)
			goto out;
		if (tcf_block_shared(block))
			q = NULL;
	}

	index_start = cb->args[0];
	index = 0;

3010 3011
	mutex_lock(&block->lock);
	list_for_each_entry(chain, &block->chain_list, list) {
3012 3013 3014 3015 3016 3017 3018
		if ((tca[TCA_CHAIN] &&
		     nla_get_u32(tca[TCA_CHAIN]) != chain->index))
			continue;
		if (index < index_start) {
			index++;
			continue;
		}
3019 3020
		if (tcf_chain_held_by_acts_only(chain))
			continue;
3021 3022
		err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
					 chain->index, net, skb, block,
3023 3024 3025
					 NETLINK_CB(cb->skb).portid,
					 cb->nlh->nlmsg_seq, NLM_F_MULTI,
					 RTM_NEWCHAIN);
3026
		if (err <= 0)
3027 3028 3029
			break;
		index++;
	}
3030
	mutex_unlock(&block->lock);
3031

3032
	if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
3033
		tcf_block_refcnt_put(block, true);
3034 3035 3036 3037 3038 3039 3040 3041 3042
	cb->args[0] = index;

out:
	/* If we did no progress, the error (EMSGSIZE) is real */
	if (skb->len == 0 && err)
		return err;
	return skb->len;
}

3043
void tcf_exts_destroy(struct tcf_exts *exts)
L
Linus Torvalds 已提交
3044 3045
{
#ifdef CONFIG_NET_CLS_ACT
3046 3047 3048 3049
	if (exts->actions) {
		tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
		kfree(exts->actions);
	}
3050
	exts->nr_actions = 0;
L
Linus Torvalds 已提交
3051 3052
#endif
}
3053
EXPORT_SYMBOL(tcf_exts_destroy);
L
Linus Torvalds 已提交
3054

3055 3056 3057
int tcf_exts_validate_ex(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
			 struct nlattr *rate_tlv, struct tcf_exts *exts,
			 u32 flags, u32 fl_flags, struct netlink_ext_ack *extack)
L
Linus Torvalds 已提交
3058 3059 3060
{
#ifdef CONFIG_NET_CLS_ACT
	{
3061
		int init_res[TCA_ACT_MAX_PRIO] = {};
L
Linus Torvalds 已提交
3062
		struct tc_action *act;
3063
		size_t attr_size = 0;
L
Linus Torvalds 已提交
3064

3065
		if (exts->police && tb[exts->police]) {
3066 3067
			struct tc_action_ops *a_o;

3068 3069 3070
			a_o = tc_action_load_ops(tb[exts->police], true,
						 !(flags & TCA_ACT_FLAGS_NO_RTNL),
						 extack);
3071 3072
			if (IS_ERR(a_o))
				return PTR_ERR(a_o);
3073
			flags |= TCA_ACT_FLAGS_POLICE | TCA_ACT_FLAGS_BIND;
3074
			act = tcf_action_init_1(net, tp, tb[exts->police],
3075 3076
						rate_tlv, a_o, init_res, flags,
						extack);
3077 3078
			module_put(a_o->owner);
			if (IS_ERR(act))
3079
				return PTR_ERR(act);
L
Linus Torvalds 已提交
3080

3081
			act->type = exts->type = TCA_OLD_COMPAT;
3082 3083
			exts->actions[0] = act;
			exts->nr_actions = 1;
3084
			tcf_idr_insert_many(exts->actions);
3085
		} else if (exts->action && tb[exts->action]) {
3086
			int err;
3087

3088
			flags |= TCA_ACT_FLAGS_BIND;
3089
			err = tcf_action_init(net, tp, tb[exts->action],
3090
					      rate_tlv, exts->actions, init_res,
3091 3092
					      &attr_size, flags, fl_flags,
					      extack);
3093
			if (err < 0)
3094
				return err;
3095
			exts->nr_actions = err;
L
Linus Torvalds 已提交
3096 3097 3098
		}
	}
#else
3099
	if ((exts->action && tb[exts->action]) ||
3100 3101
	    (exts->police && tb[exts->police])) {
		NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
L
Linus Torvalds 已提交
3102
		return -EOPNOTSUPP;
3103
	}
L
Linus Torvalds 已提交
3104 3105 3106 3107
#endif

	return 0;
}
3108 3109 3110 3111 3112 3113 3114 3115 3116
EXPORT_SYMBOL(tcf_exts_validate_ex);

int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
		      struct nlattr *rate_tlv, struct tcf_exts *exts,
		      u32 flags, struct netlink_ext_ack *extack)
{
	return tcf_exts_validate_ex(net, tp, tb, rate_tlv, exts,
				    flags, 0, extack);
}
3117
EXPORT_SYMBOL(tcf_exts_validate);
L
Linus Torvalds 已提交
3118

3119
void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
L
Linus Torvalds 已提交
3120 3121
{
#ifdef CONFIG_NET_CLS_ACT
3122 3123
	struct tcf_exts old = *dst;

3124
	*dst = *src;
3125
	tcf_exts_destroy(&old);
L
Linus Torvalds 已提交
3126 3127
#endif
}
3128
EXPORT_SYMBOL(tcf_exts_change);
L
Linus Torvalds 已提交
3129

3130 3131 3132 3133 3134 3135 3136 3137 3138
#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
3139

3140
int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
L
Linus Torvalds 已提交
3141 3142
{
#ifdef CONFIG_NET_CLS_ACT
3143 3144
	struct nlattr *nest;

3145
	if (exts->action && tcf_exts_has_actions(exts)) {
L
Linus Torvalds 已提交
3146 3147 3148 3149 3150
		/*
		 * 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
		 */
3151
		if (exts->type != TCA_OLD_COMPAT) {
3152
			nest = nla_nest_start_noflag(skb, exts->action);
3153 3154
			if (nest == NULL)
				goto nla_put_failure;
3155

3156 3157
			if (tcf_action_dump(skb, exts->actions, 0, 0, false)
			    < 0)
3158
				goto nla_put_failure;
3159
			nla_nest_end(skb, nest);
3160
		} else if (exts->police) {
3161
			struct tc_action *act = tcf_exts_first_act(exts);
3162
			nest = nla_nest_start_noflag(skb, exts->police);
3163
			if (nest == NULL || !act)
3164
				goto nla_put_failure;
3165
			if (tcf_action_dump_old(skb, act, 0, 0) < 0)
3166
				goto nla_put_failure;
3167
			nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
3168 3169 3170
		}
	}
	return 0;
3171 3172 3173

nla_put_failure:
	nla_nest_cancel(skb, nest);
L
Linus Torvalds 已提交
3174
	return -1;
3175 3176 3177
#else
	return 0;
#endif
L
Linus Torvalds 已提交
3178
}
3179
EXPORT_SYMBOL(tcf_exts_dump);
L
Linus Torvalds 已提交
3180

3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205
int tcf_exts_terse_dump(struct sk_buff *skb, struct tcf_exts *exts)
{
#ifdef CONFIG_NET_CLS_ACT
	struct nlattr *nest;

	if (!exts->action || !tcf_exts_has_actions(exts))
		return 0;

	nest = nla_nest_start_noflag(skb, exts->action);
	if (!nest)
		goto nla_put_failure;

	if (tcf_action_dump(skb, exts->actions, 0, 0, true) < 0)
		goto nla_put_failure;
	nla_nest_end(skb, nest);
	return 0;

nla_put_failure:
	nla_nest_cancel(skb, nest);
	return -1;
#else
	return 0;
#endif
}
EXPORT_SYMBOL(tcf_exts_terse_dump);
3206

3207
int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
L
Linus Torvalds 已提交
3208 3209
{
#ifdef CONFIG_NET_CLS_ACT
3210
	struct tc_action *a = tcf_exts_first_act(exts);
3211
	if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
3212
		return -1;
L
Linus Torvalds 已提交
3213 3214 3215
#endif
	return 0;
}
3216
EXPORT_SYMBOL(tcf_exts_dump_stats);
L
Linus Torvalds 已提交
3217

3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267
static void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
{
	if (*flags & TCA_CLS_FLAGS_IN_HW)
		return;
	*flags |= TCA_CLS_FLAGS_IN_HW;
	atomic_inc(&block->offloadcnt);
}

static void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
{
	if (!(*flags & TCA_CLS_FLAGS_IN_HW))
		return;
	*flags &= ~TCA_CLS_FLAGS_IN_HW;
	atomic_dec(&block->offloadcnt);
}

static void tc_cls_offload_cnt_update(struct tcf_block *block,
				      struct tcf_proto *tp, u32 *cnt,
				      u32 *flags, u32 diff, bool add)
{
	lockdep_assert_held(&block->cb_lock);

	spin_lock(&tp->lock);
	if (add) {
		if (!*cnt)
			tcf_block_offload_inc(block, flags);
		*cnt += diff;
	} else {
		*cnt -= diff;
		if (!*cnt)
			tcf_block_offload_dec(block, flags);
	}
	spin_unlock(&tp->lock);
}

static void
tc_cls_offload_cnt_reset(struct tcf_block *block, struct tcf_proto *tp,
			 u32 *cnt, u32 *flags)
{
	lockdep_assert_held(&block->cb_lock);

	spin_lock(&tp->lock);
	tcf_block_offload_dec(block, flags);
	*cnt = 0;
	spin_unlock(&tp->lock);
}

static int
__tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
		   void *type_data, bool err_stop)
3268
{
3269
	struct flow_block_cb *block_cb;
3270 3271 3272
	int ok_count = 0;
	int err;

3273
	list_for_each_entry(block_cb, &block->flow_block.cb_list, list) {
3274 3275
		err = block_cb->cb(type, type_data, block_cb->cb_priv);
		if (err) {
3276 3277
			if (err_stop)
				return err;
3278 3279 3280 3281
		} else {
			ok_count++;
		}
	}
3282 3283 3284 3285 3286 3287
	return ok_count;
}

int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
		     void *type_data, bool err_stop, bool rtnl_held)
{
3288
	bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3289 3290
	int ok_count;

3291 3292 3293
retry:
	if (take_rtnl)
		rtnl_lock();
3294
	down_read(&block->cb_lock);
3295 3296 3297 3298 3299 3300 3301 3302 3303 3304
	/* Need to obtain rtnl lock if block is bound to devs that require it.
	 * In block bind code cb_lock is obtained while holding rtnl, so we must
	 * obtain the locks in same order here.
	 */
	if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
		up_read(&block->cb_lock);
		take_rtnl = true;
		goto retry;
	}

3305
	ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3306

3307
	up_read(&block->cb_lock);
3308 3309
	if (take_rtnl)
		rtnl_unlock();
3310
	return ok_count;
3311 3312
}
EXPORT_SYMBOL(tc_setup_cb_call);
3313

3314 3315 3316 3317 3318 3319 3320 3321 3322 3323
/* Non-destructive filter add. If filter that wasn't already in hardware is
 * successfully offloaded, increment block offloads counter. On failure,
 * previously offloaded filter is considered to be intact and offloads counter
 * is not decremented.
 */

int tc_setup_cb_add(struct tcf_block *block, struct tcf_proto *tp,
		    enum tc_setup_type type, void *type_data, bool err_stop,
		    u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
{
3324
	bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3325 3326
	int ok_count;

3327 3328 3329
retry:
	if (take_rtnl)
		rtnl_lock();
3330
	down_read(&block->cb_lock);
3331 3332 3333 3334 3335 3336 3337 3338 3339 3340
	/* Need to obtain rtnl lock if block is bound to devs that require it.
	 * In block bind code cb_lock is obtained while holding rtnl, so we must
	 * obtain the locks in same order here.
	 */
	if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
		up_read(&block->cb_lock);
		take_rtnl = true;
		goto retry;
	}

3341 3342 3343 3344 3345 3346 3347
	/* Make sure all netdevs sharing this block are offload-capable. */
	if (block->nooffloaddevcnt && err_stop) {
		ok_count = -EOPNOTSUPP;
		goto err_unlock;
	}

	ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3348 3349 3350 3351 3352
	if (ok_count < 0)
		goto err_unlock;

	if (tp->ops->hw_add)
		tp->ops->hw_add(tp, type_data);
3353 3354 3355 3356 3357
	if (ok_count > 0)
		tc_cls_offload_cnt_update(block, tp, in_hw_count, flags,
					  ok_count, true);
err_unlock:
	up_read(&block->cb_lock);
3358 3359
	if (take_rtnl)
		rtnl_unlock();
3360
	return min(ok_count, 0);
3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375
}
EXPORT_SYMBOL(tc_setup_cb_add);

/* Destructive filter replace. If filter that wasn't already in hardware is
 * successfully offloaded, increment block offload counter. On failure,
 * previously offloaded filter is considered to be destroyed and offload counter
 * is decremented.
 */

int tc_setup_cb_replace(struct tcf_block *block, struct tcf_proto *tp,
			enum tc_setup_type type, void *type_data, bool err_stop,
			u32 *old_flags, unsigned int *old_in_hw_count,
			u32 *new_flags, unsigned int *new_in_hw_count,
			bool rtnl_held)
{
3376
	bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3377 3378
	int ok_count;

3379 3380 3381
retry:
	if (take_rtnl)
		rtnl_lock();
3382
	down_read(&block->cb_lock);
3383 3384 3385 3386 3387 3388 3389 3390 3391 3392
	/* Need to obtain rtnl lock if block is bound to devs that require it.
	 * In block bind code cb_lock is obtained while holding rtnl, so we must
	 * obtain the locks in same order here.
	 */
	if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
		up_read(&block->cb_lock);
		take_rtnl = true;
		goto retry;
	}

3393 3394 3395 3396 3397 3398 3399
	/* Make sure all netdevs sharing this block are offload-capable. */
	if (block->nooffloaddevcnt && err_stop) {
		ok_count = -EOPNOTSUPP;
		goto err_unlock;
	}

	tc_cls_offload_cnt_reset(block, tp, old_in_hw_count, old_flags);
3400 3401
	if (tp->ops->hw_del)
		tp->ops->hw_del(tp, type_data);
3402 3403

	ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3404 3405 3406 3407 3408
	if (ok_count < 0)
		goto err_unlock;

	if (tp->ops->hw_add)
		tp->ops->hw_add(tp, type_data);
3409
	if (ok_count > 0)
3410 3411
		tc_cls_offload_cnt_update(block, tp, new_in_hw_count,
					  new_flags, ok_count, true);
3412 3413
err_unlock:
	up_read(&block->cb_lock);
3414 3415
	if (take_rtnl)
		rtnl_unlock();
3416
	return min(ok_count, 0);
3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427
}
EXPORT_SYMBOL(tc_setup_cb_replace);

/* Destroy filter and decrement block offload counter, if filter was previously
 * offloaded.
 */

int tc_setup_cb_destroy(struct tcf_block *block, struct tcf_proto *tp,
			enum tc_setup_type type, void *type_data, bool err_stop,
			u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
{
3428
	bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3429 3430
	int ok_count;

3431 3432 3433
retry:
	if (take_rtnl)
		rtnl_lock();
3434
	down_read(&block->cb_lock);
3435 3436 3437 3438 3439 3440 3441 3442 3443 3444
	/* Need to obtain rtnl lock if block is bound to devs that require it.
	 * In block bind code cb_lock is obtained while holding rtnl, so we must
	 * obtain the locks in same order here.
	 */
	if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
		up_read(&block->cb_lock);
		take_rtnl = true;
		goto retry;
	}

3445 3446 3447
	ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);

	tc_cls_offload_cnt_reset(block, tp, in_hw_count, flags);
3448 3449 3450
	if (tp->ops->hw_del)
		tp->ops->hw_del(tp, type_data);

3451
	up_read(&block->cb_lock);
3452 3453
	if (take_rtnl)
		rtnl_unlock();
3454
	return min(ok_count, 0);
3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476
}
EXPORT_SYMBOL(tc_setup_cb_destroy);

int tc_setup_cb_reoffload(struct tcf_block *block, struct tcf_proto *tp,
			  bool add, flow_setup_cb_t *cb,
			  enum tc_setup_type type, void *type_data,
			  void *cb_priv, u32 *flags, unsigned int *in_hw_count)
{
	int err = cb(type, type_data, cb_priv);

	if (err) {
		if (add && tc_skip_sw(*flags))
			return err;
	} else {
		tc_cls_offload_cnt_update(block, tp, in_hw_count, flags, 1,
					  add);
	}

	return 0;
}
EXPORT_SYMBOL(tc_setup_cb_reoffload);

3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500
static int tcf_act_get_cookie(struct flow_action_entry *entry,
			      const struct tc_action *act)
{
	struct tc_cookie *cookie;
	int err = 0;

	rcu_read_lock();
	cookie = rcu_dereference(act->act_cookie);
	if (cookie) {
		entry->cookie = flow_action_cookie_create(cookie->data,
							  cookie->len,
							  GFP_ATOMIC);
		if (!entry->cookie)
			err = -ENOMEM;
	}
	rcu_read_unlock();
	return err;
}

static void tcf_act_put_cookie(struct flow_action_entry *entry)
{
	flow_action_cookie_destroy(entry->cookie);
}

3501
void tc_cleanup_offload_action(struct flow_action *flow_action)
3502 3503 3504 3505
{
	struct flow_action_entry *entry;
	int i;

3506 3507
	flow_action_for_each(i, entry, flow_action) {
		tcf_act_put_cookie(entry);
3508 3509
		if (entry->destructor)
			entry->destructor(entry->destructor_priv);
3510
	}
3511
}
3512
EXPORT_SYMBOL(tc_cleanup_offload_action);
3513

3514 3515
static int tc_setup_offload_act(struct tc_action *act,
				struct flow_action_entry *entry,
3516 3517
				u32 *index_inc,
				struct netlink_ext_ack *extack)
3518
{
3519
#ifdef CONFIG_NET_CLS_ACT
3520
	if (act->ops->offload_act_setup) {
3521 3522
		return act->ops->offload_act_setup(act, entry, index_inc, true,
						   extack);
3523 3524
	} else {
		NL_SET_ERR_MSG(extack, "Action does not support offload");
3525
		return -EOPNOTSUPP;
3526
	}
3527
#else
3528
	return 0;
3529 3530 3531
#endif
}

3532
int tc_setup_action(struct flow_action *flow_action,
3533 3534
		    struct tc_action *actions[],
		    struct netlink_ext_ack *extack)
3535
{
3536
	int i, j, k, index, err = 0;
3537
	struct tc_action *act;
3538

3539 3540 3541
	BUILD_BUG_ON(TCA_ACT_HW_STATS_ANY != FLOW_ACTION_HW_STATS_ANY);
	BUILD_BUG_ON(TCA_ACT_HW_STATS_IMMEDIATE != FLOW_ACTION_HW_STATS_IMMEDIATE);
	BUILD_BUG_ON(TCA_ACT_HW_STATS_DELAYED != FLOW_ACTION_HW_STATS_DELAYED);
3542

3543
	if (!actions)
3544 3545 3546
		return 0;

	j = 0;
3547
	tcf_act_for_each_action(i, act, actions) {
3548 3549 3550
		struct flow_action_entry *entry;

		entry = &flow_action->entries[j];
3551
		spin_lock_bh(&act->tcfa_lock);
3552 3553 3554
		err = tcf_act_get_cookie(entry, act);
		if (err)
			goto err_out_locked;
3555

3556
		index = 0;
3557
		err = tc_setup_offload_act(act, entry, &index, extack);
3558
		if (err)
3559
			goto err_out_locked;
3560 3561 3562 3563 3564 3565 3566 3567

		for (k = 0; k < index ; k++) {
			entry[k].hw_stats = tc_act_hw_stats(act->hw_stats);
			entry[k].hw_index = act->tcfa_index;
		}

		j += index;

3568
		spin_unlock_bh(&act->tcfa_lock);
3569
	}
3570

3571
err_out:
3572
	if (err)
3573
		tc_cleanup_offload_action(flow_action);
3574

3575
	return err;
3576 3577 3578
err_out_locked:
	spin_unlock_bh(&act->tcfa_lock);
	goto err_out;
3579
}
3580 3581

int tc_setup_offload_action(struct flow_action *flow_action,
3582 3583
			    const struct tcf_exts *exts,
			    struct netlink_ext_ack *extack)
3584 3585 3586 3587 3588
{
#ifdef CONFIG_NET_CLS_ACT
	if (!exts)
		return 0;

3589
	return tc_setup_action(flow_action, exts->actions, extack);
3590 3591 3592 3593
#else
	return 0;
#endif
}
3594
EXPORT_SYMBOL(tc_setup_offload_action);
3595

3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611
unsigned int tcf_exts_num_actions(struct tcf_exts *exts)
{
	unsigned int num_acts = 0;
	struct tc_action *act;
	int i;

	tcf_exts_for_each_action(i, act, exts) {
		if (is_tcf_pedit(act))
			num_acts += tcf_pedit_nkeys(act);
		else
			num_acts++;
	}
	return num_acts;
}
EXPORT_SYMBOL(tcf_exts_num_actions);

3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683
#ifdef CONFIG_NET_CLS_ACT
static int tcf_qevent_parse_block_index(struct nlattr *block_index_attr,
					u32 *p_block_index,
					struct netlink_ext_ack *extack)
{
	*p_block_index = nla_get_u32(block_index_attr);
	if (!*p_block_index) {
		NL_SET_ERR_MSG(extack, "Block number may not be zero");
		return -EINVAL;
	}

	return 0;
}

int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
		    enum flow_block_binder_type binder_type,
		    struct nlattr *block_index_attr,
		    struct netlink_ext_ack *extack)
{
	u32 block_index;
	int err;

	if (!block_index_attr)
		return 0;

	err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
	if (err)
		return err;

	if (!block_index)
		return 0;

	qe->info.binder_type = binder_type;
	qe->info.chain_head_change = tcf_chain_head_change_dflt;
	qe->info.chain_head_change_priv = &qe->filter_chain;
	qe->info.block_index = block_index;

	return tcf_block_get_ext(&qe->block, sch, &qe->info, extack);
}
EXPORT_SYMBOL(tcf_qevent_init);

void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch)
{
	if (qe->info.block_index)
		tcf_block_put_ext(qe->block, sch, &qe->info);
}
EXPORT_SYMBOL(tcf_qevent_destroy);

int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
			       struct netlink_ext_ack *extack)
{
	u32 block_index;
	int err;

	if (!block_index_attr)
		return 0;

	err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
	if (err)
		return err;

	/* Bounce newly-configured block or change in block. */
	if (block_index != qe->info.block_index) {
		NL_SET_ERR_MSG(extack, "Change of blocks is not supported");
		return -EINVAL;
	}

	return 0;
}
EXPORT_SYMBOL(tcf_qevent_validate_change);

struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
3684
				  struct sk_buff **to_free, int *ret)
3685 3686 3687 3688 3689 3690 3691 3692 3693
{
	struct tcf_result cl_res;
	struct tcf_proto *fl;

	if (!qe->info.block_index)
		return skb;

	fl = rcu_dereference_bh(qe->filter_chain);

3694
	switch (tcf_classify(skb, NULL, fl, &cl_res, false)) {
3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724
	case TC_ACT_SHOT:
		qdisc_qstats_drop(sch);
		__qdisc_drop(skb, to_free);
		*ret = __NET_XMIT_BYPASS;
		return NULL;
	case TC_ACT_STOLEN:
	case TC_ACT_QUEUED:
	case TC_ACT_TRAP:
		__qdisc_drop(skb, to_free);
		*ret = __NET_XMIT_STOLEN;
		return NULL;
	case TC_ACT_REDIRECT:
		skb_do_redirect(skb);
		*ret = __NET_XMIT_STOLEN;
		return NULL;
	}

	return skb;
}
EXPORT_SYMBOL(tcf_qevent_handle);

int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe)
{
	if (!qe->info.block_index)
		return 0;
	return nla_put_u32(skb, attr_name, qe->info.block_index);
}
EXPORT_SYMBOL(tcf_qevent_dump);
#endif

3725 3726 3727 3728
static __net_init int tcf_net_init(struct net *net)
{
	struct tcf_net *tn = net_generic(net, tcf_net_id);

3729
	spin_lock_init(&tn->idr_lock);
3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747
	idr_init(&tn->idr);
	return 0;
}

static void __net_exit tcf_net_exit(struct net *net)
{
	struct tcf_net *tn = net_generic(net, tcf_net_id);

	idr_destroy(&tn->idr);
}

static struct pernet_operations tcf_net_ops = {
	.init = tcf_net_init,
	.exit = tcf_net_exit,
	.id   = &tcf_net_id,
	.size = sizeof(struct tcf_net),
};

L
Linus Torvalds 已提交
3748 3749
static int __init tc_filter_init(void)
{
3750 3751
	int err;

3752 3753 3754 3755
	tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
	if (!tc_filter_wq)
		return -ENOMEM;

3756 3757 3758 3759
	err = register_pernet_subsys(&tcf_net_ops);
	if (err)
		goto err_register_pernet_subsys;

3760 3761 3762 3763
	rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL,
		      RTNL_FLAG_DOIT_UNLOCKED);
	rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL,
		      RTNL_FLAG_DOIT_UNLOCKED);
3764
	rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
3765
		      tc_dump_tfilter, RTNL_FLAG_DOIT_UNLOCKED);
3766 3767 3768 3769
	rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
	rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
	rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
		      tc_dump_chain, 0);
L
Linus Torvalds 已提交
3770 3771

	return 0;
3772 3773 3774 3775

err_register_pernet_subsys:
	destroy_workqueue(tc_filter_wq);
	return err;
L
Linus Torvalds 已提交
3776 3777 3778
}

subsys_initcall(tc_filter_init);