blob: 9974b3f04f05cfe54142d7d55cd762021509c8e5 [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 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -070044 WARN_ON(1);
David S. Millere9ce1cd2006-08-21 23:54:55 -070045}
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 McHardy4b3550ef2008-01-23 20:34:11 -080072 struct nlattr *nest;
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 McHardy4b3550ef2008-01-23 20:34:11 -080087
88 nest = nla_nest_start(skb, a->order);
89 if (nest == NULL)
90 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -070091 err = tcf_action_dump_1(skb, a, 0, 0);
92 if (err < 0) {
93 index--;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080094 nlmsg_trim(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -070095 goto done;
96 }
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080097 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -070098 n_i++;
99 if (n_i >= TCA_ACT_MAX_PRIO)
100 goto done;
101 }
102 }
103done:
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +0200104 read_unlock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700105 if (n_i)
106 cb->args[0] += n_i;
107 return n_i;
108
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800109nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800110 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700111 goto done;
112}
113
114static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a,
115 struct tcf_hashinfo *hinfo)
116{
117 struct tcf_common *p, *s_p;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800118 struct nlattr *nest;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700119 int i= 0, n_i = 0;
120
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800121 nest = nla_nest_start(skb, a->order);
122 if (nest == NULL)
123 goto nla_put_failure;
Patrick McHardy57e1c482008-01-23 20:34:28 -0800124 NLA_PUT_STRING(skb, TCA_KIND, a->ops->kind);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700125 for (i = 0; i < (hinfo->hmask + 1); i++) {
126 p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
127
128 while (p != NULL) {
129 s_p = p->tcfc_next;
130 if (ACT_P_DELETED == tcf_hash_release(p, 0, hinfo))
131 module_put(a->ops->owner);
132 n_i++;
133 p = s_p;
134 }
135 }
Patrick McHardy24beeab2008-01-23 20:34:48 -0800136 NLA_PUT_U32(skb, TCA_FCNT, n_i);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800137 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700138
139 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800140nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800141 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700142 return -EINVAL;
143}
144
145int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
146 int type, struct tc_action *a)
147{
148 struct tcf_hashinfo *hinfo = a->ops->hinfo;
149
150 if (type == RTM_DELACTION) {
151 return tcf_del_walker(skb, a, hinfo);
152 } else if (type == RTM_GETACTION) {
153 return tcf_dump_walker(skb, cb, a, hinfo);
154 } else {
155 printk("tcf_generic_walker: unknown action %d\n", type);
156 return -EINVAL;
157 }
158}
159EXPORT_SYMBOL(tcf_generic_walker);
160
161struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
162{
163 struct tcf_common *p;
164
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +0200165 read_lock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700166 for (p = hinfo->htab[tcf_hash(index, hinfo->hmask)]; p;
167 p = p->tcfc_next) {
168 if (p->tcfc_index == index)
169 break;
170 }
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +0200171 read_unlock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700172
173 return p;
174}
175EXPORT_SYMBOL(tcf_hash_lookup);
176
177u32 tcf_hash_new_index(u32 *idx_gen, struct tcf_hashinfo *hinfo)
178{
179 u32 val = *idx_gen;
180
181 do {
182 if (++val == 0)
183 val = 1;
184 } while (tcf_hash_lookup(val, hinfo));
185
186 return (*idx_gen = val);
187}
188EXPORT_SYMBOL(tcf_hash_new_index);
189
190int tcf_hash_search(struct tc_action *a, u32 index)
191{
192 struct tcf_hashinfo *hinfo = a->ops->hinfo;
193 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
194
195 if (p) {
196 a->priv = p;
197 return 1;
198 }
199 return 0;
200}
201EXPORT_SYMBOL(tcf_hash_search);
202
203struct tcf_common *tcf_hash_check(u32 index, struct tc_action *a, int bind,
204 struct tcf_hashinfo *hinfo)
205{
206 struct tcf_common *p = NULL;
207 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700208 if (bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700209 p->tcfc_bindcnt++;
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700210 p->tcfc_refcnt++;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700211 a->priv = p;
212 }
213 return p;
214}
215EXPORT_SYMBOL(tcf_hash_check);
216
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800217struct 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 -0700218{
219 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
220
221 if (unlikely(!p))
222 return p;
223 p->tcfc_refcnt = 1;
224 if (bind)
225 p->tcfc_bindcnt = 1;
226
227 spin_lock_init(&p->tcfc_lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700228 p->tcfc_index = index ? index : tcf_hash_new_index(idx_gen, hinfo);
229 p->tcfc_tm.install = jiffies;
230 p->tcfc_tm.lastuse = jiffies;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700231 if (est)
232 gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800233 &p->tcfc_lock, est);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700234 a->priv = (void *) p;
235 return p;
236}
237EXPORT_SYMBOL(tcf_hash_create);
238
239void tcf_hash_insert(struct tcf_common *p, struct tcf_hashinfo *hinfo)
240{
241 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
242
243 write_lock_bh(hinfo->lock);
244 p->tcfc_next = hinfo->htab[h];
245 hinfo->htab[h] = p;
246 write_unlock_bh(hinfo->lock);
247}
248EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250static struct tc_action_ops *act_base = NULL;
251static DEFINE_RWLOCK(act_mod_lock);
252
253int tcf_register_action(struct tc_action_ops *act)
254{
255 struct tc_action_ops *a, **ap;
256
257 write_lock(&act_mod_lock);
258 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next) {
259 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
260 write_unlock(&act_mod_lock);
261 return -EEXIST;
262 }
263 }
264 act->next = NULL;
265 *ap = act;
266 write_unlock(&act_mod_lock);
267 return 0;
268}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800269EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271int tcf_unregister_action(struct tc_action_ops *act)
272{
273 struct tc_action_ops *a, **ap;
274 int err = -ENOENT;
275
276 write_lock(&act_mod_lock);
277 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next)
278 if (a == act)
279 break;
280 if (a) {
281 *ap = a->next;
282 a->next = NULL;
283 err = 0;
284 }
285 write_unlock(&act_mod_lock);
286 return err;
287}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800288EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290/* lookup by name */
291static struct tc_action_ops *tc_lookup_action_n(char *kind)
292{
293 struct tc_action_ops *a = NULL;
294
295 if (kind) {
296 read_lock(&act_mod_lock);
297 for (a = act_base; a; a = a->next) {
298 if (strcmp(kind, a->kind) == 0) {
299 if (!try_module_get(a->owner)) {
300 read_unlock(&act_mod_lock);
301 return NULL;
302 }
303 break;
304 }
305 }
306 read_unlock(&act_mod_lock);
307 }
308 return a;
309}
310
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800311/* lookup by nlattr */
312static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 struct tc_action_ops *a = NULL;
315
316 if (kind) {
317 read_lock(&act_mod_lock);
318 for (a = act_base; a; a = a->next) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800319 if (nla_strcmp(kind, a->kind) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (!try_module_get(a->owner)) {
321 read_unlock(&act_mod_lock);
322 return NULL;
323 }
324 break;
325 }
326 }
327 read_unlock(&act_mod_lock);
328 }
329 return a;
330}
331
332#if 0
333/* lookup by id */
334static struct tc_action_ops *tc_lookup_action_id(u32 type)
335{
336 struct tc_action_ops *a = NULL;
337
338 if (type) {
339 read_lock(&act_mod_lock);
340 for (a = act_base; a; a = a->next) {
341 if (a->type == type) {
342 if (!try_module_get(a->owner)) {
343 read_unlock(&act_mod_lock);
344 return NULL;
345 }
346 break;
347 }
348 }
349 read_unlock(&act_mod_lock);
350 }
351 return a;
352}
353#endif
354
355int tcf_action_exec(struct sk_buff *skb, struct tc_action *act,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900356 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 struct tc_action *a;
359 int ret = -1;
360
361 if (skb->tc_verd & TC_NCLS) {
362 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 ret = TC_ACT_OK;
364 goto exec_done;
365 }
366 while ((a = act) != NULL) {
367repeat:
368 if (a->ops && a->ops->act) {
Patrick McHardyf43c5a02006-01-08 22:15:34 -0800369 ret = a->ops->act(skb, a, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (TC_MUNGED & skb->tc_verd) {
371 /* copied already, allow trampling */
372 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
373 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (ret == TC_ACT_REPEAT)
376 goto repeat; /* we need a ttl - JHS */
J Hadi Salim14d50e72005-05-03 16:29:13 -0700377 if (ret != TC_ACT_PIPE)
378 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380 act = a->next;
381 }
382exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return ret;
384}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800385EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387void tcf_action_destroy(struct tc_action *act, int bind)
388{
389 struct tc_action *a;
390
391 for (a = act; a; a = act) {
392 if (a->ops && a->ops->cleanup) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
394 module_put(a->ops->owner);
395 act = act->next;
396 kfree(a);
397 } else { /*FIXME: Remove later - catch insertion bugs*/
398 printk("tcf_action_destroy: BUG? destroying NULL ops\n");
399 act = act->next;
400 kfree(a);
401 }
402 }
403}
404
405int
406tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
407{
408 int err = -EINVAL;
409
410 if (a->ops == NULL || a->ops->dump == NULL)
411 return err;
412 return a->ops->dump(skb, a, bind, ref);
413}
414
415int
416tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
417{
418 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700419 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800420 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 if (a->ops == NULL || a->ops->dump == NULL)
423 return err;
424
Patrick McHardy57e1c482008-01-23 20:34:28 -0800425 NLA_PUT_STRING(skb, TCA_KIND, a->ops->kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800427 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800428 nest = nla_nest_start(skb, TCA_OPTIONS);
429 if (nest == NULL)
430 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800432 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return err;
434 }
435
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800436nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700437 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return -1;
439}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800440EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442int
443tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref)
444{
445 struct tc_action *a;
446 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800447 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 while ((a = act) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 act = a->next;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800451 nest = nla_nest_start(skb, a->order);
452 if (nest == NULL)
453 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 err = tcf_action_dump_1(skb, a, bind, ref);
455 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700456 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800457 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459
460 return 0;
461
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800462nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700463 err = -EINVAL;
464errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800465 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700466 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800469struct tc_action *tcf_action_init_1(struct nlattr *nla, struct nlattr *est,
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800470 char *name, int ovr, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 struct tc_action *a;
473 struct tc_action_ops *a_o;
474 char act_name[IFNAMSIZ];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800475 struct nlattr *tb[TCA_ACT_MAX+1];
476 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800477 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800480 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
481 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800483 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800484 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (kind == NULL)
486 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800487 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 goto err_out;
489 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800490 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
492 goto err_out;
493 }
494
495 a_o = tc_lookup_action_n(act_name);
496 if (a_o == NULL) {
497#ifdef CONFIG_KMOD
498 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800499 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 rtnl_lock();
501
502 a_o = tc_lookup_action_n(act_name);
503
504 /* We dropped the RTNL semaphore in order to
505 * perform the module load. So, even if we
506 * succeeded in loading the module we have to
507 * tell the caller to replay the request. We
508 * indicate this using -EAGAIN.
509 */
510 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800511 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 goto err_mod;
513 }
514#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800515 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 goto err_out;
517 }
518
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800519 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700520 a = kzalloc(sizeof(*a), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (a == NULL)
522 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 /* backward compatibility for policer */
525 if (name == NULL)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800526 err = a_o->init(tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 else
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800528 err = a_o->init(nla, est, a, ovr, bind);
529 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 goto err_free;
531
532 /* module count goes up only when brand new policy is created
533 if it exists and is only bound to in a_o->init() then
534 ACT_P_CREATED is not returned (a zero is).
535 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800536 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 module_put(a_o->owner);
538 a->ops = a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return a;
541
542err_free:
543 kfree(a);
544err_mod:
545 module_put(a_o->owner);
546err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800547 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800550struct tc_action *tcf_action_init(struct nlattr *nla, struct nlattr *est,
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800551 char *name, int ovr, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800553 struct nlattr *tb[TCA_ACT_MAX_PRIO+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 struct tc_action *head = NULL, *act, *act_prev = NULL;
Patrick McHardycee63722008-01-23 20:33:32 -0800555 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 int i;
557
Patrick McHardycee63722008-01-23 20:33:32 -0800558 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
559 if (err < 0)
560 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800562 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800563 act = tcf_action_init_1(tb[i], est, name, ovr, bind);
564 if (IS_ERR(act))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 goto err;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800566 act->order = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 if (head == NULL)
569 head = act;
570 else
571 act_prev->next = act;
572 act_prev = act;
573 }
574 return head;
575
576err:
577 if (head != NULL)
578 tcf_action_destroy(head, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800579 return act;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
582int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
583 int compat_mode)
584{
585 int err = 0;
586 struct gnet_dump d;
587 struct tcf_act_hdr *h = a->priv;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (h == NULL)
590 goto errout;
591
592 /* compat_mode being true specifies a call that is supposed
593 * to add additional backward compatiblity statistic TLVs.
594 */
595 if (compat_mode) {
596 if (a->type == TCA_OLD_COMPAT)
597 err = gnet_stats_start_copy_compat(skb, 0,
Patrick McHardy4bdf3992007-07-02 22:47:37 -0700598 TCA_STATS, TCA_XSTATS, &h->tcf_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 else
600 return 0;
601 } else
602 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
Patrick McHardy4bdf3992007-07-02 22:47:37 -0700603 &h->tcf_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 if (err < 0)
606 goto errout;
607
608 if (a->ops != NULL && a->ops->get_stats != NULL)
609 if (a->ops->get_stats(skb, a) < 0)
610 goto errout;
611
David S. Millere9ce1cd2006-08-21 23:54:55 -0700612 if (gnet_stats_copy_basic(&d, &h->tcf_bstats) < 0 ||
David S. Millere9ce1cd2006-08-21 23:54:55 -0700613 gnet_stats_copy_rate_est(&d, &h->tcf_rate_est) < 0 ||
David S. Millere9ce1cd2006-08-21 23:54:55 -0700614 gnet_stats_copy_queue(&d, &h->tcf_qstats) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 goto errout;
616
617 if (gnet_stats_finish_copy(&d) < 0)
618 goto errout;
619
620 return 0;
621
622errout:
623 return -1;
624}
625
626static int
627tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, u32 seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900628 u16 flags, int event, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 struct tcamsg *t;
631 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700632 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800633 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700635 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 t = NLMSG_DATA(nlh);
638 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700639 t->tca__pad1 = 0;
640 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900641
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800642 nest = nla_nest_start(skb, TCA_ACT_TAB);
643 if (nest == NULL)
644 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 if (tcf_action_dump(skb, a, bind, ref) < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800647 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800649 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900650
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700651 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return skb->len;
653
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800654nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700656 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return -1;
658}
659
660static int
661act_get_notify(u32 pid, struct nlmsghdr *n, struct tc_action *a, int event)
662{
663 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
666 if (!skb)
667 return -ENOBUFS;
668 if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
669 kfree_skb(skb);
670 return -EINVAL;
671 }
Thomas Graf2942e902006-08-15 00:30:25 -0700672
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800673 return rtnl_unicast(skb, &init_net, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
676static struct tc_action *
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800677tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800679 struct nlattr *tb[TCA_ACT_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 struct tc_action *a;
681 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800682 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Patrick McHardycee63722008-01-23 20:33:32 -0800684 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
685 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800686 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Patrick McHardycee63722008-01-23 20:33:32 -0800688 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800689 if (tb[TCA_ACT_INDEX] == NULL ||
690 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800691 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800692 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800694 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700695 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (a == NULL)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800697 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800699 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800700 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (a->ops == NULL)
702 goto err_free;
703 if (a->ops->lookup == NULL)
704 goto err_mod;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800705 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (a->ops->lookup(a, index) == 0)
707 goto err_mod;
708
709 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712err_mod:
713 module_put(a->ops->owner);
714err_free:
715 kfree(a);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800716err_out:
717 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
720static void cleanup_a(struct tc_action *act)
721{
722 struct tc_action *a;
723
724 for (a = act; a; a = act) {
725 act = a->next;
726 kfree(a);
727 }
728}
729
730static struct tc_action *create_a(int i)
731{
732 struct tc_action *act;
733
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700734 act = kzalloc(sizeof(*act), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (act == NULL) {
736 printk("create_a: failed to alloc!\n");
737 return NULL;
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 act->order = i;
740 return act;
741}
742
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800743static int tca_action_flush(struct nlattr *nla, struct nlmsghdr *n, u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 struct sk_buff *skb;
746 unsigned char *b;
747 struct nlmsghdr *nlh;
748 struct tcamsg *t;
749 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800750 struct nlattr *nest;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800751 struct nlattr *tb[TCA_ACT_MAX+1];
752 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 struct tc_action *a = create_a(0);
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700754 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 if (a == NULL) {
757 printk("tca_action_flush: couldnt create tc_action\n");
758 return err;
759 }
760
761 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
762 if (!skb) {
763 printk("tca_action_flush: failed skb alloc\n");
764 kfree(a);
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700765 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700768 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Patrick McHardycee63722008-01-23 20:33:32 -0800770 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
771 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 goto err_out;
773
Patrick McHardycee63722008-01-23 20:33:32 -0800774 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800775 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 a->ops = tc_lookup_action(kind);
777 if (a->ops == NULL)
778 goto err_out;
779
780 nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t));
781 t = NLMSG_DATA(nlh);
782 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700783 t->tca__pad1 = 0;
784 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800786 nest = nla_nest_start(skb, TCA_ACT_TAB);
787 if (nest == NULL)
788 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
791 if (err < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800792 goto nla_put_failure;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700793 if (err == 0)
794 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800796 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700798 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 nlh->nlmsg_flags |= NLM_F_ROOT;
800 module_put(a->ops->owner);
801 kfree(a);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800802 err = rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (err > 0)
804 return 0;
805
806 return err;
807
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800808nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809nlmsg_failure:
Thomas Grafebbaeab2006-07-09 11:36:23 -0700810 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700812noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 kfree_skb(skb);
814 kfree(a);
815 return err;
816}
817
818static int
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800819tca_action_gd(struct nlattr *nla, struct nlmsghdr *n, u32 pid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Patrick McHardycee63722008-01-23 20:33:32 -0800821 int i, ret;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800822 struct nlattr *tb[TCA_ACT_MAX_PRIO+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 struct tc_action *head = NULL, *act, *act_prev = NULL;
824
Patrick McHardycee63722008-01-23 20:33:32 -0800825 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
826 if (ret < 0)
827 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700830 if (tb[1] != NULL)
831 return tca_action_flush(tb[1], n, pid);
832 else
833 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800836 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800837 act = tcf_action_get_1(tb[i], n, pid);
838 if (IS_ERR(act)) {
839 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800841 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800842 act->order = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 if (head == NULL)
845 head = act;
846 else
847 act_prev->next = act;
848 act_prev = act;
849 }
850
851 if (event == RTM_GETACTION)
852 ret = act_get_notify(pid, n, head, event);
853 else { /* delete */
854 struct sk_buff *skb;
855
856 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
857 if (!skb) {
858 ret = -ENOBUFS;
859 goto err;
860 }
861
862 if (tca_get_fill(skb, head, pid, n->nlmsg_seq, 0, event,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900863 0, 1) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 kfree_skb(skb);
865 ret = -EINVAL;
866 goto err;
867 }
868
869 /* now do the delete */
870 tcf_action_destroy(head, 0);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800871 ret = rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900872 n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (ret > 0)
874 return 0;
875 return ret;
876 }
877err:
878 cleanup_a(head);
879 return ret;
880}
881
882static int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900883 u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
885 struct tcamsg *t;
886 struct nlmsghdr *nlh;
887 struct sk_buff *skb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800888 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 unsigned char *b;
890 int err = 0;
891
892 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
893 if (!skb)
894 return -ENOBUFS;
895
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700896 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700898 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 t = NLMSG_DATA(nlh);
900 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700901 t->tca__pad1 = 0;
902 t->tca__pad2 = 0;
903
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800904 nest = nla_nest_start(skb, TCA_ACT_TAB);
905 if (nest == NULL)
906 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 if (tcf_action_dump(skb, a, 0, 0) < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800909 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800911 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900912
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700913 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Patrick McHardyac6d4392005-08-14 19:29:52 -0700914 NETLINK_CB(skb).dst_group = RTNLGRP_TC;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900915
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800916 err = rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (err > 0)
918 err = 0;
919 return err;
920
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800921nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922nlmsg_failure:
Patrick McHardyf6e57462006-03-12 20:33:22 -0800923 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return -1;
925}
926
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928static int
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800929tcf_action_add(struct nlattr *nla, struct nlmsghdr *n, u32 pid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
931 int ret = 0;
932 struct tc_action *act;
933 struct tc_action *a;
934 u32 seq = n->nlmsg_seq;
935
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800936 act = tcf_action_init(nla, NULL, NULL, ovr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (act == NULL)
938 goto done;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800939 if (IS_ERR(act)) {
940 ret = PTR_ERR(act);
941 goto done;
942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 /* dump then free all the actions after update; inserted policy
945 * stays intact
946 * */
947 ret = tcf_add_notify(act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
948 for (a = act; a; a = act) {
949 act = a->next;
950 kfree(a);
951 }
952done:
953 return ret;
954}
955
956static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
957{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900958 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800959 struct nlattr *tca[TCA_ACT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 u32 pid = skb ? NETLINK_CB(skb).pid : 0;
961 int ret = 0, ovr = 0;
962
Denis V. Lunevb8542722007-12-01 00:21:31 +1100963 if (net != &init_net)
964 return -EINVAL;
965
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800966 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
967 if (ret < 0)
968 return ret;
969
970 if (tca[TCA_ACT_TAB] == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 printk("tc_ctl_action: received NO action attribs\n");
972 return -EINVAL;
973 }
974
975 /* n->nlmsg_flags&NLM_F_CREATE
976 * */
977 switch (n->nlmsg_type) {
978 case RTM_NEWACTION:
979 /* we are going to assume all other flags
980 * imply create only if it doesnt exist
981 * Note that CREATE | EXCL implies that
982 * but since we want avoid ambiguity (eg when flags
983 * is zero) then just set this
984 */
985 if (n->nlmsg_flags&NLM_F_REPLACE)
986 ovr = 1;
987replay:
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800988 ret = tcf_action_add(tca[TCA_ACT_TAB], n, pid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (ret == -EAGAIN)
990 goto replay;
991 break;
992 case RTM_DELACTION:
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800993 ret = tca_action_gd(tca[TCA_ACT_TAB], n, pid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 break;
995 case RTM_GETACTION:
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800996 ret = tca_action_gd(tca[TCA_ACT_TAB], n, pid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 break;
998 default:
999 BUG();
1000 }
1001
1002 return ret;
1003}
1004
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001005static struct nlattr *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006find_dump_kind(struct nlmsghdr *n)
1007{
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001008 struct nlattr *tb1, *tb2[TCA_ACT_MAX+1];
1009 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1010 struct nlattr *nla[TCAA_MAX + 1];
1011 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Patrick McHardyc96c9472008-01-23 20:32:58 -08001013 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001015 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (tb1 == NULL)
1017 return NULL;
1018
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001019 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1020 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Patrick McHardy6d834e02008-01-23 20:32:42 -08001023 if (tb[1] == NULL)
1024 return NULL;
1025 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1026 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001028 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Thomas Graf26dab892006-07-05 20:45:06 -07001030 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
1033static int
1034tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1035{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001036 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001038 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001039 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 struct tc_action_ops *a_o;
1041 struct tc_action a;
1042 int ret = 0;
1043 struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001044 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Denis V. Lunevb8542722007-12-01 00:21:31 +11001046 if (net != &init_net)
1047 return 0;
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (kind == NULL) {
1050 printk("tc_dump_action: action bad kind\n");
1051 return 0;
1052 }
1053
Thomas Graf26dab892006-07-05 20:45:06 -07001054 a_o = tc_lookup_action(kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (a_o == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return 0;
1057 }
1058
1059 memset(&a, 0, sizeof(struct tc_action));
1060 a.ops = a_o;
1061
1062 if (a_o->walk == NULL) {
Thomas Graf26dab892006-07-05 20:45:06 -07001063 printk("tc_dump_action: %s !capable of dumping table\n", a_o->kind);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001064 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066
1067 nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001068 cb->nlh->nlmsg_type, sizeof(*t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 t = NLMSG_DATA(nlh);
1070 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001071 t->tca__pad1 = 0;
1072 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001074 nest = nla_nest_start(skb, TCA_ACT_TAB);
1075 if (nest == NULL)
1076 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1079 if (ret < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001080 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001083 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 ret = skb->len;
1085 } else
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001086 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001088 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (NETLINK_CB(cb->skb).pid && ret)
1090 nlh->nlmsg_flags |= NLM_F_MULTI;
1091 module_put(a_o->owner);
1092 return skb->len;
1093
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001094nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095nlmsg_failure:
1096 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001097 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return skb->len;
1099}
1100
1101static int __init tc_action_init(void)
1102{
Thomas Graf708914c2007-03-22 11:56:59 -07001103 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL);
1104 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL);
1105 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return 0;
1108}
1109
1110subsys_initcall(tc_action_init);