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 <asm/uaccess.h>
18#include <asm/system.h>
19#include <linux/bitops.h>
20#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/kernel.h>
23#include <linux/string.h>
24#include <linux/mm.h>
25#include <linux/socket.h>
26#include <linux/sockios.h>
27#include <linux/in.h>
28#include <linux/errno.h>
29#include <linux/interrupt.h>
30#include <linux/netdevice.h>
31#include <linux/skbuff.h>
32#include <linux/init.h>
33#include <linux/kmod.h>
34#include <linux/netlink.h>
35#include <net/netlink.h>
36#include <net/sock.h>
37#include <net/pkt_sched.h>
38#include <net/pkt_cls.h>
39
40#define DPRINTK(format,args...)
41
42/* The list of all installed classifier types */
43
44static struct tcf_proto_ops *tcf_proto_base;
45
46/* Protects list of registered TC modules. It is pure SMP lock. */
47static DEFINE_RWLOCK(cls_mod_lock);
48
49/* Find classifier type by string name */
50
51static struct tcf_proto_ops * tcf_proto_lookup_ops(struct rtattr *kind)
52{
53	struct tcf_proto_ops *t = NULL;
54
55	if (kind) {
56		read_lock(&cls_mod_lock);
57		for (t = tcf_proto_base; t; t = t->next) {
58			if (rtattr_strcmp(kind, t->kind) == 0) {
59				if (!try_module_get(t->owner))
60					t = NULL;
61				break;
62			}
63		}
64		read_unlock(&cls_mod_lock);
65	}
66	return t;
67}
68
69/* Register(unregister) new classifier type */
70
71int register_tcf_proto_ops(struct tcf_proto_ops *ops)
72{
73	struct tcf_proto_ops *t, **tp;
74	int rc = -EEXIST;
75
76	write_lock(&cls_mod_lock);
77	for (tp = &tcf_proto_base; (t = *tp) != NULL; tp = &t->next)
78		if (!strcmp(ops->kind, t->kind))
79			goto out;
80
81	ops->next = NULL;
82	*tp = ops;
83	rc = 0;
84out:
85	write_unlock(&cls_mod_lock);
86	return rc;
87}
88
89int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
90{
91	struct tcf_proto_ops *t, **tp;
92	int rc = -ENOENT;
93
94	write_lock(&cls_mod_lock);
95	for (tp = &tcf_proto_base; (t=*tp) != NULL; tp = &t->next)
96		if (t == ops)
97			break;
98
99	if (!t)
100		goto out;
101	*tp = t->next;
102	rc = 0;
103out:
104	write_unlock(&cls_mod_lock);
105	return rc;
106}
107
108static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
109			  struct tcf_proto *tp, unsigned long fh, int event);
110
111
112/* Select new prio value from the range, managed by kernel. */
113
114static __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 first;
122}
123
124/* Add/change/delete/get a filter node */
125
126static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
127{
128	struct rtattr **tca;
129	struct tcmsg *t;
130	u32 protocol;
131	u32 prio;
132	u32 nprio;
133	u32 parent;
134	struct net_device *dev;
135	struct Qdisc  *q;
136	struct tcf_proto **back, **chain;
137	struct tcf_proto *tp;
138	struct tcf_proto_ops *tp_ops;
139	struct Qdisc_class_ops *cops;
140	unsigned long cl;
141	unsigned long fh;
142	int err;
143
144replay:
145	tca = arg;
146	t = NLMSG_DATA(n);
147	protocol = TC_H_MIN(t->tcm_info);
148	prio = TC_H_MAJ(t->tcm_info);
149	nprio = prio;
150	parent = t->tcm_parent;
151	cl = 0;
152
153	if (prio == 0) {
154		/* If no priority is given, user wants we allocated it. */
155		if (n->nlmsg_type != RTM_NEWTFILTER || !(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	if ((dev = __dev_get_by_index(t->tcm_ifindex)) == NULL)
164		return -ENODEV;
165
166	/* Find qdisc */
167	if (!parent) {
168		q = dev->qdisc_sleeping;
169		parent = q->handle;
170	} else if ((q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent))) == NULL)
171		return -EINVAL;
172
173	/* Is it classful? */
174	if ((cops = q->ops->cl_ops) == NULL)
175		return -EINVAL;
176
177	/* Do we search for filter, attached to class? */
178	if (TC_H_MIN(parent)) {
179		cl = cops->get(q, parent);
180		if (cl == 0)
181			return -ENOENT;
182	}
183
184	/* And the last stroke */
185	chain = cops->tcf_chain(q, cl);
186	err = -EINVAL;
187	if (chain == NULL)
188		goto errout;
189
190	/* Check the chain for existence of proto-tcf with this priority */
191	for (back = chain; (tp=*back) != NULL; back = &tp->next) {
192		if (tp->prio >= prio) {
193			if (tp->prio == prio) {
194				if (!nprio || (tp->protocol != protocol && protocol))
195					goto errout;
196			} else
197				tp = NULL;
198			break;
199		}
200	}
201
202	if (tp == NULL) {
203		/* Proto-tcf does not exist, create new one */
204
205		if (tca[TCA_KIND-1] == NULL || !protocol)
206			goto errout;
207
208		err = -ENOENT;
209		if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
210			goto errout;
211
212
213		/* Create new proto tcf */
214
215		err = -ENOBUFS;
216		if ((tp = kzalloc(sizeof(*tp), GFP_KERNEL)) == NULL)
217			goto errout;
218		err = -EINVAL;
219		tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND-1]);
220		if (tp_ops == NULL) {
221#ifdef CONFIG_KMOD
222			struct rtattr *kind = tca[TCA_KIND-1];
223			char name[IFNAMSIZ];
224
225			if (kind != NULL &&
226			    rtattr_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
227				rtnl_unlock();
228				request_module("cls_%s", name);
229				rtnl_lock();
230				tp_ops = tcf_proto_lookup_ops(kind);
231				/* We dropped the RTNL semaphore in order to
232				 * perform the module load.  So, even if we
233				 * succeeded in loading the module we have to
234				 * replay the request.  We indicate this using
235				 * -EAGAIN.
236				 */
237				if (tp_ops != NULL) {
238					module_put(tp_ops->owner);
239					err = -EAGAIN;
240				}
241			}
242#endif
243			kfree(tp);
244			goto errout;
245		}
246		tp->ops = tp_ops;
247		tp->protocol = protocol;
248		tp->prio = nprio ? : tcf_auto_prio(*back);
249		tp->q = q;
250		tp->classify = tp_ops->classify;
251		tp->classid = parent;
252		if ((err = tp_ops->init(tp)) != 0) {
253			module_put(tp_ops->owner);
254			kfree(tp);
255			goto errout;
256		}
257
258		qdisc_lock_tree(dev);
259		tp->next = *back;
260		*back = tp;
261		qdisc_unlock_tree(dev);
262
263	} else if (tca[TCA_KIND-1] && rtattr_strcmp(tca[TCA_KIND-1], tp->ops->kind))
264		goto errout;
265
266	fh = tp->ops->get(tp, t->tcm_handle);
267
268	if (fh == 0) {
269		if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
270			qdisc_lock_tree(dev);
271			*back = tp->next;
272			qdisc_unlock_tree(dev);
273
274			tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER);
275			tcf_destroy(tp);
276			err = 0;
277			goto errout;
278		}
279
280		err = -ENOENT;
281		if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
282			goto errout;
283	} else {
284		switch (n->nlmsg_type) {
285		case RTM_NEWTFILTER:
286			err = -EEXIST;
287			if (n->nlmsg_flags&NLM_F_EXCL)
288				goto errout;
289			break;
290		case RTM_DELTFILTER:
291			err = tp->ops->delete(tp, fh);
292			if (err == 0)
293				tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER);
294			goto errout;
295		case RTM_GETTFILTER:
296			err = tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
297			goto errout;
298		default:
299			err = -EINVAL;
300			goto errout;
301		}
302	}
303
304	err = tp->ops->change(tp, cl, t->tcm_handle, tca, &fh);
305	if (err == 0)
306		tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
307
308errout:
309	if (cl)
310		cops->put(q, cl);
311	if (err == -EAGAIN)
312		/* Replay the request. */
313		goto replay;
314	return err;
315}
316
317static int
318tcf_fill_node(struct sk_buff *skb, struct tcf_proto *tp, unsigned long fh,
319	      u32 pid, u32 seq, u16 flags, int event)
320{
321	struct tcmsg *tcm;
322	struct nlmsghdr  *nlh;
323	unsigned char *b = skb_tail_pointer(skb);
324
325	nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
326	tcm = NLMSG_DATA(nlh);
327	tcm->tcm_family = AF_UNSPEC;
328	tcm->tcm__pad1 = 0;
329	tcm->tcm__pad1 = 0;
330	tcm->tcm_ifindex = tp->q->dev->ifindex;
331	tcm->tcm_parent = tp->classid;
332	tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
333	RTA_PUT(skb, TCA_KIND, IFNAMSIZ, tp->ops->kind);
334	tcm->tcm_handle = fh;
335	if (RTM_DELTFILTER != event) {
336		tcm->tcm_handle = 0;
337		if (tp->ops->dump && tp->ops->dump(tp, fh, skb, tcm) < 0)
338			goto rtattr_failure;
339	}
340	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
341	return skb->len;
342
343nlmsg_failure:
344rtattr_failure:
345	nlmsg_trim(skb, b);
346	return -1;
347}
348
349static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
350			  struct tcf_proto *tp, unsigned long fh, int event)
351{
352	struct sk_buff *skb;
353	u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
354
355	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
356	if (!skb)
357		return -ENOBUFS;
358
359	if (tcf_fill_node(skb, tp, fh, pid, n->nlmsg_seq, 0, event) <= 0) {
360		kfree_skb(skb);
361		return -EINVAL;
362	}
363
364	return rtnetlink_send(skb, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
365}
366
367struct tcf_dump_args
368{
369	struct tcf_walker w;
370	struct sk_buff *skb;
371	struct netlink_callback *cb;
372};
373
374static int tcf_node_dump(struct tcf_proto *tp, unsigned long n, struct tcf_walker *arg)
375{
376	struct tcf_dump_args *a = (void*)arg;
377
378	return tcf_fill_node(a->skb, tp, n, NETLINK_CB(a->cb->skb).pid,
379			     a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER);
380}
381
382static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
383{
384	int t;
385	int s_t;
386	struct net_device *dev;
387	struct Qdisc *q;
388	struct tcf_proto *tp, **chain;
389	struct tcmsg *tcm = (struct tcmsg*)NLMSG_DATA(cb->nlh);
390	unsigned long cl = 0;
391	struct Qdisc_class_ops *cops;
392	struct tcf_dump_args arg;
393
394	if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
395		return skb->len;
396	if ((dev = dev_get_by_index(tcm->tcm_ifindex)) == NULL)
397		return skb->len;
398
399	if (!tcm->tcm_parent)
400		q = dev->qdisc_sleeping;
401	else
402		q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
403	if (!q)
404		goto out;
405	if ((cops = q->ops->cl_ops) == NULL)
406		goto errout;
407	if (TC_H_MIN(tcm->tcm_parent)) {
408		cl = cops->get(q, tcm->tcm_parent);
409		if (cl == 0)
410			goto errout;
411	}
412	chain = cops->tcf_chain(q, cl);
413	if (chain == NULL)
414		goto errout;
415
416	s_t = cb->args[0];
417
418	for (tp=*chain, t=0; tp; tp = tp->next, t++) {
419		if (t < s_t) continue;
420		if (TC_H_MAJ(tcm->tcm_info) &&
421		    TC_H_MAJ(tcm->tcm_info) != tp->prio)
422			continue;
423		if (TC_H_MIN(tcm->tcm_info) &&
424		    TC_H_MIN(tcm->tcm_info) != tp->protocol)
425			continue;
426		if (t > s_t)
427			memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
428		if (cb->args[1] == 0) {
429			if (tcf_fill_node(skb, tp, 0, NETLINK_CB(cb->skb).pid,
430					  cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER) <= 0) {
431				break;
432			}
433			cb->args[1] = 1;
434		}
435		if (tp->ops->walk == NULL)
436			continue;
437		arg.w.fn = tcf_node_dump;
438		arg.skb = skb;
439		arg.cb = cb;
440		arg.w.stop = 0;
441		arg.w.skip = cb->args[1]-1;
442		arg.w.count = 0;
443		tp->ops->walk(tp, &arg.w);
444		cb->args[1] = arg.w.count+1;
445		if (arg.w.stop)
446			break;
447	}
448
449	cb->args[0] = t;
450
451errout:
452	if (cl)
453		cops->put(q, cl);
454out:
455	dev_put(dev);
456	return skb->len;
457}
458
459void
460tcf_exts_destroy(struct tcf_proto *tp, struct tcf_exts *exts)
461{
462#ifdef CONFIG_NET_CLS_ACT
463	if (exts->action) {
464		tcf_action_destroy(exts->action, TCA_ACT_UNBIND);
465		exts->action = NULL;
466	}
467#elif defined CONFIG_NET_CLS_POLICE
468	if (exts->police) {
469		tcf_police_release(exts->police, TCA_ACT_UNBIND);
470		exts->police = NULL;
471	}
472#endif
473}
474
475
476int
477tcf_exts_validate(struct tcf_proto *tp, struct rtattr **tb,
478		  struct rtattr *rate_tlv, struct tcf_exts *exts,
479		  struct tcf_ext_map *map)
480{
481	memset(exts, 0, sizeof(*exts));
482
483#ifdef CONFIG_NET_CLS_ACT
484	{
485		int err;
486		struct tc_action *act;
487
488		if (map->police && tb[map->police-1]) {
489			act = tcf_action_init_1(tb[map->police-1], rate_tlv, "police",
490				TCA_ACT_NOREPLACE, TCA_ACT_BIND, &err);
491			if (act == NULL)
492				return err;
493
494			act->type = TCA_OLD_COMPAT;
495			exts->action = act;
496		} else if (map->action && tb[map->action-1]) {
497			act = tcf_action_init(tb[map->action-1], rate_tlv, NULL,
498				TCA_ACT_NOREPLACE, TCA_ACT_BIND, &err);
499			if (act == NULL)
500				return err;
501
502			exts->action = act;
503		}
504	}
505#elif defined CONFIG_NET_CLS_POLICE
506	if (map->police && tb[map->police-1]) {
507		struct tcf_police *p;
508
509		p = tcf_police_locate(tb[map->police-1], rate_tlv);
510		if (p == NULL)
511			return -EINVAL;
512
513		exts->police = p;
514	} else if (map->action && tb[map->action-1])
515		return -EOPNOTSUPP;
516#else
517	if ((map->action && tb[map->action-1]) ||
518	    (map->police && tb[map->police-1]))
519		return -EOPNOTSUPP;
520#endif
521
522	return 0;
523}
524
525void
526tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
527		struct tcf_exts *src)
528{
529#ifdef CONFIG_NET_CLS_ACT
530	if (src->action) {
531		struct tc_action *act;
532		tcf_tree_lock(tp);
533		act = xchg(&dst->action, src->action);
534		tcf_tree_unlock(tp);
535		if (act)
536			tcf_action_destroy(act, TCA_ACT_UNBIND);
537	}
538#elif defined CONFIG_NET_CLS_POLICE
539	if (src->police) {
540		struct tcf_police *p;
541		tcf_tree_lock(tp);
542		p = xchg(&dst->police, src->police);
543		tcf_tree_unlock(tp);
544		if (p)
545			tcf_police_release(p, TCA_ACT_UNBIND);
546	}
547#endif
548}
549
550int
551tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts,
552	      struct tcf_ext_map *map)
553{
554#ifdef CONFIG_NET_CLS_ACT
555	if (map->action && exts->action) {
556		/*
557		 * again for backward compatible mode - we want
558		 * to work with both old and new modes of entering
559		 * tc data even if iproute2  was newer - jhs
560		 */
561		struct rtattr *p_rta = (struct rtattr *)skb_tail_pointer(skb);
562
563		if (exts->action->type != TCA_OLD_COMPAT) {
564			RTA_PUT(skb, map->action, 0, NULL);
565			if (tcf_action_dump(skb, exts->action, 0, 0) < 0)
566				goto rtattr_failure;
567			p_rta->rta_len = skb_tail_pointer(skb) - (u8 *)p_rta;
568		} else if (map->police) {
569			RTA_PUT(skb, map->police, 0, NULL);
570			if (tcf_action_dump_old(skb, exts->action, 0, 0) < 0)
571				goto rtattr_failure;
572			p_rta->rta_len = skb_tail_pointer(skb) - (u8 *)p_rta;
573		}
574	}
575#elif defined CONFIG_NET_CLS_POLICE
576	if (map->police && exts->police) {
577		struct rtattr *p_rta = (struct rtattr *)skb_tail_pointer(skb);
578
579		RTA_PUT(skb, map->police, 0, NULL);
580
581		if (tcf_police_dump(skb, exts->police) < 0)
582			goto rtattr_failure;
583
584		p_rta->rta_len = skb_tail_pointer(skb) - (u8 *)p_rta;
585	}
586#endif
587	return 0;
588rtattr_failure: __attribute__ ((unused))
589	return -1;
590}
591
592int
593tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts,
594		    struct tcf_ext_map *map)
595{
596#ifdef CONFIG_NET_CLS_ACT
597	if (exts->action)
598		if (tcf_action_copy_stats(skb, exts->action, 1) < 0)
599			goto rtattr_failure;
600#elif defined CONFIG_NET_CLS_POLICE
601	if (exts->police)
602		if (tcf_police_dump_stats(skb, exts->police) < 0)
603			goto rtattr_failure;
604#endif
605	return 0;
606rtattr_failure: __attribute__ ((unused))
607	return -1;
608}
609
610static int __init tc_filter_init(void)
611{
612	rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL);
613	rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL);
614	rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
615						 tc_dump_tfilter);
616
617	return 0;
618}
619
620subsys_initcall(tc_filter_init);
621
622EXPORT_SYMBOL(register_tcf_proto_ops);
623EXPORT_SYMBOL(unregister_tcf_proto_ops);
624EXPORT_SYMBOL(tcf_exts_validate);
625EXPORT_SYMBOL(tcf_exts_destroy);
626EXPORT_SYMBOL(tcf_exts_change);
627EXPORT_SYMBOL(tcf_exts_dump);
628EXPORT_SYMBOL(tcf_exts_dump_stats);
629