x86: fix cpu_to_node references
[linux-2.6/mini2440.git] / net / sched / act_api.c
blob72cdb0fade205b3cc77df614de151eb5e6c114a6
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 <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <linux/errno.h>
18 #include <linux/skbuff.h>
19 #include <linux/init.h>
20 #include <linux/kmod.h>
21 #include <net/sch_generic.h>
22 #include <net/act_api.h>
23 #include <net/netlink.h>
25 void tcf_hash_destroy(struct tcf_common *p, struct tcf_hashinfo *hinfo)
27 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
28 struct tcf_common **p1p;
30 for (p1p = &hinfo->htab[h]; *p1p; p1p = &(*p1p)->tcfc_next) {
31 if (*p1p == p) {
32 write_lock_bh(hinfo->lock);
33 *p1p = p->tcfc_next;
34 write_unlock_bh(hinfo->lock);
35 gen_kill_estimator(&p->tcfc_bstats,
36 &p->tcfc_rate_est);
37 kfree(p);
38 return;
41 BUG_TRAP(0);
43 EXPORT_SYMBOL(tcf_hash_destroy);
45 int tcf_hash_release(struct tcf_common *p, int bind,
46 struct tcf_hashinfo *hinfo)
48 int ret = 0;
50 if (p) {
51 if (bind)
52 p->tcfc_bindcnt--;
54 p->tcfc_refcnt--;
55 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
56 tcf_hash_destroy(p, hinfo);
57 ret = 1;
60 return ret;
62 EXPORT_SYMBOL(tcf_hash_release);
64 static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
65 struct tc_action *a, struct tcf_hashinfo *hinfo)
67 struct tcf_common *p;
68 int err = 0, index = -1,i = 0, s_i = 0, n_i = 0;
69 struct rtattr *r ;
71 read_lock_bh(hinfo->lock);
73 s_i = cb->args[0];
75 for (i = 0; i < (hinfo->hmask + 1); i++) {
76 p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
78 for (; p; p = p->tcfc_next) {
79 index++;
80 if (index < s_i)
81 continue;
82 a->priv = p;
83 a->order = n_i;
84 r = (struct rtattr *)skb_tail_pointer(skb);
85 RTA_PUT(skb, a->order, 0, NULL);
86 err = tcf_action_dump_1(skb, a, 0, 0);
87 if (err < 0) {
88 index--;
89 nlmsg_trim(skb, r);
90 goto done;
92 r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
93 n_i++;
94 if (n_i >= TCA_ACT_MAX_PRIO)
95 goto done;
98 done:
99 read_unlock_bh(hinfo->lock);
100 if (n_i)
101 cb->args[0] += n_i;
102 return n_i;
104 rtattr_failure:
105 nlmsg_trim(skb, r);
106 goto done;
109 static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a,
110 struct tcf_hashinfo *hinfo)
112 struct tcf_common *p, *s_p;
113 struct rtattr *r ;
114 int i= 0, n_i = 0;
116 r = (struct rtattr *)skb_tail_pointer(skb);
117 RTA_PUT(skb, a->order, 0, NULL);
118 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
119 for (i = 0; i < (hinfo->hmask + 1); i++) {
120 p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
122 while (p != NULL) {
123 s_p = p->tcfc_next;
124 if (ACT_P_DELETED == tcf_hash_release(p, 0, hinfo))
125 module_put(a->ops->owner);
126 n_i++;
127 p = s_p;
130 RTA_PUT(skb, TCA_FCNT, 4, &n_i);
131 r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
133 return n_i;
134 rtattr_failure:
135 nlmsg_trim(skb, r);
136 return -EINVAL;
139 int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
140 int type, struct tc_action *a)
142 struct tcf_hashinfo *hinfo = a->ops->hinfo;
144 if (type == RTM_DELACTION) {
145 return tcf_del_walker(skb, a, hinfo);
146 } else if (type == RTM_GETACTION) {
147 return tcf_dump_walker(skb, cb, a, hinfo);
148 } else {
149 printk("tcf_generic_walker: unknown action %d\n", type);
150 return -EINVAL;
153 EXPORT_SYMBOL(tcf_generic_walker);
155 struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
157 struct tcf_common *p;
159 read_lock_bh(hinfo->lock);
160 for (p = hinfo->htab[tcf_hash(index, hinfo->hmask)]; p;
161 p = p->tcfc_next) {
162 if (p->tcfc_index == index)
163 break;
165 read_unlock_bh(hinfo->lock);
167 return p;
169 EXPORT_SYMBOL(tcf_hash_lookup);
171 u32 tcf_hash_new_index(u32 *idx_gen, struct tcf_hashinfo *hinfo)
173 u32 val = *idx_gen;
175 do {
176 if (++val == 0)
177 val = 1;
178 } while (tcf_hash_lookup(val, hinfo));
180 return (*idx_gen = val);
182 EXPORT_SYMBOL(tcf_hash_new_index);
184 int tcf_hash_search(struct tc_action *a, u32 index)
186 struct tcf_hashinfo *hinfo = a->ops->hinfo;
187 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
189 if (p) {
190 a->priv = p;
191 return 1;
193 return 0;
195 EXPORT_SYMBOL(tcf_hash_search);
197 struct tcf_common *tcf_hash_check(u32 index, struct tc_action *a, int bind,
198 struct tcf_hashinfo *hinfo)
200 struct tcf_common *p = NULL;
201 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
202 if (bind) {
203 p->tcfc_bindcnt++;
204 p->tcfc_refcnt++;
206 a->priv = p;
208 return p;
210 EXPORT_SYMBOL(tcf_hash_check);
212 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)
214 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
216 if (unlikely(!p))
217 return p;
218 p->tcfc_refcnt = 1;
219 if (bind)
220 p->tcfc_bindcnt = 1;
222 spin_lock_init(&p->tcfc_lock);
223 p->tcfc_index = index ? index : tcf_hash_new_index(idx_gen, hinfo);
224 p->tcfc_tm.install = jiffies;
225 p->tcfc_tm.lastuse = jiffies;
226 if (est)
227 gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
228 &p->tcfc_lock, est);
229 a->priv = (void *) p;
230 return p;
232 EXPORT_SYMBOL(tcf_hash_create);
234 void tcf_hash_insert(struct tcf_common *p, struct tcf_hashinfo *hinfo)
236 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
238 write_lock_bh(hinfo->lock);
239 p->tcfc_next = hinfo->htab[h];
240 hinfo->htab[h] = p;
241 write_unlock_bh(hinfo->lock);
243 EXPORT_SYMBOL(tcf_hash_insert);
245 static struct tc_action_ops *act_base = NULL;
246 static DEFINE_RWLOCK(act_mod_lock);
248 int tcf_register_action(struct tc_action_ops *act)
250 struct tc_action_ops *a, **ap;
252 write_lock(&act_mod_lock);
253 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next) {
254 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
255 write_unlock(&act_mod_lock);
256 return -EEXIST;
259 act->next = NULL;
260 *ap = act;
261 write_unlock(&act_mod_lock);
262 return 0;
265 int tcf_unregister_action(struct tc_action_ops *act)
267 struct tc_action_ops *a, **ap;
268 int err = -ENOENT;
270 write_lock(&act_mod_lock);
271 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next)
272 if (a == act)
273 break;
274 if (a) {
275 *ap = a->next;
276 a->next = NULL;
277 err = 0;
279 write_unlock(&act_mod_lock);
280 return err;
283 /* lookup by name */
284 static struct tc_action_ops *tc_lookup_action_n(char *kind)
286 struct tc_action_ops *a = NULL;
288 if (kind) {
289 read_lock(&act_mod_lock);
290 for (a = act_base; a; a = a->next) {
291 if (strcmp(kind, a->kind) == 0) {
292 if (!try_module_get(a->owner)) {
293 read_unlock(&act_mod_lock);
294 return NULL;
296 break;
299 read_unlock(&act_mod_lock);
301 return a;
304 /* lookup by rtattr */
305 static struct tc_action_ops *tc_lookup_action(struct rtattr *kind)
307 struct tc_action_ops *a = NULL;
309 if (kind) {
310 read_lock(&act_mod_lock);
311 for (a = act_base; a; a = a->next) {
312 if (rtattr_strcmp(kind, a->kind) == 0) {
313 if (!try_module_get(a->owner)) {
314 read_unlock(&act_mod_lock);
315 return NULL;
317 break;
320 read_unlock(&act_mod_lock);
322 return a;
325 #if 0
326 /* lookup by id */
327 static struct tc_action_ops *tc_lookup_action_id(u32 type)
329 struct tc_action_ops *a = NULL;
331 if (type) {
332 read_lock(&act_mod_lock);
333 for (a = act_base; a; a = a->next) {
334 if (a->type == type) {
335 if (!try_module_get(a->owner)) {
336 read_unlock(&act_mod_lock);
337 return NULL;
339 break;
342 read_unlock(&act_mod_lock);
344 return a;
346 #endif
348 int tcf_action_exec(struct sk_buff *skb, struct tc_action *act,
349 struct tcf_result *res)
351 struct tc_action *a;
352 int ret = -1;
354 if (skb->tc_verd & TC_NCLS) {
355 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
356 ret = TC_ACT_OK;
357 goto exec_done;
359 while ((a = act) != NULL) {
360 repeat:
361 if (a->ops && a->ops->act) {
362 ret = a->ops->act(skb, a, res);
363 if (TC_MUNGED & skb->tc_verd) {
364 /* copied already, allow trampling */
365 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
366 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
368 if (ret == TC_ACT_REPEAT)
369 goto repeat; /* we need a ttl - JHS */
370 if (ret != TC_ACT_PIPE)
371 goto exec_done;
373 act = a->next;
375 exec_done:
376 return ret;
379 void tcf_action_destroy(struct tc_action *act, int bind)
381 struct tc_action *a;
383 for (a = act; a; a = act) {
384 if (a->ops && a->ops->cleanup) {
385 if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
386 module_put(a->ops->owner);
387 act = act->next;
388 kfree(a);
389 } else { /*FIXME: Remove later - catch insertion bugs*/
390 printk("tcf_action_destroy: BUG? destroying NULL ops\n");
391 act = act->next;
392 kfree(a);
398 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
400 int err = -EINVAL;
402 if (a->ops == NULL || a->ops->dump == NULL)
403 return err;
404 return a->ops->dump(skb, a, bind, ref);
408 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
410 int err = -EINVAL;
411 unsigned char *b = skb_tail_pointer(skb);
412 struct rtattr *r;
414 if (a->ops == NULL || a->ops->dump == NULL)
415 return err;
417 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
418 if (tcf_action_copy_stats(skb, a, 0))
419 goto rtattr_failure;
420 r = (struct rtattr *)skb_tail_pointer(skb);
421 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
422 if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) {
423 r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
424 return err;
427 rtattr_failure:
428 nlmsg_trim(skb, b);
429 return -1;
433 tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref)
435 struct tc_action *a;
436 int err = -EINVAL;
437 unsigned char *b = skb_tail_pointer(skb);
438 struct rtattr *r ;
440 while ((a = act) != NULL) {
441 r = (struct rtattr *)skb_tail_pointer(skb);
442 act = a->next;
443 RTA_PUT(skb, a->order, 0, NULL);
444 err = tcf_action_dump_1(skb, a, bind, ref);
445 if (err < 0)
446 goto errout;
447 r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
450 return 0;
452 rtattr_failure:
453 err = -EINVAL;
454 errout:
455 nlmsg_trim(skb, b);
456 return err;
459 struct tc_action *tcf_action_init_1(struct rtattr *rta, struct rtattr *est,
460 char *name, int ovr, int bind, int *err)
462 struct tc_action *a;
463 struct tc_action_ops *a_o;
464 char act_name[IFNAMSIZ];
465 struct rtattr *tb[TCA_ACT_MAX+1];
466 struct rtattr *kind;
468 *err = -EINVAL;
470 if (name == NULL) {
471 if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
472 goto err_out;
473 kind = tb[TCA_ACT_KIND-1];
474 if (kind == NULL)
475 goto err_out;
476 if (rtattr_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
477 goto err_out;
478 } else {
479 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
480 goto err_out;
483 a_o = tc_lookup_action_n(act_name);
484 if (a_o == NULL) {
485 #ifdef CONFIG_KMOD
486 rtnl_unlock();
487 request_module("act_%s", act_name);
488 rtnl_lock();
490 a_o = tc_lookup_action_n(act_name);
492 /* We dropped the RTNL semaphore in order to
493 * perform the module load. So, even if we
494 * succeeded in loading the module we have to
495 * tell the caller to replay the request. We
496 * indicate this using -EAGAIN.
498 if (a_o != NULL) {
499 *err = -EAGAIN;
500 goto err_mod;
502 #endif
503 *err = -ENOENT;
504 goto err_out;
507 *err = -ENOMEM;
508 a = kzalloc(sizeof(*a), GFP_KERNEL);
509 if (a == NULL)
510 goto err_mod;
512 /* backward compatibility for policer */
513 if (name == NULL)
514 *err = a_o->init(tb[TCA_ACT_OPTIONS-1], est, a, ovr, bind);
515 else
516 *err = a_o->init(rta, est, a, ovr, bind);
517 if (*err < 0)
518 goto err_free;
520 /* module count goes up only when brand new policy is created
521 if it exists and is only bound to in a_o->init() then
522 ACT_P_CREATED is not returned (a zero is).
524 if (*err != ACT_P_CREATED)
525 module_put(a_o->owner);
526 a->ops = a_o;
528 *err = 0;
529 return a;
531 err_free:
532 kfree(a);
533 err_mod:
534 module_put(a_o->owner);
535 err_out:
536 return NULL;
539 struct tc_action *tcf_action_init(struct rtattr *rta, struct rtattr *est,
540 char *name, int ovr, int bind, int *err)
542 struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
543 struct tc_action *head = NULL, *act, *act_prev = NULL;
544 int i;
546 if (rtattr_parse_nested(tb, TCA_ACT_MAX_PRIO, rta) < 0) {
547 *err = -EINVAL;
548 return head;
551 for (i=0; i < TCA_ACT_MAX_PRIO && tb[i]; i++) {
552 act = tcf_action_init_1(tb[i], est, name, ovr, bind, err);
553 if (act == NULL)
554 goto err;
555 act->order = i+1;
557 if (head == NULL)
558 head = act;
559 else
560 act_prev->next = act;
561 act_prev = act;
563 return head;
565 err:
566 if (head != NULL)
567 tcf_action_destroy(head, bind);
568 return NULL;
571 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
572 int compat_mode)
574 int err = 0;
575 struct gnet_dump d;
576 struct tcf_act_hdr *h = a->priv;
578 if (h == NULL)
579 goto errout;
581 /* compat_mode being true specifies a call that is supposed
582 * to add additional backward compatiblity statistic TLVs.
584 if (compat_mode) {
585 if (a->type == TCA_OLD_COMPAT)
586 err = gnet_stats_start_copy_compat(skb, 0,
587 TCA_STATS, TCA_XSTATS, &h->tcf_lock, &d);
588 else
589 return 0;
590 } else
591 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
592 &h->tcf_lock, &d);
594 if (err < 0)
595 goto errout;
597 if (a->ops != NULL && a->ops->get_stats != NULL)
598 if (a->ops->get_stats(skb, a) < 0)
599 goto errout;
601 if (gnet_stats_copy_basic(&d, &h->tcf_bstats) < 0 ||
602 gnet_stats_copy_rate_est(&d, &h->tcf_rate_est) < 0 ||
603 gnet_stats_copy_queue(&d, &h->tcf_qstats) < 0)
604 goto errout;
606 if (gnet_stats_finish_copy(&d) < 0)
607 goto errout;
609 return 0;
611 errout:
612 return -1;
615 static int
616 tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, u32 seq,
617 u16 flags, int event, int bind, int ref)
619 struct tcamsg *t;
620 struct nlmsghdr *nlh;
621 unsigned char *b = skb_tail_pointer(skb);
622 struct rtattr *x;
624 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
626 t = NLMSG_DATA(nlh);
627 t->tca_family = AF_UNSPEC;
628 t->tca__pad1 = 0;
629 t->tca__pad2 = 0;
631 x = (struct rtattr *)skb_tail_pointer(skb);
632 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
634 if (tcf_action_dump(skb, a, bind, ref) < 0)
635 goto rtattr_failure;
637 x->rta_len = skb_tail_pointer(skb) - (u8 *)x;
639 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
640 return skb->len;
642 rtattr_failure:
643 nlmsg_failure:
644 nlmsg_trim(skb, b);
645 return -1;
648 static int
649 act_get_notify(u32 pid, struct nlmsghdr *n, struct tc_action *a, int event)
651 struct sk_buff *skb;
653 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
654 if (!skb)
655 return -ENOBUFS;
656 if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
657 kfree_skb(skb);
658 return -EINVAL;
661 return rtnl_unicast(skb, pid);
664 static struct tc_action *
665 tcf_action_get_1(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int *err)
667 struct rtattr *tb[TCA_ACT_MAX+1];
668 struct tc_action *a;
669 int index;
671 *err = -EINVAL;
672 if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
673 return NULL;
675 if (tb[TCA_ACT_INDEX - 1] == NULL ||
676 RTA_PAYLOAD(tb[TCA_ACT_INDEX - 1]) < sizeof(index))
677 return NULL;
678 index = *(int *)RTA_DATA(tb[TCA_ACT_INDEX - 1]);
680 *err = -ENOMEM;
681 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
682 if (a == NULL)
683 return NULL;
685 *err = -EINVAL;
686 a->ops = tc_lookup_action(tb[TCA_ACT_KIND - 1]);
687 if (a->ops == NULL)
688 goto err_free;
689 if (a->ops->lookup == NULL)
690 goto err_mod;
691 *err = -ENOENT;
692 if (a->ops->lookup(a, index) == 0)
693 goto err_mod;
695 module_put(a->ops->owner);
696 *err = 0;
697 return a;
698 err_mod:
699 module_put(a->ops->owner);
700 err_free:
701 kfree(a);
702 return NULL;
705 static void cleanup_a(struct tc_action *act)
707 struct tc_action *a;
709 for (a = act; a; a = act) {
710 act = a->next;
711 kfree(a);
715 static struct tc_action *create_a(int i)
717 struct tc_action *act;
719 act = kzalloc(sizeof(*act), GFP_KERNEL);
720 if (act == NULL) {
721 printk("create_a: failed to alloc!\n");
722 return NULL;
724 act->order = i;
725 return act;
728 static int tca_action_flush(struct rtattr *rta, struct nlmsghdr *n, u32 pid)
730 struct sk_buff *skb;
731 unsigned char *b;
732 struct nlmsghdr *nlh;
733 struct tcamsg *t;
734 struct netlink_callback dcb;
735 struct rtattr *x;
736 struct rtattr *tb[TCA_ACT_MAX+1];
737 struct rtattr *kind;
738 struct tc_action *a = create_a(0);
739 int err = -EINVAL;
741 if (a == NULL) {
742 printk("tca_action_flush: couldnt create tc_action\n");
743 return err;
746 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
747 if (!skb) {
748 printk("tca_action_flush: failed skb alloc\n");
749 kfree(a);
750 return -ENOBUFS;
753 b = skb_tail_pointer(skb);
755 if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
756 goto err_out;
758 kind = tb[TCA_ACT_KIND-1];
759 a->ops = tc_lookup_action(kind);
760 if (a->ops == NULL)
761 goto err_out;
763 nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t));
764 t = NLMSG_DATA(nlh);
765 t->tca_family = AF_UNSPEC;
766 t->tca__pad1 = 0;
767 t->tca__pad2 = 0;
769 x = (struct rtattr *)skb_tail_pointer(skb);
770 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
772 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
773 if (err < 0)
774 goto rtattr_failure;
776 x->rta_len = skb_tail_pointer(skb) - (u8 *)x;
778 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
779 nlh->nlmsg_flags |= NLM_F_ROOT;
780 module_put(a->ops->owner);
781 kfree(a);
782 err = rtnetlink_send(skb, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
783 if (err > 0)
784 return 0;
786 return err;
788 rtattr_failure:
789 nlmsg_failure:
790 module_put(a->ops->owner);
791 err_out:
792 kfree_skb(skb);
793 kfree(a);
794 return err;
797 static int
798 tca_action_gd(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int event)
800 int i, ret = 0;
801 struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
802 struct tc_action *head = NULL, *act, *act_prev = NULL;
804 if (rtattr_parse_nested(tb, TCA_ACT_MAX_PRIO, rta) < 0)
805 return -EINVAL;
807 if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) {
808 if (tb[0] != NULL && tb[1] == NULL)
809 return tca_action_flush(tb[0], n, pid);
812 for (i=0; i < TCA_ACT_MAX_PRIO && tb[i]; i++) {
813 act = tcf_action_get_1(tb[i], n, pid, &ret);
814 if (act == NULL)
815 goto err;
816 act->order = i+1;
818 if (head == NULL)
819 head = act;
820 else
821 act_prev->next = act;
822 act_prev = act;
825 if (event == RTM_GETACTION)
826 ret = act_get_notify(pid, n, head, event);
827 else { /* delete */
828 struct sk_buff *skb;
830 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
831 if (!skb) {
832 ret = -ENOBUFS;
833 goto err;
836 if (tca_get_fill(skb, head, pid, n->nlmsg_seq, 0, event,
837 0, 1) <= 0) {
838 kfree_skb(skb);
839 ret = -EINVAL;
840 goto err;
843 /* now do the delete */
844 tcf_action_destroy(head, 0);
845 ret = rtnetlink_send(skb, pid, RTNLGRP_TC,
846 n->nlmsg_flags&NLM_F_ECHO);
847 if (ret > 0)
848 return 0;
849 return ret;
851 err:
852 cleanup_a(head);
853 return ret;
856 static int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event,
857 u16 flags)
859 struct tcamsg *t;
860 struct nlmsghdr *nlh;
861 struct sk_buff *skb;
862 struct rtattr *x;
863 unsigned char *b;
864 int err = 0;
866 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
867 if (!skb)
868 return -ENOBUFS;
870 b = skb_tail_pointer(skb);
872 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
873 t = NLMSG_DATA(nlh);
874 t->tca_family = AF_UNSPEC;
875 t->tca__pad1 = 0;
876 t->tca__pad2 = 0;
878 x = (struct rtattr *)skb_tail_pointer(skb);
879 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
881 if (tcf_action_dump(skb, a, 0, 0) < 0)
882 goto rtattr_failure;
884 x->rta_len = skb_tail_pointer(skb) - (u8 *)x;
886 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
887 NETLINK_CB(skb).dst_group = RTNLGRP_TC;
889 err = rtnetlink_send(skb, pid, RTNLGRP_TC, flags&NLM_F_ECHO);
890 if (err > 0)
891 err = 0;
892 return err;
894 rtattr_failure:
895 nlmsg_failure:
896 kfree_skb(skb);
897 return -1;
901 static int
902 tcf_action_add(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int ovr)
904 int ret = 0;
905 struct tc_action *act;
906 struct tc_action *a;
907 u32 seq = n->nlmsg_seq;
909 act = tcf_action_init(rta, NULL, NULL, ovr, 0, &ret);
910 if (act == NULL)
911 goto done;
913 /* dump then free all the actions after update; inserted policy
914 * stays intact
915 * */
916 ret = tcf_add_notify(act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
917 for (a = act; a; a = act) {
918 act = a->next;
919 kfree(a);
921 done:
922 return ret;
925 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
927 struct rtattr **tca = arg;
928 u32 pid = skb ? NETLINK_CB(skb).pid : 0;
929 int ret = 0, ovr = 0;
931 if (tca[TCA_ACT_TAB-1] == NULL) {
932 printk("tc_ctl_action: received NO action attribs\n");
933 return -EINVAL;
936 /* n->nlmsg_flags&NLM_F_CREATE
937 * */
938 switch (n->nlmsg_type) {
939 case RTM_NEWACTION:
940 /* we are going to assume all other flags
941 * imply create only if it doesnt exist
942 * Note that CREATE | EXCL implies that
943 * but since we want avoid ambiguity (eg when flags
944 * is zero) then just set this
946 if (n->nlmsg_flags&NLM_F_REPLACE)
947 ovr = 1;
948 replay:
949 ret = tcf_action_add(tca[TCA_ACT_TAB-1], n, pid, ovr);
950 if (ret == -EAGAIN)
951 goto replay;
952 break;
953 case RTM_DELACTION:
954 ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid, RTM_DELACTION);
955 break;
956 case RTM_GETACTION:
957 ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid, RTM_GETACTION);
958 break;
959 default:
960 BUG();
963 return ret;
966 static struct rtattr *
967 find_dump_kind(struct nlmsghdr *n)
969 struct rtattr *tb1, *tb2[TCA_ACT_MAX+1];
970 struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
971 struct rtattr *rta[TCAA_MAX + 1];
972 struct rtattr *kind;
973 int min_len = NLMSG_LENGTH(sizeof(struct tcamsg));
974 int attrlen = n->nlmsg_len - NLMSG_ALIGN(min_len);
975 struct rtattr *attr = (void *) n + NLMSG_ALIGN(min_len);
977 if (rtattr_parse(rta, TCAA_MAX, attr, attrlen) < 0)
978 return NULL;
979 tb1 = rta[TCA_ACT_TAB - 1];
980 if (tb1 == NULL)
981 return NULL;
983 if (rtattr_parse(tb, TCA_ACT_MAX_PRIO, RTA_DATA(tb1),
984 NLMSG_ALIGN(RTA_PAYLOAD(tb1))) < 0)
985 return NULL;
986 if (tb[0] == NULL)
987 return NULL;
989 if (rtattr_parse(tb2, TCA_ACT_MAX, RTA_DATA(tb[0]),
990 RTA_PAYLOAD(tb[0])) < 0)
991 return NULL;
992 kind = tb2[TCA_ACT_KIND-1];
994 return kind;
997 static int
998 tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1000 struct nlmsghdr *nlh;
1001 unsigned char *b = skb_tail_pointer(skb);
1002 struct rtattr *x;
1003 struct tc_action_ops *a_o;
1004 struct tc_action a;
1005 int ret = 0;
1006 struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
1007 struct rtattr *kind = find_dump_kind(cb->nlh);
1009 if (kind == NULL) {
1010 printk("tc_dump_action: action bad kind\n");
1011 return 0;
1014 a_o = tc_lookup_action(kind);
1015 if (a_o == NULL) {
1016 return 0;
1019 memset(&a, 0, sizeof(struct tc_action));
1020 a.ops = a_o;
1022 if (a_o->walk == NULL) {
1023 printk("tc_dump_action: %s !capable of dumping table\n", a_o->kind);
1024 goto rtattr_failure;
1027 nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
1028 cb->nlh->nlmsg_type, sizeof(*t));
1029 t = NLMSG_DATA(nlh);
1030 t->tca_family = AF_UNSPEC;
1031 t->tca__pad1 = 0;
1032 t->tca__pad2 = 0;
1034 x = (struct rtattr *)skb_tail_pointer(skb);
1035 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
1037 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1038 if (ret < 0)
1039 goto rtattr_failure;
1041 if (ret > 0) {
1042 x->rta_len = skb_tail_pointer(skb) - (u8 *)x;
1043 ret = skb->len;
1044 } else
1045 nlmsg_trim(skb, x);
1047 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1048 if (NETLINK_CB(cb->skb).pid && ret)
1049 nlh->nlmsg_flags |= NLM_F_MULTI;
1050 module_put(a_o->owner);
1051 return skb->len;
1053 rtattr_failure:
1054 nlmsg_failure:
1055 module_put(a_o->owner);
1056 nlmsg_trim(skb, b);
1057 return skb->len;
1060 static int __init tc_action_init(void)
1062 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL);
1063 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL);
1064 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action);
1066 return 0;
1069 subsys_initcall(tc_action_init);
1071 EXPORT_SYMBOL(tcf_register_action);
1072 EXPORT_SYMBOL(tcf_unregister_action);
1073 EXPORT_SYMBOL(tcf_action_exec);
1074 EXPORT_SYMBOL(tcf_action_dump_1);