xref: /linux/net/sched/cls_api.c (revision 22ac5ad4a7d4e201d19b7f04ce8d79346c80a34b)
1 /*
2  * net/sched/cls_api.c	Packet classifier API.
3  *
4  *		This program is free software; you can redistribute it and/or
5  *		modify it under the terms of the GNU General Public License
6  *		as published by the Free Software Foundation; either version
7  *		2 of the License, or (at your option) any later version.
8  *
9  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  *
11  * Changes:
12  *
13  * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
14  *
15  */
16 
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/err.h>
23 #include <linux/skbuff.h>
24 #include <linux/init.h>
25 #include <linux/kmod.h>
26 #include <linux/err.h>
27 #include <linux/slab.h>
28 #include <net/net_namespace.h>
29 #include <net/sock.h>
30 #include <net/netlink.h>
31 #include <net/pkt_sched.h>
32 #include <net/pkt_cls.h>
33 
34 /* The list of all installed classifier types */
35 static LIST_HEAD(tcf_proto_base);
36 
37 /* Protects list of registered TC modules. It is pure SMP lock. */
38 static DEFINE_RWLOCK(cls_mod_lock);
39 
40 /* Find classifier type by string name */
41 
42 static const struct tcf_proto_ops *tcf_proto_lookup_ops(const char *kind)
43 {
44 	const struct tcf_proto_ops *t, *res = NULL;
45 
46 	if (kind) {
47 		read_lock(&cls_mod_lock);
48 		list_for_each_entry(t, &tcf_proto_base, head) {
49 			if (strcmp(kind, t->kind) == 0) {
50 				if (try_module_get(t->owner))
51 					res = t;
52 				break;
53 			}
54 		}
55 		read_unlock(&cls_mod_lock);
56 	}
57 	return res;
58 }
59 
60 /* Register(unregister) new classifier type */
61 
62 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
63 {
64 	struct tcf_proto_ops *t;
65 	int rc = -EEXIST;
66 
67 	write_lock(&cls_mod_lock);
68 	list_for_each_entry(t, &tcf_proto_base, head)
69 		if (!strcmp(ops->kind, t->kind))
70 			goto out;
71 
72 	list_add_tail(&ops->head, &tcf_proto_base);
73 	rc = 0;
74 out:
75 	write_unlock(&cls_mod_lock);
76 	return rc;
77 }
78 EXPORT_SYMBOL(register_tcf_proto_ops);
79 
80 static struct workqueue_struct *tc_filter_wq;
81 
82 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
83 {
84 	struct tcf_proto_ops *t;
85 	int rc = -ENOENT;
86 
87 	/* Wait for outstanding call_rcu()s, if any, from a
88 	 * tcf_proto_ops's destroy() handler.
89 	 */
90 	rcu_barrier();
91 	flush_workqueue(tc_filter_wq);
92 
93 	write_lock(&cls_mod_lock);
94 	list_for_each_entry(t, &tcf_proto_base, head) {
95 		if (t == ops) {
96 			list_del(&t->head);
97 			rc = 0;
98 			break;
99 		}
100 	}
101 	write_unlock(&cls_mod_lock);
102 	return rc;
103 }
104 EXPORT_SYMBOL(unregister_tcf_proto_ops);
105 
106 bool tcf_queue_work(struct work_struct *work)
107 {
108 	return queue_work(tc_filter_wq, work);
109 }
110 EXPORT_SYMBOL(tcf_queue_work);
111 
112 /* Select new prio value from the range, managed by kernel. */
113 
114 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
115 {
116 	u32 first = TC_H_MAKE(0xC0000000U, 0U);
117 
118 	if (tp)
119 		first = tp->prio - 1;
120 
121 	return TC_H_MAJ(first);
122 }
123 
124 static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
125 					  u32 prio, u32 parent, struct Qdisc *q,
126 					  struct tcf_chain *chain)
127 {
128 	struct tcf_proto *tp;
129 	int err;
130 
131 	tp = kzalloc(sizeof(*tp), GFP_KERNEL);
132 	if (!tp)
133 		return ERR_PTR(-ENOBUFS);
134 
135 	err = -ENOENT;
136 	tp->ops = tcf_proto_lookup_ops(kind);
137 	if (!tp->ops) {
138 #ifdef CONFIG_MODULES
139 		rtnl_unlock();
140 		request_module("cls_%s", kind);
141 		rtnl_lock();
142 		tp->ops = tcf_proto_lookup_ops(kind);
143 		/* We dropped the RTNL semaphore in order to perform
144 		 * the module load. So, even if we succeeded in loading
145 		 * the module we have to replay the request. We indicate
146 		 * this using -EAGAIN.
147 		 */
148 		if (tp->ops) {
149 			module_put(tp->ops->owner);
150 			err = -EAGAIN;
151 		} else {
152 			err = -ENOENT;
153 		}
154 		goto errout;
155 #endif
156 	}
157 	tp->classify = tp->ops->classify;
158 	tp->protocol = protocol;
159 	tp->prio = prio;
160 	tp->classid = parent;
161 	tp->q = q;
162 	tp->chain = chain;
163 
164 	err = tp->ops->init(tp);
165 	if (err) {
166 		module_put(tp->ops->owner);
167 		goto errout;
168 	}
169 	return tp;
170 
171 errout:
172 	kfree(tp);
173 	return ERR_PTR(err);
174 }
175 
176 static void tcf_proto_destroy(struct tcf_proto *tp)
177 {
178 	tp->ops->destroy(tp);
179 	module_put(tp->ops->owner);
180 	kfree_rcu(tp, rcu);
181 }
182 
183 static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
184 					  u32 chain_index)
185 {
186 	struct tcf_chain *chain;
187 
188 	chain = kzalloc(sizeof(*chain), GFP_KERNEL);
189 	if (!chain)
190 		return NULL;
191 	list_add_tail(&chain->list, &block->chain_list);
192 	chain->block = block;
193 	chain->index = chain_index;
194 	chain->refcnt = 1;
195 	return chain;
196 }
197 
198 static void tcf_chain_flush(struct tcf_chain *chain)
199 {
200 	struct tcf_proto *tp;
201 
202 	if (chain->p_filter_chain)
203 		RCU_INIT_POINTER(*chain->p_filter_chain, NULL);
204 	while ((tp = rtnl_dereference(chain->filter_chain)) != NULL) {
205 		RCU_INIT_POINTER(chain->filter_chain, tp->next);
206 		tcf_chain_put(chain);
207 		tcf_proto_destroy(tp);
208 	}
209 }
210 
211 static void tcf_chain_destroy(struct tcf_chain *chain)
212 {
213 	list_del(&chain->list);
214 	kfree(chain);
215 }
216 
217 static void tcf_chain_hold(struct tcf_chain *chain)
218 {
219 	++chain->refcnt;
220 }
221 
222 struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
223 				bool create)
224 {
225 	struct tcf_chain *chain;
226 
227 	list_for_each_entry(chain, &block->chain_list, list) {
228 		if (chain->index == chain_index) {
229 			tcf_chain_hold(chain);
230 			return chain;
231 		}
232 	}
233 
234 	return create ? tcf_chain_create(block, chain_index) : NULL;
235 }
236 EXPORT_SYMBOL(tcf_chain_get);
237 
238 void tcf_chain_put(struct tcf_chain *chain)
239 {
240 	if (--chain->refcnt == 0)
241 		tcf_chain_destroy(chain);
242 }
243 EXPORT_SYMBOL(tcf_chain_put);
244 
245 static void
246 tcf_chain_filter_chain_ptr_set(struct tcf_chain *chain,
247 			       struct tcf_proto __rcu **p_filter_chain)
248 {
249 	chain->p_filter_chain = p_filter_chain;
250 }
251 
252 static void tcf_block_offload_cmd(struct tcf_block *block, struct Qdisc *q,
253 				  struct tcf_block_ext_info *ei,
254 				  enum tc_block_command command)
255 {
256 	struct net_device *dev = q->dev_queue->dev;
257 	struct tc_block_offload bo = {};
258 
259 	if (!tc_can_offload(dev))
260 		return;
261 	bo.command = command;
262 	bo.binder_type = ei->binder_type;
263 	bo.block = block;
264 	dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
265 }
266 
267 static void tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
268 				   struct tcf_block_ext_info *ei)
269 {
270 	tcf_block_offload_cmd(block, q, ei, TC_BLOCK_BIND);
271 }
272 
273 static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
274 				     struct tcf_block_ext_info *ei)
275 {
276 	tcf_block_offload_cmd(block, q, ei, TC_BLOCK_UNBIND);
277 }
278 
279 int tcf_block_get_ext(struct tcf_block **p_block,
280 		      struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
281 		      struct tcf_block_ext_info *ei)
282 {
283 	struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
284 	struct tcf_chain *chain;
285 	int err;
286 
287 	if (!block)
288 		return -ENOMEM;
289 	INIT_LIST_HEAD(&block->chain_list);
290 	INIT_LIST_HEAD(&block->cb_list);
291 
292 	/* Create chain 0 by default, it has to be always present. */
293 	chain = tcf_chain_create(block, 0);
294 	if (!chain) {
295 		err = -ENOMEM;
296 		goto err_chain_create;
297 	}
298 	tcf_chain_filter_chain_ptr_set(chain, p_filter_chain);
299 	block->net = qdisc_net(q);
300 	block->q = q;
301 	tcf_block_offload_bind(block, q, ei);
302 	*p_block = block;
303 	return 0;
304 
305 err_chain_create:
306 	kfree(block);
307 	return err;
308 }
309 EXPORT_SYMBOL(tcf_block_get_ext);
310 
311 int tcf_block_get(struct tcf_block **p_block,
312 		  struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q)
313 {
314 	struct tcf_block_ext_info ei = {0, };
315 
316 	return tcf_block_get_ext(p_block, p_filter_chain, q, &ei);
317 }
318 EXPORT_SYMBOL(tcf_block_get);
319 
320 static void tcf_block_put_final(struct work_struct *work)
321 {
322 	struct tcf_block *block = container_of(work, struct tcf_block, work);
323 	struct tcf_chain *chain, *tmp;
324 
325 	/* At this point, all the chains should have refcnt == 1. */
326 	rtnl_lock();
327 	list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
328 		tcf_chain_put(chain);
329 	rtnl_unlock();
330 	kfree(block);
331 }
332 
333 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
334  * actions should be all removed after flushing. However, filters are destroyed
335  * in RCU callbacks, we have to hold the chains first, otherwise we would
336  * always race with RCU callbacks on this list without proper locking.
337  */
338 static void tcf_block_put_deferred(struct work_struct *work)
339 {
340 	struct tcf_block *block = container_of(work, struct tcf_block, work);
341 	struct tcf_chain *chain;
342 
343 	rtnl_lock();
344 	/* Hold a refcnt for all chains, except 0, in case they are gone. */
345 	list_for_each_entry(chain, &block->chain_list, list)
346 		if (chain->index)
347 			tcf_chain_hold(chain);
348 
349 	/* No race on the list, because no chain could be destroyed. */
350 	list_for_each_entry(chain, &block->chain_list, list)
351 		tcf_chain_flush(chain);
352 
353 	INIT_WORK(&block->work, tcf_block_put_final);
354 	/* Wait for RCU callbacks to release the reference count and make
355 	 * sure their works have been queued before this.
356 	 */
357 	rcu_barrier();
358 	tcf_queue_work(&block->work);
359 	rtnl_unlock();
360 }
361 
362 void tcf_block_put_ext(struct tcf_block *block,
363 		       struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
364 		       struct tcf_block_ext_info *ei)
365 {
366 	if (!block)
367 		return;
368 
369 	tcf_block_offload_unbind(block, q, ei);
370 
371 	INIT_WORK(&block->work, tcf_block_put_deferred);
372 	/* Wait for existing RCU callbacks to cool down, make sure their works
373 	 * have been queued before this. We can not flush pending works here
374 	 * because we are holding the RTNL lock.
375 	 */
376 	rcu_barrier();
377 	tcf_queue_work(&block->work);
378 }
379 EXPORT_SYMBOL(tcf_block_put_ext);
380 
381 void tcf_block_put(struct tcf_block *block)
382 {
383 	struct tcf_block_ext_info ei = {0, };
384 
385 	tcf_block_put_ext(block, NULL, block->q, &ei);
386 }
387 
388 EXPORT_SYMBOL(tcf_block_put);
389 
390 struct tcf_block_cb {
391 	struct list_head list;
392 	tc_setup_cb_t *cb;
393 	void *cb_ident;
394 	void *cb_priv;
395 	unsigned int refcnt;
396 };
397 
398 void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
399 {
400 	return block_cb->cb_priv;
401 }
402 EXPORT_SYMBOL(tcf_block_cb_priv);
403 
404 struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
405 					 tc_setup_cb_t *cb, void *cb_ident)
406 {	struct tcf_block_cb *block_cb;
407 
408 	list_for_each_entry(block_cb, &block->cb_list, list)
409 		if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
410 			return block_cb;
411 	return NULL;
412 }
413 EXPORT_SYMBOL(tcf_block_cb_lookup);
414 
415 void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
416 {
417 	block_cb->refcnt++;
418 }
419 EXPORT_SYMBOL(tcf_block_cb_incref);
420 
421 unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
422 {
423 	return --block_cb->refcnt;
424 }
425 EXPORT_SYMBOL(tcf_block_cb_decref);
426 
427 struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
428 					     tc_setup_cb_t *cb, void *cb_ident,
429 					     void *cb_priv)
430 {
431 	struct tcf_block_cb *block_cb;
432 
433 	block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
434 	if (!block_cb)
435 		return NULL;
436 	block_cb->cb = cb;
437 	block_cb->cb_ident = cb_ident;
438 	block_cb->cb_priv = cb_priv;
439 	list_add(&block_cb->list, &block->cb_list);
440 	return block_cb;
441 }
442 EXPORT_SYMBOL(__tcf_block_cb_register);
443 
444 int tcf_block_cb_register(struct tcf_block *block,
445 			  tc_setup_cb_t *cb, void *cb_ident,
446 			  void *cb_priv)
447 {
448 	struct tcf_block_cb *block_cb;
449 
450 	block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv);
451 	return block_cb ? 0 : -ENOMEM;
452 }
453 EXPORT_SYMBOL(tcf_block_cb_register);
454 
455 void __tcf_block_cb_unregister(struct tcf_block_cb *block_cb)
456 {
457 	list_del(&block_cb->list);
458 	kfree(block_cb);
459 }
460 EXPORT_SYMBOL(__tcf_block_cb_unregister);
461 
462 void tcf_block_cb_unregister(struct tcf_block *block,
463 			     tc_setup_cb_t *cb, void *cb_ident)
464 {
465 	struct tcf_block_cb *block_cb;
466 
467 	block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
468 	if (!block_cb)
469 		return;
470 	__tcf_block_cb_unregister(block_cb);
471 }
472 EXPORT_SYMBOL(tcf_block_cb_unregister);
473 
474 static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
475 			     void *type_data, bool err_stop)
476 {
477 	struct tcf_block_cb *block_cb;
478 	int ok_count = 0;
479 	int err;
480 
481 	list_for_each_entry(block_cb, &block->cb_list, list) {
482 		err = block_cb->cb(type, type_data, block_cb->cb_priv);
483 		if (err) {
484 			if (err_stop)
485 				return err;
486 		} else {
487 			ok_count++;
488 		}
489 	}
490 	return ok_count;
491 }
492 
493 /* Main classifier routine: scans classifier chain attached
494  * to this qdisc, (optionally) tests for protocol and asks
495  * specific classifiers.
496  */
497 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
498 		 struct tcf_result *res, bool compat_mode)
499 {
500 	__be16 protocol = tc_skb_protocol(skb);
501 #ifdef CONFIG_NET_CLS_ACT
502 	const int max_reclassify_loop = 4;
503 	const struct tcf_proto *orig_tp = tp;
504 	const struct tcf_proto *first_tp;
505 	int limit = 0;
506 
507 reclassify:
508 #endif
509 	for (; tp; tp = rcu_dereference_bh(tp->next)) {
510 		int err;
511 
512 		if (tp->protocol != protocol &&
513 		    tp->protocol != htons(ETH_P_ALL))
514 			continue;
515 
516 		err = tp->classify(skb, tp, res);
517 #ifdef CONFIG_NET_CLS_ACT
518 		if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
519 			first_tp = orig_tp;
520 			goto reset;
521 		} else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
522 			first_tp = res->goto_tp;
523 			goto reset;
524 		}
525 #endif
526 		if (err >= 0)
527 			return err;
528 	}
529 
530 	return TC_ACT_UNSPEC; /* signal: continue lookup */
531 #ifdef CONFIG_NET_CLS_ACT
532 reset:
533 	if (unlikely(limit++ >= max_reclassify_loop)) {
534 		net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
535 				       tp->q->ops->id, tp->prio & 0xffff,
536 				       ntohs(tp->protocol));
537 		return TC_ACT_SHOT;
538 	}
539 
540 	tp = first_tp;
541 	protocol = tc_skb_protocol(skb);
542 	goto reclassify;
543 #endif
544 }
545 EXPORT_SYMBOL(tcf_classify);
546 
547 struct tcf_chain_info {
548 	struct tcf_proto __rcu **pprev;
549 	struct tcf_proto __rcu *next;
550 };
551 
552 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
553 {
554 	return rtnl_dereference(*chain_info->pprev);
555 }
556 
557 static void tcf_chain_tp_insert(struct tcf_chain *chain,
558 				struct tcf_chain_info *chain_info,
559 				struct tcf_proto *tp)
560 {
561 	if (chain->p_filter_chain &&
562 	    *chain_info->pprev == chain->filter_chain)
563 		rcu_assign_pointer(*chain->p_filter_chain, tp);
564 	RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
565 	rcu_assign_pointer(*chain_info->pprev, tp);
566 	tcf_chain_hold(chain);
567 }
568 
569 static void tcf_chain_tp_remove(struct tcf_chain *chain,
570 				struct tcf_chain_info *chain_info,
571 				struct tcf_proto *tp)
572 {
573 	struct tcf_proto *next = rtnl_dereference(chain_info->next);
574 
575 	if (chain->p_filter_chain && tp == chain->filter_chain)
576 		RCU_INIT_POINTER(*chain->p_filter_chain, next);
577 	RCU_INIT_POINTER(*chain_info->pprev, next);
578 	tcf_chain_put(chain);
579 }
580 
581 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
582 					   struct tcf_chain_info *chain_info,
583 					   u32 protocol, u32 prio,
584 					   bool prio_allocate)
585 {
586 	struct tcf_proto **pprev;
587 	struct tcf_proto *tp;
588 
589 	/* Check the chain for existence of proto-tcf with this priority */
590 	for (pprev = &chain->filter_chain;
591 	     (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
592 		if (tp->prio >= prio) {
593 			if (tp->prio == prio) {
594 				if (prio_allocate ||
595 				    (tp->protocol != protocol && protocol))
596 					return ERR_PTR(-EINVAL);
597 			} else {
598 				tp = NULL;
599 			}
600 			break;
601 		}
602 	}
603 	chain_info->pprev = pprev;
604 	chain_info->next = tp ? tp->next : NULL;
605 	return tp;
606 }
607 
608 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
609 			 struct tcf_proto *tp, struct Qdisc *q, u32 parent,
610 			 void *fh, u32 portid, u32 seq, u16 flags, int event)
611 {
612 	struct tcmsg *tcm;
613 	struct nlmsghdr  *nlh;
614 	unsigned char *b = skb_tail_pointer(skb);
615 
616 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
617 	if (!nlh)
618 		goto out_nlmsg_trim;
619 	tcm = nlmsg_data(nlh);
620 	tcm->tcm_family = AF_UNSPEC;
621 	tcm->tcm__pad1 = 0;
622 	tcm->tcm__pad2 = 0;
623 	tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
624 	tcm->tcm_parent = parent;
625 	tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
626 	if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
627 		goto nla_put_failure;
628 	if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
629 		goto nla_put_failure;
630 	if (!fh) {
631 		tcm->tcm_handle = 0;
632 	} else {
633 		if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
634 			goto nla_put_failure;
635 	}
636 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
637 	return skb->len;
638 
639 out_nlmsg_trim:
640 nla_put_failure:
641 	nlmsg_trim(skb, b);
642 	return -1;
643 }
644 
645 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
646 			  struct nlmsghdr *n, struct tcf_proto *tp,
647 			  struct Qdisc *q, u32 parent,
648 			  void *fh, int event, bool unicast)
649 {
650 	struct sk_buff *skb;
651 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
652 
653 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
654 	if (!skb)
655 		return -ENOBUFS;
656 
657 	if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq,
658 			  n->nlmsg_flags, event) <= 0) {
659 		kfree_skb(skb);
660 		return -EINVAL;
661 	}
662 
663 	if (unicast)
664 		return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
665 
666 	return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
667 			      n->nlmsg_flags & NLM_F_ECHO);
668 }
669 
670 static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
671 			      struct nlmsghdr *n, struct tcf_proto *tp,
672 			      struct Qdisc *q, u32 parent,
673 			      void *fh, bool unicast, bool *last)
674 {
675 	struct sk_buff *skb;
676 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
677 	int err;
678 
679 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
680 	if (!skb)
681 		return -ENOBUFS;
682 
683 	if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq,
684 			  n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
685 		kfree_skb(skb);
686 		return -EINVAL;
687 	}
688 
689 	err = tp->ops->delete(tp, fh, last);
690 	if (err) {
691 		kfree_skb(skb);
692 		return err;
693 	}
694 
695 	if (unicast)
696 		return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
697 
698 	return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
699 			      n->nlmsg_flags & NLM_F_ECHO);
700 }
701 
702 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
703 				 struct Qdisc *q, u32 parent,
704 				 struct nlmsghdr *n,
705 				 struct tcf_chain *chain, int event)
706 {
707 	struct tcf_proto *tp;
708 
709 	for (tp = rtnl_dereference(chain->filter_chain);
710 	     tp; tp = rtnl_dereference(tp->next))
711 		tfilter_notify(net, oskb, n, tp, q, parent, 0, event, false);
712 }
713 
714 /* Add/change/delete/get a filter node */
715 
716 static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
717 			  struct netlink_ext_ack *extack)
718 {
719 	struct net *net = sock_net(skb->sk);
720 	struct nlattr *tca[TCA_MAX + 1];
721 	struct tcmsg *t;
722 	u32 protocol;
723 	u32 prio;
724 	bool prio_allocate;
725 	u32 parent;
726 	u32 chain_index;
727 	struct net_device *dev;
728 	struct Qdisc  *q;
729 	struct tcf_chain_info chain_info;
730 	struct tcf_chain *chain = NULL;
731 	struct tcf_block *block;
732 	struct tcf_proto *tp;
733 	const struct Qdisc_class_ops *cops;
734 	unsigned long cl;
735 	void *fh;
736 	int err;
737 	int tp_created;
738 
739 	if ((n->nlmsg_type != RTM_GETTFILTER) &&
740 	    !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
741 		return -EPERM;
742 
743 replay:
744 	tp_created = 0;
745 
746 	err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
747 	if (err < 0)
748 		return err;
749 
750 	t = nlmsg_data(n);
751 	protocol = TC_H_MIN(t->tcm_info);
752 	prio = TC_H_MAJ(t->tcm_info);
753 	prio_allocate = false;
754 	parent = t->tcm_parent;
755 	cl = 0;
756 
757 	if (prio == 0) {
758 		switch (n->nlmsg_type) {
759 		case RTM_DELTFILTER:
760 			if (protocol || t->tcm_handle || tca[TCA_KIND])
761 				return -ENOENT;
762 			break;
763 		case RTM_NEWTFILTER:
764 			/* If no priority is provided by the user,
765 			 * we allocate one.
766 			 */
767 			if (n->nlmsg_flags & NLM_F_CREATE) {
768 				prio = TC_H_MAKE(0x80000000U, 0U);
769 				prio_allocate = true;
770 				break;
771 			}
772 			/* fall-through */
773 		default:
774 			return -ENOENT;
775 		}
776 	}
777 
778 	/* Find head of filter chain. */
779 
780 	/* Find link */
781 	dev = __dev_get_by_index(net, t->tcm_ifindex);
782 	if (dev == NULL)
783 		return -ENODEV;
784 
785 	/* Find qdisc */
786 	if (!parent) {
787 		q = dev->qdisc;
788 		parent = q->handle;
789 	} else {
790 		q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
791 		if (q == NULL)
792 			return -EINVAL;
793 	}
794 
795 	/* Is it classful? */
796 	cops = q->ops->cl_ops;
797 	if (!cops)
798 		return -EINVAL;
799 
800 	if (!cops->tcf_block)
801 		return -EOPNOTSUPP;
802 
803 	/* Do we search for filter, attached to class? */
804 	if (TC_H_MIN(parent)) {
805 		cl = cops->find(q, parent);
806 		if (cl == 0)
807 			return -ENOENT;
808 	}
809 
810 	/* And the last stroke */
811 	block = cops->tcf_block(q, cl);
812 	if (!block) {
813 		err = -EINVAL;
814 		goto errout;
815 	}
816 
817 	chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
818 	if (chain_index > TC_ACT_EXT_VAL_MASK) {
819 		err = -EINVAL;
820 		goto errout;
821 	}
822 	chain = tcf_chain_get(block, chain_index,
823 			      n->nlmsg_type == RTM_NEWTFILTER);
824 	if (!chain) {
825 		err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;
826 		goto errout;
827 	}
828 
829 	if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
830 		tfilter_notify_chain(net, skb, q, parent, n,
831 				     chain, RTM_DELTFILTER);
832 		tcf_chain_flush(chain);
833 		err = 0;
834 		goto errout;
835 	}
836 
837 	tp = tcf_chain_tp_find(chain, &chain_info, protocol,
838 			       prio, prio_allocate);
839 	if (IS_ERR(tp)) {
840 		err = PTR_ERR(tp);
841 		goto errout;
842 	}
843 
844 	if (tp == NULL) {
845 		/* Proto-tcf does not exist, create new one */
846 
847 		if (tca[TCA_KIND] == NULL || !protocol) {
848 			err = -EINVAL;
849 			goto errout;
850 		}
851 
852 		if (n->nlmsg_type != RTM_NEWTFILTER ||
853 		    !(n->nlmsg_flags & NLM_F_CREATE)) {
854 			err = -ENOENT;
855 			goto errout;
856 		}
857 
858 		if (prio_allocate)
859 			prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
860 
861 		tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
862 				      protocol, prio, parent, q, chain);
863 		if (IS_ERR(tp)) {
864 			err = PTR_ERR(tp);
865 			goto errout;
866 		}
867 		tp_created = 1;
868 	} else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
869 		err = -EINVAL;
870 		goto errout;
871 	}
872 
873 	fh = tp->ops->get(tp, t->tcm_handle);
874 
875 	if (!fh) {
876 		if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
877 			tcf_chain_tp_remove(chain, &chain_info, tp);
878 			tfilter_notify(net, skb, n, tp, q, parent, fh,
879 				       RTM_DELTFILTER, false);
880 			tcf_proto_destroy(tp);
881 			err = 0;
882 			goto errout;
883 		}
884 
885 		if (n->nlmsg_type != RTM_NEWTFILTER ||
886 		    !(n->nlmsg_flags & NLM_F_CREATE)) {
887 			err = -ENOENT;
888 			goto errout;
889 		}
890 	} else {
891 		bool last;
892 
893 		switch (n->nlmsg_type) {
894 		case RTM_NEWTFILTER:
895 			if (n->nlmsg_flags & NLM_F_EXCL) {
896 				if (tp_created)
897 					tcf_proto_destroy(tp);
898 				err = -EEXIST;
899 				goto errout;
900 			}
901 			break;
902 		case RTM_DELTFILTER:
903 			err = tfilter_del_notify(net, skb, n, tp, q, parent,
904 						 fh, false, &last);
905 			if (err)
906 				goto errout;
907 			if (last) {
908 				tcf_chain_tp_remove(chain, &chain_info, tp);
909 				tcf_proto_destroy(tp);
910 			}
911 			goto errout;
912 		case RTM_GETTFILTER:
913 			err = tfilter_notify(net, skb, n, tp, q, parent, fh,
914 					     RTM_NEWTFILTER, true);
915 			goto errout;
916 		default:
917 			err = -EINVAL;
918 			goto errout;
919 		}
920 	}
921 
922 	err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
923 			      n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
924 	if (err == 0) {
925 		if (tp_created)
926 			tcf_chain_tp_insert(chain, &chain_info, tp);
927 		tfilter_notify(net, skb, n, tp, q, parent, fh,
928 			       RTM_NEWTFILTER, false);
929 	} else {
930 		if (tp_created)
931 			tcf_proto_destroy(tp);
932 	}
933 
934 errout:
935 	if (chain)
936 		tcf_chain_put(chain);
937 	if (err == -EAGAIN)
938 		/* Replay the request. */
939 		goto replay;
940 	return err;
941 }
942 
943 struct tcf_dump_args {
944 	struct tcf_walker w;
945 	struct sk_buff *skb;
946 	struct netlink_callback *cb;
947 	struct Qdisc *q;
948 	u32 parent;
949 };
950 
951 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
952 {
953 	struct tcf_dump_args *a = (void *)arg;
954 	struct net *net = sock_net(a->skb->sk);
955 
956 	return tcf_fill_node(net, a->skb, tp, a->q, a->parent,
957 			     n, NETLINK_CB(a->cb->skb).portid,
958 			     a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
959 			     RTM_NEWTFILTER);
960 }
961 
962 static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
963 			   struct sk_buff *skb, struct netlink_callback *cb,
964 			   long index_start, long *p_index)
965 {
966 	struct net *net = sock_net(skb->sk);
967 	struct tcmsg *tcm = nlmsg_data(cb->nlh);
968 	struct tcf_dump_args arg;
969 	struct tcf_proto *tp;
970 
971 	for (tp = rtnl_dereference(chain->filter_chain);
972 	     tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
973 		if (*p_index < index_start)
974 			continue;
975 		if (TC_H_MAJ(tcm->tcm_info) &&
976 		    TC_H_MAJ(tcm->tcm_info) != tp->prio)
977 			continue;
978 		if (TC_H_MIN(tcm->tcm_info) &&
979 		    TC_H_MIN(tcm->tcm_info) != tp->protocol)
980 			continue;
981 		if (*p_index > index_start)
982 			memset(&cb->args[1], 0,
983 			       sizeof(cb->args) - sizeof(cb->args[0]));
984 		if (cb->args[1] == 0) {
985 			if (tcf_fill_node(net, skb, tp, q, parent, 0,
986 					  NETLINK_CB(cb->skb).portid,
987 					  cb->nlh->nlmsg_seq, NLM_F_MULTI,
988 					  RTM_NEWTFILTER) <= 0)
989 				return false;
990 
991 			cb->args[1] = 1;
992 		}
993 		if (!tp->ops->walk)
994 			continue;
995 		arg.w.fn = tcf_node_dump;
996 		arg.skb = skb;
997 		arg.cb = cb;
998 		arg.q = q;
999 		arg.parent = parent;
1000 		arg.w.stop = 0;
1001 		arg.w.skip = cb->args[1] - 1;
1002 		arg.w.count = 0;
1003 		tp->ops->walk(tp, &arg.w);
1004 		cb->args[1] = arg.w.count + 1;
1005 		if (arg.w.stop)
1006 			return false;
1007 	}
1008 	return true;
1009 }
1010 
1011 /* called with RTNL */
1012 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1013 {
1014 	struct net *net = sock_net(skb->sk);
1015 	struct nlattr *tca[TCA_MAX + 1];
1016 	struct net_device *dev;
1017 	struct Qdisc *q;
1018 	struct tcf_block *block;
1019 	struct tcf_chain *chain;
1020 	struct tcmsg *tcm = nlmsg_data(cb->nlh);
1021 	unsigned long cl = 0;
1022 	const struct Qdisc_class_ops *cops;
1023 	long index_start;
1024 	long index;
1025 	u32 parent;
1026 	int err;
1027 
1028 	if (nlmsg_len(cb->nlh) < sizeof(*tcm))
1029 		return skb->len;
1030 
1031 	err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1032 	if (err)
1033 		return err;
1034 
1035 	dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1036 	if (!dev)
1037 		return skb->len;
1038 
1039 	parent = tcm->tcm_parent;
1040 	if (!parent) {
1041 		q = dev->qdisc;
1042 		parent = q->handle;
1043 	} else {
1044 		q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1045 	}
1046 	if (!q)
1047 		goto out;
1048 	cops = q->ops->cl_ops;
1049 	if (!cops)
1050 		goto out;
1051 	if (!cops->tcf_block)
1052 		goto out;
1053 	if (TC_H_MIN(tcm->tcm_parent)) {
1054 		cl = cops->find(q, tcm->tcm_parent);
1055 		if (cl == 0)
1056 			goto out;
1057 	}
1058 	block = cops->tcf_block(q, cl);
1059 	if (!block)
1060 		goto out;
1061 
1062 	index_start = cb->args[0];
1063 	index = 0;
1064 
1065 	list_for_each_entry(chain, &block->chain_list, list) {
1066 		if (tca[TCA_CHAIN] &&
1067 		    nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1068 			continue;
1069 		if (!tcf_chain_dump(chain, q, parent, skb, cb,
1070 				    index_start, &index))
1071 			break;
1072 	}
1073 
1074 	cb->args[0] = index;
1075 
1076 out:
1077 	return skb->len;
1078 }
1079 
1080 void tcf_exts_destroy(struct tcf_exts *exts)
1081 {
1082 #ifdef CONFIG_NET_CLS_ACT
1083 	LIST_HEAD(actions);
1084 
1085 	ASSERT_RTNL();
1086 	tcf_exts_to_list(exts, &actions);
1087 	tcf_action_destroy(&actions, TCA_ACT_UNBIND);
1088 	kfree(exts->actions);
1089 	exts->nr_actions = 0;
1090 #endif
1091 }
1092 EXPORT_SYMBOL(tcf_exts_destroy);
1093 
1094 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
1095 		      struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
1096 {
1097 #ifdef CONFIG_NET_CLS_ACT
1098 	{
1099 		struct tc_action *act;
1100 
1101 		if (exts->police && tb[exts->police]) {
1102 			act = tcf_action_init_1(net, tp, tb[exts->police],
1103 						rate_tlv, "police", ovr,
1104 						TCA_ACT_BIND);
1105 			if (IS_ERR(act))
1106 				return PTR_ERR(act);
1107 
1108 			act->type = exts->type = TCA_OLD_COMPAT;
1109 			exts->actions[0] = act;
1110 			exts->nr_actions = 1;
1111 		} else if (exts->action && tb[exts->action]) {
1112 			LIST_HEAD(actions);
1113 			int err, i = 0;
1114 
1115 			err = tcf_action_init(net, tp, tb[exts->action],
1116 					      rate_tlv, NULL, ovr, TCA_ACT_BIND,
1117 					      &actions);
1118 			if (err)
1119 				return err;
1120 			list_for_each_entry(act, &actions, list)
1121 				exts->actions[i++] = act;
1122 			exts->nr_actions = i;
1123 		}
1124 	}
1125 #else
1126 	if ((exts->action && tb[exts->action]) ||
1127 	    (exts->police && tb[exts->police]))
1128 		return -EOPNOTSUPP;
1129 #endif
1130 
1131 	return 0;
1132 }
1133 EXPORT_SYMBOL(tcf_exts_validate);
1134 
1135 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
1136 {
1137 #ifdef CONFIG_NET_CLS_ACT
1138 	struct tcf_exts old = *dst;
1139 
1140 	*dst = *src;
1141 	tcf_exts_destroy(&old);
1142 #endif
1143 }
1144 EXPORT_SYMBOL(tcf_exts_change);
1145 
1146 #ifdef CONFIG_NET_CLS_ACT
1147 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
1148 {
1149 	if (exts->nr_actions == 0)
1150 		return NULL;
1151 	else
1152 		return exts->actions[0];
1153 }
1154 #endif
1155 
1156 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
1157 {
1158 #ifdef CONFIG_NET_CLS_ACT
1159 	struct nlattr *nest;
1160 
1161 	if (exts->action && tcf_exts_has_actions(exts)) {
1162 		/*
1163 		 * again for backward compatible mode - we want
1164 		 * to work with both old and new modes of entering
1165 		 * tc data even if iproute2  was newer - jhs
1166 		 */
1167 		if (exts->type != TCA_OLD_COMPAT) {
1168 			LIST_HEAD(actions);
1169 
1170 			nest = nla_nest_start(skb, exts->action);
1171 			if (nest == NULL)
1172 				goto nla_put_failure;
1173 
1174 			tcf_exts_to_list(exts, &actions);
1175 			if (tcf_action_dump(skb, &actions, 0, 0) < 0)
1176 				goto nla_put_failure;
1177 			nla_nest_end(skb, nest);
1178 		} else if (exts->police) {
1179 			struct tc_action *act = tcf_exts_first_act(exts);
1180 			nest = nla_nest_start(skb, exts->police);
1181 			if (nest == NULL || !act)
1182 				goto nla_put_failure;
1183 			if (tcf_action_dump_old(skb, act, 0, 0) < 0)
1184 				goto nla_put_failure;
1185 			nla_nest_end(skb, nest);
1186 		}
1187 	}
1188 	return 0;
1189 
1190 nla_put_failure:
1191 	nla_nest_cancel(skb, nest);
1192 	return -1;
1193 #else
1194 	return 0;
1195 #endif
1196 }
1197 EXPORT_SYMBOL(tcf_exts_dump);
1198 
1199 
1200 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
1201 {
1202 #ifdef CONFIG_NET_CLS_ACT
1203 	struct tc_action *a = tcf_exts_first_act(exts);
1204 	if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
1205 		return -1;
1206 #endif
1207 	return 0;
1208 }
1209 EXPORT_SYMBOL(tcf_exts_dump_stats);
1210 
1211 static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
1212 				       enum tc_setup_type type,
1213 				       void *type_data, bool err_stop)
1214 {
1215 	int ok_count = 0;
1216 #ifdef CONFIG_NET_CLS_ACT
1217 	const struct tc_action *a;
1218 	struct net_device *dev;
1219 	int i, ret;
1220 
1221 	if (!tcf_exts_has_actions(exts))
1222 		return 0;
1223 
1224 	for (i = 0; i < exts->nr_actions; i++) {
1225 		a = exts->actions[i];
1226 		if (!a->ops->get_dev)
1227 			continue;
1228 		dev = a->ops->get_dev(a);
1229 		if (!dev || !tc_can_offload(dev))
1230 			continue;
1231 		ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
1232 		if (ret < 0)
1233 			return ret;
1234 		ok_count += ret;
1235 	}
1236 #endif
1237 	return ok_count;
1238 }
1239 
1240 int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
1241 		     enum tc_setup_type type, void *type_data, bool err_stop)
1242 {
1243 	int ok_count;
1244 	int ret;
1245 
1246 	ret = tcf_block_cb_call(block, type, type_data, err_stop);
1247 	if (ret < 0)
1248 		return ret;
1249 	ok_count = ret;
1250 
1251 	if (!exts)
1252 		return ok_count;
1253 	ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
1254 	if (ret < 0)
1255 		return ret;
1256 	ok_count += ret;
1257 
1258 	return ok_count;
1259 }
1260 EXPORT_SYMBOL(tc_setup_cb_call);
1261 
1262 static int __init tc_filter_init(void)
1263 {
1264 	tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
1265 	if (!tc_filter_wq)
1266 		return -ENOMEM;
1267 
1268 	rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, 0);
1269 	rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, 0);
1270 	rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
1271 		      tc_dump_tfilter, 0);
1272 
1273 	return 0;
1274 }
1275 
1276 subsys_initcall(tc_filter_init);
1277