blob: 87818d7fb62398cc9bd5b542c54d937911cd77b1 [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>
Patrick McHardyab27cfb2008-01-23 20:33:13 -080021#include <linux/err.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110022#include <net/net_namespace.h>
23#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <net/sch_generic.h>
25#include <net/act_api.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070026#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
David S. Millere9ce1cd2006-08-21 23:54:55 -070028void tcf_hash_destroy(struct tcf_common *p, struct tcf_hashinfo *hinfo)
29{
30 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
31 struct tcf_common **p1p;
32
33 for (p1p = &hinfo->htab[h]; *p1p; p1p = &(*p1p)->tcfc_next) {
34 if (*p1p == p) {
35 write_lock_bh(hinfo->lock);
36 *p1p = p->tcfc_next;
37 write_unlock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -070038 gen_kill_estimator(&p->tcfc_bstats,
39 &p->tcfc_rate_est);
David S. Millere9ce1cd2006-08-21 23:54:55 -070040 kfree(p);
41 return;
42 }
43 }
44 BUG_TRAP(0);
45}
46EXPORT_SYMBOL(tcf_hash_destroy);
47
48int tcf_hash_release(struct tcf_common *p, int bind,
49 struct tcf_hashinfo *hinfo)
50{
51 int ret = 0;
52
53 if (p) {
54 if (bind)
55 p->tcfc_bindcnt--;
56
57 p->tcfc_refcnt--;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090058 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070059 tcf_hash_destroy(p, hinfo);
60 ret = 1;
61 }
62 }
63 return ret;
64}
65EXPORT_SYMBOL(tcf_hash_release);
66
67static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
68 struct tc_action *a, struct tcf_hashinfo *hinfo)
69{
70 struct tcf_common *p;
71 int err = 0, index = -1,i = 0, s_i = 0, n_i = 0;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080072 struct nlattr *r ;
David S. Millere9ce1cd2006-08-21 23:54:55 -070073
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +020074 read_lock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -070075
76 s_i = cb->args[0];
77
78 for (i = 0; i < (hinfo->hmask + 1); i++) {
79 p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
80
81 for (; p; p = p->tcfc_next) {
82 index++;
83 if (index < s_i)
84 continue;
85 a->priv = p;
86 a->order = n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080087 r = (struct nlattr *)skb_tail_pointer(skb);
88 NLA_PUT(skb, a->order, 0, NULL);
David S. Millere9ce1cd2006-08-21 23:54:55 -070089 err = tcf_action_dump_1(skb, a, 0, 0);
90 if (err < 0) {
91 index--;
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070092 nlmsg_trim(skb, r);
David S. Millere9ce1cd2006-08-21 23:54:55 -070093 goto done;
94 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -080095 r->nla_len = skb_tail_pointer(skb) - (u8 *)r;
David S. Millere9ce1cd2006-08-21 23:54:55 -070096 n_i++;
97 if (n_i >= TCA_ACT_MAX_PRIO)
98 goto done;
99 }
100 }
101done:
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +0200102 read_unlock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700103 if (n_i)
104 cb->args[0] += n_i;
105 return n_i;
106
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800107nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700108 nlmsg_trim(skb, r);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700109 goto done;
110}
111
112static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a,
113 struct tcf_hashinfo *hinfo)
114{
115 struct tcf_common *p, *s_p;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800116 struct nlattr *r ;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700117 int i= 0, n_i = 0;
118
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800119 r = (struct nlattr *)skb_tail_pointer(skb);
120 NLA_PUT(skb, a->order, 0, NULL);
121 NLA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700122 for (i = 0; i < (hinfo->hmask + 1); i++) {
123 p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
124
125 while (p != NULL) {
126 s_p = p->tcfc_next;
127 if (ACT_P_DELETED == tcf_hash_release(p, 0, hinfo))
128 module_put(a->ops->owner);
129 n_i++;
130 p = s_p;
131 }
132 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800133 NLA_PUT(skb, TCA_FCNT, 4, &n_i);
134 r->nla_len = skb_tail_pointer(skb) - (u8 *)r;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700135
136 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800137nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700138 nlmsg_trim(skb, r);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700139 return -EINVAL;
140}
141
142int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
143 int type, struct tc_action *a)
144{
145 struct tcf_hashinfo *hinfo = a->ops->hinfo;
146
147 if (type == RTM_DELACTION) {
148 return tcf_del_walker(skb, a, hinfo);
149 } else if (type == RTM_GETACTION) {
150 return tcf_dump_walker(skb, cb, a, hinfo);
151 } else {
152 printk("tcf_generic_walker: unknown action %d\n", type);
153 return -EINVAL;
154 }
155}
156EXPORT_SYMBOL(tcf_generic_walker);
157
158struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
159{
160 struct tcf_common *p;
161
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +0200162 read_lock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700163 for (p = hinfo->htab[tcf_hash(index, hinfo->hmask)]; p;
164 p = p->tcfc_next) {
165 if (p->tcfc_index == index)
166 break;
167 }
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +0200168 read_unlock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700169
170 return p;
171}
172EXPORT_SYMBOL(tcf_hash_lookup);
173
174u32 tcf_hash_new_index(u32 *idx_gen, struct tcf_hashinfo *hinfo)
175{
176 u32 val = *idx_gen;
177
178 do {
179 if (++val == 0)
180 val = 1;
181 } while (tcf_hash_lookup(val, hinfo));
182
183 return (*idx_gen = val);
184}
185EXPORT_SYMBOL(tcf_hash_new_index);
186
187int tcf_hash_search(struct tc_action *a, u32 index)
188{
189 struct tcf_hashinfo *hinfo = a->ops->hinfo;
190 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
191
192 if (p) {
193 a->priv = p;
194 return 1;
195 }
196 return 0;
197}
198EXPORT_SYMBOL(tcf_hash_search);
199
200struct tcf_common *tcf_hash_check(u32 index, struct tc_action *a, int bind,
201 struct tcf_hashinfo *hinfo)
202{
203 struct tcf_common *p = NULL;
204 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
205 if (bind) {
206 p->tcfc_bindcnt++;
207 p->tcfc_refcnt++;
208 }
209 a->priv = p;
210 }
211 return p;
212}
213EXPORT_SYMBOL(tcf_hash_check);
214
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800215struct 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 -0700216{
217 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
218
219 if (unlikely(!p))
220 return p;
221 p->tcfc_refcnt = 1;
222 if (bind)
223 p->tcfc_bindcnt = 1;
224
225 spin_lock_init(&p->tcfc_lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700226 p->tcfc_index = index ? index : tcf_hash_new_index(idx_gen, hinfo);
227 p->tcfc_tm.install = jiffies;
228 p->tcfc_tm.lastuse = jiffies;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700229 if (est)
230 gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800231 &p->tcfc_lock, est);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700232 a->priv = (void *) p;
233 return p;
234}
235EXPORT_SYMBOL(tcf_hash_create);
236
237void tcf_hash_insert(struct tcf_common *p, struct tcf_hashinfo *hinfo)
238{
239 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
240
241 write_lock_bh(hinfo->lock);
242 p->tcfc_next = hinfo->htab[h];
243 hinfo->htab[h] = p;
244 write_unlock_bh(hinfo->lock);
245}
246EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248static struct tc_action_ops *act_base = NULL;
249static DEFINE_RWLOCK(act_mod_lock);
250
251int tcf_register_action(struct tc_action_ops *act)
252{
253 struct tc_action_ops *a, **ap;
254
255 write_lock(&act_mod_lock);
256 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next) {
257 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
258 write_unlock(&act_mod_lock);
259 return -EEXIST;
260 }
261 }
262 act->next = NULL;
263 *ap = act;
264 write_unlock(&act_mod_lock);
265 return 0;
266}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800267EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269int tcf_unregister_action(struct tc_action_ops *act)
270{
271 struct tc_action_ops *a, **ap;
272 int err = -ENOENT;
273
274 write_lock(&act_mod_lock);
275 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next)
276 if (a == act)
277 break;
278 if (a) {
279 *ap = a->next;
280 a->next = NULL;
281 err = 0;
282 }
283 write_unlock(&act_mod_lock);
284 return err;
285}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800286EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288/* lookup by name */
289static struct tc_action_ops *tc_lookup_action_n(char *kind)
290{
291 struct tc_action_ops *a = NULL;
292
293 if (kind) {
294 read_lock(&act_mod_lock);
295 for (a = act_base; a; a = a->next) {
296 if (strcmp(kind, a->kind) == 0) {
297 if (!try_module_get(a->owner)) {
298 read_unlock(&act_mod_lock);
299 return NULL;
300 }
301 break;
302 }
303 }
304 read_unlock(&act_mod_lock);
305 }
306 return a;
307}
308
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800309/* lookup by nlattr */
310static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 struct tc_action_ops *a = NULL;
313
314 if (kind) {
315 read_lock(&act_mod_lock);
316 for (a = act_base; a; a = a->next) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800317 if (nla_strcmp(kind, a->kind) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (!try_module_get(a->owner)) {
319 read_unlock(&act_mod_lock);
320 return NULL;
321 }
322 break;
323 }
324 }
325 read_unlock(&act_mod_lock);
326 }
327 return a;
328}
329
330#if 0
331/* lookup by id */
332static struct tc_action_ops *tc_lookup_action_id(u32 type)
333{
334 struct tc_action_ops *a = NULL;
335
336 if (type) {
337 read_lock(&act_mod_lock);
338 for (a = act_base; a; a = a->next) {
339 if (a->type == type) {
340 if (!try_module_get(a->owner)) {
341 read_unlock(&act_mod_lock);
342 return NULL;
343 }
344 break;
345 }
346 }
347 read_unlock(&act_mod_lock);
348 }
349 return a;
350}
351#endif
352
353int tcf_action_exec(struct sk_buff *skb, struct tc_action *act,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900354 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 struct tc_action *a;
357 int ret = -1;
358
359 if (skb->tc_verd & TC_NCLS) {
360 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 ret = TC_ACT_OK;
362 goto exec_done;
363 }
364 while ((a = act) != NULL) {
365repeat:
366 if (a->ops && a->ops->act) {
Patrick McHardyf43c5a02006-01-08 22:15:34 -0800367 ret = a->ops->act(skb, a, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (TC_MUNGED & skb->tc_verd) {
369 /* copied already, allow trampling */
370 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
371 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (ret == TC_ACT_REPEAT)
374 goto repeat; /* we need a ttl - JHS */
J Hadi Salim14d50e72005-05-03 16:29:13 -0700375 if (ret != TC_ACT_PIPE)
376 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378 act = a->next;
379 }
380exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return ret;
382}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800383EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385void tcf_action_destroy(struct tc_action *act, int bind)
386{
387 struct tc_action *a;
388
389 for (a = act; a; a = act) {
390 if (a->ops && a->ops->cleanup) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
392 module_put(a->ops->owner);
393 act = act->next;
394 kfree(a);
395 } else { /*FIXME: Remove later - catch insertion bugs*/
396 printk("tcf_action_destroy: BUG? destroying NULL ops\n");
397 act = act->next;
398 kfree(a);
399 }
400 }
401}
402
403int
404tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
405{
406 int err = -EINVAL;
407
408 if (a->ops == NULL || a->ops->dump == NULL)
409 return err;
410 return a->ops->dump(skb, a, bind, ref);
411}
412
413int
414tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
415{
416 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700417 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800418 struct nlattr *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 if (a->ops == NULL || a->ops->dump == NULL)
421 return err;
422
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800423 NLA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800425 goto nla_put_failure;
426 r = (struct nlattr *)skb_tail_pointer(skb);
427 NLA_PUT(skb, TCA_OPTIONS, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800429 r->nla_len = skb_tail_pointer(skb) - (u8 *)r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return err;
431 }
432
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800433nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700434 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return -1;
436}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800437EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439int
440tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref)
441{
442 struct tc_action *a;
443 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700444 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800445 struct nlattr *r ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 while ((a = act) != NULL) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800448 r = (struct nlattr *)skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 act = a->next;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800450 NLA_PUT(skb, a->order, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 err = tcf_action_dump_1(skb, a, bind, ref);
452 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700453 goto errout;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800454 r->nla_len = skb_tail_pointer(skb) - (u8 *)r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456
457 return 0;
458
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800459nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700460 err = -EINVAL;
461errout:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700462 nlmsg_trim(skb, b);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700463 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800466struct tc_action *tcf_action_init_1(struct nlattr *nla, struct nlattr *est,
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800467 char *name, int ovr, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 struct tc_action *a;
470 struct tc_action_ops *a_o;
471 char act_name[IFNAMSIZ];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800472 struct nlattr *tb[TCA_ACT_MAX+1];
473 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800474 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800477 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
478 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800480 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800481 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (kind == NULL)
483 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800484 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 goto err_out;
486 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800487 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
489 goto err_out;
490 }
491
492 a_o = tc_lookup_action_n(act_name);
493 if (a_o == NULL) {
494#ifdef CONFIG_KMOD
495 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800496 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 rtnl_lock();
498
499 a_o = tc_lookup_action_n(act_name);
500
501 /* We dropped the RTNL semaphore in order to
502 * perform the module load. So, even if we
503 * succeeded in loading the module we have to
504 * tell the caller to replay the request. We
505 * indicate this using -EAGAIN.
506 */
507 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800508 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 goto err_mod;
510 }
511#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800512 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 goto err_out;
514 }
515
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800516 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700517 a = kzalloc(sizeof(*a), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (a == NULL)
519 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 /* backward compatibility for policer */
522 if (name == NULL)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800523 err = a_o->init(tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 else
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800525 err = a_o->init(nla, est, a, ovr, bind);
526 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 goto err_free;
528
529 /* module count goes up only when brand new policy is created
530 if it exists and is only bound to in a_o->init() then
531 ACT_P_CREATED is not returned (a zero is).
532 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800533 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 module_put(a_o->owner);
535 a->ops = a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return a;
538
539err_free:
540 kfree(a);
541err_mod:
542 module_put(a_o->owner);
543err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800544 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800547struct tc_action *tcf_action_init(struct nlattr *nla, struct nlattr *est,
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800548 char *name, int ovr, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800550 struct nlattr *tb[TCA_ACT_MAX_PRIO+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct tc_action *head = NULL, *act, *act_prev = NULL;
Patrick McHardycee63722008-01-23 20:33:32 -0800552 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 int i;
554
Patrick McHardycee63722008-01-23 20:33:32 -0800555 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
556 if (err < 0)
557 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800559 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800560 act = tcf_action_init_1(tb[i], est, name, ovr, bind);
561 if (IS_ERR(act))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto err;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800563 act->order = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 if (head == NULL)
566 head = act;
567 else
568 act_prev->next = act;
569 act_prev = act;
570 }
571 return head;
572
573err:
574 if (head != NULL)
575 tcf_action_destroy(head, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800576 return act;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
580 int compat_mode)
581{
582 int err = 0;
583 struct gnet_dump d;
584 struct tcf_act_hdr *h = a->priv;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (h == NULL)
587 goto errout;
588
589 /* compat_mode being true specifies a call that is supposed
590 * to add additional backward compatiblity statistic TLVs.
591 */
592 if (compat_mode) {
593 if (a->type == TCA_OLD_COMPAT)
594 err = gnet_stats_start_copy_compat(skb, 0,
Patrick McHardy4bdf3992007-07-02 22:47:37 -0700595 TCA_STATS, TCA_XSTATS, &h->tcf_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 else
597 return 0;
598 } else
599 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
Patrick McHardy4bdf3992007-07-02 22:47:37 -0700600 &h->tcf_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 if (err < 0)
603 goto errout;
604
605 if (a->ops != NULL && a->ops->get_stats != NULL)
606 if (a->ops->get_stats(skb, a) < 0)
607 goto errout;
608
David S. Millere9ce1cd2006-08-21 23:54:55 -0700609 if (gnet_stats_copy_basic(&d, &h->tcf_bstats) < 0 ||
David S. Millere9ce1cd2006-08-21 23:54:55 -0700610 gnet_stats_copy_rate_est(&d, &h->tcf_rate_est) < 0 ||
David S. Millere9ce1cd2006-08-21 23:54:55 -0700611 gnet_stats_copy_queue(&d, &h->tcf_qstats) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 goto errout;
613
614 if (gnet_stats_finish_copy(&d) < 0)
615 goto errout;
616
617 return 0;
618
619errout:
620 return -1;
621}
622
623static int
624tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, u32 seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900625 u16 flags, int event, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 struct tcamsg *t;
628 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700629 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800630 struct nlattr *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700632 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 t = NLMSG_DATA(nlh);
635 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700636 t->tca__pad1 = 0;
637 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900638
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800639 x = (struct nlattr *)skb_tail_pointer(skb);
640 NLA_PUT(skb, TCA_ACT_TAB, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 if (tcf_action_dump(skb, a, bind, ref) < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800643 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800645 x->nla_len = skb_tail_pointer(skb) - (u8 *)x;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900646
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700647 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return skb->len;
649
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800650nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700652 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return -1;
654}
655
656static int
657act_get_notify(u32 pid, struct nlmsghdr *n, struct tc_action *a, int event)
658{
659 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
662 if (!skb)
663 return -ENOBUFS;
664 if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
665 kfree_skb(skb);
666 return -EINVAL;
667 }
Thomas Graf2942e902006-08-15 00:30:25 -0700668
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800669 return rtnl_unicast(skb, &init_net, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
672static struct tc_action *
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800673tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800675 struct nlattr *tb[TCA_ACT_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 struct tc_action *a;
677 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800678 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Patrick McHardycee63722008-01-23 20:33:32 -0800680 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
681 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800682 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Patrick McHardycee63722008-01-23 20:33:32 -0800684 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800685 if (tb[TCA_ACT_INDEX] == NULL ||
686 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800687 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800688 index = *(int *)nla_data(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800690 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700691 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (a == NULL)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800693 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800695 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800696 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (a->ops == NULL)
698 goto err_free;
699 if (a->ops->lookup == NULL)
700 goto err_mod;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800701 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (a->ops->lookup(a, index) == 0)
703 goto err_mod;
704
705 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708err_mod:
709 module_put(a->ops->owner);
710err_free:
711 kfree(a);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800712err_out:
713 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
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
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800739static int tca_action_flush(struct nlattr *nla, struct nlmsghdr *n, u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 struct sk_buff *skb;
742 unsigned char *b;
743 struct nlmsghdr *nlh;
744 struct tcamsg *t;
745 struct netlink_callback dcb;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800746 struct nlattr *x;
747 struct nlattr *tb[TCA_ACT_MAX+1];
748 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 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
Patrick McHardycee63722008-01-23 20:33:32 -0800766 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
767 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 goto err_out;
769
Patrick McHardycee63722008-01-23 20:33:32 -0800770 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800771 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 a->ops = tc_lookup_action(kind);
773 if (a->ops == NULL)
774 goto err_out;
775
776 nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t));
777 t = NLMSG_DATA(nlh);
778 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700779 t->tca__pad1 = 0;
780 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800782 x = (struct nlattr *)skb_tail_pointer(skb);
783 NLA_PUT(skb, TCA_ACT_TAB, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
786 if (err < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800787 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800789 x->nla_len = skb_tail_pointer(skb) - (u8 *)x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700791 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 nlh->nlmsg_flags |= NLM_F_ROOT;
793 module_put(a->ops->owner);
794 kfree(a);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800795 err = rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (err > 0)
797 return 0;
798
799 return err;
800
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800801nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802nlmsg_failure:
Thomas Grafebbaeab2006-07-09 11:36:23 -0700803 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804err_out:
805 kfree_skb(skb);
806 kfree(a);
807 return err;
808}
809
810static int
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800811tca_action_gd(struct nlattr *nla, struct nlmsghdr *n, u32 pid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Patrick McHardycee63722008-01-23 20:33:32 -0800813 int i, ret;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800814 struct nlattr *tb[TCA_ACT_MAX_PRIO+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 struct tc_action *head = NULL, *act, *act_prev = NULL;
816
Patrick McHardycee63722008-01-23 20:33:32 -0800817 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
818 if (ret < 0)
819 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) {
822 if (tb[0] != NULL && tb[1] == NULL)
823 return tca_action_flush(tb[0], n, pid);
824 }
825
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800826 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800827 act = tcf_action_get_1(tb[i], n, pid);
828 if (IS_ERR(act)) {
829 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800831 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800832 act->order = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 if (head == NULL)
835 head = act;
836 else
837 act_prev->next = act;
838 act_prev = act;
839 }
840
841 if (event == RTM_GETACTION)
842 ret = act_get_notify(pid, n, head, event);
843 else { /* delete */
844 struct sk_buff *skb;
845
846 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
847 if (!skb) {
848 ret = -ENOBUFS;
849 goto err;
850 }
851
852 if (tca_get_fill(skb, head, pid, n->nlmsg_seq, 0, event,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900853 0, 1) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 kfree_skb(skb);
855 ret = -EINVAL;
856 goto err;
857 }
858
859 /* now do the delete */
860 tcf_action_destroy(head, 0);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800861 ret = rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900862 n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (ret > 0)
864 return 0;
865 return ret;
866 }
867err:
868 cleanup_a(head);
869 return ret;
870}
871
872static int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900873 u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
875 struct tcamsg *t;
876 struct nlmsghdr *nlh;
877 struct sk_buff *skb;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800878 struct nlattr *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 unsigned char *b;
880 int err = 0;
881
882 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
883 if (!skb)
884 return -ENOBUFS;
885
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700886 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700888 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 t = NLMSG_DATA(nlh);
890 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700891 t->tca__pad1 = 0;
892 t->tca__pad2 = 0;
893
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800894 x = (struct nlattr *)skb_tail_pointer(skb);
895 NLA_PUT(skb, TCA_ACT_TAB, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 if (tcf_action_dump(skb, a, 0, 0) < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800898 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800900 x->nla_len = skb_tail_pointer(skb) - (u8 *)x;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900901
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700902 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Patrick McHardyac6d4392005-08-14 19:29:52 -0700903 NETLINK_CB(skb).dst_group = RTNLGRP_TC;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900904
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800905 err = rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (err > 0)
907 err = 0;
908 return err;
909
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800910nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911nlmsg_failure:
Patrick McHardyf6e57462006-03-12 20:33:22 -0800912 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return -1;
914}
915
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917static int
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800918tcf_action_add(struct nlattr *nla, struct nlmsghdr *n, u32 pid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 int ret = 0;
921 struct tc_action *act;
922 struct tc_action *a;
923 u32 seq = n->nlmsg_seq;
924
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800925 act = tcf_action_init(nla, NULL, NULL, ovr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (act == NULL)
927 goto done;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800928 if (IS_ERR(act)) {
929 ret = PTR_ERR(act);
930 goto done;
931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 /* dump then free all the actions after update; inserted policy
934 * stays intact
935 * */
936 ret = tcf_add_notify(act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
937 for (a = act; a; a = act) {
938 act = a->next;
939 kfree(a);
940 }
941done:
942 return ret;
943}
944
945static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
946{
Denis V. Lunevb8542722007-12-01 00:21:31 +1100947 struct net *net = skb->sk->sk_net;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800948 struct nlattr *tca[TCA_ACT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 u32 pid = skb ? NETLINK_CB(skb).pid : 0;
950 int ret = 0, ovr = 0;
951
Denis V. Lunevb8542722007-12-01 00:21:31 +1100952 if (net != &init_net)
953 return -EINVAL;
954
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800955 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
956 if (ret < 0)
957 return ret;
958
959 if (tca[TCA_ACT_TAB] == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 printk("tc_ctl_action: received NO action attribs\n");
961 return -EINVAL;
962 }
963
964 /* n->nlmsg_flags&NLM_F_CREATE
965 * */
966 switch (n->nlmsg_type) {
967 case RTM_NEWACTION:
968 /* we are going to assume all other flags
969 * imply create only if it doesnt exist
970 * Note that CREATE | EXCL implies that
971 * but since we want avoid ambiguity (eg when flags
972 * is zero) then just set this
973 */
974 if (n->nlmsg_flags&NLM_F_REPLACE)
975 ovr = 1;
976replay:
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800977 ret = tcf_action_add(tca[TCA_ACT_TAB], n, pid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (ret == -EAGAIN)
979 goto replay;
980 break;
981 case RTM_DELACTION:
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800982 ret = tca_action_gd(tca[TCA_ACT_TAB], n, pid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 break;
984 case RTM_GETACTION:
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800985 ret = tca_action_gd(tca[TCA_ACT_TAB], n, pid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 break;
987 default:
988 BUG();
989 }
990
991 return ret;
992}
993
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800994static struct nlattr *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995find_dump_kind(struct nlmsghdr *n)
996{
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800997 struct nlattr *tb1, *tb2[TCA_ACT_MAX+1];
998 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
999 struct nlattr *nla[TCAA_MAX + 1];
1000 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Patrick McHardyc96c9472008-01-23 20:32:58 -08001002 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001004 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (tb1 == NULL)
1006 return NULL;
1007
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001008 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1009 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Patrick McHardy6d834e02008-01-23 20:32:42 -08001012 if (tb[1] == NULL)
1013 return NULL;
1014 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1015 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001017 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Thomas Graf26dab892006-07-05 20:45:06 -07001019 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
1022static int
1023tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1024{
Denis V. Lunevb8542722007-12-01 00:21:31 +11001025 struct net *net = skb->sk->sk_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001027 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001028 struct nlattr *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 struct tc_action_ops *a_o;
1030 struct tc_action a;
1031 int ret = 0;
1032 struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001033 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Denis V. Lunevb8542722007-12-01 00:21:31 +11001035 if (net != &init_net)
1036 return 0;
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (kind == NULL) {
1039 printk("tc_dump_action: action bad kind\n");
1040 return 0;
1041 }
1042
Thomas Graf26dab892006-07-05 20:45:06 -07001043 a_o = tc_lookup_action(kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (a_o == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return 0;
1046 }
1047
1048 memset(&a, 0, sizeof(struct tc_action));
1049 a.ops = a_o;
1050
1051 if (a_o->walk == NULL) {
Thomas Graf26dab892006-07-05 20:45:06 -07001052 printk("tc_dump_action: %s !capable of dumping table\n", a_o->kind);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001053 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
1055
1056 nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001057 cb->nlh->nlmsg_type, sizeof(*t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 t = NLMSG_DATA(nlh);
1059 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001060 t->tca__pad1 = 0;
1061 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001063 x = (struct nlattr *)skb_tail_pointer(skb);
1064 NLA_PUT(skb, TCA_ACT_TAB, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1067 if (ret < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001068 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 if (ret > 0) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001071 x->nla_len = skb_tail_pointer(skb) - (u8 *)x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 ret = skb->len;
1073 } else
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001074 nlmsg_trim(skb, x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001076 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (NETLINK_CB(cb->skb).pid && ret)
1078 nlh->nlmsg_flags |= NLM_F_MULTI;
1079 module_put(a_o->owner);
1080 return skb->len;
1081
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001082nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083nlmsg_failure:
1084 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001085 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return skb->len;
1087}
1088
1089static int __init tc_action_init(void)
1090{
Thomas Graf708914c2007-03-22 11:56:59 -07001091 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL);
1092 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL);
1093 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return 0;
1096}
1097
1098subsys_initcall(tc_action_init);