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