blob: ae077ed208af79306e3a4d6a4e74ba8cf2caf2e9 [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>
Denis V. Lunevb8542722007-12-01 00:21:31 +110021#include <net/net_namespace.h>
22#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <net/sch_generic.h>
24#include <net/act_api.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070025#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
David S. Millere9ce1cd2006-08-21 23:54:55 -070027void 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);
David S. Millere9ce1cd2006-08-21 23:54:55 -070037 gen_kill_estimator(&p->tcfc_bstats,
38 &p->tcfc_rate_est);
David S. Millere9ce1cd2006-08-21 23:54:55 -070039 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--;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090057 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070058 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;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080071 struct nlattr *r ;
David S. Millere9ce1cd2006-08-21 23:54:55 -070072
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +020073 read_lock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -070074
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;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080086 r = (struct nlattr *)skb_tail_pointer(skb);
87 NLA_PUT(skb, a->order, 0, NULL);
David S. Millere9ce1cd2006-08-21 23:54:55 -070088 err = tcf_action_dump_1(skb, a, 0, 0);
89 if (err < 0) {
90 index--;
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070091 nlmsg_trim(skb, r);
David S. Millere9ce1cd2006-08-21 23:54:55 -070092 goto done;
93 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -080094 r->nla_len = skb_tail_pointer(skb) - (u8 *)r;
David S. Millere9ce1cd2006-08-21 23:54:55 -070095 n_i++;
96 if (n_i >= TCA_ACT_MAX_PRIO)
97 goto done;
98 }
99 }
100done:
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +0200101 read_unlock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700102 if (n_i)
103 cb->args[0] += n_i;
104 return n_i;
105
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800106nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700107 nlmsg_trim(skb, r);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700108 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;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800115 struct nlattr *r ;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700116 int i= 0, n_i = 0;
117
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800118 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);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700121 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 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800132 NLA_PUT(skb, TCA_FCNT, 4, &n_i);
133 r->nla_len = skb_tail_pointer(skb) - (u8 *)r;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700134
135 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800136nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700137 nlmsg_trim(skb, r);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700138 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
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +0200161 read_lock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700162 for (p = hinfo->htab[tcf_hash(index, hinfo->hmask)]; p;
163 p = p->tcfc_next) {
164 if (p->tcfc_index == index)
165 break;
166 }
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +0200167 read_unlock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700168
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
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800214struct 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)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700215{
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);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700225 p->tcfc_index = index ? index : tcf_hash_new_index(idx_gen, hinfo);
226 p->tcfc_tm.install = jiffies;
227 p->tcfc_tm.lastuse = jiffies;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700228 if (est)
229 gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800230 &p->tcfc_lock, est);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700231 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
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}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800266EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
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}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800285EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
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
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800308/* lookup by nlattr */
309static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
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) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800316 if (nla_strcmp(kind, a->kind) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 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,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900353 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 ret = TC_ACT_OK;
361 goto exec_done;
362 }
363 while ((a = act) != NULL) {
364repeat:
365 if (a->ops && a->ops->act) {
Patrick McHardyf43c5a02006-01-08 22:15:34 -0800366 ret = a->ops->act(skb, a, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (ret == TC_ACT_REPEAT)
373 goto repeat; /* we need a ttl - JHS */
J Hadi Salim14d50e72005-05-03 16:29:13 -0700374 if (ret != TC_ACT_PIPE)
375 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377 act = a->next;
378 }
379exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return ret;
381}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800382EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 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;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700416 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800417 struct nlattr *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 if (a->ops == NULL || a->ops->dump == NULL)
420 return err;
421
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800422 NLA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800424 goto nla_put_failure;
425 r = (struct nlattr *)skb_tail_pointer(skb);
426 NLA_PUT(skb, TCA_OPTIONS, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800428 r->nla_len = skb_tail_pointer(skb) - (u8 *)r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return err;
430 }
431
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800432nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700433 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return -1;
435}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800436EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
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;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700443 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800444 struct nlattr *r ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 while ((a = act) != NULL) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800447 r = (struct nlattr *)skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 act = a->next;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800449 NLA_PUT(skb, a->order, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 err = tcf_action_dump_1(skb, a, bind, ref);
451 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700452 goto errout;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800453 r->nla_len = skb_tail_pointer(skb) - (u8 *)r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
456 return 0;
457
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800458nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700459 err = -EINVAL;
460errout:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700461 nlmsg_trim(skb, b);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700462 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800465struct tc_action *tcf_action_init_1(struct nlattr *nla, struct nlattr *est,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900466 char *name, int ovr, int bind, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 struct tc_action *a;
469 struct tc_action_ops *a_o;
470 char act_name[IFNAMSIZ];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800471 struct nlattr *tb[TCA_ACT_MAX+1];
472 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 *err = -EINVAL;
475
476 if (name == NULL) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800477 if (nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800479 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (kind == NULL)
481 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800482 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 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();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800493 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 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
Thomas Grafd152b4e2006-07-05 20:45:57 -0700509 *err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 goto err_out;
511 }
512
513 *err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700514 a = kzalloc(sizeof(*a), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (a == NULL)
516 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 /* backward compatibility for policer */
519 if (name == NULL)
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800520 *err = a_o->init(tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 else
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800522 *err = a_o->init(nla, est, a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
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
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800545struct tc_action *tcf_action_init(struct nlattr *nla, struct nlattr *est,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900546 char *name, int ovr, int bind, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800548 struct nlattr *tb[TCA_ACT_MAX_PRIO+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 struct tc_action *head = NULL, *act, *act_prev = NULL;
550 int i;
551
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800552 if (nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 *err = -EINVAL;
554 return head;
555 }
556
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800557 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 act = tcf_action_init_1(tb[i], est, name, ovr, bind, err);
559 if (act == NULL)
560 goto err;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800561 act->order = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
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;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 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,
Patrick McHardy4bdf3992007-07-02 22:47:37 -0700593 TCA_STATS, TCA_XSTATS, &h->tcf_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 else
595 return 0;
596 } else
597 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
Patrick McHardy4bdf3992007-07-02 22:47:37 -0700598 &h->tcf_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
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
David S. Millere9ce1cd2006-08-21 23:54:55 -0700607 if (gnet_stats_copy_basic(&d, &h->tcf_bstats) < 0 ||
David S. Millere9ce1cd2006-08-21 23:54:55 -0700608 gnet_stats_copy_rate_est(&d, &h->tcf_rate_est) < 0 ||
David S. Millere9ce1cd2006-08-21 23:54:55 -0700609 gnet_stats_copy_queue(&d, &h->tcf_qstats) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 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,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900623 u16 flags, int event, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 struct tcamsg *t;
626 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700627 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800628 struct nlattr *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700630 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 t = NLMSG_DATA(nlh);
633 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700634 t->tca__pad1 = 0;
635 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900636
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800637 x = (struct nlattr *)skb_tail_pointer(skb);
638 NLA_PUT(skb, TCA_ACT_TAB, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 if (tcf_action_dump(skb, a, bind, ref) < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800641 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800643 x->nla_len = skb_tail_pointer(skb) - (u8 *)x;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900644
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700645 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return skb->len;
647
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800648nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700650 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
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 }
Thomas Graf2942e902006-08-15 00:30:25 -0700666
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800667 return rtnl_unicast(skb, &init_net, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
670static struct tc_action *
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800671tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 pid, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800673 struct nlattr *tb[TCA_ACT_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 struct tc_action *a;
675 int index;
676
677 *err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800678 if (nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return NULL;
680
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800681 if (tb[TCA_ACT_INDEX] == NULL ||
682 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800684 index = *(int *)nla_data(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 *err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700687 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (a == NULL)
689 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 *err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800692 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 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
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700725 act = kzalloc(sizeof(*act), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (act == NULL) {
727 printk("create_a: failed to alloc!\n");
728 return NULL;
729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 act->order = i;
731 return act;
732}
733
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800734static int tca_action_flush(struct nlattr *nla, struct nlmsghdr *n, u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
736 struct sk_buff *skb;
737 unsigned char *b;
738 struct nlmsghdr *nlh;
739 struct tcamsg *t;
740 struct netlink_callback dcb;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800741 struct nlattr *x;
742 struct nlattr *tb[TCA_ACT_MAX+1];
743 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 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
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700759 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800761 if (nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 goto err_out;
763
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800764 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 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;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700772 t->tca__pad1 = 0;
773 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800775 x = (struct nlattr *)skb_tail_pointer(skb);
776 NLA_PUT(skb, TCA_ACT_TAB, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
779 if (err < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800780 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800782 x->nla_len = skb_tail_pointer(skb) - (u8 *)x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700784 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 nlh->nlmsg_flags |= NLM_F_ROOT;
786 module_put(a->ops->owner);
787 kfree(a);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800788 err = rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (err > 0)
790 return 0;
791
792 return err;
793
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800794nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795nlmsg_failure:
Thomas Grafebbaeab2006-07-09 11:36:23 -0700796 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797err_out:
798 kfree_skb(skb);
799 kfree(a);
800 return err;
801}
802
803static int
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800804tca_action_gd(struct nlattr *nla, struct nlmsghdr *n, u32 pid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 int i, ret = 0;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800807 struct nlattr *tb[TCA_ACT_MAX_PRIO+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 struct tc_action *head = NULL, *act, *act_prev = NULL;
809
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800810 if (nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 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
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800818 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 act = tcf_action_get_1(tb[i], n, pid, &ret);
820 if (act == NULL)
821 goto err;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800822 act->order = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
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,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900843 0, 1) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 kfree_skb(skb);
845 ret = -EINVAL;
846 goto err;
847 }
848
849 /* now do the delete */
850 tcf_action_destroy(head, 0);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800851 ret = rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900852 n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 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,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900863 u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 struct tcamsg *t;
866 struct nlmsghdr *nlh;
867 struct sk_buff *skb;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800868 struct nlattr *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 unsigned char *b;
870 int err = 0;
871
872 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
873 if (!skb)
874 return -ENOBUFS;
875
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700876 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700878 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 t = NLMSG_DATA(nlh);
880 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700881 t->tca__pad1 = 0;
882 t->tca__pad2 = 0;
883
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800884 x = (struct nlattr *)skb_tail_pointer(skb);
885 NLA_PUT(skb, TCA_ACT_TAB, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 if (tcf_action_dump(skb, a, 0, 0) < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800888 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800890 x->nla_len = skb_tail_pointer(skb) - (u8 *)x;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900891
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700892 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Patrick McHardyac6d4392005-08-14 19:29:52 -0700893 NETLINK_CB(skb).dst_group = RTNLGRP_TC;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900894
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800895 err = rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (err > 0)
897 err = 0;
898 return err;
899
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800900nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901nlmsg_failure:
Patrick McHardyf6e57462006-03-12 20:33:22 -0800902 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return -1;
904}
905
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907static int
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800908tcf_action_add(struct nlattr *nla, struct nlmsghdr *n, u32 pid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910 int ret = 0;
911 struct tc_action *act;
912 struct tc_action *a;
913 u32 seq = n->nlmsg_seq;
914
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800915 act = tcf_action_init(nla, NULL, NULL, ovr, 0, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 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{
Denis V. Lunevb8542722007-12-01 00:21:31 +1100933 struct net *net = skb->sk->sk_net;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800934 struct nlattr *tca[TCA_ACT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 u32 pid = skb ? NETLINK_CB(skb).pid : 0;
936 int ret = 0, ovr = 0;
937
Denis V. Lunevb8542722007-12-01 00:21:31 +1100938 if (net != &init_net)
939 return -EINVAL;
940
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800941 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 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:
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800963 ret = tcf_action_add(tca[TCA_ACT_TAB], n, pid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (ret == -EAGAIN)
965 goto replay;
966 break;
967 case RTM_DELACTION:
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800968 ret = tca_action_gd(tca[TCA_ACT_TAB], n, pid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 break;
970 case RTM_GETACTION:
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800971 ret = tca_action_gd(tca[TCA_ACT_TAB], n, pid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 break;
973 default:
974 BUG();
975 }
976
977 return ret;
978}
979
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800980static struct nlattr *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981find_dump_kind(struct nlmsghdr *n)
982{
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800983 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 int min_len = NLMSG_LENGTH(sizeof(struct tcamsg));
988 int attrlen = n->nlmsg_len - NLMSG_ALIGN(min_len);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800989 struct nlattr *attr = (void *) n + NLMSG_ALIGN(min_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800991 if (nla_parse(nla, TCAA_MAX, attr, attrlen, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800993 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (tb1 == NULL)
995 return NULL;
996
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800997 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
998 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Patrick McHardy6d834e02008-01-23 20:32:42 -08001001 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001006 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Thomas Graf26dab892006-07-05 20:45:06 -07001008 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
1011static int
1012tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1013{
Denis V. Lunevb8542722007-12-01 00:21:31 +11001014 struct net *net = skb->sk->sk_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001016 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001017 struct nlattr *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 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);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001022 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Denis V. Lunevb8542722007-12-01 00:21:31 +11001024 if (net != &init_net)
1025 return 0;
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (kind == NULL) {
1028 printk("tc_dump_action: action bad kind\n");
1029 return 0;
1030 }
1031
Thomas Graf26dab892006-07-05 20:45:06 -07001032 a_o = tc_lookup_action(kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (a_o == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return 0;
1035 }
1036
1037 memset(&a, 0, sizeof(struct tc_action));
1038 a.ops = a_o;
1039
1040 if (a_o->walk == NULL) {
Thomas Graf26dab892006-07-05 20:45:06 -07001041 printk("tc_dump_action: %s !capable of dumping table\n", a_o->kind);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001042 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044
1045 nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001046 cb->nlh->nlmsg_type, sizeof(*t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 t = NLMSG_DATA(nlh);
1048 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001049 t->tca__pad1 = 0;
1050 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001052 x = (struct nlattr *)skb_tail_pointer(skb);
1053 NLA_PUT(skb, TCA_ACT_TAB, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1056 if (ret < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001057 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 if (ret > 0) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001060 x->nla_len = skb_tail_pointer(skb) - (u8 *)x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 ret = skb->len;
1062 } else
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001063 nlmsg_trim(skb, x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001065 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 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
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001071nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072nlmsg_failure:
1073 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001074 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return skb->len;
1076}
1077
1078static int __init tc_action_init(void)
1079{
Thomas Graf708914c2007-03-22 11:56:59 -07001080 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return 0;
1085}
1086
1087subsys_initcall(tc_action_init);