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