blob: 72bb9bd1a22af5baa939036818e647a457eff278 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/types.h>
18#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070033#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
David S. Millere9ce1cd2006-08-21 23:54:55 -070035void 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);
David S. Millere9ce1cd2006-08-21 23:54:55 -070045 gen_kill_estimator(&p->tcfc_bstats,
46 &p->tcfc_rate_est);
David S. Millere9ce1cd2006-08-21 23:54:55 -070047 kfree(p);
48 return;
49 }
50 }
51 BUG_TRAP(0);
52}
53EXPORT_SYMBOL(tcf_hash_destroy);
54
55int 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--;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090065 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070066 tcf_hash_destroy(p, hinfo);
67 ret = 1;
68 }
69 }
70 return ret;
71}
72EXPORT_SYMBOL(tcf_hash_release);
73
74static 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;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070094 r = (struct rtattr *)skb_tail_pointer(skb);
David S. Millere9ce1cd2006-08-21 23:54:55 -070095 RTA_PUT(skb, a->order, 0, NULL);
96 err = tcf_action_dump_1(skb, a, 0, 0);
97 if (err < 0) {
98 index--;
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070099 nlmsg_trim(skb, r);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700100 goto done;
101 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700102 r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700103 n_i++;
104 if (n_i >= TCA_ACT_MAX_PRIO)
105 goto done;
106 }
107 }
108done:
109 read_unlock(hinfo->lock);
110 if (n_i)
111 cb->args[0] += n_i;
112 return n_i;
113
114rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700115 nlmsg_trim(skb, r);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700116 goto done;
117}
118
119static 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
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700126 r = (struct rtattr *)skb_tail_pointer(skb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700127 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);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700141 r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700142
143 return n_i;
144rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700145 nlmsg_trim(skb, r);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700146 return -EINVAL;
147}
148
149int 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}
163EXPORT_SYMBOL(tcf_generic_walker);
164
165struct 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}
179EXPORT_SYMBOL(tcf_hash_lookup);
180
181u32 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}
192EXPORT_SYMBOL(tcf_hash_new_index);
193
194int 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}
205EXPORT_SYMBOL(tcf_hash_search);
206
207struct 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}
220EXPORT_SYMBOL(tcf_hash_check);
221
222struct 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;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700237 if (est)
238 gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
239 p->tcfc_stats_lock, est);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700240 a->priv = (void *) p;
241 return p;
242}
243EXPORT_SYMBOL(tcf_hash_create);
244
245void 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}
254EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256static struct tc_action_ops *act_base = NULL;
257static DEFINE_RWLOCK(act_mod_lock);
258
259int 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
276int 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 */
295static 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 */
316static 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 */
338static 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
359int tcf_action_exec(struct sk_buff *skb, struct tc_action *act,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900360 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 ret = TC_ACT_OK;
368 goto exec_done;
369 }
370 while ((a = act) != NULL) {
371repeat:
372 if (a->ops && a->ops->act) {
Patrick McHardyf43c5a02006-01-08 22:15:34 -0800373 ret = a->ops->act(skb, a, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (ret == TC_ACT_REPEAT)
380 goto repeat; /* we need a ttl - JHS */
J Hadi Salim14d50e72005-05-03 16:29:13 -0700381 if (ret != TC_ACT_PIPE)
382 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384 act = a->next;
385 }
386exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return ret;
388}
389
390void 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 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
408int
409tcf_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
418int
419tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
420{
421 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700422 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 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;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700431 r = (struct rtattr *)skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
433 if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) {
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700434 r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return err;
436 }
437
438rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700439 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return -1;
441}
442
443int
444tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref)
445{
446 struct tc_action *a;
447 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700448 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 struct rtattr *r ;
450
451 while ((a = act) != NULL) {
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700452 r = (struct rtattr *)skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 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)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700457 goto errout;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700458 r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
461 return 0;
462
463rtattr_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700464 err = -EINVAL;
465errout:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700466 nlmsg_trim(skb, b);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700467 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
470struct tc_action *tcf_action_init_1(struct rtattr *rta, struct rtattr *est,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900471 char *name, int ovr, int bind, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
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();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800498 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 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
Thomas Grafd152b4e2006-07-05 20:45:57 -0700514 *err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 goto err_out;
516 }
517
518 *err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700519 a = kzalloc(sizeof(*a), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (a == NULL)
521 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 *err = 0;
540 return a;
541
542err_free:
543 kfree(a);
544err_mod:
545 module_put(a_o->owner);
546err_out:
547 return NULL;
548}
549
550struct tc_action *tcf_action_init(struct rtattr *rta, struct rtattr *est,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900551 char *name, int ovr, int bind, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
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
576err:
577 if (head != NULL)
578 tcf_action_destroy(head, bind);
579 return NULL;
580}
581
582int 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;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 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,
David S. Millere9ce1cd2006-08-21 23:54:55 -0700598 TCA_STATS, TCA_XSTATS, h->tcf_stats_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 else
600 return 0;
601 } else
602 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
David S. Millere9ce1cd2006-08-21 23:54:55 -0700603 h->tcf_stats_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
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
David S. Millere9ce1cd2006-08-21 23:54:55 -0700612 if (gnet_stats_copy_basic(&d, &h->tcf_bstats) < 0 ||
David S. Millere9ce1cd2006-08-21 23:54:55 -0700613 gnet_stats_copy_rate_est(&d, &h->tcf_rate_est) < 0 ||
David S. Millere9ce1cd2006-08-21 23:54:55 -0700614 gnet_stats_copy_queue(&d, &h->tcf_qstats) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 goto errout;
616
617 if (gnet_stats_finish_copy(&d) < 0)
618 goto errout;
619
620 return 0;
621
622errout:
623 return -1;
624}
625
626static int
627tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, u32 seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900628 u16 flags, int event, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 struct tcamsg *t;
631 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700632 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 struct rtattr *x;
634
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700635 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 t = NLMSG_DATA(nlh);
638 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700639 t->tca__pad1 = 0;
640 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900641
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700642 x = (struct rtattr *)skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
644
645 if (tcf_action_dump(skb, a, bind, ref) < 0)
646 goto rtattr_failure;
647
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700648 x->rta_len = skb_tail_pointer(skb) - (u8 *)x;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900649
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700650 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return skb->len;
652
653rtattr_failure:
654nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700655 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return -1;
657}
658
659static int
660act_get_notify(u32 pid, struct nlmsghdr *n, struct tc_action *a, int event)
661{
662 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
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 }
Thomas Graf2942e902006-08-15 00:30:25 -0700671
672 return rtnl_unicast(skb, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
675static struct tc_action *
676tcf_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;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700692 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (a == NULL)
694 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
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;
709err_mod:
710 module_put(a->ops->owner);
711err_free:
712 kfree(a);
713 return NULL;
714}
715
716static 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
726static struct tc_action *create_a(int i)
727{
728 struct tc_action *act;
729
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700730 act = kzalloc(sizeof(*act), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (act == NULL) {
732 printk("create_a: failed to alloc!\n");
733 return NULL;
734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 act->order = i;
736 return act;
737}
738
739static 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
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700764 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
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;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700777 t->tca__pad1 = 0;
778 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700780 x = (struct rtattr *)skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 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
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700787 x->rta_len = skb_tail_pointer(skb) - (u8 *)x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700789 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 nlh->nlmsg_flags |= NLM_F_ROOT;
791 module_put(a->ops->owner);
792 kfree(a);
Patrick McHardyac6d4392005-08-14 19:29:52 -0700793 err = rtnetlink_send(skb, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (err > 0)
795 return 0;
796
797 return err;
798
799rtattr_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800nlmsg_failure:
Thomas Grafebbaeab2006-07-09 11:36:23 -0700801 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802err_out:
803 kfree_skb(skb);
804 kfree(a);
805 return err;
806}
807
808static int
809tca_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,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900848 0, 1) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 kfree_skb(skb);
850 ret = -EINVAL;
851 goto err;
852 }
853
854 /* now do the delete */
855 tcf_action_destroy(head, 0);
Patrick McHardyac6d4392005-08-14 19:29:52 -0700856 ret = rtnetlink_send(skb, pid, RTNLGRP_TC,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900857 n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (ret > 0)
859 return 0;
860 return ret;
861 }
862err:
863 cleanup_a(head);
864 return ret;
865}
866
867static int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900868 u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
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
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700881 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700883 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 t = NLMSG_DATA(nlh);
885 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700886 t->tca__pad1 = 0;
887 t->tca__pad2 = 0;
888
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700889 x = (struct rtattr *)skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
891
892 if (tcf_action_dump(skb, a, 0, 0) < 0)
893 goto rtattr_failure;
894
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700895 x->rta_len = skb_tail_pointer(skb) - (u8 *)x;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900896
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700897 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Patrick McHardyac6d4392005-08-14 19:29:52 -0700898 NETLINK_CB(skb).dst_group = RTNLGRP_TC;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900899
Patrick McHardyac6d4392005-08-14 19:29:52 -0700900 err = rtnetlink_send(skb, pid, RTNLGRP_TC, flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (err > 0)
902 err = 0;
903 return err;
904
905rtattr_failure:
906nlmsg_failure:
Patrick McHardyf6e57462006-03-12 20:33:22 -0800907 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return -1;
909}
910
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912static int
913tcf_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 }
932done:
933 return ret;
934}
935
936static 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;
959replay:
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
Thomas Graf26dab892006-07-05 20:45:06 -0700977static struct rtattr *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978find_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),
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900995 NLMSG_ALIGN(RTA_PAYLOAD(tb1))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return NULL;
997 if (tb[0] == NULL)
998 return NULL;
999
1000 if (rtattr_parse(tb2, TCA_ACT_MAX, RTA_DATA(tb[0]),
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001001 RTA_PAYLOAD(tb[0])) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return NULL;
1003 kind = tb2[TCA_ACT_KIND-1];
1004
Thomas Graf26dab892006-07-05 20:45:06 -07001005 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
1008static int
1009tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1010{
1011 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001012 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 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);
Thomas Graf26dab892006-07-05 20:45:06 -07001018 struct rtattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 if (kind == NULL) {
1021 printk("tc_dump_action: action bad kind\n");
1022 return 0;
1023 }
1024
Thomas Graf26dab892006-07-05 20:45:06 -07001025 a_o = tc_lookup_action(kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (a_o == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return 0;
1028 }
1029
1030 memset(&a, 0, sizeof(struct tc_action));
1031 a.ops = a_o;
1032
1033 if (a_o->walk == NULL) {
Thomas Graf26dab892006-07-05 20:45:06 -07001034 printk("tc_dump_action: %s !capable of dumping table\n", a_o->kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 goto rtattr_failure;
1036 }
1037
1038 nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001039 cb->nlh->nlmsg_type, sizeof(*t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 t = NLMSG_DATA(nlh);
1041 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001042 t->tca__pad1 = 0;
1043 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001045 x = (struct rtattr *)skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 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) {
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001053 x->rta_len = skb_tail_pointer(skb) - (u8 *)x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 ret = skb->len;
1055 } else
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001056 nlmsg_trim(skb, x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001058 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 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
1064rtattr_failure:
1065nlmsg_failure:
1066 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001067 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return skb->len;
1069}
1070
1071static int __init tc_action_init(void)
1072{
Thomas Graf708914c2007-03-22 11:56:59 -07001073 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return 0;
1078}
1079
1080subsys_initcall(tc_action_init);
1081
1082EXPORT_SYMBOL(tcf_register_action);
1083EXPORT_SYMBOL(tcf_unregister_action);
1084EXPORT_SYMBOL(tcf_action_exec);
1085EXPORT_SYMBOL(tcf_action_dump_1);