blob: ea80f82dbb6a5c4b2e21fdc07ee42e1fcd645a9f [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
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800476 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 if (name == NULL) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800479 if (nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 goto err_out;
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 {
487 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
488 goto err_out;
489 }
490
491 a_o = tc_lookup_action_n(act_name);
492 if (a_o == NULL) {
493#ifdef CONFIG_KMOD
494 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800495 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 rtnl_lock();
497
498 a_o = tc_lookup_action_n(act_name);
499
500 /* We dropped the RTNL semaphore in order to
501 * perform the module load. So, even if we
502 * succeeded in loading the module we have to
503 * tell the caller to replay the request. We
504 * indicate this using -EAGAIN.
505 */
506 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800507 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 goto err_mod;
509 }
510#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800511 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 goto err_out;
513 }
514
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800515 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700516 a = kzalloc(sizeof(*a), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (a == NULL)
518 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 /* backward compatibility for policer */
521 if (name == NULL)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800522 err = a_o->init(tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 else
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800524 err = a_o->init(nla, est, a, ovr, bind);
525 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 goto err_free;
527
528 /* module count goes up only when brand new policy is created
529 if it exists and is only bound to in a_o->init() then
530 ACT_P_CREATED is not returned (a zero is).
531 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800532 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 module_put(a_o->owner);
534 a->ops = a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return a;
537
538err_free:
539 kfree(a);
540err_mod:
541 module_put(a_o->owner);
542err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800543 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800546struct tc_action *tcf_action_init(struct nlattr *nla, struct nlattr *est,
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800547 char *name, int ovr, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800549 struct nlattr *tb[TCA_ACT_MAX_PRIO+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 struct tc_action *head = NULL, *act, *act_prev = NULL;
551 int i;
552
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800553 if (nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL) < 0)
554 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800556 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800557 act = tcf_action_init_1(tb[i], est, name, ovr, bind);
558 if (IS_ERR(act))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 goto err;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800560 act->order = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 if (head == NULL)
563 head = act;
564 else
565 act_prev->next = act;
566 act_prev = act;
567 }
568 return head;
569
570err:
571 if (head != NULL)
572 tcf_action_destroy(head, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800573 return act;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
577 int compat_mode)
578{
579 int err = 0;
580 struct gnet_dump d;
581 struct tcf_act_hdr *h = a->priv;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (h == NULL)
584 goto errout;
585
586 /* compat_mode being true specifies a call that is supposed
587 * to add additional backward compatiblity statistic TLVs.
588 */
589 if (compat_mode) {
590 if (a->type == TCA_OLD_COMPAT)
591 err = gnet_stats_start_copy_compat(skb, 0,
Patrick McHardy4bdf3992007-07-02 22:47:37 -0700592 TCA_STATS, TCA_XSTATS, &h->tcf_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 else
594 return 0;
595 } else
596 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
Patrick McHardy4bdf3992007-07-02 22:47:37 -0700597 &h->tcf_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 if (err < 0)
600 goto errout;
601
602 if (a->ops != NULL && a->ops->get_stats != NULL)
603 if (a->ops->get_stats(skb, a) < 0)
604 goto errout;
605
David S. Millere9ce1cd2006-08-21 23:54:55 -0700606 if (gnet_stats_copy_basic(&d, &h->tcf_bstats) < 0 ||
David S. Millere9ce1cd2006-08-21 23:54:55 -0700607 gnet_stats_copy_rate_est(&d, &h->tcf_rate_est) < 0 ||
David S. Millere9ce1cd2006-08-21 23:54:55 -0700608 gnet_stats_copy_queue(&d, &h->tcf_qstats) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 goto errout;
610
611 if (gnet_stats_finish_copy(&d) < 0)
612 goto errout;
613
614 return 0;
615
616errout:
617 return -1;
618}
619
620static int
621tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, u32 seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900622 u16 flags, int event, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 struct tcamsg *t;
625 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700626 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800627 struct nlattr *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700629 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 t = NLMSG_DATA(nlh);
632 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700633 t->tca__pad1 = 0;
634 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900635
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800636 x = (struct nlattr *)skb_tail_pointer(skb);
637 NLA_PUT(skb, TCA_ACT_TAB, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if (tcf_action_dump(skb, a, bind, ref) < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800640 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800642 x->nla_len = skb_tail_pointer(skb) - (u8 *)x;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900643
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700644 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return skb->len;
646
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800647nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700649 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return -1;
651}
652
653static int
654act_get_notify(u32 pid, struct nlmsghdr *n, struct tc_action *a, int event)
655{
656 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
659 if (!skb)
660 return -ENOBUFS;
661 if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
662 kfree_skb(skb);
663 return -EINVAL;
664 }
Thomas Graf2942e902006-08-15 00:30:25 -0700665
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800666 return rtnl_unicast(skb, &init_net, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
669static struct tc_action *
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800670tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800672 struct nlattr *tb[TCA_ACT_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 struct tc_action *a;
674 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800675 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800677 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800678 if (nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL) < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800679 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800681 if (tb[TCA_ACT_INDEX] == NULL ||
682 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800683 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800684 index = *(int *)nla_data(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800686 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)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800689 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800691 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;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800697 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (a->ops->lookup(a, index) == 0)
699 goto err_mod;
700
701 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704err_mod:
705 module_put(a->ops->owner);
706err_free:
707 kfree(a);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800708err_out:
709 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712static void cleanup_a(struct tc_action *act)
713{
714 struct tc_action *a;
715
716 for (a = act; a; a = act) {
717 act = a->next;
718 kfree(a);
719 }
720}
721
722static struct tc_action *create_a(int i)
723{
724 struct tc_action *act;
725
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700726 act = kzalloc(sizeof(*act), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (act == NULL) {
728 printk("create_a: failed to alloc!\n");
729 return NULL;
730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 act->order = i;
732 return act;
733}
734
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800735static int tca_action_flush(struct nlattr *nla, struct nlmsghdr *n, u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 struct sk_buff *skb;
738 unsigned char *b;
739 struct nlmsghdr *nlh;
740 struct tcamsg *t;
741 struct netlink_callback dcb;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800742 struct nlattr *x;
743 struct nlattr *tb[TCA_ACT_MAX+1];
744 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 struct tc_action *a = create_a(0);
746 int err = -EINVAL;
747
748 if (a == NULL) {
749 printk("tca_action_flush: couldnt create tc_action\n");
750 return err;
751 }
752
753 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
754 if (!skb) {
755 printk("tca_action_flush: failed skb alloc\n");
756 kfree(a);
757 return -ENOBUFS;
758 }
759
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700760 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800762 if (nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 goto err_out;
764
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800765 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 a->ops = tc_lookup_action(kind);
767 if (a->ops == NULL)
768 goto err_out;
769
770 nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t));
771 t = NLMSG_DATA(nlh);
772 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700773 t->tca__pad1 = 0;
774 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800776 x = (struct nlattr *)skb_tail_pointer(skb);
777 NLA_PUT(skb, TCA_ACT_TAB, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
780 if (err < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800781 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800783 x->nla_len = skb_tail_pointer(skb) - (u8 *)x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700785 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 nlh->nlmsg_flags |= NLM_F_ROOT;
787 module_put(a->ops->owner);
788 kfree(a);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800789 err = rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (err > 0)
791 return 0;
792
793 return err;
794
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800795nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796nlmsg_failure:
Thomas Grafebbaeab2006-07-09 11:36:23 -0700797 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798err_out:
799 kfree_skb(skb);
800 kfree(a);
801 return err;
802}
803
804static int
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800805tca_action_gd(struct nlattr *nla, struct nlmsghdr *n, u32 pid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 int i, ret = 0;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800808 struct nlattr *tb[TCA_ACT_MAX_PRIO+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 struct tc_action *head = NULL, *act, *act_prev = NULL;
810
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800811 if (nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return -EINVAL;
813
814 if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) {
815 if (tb[0] != NULL && tb[1] == NULL)
816 return tca_action_flush(tb[0], n, pid);
817 }
818
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800819 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800820 act = tcf_action_get_1(tb[i], n, pid);
821 if (IS_ERR(act)) {
822 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800824 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800825 act->order = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 if (head == NULL)
828 head = act;
829 else
830 act_prev->next = act;
831 act_prev = act;
832 }
833
834 if (event == RTM_GETACTION)
835 ret = act_get_notify(pid, n, head, event);
836 else { /* delete */
837 struct sk_buff *skb;
838
839 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
840 if (!skb) {
841 ret = -ENOBUFS;
842 goto err;
843 }
844
845 if (tca_get_fill(skb, head, pid, n->nlmsg_seq, 0, event,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900846 0, 1) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 kfree_skb(skb);
848 ret = -EINVAL;
849 goto err;
850 }
851
852 /* now do the delete */
853 tcf_action_destroy(head, 0);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800854 ret = rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900855 n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (ret > 0)
857 return 0;
858 return ret;
859 }
860err:
861 cleanup_a(head);
862 return ret;
863}
864
865static int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900866 u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868 struct tcamsg *t;
869 struct nlmsghdr *nlh;
870 struct sk_buff *skb;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800871 struct nlattr *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 unsigned char *b;
873 int err = 0;
874
875 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
876 if (!skb)
877 return -ENOBUFS;
878
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700879 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700881 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 t = NLMSG_DATA(nlh);
883 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700884 t->tca__pad1 = 0;
885 t->tca__pad2 = 0;
886
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800887 x = (struct nlattr *)skb_tail_pointer(skb);
888 NLA_PUT(skb, TCA_ACT_TAB, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 if (tcf_action_dump(skb, a, 0, 0) < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800891 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800893 x->nla_len = skb_tail_pointer(skb) - (u8 *)x;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900894
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700895 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Patrick McHardyac6d4392005-08-14 19:29:52 -0700896 NETLINK_CB(skb).dst_group = RTNLGRP_TC;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900897
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800898 err = rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (err > 0)
900 err = 0;
901 return err;
902
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800903nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904nlmsg_failure:
Patrick McHardyf6e57462006-03-12 20:33:22 -0800905 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return -1;
907}
908
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910static int
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800911tcf_action_add(struct nlattr *nla, struct nlmsghdr *n, u32 pid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
913 int ret = 0;
914 struct tc_action *act;
915 struct tc_action *a;
916 u32 seq = n->nlmsg_seq;
917
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800918 act = tcf_action_init(nla, NULL, NULL, ovr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (act == NULL)
920 goto done;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800921 if (IS_ERR(act)) {
922 ret = PTR_ERR(act);
923 goto done;
924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 /* dump then free all the actions after update; inserted policy
927 * stays intact
928 * */
929 ret = tcf_add_notify(act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
930 for (a = act; a; a = act) {
931 act = a->next;
932 kfree(a);
933 }
934done:
935 return ret;
936}
937
938static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
939{
Denis V. Lunevb8542722007-12-01 00:21:31 +1100940 struct net *net = skb->sk->sk_net;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800941 struct nlattr *tca[TCA_ACT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 u32 pid = skb ? NETLINK_CB(skb).pid : 0;
943 int ret = 0, ovr = 0;
944
Denis V. Lunevb8542722007-12-01 00:21:31 +1100945 if (net != &init_net)
946 return -EINVAL;
947
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800948 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
949 if (ret < 0)
950 return ret;
951
952 if (tca[TCA_ACT_TAB] == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 printk("tc_ctl_action: received NO action attribs\n");
954 return -EINVAL;
955 }
956
957 /* n->nlmsg_flags&NLM_F_CREATE
958 * */
959 switch (n->nlmsg_type) {
960 case RTM_NEWACTION:
961 /* we are going to assume all other flags
962 * imply create only if it doesnt exist
963 * Note that CREATE | EXCL implies that
964 * but since we want avoid ambiguity (eg when flags
965 * is zero) then just set this
966 */
967 if (n->nlmsg_flags&NLM_F_REPLACE)
968 ovr = 1;
969replay:
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800970 ret = tcf_action_add(tca[TCA_ACT_TAB], n, pid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (ret == -EAGAIN)
972 goto replay;
973 break;
974 case RTM_DELACTION:
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800975 ret = tca_action_gd(tca[TCA_ACT_TAB], n, pid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 break;
977 case RTM_GETACTION:
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800978 ret = tca_action_gd(tca[TCA_ACT_TAB], n, pid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 break;
980 default:
981 BUG();
982 }
983
984 return ret;
985}
986
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800987static struct nlattr *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988find_dump_kind(struct nlmsghdr *n)
989{
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800990 struct nlattr *tb1, *tb2[TCA_ACT_MAX+1];
991 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
992 struct nlattr *nla[TCAA_MAX + 1];
993 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Patrick McHardyc96c9472008-01-23 20:32:58 -0800995 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800997 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (tb1 == NULL)
999 return NULL;
1000
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001001 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1002 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Patrick McHardy6d834e02008-01-23 20:32:42 -08001005 if (tb[1] == NULL)
1006 return NULL;
1007 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1008 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001010 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Thomas Graf26dab892006-07-05 20:45:06 -07001012 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
1015static int
1016tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1017{
Denis V. Lunevb8542722007-12-01 00:21:31 +11001018 struct net *net = skb->sk->sk_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001020 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001021 struct nlattr *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 struct tc_action_ops *a_o;
1023 struct tc_action a;
1024 int ret = 0;
1025 struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001026 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Denis V. Lunevb8542722007-12-01 00:21:31 +11001028 if (net != &init_net)
1029 return 0;
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (kind == NULL) {
1032 printk("tc_dump_action: action bad kind\n");
1033 return 0;
1034 }
1035
Thomas Graf26dab892006-07-05 20:45:06 -07001036 a_o = tc_lookup_action(kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (a_o == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return 0;
1039 }
1040
1041 memset(&a, 0, sizeof(struct tc_action));
1042 a.ops = a_o;
1043
1044 if (a_o->walk == NULL) {
Thomas Graf26dab892006-07-05 20:45:06 -07001045 printk("tc_dump_action: %s !capable of dumping table\n", a_o->kind);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001046 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048
1049 nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001050 cb->nlh->nlmsg_type, sizeof(*t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 t = NLMSG_DATA(nlh);
1052 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001053 t->tca__pad1 = 0;
1054 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001056 x = (struct nlattr *)skb_tail_pointer(skb);
1057 NLA_PUT(skb, TCA_ACT_TAB, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1060 if (ret < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001061 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 if (ret > 0) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001064 x->nla_len = skb_tail_pointer(skb) - (u8 *)x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 ret = skb->len;
1066 } else
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001067 nlmsg_trim(skb, x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001069 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (NETLINK_CB(cb->skb).pid && ret)
1071 nlh->nlmsg_flags |= NLM_F_MULTI;
1072 module_put(a_o->owner);
1073 return skb->len;
1074
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001075nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076nlmsg_failure:
1077 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001078 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return skb->len;
1080}
1081
1082static int __init tc_action_init(void)
1083{
Thomas Graf708914c2007-03-22 11:56:59 -07001084 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL);
1085 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL);
1086 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return 0;
1089}
1090
1091subsys_initcall(tc_action_init);