xref: /openbmc/linux/net/sched/cls_api.c (revision cc966c92)
1 /*
2  * net/sched/cls_api.c	Packet classifier API.
3  *
4  *		This program is free software; you can redistribute it and/or
5  *		modify it under the terms of the GNU General Public License
6  *		as published by the Free Software Foundation; either version
7  *		2 of the License, or (at your option) any later version.
8  *
9  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  *
11  * Changes:
12  *
13  * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
14  *
15  */
16 
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/err.h>
23 #include <linux/skbuff.h>
24 #include <linux/init.h>
25 #include <linux/kmod.h>
26 #include <linux/slab.h>
27 #include <linux/idr.h>
28 #include <linux/rhashtable.h>
29 #include <net/net_namespace.h>
30 #include <net/sock.h>
31 #include <net/netlink.h>
32 #include <net/pkt_sched.h>
33 #include <net/pkt_cls.h>
34 #include <net/tc_act/tc_pedit.h>
35 #include <net/tc_act/tc_mirred.h>
36 #include <net/tc_act/tc_vlan.h>
37 #include <net/tc_act/tc_tunnel_key.h>
38 #include <net/tc_act/tc_csum.h>
39 #include <net/tc_act/tc_gact.h>
40 #include <net/tc_act/tc_police.h>
41 #include <net/tc_act/tc_sample.h>
42 #include <net/tc_act/tc_skbedit.h>
43 
44 extern const struct nla_policy rtm_tca_policy[TCA_MAX + 1];
45 
46 /* The list of all installed classifier types */
47 static LIST_HEAD(tcf_proto_base);
48 
49 /* Protects list of registered TC modules. It is pure SMP lock. */
50 static DEFINE_RWLOCK(cls_mod_lock);
51 
52 /* Find classifier type by string name */
53 
54 static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind)
55 {
56 	const struct tcf_proto_ops *t, *res = NULL;
57 
58 	if (kind) {
59 		read_lock(&cls_mod_lock);
60 		list_for_each_entry(t, &tcf_proto_base, head) {
61 			if (strcmp(kind, t->kind) == 0) {
62 				if (try_module_get(t->owner))
63 					res = t;
64 				break;
65 			}
66 		}
67 		read_unlock(&cls_mod_lock);
68 	}
69 	return res;
70 }
71 
72 static const struct tcf_proto_ops *
73 tcf_proto_lookup_ops(const char *kind, bool rtnl_held,
74 		     struct netlink_ext_ack *extack)
75 {
76 	const struct tcf_proto_ops *ops;
77 
78 	ops = __tcf_proto_lookup_ops(kind);
79 	if (ops)
80 		return ops;
81 #ifdef CONFIG_MODULES
82 	if (rtnl_held)
83 		rtnl_unlock();
84 	request_module("cls_%s", kind);
85 	if (rtnl_held)
86 		rtnl_lock();
87 	ops = __tcf_proto_lookup_ops(kind);
88 	/* We dropped the RTNL semaphore in order to perform
89 	 * the module load. So, even if we succeeded in loading
90 	 * the module we have to replay the request. We indicate
91 	 * this using -EAGAIN.
92 	 */
93 	if (ops) {
94 		module_put(ops->owner);
95 		return ERR_PTR(-EAGAIN);
96 	}
97 #endif
98 	NL_SET_ERR_MSG(extack, "TC classifier not found");
99 	return ERR_PTR(-ENOENT);
100 }
101 
102 /* Register(unregister) new classifier type */
103 
104 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
105 {
106 	struct tcf_proto_ops *t;
107 	int rc = -EEXIST;
108 
109 	write_lock(&cls_mod_lock);
110 	list_for_each_entry(t, &tcf_proto_base, head)
111 		if (!strcmp(ops->kind, t->kind))
112 			goto out;
113 
114 	list_add_tail(&ops->head, &tcf_proto_base);
115 	rc = 0;
116 out:
117 	write_unlock(&cls_mod_lock);
118 	return rc;
119 }
120 EXPORT_SYMBOL(register_tcf_proto_ops);
121 
122 static struct workqueue_struct *tc_filter_wq;
123 
124 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
125 {
126 	struct tcf_proto_ops *t;
127 	int rc = -ENOENT;
128 
129 	/* Wait for outstanding call_rcu()s, if any, from a
130 	 * tcf_proto_ops's destroy() handler.
131 	 */
132 	rcu_barrier();
133 	flush_workqueue(tc_filter_wq);
134 
135 	write_lock(&cls_mod_lock);
136 	list_for_each_entry(t, &tcf_proto_base, head) {
137 		if (t == ops) {
138 			list_del(&t->head);
139 			rc = 0;
140 			break;
141 		}
142 	}
143 	write_unlock(&cls_mod_lock);
144 	return rc;
145 }
146 EXPORT_SYMBOL(unregister_tcf_proto_ops);
147 
148 bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
149 {
150 	INIT_RCU_WORK(rwork, func);
151 	return queue_rcu_work(tc_filter_wq, rwork);
152 }
153 EXPORT_SYMBOL(tcf_queue_work);
154 
155 /* Select new prio value from the range, managed by kernel. */
156 
157 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
158 {
159 	u32 first = TC_H_MAKE(0xC0000000U, 0U);
160 
161 	if (tp)
162 		first = tp->prio - 1;
163 
164 	return TC_H_MAJ(first);
165 }
166 
167 static bool tcf_proto_is_unlocked(const char *kind)
168 {
169 	const struct tcf_proto_ops *ops;
170 	bool ret;
171 
172 	ops = tcf_proto_lookup_ops(kind, false, NULL);
173 	/* On error return false to take rtnl lock. Proto lookup/create
174 	 * functions will perform lookup again and properly handle errors.
175 	 */
176 	if (IS_ERR(ops))
177 		return false;
178 
179 	ret = !!(ops->flags & TCF_PROTO_OPS_DOIT_UNLOCKED);
180 	module_put(ops->owner);
181 	return ret;
182 }
183 
184 static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
185 					  u32 prio, struct tcf_chain *chain,
186 					  bool rtnl_held,
187 					  struct netlink_ext_ack *extack)
188 {
189 	struct tcf_proto *tp;
190 	int err;
191 
192 	tp = kzalloc(sizeof(*tp), GFP_KERNEL);
193 	if (!tp)
194 		return ERR_PTR(-ENOBUFS);
195 
196 	tp->ops = tcf_proto_lookup_ops(kind, rtnl_held, extack);
197 	if (IS_ERR(tp->ops)) {
198 		err = PTR_ERR(tp->ops);
199 		goto errout;
200 	}
201 	tp->classify = tp->ops->classify;
202 	tp->protocol = protocol;
203 	tp->prio = prio;
204 	tp->chain = chain;
205 	spin_lock_init(&tp->lock);
206 	refcount_set(&tp->refcnt, 1);
207 
208 	err = tp->ops->init(tp);
209 	if (err) {
210 		module_put(tp->ops->owner);
211 		goto errout;
212 	}
213 	return tp;
214 
215 errout:
216 	kfree(tp);
217 	return ERR_PTR(err);
218 }
219 
220 static void tcf_proto_get(struct tcf_proto *tp)
221 {
222 	refcount_inc(&tp->refcnt);
223 }
224 
225 static void tcf_chain_put(struct tcf_chain *chain);
226 
227 static void tcf_proto_destroy(struct tcf_proto *tp, bool rtnl_held,
228 			      struct netlink_ext_ack *extack)
229 {
230 	tp->ops->destroy(tp, rtnl_held, extack);
231 	tcf_chain_put(tp->chain);
232 	module_put(tp->ops->owner);
233 	kfree_rcu(tp, rcu);
234 }
235 
236 static void tcf_proto_put(struct tcf_proto *tp, bool rtnl_held,
237 			  struct netlink_ext_ack *extack)
238 {
239 	if (refcount_dec_and_test(&tp->refcnt))
240 		tcf_proto_destroy(tp, rtnl_held, extack);
241 }
242 
243 static int walker_check_empty(struct tcf_proto *tp, void *fh,
244 			      struct tcf_walker *arg)
245 {
246 	if (fh) {
247 		arg->nonempty = true;
248 		return -1;
249 	}
250 	return 0;
251 }
252 
253 static bool tcf_proto_is_empty(struct tcf_proto *tp, bool rtnl_held)
254 {
255 	struct tcf_walker walker = { .fn = walker_check_empty, };
256 
257 	if (tp->ops->walk) {
258 		tp->ops->walk(tp, &walker, rtnl_held);
259 		return !walker.nonempty;
260 	}
261 	return true;
262 }
263 
264 static bool tcf_proto_check_delete(struct tcf_proto *tp, bool rtnl_held)
265 {
266 	spin_lock(&tp->lock);
267 	if (tcf_proto_is_empty(tp, rtnl_held))
268 		tp->deleting = true;
269 	spin_unlock(&tp->lock);
270 	return tp->deleting;
271 }
272 
273 static void tcf_proto_mark_delete(struct tcf_proto *tp)
274 {
275 	spin_lock(&tp->lock);
276 	tp->deleting = true;
277 	spin_unlock(&tp->lock);
278 }
279 
280 static bool tcf_proto_is_deleting(struct tcf_proto *tp)
281 {
282 	bool deleting;
283 
284 	spin_lock(&tp->lock);
285 	deleting = tp->deleting;
286 	spin_unlock(&tp->lock);
287 
288 	return deleting;
289 }
290 
291 #define ASSERT_BLOCK_LOCKED(block)					\
292 	lockdep_assert_held(&(block)->lock)
293 
294 struct tcf_filter_chain_list_item {
295 	struct list_head list;
296 	tcf_chain_head_change_t *chain_head_change;
297 	void *chain_head_change_priv;
298 };
299 
300 static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
301 					  u32 chain_index)
302 {
303 	struct tcf_chain *chain;
304 
305 	ASSERT_BLOCK_LOCKED(block);
306 
307 	chain = kzalloc(sizeof(*chain), GFP_KERNEL);
308 	if (!chain)
309 		return NULL;
310 	list_add_tail(&chain->list, &block->chain_list);
311 	mutex_init(&chain->filter_chain_lock);
312 	chain->block = block;
313 	chain->index = chain_index;
314 	chain->refcnt = 1;
315 	if (!chain->index)
316 		block->chain0.chain = chain;
317 	return chain;
318 }
319 
320 static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
321 				       struct tcf_proto *tp_head)
322 {
323 	if (item->chain_head_change)
324 		item->chain_head_change(tp_head, item->chain_head_change_priv);
325 }
326 
327 static void tcf_chain0_head_change(struct tcf_chain *chain,
328 				   struct tcf_proto *tp_head)
329 {
330 	struct tcf_filter_chain_list_item *item;
331 	struct tcf_block *block = chain->block;
332 
333 	if (chain->index)
334 		return;
335 
336 	mutex_lock(&block->lock);
337 	list_for_each_entry(item, &block->chain0.filter_chain_list, list)
338 		tcf_chain_head_change_item(item, tp_head);
339 	mutex_unlock(&block->lock);
340 }
341 
342 /* Returns true if block can be safely freed. */
343 
344 static bool tcf_chain_detach(struct tcf_chain *chain)
345 {
346 	struct tcf_block *block = chain->block;
347 
348 	ASSERT_BLOCK_LOCKED(block);
349 
350 	list_del(&chain->list);
351 	if (!chain->index)
352 		block->chain0.chain = NULL;
353 
354 	if (list_empty(&block->chain_list) &&
355 	    refcount_read(&block->refcnt) == 0)
356 		return true;
357 
358 	return false;
359 }
360 
361 static void tcf_block_destroy(struct tcf_block *block)
362 {
363 	mutex_destroy(&block->lock);
364 	kfree_rcu(block, rcu);
365 }
366 
367 static void tcf_chain_destroy(struct tcf_chain *chain, bool free_block)
368 {
369 	struct tcf_block *block = chain->block;
370 
371 	mutex_destroy(&chain->filter_chain_lock);
372 	kfree_rcu(chain, rcu);
373 	if (free_block)
374 		tcf_block_destroy(block);
375 }
376 
377 static void tcf_chain_hold(struct tcf_chain *chain)
378 {
379 	ASSERT_BLOCK_LOCKED(chain->block);
380 
381 	++chain->refcnt;
382 }
383 
384 static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
385 {
386 	ASSERT_BLOCK_LOCKED(chain->block);
387 
388 	/* In case all the references are action references, this
389 	 * chain should not be shown to the user.
390 	 */
391 	return chain->refcnt == chain->action_refcnt;
392 }
393 
394 static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
395 					  u32 chain_index)
396 {
397 	struct tcf_chain *chain;
398 
399 	ASSERT_BLOCK_LOCKED(block);
400 
401 	list_for_each_entry(chain, &block->chain_list, list) {
402 		if (chain->index == chain_index)
403 			return chain;
404 	}
405 	return NULL;
406 }
407 
408 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
409 			   u32 seq, u16 flags, int event, bool unicast);
410 
411 static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
412 					 u32 chain_index, bool create,
413 					 bool by_act)
414 {
415 	struct tcf_chain *chain = NULL;
416 	bool is_first_reference;
417 
418 	mutex_lock(&block->lock);
419 	chain = tcf_chain_lookup(block, chain_index);
420 	if (chain) {
421 		tcf_chain_hold(chain);
422 	} else {
423 		if (!create)
424 			goto errout;
425 		chain = tcf_chain_create(block, chain_index);
426 		if (!chain)
427 			goto errout;
428 	}
429 
430 	if (by_act)
431 		++chain->action_refcnt;
432 	is_first_reference = chain->refcnt - chain->action_refcnt == 1;
433 	mutex_unlock(&block->lock);
434 
435 	/* Send notification only in case we got the first
436 	 * non-action reference. Until then, the chain acts only as
437 	 * a placeholder for actions pointing to it and user ought
438 	 * not know about them.
439 	 */
440 	if (is_first_reference && !by_act)
441 		tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
442 				RTM_NEWCHAIN, false);
443 
444 	return chain;
445 
446 errout:
447 	mutex_unlock(&block->lock);
448 	return chain;
449 }
450 
451 static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
452 				       bool create)
453 {
454 	return __tcf_chain_get(block, chain_index, create, false);
455 }
456 
457 struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
458 {
459 	return __tcf_chain_get(block, chain_index, true, true);
460 }
461 EXPORT_SYMBOL(tcf_chain_get_by_act);
462 
463 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
464 			       void *tmplt_priv);
465 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
466 				  void *tmplt_priv, u32 chain_index,
467 				  struct tcf_block *block, struct sk_buff *oskb,
468 				  u32 seq, u16 flags, bool unicast);
469 
470 static void __tcf_chain_put(struct tcf_chain *chain, bool by_act,
471 			    bool explicitly_created)
472 {
473 	struct tcf_block *block = chain->block;
474 	const struct tcf_proto_ops *tmplt_ops;
475 	bool free_block = false;
476 	unsigned int refcnt;
477 	void *tmplt_priv;
478 
479 	mutex_lock(&block->lock);
480 	if (explicitly_created) {
481 		if (!chain->explicitly_created) {
482 			mutex_unlock(&block->lock);
483 			return;
484 		}
485 		chain->explicitly_created = false;
486 	}
487 
488 	if (by_act)
489 		chain->action_refcnt--;
490 
491 	/* tc_chain_notify_delete can't be called while holding block lock.
492 	 * However, when block is unlocked chain can be changed concurrently, so
493 	 * save these to temporary variables.
494 	 */
495 	refcnt = --chain->refcnt;
496 	tmplt_ops = chain->tmplt_ops;
497 	tmplt_priv = chain->tmplt_priv;
498 
499 	/* The last dropped non-action reference will trigger notification. */
500 	if (refcnt - chain->action_refcnt == 0 && !by_act) {
501 		tc_chain_notify_delete(tmplt_ops, tmplt_priv, chain->index,
502 				       block, NULL, 0, 0, false);
503 		/* Last reference to chain, no need to lock. */
504 		chain->flushing = false;
505 	}
506 
507 	if (refcnt == 0)
508 		free_block = tcf_chain_detach(chain);
509 	mutex_unlock(&block->lock);
510 
511 	if (refcnt == 0) {
512 		tc_chain_tmplt_del(tmplt_ops, tmplt_priv);
513 		tcf_chain_destroy(chain, free_block);
514 	}
515 }
516 
517 static void tcf_chain_put(struct tcf_chain *chain)
518 {
519 	__tcf_chain_put(chain, false, false);
520 }
521 
522 void tcf_chain_put_by_act(struct tcf_chain *chain)
523 {
524 	__tcf_chain_put(chain, true, false);
525 }
526 EXPORT_SYMBOL(tcf_chain_put_by_act);
527 
528 static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
529 {
530 	__tcf_chain_put(chain, false, true);
531 }
532 
533 static void tcf_chain_flush(struct tcf_chain *chain, bool rtnl_held)
534 {
535 	struct tcf_proto *tp, *tp_next;
536 
537 	mutex_lock(&chain->filter_chain_lock);
538 	tp = tcf_chain_dereference(chain->filter_chain, chain);
539 	RCU_INIT_POINTER(chain->filter_chain, NULL);
540 	tcf_chain0_head_change(chain, NULL);
541 	chain->flushing = true;
542 	mutex_unlock(&chain->filter_chain_lock);
543 
544 	while (tp) {
545 		tp_next = rcu_dereference_protected(tp->next, 1);
546 		tcf_proto_put(tp, rtnl_held, NULL);
547 		tp = tp_next;
548 	}
549 }
550 
551 static struct tcf_block *tc_dev_ingress_block(struct net_device *dev)
552 {
553 	const struct Qdisc_class_ops *cops;
554 	struct Qdisc *qdisc;
555 
556 	if (!dev_ingress_queue(dev))
557 		return NULL;
558 
559 	qdisc = dev_ingress_queue(dev)->qdisc_sleeping;
560 	if (!qdisc)
561 		return NULL;
562 
563 	cops = qdisc->ops->cl_ops;
564 	if (!cops)
565 		return NULL;
566 
567 	if (!cops->tcf_block)
568 		return NULL;
569 
570 	return cops->tcf_block(qdisc, TC_H_MIN_INGRESS, NULL);
571 }
572 
573 static struct rhashtable indr_setup_block_ht;
574 
575 struct tc_indr_block_dev {
576 	struct rhash_head ht_node;
577 	struct net_device *dev;
578 	unsigned int refcnt;
579 	struct list_head cb_list;
580 	struct tcf_block *block;
581 };
582 
583 struct tc_indr_block_cb {
584 	struct list_head list;
585 	void *cb_priv;
586 	tc_indr_block_bind_cb_t *cb;
587 	void *cb_ident;
588 };
589 
590 static const struct rhashtable_params tc_indr_setup_block_ht_params = {
591 	.key_offset	= offsetof(struct tc_indr_block_dev, dev),
592 	.head_offset	= offsetof(struct tc_indr_block_dev, ht_node),
593 	.key_len	= sizeof(struct net_device *),
594 };
595 
596 static struct tc_indr_block_dev *
597 tc_indr_block_dev_lookup(struct net_device *dev)
598 {
599 	return rhashtable_lookup_fast(&indr_setup_block_ht, &dev,
600 				      tc_indr_setup_block_ht_params);
601 }
602 
603 static struct tc_indr_block_dev *tc_indr_block_dev_get(struct net_device *dev)
604 {
605 	struct tc_indr_block_dev *indr_dev;
606 
607 	indr_dev = tc_indr_block_dev_lookup(dev);
608 	if (indr_dev)
609 		goto inc_ref;
610 
611 	indr_dev = kzalloc(sizeof(*indr_dev), GFP_KERNEL);
612 	if (!indr_dev)
613 		return NULL;
614 
615 	INIT_LIST_HEAD(&indr_dev->cb_list);
616 	indr_dev->dev = dev;
617 	indr_dev->block = tc_dev_ingress_block(dev);
618 	if (rhashtable_insert_fast(&indr_setup_block_ht, &indr_dev->ht_node,
619 				   tc_indr_setup_block_ht_params)) {
620 		kfree(indr_dev);
621 		return NULL;
622 	}
623 
624 inc_ref:
625 	indr_dev->refcnt++;
626 	return indr_dev;
627 }
628 
629 static void tc_indr_block_dev_put(struct tc_indr_block_dev *indr_dev)
630 {
631 	if (--indr_dev->refcnt)
632 		return;
633 
634 	rhashtable_remove_fast(&indr_setup_block_ht, &indr_dev->ht_node,
635 			       tc_indr_setup_block_ht_params);
636 	kfree(indr_dev);
637 }
638 
639 static struct tc_indr_block_cb *
640 tc_indr_block_cb_lookup(struct tc_indr_block_dev *indr_dev,
641 			tc_indr_block_bind_cb_t *cb, void *cb_ident)
642 {
643 	struct tc_indr_block_cb *indr_block_cb;
644 
645 	list_for_each_entry(indr_block_cb, &indr_dev->cb_list, list)
646 		if (indr_block_cb->cb == cb &&
647 		    indr_block_cb->cb_ident == cb_ident)
648 			return indr_block_cb;
649 	return NULL;
650 }
651 
652 static struct tc_indr_block_cb *
653 tc_indr_block_cb_add(struct tc_indr_block_dev *indr_dev, void *cb_priv,
654 		     tc_indr_block_bind_cb_t *cb, void *cb_ident)
655 {
656 	struct tc_indr_block_cb *indr_block_cb;
657 
658 	indr_block_cb = tc_indr_block_cb_lookup(indr_dev, cb, cb_ident);
659 	if (indr_block_cb)
660 		return ERR_PTR(-EEXIST);
661 
662 	indr_block_cb = kzalloc(sizeof(*indr_block_cb), GFP_KERNEL);
663 	if (!indr_block_cb)
664 		return ERR_PTR(-ENOMEM);
665 
666 	indr_block_cb->cb_priv = cb_priv;
667 	indr_block_cb->cb = cb;
668 	indr_block_cb->cb_ident = cb_ident;
669 	list_add(&indr_block_cb->list, &indr_dev->cb_list);
670 
671 	return indr_block_cb;
672 }
673 
674 static void tc_indr_block_cb_del(struct tc_indr_block_cb *indr_block_cb)
675 {
676 	list_del(&indr_block_cb->list);
677 	kfree(indr_block_cb);
678 }
679 
680 static void tc_indr_block_ing_cmd(struct tc_indr_block_dev *indr_dev,
681 				  struct tc_indr_block_cb *indr_block_cb,
682 				  enum tc_block_command command)
683 {
684 	struct tc_block_offload bo = {
685 		.command	= command,
686 		.binder_type	= TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS,
687 		.block		= indr_dev->block,
688 	};
689 
690 	if (!indr_dev->block)
691 		return;
692 
693 	indr_block_cb->cb(indr_dev->dev, indr_block_cb->cb_priv, TC_SETUP_BLOCK,
694 			  &bo);
695 }
696 
697 int __tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
698 				tc_indr_block_bind_cb_t *cb, void *cb_ident)
699 {
700 	struct tc_indr_block_cb *indr_block_cb;
701 	struct tc_indr_block_dev *indr_dev;
702 	int err;
703 
704 	indr_dev = tc_indr_block_dev_get(dev);
705 	if (!indr_dev)
706 		return -ENOMEM;
707 
708 	indr_block_cb = tc_indr_block_cb_add(indr_dev, cb_priv, cb, cb_ident);
709 	err = PTR_ERR_OR_ZERO(indr_block_cb);
710 	if (err)
711 		goto err_dev_put;
712 
713 	tc_indr_block_ing_cmd(indr_dev, indr_block_cb, TC_BLOCK_BIND);
714 	return 0;
715 
716 err_dev_put:
717 	tc_indr_block_dev_put(indr_dev);
718 	return err;
719 }
720 EXPORT_SYMBOL_GPL(__tc_indr_block_cb_register);
721 
722 int tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
723 			      tc_indr_block_bind_cb_t *cb, void *cb_ident)
724 {
725 	int err;
726 
727 	rtnl_lock();
728 	err = __tc_indr_block_cb_register(dev, cb_priv, cb, cb_ident);
729 	rtnl_unlock();
730 
731 	return err;
732 }
733 EXPORT_SYMBOL_GPL(tc_indr_block_cb_register);
734 
735 void __tc_indr_block_cb_unregister(struct net_device *dev,
736 				   tc_indr_block_bind_cb_t *cb, void *cb_ident)
737 {
738 	struct tc_indr_block_cb *indr_block_cb;
739 	struct tc_indr_block_dev *indr_dev;
740 
741 	indr_dev = tc_indr_block_dev_lookup(dev);
742 	if (!indr_dev)
743 		return;
744 
745 	indr_block_cb = tc_indr_block_cb_lookup(indr_dev, cb, cb_ident);
746 	if (!indr_block_cb)
747 		return;
748 
749 	/* Send unbind message if required to free any block cbs. */
750 	tc_indr_block_ing_cmd(indr_dev, indr_block_cb, TC_BLOCK_UNBIND);
751 	tc_indr_block_cb_del(indr_block_cb);
752 	tc_indr_block_dev_put(indr_dev);
753 }
754 EXPORT_SYMBOL_GPL(__tc_indr_block_cb_unregister);
755 
756 void tc_indr_block_cb_unregister(struct net_device *dev,
757 				 tc_indr_block_bind_cb_t *cb, void *cb_ident)
758 {
759 	rtnl_lock();
760 	__tc_indr_block_cb_unregister(dev, cb, cb_ident);
761 	rtnl_unlock();
762 }
763 EXPORT_SYMBOL_GPL(tc_indr_block_cb_unregister);
764 
765 static void tc_indr_block_call(struct tcf_block *block, struct net_device *dev,
766 			       struct tcf_block_ext_info *ei,
767 			       enum tc_block_command command,
768 			       struct netlink_ext_ack *extack)
769 {
770 	struct tc_indr_block_cb *indr_block_cb;
771 	struct tc_indr_block_dev *indr_dev;
772 	struct tc_block_offload bo = {
773 		.command	= command,
774 		.binder_type	= ei->binder_type,
775 		.block		= block,
776 		.extack		= extack,
777 	};
778 
779 	indr_dev = tc_indr_block_dev_lookup(dev);
780 	if (!indr_dev)
781 		return;
782 
783 	indr_dev->block = command == TC_BLOCK_BIND ? block : NULL;
784 
785 	list_for_each_entry(indr_block_cb, &indr_dev->cb_list, list)
786 		indr_block_cb->cb(dev, indr_block_cb->cb_priv, TC_SETUP_BLOCK,
787 				  &bo);
788 }
789 
790 static bool tcf_block_offload_in_use(struct tcf_block *block)
791 {
792 	return block->offloadcnt;
793 }
794 
795 static int tcf_block_offload_cmd(struct tcf_block *block,
796 				 struct net_device *dev,
797 				 struct tcf_block_ext_info *ei,
798 				 enum tc_block_command command,
799 				 struct netlink_ext_ack *extack)
800 {
801 	struct tc_block_offload bo = {};
802 
803 	bo.command = command;
804 	bo.binder_type = ei->binder_type;
805 	bo.block = block;
806 	bo.extack = extack;
807 	return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
808 }
809 
810 static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
811 				  struct tcf_block_ext_info *ei,
812 				  struct netlink_ext_ack *extack)
813 {
814 	struct net_device *dev = q->dev_queue->dev;
815 	int err;
816 
817 	if (!dev->netdev_ops->ndo_setup_tc)
818 		goto no_offload_dev_inc;
819 
820 	/* If tc offload feature is disabled and the block we try to bind
821 	 * to already has some offloaded filters, forbid to bind.
822 	 */
823 	if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) {
824 		NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
825 		return -EOPNOTSUPP;
826 	}
827 
828 	err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND, extack);
829 	if (err == -EOPNOTSUPP)
830 		goto no_offload_dev_inc;
831 	if (err)
832 		return err;
833 
834 	tc_indr_block_call(block, dev, ei, TC_BLOCK_BIND, extack);
835 	return 0;
836 
837 no_offload_dev_inc:
838 	if (tcf_block_offload_in_use(block))
839 		return -EOPNOTSUPP;
840 	block->nooffloaddevcnt++;
841 	tc_indr_block_call(block, dev, ei, TC_BLOCK_BIND, extack);
842 	return 0;
843 }
844 
845 static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
846 				     struct tcf_block_ext_info *ei)
847 {
848 	struct net_device *dev = q->dev_queue->dev;
849 	int err;
850 
851 	tc_indr_block_call(block, dev, ei, TC_BLOCK_UNBIND, NULL);
852 
853 	if (!dev->netdev_ops->ndo_setup_tc)
854 		goto no_offload_dev_dec;
855 	err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND, NULL);
856 	if (err == -EOPNOTSUPP)
857 		goto no_offload_dev_dec;
858 	return;
859 
860 no_offload_dev_dec:
861 	WARN_ON(block->nooffloaddevcnt-- == 0);
862 }
863 
864 static int
865 tcf_chain0_head_change_cb_add(struct tcf_block *block,
866 			      struct tcf_block_ext_info *ei,
867 			      struct netlink_ext_ack *extack)
868 {
869 	struct tcf_filter_chain_list_item *item;
870 	struct tcf_chain *chain0;
871 
872 	item = kmalloc(sizeof(*item), GFP_KERNEL);
873 	if (!item) {
874 		NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
875 		return -ENOMEM;
876 	}
877 	item->chain_head_change = ei->chain_head_change;
878 	item->chain_head_change_priv = ei->chain_head_change_priv;
879 
880 	mutex_lock(&block->lock);
881 	chain0 = block->chain0.chain;
882 	if (chain0)
883 		tcf_chain_hold(chain0);
884 	else
885 		list_add(&item->list, &block->chain0.filter_chain_list);
886 	mutex_unlock(&block->lock);
887 
888 	if (chain0) {
889 		struct tcf_proto *tp_head;
890 
891 		mutex_lock(&chain0->filter_chain_lock);
892 
893 		tp_head = tcf_chain_dereference(chain0->filter_chain, chain0);
894 		if (tp_head)
895 			tcf_chain_head_change_item(item, tp_head);
896 
897 		mutex_lock(&block->lock);
898 		list_add(&item->list, &block->chain0.filter_chain_list);
899 		mutex_unlock(&block->lock);
900 
901 		mutex_unlock(&chain0->filter_chain_lock);
902 		tcf_chain_put(chain0);
903 	}
904 
905 	return 0;
906 }
907 
908 static void
909 tcf_chain0_head_change_cb_del(struct tcf_block *block,
910 			      struct tcf_block_ext_info *ei)
911 {
912 	struct tcf_filter_chain_list_item *item;
913 
914 	mutex_lock(&block->lock);
915 	list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
916 		if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
917 		    (item->chain_head_change == ei->chain_head_change &&
918 		     item->chain_head_change_priv == ei->chain_head_change_priv)) {
919 			if (block->chain0.chain)
920 				tcf_chain_head_change_item(item, NULL);
921 			list_del(&item->list);
922 			mutex_unlock(&block->lock);
923 
924 			kfree(item);
925 			return;
926 		}
927 	}
928 	mutex_unlock(&block->lock);
929 	WARN_ON(1);
930 }
931 
932 struct tcf_net {
933 	spinlock_t idr_lock; /* Protects idr */
934 	struct idr idr;
935 };
936 
937 static unsigned int tcf_net_id;
938 
939 static int tcf_block_insert(struct tcf_block *block, struct net *net,
940 			    struct netlink_ext_ack *extack)
941 {
942 	struct tcf_net *tn = net_generic(net, tcf_net_id);
943 	int err;
944 
945 	idr_preload(GFP_KERNEL);
946 	spin_lock(&tn->idr_lock);
947 	err = idr_alloc_u32(&tn->idr, block, &block->index, block->index,
948 			    GFP_NOWAIT);
949 	spin_unlock(&tn->idr_lock);
950 	idr_preload_end();
951 
952 	return err;
953 }
954 
955 static void tcf_block_remove(struct tcf_block *block, struct net *net)
956 {
957 	struct tcf_net *tn = net_generic(net, tcf_net_id);
958 
959 	spin_lock(&tn->idr_lock);
960 	idr_remove(&tn->idr, block->index);
961 	spin_unlock(&tn->idr_lock);
962 }
963 
964 static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
965 					  u32 block_index,
966 					  struct netlink_ext_ack *extack)
967 {
968 	struct tcf_block *block;
969 
970 	block = kzalloc(sizeof(*block), GFP_KERNEL);
971 	if (!block) {
972 		NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
973 		return ERR_PTR(-ENOMEM);
974 	}
975 	mutex_init(&block->lock);
976 	INIT_LIST_HEAD(&block->chain_list);
977 	INIT_LIST_HEAD(&block->cb_list);
978 	INIT_LIST_HEAD(&block->owner_list);
979 	INIT_LIST_HEAD(&block->chain0.filter_chain_list);
980 
981 	refcount_set(&block->refcnt, 1);
982 	block->net = net;
983 	block->index = block_index;
984 
985 	/* Don't store q pointer for blocks which are shared */
986 	if (!tcf_block_shared(block))
987 		block->q = q;
988 	return block;
989 }
990 
991 static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
992 {
993 	struct tcf_net *tn = net_generic(net, tcf_net_id);
994 
995 	return idr_find(&tn->idr, block_index);
996 }
997 
998 static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index)
999 {
1000 	struct tcf_block *block;
1001 
1002 	rcu_read_lock();
1003 	block = tcf_block_lookup(net, block_index);
1004 	if (block && !refcount_inc_not_zero(&block->refcnt))
1005 		block = NULL;
1006 	rcu_read_unlock();
1007 
1008 	return block;
1009 }
1010 
1011 static struct tcf_chain *
1012 __tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
1013 {
1014 	mutex_lock(&block->lock);
1015 	if (chain)
1016 		chain = list_is_last(&chain->list, &block->chain_list) ?
1017 			NULL : list_next_entry(chain, list);
1018 	else
1019 		chain = list_first_entry_or_null(&block->chain_list,
1020 						 struct tcf_chain, list);
1021 
1022 	/* skip all action-only chains */
1023 	while (chain && tcf_chain_held_by_acts_only(chain))
1024 		chain = list_is_last(&chain->list, &block->chain_list) ?
1025 			NULL : list_next_entry(chain, list);
1026 
1027 	if (chain)
1028 		tcf_chain_hold(chain);
1029 	mutex_unlock(&block->lock);
1030 
1031 	return chain;
1032 }
1033 
1034 /* Function to be used by all clients that want to iterate over all chains on
1035  * block. It properly obtains block->lock and takes reference to chain before
1036  * returning it. Users of this function must be tolerant to concurrent chain
1037  * insertion/deletion or ensure that no concurrent chain modification is
1038  * possible. Note that all netlink dump callbacks cannot guarantee to provide
1039  * consistent dump because rtnl lock is released each time skb is filled with
1040  * data and sent to user-space.
1041  */
1042 
1043 struct tcf_chain *
1044 tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
1045 {
1046 	struct tcf_chain *chain_next = __tcf_get_next_chain(block, chain);
1047 
1048 	if (chain)
1049 		tcf_chain_put(chain);
1050 
1051 	return chain_next;
1052 }
1053 EXPORT_SYMBOL(tcf_get_next_chain);
1054 
1055 static struct tcf_proto *
1056 __tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
1057 {
1058 	u32 prio = 0;
1059 
1060 	ASSERT_RTNL();
1061 	mutex_lock(&chain->filter_chain_lock);
1062 
1063 	if (!tp) {
1064 		tp = tcf_chain_dereference(chain->filter_chain, chain);
1065 	} else if (tcf_proto_is_deleting(tp)) {
1066 		/* 'deleting' flag is set and chain->filter_chain_lock was
1067 		 * unlocked, which means next pointer could be invalid. Restart
1068 		 * search.
1069 		 */
1070 		prio = tp->prio + 1;
1071 		tp = tcf_chain_dereference(chain->filter_chain, chain);
1072 
1073 		for (; tp; tp = tcf_chain_dereference(tp->next, chain))
1074 			if (!tp->deleting && tp->prio >= prio)
1075 				break;
1076 	} else {
1077 		tp = tcf_chain_dereference(tp->next, chain);
1078 	}
1079 
1080 	if (tp)
1081 		tcf_proto_get(tp);
1082 
1083 	mutex_unlock(&chain->filter_chain_lock);
1084 
1085 	return tp;
1086 }
1087 
1088 /* Function to be used by all clients that want to iterate over all tp's on
1089  * chain. Users of this function must be tolerant to concurrent tp
1090  * insertion/deletion or ensure that no concurrent chain modification is
1091  * possible. Note that all netlink dump callbacks cannot guarantee to provide
1092  * consistent dump because rtnl lock is released each time skb is filled with
1093  * data and sent to user-space.
1094  */
1095 
1096 struct tcf_proto *
1097 tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp,
1098 		   bool rtnl_held)
1099 {
1100 	struct tcf_proto *tp_next = __tcf_get_next_proto(chain, tp);
1101 
1102 	if (tp)
1103 		tcf_proto_put(tp, rtnl_held, NULL);
1104 
1105 	return tp_next;
1106 }
1107 EXPORT_SYMBOL(tcf_get_next_proto);
1108 
1109 static void tcf_block_flush_all_chains(struct tcf_block *block, bool rtnl_held)
1110 {
1111 	struct tcf_chain *chain;
1112 
1113 	/* Last reference to block. At this point chains cannot be added or
1114 	 * removed concurrently.
1115 	 */
1116 	for (chain = tcf_get_next_chain(block, NULL);
1117 	     chain;
1118 	     chain = tcf_get_next_chain(block, chain)) {
1119 		tcf_chain_put_explicitly_created(chain);
1120 		tcf_chain_flush(chain, rtnl_held);
1121 	}
1122 }
1123 
1124 /* Lookup Qdisc and increments its reference counter.
1125  * Set parent, if necessary.
1126  */
1127 
1128 static int __tcf_qdisc_find(struct net *net, struct Qdisc **q,
1129 			    u32 *parent, int ifindex, bool rtnl_held,
1130 			    struct netlink_ext_ack *extack)
1131 {
1132 	const struct Qdisc_class_ops *cops;
1133 	struct net_device *dev;
1134 	int err = 0;
1135 
1136 	if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
1137 		return 0;
1138 
1139 	rcu_read_lock();
1140 
1141 	/* Find link */
1142 	dev = dev_get_by_index_rcu(net, ifindex);
1143 	if (!dev) {
1144 		rcu_read_unlock();
1145 		return -ENODEV;
1146 	}
1147 
1148 	/* Find qdisc */
1149 	if (!*parent) {
1150 		*q = dev->qdisc;
1151 		*parent = (*q)->handle;
1152 	} else {
1153 		*q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent));
1154 		if (!*q) {
1155 			NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1156 			err = -EINVAL;
1157 			goto errout_rcu;
1158 		}
1159 	}
1160 
1161 	*q = qdisc_refcount_inc_nz(*q);
1162 	if (!*q) {
1163 		NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1164 		err = -EINVAL;
1165 		goto errout_rcu;
1166 	}
1167 
1168 	/* Is it classful? */
1169 	cops = (*q)->ops->cl_ops;
1170 	if (!cops) {
1171 		NL_SET_ERR_MSG(extack, "Qdisc not classful");
1172 		err = -EINVAL;
1173 		goto errout_qdisc;
1174 	}
1175 
1176 	if (!cops->tcf_block) {
1177 		NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
1178 		err = -EOPNOTSUPP;
1179 		goto errout_qdisc;
1180 	}
1181 
1182 errout_rcu:
1183 	/* At this point we know that qdisc is not noop_qdisc,
1184 	 * which means that qdisc holds a reference to net_device
1185 	 * and we hold a reference to qdisc, so it is safe to release
1186 	 * rcu read lock.
1187 	 */
1188 	rcu_read_unlock();
1189 	return err;
1190 
1191 errout_qdisc:
1192 	rcu_read_unlock();
1193 
1194 	if (rtnl_held)
1195 		qdisc_put(*q);
1196 	else
1197 		qdisc_put_unlocked(*q);
1198 	*q = NULL;
1199 
1200 	return err;
1201 }
1202 
1203 static int __tcf_qdisc_cl_find(struct Qdisc *q, u32 parent, unsigned long *cl,
1204 			       int ifindex, struct netlink_ext_ack *extack)
1205 {
1206 	if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
1207 		return 0;
1208 
1209 	/* Do we search for filter, attached to class? */
1210 	if (TC_H_MIN(parent)) {
1211 		const struct Qdisc_class_ops *cops = q->ops->cl_ops;
1212 
1213 		*cl = cops->find(q, parent);
1214 		if (*cl == 0) {
1215 			NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
1216 			return -ENOENT;
1217 		}
1218 	}
1219 
1220 	return 0;
1221 }
1222 
1223 static struct tcf_block *__tcf_block_find(struct net *net, struct Qdisc *q,
1224 					  unsigned long cl, int ifindex,
1225 					  u32 block_index,
1226 					  struct netlink_ext_ack *extack)
1227 {
1228 	struct tcf_block *block;
1229 
1230 	if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1231 		block = tcf_block_refcnt_get(net, block_index);
1232 		if (!block) {
1233 			NL_SET_ERR_MSG(extack, "Block of given index was not found");
1234 			return ERR_PTR(-EINVAL);
1235 		}
1236 	} else {
1237 		const struct Qdisc_class_ops *cops = q->ops->cl_ops;
1238 
1239 		block = cops->tcf_block(q, cl, extack);
1240 		if (!block)
1241 			return ERR_PTR(-EINVAL);
1242 
1243 		if (tcf_block_shared(block)) {
1244 			NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
1245 			return ERR_PTR(-EOPNOTSUPP);
1246 		}
1247 
1248 		/* Always take reference to block in order to support execution
1249 		 * of rules update path of cls API without rtnl lock. Caller
1250 		 * must release block when it is finished using it. 'if' block
1251 		 * of this conditional obtain reference to block by calling
1252 		 * tcf_block_refcnt_get().
1253 		 */
1254 		refcount_inc(&block->refcnt);
1255 	}
1256 
1257 	return block;
1258 }
1259 
1260 static void __tcf_block_put(struct tcf_block *block, struct Qdisc *q,
1261 			    struct tcf_block_ext_info *ei, bool rtnl_held)
1262 {
1263 	if (refcount_dec_and_mutex_lock(&block->refcnt, &block->lock)) {
1264 		/* Flushing/putting all chains will cause the block to be
1265 		 * deallocated when last chain is freed. However, if chain_list
1266 		 * is empty, block has to be manually deallocated. After block
1267 		 * reference counter reached 0, it is no longer possible to
1268 		 * increment it or add new chains to block.
1269 		 */
1270 		bool free_block = list_empty(&block->chain_list);
1271 
1272 		mutex_unlock(&block->lock);
1273 		if (tcf_block_shared(block))
1274 			tcf_block_remove(block, block->net);
1275 
1276 		if (q)
1277 			tcf_block_offload_unbind(block, q, ei);
1278 
1279 		if (free_block)
1280 			tcf_block_destroy(block);
1281 		else
1282 			tcf_block_flush_all_chains(block, rtnl_held);
1283 	} else if (q) {
1284 		tcf_block_offload_unbind(block, q, ei);
1285 	}
1286 }
1287 
1288 static void tcf_block_refcnt_put(struct tcf_block *block, bool rtnl_held)
1289 {
1290 	__tcf_block_put(block, NULL, NULL, rtnl_held);
1291 }
1292 
1293 /* Find tcf block.
1294  * Set q, parent, cl when appropriate.
1295  */
1296 
1297 static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
1298 					u32 *parent, unsigned long *cl,
1299 					int ifindex, u32 block_index,
1300 					struct netlink_ext_ack *extack)
1301 {
1302 	struct tcf_block *block;
1303 	int err = 0;
1304 
1305 	ASSERT_RTNL();
1306 
1307 	err = __tcf_qdisc_find(net, q, parent, ifindex, true, extack);
1308 	if (err)
1309 		goto errout;
1310 
1311 	err = __tcf_qdisc_cl_find(*q, *parent, cl, ifindex, extack);
1312 	if (err)
1313 		goto errout_qdisc;
1314 
1315 	block = __tcf_block_find(net, *q, *cl, ifindex, block_index, extack);
1316 	if (IS_ERR(block)) {
1317 		err = PTR_ERR(block);
1318 		goto errout_qdisc;
1319 	}
1320 
1321 	return block;
1322 
1323 errout_qdisc:
1324 	if (*q)
1325 		qdisc_put(*q);
1326 errout:
1327 	*q = NULL;
1328 	return ERR_PTR(err);
1329 }
1330 
1331 static void tcf_block_release(struct Qdisc *q, struct tcf_block *block,
1332 			      bool rtnl_held)
1333 {
1334 	if (!IS_ERR_OR_NULL(block))
1335 		tcf_block_refcnt_put(block, rtnl_held);
1336 
1337 	if (q) {
1338 		if (rtnl_held)
1339 			qdisc_put(q);
1340 		else
1341 			qdisc_put_unlocked(q);
1342 	}
1343 }
1344 
1345 struct tcf_block_owner_item {
1346 	struct list_head list;
1347 	struct Qdisc *q;
1348 	enum tcf_block_binder_type binder_type;
1349 };
1350 
1351 static void
1352 tcf_block_owner_netif_keep_dst(struct tcf_block *block,
1353 			       struct Qdisc *q,
1354 			       enum tcf_block_binder_type binder_type)
1355 {
1356 	if (block->keep_dst &&
1357 	    binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
1358 	    binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1359 		netif_keep_dst(qdisc_dev(q));
1360 }
1361 
1362 void tcf_block_netif_keep_dst(struct tcf_block *block)
1363 {
1364 	struct tcf_block_owner_item *item;
1365 
1366 	block->keep_dst = true;
1367 	list_for_each_entry(item, &block->owner_list, list)
1368 		tcf_block_owner_netif_keep_dst(block, item->q,
1369 					       item->binder_type);
1370 }
1371 EXPORT_SYMBOL(tcf_block_netif_keep_dst);
1372 
1373 static int tcf_block_owner_add(struct tcf_block *block,
1374 			       struct Qdisc *q,
1375 			       enum tcf_block_binder_type binder_type)
1376 {
1377 	struct tcf_block_owner_item *item;
1378 
1379 	item = kmalloc(sizeof(*item), GFP_KERNEL);
1380 	if (!item)
1381 		return -ENOMEM;
1382 	item->q = q;
1383 	item->binder_type = binder_type;
1384 	list_add(&item->list, &block->owner_list);
1385 	return 0;
1386 }
1387 
1388 static void tcf_block_owner_del(struct tcf_block *block,
1389 				struct Qdisc *q,
1390 				enum tcf_block_binder_type binder_type)
1391 {
1392 	struct tcf_block_owner_item *item;
1393 
1394 	list_for_each_entry(item, &block->owner_list, list) {
1395 		if (item->q == q && item->binder_type == binder_type) {
1396 			list_del(&item->list);
1397 			kfree(item);
1398 			return;
1399 		}
1400 	}
1401 	WARN_ON(1);
1402 }
1403 
1404 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
1405 		      struct tcf_block_ext_info *ei,
1406 		      struct netlink_ext_ack *extack)
1407 {
1408 	struct net *net = qdisc_net(q);
1409 	struct tcf_block *block = NULL;
1410 	int err;
1411 
1412 	if (ei->block_index)
1413 		/* block_index not 0 means the shared block is requested */
1414 		block = tcf_block_refcnt_get(net, ei->block_index);
1415 
1416 	if (!block) {
1417 		block = tcf_block_create(net, q, ei->block_index, extack);
1418 		if (IS_ERR(block))
1419 			return PTR_ERR(block);
1420 		if (tcf_block_shared(block)) {
1421 			err = tcf_block_insert(block, net, extack);
1422 			if (err)
1423 				goto err_block_insert;
1424 		}
1425 	}
1426 
1427 	err = tcf_block_owner_add(block, q, ei->binder_type);
1428 	if (err)
1429 		goto err_block_owner_add;
1430 
1431 	tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
1432 
1433 	err = tcf_chain0_head_change_cb_add(block, ei, extack);
1434 	if (err)
1435 		goto err_chain0_head_change_cb_add;
1436 
1437 	err = tcf_block_offload_bind(block, q, ei, extack);
1438 	if (err)
1439 		goto err_block_offload_bind;
1440 
1441 	*p_block = block;
1442 	return 0;
1443 
1444 err_block_offload_bind:
1445 	tcf_chain0_head_change_cb_del(block, ei);
1446 err_chain0_head_change_cb_add:
1447 	tcf_block_owner_del(block, q, ei->binder_type);
1448 err_block_owner_add:
1449 err_block_insert:
1450 	tcf_block_refcnt_put(block, true);
1451 	return err;
1452 }
1453 EXPORT_SYMBOL(tcf_block_get_ext);
1454 
1455 static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
1456 {
1457 	struct tcf_proto __rcu **p_filter_chain = priv;
1458 
1459 	rcu_assign_pointer(*p_filter_chain, tp_head);
1460 }
1461 
1462 int tcf_block_get(struct tcf_block **p_block,
1463 		  struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
1464 		  struct netlink_ext_ack *extack)
1465 {
1466 	struct tcf_block_ext_info ei = {
1467 		.chain_head_change = tcf_chain_head_change_dflt,
1468 		.chain_head_change_priv = p_filter_chain,
1469 	};
1470 
1471 	WARN_ON(!p_filter_chain);
1472 	return tcf_block_get_ext(p_block, q, &ei, extack);
1473 }
1474 EXPORT_SYMBOL(tcf_block_get);
1475 
1476 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
1477  * actions should be all removed after flushing.
1478  */
1479 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
1480 		       struct tcf_block_ext_info *ei)
1481 {
1482 	if (!block)
1483 		return;
1484 	tcf_chain0_head_change_cb_del(block, ei);
1485 	tcf_block_owner_del(block, q, ei->binder_type);
1486 
1487 	__tcf_block_put(block, q, ei, true);
1488 }
1489 EXPORT_SYMBOL(tcf_block_put_ext);
1490 
1491 void tcf_block_put(struct tcf_block *block)
1492 {
1493 	struct tcf_block_ext_info ei = {0, };
1494 
1495 	if (!block)
1496 		return;
1497 	tcf_block_put_ext(block, block->q, &ei);
1498 }
1499 
1500 EXPORT_SYMBOL(tcf_block_put);
1501 
1502 struct tcf_block_cb {
1503 	struct list_head list;
1504 	tc_setup_cb_t *cb;
1505 	void *cb_ident;
1506 	void *cb_priv;
1507 	unsigned int refcnt;
1508 };
1509 
1510 void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
1511 {
1512 	return block_cb->cb_priv;
1513 }
1514 EXPORT_SYMBOL(tcf_block_cb_priv);
1515 
1516 struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
1517 					 tc_setup_cb_t *cb, void *cb_ident)
1518 {	struct tcf_block_cb *block_cb;
1519 
1520 	list_for_each_entry(block_cb, &block->cb_list, list)
1521 		if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
1522 			return block_cb;
1523 	return NULL;
1524 }
1525 EXPORT_SYMBOL(tcf_block_cb_lookup);
1526 
1527 void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
1528 {
1529 	block_cb->refcnt++;
1530 }
1531 EXPORT_SYMBOL(tcf_block_cb_incref);
1532 
1533 unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
1534 {
1535 	return --block_cb->refcnt;
1536 }
1537 EXPORT_SYMBOL(tcf_block_cb_decref);
1538 
1539 static int
1540 tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb,
1541 			    void *cb_priv, bool add, bool offload_in_use,
1542 			    struct netlink_ext_ack *extack)
1543 {
1544 	struct tcf_chain *chain, *chain_prev;
1545 	struct tcf_proto *tp, *tp_prev;
1546 	int err;
1547 
1548 	for (chain = __tcf_get_next_chain(block, NULL);
1549 	     chain;
1550 	     chain_prev = chain,
1551 		     chain = __tcf_get_next_chain(block, chain),
1552 		     tcf_chain_put(chain_prev)) {
1553 		for (tp = __tcf_get_next_proto(chain, NULL); tp;
1554 		     tp_prev = tp,
1555 			     tp = __tcf_get_next_proto(chain, tp),
1556 			     tcf_proto_put(tp_prev, true, NULL)) {
1557 			if (tp->ops->reoffload) {
1558 				err = tp->ops->reoffload(tp, add, cb, cb_priv,
1559 							 extack);
1560 				if (err && add)
1561 					goto err_playback_remove;
1562 			} else if (add && offload_in_use) {
1563 				err = -EOPNOTSUPP;
1564 				NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
1565 				goto err_playback_remove;
1566 			}
1567 		}
1568 	}
1569 
1570 	return 0;
1571 
1572 err_playback_remove:
1573 	tcf_proto_put(tp, true, NULL);
1574 	tcf_chain_put(chain);
1575 	tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
1576 				    extack);
1577 	return err;
1578 }
1579 
1580 struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
1581 					     tc_setup_cb_t *cb, void *cb_ident,
1582 					     void *cb_priv,
1583 					     struct netlink_ext_ack *extack)
1584 {
1585 	struct tcf_block_cb *block_cb;
1586 	int err;
1587 
1588 	/* Replay any already present rules */
1589 	err = tcf_block_playback_offloads(block, cb, cb_priv, true,
1590 					  tcf_block_offload_in_use(block),
1591 					  extack);
1592 	if (err)
1593 		return ERR_PTR(err);
1594 
1595 	block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
1596 	if (!block_cb)
1597 		return ERR_PTR(-ENOMEM);
1598 	block_cb->cb = cb;
1599 	block_cb->cb_ident = cb_ident;
1600 	block_cb->cb_priv = cb_priv;
1601 	list_add(&block_cb->list, &block->cb_list);
1602 	return block_cb;
1603 }
1604 EXPORT_SYMBOL(__tcf_block_cb_register);
1605 
1606 int tcf_block_cb_register(struct tcf_block *block,
1607 			  tc_setup_cb_t *cb, void *cb_ident,
1608 			  void *cb_priv, struct netlink_ext_ack *extack)
1609 {
1610 	struct tcf_block_cb *block_cb;
1611 
1612 	block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv,
1613 					   extack);
1614 	return PTR_ERR_OR_ZERO(block_cb);
1615 }
1616 EXPORT_SYMBOL(tcf_block_cb_register);
1617 
1618 void __tcf_block_cb_unregister(struct tcf_block *block,
1619 			       struct tcf_block_cb *block_cb)
1620 {
1621 	tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv,
1622 				    false, tcf_block_offload_in_use(block),
1623 				    NULL);
1624 	list_del(&block_cb->list);
1625 	kfree(block_cb);
1626 }
1627 EXPORT_SYMBOL(__tcf_block_cb_unregister);
1628 
1629 void tcf_block_cb_unregister(struct tcf_block *block,
1630 			     tc_setup_cb_t *cb, void *cb_ident)
1631 {
1632 	struct tcf_block_cb *block_cb;
1633 
1634 	block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
1635 	if (!block_cb)
1636 		return;
1637 	__tcf_block_cb_unregister(block, block_cb);
1638 }
1639 EXPORT_SYMBOL(tcf_block_cb_unregister);
1640 
1641 /* Main classifier routine: scans classifier chain attached
1642  * to this qdisc, (optionally) tests for protocol and asks
1643  * specific classifiers.
1644  */
1645 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
1646 		 struct tcf_result *res, bool compat_mode)
1647 {
1648 #ifdef CONFIG_NET_CLS_ACT
1649 	const int max_reclassify_loop = 4;
1650 	const struct tcf_proto *orig_tp = tp;
1651 	const struct tcf_proto *first_tp;
1652 	int limit = 0;
1653 
1654 reclassify:
1655 #endif
1656 	for (; tp; tp = rcu_dereference_bh(tp->next)) {
1657 		__be16 protocol = tc_skb_protocol(skb);
1658 		int err;
1659 
1660 		if (tp->protocol != protocol &&
1661 		    tp->protocol != htons(ETH_P_ALL))
1662 			continue;
1663 
1664 		err = tp->classify(skb, tp, res);
1665 #ifdef CONFIG_NET_CLS_ACT
1666 		if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
1667 			first_tp = orig_tp;
1668 			goto reset;
1669 		} else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
1670 			first_tp = res->goto_tp;
1671 			goto reset;
1672 		}
1673 #endif
1674 		if (err >= 0)
1675 			return err;
1676 	}
1677 
1678 	return TC_ACT_UNSPEC; /* signal: continue lookup */
1679 #ifdef CONFIG_NET_CLS_ACT
1680 reset:
1681 	if (unlikely(limit++ >= max_reclassify_loop)) {
1682 		net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
1683 				       tp->chain->block->index,
1684 				       tp->prio & 0xffff,
1685 				       ntohs(tp->protocol));
1686 		return TC_ACT_SHOT;
1687 	}
1688 
1689 	tp = first_tp;
1690 	goto reclassify;
1691 #endif
1692 }
1693 EXPORT_SYMBOL(tcf_classify);
1694 
1695 struct tcf_chain_info {
1696 	struct tcf_proto __rcu **pprev;
1697 	struct tcf_proto __rcu *next;
1698 };
1699 
1700 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain *chain,
1701 					   struct tcf_chain_info *chain_info)
1702 {
1703 	return tcf_chain_dereference(*chain_info->pprev, chain);
1704 }
1705 
1706 static int tcf_chain_tp_insert(struct tcf_chain *chain,
1707 			       struct tcf_chain_info *chain_info,
1708 			       struct tcf_proto *tp)
1709 {
1710 	if (chain->flushing)
1711 		return -EAGAIN;
1712 
1713 	if (*chain_info->pprev == chain->filter_chain)
1714 		tcf_chain0_head_change(chain, tp);
1715 	tcf_proto_get(tp);
1716 	RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info));
1717 	rcu_assign_pointer(*chain_info->pprev, tp);
1718 
1719 	return 0;
1720 }
1721 
1722 static void tcf_chain_tp_remove(struct tcf_chain *chain,
1723 				struct tcf_chain_info *chain_info,
1724 				struct tcf_proto *tp)
1725 {
1726 	struct tcf_proto *next = tcf_chain_dereference(chain_info->next, chain);
1727 
1728 	tcf_proto_mark_delete(tp);
1729 	if (tp == chain->filter_chain)
1730 		tcf_chain0_head_change(chain, next);
1731 	RCU_INIT_POINTER(*chain_info->pprev, next);
1732 }
1733 
1734 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1735 					   struct tcf_chain_info *chain_info,
1736 					   u32 protocol, u32 prio,
1737 					   bool prio_allocate);
1738 
1739 /* Try to insert new proto.
1740  * If proto with specified priority already exists, free new proto
1741  * and return existing one.
1742  */
1743 
1744 static struct tcf_proto *tcf_chain_tp_insert_unique(struct tcf_chain *chain,
1745 						    struct tcf_proto *tp_new,
1746 						    u32 protocol, u32 prio,
1747 						    bool rtnl_held)
1748 {
1749 	struct tcf_chain_info chain_info;
1750 	struct tcf_proto *tp;
1751 	int err = 0;
1752 
1753 	mutex_lock(&chain->filter_chain_lock);
1754 
1755 	tp = tcf_chain_tp_find(chain, &chain_info,
1756 			       protocol, prio, false);
1757 	if (!tp)
1758 		err = tcf_chain_tp_insert(chain, &chain_info, tp_new);
1759 	mutex_unlock(&chain->filter_chain_lock);
1760 
1761 	if (tp) {
1762 		tcf_proto_destroy(tp_new, rtnl_held, NULL);
1763 		tp_new = tp;
1764 	} else if (err) {
1765 		tcf_proto_destroy(tp_new, rtnl_held, NULL);
1766 		tp_new = ERR_PTR(err);
1767 	}
1768 
1769 	return tp_new;
1770 }
1771 
1772 static void tcf_chain_tp_delete_empty(struct tcf_chain *chain,
1773 				      struct tcf_proto *tp, bool rtnl_held,
1774 				      struct netlink_ext_ack *extack)
1775 {
1776 	struct tcf_chain_info chain_info;
1777 	struct tcf_proto *tp_iter;
1778 	struct tcf_proto **pprev;
1779 	struct tcf_proto *next;
1780 
1781 	mutex_lock(&chain->filter_chain_lock);
1782 
1783 	/* Atomically find and remove tp from chain. */
1784 	for (pprev = &chain->filter_chain;
1785 	     (tp_iter = tcf_chain_dereference(*pprev, chain));
1786 	     pprev = &tp_iter->next) {
1787 		if (tp_iter == tp) {
1788 			chain_info.pprev = pprev;
1789 			chain_info.next = tp_iter->next;
1790 			WARN_ON(tp_iter->deleting);
1791 			break;
1792 		}
1793 	}
1794 	/* Verify that tp still exists and no new filters were inserted
1795 	 * concurrently.
1796 	 * Mark tp for deletion if it is empty.
1797 	 */
1798 	if (!tp_iter || !tcf_proto_check_delete(tp, rtnl_held)) {
1799 		mutex_unlock(&chain->filter_chain_lock);
1800 		return;
1801 	}
1802 
1803 	next = tcf_chain_dereference(chain_info.next, chain);
1804 	if (tp == chain->filter_chain)
1805 		tcf_chain0_head_change(chain, next);
1806 	RCU_INIT_POINTER(*chain_info.pprev, next);
1807 	mutex_unlock(&chain->filter_chain_lock);
1808 
1809 	tcf_proto_put(tp, rtnl_held, extack);
1810 }
1811 
1812 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1813 					   struct tcf_chain_info *chain_info,
1814 					   u32 protocol, u32 prio,
1815 					   bool prio_allocate)
1816 {
1817 	struct tcf_proto **pprev;
1818 	struct tcf_proto *tp;
1819 
1820 	/* Check the chain for existence of proto-tcf with this priority */
1821 	for (pprev = &chain->filter_chain;
1822 	     (tp = tcf_chain_dereference(*pprev, chain));
1823 	     pprev = &tp->next) {
1824 		if (tp->prio >= prio) {
1825 			if (tp->prio == prio) {
1826 				if (prio_allocate ||
1827 				    (tp->protocol != protocol && protocol))
1828 					return ERR_PTR(-EINVAL);
1829 			} else {
1830 				tp = NULL;
1831 			}
1832 			break;
1833 		}
1834 	}
1835 	chain_info->pprev = pprev;
1836 	if (tp) {
1837 		chain_info->next = tp->next;
1838 		tcf_proto_get(tp);
1839 	} else {
1840 		chain_info->next = NULL;
1841 	}
1842 	return tp;
1843 }
1844 
1845 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
1846 			 struct tcf_proto *tp, struct tcf_block *block,
1847 			 struct Qdisc *q, u32 parent, void *fh,
1848 			 u32 portid, u32 seq, u16 flags, int event,
1849 			 bool rtnl_held)
1850 {
1851 	struct tcmsg *tcm;
1852 	struct nlmsghdr  *nlh;
1853 	unsigned char *b = skb_tail_pointer(skb);
1854 
1855 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1856 	if (!nlh)
1857 		goto out_nlmsg_trim;
1858 	tcm = nlmsg_data(nlh);
1859 	tcm->tcm_family = AF_UNSPEC;
1860 	tcm->tcm__pad1 = 0;
1861 	tcm->tcm__pad2 = 0;
1862 	if (q) {
1863 		tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1864 		tcm->tcm_parent = parent;
1865 	} else {
1866 		tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1867 		tcm->tcm_block_index = block->index;
1868 	}
1869 	tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1870 	if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1871 		goto nla_put_failure;
1872 	if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1873 		goto nla_put_failure;
1874 	if (!fh) {
1875 		tcm->tcm_handle = 0;
1876 	} else {
1877 		if (tp->ops->dump &&
1878 		    tp->ops->dump(net, tp, fh, skb, tcm, rtnl_held) < 0)
1879 			goto nla_put_failure;
1880 	}
1881 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1882 	return skb->len;
1883 
1884 out_nlmsg_trim:
1885 nla_put_failure:
1886 	nlmsg_trim(skb, b);
1887 	return -1;
1888 }
1889 
1890 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1891 			  struct nlmsghdr *n, struct tcf_proto *tp,
1892 			  struct tcf_block *block, struct Qdisc *q,
1893 			  u32 parent, void *fh, int event, bool unicast,
1894 			  bool rtnl_held)
1895 {
1896 	struct sk_buff *skb;
1897 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1898 	int err = 0;
1899 
1900 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1901 	if (!skb)
1902 		return -ENOBUFS;
1903 
1904 	if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1905 			  n->nlmsg_seq, n->nlmsg_flags, event,
1906 			  rtnl_held) <= 0) {
1907 		kfree_skb(skb);
1908 		return -EINVAL;
1909 	}
1910 
1911 	if (unicast)
1912 		err = netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1913 	else
1914 		err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1915 				     n->nlmsg_flags & NLM_F_ECHO);
1916 
1917 	if (err > 0)
1918 		err = 0;
1919 	return err;
1920 }
1921 
1922 static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1923 			      struct nlmsghdr *n, struct tcf_proto *tp,
1924 			      struct tcf_block *block, struct Qdisc *q,
1925 			      u32 parent, void *fh, bool unicast, bool *last,
1926 			      bool rtnl_held, struct netlink_ext_ack *extack)
1927 {
1928 	struct sk_buff *skb;
1929 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1930 	int err;
1931 
1932 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1933 	if (!skb)
1934 		return -ENOBUFS;
1935 
1936 	if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1937 			  n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER,
1938 			  rtnl_held) <= 0) {
1939 		NL_SET_ERR_MSG(extack, "Failed to build del event notification");
1940 		kfree_skb(skb);
1941 		return -EINVAL;
1942 	}
1943 
1944 	err = tp->ops->delete(tp, fh, last, rtnl_held, extack);
1945 	if (err) {
1946 		kfree_skb(skb);
1947 		return err;
1948 	}
1949 
1950 	if (unicast)
1951 		err = netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1952 	else
1953 		err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1954 				     n->nlmsg_flags & NLM_F_ECHO);
1955 	if (err < 0)
1956 		NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1957 
1958 	if (err > 0)
1959 		err = 0;
1960 	return err;
1961 }
1962 
1963 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
1964 				 struct tcf_block *block, struct Qdisc *q,
1965 				 u32 parent, struct nlmsghdr *n,
1966 				 struct tcf_chain *chain, int event,
1967 				 bool rtnl_held)
1968 {
1969 	struct tcf_proto *tp;
1970 
1971 	for (tp = tcf_get_next_proto(chain, NULL, rtnl_held);
1972 	     tp; tp = tcf_get_next_proto(chain, tp, rtnl_held))
1973 		tfilter_notify(net, oskb, n, tp, block,
1974 			       q, parent, NULL, event, false, rtnl_held);
1975 }
1976 
1977 static void tfilter_put(struct tcf_proto *tp, void *fh)
1978 {
1979 	if (tp->ops->put && fh)
1980 		tp->ops->put(tp, fh);
1981 }
1982 
1983 static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1984 			  struct netlink_ext_ack *extack)
1985 {
1986 	struct net *net = sock_net(skb->sk);
1987 	struct nlattr *tca[TCA_MAX + 1];
1988 	struct tcmsg *t;
1989 	u32 protocol;
1990 	u32 prio;
1991 	bool prio_allocate;
1992 	u32 parent;
1993 	u32 chain_index;
1994 	struct Qdisc *q = NULL;
1995 	struct tcf_chain_info chain_info;
1996 	struct tcf_chain *chain = NULL;
1997 	struct tcf_block *block;
1998 	struct tcf_proto *tp;
1999 	unsigned long cl;
2000 	void *fh;
2001 	int err;
2002 	int tp_created;
2003 	bool rtnl_held = false;
2004 
2005 	if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
2006 		return -EPERM;
2007 
2008 replay:
2009 	tp_created = 0;
2010 
2011 	err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2012 				     rtm_tca_policy, extack);
2013 	if (err < 0)
2014 		return err;
2015 
2016 	t = nlmsg_data(n);
2017 	protocol = TC_H_MIN(t->tcm_info);
2018 	prio = TC_H_MAJ(t->tcm_info);
2019 	prio_allocate = false;
2020 	parent = t->tcm_parent;
2021 	tp = NULL;
2022 	cl = 0;
2023 	block = NULL;
2024 
2025 	if (prio == 0) {
2026 		/* If no priority is provided by the user,
2027 		 * we allocate one.
2028 		 */
2029 		if (n->nlmsg_flags & NLM_F_CREATE) {
2030 			prio = TC_H_MAKE(0x80000000U, 0U);
2031 			prio_allocate = true;
2032 		} else {
2033 			NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2034 			return -ENOENT;
2035 		}
2036 	}
2037 
2038 	/* Find head of filter chain. */
2039 
2040 	err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2041 	if (err)
2042 		return err;
2043 
2044 	/* Take rtnl mutex if rtnl_held was set to true on previous iteration,
2045 	 * block is shared (no qdisc found), qdisc is not unlocked, classifier
2046 	 * type is not specified, classifier is not unlocked.
2047 	 */
2048 	if (rtnl_held ||
2049 	    (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2050 	    !tca[TCA_KIND] || !tcf_proto_is_unlocked(nla_data(tca[TCA_KIND]))) {
2051 		rtnl_held = true;
2052 		rtnl_lock();
2053 	}
2054 
2055 	err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2056 	if (err)
2057 		goto errout;
2058 
2059 	block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2060 				 extack);
2061 	if (IS_ERR(block)) {
2062 		err = PTR_ERR(block);
2063 		goto errout;
2064 	}
2065 
2066 	chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2067 	if (chain_index > TC_ACT_EXT_VAL_MASK) {
2068 		NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2069 		err = -EINVAL;
2070 		goto errout;
2071 	}
2072 	chain = tcf_chain_get(block, chain_index, true);
2073 	if (!chain) {
2074 		NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
2075 		err = -ENOMEM;
2076 		goto errout;
2077 	}
2078 
2079 	mutex_lock(&chain->filter_chain_lock);
2080 	tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2081 			       prio, prio_allocate);
2082 	if (IS_ERR(tp)) {
2083 		NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2084 		err = PTR_ERR(tp);
2085 		goto errout_locked;
2086 	}
2087 
2088 	if (tp == NULL) {
2089 		struct tcf_proto *tp_new = NULL;
2090 
2091 		if (chain->flushing) {
2092 			err = -EAGAIN;
2093 			goto errout_locked;
2094 		}
2095 
2096 		/* Proto-tcf does not exist, create new one */
2097 
2098 		if (tca[TCA_KIND] == NULL || !protocol) {
2099 			NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
2100 			err = -EINVAL;
2101 			goto errout_locked;
2102 		}
2103 
2104 		if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2105 			NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2106 			err = -ENOENT;
2107 			goto errout_locked;
2108 		}
2109 
2110 		if (prio_allocate)
2111 			prio = tcf_auto_prio(tcf_chain_tp_prev(chain,
2112 							       &chain_info));
2113 
2114 		mutex_unlock(&chain->filter_chain_lock);
2115 		tp_new = tcf_proto_create(nla_data(tca[TCA_KIND]),
2116 					  protocol, prio, chain, rtnl_held,
2117 					  extack);
2118 		if (IS_ERR(tp_new)) {
2119 			err = PTR_ERR(tp_new);
2120 			goto errout_tp;
2121 		}
2122 
2123 		tp_created = 1;
2124 		tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio,
2125 						rtnl_held);
2126 		if (IS_ERR(tp)) {
2127 			err = PTR_ERR(tp);
2128 			goto errout_tp;
2129 		}
2130 	} else {
2131 		mutex_unlock(&chain->filter_chain_lock);
2132 	}
2133 
2134 	if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2135 		NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2136 		err = -EINVAL;
2137 		goto errout;
2138 	}
2139 
2140 	fh = tp->ops->get(tp, t->tcm_handle);
2141 
2142 	if (!fh) {
2143 		if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2144 			NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2145 			err = -ENOENT;
2146 			goto errout;
2147 		}
2148 	} else if (n->nlmsg_flags & NLM_F_EXCL) {
2149 		tfilter_put(tp, fh);
2150 		NL_SET_ERR_MSG(extack, "Filter already exists");
2151 		err = -EEXIST;
2152 		goto errout;
2153 	}
2154 
2155 	if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
2156 		NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
2157 		err = -EINVAL;
2158 		goto errout;
2159 	}
2160 
2161 	err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
2162 			      n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
2163 			      rtnl_held, extack);
2164 	if (err == 0) {
2165 		tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2166 			       RTM_NEWTFILTER, false, rtnl_held);
2167 		tfilter_put(tp, fh);
2168 	}
2169 
2170 errout:
2171 	if (err && tp_created)
2172 		tcf_chain_tp_delete_empty(chain, tp, rtnl_held, NULL);
2173 errout_tp:
2174 	if (chain) {
2175 		if (tp && !IS_ERR(tp))
2176 			tcf_proto_put(tp, rtnl_held, NULL);
2177 		if (!tp_created)
2178 			tcf_chain_put(chain);
2179 	}
2180 	tcf_block_release(q, block, rtnl_held);
2181 
2182 	if (rtnl_held)
2183 		rtnl_unlock();
2184 
2185 	if (err == -EAGAIN) {
2186 		/* Take rtnl lock in case EAGAIN is caused by concurrent flush
2187 		 * of target chain.
2188 		 */
2189 		rtnl_held = true;
2190 		/* Replay the request. */
2191 		goto replay;
2192 	}
2193 	return err;
2194 
2195 errout_locked:
2196 	mutex_unlock(&chain->filter_chain_lock);
2197 	goto errout;
2198 }
2199 
2200 static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2201 			  struct netlink_ext_ack *extack)
2202 {
2203 	struct net *net = sock_net(skb->sk);
2204 	struct nlattr *tca[TCA_MAX + 1];
2205 	struct tcmsg *t;
2206 	u32 protocol;
2207 	u32 prio;
2208 	u32 parent;
2209 	u32 chain_index;
2210 	struct Qdisc *q = NULL;
2211 	struct tcf_chain_info chain_info;
2212 	struct tcf_chain *chain = NULL;
2213 	struct tcf_block *block = NULL;
2214 	struct tcf_proto *tp = NULL;
2215 	unsigned long cl = 0;
2216 	void *fh = NULL;
2217 	int err;
2218 	bool rtnl_held = false;
2219 
2220 	if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
2221 		return -EPERM;
2222 
2223 	err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2224 				     rtm_tca_policy, extack);
2225 	if (err < 0)
2226 		return err;
2227 
2228 	t = nlmsg_data(n);
2229 	protocol = TC_H_MIN(t->tcm_info);
2230 	prio = TC_H_MAJ(t->tcm_info);
2231 	parent = t->tcm_parent;
2232 
2233 	if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
2234 		NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
2235 		return -ENOENT;
2236 	}
2237 
2238 	/* Find head of filter chain. */
2239 
2240 	err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2241 	if (err)
2242 		return err;
2243 
2244 	/* Take rtnl mutex if flushing whole chain, block is shared (no qdisc
2245 	 * found), qdisc is not unlocked, classifier type is not specified,
2246 	 * classifier is not unlocked.
2247 	 */
2248 	if (!prio ||
2249 	    (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2250 	    !tca[TCA_KIND] || !tcf_proto_is_unlocked(nla_data(tca[TCA_KIND]))) {
2251 		rtnl_held = true;
2252 		rtnl_lock();
2253 	}
2254 
2255 	err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2256 	if (err)
2257 		goto errout;
2258 
2259 	block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2260 				 extack);
2261 	if (IS_ERR(block)) {
2262 		err = PTR_ERR(block);
2263 		goto errout;
2264 	}
2265 
2266 	chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2267 	if (chain_index > TC_ACT_EXT_VAL_MASK) {
2268 		NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2269 		err = -EINVAL;
2270 		goto errout;
2271 	}
2272 	chain = tcf_chain_get(block, chain_index, false);
2273 	if (!chain) {
2274 		/* User requested flush on non-existent chain. Nothing to do,
2275 		 * so just return success.
2276 		 */
2277 		if (prio == 0) {
2278 			err = 0;
2279 			goto errout;
2280 		}
2281 		NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2282 		err = -ENOENT;
2283 		goto errout;
2284 	}
2285 
2286 	if (prio == 0) {
2287 		tfilter_notify_chain(net, skb, block, q, parent, n,
2288 				     chain, RTM_DELTFILTER, rtnl_held);
2289 		tcf_chain_flush(chain, rtnl_held);
2290 		err = 0;
2291 		goto errout;
2292 	}
2293 
2294 	mutex_lock(&chain->filter_chain_lock);
2295 	tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2296 			       prio, false);
2297 	if (!tp || IS_ERR(tp)) {
2298 		NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2299 		err = tp ? PTR_ERR(tp) : -ENOENT;
2300 		goto errout_locked;
2301 	} else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2302 		NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2303 		err = -EINVAL;
2304 		goto errout_locked;
2305 	} else if (t->tcm_handle == 0) {
2306 		tcf_chain_tp_remove(chain, &chain_info, tp);
2307 		mutex_unlock(&chain->filter_chain_lock);
2308 
2309 		tcf_proto_put(tp, rtnl_held, NULL);
2310 		tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2311 			       RTM_DELTFILTER, false, rtnl_held);
2312 		err = 0;
2313 		goto errout;
2314 	}
2315 	mutex_unlock(&chain->filter_chain_lock);
2316 
2317 	fh = tp->ops->get(tp, t->tcm_handle);
2318 
2319 	if (!fh) {
2320 		NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2321 		err = -ENOENT;
2322 	} else {
2323 		bool last;
2324 
2325 		err = tfilter_del_notify(net, skb, n, tp, block,
2326 					 q, parent, fh, false, &last,
2327 					 rtnl_held, extack);
2328 
2329 		if (err)
2330 			goto errout;
2331 		if (last)
2332 			tcf_chain_tp_delete_empty(chain, tp, rtnl_held, extack);
2333 	}
2334 
2335 errout:
2336 	if (chain) {
2337 		if (tp && !IS_ERR(tp))
2338 			tcf_proto_put(tp, rtnl_held, NULL);
2339 		tcf_chain_put(chain);
2340 	}
2341 	tcf_block_release(q, block, rtnl_held);
2342 
2343 	if (rtnl_held)
2344 		rtnl_unlock();
2345 
2346 	return err;
2347 
2348 errout_locked:
2349 	mutex_unlock(&chain->filter_chain_lock);
2350 	goto errout;
2351 }
2352 
2353 static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2354 			  struct netlink_ext_ack *extack)
2355 {
2356 	struct net *net = sock_net(skb->sk);
2357 	struct nlattr *tca[TCA_MAX + 1];
2358 	struct tcmsg *t;
2359 	u32 protocol;
2360 	u32 prio;
2361 	u32 parent;
2362 	u32 chain_index;
2363 	struct Qdisc *q = NULL;
2364 	struct tcf_chain_info chain_info;
2365 	struct tcf_chain *chain = NULL;
2366 	struct tcf_block *block = NULL;
2367 	struct tcf_proto *tp = NULL;
2368 	unsigned long cl = 0;
2369 	void *fh = NULL;
2370 	int err;
2371 	bool rtnl_held = false;
2372 
2373 	err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2374 				     rtm_tca_policy, extack);
2375 	if (err < 0)
2376 		return err;
2377 
2378 	t = nlmsg_data(n);
2379 	protocol = TC_H_MIN(t->tcm_info);
2380 	prio = TC_H_MAJ(t->tcm_info);
2381 	parent = t->tcm_parent;
2382 
2383 	if (prio == 0) {
2384 		NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2385 		return -ENOENT;
2386 	}
2387 
2388 	/* Find head of filter chain. */
2389 
2390 	err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2391 	if (err)
2392 		return err;
2393 
2394 	/* Take rtnl mutex if block is shared (no qdisc found), qdisc is not
2395 	 * unlocked, classifier type is not specified, classifier is not
2396 	 * unlocked.
2397 	 */
2398 	if ((q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2399 	    !tca[TCA_KIND] || !tcf_proto_is_unlocked(nla_data(tca[TCA_KIND]))) {
2400 		rtnl_held = true;
2401 		rtnl_lock();
2402 	}
2403 
2404 	err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2405 	if (err)
2406 		goto errout;
2407 
2408 	block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2409 				 extack);
2410 	if (IS_ERR(block)) {
2411 		err = PTR_ERR(block);
2412 		goto errout;
2413 	}
2414 
2415 	chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2416 	if (chain_index > TC_ACT_EXT_VAL_MASK) {
2417 		NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2418 		err = -EINVAL;
2419 		goto errout;
2420 	}
2421 	chain = tcf_chain_get(block, chain_index, false);
2422 	if (!chain) {
2423 		NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2424 		err = -EINVAL;
2425 		goto errout;
2426 	}
2427 
2428 	mutex_lock(&chain->filter_chain_lock);
2429 	tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2430 			       prio, false);
2431 	mutex_unlock(&chain->filter_chain_lock);
2432 	if (!tp || IS_ERR(tp)) {
2433 		NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2434 		err = tp ? PTR_ERR(tp) : -ENOENT;
2435 		goto errout;
2436 	} else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2437 		NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2438 		err = -EINVAL;
2439 		goto errout;
2440 	}
2441 
2442 	fh = tp->ops->get(tp, t->tcm_handle);
2443 
2444 	if (!fh) {
2445 		NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2446 		err = -ENOENT;
2447 	} else {
2448 		err = tfilter_notify(net, skb, n, tp, block, q, parent,
2449 				     fh, RTM_NEWTFILTER, true, rtnl_held);
2450 		if (err < 0)
2451 			NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
2452 	}
2453 
2454 	tfilter_put(tp, fh);
2455 errout:
2456 	if (chain) {
2457 		if (tp && !IS_ERR(tp))
2458 			tcf_proto_put(tp, rtnl_held, NULL);
2459 		tcf_chain_put(chain);
2460 	}
2461 	tcf_block_release(q, block, rtnl_held);
2462 
2463 	if (rtnl_held)
2464 		rtnl_unlock();
2465 
2466 	return err;
2467 }
2468 
2469 struct tcf_dump_args {
2470 	struct tcf_walker w;
2471 	struct sk_buff *skb;
2472 	struct netlink_callback *cb;
2473 	struct tcf_block *block;
2474 	struct Qdisc *q;
2475 	u32 parent;
2476 };
2477 
2478 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
2479 {
2480 	struct tcf_dump_args *a = (void *)arg;
2481 	struct net *net = sock_net(a->skb->sk);
2482 
2483 	return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
2484 			     n, NETLINK_CB(a->cb->skb).portid,
2485 			     a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
2486 			     RTM_NEWTFILTER, true);
2487 }
2488 
2489 static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
2490 			   struct sk_buff *skb, struct netlink_callback *cb,
2491 			   long index_start, long *p_index)
2492 {
2493 	struct net *net = sock_net(skb->sk);
2494 	struct tcf_block *block = chain->block;
2495 	struct tcmsg *tcm = nlmsg_data(cb->nlh);
2496 	struct tcf_proto *tp, *tp_prev;
2497 	struct tcf_dump_args arg;
2498 
2499 	for (tp = __tcf_get_next_proto(chain, NULL);
2500 	     tp;
2501 	     tp_prev = tp,
2502 		     tp = __tcf_get_next_proto(chain, tp),
2503 		     tcf_proto_put(tp_prev, true, NULL),
2504 		     (*p_index)++) {
2505 		if (*p_index < index_start)
2506 			continue;
2507 		if (TC_H_MAJ(tcm->tcm_info) &&
2508 		    TC_H_MAJ(tcm->tcm_info) != tp->prio)
2509 			continue;
2510 		if (TC_H_MIN(tcm->tcm_info) &&
2511 		    TC_H_MIN(tcm->tcm_info) != tp->protocol)
2512 			continue;
2513 		if (*p_index > index_start)
2514 			memset(&cb->args[1], 0,
2515 			       sizeof(cb->args) - sizeof(cb->args[0]));
2516 		if (cb->args[1] == 0) {
2517 			if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
2518 					  NETLINK_CB(cb->skb).portid,
2519 					  cb->nlh->nlmsg_seq, NLM_F_MULTI,
2520 					  RTM_NEWTFILTER, true) <= 0)
2521 				goto errout;
2522 			cb->args[1] = 1;
2523 		}
2524 		if (!tp->ops->walk)
2525 			continue;
2526 		arg.w.fn = tcf_node_dump;
2527 		arg.skb = skb;
2528 		arg.cb = cb;
2529 		arg.block = block;
2530 		arg.q = q;
2531 		arg.parent = parent;
2532 		arg.w.stop = 0;
2533 		arg.w.skip = cb->args[1] - 1;
2534 		arg.w.count = 0;
2535 		arg.w.cookie = cb->args[2];
2536 		tp->ops->walk(tp, &arg.w, true);
2537 		cb->args[2] = arg.w.cookie;
2538 		cb->args[1] = arg.w.count + 1;
2539 		if (arg.w.stop)
2540 			goto errout;
2541 	}
2542 	return true;
2543 
2544 errout:
2545 	tcf_proto_put(tp, true, NULL);
2546 	return false;
2547 }
2548 
2549 /* called with RTNL */
2550 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
2551 {
2552 	struct tcf_chain *chain, *chain_prev;
2553 	struct net *net = sock_net(skb->sk);
2554 	struct nlattr *tca[TCA_MAX + 1];
2555 	struct Qdisc *q = NULL;
2556 	struct tcf_block *block;
2557 	struct tcmsg *tcm = nlmsg_data(cb->nlh);
2558 	long index_start;
2559 	long index;
2560 	u32 parent;
2561 	int err;
2562 
2563 	if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2564 		return skb->len;
2565 
2566 	err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
2567 				     NULL, cb->extack);
2568 	if (err)
2569 		return err;
2570 
2571 	if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2572 		block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2573 		if (!block)
2574 			goto out;
2575 		/* If we work with block index, q is NULL and parent value
2576 		 * will never be used in the following code. The check
2577 		 * in tcf_fill_node prevents it. However, compiler does not
2578 		 * see that far, so set parent to zero to silence the warning
2579 		 * about parent being uninitialized.
2580 		 */
2581 		parent = 0;
2582 	} else {
2583 		const struct Qdisc_class_ops *cops;
2584 		struct net_device *dev;
2585 		unsigned long cl = 0;
2586 
2587 		dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2588 		if (!dev)
2589 			return skb->len;
2590 
2591 		parent = tcm->tcm_parent;
2592 		if (!parent) {
2593 			q = dev->qdisc;
2594 			parent = q->handle;
2595 		} else {
2596 			q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2597 		}
2598 		if (!q)
2599 			goto out;
2600 		cops = q->ops->cl_ops;
2601 		if (!cops)
2602 			goto out;
2603 		if (!cops->tcf_block)
2604 			goto out;
2605 		if (TC_H_MIN(tcm->tcm_parent)) {
2606 			cl = cops->find(q, tcm->tcm_parent);
2607 			if (cl == 0)
2608 				goto out;
2609 		}
2610 		block = cops->tcf_block(q, cl, NULL);
2611 		if (!block)
2612 			goto out;
2613 		if (tcf_block_shared(block))
2614 			q = NULL;
2615 	}
2616 
2617 	index_start = cb->args[0];
2618 	index = 0;
2619 
2620 	for (chain = __tcf_get_next_chain(block, NULL);
2621 	     chain;
2622 	     chain_prev = chain,
2623 		     chain = __tcf_get_next_chain(block, chain),
2624 		     tcf_chain_put(chain_prev)) {
2625 		if (tca[TCA_CHAIN] &&
2626 		    nla_get_u32(tca[TCA_CHAIN]) != chain->index)
2627 			continue;
2628 		if (!tcf_chain_dump(chain, q, parent, skb, cb,
2629 				    index_start, &index)) {
2630 			tcf_chain_put(chain);
2631 			err = -EMSGSIZE;
2632 			break;
2633 		}
2634 	}
2635 
2636 	if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
2637 		tcf_block_refcnt_put(block, true);
2638 	cb->args[0] = index;
2639 
2640 out:
2641 	/* If we did no progress, the error (EMSGSIZE) is real */
2642 	if (skb->len == 0 && err)
2643 		return err;
2644 	return skb->len;
2645 }
2646 
2647 static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops,
2648 			      void *tmplt_priv, u32 chain_index,
2649 			      struct net *net, struct sk_buff *skb,
2650 			      struct tcf_block *block,
2651 			      u32 portid, u32 seq, u16 flags, int event)
2652 {
2653 	unsigned char *b = skb_tail_pointer(skb);
2654 	const struct tcf_proto_ops *ops;
2655 	struct nlmsghdr *nlh;
2656 	struct tcmsg *tcm;
2657 	void *priv;
2658 
2659 	ops = tmplt_ops;
2660 	priv = tmplt_priv;
2661 
2662 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
2663 	if (!nlh)
2664 		goto out_nlmsg_trim;
2665 	tcm = nlmsg_data(nlh);
2666 	tcm->tcm_family = AF_UNSPEC;
2667 	tcm->tcm__pad1 = 0;
2668 	tcm->tcm__pad2 = 0;
2669 	tcm->tcm_handle = 0;
2670 	if (block->q) {
2671 		tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
2672 		tcm->tcm_parent = block->q->handle;
2673 	} else {
2674 		tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
2675 		tcm->tcm_block_index = block->index;
2676 	}
2677 
2678 	if (nla_put_u32(skb, TCA_CHAIN, chain_index))
2679 		goto nla_put_failure;
2680 
2681 	if (ops) {
2682 		if (nla_put_string(skb, TCA_KIND, ops->kind))
2683 			goto nla_put_failure;
2684 		if (ops->tmplt_dump(skb, net, priv) < 0)
2685 			goto nla_put_failure;
2686 	}
2687 
2688 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2689 	return skb->len;
2690 
2691 out_nlmsg_trim:
2692 nla_put_failure:
2693 	nlmsg_trim(skb, b);
2694 	return -EMSGSIZE;
2695 }
2696 
2697 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
2698 			   u32 seq, u16 flags, int event, bool unicast)
2699 {
2700 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2701 	struct tcf_block *block = chain->block;
2702 	struct net *net = block->net;
2703 	struct sk_buff *skb;
2704 	int err = 0;
2705 
2706 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2707 	if (!skb)
2708 		return -ENOBUFS;
2709 
2710 	if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
2711 			       chain->index, net, skb, block, portid,
2712 			       seq, flags, event) <= 0) {
2713 		kfree_skb(skb);
2714 		return -EINVAL;
2715 	}
2716 
2717 	if (unicast)
2718 		err = netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
2719 	else
2720 		err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
2721 				     flags & NLM_F_ECHO);
2722 
2723 	if (err > 0)
2724 		err = 0;
2725 	return err;
2726 }
2727 
2728 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
2729 				  void *tmplt_priv, u32 chain_index,
2730 				  struct tcf_block *block, struct sk_buff *oskb,
2731 				  u32 seq, u16 flags, bool unicast)
2732 {
2733 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2734 	struct net *net = block->net;
2735 	struct sk_buff *skb;
2736 
2737 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2738 	if (!skb)
2739 		return -ENOBUFS;
2740 
2741 	if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb,
2742 			       block, portid, seq, flags, RTM_DELCHAIN) <= 0) {
2743 		kfree_skb(skb);
2744 		return -EINVAL;
2745 	}
2746 
2747 	if (unicast)
2748 		return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
2749 
2750 	return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
2751 }
2752 
2753 static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
2754 			      struct nlattr **tca,
2755 			      struct netlink_ext_ack *extack)
2756 {
2757 	const struct tcf_proto_ops *ops;
2758 	void *tmplt_priv;
2759 
2760 	/* If kind is not set, user did not specify template. */
2761 	if (!tca[TCA_KIND])
2762 		return 0;
2763 
2764 	ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), true, extack);
2765 	if (IS_ERR(ops))
2766 		return PTR_ERR(ops);
2767 	if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
2768 		NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
2769 		return -EOPNOTSUPP;
2770 	}
2771 
2772 	tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
2773 	if (IS_ERR(tmplt_priv)) {
2774 		module_put(ops->owner);
2775 		return PTR_ERR(tmplt_priv);
2776 	}
2777 	chain->tmplt_ops = ops;
2778 	chain->tmplt_priv = tmplt_priv;
2779 	return 0;
2780 }
2781 
2782 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
2783 			       void *tmplt_priv)
2784 {
2785 	/* If template ops are set, no work to do for us. */
2786 	if (!tmplt_ops)
2787 		return;
2788 
2789 	tmplt_ops->tmplt_destroy(tmplt_priv);
2790 	module_put(tmplt_ops->owner);
2791 }
2792 
2793 /* Add/delete/get a chain */
2794 
2795 static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
2796 			struct netlink_ext_ack *extack)
2797 {
2798 	struct net *net = sock_net(skb->sk);
2799 	struct nlattr *tca[TCA_MAX + 1];
2800 	struct tcmsg *t;
2801 	u32 parent;
2802 	u32 chain_index;
2803 	struct Qdisc *q = NULL;
2804 	struct tcf_chain *chain = NULL;
2805 	struct tcf_block *block;
2806 	unsigned long cl;
2807 	int err;
2808 
2809 	if (n->nlmsg_type != RTM_GETCHAIN &&
2810 	    !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
2811 		return -EPERM;
2812 
2813 replay:
2814 	err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2815 				     rtm_tca_policy, extack);
2816 	if (err < 0)
2817 		return err;
2818 
2819 	t = nlmsg_data(n);
2820 	parent = t->tcm_parent;
2821 	cl = 0;
2822 
2823 	block = tcf_block_find(net, &q, &parent, &cl,
2824 			       t->tcm_ifindex, t->tcm_block_index, extack);
2825 	if (IS_ERR(block))
2826 		return PTR_ERR(block);
2827 
2828 	chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2829 	if (chain_index > TC_ACT_EXT_VAL_MASK) {
2830 		NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2831 		err = -EINVAL;
2832 		goto errout_block;
2833 	}
2834 
2835 	mutex_lock(&block->lock);
2836 	chain = tcf_chain_lookup(block, chain_index);
2837 	if (n->nlmsg_type == RTM_NEWCHAIN) {
2838 		if (chain) {
2839 			if (tcf_chain_held_by_acts_only(chain)) {
2840 				/* The chain exists only because there is
2841 				 * some action referencing it.
2842 				 */
2843 				tcf_chain_hold(chain);
2844 			} else {
2845 				NL_SET_ERR_MSG(extack, "Filter chain already exists");
2846 				err = -EEXIST;
2847 				goto errout_block_locked;
2848 			}
2849 		} else {
2850 			if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2851 				NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
2852 				err = -ENOENT;
2853 				goto errout_block_locked;
2854 			}
2855 			chain = tcf_chain_create(block, chain_index);
2856 			if (!chain) {
2857 				NL_SET_ERR_MSG(extack, "Failed to create filter chain");
2858 				err = -ENOMEM;
2859 				goto errout_block_locked;
2860 			}
2861 		}
2862 	} else {
2863 		if (!chain || tcf_chain_held_by_acts_only(chain)) {
2864 			NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2865 			err = -EINVAL;
2866 			goto errout_block_locked;
2867 		}
2868 		tcf_chain_hold(chain);
2869 	}
2870 
2871 	if (n->nlmsg_type == RTM_NEWCHAIN) {
2872 		/* Modifying chain requires holding parent block lock. In case
2873 		 * the chain was successfully added, take a reference to the
2874 		 * chain. This ensures that an empty chain does not disappear at
2875 		 * the end of this function.
2876 		 */
2877 		tcf_chain_hold(chain);
2878 		chain->explicitly_created = true;
2879 	}
2880 	mutex_unlock(&block->lock);
2881 
2882 	switch (n->nlmsg_type) {
2883 	case RTM_NEWCHAIN:
2884 		err = tc_chain_tmplt_add(chain, net, tca, extack);
2885 		if (err) {
2886 			tcf_chain_put_explicitly_created(chain);
2887 			goto errout;
2888 		}
2889 
2890 		tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
2891 				RTM_NEWCHAIN, false);
2892 		break;
2893 	case RTM_DELCHAIN:
2894 		tfilter_notify_chain(net, skb, block, q, parent, n,
2895 				     chain, RTM_DELTFILTER, true);
2896 		/* Flush the chain first as the user requested chain removal. */
2897 		tcf_chain_flush(chain, true);
2898 		/* In case the chain was successfully deleted, put a reference
2899 		 * to the chain previously taken during addition.
2900 		 */
2901 		tcf_chain_put_explicitly_created(chain);
2902 		break;
2903 	case RTM_GETCHAIN:
2904 		err = tc_chain_notify(chain, skb, n->nlmsg_seq,
2905 				      n->nlmsg_seq, n->nlmsg_type, true);
2906 		if (err < 0)
2907 			NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
2908 		break;
2909 	default:
2910 		err = -EOPNOTSUPP;
2911 		NL_SET_ERR_MSG(extack, "Unsupported message type");
2912 		goto errout;
2913 	}
2914 
2915 errout:
2916 	tcf_chain_put(chain);
2917 errout_block:
2918 	tcf_block_release(q, block, true);
2919 	if (err == -EAGAIN)
2920 		/* Replay the request. */
2921 		goto replay;
2922 	return err;
2923 
2924 errout_block_locked:
2925 	mutex_unlock(&block->lock);
2926 	goto errout_block;
2927 }
2928 
2929 /* called with RTNL */
2930 static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
2931 {
2932 	struct net *net = sock_net(skb->sk);
2933 	struct nlattr *tca[TCA_MAX + 1];
2934 	struct Qdisc *q = NULL;
2935 	struct tcf_block *block;
2936 	struct tcmsg *tcm = nlmsg_data(cb->nlh);
2937 	struct tcf_chain *chain;
2938 	long index_start;
2939 	long index;
2940 	u32 parent;
2941 	int err;
2942 
2943 	if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2944 		return skb->len;
2945 
2946 	err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
2947 				     rtm_tca_policy, cb->extack);
2948 	if (err)
2949 		return err;
2950 
2951 	if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2952 		block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2953 		if (!block)
2954 			goto out;
2955 		/* If we work with block index, q is NULL and parent value
2956 		 * will never be used in the following code. The check
2957 		 * in tcf_fill_node prevents it. However, compiler does not
2958 		 * see that far, so set parent to zero to silence the warning
2959 		 * about parent being uninitialized.
2960 		 */
2961 		parent = 0;
2962 	} else {
2963 		const struct Qdisc_class_ops *cops;
2964 		struct net_device *dev;
2965 		unsigned long cl = 0;
2966 
2967 		dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2968 		if (!dev)
2969 			return skb->len;
2970 
2971 		parent = tcm->tcm_parent;
2972 		if (!parent) {
2973 			q = dev->qdisc;
2974 			parent = q->handle;
2975 		} else {
2976 			q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2977 		}
2978 		if (!q)
2979 			goto out;
2980 		cops = q->ops->cl_ops;
2981 		if (!cops)
2982 			goto out;
2983 		if (!cops->tcf_block)
2984 			goto out;
2985 		if (TC_H_MIN(tcm->tcm_parent)) {
2986 			cl = cops->find(q, tcm->tcm_parent);
2987 			if (cl == 0)
2988 				goto out;
2989 		}
2990 		block = cops->tcf_block(q, cl, NULL);
2991 		if (!block)
2992 			goto out;
2993 		if (tcf_block_shared(block))
2994 			q = NULL;
2995 	}
2996 
2997 	index_start = cb->args[0];
2998 	index = 0;
2999 
3000 	mutex_lock(&block->lock);
3001 	list_for_each_entry(chain, &block->chain_list, list) {
3002 		if ((tca[TCA_CHAIN] &&
3003 		     nla_get_u32(tca[TCA_CHAIN]) != chain->index))
3004 			continue;
3005 		if (index < index_start) {
3006 			index++;
3007 			continue;
3008 		}
3009 		if (tcf_chain_held_by_acts_only(chain))
3010 			continue;
3011 		err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
3012 					 chain->index, net, skb, block,
3013 					 NETLINK_CB(cb->skb).portid,
3014 					 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3015 					 RTM_NEWCHAIN);
3016 		if (err <= 0)
3017 			break;
3018 		index++;
3019 	}
3020 	mutex_unlock(&block->lock);
3021 
3022 	if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
3023 		tcf_block_refcnt_put(block, true);
3024 	cb->args[0] = index;
3025 
3026 out:
3027 	/* If we did no progress, the error (EMSGSIZE) is real */
3028 	if (skb->len == 0 && err)
3029 		return err;
3030 	return skb->len;
3031 }
3032 
3033 void tcf_exts_destroy(struct tcf_exts *exts)
3034 {
3035 #ifdef CONFIG_NET_CLS_ACT
3036 	tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
3037 	kfree(exts->actions);
3038 	exts->nr_actions = 0;
3039 #endif
3040 }
3041 EXPORT_SYMBOL(tcf_exts_destroy);
3042 
3043 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
3044 		      struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
3045 		      bool rtnl_held, struct netlink_ext_ack *extack)
3046 {
3047 #ifdef CONFIG_NET_CLS_ACT
3048 	{
3049 		struct tc_action *act;
3050 		size_t attr_size = 0;
3051 
3052 		if (exts->police && tb[exts->police]) {
3053 			act = tcf_action_init_1(net, tp, tb[exts->police],
3054 						rate_tlv, "police", ovr,
3055 						TCA_ACT_BIND, rtnl_held,
3056 						extack);
3057 			if (IS_ERR(act))
3058 				return PTR_ERR(act);
3059 
3060 			act->type = exts->type = TCA_OLD_COMPAT;
3061 			exts->actions[0] = act;
3062 			exts->nr_actions = 1;
3063 		} else if (exts->action && tb[exts->action]) {
3064 			int err;
3065 
3066 			err = tcf_action_init(net, tp, tb[exts->action],
3067 					      rate_tlv, NULL, ovr, TCA_ACT_BIND,
3068 					      exts->actions, &attr_size,
3069 					      rtnl_held, extack);
3070 			if (err < 0)
3071 				return err;
3072 			exts->nr_actions = err;
3073 		}
3074 	}
3075 #else
3076 	if ((exts->action && tb[exts->action]) ||
3077 	    (exts->police && tb[exts->police])) {
3078 		NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
3079 		return -EOPNOTSUPP;
3080 	}
3081 #endif
3082 
3083 	return 0;
3084 }
3085 EXPORT_SYMBOL(tcf_exts_validate);
3086 
3087 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
3088 {
3089 #ifdef CONFIG_NET_CLS_ACT
3090 	struct tcf_exts old = *dst;
3091 
3092 	*dst = *src;
3093 	tcf_exts_destroy(&old);
3094 #endif
3095 }
3096 EXPORT_SYMBOL(tcf_exts_change);
3097 
3098 #ifdef CONFIG_NET_CLS_ACT
3099 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
3100 {
3101 	if (exts->nr_actions == 0)
3102 		return NULL;
3103 	else
3104 		return exts->actions[0];
3105 }
3106 #endif
3107 
3108 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
3109 {
3110 #ifdef CONFIG_NET_CLS_ACT
3111 	struct nlattr *nest;
3112 
3113 	if (exts->action && tcf_exts_has_actions(exts)) {
3114 		/*
3115 		 * again for backward compatible mode - we want
3116 		 * to work with both old and new modes of entering
3117 		 * tc data even if iproute2  was newer - jhs
3118 		 */
3119 		if (exts->type != TCA_OLD_COMPAT) {
3120 			nest = nla_nest_start_noflag(skb, exts->action);
3121 			if (nest == NULL)
3122 				goto nla_put_failure;
3123 
3124 			if (tcf_action_dump(skb, exts->actions, 0, 0) < 0)
3125 				goto nla_put_failure;
3126 			nla_nest_end(skb, nest);
3127 		} else if (exts->police) {
3128 			struct tc_action *act = tcf_exts_first_act(exts);
3129 			nest = nla_nest_start_noflag(skb, exts->police);
3130 			if (nest == NULL || !act)
3131 				goto nla_put_failure;
3132 			if (tcf_action_dump_old(skb, act, 0, 0) < 0)
3133 				goto nla_put_failure;
3134 			nla_nest_end(skb, nest);
3135 		}
3136 	}
3137 	return 0;
3138 
3139 nla_put_failure:
3140 	nla_nest_cancel(skb, nest);
3141 	return -1;
3142 #else
3143 	return 0;
3144 #endif
3145 }
3146 EXPORT_SYMBOL(tcf_exts_dump);
3147 
3148 
3149 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
3150 {
3151 #ifdef CONFIG_NET_CLS_ACT
3152 	struct tc_action *a = tcf_exts_first_act(exts);
3153 	if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
3154 		return -1;
3155 #endif
3156 	return 0;
3157 }
3158 EXPORT_SYMBOL(tcf_exts_dump_stats);
3159 
3160 int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3161 		     void *type_data, bool err_stop)
3162 {
3163 	struct tcf_block_cb *block_cb;
3164 	int ok_count = 0;
3165 	int err;
3166 
3167 	/* Make sure all netdevs sharing this block are offload-capable. */
3168 	if (block->nooffloaddevcnt && err_stop)
3169 		return -EOPNOTSUPP;
3170 
3171 	list_for_each_entry(block_cb, &block->cb_list, list) {
3172 		err = block_cb->cb(type, type_data, block_cb->cb_priv);
3173 		if (err) {
3174 			if (err_stop)
3175 				return err;
3176 		} else {
3177 			ok_count++;
3178 		}
3179 	}
3180 	return ok_count;
3181 }
3182 EXPORT_SYMBOL(tc_setup_cb_call);
3183 
3184 int tc_setup_flow_action(struct flow_action *flow_action,
3185 			 const struct tcf_exts *exts)
3186 {
3187 	const struct tc_action *act;
3188 	int i, j, k;
3189 
3190 	if (!exts)
3191 		return 0;
3192 
3193 	j = 0;
3194 	tcf_exts_for_each_action(i, act, exts) {
3195 		struct flow_action_entry *entry;
3196 
3197 		entry = &flow_action->entries[j];
3198 		if (is_tcf_gact_ok(act)) {
3199 			entry->id = FLOW_ACTION_ACCEPT;
3200 		} else if (is_tcf_gact_shot(act)) {
3201 			entry->id = FLOW_ACTION_DROP;
3202 		} else if (is_tcf_gact_trap(act)) {
3203 			entry->id = FLOW_ACTION_TRAP;
3204 		} else if (is_tcf_gact_goto_chain(act)) {
3205 			entry->id = FLOW_ACTION_GOTO;
3206 			entry->chain_index = tcf_gact_goto_chain_index(act);
3207 		} else if (is_tcf_mirred_egress_redirect(act)) {
3208 			entry->id = FLOW_ACTION_REDIRECT;
3209 			entry->dev = tcf_mirred_dev(act);
3210 		} else if (is_tcf_mirred_egress_mirror(act)) {
3211 			entry->id = FLOW_ACTION_MIRRED;
3212 			entry->dev = tcf_mirred_dev(act);
3213 		} else if (is_tcf_vlan(act)) {
3214 			switch (tcf_vlan_action(act)) {
3215 			case TCA_VLAN_ACT_PUSH:
3216 				entry->id = FLOW_ACTION_VLAN_PUSH;
3217 				entry->vlan.vid = tcf_vlan_push_vid(act);
3218 				entry->vlan.proto = tcf_vlan_push_proto(act);
3219 				entry->vlan.prio = tcf_vlan_push_prio(act);
3220 				break;
3221 			case TCA_VLAN_ACT_POP:
3222 				entry->id = FLOW_ACTION_VLAN_POP;
3223 				break;
3224 			case TCA_VLAN_ACT_MODIFY:
3225 				entry->id = FLOW_ACTION_VLAN_MANGLE;
3226 				entry->vlan.vid = tcf_vlan_push_vid(act);
3227 				entry->vlan.proto = tcf_vlan_push_proto(act);
3228 				entry->vlan.prio = tcf_vlan_push_prio(act);
3229 				break;
3230 			default:
3231 				goto err_out;
3232 			}
3233 		} else if (is_tcf_tunnel_set(act)) {
3234 			entry->id = FLOW_ACTION_TUNNEL_ENCAP;
3235 			entry->tunnel = tcf_tunnel_info(act);
3236 		} else if (is_tcf_tunnel_release(act)) {
3237 			entry->id = FLOW_ACTION_TUNNEL_DECAP;
3238 		} else if (is_tcf_pedit(act)) {
3239 			for (k = 0; k < tcf_pedit_nkeys(act); k++) {
3240 				switch (tcf_pedit_cmd(act, k)) {
3241 				case TCA_PEDIT_KEY_EX_CMD_SET:
3242 					entry->id = FLOW_ACTION_MANGLE;
3243 					break;
3244 				case TCA_PEDIT_KEY_EX_CMD_ADD:
3245 					entry->id = FLOW_ACTION_ADD;
3246 					break;
3247 				default:
3248 					goto err_out;
3249 				}
3250 				entry->mangle.htype = tcf_pedit_htype(act, k);
3251 				entry->mangle.mask = tcf_pedit_mask(act, k);
3252 				entry->mangle.val = tcf_pedit_val(act, k);
3253 				entry->mangle.offset = tcf_pedit_offset(act, k);
3254 				entry = &flow_action->entries[++j];
3255 			}
3256 		} else if (is_tcf_csum(act)) {
3257 			entry->id = FLOW_ACTION_CSUM;
3258 			entry->csum_flags = tcf_csum_update_flags(act);
3259 		} else if (is_tcf_skbedit_mark(act)) {
3260 			entry->id = FLOW_ACTION_MARK;
3261 			entry->mark = tcf_skbedit_mark(act);
3262 		} else if (is_tcf_sample(act)) {
3263 			entry->id = FLOW_ACTION_SAMPLE;
3264 			entry->sample.psample_group =
3265 				tcf_sample_psample_group(act);
3266 			entry->sample.trunc_size = tcf_sample_trunc_size(act);
3267 			entry->sample.truncate = tcf_sample_truncate(act);
3268 			entry->sample.rate = tcf_sample_rate(act);
3269 		} else if (is_tcf_police(act)) {
3270 			entry->id = FLOW_ACTION_POLICE;
3271 			entry->police.burst = tcf_police_tcfp_burst(act);
3272 			entry->police.rate_bytes_ps =
3273 				tcf_police_rate_bytes_ps(act);
3274 		} else {
3275 			goto err_out;
3276 		}
3277 
3278 		if (!is_tcf_pedit(act))
3279 			j++;
3280 	}
3281 	return 0;
3282 err_out:
3283 	return -EOPNOTSUPP;
3284 }
3285 EXPORT_SYMBOL(tc_setup_flow_action);
3286 
3287 unsigned int tcf_exts_num_actions(struct tcf_exts *exts)
3288 {
3289 	unsigned int num_acts = 0;
3290 	struct tc_action *act;
3291 	int i;
3292 
3293 	tcf_exts_for_each_action(i, act, exts) {
3294 		if (is_tcf_pedit(act))
3295 			num_acts += tcf_pedit_nkeys(act);
3296 		else
3297 			num_acts++;
3298 	}
3299 	return num_acts;
3300 }
3301 EXPORT_SYMBOL(tcf_exts_num_actions);
3302 
3303 static __net_init int tcf_net_init(struct net *net)
3304 {
3305 	struct tcf_net *tn = net_generic(net, tcf_net_id);
3306 
3307 	spin_lock_init(&tn->idr_lock);
3308 	idr_init(&tn->idr);
3309 	return 0;
3310 }
3311 
3312 static void __net_exit tcf_net_exit(struct net *net)
3313 {
3314 	struct tcf_net *tn = net_generic(net, tcf_net_id);
3315 
3316 	idr_destroy(&tn->idr);
3317 }
3318 
3319 static struct pernet_operations tcf_net_ops = {
3320 	.init = tcf_net_init,
3321 	.exit = tcf_net_exit,
3322 	.id   = &tcf_net_id,
3323 	.size = sizeof(struct tcf_net),
3324 };
3325 
3326 static int __init tc_filter_init(void)
3327 {
3328 	int err;
3329 
3330 	tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
3331 	if (!tc_filter_wq)
3332 		return -ENOMEM;
3333 
3334 	err = register_pernet_subsys(&tcf_net_ops);
3335 	if (err)
3336 		goto err_register_pernet_subsys;
3337 
3338 	err = rhashtable_init(&indr_setup_block_ht,
3339 			      &tc_indr_setup_block_ht_params);
3340 	if (err)
3341 		goto err_rhash_setup_block_ht;
3342 
3343 	rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL,
3344 		      RTNL_FLAG_DOIT_UNLOCKED);
3345 	rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL,
3346 		      RTNL_FLAG_DOIT_UNLOCKED);
3347 	rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
3348 		      tc_dump_tfilter, RTNL_FLAG_DOIT_UNLOCKED);
3349 	rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
3350 	rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
3351 	rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
3352 		      tc_dump_chain, 0);
3353 
3354 	return 0;
3355 
3356 err_rhash_setup_block_ht:
3357 	unregister_pernet_subsys(&tcf_net_ops);
3358 err_register_pernet_subsys:
3359 	destroy_workqueue(tc_filter_wq);
3360 	return err;
3361 }
3362 
3363 subsys_initcall(tc_filter_init);
3364