]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/sched/act_api.c
[NET_SCHED]: Remove CONFIG_NET_ESTIMATOR option
[mirror_ubuntu-artful-kernel.git] / net / sched / act_api.c
1 /*
2 * net/sched/act_api.c Packet action 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 * Author: Jamal Hadi Salim
10 *
11 *
12 */
13
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/init.h>
29 #include <linux/kmod.h>
30 #include <net/sock.h>
31 #include <net/sch_generic.h>
32 #include <net/act_api.h>
33 #include <net/netlink.h>
34
35 void tcf_hash_destroy(struct tcf_common *p, struct tcf_hashinfo *hinfo)
36 {
37 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
38 struct tcf_common **p1p;
39
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 gen_kill_estimator(&p->tcfc_bstats,
46 &p->tcfc_rate_est);
47 kfree(p);
48 return;
49 }
50 }
51 BUG_TRAP(0);
52 }
53 EXPORT_SYMBOL(tcf_hash_destroy);
54
55 int tcf_hash_release(struct tcf_common *p, int bind,
56 struct tcf_hashinfo *hinfo)
57 {
58 int ret = 0;
59
60 if (p) {
61 if (bind)
62 p->tcfc_bindcnt--;
63
64 p->tcfc_refcnt--;
65 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
66 tcf_hash_destroy(p, hinfo);
67 ret = 1;
68 }
69 }
70 return ret;
71 }
72 EXPORT_SYMBOL(tcf_hash_release);
73
74 static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
75 struct tc_action *a, struct tcf_hashinfo *hinfo)
76 {
77 struct tcf_common *p;
78 int err = 0, index = -1,i = 0, s_i = 0, n_i = 0;
79 struct rtattr *r ;
80
81 read_lock(hinfo->lock);
82
83 s_i = cb->args[0];
84
85 for (i = 0; i < (hinfo->hmask + 1); i++) {
86 p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
87
88 for (; p; p = p->tcfc_next) {
89 index++;
90 if (index < s_i)
91 continue;
92 a->priv = p;
93 a->order = n_i;
94 r = (struct rtattr *)skb_tail_pointer(skb);
95 RTA_PUT(skb, a->order, 0, NULL);
96 err = tcf_action_dump_1(skb, a, 0, 0);
97 if (err < 0) {
98 index--;
99 nlmsg_trim(skb, r);
100 goto done;
101 }
102 r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
103 n_i++;
104 if (n_i >= TCA_ACT_MAX_PRIO)
105 goto done;
106 }
107 }
108 done:
109 read_unlock(hinfo->lock);
110 if (n_i)
111 cb->args[0] += n_i;
112 return n_i;
113
114 rtattr_failure:
115 nlmsg_trim(skb, r);
116 goto done;
117 }
118
119 static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a,
120 struct tcf_hashinfo *hinfo)
121 {
122 struct tcf_common *p, *s_p;
123 struct rtattr *r ;
124 int i= 0, n_i = 0;
125
126 r = (struct rtattr *)skb_tail_pointer(skb);
127 RTA_PUT(skb, a->order, 0, NULL);
128 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
129 for (i = 0; i < (hinfo->hmask + 1); i++) {
130 p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
131
132 while (p != NULL) {
133 s_p = p->tcfc_next;
134 if (ACT_P_DELETED == tcf_hash_release(p, 0, hinfo))
135 module_put(a->ops->owner);
136 n_i++;
137 p = s_p;
138 }
139 }
140 RTA_PUT(skb, TCA_FCNT, 4, &n_i);
141 r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
142
143 return n_i;
144 rtattr_failure:
145 nlmsg_trim(skb, r);
146 return -EINVAL;
147 }
148
149 int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
150 int type, struct tc_action *a)
151 {
152 struct tcf_hashinfo *hinfo = a->ops->hinfo;
153
154 if (type == RTM_DELACTION) {
155 return tcf_del_walker(skb, a, hinfo);
156 } else if (type == RTM_GETACTION) {
157 return tcf_dump_walker(skb, cb, a, hinfo);
158 } else {
159 printk("tcf_generic_walker: unknown action %d\n", type);
160 return -EINVAL;
161 }
162 }
163 EXPORT_SYMBOL(tcf_generic_walker);
164
165 struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
166 {
167 struct tcf_common *p;
168
169 read_lock(hinfo->lock);
170 for (p = hinfo->htab[tcf_hash(index, hinfo->hmask)]; p;
171 p = p->tcfc_next) {
172 if (p->tcfc_index == index)
173 break;
174 }
175 read_unlock(hinfo->lock);
176
177 return p;
178 }
179 EXPORT_SYMBOL(tcf_hash_lookup);
180
181 u32 tcf_hash_new_index(u32 *idx_gen, struct tcf_hashinfo *hinfo)
182 {
183 u32 val = *idx_gen;
184
185 do {
186 if (++val == 0)
187 val = 1;
188 } while (tcf_hash_lookup(val, hinfo));
189
190 return (*idx_gen = val);
191 }
192 EXPORT_SYMBOL(tcf_hash_new_index);
193
194 int tcf_hash_search(struct tc_action *a, u32 index)
195 {
196 struct tcf_hashinfo *hinfo = a->ops->hinfo;
197 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
198
199 if (p) {
200 a->priv = p;
201 return 1;
202 }
203 return 0;
204 }
205 EXPORT_SYMBOL(tcf_hash_search);
206
207 struct tcf_common *tcf_hash_check(u32 index, struct tc_action *a, int bind,
208 struct tcf_hashinfo *hinfo)
209 {
210 struct tcf_common *p = NULL;
211 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
212 if (bind) {
213 p->tcfc_bindcnt++;
214 p->tcfc_refcnt++;
215 }
216 a->priv = p;
217 }
218 return p;
219 }
220 EXPORT_SYMBOL(tcf_hash_check);
221
222 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)
223 {
224 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
225
226 if (unlikely(!p))
227 return p;
228 p->tcfc_refcnt = 1;
229 if (bind)
230 p->tcfc_bindcnt = 1;
231
232 spin_lock_init(&p->tcfc_lock);
233 p->tcfc_stats_lock = &p->tcfc_lock;
234 p->tcfc_index = index ? index : tcf_hash_new_index(idx_gen, hinfo);
235 p->tcfc_tm.install = jiffies;
236 p->tcfc_tm.lastuse = jiffies;
237 if (est)
238 gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
239 p->tcfc_stats_lock, est);
240 a->priv = (void *) p;
241 return p;
242 }
243 EXPORT_SYMBOL(tcf_hash_create);
244
245 void tcf_hash_insert(struct tcf_common *p, struct tcf_hashinfo *hinfo)
246 {
247 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
248
249 write_lock_bh(hinfo->lock);
250 p->tcfc_next = hinfo->htab[h];
251 hinfo->htab[h] = p;
252 write_unlock_bh(hinfo->lock);
253 }
254 EXPORT_SYMBOL(tcf_hash_insert);
255
256 static struct tc_action_ops *act_base = NULL;
257 static DEFINE_RWLOCK(act_mod_lock);
258
259 int tcf_register_action(struct tc_action_ops *act)
260 {
261 struct tc_action_ops *a, **ap;
262
263 write_lock(&act_mod_lock);
264 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next) {
265 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
266 write_unlock(&act_mod_lock);
267 return -EEXIST;
268 }
269 }
270 act->next = NULL;
271 *ap = act;
272 write_unlock(&act_mod_lock);
273 return 0;
274 }
275
276 int tcf_unregister_action(struct tc_action_ops *act)
277 {
278 struct tc_action_ops *a, **ap;
279 int err = -ENOENT;
280
281 write_lock(&act_mod_lock);
282 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next)
283 if (a == act)
284 break;
285 if (a) {
286 *ap = a->next;
287 a->next = NULL;
288 err = 0;
289 }
290 write_unlock(&act_mod_lock);
291 return err;
292 }
293
294 /* lookup by name */
295 static struct tc_action_ops *tc_lookup_action_n(char *kind)
296 {
297 struct tc_action_ops *a = NULL;
298
299 if (kind) {
300 read_lock(&act_mod_lock);
301 for (a = act_base; a; a = a->next) {
302 if (strcmp(kind, a->kind) == 0) {
303 if (!try_module_get(a->owner)) {
304 read_unlock(&act_mod_lock);
305 return NULL;
306 }
307 break;
308 }
309 }
310 read_unlock(&act_mod_lock);
311 }
312 return a;
313 }
314
315 /* lookup by rtattr */
316 static struct tc_action_ops *tc_lookup_action(struct rtattr *kind)
317 {
318 struct tc_action_ops *a = NULL;
319
320 if (kind) {
321 read_lock(&act_mod_lock);
322 for (a = act_base; a; a = a->next) {
323 if (rtattr_strcmp(kind, a->kind) == 0) {
324 if (!try_module_get(a->owner)) {
325 read_unlock(&act_mod_lock);
326 return NULL;
327 }
328 break;
329 }
330 }
331 read_unlock(&act_mod_lock);
332 }
333 return a;
334 }
335
336 #if 0
337 /* lookup by id */
338 static struct tc_action_ops *tc_lookup_action_id(u32 type)
339 {
340 struct tc_action_ops *a = NULL;
341
342 if (type) {
343 read_lock(&act_mod_lock);
344 for (a = act_base; a; a = a->next) {
345 if (a->type == type) {
346 if (!try_module_get(a->owner)) {
347 read_unlock(&act_mod_lock);
348 return NULL;
349 }
350 break;
351 }
352 }
353 read_unlock(&act_mod_lock);
354 }
355 return a;
356 }
357 #endif
358
359 int tcf_action_exec(struct sk_buff *skb, struct tc_action *act,
360 struct tcf_result *res)
361 {
362 struct tc_action *a;
363 int ret = -1;
364
365 if (skb->tc_verd & TC_NCLS) {
366 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
367 ret = TC_ACT_OK;
368 goto exec_done;
369 }
370 while ((a = act) != NULL) {
371 repeat:
372 if (a->ops && a->ops->act) {
373 ret = a->ops->act(skb, a, res);
374 if (TC_MUNGED & skb->tc_verd) {
375 /* copied already, allow trampling */
376 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
377 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
378 }
379 if (ret == TC_ACT_REPEAT)
380 goto repeat; /* we need a ttl - JHS */
381 if (ret != TC_ACT_PIPE)
382 goto exec_done;
383 }
384 act = a->next;
385 }
386 exec_done:
387 return ret;
388 }
389
390 void tcf_action_destroy(struct tc_action *act, int bind)
391 {
392 struct tc_action *a;
393
394 for (a = act; a; a = act) {
395 if (a->ops && a->ops->cleanup) {
396 if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
397 module_put(a->ops->owner);
398 act = act->next;
399 kfree(a);
400 } else { /*FIXME: Remove later - catch insertion bugs*/
401 printk("tcf_action_destroy: BUG? destroying NULL ops\n");
402 act = act->next;
403 kfree(a);
404 }
405 }
406 }
407
408 int
409 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
410 {
411 int err = -EINVAL;
412
413 if (a->ops == NULL || a->ops->dump == NULL)
414 return err;
415 return a->ops->dump(skb, a, bind, ref);
416 }
417
418 int
419 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
420 {
421 int err = -EINVAL;
422 unsigned char *b = skb_tail_pointer(skb);
423 struct rtattr *r;
424
425 if (a->ops == NULL || a->ops->dump == NULL)
426 return err;
427
428 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
429 if (tcf_action_copy_stats(skb, a, 0))
430 goto rtattr_failure;
431 r = (struct rtattr *)skb_tail_pointer(skb);
432 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
433 if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) {
434 r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
435 return err;
436 }
437
438 rtattr_failure:
439 nlmsg_trim(skb, b);
440 return -1;
441 }
442
443 int
444 tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref)
445 {
446 struct tc_action *a;
447 int err = -EINVAL;
448 unsigned char *b = skb_tail_pointer(skb);
449 struct rtattr *r ;
450
451 while ((a = act) != NULL) {
452 r = (struct rtattr *)skb_tail_pointer(skb);
453 act = a->next;
454 RTA_PUT(skb, a->order, 0, NULL);
455 err = tcf_action_dump_1(skb, a, bind, ref);
456 if (err < 0)
457 goto errout;
458 r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
459 }
460
461 return 0;
462
463 rtattr_failure:
464 err = -EINVAL;
465 errout:
466 nlmsg_trim(skb, b);
467 return err;
468 }
469
470 struct tc_action *tcf_action_init_1(struct rtattr *rta, struct rtattr *est,
471 char *name, int ovr, int bind, int *err)
472 {
473 struct tc_action *a;
474 struct tc_action_ops *a_o;
475 char act_name[IFNAMSIZ];
476 struct rtattr *tb[TCA_ACT_MAX+1];
477 struct rtattr *kind;
478
479 *err = -EINVAL;
480
481 if (name == NULL) {
482 if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
483 goto err_out;
484 kind = tb[TCA_ACT_KIND-1];
485 if (kind == NULL)
486 goto err_out;
487 if (rtattr_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
488 goto err_out;
489 } else {
490 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
491 goto err_out;
492 }
493
494 a_o = tc_lookup_action_n(act_name);
495 if (a_o == NULL) {
496 #ifdef CONFIG_KMOD
497 rtnl_unlock();
498 request_module("act_%s", act_name);
499 rtnl_lock();
500
501 a_o = tc_lookup_action_n(act_name);
502
503 /* We dropped the RTNL semaphore in order to
504 * perform the module load. So, even if we
505 * succeeded in loading the module we have to
506 * tell the caller to replay the request. We
507 * indicate this using -EAGAIN.
508 */
509 if (a_o != NULL) {
510 *err = -EAGAIN;
511 goto err_mod;
512 }
513 #endif
514 *err = -ENOENT;
515 goto err_out;
516 }
517
518 *err = -ENOMEM;
519 a = kzalloc(sizeof(*a), GFP_KERNEL);
520 if (a == NULL)
521 goto err_mod;
522
523 /* backward compatibility for policer */
524 if (name == NULL)
525 *err = a_o->init(tb[TCA_ACT_OPTIONS-1], est, a, ovr, bind);
526 else
527 *err = a_o->init(rta, est, a, ovr, bind);
528 if (*err < 0)
529 goto err_free;
530
531 /* module count goes up only when brand new policy is created
532 if it exists and is only bound to in a_o->init() then
533 ACT_P_CREATED is not returned (a zero is).
534 */
535 if (*err != ACT_P_CREATED)
536 module_put(a_o->owner);
537 a->ops = a_o;
538
539 *err = 0;
540 return a;
541
542 err_free:
543 kfree(a);
544 err_mod:
545 module_put(a_o->owner);
546 err_out:
547 return NULL;
548 }
549
550 struct tc_action *tcf_action_init(struct rtattr *rta, struct rtattr *est,
551 char *name, int ovr, int bind, int *err)
552 {
553 struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
554 struct tc_action *head = NULL, *act, *act_prev = NULL;
555 int i;
556
557 if (rtattr_parse_nested(tb, TCA_ACT_MAX_PRIO, rta) < 0) {
558 *err = -EINVAL;
559 return head;
560 }
561
562 for (i=0; i < TCA_ACT_MAX_PRIO && tb[i]; i++) {
563 act = tcf_action_init_1(tb[i], est, name, ovr, bind, err);
564 if (act == NULL)
565 goto err;
566 act->order = i+1;
567
568 if (head == NULL)
569 head = act;
570 else
571 act_prev->next = act;
572 act_prev = act;
573 }
574 return head;
575
576 err:
577 if (head != NULL)
578 tcf_action_destroy(head, bind);
579 return NULL;
580 }
581
582 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
583 int compat_mode)
584 {
585 int err = 0;
586 struct gnet_dump d;
587 struct tcf_act_hdr *h = a->priv;
588
589 if (h == NULL)
590 goto errout;
591
592 /* compat_mode being true specifies a call that is supposed
593 * to add additional backward compatiblity statistic TLVs.
594 */
595 if (compat_mode) {
596 if (a->type == TCA_OLD_COMPAT)
597 err = gnet_stats_start_copy_compat(skb, 0,
598 TCA_STATS, TCA_XSTATS, h->tcf_stats_lock, &d);
599 else
600 return 0;
601 } else
602 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
603 h->tcf_stats_lock, &d);
604
605 if (err < 0)
606 goto errout;
607
608 if (a->ops != NULL && a->ops->get_stats != NULL)
609 if (a->ops->get_stats(skb, a) < 0)
610 goto errout;
611
612 if (gnet_stats_copy_basic(&d, &h->tcf_bstats) < 0 ||
613 gnet_stats_copy_rate_est(&d, &h->tcf_rate_est) < 0 ||
614 gnet_stats_copy_queue(&d, &h->tcf_qstats) < 0)
615 goto errout;
616
617 if (gnet_stats_finish_copy(&d) < 0)
618 goto errout;
619
620 return 0;
621
622 errout:
623 return -1;
624 }
625
626 static int
627 tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, u32 seq,
628 u16 flags, int event, int bind, int ref)
629 {
630 struct tcamsg *t;
631 struct nlmsghdr *nlh;
632 unsigned char *b = skb_tail_pointer(skb);
633 struct rtattr *x;
634
635 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
636
637 t = NLMSG_DATA(nlh);
638 t->tca_family = AF_UNSPEC;
639 t->tca__pad1 = 0;
640 t->tca__pad2 = 0;
641
642 x = (struct rtattr *)skb_tail_pointer(skb);
643 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
644
645 if (tcf_action_dump(skb, a, bind, ref) < 0)
646 goto rtattr_failure;
647
648 x->rta_len = skb_tail_pointer(skb) - (u8 *)x;
649
650 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
651 return skb->len;
652
653 rtattr_failure:
654 nlmsg_failure:
655 nlmsg_trim(skb, b);
656 return -1;
657 }
658
659 static int
660 act_get_notify(u32 pid, struct nlmsghdr *n, struct tc_action *a, int event)
661 {
662 struct sk_buff *skb;
663
664 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
665 if (!skb)
666 return -ENOBUFS;
667 if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
668 kfree_skb(skb);
669 return -EINVAL;
670 }
671
672 return rtnl_unicast(skb, pid);
673 }
674
675 static struct tc_action *
676 tcf_action_get_1(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int *err)
677 {
678 struct rtattr *tb[TCA_ACT_MAX+1];
679 struct tc_action *a;
680 int index;
681
682 *err = -EINVAL;
683 if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
684 return NULL;
685
686 if (tb[TCA_ACT_INDEX - 1] == NULL ||
687 RTA_PAYLOAD(tb[TCA_ACT_INDEX - 1]) < sizeof(index))
688 return NULL;
689 index = *(int *)RTA_DATA(tb[TCA_ACT_INDEX - 1]);
690
691 *err = -ENOMEM;
692 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
693 if (a == NULL)
694 return NULL;
695
696 *err = -EINVAL;
697 a->ops = tc_lookup_action(tb[TCA_ACT_KIND - 1]);
698 if (a->ops == NULL)
699 goto err_free;
700 if (a->ops->lookup == NULL)
701 goto err_mod;
702 *err = -ENOENT;
703 if (a->ops->lookup(a, index) == 0)
704 goto err_mod;
705
706 module_put(a->ops->owner);
707 *err = 0;
708 return a;
709 err_mod:
710 module_put(a->ops->owner);
711 err_free:
712 kfree(a);
713 return NULL;
714 }
715
716 static void cleanup_a(struct tc_action *act)
717 {
718 struct tc_action *a;
719
720 for (a = act; a; a = act) {
721 act = a->next;
722 kfree(a);
723 }
724 }
725
726 static struct tc_action *create_a(int i)
727 {
728 struct tc_action *act;
729
730 act = kzalloc(sizeof(*act), GFP_KERNEL);
731 if (act == NULL) {
732 printk("create_a: failed to alloc!\n");
733 return NULL;
734 }
735 act->order = i;
736 return act;
737 }
738
739 static int tca_action_flush(struct rtattr *rta, struct nlmsghdr *n, u32 pid)
740 {
741 struct sk_buff *skb;
742 unsigned char *b;
743 struct nlmsghdr *nlh;
744 struct tcamsg *t;
745 struct netlink_callback dcb;
746 struct rtattr *x;
747 struct rtattr *tb[TCA_ACT_MAX+1];
748 struct rtattr *kind;
749 struct tc_action *a = create_a(0);
750 int err = -EINVAL;
751
752 if (a == NULL) {
753 printk("tca_action_flush: couldnt create tc_action\n");
754 return err;
755 }
756
757 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
758 if (!skb) {
759 printk("tca_action_flush: failed skb alloc\n");
760 kfree(a);
761 return -ENOBUFS;
762 }
763
764 b = skb_tail_pointer(skb);
765
766 if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
767 goto err_out;
768
769 kind = tb[TCA_ACT_KIND-1];
770 a->ops = tc_lookup_action(kind);
771 if (a->ops == NULL)
772 goto err_out;
773
774 nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t));
775 t = NLMSG_DATA(nlh);
776 t->tca_family = AF_UNSPEC;
777 t->tca__pad1 = 0;
778 t->tca__pad2 = 0;
779
780 x = (struct rtattr *)skb_tail_pointer(skb);
781 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
782
783 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
784 if (err < 0)
785 goto rtattr_failure;
786
787 x->rta_len = skb_tail_pointer(skb) - (u8 *)x;
788
789 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
790 nlh->nlmsg_flags |= NLM_F_ROOT;
791 module_put(a->ops->owner);
792 kfree(a);
793 err = rtnetlink_send(skb, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
794 if (err > 0)
795 return 0;
796
797 return err;
798
799 rtattr_failure:
800 nlmsg_failure:
801 module_put(a->ops->owner);
802 err_out:
803 kfree_skb(skb);
804 kfree(a);
805 return err;
806 }
807
808 static int
809 tca_action_gd(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int event)
810 {
811 int i, ret = 0;
812 struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
813 struct tc_action *head = NULL, *act, *act_prev = NULL;
814
815 if (rtattr_parse_nested(tb, TCA_ACT_MAX_PRIO, rta) < 0)
816 return -EINVAL;
817
818 if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) {
819 if (tb[0] != NULL && tb[1] == NULL)
820 return tca_action_flush(tb[0], n, pid);
821 }
822
823 for (i=0; i < TCA_ACT_MAX_PRIO && tb[i]; i++) {
824 act = tcf_action_get_1(tb[i], n, pid, &ret);
825 if (act == NULL)
826 goto err;
827 act->order = i+1;
828
829 if (head == NULL)
830 head = act;
831 else
832 act_prev->next = act;
833 act_prev = act;
834 }
835
836 if (event == RTM_GETACTION)
837 ret = act_get_notify(pid, n, head, event);
838 else { /* delete */
839 struct sk_buff *skb;
840
841 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
842 if (!skb) {
843 ret = -ENOBUFS;
844 goto err;
845 }
846
847 if (tca_get_fill(skb, head, pid, n->nlmsg_seq, 0, event,
848 0, 1) <= 0) {
849 kfree_skb(skb);
850 ret = -EINVAL;
851 goto err;
852 }
853
854 /* now do the delete */
855 tcf_action_destroy(head, 0);
856 ret = rtnetlink_send(skb, pid, RTNLGRP_TC,
857 n->nlmsg_flags&NLM_F_ECHO);
858 if (ret > 0)
859 return 0;
860 return ret;
861 }
862 err:
863 cleanup_a(head);
864 return ret;
865 }
866
867 static int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event,
868 u16 flags)
869 {
870 struct tcamsg *t;
871 struct nlmsghdr *nlh;
872 struct sk_buff *skb;
873 struct rtattr *x;
874 unsigned char *b;
875 int err = 0;
876
877 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
878 if (!skb)
879 return -ENOBUFS;
880
881 b = skb_tail_pointer(skb);
882
883 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
884 t = NLMSG_DATA(nlh);
885 t->tca_family = AF_UNSPEC;
886 t->tca__pad1 = 0;
887 t->tca__pad2 = 0;
888
889 x = (struct rtattr *)skb_tail_pointer(skb);
890 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
891
892 if (tcf_action_dump(skb, a, 0, 0) < 0)
893 goto rtattr_failure;
894
895 x->rta_len = skb_tail_pointer(skb) - (u8 *)x;
896
897 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
898 NETLINK_CB(skb).dst_group = RTNLGRP_TC;
899
900 err = rtnetlink_send(skb, pid, RTNLGRP_TC, flags&NLM_F_ECHO);
901 if (err > 0)
902 err = 0;
903 return err;
904
905 rtattr_failure:
906 nlmsg_failure:
907 kfree_skb(skb);
908 return -1;
909 }
910
911
912 static int
913 tcf_action_add(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int ovr)
914 {
915 int ret = 0;
916 struct tc_action *act;
917 struct tc_action *a;
918 u32 seq = n->nlmsg_seq;
919
920 act = tcf_action_init(rta, NULL, NULL, ovr, 0, &ret);
921 if (act == NULL)
922 goto done;
923
924 /* dump then free all the actions after update; inserted policy
925 * stays intact
926 * */
927 ret = tcf_add_notify(act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
928 for (a = act; a; a = act) {
929 act = a->next;
930 kfree(a);
931 }
932 done:
933 return ret;
934 }
935
936 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
937 {
938 struct rtattr **tca = arg;
939 u32 pid = skb ? NETLINK_CB(skb).pid : 0;
940 int ret = 0, ovr = 0;
941
942 if (tca[TCA_ACT_TAB-1] == NULL) {
943 printk("tc_ctl_action: received NO action attribs\n");
944 return -EINVAL;
945 }
946
947 /* n->nlmsg_flags&NLM_F_CREATE
948 * */
949 switch (n->nlmsg_type) {
950 case RTM_NEWACTION:
951 /* we are going to assume all other flags
952 * imply create only if it doesnt exist
953 * Note that CREATE | EXCL implies that
954 * but since we want avoid ambiguity (eg when flags
955 * is zero) then just set this
956 */
957 if (n->nlmsg_flags&NLM_F_REPLACE)
958 ovr = 1;
959 replay:
960 ret = tcf_action_add(tca[TCA_ACT_TAB-1], n, pid, ovr);
961 if (ret == -EAGAIN)
962 goto replay;
963 break;
964 case RTM_DELACTION:
965 ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid, RTM_DELACTION);
966 break;
967 case RTM_GETACTION:
968 ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid, RTM_GETACTION);
969 break;
970 default:
971 BUG();
972 }
973
974 return ret;
975 }
976
977 static struct rtattr *
978 find_dump_kind(struct nlmsghdr *n)
979 {
980 struct rtattr *tb1, *tb2[TCA_ACT_MAX+1];
981 struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
982 struct rtattr *rta[TCAA_MAX + 1];
983 struct rtattr *kind;
984 int min_len = NLMSG_LENGTH(sizeof(struct tcamsg));
985 int attrlen = n->nlmsg_len - NLMSG_ALIGN(min_len);
986 struct rtattr *attr = (void *) n + NLMSG_ALIGN(min_len);
987
988 if (rtattr_parse(rta, TCAA_MAX, attr, attrlen) < 0)
989 return NULL;
990 tb1 = rta[TCA_ACT_TAB - 1];
991 if (tb1 == NULL)
992 return NULL;
993
994 if (rtattr_parse(tb, TCA_ACT_MAX_PRIO, RTA_DATA(tb1),
995 NLMSG_ALIGN(RTA_PAYLOAD(tb1))) < 0)
996 return NULL;
997 if (tb[0] == NULL)
998 return NULL;
999
1000 if (rtattr_parse(tb2, TCA_ACT_MAX, RTA_DATA(tb[0]),
1001 RTA_PAYLOAD(tb[0])) < 0)
1002 return NULL;
1003 kind = tb2[TCA_ACT_KIND-1];
1004
1005 return kind;
1006 }
1007
1008 static int
1009 tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1010 {
1011 struct nlmsghdr *nlh;
1012 unsigned char *b = skb_tail_pointer(skb);
1013 struct rtattr *x;
1014 struct tc_action_ops *a_o;
1015 struct tc_action a;
1016 int ret = 0;
1017 struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
1018 struct rtattr *kind = find_dump_kind(cb->nlh);
1019
1020 if (kind == NULL) {
1021 printk("tc_dump_action: action bad kind\n");
1022 return 0;
1023 }
1024
1025 a_o = tc_lookup_action(kind);
1026 if (a_o == NULL) {
1027 return 0;
1028 }
1029
1030 memset(&a, 0, sizeof(struct tc_action));
1031 a.ops = a_o;
1032
1033 if (a_o->walk == NULL) {
1034 printk("tc_dump_action: %s !capable of dumping table\n", a_o->kind);
1035 goto rtattr_failure;
1036 }
1037
1038 nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
1039 cb->nlh->nlmsg_type, sizeof(*t));
1040 t = NLMSG_DATA(nlh);
1041 t->tca_family = AF_UNSPEC;
1042 t->tca__pad1 = 0;
1043 t->tca__pad2 = 0;
1044
1045 x = (struct rtattr *)skb_tail_pointer(skb);
1046 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
1047
1048 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1049 if (ret < 0)
1050 goto rtattr_failure;
1051
1052 if (ret > 0) {
1053 x->rta_len = skb_tail_pointer(skb) - (u8 *)x;
1054 ret = skb->len;
1055 } else
1056 nlmsg_trim(skb, x);
1057
1058 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1059 if (NETLINK_CB(cb->skb).pid && ret)
1060 nlh->nlmsg_flags |= NLM_F_MULTI;
1061 module_put(a_o->owner);
1062 return skb->len;
1063
1064 rtattr_failure:
1065 nlmsg_failure:
1066 module_put(a_o->owner);
1067 nlmsg_trim(skb, b);
1068 return skb->len;
1069 }
1070
1071 static int __init tc_action_init(void)
1072 {
1073 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL);
1074 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL);
1075 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action);
1076
1077 return 0;
1078 }
1079
1080 subsys_initcall(tc_action_init);
1081
1082 EXPORT_SYMBOL(tcf_register_action);
1083 EXPORT_SYMBOL(tcf_unregister_action);
1084 EXPORT_SYMBOL(tcf_action_exec);
1085 EXPORT_SYMBOL(tcf_action_dump_1);