ocfs2: Fix extent lookup to return true size of holes
[linux-2.6/s3c2410-cpufreq.git] / net / sched / act_api.c
blobcb21617a5670516a9719623a5cceecd62bdca961
1 /*
2 * net/sched/act_api.c Packet action 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 * Author: Jamal Hadi Salim
14 #include <asm/uaccess.h>
15 #include <asm/system.h>
16 #include <linux/bitops.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/mm.h>
21 #include <linux/socket.h>
22 #include <linux/sockios.h>
23 #include <linux/in.h>
24 #include <linux/errno.h>
25 #include <linux/interrupt.h>
26 #include <linux/netdevice.h>
27 #include <linux/skbuff.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/init.h>
30 #include <linux/kmod.h>
31 #include <net/sock.h>
32 #include <net/sch_generic.h>
33 #include <net/act_api.h>
35 void tcf_hash_destroy(struct tcf_common *p, struct tcf_hashinfo *hinfo)
37 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
38 struct tcf_common **p1p;
40 for (p1p = &hinfo->htab[h]; *p1p; p1p = &(*p1p)->tcfc_next) {
41 if (*p1p == p) {
42 write_lock_bh(hinfo->lock);
43 *p1p = p->tcfc_next;
44 write_unlock_bh(hinfo->lock);
45 #ifdef CONFIG_NET_ESTIMATOR
46 gen_kill_estimator(&p->tcfc_bstats,
47 &p->tcfc_rate_est);
48 #endif
49 kfree(p);
50 return;
53 BUG_TRAP(0);
55 EXPORT_SYMBOL(tcf_hash_destroy);
57 int tcf_hash_release(struct tcf_common *p, int bind,
58 struct tcf_hashinfo *hinfo)
60 int ret = 0;
62 if (p) {
63 if (bind)
64 p->tcfc_bindcnt--;
66 p->tcfc_refcnt--;
67 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
68 tcf_hash_destroy(p, hinfo);
69 ret = 1;
72 return ret;
74 EXPORT_SYMBOL(tcf_hash_release);
76 static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
77 struct tc_action *a, struct tcf_hashinfo *hinfo)
79 struct tcf_common *p;
80 int err = 0, index = -1,i = 0, s_i = 0, n_i = 0;
81 struct rtattr *r ;
83 read_lock(hinfo->lock);
85 s_i = cb->args[0];
87 for (i = 0; i < (hinfo->hmask + 1); i++) {
88 p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
90 for (; p; p = p->tcfc_next) {
91 index++;
92 if (index < s_i)
93 continue;
94 a->priv = p;
95 a->order = n_i;
96 r = (struct rtattr*) skb->tail;
97 RTA_PUT(skb, a->order, 0, NULL);
98 err = tcf_action_dump_1(skb, a, 0, 0);
99 if (err < 0) {
100 index--;
101 skb_trim(skb, (u8*)r - skb->data);
102 goto done;
104 r->rta_len = skb->tail - (u8*)r;
105 n_i++;
106 if (n_i >= TCA_ACT_MAX_PRIO)
107 goto done;
110 done:
111 read_unlock(hinfo->lock);
112 if (n_i)
113 cb->args[0] += n_i;
114 return n_i;
116 rtattr_failure:
117 skb_trim(skb, (u8*)r - skb->data);
118 goto done;
121 static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a,
122 struct tcf_hashinfo *hinfo)
124 struct tcf_common *p, *s_p;
125 struct rtattr *r ;
126 int i= 0, n_i = 0;
128 r = (struct rtattr*) skb->tail;
129 RTA_PUT(skb, a->order, 0, NULL);
130 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
131 for (i = 0; i < (hinfo->hmask + 1); i++) {
132 p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
134 while (p != NULL) {
135 s_p = p->tcfc_next;
136 if (ACT_P_DELETED == tcf_hash_release(p, 0, hinfo))
137 module_put(a->ops->owner);
138 n_i++;
139 p = s_p;
142 RTA_PUT(skb, TCA_FCNT, 4, &n_i);
143 r->rta_len = skb->tail - (u8*)r;
145 return n_i;
146 rtattr_failure:
147 skb_trim(skb, (u8*)r - skb->data);
148 return -EINVAL;
151 int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
152 int type, struct tc_action *a)
154 struct tcf_hashinfo *hinfo = a->ops->hinfo;
156 if (type == RTM_DELACTION) {
157 return tcf_del_walker(skb, a, hinfo);
158 } else if (type == RTM_GETACTION) {
159 return tcf_dump_walker(skb, cb, a, hinfo);
160 } else {
161 printk("tcf_generic_walker: unknown action %d\n", type);
162 return -EINVAL;
165 EXPORT_SYMBOL(tcf_generic_walker);
167 struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
169 struct tcf_common *p;
171 read_lock(hinfo->lock);
172 for (p = hinfo->htab[tcf_hash(index, hinfo->hmask)]; p;
173 p = p->tcfc_next) {
174 if (p->tcfc_index == index)
175 break;
177 read_unlock(hinfo->lock);
179 return p;
181 EXPORT_SYMBOL(tcf_hash_lookup);
183 u32 tcf_hash_new_index(u32 *idx_gen, struct tcf_hashinfo *hinfo)
185 u32 val = *idx_gen;
187 do {
188 if (++val == 0)
189 val = 1;
190 } while (tcf_hash_lookup(val, hinfo));
192 return (*idx_gen = val);
194 EXPORT_SYMBOL(tcf_hash_new_index);
196 int tcf_hash_search(struct tc_action *a, u32 index)
198 struct tcf_hashinfo *hinfo = a->ops->hinfo;
199 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
201 if (p) {
202 a->priv = p;
203 return 1;
205 return 0;
207 EXPORT_SYMBOL(tcf_hash_search);
209 struct tcf_common *tcf_hash_check(u32 index, struct tc_action *a, int bind,
210 struct tcf_hashinfo *hinfo)
212 struct tcf_common *p = NULL;
213 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
214 if (bind) {
215 p->tcfc_bindcnt++;
216 p->tcfc_refcnt++;
218 a->priv = p;
220 return p;
222 EXPORT_SYMBOL(tcf_hash_check);
224 struct tcf_common *tcf_hash_create(u32 index, struct rtattr *est, struct tc_action *a, int size, int bind, u32 *idx_gen, struct tcf_hashinfo *hinfo)
226 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
228 if (unlikely(!p))
229 return p;
230 p->tcfc_refcnt = 1;
231 if (bind)
232 p->tcfc_bindcnt = 1;
234 spin_lock_init(&p->tcfc_lock);
235 p->tcfc_stats_lock = &p->tcfc_lock;
236 p->tcfc_index = index ? index : tcf_hash_new_index(idx_gen, hinfo);
237 p->tcfc_tm.install = jiffies;
238 p->tcfc_tm.lastuse = jiffies;
239 #ifdef CONFIG_NET_ESTIMATOR
240 if (est)
241 gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
242 p->tcfc_stats_lock, est);
243 #endif
244 a->priv = (void *) p;
245 return p;
247 EXPORT_SYMBOL(tcf_hash_create);
249 void tcf_hash_insert(struct tcf_common *p, struct tcf_hashinfo *hinfo)
251 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
253 write_lock_bh(hinfo->lock);
254 p->tcfc_next = hinfo->htab[h];
255 hinfo->htab[h] = p;
256 write_unlock_bh(hinfo->lock);
258 EXPORT_SYMBOL(tcf_hash_insert);
260 static struct tc_action_ops *act_base = NULL;
261 static DEFINE_RWLOCK(act_mod_lock);
263 int tcf_register_action(struct tc_action_ops *act)
265 struct tc_action_ops *a, **ap;
267 write_lock(&act_mod_lock);
268 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next) {
269 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
270 write_unlock(&act_mod_lock);
271 return -EEXIST;
274 act->next = NULL;
275 *ap = act;
276 write_unlock(&act_mod_lock);
277 return 0;
280 int tcf_unregister_action(struct tc_action_ops *act)
282 struct tc_action_ops *a, **ap;
283 int err = -ENOENT;
285 write_lock(&act_mod_lock);
286 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next)
287 if (a == act)
288 break;
289 if (a) {
290 *ap = a->next;
291 a->next = NULL;
292 err = 0;
294 write_unlock(&act_mod_lock);
295 return err;
298 /* lookup by name */
299 static struct tc_action_ops *tc_lookup_action_n(char *kind)
301 struct tc_action_ops *a = NULL;
303 if (kind) {
304 read_lock(&act_mod_lock);
305 for (a = act_base; a; a = a->next) {
306 if (strcmp(kind, a->kind) == 0) {
307 if (!try_module_get(a->owner)) {
308 read_unlock(&act_mod_lock);
309 return NULL;
311 break;
314 read_unlock(&act_mod_lock);
316 return a;
319 /* lookup by rtattr */
320 static struct tc_action_ops *tc_lookup_action(struct rtattr *kind)
322 struct tc_action_ops *a = NULL;
324 if (kind) {
325 read_lock(&act_mod_lock);
326 for (a = act_base; a; a = a->next) {
327 if (rtattr_strcmp(kind, a->kind) == 0) {
328 if (!try_module_get(a->owner)) {
329 read_unlock(&act_mod_lock);
330 return NULL;
332 break;
335 read_unlock(&act_mod_lock);
337 return a;
340 #if 0
341 /* lookup by id */
342 static struct tc_action_ops *tc_lookup_action_id(u32 type)
344 struct tc_action_ops *a = NULL;
346 if (type) {
347 read_lock(&act_mod_lock);
348 for (a = act_base; a; a = a->next) {
349 if (a->type == type) {
350 if (!try_module_get(a->owner)) {
351 read_unlock(&act_mod_lock);
352 return NULL;
354 break;
357 read_unlock(&act_mod_lock);
359 return a;
361 #endif
363 int tcf_action_exec(struct sk_buff *skb, struct tc_action *act,
364 struct tcf_result *res)
366 struct tc_action *a;
367 int ret = -1;
369 if (skb->tc_verd & TC_NCLS) {
370 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
371 ret = TC_ACT_OK;
372 goto exec_done;
374 while ((a = act) != NULL) {
375 repeat:
376 if (a->ops && a->ops->act) {
377 ret = a->ops->act(skb, a, res);
378 if (TC_MUNGED & skb->tc_verd) {
379 /* copied already, allow trampling */
380 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
381 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
383 if (ret == TC_ACT_REPEAT)
384 goto repeat; /* we need a ttl - JHS */
385 if (ret != TC_ACT_PIPE)
386 goto exec_done;
388 act = a->next;
390 exec_done:
391 return ret;
394 void tcf_action_destroy(struct tc_action *act, int bind)
396 struct tc_action *a;
398 for (a = act; a; a = act) {
399 if (a->ops && a->ops->cleanup) {
400 if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
401 module_put(a->ops->owner);
402 act = act->next;
403 kfree(a);
404 } else { /*FIXME: Remove later - catch insertion bugs*/
405 printk("tcf_action_destroy: BUG? destroying NULL ops\n");
406 act = act->next;
407 kfree(a);
413 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
415 int err = -EINVAL;
417 if (a->ops == NULL || a->ops->dump == NULL)
418 return err;
419 return a->ops->dump(skb, a, bind, ref);
423 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
425 int err = -EINVAL;
426 unsigned char *b = skb->tail;
427 struct rtattr *r;
429 if (a->ops == NULL || a->ops->dump == NULL)
430 return err;
432 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
433 if (tcf_action_copy_stats(skb, a, 0))
434 goto rtattr_failure;
435 r = (struct rtattr*) skb->tail;
436 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
437 if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) {
438 r->rta_len = skb->tail - (u8*)r;
439 return err;
442 rtattr_failure:
443 skb_trim(skb, b - skb->data);
444 return -1;
448 tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref)
450 struct tc_action *a;
451 int err = -EINVAL;
452 unsigned char *b = skb->tail;
453 struct rtattr *r ;
455 while ((a = act) != NULL) {
456 r = (struct rtattr*) skb->tail;
457 act = a->next;
458 RTA_PUT(skb, a->order, 0, NULL);
459 err = tcf_action_dump_1(skb, a, bind, ref);
460 if (err < 0)
461 goto errout;
462 r->rta_len = skb->tail - (u8*)r;
465 return 0;
467 rtattr_failure:
468 err = -EINVAL;
469 errout:
470 skb_trim(skb, b - skb->data);
471 return err;
474 struct tc_action *tcf_action_init_1(struct rtattr *rta, struct rtattr *est,
475 char *name, int ovr, int bind, int *err)
477 struct tc_action *a;
478 struct tc_action_ops *a_o;
479 char act_name[IFNAMSIZ];
480 struct rtattr *tb[TCA_ACT_MAX+1];
481 struct rtattr *kind;
483 *err = -EINVAL;
485 if (name == NULL) {
486 if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
487 goto err_out;
488 kind = tb[TCA_ACT_KIND-1];
489 if (kind == NULL)
490 goto err_out;
491 if (rtattr_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
492 goto err_out;
493 } else {
494 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
495 goto err_out;
498 a_o = tc_lookup_action_n(act_name);
499 if (a_o == NULL) {
500 #ifdef CONFIG_KMOD
501 rtnl_unlock();
502 request_module("act_%s", act_name);
503 rtnl_lock();
505 a_o = tc_lookup_action_n(act_name);
507 /* We dropped the RTNL semaphore in order to
508 * perform the module load. So, even if we
509 * succeeded in loading the module we have to
510 * tell the caller to replay the request. We
511 * indicate this using -EAGAIN.
513 if (a_o != NULL) {
514 *err = -EAGAIN;
515 goto err_mod;
517 #endif
518 *err = -ENOENT;
519 goto err_out;
522 *err = -ENOMEM;
523 a = kzalloc(sizeof(*a), GFP_KERNEL);
524 if (a == NULL)
525 goto err_mod;
527 /* backward compatibility for policer */
528 if (name == NULL)
529 *err = a_o->init(tb[TCA_ACT_OPTIONS-1], est, a, ovr, bind);
530 else
531 *err = a_o->init(rta, est, a, ovr, bind);
532 if (*err < 0)
533 goto err_free;
535 /* module count goes up only when brand new policy is created
536 if it exists and is only bound to in a_o->init() then
537 ACT_P_CREATED is not returned (a zero is).
539 if (*err != ACT_P_CREATED)
540 module_put(a_o->owner);
541 a->ops = a_o;
543 *err = 0;
544 return a;
546 err_free:
547 kfree(a);
548 err_mod:
549 module_put(a_o->owner);
550 err_out:
551 return NULL;
554 struct tc_action *tcf_action_init(struct rtattr *rta, struct rtattr *est,
555 char *name, int ovr, int bind, int *err)
557 struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
558 struct tc_action *head = NULL, *act, *act_prev = NULL;
559 int i;
561 if (rtattr_parse_nested(tb, TCA_ACT_MAX_PRIO, rta) < 0) {
562 *err = -EINVAL;
563 return head;
566 for (i=0; i < TCA_ACT_MAX_PRIO && tb[i]; i++) {
567 act = tcf_action_init_1(tb[i], est, name, ovr, bind, err);
568 if (act == NULL)
569 goto err;
570 act->order = i+1;
572 if (head == NULL)
573 head = act;
574 else
575 act_prev->next = act;
576 act_prev = act;
578 return head;
580 err:
581 if (head != NULL)
582 tcf_action_destroy(head, bind);
583 return NULL;
586 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
587 int compat_mode)
589 int err = 0;
590 struct gnet_dump d;
591 struct tcf_act_hdr *h = a->priv;
593 if (h == NULL)
594 goto errout;
596 /* compat_mode being true specifies a call that is supposed
597 * to add additional backward compatiblity statistic TLVs.
599 if (compat_mode) {
600 if (a->type == TCA_OLD_COMPAT)
601 err = gnet_stats_start_copy_compat(skb, 0,
602 TCA_STATS, TCA_XSTATS, h->tcf_stats_lock, &d);
603 else
604 return 0;
605 } else
606 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
607 h->tcf_stats_lock, &d);
609 if (err < 0)
610 goto errout;
612 if (a->ops != NULL && a->ops->get_stats != NULL)
613 if (a->ops->get_stats(skb, a) < 0)
614 goto errout;
616 if (gnet_stats_copy_basic(&d, &h->tcf_bstats) < 0 ||
617 #ifdef CONFIG_NET_ESTIMATOR
618 gnet_stats_copy_rate_est(&d, &h->tcf_rate_est) < 0 ||
619 #endif
620 gnet_stats_copy_queue(&d, &h->tcf_qstats) < 0)
621 goto errout;
623 if (gnet_stats_finish_copy(&d) < 0)
624 goto errout;
626 return 0;
628 errout:
629 return -1;
632 static int
633 tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, u32 seq,
634 u16 flags, int event, int bind, int ref)
636 struct tcamsg *t;
637 struct nlmsghdr *nlh;
638 unsigned char *b = skb->tail;
639 struct rtattr *x;
641 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
643 t = NLMSG_DATA(nlh);
644 t->tca_family = AF_UNSPEC;
645 t->tca__pad1 = 0;
646 t->tca__pad2 = 0;
648 x = (struct rtattr*) skb->tail;
649 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
651 if (tcf_action_dump(skb, a, bind, ref) < 0)
652 goto rtattr_failure;
654 x->rta_len = skb->tail - (u8*)x;
656 nlh->nlmsg_len = skb->tail - b;
657 return skb->len;
659 rtattr_failure:
660 nlmsg_failure:
661 skb_trim(skb, b - skb->data);
662 return -1;
665 static int
666 act_get_notify(u32 pid, struct nlmsghdr *n, struct tc_action *a, int event)
668 struct sk_buff *skb;
670 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
671 if (!skb)
672 return -ENOBUFS;
673 if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
674 kfree_skb(skb);
675 return -EINVAL;
678 return rtnl_unicast(skb, pid);
681 static struct tc_action *
682 tcf_action_get_1(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int *err)
684 struct rtattr *tb[TCA_ACT_MAX+1];
685 struct tc_action *a;
686 int index;
688 *err = -EINVAL;
689 if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
690 return NULL;
692 if (tb[TCA_ACT_INDEX - 1] == NULL ||
693 RTA_PAYLOAD(tb[TCA_ACT_INDEX - 1]) < sizeof(index))
694 return NULL;
695 index = *(int *)RTA_DATA(tb[TCA_ACT_INDEX - 1]);
697 *err = -ENOMEM;
698 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
699 if (a == NULL)
700 return NULL;
702 *err = -EINVAL;
703 a->ops = tc_lookup_action(tb[TCA_ACT_KIND - 1]);
704 if (a->ops == NULL)
705 goto err_free;
706 if (a->ops->lookup == NULL)
707 goto err_mod;
708 *err = -ENOENT;
709 if (a->ops->lookup(a, index) == 0)
710 goto err_mod;
712 module_put(a->ops->owner);
713 *err = 0;
714 return a;
715 err_mod:
716 module_put(a->ops->owner);
717 err_free:
718 kfree(a);
719 return NULL;
722 static void cleanup_a(struct tc_action *act)
724 struct tc_action *a;
726 for (a = act; a; a = act) {
727 act = a->next;
728 kfree(a);
732 static struct tc_action *create_a(int i)
734 struct tc_action *act;
736 act = kzalloc(sizeof(*act), GFP_KERNEL);
737 if (act == NULL) {
738 printk("create_a: failed to alloc!\n");
739 return NULL;
741 act->order = i;
742 return act;
745 static int tca_action_flush(struct rtattr *rta, struct nlmsghdr *n, u32 pid)
747 struct sk_buff *skb;
748 unsigned char *b;
749 struct nlmsghdr *nlh;
750 struct tcamsg *t;
751 struct netlink_callback dcb;
752 struct rtattr *x;
753 struct rtattr *tb[TCA_ACT_MAX+1];
754 struct rtattr *kind;
755 struct tc_action *a = create_a(0);
756 int err = -EINVAL;
758 if (a == NULL) {
759 printk("tca_action_flush: couldnt create tc_action\n");
760 return err;
763 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
764 if (!skb) {
765 printk("tca_action_flush: failed skb alloc\n");
766 kfree(a);
767 return -ENOBUFS;
770 b = (unsigned char *)skb->tail;
772 if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
773 goto err_out;
775 kind = tb[TCA_ACT_KIND-1];
776 a->ops = tc_lookup_action(kind);
777 if (a->ops == NULL)
778 goto err_out;
780 nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t));
781 t = NLMSG_DATA(nlh);
782 t->tca_family = AF_UNSPEC;
783 t->tca__pad1 = 0;
784 t->tca__pad2 = 0;
786 x = (struct rtattr *) skb->tail;
787 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
789 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
790 if (err < 0)
791 goto rtattr_failure;
793 x->rta_len = skb->tail - (u8 *) x;
795 nlh->nlmsg_len = skb->tail - b;
796 nlh->nlmsg_flags |= NLM_F_ROOT;
797 module_put(a->ops->owner);
798 kfree(a);
799 err = rtnetlink_send(skb, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
800 if (err > 0)
801 return 0;
803 return err;
805 rtattr_failure:
806 nlmsg_failure:
807 module_put(a->ops->owner);
808 err_out:
809 kfree_skb(skb);
810 kfree(a);
811 return err;
814 static int
815 tca_action_gd(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int event)
817 int i, ret = 0;
818 struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
819 struct tc_action *head = NULL, *act, *act_prev = NULL;
821 if (rtattr_parse_nested(tb, TCA_ACT_MAX_PRIO, rta) < 0)
822 return -EINVAL;
824 if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) {
825 if (tb[0] != NULL && tb[1] == NULL)
826 return tca_action_flush(tb[0], n, pid);
829 for (i=0; i < TCA_ACT_MAX_PRIO && tb[i]; i++) {
830 act = tcf_action_get_1(tb[i], n, pid, &ret);
831 if (act == NULL)
832 goto err;
833 act->order = i+1;
835 if (head == NULL)
836 head = act;
837 else
838 act_prev->next = act;
839 act_prev = act;
842 if (event == RTM_GETACTION)
843 ret = act_get_notify(pid, n, head, event);
844 else { /* delete */
845 struct sk_buff *skb;
847 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
848 if (!skb) {
849 ret = -ENOBUFS;
850 goto err;
853 if (tca_get_fill(skb, head, pid, n->nlmsg_seq, 0, event,
854 0, 1) <= 0) {
855 kfree_skb(skb);
856 ret = -EINVAL;
857 goto err;
860 /* now do the delete */
861 tcf_action_destroy(head, 0);
862 ret = rtnetlink_send(skb, pid, RTNLGRP_TC,
863 n->nlmsg_flags&NLM_F_ECHO);
864 if (ret > 0)
865 return 0;
866 return ret;
868 err:
869 cleanup_a(head);
870 return ret;
873 static int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event,
874 u16 flags)
876 struct tcamsg *t;
877 struct nlmsghdr *nlh;
878 struct sk_buff *skb;
879 struct rtattr *x;
880 unsigned char *b;
881 int err = 0;
883 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
884 if (!skb)
885 return -ENOBUFS;
887 b = (unsigned char *)skb->tail;
889 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
890 t = NLMSG_DATA(nlh);
891 t->tca_family = AF_UNSPEC;
892 t->tca__pad1 = 0;
893 t->tca__pad2 = 0;
895 x = (struct rtattr*) skb->tail;
896 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
898 if (tcf_action_dump(skb, a, 0, 0) < 0)
899 goto rtattr_failure;
901 x->rta_len = skb->tail - (u8*)x;
903 nlh->nlmsg_len = skb->tail - b;
904 NETLINK_CB(skb).dst_group = RTNLGRP_TC;
906 err = rtnetlink_send(skb, pid, RTNLGRP_TC, flags&NLM_F_ECHO);
907 if (err > 0)
908 err = 0;
909 return err;
911 rtattr_failure:
912 nlmsg_failure:
913 kfree_skb(skb);
914 return -1;
918 static int
919 tcf_action_add(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int ovr)
921 int ret = 0;
922 struct tc_action *act;
923 struct tc_action *a;
924 u32 seq = n->nlmsg_seq;
926 act = tcf_action_init(rta, NULL, NULL, ovr, 0, &ret);
927 if (act == NULL)
928 goto done;
930 /* dump then free all the actions after update; inserted policy
931 * stays intact
932 * */
933 ret = tcf_add_notify(act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
934 for (a = act; a; a = act) {
935 act = a->next;
936 kfree(a);
938 done:
939 return ret;
942 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
944 struct rtattr **tca = arg;
945 u32 pid = skb ? NETLINK_CB(skb).pid : 0;
946 int ret = 0, ovr = 0;
948 if (tca[TCA_ACT_TAB-1] == NULL) {
949 printk("tc_ctl_action: received NO action attribs\n");
950 return -EINVAL;
953 /* n->nlmsg_flags&NLM_F_CREATE
954 * */
955 switch (n->nlmsg_type) {
956 case RTM_NEWACTION:
957 /* we are going to assume all other flags
958 * imply create only if it doesnt exist
959 * Note that CREATE | EXCL implies that
960 * but since we want avoid ambiguity (eg when flags
961 * is zero) then just set this
963 if (n->nlmsg_flags&NLM_F_REPLACE)
964 ovr = 1;
965 replay:
966 ret = tcf_action_add(tca[TCA_ACT_TAB-1], n, pid, ovr);
967 if (ret == -EAGAIN)
968 goto replay;
969 break;
970 case RTM_DELACTION:
971 ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid, RTM_DELACTION);
972 break;
973 case RTM_GETACTION:
974 ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid, RTM_GETACTION);
975 break;
976 default:
977 BUG();
980 return ret;
983 static struct rtattr *
984 find_dump_kind(struct nlmsghdr *n)
986 struct rtattr *tb1, *tb2[TCA_ACT_MAX+1];
987 struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
988 struct rtattr *rta[TCAA_MAX + 1];
989 struct rtattr *kind;
990 int min_len = NLMSG_LENGTH(sizeof(struct tcamsg));
991 int attrlen = n->nlmsg_len - NLMSG_ALIGN(min_len);
992 struct rtattr *attr = (void *) n + NLMSG_ALIGN(min_len);
994 if (rtattr_parse(rta, TCAA_MAX, attr, attrlen) < 0)
995 return NULL;
996 tb1 = rta[TCA_ACT_TAB - 1];
997 if (tb1 == NULL)
998 return NULL;
1000 if (rtattr_parse(tb, TCA_ACT_MAX_PRIO, RTA_DATA(tb1),
1001 NLMSG_ALIGN(RTA_PAYLOAD(tb1))) < 0)
1002 return NULL;
1003 if (tb[0] == NULL)
1004 return NULL;
1006 if (rtattr_parse(tb2, TCA_ACT_MAX, RTA_DATA(tb[0]),
1007 RTA_PAYLOAD(tb[0])) < 0)
1008 return NULL;
1009 kind = tb2[TCA_ACT_KIND-1];
1011 return kind;
1014 static int
1015 tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1017 struct nlmsghdr *nlh;
1018 unsigned char *b = skb->tail;
1019 struct rtattr *x;
1020 struct tc_action_ops *a_o;
1021 struct tc_action a;
1022 int ret = 0;
1023 struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
1024 struct rtattr *kind = find_dump_kind(cb->nlh);
1026 if (kind == NULL) {
1027 printk("tc_dump_action: action bad kind\n");
1028 return 0;
1031 a_o = tc_lookup_action(kind);
1032 if (a_o == NULL) {
1033 return 0;
1036 memset(&a, 0, sizeof(struct tc_action));
1037 a.ops = a_o;
1039 if (a_o->walk == NULL) {
1040 printk("tc_dump_action: %s !capable of dumping table\n", a_o->kind);
1041 goto rtattr_failure;
1044 nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
1045 cb->nlh->nlmsg_type, sizeof(*t));
1046 t = NLMSG_DATA(nlh);
1047 t->tca_family = AF_UNSPEC;
1048 t->tca__pad1 = 0;
1049 t->tca__pad2 = 0;
1051 x = (struct rtattr *) skb->tail;
1052 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
1054 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1055 if (ret < 0)
1056 goto rtattr_failure;
1058 if (ret > 0) {
1059 x->rta_len = skb->tail - (u8 *) x;
1060 ret = skb->len;
1061 } else
1062 skb_trim(skb, (u8*)x - skb->data);
1064 nlh->nlmsg_len = skb->tail - b;
1065 if (NETLINK_CB(cb->skb).pid && ret)
1066 nlh->nlmsg_flags |= NLM_F_MULTI;
1067 module_put(a_o->owner);
1068 return skb->len;
1070 rtattr_failure:
1071 nlmsg_failure:
1072 module_put(a_o->owner);
1073 skb_trim(skb, b - skb->data);
1074 return skb->len;
1077 static int __init tc_action_init(void)
1079 struct rtnetlink_link *link_p = rtnetlink_links[PF_UNSPEC];
1081 if (link_p) {
1082 link_p[RTM_NEWACTION-RTM_BASE].doit = tc_ctl_action;
1083 link_p[RTM_DELACTION-RTM_BASE].doit = tc_ctl_action;
1084 link_p[RTM_GETACTION-RTM_BASE].doit = tc_ctl_action;
1085 link_p[RTM_GETACTION-RTM_BASE].dumpit = tc_dump_action;
1088 return 0;
1091 subsys_initcall(tc_action_init);
1093 EXPORT_SYMBOL(tcf_register_action);
1094 EXPORT_SYMBOL(tcf_unregister_action);
1095 EXPORT_SYMBOL(tcf_action_exec);
1096 EXPORT_SYMBOL(tcf_action_dump_1);