[NET_SCHED]: act_api: fix netlink API conversion bug
[GitHub/mt8127/android_kernel_alcatel_ttab.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;
7ba699c6 71 struct nlattr *r ;
e9ce1cd3 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;
7ba699c6
PM
86 r = (struct nlattr *)skb_tail_pointer(skb);
87 NLA_PUT(skb, a->order, 0, NULL);
e9ce1cd3
DM
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 }
7ba699c6 94 r->nla_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
7ba699c6 106nla_put_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;
7ba699c6 115 struct nlattr *r ;
e9ce1cd3
DM
116 int i= 0, n_i = 0;
117
7ba699c6
PM
118 r = (struct nlattr *)skb_tail_pointer(skb);
119 NLA_PUT(skb, a->order, 0, NULL);
120 NLA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
e9ce1cd3
DM
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 }
7ba699c6
PM
132 NLA_PUT(skb, TCA_FCNT, 4, &n_i);
133 r->nla_len = skb_tail_pointer(skb) - (u8 *)r;
e9ce1cd3
DM
134
135 return n_i;
7ba699c6 136nla_put_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
7ba699c6 214struct tcf_common *tcf_hash_create(u32 index, struct nlattr *est, struct tc_action *a, int size, int bind, u32 *idx_gen, struct tcf_hashinfo *hinfo)
e9ce1cd3
DM
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,
7ba699c6 230 &p->tcfc_lock, 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
7ba699c6
PM
308/* lookup by nlattr */
309static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
1da177e4
LT
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) {
7ba699c6 316 if (nla_strcmp(kind, a->kind) == 0) {
1da177e4
LT
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);
7ba699c6 417 struct nlattr *r;
1da177e4
LT
418
419 if (a->ops == NULL || a->ops->dump == NULL)
420 return err;
421
7ba699c6 422 NLA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
1da177e4 423 if (tcf_action_copy_stats(skb, a, 0))
7ba699c6
PM
424 goto nla_put_failure;
425 r = (struct nlattr *)skb_tail_pointer(skb);
426 NLA_PUT(skb, TCA_OPTIONS, 0, NULL);
1da177e4 427 if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) {
7ba699c6 428 r->nla_len = skb_tail_pointer(skb) - (u8 *)r;
1da177e4
LT
429 return err;
430 }
431
7ba699c6 432nla_put_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);
7ba699c6 444 struct nlattr *r ;
1da177e4
LT
445
446 while ((a = act) != NULL) {
7ba699c6 447 r = (struct nlattr *)skb_tail_pointer(skb);
1da177e4 448 act = a->next;
7ba699c6 449 NLA_PUT(skb, a->order, 0, NULL);
1da177e4
LT
450 err = tcf_action_dump_1(skb, a, bind, ref);
451 if (err < 0)
4fe683f5 452 goto errout;
7ba699c6 453 r->nla_len = skb_tail_pointer(skb) - (u8 *)r;
1da177e4
LT
454 }
455
456 return 0;
457
7ba699c6 458nla_put_failure:
4fe683f5
TG
459 err = -EINVAL;
460errout:
dc5fc579 461 nlmsg_trim(skb, b);
4fe683f5 462 return err;
1da177e4
LT
463}
464
7ba699c6 465struct tc_action *tcf_action_init_1(struct nlattr *nla, struct nlattr *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];
7ba699c6
PM
471 struct nlattr *tb[TCA_ACT_MAX+1];
472 struct nlattr *kind;
1da177e4
LT
473
474 *err = -EINVAL;
475
476 if (name == NULL) {
7ba699c6 477 if (nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL) < 0)
1da177e4 478 goto err_out;
7ba699c6 479 kind = tb[TCA_ACT_KIND];
1da177e4
LT
480 if (kind == NULL)
481 goto err_out;
7ba699c6 482 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
1da177e4
LT
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)
7ba699c6 520 *err = a_o->init(tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
1da177e4 521 else
7ba699c6 522 *err = a_o->init(nla, est, a, ovr, bind);
1da177e4
LT
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
7ba699c6 545struct tc_action *tcf_action_init(struct nlattr *nla, struct nlattr *est,
10297b99 546 char *name, int ovr, int bind, int *err)
1da177e4 547{
7ba699c6 548 struct nlattr *tb[TCA_ACT_MAX_PRIO+1];
1da177e4
LT
549 struct tc_action *head = NULL, *act, *act_prev = NULL;
550 int i;
551
7ba699c6 552 if (nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL) < 0) {
1da177e4
LT
553 *err = -EINVAL;
554 return head;
555 }
556
7ba699c6 557 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
1da177e4
LT
558 act = tcf_action_init_1(tb[i], est, name, ovr, bind, err);
559 if (act == NULL)
560 goto err;
7ba699c6 561 act->order = i;
1da177e4
LT
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);
7ba699c6 628 struct nlattr *x;
1da177e4 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
7ba699c6
PM
637 x = (struct nlattr *)skb_tail_pointer(skb);
638 NLA_PUT(skb, TCA_ACT_TAB, 0, NULL);
1da177e4
LT
639
640 if (tcf_action_dump(skb, a, bind, ref) < 0)
7ba699c6 641 goto nla_put_failure;
1da177e4 642
7ba699c6 643 x->nla_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
7ba699c6 648nla_put_failure:
1da177e4 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 *
7ba699c6 671tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 pid, int *err)
1da177e4 672{
7ba699c6 673 struct nlattr *tb[TCA_ACT_MAX+1];
1da177e4
LT
674 struct tc_action *a;
675 int index;
676
677 *err = -EINVAL;
7ba699c6 678 if (nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL) < 0)
1da177e4
LT
679 return NULL;
680
7ba699c6
PM
681 if (tb[TCA_ACT_INDEX] == NULL ||
682 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
1da177e4 683 return NULL;
7ba699c6 684 index = *(int *)nla_data(tb[TCA_ACT_INDEX]);
1da177e4
LT
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;
7ba699c6 692 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
1da177e4
LT
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
7ba699c6 734static int tca_action_flush(struct nlattr *nla, struct nlmsghdr *n, u32 pid)
1da177e4
LT
735{
736 struct sk_buff *skb;
737 unsigned char *b;
738 struct nlmsghdr *nlh;
739 struct tcamsg *t;
740 struct netlink_callback dcb;
7ba699c6
PM
741 struct nlattr *x;
742 struct nlattr *tb[TCA_ACT_MAX+1];
743 struct nlattr *kind;
1da177e4
LT
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 760
7ba699c6 761 if (nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL) < 0)
1da177e4
LT
762 goto err_out;
763
7ba699c6 764 kind = tb[TCA_ACT_KIND];
1da177e4
LT
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
7ba699c6
PM
775 x = (struct nlattr *)skb_tail_pointer(skb);
776 NLA_PUT(skb, TCA_ACT_TAB, 0, NULL);
1da177e4
LT
777
778 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
779 if (err < 0)
7ba699c6 780 goto nla_put_failure;
1da177e4 781
7ba699c6 782 x->nla_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
7ba699c6 794nla_put_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
7ba699c6 804tca_action_gd(struct nlattr *nla, struct nlmsghdr *n, u32 pid, int event)
1da177e4
LT
805{
806 int i, ret = 0;
7ba699c6 807 struct nlattr *tb[TCA_ACT_MAX_PRIO+1];
1da177e4
LT
808 struct tc_action *head = NULL, *act, *act_prev = NULL;
809
7ba699c6 810 if (nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL) < 0)
1da177e4
LT
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
7ba699c6 818 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
1da177e4
LT
819 act = tcf_action_get_1(tb[i], n, pid, &ret);
820 if (act == NULL)
821 goto err;
7ba699c6 822 act->order = i;
1da177e4
LT
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;
7ba699c6 868 struct nlattr *x;
1da177e4
LT
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
7ba699c6
PM
884 x = (struct nlattr *)skb_tail_pointer(skb);
885 NLA_PUT(skb, TCA_ACT_TAB, 0, NULL);
1da177e4
LT
886
887 if (tcf_action_dump(skb, a, 0, 0) < 0)
7ba699c6 888 goto nla_put_failure;
1da177e4 889
7ba699c6 890 x->nla_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
7ba699c6 900nla_put_failure:
1da177e4 901nlmsg_failure:
f6e57464 902 kfree_skb(skb);
1da177e4
LT
903 return -1;
904}
905
10297b99 906
1da177e4 907static int
7ba699c6 908tcf_action_add(struct nlattr *nla, struct nlmsghdr *n, u32 pid, int ovr)
1da177e4
LT
909{
910 int ret = 0;
911 struct tc_action *act;
912 struct tc_action *a;
913 u32 seq = n->nlmsg_seq;
914
7ba699c6 915 act = tcf_action_init(nla, NULL, NULL, ovr, 0, &ret);
1da177e4
LT
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;
7ba699c6 934 struct nlattr *tca[TCA_ACT_MAX + 1];
1da177e4
LT
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
7ba699c6
PM
941 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
942 if (ret < 0)
943 return ret;
944
945 if (tca[TCA_ACT_TAB] == NULL) {
1da177e4
LT
946 printk("tc_ctl_action: received NO action attribs\n");
947 return -EINVAL;
948 }
949
950 /* n->nlmsg_flags&NLM_F_CREATE
951 * */
952 switch (n->nlmsg_type) {
953 case RTM_NEWACTION:
954 /* we are going to assume all other flags
955 * imply create only if it doesnt exist
956 * Note that CREATE | EXCL implies that
957 * but since we want avoid ambiguity (eg when flags
958 * is zero) then just set this
959 */
960 if (n->nlmsg_flags&NLM_F_REPLACE)
961 ovr = 1;
962replay:
7ba699c6 963 ret = tcf_action_add(tca[TCA_ACT_TAB], n, pid, ovr);
1da177e4
LT
964 if (ret == -EAGAIN)
965 goto replay;
966 break;
967 case RTM_DELACTION:
7ba699c6 968 ret = tca_action_gd(tca[TCA_ACT_TAB], n, pid, RTM_DELACTION);
1da177e4
LT
969 break;
970 case RTM_GETACTION:
7ba699c6 971 ret = tca_action_gd(tca[TCA_ACT_TAB], n, pid, RTM_GETACTION);
1da177e4
LT
972 break;
973 default:
974 BUG();
975 }
976
977 return ret;
978}
979
7ba699c6 980static struct nlattr *
1da177e4
LT
981find_dump_kind(struct nlmsghdr *n)
982{
7ba699c6
PM
983 struct nlattr *tb1, *tb2[TCA_ACT_MAX+1];
984 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
985 struct nlattr *nla[TCAA_MAX + 1];
986 struct nlattr *kind;
1da177e4
LT
987 int min_len = NLMSG_LENGTH(sizeof(struct tcamsg));
988 int attrlen = n->nlmsg_len - NLMSG_ALIGN(min_len);
7ba699c6 989 struct nlattr *attr = (void *) n + NLMSG_ALIGN(min_len);
1da177e4 990
7ba699c6 991 if (nla_parse(nla, TCAA_MAX, attr, attrlen, NULL) < 0)
1da177e4 992 return NULL;
7ba699c6 993 tb1 = nla[TCA_ACT_TAB];
1da177e4
LT
994 if (tb1 == NULL)
995 return NULL;
996
7ba699c6
PM
997 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
998 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
1da177e4 999 return NULL;
1da177e4 1000
6d834e04
PM
1001 if (tb[1] == NULL)
1002 return NULL;
1003 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1004 nla_len(tb[1]), NULL) < 0)
1da177e4 1005 return NULL;
7ba699c6 1006 kind = tb2[TCA_ACT_KIND];
1da177e4 1007
26dab893 1008 return kind;
1da177e4
LT
1009}
1010
1011static int
1012tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1013{
b854272b 1014 struct net *net = skb->sk->sk_net;
1da177e4 1015 struct nlmsghdr *nlh;
27a884dc 1016 unsigned char *b = skb_tail_pointer(skb);
7ba699c6 1017 struct nlattr *x;
1da177e4
LT
1018 struct tc_action_ops *a_o;
1019 struct tc_action a;
1020 int ret = 0;
1021 struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
7ba699c6 1022 struct nlattr *kind = find_dump_kind(cb->nlh);
1da177e4 1023
b854272b
DL
1024 if (net != &init_net)
1025 return 0;
1026
1da177e4
LT
1027 if (kind == NULL) {
1028 printk("tc_dump_action: action bad kind\n");
1029 return 0;
1030 }
1031
26dab893 1032 a_o = tc_lookup_action(kind);
1da177e4 1033 if (a_o == NULL) {
1da177e4
LT
1034 return 0;
1035 }
1036
1037 memset(&a, 0, sizeof(struct tc_action));
1038 a.ops = a_o;
1039
1040 if (a_o->walk == NULL) {
26dab893 1041 printk("tc_dump_action: %s !capable of dumping table\n", a_o->kind);
7ba699c6 1042 goto nla_put_failure;
1da177e4
LT
1043 }
1044
1045 nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
10297b99 1046 cb->nlh->nlmsg_type, sizeof(*t));
1da177e4
LT
1047 t = NLMSG_DATA(nlh);
1048 t->tca_family = AF_UNSPEC;
9ef1d4c7
PM
1049 t->tca__pad1 = 0;
1050 t->tca__pad2 = 0;
1da177e4 1051
7ba699c6
PM
1052 x = (struct nlattr *)skb_tail_pointer(skb);
1053 NLA_PUT(skb, TCA_ACT_TAB, 0, NULL);
1da177e4
LT
1054
1055 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1056 if (ret < 0)
7ba699c6 1057 goto nla_put_failure;
1da177e4
LT
1058
1059 if (ret > 0) {
7ba699c6 1060 x->nla_len = skb_tail_pointer(skb) - (u8 *)x;
1da177e4
LT
1061 ret = skb->len;
1062 } else
dc5fc579 1063 nlmsg_trim(skb, x);
1da177e4 1064
27a884dc 1065 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1da177e4
LT
1066 if (NETLINK_CB(cb->skb).pid && ret)
1067 nlh->nlmsg_flags |= NLM_F_MULTI;
1068 module_put(a_o->owner);
1069 return skb->len;
1070
7ba699c6 1071nla_put_failure:
1da177e4
LT
1072nlmsg_failure:
1073 module_put(a_o->owner);
dc5fc579 1074 nlmsg_trim(skb, b);
1da177e4
LT
1075 return skb->len;
1076}
1077
1078static int __init tc_action_init(void)
1079{
708914cc
TG
1080 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL);
1081 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL);
1082 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action);
1da177e4 1083
1da177e4
LT
1084 return 0;
1085}
1086
1087subsys_initcall(tc_action_init);