xref: /linux/net/sched/cls_api.c (revision 2d87650a3bf1b80f7d0d150ee1af3f8a89e5b7aa)
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/skbuff.h>
23 #include <linux/init.h>
24 #include <linux/kmod.h>
25 #include <linux/err.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(struct nlattr *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 (nla_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 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
80 {
81 	struct tcf_proto_ops *t;
82 	int rc = -ENOENT;
83 
84 	write_lock(&cls_mod_lock);
85 	list_for_each_entry(t, &tcf_proto_base, head) {
86 		if (t == ops) {
87 			list_del(&t->head);
88 			rc = 0;
89 			break;
90 		}
91 	}
92 	write_unlock(&cls_mod_lock);
93 	return rc;
94 }
95 EXPORT_SYMBOL(unregister_tcf_proto_ops);
96 
97 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
98 			  struct nlmsghdr *n, struct tcf_proto *tp,
99 			  unsigned long fh, int event);
100 
101 
102 /* Select new prio value from the range, managed by kernel. */
103 
104 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
105 {
106 	u32 first = TC_H_MAKE(0xC0000000U, 0U);
107 
108 	if (tp)
109 		first = tp->prio - 1;
110 
111 	return first;
112 }
113 
114 /* Add/change/delete/get a filter node */
115 
116 static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n)
117 {
118 	struct net *net = sock_net(skb->sk);
119 	struct nlattr *tca[TCA_MAX + 1];
120 	spinlock_t *root_lock;
121 	struct tcmsg *t;
122 	u32 protocol;
123 	u32 prio;
124 	u32 nprio;
125 	u32 parent;
126 	struct net_device *dev;
127 	struct Qdisc  *q;
128 	struct tcf_proto **back, **chain;
129 	struct tcf_proto *tp;
130 	const struct tcf_proto_ops *tp_ops;
131 	const struct Qdisc_class_ops *cops;
132 	unsigned long cl;
133 	unsigned long fh;
134 	int err;
135 	int tp_created = 0;
136 
137 	if ((n->nlmsg_type != RTM_GETTFILTER) && !capable(CAP_NET_ADMIN))
138 		return -EPERM;
139 
140 replay:
141 	err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL);
142 	if (err < 0)
143 		return err;
144 
145 	t = nlmsg_data(n);
146 	protocol = TC_H_MIN(t->tcm_info);
147 	prio = TC_H_MAJ(t->tcm_info);
148 	nprio = prio;
149 	parent = t->tcm_parent;
150 	cl = 0;
151 
152 	if (prio == 0) {
153 		/* If no priority is given, user wants we allocated it. */
154 		if (n->nlmsg_type != RTM_NEWTFILTER ||
155 		    !(n->nlmsg_flags & NLM_F_CREATE))
156 			return -ENOENT;
157 		prio = TC_H_MAKE(0x80000000U, 0U);
158 	}
159 
160 	/* Find head of filter chain. */
161 
162 	/* Find link */
163 	dev = __dev_get_by_index(net, t->tcm_ifindex);
164 	if (dev == NULL)
165 		return -ENODEV;
166 
167 	/* Find qdisc */
168 	if (!parent) {
169 		q = dev->qdisc;
170 		parent = q->handle;
171 	} else {
172 		q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
173 		if (q == NULL)
174 			return -EINVAL;
175 	}
176 
177 	/* Is it classful? */
178 	cops = q->ops->cl_ops;
179 	if (!cops)
180 		return -EINVAL;
181 
182 	if (cops->tcf_chain == NULL)
183 		return -EOPNOTSUPP;
184 
185 	/* Do we search for filter, attached to class? */
186 	if (TC_H_MIN(parent)) {
187 		cl = cops->get(q, parent);
188 		if (cl == 0)
189 			return -ENOENT;
190 	}
191 
192 	/* And the last stroke */
193 	chain = cops->tcf_chain(q, cl);
194 	err = -EINVAL;
195 	if (chain == NULL)
196 		goto errout;
197 
198 	/* Check the chain for existence of proto-tcf with this priority */
199 	for (back = chain; (tp = *back) != NULL; back = &tp->next) {
200 		if (tp->prio >= prio) {
201 			if (tp->prio == prio) {
202 				if (!nprio ||
203 				    (tp->protocol != protocol && protocol))
204 					goto errout;
205 			} else
206 				tp = NULL;
207 			break;
208 		}
209 	}
210 
211 	root_lock = qdisc_root_sleeping_lock(q);
212 
213 	if (tp == NULL) {
214 		/* Proto-tcf does not exist, create new one */
215 
216 		if (tca[TCA_KIND] == NULL || !protocol)
217 			goto errout;
218 
219 		err = -ENOENT;
220 		if (n->nlmsg_type != RTM_NEWTFILTER ||
221 		    !(n->nlmsg_flags & NLM_F_CREATE))
222 			goto errout;
223 
224 
225 		/* Create new proto tcf */
226 
227 		err = -ENOBUFS;
228 		tp = kzalloc(sizeof(*tp), GFP_KERNEL);
229 		if (tp == NULL)
230 			goto errout;
231 		err = -ENOENT;
232 		tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND]);
233 		if (tp_ops == NULL) {
234 #ifdef CONFIG_MODULES
235 			struct nlattr *kind = tca[TCA_KIND];
236 			char name[IFNAMSIZ];
237 
238 			if (kind != NULL &&
239 			    nla_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
240 				rtnl_unlock();
241 				request_module("cls_%s", name);
242 				rtnl_lock();
243 				tp_ops = tcf_proto_lookup_ops(kind);
244 				/* We dropped the RTNL semaphore in order to
245 				 * perform the module load.  So, even if we
246 				 * succeeded in loading the module we have to
247 				 * replay the request.  We indicate this using
248 				 * -EAGAIN.
249 				 */
250 				if (tp_ops != NULL) {
251 					module_put(tp_ops->owner);
252 					err = -EAGAIN;
253 				}
254 			}
255 #endif
256 			kfree(tp);
257 			goto errout;
258 		}
259 		tp->ops = tp_ops;
260 		tp->protocol = protocol;
261 		tp->prio = nprio ? : TC_H_MAJ(tcf_auto_prio(*back));
262 		tp->q = q;
263 		tp->classify = tp_ops->classify;
264 		tp->classid = parent;
265 
266 		err = tp_ops->init(tp);
267 		if (err != 0) {
268 			module_put(tp_ops->owner);
269 			kfree(tp);
270 			goto errout;
271 		}
272 
273 		tp_created = 1;
274 
275 	} else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind))
276 		goto errout;
277 
278 	fh = tp->ops->get(tp, t->tcm_handle);
279 
280 	if (fh == 0) {
281 		if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
282 			spin_lock_bh(root_lock);
283 			*back = tp->next;
284 			spin_unlock_bh(root_lock);
285 
286 			tfilter_notify(net, skb, n, tp, fh, RTM_DELTFILTER);
287 			tcf_destroy(tp);
288 			err = 0;
289 			goto errout;
290 		}
291 
292 		err = -ENOENT;
293 		if (n->nlmsg_type != RTM_NEWTFILTER ||
294 		    !(n->nlmsg_flags & NLM_F_CREATE))
295 			goto errout;
296 	} else {
297 		switch (n->nlmsg_type) {
298 		case RTM_NEWTFILTER:
299 			err = -EEXIST;
300 			if (n->nlmsg_flags & NLM_F_EXCL) {
301 				if (tp_created)
302 					tcf_destroy(tp);
303 				goto errout;
304 			}
305 			break;
306 		case RTM_DELTFILTER:
307 			err = tp->ops->delete(tp, fh);
308 			if (err == 0)
309 				tfilter_notify(net, skb, n, tp, fh, RTM_DELTFILTER);
310 			goto errout;
311 		case RTM_GETTFILTER:
312 			err = tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER);
313 			goto errout;
314 		default:
315 			err = -EINVAL;
316 			goto errout;
317 		}
318 	}
319 
320 	err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh);
321 	if (err == 0) {
322 		if (tp_created) {
323 			spin_lock_bh(root_lock);
324 			tp->next = *back;
325 			*back = tp;
326 			spin_unlock_bh(root_lock);
327 		}
328 		tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER);
329 	} else {
330 		if (tp_created)
331 			tcf_destroy(tp);
332 	}
333 
334 errout:
335 	if (cl)
336 		cops->put(q, cl);
337 	if (err == -EAGAIN)
338 		/* Replay the request. */
339 		goto replay;
340 	return err;
341 }
342 
343 static int tcf_fill_node(struct sk_buff *skb, struct tcf_proto *tp,
344 			 unsigned long fh, u32 portid, u32 seq, u16 flags, int event)
345 {
346 	struct tcmsg *tcm;
347 	struct nlmsghdr  *nlh;
348 	unsigned char *b = skb_tail_pointer(skb);
349 
350 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
351 	if (!nlh)
352 		goto out_nlmsg_trim;
353 	tcm = nlmsg_data(nlh);
354 	tcm->tcm_family = AF_UNSPEC;
355 	tcm->tcm__pad1 = 0;
356 	tcm->tcm__pad2 = 0;
357 	tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex;
358 	tcm->tcm_parent = tp->classid;
359 	tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
360 	if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
361 		goto nla_put_failure;
362 	tcm->tcm_handle = fh;
363 	if (RTM_DELTFILTER != event) {
364 		tcm->tcm_handle = 0;
365 		if (tp->ops->dump && tp->ops->dump(tp, fh, skb, tcm) < 0)
366 			goto nla_put_failure;
367 	}
368 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
369 	return skb->len;
370 
371 out_nlmsg_trim:
372 nla_put_failure:
373 	nlmsg_trim(skb, b);
374 	return -1;
375 }
376 
377 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
378 			  struct nlmsghdr *n, struct tcf_proto *tp,
379 			  unsigned long fh, int event)
380 {
381 	struct sk_buff *skb;
382 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
383 
384 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
385 	if (!skb)
386 		return -ENOBUFS;
387 
388 	if (tcf_fill_node(skb, tp, fh, portid, n->nlmsg_seq, 0, event) <= 0) {
389 		kfree_skb(skb);
390 		return -EINVAL;
391 	}
392 
393 	return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
394 			      n->nlmsg_flags & NLM_F_ECHO);
395 }
396 
397 struct tcf_dump_args {
398 	struct tcf_walker w;
399 	struct sk_buff *skb;
400 	struct netlink_callback *cb;
401 };
402 
403 static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
404 			 struct tcf_walker *arg)
405 {
406 	struct tcf_dump_args *a = (void *)arg;
407 
408 	return tcf_fill_node(a->skb, tp, n, NETLINK_CB(a->cb->skb).portid,
409 			     a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER);
410 }
411 
412 /* called with RTNL */
413 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
414 {
415 	struct net *net = sock_net(skb->sk);
416 	int t;
417 	int s_t;
418 	struct net_device *dev;
419 	struct Qdisc *q;
420 	struct tcf_proto *tp, **chain;
421 	struct tcmsg *tcm = nlmsg_data(cb->nlh);
422 	unsigned long cl = 0;
423 	const struct Qdisc_class_ops *cops;
424 	struct tcf_dump_args arg;
425 
426 	if (nlmsg_len(cb->nlh) < sizeof(*tcm))
427 		return skb->len;
428 	dev = __dev_get_by_index(net, tcm->tcm_ifindex);
429 	if (!dev)
430 		return skb->len;
431 
432 	if (!tcm->tcm_parent)
433 		q = dev->qdisc;
434 	else
435 		q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
436 	if (!q)
437 		goto out;
438 	cops = q->ops->cl_ops;
439 	if (!cops)
440 		goto errout;
441 	if (cops->tcf_chain == NULL)
442 		goto errout;
443 	if (TC_H_MIN(tcm->tcm_parent)) {
444 		cl = cops->get(q, tcm->tcm_parent);
445 		if (cl == 0)
446 			goto errout;
447 	}
448 	chain = cops->tcf_chain(q, cl);
449 	if (chain == NULL)
450 		goto errout;
451 
452 	s_t = cb->args[0];
453 
454 	for (tp = *chain, t = 0; tp; tp = tp->next, t++) {
455 		if (t < s_t)
456 			continue;
457 		if (TC_H_MAJ(tcm->tcm_info) &&
458 		    TC_H_MAJ(tcm->tcm_info) != tp->prio)
459 			continue;
460 		if (TC_H_MIN(tcm->tcm_info) &&
461 		    TC_H_MIN(tcm->tcm_info) != tp->protocol)
462 			continue;
463 		if (t > s_t)
464 			memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
465 		if (cb->args[1] == 0) {
466 			if (tcf_fill_node(skb, tp, 0, NETLINK_CB(cb->skb).portid,
467 					  cb->nlh->nlmsg_seq, NLM_F_MULTI,
468 					  RTM_NEWTFILTER) <= 0)
469 				break;
470 
471 			cb->args[1] = 1;
472 		}
473 		if (tp->ops->walk == NULL)
474 			continue;
475 		arg.w.fn = tcf_node_dump;
476 		arg.skb = skb;
477 		arg.cb = cb;
478 		arg.w.stop = 0;
479 		arg.w.skip = cb->args[1] - 1;
480 		arg.w.count = 0;
481 		tp->ops->walk(tp, &arg.w);
482 		cb->args[1] = arg.w.count + 1;
483 		if (arg.w.stop)
484 			break;
485 	}
486 
487 	cb->args[0] = t;
488 
489 errout:
490 	if (cl)
491 		cops->put(q, cl);
492 out:
493 	return skb->len;
494 }
495 
496 void tcf_exts_destroy(struct tcf_proto *tp, struct tcf_exts *exts)
497 {
498 #ifdef CONFIG_NET_CLS_ACT
499 	tcf_action_destroy(&exts->actions, TCA_ACT_UNBIND);
500 	INIT_LIST_HEAD(&exts->actions);
501 #endif
502 }
503 EXPORT_SYMBOL(tcf_exts_destroy);
504 
505 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
506 		  struct nlattr *rate_tlv, struct tcf_exts *exts)
507 {
508 #ifdef CONFIG_NET_CLS_ACT
509 	{
510 		struct tc_action *act;
511 
512 		INIT_LIST_HEAD(&exts->actions);
513 		if (exts->police && tb[exts->police]) {
514 			act = tcf_action_init_1(net, tb[exts->police], rate_tlv,
515 						"police", TCA_ACT_NOREPLACE,
516 						TCA_ACT_BIND);
517 			if (IS_ERR(act))
518 				return PTR_ERR(act);
519 
520 			act->type = exts->type = TCA_OLD_COMPAT;
521 			list_add(&act->list, &exts->actions);
522 		} else if (exts->action && tb[exts->action]) {
523 			int err;
524 			err = tcf_action_init(net, tb[exts->action], rate_tlv,
525 					      NULL, TCA_ACT_NOREPLACE,
526 					      TCA_ACT_BIND, &exts->actions);
527 			if (err)
528 				return err;
529 		}
530 	}
531 #else
532 	if ((exts->action && tb[exts->action]) ||
533 	    (exts->police && tb[exts->police]))
534 		return -EOPNOTSUPP;
535 #endif
536 
537 	return 0;
538 }
539 EXPORT_SYMBOL(tcf_exts_validate);
540 
541 void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
542 		     struct tcf_exts *src)
543 {
544 #ifdef CONFIG_NET_CLS_ACT
545 	if (!list_empty(&src->actions)) {
546 		LIST_HEAD(tmp);
547 		tcf_tree_lock(tp);
548 		list_splice_init(&dst->actions, &tmp);
549 		list_splice(&src->actions, &dst->actions);
550 		tcf_tree_unlock(tp);
551 		tcf_action_destroy(&tmp, TCA_ACT_UNBIND);
552 	}
553 #endif
554 }
555 EXPORT_SYMBOL(tcf_exts_change);
556 
557 #define tcf_exts_first_act(ext) \
558 		list_first_entry(&(exts)->actions, struct tc_action, list)
559 
560 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
561 {
562 #ifdef CONFIG_NET_CLS_ACT
563 	if (exts->action && !list_empty(&exts->actions)) {
564 		/*
565 		 * again for backward compatible mode - we want
566 		 * to work with both old and new modes of entering
567 		 * tc data even if iproute2  was newer - jhs
568 		 */
569 		struct nlattr *nest;
570 		if (exts->type != TCA_OLD_COMPAT) {
571 			nest = nla_nest_start(skb, exts->action);
572 			if (nest == NULL)
573 				goto nla_put_failure;
574 			if (tcf_action_dump(skb, &exts->actions, 0, 0) < 0)
575 				goto nla_put_failure;
576 			nla_nest_end(skb, nest);
577 		} else if (exts->police) {
578 			struct tc_action *act = tcf_exts_first_act(exts);
579 			nest = nla_nest_start(skb, exts->police);
580 			if (nest == NULL)
581 				goto nla_put_failure;
582 			if (tcf_action_dump_old(skb, act, 0, 0) < 0)
583 				goto nla_put_failure;
584 			nla_nest_end(skb, nest);
585 		}
586 	}
587 #endif
588 	return 0;
589 nla_put_failure: __attribute__ ((unused))
590 	return -1;
591 }
592 EXPORT_SYMBOL(tcf_exts_dump);
593 
594 
595 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
596 {
597 #ifdef CONFIG_NET_CLS_ACT
598 	struct tc_action *a = tcf_exts_first_act(exts);
599 	if (tcf_action_copy_stats(skb, a, 1) < 0)
600 		return -1;
601 #endif
602 	return 0;
603 }
604 EXPORT_SYMBOL(tcf_exts_dump_stats);
605 
606 static int __init tc_filter_init(void)
607 {
608 	rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, NULL);
609 	rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, NULL);
610 	rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
611 		      tc_dump_tfilter, NULL);
612 
613 	return 0;
614 }
615 
616 subsys_initcall(tc_filter_init);
617