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