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