xref: /openbmc/linux/net/sched/cls_api.c (revision d699090510c3223641a23834b4710e2d4309a6ad)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net/sched/cls_api.c	Packet classifier API.
4  *
5  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6  *
7  * Changes:
8  *
9  * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
10  */
11 
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/string.h>
16 #include <linux/errno.h>
17 #include <linux/err.h>
18 #include <linux/skbuff.h>
19 #include <linux/init.h>
20 #include <linux/kmod.h>
21 #include <linux/slab.h>
22 #include <linux/idr.h>
23 #include <linux/jhash.h>
24 #include <linux/rculist.h>
25 #include <linux/rhashtable.h>
26 #include <net/net_namespace.h>
27 #include <net/sock.h>
28 #include <net/netlink.h>
29 #include <net/pkt_sched.h>
30 #include <net/pkt_cls.h>
31 #include <net/tc_act/tc_pedit.h>
32 #include <net/tc_act/tc_mirred.h>
33 #include <net/tc_act/tc_vlan.h>
34 #include <net/tc_act/tc_tunnel_key.h>
35 #include <net/tc_act/tc_csum.h>
36 #include <net/tc_act/tc_gact.h>
37 #include <net/tc_act/tc_police.h>
38 #include <net/tc_act/tc_sample.h>
39 #include <net/tc_act/tc_skbedit.h>
40 #include <net/tc_act/tc_ct.h>
41 #include <net/tc_act/tc_mpls.h>
42 #include <net/tc_act/tc_gate.h>
43 #include <net/flow_offload.h>
44 #include <net/tc_wrapper.h>
45 
46 /* The list of all installed classifier types */
47 static LIST_HEAD(tcf_proto_base);
48 
49 /* Protects list of registered TC modules. It is pure SMP lock. */
50 static DEFINE_RWLOCK(cls_mod_lock);
51 
52 static struct xarray tcf_exts_miss_cookies_xa;
53 struct tcf_exts_miss_cookie_node {
54 	const struct tcf_chain *chain;
55 	const struct tcf_proto *tp;
56 	const struct tcf_exts *exts;
57 	u32 chain_index;
58 	u32 tp_prio;
59 	u32 handle;
60 	u32 miss_cookie_base;
61 	struct rcu_head rcu;
62 };
63 
64 /* Each tc action entry cookie will be comprised of 32bit miss_cookie_base +
65  * action index in the exts tc actions array.
66  */
67 union tcf_exts_miss_cookie {
68 	struct {
69 		u32 miss_cookie_base;
70 		u32 act_index;
71 	};
72 	u64 miss_cookie;
73 };
74 
75 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
76 static int
tcf_exts_miss_cookie_base_alloc(struct tcf_exts * exts,struct tcf_proto * tp,u32 handle)77 tcf_exts_miss_cookie_base_alloc(struct tcf_exts *exts, struct tcf_proto *tp,
78 				u32 handle)
79 {
80 	struct tcf_exts_miss_cookie_node *n;
81 	static u32 next;
82 	int err;
83 
84 	if (WARN_ON(!handle || !tp->ops->get_exts))
85 		return -EINVAL;
86 
87 	n = kzalloc(sizeof(*n), GFP_KERNEL);
88 	if (!n)
89 		return -ENOMEM;
90 
91 	n->chain_index = tp->chain->index;
92 	n->chain = tp->chain;
93 	n->tp_prio = tp->prio;
94 	n->tp = tp;
95 	n->exts = exts;
96 	n->handle = handle;
97 
98 	err = xa_alloc_cyclic(&tcf_exts_miss_cookies_xa, &n->miss_cookie_base,
99 			      n, xa_limit_32b, &next, GFP_KERNEL);
100 	if (err < 0)
101 		goto err_xa_alloc;
102 
103 	exts->miss_cookie_node = n;
104 	return 0;
105 
106 err_xa_alloc:
107 	kfree(n);
108 	return err;
109 }
110 
tcf_exts_miss_cookie_base_destroy(struct tcf_exts * exts)111 static void tcf_exts_miss_cookie_base_destroy(struct tcf_exts *exts)
112 {
113 	struct tcf_exts_miss_cookie_node *n;
114 
115 	if (!exts->miss_cookie_node)
116 		return;
117 
118 	n = exts->miss_cookie_node;
119 	xa_erase(&tcf_exts_miss_cookies_xa, n->miss_cookie_base);
120 	kfree_rcu(n, rcu);
121 }
122 
123 static struct tcf_exts_miss_cookie_node *
tcf_exts_miss_cookie_lookup(u64 miss_cookie,int * act_index)124 tcf_exts_miss_cookie_lookup(u64 miss_cookie, int *act_index)
125 {
126 	union tcf_exts_miss_cookie mc = { .miss_cookie = miss_cookie, };
127 
128 	*act_index = mc.act_index;
129 	return xa_load(&tcf_exts_miss_cookies_xa, mc.miss_cookie_base);
130 }
131 #else /* IS_ENABLED(CONFIG_NET_TC_SKB_EXT) */
132 static int
tcf_exts_miss_cookie_base_alloc(struct tcf_exts * exts,struct tcf_proto * tp,u32 handle)133 tcf_exts_miss_cookie_base_alloc(struct tcf_exts *exts, struct tcf_proto *tp,
134 				u32 handle)
135 {
136 	return 0;
137 }
138 
tcf_exts_miss_cookie_base_destroy(struct tcf_exts * exts)139 static void tcf_exts_miss_cookie_base_destroy(struct tcf_exts *exts)
140 {
141 }
142 #endif /* IS_ENABLED(CONFIG_NET_TC_SKB_EXT) */
143 
tcf_exts_miss_cookie_get(u32 miss_cookie_base,int act_index)144 static u64 tcf_exts_miss_cookie_get(u32 miss_cookie_base, int act_index)
145 {
146 	union tcf_exts_miss_cookie mc = { .act_index = act_index, };
147 
148 	if (!miss_cookie_base)
149 		return 0;
150 
151 	mc.miss_cookie_base = miss_cookie_base;
152 	return mc.miss_cookie;
153 }
154 
155 #ifdef CONFIG_NET_CLS_ACT
156 DEFINE_STATIC_KEY_FALSE(tc_skb_ext_tc);
157 EXPORT_SYMBOL(tc_skb_ext_tc);
158 
tc_skb_ext_tc_enable(void)159 void tc_skb_ext_tc_enable(void)
160 {
161 	static_branch_inc(&tc_skb_ext_tc);
162 }
163 EXPORT_SYMBOL(tc_skb_ext_tc_enable);
164 
tc_skb_ext_tc_disable(void)165 void tc_skb_ext_tc_disable(void)
166 {
167 	static_branch_dec(&tc_skb_ext_tc);
168 }
169 EXPORT_SYMBOL(tc_skb_ext_tc_disable);
170 #endif
171 
destroy_obj_hashfn(const struct tcf_proto * tp)172 static u32 destroy_obj_hashfn(const struct tcf_proto *tp)
173 {
174 	return jhash_3words(tp->chain->index, tp->prio,
175 			    (__force __u32)tp->protocol, 0);
176 }
177 
tcf_proto_signal_destroying(struct tcf_chain * chain,struct tcf_proto * tp)178 static void tcf_proto_signal_destroying(struct tcf_chain *chain,
179 					struct tcf_proto *tp)
180 {
181 	struct tcf_block *block = chain->block;
182 
183 	mutex_lock(&block->proto_destroy_lock);
184 	hash_add_rcu(block->proto_destroy_ht, &tp->destroy_ht_node,
185 		     destroy_obj_hashfn(tp));
186 	mutex_unlock(&block->proto_destroy_lock);
187 }
188 
tcf_proto_cmp(const struct tcf_proto * tp1,const struct tcf_proto * tp2)189 static bool tcf_proto_cmp(const struct tcf_proto *tp1,
190 			  const struct tcf_proto *tp2)
191 {
192 	return tp1->chain->index == tp2->chain->index &&
193 	       tp1->prio == tp2->prio &&
194 	       tp1->protocol == tp2->protocol;
195 }
196 
tcf_proto_exists_destroying(struct tcf_chain * chain,struct tcf_proto * tp)197 static bool tcf_proto_exists_destroying(struct tcf_chain *chain,
198 					struct tcf_proto *tp)
199 {
200 	u32 hash = destroy_obj_hashfn(tp);
201 	struct tcf_proto *iter;
202 	bool found = false;
203 
204 	rcu_read_lock();
205 	hash_for_each_possible_rcu(chain->block->proto_destroy_ht, iter,
206 				   destroy_ht_node, hash) {
207 		if (tcf_proto_cmp(tp, iter)) {
208 			found = true;
209 			break;
210 		}
211 	}
212 	rcu_read_unlock();
213 
214 	return found;
215 }
216 
217 static void
tcf_proto_signal_destroyed(struct tcf_chain * chain,struct tcf_proto * tp)218 tcf_proto_signal_destroyed(struct tcf_chain *chain, struct tcf_proto *tp)
219 {
220 	struct tcf_block *block = chain->block;
221 
222 	mutex_lock(&block->proto_destroy_lock);
223 	if (hash_hashed(&tp->destroy_ht_node))
224 		hash_del_rcu(&tp->destroy_ht_node);
225 	mutex_unlock(&block->proto_destroy_lock);
226 }
227 
228 /* Find classifier type by string name */
229 
__tcf_proto_lookup_ops(const char * kind)230 static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind)
231 {
232 	const struct tcf_proto_ops *t, *res = NULL;
233 
234 	if (kind) {
235 		read_lock(&cls_mod_lock);
236 		list_for_each_entry(t, &tcf_proto_base, head) {
237 			if (strcmp(kind, t->kind) == 0) {
238 				if (try_module_get(t->owner))
239 					res = t;
240 				break;
241 			}
242 		}
243 		read_unlock(&cls_mod_lock);
244 	}
245 	return res;
246 }
247 
248 static const struct tcf_proto_ops *
tcf_proto_lookup_ops(const char * kind,bool rtnl_held,struct netlink_ext_ack * extack)249 tcf_proto_lookup_ops(const char *kind, bool rtnl_held,
250 		     struct netlink_ext_ack *extack)
251 {
252 	const struct tcf_proto_ops *ops;
253 
254 	ops = __tcf_proto_lookup_ops(kind);
255 	if (ops)
256 		return ops;
257 #ifdef CONFIG_MODULES
258 	if (rtnl_held)
259 		rtnl_unlock();
260 	request_module("cls_%s", kind);
261 	if (rtnl_held)
262 		rtnl_lock();
263 	ops = __tcf_proto_lookup_ops(kind);
264 	/* We dropped the RTNL semaphore in order to perform
265 	 * the module load. So, even if we succeeded in loading
266 	 * the module we have to replay the request. We indicate
267 	 * this using -EAGAIN.
268 	 */
269 	if (ops) {
270 		module_put(ops->owner);
271 		return ERR_PTR(-EAGAIN);
272 	}
273 #endif
274 	NL_SET_ERR_MSG(extack, "TC classifier not found");
275 	return ERR_PTR(-ENOENT);
276 }
277 
278 /* Register(unregister) new classifier type */
279 
register_tcf_proto_ops(struct tcf_proto_ops * ops)280 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
281 {
282 	struct tcf_proto_ops *t;
283 	int rc = -EEXIST;
284 
285 	write_lock(&cls_mod_lock);
286 	list_for_each_entry(t, &tcf_proto_base, head)
287 		if (!strcmp(ops->kind, t->kind))
288 			goto out;
289 
290 	list_add_tail(&ops->head, &tcf_proto_base);
291 	rc = 0;
292 out:
293 	write_unlock(&cls_mod_lock);
294 	return rc;
295 }
296 EXPORT_SYMBOL(register_tcf_proto_ops);
297 
298 static struct workqueue_struct *tc_filter_wq;
299 
unregister_tcf_proto_ops(struct tcf_proto_ops * ops)300 void unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
301 {
302 	struct tcf_proto_ops *t;
303 	int rc = -ENOENT;
304 
305 	/* Wait for outstanding call_rcu()s, if any, from a
306 	 * tcf_proto_ops's destroy() handler.
307 	 */
308 	rcu_barrier();
309 	flush_workqueue(tc_filter_wq);
310 
311 	write_lock(&cls_mod_lock);
312 	list_for_each_entry(t, &tcf_proto_base, head) {
313 		if (t == ops) {
314 			list_del(&t->head);
315 			rc = 0;
316 			break;
317 		}
318 	}
319 	write_unlock(&cls_mod_lock);
320 
321 	WARN(rc, "unregister tc filter kind(%s) failed %d\n", ops->kind, rc);
322 }
323 EXPORT_SYMBOL(unregister_tcf_proto_ops);
324 
tcf_queue_work(struct rcu_work * rwork,work_func_t func)325 bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
326 {
327 	INIT_RCU_WORK(rwork, func);
328 	return queue_rcu_work(tc_filter_wq, rwork);
329 }
330 EXPORT_SYMBOL(tcf_queue_work);
331 
332 /* Select new prio value from the range, managed by kernel. */
333 
tcf_auto_prio(struct tcf_proto * tp)334 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
335 {
336 	u32 first = TC_H_MAKE(0xC0000000U, 0U);
337 
338 	if (tp)
339 		first = tp->prio - 1;
340 
341 	return TC_H_MAJ(first);
342 }
343 
tcf_proto_check_kind(struct nlattr * kind,char * name)344 static bool tcf_proto_check_kind(struct nlattr *kind, char *name)
345 {
346 	if (kind)
347 		return nla_strscpy(name, kind, IFNAMSIZ) < 0;
348 	memset(name, 0, IFNAMSIZ);
349 	return false;
350 }
351 
tcf_proto_is_unlocked(const char * kind)352 static bool tcf_proto_is_unlocked(const char *kind)
353 {
354 	const struct tcf_proto_ops *ops;
355 	bool ret;
356 
357 	if (strlen(kind) == 0)
358 		return false;
359 
360 	ops = tcf_proto_lookup_ops(kind, false, NULL);
361 	/* On error return false to take rtnl lock. Proto lookup/create
362 	 * functions will perform lookup again and properly handle errors.
363 	 */
364 	if (IS_ERR(ops))
365 		return false;
366 
367 	ret = !!(ops->flags & TCF_PROTO_OPS_DOIT_UNLOCKED);
368 	module_put(ops->owner);
369 	return ret;
370 }
371 
tcf_proto_create(const char * kind,u32 protocol,u32 prio,struct tcf_chain * chain,bool rtnl_held,struct netlink_ext_ack * extack)372 static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
373 					  u32 prio, struct tcf_chain *chain,
374 					  bool rtnl_held,
375 					  struct netlink_ext_ack *extack)
376 {
377 	struct tcf_proto *tp;
378 	int err;
379 
380 	tp = kzalloc(sizeof(*tp), GFP_KERNEL);
381 	if (!tp)
382 		return ERR_PTR(-ENOBUFS);
383 
384 	tp->ops = tcf_proto_lookup_ops(kind, rtnl_held, extack);
385 	if (IS_ERR(tp->ops)) {
386 		err = PTR_ERR(tp->ops);
387 		goto errout;
388 	}
389 	tp->classify = tp->ops->classify;
390 	tp->protocol = protocol;
391 	tp->prio = prio;
392 	tp->chain = chain;
393 	spin_lock_init(&tp->lock);
394 	refcount_set(&tp->refcnt, 1);
395 
396 	err = tp->ops->init(tp);
397 	if (err) {
398 		module_put(tp->ops->owner);
399 		goto errout;
400 	}
401 	return tp;
402 
403 errout:
404 	kfree(tp);
405 	return ERR_PTR(err);
406 }
407 
tcf_proto_get(struct tcf_proto * tp)408 static void tcf_proto_get(struct tcf_proto *tp)
409 {
410 	refcount_inc(&tp->refcnt);
411 }
412 
413 static void tcf_chain_put(struct tcf_chain *chain);
414 
tcf_proto_destroy(struct tcf_proto * tp,bool rtnl_held,bool sig_destroy,struct netlink_ext_ack * extack)415 static void tcf_proto_destroy(struct tcf_proto *tp, bool rtnl_held,
416 			      bool sig_destroy, struct netlink_ext_ack *extack)
417 {
418 	tp->ops->destroy(tp, rtnl_held, extack);
419 	if (sig_destroy)
420 		tcf_proto_signal_destroyed(tp->chain, tp);
421 	tcf_chain_put(tp->chain);
422 	module_put(tp->ops->owner);
423 	kfree_rcu(tp, rcu);
424 }
425 
tcf_proto_put(struct tcf_proto * tp,bool rtnl_held,struct netlink_ext_ack * extack)426 static void tcf_proto_put(struct tcf_proto *tp, bool rtnl_held,
427 			  struct netlink_ext_ack *extack)
428 {
429 	if (refcount_dec_and_test(&tp->refcnt))
430 		tcf_proto_destroy(tp, rtnl_held, true, extack);
431 }
432 
tcf_proto_check_delete(struct tcf_proto * tp)433 static bool tcf_proto_check_delete(struct tcf_proto *tp)
434 {
435 	if (tp->ops->delete_empty)
436 		return tp->ops->delete_empty(tp);
437 
438 	tp->deleting = true;
439 	return tp->deleting;
440 }
441 
tcf_proto_mark_delete(struct tcf_proto * tp)442 static void tcf_proto_mark_delete(struct tcf_proto *tp)
443 {
444 	spin_lock(&tp->lock);
445 	tp->deleting = true;
446 	spin_unlock(&tp->lock);
447 }
448 
tcf_proto_is_deleting(struct tcf_proto * tp)449 static bool tcf_proto_is_deleting(struct tcf_proto *tp)
450 {
451 	bool deleting;
452 
453 	spin_lock(&tp->lock);
454 	deleting = tp->deleting;
455 	spin_unlock(&tp->lock);
456 
457 	return deleting;
458 }
459 
460 #define ASSERT_BLOCK_LOCKED(block)					\
461 	lockdep_assert_held(&(block)->lock)
462 
463 struct tcf_filter_chain_list_item {
464 	struct list_head list;
465 	tcf_chain_head_change_t *chain_head_change;
466 	void *chain_head_change_priv;
467 };
468 
tcf_chain_create(struct tcf_block * block,u32 chain_index)469 static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
470 					  u32 chain_index)
471 {
472 	struct tcf_chain *chain;
473 
474 	ASSERT_BLOCK_LOCKED(block);
475 
476 	chain = kzalloc(sizeof(*chain), GFP_KERNEL);
477 	if (!chain)
478 		return NULL;
479 	list_add_tail_rcu(&chain->list, &block->chain_list);
480 	mutex_init(&chain->filter_chain_lock);
481 	chain->block = block;
482 	chain->index = chain_index;
483 	chain->refcnt = 1;
484 	if (!chain->index)
485 		block->chain0.chain = chain;
486 	return chain;
487 }
488 
tcf_chain_head_change_item(struct tcf_filter_chain_list_item * item,struct tcf_proto * tp_head)489 static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
490 				       struct tcf_proto *tp_head)
491 {
492 	if (item->chain_head_change)
493 		item->chain_head_change(tp_head, item->chain_head_change_priv);
494 }
495 
tcf_chain0_head_change(struct tcf_chain * chain,struct tcf_proto * tp_head)496 static void tcf_chain0_head_change(struct tcf_chain *chain,
497 				   struct tcf_proto *tp_head)
498 {
499 	struct tcf_filter_chain_list_item *item;
500 	struct tcf_block *block = chain->block;
501 
502 	if (chain->index)
503 		return;
504 
505 	mutex_lock(&block->lock);
506 	list_for_each_entry(item, &block->chain0.filter_chain_list, list)
507 		tcf_chain_head_change_item(item, tp_head);
508 	mutex_unlock(&block->lock);
509 }
510 
511 /* Returns true if block can be safely freed. */
512 
tcf_chain_detach(struct tcf_chain * chain)513 static bool tcf_chain_detach(struct tcf_chain *chain)
514 {
515 	struct tcf_block *block = chain->block;
516 
517 	ASSERT_BLOCK_LOCKED(block);
518 
519 	list_del_rcu(&chain->list);
520 	if (!chain->index)
521 		block->chain0.chain = NULL;
522 
523 	if (list_empty(&block->chain_list) &&
524 	    refcount_read(&block->refcnt) == 0)
525 		return true;
526 
527 	return false;
528 }
529 
tcf_block_destroy(struct tcf_block * block)530 static void tcf_block_destroy(struct tcf_block *block)
531 {
532 	mutex_destroy(&block->lock);
533 	mutex_destroy(&block->proto_destroy_lock);
534 	kfree_rcu(block, rcu);
535 }
536 
tcf_chain_destroy(struct tcf_chain * chain,bool free_block)537 static void tcf_chain_destroy(struct tcf_chain *chain, bool free_block)
538 {
539 	struct tcf_block *block = chain->block;
540 
541 	mutex_destroy(&chain->filter_chain_lock);
542 	kfree_rcu(chain, rcu);
543 	if (free_block)
544 		tcf_block_destroy(block);
545 }
546 
tcf_chain_hold(struct tcf_chain * chain)547 static void tcf_chain_hold(struct tcf_chain *chain)
548 {
549 	ASSERT_BLOCK_LOCKED(chain->block);
550 
551 	++chain->refcnt;
552 }
553 
tcf_chain_held_by_acts_only(struct tcf_chain * chain)554 static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
555 {
556 	ASSERT_BLOCK_LOCKED(chain->block);
557 
558 	/* In case all the references are action references, this
559 	 * chain should not be shown to the user.
560 	 */
561 	return chain->refcnt == chain->action_refcnt;
562 }
563 
tcf_chain_lookup(struct tcf_block * block,u32 chain_index)564 static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
565 					  u32 chain_index)
566 {
567 	struct tcf_chain *chain;
568 
569 	ASSERT_BLOCK_LOCKED(block);
570 
571 	list_for_each_entry(chain, &block->chain_list, list) {
572 		if (chain->index == chain_index)
573 			return chain;
574 	}
575 	return NULL;
576 }
577 
578 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
tcf_chain_lookup_rcu(const struct tcf_block * block,u32 chain_index)579 static struct tcf_chain *tcf_chain_lookup_rcu(const struct tcf_block *block,
580 					      u32 chain_index)
581 {
582 	struct tcf_chain *chain;
583 
584 	list_for_each_entry_rcu(chain, &block->chain_list, list) {
585 		if (chain->index == chain_index)
586 			return chain;
587 	}
588 	return NULL;
589 }
590 #endif
591 
592 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
593 			   u32 seq, u16 flags, int event, bool unicast,
594 			   struct netlink_ext_ack *extack);
595 
__tcf_chain_get(struct tcf_block * block,u32 chain_index,bool create,bool by_act)596 static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
597 					 u32 chain_index, bool create,
598 					 bool by_act)
599 {
600 	struct tcf_chain *chain = NULL;
601 	bool is_first_reference;
602 
603 	mutex_lock(&block->lock);
604 	chain = tcf_chain_lookup(block, chain_index);
605 	if (chain) {
606 		tcf_chain_hold(chain);
607 	} else {
608 		if (!create)
609 			goto errout;
610 		chain = tcf_chain_create(block, chain_index);
611 		if (!chain)
612 			goto errout;
613 	}
614 
615 	if (by_act)
616 		++chain->action_refcnt;
617 	is_first_reference = chain->refcnt - chain->action_refcnt == 1;
618 	mutex_unlock(&block->lock);
619 
620 	/* Send notification only in case we got the first
621 	 * non-action reference. Until then, the chain acts only as
622 	 * a placeholder for actions pointing to it and user ought
623 	 * not know about them.
624 	 */
625 	if (is_first_reference && !by_act)
626 		tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
627 				RTM_NEWCHAIN, false, NULL);
628 
629 	return chain;
630 
631 errout:
632 	mutex_unlock(&block->lock);
633 	return chain;
634 }
635 
tcf_chain_get(struct tcf_block * block,u32 chain_index,bool create)636 static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
637 				       bool create)
638 {
639 	return __tcf_chain_get(block, chain_index, create, false);
640 }
641 
tcf_chain_get_by_act(struct tcf_block * block,u32 chain_index)642 struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
643 {
644 	return __tcf_chain_get(block, chain_index, true, true);
645 }
646 EXPORT_SYMBOL(tcf_chain_get_by_act);
647 
648 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
649 			       void *tmplt_priv);
650 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
651 				  void *tmplt_priv, u32 chain_index,
652 				  struct tcf_block *block, struct sk_buff *oskb,
653 				  u32 seq, u16 flags, bool unicast);
654 
__tcf_chain_put(struct tcf_chain * chain,bool by_act,bool explicitly_created)655 static void __tcf_chain_put(struct tcf_chain *chain, bool by_act,
656 			    bool explicitly_created)
657 {
658 	struct tcf_block *block = chain->block;
659 	const struct tcf_proto_ops *tmplt_ops;
660 	unsigned int refcnt, non_act_refcnt;
661 	bool free_block = false;
662 	void *tmplt_priv;
663 
664 	mutex_lock(&block->lock);
665 	if (explicitly_created) {
666 		if (!chain->explicitly_created) {
667 			mutex_unlock(&block->lock);
668 			return;
669 		}
670 		chain->explicitly_created = false;
671 	}
672 
673 	if (by_act)
674 		chain->action_refcnt--;
675 
676 	/* tc_chain_notify_delete can't be called while holding block lock.
677 	 * However, when block is unlocked chain can be changed concurrently, so
678 	 * save these to temporary variables.
679 	 */
680 	refcnt = --chain->refcnt;
681 	non_act_refcnt = refcnt - chain->action_refcnt;
682 	tmplt_ops = chain->tmplt_ops;
683 	tmplt_priv = chain->tmplt_priv;
684 
685 	if (non_act_refcnt == chain->explicitly_created && !by_act) {
686 		if (non_act_refcnt == 0)
687 			tc_chain_notify_delete(tmplt_ops, tmplt_priv,
688 					       chain->index, block, NULL, 0, 0,
689 					       false);
690 		/* Last reference to chain, no need to lock. */
691 		chain->flushing = false;
692 	}
693 
694 	if (refcnt == 0)
695 		free_block = tcf_chain_detach(chain);
696 	mutex_unlock(&block->lock);
697 
698 	if (refcnt == 0) {
699 		tc_chain_tmplt_del(tmplt_ops, tmplt_priv);
700 		tcf_chain_destroy(chain, free_block);
701 	}
702 }
703 
tcf_chain_put(struct tcf_chain * chain)704 static void tcf_chain_put(struct tcf_chain *chain)
705 {
706 	__tcf_chain_put(chain, false, false);
707 }
708 
tcf_chain_put_by_act(struct tcf_chain * chain)709 void tcf_chain_put_by_act(struct tcf_chain *chain)
710 {
711 	__tcf_chain_put(chain, true, false);
712 }
713 EXPORT_SYMBOL(tcf_chain_put_by_act);
714 
tcf_chain_put_explicitly_created(struct tcf_chain * chain)715 static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
716 {
717 	__tcf_chain_put(chain, false, true);
718 }
719 
tcf_chain_flush(struct tcf_chain * chain,bool rtnl_held)720 static void tcf_chain_flush(struct tcf_chain *chain, bool rtnl_held)
721 {
722 	struct tcf_proto *tp, *tp_next;
723 
724 	mutex_lock(&chain->filter_chain_lock);
725 	tp = tcf_chain_dereference(chain->filter_chain, chain);
726 	while (tp) {
727 		tp_next = rcu_dereference_protected(tp->next, 1);
728 		tcf_proto_signal_destroying(chain, tp);
729 		tp = tp_next;
730 	}
731 	tp = tcf_chain_dereference(chain->filter_chain, chain);
732 	RCU_INIT_POINTER(chain->filter_chain, NULL);
733 	tcf_chain0_head_change(chain, NULL);
734 	chain->flushing = true;
735 	mutex_unlock(&chain->filter_chain_lock);
736 
737 	while (tp) {
738 		tp_next = rcu_dereference_protected(tp->next, 1);
739 		tcf_proto_put(tp, rtnl_held, NULL);
740 		tp = tp_next;
741 	}
742 }
743 
744 static int tcf_block_setup(struct tcf_block *block,
745 			   struct flow_block_offload *bo);
746 
tcf_block_offload_init(struct flow_block_offload * bo,struct net_device * dev,struct Qdisc * sch,enum flow_block_command command,enum flow_block_binder_type binder_type,struct flow_block * flow_block,bool shared,struct netlink_ext_ack * extack)747 static void tcf_block_offload_init(struct flow_block_offload *bo,
748 				   struct net_device *dev, struct Qdisc *sch,
749 				   enum flow_block_command command,
750 				   enum flow_block_binder_type binder_type,
751 				   struct flow_block *flow_block,
752 				   bool shared, struct netlink_ext_ack *extack)
753 {
754 	bo->net = dev_net(dev);
755 	bo->command = command;
756 	bo->binder_type = binder_type;
757 	bo->block = flow_block;
758 	bo->block_shared = shared;
759 	bo->extack = extack;
760 	bo->sch = sch;
761 	bo->cb_list_head = &flow_block->cb_list;
762 	INIT_LIST_HEAD(&bo->cb_list);
763 }
764 
765 static void tcf_block_unbind(struct tcf_block *block,
766 			     struct flow_block_offload *bo);
767 
tc_block_indr_cleanup(struct flow_block_cb * block_cb)768 static void tc_block_indr_cleanup(struct flow_block_cb *block_cb)
769 {
770 	struct tcf_block *block = block_cb->indr.data;
771 	struct net_device *dev = block_cb->indr.dev;
772 	struct Qdisc *sch = block_cb->indr.sch;
773 	struct netlink_ext_ack extack = {};
774 	struct flow_block_offload bo = {};
775 
776 	tcf_block_offload_init(&bo, dev, sch, FLOW_BLOCK_UNBIND,
777 			       block_cb->indr.binder_type,
778 			       &block->flow_block, tcf_block_shared(block),
779 			       &extack);
780 	rtnl_lock();
781 	down_write(&block->cb_lock);
782 	list_del(&block_cb->driver_list);
783 	list_move(&block_cb->list, &bo.cb_list);
784 	tcf_block_unbind(block, &bo);
785 	up_write(&block->cb_lock);
786 	rtnl_unlock();
787 }
788 
tcf_block_offload_in_use(struct tcf_block * block)789 static bool tcf_block_offload_in_use(struct tcf_block *block)
790 {
791 	return atomic_read(&block->offloadcnt);
792 }
793 
tcf_block_offload_cmd(struct tcf_block * block,struct net_device * dev,struct Qdisc * sch,struct tcf_block_ext_info * ei,enum flow_block_command command,struct netlink_ext_ack * extack)794 static int tcf_block_offload_cmd(struct tcf_block *block,
795 				 struct net_device *dev, struct Qdisc *sch,
796 				 struct tcf_block_ext_info *ei,
797 				 enum flow_block_command command,
798 				 struct netlink_ext_ack *extack)
799 {
800 	struct flow_block_offload bo = {};
801 
802 	tcf_block_offload_init(&bo, dev, sch, command, ei->binder_type,
803 			       &block->flow_block, tcf_block_shared(block),
804 			       extack);
805 
806 	if (dev->netdev_ops->ndo_setup_tc) {
807 		int err;
808 
809 		err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
810 		if (err < 0) {
811 			if (err != -EOPNOTSUPP)
812 				NL_SET_ERR_MSG(extack, "Driver ndo_setup_tc failed");
813 			return err;
814 		}
815 
816 		return tcf_block_setup(block, &bo);
817 	}
818 
819 	flow_indr_dev_setup_offload(dev, sch, TC_SETUP_BLOCK, block, &bo,
820 				    tc_block_indr_cleanup);
821 	tcf_block_setup(block, &bo);
822 
823 	return -EOPNOTSUPP;
824 }
825 
tcf_block_offload_bind(struct tcf_block * block,struct Qdisc * q,struct tcf_block_ext_info * ei,struct netlink_ext_ack * extack)826 static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
827 				  struct tcf_block_ext_info *ei,
828 				  struct netlink_ext_ack *extack)
829 {
830 	struct net_device *dev = q->dev_queue->dev;
831 	int err;
832 
833 	down_write(&block->cb_lock);
834 
835 	/* If tc offload feature is disabled and the block we try to bind
836 	 * to already has some offloaded filters, forbid to bind.
837 	 */
838 	if (dev->netdev_ops->ndo_setup_tc &&
839 	    !tc_can_offload(dev) &&
840 	    tcf_block_offload_in_use(block)) {
841 		NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
842 		err = -EOPNOTSUPP;
843 		goto err_unlock;
844 	}
845 
846 	err = tcf_block_offload_cmd(block, dev, q, ei, FLOW_BLOCK_BIND, extack);
847 	if (err == -EOPNOTSUPP)
848 		goto no_offload_dev_inc;
849 	if (err)
850 		goto err_unlock;
851 
852 	up_write(&block->cb_lock);
853 	return 0;
854 
855 no_offload_dev_inc:
856 	if (tcf_block_offload_in_use(block))
857 		goto err_unlock;
858 
859 	err = 0;
860 	block->nooffloaddevcnt++;
861 err_unlock:
862 	up_write(&block->cb_lock);
863 	return err;
864 }
865 
tcf_block_offload_unbind(struct tcf_block * block,struct Qdisc * q,struct tcf_block_ext_info * ei)866 static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
867 				     struct tcf_block_ext_info *ei)
868 {
869 	struct net_device *dev = q->dev_queue->dev;
870 	int err;
871 
872 	down_write(&block->cb_lock);
873 	err = tcf_block_offload_cmd(block, dev, q, ei, FLOW_BLOCK_UNBIND, NULL);
874 	if (err == -EOPNOTSUPP)
875 		goto no_offload_dev_dec;
876 	up_write(&block->cb_lock);
877 	return;
878 
879 no_offload_dev_dec:
880 	WARN_ON(block->nooffloaddevcnt-- == 0);
881 	up_write(&block->cb_lock);
882 }
883 
884 static int
tcf_chain0_head_change_cb_add(struct tcf_block * block,struct tcf_block_ext_info * ei,struct netlink_ext_ack * extack)885 tcf_chain0_head_change_cb_add(struct tcf_block *block,
886 			      struct tcf_block_ext_info *ei,
887 			      struct netlink_ext_ack *extack)
888 {
889 	struct tcf_filter_chain_list_item *item;
890 	struct tcf_chain *chain0;
891 
892 	item = kmalloc(sizeof(*item), GFP_KERNEL);
893 	if (!item) {
894 		NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
895 		return -ENOMEM;
896 	}
897 	item->chain_head_change = ei->chain_head_change;
898 	item->chain_head_change_priv = ei->chain_head_change_priv;
899 
900 	mutex_lock(&block->lock);
901 	chain0 = block->chain0.chain;
902 	if (chain0)
903 		tcf_chain_hold(chain0);
904 	else
905 		list_add(&item->list, &block->chain0.filter_chain_list);
906 	mutex_unlock(&block->lock);
907 
908 	if (chain0) {
909 		struct tcf_proto *tp_head;
910 
911 		mutex_lock(&chain0->filter_chain_lock);
912 
913 		tp_head = tcf_chain_dereference(chain0->filter_chain, chain0);
914 		if (tp_head)
915 			tcf_chain_head_change_item(item, tp_head);
916 
917 		mutex_lock(&block->lock);
918 		list_add(&item->list, &block->chain0.filter_chain_list);
919 		mutex_unlock(&block->lock);
920 
921 		mutex_unlock(&chain0->filter_chain_lock);
922 		tcf_chain_put(chain0);
923 	}
924 
925 	return 0;
926 }
927 
928 static void
tcf_chain0_head_change_cb_del(struct tcf_block * block,struct tcf_block_ext_info * ei)929 tcf_chain0_head_change_cb_del(struct tcf_block *block,
930 			      struct tcf_block_ext_info *ei)
931 {
932 	struct tcf_filter_chain_list_item *item;
933 
934 	mutex_lock(&block->lock);
935 	list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
936 		if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
937 		    (item->chain_head_change == ei->chain_head_change &&
938 		     item->chain_head_change_priv == ei->chain_head_change_priv)) {
939 			if (block->chain0.chain)
940 				tcf_chain_head_change_item(item, NULL);
941 			list_del(&item->list);
942 			mutex_unlock(&block->lock);
943 
944 			kfree(item);
945 			return;
946 		}
947 	}
948 	mutex_unlock(&block->lock);
949 	WARN_ON(1);
950 }
951 
952 struct tcf_net {
953 	spinlock_t idr_lock; /* Protects idr */
954 	struct idr idr;
955 };
956 
957 static unsigned int tcf_net_id;
958 
tcf_block_insert(struct tcf_block * block,struct net * net,struct netlink_ext_ack * extack)959 static int tcf_block_insert(struct tcf_block *block, struct net *net,
960 			    struct netlink_ext_ack *extack)
961 {
962 	struct tcf_net *tn = net_generic(net, tcf_net_id);
963 	int err;
964 
965 	idr_preload(GFP_KERNEL);
966 	spin_lock(&tn->idr_lock);
967 	err = idr_alloc_u32(&tn->idr, block, &block->index, block->index,
968 			    GFP_NOWAIT);
969 	spin_unlock(&tn->idr_lock);
970 	idr_preload_end();
971 
972 	return err;
973 }
974 
tcf_block_remove(struct tcf_block * block,struct net * net)975 static void tcf_block_remove(struct tcf_block *block, struct net *net)
976 {
977 	struct tcf_net *tn = net_generic(net, tcf_net_id);
978 
979 	spin_lock(&tn->idr_lock);
980 	idr_remove(&tn->idr, block->index);
981 	spin_unlock(&tn->idr_lock);
982 }
983 
tcf_block_create(struct net * net,struct Qdisc * q,u32 block_index,struct netlink_ext_ack * extack)984 static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
985 					  u32 block_index,
986 					  struct netlink_ext_ack *extack)
987 {
988 	struct tcf_block *block;
989 
990 	block = kzalloc(sizeof(*block), GFP_KERNEL);
991 	if (!block) {
992 		NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
993 		return ERR_PTR(-ENOMEM);
994 	}
995 	mutex_init(&block->lock);
996 	mutex_init(&block->proto_destroy_lock);
997 	init_rwsem(&block->cb_lock);
998 	flow_block_init(&block->flow_block);
999 	INIT_LIST_HEAD(&block->chain_list);
1000 	INIT_LIST_HEAD(&block->owner_list);
1001 	INIT_LIST_HEAD(&block->chain0.filter_chain_list);
1002 
1003 	refcount_set(&block->refcnt, 1);
1004 	block->net = net;
1005 	block->index = block_index;
1006 
1007 	/* Don't store q pointer for blocks which are shared */
1008 	if (!tcf_block_shared(block))
1009 		block->q = q;
1010 	return block;
1011 }
1012 
tcf_block_lookup(struct net * net,u32 block_index)1013 static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
1014 {
1015 	struct tcf_net *tn = net_generic(net, tcf_net_id);
1016 
1017 	return idr_find(&tn->idr, block_index);
1018 }
1019 
tcf_block_refcnt_get(struct net * net,u32 block_index)1020 static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index)
1021 {
1022 	struct tcf_block *block;
1023 
1024 	rcu_read_lock();
1025 	block = tcf_block_lookup(net, block_index);
1026 	if (block && !refcount_inc_not_zero(&block->refcnt))
1027 		block = NULL;
1028 	rcu_read_unlock();
1029 
1030 	return block;
1031 }
1032 
1033 static struct tcf_chain *
__tcf_get_next_chain(struct tcf_block * block,struct tcf_chain * chain)1034 __tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
1035 {
1036 	mutex_lock(&block->lock);
1037 	if (chain)
1038 		chain = list_is_last(&chain->list, &block->chain_list) ?
1039 			NULL : list_next_entry(chain, list);
1040 	else
1041 		chain = list_first_entry_or_null(&block->chain_list,
1042 						 struct tcf_chain, list);
1043 
1044 	/* skip all action-only chains */
1045 	while (chain && tcf_chain_held_by_acts_only(chain))
1046 		chain = list_is_last(&chain->list, &block->chain_list) ?
1047 			NULL : list_next_entry(chain, list);
1048 
1049 	if (chain)
1050 		tcf_chain_hold(chain);
1051 	mutex_unlock(&block->lock);
1052 
1053 	return chain;
1054 }
1055 
1056 /* Function to be used by all clients that want to iterate over all chains on
1057  * block. It properly obtains block->lock and takes reference to chain before
1058  * returning it. Users of this function must be tolerant to concurrent chain
1059  * insertion/deletion or ensure that no concurrent chain modification is
1060  * possible. Note that all netlink dump callbacks cannot guarantee to provide
1061  * consistent dump because rtnl lock is released each time skb is filled with
1062  * data and sent to user-space.
1063  */
1064 
1065 struct tcf_chain *
tcf_get_next_chain(struct tcf_block * block,struct tcf_chain * chain)1066 tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
1067 {
1068 	struct tcf_chain *chain_next = __tcf_get_next_chain(block, chain);
1069 
1070 	if (chain)
1071 		tcf_chain_put(chain);
1072 
1073 	return chain_next;
1074 }
1075 EXPORT_SYMBOL(tcf_get_next_chain);
1076 
1077 static struct tcf_proto *
__tcf_get_next_proto(struct tcf_chain * chain,struct tcf_proto * tp)1078 __tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
1079 {
1080 	u32 prio = 0;
1081 
1082 	ASSERT_RTNL();
1083 	mutex_lock(&chain->filter_chain_lock);
1084 
1085 	if (!tp) {
1086 		tp = tcf_chain_dereference(chain->filter_chain, chain);
1087 	} else if (tcf_proto_is_deleting(tp)) {
1088 		/* 'deleting' flag is set and chain->filter_chain_lock was
1089 		 * unlocked, which means next pointer could be invalid. Restart
1090 		 * search.
1091 		 */
1092 		prio = tp->prio + 1;
1093 		tp = tcf_chain_dereference(chain->filter_chain, chain);
1094 
1095 		for (; tp; tp = tcf_chain_dereference(tp->next, chain))
1096 			if (!tp->deleting && tp->prio >= prio)
1097 				break;
1098 	} else {
1099 		tp = tcf_chain_dereference(tp->next, chain);
1100 	}
1101 
1102 	if (tp)
1103 		tcf_proto_get(tp);
1104 
1105 	mutex_unlock(&chain->filter_chain_lock);
1106 
1107 	return tp;
1108 }
1109 
1110 /* Function to be used by all clients that want to iterate over all tp's on
1111  * chain. Users of this function must be tolerant to concurrent tp
1112  * insertion/deletion or ensure that no concurrent chain modification is
1113  * possible. Note that all netlink dump callbacks cannot guarantee to provide
1114  * consistent dump because rtnl lock is released each time skb is filled with
1115  * data and sent to user-space.
1116  */
1117 
1118 struct tcf_proto *
tcf_get_next_proto(struct tcf_chain * chain,struct tcf_proto * tp)1119 tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
1120 {
1121 	struct tcf_proto *tp_next = __tcf_get_next_proto(chain, tp);
1122 
1123 	if (tp)
1124 		tcf_proto_put(tp, true, NULL);
1125 
1126 	return tp_next;
1127 }
1128 EXPORT_SYMBOL(tcf_get_next_proto);
1129 
tcf_block_flush_all_chains(struct tcf_block * block,bool rtnl_held)1130 static void tcf_block_flush_all_chains(struct tcf_block *block, bool rtnl_held)
1131 {
1132 	struct tcf_chain *chain;
1133 
1134 	/* Last reference to block. At this point chains cannot be added or
1135 	 * removed concurrently.
1136 	 */
1137 	for (chain = tcf_get_next_chain(block, NULL);
1138 	     chain;
1139 	     chain = tcf_get_next_chain(block, chain)) {
1140 		tcf_chain_put_explicitly_created(chain);
1141 		tcf_chain_flush(chain, rtnl_held);
1142 	}
1143 }
1144 
1145 /* Lookup Qdisc and increments its reference counter.
1146  * Set parent, if necessary.
1147  */
1148 
__tcf_qdisc_find(struct net * net,struct Qdisc ** q,u32 * parent,int ifindex,bool rtnl_held,struct netlink_ext_ack * extack)1149 static int __tcf_qdisc_find(struct net *net, struct Qdisc **q,
1150 			    u32 *parent, int ifindex, bool rtnl_held,
1151 			    struct netlink_ext_ack *extack)
1152 {
1153 	const struct Qdisc_class_ops *cops;
1154 	struct net_device *dev;
1155 	int err = 0;
1156 
1157 	if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
1158 		return 0;
1159 
1160 	rcu_read_lock();
1161 
1162 	/* Find link */
1163 	dev = dev_get_by_index_rcu(net, ifindex);
1164 	if (!dev) {
1165 		rcu_read_unlock();
1166 		return -ENODEV;
1167 	}
1168 
1169 	/* Find qdisc */
1170 	if (!*parent) {
1171 		*q = rcu_dereference(dev->qdisc);
1172 		*parent = (*q)->handle;
1173 	} else {
1174 		*q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent));
1175 		if (!*q) {
1176 			NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1177 			err = -EINVAL;
1178 			goto errout_rcu;
1179 		}
1180 	}
1181 
1182 	*q = qdisc_refcount_inc_nz(*q);
1183 	if (!*q) {
1184 		NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1185 		err = -EINVAL;
1186 		goto errout_rcu;
1187 	}
1188 
1189 	/* Is it classful? */
1190 	cops = (*q)->ops->cl_ops;
1191 	if (!cops) {
1192 		NL_SET_ERR_MSG(extack, "Qdisc not classful");
1193 		err = -EINVAL;
1194 		goto errout_qdisc;
1195 	}
1196 
1197 	if (!cops->tcf_block) {
1198 		NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
1199 		err = -EOPNOTSUPP;
1200 		goto errout_qdisc;
1201 	}
1202 
1203 errout_rcu:
1204 	/* At this point we know that qdisc is not noop_qdisc,
1205 	 * which means that qdisc holds a reference to net_device
1206 	 * and we hold a reference to qdisc, so it is safe to release
1207 	 * rcu read lock.
1208 	 */
1209 	rcu_read_unlock();
1210 	return err;
1211 
1212 errout_qdisc:
1213 	rcu_read_unlock();
1214 
1215 	if (rtnl_held)
1216 		qdisc_put(*q);
1217 	else
1218 		qdisc_put_unlocked(*q);
1219 	*q = NULL;
1220 
1221 	return err;
1222 }
1223 
__tcf_qdisc_cl_find(struct Qdisc * q,u32 parent,unsigned long * cl,int ifindex,struct netlink_ext_ack * extack)1224 static int __tcf_qdisc_cl_find(struct Qdisc *q, u32 parent, unsigned long *cl,
1225 			       int ifindex, struct netlink_ext_ack *extack)
1226 {
1227 	if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
1228 		return 0;
1229 
1230 	/* Do we search for filter, attached to class? */
1231 	if (TC_H_MIN(parent)) {
1232 		const struct Qdisc_class_ops *cops = q->ops->cl_ops;
1233 
1234 		*cl = cops->find(q, parent);
1235 		if (*cl == 0) {
1236 			NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
1237 			return -ENOENT;
1238 		}
1239 	}
1240 
1241 	return 0;
1242 }
1243 
__tcf_block_find(struct net * net,struct Qdisc * q,unsigned long cl,int ifindex,u32 block_index,struct netlink_ext_ack * extack)1244 static struct tcf_block *__tcf_block_find(struct net *net, struct Qdisc *q,
1245 					  unsigned long cl, int ifindex,
1246 					  u32 block_index,
1247 					  struct netlink_ext_ack *extack)
1248 {
1249 	struct tcf_block *block;
1250 
1251 	if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1252 		block = tcf_block_refcnt_get(net, block_index);
1253 		if (!block) {
1254 			NL_SET_ERR_MSG(extack, "Block of given index was not found");
1255 			return ERR_PTR(-EINVAL);
1256 		}
1257 	} else {
1258 		const struct Qdisc_class_ops *cops = q->ops->cl_ops;
1259 
1260 		block = cops->tcf_block(q, cl, extack);
1261 		if (!block)
1262 			return ERR_PTR(-EINVAL);
1263 
1264 		if (tcf_block_shared(block)) {
1265 			NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
1266 			return ERR_PTR(-EOPNOTSUPP);
1267 		}
1268 
1269 		/* Always take reference to block in order to support execution
1270 		 * of rules update path of cls API without rtnl lock. Caller
1271 		 * must release block when it is finished using it. 'if' block
1272 		 * of this conditional obtain reference to block by calling
1273 		 * tcf_block_refcnt_get().
1274 		 */
1275 		refcount_inc(&block->refcnt);
1276 	}
1277 
1278 	return block;
1279 }
1280 
__tcf_block_put(struct tcf_block * block,struct Qdisc * q,struct tcf_block_ext_info * ei,bool rtnl_held)1281 static void __tcf_block_put(struct tcf_block *block, struct Qdisc *q,
1282 			    struct tcf_block_ext_info *ei, bool rtnl_held)
1283 {
1284 	if (refcount_dec_and_mutex_lock(&block->refcnt, &block->lock)) {
1285 		/* Flushing/putting all chains will cause the block to be
1286 		 * deallocated when last chain is freed. However, if chain_list
1287 		 * is empty, block has to be manually deallocated. After block
1288 		 * reference counter reached 0, it is no longer possible to
1289 		 * increment it or add new chains to block.
1290 		 */
1291 		bool free_block = list_empty(&block->chain_list);
1292 
1293 		mutex_unlock(&block->lock);
1294 		if (tcf_block_shared(block))
1295 			tcf_block_remove(block, block->net);
1296 
1297 		if (q)
1298 			tcf_block_offload_unbind(block, q, ei);
1299 
1300 		if (free_block)
1301 			tcf_block_destroy(block);
1302 		else
1303 			tcf_block_flush_all_chains(block, rtnl_held);
1304 	} else if (q) {
1305 		tcf_block_offload_unbind(block, q, ei);
1306 	}
1307 }
1308 
tcf_block_refcnt_put(struct tcf_block * block,bool rtnl_held)1309 static void tcf_block_refcnt_put(struct tcf_block *block, bool rtnl_held)
1310 {
1311 	__tcf_block_put(block, NULL, NULL, rtnl_held);
1312 }
1313 
1314 /* Find tcf block.
1315  * Set q, parent, cl when appropriate.
1316  */
1317 
tcf_block_find(struct net * net,struct Qdisc ** q,u32 * parent,unsigned long * cl,int ifindex,u32 block_index,struct netlink_ext_ack * extack)1318 static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
1319 					u32 *parent, unsigned long *cl,
1320 					int ifindex, u32 block_index,
1321 					struct netlink_ext_ack *extack)
1322 {
1323 	struct tcf_block *block;
1324 	int err = 0;
1325 
1326 	ASSERT_RTNL();
1327 
1328 	err = __tcf_qdisc_find(net, q, parent, ifindex, true, extack);
1329 	if (err)
1330 		goto errout;
1331 
1332 	err = __tcf_qdisc_cl_find(*q, *parent, cl, ifindex, extack);
1333 	if (err)
1334 		goto errout_qdisc;
1335 
1336 	block = __tcf_block_find(net, *q, *cl, ifindex, block_index, extack);
1337 	if (IS_ERR(block)) {
1338 		err = PTR_ERR(block);
1339 		goto errout_qdisc;
1340 	}
1341 
1342 	return block;
1343 
1344 errout_qdisc:
1345 	if (*q)
1346 		qdisc_put(*q);
1347 errout:
1348 	*q = NULL;
1349 	return ERR_PTR(err);
1350 }
1351 
tcf_block_release(struct Qdisc * q,struct tcf_block * block,bool rtnl_held)1352 static void tcf_block_release(struct Qdisc *q, struct tcf_block *block,
1353 			      bool rtnl_held)
1354 {
1355 	if (!IS_ERR_OR_NULL(block))
1356 		tcf_block_refcnt_put(block, rtnl_held);
1357 
1358 	if (q) {
1359 		if (rtnl_held)
1360 			qdisc_put(q);
1361 		else
1362 			qdisc_put_unlocked(q);
1363 	}
1364 }
1365 
1366 struct tcf_block_owner_item {
1367 	struct list_head list;
1368 	struct Qdisc *q;
1369 	enum flow_block_binder_type binder_type;
1370 };
1371 
1372 static void
tcf_block_owner_netif_keep_dst(struct tcf_block * block,struct Qdisc * q,enum flow_block_binder_type binder_type)1373 tcf_block_owner_netif_keep_dst(struct tcf_block *block,
1374 			       struct Qdisc *q,
1375 			       enum flow_block_binder_type binder_type)
1376 {
1377 	if (block->keep_dst &&
1378 	    binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
1379 	    binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1380 		netif_keep_dst(qdisc_dev(q));
1381 }
1382 
tcf_block_netif_keep_dst(struct tcf_block * block)1383 void tcf_block_netif_keep_dst(struct tcf_block *block)
1384 {
1385 	struct tcf_block_owner_item *item;
1386 
1387 	block->keep_dst = true;
1388 	list_for_each_entry(item, &block->owner_list, list)
1389 		tcf_block_owner_netif_keep_dst(block, item->q,
1390 					       item->binder_type);
1391 }
1392 EXPORT_SYMBOL(tcf_block_netif_keep_dst);
1393 
tcf_block_owner_add(struct tcf_block * block,struct Qdisc * q,enum flow_block_binder_type binder_type)1394 static int tcf_block_owner_add(struct tcf_block *block,
1395 			       struct Qdisc *q,
1396 			       enum flow_block_binder_type binder_type)
1397 {
1398 	struct tcf_block_owner_item *item;
1399 
1400 	item = kmalloc(sizeof(*item), GFP_KERNEL);
1401 	if (!item)
1402 		return -ENOMEM;
1403 	item->q = q;
1404 	item->binder_type = binder_type;
1405 	list_add(&item->list, &block->owner_list);
1406 	return 0;
1407 }
1408 
tcf_block_owner_del(struct tcf_block * block,struct Qdisc * q,enum flow_block_binder_type binder_type)1409 static void tcf_block_owner_del(struct tcf_block *block,
1410 				struct Qdisc *q,
1411 				enum flow_block_binder_type binder_type)
1412 {
1413 	struct tcf_block_owner_item *item;
1414 
1415 	list_for_each_entry(item, &block->owner_list, list) {
1416 		if (item->q == q && item->binder_type == binder_type) {
1417 			list_del(&item->list);
1418 			kfree(item);
1419 			return;
1420 		}
1421 	}
1422 	WARN_ON(1);
1423 }
1424 
tcf_block_get_ext(struct tcf_block ** p_block,struct Qdisc * q,struct tcf_block_ext_info * ei,struct netlink_ext_ack * extack)1425 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
1426 		      struct tcf_block_ext_info *ei,
1427 		      struct netlink_ext_ack *extack)
1428 {
1429 	struct net *net = qdisc_net(q);
1430 	struct tcf_block *block = NULL;
1431 	int err;
1432 
1433 	if (ei->block_index)
1434 		/* block_index not 0 means the shared block is requested */
1435 		block = tcf_block_refcnt_get(net, ei->block_index);
1436 
1437 	if (!block) {
1438 		block = tcf_block_create(net, q, ei->block_index, extack);
1439 		if (IS_ERR(block))
1440 			return PTR_ERR(block);
1441 		if (tcf_block_shared(block)) {
1442 			err = tcf_block_insert(block, net, extack);
1443 			if (err)
1444 				goto err_block_insert;
1445 		}
1446 	}
1447 
1448 	err = tcf_block_owner_add(block, q, ei->binder_type);
1449 	if (err)
1450 		goto err_block_owner_add;
1451 
1452 	tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
1453 
1454 	err = tcf_chain0_head_change_cb_add(block, ei, extack);
1455 	if (err)
1456 		goto err_chain0_head_change_cb_add;
1457 
1458 	err = tcf_block_offload_bind(block, q, ei, extack);
1459 	if (err)
1460 		goto err_block_offload_bind;
1461 
1462 	*p_block = block;
1463 	return 0;
1464 
1465 err_block_offload_bind:
1466 	tcf_chain0_head_change_cb_del(block, ei);
1467 err_chain0_head_change_cb_add:
1468 	tcf_block_owner_del(block, q, ei->binder_type);
1469 err_block_owner_add:
1470 err_block_insert:
1471 	tcf_block_refcnt_put(block, true);
1472 	return err;
1473 }
1474 EXPORT_SYMBOL(tcf_block_get_ext);
1475 
tcf_chain_head_change_dflt(struct tcf_proto * tp_head,void * priv)1476 static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
1477 {
1478 	struct tcf_proto __rcu **p_filter_chain = priv;
1479 
1480 	rcu_assign_pointer(*p_filter_chain, tp_head);
1481 }
1482 
tcf_block_get(struct tcf_block ** p_block,struct tcf_proto __rcu ** p_filter_chain,struct Qdisc * q,struct netlink_ext_ack * extack)1483 int tcf_block_get(struct tcf_block **p_block,
1484 		  struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
1485 		  struct netlink_ext_ack *extack)
1486 {
1487 	struct tcf_block_ext_info ei = {
1488 		.chain_head_change = tcf_chain_head_change_dflt,
1489 		.chain_head_change_priv = p_filter_chain,
1490 	};
1491 
1492 	WARN_ON(!p_filter_chain);
1493 	return tcf_block_get_ext(p_block, q, &ei, extack);
1494 }
1495 EXPORT_SYMBOL(tcf_block_get);
1496 
1497 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
1498  * actions should be all removed after flushing.
1499  */
tcf_block_put_ext(struct tcf_block * block,struct Qdisc * q,struct tcf_block_ext_info * ei)1500 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
1501 		       struct tcf_block_ext_info *ei)
1502 {
1503 	if (!block)
1504 		return;
1505 	tcf_chain0_head_change_cb_del(block, ei);
1506 	tcf_block_owner_del(block, q, ei->binder_type);
1507 
1508 	__tcf_block_put(block, q, ei, true);
1509 }
1510 EXPORT_SYMBOL(tcf_block_put_ext);
1511 
tcf_block_put(struct tcf_block * block)1512 void tcf_block_put(struct tcf_block *block)
1513 {
1514 	struct tcf_block_ext_info ei = {0, };
1515 
1516 	if (!block)
1517 		return;
1518 	tcf_block_put_ext(block, block->q, &ei);
1519 }
1520 
1521 EXPORT_SYMBOL(tcf_block_put);
1522 
1523 static int
tcf_block_playback_offloads(struct tcf_block * block,flow_setup_cb_t * cb,void * cb_priv,bool add,bool offload_in_use,struct netlink_ext_ack * extack)1524 tcf_block_playback_offloads(struct tcf_block *block, flow_setup_cb_t *cb,
1525 			    void *cb_priv, bool add, bool offload_in_use,
1526 			    struct netlink_ext_ack *extack)
1527 {
1528 	struct tcf_chain *chain, *chain_prev;
1529 	struct tcf_proto *tp, *tp_prev;
1530 	int err;
1531 
1532 	lockdep_assert_held(&block->cb_lock);
1533 
1534 	for (chain = __tcf_get_next_chain(block, NULL);
1535 	     chain;
1536 	     chain_prev = chain,
1537 		     chain = __tcf_get_next_chain(block, chain),
1538 		     tcf_chain_put(chain_prev)) {
1539 		if (chain->tmplt_ops && add)
1540 			chain->tmplt_ops->tmplt_reoffload(chain, true, cb,
1541 							  cb_priv);
1542 		for (tp = __tcf_get_next_proto(chain, NULL); tp;
1543 		     tp_prev = tp,
1544 			     tp = __tcf_get_next_proto(chain, tp),
1545 			     tcf_proto_put(tp_prev, true, NULL)) {
1546 			if (tp->ops->reoffload) {
1547 				err = tp->ops->reoffload(tp, add, cb, cb_priv,
1548 							 extack);
1549 				if (err && add)
1550 					goto err_playback_remove;
1551 			} else if (add && offload_in_use) {
1552 				err = -EOPNOTSUPP;
1553 				NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
1554 				goto err_playback_remove;
1555 			}
1556 		}
1557 		if (chain->tmplt_ops && !add)
1558 			chain->tmplt_ops->tmplt_reoffload(chain, false, cb,
1559 							  cb_priv);
1560 	}
1561 
1562 	return 0;
1563 
1564 err_playback_remove:
1565 	tcf_proto_put(tp, true, NULL);
1566 	tcf_chain_put(chain);
1567 	tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
1568 				    extack);
1569 	return err;
1570 }
1571 
tcf_block_bind(struct tcf_block * block,struct flow_block_offload * bo)1572 static int tcf_block_bind(struct tcf_block *block,
1573 			  struct flow_block_offload *bo)
1574 {
1575 	struct flow_block_cb *block_cb, *next;
1576 	int err, i = 0;
1577 
1578 	lockdep_assert_held(&block->cb_lock);
1579 
1580 	list_for_each_entry(block_cb, &bo->cb_list, list) {
1581 		err = tcf_block_playback_offloads(block, block_cb->cb,
1582 						  block_cb->cb_priv, true,
1583 						  tcf_block_offload_in_use(block),
1584 						  bo->extack);
1585 		if (err)
1586 			goto err_unroll;
1587 		if (!bo->unlocked_driver_cb)
1588 			block->lockeddevcnt++;
1589 
1590 		i++;
1591 	}
1592 	list_splice(&bo->cb_list, &block->flow_block.cb_list);
1593 
1594 	return 0;
1595 
1596 err_unroll:
1597 	list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
1598 		list_del(&block_cb->driver_list);
1599 		if (i-- > 0) {
1600 			list_del(&block_cb->list);
1601 			tcf_block_playback_offloads(block, block_cb->cb,
1602 						    block_cb->cb_priv, false,
1603 						    tcf_block_offload_in_use(block),
1604 						    NULL);
1605 			if (!bo->unlocked_driver_cb)
1606 				block->lockeddevcnt--;
1607 		}
1608 		flow_block_cb_free(block_cb);
1609 	}
1610 
1611 	return err;
1612 }
1613 
tcf_block_unbind(struct tcf_block * block,struct flow_block_offload * bo)1614 static void tcf_block_unbind(struct tcf_block *block,
1615 			     struct flow_block_offload *bo)
1616 {
1617 	struct flow_block_cb *block_cb, *next;
1618 
1619 	lockdep_assert_held(&block->cb_lock);
1620 
1621 	list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
1622 		tcf_block_playback_offloads(block, block_cb->cb,
1623 					    block_cb->cb_priv, false,
1624 					    tcf_block_offload_in_use(block),
1625 					    NULL);
1626 		list_del(&block_cb->list);
1627 		flow_block_cb_free(block_cb);
1628 		if (!bo->unlocked_driver_cb)
1629 			block->lockeddevcnt--;
1630 	}
1631 }
1632 
tcf_block_setup(struct tcf_block * block,struct flow_block_offload * bo)1633 static int tcf_block_setup(struct tcf_block *block,
1634 			   struct flow_block_offload *bo)
1635 {
1636 	int err;
1637 
1638 	switch (bo->command) {
1639 	case FLOW_BLOCK_BIND:
1640 		err = tcf_block_bind(block, bo);
1641 		break;
1642 	case FLOW_BLOCK_UNBIND:
1643 		err = 0;
1644 		tcf_block_unbind(block, bo);
1645 		break;
1646 	default:
1647 		WARN_ON_ONCE(1);
1648 		err = -EOPNOTSUPP;
1649 	}
1650 
1651 	return err;
1652 }
1653 
1654 /* Main classifier routine: scans classifier chain attached
1655  * to this qdisc, (optionally) tests for protocol and asks
1656  * specific classifiers.
1657  */
__tcf_classify(struct sk_buff * skb,const struct tcf_proto * tp,const struct tcf_proto * orig_tp,struct tcf_result * res,bool compat_mode,struct tcf_exts_miss_cookie_node * n,int act_index,u32 * last_executed_chain)1658 static inline int __tcf_classify(struct sk_buff *skb,
1659 				 const struct tcf_proto *tp,
1660 				 const struct tcf_proto *orig_tp,
1661 				 struct tcf_result *res,
1662 				 bool compat_mode,
1663 				 struct tcf_exts_miss_cookie_node *n,
1664 				 int act_index,
1665 				 u32 *last_executed_chain)
1666 {
1667 #ifdef CONFIG_NET_CLS_ACT
1668 	const int max_reclassify_loop = 16;
1669 	const struct tcf_proto *first_tp;
1670 	int limit = 0;
1671 
1672 reclassify:
1673 #endif
1674 	for (; tp; tp = rcu_dereference_bh(tp->next)) {
1675 		__be16 protocol = skb_protocol(skb, false);
1676 		int err = 0;
1677 
1678 		if (n) {
1679 			struct tcf_exts *exts;
1680 
1681 			if (n->tp_prio != tp->prio)
1682 				continue;
1683 
1684 			/* We re-lookup the tp and chain based on index instead
1685 			 * of having hard refs and locks to them, so do a sanity
1686 			 * check if any of tp,chain,exts was replaced by the
1687 			 * time we got here with a cookie from hardware.
1688 			 */
1689 			if (unlikely(n->tp != tp || n->tp->chain != n->chain ||
1690 				     !tp->ops->get_exts))
1691 				return TC_ACT_SHOT;
1692 
1693 			exts = tp->ops->get_exts(tp, n->handle);
1694 			if (unlikely(!exts || n->exts != exts))
1695 				return TC_ACT_SHOT;
1696 
1697 			n = NULL;
1698 			err = tcf_exts_exec_ex(skb, exts, act_index, res);
1699 		} else {
1700 			if (tp->protocol != protocol &&
1701 			    tp->protocol != htons(ETH_P_ALL))
1702 				continue;
1703 
1704 			err = tc_classify(skb, tp, res);
1705 		}
1706 #ifdef CONFIG_NET_CLS_ACT
1707 		if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
1708 			first_tp = orig_tp;
1709 			*last_executed_chain = first_tp->chain->index;
1710 			goto reset;
1711 		} else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
1712 			first_tp = res->goto_tp;
1713 			*last_executed_chain = err & TC_ACT_EXT_VAL_MASK;
1714 			goto reset;
1715 		}
1716 #endif
1717 		if (err >= 0)
1718 			return err;
1719 	}
1720 
1721 	if (unlikely(n))
1722 		return TC_ACT_SHOT;
1723 
1724 	return TC_ACT_UNSPEC; /* signal: continue lookup */
1725 #ifdef CONFIG_NET_CLS_ACT
1726 reset:
1727 	if (unlikely(limit++ >= max_reclassify_loop)) {
1728 		net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
1729 				       tp->chain->block->index,
1730 				       tp->prio & 0xffff,
1731 				       ntohs(tp->protocol));
1732 		return TC_ACT_SHOT;
1733 	}
1734 
1735 	tp = first_tp;
1736 	goto reclassify;
1737 #endif
1738 }
1739 
tcf_classify(struct sk_buff * skb,const struct tcf_block * block,const struct tcf_proto * tp,struct tcf_result * res,bool compat_mode)1740 int tcf_classify(struct sk_buff *skb,
1741 		 const struct tcf_block *block,
1742 		 const struct tcf_proto *tp,
1743 		 struct tcf_result *res, bool compat_mode)
1744 {
1745 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1746 	u32 last_executed_chain = 0;
1747 
1748 	return __tcf_classify(skb, tp, tp, res, compat_mode, NULL, 0,
1749 			      &last_executed_chain);
1750 #else
1751 	u32 last_executed_chain = tp ? tp->chain->index : 0;
1752 	struct tcf_exts_miss_cookie_node *n = NULL;
1753 	const struct tcf_proto *orig_tp = tp;
1754 	struct tc_skb_ext *ext;
1755 	int act_index = 0;
1756 	int ret;
1757 
1758 	if (block) {
1759 		ext = skb_ext_find(skb, TC_SKB_EXT);
1760 
1761 		if (ext && (ext->chain || ext->act_miss)) {
1762 			struct tcf_chain *fchain;
1763 			u32 chain;
1764 
1765 			if (ext->act_miss) {
1766 				n = tcf_exts_miss_cookie_lookup(ext->act_miss_cookie,
1767 								&act_index);
1768 				if (!n)
1769 					return TC_ACT_SHOT;
1770 
1771 				chain = n->chain_index;
1772 			} else {
1773 				chain = ext->chain;
1774 			}
1775 
1776 			fchain = tcf_chain_lookup_rcu(block, chain);
1777 			if (!fchain)
1778 				return TC_ACT_SHOT;
1779 
1780 			/* Consume, so cloned/redirect skbs won't inherit ext */
1781 			skb_ext_del(skb, TC_SKB_EXT);
1782 
1783 			tp = rcu_dereference_bh(fchain->filter_chain);
1784 			last_executed_chain = fchain->index;
1785 		}
1786 	}
1787 
1788 	ret = __tcf_classify(skb, tp, orig_tp, res, compat_mode, n, act_index,
1789 			     &last_executed_chain);
1790 
1791 	if (tc_skb_ext_tc_enabled()) {
1792 		/* If we missed on some chain */
1793 		if (ret == TC_ACT_UNSPEC && last_executed_chain) {
1794 			struct tc_skb_cb *cb = tc_skb_cb(skb);
1795 
1796 			ext = tc_skb_ext_alloc(skb);
1797 			if (WARN_ON_ONCE(!ext))
1798 				return TC_ACT_SHOT;
1799 			ext->chain = last_executed_chain;
1800 			ext->mru = cb->mru;
1801 			ext->post_ct = cb->post_ct;
1802 			ext->post_ct_snat = cb->post_ct_snat;
1803 			ext->post_ct_dnat = cb->post_ct_dnat;
1804 			ext->zone = cb->zone;
1805 		}
1806 	}
1807 
1808 	return ret;
1809 #endif
1810 }
1811 EXPORT_SYMBOL(tcf_classify);
1812 
1813 struct tcf_chain_info {
1814 	struct tcf_proto __rcu **pprev;
1815 	struct tcf_proto __rcu *next;
1816 };
1817 
tcf_chain_tp_prev(struct tcf_chain * chain,struct tcf_chain_info * chain_info)1818 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain *chain,
1819 					   struct tcf_chain_info *chain_info)
1820 {
1821 	return tcf_chain_dereference(*chain_info->pprev, chain);
1822 }
1823 
tcf_chain_tp_insert(struct tcf_chain * chain,struct tcf_chain_info * chain_info,struct tcf_proto * tp)1824 static int tcf_chain_tp_insert(struct tcf_chain *chain,
1825 			       struct tcf_chain_info *chain_info,
1826 			       struct tcf_proto *tp)
1827 {
1828 	if (chain->flushing)
1829 		return -EAGAIN;
1830 
1831 	RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info));
1832 	if (*chain_info->pprev == chain->filter_chain)
1833 		tcf_chain0_head_change(chain, tp);
1834 	tcf_proto_get(tp);
1835 	rcu_assign_pointer(*chain_info->pprev, tp);
1836 
1837 	return 0;
1838 }
1839 
tcf_chain_tp_remove(struct tcf_chain * chain,struct tcf_chain_info * chain_info,struct tcf_proto * tp)1840 static void tcf_chain_tp_remove(struct tcf_chain *chain,
1841 				struct tcf_chain_info *chain_info,
1842 				struct tcf_proto *tp)
1843 {
1844 	struct tcf_proto *next = tcf_chain_dereference(chain_info->next, chain);
1845 
1846 	tcf_proto_mark_delete(tp);
1847 	if (tp == chain->filter_chain)
1848 		tcf_chain0_head_change(chain, next);
1849 	RCU_INIT_POINTER(*chain_info->pprev, next);
1850 }
1851 
1852 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1853 					   struct tcf_chain_info *chain_info,
1854 					   u32 protocol, u32 prio,
1855 					   bool prio_allocate);
1856 
1857 /* Try to insert new proto.
1858  * If proto with specified priority already exists, free new proto
1859  * and return existing one.
1860  */
1861 
tcf_chain_tp_insert_unique(struct tcf_chain * chain,struct tcf_proto * tp_new,u32 protocol,u32 prio,bool rtnl_held)1862 static struct tcf_proto *tcf_chain_tp_insert_unique(struct tcf_chain *chain,
1863 						    struct tcf_proto *tp_new,
1864 						    u32 protocol, u32 prio,
1865 						    bool rtnl_held)
1866 {
1867 	struct tcf_chain_info chain_info;
1868 	struct tcf_proto *tp;
1869 	int err = 0;
1870 
1871 	mutex_lock(&chain->filter_chain_lock);
1872 
1873 	if (tcf_proto_exists_destroying(chain, tp_new)) {
1874 		mutex_unlock(&chain->filter_chain_lock);
1875 		tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1876 		return ERR_PTR(-EAGAIN);
1877 	}
1878 
1879 	tp = tcf_chain_tp_find(chain, &chain_info,
1880 			       protocol, prio, false);
1881 	if (!tp)
1882 		err = tcf_chain_tp_insert(chain, &chain_info, tp_new);
1883 	mutex_unlock(&chain->filter_chain_lock);
1884 
1885 	if (tp) {
1886 		tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1887 		tp_new = tp;
1888 	} else if (err) {
1889 		tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1890 		tp_new = ERR_PTR(err);
1891 	}
1892 
1893 	return tp_new;
1894 }
1895 
tcf_chain_tp_delete_empty(struct tcf_chain * chain,struct tcf_proto * tp,bool rtnl_held,struct netlink_ext_ack * extack)1896 static void tcf_chain_tp_delete_empty(struct tcf_chain *chain,
1897 				      struct tcf_proto *tp, bool rtnl_held,
1898 				      struct netlink_ext_ack *extack)
1899 {
1900 	struct tcf_chain_info chain_info;
1901 	struct tcf_proto *tp_iter;
1902 	struct tcf_proto **pprev;
1903 	struct tcf_proto *next;
1904 
1905 	mutex_lock(&chain->filter_chain_lock);
1906 
1907 	/* Atomically find and remove tp from chain. */
1908 	for (pprev = &chain->filter_chain;
1909 	     (tp_iter = tcf_chain_dereference(*pprev, chain));
1910 	     pprev = &tp_iter->next) {
1911 		if (tp_iter == tp) {
1912 			chain_info.pprev = pprev;
1913 			chain_info.next = tp_iter->next;
1914 			WARN_ON(tp_iter->deleting);
1915 			break;
1916 		}
1917 	}
1918 	/* Verify that tp still exists and no new filters were inserted
1919 	 * concurrently.
1920 	 * Mark tp for deletion if it is empty.
1921 	 */
1922 	if (!tp_iter || !tcf_proto_check_delete(tp)) {
1923 		mutex_unlock(&chain->filter_chain_lock);
1924 		return;
1925 	}
1926 
1927 	tcf_proto_signal_destroying(chain, tp);
1928 	next = tcf_chain_dereference(chain_info.next, chain);
1929 	if (tp == chain->filter_chain)
1930 		tcf_chain0_head_change(chain, next);
1931 	RCU_INIT_POINTER(*chain_info.pprev, next);
1932 	mutex_unlock(&chain->filter_chain_lock);
1933 
1934 	tcf_proto_put(tp, rtnl_held, extack);
1935 }
1936 
tcf_chain_tp_find(struct tcf_chain * chain,struct tcf_chain_info * chain_info,u32 protocol,u32 prio,bool prio_allocate)1937 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1938 					   struct tcf_chain_info *chain_info,
1939 					   u32 protocol, u32 prio,
1940 					   bool prio_allocate)
1941 {
1942 	struct tcf_proto **pprev;
1943 	struct tcf_proto *tp;
1944 
1945 	/* Check the chain for existence of proto-tcf with this priority */
1946 	for (pprev = &chain->filter_chain;
1947 	     (tp = tcf_chain_dereference(*pprev, chain));
1948 	     pprev = &tp->next) {
1949 		if (tp->prio >= prio) {
1950 			if (tp->prio == prio) {
1951 				if (prio_allocate ||
1952 				    (tp->protocol != protocol && protocol))
1953 					return ERR_PTR(-EINVAL);
1954 			} else {
1955 				tp = NULL;
1956 			}
1957 			break;
1958 		}
1959 	}
1960 	chain_info->pprev = pprev;
1961 	if (tp) {
1962 		chain_info->next = tp->next;
1963 		tcf_proto_get(tp);
1964 	} else {
1965 		chain_info->next = NULL;
1966 	}
1967 	return tp;
1968 }
1969 
tcf_fill_node(struct net * net,struct sk_buff * skb,struct tcf_proto * tp,struct tcf_block * block,struct Qdisc * q,u32 parent,void * fh,u32 portid,u32 seq,u16 flags,int event,bool terse_dump,bool rtnl_held,struct netlink_ext_ack * extack)1970 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
1971 			 struct tcf_proto *tp, struct tcf_block *block,
1972 			 struct Qdisc *q, u32 parent, void *fh,
1973 			 u32 portid, u32 seq, u16 flags, int event,
1974 			 bool terse_dump, bool rtnl_held,
1975 			 struct netlink_ext_ack *extack)
1976 {
1977 	struct tcmsg *tcm;
1978 	struct nlmsghdr  *nlh;
1979 	unsigned char *b = skb_tail_pointer(skb);
1980 	int ret = -EMSGSIZE;
1981 
1982 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1983 	if (!nlh)
1984 		goto out_nlmsg_trim;
1985 	tcm = nlmsg_data(nlh);
1986 	tcm->tcm_family = AF_UNSPEC;
1987 	tcm->tcm__pad1 = 0;
1988 	tcm->tcm__pad2 = 0;
1989 	if (q) {
1990 		tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1991 		tcm->tcm_parent = parent;
1992 	} else {
1993 		tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1994 		tcm->tcm_block_index = block->index;
1995 	}
1996 	tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1997 	if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1998 		goto nla_put_failure;
1999 	if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
2000 		goto nla_put_failure;
2001 	if (!fh) {
2002 		tcm->tcm_handle = 0;
2003 	} else if (terse_dump) {
2004 		if (tp->ops->terse_dump) {
2005 			if (tp->ops->terse_dump(net, tp, fh, skb, tcm,
2006 						rtnl_held) < 0)
2007 				goto nla_put_failure;
2008 		} else {
2009 			goto cls_op_not_supp;
2010 		}
2011 	} else {
2012 		if (tp->ops->dump &&
2013 		    tp->ops->dump(net, tp, fh, skb, tcm, rtnl_held) < 0)
2014 			goto nla_put_failure;
2015 	}
2016 
2017 	if (extack && extack->_msg &&
2018 	    nla_put_string(skb, TCA_EXT_WARN_MSG, extack->_msg))
2019 		goto nla_put_failure;
2020 
2021 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2022 
2023 	return skb->len;
2024 
2025 cls_op_not_supp:
2026 	ret = -EOPNOTSUPP;
2027 out_nlmsg_trim:
2028 nla_put_failure:
2029 	nlmsg_trim(skb, b);
2030 	return ret;
2031 }
2032 
tfilter_notify_prep(struct net * net,struct sk_buff * oskb,struct nlmsghdr * n,struct tcf_proto * tp,struct tcf_block * block,struct Qdisc * q,u32 parent,void * fh,int event,u32 portid,bool rtnl_held,struct netlink_ext_ack * extack)2033 static struct sk_buff *tfilter_notify_prep(struct net *net,
2034 					   struct sk_buff *oskb,
2035 					   struct nlmsghdr *n,
2036 					   struct tcf_proto *tp,
2037 					   struct tcf_block *block,
2038 					   struct Qdisc *q, u32 parent,
2039 					   void *fh, int event,
2040 					   u32 portid, bool rtnl_held,
2041 					   struct netlink_ext_ack *extack)
2042 {
2043 	unsigned int size = oskb ? max(NLMSG_GOODSIZE, oskb->len) : NLMSG_GOODSIZE;
2044 	struct sk_buff *skb;
2045 	int ret;
2046 
2047 retry:
2048 	skb = alloc_skb(size, GFP_KERNEL);
2049 	if (!skb)
2050 		return ERR_PTR(-ENOBUFS);
2051 
2052 	ret = tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
2053 			    n->nlmsg_seq, n->nlmsg_flags, event, false,
2054 			    rtnl_held, extack);
2055 	if (ret <= 0) {
2056 		kfree_skb(skb);
2057 		if (ret == -EMSGSIZE) {
2058 			size += NLMSG_GOODSIZE;
2059 			goto retry;
2060 		}
2061 		return ERR_PTR(-EINVAL);
2062 	}
2063 	return skb;
2064 }
2065 
tfilter_notify(struct net * net,struct sk_buff * oskb,struct nlmsghdr * n,struct tcf_proto * tp,struct tcf_block * block,struct Qdisc * q,u32 parent,void * fh,int event,bool unicast,bool rtnl_held,struct netlink_ext_ack * extack)2066 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
2067 			  struct nlmsghdr *n, struct tcf_proto *tp,
2068 			  struct tcf_block *block, struct Qdisc *q,
2069 			  u32 parent, void *fh, int event, bool unicast,
2070 			  bool rtnl_held, struct netlink_ext_ack *extack)
2071 {
2072 	struct sk_buff *skb;
2073 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2074 	int err = 0;
2075 
2076 	if (!unicast && !rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC))
2077 		return 0;
2078 
2079 	skb = tfilter_notify_prep(net, oskb, n, tp, block, q, parent, fh, event,
2080 				  portid, rtnl_held, extack);
2081 	if (IS_ERR(skb))
2082 		return PTR_ERR(skb);
2083 
2084 	if (unicast)
2085 		err = rtnl_unicast(skb, net, portid);
2086 	else
2087 		err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
2088 				     n->nlmsg_flags & NLM_F_ECHO);
2089 	return err;
2090 }
2091 
tfilter_del_notify(struct net * net,struct sk_buff * oskb,struct nlmsghdr * n,struct tcf_proto * tp,struct tcf_block * block,struct Qdisc * q,u32 parent,void * fh,bool unicast,bool * last,bool rtnl_held,struct netlink_ext_ack * extack)2092 static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
2093 			      struct nlmsghdr *n, struct tcf_proto *tp,
2094 			      struct tcf_block *block, struct Qdisc *q,
2095 			      u32 parent, void *fh, bool unicast, bool *last,
2096 			      bool rtnl_held, struct netlink_ext_ack *extack)
2097 {
2098 	struct sk_buff *skb;
2099 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2100 	int err;
2101 
2102 	if (!rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC))
2103 		return tp->ops->delete(tp, fh, last, rtnl_held, extack);
2104 
2105 	skb = tfilter_notify_prep(net, oskb, n, tp, block, q, parent, fh,
2106 				  RTM_DELTFILTER, portid, rtnl_held, extack);
2107 	if (IS_ERR(skb)) {
2108 		NL_SET_ERR_MSG(extack, "Failed to build del event notification");
2109 		return PTR_ERR(skb);
2110 	}
2111 
2112 	err = tp->ops->delete(tp, fh, last, rtnl_held, extack);
2113 	if (err) {
2114 		kfree_skb(skb);
2115 		return err;
2116 	}
2117 
2118 	if (unicast)
2119 		err = rtnl_unicast(skb, net, portid);
2120 	else
2121 		err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
2122 				     n->nlmsg_flags & NLM_F_ECHO);
2123 	if (err < 0)
2124 		NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
2125 
2126 	return err;
2127 }
2128 
tfilter_notify_chain(struct net * net,struct sk_buff * oskb,struct tcf_block * block,struct Qdisc * q,u32 parent,struct nlmsghdr * n,struct tcf_chain * chain,int event,struct netlink_ext_ack * extack)2129 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
2130 				 struct tcf_block *block, struct Qdisc *q,
2131 				 u32 parent, struct nlmsghdr *n,
2132 				 struct tcf_chain *chain, int event,
2133 				 struct netlink_ext_ack *extack)
2134 {
2135 	struct tcf_proto *tp;
2136 
2137 	for (tp = tcf_get_next_proto(chain, NULL);
2138 	     tp; tp = tcf_get_next_proto(chain, tp))
2139 		tfilter_notify(net, oskb, n, tp, block, q, parent, NULL,
2140 			       event, false, true, extack);
2141 }
2142 
tfilter_put(struct tcf_proto * tp,void * fh)2143 static void tfilter_put(struct tcf_proto *tp, void *fh)
2144 {
2145 	if (tp->ops->put && fh)
2146 		tp->ops->put(tp, fh);
2147 }
2148 
is_qdisc_ingress(__u32 classid)2149 static bool is_qdisc_ingress(__u32 classid)
2150 {
2151 	return (TC_H_MIN(classid) == TC_H_MIN(TC_H_MIN_INGRESS));
2152 }
2153 
tc_new_tfilter(struct sk_buff * skb,struct nlmsghdr * n,struct netlink_ext_ack * extack)2154 static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2155 			  struct netlink_ext_ack *extack)
2156 {
2157 	struct net *net = sock_net(skb->sk);
2158 	struct nlattr *tca[TCA_MAX + 1];
2159 	char name[IFNAMSIZ];
2160 	struct tcmsg *t;
2161 	u32 protocol;
2162 	u32 prio;
2163 	bool prio_allocate;
2164 	u32 parent;
2165 	u32 chain_index;
2166 	struct Qdisc *q;
2167 	struct tcf_chain_info chain_info;
2168 	struct tcf_chain *chain;
2169 	struct tcf_block *block;
2170 	struct tcf_proto *tp;
2171 	unsigned long cl;
2172 	void *fh;
2173 	int err;
2174 	int tp_created;
2175 	bool rtnl_held = false;
2176 	u32 flags;
2177 
2178 replay:
2179 	tp_created = 0;
2180 
2181 	err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2182 				     rtm_tca_policy, extack);
2183 	if (err < 0)
2184 		return err;
2185 
2186 	t = nlmsg_data(n);
2187 	protocol = TC_H_MIN(t->tcm_info);
2188 	prio = TC_H_MAJ(t->tcm_info);
2189 	prio_allocate = false;
2190 	parent = t->tcm_parent;
2191 	tp = NULL;
2192 	cl = 0;
2193 	block = NULL;
2194 	q = NULL;
2195 	chain = NULL;
2196 	flags = 0;
2197 
2198 	if (prio == 0) {
2199 		/* If no priority is provided by the user,
2200 		 * we allocate one.
2201 		 */
2202 		if (n->nlmsg_flags & NLM_F_CREATE) {
2203 			prio = TC_H_MAKE(0x80000000U, 0U);
2204 			prio_allocate = true;
2205 		} else {
2206 			NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2207 			return -ENOENT;
2208 		}
2209 	}
2210 
2211 	/* Find head of filter chain. */
2212 
2213 	err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2214 	if (err)
2215 		return err;
2216 
2217 	if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2218 		NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2219 		err = -EINVAL;
2220 		goto errout;
2221 	}
2222 
2223 	/* Take rtnl mutex if rtnl_held was set to true on previous iteration,
2224 	 * block is shared (no qdisc found), qdisc is not unlocked, classifier
2225 	 * type is not specified, classifier is not unlocked.
2226 	 */
2227 	if (rtnl_held ||
2228 	    (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2229 	    !tcf_proto_is_unlocked(name)) {
2230 		rtnl_held = true;
2231 		rtnl_lock();
2232 	}
2233 
2234 	err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2235 	if (err)
2236 		goto errout;
2237 
2238 	block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2239 				 extack);
2240 	if (IS_ERR(block)) {
2241 		err = PTR_ERR(block);
2242 		goto errout;
2243 	}
2244 	block->classid = parent;
2245 
2246 	chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2247 	if (chain_index > TC_ACT_EXT_VAL_MASK) {
2248 		NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2249 		err = -EINVAL;
2250 		goto errout;
2251 	}
2252 	chain = tcf_chain_get(block, chain_index, true);
2253 	if (!chain) {
2254 		NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
2255 		err = -ENOMEM;
2256 		goto errout;
2257 	}
2258 
2259 	mutex_lock(&chain->filter_chain_lock);
2260 	tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2261 			       prio, prio_allocate);
2262 	if (IS_ERR(tp)) {
2263 		NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2264 		err = PTR_ERR(tp);
2265 		goto errout_locked;
2266 	}
2267 
2268 	if (tp == NULL) {
2269 		struct tcf_proto *tp_new = NULL;
2270 
2271 		if (chain->flushing) {
2272 			err = -EAGAIN;
2273 			goto errout_locked;
2274 		}
2275 
2276 		/* Proto-tcf does not exist, create new one */
2277 
2278 		if (tca[TCA_KIND] == NULL || !protocol) {
2279 			NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
2280 			err = -EINVAL;
2281 			goto errout_locked;
2282 		}
2283 
2284 		if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2285 			NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2286 			err = -ENOENT;
2287 			goto errout_locked;
2288 		}
2289 
2290 		if (prio_allocate)
2291 			prio = tcf_auto_prio(tcf_chain_tp_prev(chain,
2292 							       &chain_info));
2293 
2294 		mutex_unlock(&chain->filter_chain_lock);
2295 		tp_new = tcf_proto_create(name, protocol, prio, chain,
2296 					  rtnl_held, extack);
2297 		if (IS_ERR(tp_new)) {
2298 			err = PTR_ERR(tp_new);
2299 			goto errout_tp;
2300 		}
2301 
2302 		tp_created = 1;
2303 		tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio,
2304 						rtnl_held);
2305 		if (IS_ERR(tp)) {
2306 			err = PTR_ERR(tp);
2307 			goto errout_tp;
2308 		}
2309 	} else {
2310 		mutex_unlock(&chain->filter_chain_lock);
2311 	}
2312 
2313 	if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2314 		NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2315 		err = -EINVAL;
2316 		goto errout;
2317 	}
2318 
2319 	fh = tp->ops->get(tp, t->tcm_handle);
2320 
2321 	if (!fh) {
2322 		if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2323 			NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2324 			err = -ENOENT;
2325 			goto errout;
2326 		}
2327 	} else if (n->nlmsg_flags & NLM_F_EXCL) {
2328 		tfilter_put(tp, fh);
2329 		NL_SET_ERR_MSG(extack, "Filter already exists");
2330 		err = -EEXIST;
2331 		goto errout;
2332 	}
2333 
2334 	if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
2335 		tfilter_put(tp, fh);
2336 		NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
2337 		err = -EINVAL;
2338 		goto errout;
2339 	}
2340 
2341 	if (!(n->nlmsg_flags & NLM_F_CREATE))
2342 		flags |= TCA_ACT_FLAGS_REPLACE;
2343 	if (!rtnl_held)
2344 		flags |= TCA_ACT_FLAGS_NO_RTNL;
2345 	if (is_qdisc_ingress(parent))
2346 		flags |= TCA_ACT_FLAGS_AT_INGRESS;
2347 	err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
2348 			      flags, extack);
2349 	if (err == 0) {
2350 		tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2351 			       RTM_NEWTFILTER, false, rtnl_held, extack);
2352 		tfilter_put(tp, fh);
2353 		/* q pointer is NULL for shared blocks */
2354 		if (q)
2355 			q->flags &= ~TCQ_F_CAN_BYPASS;
2356 	}
2357 
2358 errout:
2359 	if (err && tp_created)
2360 		tcf_chain_tp_delete_empty(chain, tp, rtnl_held, NULL);
2361 errout_tp:
2362 	if (chain) {
2363 		if (tp && !IS_ERR(tp))
2364 			tcf_proto_put(tp, rtnl_held, NULL);
2365 		if (!tp_created)
2366 			tcf_chain_put(chain);
2367 	}
2368 	tcf_block_release(q, block, rtnl_held);
2369 
2370 	if (rtnl_held)
2371 		rtnl_unlock();
2372 
2373 	if (err == -EAGAIN) {
2374 		/* Take rtnl lock in case EAGAIN is caused by concurrent flush
2375 		 * of target chain.
2376 		 */
2377 		rtnl_held = true;
2378 		/* Replay the request. */
2379 		goto replay;
2380 	}
2381 	return err;
2382 
2383 errout_locked:
2384 	mutex_unlock(&chain->filter_chain_lock);
2385 	goto errout;
2386 }
2387 
tc_del_tfilter(struct sk_buff * skb,struct nlmsghdr * n,struct netlink_ext_ack * extack)2388 static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2389 			  struct netlink_ext_ack *extack)
2390 {
2391 	struct net *net = sock_net(skb->sk);
2392 	struct nlattr *tca[TCA_MAX + 1];
2393 	char name[IFNAMSIZ];
2394 	struct tcmsg *t;
2395 	u32 protocol;
2396 	u32 prio;
2397 	u32 parent;
2398 	u32 chain_index;
2399 	struct Qdisc *q = NULL;
2400 	struct tcf_chain_info chain_info;
2401 	struct tcf_chain *chain = NULL;
2402 	struct tcf_block *block = NULL;
2403 	struct tcf_proto *tp = NULL;
2404 	unsigned long cl = 0;
2405 	void *fh = NULL;
2406 	int err;
2407 	bool rtnl_held = false;
2408 
2409 	err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2410 				     rtm_tca_policy, extack);
2411 	if (err < 0)
2412 		return err;
2413 
2414 	t = nlmsg_data(n);
2415 	protocol = TC_H_MIN(t->tcm_info);
2416 	prio = TC_H_MAJ(t->tcm_info);
2417 	parent = t->tcm_parent;
2418 
2419 	if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
2420 		NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
2421 		return -ENOENT;
2422 	}
2423 
2424 	/* Find head of filter chain. */
2425 
2426 	err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2427 	if (err)
2428 		return err;
2429 
2430 	if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2431 		NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2432 		err = -EINVAL;
2433 		goto errout;
2434 	}
2435 	/* Take rtnl mutex if flushing whole chain, block is shared (no qdisc
2436 	 * found), qdisc is not unlocked, classifier type is not specified,
2437 	 * classifier is not unlocked.
2438 	 */
2439 	if (!prio ||
2440 	    (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2441 	    !tcf_proto_is_unlocked(name)) {
2442 		rtnl_held = true;
2443 		rtnl_lock();
2444 	}
2445 
2446 	err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2447 	if (err)
2448 		goto errout;
2449 
2450 	block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2451 				 extack);
2452 	if (IS_ERR(block)) {
2453 		err = PTR_ERR(block);
2454 		goto errout;
2455 	}
2456 
2457 	chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2458 	if (chain_index > TC_ACT_EXT_VAL_MASK) {
2459 		NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2460 		err = -EINVAL;
2461 		goto errout;
2462 	}
2463 	chain = tcf_chain_get(block, chain_index, false);
2464 	if (!chain) {
2465 		/* User requested flush on non-existent chain. Nothing to do,
2466 		 * so just return success.
2467 		 */
2468 		if (prio == 0) {
2469 			err = 0;
2470 			goto errout;
2471 		}
2472 		NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2473 		err = -ENOENT;
2474 		goto errout;
2475 	}
2476 
2477 	if (prio == 0) {
2478 		tfilter_notify_chain(net, skb, block, q, parent, n,
2479 				     chain, RTM_DELTFILTER, extack);
2480 		tcf_chain_flush(chain, rtnl_held);
2481 		err = 0;
2482 		goto errout;
2483 	}
2484 
2485 	mutex_lock(&chain->filter_chain_lock);
2486 	tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2487 			       prio, false);
2488 	if (!tp || IS_ERR(tp)) {
2489 		NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2490 		err = tp ? PTR_ERR(tp) : -ENOENT;
2491 		goto errout_locked;
2492 	} else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2493 		NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2494 		err = -EINVAL;
2495 		goto errout_locked;
2496 	} else if (t->tcm_handle == 0) {
2497 		tcf_proto_signal_destroying(chain, tp);
2498 		tcf_chain_tp_remove(chain, &chain_info, tp);
2499 		mutex_unlock(&chain->filter_chain_lock);
2500 
2501 		tcf_proto_put(tp, rtnl_held, NULL);
2502 		tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2503 			       RTM_DELTFILTER, false, rtnl_held, extack);
2504 		err = 0;
2505 		goto errout;
2506 	}
2507 	mutex_unlock(&chain->filter_chain_lock);
2508 
2509 	fh = tp->ops->get(tp, t->tcm_handle);
2510 
2511 	if (!fh) {
2512 		NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2513 		err = -ENOENT;
2514 	} else {
2515 		bool last;
2516 
2517 		err = tfilter_del_notify(net, skb, n, tp, block,
2518 					 q, parent, fh, false, &last,
2519 					 rtnl_held, extack);
2520 
2521 		if (err)
2522 			goto errout;
2523 		if (last)
2524 			tcf_chain_tp_delete_empty(chain, tp, rtnl_held, extack);
2525 	}
2526 
2527 errout:
2528 	if (chain) {
2529 		if (tp && !IS_ERR(tp))
2530 			tcf_proto_put(tp, rtnl_held, NULL);
2531 		tcf_chain_put(chain);
2532 	}
2533 	tcf_block_release(q, block, rtnl_held);
2534 
2535 	if (rtnl_held)
2536 		rtnl_unlock();
2537 
2538 	return err;
2539 
2540 errout_locked:
2541 	mutex_unlock(&chain->filter_chain_lock);
2542 	goto errout;
2543 }
2544 
tc_get_tfilter(struct sk_buff * skb,struct nlmsghdr * n,struct netlink_ext_ack * extack)2545 static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2546 			  struct netlink_ext_ack *extack)
2547 {
2548 	struct net *net = sock_net(skb->sk);
2549 	struct nlattr *tca[TCA_MAX + 1];
2550 	char name[IFNAMSIZ];
2551 	struct tcmsg *t;
2552 	u32 protocol;
2553 	u32 prio;
2554 	u32 parent;
2555 	u32 chain_index;
2556 	struct Qdisc *q = NULL;
2557 	struct tcf_chain_info chain_info;
2558 	struct tcf_chain *chain = NULL;
2559 	struct tcf_block *block = NULL;
2560 	struct tcf_proto *tp = NULL;
2561 	unsigned long cl = 0;
2562 	void *fh = NULL;
2563 	int err;
2564 	bool rtnl_held = false;
2565 
2566 	err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2567 				     rtm_tca_policy, extack);
2568 	if (err < 0)
2569 		return err;
2570 
2571 	t = nlmsg_data(n);
2572 	protocol = TC_H_MIN(t->tcm_info);
2573 	prio = TC_H_MAJ(t->tcm_info);
2574 	parent = t->tcm_parent;
2575 
2576 	if (prio == 0) {
2577 		NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2578 		return -ENOENT;
2579 	}
2580 
2581 	/* Find head of filter chain. */
2582 
2583 	err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2584 	if (err)
2585 		return err;
2586 
2587 	if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2588 		NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2589 		err = -EINVAL;
2590 		goto errout;
2591 	}
2592 	/* Take rtnl mutex if block is shared (no qdisc found), qdisc is not
2593 	 * unlocked, classifier type is not specified, classifier is not
2594 	 * unlocked.
2595 	 */
2596 	if ((q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2597 	    !tcf_proto_is_unlocked(name)) {
2598 		rtnl_held = true;
2599 		rtnl_lock();
2600 	}
2601 
2602 	err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2603 	if (err)
2604 		goto errout;
2605 
2606 	block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2607 				 extack);
2608 	if (IS_ERR(block)) {
2609 		err = PTR_ERR(block);
2610 		goto errout;
2611 	}
2612 
2613 	chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2614 	if (chain_index > TC_ACT_EXT_VAL_MASK) {
2615 		NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2616 		err = -EINVAL;
2617 		goto errout;
2618 	}
2619 	chain = tcf_chain_get(block, chain_index, false);
2620 	if (!chain) {
2621 		NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2622 		err = -EINVAL;
2623 		goto errout;
2624 	}
2625 
2626 	mutex_lock(&chain->filter_chain_lock);
2627 	tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2628 			       prio, false);
2629 	mutex_unlock(&chain->filter_chain_lock);
2630 	if (!tp || IS_ERR(tp)) {
2631 		NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2632 		err = tp ? PTR_ERR(tp) : -ENOENT;
2633 		goto errout;
2634 	} else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2635 		NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2636 		err = -EINVAL;
2637 		goto errout;
2638 	}
2639 
2640 	fh = tp->ops->get(tp, t->tcm_handle);
2641 
2642 	if (!fh) {
2643 		NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2644 		err = -ENOENT;
2645 	} else {
2646 		err = tfilter_notify(net, skb, n, tp, block, q, parent,
2647 				     fh, RTM_NEWTFILTER, true, rtnl_held, NULL);
2648 		if (err < 0)
2649 			NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
2650 	}
2651 
2652 	tfilter_put(tp, fh);
2653 errout:
2654 	if (chain) {
2655 		if (tp && !IS_ERR(tp))
2656 			tcf_proto_put(tp, rtnl_held, NULL);
2657 		tcf_chain_put(chain);
2658 	}
2659 	tcf_block_release(q, block, rtnl_held);
2660 
2661 	if (rtnl_held)
2662 		rtnl_unlock();
2663 
2664 	return err;
2665 }
2666 
2667 struct tcf_dump_args {
2668 	struct tcf_walker w;
2669 	struct sk_buff *skb;
2670 	struct netlink_callback *cb;
2671 	struct tcf_block *block;
2672 	struct Qdisc *q;
2673 	u32 parent;
2674 	bool terse_dump;
2675 };
2676 
tcf_node_dump(struct tcf_proto * tp,void * n,struct tcf_walker * arg)2677 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
2678 {
2679 	struct tcf_dump_args *a = (void *)arg;
2680 	struct net *net = sock_net(a->skb->sk);
2681 
2682 	return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
2683 			     n, NETLINK_CB(a->cb->skb).portid,
2684 			     a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
2685 			     RTM_NEWTFILTER, a->terse_dump, true, NULL);
2686 }
2687 
tcf_chain_dump(struct tcf_chain * chain,struct Qdisc * q,u32 parent,struct sk_buff * skb,struct netlink_callback * cb,long index_start,long * p_index,bool terse)2688 static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
2689 			   struct sk_buff *skb, struct netlink_callback *cb,
2690 			   long index_start, long *p_index, bool terse)
2691 {
2692 	struct net *net = sock_net(skb->sk);
2693 	struct tcf_block *block = chain->block;
2694 	struct tcmsg *tcm = nlmsg_data(cb->nlh);
2695 	struct tcf_proto *tp, *tp_prev;
2696 	struct tcf_dump_args arg;
2697 
2698 	for (tp = __tcf_get_next_proto(chain, NULL);
2699 	     tp;
2700 	     tp_prev = tp,
2701 		     tp = __tcf_get_next_proto(chain, tp),
2702 		     tcf_proto_put(tp_prev, true, NULL),
2703 		     (*p_index)++) {
2704 		if (*p_index < index_start)
2705 			continue;
2706 		if (TC_H_MAJ(tcm->tcm_info) &&
2707 		    TC_H_MAJ(tcm->tcm_info) != tp->prio)
2708 			continue;
2709 		if (TC_H_MIN(tcm->tcm_info) &&
2710 		    TC_H_MIN(tcm->tcm_info) != tp->protocol)
2711 			continue;
2712 		if (*p_index > index_start)
2713 			memset(&cb->args[1], 0,
2714 			       sizeof(cb->args) - sizeof(cb->args[0]));
2715 		if (cb->args[1] == 0) {
2716 			if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
2717 					  NETLINK_CB(cb->skb).portid,
2718 					  cb->nlh->nlmsg_seq, NLM_F_MULTI,
2719 					  RTM_NEWTFILTER, false, true, NULL) <= 0)
2720 				goto errout;
2721 			cb->args[1] = 1;
2722 		}
2723 		if (!tp->ops->walk)
2724 			continue;
2725 		arg.w.fn = tcf_node_dump;
2726 		arg.skb = skb;
2727 		arg.cb = cb;
2728 		arg.block = block;
2729 		arg.q = q;
2730 		arg.parent = parent;
2731 		arg.w.stop = 0;
2732 		arg.w.skip = cb->args[1] - 1;
2733 		arg.w.count = 0;
2734 		arg.w.cookie = cb->args[2];
2735 		arg.terse_dump = terse;
2736 		tp->ops->walk(tp, &arg.w, true);
2737 		cb->args[2] = arg.w.cookie;
2738 		cb->args[1] = arg.w.count + 1;
2739 		if (arg.w.stop)
2740 			goto errout;
2741 	}
2742 	return true;
2743 
2744 errout:
2745 	tcf_proto_put(tp, true, NULL);
2746 	return false;
2747 }
2748 
2749 static const struct nla_policy tcf_tfilter_dump_policy[TCA_MAX + 1] = {
2750 	[TCA_DUMP_FLAGS] = NLA_POLICY_BITFIELD32(TCA_DUMP_FLAGS_TERSE),
2751 };
2752 
2753 /* called with RTNL */
tc_dump_tfilter(struct sk_buff * skb,struct netlink_callback * cb)2754 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
2755 {
2756 	struct tcf_chain *chain, *chain_prev;
2757 	struct net *net = sock_net(skb->sk);
2758 	struct nlattr *tca[TCA_MAX + 1];
2759 	struct Qdisc *q = NULL;
2760 	struct tcf_block *block;
2761 	struct tcmsg *tcm = nlmsg_data(cb->nlh);
2762 	bool terse_dump = false;
2763 	long index_start;
2764 	long index;
2765 	u32 parent;
2766 	int err;
2767 
2768 	if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2769 		return skb->len;
2770 
2771 	err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
2772 				     tcf_tfilter_dump_policy, cb->extack);
2773 	if (err)
2774 		return err;
2775 
2776 	if (tca[TCA_DUMP_FLAGS]) {
2777 		struct nla_bitfield32 flags =
2778 			nla_get_bitfield32(tca[TCA_DUMP_FLAGS]);
2779 
2780 		terse_dump = flags.value & TCA_DUMP_FLAGS_TERSE;
2781 	}
2782 
2783 	if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2784 		block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2785 		if (!block)
2786 			goto out;
2787 		/* If we work with block index, q is NULL and parent value
2788 		 * will never be used in the following code. The check
2789 		 * in tcf_fill_node prevents it. However, compiler does not
2790 		 * see that far, so set parent to zero to silence the warning
2791 		 * about parent being uninitialized.
2792 		 */
2793 		parent = 0;
2794 	} else {
2795 		const struct Qdisc_class_ops *cops;
2796 		struct net_device *dev;
2797 		unsigned long cl = 0;
2798 
2799 		dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2800 		if (!dev)
2801 			return skb->len;
2802 
2803 		parent = tcm->tcm_parent;
2804 		if (!parent)
2805 			q = rtnl_dereference(dev->qdisc);
2806 		else
2807 			q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2808 		if (!q)
2809 			goto out;
2810 		cops = q->ops->cl_ops;
2811 		if (!cops)
2812 			goto out;
2813 		if (!cops->tcf_block)
2814 			goto out;
2815 		if (TC_H_MIN(tcm->tcm_parent)) {
2816 			cl = cops->find(q, tcm->tcm_parent);
2817 			if (cl == 0)
2818 				goto out;
2819 		}
2820 		block = cops->tcf_block(q, cl, NULL);
2821 		if (!block)
2822 			goto out;
2823 		parent = block->classid;
2824 		if (tcf_block_shared(block))
2825 			q = NULL;
2826 	}
2827 
2828 	index_start = cb->args[0];
2829 	index = 0;
2830 
2831 	for (chain = __tcf_get_next_chain(block, NULL);
2832 	     chain;
2833 	     chain_prev = chain,
2834 		     chain = __tcf_get_next_chain(block, chain),
2835 		     tcf_chain_put(chain_prev)) {
2836 		if (tca[TCA_CHAIN] &&
2837 		    nla_get_u32(tca[TCA_CHAIN]) != chain->index)
2838 			continue;
2839 		if (!tcf_chain_dump(chain, q, parent, skb, cb,
2840 				    index_start, &index, terse_dump)) {
2841 			tcf_chain_put(chain);
2842 			err = -EMSGSIZE;
2843 			break;
2844 		}
2845 	}
2846 
2847 	if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
2848 		tcf_block_refcnt_put(block, true);
2849 	cb->args[0] = index;
2850 
2851 out:
2852 	/* If we did no progress, the error (EMSGSIZE) is real */
2853 	if (skb->len == 0 && err)
2854 		return err;
2855 	return skb->len;
2856 }
2857 
tc_chain_fill_node(const struct tcf_proto_ops * tmplt_ops,void * tmplt_priv,u32 chain_index,struct net * net,struct sk_buff * skb,struct tcf_block * block,u32 portid,u32 seq,u16 flags,int event,struct netlink_ext_ack * extack)2858 static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops,
2859 			      void *tmplt_priv, u32 chain_index,
2860 			      struct net *net, struct sk_buff *skb,
2861 			      struct tcf_block *block,
2862 			      u32 portid, u32 seq, u16 flags, int event,
2863 			      struct netlink_ext_ack *extack)
2864 {
2865 	unsigned char *b = skb_tail_pointer(skb);
2866 	const struct tcf_proto_ops *ops;
2867 	struct nlmsghdr *nlh;
2868 	struct tcmsg *tcm;
2869 	void *priv;
2870 
2871 	ops = tmplt_ops;
2872 	priv = tmplt_priv;
2873 
2874 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
2875 	if (!nlh)
2876 		goto out_nlmsg_trim;
2877 	tcm = nlmsg_data(nlh);
2878 	tcm->tcm_family = AF_UNSPEC;
2879 	tcm->tcm__pad1 = 0;
2880 	tcm->tcm__pad2 = 0;
2881 	tcm->tcm_handle = 0;
2882 	if (block->q) {
2883 		tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
2884 		tcm->tcm_parent = block->q->handle;
2885 	} else {
2886 		tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
2887 		tcm->tcm_block_index = block->index;
2888 	}
2889 
2890 	if (nla_put_u32(skb, TCA_CHAIN, chain_index))
2891 		goto nla_put_failure;
2892 
2893 	if (ops) {
2894 		if (nla_put_string(skb, TCA_KIND, ops->kind))
2895 			goto nla_put_failure;
2896 		if (ops->tmplt_dump(skb, net, priv) < 0)
2897 			goto nla_put_failure;
2898 	}
2899 
2900 	if (extack && extack->_msg &&
2901 	    nla_put_string(skb, TCA_EXT_WARN_MSG, extack->_msg))
2902 		goto out_nlmsg_trim;
2903 
2904 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2905 
2906 	return skb->len;
2907 
2908 out_nlmsg_trim:
2909 nla_put_failure:
2910 	nlmsg_trim(skb, b);
2911 	return -EMSGSIZE;
2912 }
2913 
tc_chain_notify(struct tcf_chain * chain,struct sk_buff * oskb,u32 seq,u16 flags,int event,bool unicast,struct netlink_ext_ack * extack)2914 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
2915 			   u32 seq, u16 flags, int event, bool unicast,
2916 			   struct netlink_ext_ack *extack)
2917 {
2918 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2919 	struct tcf_block *block = chain->block;
2920 	struct net *net = block->net;
2921 	struct sk_buff *skb;
2922 	int err = 0;
2923 
2924 	if (!unicast && !rtnl_notify_needed(net, flags, RTNLGRP_TC))
2925 		return 0;
2926 
2927 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2928 	if (!skb)
2929 		return -ENOBUFS;
2930 
2931 	if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
2932 			       chain->index, net, skb, block, portid,
2933 			       seq, flags, event, extack) <= 0) {
2934 		kfree_skb(skb);
2935 		return -EINVAL;
2936 	}
2937 
2938 	if (unicast)
2939 		err = rtnl_unicast(skb, net, portid);
2940 	else
2941 		err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
2942 				     flags & NLM_F_ECHO);
2943 
2944 	return err;
2945 }
2946 
tc_chain_notify_delete(const struct tcf_proto_ops * tmplt_ops,void * tmplt_priv,u32 chain_index,struct tcf_block * block,struct sk_buff * oskb,u32 seq,u16 flags,bool unicast)2947 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
2948 				  void *tmplt_priv, u32 chain_index,
2949 				  struct tcf_block *block, struct sk_buff *oskb,
2950 				  u32 seq, u16 flags, bool unicast)
2951 {
2952 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2953 	struct net *net = block->net;
2954 	struct sk_buff *skb;
2955 
2956 	if (!rtnl_notify_needed(net, flags, RTNLGRP_TC))
2957 		return 0;
2958 
2959 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2960 	if (!skb)
2961 		return -ENOBUFS;
2962 
2963 	if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb,
2964 			       block, portid, seq, flags, RTM_DELCHAIN, NULL) <= 0) {
2965 		kfree_skb(skb);
2966 		return -EINVAL;
2967 	}
2968 
2969 	if (unicast)
2970 		return rtnl_unicast(skb, net, portid);
2971 
2972 	return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
2973 }
2974 
tc_chain_tmplt_add(struct tcf_chain * chain,struct net * net,struct nlattr ** tca,struct netlink_ext_ack * extack)2975 static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
2976 			      struct nlattr **tca,
2977 			      struct netlink_ext_ack *extack)
2978 {
2979 	const struct tcf_proto_ops *ops;
2980 	char name[IFNAMSIZ];
2981 	void *tmplt_priv;
2982 
2983 	/* If kind is not set, user did not specify template. */
2984 	if (!tca[TCA_KIND])
2985 		return 0;
2986 
2987 	if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2988 		NL_SET_ERR_MSG(extack, "Specified TC chain template name too long");
2989 		return -EINVAL;
2990 	}
2991 
2992 	ops = tcf_proto_lookup_ops(name, true, extack);
2993 	if (IS_ERR(ops))
2994 		return PTR_ERR(ops);
2995 	if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump ||
2996 	    !ops->tmplt_reoffload) {
2997 		NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
2998 		module_put(ops->owner);
2999 		return -EOPNOTSUPP;
3000 	}
3001 
3002 	tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
3003 	if (IS_ERR(tmplt_priv)) {
3004 		module_put(ops->owner);
3005 		return PTR_ERR(tmplt_priv);
3006 	}
3007 	chain->tmplt_ops = ops;
3008 	chain->tmplt_priv = tmplt_priv;
3009 	return 0;
3010 }
3011 
tc_chain_tmplt_del(const struct tcf_proto_ops * tmplt_ops,void * tmplt_priv)3012 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
3013 			       void *tmplt_priv)
3014 {
3015 	/* If template ops are set, no work to do for us. */
3016 	if (!tmplt_ops)
3017 		return;
3018 
3019 	tmplt_ops->tmplt_destroy(tmplt_priv);
3020 	module_put(tmplt_ops->owner);
3021 }
3022 
3023 /* Add/delete/get a chain */
3024 
tc_ctl_chain(struct sk_buff * skb,struct nlmsghdr * n,struct netlink_ext_ack * extack)3025 static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
3026 			struct netlink_ext_ack *extack)
3027 {
3028 	struct net *net = sock_net(skb->sk);
3029 	struct nlattr *tca[TCA_MAX + 1];
3030 	struct tcmsg *t;
3031 	u32 parent;
3032 	u32 chain_index;
3033 	struct Qdisc *q;
3034 	struct tcf_chain *chain;
3035 	struct tcf_block *block;
3036 	unsigned long cl;
3037 	int err;
3038 
3039 replay:
3040 	q = NULL;
3041 	err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
3042 				     rtm_tca_policy, extack);
3043 	if (err < 0)
3044 		return err;
3045 
3046 	t = nlmsg_data(n);
3047 	parent = t->tcm_parent;
3048 	cl = 0;
3049 
3050 	block = tcf_block_find(net, &q, &parent, &cl,
3051 			       t->tcm_ifindex, t->tcm_block_index, extack);
3052 	if (IS_ERR(block))
3053 		return PTR_ERR(block);
3054 
3055 	chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
3056 	if (chain_index > TC_ACT_EXT_VAL_MASK) {
3057 		NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
3058 		err = -EINVAL;
3059 		goto errout_block;
3060 	}
3061 
3062 	mutex_lock(&block->lock);
3063 	chain = tcf_chain_lookup(block, chain_index);
3064 	if (n->nlmsg_type == RTM_NEWCHAIN) {
3065 		if (chain) {
3066 			if (tcf_chain_held_by_acts_only(chain)) {
3067 				/* The chain exists only because there is
3068 				 * some action referencing it.
3069 				 */
3070 				tcf_chain_hold(chain);
3071 			} else {
3072 				NL_SET_ERR_MSG(extack, "Filter chain already exists");
3073 				err = -EEXIST;
3074 				goto errout_block_locked;
3075 			}
3076 		} else {
3077 			if (!(n->nlmsg_flags & NLM_F_CREATE)) {
3078 				NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
3079 				err = -ENOENT;
3080 				goto errout_block_locked;
3081 			}
3082 			chain = tcf_chain_create(block, chain_index);
3083 			if (!chain) {
3084 				NL_SET_ERR_MSG(extack, "Failed to create filter chain");
3085 				err = -ENOMEM;
3086 				goto errout_block_locked;
3087 			}
3088 		}
3089 	} else {
3090 		if (!chain || tcf_chain_held_by_acts_only(chain)) {
3091 			NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
3092 			err = -EINVAL;
3093 			goto errout_block_locked;
3094 		}
3095 		tcf_chain_hold(chain);
3096 	}
3097 
3098 	if (n->nlmsg_type == RTM_NEWCHAIN) {
3099 		/* Modifying chain requires holding parent block lock. In case
3100 		 * the chain was successfully added, take a reference to the
3101 		 * chain. This ensures that an empty chain does not disappear at
3102 		 * the end of this function.
3103 		 */
3104 		tcf_chain_hold(chain);
3105 		chain->explicitly_created = true;
3106 	}
3107 	mutex_unlock(&block->lock);
3108 
3109 	switch (n->nlmsg_type) {
3110 	case RTM_NEWCHAIN:
3111 		err = tc_chain_tmplt_add(chain, net, tca, extack);
3112 		if (err) {
3113 			tcf_chain_put_explicitly_created(chain);
3114 			goto errout;
3115 		}
3116 
3117 		tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
3118 				RTM_NEWCHAIN, false, extack);
3119 		break;
3120 	case RTM_DELCHAIN:
3121 		tfilter_notify_chain(net, skb, block, q, parent, n,
3122 				     chain, RTM_DELTFILTER, extack);
3123 		/* Flush the chain first as the user requested chain removal. */
3124 		tcf_chain_flush(chain, true);
3125 		/* In case the chain was successfully deleted, put a reference
3126 		 * to the chain previously taken during addition.
3127 		 */
3128 		tcf_chain_put_explicitly_created(chain);
3129 		break;
3130 	case RTM_GETCHAIN:
3131 		err = tc_chain_notify(chain, skb, n->nlmsg_seq,
3132 				      n->nlmsg_flags, n->nlmsg_type, true, extack);
3133 		if (err < 0)
3134 			NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
3135 		break;
3136 	default:
3137 		err = -EOPNOTSUPP;
3138 		NL_SET_ERR_MSG(extack, "Unsupported message type");
3139 		goto errout;
3140 	}
3141 
3142 errout:
3143 	tcf_chain_put(chain);
3144 errout_block:
3145 	tcf_block_release(q, block, true);
3146 	if (err == -EAGAIN)
3147 		/* Replay the request. */
3148 		goto replay;
3149 	return err;
3150 
3151 errout_block_locked:
3152 	mutex_unlock(&block->lock);
3153 	goto errout_block;
3154 }
3155 
3156 /* called with RTNL */
tc_dump_chain(struct sk_buff * skb,struct netlink_callback * cb)3157 static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
3158 {
3159 	struct net *net = sock_net(skb->sk);
3160 	struct nlattr *tca[TCA_MAX + 1];
3161 	struct Qdisc *q = NULL;
3162 	struct tcf_block *block;
3163 	struct tcmsg *tcm = nlmsg_data(cb->nlh);
3164 	struct tcf_chain *chain;
3165 	long index_start;
3166 	long index;
3167 	int err;
3168 
3169 	if (nlmsg_len(cb->nlh) < sizeof(*tcm))
3170 		return skb->len;
3171 
3172 	err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
3173 				     rtm_tca_policy, cb->extack);
3174 	if (err)
3175 		return err;
3176 
3177 	if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
3178 		block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
3179 		if (!block)
3180 			goto out;
3181 	} else {
3182 		const struct Qdisc_class_ops *cops;
3183 		struct net_device *dev;
3184 		unsigned long cl = 0;
3185 
3186 		dev = __dev_get_by_index(net, tcm->tcm_ifindex);
3187 		if (!dev)
3188 			return skb->len;
3189 
3190 		if (!tcm->tcm_parent)
3191 			q = rtnl_dereference(dev->qdisc);
3192 		else
3193 			q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
3194 
3195 		if (!q)
3196 			goto out;
3197 		cops = q->ops->cl_ops;
3198 		if (!cops)
3199 			goto out;
3200 		if (!cops->tcf_block)
3201 			goto out;
3202 		if (TC_H_MIN(tcm->tcm_parent)) {
3203 			cl = cops->find(q, tcm->tcm_parent);
3204 			if (cl == 0)
3205 				goto out;
3206 		}
3207 		block = cops->tcf_block(q, cl, NULL);
3208 		if (!block)
3209 			goto out;
3210 		if (tcf_block_shared(block))
3211 			q = NULL;
3212 	}
3213 
3214 	index_start = cb->args[0];
3215 	index = 0;
3216 
3217 	mutex_lock(&block->lock);
3218 	list_for_each_entry(chain, &block->chain_list, list) {
3219 		if ((tca[TCA_CHAIN] &&
3220 		     nla_get_u32(tca[TCA_CHAIN]) != chain->index))
3221 			continue;
3222 		if (index < index_start) {
3223 			index++;
3224 			continue;
3225 		}
3226 		if (tcf_chain_held_by_acts_only(chain))
3227 			continue;
3228 		err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
3229 					 chain->index, net, skb, block,
3230 					 NETLINK_CB(cb->skb).portid,
3231 					 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3232 					 RTM_NEWCHAIN, NULL);
3233 		if (err <= 0)
3234 			break;
3235 		index++;
3236 	}
3237 	mutex_unlock(&block->lock);
3238 
3239 	if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
3240 		tcf_block_refcnt_put(block, true);
3241 	cb->args[0] = index;
3242 
3243 out:
3244 	/* If we did no progress, the error (EMSGSIZE) is real */
3245 	if (skb->len == 0 && err)
3246 		return err;
3247 	return skb->len;
3248 }
3249 
tcf_exts_init_ex(struct tcf_exts * exts,struct net * net,int action,int police,struct tcf_proto * tp,u32 handle,bool use_action_miss)3250 int tcf_exts_init_ex(struct tcf_exts *exts, struct net *net, int action,
3251 		     int police, struct tcf_proto *tp, u32 handle,
3252 		     bool use_action_miss)
3253 {
3254 	int err = 0;
3255 
3256 #ifdef CONFIG_NET_CLS_ACT
3257 	exts->type = 0;
3258 	exts->nr_actions = 0;
3259 	exts->miss_cookie_node = NULL;
3260 	/* Note: we do not own yet a reference on net.
3261 	 * This reference might be taken later from tcf_exts_get_net().
3262 	 */
3263 	exts->net = net;
3264 	exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
3265 				GFP_KERNEL);
3266 	if (!exts->actions)
3267 		return -ENOMEM;
3268 #endif
3269 
3270 	exts->action = action;
3271 	exts->police = police;
3272 
3273 	if (!use_action_miss)
3274 		return 0;
3275 
3276 	err = tcf_exts_miss_cookie_base_alloc(exts, tp, handle);
3277 	if (err)
3278 		goto err_miss_alloc;
3279 
3280 	return 0;
3281 
3282 err_miss_alloc:
3283 	tcf_exts_destroy(exts);
3284 #ifdef CONFIG_NET_CLS_ACT
3285 	exts->actions = NULL;
3286 #endif
3287 	return err;
3288 }
3289 EXPORT_SYMBOL(tcf_exts_init_ex);
3290 
tcf_exts_destroy(struct tcf_exts * exts)3291 void tcf_exts_destroy(struct tcf_exts *exts)
3292 {
3293 	tcf_exts_miss_cookie_base_destroy(exts);
3294 
3295 #ifdef CONFIG_NET_CLS_ACT
3296 	if (exts->actions) {
3297 		tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
3298 		kfree(exts->actions);
3299 	}
3300 	exts->nr_actions = 0;
3301 #endif
3302 }
3303 EXPORT_SYMBOL(tcf_exts_destroy);
3304 
tcf_exts_validate_ex(struct net * net,struct tcf_proto * tp,struct nlattr ** tb,struct nlattr * rate_tlv,struct tcf_exts * exts,u32 flags,u32 fl_flags,struct netlink_ext_ack * extack)3305 int tcf_exts_validate_ex(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
3306 			 struct nlattr *rate_tlv, struct tcf_exts *exts,
3307 			 u32 flags, u32 fl_flags, struct netlink_ext_ack *extack)
3308 {
3309 #ifdef CONFIG_NET_CLS_ACT
3310 	{
3311 		int init_res[TCA_ACT_MAX_PRIO] = {};
3312 		struct tc_action *act;
3313 		size_t attr_size = 0;
3314 
3315 		if (exts->police && tb[exts->police]) {
3316 			struct tc_action_ops *a_o;
3317 
3318 			a_o = tc_action_load_ops(tb[exts->police], true,
3319 						 !(flags & TCA_ACT_FLAGS_NO_RTNL),
3320 						 extack);
3321 			if (IS_ERR(a_o))
3322 				return PTR_ERR(a_o);
3323 			flags |= TCA_ACT_FLAGS_POLICE | TCA_ACT_FLAGS_BIND;
3324 			act = tcf_action_init_1(net, tp, tb[exts->police],
3325 						rate_tlv, a_o, init_res, flags,
3326 						extack);
3327 			module_put(a_o->owner);
3328 			if (IS_ERR(act))
3329 				return PTR_ERR(act);
3330 
3331 			act->type = exts->type = TCA_OLD_COMPAT;
3332 			exts->actions[0] = act;
3333 			exts->nr_actions = 1;
3334 			tcf_idr_insert_many(exts->actions);
3335 		} else if (exts->action && tb[exts->action]) {
3336 			int err;
3337 
3338 			flags |= TCA_ACT_FLAGS_BIND;
3339 			err = tcf_action_init(net, tp, tb[exts->action],
3340 					      rate_tlv, exts->actions, init_res,
3341 					      &attr_size, flags, fl_flags,
3342 					      extack);
3343 			if (err < 0)
3344 				return err;
3345 			exts->nr_actions = err;
3346 		}
3347 	}
3348 #else
3349 	if ((exts->action && tb[exts->action]) ||
3350 	    (exts->police && tb[exts->police])) {
3351 		NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
3352 		return -EOPNOTSUPP;
3353 	}
3354 #endif
3355 
3356 	return 0;
3357 }
3358 EXPORT_SYMBOL(tcf_exts_validate_ex);
3359 
tcf_exts_validate(struct net * net,struct tcf_proto * tp,struct nlattr ** tb,struct nlattr * rate_tlv,struct tcf_exts * exts,u32 flags,struct netlink_ext_ack * extack)3360 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
3361 		      struct nlattr *rate_tlv, struct tcf_exts *exts,
3362 		      u32 flags, struct netlink_ext_ack *extack)
3363 {
3364 	return tcf_exts_validate_ex(net, tp, tb, rate_tlv, exts,
3365 				    flags, 0, extack);
3366 }
3367 EXPORT_SYMBOL(tcf_exts_validate);
3368 
tcf_exts_change(struct tcf_exts * dst,struct tcf_exts * src)3369 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
3370 {
3371 #ifdef CONFIG_NET_CLS_ACT
3372 	struct tcf_exts old = *dst;
3373 
3374 	*dst = *src;
3375 	tcf_exts_destroy(&old);
3376 #endif
3377 }
3378 EXPORT_SYMBOL(tcf_exts_change);
3379 
3380 #ifdef CONFIG_NET_CLS_ACT
tcf_exts_first_act(struct tcf_exts * exts)3381 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
3382 {
3383 	if (exts->nr_actions == 0)
3384 		return NULL;
3385 	else
3386 		return exts->actions[0];
3387 }
3388 #endif
3389 
tcf_exts_dump(struct sk_buff * skb,struct tcf_exts * exts)3390 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
3391 {
3392 #ifdef CONFIG_NET_CLS_ACT
3393 	struct nlattr *nest;
3394 
3395 	if (exts->action && tcf_exts_has_actions(exts)) {
3396 		/*
3397 		 * again for backward compatible mode - we want
3398 		 * to work with both old and new modes of entering
3399 		 * tc data even if iproute2  was newer - jhs
3400 		 */
3401 		if (exts->type != TCA_OLD_COMPAT) {
3402 			nest = nla_nest_start_noflag(skb, exts->action);
3403 			if (nest == NULL)
3404 				goto nla_put_failure;
3405 
3406 			if (tcf_action_dump(skb, exts->actions, 0, 0, false)
3407 			    < 0)
3408 				goto nla_put_failure;
3409 			nla_nest_end(skb, nest);
3410 		} else if (exts->police) {
3411 			struct tc_action *act = tcf_exts_first_act(exts);
3412 			nest = nla_nest_start_noflag(skb, exts->police);
3413 			if (nest == NULL || !act)
3414 				goto nla_put_failure;
3415 			if (tcf_action_dump_old(skb, act, 0, 0) < 0)
3416 				goto nla_put_failure;
3417 			nla_nest_end(skb, nest);
3418 		}
3419 	}
3420 	return 0;
3421 
3422 nla_put_failure:
3423 	nla_nest_cancel(skb, nest);
3424 	return -1;
3425 #else
3426 	return 0;
3427 #endif
3428 }
3429 EXPORT_SYMBOL(tcf_exts_dump);
3430 
tcf_exts_terse_dump(struct sk_buff * skb,struct tcf_exts * exts)3431 int tcf_exts_terse_dump(struct sk_buff *skb, struct tcf_exts *exts)
3432 {
3433 #ifdef CONFIG_NET_CLS_ACT
3434 	struct nlattr *nest;
3435 
3436 	if (!exts->action || !tcf_exts_has_actions(exts))
3437 		return 0;
3438 
3439 	nest = nla_nest_start_noflag(skb, exts->action);
3440 	if (!nest)
3441 		goto nla_put_failure;
3442 
3443 	if (tcf_action_dump(skb, exts->actions, 0, 0, true) < 0)
3444 		goto nla_put_failure;
3445 	nla_nest_end(skb, nest);
3446 	return 0;
3447 
3448 nla_put_failure:
3449 	nla_nest_cancel(skb, nest);
3450 	return -1;
3451 #else
3452 	return 0;
3453 #endif
3454 }
3455 EXPORT_SYMBOL(tcf_exts_terse_dump);
3456 
tcf_exts_dump_stats(struct sk_buff * skb,struct tcf_exts * exts)3457 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
3458 {
3459 #ifdef CONFIG_NET_CLS_ACT
3460 	struct tc_action *a = tcf_exts_first_act(exts);
3461 	if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
3462 		return -1;
3463 #endif
3464 	return 0;
3465 }
3466 EXPORT_SYMBOL(tcf_exts_dump_stats);
3467 
tcf_block_offload_inc(struct tcf_block * block,u32 * flags)3468 static void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
3469 {
3470 	if (*flags & TCA_CLS_FLAGS_IN_HW)
3471 		return;
3472 	*flags |= TCA_CLS_FLAGS_IN_HW;
3473 	atomic_inc(&block->offloadcnt);
3474 }
3475 
tcf_block_offload_dec(struct tcf_block * block,u32 * flags)3476 static void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
3477 {
3478 	if (!(*flags & TCA_CLS_FLAGS_IN_HW))
3479 		return;
3480 	*flags &= ~TCA_CLS_FLAGS_IN_HW;
3481 	atomic_dec(&block->offloadcnt);
3482 }
3483 
tc_cls_offload_cnt_update(struct tcf_block * block,struct tcf_proto * tp,u32 * cnt,u32 * flags,u32 diff,bool add)3484 static void tc_cls_offload_cnt_update(struct tcf_block *block,
3485 				      struct tcf_proto *tp, u32 *cnt,
3486 				      u32 *flags, u32 diff, bool add)
3487 {
3488 	lockdep_assert_held(&block->cb_lock);
3489 
3490 	spin_lock(&tp->lock);
3491 	if (add) {
3492 		if (!*cnt)
3493 			tcf_block_offload_inc(block, flags);
3494 		*cnt += diff;
3495 	} else {
3496 		*cnt -= diff;
3497 		if (!*cnt)
3498 			tcf_block_offload_dec(block, flags);
3499 	}
3500 	spin_unlock(&tp->lock);
3501 }
3502 
3503 static void
tc_cls_offload_cnt_reset(struct tcf_block * block,struct tcf_proto * tp,u32 * cnt,u32 * flags)3504 tc_cls_offload_cnt_reset(struct tcf_block *block, struct tcf_proto *tp,
3505 			 u32 *cnt, u32 *flags)
3506 {
3507 	lockdep_assert_held(&block->cb_lock);
3508 
3509 	spin_lock(&tp->lock);
3510 	tcf_block_offload_dec(block, flags);
3511 	*cnt = 0;
3512 	spin_unlock(&tp->lock);
3513 }
3514 
3515 static int
__tc_setup_cb_call(struct tcf_block * block,enum tc_setup_type type,void * type_data,bool err_stop)3516 __tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3517 		   void *type_data, bool err_stop)
3518 {
3519 	struct flow_block_cb *block_cb;
3520 	int ok_count = 0;
3521 	int err;
3522 
3523 	list_for_each_entry(block_cb, &block->flow_block.cb_list, list) {
3524 		err = block_cb->cb(type, type_data, block_cb->cb_priv);
3525 		if (err) {
3526 			if (err_stop)
3527 				return err;
3528 		} else {
3529 			ok_count++;
3530 		}
3531 	}
3532 	return ok_count;
3533 }
3534 
tc_setup_cb_call(struct tcf_block * block,enum tc_setup_type type,void * type_data,bool err_stop,bool rtnl_held)3535 int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3536 		     void *type_data, bool err_stop, bool rtnl_held)
3537 {
3538 	bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3539 	int ok_count;
3540 
3541 retry:
3542 	if (take_rtnl)
3543 		rtnl_lock();
3544 	down_read(&block->cb_lock);
3545 	/* Need to obtain rtnl lock if block is bound to devs that require it.
3546 	 * In block bind code cb_lock is obtained while holding rtnl, so we must
3547 	 * obtain the locks in same order here.
3548 	 */
3549 	if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3550 		up_read(&block->cb_lock);
3551 		take_rtnl = true;
3552 		goto retry;
3553 	}
3554 
3555 	ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3556 
3557 	up_read(&block->cb_lock);
3558 	if (take_rtnl)
3559 		rtnl_unlock();
3560 	return ok_count;
3561 }
3562 EXPORT_SYMBOL(tc_setup_cb_call);
3563 
3564 /* Non-destructive filter add. If filter that wasn't already in hardware is
3565  * successfully offloaded, increment block offloads counter. On failure,
3566  * previously offloaded filter is considered to be intact and offloads counter
3567  * is not decremented.
3568  */
3569 
tc_setup_cb_add(struct tcf_block * block,struct tcf_proto * tp,enum tc_setup_type type,void * type_data,bool err_stop,u32 * flags,unsigned int * in_hw_count,bool rtnl_held)3570 int tc_setup_cb_add(struct tcf_block *block, struct tcf_proto *tp,
3571 		    enum tc_setup_type type, void *type_data, bool err_stop,
3572 		    u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
3573 {
3574 	bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3575 	int ok_count;
3576 
3577 retry:
3578 	if (take_rtnl)
3579 		rtnl_lock();
3580 	down_read(&block->cb_lock);
3581 	/* Need to obtain rtnl lock if block is bound to devs that require it.
3582 	 * In block bind code cb_lock is obtained while holding rtnl, so we must
3583 	 * obtain the locks in same order here.
3584 	 */
3585 	if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3586 		up_read(&block->cb_lock);
3587 		take_rtnl = true;
3588 		goto retry;
3589 	}
3590 
3591 	/* Make sure all netdevs sharing this block are offload-capable. */
3592 	if (block->nooffloaddevcnt && err_stop) {
3593 		ok_count = -EOPNOTSUPP;
3594 		goto err_unlock;
3595 	}
3596 
3597 	ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3598 	if (ok_count < 0)
3599 		goto err_unlock;
3600 
3601 	if (tp->ops->hw_add)
3602 		tp->ops->hw_add(tp, type_data);
3603 	if (ok_count > 0)
3604 		tc_cls_offload_cnt_update(block, tp, in_hw_count, flags,
3605 					  ok_count, true);
3606 err_unlock:
3607 	up_read(&block->cb_lock);
3608 	if (take_rtnl)
3609 		rtnl_unlock();
3610 	return min(ok_count, 0);
3611 }
3612 EXPORT_SYMBOL(tc_setup_cb_add);
3613 
3614 /* Destructive filter replace. If filter that wasn't already in hardware is
3615  * successfully offloaded, increment block offload counter. On failure,
3616  * previously offloaded filter is considered to be destroyed and offload counter
3617  * is decremented.
3618  */
3619 
tc_setup_cb_replace(struct tcf_block * block,struct tcf_proto * tp,enum tc_setup_type type,void * type_data,bool err_stop,u32 * old_flags,unsigned int * old_in_hw_count,u32 * new_flags,unsigned int * new_in_hw_count,bool rtnl_held)3620 int tc_setup_cb_replace(struct tcf_block *block, struct tcf_proto *tp,
3621 			enum tc_setup_type type, void *type_data, bool err_stop,
3622 			u32 *old_flags, unsigned int *old_in_hw_count,
3623 			u32 *new_flags, unsigned int *new_in_hw_count,
3624 			bool rtnl_held)
3625 {
3626 	bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3627 	int ok_count;
3628 
3629 retry:
3630 	if (take_rtnl)
3631 		rtnl_lock();
3632 	down_read(&block->cb_lock);
3633 	/* Need to obtain rtnl lock if block is bound to devs that require it.
3634 	 * In block bind code cb_lock is obtained while holding rtnl, so we must
3635 	 * obtain the locks in same order here.
3636 	 */
3637 	if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3638 		up_read(&block->cb_lock);
3639 		take_rtnl = true;
3640 		goto retry;
3641 	}
3642 
3643 	/* Make sure all netdevs sharing this block are offload-capable. */
3644 	if (block->nooffloaddevcnt && err_stop) {
3645 		ok_count = -EOPNOTSUPP;
3646 		goto err_unlock;
3647 	}
3648 
3649 	tc_cls_offload_cnt_reset(block, tp, old_in_hw_count, old_flags);
3650 	if (tp->ops->hw_del)
3651 		tp->ops->hw_del(tp, type_data);
3652 
3653 	ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3654 	if (ok_count < 0)
3655 		goto err_unlock;
3656 
3657 	if (tp->ops->hw_add)
3658 		tp->ops->hw_add(tp, type_data);
3659 	if (ok_count > 0)
3660 		tc_cls_offload_cnt_update(block, tp, new_in_hw_count,
3661 					  new_flags, ok_count, true);
3662 err_unlock:
3663 	up_read(&block->cb_lock);
3664 	if (take_rtnl)
3665 		rtnl_unlock();
3666 	return min(ok_count, 0);
3667 }
3668 EXPORT_SYMBOL(tc_setup_cb_replace);
3669 
3670 /* Destroy filter and decrement block offload counter, if filter was previously
3671  * offloaded.
3672  */
3673 
tc_setup_cb_destroy(struct tcf_block * block,struct tcf_proto * tp,enum tc_setup_type type,void * type_data,bool err_stop,u32 * flags,unsigned int * in_hw_count,bool rtnl_held)3674 int tc_setup_cb_destroy(struct tcf_block *block, struct tcf_proto *tp,
3675 			enum tc_setup_type type, void *type_data, bool err_stop,
3676 			u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
3677 {
3678 	bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3679 	int ok_count;
3680 
3681 retry:
3682 	if (take_rtnl)
3683 		rtnl_lock();
3684 	down_read(&block->cb_lock);
3685 	/* Need to obtain rtnl lock if block is bound to devs that require it.
3686 	 * In block bind code cb_lock is obtained while holding rtnl, so we must
3687 	 * obtain the locks in same order here.
3688 	 */
3689 	if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3690 		up_read(&block->cb_lock);
3691 		take_rtnl = true;
3692 		goto retry;
3693 	}
3694 
3695 	ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3696 
3697 	tc_cls_offload_cnt_reset(block, tp, in_hw_count, flags);
3698 	if (tp->ops->hw_del)
3699 		tp->ops->hw_del(tp, type_data);
3700 
3701 	up_read(&block->cb_lock);
3702 	if (take_rtnl)
3703 		rtnl_unlock();
3704 	return min(ok_count, 0);
3705 }
3706 EXPORT_SYMBOL(tc_setup_cb_destroy);
3707 
tc_setup_cb_reoffload(struct tcf_block * block,struct tcf_proto * tp,bool add,flow_setup_cb_t * cb,enum tc_setup_type type,void * type_data,void * cb_priv,u32 * flags,unsigned int * in_hw_count)3708 int tc_setup_cb_reoffload(struct tcf_block *block, struct tcf_proto *tp,
3709 			  bool add, flow_setup_cb_t *cb,
3710 			  enum tc_setup_type type, void *type_data,
3711 			  void *cb_priv, u32 *flags, unsigned int *in_hw_count)
3712 {
3713 	int err = cb(type, type_data, cb_priv);
3714 
3715 	if (err) {
3716 		if (add && tc_skip_sw(*flags))
3717 			return err;
3718 	} else {
3719 		tc_cls_offload_cnt_update(block, tp, in_hw_count, flags, 1,
3720 					  add);
3721 	}
3722 
3723 	return 0;
3724 }
3725 EXPORT_SYMBOL(tc_setup_cb_reoffload);
3726 
tcf_act_get_user_cookie(struct flow_action_entry * entry,const struct tc_action * act)3727 static int tcf_act_get_user_cookie(struct flow_action_entry *entry,
3728 				   const struct tc_action *act)
3729 {
3730 	struct tc_cookie *user_cookie;
3731 	int err = 0;
3732 
3733 	rcu_read_lock();
3734 	user_cookie = rcu_dereference(act->user_cookie);
3735 	if (user_cookie) {
3736 		entry->user_cookie = flow_action_cookie_create(user_cookie->data,
3737 							       user_cookie->len,
3738 							       GFP_ATOMIC);
3739 		if (!entry->user_cookie)
3740 			err = -ENOMEM;
3741 	}
3742 	rcu_read_unlock();
3743 	return err;
3744 }
3745 
tcf_act_put_user_cookie(struct flow_action_entry * entry)3746 static void tcf_act_put_user_cookie(struct flow_action_entry *entry)
3747 {
3748 	flow_action_cookie_destroy(entry->user_cookie);
3749 }
3750 
tc_cleanup_offload_action(struct flow_action * flow_action)3751 void tc_cleanup_offload_action(struct flow_action *flow_action)
3752 {
3753 	struct flow_action_entry *entry;
3754 	int i;
3755 
3756 	flow_action_for_each(i, entry, flow_action) {
3757 		tcf_act_put_user_cookie(entry);
3758 		if (entry->destructor)
3759 			entry->destructor(entry->destructor_priv);
3760 	}
3761 }
3762 EXPORT_SYMBOL(tc_cleanup_offload_action);
3763 
tc_setup_offload_act(struct tc_action * act,struct flow_action_entry * entry,u32 * index_inc,struct netlink_ext_ack * extack)3764 static int tc_setup_offload_act(struct tc_action *act,
3765 				struct flow_action_entry *entry,
3766 				u32 *index_inc,
3767 				struct netlink_ext_ack *extack)
3768 {
3769 #ifdef CONFIG_NET_CLS_ACT
3770 	if (act->ops->offload_act_setup) {
3771 		return act->ops->offload_act_setup(act, entry, index_inc, true,
3772 						   extack);
3773 	} else {
3774 		NL_SET_ERR_MSG(extack, "Action does not support offload");
3775 		return -EOPNOTSUPP;
3776 	}
3777 #else
3778 	return 0;
3779 #endif
3780 }
3781 
tc_setup_action(struct flow_action * flow_action,struct tc_action * actions[],u32 miss_cookie_base,struct netlink_ext_ack * extack)3782 int tc_setup_action(struct flow_action *flow_action,
3783 		    struct tc_action *actions[],
3784 		    u32 miss_cookie_base,
3785 		    struct netlink_ext_ack *extack)
3786 {
3787 	int i, j, k, index, err = 0;
3788 	struct tc_action *act;
3789 
3790 	BUILD_BUG_ON(TCA_ACT_HW_STATS_ANY != FLOW_ACTION_HW_STATS_ANY);
3791 	BUILD_BUG_ON(TCA_ACT_HW_STATS_IMMEDIATE != FLOW_ACTION_HW_STATS_IMMEDIATE);
3792 	BUILD_BUG_ON(TCA_ACT_HW_STATS_DELAYED != FLOW_ACTION_HW_STATS_DELAYED);
3793 
3794 	if (!actions)
3795 		return 0;
3796 
3797 	j = 0;
3798 	tcf_act_for_each_action(i, act, actions) {
3799 		struct flow_action_entry *entry;
3800 
3801 		entry = &flow_action->entries[j];
3802 		spin_lock_bh(&act->tcfa_lock);
3803 		err = tcf_act_get_user_cookie(entry, act);
3804 		if (err)
3805 			goto err_out_locked;
3806 
3807 		index = 0;
3808 		err = tc_setup_offload_act(act, entry, &index, extack);
3809 		if (err)
3810 			goto err_out_locked;
3811 
3812 		for (k = 0; k < index ; k++) {
3813 			entry[k].hw_stats = tc_act_hw_stats(act->hw_stats);
3814 			entry[k].hw_index = act->tcfa_index;
3815 			entry[k].cookie = (unsigned long)act;
3816 			entry[k].miss_cookie =
3817 				tcf_exts_miss_cookie_get(miss_cookie_base, i);
3818 		}
3819 
3820 		j += index;
3821 
3822 		spin_unlock_bh(&act->tcfa_lock);
3823 	}
3824 
3825 err_out:
3826 	if (err)
3827 		tc_cleanup_offload_action(flow_action);
3828 
3829 	return err;
3830 err_out_locked:
3831 	spin_unlock_bh(&act->tcfa_lock);
3832 	goto err_out;
3833 }
3834 
tc_setup_offload_action(struct flow_action * flow_action,const struct tcf_exts * exts,struct netlink_ext_ack * extack)3835 int tc_setup_offload_action(struct flow_action *flow_action,
3836 			    const struct tcf_exts *exts,
3837 			    struct netlink_ext_ack *extack)
3838 {
3839 #ifdef CONFIG_NET_CLS_ACT
3840 	u32 miss_cookie_base;
3841 
3842 	if (!exts)
3843 		return 0;
3844 
3845 	miss_cookie_base = exts->miss_cookie_node ?
3846 			   exts->miss_cookie_node->miss_cookie_base : 0;
3847 	return tc_setup_action(flow_action, exts->actions, miss_cookie_base,
3848 			       extack);
3849 #else
3850 	return 0;
3851 #endif
3852 }
3853 EXPORT_SYMBOL(tc_setup_offload_action);
3854 
tcf_exts_num_actions(struct tcf_exts * exts)3855 unsigned int tcf_exts_num_actions(struct tcf_exts *exts)
3856 {
3857 	unsigned int num_acts = 0;
3858 	struct tc_action *act;
3859 	int i;
3860 
3861 	tcf_exts_for_each_action(i, act, exts) {
3862 		if (is_tcf_pedit(act))
3863 			num_acts += tcf_pedit_nkeys(act);
3864 		else
3865 			num_acts++;
3866 	}
3867 	return num_acts;
3868 }
3869 EXPORT_SYMBOL(tcf_exts_num_actions);
3870 
3871 #ifdef CONFIG_NET_CLS_ACT
tcf_qevent_parse_block_index(struct nlattr * block_index_attr,u32 * p_block_index,struct netlink_ext_ack * extack)3872 static int tcf_qevent_parse_block_index(struct nlattr *block_index_attr,
3873 					u32 *p_block_index,
3874 					struct netlink_ext_ack *extack)
3875 {
3876 	*p_block_index = nla_get_u32(block_index_attr);
3877 	if (!*p_block_index) {
3878 		NL_SET_ERR_MSG(extack, "Block number may not be zero");
3879 		return -EINVAL;
3880 	}
3881 
3882 	return 0;
3883 }
3884 
tcf_qevent_init(struct tcf_qevent * qe,struct Qdisc * sch,enum flow_block_binder_type binder_type,struct nlattr * block_index_attr,struct netlink_ext_ack * extack)3885 int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
3886 		    enum flow_block_binder_type binder_type,
3887 		    struct nlattr *block_index_attr,
3888 		    struct netlink_ext_ack *extack)
3889 {
3890 	u32 block_index;
3891 	int err;
3892 
3893 	if (!block_index_attr)
3894 		return 0;
3895 
3896 	err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
3897 	if (err)
3898 		return err;
3899 
3900 	qe->info.binder_type = binder_type;
3901 	qe->info.chain_head_change = tcf_chain_head_change_dflt;
3902 	qe->info.chain_head_change_priv = &qe->filter_chain;
3903 	qe->info.block_index = block_index;
3904 
3905 	return tcf_block_get_ext(&qe->block, sch, &qe->info, extack);
3906 }
3907 EXPORT_SYMBOL(tcf_qevent_init);
3908 
tcf_qevent_destroy(struct tcf_qevent * qe,struct Qdisc * sch)3909 void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch)
3910 {
3911 	if (qe->info.block_index)
3912 		tcf_block_put_ext(qe->block, sch, &qe->info);
3913 }
3914 EXPORT_SYMBOL(tcf_qevent_destroy);
3915 
tcf_qevent_validate_change(struct tcf_qevent * qe,struct nlattr * block_index_attr,struct netlink_ext_ack * extack)3916 int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
3917 			       struct netlink_ext_ack *extack)
3918 {
3919 	u32 block_index;
3920 	int err;
3921 
3922 	if (!block_index_attr)
3923 		return 0;
3924 
3925 	err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
3926 	if (err)
3927 		return err;
3928 
3929 	/* Bounce newly-configured block or change in block. */
3930 	if (block_index != qe->info.block_index) {
3931 		NL_SET_ERR_MSG(extack, "Change of blocks is not supported");
3932 		return -EINVAL;
3933 	}
3934 
3935 	return 0;
3936 }
3937 EXPORT_SYMBOL(tcf_qevent_validate_change);
3938 
tcf_qevent_handle(struct tcf_qevent * qe,struct Qdisc * sch,struct sk_buff * skb,struct sk_buff ** to_free,int * ret)3939 struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
3940 				  struct sk_buff **to_free, int *ret)
3941 {
3942 	struct tcf_result cl_res;
3943 	struct tcf_proto *fl;
3944 
3945 	if (!qe->info.block_index)
3946 		return skb;
3947 
3948 	fl = rcu_dereference_bh(qe->filter_chain);
3949 
3950 	switch (tcf_classify(skb, NULL, fl, &cl_res, false)) {
3951 	case TC_ACT_SHOT:
3952 		qdisc_qstats_drop(sch);
3953 		__qdisc_drop(skb, to_free);
3954 		*ret = __NET_XMIT_BYPASS;
3955 		return NULL;
3956 	case TC_ACT_STOLEN:
3957 	case TC_ACT_QUEUED:
3958 	case TC_ACT_TRAP:
3959 		__qdisc_drop(skb, to_free);
3960 		*ret = __NET_XMIT_STOLEN;
3961 		return NULL;
3962 	case TC_ACT_REDIRECT:
3963 		skb_do_redirect(skb);
3964 		*ret = __NET_XMIT_STOLEN;
3965 		return NULL;
3966 	}
3967 
3968 	return skb;
3969 }
3970 EXPORT_SYMBOL(tcf_qevent_handle);
3971 
tcf_qevent_dump(struct sk_buff * skb,int attr_name,struct tcf_qevent * qe)3972 int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe)
3973 {
3974 	if (!qe->info.block_index)
3975 		return 0;
3976 	return nla_put_u32(skb, attr_name, qe->info.block_index);
3977 }
3978 EXPORT_SYMBOL(tcf_qevent_dump);
3979 #endif
3980 
tcf_net_init(struct net * net)3981 static __net_init int tcf_net_init(struct net *net)
3982 {
3983 	struct tcf_net *tn = net_generic(net, tcf_net_id);
3984 
3985 	spin_lock_init(&tn->idr_lock);
3986 	idr_init(&tn->idr);
3987 	return 0;
3988 }
3989 
tcf_net_exit(struct net * net)3990 static void __net_exit tcf_net_exit(struct net *net)
3991 {
3992 	struct tcf_net *tn = net_generic(net, tcf_net_id);
3993 
3994 	idr_destroy(&tn->idr);
3995 }
3996 
3997 static struct pernet_operations tcf_net_ops = {
3998 	.init = tcf_net_init,
3999 	.exit = tcf_net_exit,
4000 	.id   = &tcf_net_id,
4001 	.size = sizeof(struct tcf_net),
4002 };
4003 
tc_filter_init(void)4004 static int __init tc_filter_init(void)
4005 {
4006 	int err;
4007 
4008 	tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
4009 	if (!tc_filter_wq)
4010 		return -ENOMEM;
4011 
4012 	err = register_pernet_subsys(&tcf_net_ops);
4013 	if (err)
4014 		goto err_register_pernet_subsys;
4015 
4016 	xa_init_flags(&tcf_exts_miss_cookies_xa, XA_FLAGS_ALLOC1);
4017 
4018 	rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL,
4019 		      RTNL_FLAG_DOIT_UNLOCKED);
4020 	rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL,
4021 		      RTNL_FLAG_DOIT_UNLOCKED);
4022 	rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
4023 		      tc_dump_tfilter, RTNL_FLAG_DOIT_UNLOCKED);
4024 	rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
4025 	rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
4026 	rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
4027 		      tc_dump_chain, 0);
4028 
4029 	return 0;
4030 
4031 err_register_pernet_subsys:
4032 	destroy_workqueue(tc_filter_wq);
4033 	return err;
4034 }
4035 
4036 subsys_initcall(tc_filter_init);
4037