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