xref: /linux/net/sched/cls_api.c (revision a5d9265e017f081f0dc133c0e2f45103d027b874)
1 /*
2  * net/sched/cls_api.c	Packet classifier API.
3  *
4  *		This program is free software; you can redistribute it and/or
5  *		modify it under the terms of the GNU General Public License
6  *		as published by the Free Software Foundation; either version
7  *		2 of the License, or (at your option) any later version.
8  *
9  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  *
11  * Changes:
12  *
13  * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
14  *
15  */
16 
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/err.h>
23 #include <linux/skbuff.h>
24 #include <linux/init.h>
25 #include <linux/kmod.h>
26 #include <linux/slab.h>
27 #include <linux/idr.h>
28 #include <linux/rhashtable.h>
29 #include <net/net_namespace.h>
30 #include <net/sock.h>
31 #include <net/netlink.h>
32 #include <net/pkt_sched.h>
33 #include <net/pkt_cls.h>
34 #include <net/tc_act/tc_pedit.h>
35 #include <net/tc_act/tc_mirred.h>
36 #include <net/tc_act/tc_vlan.h>
37 #include <net/tc_act/tc_tunnel_key.h>
38 #include <net/tc_act/tc_csum.h>
39 #include <net/tc_act/tc_gact.h>
40 #include <net/tc_act/tc_skbedit.h>
41 
42 extern const struct nla_policy rtm_tca_policy[TCA_MAX + 1];
43 
44 /* The list of all installed classifier types */
45 static LIST_HEAD(tcf_proto_base);
46 
47 /* Protects list of registered TC modules. It is pure SMP lock. */
48 static DEFINE_RWLOCK(cls_mod_lock);
49 
50 /* Find classifier type by string name */
51 
52 static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind)
53 {
54 	const struct tcf_proto_ops *t, *res = NULL;
55 
56 	if (kind) {
57 		read_lock(&cls_mod_lock);
58 		list_for_each_entry(t, &tcf_proto_base, head) {
59 			if (strcmp(kind, t->kind) == 0) {
60 				if (try_module_get(t->owner))
61 					res = t;
62 				break;
63 			}
64 		}
65 		read_unlock(&cls_mod_lock);
66 	}
67 	return res;
68 }
69 
70 static const struct tcf_proto_ops *
71 tcf_proto_lookup_ops(const char *kind, bool rtnl_held,
72 		     struct netlink_ext_ack *extack)
73 {
74 	const struct tcf_proto_ops *ops;
75 
76 	ops = __tcf_proto_lookup_ops(kind);
77 	if (ops)
78 		return ops;
79 #ifdef CONFIG_MODULES
80 	if (rtnl_held)
81 		rtnl_unlock();
82 	request_module("cls_%s", kind);
83 	if (rtnl_held)
84 		rtnl_lock();
85 	ops = __tcf_proto_lookup_ops(kind);
86 	/* We dropped the RTNL semaphore in order to perform
87 	 * the module load. So, even if we succeeded in loading
88 	 * the module we have to replay the request. We indicate
89 	 * this using -EAGAIN.
90 	 */
91 	if (ops) {
92 		module_put(ops->owner);
93 		return ERR_PTR(-EAGAIN);
94 	}
95 #endif
96 	NL_SET_ERR_MSG(extack, "TC classifier not found");
97 	return ERR_PTR(-ENOENT);
98 }
99 
100 /* Register(unregister) new classifier type */
101 
102 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
103 {
104 	struct tcf_proto_ops *t;
105 	int rc = -EEXIST;
106 
107 	write_lock(&cls_mod_lock);
108 	list_for_each_entry(t, &tcf_proto_base, head)
109 		if (!strcmp(ops->kind, t->kind))
110 			goto out;
111 
112 	list_add_tail(&ops->head, &tcf_proto_base);
113 	rc = 0;
114 out:
115 	write_unlock(&cls_mod_lock);
116 	return rc;
117 }
118 EXPORT_SYMBOL(register_tcf_proto_ops);
119 
120 static struct workqueue_struct *tc_filter_wq;
121 
122 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
123 {
124 	struct tcf_proto_ops *t;
125 	int rc = -ENOENT;
126 
127 	/* Wait for outstanding call_rcu()s, if any, from a
128 	 * tcf_proto_ops's destroy() handler.
129 	 */
130 	rcu_barrier();
131 	flush_workqueue(tc_filter_wq);
132 
133 	write_lock(&cls_mod_lock);
134 	list_for_each_entry(t, &tcf_proto_base, head) {
135 		if (t == ops) {
136 			list_del(&t->head);
137 			rc = 0;
138 			break;
139 		}
140 	}
141 	write_unlock(&cls_mod_lock);
142 	return rc;
143 }
144 EXPORT_SYMBOL(unregister_tcf_proto_ops);
145 
146 bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
147 {
148 	INIT_RCU_WORK(rwork, func);
149 	return queue_rcu_work(tc_filter_wq, rwork);
150 }
151 EXPORT_SYMBOL(tcf_queue_work);
152 
153 /* Select new prio value from the range, managed by kernel. */
154 
155 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
156 {
157 	u32 first = TC_H_MAKE(0xC0000000U, 0U);
158 
159 	if (tp)
160 		first = tp->prio - 1;
161 
162 	return TC_H_MAJ(first);
163 }
164 
165 static bool tcf_proto_is_unlocked(const char *kind)
166 {
167 	const struct tcf_proto_ops *ops;
168 	bool ret;
169 
170 	ops = tcf_proto_lookup_ops(kind, false, NULL);
171 	/* On error return false to take rtnl lock. Proto lookup/create
172 	 * functions will perform lookup again and properly handle errors.
173 	 */
174 	if (IS_ERR(ops))
175 		return false;
176 
177 	ret = !!(ops->flags & TCF_PROTO_OPS_DOIT_UNLOCKED);
178 	module_put(ops->owner);
179 	return ret;
180 }
181 
182 static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
183 					  u32 prio, struct tcf_chain *chain,
184 					  bool rtnl_held,
185 					  struct netlink_ext_ack *extack)
186 {
187 	struct tcf_proto *tp;
188 	int err;
189 
190 	tp = kzalloc(sizeof(*tp), GFP_KERNEL);
191 	if (!tp)
192 		return ERR_PTR(-ENOBUFS);
193 
194 	tp->ops = tcf_proto_lookup_ops(kind, rtnl_held, extack);
195 	if (IS_ERR(tp->ops)) {
196 		err = PTR_ERR(tp->ops);
197 		goto errout;
198 	}
199 	tp->classify = tp->ops->classify;
200 	tp->protocol = protocol;
201 	tp->prio = prio;
202 	tp->chain = chain;
203 	spin_lock_init(&tp->lock);
204 	refcount_set(&tp->refcnt, 1);
205 
206 	err = tp->ops->init(tp);
207 	if (err) {
208 		module_put(tp->ops->owner);
209 		goto errout;
210 	}
211 	return tp;
212 
213 errout:
214 	kfree(tp);
215 	return ERR_PTR(err);
216 }
217 
218 static void tcf_proto_get(struct tcf_proto *tp)
219 {
220 	refcount_inc(&tp->refcnt);
221 }
222 
223 static void tcf_chain_put(struct tcf_chain *chain);
224 
225 static void tcf_proto_destroy(struct tcf_proto *tp, bool rtnl_held,
226 			      struct netlink_ext_ack *extack)
227 {
228 	tp->ops->destroy(tp, rtnl_held, extack);
229 	tcf_chain_put(tp->chain);
230 	module_put(tp->ops->owner);
231 	kfree_rcu(tp, rcu);
232 }
233 
234 static void tcf_proto_put(struct tcf_proto *tp, bool rtnl_held,
235 			  struct netlink_ext_ack *extack)
236 {
237 	if (refcount_dec_and_test(&tp->refcnt))
238 		tcf_proto_destroy(tp, rtnl_held, extack);
239 }
240 
241 static int walker_noop(struct tcf_proto *tp, void *d, struct tcf_walker *arg)
242 {
243 	return -1;
244 }
245 
246 static bool tcf_proto_is_empty(struct tcf_proto *tp, bool rtnl_held)
247 {
248 	struct tcf_walker walker = { .fn = walker_noop, };
249 
250 	if (tp->ops->walk) {
251 		tp->ops->walk(tp, &walker, rtnl_held);
252 		return !walker.stop;
253 	}
254 	return true;
255 }
256 
257 static bool tcf_proto_check_delete(struct tcf_proto *tp, bool rtnl_held)
258 {
259 	spin_lock(&tp->lock);
260 	if (tcf_proto_is_empty(tp, rtnl_held))
261 		tp->deleting = true;
262 	spin_unlock(&tp->lock);
263 	return tp->deleting;
264 }
265 
266 static void tcf_proto_mark_delete(struct tcf_proto *tp)
267 {
268 	spin_lock(&tp->lock);
269 	tp->deleting = true;
270 	spin_unlock(&tp->lock);
271 }
272 
273 static bool tcf_proto_is_deleting(struct tcf_proto *tp)
274 {
275 	bool deleting;
276 
277 	spin_lock(&tp->lock);
278 	deleting = tp->deleting;
279 	spin_unlock(&tp->lock);
280 
281 	return deleting;
282 }
283 
284 #define ASSERT_BLOCK_LOCKED(block)					\
285 	lockdep_assert_held(&(block)->lock)
286 
287 struct tcf_filter_chain_list_item {
288 	struct list_head list;
289 	tcf_chain_head_change_t *chain_head_change;
290 	void *chain_head_change_priv;
291 };
292 
293 static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
294 					  u32 chain_index)
295 {
296 	struct tcf_chain *chain;
297 
298 	ASSERT_BLOCK_LOCKED(block);
299 
300 	chain = kzalloc(sizeof(*chain), GFP_KERNEL);
301 	if (!chain)
302 		return NULL;
303 	list_add_tail(&chain->list, &block->chain_list);
304 	mutex_init(&chain->filter_chain_lock);
305 	chain->block = block;
306 	chain->index = chain_index;
307 	chain->refcnt = 1;
308 	if (!chain->index)
309 		block->chain0.chain = chain;
310 	return chain;
311 }
312 
313 static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
314 				       struct tcf_proto *tp_head)
315 {
316 	if (item->chain_head_change)
317 		item->chain_head_change(tp_head, item->chain_head_change_priv);
318 }
319 
320 static void tcf_chain0_head_change(struct tcf_chain *chain,
321 				   struct tcf_proto *tp_head)
322 {
323 	struct tcf_filter_chain_list_item *item;
324 	struct tcf_block *block = chain->block;
325 
326 	if (chain->index)
327 		return;
328 
329 	mutex_lock(&block->lock);
330 	list_for_each_entry(item, &block->chain0.filter_chain_list, list)
331 		tcf_chain_head_change_item(item, tp_head);
332 	mutex_unlock(&block->lock);
333 }
334 
335 /* Returns true if block can be safely freed. */
336 
337 static bool tcf_chain_detach(struct tcf_chain *chain)
338 {
339 	struct tcf_block *block = chain->block;
340 
341 	ASSERT_BLOCK_LOCKED(block);
342 
343 	list_del(&chain->list);
344 	if (!chain->index)
345 		block->chain0.chain = NULL;
346 
347 	if (list_empty(&block->chain_list) &&
348 	    refcount_read(&block->refcnt) == 0)
349 		return true;
350 
351 	return false;
352 }
353 
354 static void tcf_block_destroy(struct tcf_block *block)
355 {
356 	mutex_destroy(&block->lock);
357 	kfree_rcu(block, rcu);
358 }
359 
360 static void tcf_chain_destroy(struct tcf_chain *chain, bool free_block)
361 {
362 	struct tcf_block *block = chain->block;
363 
364 	mutex_destroy(&chain->filter_chain_lock);
365 	kfree(chain);
366 	if (free_block)
367 		tcf_block_destroy(block);
368 }
369 
370 static void tcf_chain_hold(struct tcf_chain *chain)
371 {
372 	ASSERT_BLOCK_LOCKED(chain->block);
373 
374 	++chain->refcnt;
375 }
376 
377 static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
378 {
379 	ASSERT_BLOCK_LOCKED(chain->block);
380 
381 	/* In case all the references are action references, this
382 	 * chain should not be shown to the user.
383 	 */
384 	return chain->refcnt == chain->action_refcnt;
385 }
386 
387 static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
388 					  u32 chain_index)
389 {
390 	struct tcf_chain *chain;
391 
392 	ASSERT_BLOCK_LOCKED(block);
393 
394 	list_for_each_entry(chain, &block->chain_list, list) {
395 		if (chain->index == chain_index)
396 			return chain;
397 	}
398 	return NULL;
399 }
400 
401 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
402 			   u32 seq, u16 flags, int event, bool unicast);
403 
404 static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
405 					 u32 chain_index, bool create,
406 					 bool by_act)
407 {
408 	struct tcf_chain *chain = NULL;
409 	bool is_first_reference;
410 
411 	mutex_lock(&block->lock);
412 	chain = tcf_chain_lookup(block, chain_index);
413 	if (chain) {
414 		tcf_chain_hold(chain);
415 	} else {
416 		if (!create)
417 			goto errout;
418 		chain = tcf_chain_create(block, chain_index);
419 		if (!chain)
420 			goto errout;
421 	}
422 
423 	if (by_act)
424 		++chain->action_refcnt;
425 	is_first_reference = chain->refcnt - chain->action_refcnt == 1;
426 	mutex_unlock(&block->lock);
427 
428 	/* Send notification only in case we got the first
429 	 * non-action reference. Until then, the chain acts only as
430 	 * a placeholder for actions pointing to it and user ought
431 	 * not know about them.
432 	 */
433 	if (is_first_reference && !by_act)
434 		tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
435 				RTM_NEWCHAIN, false);
436 
437 	return chain;
438 
439 errout:
440 	mutex_unlock(&block->lock);
441 	return chain;
442 }
443 
444 static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
445 				       bool create)
446 {
447 	return __tcf_chain_get(block, chain_index, create, false);
448 }
449 
450 struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
451 {
452 	return __tcf_chain_get(block, chain_index, true, true);
453 }
454 EXPORT_SYMBOL(tcf_chain_get_by_act);
455 
456 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
457 			       void *tmplt_priv);
458 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
459 				  void *tmplt_priv, u32 chain_index,
460 				  struct tcf_block *block, struct sk_buff *oskb,
461 				  u32 seq, u16 flags, bool unicast);
462 
463 static void __tcf_chain_put(struct tcf_chain *chain, bool by_act,
464 			    bool explicitly_created)
465 {
466 	struct tcf_block *block = chain->block;
467 	const struct tcf_proto_ops *tmplt_ops;
468 	bool is_last, free_block = false;
469 	unsigned int refcnt;
470 	void *tmplt_priv;
471 	u32 chain_index;
472 
473 	mutex_lock(&block->lock);
474 	if (explicitly_created) {
475 		if (!chain->explicitly_created) {
476 			mutex_unlock(&block->lock);
477 			return;
478 		}
479 		chain->explicitly_created = false;
480 	}
481 
482 	if (by_act)
483 		chain->action_refcnt--;
484 
485 	/* tc_chain_notify_delete can't be called while holding block lock.
486 	 * However, when block is unlocked chain can be changed concurrently, so
487 	 * save these to temporary variables.
488 	 */
489 	refcnt = --chain->refcnt;
490 	is_last = refcnt - chain->action_refcnt == 0;
491 	tmplt_ops = chain->tmplt_ops;
492 	tmplt_priv = chain->tmplt_priv;
493 	chain_index = chain->index;
494 
495 	if (refcnt == 0)
496 		free_block = tcf_chain_detach(chain);
497 	mutex_unlock(&block->lock);
498 
499 	/* The last dropped non-action reference will trigger notification. */
500 	if (is_last && !by_act) {
501 		tc_chain_notify_delete(tmplt_ops, tmplt_priv, chain_index,
502 				       block, NULL, 0, 0, false);
503 		/* Last reference to chain, no need to lock. */
504 		chain->flushing = false;
505 	}
506 
507 	if (refcnt == 0) {
508 		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 		goto errout_qdisc;
1314 
1315 	return block;
1316 
1317 errout_qdisc:
1318 	if (*q)
1319 		qdisc_put(*q);
1320 errout:
1321 	*q = NULL;
1322 	return ERR_PTR(err);
1323 }
1324 
1325 static void tcf_block_release(struct Qdisc *q, struct tcf_block *block,
1326 			      bool rtnl_held)
1327 {
1328 	if (!IS_ERR_OR_NULL(block))
1329 		tcf_block_refcnt_put(block, rtnl_held);
1330 
1331 	if (q) {
1332 		if (rtnl_held)
1333 			qdisc_put(q);
1334 		else
1335 			qdisc_put_unlocked(q);
1336 	}
1337 }
1338 
1339 struct tcf_block_owner_item {
1340 	struct list_head list;
1341 	struct Qdisc *q;
1342 	enum tcf_block_binder_type binder_type;
1343 };
1344 
1345 static void
1346 tcf_block_owner_netif_keep_dst(struct tcf_block *block,
1347 			       struct Qdisc *q,
1348 			       enum tcf_block_binder_type binder_type)
1349 {
1350 	if (block->keep_dst &&
1351 	    binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
1352 	    binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1353 		netif_keep_dst(qdisc_dev(q));
1354 }
1355 
1356 void tcf_block_netif_keep_dst(struct tcf_block *block)
1357 {
1358 	struct tcf_block_owner_item *item;
1359 
1360 	block->keep_dst = true;
1361 	list_for_each_entry(item, &block->owner_list, list)
1362 		tcf_block_owner_netif_keep_dst(block, item->q,
1363 					       item->binder_type);
1364 }
1365 EXPORT_SYMBOL(tcf_block_netif_keep_dst);
1366 
1367 static int tcf_block_owner_add(struct tcf_block *block,
1368 			       struct Qdisc *q,
1369 			       enum tcf_block_binder_type binder_type)
1370 {
1371 	struct tcf_block_owner_item *item;
1372 
1373 	item = kmalloc(sizeof(*item), GFP_KERNEL);
1374 	if (!item)
1375 		return -ENOMEM;
1376 	item->q = q;
1377 	item->binder_type = binder_type;
1378 	list_add(&item->list, &block->owner_list);
1379 	return 0;
1380 }
1381 
1382 static void tcf_block_owner_del(struct tcf_block *block,
1383 				struct Qdisc *q,
1384 				enum tcf_block_binder_type binder_type)
1385 {
1386 	struct tcf_block_owner_item *item;
1387 
1388 	list_for_each_entry(item, &block->owner_list, list) {
1389 		if (item->q == q && item->binder_type == binder_type) {
1390 			list_del(&item->list);
1391 			kfree(item);
1392 			return;
1393 		}
1394 	}
1395 	WARN_ON(1);
1396 }
1397 
1398 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
1399 		      struct tcf_block_ext_info *ei,
1400 		      struct netlink_ext_ack *extack)
1401 {
1402 	struct net *net = qdisc_net(q);
1403 	struct tcf_block *block = NULL;
1404 	int err;
1405 
1406 	if (ei->block_index)
1407 		/* block_index not 0 means the shared block is requested */
1408 		block = tcf_block_refcnt_get(net, ei->block_index);
1409 
1410 	if (!block) {
1411 		block = tcf_block_create(net, q, ei->block_index, extack);
1412 		if (IS_ERR(block))
1413 			return PTR_ERR(block);
1414 		if (tcf_block_shared(block)) {
1415 			err = tcf_block_insert(block, net, extack);
1416 			if (err)
1417 				goto err_block_insert;
1418 		}
1419 	}
1420 
1421 	err = tcf_block_owner_add(block, q, ei->binder_type);
1422 	if (err)
1423 		goto err_block_owner_add;
1424 
1425 	tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
1426 
1427 	err = tcf_chain0_head_change_cb_add(block, ei, extack);
1428 	if (err)
1429 		goto err_chain0_head_change_cb_add;
1430 
1431 	err = tcf_block_offload_bind(block, q, ei, extack);
1432 	if (err)
1433 		goto err_block_offload_bind;
1434 
1435 	*p_block = block;
1436 	return 0;
1437 
1438 err_block_offload_bind:
1439 	tcf_chain0_head_change_cb_del(block, ei);
1440 err_chain0_head_change_cb_add:
1441 	tcf_block_owner_del(block, q, ei->binder_type);
1442 err_block_owner_add:
1443 err_block_insert:
1444 	tcf_block_refcnt_put(block, true);
1445 	return err;
1446 }
1447 EXPORT_SYMBOL(tcf_block_get_ext);
1448 
1449 static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
1450 {
1451 	struct tcf_proto __rcu **p_filter_chain = priv;
1452 
1453 	rcu_assign_pointer(*p_filter_chain, tp_head);
1454 }
1455 
1456 int tcf_block_get(struct tcf_block **p_block,
1457 		  struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
1458 		  struct netlink_ext_ack *extack)
1459 {
1460 	struct tcf_block_ext_info ei = {
1461 		.chain_head_change = tcf_chain_head_change_dflt,
1462 		.chain_head_change_priv = p_filter_chain,
1463 	};
1464 
1465 	WARN_ON(!p_filter_chain);
1466 	return tcf_block_get_ext(p_block, q, &ei, extack);
1467 }
1468 EXPORT_SYMBOL(tcf_block_get);
1469 
1470 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
1471  * actions should be all removed after flushing.
1472  */
1473 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
1474 		       struct tcf_block_ext_info *ei)
1475 {
1476 	if (!block)
1477 		return;
1478 	tcf_chain0_head_change_cb_del(block, ei);
1479 	tcf_block_owner_del(block, q, ei->binder_type);
1480 
1481 	__tcf_block_put(block, q, ei, true);
1482 }
1483 EXPORT_SYMBOL(tcf_block_put_ext);
1484 
1485 void tcf_block_put(struct tcf_block *block)
1486 {
1487 	struct tcf_block_ext_info ei = {0, };
1488 
1489 	if (!block)
1490 		return;
1491 	tcf_block_put_ext(block, block->q, &ei);
1492 }
1493 
1494 EXPORT_SYMBOL(tcf_block_put);
1495 
1496 struct tcf_block_cb {
1497 	struct list_head list;
1498 	tc_setup_cb_t *cb;
1499 	void *cb_ident;
1500 	void *cb_priv;
1501 	unsigned int refcnt;
1502 };
1503 
1504 void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
1505 {
1506 	return block_cb->cb_priv;
1507 }
1508 EXPORT_SYMBOL(tcf_block_cb_priv);
1509 
1510 struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
1511 					 tc_setup_cb_t *cb, void *cb_ident)
1512 {	struct tcf_block_cb *block_cb;
1513 
1514 	list_for_each_entry(block_cb, &block->cb_list, list)
1515 		if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
1516 			return block_cb;
1517 	return NULL;
1518 }
1519 EXPORT_SYMBOL(tcf_block_cb_lookup);
1520 
1521 void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
1522 {
1523 	block_cb->refcnt++;
1524 }
1525 EXPORT_SYMBOL(tcf_block_cb_incref);
1526 
1527 unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
1528 {
1529 	return --block_cb->refcnt;
1530 }
1531 EXPORT_SYMBOL(tcf_block_cb_decref);
1532 
1533 static int
1534 tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb,
1535 			    void *cb_priv, bool add, bool offload_in_use,
1536 			    struct netlink_ext_ack *extack)
1537 {
1538 	struct tcf_chain *chain, *chain_prev;
1539 	struct tcf_proto *tp, *tp_prev;
1540 	int err;
1541 
1542 	for (chain = __tcf_get_next_chain(block, NULL);
1543 	     chain;
1544 	     chain_prev = chain,
1545 		     chain = __tcf_get_next_chain(block, chain),
1546 		     tcf_chain_put(chain_prev)) {
1547 		for (tp = __tcf_get_next_proto(chain, NULL); tp;
1548 		     tp_prev = tp,
1549 			     tp = __tcf_get_next_proto(chain, tp),
1550 			     tcf_proto_put(tp_prev, true, NULL)) {
1551 			if (tp->ops->reoffload) {
1552 				err = tp->ops->reoffload(tp, add, cb, cb_priv,
1553 							 extack);
1554 				if (err && add)
1555 					goto err_playback_remove;
1556 			} else if (add && offload_in_use) {
1557 				err = -EOPNOTSUPP;
1558 				NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
1559 				goto err_playback_remove;
1560 			}
1561 		}
1562 	}
1563 
1564 	return 0;
1565 
1566 err_playback_remove:
1567 	tcf_proto_put(tp, true, NULL);
1568 	tcf_chain_put(chain);
1569 	tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
1570 				    extack);
1571 	return err;
1572 }
1573 
1574 struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
1575 					     tc_setup_cb_t *cb, void *cb_ident,
1576 					     void *cb_priv,
1577 					     struct netlink_ext_ack *extack)
1578 {
1579 	struct tcf_block_cb *block_cb;
1580 	int err;
1581 
1582 	/* Replay any already present rules */
1583 	err = tcf_block_playback_offloads(block, cb, cb_priv, true,
1584 					  tcf_block_offload_in_use(block),
1585 					  extack);
1586 	if (err)
1587 		return ERR_PTR(err);
1588 
1589 	block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
1590 	if (!block_cb)
1591 		return ERR_PTR(-ENOMEM);
1592 	block_cb->cb = cb;
1593 	block_cb->cb_ident = cb_ident;
1594 	block_cb->cb_priv = cb_priv;
1595 	list_add(&block_cb->list, &block->cb_list);
1596 	return block_cb;
1597 }
1598 EXPORT_SYMBOL(__tcf_block_cb_register);
1599 
1600 int tcf_block_cb_register(struct tcf_block *block,
1601 			  tc_setup_cb_t *cb, void *cb_ident,
1602 			  void *cb_priv, struct netlink_ext_ack *extack)
1603 {
1604 	struct tcf_block_cb *block_cb;
1605 
1606 	block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv,
1607 					   extack);
1608 	return PTR_ERR_OR_ZERO(block_cb);
1609 }
1610 EXPORT_SYMBOL(tcf_block_cb_register);
1611 
1612 void __tcf_block_cb_unregister(struct tcf_block *block,
1613 			       struct tcf_block_cb *block_cb)
1614 {
1615 	tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv,
1616 				    false, tcf_block_offload_in_use(block),
1617 				    NULL);
1618 	list_del(&block_cb->list);
1619 	kfree(block_cb);
1620 }
1621 EXPORT_SYMBOL(__tcf_block_cb_unregister);
1622 
1623 void tcf_block_cb_unregister(struct tcf_block *block,
1624 			     tc_setup_cb_t *cb, void *cb_ident)
1625 {
1626 	struct tcf_block_cb *block_cb;
1627 
1628 	block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
1629 	if (!block_cb)
1630 		return;
1631 	__tcf_block_cb_unregister(block, block_cb);
1632 }
1633 EXPORT_SYMBOL(tcf_block_cb_unregister);
1634 
1635 /* Main classifier routine: scans classifier chain attached
1636  * to this qdisc, (optionally) tests for protocol and asks
1637  * specific classifiers.
1638  */
1639 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
1640 		 struct tcf_result *res, bool compat_mode)
1641 {
1642 #ifdef CONFIG_NET_CLS_ACT
1643 	const int max_reclassify_loop = 4;
1644 	const struct tcf_proto *orig_tp = tp;
1645 	const struct tcf_proto *first_tp;
1646 	int limit = 0;
1647 
1648 reclassify:
1649 #endif
1650 	for (; tp; tp = rcu_dereference_bh(tp->next)) {
1651 		__be16 protocol = tc_skb_protocol(skb);
1652 		int err;
1653 
1654 		if (tp->protocol != protocol &&
1655 		    tp->protocol != htons(ETH_P_ALL))
1656 			continue;
1657 
1658 		err = tp->classify(skb, tp, res);
1659 #ifdef CONFIG_NET_CLS_ACT
1660 		if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
1661 			first_tp = orig_tp;
1662 			goto reset;
1663 		} else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
1664 			first_tp = res->goto_tp;
1665 			goto reset;
1666 		}
1667 #endif
1668 		if (err >= 0)
1669 			return err;
1670 	}
1671 
1672 	return TC_ACT_UNSPEC; /* signal: continue lookup */
1673 #ifdef CONFIG_NET_CLS_ACT
1674 reset:
1675 	if (unlikely(limit++ >= max_reclassify_loop)) {
1676 		net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
1677 				       tp->chain->block->index,
1678 				       tp->prio & 0xffff,
1679 				       ntohs(tp->protocol));
1680 		return TC_ACT_SHOT;
1681 	}
1682 
1683 	tp = first_tp;
1684 	goto reclassify;
1685 #endif
1686 }
1687 EXPORT_SYMBOL(tcf_classify);
1688 
1689 struct tcf_chain_info {
1690 	struct tcf_proto __rcu **pprev;
1691 	struct tcf_proto __rcu *next;
1692 };
1693 
1694 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain *chain,
1695 					   struct tcf_chain_info *chain_info)
1696 {
1697 	return tcf_chain_dereference(*chain_info->pprev, chain);
1698 }
1699 
1700 static int tcf_chain_tp_insert(struct tcf_chain *chain,
1701 			       struct tcf_chain_info *chain_info,
1702 			       struct tcf_proto *tp)
1703 {
1704 	if (chain->flushing)
1705 		return -EAGAIN;
1706 
1707 	if (*chain_info->pprev == chain->filter_chain)
1708 		tcf_chain0_head_change(chain, tp);
1709 	tcf_proto_get(tp);
1710 	RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info));
1711 	rcu_assign_pointer(*chain_info->pprev, tp);
1712 
1713 	return 0;
1714 }
1715 
1716 static void tcf_chain_tp_remove(struct tcf_chain *chain,
1717 				struct tcf_chain_info *chain_info,
1718 				struct tcf_proto *tp)
1719 {
1720 	struct tcf_proto *next = tcf_chain_dereference(chain_info->next, chain);
1721 
1722 	tcf_proto_mark_delete(tp);
1723 	if (tp == chain->filter_chain)
1724 		tcf_chain0_head_change(chain, next);
1725 	RCU_INIT_POINTER(*chain_info->pprev, next);
1726 }
1727 
1728 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1729 					   struct tcf_chain_info *chain_info,
1730 					   u32 protocol, u32 prio,
1731 					   bool prio_allocate);
1732 
1733 /* Try to insert new proto.
1734  * If proto with specified priority already exists, free new proto
1735  * and return existing one.
1736  */
1737 
1738 static struct tcf_proto *tcf_chain_tp_insert_unique(struct tcf_chain *chain,
1739 						    struct tcf_proto *tp_new,
1740 						    u32 protocol, u32 prio,
1741 						    bool rtnl_held)
1742 {
1743 	struct tcf_chain_info chain_info;
1744 	struct tcf_proto *tp;
1745 	int err = 0;
1746 
1747 	mutex_lock(&chain->filter_chain_lock);
1748 
1749 	tp = tcf_chain_tp_find(chain, &chain_info,
1750 			       protocol, prio, false);
1751 	if (!tp)
1752 		err = tcf_chain_tp_insert(chain, &chain_info, tp_new);
1753 	mutex_unlock(&chain->filter_chain_lock);
1754 
1755 	if (tp) {
1756 		tcf_proto_destroy(tp_new, rtnl_held, NULL);
1757 		tp_new = tp;
1758 	} else if (err) {
1759 		tcf_proto_destroy(tp_new, rtnl_held, NULL);
1760 		tp_new = ERR_PTR(err);
1761 	}
1762 
1763 	return tp_new;
1764 }
1765 
1766 static void tcf_chain_tp_delete_empty(struct tcf_chain *chain,
1767 				      struct tcf_proto *tp, bool rtnl_held,
1768 				      struct netlink_ext_ack *extack)
1769 {
1770 	struct tcf_chain_info chain_info;
1771 	struct tcf_proto *tp_iter;
1772 	struct tcf_proto **pprev;
1773 	struct tcf_proto *next;
1774 
1775 	mutex_lock(&chain->filter_chain_lock);
1776 
1777 	/* Atomically find and remove tp from chain. */
1778 	for (pprev = &chain->filter_chain;
1779 	     (tp_iter = tcf_chain_dereference(*pprev, chain));
1780 	     pprev = &tp_iter->next) {
1781 		if (tp_iter == tp) {
1782 			chain_info.pprev = pprev;
1783 			chain_info.next = tp_iter->next;
1784 			WARN_ON(tp_iter->deleting);
1785 			break;
1786 		}
1787 	}
1788 	/* Verify that tp still exists and no new filters were inserted
1789 	 * concurrently.
1790 	 * Mark tp for deletion if it is empty.
1791 	 */
1792 	if (!tp_iter || !tcf_proto_check_delete(tp, rtnl_held)) {
1793 		mutex_unlock(&chain->filter_chain_lock);
1794 		return;
1795 	}
1796 
1797 	next = tcf_chain_dereference(chain_info.next, chain);
1798 	if (tp == chain->filter_chain)
1799 		tcf_chain0_head_change(chain, next);
1800 	RCU_INIT_POINTER(*chain_info.pprev, next);
1801 	mutex_unlock(&chain->filter_chain_lock);
1802 
1803 	tcf_proto_put(tp, rtnl_held, extack);
1804 }
1805 
1806 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1807 					   struct tcf_chain_info *chain_info,
1808 					   u32 protocol, u32 prio,
1809 					   bool prio_allocate)
1810 {
1811 	struct tcf_proto **pprev;
1812 	struct tcf_proto *tp;
1813 
1814 	/* Check the chain for existence of proto-tcf with this priority */
1815 	for (pprev = &chain->filter_chain;
1816 	     (tp = tcf_chain_dereference(*pprev, chain));
1817 	     pprev = &tp->next) {
1818 		if (tp->prio >= prio) {
1819 			if (tp->prio == prio) {
1820 				if (prio_allocate ||
1821 				    (tp->protocol != protocol && protocol))
1822 					return ERR_PTR(-EINVAL);
1823 			} else {
1824 				tp = NULL;
1825 			}
1826 			break;
1827 		}
1828 	}
1829 	chain_info->pprev = pprev;
1830 	if (tp) {
1831 		chain_info->next = tp->next;
1832 		tcf_proto_get(tp);
1833 	} else {
1834 		chain_info->next = NULL;
1835 	}
1836 	return tp;
1837 }
1838 
1839 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
1840 			 struct tcf_proto *tp, struct tcf_block *block,
1841 			 struct Qdisc *q, u32 parent, void *fh,
1842 			 u32 portid, u32 seq, u16 flags, int event,
1843 			 bool rtnl_held)
1844 {
1845 	struct tcmsg *tcm;
1846 	struct nlmsghdr  *nlh;
1847 	unsigned char *b = skb_tail_pointer(skb);
1848 
1849 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1850 	if (!nlh)
1851 		goto out_nlmsg_trim;
1852 	tcm = nlmsg_data(nlh);
1853 	tcm->tcm_family = AF_UNSPEC;
1854 	tcm->tcm__pad1 = 0;
1855 	tcm->tcm__pad2 = 0;
1856 	if (q) {
1857 		tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1858 		tcm->tcm_parent = parent;
1859 	} else {
1860 		tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1861 		tcm->tcm_block_index = block->index;
1862 	}
1863 	tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1864 	if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1865 		goto nla_put_failure;
1866 	if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1867 		goto nla_put_failure;
1868 	if (!fh) {
1869 		tcm->tcm_handle = 0;
1870 	} else {
1871 		if (tp->ops->dump &&
1872 		    tp->ops->dump(net, tp, fh, skb, tcm, rtnl_held) < 0)
1873 			goto nla_put_failure;
1874 	}
1875 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1876 	return skb->len;
1877 
1878 out_nlmsg_trim:
1879 nla_put_failure:
1880 	nlmsg_trim(skb, b);
1881 	return -1;
1882 }
1883 
1884 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1885 			  struct nlmsghdr *n, struct tcf_proto *tp,
1886 			  struct tcf_block *block, struct Qdisc *q,
1887 			  u32 parent, void *fh, int event, bool unicast,
1888 			  bool rtnl_held)
1889 {
1890 	struct sk_buff *skb;
1891 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1892 
1893 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1894 	if (!skb)
1895 		return -ENOBUFS;
1896 
1897 	if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1898 			  n->nlmsg_seq, n->nlmsg_flags, event,
1899 			  rtnl_held) <= 0) {
1900 		kfree_skb(skb);
1901 		return -EINVAL;
1902 	}
1903 
1904 	if (unicast)
1905 		return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1906 
1907 	return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1908 			      n->nlmsg_flags & NLM_F_ECHO);
1909 }
1910 
1911 static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1912 			      struct nlmsghdr *n, struct tcf_proto *tp,
1913 			      struct tcf_block *block, struct Qdisc *q,
1914 			      u32 parent, void *fh, bool unicast, bool *last,
1915 			      bool rtnl_held, struct netlink_ext_ack *extack)
1916 {
1917 	struct sk_buff *skb;
1918 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1919 	int err;
1920 
1921 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1922 	if (!skb)
1923 		return -ENOBUFS;
1924 
1925 	if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1926 			  n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER,
1927 			  rtnl_held) <= 0) {
1928 		NL_SET_ERR_MSG(extack, "Failed to build del event notification");
1929 		kfree_skb(skb);
1930 		return -EINVAL;
1931 	}
1932 
1933 	err = tp->ops->delete(tp, fh, last, rtnl_held, extack);
1934 	if (err) {
1935 		kfree_skb(skb);
1936 		return err;
1937 	}
1938 
1939 	if (unicast)
1940 		return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1941 
1942 	err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1943 			     n->nlmsg_flags & NLM_F_ECHO);
1944 	if (err < 0)
1945 		NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1946 	return err;
1947 }
1948 
1949 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
1950 				 struct tcf_block *block, struct Qdisc *q,
1951 				 u32 parent, struct nlmsghdr *n,
1952 				 struct tcf_chain *chain, int event,
1953 				 bool rtnl_held)
1954 {
1955 	struct tcf_proto *tp;
1956 
1957 	for (tp = tcf_get_next_proto(chain, NULL, rtnl_held);
1958 	     tp; tp = tcf_get_next_proto(chain, tp, rtnl_held))
1959 		tfilter_notify(net, oskb, n, tp, block,
1960 			       q, parent, NULL, event, false, rtnl_held);
1961 }
1962 
1963 static void tfilter_put(struct tcf_proto *tp, void *fh)
1964 {
1965 	if (tp->ops->put && fh)
1966 		tp->ops->put(tp, fh);
1967 }
1968 
1969 static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1970 			  struct netlink_ext_ack *extack)
1971 {
1972 	struct net *net = sock_net(skb->sk);
1973 	struct nlattr *tca[TCA_MAX + 1];
1974 	struct tcmsg *t;
1975 	u32 protocol;
1976 	u32 prio;
1977 	bool prio_allocate;
1978 	u32 parent;
1979 	u32 chain_index;
1980 	struct Qdisc *q = NULL;
1981 	struct tcf_chain_info chain_info;
1982 	struct tcf_chain *chain = NULL;
1983 	struct tcf_block *block;
1984 	struct tcf_proto *tp;
1985 	unsigned long cl;
1986 	void *fh;
1987 	int err;
1988 	int tp_created;
1989 	bool rtnl_held = false;
1990 
1991 	if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1992 		return -EPERM;
1993 
1994 replay:
1995 	tp_created = 0;
1996 
1997 	err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
1998 	if (err < 0)
1999 		return err;
2000 
2001 	t = nlmsg_data(n);
2002 	protocol = TC_H_MIN(t->tcm_info);
2003 	prio = TC_H_MAJ(t->tcm_info);
2004 	prio_allocate = false;
2005 	parent = t->tcm_parent;
2006 	tp = NULL;
2007 	cl = 0;
2008 	block = NULL;
2009 
2010 	if (prio == 0) {
2011 		/* If no priority is provided by the user,
2012 		 * we allocate one.
2013 		 */
2014 		if (n->nlmsg_flags & NLM_F_CREATE) {
2015 			prio = TC_H_MAKE(0x80000000U, 0U);
2016 			prio_allocate = true;
2017 		} else {
2018 			NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2019 			return -ENOENT;
2020 		}
2021 	}
2022 
2023 	/* Find head of filter chain. */
2024 
2025 	err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2026 	if (err)
2027 		return err;
2028 
2029 	/* Take rtnl mutex if rtnl_held was set to true on previous iteration,
2030 	 * block is shared (no qdisc found), qdisc is not unlocked, classifier
2031 	 * type is not specified, classifier is not unlocked.
2032 	 */
2033 	if (rtnl_held ||
2034 	    (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2035 	    !tca[TCA_KIND] || !tcf_proto_is_unlocked(nla_data(tca[TCA_KIND]))) {
2036 		rtnl_held = true;
2037 		rtnl_lock();
2038 	}
2039 
2040 	err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2041 	if (err)
2042 		goto errout;
2043 
2044 	block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2045 				 extack);
2046 	if (IS_ERR(block)) {
2047 		err = PTR_ERR(block);
2048 		goto errout;
2049 	}
2050 
2051 	chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2052 	if (chain_index > TC_ACT_EXT_VAL_MASK) {
2053 		NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2054 		err = -EINVAL;
2055 		goto errout;
2056 	}
2057 	chain = tcf_chain_get(block, chain_index, true);
2058 	if (!chain) {
2059 		NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
2060 		err = -ENOMEM;
2061 		goto errout;
2062 	}
2063 
2064 	mutex_lock(&chain->filter_chain_lock);
2065 	tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2066 			       prio, prio_allocate);
2067 	if (IS_ERR(tp)) {
2068 		NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2069 		err = PTR_ERR(tp);
2070 		goto errout_locked;
2071 	}
2072 
2073 	if (tp == NULL) {
2074 		struct tcf_proto *tp_new = NULL;
2075 
2076 		if (chain->flushing) {
2077 			err = -EAGAIN;
2078 			goto errout_locked;
2079 		}
2080 
2081 		/* Proto-tcf does not exist, create new one */
2082 
2083 		if (tca[TCA_KIND] == NULL || !protocol) {
2084 			NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
2085 			err = -EINVAL;
2086 			goto errout_locked;
2087 		}
2088 
2089 		if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2090 			NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2091 			err = -ENOENT;
2092 			goto errout_locked;
2093 		}
2094 
2095 		if (prio_allocate)
2096 			prio = tcf_auto_prio(tcf_chain_tp_prev(chain,
2097 							       &chain_info));
2098 
2099 		mutex_unlock(&chain->filter_chain_lock);
2100 		tp_new = tcf_proto_create(nla_data(tca[TCA_KIND]),
2101 					  protocol, prio, chain, rtnl_held,
2102 					  extack);
2103 		if (IS_ERR(tp_new)) {
2104 			err = PTR_ERR(tp_new);
2105 			goto errout_tp;
2106 		}
2107 
2108 		tp_created = 1;
2109 		tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio,
2110 						rtnl_held);
2111 		if (IS_ERR(tp)) {
2112 			err = PTR_ERR(tp);
2113 			goto errout_tp;
2114 		}
2115 	} else {
2116 		mutex_unlock(&chain->filter_chain_lock);
2117 	}
2118 
2119 	if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2120 		NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2121 		err = -EINVAL;
2122 		goto errout;
2123 	}
2124 
2125 	fh = tp->ops->get(tp, t->tcm_handle);
2126 
2127 	if (!fh) {
2128 		if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2129 			NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2130 			err = -ENOENT;
2131 			goto errout;
2132 		}
2133 	} else if (n->nlmsg_flags & NLM_F_EXCL) {
2134 		tfilter_put(tp, fh);
2135 		NL_SET_ERR_MSG(extack, "Filter already exists");
2136 		err = -EEXIST;
2137 		goto errout;
2138 	}
2139 
2140 	if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
2141 		NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
2142 		err = -EINVAL;
2143 		goto errout;
2144 	}
2145 
2146 	err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
2147 			      n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
2148 			      rtnl_held, extack);
2149 	if (err == 0) {
2150 		tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2151 			       RTM_NEWTFILTER, false, rtnl_held);
2152 		tfilter_put(tp, fh);
2153 	}
2154 
2155 errout:
2156 	if (err && tp_created)
2157 		tcf_chain_tp_delete_empty(chain, tp, rtnl_held, NULL);
2158 errout_tp:
2159 	if (chain) {
2160 		if (tp && !IS_ERR(tp))
2161 			tcf_proto_put(tp, rtnl_held, NULL);
2162 		if (!tp_created)
2163 			tcf_chain_put(chain);
2164 	}
2165 	tcf_block_release(q, block, rtnl_held);
2166 
2167 	if (rtnl_held)
2168 		rtnl_unlock();
2169 
2170 	if (err == -EAGAIN) {
2171 		/* Take rtnl lock in case EAGAIN is caused by concurrent flush
2172 		 * of target chain.
2173 		 */
2174 		rtnl_held = true;
2175 		/* Replay the request. */
2176 		goto replay;
2177 	}
2178 	return err;
2179 
2180 errout_locked:
2181 	mutex_unlock(&chain->filter_chain_lock);
2182 	goto errout;
2183 }
2184 
2185 static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2186 			  struct netlink_ext_ack *extack)
2187 {
2188 	struct net *net = sock_net(skb->sk);
2189 	struct nlattr *tca[TCA_MAX + 1];
2190 	struct tcmsg *t;
2191 	u32 protocol;
2192 	u32 prio;
2193 	u32 parent;
2194 	u32 chain_index;
2195 	struct Qdisc *q = NULL;
2196 	struct tcf_chain_info chain_info;
2197 	struct tcf_chain *chain = NULL;
2198 	struct tcf_block *block = NULL;
2199 	struct tcf_proto *tp = NULL;
2200 	unsigned long cl = 0;
2201 	void *fh = NULL;
2202 	int err;
2203 	bool rtnl_held = false;
2204 
2205 	if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
2206 		return -EPERM;
2207 
2208 	err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
2209 	if (err < 0)
2210 		return err;
2211 
2212 	t = nlmsg_data(n);
2213 	protocol = TC_H_MIN(t->tcm_info);
2214 	prio = TC_H_MAJ(t->tcm_info);
2215 	parent = t->tcm_parent;
2216 
2217 	if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
2218 		NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
2219 		return -ENOENT;
2220 	}
2221 
2222 	/* Find head of filter chain. */
2223 
2224 	err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2225 	if (err)
2226 		return err;
2227 
2228 	/* Take rtnl mutex if flushing whole chain, block is shared (no qdisc
2229 	 * found), qdisc is not unlocked, classifier type is not specified,
2230 	 * classifier is not unlocked.
2231 	 */
2232 	if (!prio ||
2233 	    (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2234 	    !tca[TCA_KIND] || !tcf_proto_is_unlocked(nla_data(tca[TCA_KIND]))) {
2235 		rtnl_held = true;
2236 		rtnl_lock();
2237 	}
2238 
2239 	err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2240 	if (err)
2241 		goto errout;
2242 
2243 	block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2244 				 extack);
2245 	if (IS_ERR(block)) {
2246 		err = PTR_ERR(block);
2247 		goto errout;
2248 	}
2249 
2250 	chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2251 	if (chain_index > TC_ACT_EXT_VAL_MASK) {
2252 		NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2253 		err = -EINVAL;
2254 		goto errout;
2255 	}
2256 	chain = tcf_chain_get(block, chain_index, false);
2257 	if (!chain) {
2258 		/* User requested flush on non-existent chain. Nothing to do,
2259 		 * so just return success.
2260 		 */
2261 		if (prio == 0) {
2262 			err = 0;
2263 			goto errout;
2264 		}
2265 		NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2266 		err = -ENOENT;
2267 		goto errout;
2268 	}
2269 
2270 	if (prio == 0) {
2271 		tfilter_notify_chain(net, skb, block, q, parent, n,
2272 				     chain, RTM_DELTFILTER, rtnl_held);
2273 		tcf_chain_flush(chain, rtnl_held);
2274 		err = 0;
2275 		goto errout;
2276 	}
2277 
2278 	mutex_lock(&chain->filter_chain_lock);
2279 	tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2280 			       prio, false);
2281 	if (!tp || IS_ERR(tp)) {
2282 		NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2283 		err = tp ? PTR_ERR(tp) : -ENOENT;
2284 		goto errout_locked;
2285 	} else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2286 		NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2287 		err = -EINVAL;
2288 		goto errout_locked;
2289 	} else if (t->tcm_handle == 0) {
2290 		tcf_chain_tp_remove(chain, &chain_info, tp);
2291 		mutex_unlock(&chain->filter_chain_lock);
2292 
2293 		tcf_proto_put(tp, rtnl_held, NULL);
2294 		tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2295 			       RTM_DELTFILTER, false, rtnl_held);
2296 		err = 0;
2297 		goto errout;
2298 	}
2299 	mutex_unlock(&chain->filter_chain_lock);
2300 
2301 	fh = tp->ops->get(tp, t->tcm_handle);
2302 
2303 	if (!fh) {
2304 		NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2305 		err = -ENOENT;
2306 	} else {
2307 		bool last;
2308 
2309 		err = tfilter_del_notify(net, skb, n, tp, block,
2310 					 q, parent, fh, false, &last,
2311 					 rtnl_held, extack);
2312 
2313 		if (err)
2314 			goto errout;
2315 		if (last)
2316 			tcf_chain_tp_delete_empty(chain, tp, rtnl_held, extack);
2317 	}
2318 
2319 errout:
2320 	if (chain) {
2321 		if (tp && !IS_ERR(tp))
2322 			tcf_proto_put(tp, rtnl_held, NULL);
2323 		tcf_chain_put(chain);
2324 	}
2325 	tcf_block_release(q, block, rtnl_held);
2326 
2327 	if (rtnl_held)
2328 		rtnl_unlock();
2329 
2330 	return err;
2331 
2332 errout_locked:
2333 	mutex_unlock(&chain->filter_chain_lock);
2334 	goto errout;
2335 }
2336 
2337 static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2338 			  struct netlink_ext_ack *extack)
2339 {
2340 	struct net *net = sock_net(skb->sk);
2341 	struct nlattr *tca[TCA_MAX + 1];
2342 	struct tcmsg *t;
2343 	u32 protocol;
2344 	u32 prio;
2345 	u32 parent;
2346 	u32 chain_index;
2347 	struct Qdisc *q = NULL;
2348 	struct tcf_chain_info chain_info;
2349 	struct tcf_chain *chain = NULL;
2350 	struct tcf_block *block = NULL;
2351 	struct tcf_proto *tp = NULL;
2352 	unsigned long cl = 0;
2353 	void *fh = NULL;
2354 	int err;
2355 	bool rtnl_held = false;
2356 
2357 	err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
2358 	if (err < 0)
2359 		return err;
2360 
2361 	t = nlmsg_data(n);
2362 	protocol = TC_H_MIN(t->tcm_info);
2363 	prio = TC_H_MAJ(t->tcm_info);
2364 	parent = t->tcm_parent;
2365 
2366 	if (prio == 0) {
2367 		NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2368 		return -ENOENT;
2369 	}
2370 
2371 	/* Find head of filter chain. */
2372 
2373 	err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2374 	if (err)
2375 		return err;
2376 
2377 	/* Take rtnl mutex if block is shared (no qdisc found), qdisc is not
2378 	 * unlocked, classifier type is not specified, classifier is not
2379 	 * unlocked.
2380 	 */
2381 	if ((q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2382 	    !tca[TCA_KIND] || !tcf_proto_is_unlocked(nla_data(tca[TCA_KIND]))) {
2383 		rtnl_held = true;
2384 		rtnl_lock();
2385 	}
2386 
2387 	err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2388 	if (err)
2389 		goto errout;
2390 
2391 	block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2392 				 extack);
2393 	if (IS_ERR(block)) {
2394 		err = PTR_ERR(block);
2395 		goto errout;
2396 	}
2397 
2398 	chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2399 	if (chain_index > TC_ACT_EXT_VAL_MASK) {
2400 		NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2401 		err = -EINVAL;
2402 		goto errout;
2403 	}
2404 	chain = tcf_chain_get(block, chain_index, false);
2405 	if (!chain) {
2406 		NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2407 		err = -EINVAL;
2408 		goto errout;
2409 	}
2410 
2411 	mutex_lock(&chain->filter_chain_lock);
2412 	tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2413 			       prio, false);
2414 	mutex_unlock(&chain->filter_chain_lock);
2415 	if (!tp || IS_ERR(tp)) {
2416 		NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2417 		err = tp ? PTR_ERR(tp) : -ENOENT;
2418 		goto errout;
2419 	} else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2420 		NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2421 		err = -EINVAL;
2422 		goto errout;
2423 	}
2424 
2425 	fh = tp->ops->get(tp, t->tcm_handle);
2426 
2427 	if (!fh) {
2428 		NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2429 		err = -ENOENT;
2430 	} else {
2431 		err = tfilter_notify(net, skb, n, tp, block, q, parent,
2432 				     fh, RTM_NEWTFILTER, true, rtnl_held);
2433 		if (err < 0)
2434 			NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
2435 	}
2436 
2437 	tfilter_put(tp, fh);
2438 errout:
2439 	if (chain) {
2440 		if (tp && !IS_ERR(tp))
2441 			tcf_proto_put(tp, rtnl_held, NULL);
2442 		tcf_chain_put(chain);
2443 	}
2444 	tcf_block_release(q, block, rtnl_held);
2445 
2446 	if (rtnl_held)
2447 		rtnl_unlock();
2448 
2449 	return err;
2450 }
2451 
2452 struct tcf_dump_args {
2453 	struct tcf_walker w;
2454 	struct sk_buff *skb;
2455 	struct netlink_callback *cb;
2456 	struct tcf_block *block;
2457 	struct Qdisc *q;
2458 	u32 parent;
2459 };
2460 
2461 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
2462 {
2463 	struct tcf_dump_args *a = (void *)arg;
2464 	struct net *net = sock_net(a->skb->sk);
2465 
2466 	return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
2467 			     n, NETLINK_CB(a->cb->skb).portid,
2468 			     a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
2469 			     RTM_NEWTFILTER, true);
2470 }
2471 
2472 static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
2473 			   struct sk_buff *skb, struct netlink_callback *cb,
2474 			   long index_start, long *p_index)
2475 {
2476 	struct net *net = sock_net(skb->sk);
2477 	struct tcf_block *block = chain->block;
2478 	struct tcmsg *tcm = nlmsg_data(cb->nlh);
2479 	struct tcf_proto *tp, *tp_prev;
2480 	struct tcf_dump_args arg;
2481 
2482 	for (tp = __tcf_get_next_proto(chain, NULL);
2483 	     tp;
2484 	     tp_prev = tp,
2485 		     tp = __tcf_get_next_proto(chain, tp),
2486 		     tcf_proto_put(tp_prev, true, NULL),
2487 		     (*p_index)++) {
2488 		if (*p_index < index_start)
2489 			continue;
2490 		if (TC_H_MAJ(tcm->tcm_info) &&
2491 		    TC_H_MAJ(tcm->tcm_info) != tp->prio)
2492 			continue;
2493 		if (TC_H_MIN(tcm->tcm_info) &&
2494 		    TC_H_MIN(tcm->tcm_info) != tp->protocol)
2495 			continue;
2496 		if (*p_index > index_start)
2497 			memset(&cb->args[1], 0,
2498 			       sizeof(cb->args) - sizeof(cb->args[0]));
2499 		if (cb->args[1] == 0) {
2500 			if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
2501 					  NETLINK_CB(cb->skb).portid,
2502 					  cb->nlh->nlmsg_seq, NLM_F_MULTI,
2503 					  RTM_NEWTFILTER, true) <= 0)
2504 				goto errout;
2505 			cb->args[1] = 1;
2506 		}
2507 		if (!tp->ops->walk)
2508 			continue;
2509 		arg.w.fn = tcf_node_dump;
2510 		arg.skb = skb;
2511 		arg.cb = cb;
2512 		arg.block = block;
2513 		arg.q = q;
2514 		arg.parent = parent;
2515 		arg.w.stop = 0;
2516 		arg.w.skip = cb->args[1] - 1;
2517 		arg.w.count = 0;
2518 		arg.w.cookie = cb->args[2];
2519 		tp->ops->walk(tp, &arg.w, true);
2520 		cb->args[2] = arg.w.cookie;
2521 		cb->args[1] = arg.w.count + 1;
2522 		if (arg.w.stop)
2523 			goto errout;
2524 	}
2525 	return true;
2526 
2527 errout:
2528 	tcf_proto_put(tp, true, NULL);
2529 	return false;
2530 }
2531 
2532 /* called with RTNL */
2533 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
2534 {
2535 	struct tcf_chain *chain, *chain_prev;
2536 	struct net *net = sock_net(skb->sk);
2537 	struct nlattr *tca[TCA_MAX + 1];
2538 	struct Qdisc *q = NULL;
2539 	struct tcf_block *block;
2540 	struct tcmsg *tcm = nlmsg_data(cb->nlh);
2541 	long index_start;
2542 	long index;
2543 	u32 parent;
2544 	int err;
2545 
2546 	if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2547 		return skb->len;
2548 
2549 	err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL,
2550 			  cb->extack);
2551 	if (err)
2552 		return err;
2553 
2554 	if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2555 		block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2556 		if (!block)
2557 			goto out;
2558 		/* If we work with block index, q is NULL and parent value
2559 		 * will never be used in the following code. The check
2560 		 * in tcf_fill_node prevents it. However, compiler does not
2561 		 * see that far, so set parent to zero to silence the warning
2562 		 * about parent being uninitialized.
2563 		 */
2564 		parent = 0;
2565 	} else {
2566 		const struct Qdisc_class_ops *cops;
2567 		struct net_device *dev;
2568 		unsigned long cl = 0;
2569 
2570 		dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2571 		if (!dev)
2572 			return skb->len;
2573 
2574 		parent = tcm->tcm_parent;
2575 		if (!parent) {
2576 			q = dev->qdisc;
2577 			parent = q->handle;
2578 		} else {
2579 			q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2580 		}
2581 		if (!q)
2582 			goto out;
2583 		cops = q->ops->cl_ops;
2584 		if (!cops)
2585 			goto out;
2586 		if (!cops->tcf_block)
2587 			goto out;
2588 		if (TC_H_MIN(tcm->tcm_parent)) {
2589 			cl = cops->find(q, tcm->tcm_parent);
2590 			if (cl == 0)
2591 				goto out;
2592 		}
2593 		block = cops->tcf_block(q, cl, NULL);
2594 		if (!block)
2595 			goto out;
2596 		if (tcf_block_shared(block))
2597 			q = NULL;
2598 	}
2599 
2600 	index_start = cb->args[0];
2601 	index = 0;
2602 
2603 	for (chain = __tcf_get_next_chain(block, NULL);
2604 	     chain;
2605 	     chain_prev = chain,
2606 		     chain = __tcf_get_next_chain(block, chain),
2607 		     tcf_chain_put(chain_prev)) {
2608 		if (tca[TCA_CHAIN] &&
2609 		    nla_get_u32(tca[TCA_CHAIN]) != chain->index)
2610 			continue;
2611 		if (!tcf_chain_dump(chain, q, parent, skb, cb,
2612 				    index_start, &index)) {
2613 			tcf_chain_put(chain);
2614 			err = -EMSGSIZE;
2615 			break;
2616 		}
2617 	}
2618 
2619 	if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
2620 		tcf_block_refcnt_put(block, true);
2621 	cb->args[0] = index;
2622 
2623 out:
2624 	/* If we did no progress, the error (EMSGSIZE) is real */
2625 	if (skb->len == 0 && err)
2626 		return err;
2627 	return skb->len;
2628 }
2629 
2630 static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops,
2631 			      void *tmplt_priv, u32 chain_index,
2632 			      struct net *net, struct sk_buff *skb,
2633 			      struct tcf_block *block,
2634 			      u32 portid, u32 seq, u16 flags, int event)
2635 {
2636 	unsigned char *b = skb_tail_pointer(skb);
2637 	const struct tcf_proto_ops *ops;
2638 	struct nlmsghdr *nlh;
2639 	struct tcmsg *tcm;
2640 	void *priv;
2641 
2642 	ops = tmplt_ops;
2643 	priv = tmplt_priv;
2644 
2645 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
2646 	if (!nlh)
2647 		goto out_nlmsg_trim;
2648 	tcm = nlmsg_data(nlh);
2649 	tcm->tcm_family = AF_UNSPEC;
2650 	tcm->tcm__pad1 = 0;
2651 	tcm->tcm__pad2 = 0;
2652 	tcm->tcm_handle = 0;
2653 	if (block->q) {
2654 		tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
2655 		tcm->tcm_parent = block->q->handle;
2656 	} else {
2657 		tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
2658 		tcm->tcm_block_index = block->index;
2659 	}
2660 
2661 	if (nla_put_u32(skb, TCA_CHAIN, chain_index))
2662 		goto nla_put_failure;
2663 
2664 	if (ops) {
2665 		if (nla_put_string(skb, TCA_KIND, ops->kind))
2666 			goto nla_put_failure;
2667 		if (ops->tmplt_dump(skb, net, priv) < 0)
2668 			goto nla_put_failure;
2669 	}
2670 
2671 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2672 	return skb->len;
2673 
2674 out_nlmsg_trim:
2675 nla_put_failure:
2676 	nlmsg_trim(skb, b);
2677 	return -EMSGSIZE;
2678 }
2679 
2680 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
2681 			   u32 seq, u16 flags, int event, bool unicast)
2682 {
2683 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2684 	struct tcf_block *block = chain->block;
2685 	struct net *net = block->net;
2686 	struct sk_buff *skb;
2687 
2688 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2689 	if (!skb)
2690 		return -ENOBUFS;
2691 
2692 	if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
2693 			       chain->index, net, skb, block, portid,
2694 			       seq, flags, event) <= 0) {
2695 		kfree_skb(skb);
2696 		return -EINVAL;
2697 	}
2698 
2699 	if (unicast)
2700 		return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
2701 
2702 	return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
2703 }
2704 
2705 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
2706 				  void *tmplt_priv, u32 chain_index,
2707 				  struct tcf_block *block, struct sk_buff *oskb,
2708 				  u32 seq, u16 flags, bool unicast)
2709 {
2710 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2711 	struct net *net = block->net;
2712 	struct sk_buff *skb;
2713 
2714 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2715 	if (!skb)
2716 		return -ENOBUFS;
2717 
2718 	if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb,
2719 			       block, portid, seq, flags, RTM_DELCHAIN) <= 0) {
2720 		kfree_skb(skb);
2721 		return -EINVAL;
2722 	}
2723 
2724 	if (unicast)
2725 		return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
2726 
2727 	return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
2728 }
2729 
2730 static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
2731 			      struct nlattr **tca,
2732 			      struct netlink_ext_ack *extack)
2733 {
2734 	const struct tcf_proto_ops *ops;
2735 	void *tmplt_priv;
2736 
2737 	/* If kind is not set, user did not specify template. */
2738 	if (!tca[TCA_KIND])
2739 		return 0;
2740 
2741 	ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), true, extack);
2742 	if (IS_ERR(ops))
2743 		return PTR_ERR(ops);
2744 	if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
2745 		NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
2746 		return -EOPNOTSUPP;
2747 	}
2748 
2749 	tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
2750 	if (IS_ERR(tmplt_priv)) {
2751 		module_put(ops->owner);
2752 		return PTR_ERR(tmplt_priv);
2753 	}
2754 	chain->tmplt_ops = ops;
2755 	chain->tmplt_priv = tmplt_priv;
2756 	return 0;
2757 }
2758 
2759 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
2760 			       void *tmplt_priv)
2761 {
2762 	/* If template ops are set, no work to do for us. */
2763 	if (!tmplt_ops)
2764 		return;
2765 
2766 	tmplt_ops->tmplt_destroy(tmplt_priv);
2767 	module_put(tmplt_ops->owner);
2768 }
2769 
2770 /* Add/delete/get a chain */
2771 
2772 static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
2773 			struct netlink_ext_ack *extack)
2774 {
2775 	struct net *net = sock_net(skb->sk);
2776 	struct nlattr *tca[TCA_MAX + 1];
2777 	struct tcmsg *t;
2778 	u32 parent;
2779 	u32 chain_index;
2780 	struct Qdisc *q = NULL;
2781 	struct tcf_chain *chain = NULL;
2782 	struct tcf_block *block;
2783 	unsigned long cl;
2784 	int err;
2785 
2786 	if (n->nlmsg_type != RTM_GETCHAIN &&
2787 	    !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
2788 		return -EPERM;
2789 
2790 replay:
2791 	err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
2792 	if (err < 0)
2793 		return err;
2794 
2795 	t = nlmsg_data(n);
2796 	parent = t->tcm_parent;
2797 	cl = 0;
2798 
2799 	block = tcf_block_find(net, &q, &parent, &cl,
2800 			       t->tcm_ifindex, t->tcm_block_index, extack);
2801 	if (IS_ERR(block))
2802 		return PTR_ERR(block);
2803 
2804 	chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2805 	if (chain_index > TC_ACT_EXT_VAL_MASK) {
2806 		NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2807 		err = -EINVAL;
2808 		goto errout_block;
2809 	}
2810 
2811 	mutex_lock(&block->lock);
2812 	chain = tcf_chain_lookup(block, chain_index);
2813 	if (n->nlmsg_type == RTM_NEWCHAIN) {
2814 		if (chain) {
2815 			if (tcf_chain_held_by_acts_only(chain)) {
2816 				/* The chain exists only because there is
2817 				 * some action referencing it.
2818 				 */
2819 				tcf_chain_hold(chain);
2820 			} else {
2821 				NL_SET_ERR_MSG(extack, "Filter chain already exists");
2822 				err = -EEXIST;
2823 				goto errout_block_locked;
2824 			}
2825 		} else {
2826 			if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2827 				NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
2828 				err = -ENOENT;
2829 				goto errout_block_locked;
2830 			}
2831 			chain = tcf_chain_create(block, chain_index);
2832 			if (!chain) {
2833 				NL_SET_ERR_MSG(extack, "Failed to create filter chain");
2834 				err = -ENOMEM;
2835 				goto errout_block_locked;
2836 			}
2837 		}
2838 	} else {
2839 		if (!chain || tcf_chain_held_by_acts_only(chain)) {
2840 			NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2841 			err = -EINVAL;
2842 			goto errout_block_locked;
2843 		}
2844 		tcf_chain_hold(chain);
2845 	}
2846 
2847 	if (n->nlmsg_type == RTM_NEWCHAIN) {
2848 		/* Modifying chain requires holding parent block lock. In case
2849 		 * the chain was successfully added, take a reference to the
2850 		 * chain. This ensures that an empty chain does not disappear at
2851 		 * the end of this function.
2852 		 */
2853 		tcf_chain_hold(chain);
2854 		chain->explicitly_created = true;
2855 	}
2856 	mutex_unlock(&block->lock);
2857 
2858 	switch (n->nlmsg_type) {
2859 	case RTM_NEWCHAIN:
2860 		err = tc_chain_tmplt_add(chain, net, tca, extack);
2861 		if (err) {
2862 			tcf_chain_put_explicitly_created(chain);
2863 			goto errout;
2864 		}
2865 
2866 		tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
2867 				RTM_NEWCHAIN, false);
2868 		break;
2869 	case RTM_DELCHAIN:
2870 		tfilter_notify_chain(net, skb, block, q, parent, n,
2871 				     chain, RTM_DELTFILTER, true);
2872 		/* Flush the chain first as the user requested chain removal. */
2873 		tcf_chain_flush(chain, true);
2874 		/* In case the chain was successfully deleted, put a reference
2875 		 * to the chain previously taken during addition.
2876 		 */
2877 		tcf_chain_put_explicitly_created(chain);
2878 		break;
2879 	case RTM_GETCHAIN:
2880 		err = tc_chain_notify(chain, skb, n->nlmsg_seq,
2881 				      n->nlmsg_seq, n->nlmsg_type, true);
2882 		if (err < 0)
2883 			NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
2884 		break;
2885 	default:
2886 		err = -EOPNOTSUPP;
2887 		NL_SET_ERR_MSG(extack, "Unsupported message type");
2888 		goto errout;
2889 	}
2890 
2891 errout:
2892 	tcf_chain_put(chain);
2893 errout_block:
2894 	tcf_block_release(q, block, true);
2895 	if (err == -EAGAIN)
2896 		/* Replay the request. */
2897 		goto replay;
2898 	return err;
2899 
2900 errout_block_locked:
2901 	mutex_unlock(&block->lock);
2902 	goto errout_block;
2903 }
2904 
2905 /* called with RTNL */
2906 static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
2907 {
2908 	struct tcf_chain *chain, *chain_prev;
2909 	struct net *net = sock_net(skb->sk);
2910 	struct nlattr *tca[TCA_MAX + 1];
2911 	struct Qdisc *q = NULL;
2912 	struct tcf_block *block;
2913 	struct tcmsg *tcm = nlmsg_data(cb->nlh);
2914 	long index_start;
2915 	long index;
2916 	u32 parent;
2917 	int err;
2918 
2919 	if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2920 		return skb->len;
2921 
2922 	err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, rtm_tca_policy,
2923 			  cb->extack);
2924 	if (err)
2925 		return err;
2926 
2927 	if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2928 		block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2929 		if (!block)
2930 			goto out;
2931 		/* If we work with block index, q is NULL and parent value
2932 		 * will never be used in the following code. The check
2933 		 * in tcf_fill_node prevents it. However, compiler does not
2934 		 * see that far, so set parent to zero to silence the warning
2935 		 * about parent being uninitialized.
2936 		 */
2937 		parent = 0;
2938 	} else {
2939 		const struct Qdisc_class_ops *cops;
2940 		struct net_device *dev;
2941 		unsigned long cl = 0;
2942 
2943 		dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2944 		if (!dev)
2945 			return skb->len;
2946 
2947 		parent = tcm->tcm_parent;
2948 		if (!parent) {
2949 			q = dev->qdisc;
2950 			parent = q->handle;
2951 		} else {
2952 			q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2953 		}
2954 		if (!q)
2955 			goto out;
2956 		cops = q->ops->cl_ops;
2957 		if (!cops)
2958 			goto out;
2959 		if (!cops->tcf_block)
2960 			goto out;
2961 		if (TC_H_MIN(tcm->tcm_parent)) {
2962 			cl = cops->find(q, tcm->tcm_parent);
2963 			if (cl == 0)
2964 				goto out;
2965 		}
2966 		block = cops->tcf_block(q, cl, NULL);
2967 		if (!block)
2968 			goto out;
2969 		if (tcf_block_shared(block))
2970 			q = NULL;
2971 	}
2972 
2973 	index_start = cb->args[0];
2974 	index = 0;
2975 
2976 	for (chain = __tcf_get_next_chain(block, NULL);
2977 	     chain;
2978 	     chain_prev = chain,
2979 		     chain = __tcf_get_next_chain(block, chain),
2980 		     tcf_chain_put(chain_prev)) {
2981 		if ((tca[TCA_CHAIN] &&
2982 		     nla_get_u32(tca[TCA_CHAIN]) != chain->index))
2983 			continue;
2984 		if (index < index_start) {
2985 			index++;
2986 			continue;
2987 		}
2988 		err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
2989 					 chain->index, net, skb, block,
2990 					 NETLINK_CB(cb->skb).portid,
2991 					 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2992 					 RTM_NEWCHAIN);
2993 		if (err <= 0) {
2994 			tcf_chain_put(chain);
2995 			break;
2996 		}
2997 		index++;
2998 	}
2999 
3000 	if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
3001 		tcf_block_refcnt_put(block, true);
3002 	cb->args[0] = index;
3003 
3004 out:
3005 	/* If we did no progress, the error (EMSGSIZE) is real */
3006 	if (skb->len == 0 && err)
3007 		return err;
3008 	return skb->len;
3009 }
3010 
3011 void tcf_exts_destroy(struct tcf_exts *exts)
3012 {
3013 #ifdef CONFIG_NET_CLS_ACT
3014 	tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
3015 	kfree(exts->actions);
3016 	exts->nr_actions = 0;
3017 #endif
3018 }
3019 EXPORT_SYMBOL(tcf_exts_destroy);
3020 
3021 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
3022 		      struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
3023 		      bool rtnl_held, struct netlink_ext_ack *extack)
3024 {
3025 #ifdef CONFIG_NET_CLS_ACT
3026 	{
3027 		struct tc_action *act;
3028 		size_t attr_size = 0;
3029 
3030 		if (exts->police && tb[exts->police]) {
3031 			act = tcf_action_init_1(net, tp, tb[exts->police],
3032 						rate_tlv, "police", ovr,
3033 						TCA_ACT_BIND, rtnl_held,
3034 						extack);
3035 			if (IS_ERR(act))
3036 				return PTR_ERR(act);
3037 
3038 			act->type = exts->type = TCA_OLD_COMPAT;
3039 			exts->actions[0] = act;
3040 			exts->nr_actions = 1;
3041 		} else if (exts->action && tb[exts->action]) {
3042 			int err;
3043 
3044 			err = tcf_action_init(net, tp, tb[exts->action],
3045 					      rate_tlv, NULL, ovr, TCA_ACT_BIND,
3046 					      exts->actions, &attr_size,
3047 					      rtnl_held, extack);
3048 			if (err < 0)
3049 				return err;
3050 			exts->nr_actions = err;
3051 		}
3052 		exts->net = net;
3053 	}
3054 #else
3055 	if ((exts->action && tb[exts->action]) ||
3056 	    (exts->police && tb[exts->police])) {
3057 		NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
3058 		return -EOPNOTSUPP;
3059 	}
3060 #endif
3061 
3062 	return 0;
3063 }
3064 EXPORT_SYMBOL(tcf_exts_validate);
3065 
3066 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
3067 {
3068 #ifdef CONFIG_NET_CLS_ACT
3069 	struct tcf_exts old = *dst;
3070 
3071 	*dst = *src;
3072 	tcf_exts_destroy(&old);
3073 #endif
3074 }
3075 EXPORT_SYMBOL(tcf_exts_change);
3076 
3077 #ifdef CONFIG_NET_CLS_ACT
3078 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
3079 {
3080 	if (exts->nr_actions == 0)
3081 		return NULL;
3082 	else
3083 		return exts->actions[0];
3084 }
3085 #endif
3086 
3087 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
3088 {
3089 #ifdef CONFIG_NET_CLS_ACT
3090 	struct nlattr *nest;
3091 
3092 	if (exts->action && tcf_exts_has_actions(exts)) {
3093 		/*
3094 		 * again for backward compatible mode - we want
3095 		 * to work with both old and new modes of entering
3096 		 * tc data even if iproute2  was newer - jhs
3097 		 */
3098 		if (exts->type != TCA_OLD_COMPAT) {
3099 			nest = nla_nest_start(skb, exts->action);
3100 			if (nest == NULL)
3101 				goto nla_put_failure;
3102 
3103 			if (tcf_action_dump(skb, exts->actions, 0, 0) < 0)
3104 				goto nla_put_failure;
3105 			nla_nest_end(skb, nest);
3106 		} else if (exts->police) {
3107 			struct tc_action *act = tcf_exts_first_act(exts);
3108 			nest = nla_nest_start(skb, exts->police);
3109 			if (nest == NULL || !act)
3110 				goto nla_put_failure;
3111 			if (tcf_action_dump_old(skb, act, 0, 0) < 0)
3112 				goto nla_put_failure;
3113 			nla_nest_end(skb, nest);
3114 		}
3115 	}
3116 	return 0;
3117 
3118 nla_put_failure:
3119 	nla_nest_cancel(skb, nest);
3120 	return -1;
3121 #else
3122 	return 0;
3123 #endif
3124 }
3125 EXPORT_SYMBOL(tcf_exts_dump);
3126 
3127 
3128 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
3129 {
3130 #ifdef CONFIG_NET_CLS_ACT
3131 	struct tc_action *a = tcf_exts_first_act(exts);
3132 	if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
3133 		return -1;
3134 #endif
3135 	return 0;
3136 }
3137 EXPORT_SYMBOL(tcf_exts_dump_stats);
3138 
3139 int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3140 		     void *type_data, bool err_stop)
3141 {
3142 	struct tcf_block_cb *block_cb;
3143 	int ok_count = 0;
3144 	int err;
3145 
3146 	/* Make sure all netdevs sharing this block are offload-capable. */
3147 	if (block->nooffloaddevcnt && err_stop)
3148 		return -EOPNOTSUPP;
3149 
3150 	list_for_each_entry(block_cb, &block->cb_list, list) {
3151 		err = block_cb->cb(type, type_data, block_cb->cb_priv);
3152 		if (err) {
3153 			if (err_stop)
3154 				return err;
3155 		} else {
3156 			ok_count++;
3157 		}
3158 	}
3159 	return ok_count;
3160 }
3161 EXPORT_SYMBOL(tc_setup_cb_call);
3162 
3163 int tc_setup_flow_action(struct flow_action *flow_action,
3164 			 const struct tcf_exts *exts)
3165 {
3166 	const struct tc_action *act;
3167 	int i, j, k;
3168 
3169 	if (!exts)
3170 		return 0;
3171 
3172 	j = 0;
3173 	tcf_exts_for_each_action(i, act, exts) {
3174 		struct flow_action_entry *entry;
3175 
3176 		entry = &flow_action->entries[j];
3177 		if (is_tcf_gact_ok(act)) {
3178 			entry->id = FLOW_ACTION_ACCEPT;
3179 		} else if (is_tcf_gact_shot(act)) {
3180 			entry->id = FLOW_ACTION_DROP;
3181 		} else if (is_tcf_gact_trap(act)) {
3182 			entry->id = FLOW_ACTION_TRAP;
3183 		} else if (is_tcf_gact_goto_chain(act)) {
3184 			entry->id = FLOW_ACTION_GOTO;
3185 			entry->chain_index = tcf_gact_goto_chain_index(act);
3186 		} else if (is_tcf_mirred_egress_redirect(act)) {
3187 			entry->id = FLOW_ACTION_REDIRECT;
3188 			entry->dev = tcf_mirred_dev(act);
3189 		} else if (is_tcf_mirred_egress_mirror(act)) {
3190 			entry->id = FLOW_ACTION_MIRRED;
3191 			entry->dev = tcf_mirred_dev(act);
3192 		} else if (is_tcf_vlan(act)) {
3193 			switch (tcf_vlan_action(act)) {
3194 			case TCA_VLAN_ACT_PUSH:
3195 				entry->id = FLOW_ACTION_VLAN_PUSH;
3196 				entry->vlan.vid = tcf_vlan_push_vid(act);
3197 				entry->vlan.proto = tcf_vlan_push_proto(act);
3198 				entry->vlan.prio = tcf_vlan_push_prio(act);
3199 				break;
3200 			case TCA_VLAN_ACT_POP:
3201 				entry->id = FLOW_ACTION_VLAN_POP;
3202 				break;
3203 			case TCA_VLAN_ACT_MODIFY:
3204 				entry->id = FLOW_ACTION_VLAN_MANGLE;
3205 				entry->vlan.vid = tcf_vlan_push_vid(act);
3206 				entry->vlan.proto = tcf_vlan_push_proto(act);
3207 				entry->vlan.prio = tcf_vlan_push_prio(act);
3208 				break;
3209 			default:
3210 				goto err_out;
3211 			}
3212 		} else if (is_tcf_tunnel_set(act)) {
3213 			entry->id = FLOW_ACTION_TUNNEL_ENCAP;
3214 			entry->tunnel = tcf_tunnel_info(act);
3215 		} else if (is_tcf_tunnel_release(act)) {
3216 			entry->id = FLOW_ACTION_TUNNEL_DECAP;
3217 			entry->tunnel = tcf_tunnel_info(act);
3218 		} else if (is_tcf_pedit(act)) {
3219 			for (k = 0; k < tcf_pedit_nkeys(act); k++) {
3220 				switch (tcf_pedit_cmd(act, k)) {
3221 				case TCA_PEDIT_KEY_EX_CMD_SET:
3222 					entry->id = FLOW_ACTION_MANGLE;
3223 					break;
3224 				case TCA_PEDIT_KEY_EX_CMD_ADD:
3225 					entry->id = FLOW_ACTION_ADD;
3226 					break;
3227 				default:
3228 					goto err_out;
3229 				}
3230 				entry->mangle.htype = tcf_pedit_htype(act, k);
3231 				entry->mangle.mask = tcf_pedit_mask(act, k);
3232 				entry->mangle.val = tcf_pedit_val(act, k);
3233 				entry->mangle.offset = tcf_pedit_offset(act, k);
3234 				entry = &flow_action->entries[++j];
3235 			}
3236 		} else if (is_tcf_csum(act)) {
3237 			entry->id = FLOW_ACTION_CSUM;
3238 			entry->csum_flags = tcf_csum_update_flags(act);
3239 		} else if (is_tcf_skbedit_mark(act)) {
3240 			entry->id = FLOW_ACTION_MARK;
3241 			entry->mark = tcf_skbedit_mark(act);
3242 		} else {
3243 			goto err_out;
3244 		}
3245 
3246 		if (!is_tcf_pedit(act))
3247 			j++;
3248 	}
3249 	return 0;
3250 err_out:
3251 	return -EOPNOTSUPP;
3252 }
3253 EXPORT_SYMBOL(tc_setup_flow_action);
3254 
3255 unsigned int tcf_exts_num_actions(struct tcf_exts *exts)
3256 {
3257 	unsigned int num_acts = 0;
3258 	struct tc_action *act;
3259 	int i;
3260 
3261 	tcf_exts_for_each_action(i, act, exts) {
3262 		if (is_tcf_pedit(act))
3263 			num_acts += tcf_pedit_nkeys(act);
3264 		else
3265 			num_acts++;
3266 	}
3267 	return num_acts;
3268 }
3269 EXPORT_SYMBOL(tcf_exts_num_actions);
3270 
3271 static __net_init int tcf_net_init(struct net *net)
3272 {
3273 	struct tcf_net *tn = net_generic(net, tcf_net_id);
3274 
3275 	spin_lock_init(&tn->idr_lock);
3276 	idr_init(&tn->idr);
3277 	return 0;
3278 }
3279 
3280 static void __net_exit tcf_net_exit(struct net *net)
3281 {
3282 	struct tcf_net *tn = net_generic(net, tcf_net_id);
3283 
3284 	idr_destroy(&tn->idr);
3285 }
3286 
3287 static struct pernet_operations tcf_net_ops = {
3288 	.init = tcf_net_init,
3289 	.exit = tcf_net_exit,
3290 	.id   = &tcf_net_id,
3291 	.size = sizeof(struct tcf_net),
3292 };
3293 
3294 static int __init tc_filter_init(void)
3295 {
3296 	int err;
3297 
3298 	tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
3299 	if (!tc_filter_wq)
3300 		return -ENOMEM;
3301 
3302 	err = register_pernet_subsys(&tcf_net_ops);
3303 	if (err)
3304 		goto err_register_pernet_subsys;
3305 
3306 	err = rhashtable_init(&indr_setup_block_ht,
3307 			      &tc_indr_setup_block_ht_params);
3308 	if (err)
3309 		goto err_rhash_setup_block_ht;
3310 
3311 	rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL,
3312 		      RTNL_FLAG_DOIT_UNLOCKED);
3313 	rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL,
3314 		      RTNL_FLAG_DOIT_UNLOCKED);
3315 	rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
3316 		      tc_dump_tfilter, RTNL_FLAG_DOIT_UNLOCKED);
3317 	rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
3318 	rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
3319 	rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
3320 		      tc_dump_chain, 0);
3321 
3322 	return 0;
3323 
3324 err_rhash_setup_block_ht:
3325 	unregister_pernet_subsys(&tcf_net_ops);
3326 err_register_pernet_subsys:
3327 	destroy_workqueue(tc_filter_wq);
3328 	return err;
3329 }
3330 
3331 subsys_initcall(tc_filter_init);
3332