blob: feef366cad5dab0ad6f36a18ae0ec7203b1f1641 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/types.h>
15#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/init.h>
20#include <linux/kmod.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <net/sch_generic.h>
22#include <net/act_api.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070023#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
David S. Millere9ce1cd2006-08-21 23:54:55 -070025void tcf_hash_destroy(struct tcf_common *p, struct tcf_hashinfo *hinfo)
26{
27 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
28 struct tcf_common **p1p;
29
30 for (p1p = &hinfo->htab[h]; *p1p; p1p = &(*p1p)->tcfc_next) {
31 if (*p1p == p) {
32 write_lock_bh(hinfo->lock);
33 *p1p = p->tcfc_next;
34 write_unlock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -070035 gen_kill_estimator(&p->tcfc_bstats,
36 &p->tcfc_rate_est);
David S. Millere9ce1cd2006-08-21 23:54:55 -070037 kfree(p);
38 return;
39 }
40 }
41 BUG_TRAP(0);
42}
43EXPORT_SYMBOL(tcf_hash_destroy);
44
45int tcf_hash_release(struct tcf_common *p, int bind,
46 struct tcf_hashinfo *hinfo)
47{
48 int ret = 0;
49
50 if (p) {
51 if (bind)
52 p->tcfc_bindcnt--;
53
54 p->tcfc_refcnt--;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090055 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070056 tcf_hash_destroy(p, hinfo);
57 ret = 1;
58 }
59 }
60 return ret;
61}
62EXPORT_SYMBOL(tcf_hash_release);
63
64static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
65 struct tc_action *a, struct tcf_hashinfo *hinfo)
66{
67 struct tcf_common *p;
68 int err = 0, index = -1,i = 0, s_i = 0, n_i = 0;
69 struct rtattr *r ;
70
71 read_lock(hinfo->lock);
72
73 s_i = cb->args[0];
74
75 for (i = 0; i < (hinfo->hmask + 1); i++) {
76 p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
77
78 for (; p; p = p->tcfc_next) {
79 index++;
80 if (index < s_i)
81 continue;
82 a->priv = p;
83 a->order = n_i;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070084 r = (struct rtattr *)skb_tail_pointer(skb);
David S. Millere9ce1cd2006-08-21 23:54:55 -070085 RTA_PUT(skb, a->order, 0, NULL);
86 err = tcf_action_dump_1(skb, a, 0, 0);
87 if (err < 0) {
88 index--;
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070089 nlmsg_trim(skb, r);
David S. Millere9ce1cd2006-08-21 23:54:55 -070090 goto done;
91 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070092 r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
David S. Millere9ce1cd2006-08-21 23:54:55 -070093 n_i++;
94 if (n_i >= TCA_ACT_MAX_PRIO)
95 goto done;
96 }
97 }
98done:
99 read_unlock(hinfo->lock);
100 if (n_i)
101 cb->args[0] += n_i;
102 return n_i;
103
104rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700105 nlmsg_trim(skb, r);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700106 goto done;
107}
108
109static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a,
110 struct tcf_hashinfo *hinfo)
111{
112 struct tcf_common *p, *s_p;
113 struct rtattr *r ;
114 int i= 0, n_i = 0;
115
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700116 r = (struct rtattr *)skb_tail_pointer(skb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700117 RTA_PUT(skb, a->order, 0, NULL);
118 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
119 for (i = 0; i < (hinfo->hmask + 1); i++) {
120 p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
121
122 while (p != NULL) {
123 s_p = p->tcfc_next;
124 if (ACT_P_DELETED == tcf_hash_release(p, 0, hinfo))
125 module_put(a->ops->owner);
126 n_i++;
127 p = s_p;
128 }
129 }
130 RTA_PUT(skb, TCA_FCNT, 4, &n_i);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700131 r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700132
133 return n_i;
134rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700135 nlmsg_trim(skb, r);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700136 return -EINVAL;
137}
138
139int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
140 int type, struct tc_action *a)
141{
142 struct tcf_hashinfo *hinfo = a->ops->hinfo;
143
144 if (type == RTM_DELACTION) {
145 return tcf_del_walker(skb, a, hinfo);
146 } else if (type == RTM_GETACTION) {
147 return tcf_dump_walker(skb, cb, a, hinfo);
148 } else {
149 printk("tcf_generic_walker: unknown action %d\n", type);
150 return -EINVAL;
151 }
152}
153EXPORT_SYMBOL(tcf_generic_walker);
154
155struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
156{
157 struct tcf_common *p;
158
159 read_lock(hinfo->lock);
160 for (p = hinfo->htab[tcf_hash(index, hinfo->hmask)]; p;
161 p = p->tcfc_next) {
162 if (p->tcfc_index == index)
163 break;
164 }
165 read_unlock(hinfo->lock);
166
167 return p;
168}
169EXPORT_SYMBOL(tcf_hash_lookup);
170
171u32 tcf_hash_new_index(u32 *idx_gen, struct tcf_hashinfo *hinfo)
172{
173 u32 val = *idx_gen;
174
175 do {
176 if (++val == 0)
177 val = 1;
178 } while (tcf_hash_lookup(val, hinfo));
179
180 return (*idx_gen = val);
181}
182EXPORT_SYMBOL(tcf_hash_new_index);
183
184int tcf_hash_search(struct tc_action *a, u32 index)
185{
186 struct tcf_hashinfo *hinfo = a->ops->hinfo;
187 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
188
189 if (p) {
190 a->priv = p;
191 return 1;
192 }
193 return 0;
194}
195EXPORT_SYMBOL(tcf_hash_search);
196
197struct tcf_common *tcf_hash_check(u32 index, struct tc_action *a, int bind,
198 struct tcf_hashinfo *hinfo)
199{
200 struct tcf_common *p = NULL;
201 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
202 if (bind) {
203 p->tcfc_bindcnt++;
204 p->tcfc_refcnt++;
205 }
206 a->priv = p;
207 }
208 return p;
209}
210EXPORT_SYMBOL(tcf_hash_check);
211
212struct 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)
213{
214 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
215
216 if (unlikely(!p))
217 return p;
218 p->tcfc_refcnt = 1;
219 if (bind)
220 p->tcfc_bindcnt = 1;
221
222 spin_lock_init(&p->tcfc_lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700223 p->tcfc_index = index ? index : tcf_hash_new_index(idx_gen, hinfo);
224 p->tcfc_tm.install = jiffies;
225 p->tcfc_tm.lastuse = jiffies;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700226 if (est)
227 gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
Patrick McHardy4bdf3992007-07-02 22:47:37 -0700228 &p->tcfc_lock, est);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700229 a->priv = (void *) p;
230 return p;
231}
232EXPORT_SYMBOL(tcf_hash_create);
233
234void tcf_hash_insert(struct tcf_common *p, struct tcf_hashinfo *hinfo)
235{
236 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
237
238 write_lock_bh(hinfo->lock);
239 p->tcfc_next = hinfo->htab[h];
240 hinfo->htab[h] = p;
241 write_unlock_bh(hinfo->lock);
242}
243EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245static struct tc_action_ops *act_base = NULL;
246static DEFINE_RWLOCK(act_mod_lock);
247
248int tcf_register_action(struct tc_action_ops *act)
249{
250 struct tc_action_ops *a, **ap;
251
252 write_lock(&act_mod_lock);
253 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next) {
254 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
255 write_unlock(&act_mod_lock);
256 return -EEXIST;
257 }
258 }
259 act->next = NULL;
260 *ap = act;
261 write_unlock(&act_mod_lock);
262 return 0;
263}
264
265int tcf_unregister_action(struct tc_action_ops *act)
266{
267 struct tc_action_ops *a, **ap;
268 int err = -ENOENT;
269
270 write_lock(&act_mod_lock);
271 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next)
272 if (a == act)
273 break;
274 if (a) {
275 *ap = a->next;
276 a->next = NULL;
277 err = 0;
278 }
279 write_unlock(&act_mod_lock);
280 return err;
281}
282
283/* lookup by name */
284static struct tc_action_ops *tc_lookup_action_n(char *kind)
285{
286 struct tc_action_ops *a = NULL;
287
288 if (kind) {
289 read_lock(&act_mod_lock);
290 for (a = act_base; a; a = a->next) {
291 if (strcmp(kind, a->kind) == 0) {
292 if (!try_module_get(a->owner)) {
293 read_unlock(&act_mod_lock);
294 return NULL;
295 }
296 break;
297 }
298 }
299 read_unlock(&act_mod_lock);
300 }
301 return a;
302}
303
304/* lookup by rtattr */
305static struct tc_action_ops *tc_lookup_action(struct rtattr *kind)
306{
307 struct tc_action_ops *a = NULL;
308
309 if (kind) {
310 read_lock(&act_mod_lock);
311 for (a = act_base; a; a = a->next) {
312 if (rtattr_strcmp(kind, a->kind) == 0) {
313 if (!try_module_get(a->owner)) {
314 read_unlock(&act_mod_lock);
315 return NULL;
316 }
317 break;
318 }
319 }
320 read_unlock(&act_mod_lock);
321 }
322 return a;
323}
324
325#if 0
326/* lookup by id */
327static struct tc_action_ops *tc_lookup_action_id(u32 type)
328{
329 struct tc_action_ops *a = NULL;
330
331 if (type) {
332 read_lock(&act_mod_lock);
333 for (a = act_base; a; a = a->next) {
334 if (a->type == type) {
335 if (!try_module_get(a->owner)) {
336 read_unlock(&act_mod_lock);
337 return NULL;
338 }
339 break;
340 }
341 }
342 read_unlock(&act_mod_lock);
343 }
344 return a;
345}
346#endif
347
348int tcf_action_exec(struct sk_buff *skb, struct tc_action *act,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900349 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 struct tc_action *a;
352 int ret = -1;
353
354 if (skb->tc_verd & TC_NCLS) {
355 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 ret = TC_ACT_OK;
357 goto exec_done;
358 }
359 while ((a = act) != NULL) {
360repeat:
361 if (a->ops && a->ops->act) {
Patrick McHardyf43c5a02006-01-08 22:15:34 -0800362 ret = a->ops->act(skb, a, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (TC_MUNGED & skb->tc_verd) {
364 /* copied already, allow trampling */
365 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
366 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (ret == TC_ACT_REPEAT)
369 goto repeat; /* we need a ttl - JHS */
J Hadi Salim14d50e72005-05-03 16:29:13 -0700370 if (ret != TC_ACT_PIPE)
371 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373 act = a->next;
374 }
375exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return ret;
377}
378
379void tcf_action_destroy(struct tc_action *act, int bind)
380{
381 struct tc_action *a;
382
383 for (a = act; a; a = act) {
384 if (a->ops && a->ops->cleanup) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
386 module_put(a->ops->owner);
387 act = act->next;
388 kfree(a);
389 } else { /*FIXME: Remove later - catch insertion bugs*/
390 printk("tcf_action_destroy: BUG? destroying NULL ops\n");
391 act = act->next;
392 kfree(a);
393 }
394 }
395}
396
397int
398tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
399{
400 int err = -EINVAL;
401
402 if (a->ops == NULL || a->ops->dump == NULL)
403 return err;
404 return a->ops->dump(skb, a, bind, ref);
405}
406
407int
408tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
409{
410 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700411 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 struct rtattr *r;
413
414 if (a->ops == NULL || a->ops->dump == NULL)
415 return err;
416
417 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
418 if (tcf_action_copy_stats(skb, a, 0))
419 goto rtattr_failure;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700420 r = (struct rtattr *)skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
422 if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) {
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700423 r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return err;
425 }
426
427rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700428 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return -1;
430}
431
432int
433tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref)
434{
435 struct tc_action *a;
436 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700437 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 struct rtattr *r ;
439
440 while ((a = act) != NULL) {
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700441 r = (struct rtattr *)skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 act = a->next;
443 RTA_PUT(skb, a->order, 0, NULL);
444 err = tcf_action_dump_1(skb, a, bind, ref);
445 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700446 goto errout;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700447 r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449
450 return 0;
451
452rtattr_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700453 err = -EINVAL;
454errout:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700455 nlmsg_trim(skb, b);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700456 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459struct tc_action *tcf_action_init_1(struct rtattr *rta, struct rtattr *est,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900460 char *name, int ovr, int bind, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 struct tc_action *a;
463 struct tc_action_ops *a_o;
464 char act_name[IFNAMSIZ];
465 struct rtattr *tb[TCA_ACT_MAX+1];
466 struct rtattr *kind;
467
468 *err = -EINVAL;
469
470 if (name == NULL) {
471 if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
472 goto err_out;
473 kind = tb[TCA_ACT_KIND-1];
474 if (kind == NULL)
475 goto err_out;
476 if (rtattr_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
477 goto err_out;
478 } else {
479 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
480 goto err_out;
481 }
482
483 a_o = tc_lookup_action_n(act_name);
484 if (a_o == NULL) {
485#ifdef CONFIG_KMOD
486 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800487 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 rtnl_lock();
489
490 a_o = tc_lookup_action_n(act_name);
491
492 /* We dropped the RTNL semaphore in order to
493 * perform the module load. So, even if we
494 * succeeded in loading the module we have to
495 * tell the caller to replay the request. We
496 * indicate this using -EAGAIN.
497 */
498 if (a_o != NULL) {
499 *err = -EAGAIN;
500 goto err_mod;
501 }
502#endif
Thomas Grafd152b4e2006-07-05 20:45:57 -0700503 *err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 goto err_out;
505 }
506
507 *err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700508 a = kzalloc(sizeof(*a), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (a == NULL)
510 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 /* backward compatibility for policer */
513 if (name == NULL)
514 *err = a_o->init(tb[TCA_ACT_OPTIONS-1], est, a, ovr, bind);
515 else
516 *err = a_o->init(rta, est, a, ovr, bind);
517 if (*err < 0)
518 goto err_free;
519
520 /* module count goes up only when brand new policy is created
521 if it exists and is only bound to in a_o->init() then
522 ACT_P_CREATED is not returned (a zero is).
523 */
524 if (*err != ACT_P_CREATED)
525 module_put(a_o->owner);
526 a->ops = a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 *err = 0;
529 return a;
530
531err_free:
532 kfree(a);
533err_mod:
534 module_put(a_o->owner);
535err_out:
536 return NULL;
537}
538
539struct tc_action *tcf_action_init(struct rtattr *rta, struct rtattr *est,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900540 char *name, int ovr, int bind, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
543 struct tc_action *head = NULL, *act, *act_prev = NULL;
544 int i;
545
546 if (rtattr_parse_nested(tb, TCA_ACT_MAX_PRIO, rta) < 0) {
547 *err = -EINVAL;
548 return head;
549 }
550
551 for (i=0; i < TCA_ACT_MAX_PRIO && tb[i]; i++) {
552 act = tcf_action_init_1(tb[i], est, name, ovr, bind, err);
553 if (act == NULL)
554 goto err;
555 act->order = i+1;
556
557 if (head == NULL)
558 head = act;
559 else
560 act_prev->next = act;
561 act_prev = act;
562 }
563 return head;
564
565err:
566 if (head != NULL)
567 tcf_action_destroy(head, bind);
568 return NULL;
569}
570
571int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
572 int compat_mode)
573{
574 int err = 0;
575 struct gnet_dump d;
576 struct tcf_act_hdr *h = a->priv;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (h == NULL)
579 goto errout;
580
581 /* compat_mode being true specifies a call that is supposed
582 * to add additional backward compatiblity statistic TLVs.
583 */
584 if (compat_mode) {
585 if (a->type == TCA_OLD_COMPAT)
586 err = gnet_stats_start_copy_compat(skb, 0,
Patrick McHardy4bdf3992007-07-02 22:47:37 -0700587 TCA_STATS, TCA_XSTATS, &h->tcf_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 else
589 return 0;
590 } else
591 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
Patrick McHardy4bdf3992007-07-02 22:47:37 -0700592 &h->tcf_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 if (err < 0)
595 goto errout;
596
597 if (a->ops != NULL && a->ops->get_stats != NULL)
598 if (a->ops->get_stats(skb, a) < 0)
599 goto errout;
600
David S. Millere9ce1cd2006-08-21 23:54:55 -0700601 if (gnet_stats_copy_basic(&d, &h->tcf_bstats) < 0 ||
David S. Millere9ce1cd2006-08-21 23:54:55 -0700602 gnet_stats_copy_rate_est(&d, &h->tcf_rate_est) < 0 ||
David S. Millere9ce1cd2006-08-21 23:54:55 -0700603 gnet_stats_copy_queue(&d, &h->tcf_qstats) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 goto errout;
605
606 if (gnet_stats_finish_copy(&d) < 0)
607 goto errout;
608
609 return 0;
610
611errout:
612 return -1;
613}
614
615static int
616tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, u32 seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900617 u16 flags, int event, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 struct tcamsg *t;
620 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700621 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 struct rtattr *x;
623
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700624 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 t = NLMSG_DATA(nlh);
627 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700628 t->tca__pad1 = 0;
629 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900630
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700631 x = (struct rtattr *)skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
633
634 if (tcf_action_dump(skb, a, bind, ref) < 0)
635 goto rtattr_failure;
636
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700637 x->rta_len = skb_tail_pointer(skb) - (u8 *)x;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900638
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700639 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return skb->len;
641
642rtattr_failure:
643nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700644 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return -1;
646}
647
648static int
649act_get_notify(u32 pid, struct nlmsghdr *n, struct tc_action *a, int event)
650{
651 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
654 if (!skb)
655 return -ENOBUFS;
656 if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
657 kfree_skb(skb);
658 return -EINVAL;
659 }
Thomas Graf2942e902006-08-15 00:30:25 -0700660
661 return rtnl_unicast(skb, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
664static struct tc_action *
665tcf_action_get_1(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int *err)
666{
667 struct rtattr *tb[TCA_ACT_MAX+1];
668 struct tc_action *a;
669 int index;
670
671 *err = -EINVAL;
672 if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
673 return NULL;
674
675 if (tb[TCA_ACT_INDEX - 1] == NULL ||
676 RTA_PAYLOAD(tb[TCA_ACT_INDEX - 1]) < sizeof(index))
677 return NULL;
678 index = *(int *)RTA_DATA(tb[TCA_ACT_INDEX - 1]);
679
680 *err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700681 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (a == NULL)
683 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 *err = -EINVAL;
686 a->ops = tc_lookup_action(tb[TCA_ACT_KIND - 1]);
687 if (a->ops == NULL)
688 goto err_free;
689 if (a->ops->lookup == NULL)
690 goto err_mod;
691 *err = -ENOENT;
692 if (a->ops->lookup(a, index) == 0)
693 goto err_mod;
694
695 module_put(a->ops->owner);
696 *err = 0;
697 return a;
698err_mod:
699 module_put(a->ops->owner);
700err_free:
701 kfree(a);
702 return NULL;
703}
704
705static void cleanup_a(struct tc_action *act)
706{
707 struct tc_action *a;
708
709 for (a = act; a; a = act) {
710 act = a->next;
711 kfree(a);
712 }
713}
714
715static struct tc_action *create_a(int i)
716{
717 struct tc_action *act;
718
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700719 act = kzalloc(sizeof(*act), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (act == NULL) {
721 printk("create_a: failed to alloc!\n");
722 return NULL;
723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 act->order = i;
725 return act;
726}
727
728static int tca_action_flush(struct rtattr *rta, struct nlmsghdr *n, u32 pid)
729{
730 struct sk_buff *skb;
731 unsigned char *b;
732 struct nlmsghdr *nlh;
733 struct tcamsg *t;
734 struct netlink_callback dcb;
735 struct rtattr *x;
736 struct rtattr *tb[TCA_ACT_MAX+1];
737 struct rtattr *kind;
738 struct tc_action *a = create_a(0);
739 int err = -EINVAL;
740
741 if (a == NULL) {
742 printk("tca_action_flush: couldnt create tc_action\n");
743 return err;
744 }
745
746 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
747 if (!skb) {
748 printk("tca_action_flush: failed skb alloc\n");
749 kfree(a);
750 return -ENOBUFS;
751 }
752
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700753 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
756 goto err_out;
757
758 kind = tb[TCA_ACT_KIND-1];
759 a->ops = tc_lookup_action(kind);
760 if (a->ops == NULL)
761 goto err_out;
762
763 nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t));
764 t = NLMSG_DATA(nlh);
765 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700766 t->tca__pad1 = 0;
767 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700769 x = (struct rtattr *)skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
771
772 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
773 if (err < 0)
774 goto rtattr_failure;
775
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700776 x->rta_len = skb_tail_pointer(skb) - (u8 *)x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700778 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 nlh->nlmsg_flags |= NLM_F_ROOT;
780 module_put(a->ops->owner);
781 kfree(a);
Patrick McHardyac6d4392005-08-14 19:29:52 -0700782 err = rtnetlink_send(skb, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (err > 0)
784 return 0;
785
786 return err;
787
788rtattr_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789nlmsg_failure:
Thomas Grafebbaeab2006-07-09 11:36:23 -0700790 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791err_out:
792 kfree_skb(skb);
793 kfree(a);
794 return err;
795}
796
797static int
798tca_action_gd(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int event)
799{
800 int i, ret = 0;
801 struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
802 struct tc_action *head = NULL, *act, *act_prev = NULL;
803
804 if (rtattr_parse_nested(tb, TCA_ACT_MAX_PRIO, rta) < 0)
805 return -EINVAL;
806
807 if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) {
808 if (tb[0] != NULL && tb[1] == NULL)
809 return tca_action_flush(tb[0], n, pid);
810 }
811
812 for (i=0; i < TCA_ACT_MAX_PRIO && tb[i]; i++) {
813 act = tcf_action_get_1(tb[i], n, pid, &ret);
814 if (act == NULL)
815 goto err;
816 act->order = i+1;
817
818 if (head == NULL)
819 head = act;
820 else
821 act_prev->next = act;
822 act_prev = act;
823 }
824
825 if (event == RTM_GETACTION)
826 ret = act_get_notify(pid, n, head, event);
827 else { /* delete */
828 struct sk_buff *skb;
829
830 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
831 if (!skb) {
832 ret = -ENOBUFS;
833 goto err;
834 }
835
836 if (tca_get_fill(skb, head, pid, n->nlmsg_seq, 0, event,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900837 0, 1) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 kfree_skb(skb);
839 ret = -EINVAL;
840 goto err;
841 }
842
843 /* now do the delete */
844 tcf_action_destroy(head, 0);
Patrick McHardyac6d4392005-08-14 19:29:52 -0700845 ret = rtnetlink_send(skb, pid, RTNLGRP_TC,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900846 n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (ret > 0)
848 return 0;
849 return ret;
850 }
851err:
852 cleanup_a(head);
853 return ret;
854}
855
856static int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900857 u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 struct tcamsg *t;
860 struct nlmsghdr *nlh;
861 struct sk_buff *skb;
862 struct rtattr *x;
863 unsigned char *b;
864 int err = 0;
865
866 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
867 if (!skb)
868 return -ENOBUFS;
869
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700870 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700872 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 t = NLMSG_DATA(nlh);
874 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700875 t->tca__pad1 = 0;
876 t->tca__pad2 = 0;
877
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700878 x = (struct rtattr *)skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
880
881 if (tcf_action_dump(skb, a, 0, 0) < 0)
882 goto rtattr_failure;
883
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700884 x->rta_len = skb_tail_pointer(skb) - (u8 *)x;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900885
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700886 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Patrick McHardyac6d4392005-08-14 19:29:52 -0700887 NETLINK_CB(skb).dst_group = RTNLGRP_TC;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900888
Patrick McHardyac6d4392005-08-14 19:29:52 -0700889 err = rtnetlink_send(skb, pid, RTNLGRP_TC, flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (err > 0)
891 err = 0;
892 return err;
893
894rtattr_failure:
895nlmsg_failure:
Patrick McHardyf6e57462006-03-12 20:33:22 -0800896 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return -1;
898}
899
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901static int
902tcf_action_add(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int ovr)
903{
904 int ret = 0;
905 struct tc_action *act;
906 struct tc_action *a;
907 u32 seq = n->nlmsg_seq;
908
909 act = tcf_action_init(rta, NULL, NULL, ovr, 0, &ret);
910 if (act == NULL)
911 goto done;
912
913 /* dump then free all the actions after update; inserted policy
914 * stays intact
915 * */
916 ret = tcf_add_notify(act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
917 for (a = act; a; a = act) {
918 act = a->next;
919 kfree(a);
920 }
921done:
922 return ret;
923}
924
925static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
926{
927 struct rtattr **tca = arg;
928 u32 pid = skb ? NETLINK_CB(skb).pid : 0;
929 int ret = 0, ovr = 0;
930
931 if (tca[TCA_ACT_TAB-1] == NULL) {
932 printk("tc_ctl_action: received NO action attribs\n");
933 return -EINVAL;
934 }
935
936 /* n->nlmsg_flags&NLM_F_CREATE
937 * */
938 switch (n->nlmsg_type) {
939 case RTM_NEWACTION:
940 /* we are going to assume all other flags
941 * imply create only if it doesnt exist
942 * Note that CREATE | EXCL implies that
943 * but since we want avoid ambiguity (eg when flags
944 * is zero) then just set this
945 */
946 if (n->nlmsg_flags&NLM_F_REPLACE)
947 ovr = 1;
948replay:
949 ret = tcf_action_add(tca[TCA_ACT_TAB-1], n, pid, ovr);
950 if (ret == -EAGAIN)
951 goto replay;
952 break;
953 case RTM_DELACTION:
954 ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid, RTM_DELACTION);
955 break;
956 case RTM_GETACTION:
957 ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid, RTM_GETACTION);
958 break;
959 default:
960 BUG();
961 }
962
963 return ret;
964}
965
Thomas Graf26dab892006-07-05 20:45:06 -0700966static struct rtattr *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967find_dump_kind(struct nlmsghdr *n)
968{
969 struct rtattr *tb1, *tb2[TCA_ACT_MAX+1];
970 struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
971 struct rtattr *rta[TCAA_MAX + 1];
972 struct rtattr *kind;
973 int min_len = NLMSG_LENGTH(sizeof(struct tcamsg));
974 int attrlen = n->nlmsg_len - NLMSG_ALIGN(min_len);
975 struct rtattr *attr = (void *) n + NLMSG_ALIGN(min_len);
976
977 if (rtattr_parse(rta, TCAA_MAX, attr, attrlen) < 0)
978 return NULL;
979 tb1 = rta[TCA_ACT_TAB - 1];
980 if (tb1 == NULL)
981 return NULL;
982
983 if (rtattr_parse(tb, TCA_ACT_MAX_PRIO, RTA_DATA(tb1),
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900984 NLMSG_ALIGN(RTA_PAYLOAD(tb1))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return NULL;
986 if (tb[0] == NULL)
987 return NULL;
988
989 if (rtattr_parse(tb2, TCA_ACT_MAX, RTA_DATA(tb[0]),
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900990 RTA_PAYLOAD(tb[0])) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return NULL;
992 kind = tb2[TCA_ACT_KIND-1];
993
Thomas Graf26dab892006-07-05 20:45:06 -0700994 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
997static int
998tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
999{
1000 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001001 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 struct rtattr *x;
1003 struct tc_action_ops *a_o;
1004 struct tc_action a;
1005 int ret = 0;
1006 struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
Thomas Graf26dab892006-07-05 20:45:06 -07001007 struct rtattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 if (kind == NULL) {
1010 printk("tc_dump_action: action bad kind\n");
1011 return 0;
1012 }
1013
Thomas Graf26dab892006-07-05 20:45:06 -07001014 a_o = tc_lookup_action(kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (a_o == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return 0;
1017 }
1018
1019 memset(&a, 0, sizeof(struct tc_action));
1020 a.ops = a_o;
1021
1022 if (a_o->walk == NULL) {
Thomas Graf26dab892006-07-05 20:45:06 -07001023 printk("tc_dump_action: %s !capable of dumping table\n", a_o->kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 goto rtattr_failure;
1025 }
1026
1027 nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001028 cb->nlh->nlmsg_type, sizeof(*t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 t = NLMSG_DATA(nlh);
1030 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001031 t->tca__pad1 = 0;
1032 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001034 x = (struct rtattr *)skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
1036
1037 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1038 if (ret < 0)
1039 goto rtattr_failure;
1040
1041 if (ret > 0) {
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001042 x->rta_len = skb_tail_pointer(skb) - (u8 *)x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 ret = skb->len;
1044 } else
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001045 nlmsg_trim(skb, x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001047 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (NETLINK_CB(cb->skb).pid && ret)
1049 nlh->nlmsg_flags |= NLM_F_MULTI;
1050 module_put(a_o->owner);
1051 return skb->len;
1052
1053rtattr_failure:
1054nlmsg_failure:
1055 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001056 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return skb->len;
1058}
1059
1060static int __init tc_action_init(void)
1061{
Thomas Graf708914c2007-03-22 11:56:59 -07001062 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL);
1063 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL);
1064 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return 0;
1067}
1068
1069subsys_initcall(tc_action_init);
1070
1071EXPORT_SYMBOL(tcf_register_action);
1072EXPORT_SYMBOL(tcf_unregister_action);
1073EXPORT_SYMBOL(tcf_action_exec);
1074EXPORT_SYMBOL(tcf_action_dump_1);