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