blob: 37a1840216474d77c2ad534903059bbeb1a840ea [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/kernel.h>
23#include <linux/sched.h>
24#include <linux/string.h>
25#include <linux/mm.h>
26#include <linux/socket.h>
27#include <linux/sockios.h>
28#include <linux/in.h>
29#include <linux/errno.h>
30#include <linux/interrupt.h>
31#include <linux/netdevice.h>
32#include <linux/skbuff.h>
33#include <linux/rtnetlink.h>
34#include <linux/init.h>
35#include <linux/kmod.h>
36#include <net/sock.h>
37#include <net/pkt_sched.h>
38#include <net/pkt_cls.h>
39
40#if 0 /* control */
41#define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
42#else
43#define DPRINTK(format,args...)
44#endif
45
46/* The list of all installed classifier types */
47
48static struct tcf_proto_ops *tcf_proto_base;
49
50/* Protects list of registered TC modules. It is pure SMP lock. */
51static DEFINE_RWLOCK(cls_mod_lock);
52
53/* Find classifier type by string name */
54
55static struct tcf_proto_ops * tcf_proto_lookup_ops(struct rtattr *kind)
56{
57 struct tcf_proto_ops *t = NULL;
58
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;
66 }
67 }
68 read_unlock(&cls_mod_lock);
69 }
70 return t;
71}
72
73/* Register(unregister) new classifier type */
74
75int register_tcf_proto_ops(struct tcf_proto_ops *ops)
76{
77 struct tcf_proto_ops *t, **tp;
78 int rc = -EEXIST;
79
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;
84
85 ops->next = NULL;
86 *tp = ops;
87 rc = 0;
88out:
89 write_unlock(&cls_mod_lock);
90 return rc;
91}
92
93int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
94{
95 struct tcf_proto_ops *t, **tp;
96 int rc = -ENOENT;
97
98 write_lock(&cls_mod_lock);
99 for (tp = &tcf_proto_base; (t=*tp) != NULL; tp = &t->next)
100 if (t == ops)
101 break;
102
103 if (!t)
104 goto out;
105 *tp = t->next;
106 rc = 0;
107out:
108 write_unlock(&cls_mod_lock);
109 return rc;
110}
111
112static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
113 struct tcf_proto *tp, unsigned long fh, int event);
114
115
116/* Select new prio value from the range, managed by kernel. */
117
118static __inline__ u32 tcf_auto_prio(struct tcf_proto *tp)
119{
120 u32 first = TC_H_MAKE(0xC0000000U,0U);
121
122 if (tp)
123 first = tp->prio-1;
124
125 return first;
126}
127
128/* Add/change/delete/get a filter node */
129
130static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
131{
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;
147
148replay:
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;
156
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);
162 }
163
164 /* Find head of filter chain. */
165
166 /* Find link */
167 if ((dev = __dev_get_by_index(t->tcm_ifindex)) == NULL)
168 return -ENODEV;
169
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;
176
177 /* Is it classful? */
178 if ((cops = q->ops->cl_ops) == NULL)
179 return -EINVAL;
180
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;
186 }
187
188 /* And the last stroke */
189 chain = cops->tcf_chain(q, cl);
190 err = -EINVAL;
191 if (chain == NULL)
192 goto errout;
193
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;
203 }
204 }
205
206 if (tp == NULL) {
207 /* Proto-tcf does not exist, create new one */
208
209 if (tca[TCA_KIND-1] == NULL || !protocol)
210 goto errout;
211
212 err = -ENOENT;
213 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
214 goto errout;
215
216
217 /* Create new proto tcf */
218
219 err = -ENOBUFS;
220 if ((tp = kmalloc(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];
228
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.
240 */
241 if (tp_ops != NULL) {
242 module_put(tp_ops->owner);
243 err = -EAGAIN;
244 }
245 }
246#endif
247 kfree(tp);
248 goto errout;
249 }
250 memset(tp, 0, sizeof(*tp));
251 tp->ops = tp_ops;
252 tp->protocol = protocol;
253 tp->prio = nprio ? : tcf_auto_prio(*back);
254 tp->q = q;
255 tp->classify = tp_ops->classify;
256 tp->classid = parent;
257 if ((err = tp_ops->init(tp)) != 0) {
258 module_put(tp_ops->owner);
259 kfree(tp);
260 goto errout;
261 }
262
263 qdisc_lock_tree(dev);
264 tp->next = *back;
265 *back = tp;
266 qdisc_unlock_tree(dev);
267
268 } else if (tca[TCA_KIND-1] && rtattr_strcmp(tca[TCA_KIND-1], tp->ops->kind))
269 goto errout;
270
271 fh = tp->ops->get(tp, t->tcm_handle);
272
273 if (fh == 0) {
274 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
275 qdisc_lock_tree(dev);
276 *back = tp->next;
277 qdisc_unlock_tree(dev);
278
279 tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER);
280 tcf_destroy(tp);
281 err = 0;
282 goto errout;
283 }
284
285 err = -ENOENT;
286 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
287 goto errout;
288 } else {
289 switch (n->nlmsg_type) {
290 case RTM_NEWTFILTER:
291 err = -EEXIST;
292 if (n->nlmsg_flags&NLM_F_EXCL)
293 goto errout;
294 break;
295 case RTM_DELTFILTER:
296 err = tp->ops->delete(tp, fh);
297 if (err == 0)
298 tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER);
299 goto errout;
300 case RTM_GETTFILTER:
301 err = tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
302 goto errout;
303 default:
304 err = -EINVAL;
305 goto errout;
306 }
307 }
308
309 err = tp->ops->change(tp, cl, t->tcm_handle, tca, &fh);
310 if (err == 0)
311 tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
312
313errout:
314 if (cl)
315 cops->put(q, cl);
316 if (err == -EAGAIN)
317 /* Replay the request. */
318 goto replay;
319 return err;
320}
321
322static int
323tcf_fill_node(struct sk_buff *skb, struct tcf_proto *tp, unsigned long fh,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700324 u32 pid, u32 seq, u16 flags, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 struct tcmsg *tcm;
327 struct nlmsghdr *nlh;
328 unsigned char *b = skb->tail;
329
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700330 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 tcm = NLMSG_DATA(nlh);
332 tcm->tcm_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700333 tcm->tcm__pad1 = 0;
334 tcm->tcm__pad1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 tcm->tcm_ifindex = tp->q->dev->ifindex;
336 tcm->tcm_parent = tp->classid;
337 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
338 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, tp->ops->kind);
339 tcm->tcm_handle = fh;
340 if (RTM_DELTFILTER != event) {
341 tcm->tcm_handle = 0;
342 if (tp->ops->dump && tp->ops->dump(tp, fh, skb, tcm) < 0)
343 goto rtattr_failure;
344 }
345 nlh->nlmsg_len = skb->tail - b;
346 return skb->len;
347
348nlmsg_failure:
349rtattr_failure:
350 skb_trim(skb, b - skb->data);
351 return -1;
352}
353
354static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
355 struct tcf_proto *tp, unsigned long fh, int event)
356{
357 struct sk_buff *skb;
358 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
359
360 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
361 if (!skb)
362 return -ENOBUFS;
363
364 if (tcf_fill_node(skb, tp, fh, pid, n->nlmsg_seq, 0, event) <= 0) {
365 kfree_skb(skb);
366 return -EINVAL;
367 }
368
Patrick McHardyac6d4392005-08-14 19:29:52 -0700369 return rtnetlink_send(skb, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372struct tcf_dump_args
373{
374 struct tcf_walker w;
375 struct sk_buff *skb;
376 struct netlink_callback *cb;
377};
378
379static int tcf_node_dump(struct tcf_proto *tp, unsigned long n, struct tcf_walker *arg)
380{
381 struct tcf_dump_args *a = (void*)arg;
382
383 return tcf_fill_node(a->skb, tp, n, NETLINK_CB(a->cb->skb).pid,
384 a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER);
385}
386
387static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
388{
389 int t;
390 int s_t;
391 struct net_device *dev;
392 struct Qdisc *q;
393 struct tcf_proto *tp, **chain;
394 struct tcmsg *tcm = (struct tcmsg*)NLMSG_DATA(cb->nlh);
395 unsigned long cl = 0;
396 struct Qdisc_class_ops *cops;
397 struct tcf_dump_args arg;
398
399 if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
400 return skb->len;
401 if ((dev = dev_get_by_index(tcm->tcm_ifindex)) == NULL)
402 return skb->len;
403
Patrick McHardy85670cc2006-09-27 16:45:45 -0700404 read_lock(&qdisc_tree_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (!tcm->tcm_parent)
406 q = dev->qdisc_sleeping;
407 else
408 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
409 if (!q)
410 goto out;
411 if ((cops = q->ops->cl_ops) == NULL)
412 goto errout;
413 if (TC_H_MIN(tcm->tcm_parent)) {
414 cl = cops->get(q, tcm->tcm_parent);
415 if (cl == 0)
416 goto errout;
417 }
418 chain = cops->tcf_chain(q, cl);
419 if (chain == NULL)
420 goto errout;
421
422 s_t = cb->args[0];
423
424 for (tp=*chain, t=0; tp; tp = tp->next, t++) {
425 if (t < s_t) continue;
426 if (TC_H_MAJ(tcm->tcm_info) &&
427 TC_H_MAJ(tcm->tcm_info) != tp->prio)
428 continue;
429 if (TC_H_MIN(tcm->tcm_info) &&
430 TC_H_MIN(tcm->tcm_info) != tp->protocol)
431 continue;
432 if (t > s_t)
433 memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
434 if (cb->args[1] == 0) {
435 if (tcf_fill_node(skb, tp, 0, NETLINK_CB(cb->skb).pid,
436 cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER) <= 0) {
437 break;
438 }
439 cb->args[1] = 1;
440 }
441 if (tp->ops->walk == NULL)
442 continue;
443 arg.w.fn = tcf_node_dump;
444 arg.skb = skb;
445 arg.cb = cb;
446 arg.w.stop = 0;
447 arg.w.skip = cb->args[1]-1;
448 arg.w.count = 0;
449 tp->ops->walk(tp, &arg.w);
450 cb->args[1] = arg.w.count+1;
451 if (arg.w.stop)
452 break;
453 }
454
455 cb->args[0] = t;
456
457errout:
458 if (cl)
459 cops->put(q, cl);
460out:
Patrick McHardy85670cc2006-09-27 16:45:45 -0700461 read_unlock(&qdisc_tree_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 dev_put(dev);
463 return skb->len;
464}
465
466void
467tcf_exts_destroy(struct tcf_proto *tp, struct tcf_exts *exts)
468{
469#ifdef CONFIG_NET_CLS_ACT
470 if (exts->action) {
471 tcf_action_destroy(exts->action, TCA_ACT_UNBIND);
472 exts->action = NULL;
473 }
474#elif defined CONFIG_NET_CLS_POLICE
475 if (exts->police) {
476 tcf_police_release(exts->police, TCA_ACT_UNBIND);
477 exts->police = NULL;
478 }
479#endif
480}
481
482
483int
484tcf_exts_validate(struct tcf_proto *tp, struct rtattr **tb,
485 struct rtattr *rate_tlv, struct tcf_exts *exts,
486 struct tcf_ext_map *map)
487{
488 memset(exts, 0, sizeof(*exts));
489
490#ifdef CONFIG_NET_CLS_ACT
491 {
492 int err;
493 struct tc_action *act;
494
495 if (map->police && tb[map->police-1]) {
496 act = tcf_action_init_1(tb[map->police-1], rate_tlv, "police",
497 TCA_ACT_NOREPLACE, TCA_ACT_BIND, &err);
498 if (act == NULL)
499 return err;
500
501 act->type = TCA_OLD_COMPAT;
502 exts->action = act;
503 } else if (map->action && tb[map->action-1]) {
504 act = tcf_action_init(tb[map->action-1], rate_tlv, NULL,
505 TCA_ACT_NOREPLACE, TCA_ACT_BIND, &err);
506 if (act == NULL)
507 return err;
508
509 exts->action = act;
510 }
511 }
512#elif defined CONFIG_NET_CLS_POLICE
513 if (map->police && tb[map->police-1]) {
514 struct tcf_police *p;
515
516 p = tcf_police_locate(tb[map->police-1], rate_tlv);
517 if (p == NULL)
518 return -EINVAL;
519
520 exts->police = p;
521 } else if (map->action && tb[map->action-1])
522 return -EOPNOTSUPP;
523#else
524 if ((map->action && tb[map->action-1]) ||
525 (map->police && tb[map->police-1]))
526 return -EOPNOTSUPP;
527#endif
528
529 return 0;
530}
531
532void
533tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
534 struct tcf_exts *src)
535{
536#ifdef CONFIG_NET_CLS_ACT
537 if (src->action) {
538 struct tc_action *act;
539 tcf_tree_lock(tp);
540 act = xchg(&dst->action, src->action);
541 tcf_tree_unlock(tp);
542 if (act)
543 tcf_action_destroy(act, TCA_ACT_UNBIND);
544 }
545#elif defined CONFIG_NET_CLS_POLICE
546 if (src->police) {
547 struct tcf_police *p;
548 tcf_tree_lock(tp);
549 p = xchg(&dst->police, src->police);
550 tcf_tree_unlock(tp);
551 if (p)
552 tcf_police_release(p, TCA_ACT_UNBIND);
553 }
554#endif
555}
556
557int
558tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts,
559 struct tcf_ext_map *map)
560{
561#ifdef CONFIG_NET_CLS_ACT
562 if (map->action && exts->action) {
563 /*
564 * again for backward compatible mode - we want
565 * to work with both old and new modes of entering
566 * tc data even if iproute2 was newer - jhs
567 */
568 struct rtattr * p_rta = (struct rtattr*) skb->tail;
569
570 if (exts->action->type != TCA_OLD_COMPAT) {
571 RTA_PUT(skb, map->action, 0, NULL);
572 if (tcf_action_dump(skb, exts->action, 0, 0) < 0)
573 goto rtattr_failure;
574 p_rta->rta_len = skb->tail - (u8*)p_rta;
575 } else if (map->police) {
576 RTA_PUT(skb, map->police, 0, NULL);
577 if (tcf_action_dump_old(skb, exts->action, 0, 0) < 0)
578 goto rtattr_failure;
579 p_rta->rta_len = skb->tail - (u8*)p_rta;
580 }
581 }
582#elif defined CONFIG_NET_CLS_POLICE
583 if (map->police && exts->police) {
584 struct rtattr * p_rta = (struct rtattr*) skb->tail;
585
586 RTA_PUT(skb, map->police, 0, NULL);
587
588 if (tcf_police_dump(skb, exts->police) < 0)
589 goto rtattr_failure;
590
591 p_rta->rta_len = skb->tail - (u8*)p_rta;
592 }
593#endif
594 return 0;
595rtattr_failure: __attribute__ ((unused))
596 return -1;
597}
598
599int
600tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts,
601 struct tcf_ext_map *map)
602{
603#ifdef CONFIG_NET_CLS_ACT
604 if (exts->action)
605 if (tcf_action_copy_stats(skb, exts->action, 1) < 0)
606 goto rtattr_failure;
607#elif defined CONFIG_NET_CLS_POLICE
608 if (exts->police)
609 if (tcf_police_dump_stats(skb, exts->police) < 0)
610 goto rtattr_failure;
611#endif
612 return 0;
613rtattr_failure: __attribute__ ((unused))
614 return -1;
615}
616
617static int __init tc_filter_init(void)
618{
619 struct rtnetlink_link *link_p = rtnetlink_links[PF_UNSPEC];
620
621 /* Setup rtnetlink links. It is made here to avoid
622 exporting large number of public symbols.
623 */
624
625 if (link_p) {
626 link_p[RTM_NEWTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
627 link_p[RTM_DELTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
628 link_p[RTM_GETTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
629 link_p[RTM_GETTFILTER-RTM_BASE].dumpit = tc_dump_tfilter;
630 }
631 return 0;
632}
633
634subsys_initcall(tc_filter_init);
635
636EXPORT_SYMBOL(register_tcf_proto_ops);
637EXPORT_SYMBOL(unregister_tcf_proto_ops);
638EXPORT_SYMBOL(tcf_exts_validate);
639EXPORT_SYMBOL(tcf_exts_destroy);
640EXPORT_SYMBOL(tcf_exts_change);
641EXPORT_SYMBOL(tcf_exts_dump);
642EXPORT_SYMBOL(tcf_exts_dump_stats);