[PPP_MPPE]: Fix "osize too small" check.
[usb.git] / net / sched / cls_api.c
blobebf94edf0478b6517d7b22b95e3cbb225a6229e1
1 /*
2 * net/sched/cls_api.c Packet classifier API.
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
11 * Changes:
13 * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
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>
40 #if 0 /* control */
41 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
42 #else
43 #define DPRINTK(format,args...)
44 #endif
46 /* The list of all installed classifier types */
48 static struct tcf_proto_ops *tcf_proto_base;
50 /* Protects list of registered TC modules. It is pure SMP lock. */
51 static DEFINE_RWLOCK(cls_mod_lock);
53 /* Find classifier type by string name */
55 static struct tcf_proto_ops * tcf_proto_lookup_ops(struct rtattr *kind)
57 struct tcf_proto_ops *t = NULL;
59 if (kind) {
60 read_lock(&cls_mod_lock);
61 for (t = tcf_proto_base; t; t = t->next) {
62 if (rtattr_strcmp(kind, t->kind) == 0) {
63 if (!try_module_get(t->owner))
64 t = NULL;
65 break;
68 read_unlock(&cls_mod_lock);
70 return t;
73 /* Register(unregister) new classifier type */
75 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
77 struct tcf_proto_ops *t, **tp;
78 int rc = -EEXIST;
80 write_lock(&cls_mod_lock);
81 for (tp = &tcf_proto_base; (t = *tp) != NULL; tp = &t->next)
82 if (!strcmp(ops->kind, t->kind))
83 goto out;
85 ops->next = NULL;
86 *tp = ops;
87 rc = 0;
88 out:
89 write_unlock(&cls_mod_lock);
90 return rc;
93 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
95 struct tcf_proto_ops *t, **tp;
96 int rc = -ENOENT;
98 write_lock(&cls_mod_lock);
99 for (tp = &tcf_proto_base; (t=*tp) != NULL; tp = &t->next)
100 if (t == ops)
101 break;
103 if (!t)
104 goto out;
105 *tp = t->next;
106 rc = 0;
107 out:
108 write_unlock(&cls_mod_lock);
109 return rc;
112 static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
113 struct tcf_proto *tp, unsigned long fh, int event);
116 /* Select new prio value from the range, managed by kernel. */
118 static __inline__ u32 tcf_auto_prio(struct tcf_proto *tp)
120 u32 first = TC_H_MAKE(0xC0000000U,0U);
122 if (tp)
123 first = tp->prio-1;
125 return first;
128 /* Add/change/delete/get a filter node */
130 static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
132 struct rtattr **tca;
133 struct tcmsg *t;
134 u32 protocol;
135 u32 prio;
136 u32 nprio;
137 u32 parent;
138 struct net_device *dev;
139 struct Qdisc *q;
140 struct tcf_proto **back, **chain;
141 struct tcf_proto *tp;
142 struct tcf_proto_ops *tp_ops;
143 struct Qdisc_class_ops *cops;
144 unsigned long cl;
145 unsigned long fh;
146 int err;
148 replay:
149 tca = arg;
150 t = NLMSG_DATA(n);
151 protocol = TC_H_MIN(t->tcm_info);
152 prio = TC_H_MAJ(t->tcm_info);
153 nprio = prio;
154 parent = t->tcm_parent;
155 cl = 0;
157 if (prio == 0) {
158 /* If no priority is given, user wants we allocated it. */
159 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
160 return -ENOENT;
161 prio = TC_H_MAKE(0x80000000U,0U);
164 /* Find head of filter chain. */
166 /* Find link */
167 if ((dev = __dev_get_by_index(t->tcm_ifindex)) == NULL)
168 return -ENODEV;
170 /* Find qdisc */
171 if (!parent) {
172 q = dev->qdisc_sleeping;
173 parent = q->handle;
174 } else if ((q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent))) == NULL)
175 return -EINVAL;
177 /* Is it classful? */
178 if ((cops = q->ops->cl_ops) == NULL)
179 return -EINVAL;
181 /* Do we search for filter, attached to class? */
182 if (TC_H_MIN(parent)) {
183 cl = cops->get(q, parent);
184 if (cl == 0)
185 return -ENOENT;
188 /* And the last stroke */
189 chain = cops->tcf_chain(q, cl);
190 err = -EINVAL;
191 if (chain == NULL)
192 goto errout;
194 /* Check the chain for existence of proto-tcf with this priority */
195 for (back = chain; (tp=*back) != NULL; back = &tp->next) {
196 if (tp->prio >= prio) {
197 if (tp->prio == prio) {
198 if (!nprio || (tp->protocol != protocol && protocol))
199 goto errout;
200 } else
201 tp = NULL;
202 break;
206 if (tp == NULL) {
207 /* Proto-tcf does not exist, create new one */
209 if (tca[TCA_KIND-1] == NULL || !protocol)
210 goto errout;
212 err = -ENOENT;
213 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
214 goto errout;
217 /* Create new proto tcf */
219 err = -ENOBUFS;
220 if ((tp = kzalloc(sizeof(*tp), GFP_KERNEL)) == NULL)
221 goto errout;
222 err = -EINVAL;
223 tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND-1]);
224 if (tp_ops == NULL) {
225 #ifdef CONFIG_KMOD
226 struct rtattr *kind = tca[TCA_KIND-1];
227 char name[IFNAMSIZ];
229 if (kind != NULL &&
230 rtattr_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
231 rtnl_unlock();
232 request_module("cls_%s", name);
233 rtnl_lock();
234 tp_ops = tcf_proto_lookup_ops(kind);
235 /* We dropped the RTNL semaphore in order to
236 * perform the module load. So, even if we
237 * succeeded in loading the module we have to
238 * replay the request. We indicate this using
239 * -EAGAIN.
241 if (tp_ops != NULL) {
242 module_put(tp_ops->owner);
243 err = -EAGAIN;
246 #endif
247 kfree(tp);
248 goto errout;
250 tp->ops = tp_ops;
251 tp->protocol = protocol;
252 tp->prio = nprio ? : tcf_auto_prio(*back);
253 tp->q = q;
254 tp->classify = tp_ops->classify;
255 tp->classid = parent;
256 if ((err = tp_ops->init(tp)) != 0) {
257 module_put(tp_ops->owner);
258 kfree(tp);
259 goto errout;
262 qdisc_lock_tree(dev);
263 tp->next = *back;
264 *back = tp;
265 qdisc_unlock_tree(dev);
267 } else if (tca[TCA_KIND-1] && rtattr_strcmp(tca[TCA_KIND-1], tp->ops->kind))
268 goto errout;
270 fh = tp->ops->get(tp, t->tcm_handle);
272 if (fh == 0) {
273 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
274 qdisc_lock_tree(dev);
275 *back = tp->next;
276 qdisc_unlock_tree(dev);
278 tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER);
279 tcf_destroy(tp);
280 err = 0;
281 goto errout;
284 err = -ENOENT;
285 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
286 goto errout;
287 } else {
288 switch (n->nlmsg_type) {
289 case RTM_NEWTFILTER:
290 err = -EEXIST;
291 if (n->nlmsg_flags&NLM_F_EXCL)
292 goto errout;
293 break;
294 case RTM_DELTFILTER:
295 err = tp->ops->delete(tp, fh);
296 if (err == 0)
297 tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER);
298 goto errout;
299 case RTM_GETTFILTER:
300 err = tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
301 goto errout;
302 default:
303 err = -EINVAL;
304 goto errout;
308 err = tp->ops->change(tp, cl, t->tcm_handle, tca, &fh);
309 if (err == 0)
310 tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
312 errout:
313 if (cl)
314 cops->put(q, cl);
315 if (err == -EAGAIN)
316 /* Replay the request. */
317 goto replay;
318 return err;
321 static int
322 tcf_fill_node(struct sk_buff *skb, struct tcf_proto *tp, unsigned long fh,
323 u32 pid, u32 seq, u16 flags, int event)
325 struct tcmsg *tcm;
326 struct nlmsghdr *nlh;
327 unsigned char *b = skb_tail_pointer(skb);
329 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
330 tcm = NLMSG_DATA(nlh);
331 tcm->tcm_family = AF_UNSPEC;
332 tcm->tcm__pad1 = 0;
333 tcm->tcm__pad1 = 0;
334 tcm->tcm_ifindex = tp->q->dev->ifindex;
335 tcm->tcm_parent = tp->classid;
336 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
337 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, tp->ops->kind);
338 tcm->tcm_handle = fh;
339 if (RTM_DELTFILTER != event) {
340 tcm->tcm_handle = 0;
341 if (tp->ops->dump && tp->ops->dump(tp, fh, skb, tcm) < 0)
342 goto rtattr_failure;
344 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
345 return skb->len;
347 nlmsg_failure:
348 rtattr_failure:
349 nlmsg_trim(skb, b);
350 return -1;
353 static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
354 struct tcf_proto *tp, unsigned long fh, int event)
356 struct sk_buff *skb;
357 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
359 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
360 if (!skb)
361 return -ENOBUFS;
363 if (tcf_fill_node(skb, tp, fh, pid, n->nlmsg_seq, 0, event) <= 0) {
364 kfree_skb(skb);
365 return -EINVAL;
368 return rtnetlink_send(skb, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
371 struct tcf_dump_args
373 struct tcf_walker w;
374 struct sk_buff *skb;
375 struct netlink_callback *cb;
378 static int tcf_node_dump(struct tcf_proto *tp, unsigned long n, struct tcf_walker *arg)
380 struct tcf_dump_args *a = (void*)arg;
382 return tcf_fill_node(a->skb, tp, n, NETLINK_CB(a->cb->skb).pid,
383 a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER);
386 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
388 int t;
389 int s_t;
390 struct net_device *dev;
391 struct Qdisc *q;
392 struct tcf_proto *tp, **chain;
393 struct tcmsg *tcm = (struct tcmsg*)NLMSG_DATA(cb->nlh);
394 unsigned long cl = 0;
395 struct Qdisc_class_ops *cops;
396 struct tcf_dump_args arg;
398 if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
399 return skb->len;
400 if ((dev = dev_get_by_index(tcm->tcm_ifindex)) == NULL)
401 return skb->len;
403 if (!tcm->tcm_parent)
404 q = dev->qdisc_sleeping;
405 else
406 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
407 if (!q)
408 goto out;
409 if ((cops = q->ops->cl_ops) == NULL)
410 goto errout;
411 if (TC_H_MIN(tcm->tcm_parent)) {
412 cl = cops->get(q, tcm->tcm_parent);
413 if (cl == 0)
414 goto errout;
416 chain = cops->tcf_chain(q, cl);
417 if (chain == NULL)
418 goto errout;
420 s_t = cb->args[0];
422 for (tp=*chain, t=0; tp; tp = tp->next, t++) {
423 if (t < s_t) continue;
424 if (TC_H_MAJ(tcm->tcm_info) &&
425 TC_H_MAJ(tcm->tcm_info) != tp->prio)
426 continue;
427 if (TC_H_MIN(tcm->tcm_info) &&
428 TC_H_MIN(tcm->tcm_info) != tp->protocol)
429 continue;
430 if (t > s_t)
431 memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
432 if (cb->args[1] == 0) {
433 if (tcf_fill_node(skb, tp, 0, NETLINK_CB(cb->skb).pid,
434 cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER) <= 0) {
435 break;
437 cb->args[1] = 1;
439 if (tp->ops->walk == NULL)
440 continue;
441 arg.w.fn = tcf_node_dump;
442 arg.skb = skb;
443 arg.cb = cb;
444 arg.w.stop = 0;
445 arg.w.skip = cb->args[1]-1;
446 arg.w.count = 0;
447 tp->ops->walk(tp, &arg.w);
448 cb->args[1] = arg.w.count+1;
449 if (arg.w.stop)
450 break;
453 cb->args[0] = t;
455 errout:
456 if (cl)
457 cops->put(q, cl);
458 out:
459 dev_put(dev);
460 return skb->len;
463 void
464 tcf_exts_destroy(struct tcf_proto *tp, struct tcf_exts *exts)
466 #ifdef CONFIG_NET_CLS_ACT
467 if (exts->action) {
468 tcf_action_destroy(exts->action, TCA_ACT_UNBIND);
469 exts->action = NULL;
471 #elif defined CONFIG_NET_CLS_POLICE
472 if (exts->police) {
473 tcf_police_release(exts->police, TCA_ACT_UNBIND);
474 exts->police = NULL;
476 #endif
481 tcf_exts_validate(struct tcf_proto *tp, struct rtattr **tb,
482 struct rtattr *rate_tlv, struct tcf_exts *exts,
483 struct tcf_ext_map *map)
485 memset(exts, 0, sizeof(*exts));
487 #ifdef CONFIG_NET_CLS_ACT
489 int err;
490 struct tc_action *act;
492 if (map->police && tb[map->police-1]) {
493 act = tcf_action_init_1(tb[map->police-1], rate_tlv, "police",
494 TCA_ACT_NOREPLACE, TCA_ACT_BIND, &err);
495 if (act == NULL)
496 return err;
498 act->type = TCA_OLD_COMPAT;
499 exts->action = act;
500 } else if (map->action && tb[map->action-1]) {
501 act = tcf_action_init(tb[map->action-1], rate_tlv, NULL,
502 TCA_ACT_NOREPLACE, TCA_ACT_BIND, &err);
503 if (act == NULL)
504 return err;
506 exts->action = act;
509 #elif defined CONFIG_NET_CLS_POLICE
510 if (map->police && tb[map->police-1]) {
511 struct tcf_police *p;
513 p = tcf_police_locate(tb[map->police-1], rate_tlv);
514 if (p == NULL)
515 return -EINVAL;
517 exts->police = p;
518 } else if (map->action && tb[map->action-1])
519 return -EOPNOTSUPP;
520 #else
521 if ((map->action && tb[map->action-1]) ||
522 (map->police && tb[map->police-1]))
523 return -EOPNOTSUPP;
524 #endif
526 return 0;
529 void
530 tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
531 struct tcf_exts *src)
533 #ifdef CONFIG_NET_CLS_ACT
534 if (src->action) {
535 struct tc_action *act;
536 tcf_tree_lock(tp);
537 act = xchg(&dst->action, src->action);
538 tcf_tree_unlock(tp);
539 if (act)
540 tcf_action_destroy(act, TCA_ACT_UNBIND);
542 #elif defined CONFIG_NET_CLS_POLICE
543 if (src->police) {
544 struct tcf_police *p;
545 tcf_tree_lock(tp);
546 p = xchg(&dst->police, src->police);
547 tcf_tree_unlock(tp);
548 if (p)
549 tcf_police_release(p, TCA_ACT_UNBIND);
551 #endif
555 tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts,
556 struct tcf_ext_map *map)
558 #ifdef CONFIG_NET_CLS_ACT
559 if (map->action && exts->action) {
561 * again for backward compatible mode - we want
562 * to work with both old and new modes of entering
563 * tc data even if iproute2 was newer - jhs
565 struct rtattr *p_rta = (struct rtattr *)skb_tail_pointer(skb);
567 if (exts->action->type != TCA_OLD_COMPAT) {
568 RTA_PUT(skb, map->action, 0, NULL);
569 if (tcf_action_dump(skb, exts->action, 0, 0) < 0)
570 goto rtattr_failure;
571 p_rta->rta_len = skb_tail_pointer(skb) - (u8 *)p_rta;
572 } else if (map->police) {
573 RTA_PUT(skb, map->police, 0, NULL);
574 if (tcf_action_dump_old(skb, exts->action, 0, 0) < 0)
575 goto rtattr_failure;
576 p_rta->rta_len = skb_tail_pointer(skb) - (u8 *)p_rta;
579 #elif defined CONFIG_NET_CLS_POLICE
580 if (map->police && exts->police) {
581 struct rtattr *p_rta = (struct rtattr *)skb_tail_pointer(skb);
583 RTA_PUT(skb, map->police, 0, NULL);
585 if (tcf_police_dump(skb, exts->police) < 0)
586 goto rtattr_failure;
588 p_rta->rta_len = skb_tail_pointer(skb) - (u8 *)p_rta;
590 #endif
591 return 0;
592 rtattr_failure: __attribute__ ((unused))
593 return -1;
597 tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts,
598 struct tcf_ext_map *map)
600 #ifdef CONFIG_NET_CLS_ACT
601 if (exts->action)
602 if (tcf_action_copy_stats(skb, exts->action, 1) < 0)
603 goto rtattr_failure;
604 #elif defined CONFIG_NET_CLS_POLICE
605 if (exts->police)
606 if (tcf_police_dump_stats(skb, exts->police) < 0)
607 goto rtattr_failure;
608 #endif
609 return 0;
610 rtattr_failure: __attribute__ ((unused))
611 return -1;
614 static int __init tc_filter_init(void)
616 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL);
617 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL);
618 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
619 tc_dump_tfilter);
621 return 0;
624 subsys_initcall(tc_filter_init);
626 EXPORT_SYMBOL(register_tcf_proto_ops);
627 EXPORT_SYMBOL(unregister_tcf_proto_ops);
628 EXPORT_SYMBOL(tcf_exts_validate);
629 EXPORT_SYMBOL(tcf_exts_destroy);
630 EXPORT_SYMBOL(tcf_exts_change);
631 EXPORT_SYMBOL(tcf_exts_dump);
632 EXPORT_SYMBOL(tcf_exts_dump_stats);