blob: 019045174fc35f0812565c143793ca3fa382a8c7 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/init.h>
21#include <linux/kmod.h>
Patrick McHardyab27cfb2008-01-23 20:33:13 -080022#include <linux/err.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110023#include <net/net_namespace.h>
24#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <net/sch_generic.h>
26#include <net/act_api.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070027#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
David S. Millere9ce1cd2006-08-21 23:54:55 -070029void tcf_hash_destroy(struct tcf_common *p, struct tcf_hashinfo *hinfo)
30{
31 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
32 struct tcf_common **p1p;
33
34 for (p1p = &hinfo->htab[h]; *p1p; p1p = &(*p1p)->tcfc_next) {
35 if (*p1p == p) {
36 write_lock_bh(hinfo->lock);
37 *p1p = p->tcfc_next;
38 write_unlock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -070039 gen_kill_estimator(&p->tcfc_bstats,
40 &p->tcfc_rate_est);
David S. Millere9ce1cd2006-08-21 23:54:55 -070041 kfree(p);
42 return;
43 }
44 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -070045 WARN_ON(1);
David S. Millere9ce1cd2006-08-21 23:54:55 -070046}
47EXPORT_SYMBOL(tcf_hash_destroy);
48
49int tcf_hash_release(struct tcf_common *p, int bind,
50 struct tcf_hashinfo *hinfo)
51{
52 int ret = 0;
53
54 if (p) {
55 if (bind)
56 p->tcfc_bindcnt--;
57
58 p->tcfc_refcnt--;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090059 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070060 tcf_hash_destroy(p, hinfo);
61 ret = 1;
62 }
63 }
64 return ret;
65}
66EXPORT_SYMBOL(tcf_hash_release);
67
68static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
69 struct tc_action *a, struct tcf_hashinfo *hinfo)
70{
71 struct tcf_common *p;
72 int err = 0, index = -1,i = 0, s_i = 0, n_i = 0;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080073 struct nlattr *nest;
David S. Millere9ce1cd2006-08-21 23:54:55 -070074
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +020075 read_lock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -070076
77 s_i = cb->args[0];
78
79 for (i = 0; i < (hinfo->hmask + 1); i++) {
80 p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
81
82 for (; p; p = p->tcfc_next) {
83 index++;
84 if (index < s_i)
85 continue;
86 a->priv = p;
87 a->order = n_i;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080088
89 nest = nla_nest_start(skb, a->order);
90 if (nest == NULL)
91 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -070092 err = tcf_action_dump_1(skb, a, 0, 0);
93 if (err < 0) {
94 index--;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080095 nlmsg_trim(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -070096 goto done;
97 }
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080098 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -070099 n_i++;
100 if (n_i >= TCA_ACT_MAX_PRIO)
101 goto done;
102 }
103 }
104done:
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +0200105 read_unlock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700106 if (n_i)
107 cb->args[0] += n_i;
108 return n_i;
109
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800110nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800111 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700112 goto done;
113}
114
115static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a,
116 struct tcf_hashinfo *hinfo)
117{
118 struct tcf_common *p, *s_p;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800119 struct nlattr *nest;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700120 int i= 0, n_i = 0;
121
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800122 nest = nla_nest_start(skb, a->order);
123 if (nest == NULL)
124 goto nla_put_failure;
Patrick McHardy57e1c482008-01-23 20:34:28 -0800125 NLA_PUT_STRING(skb, TCA_KIND, a->ops->kind);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700126 for (i = 0; i < (hinfo->hmask + 1); i++) {
127 p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
128
129 while (p != NULL) {
130 s_p = p->tcfc_next;
131 if (ACT_P_DELETED == tcf_hash_release(p, 0, hinfo))
132 module_put(a->ops->owner);
133 n_i++;
134 p = s_p;
135 }
136 }
Patrick McHardy24beeab2008-01-23 20:34:48 -0800137 NLA_PUT_U32(skb, TCA_FCNT, n_i);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800138 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700139
140 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800141nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800142 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700143 return -EINVAL;
144}
145
146int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
147 int type, struct tc_action *a)
148{
149 struct tcf_hashinfo *hinfo = a->ops->hinfo;
150
151 if (type == RTM_DELACTION) {
152 return tcf_del_walker(skb, a, hinfo);
153 } else if (type == RTM_GETACTION) {
154 return tcf_dump_walker(skb, cb, a, hinfo);
155 } else {
156 printk("tcf_generic_walker: unknown action %d\n", type);
157 return -EINVAL;
158 }
159}
160EXPORT_SYMBOL(tcf_generic_walker);
161
162struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
163{
164 struct tcf_common *p;
165
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +0200166 read_lock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700167 for (p = hinfo->htab[tcf_hash(index, hinfo->hmask)]; p;
168 p = p->tcfc_next) {
169 if (p->tcfc_index == index)
170 break;
171 }
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +0200172 read_unlock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700173
174 return p;
175}
176EXPORT_SYMBOL(tcf_hash_lookup);
177
178u32 tcf_hash_new_index(u32 *idx_gen, struct tcf_hashinfo *hinfo)
179{
180 u32 val = *idx_gen;
181
182 do {
183 if (++val == 0)
184 val = 1;
185 } while (tcf_hash_lookup(val, hinfo));
186
187 return (*idx_gen = val);
188}
189EXPORT_SYMBOL(tcf_hash_new_index);
190
191int tcf_hash_search(struct tc_action *a, u32 index)
192{
193 struct tcf_hashinfo *hinfo = a->ops->hinfo;
194 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
195
196 if (p) {
197 a->priv = p;
198 return 1;
199 }
200 return 0;
201}
202EXPORT_SYMBOL(tcf_hash_search);
203
204struct tcf_common *tcf_hash_check(u32 index, struct tc_action *a, int bind,
205 struct tcf_hashinfo *hinfo)
206{
207 struct tcf_common *p = NULL;
208 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700209 if (bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700210 p->tcfc_bindcnt++;
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700211 p->tcfc_refcnt++;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700212 a->priv = p;
213 }
214 return p;
215}
216EXPORT_SYMBOL(tcf_hash_check);
217
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800218struct tcf_common *tcf_hash_create(u32 index, struct nlattr *est,
219 struct tc_action *a, int size, int bind,
220 u32 *idx_gen, struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700221{
222 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
223
224 if (unlikely(!p))
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800225 return ERR_PTR(-ENOMEM);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700226 p->tcfc_refcnt = 1;
227 if (bind)
228 p->tcfc_bindcnt = 1;
229
230 spin_lock_init(&p->tcfc_lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700231 p->tcfc_index = index ? index : tcf_hash_new_index(idx_gen, hinfo);
232 p->tcfc_tm.install = jiffies;
233 p->tcfc_tm.lastuse = jiffies;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800234 if (est) {
235 int err = gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
236 &p->tcfc_lock, est);
237 if (err) {
238 kfree(p);
239 return ERR_PTR(err);
240 }
241 }
242
David S. Millere9ce1cd2006-08-21 23:54:55 -0700243 a->priv = (void *) p;
244 return p;
245}
246EXPORT_SYMBOL(tcf_hash_create);
247
248void tcf_hash_insert(struct tcf_common *p, struct tcf_hashinfo *hinfo)
249{
250 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
251
252 write_lock_bh(hinfo->lock);
253 p->tcfc_next = hinfo->htab[h];
254 hinfo->htab[h] = p;
255 write_unlock_bh(hinfo->lock);
256}
257EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259static struct tc_action_ops *act_base = NULL;
260static DEFINE_RWLOCK(act_mod_lock);
261
262int tcf_register_action(struct tc_action_ops *act)
263{
264 struct tc_action_ops *a, **ap;
265
266 write_lock(&act_mod_lock);
267 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next) {
268 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
269 write_unlock(&act_mod_lock);
270 return -EEXIST;
271 }
272 }
273 act->next = NULL;
274 *ap = act;
275 write_unlock(&act_mod_lock);
276 return 0;
277}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800278EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280int tcf_unregister_action(struct tc_action_ops *act)
281{
282 struct tc_action_ops *a, **ap;
283 int err = -ENOENT;
284
285 write_lock(&act_mod_lock);
286 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next)
287 if (a == act)
288 break;
289 if (a) {
290 *ap = a->next;
291 a->next = NULL;
292 err = 0;
293 }
294 write_unlock(&act_mod_lock);
295 return err;
296}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800297EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299/* lookup by name */
300static struct tc_action_ops *tc_lookup_action_n(char *kind)
301{
302 struct tc_action_ops *a = NULL;
303
304 if (kind) {
305 read_lock(&act_mod_lock);
306 for (a = act_base; a; a = a->next) {
307 if (strcmp(kind, a->kind) == 0) {
308 if (!try_module_get(a->owner)) {
309 read_unlock(&act_mod_lock);
310 return NULL;
311 }
312 break;
313 }
314 }
315 read_unlock(&act_mod_lock);
316 }
317 return a;
318}
319
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800320/* lookup by nlattr */
321static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 struct tc_action_ops *a = NULL;
324
325 if (kind) {
326 read_lock(&act_mod_lock);
327 for (a = act_base; a; a = a->next) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800328 if (nla_strcmp(kind, a->kind) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (!try_module_get(a->owner)) {
330 read_unlock(&act_mod_lock);
331 return NULL;
332 }
333 break;
334 }
335 }
336 read_unlock(&act_mod_lock);
337 }
338 return a;
339}
340
341#if 0
342/* lookup by id */
343static struct tc_action_ops *tc_lookup_action_id(u32 type)
344{
345 struct tc_action_ops *a = NULL;
346
347 if (type) {
348 read_lock(&act_mod_lock);
349 for (a = act_base; a; a = a->next) {
350 if (a->type == type) {
351 if (!try_module_get(a->owner)) {
352 read_unlock(&act_mod_lock);
353 return NULL;
354 }
355 break;
356 }
357 }
358 read_unlock(&act_mod_lock);
359 }
360 return a;
361}
362#endif
363
364int tcf_action_exec(struct sk_buff *skb, struct tc_action *act,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900365 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 struct tc_action *a;
368 int ret = -1;
369
370 if (skb->tc_verd & TC_NCLS) {
371 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 ret = TC_ACT_OK;
373 goto exec_done;
374 }
375 while ((a = act) != NULL) {
376repeat:
377 if (a->ops && a->ops->act) {
Patrick McHardyf43c5a02006-01-08 22:15:34 -0800378 ret = a->ops->act(skb, a, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (TC_MUNGED & skb->tc_verd) {
380 /* copied already, allow trampling */
381 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
382 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (ret == TC_ACT_REPEAT)
385 goto repeat; /* we need a ttl - JHS */
J Hadi Salim14d50e72005-05-03 16:29:13 -0700386 if (ret != TC_ACT_PIPE)
387 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389 act = a->next;
390 }
391exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return ret;
393}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800394EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396void tcf_action_destroy(struct tc_action *act, int bind)
397{
398 struct tc_action *a;
399
400 for (a = act; a; a = act) {
401 if (a->ops && a->ops->cleanup) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
403 module_put(a->ops->owner);
404 act = act->next;
405 kfree(a);
406 } else { /*FIXME: Remove later - catch insertion bugs*/
407 printk("tcf_action_destroy: BUG? destroying NULL ops\n");
408 act = act->next;
409 kfree(a);
410 }
411 }
412}
413
414int
415tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
416{
417 int err = -EINVAL;
418
419 if (a->ops == NULL || a->ops->dump == NULL)
420 return err;
421 return a->ops->dump(skb, a, bind, ref);
422}
423
424int
425tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
426{
427 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700428 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800429 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 if (a->ops == NULL || a->ops->dump == NULL)
432 return err;
433
Patrick McHardy57e1c482008-01-23 20:34:28 -0800434 NLA_PUT_STRING(skb, TCA_KIND, a->ops->kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800436 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800437 nest = nla_nest_start(skb, TCA_OPTIONS);
438 if (nest == NULL)
439 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800441 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return err;
443 }
444
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800445nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700446 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return -1;
448}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800449EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451int
452tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref)
453{
454 struct tc_action *a;
455 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800456 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 while ((a = act) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 act = a->next;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800460 nest = nla_nest_start(skb, a->order);
461 if (nest == NULL)
462 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 err = tcf_action_dump_1(skb, a, bind, ref);
464 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700465 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800466 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468
469 return 0;
470
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800471nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700472 err = -EINVAL;
473errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800474 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700475 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800478struct tc_action *tcf_action_init_1(struct nlattr *nla, struct nlattr *est,
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800479 char *name, int ovr, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 struct tc_action *a;
482 struct tc_action_ops *a_o;
483 char act_name[IFNAMSIZ];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800484 struct nlattr *tb[TCA_ACT_MAX+1];
485 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800486 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800489 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
490 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800492 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800493 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (kind == NULL)
495 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800496 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 goto err_out;
498 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800499 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
501 goto err_out;
502 }
503
504 a_o = tc_lookup_action_n(act_name);
505 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700506#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800508 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 rtnl_lock();
510
511 a_o = tc_lookup_action_n(act_name);
512
513 /* We dropped the RTNL semaphore in order to
514 * perform the module load. So, even if we
515 * succeeded in loading the module we have to
516 * tell the caller to replay the request. We
517 * indicate this using -EAGAIN.
518 */
519 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800520 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 goto err_mod;
522 }
523#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800524 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto err_out;
526 }
527
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800528 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700529 a = kzalloc(sizeof(*a), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (a == NULL)
531 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /* backward compatibility for policer */
534 if (name == NULL)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800535 err = a_o->init(tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 else
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800537 err = a_o->init(nla, est, a, ovr, bind);
538 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 goto err_free;
540
541 /* module count goes up only when brand new policy is created
542 if it exists and is only bound to in a_o->init() then
543 ACT_P_CREATED is not returned (a zero is).
544 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800545 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 module_put(a_o->owner);
547 a->ops = a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return a;
550
551err_free:
552 kfree(a);
553err_mod:
554 module_put(a_o->owner);
555err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800556 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800559struct tc_action *tcf_action_init(struct nlattr *nla, struct nlattr *est,
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800560 char *name, int ovr, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800562 struct nlattr *tb[TCA_ACT_MAX_PRIO+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 struct tc_action *head = NULL, *act, *act_prev = NULL;
Patrick McHardycee63722008-01-23 20:33:32 -0800564 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int i;
566
Patrick McHardycee63722008-01-23 20:33:32 -0800567 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
568 if (err < 0)
569 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800571 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800572 act = tcf_action_init_1(tb[i], est, name, ovr, bind);
573 if (IS_ERR(act))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 goto err;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800575 act->order = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 if (head == NULL)
578 head = act;
579 else
580 act_prev->next = act;
581 act_prev = act;
582 }
583 return head;
584
585err:
586 if (head != NULL)
587 tcf_action_destroy(head, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800588 return act;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
592 int compat_mode)
593{
594 int err = 0;
595 struct gnet_dump d;
596 struct tcf_act_hdr *h = a->priv;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (h == NULL)
599 goto errout;
600
601 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400602 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 */
604 if (compat_mode) {
605 if (a->type == TCA_OLD_COMPAT)
606 err = gnet_stats_start_copy_compat(skb, 0,
Patrick McHardy4bdf3992007-07-02 22:47:37 -0700607 TCA_STATS, TCA_XSTATS, &h->tcf_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 else
609 return 0;
610 } else
611 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
Patrick McHardy4bdf3992007-07-02 22:47:37 -0700612 &h->tcf_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 if (err < 0)
615 goto errout;
616
617 if (a->ops != NULL && a->ops->get_stats != NULL)
618 if (a->ops->get_stats(skb, a) < 0)
619 goto errout;
620
David S. Millere9ce1cd2006-08-21 23:54:55 -0700621 if (gnet_stats_copy_basic(&d, &h->tcf_bstats) < 0 ||
Eric Dumazetd250a5f2009-10-02 10:32:18 +0000622 gnet_stats_copy_rate_est(&d, &h->tcf_bstats,
623 &h->tcf_rate_est) < 0 ||
David S. Millere9ce1cd2006-08-21 23:54:55 -0700624 gnet_stats_copy_queue(&d, &h->tcf_qstats) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 goto errout;
626
627 if (gnet_stats_finish_copy(&d) < 0)
628 goto errout;
629
630 return 0;
631
632errout:
633 return -1;
634}
635
636static int
637tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, u32 seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900638 u16 flags, int event, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 struct tcamsg *t;
641 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700642 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800643 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700645 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 t = NLMSG_DATA(nlh);
648 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700649 t->tca__pad1 = 0;
650 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900651
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800652 nest = nla_nest_start(skb, TCA_ACT_TAB);
653 if (nest == NULL)
654 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 if (tcf_action_dump(skb, a, bind, ref) < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800657 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800659 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900660
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700661 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return skb->len;
663
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800664nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700666 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return -1;
668}
669
670static int
Tom Goff7316ae82010-03-19 15:40:13 +0000671act_get_notify(struct net *net, u32 pid, struct nlmsghdr *n,
672 struct tc_action *a, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
677 if (!skb)
678 return -ENOBUFS;
679 if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
680 kfree_skb(skb);
681 return -EINVAL;
682 }
Thomas Graf2942e902006-08-15 00:30:25 -0700683
Tom Goff7316ae82010-03-19 15:40:13 +0000684 return rtnl_unicast(skb, net, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
687static struct tc_action *
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800688tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800690 struct nlattr *tb[TCA_ACT_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 struct tc_action *a;
692 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800693 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Patrick McHardycee63722008-01-23 20:33:32 -0800695 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
696 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800697 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Patrick McHardycee63722008-01-23 20:33:32 -0800699 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800700 if (tb[TCA_ACT_INDEX] == NULL ||
701 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800702 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800703 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800705 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700706 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (a == NULL)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800708 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800710 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800711 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (a->ops == NULL)
713 goto err_free;
714 if (a->ops->lookup == NULL)
715 goto err_mod;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800716 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (a->ops->lookup(a, index) == 0)
718 goto err_mod;
719
720 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723err_mod:
724 module_put(a->ops->owner);
725err_free:
726 kfree(a);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800727err_out:
728 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
731static void cleanup_a(struct tc_action *act)
732{
733 struct tc_action *a;
734
735 for (a = act; a; a = act) {
736 act = a->next;
737 kfree(a);
738 }
739}
740
741static struct tc_action *create_a(int i)
742{
743 struct tc_action *act;
744
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700745 act = kzalloc(sizeof(*act), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (act == NULL) {
747 printk("create_a: failed to alloc!\n");
748 return NULL;
749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 act->order = i;
751 return act;
752}
753
Tom Goff7316ae82010-03-19 15:40:13 +0000754static int tca_action_flush(struct net *net, struct nlattr *nla,
755 struct nlmsghdr *n, u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 struct sk_buff *skb;
758 unsigned char *b;
759 struct nlmsghdr *nlh;
760 struct tcamsg *t;
761 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800762 struct nlattr *nest;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800763 struct nlattr *tb[TCA_ACT_MAX+1];
764 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 struct tc_action *a = create_a(0);
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700766 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 if (a == NULL) {
769 printk("tca_action_flush: couldnt create tc_action\n");
770 return err;
771 }
772
773 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
774 if (!skb) {
775 printk("tca_action_flush: failed skb alloc\n");
776 kfree(a);
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700777 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700780 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Patrick McHardycee63722008-01-23 20:33:32 -0800782 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
783 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 goto err_out;
785
Patrick McHardycee63722008-01-23 20:33:32 -0800786 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800787 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 a->ops = tc_lookup_action(kind);
789 if (a->ops == NULL)
790 goto err_out;
791
792 nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t));
793 t = NLMSG_DATA(nlh);
794 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700795 t->tca__pad1 = 0;
796 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800798 nest = nla_nest_start(skb, TCA_ACT_TAB);
799 if (nest == NULL)
800 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
803 if (err < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800804 goto nla_put_failure;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700805 if (err == 0)
806 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800808 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700810 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 nlh->nlmsg_flags |= NLM_F_ROOT;
812 module_put(a->ops->owner);
813 kfree(a);
Tom Goff7316ae82010-03-19 15:40:13 +0000814 err = rtnetlink_send(skb, net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (err > 0)
816 return 0;
817
818 return err;
819
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800820nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821nlmsg_failure:
Thomas Grafebbaeab2006-07-09 11:36:23 -0700822 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700824noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 kfree_skb(skb);
826 kfree(a);
827 return err;
828}
829
830static int
Tom Goff7316ae82010-03-19 15:40:13 +0000831tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
832 u32 pid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Patrick McHardycee63722008-01-23 20:33:32 -0800834 int i, ret;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800835 struct nlattr *tb[TCA_ACT_MAX_PRIO+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 struct tc_action *head = NULL, *act, *act_prev = NULL;
837
Patrick McHardycee63722008-01-23 20:33:32 -0800838 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
839 if (ret < 0)
840 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700843 if (tb[1] != NULL)
Tom Goff7316ae82010-03-19 15:40:13 +0000844 return tca_action_flush(net, tb[1], n, pid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700845 else
846 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800849 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800850 act = tcf_action_get_1(tb[i], n, pid);
851 if (IS_ERR(act)) {
852 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800854 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800855 act->order = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 if (head == NULL)
858 head = act;
859 else
860 act_prev->next = act;
861 act_prev = act;
862 }
863
864 if (event == RTM_GETACTION)
Tom Goff7316ae82010-03-19 15:40:13 +0000865 ret = act_get_notify(net, pid, n, head, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 else { /* delete */
867 struct sk_buff *skb;
868
869 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
870 if (!skb) {
871 ret = -ENOBUFS;
872 goto err;
873 }
874
875 if (tca_get_fill(skb, head, pid, n->nlmsg_seq, 0, event,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900876 0, 1) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 kfree_skb(skb);
878 ret = -EINVAL;
879 goto err;
880 }
881
882 /* now do the delete */
883 tcf_action_destroy(head, 0);
Tom Goff7316ae82010-03-19 15:40:13 +0000884 ret = rtnetlink_send(skb, net, pid, RTNLGRP_TC,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900885 n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (ret > 0)
887 return 0;
888 return ret;
889 }
890err:
891 cleanup_a(head);
892 return ret;
893}
894
Tom Goff7316ae82010-03-19 15:40:13 +0000895static int tcf_add_notify(struct net *net, struct tc_action *a,
896 u32 pid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
898 struct tcamsg *t;
899 struct nlmsghdr *nlh;
900 struct sk_buff *skb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800901 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 unsigned char *b;
903 int err = 0;
904
905 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
906 if (!skb)
907 return -ENOBUFS;
908
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700909 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700911 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 t = NLMSG_DATA(nlh);
913 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700914 t->tca__pad1 = 0;
915 t->tca__pad2 = 0;
916
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800917 nest = nla_nest_start(skb, TCA_ACT_TAB);
918 if (nest == NULL)
919 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (tcf_action_dump(skb, a, 0, 0) < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800922 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800924 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900925
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700926 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Patrick McHardyac6d4392005-08-14 19:29:52 -0700927 NETLINK_CB(skb).dst_group = RTNLGRP_TC;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900928
Tom Goff7316ae82010-03-19 15:40:13 +0000929 err = rtnetlink_send(skb, net, pid, RTNLGRP_TC, flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (err > 0)
931 err = 0;
932 return err;
933
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800934nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935nlmsg_failure:
Patrick McHardyf6e57462006-03-12 20:33:22 -0800936 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return -1;
938}
939
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941static int
Tom Goff7316ae82010-03-19 15:40:13 +0000942tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
943 u32 pid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
945 int ret = 0;
946 struct tc_action *act;
947 struct tc_action *a;
948 u32 seq = n->nlmsg_seq;
949
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800950 act = tcf_action_init(nla, NULL, NULL, ovr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (act == NULL)
952 goto done;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800953 if (IS_ERR(act)) {
954 ret = PTR_ERR(act);
955 goto done;
956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 /* dump then free all the actions after update; inserted policy
959 * stays intact
960 * */
Tom Goff7316ae82010-03-19 15:40:13 +0000961 ret = tcf_add_notify(net, act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 for (a = act; a; a = act) {
963 act = a->next;
964 kfree(a);
965 }
966done:
967 return ret;
968}
969
970static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
971{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900972 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800973 struct nlattr *tca[TCA_ACT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 u32 pid = skb ? NETLINK_CB(skb).pid : 0;
975 int ret = 0, ovr = 0;
976
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800977 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
978 if (ret < 0)
979 return ret;
980
981 if (tca[TCA_ACT_TAB] == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 printk("tc_ctl_action: received NO action attribs\n");
983 return -EINVAL;
984 }
985
986 /* n->nlmsg_flags&NLM_F_CREATE
987 * */
988 switch (n->nlmsg_type) {
989 case RTM_NEWACTION:
990 /* we are going to assume all other flags
991 * imply create only if it doesnt exist
992 * Note that CREATE | EXCL implies that
993 * but since we want avoid ambiguity (eg when flags
994 * is zero) then just set this
995 */
996 if (n->nlmsg_flags&NLM_F_REPLACE)
997 ovr = 1;
998replay:
Tom Goff7316ae82010-03-19 15:40:13 +0000999 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, pid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (ret == -EAGAIN)
1001 goto replay;
1002 break;
1003 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001004 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1005 pid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 break;
1007 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001008 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1009 pid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 break;
1011 default:
1012 BUG();
1013 }
1014
1015 return ret;
1016}
1017
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001018static struct nlattr *
Patrick McHardy3a6c2b42009-08-25 16:07:40 +02001019find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001021 struct nlattr *tb1, *tb2[TCA_ACT_MAX+1];
1022 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1023 struct nlattr *nla[TCAA_MAX + 1];
1024 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Patrick McHardyc96c9472008-01-23 20:32:58 -08001026 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001028 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (tb1 == NULL)
1030 return NULL;
1031
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001032 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1033 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Patrick McHardy6d834e02008-01-23 20:32:42 -08001036 if (tb[1] == NULL)
1037 return NULL;
1038 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1039 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001041 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Thomas Graf26dab892006-07-05 20:45:06 -07001043 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
1046static int
1047tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1048{
1049 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001050 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001051 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 struct tc_action_ops *a_o;
1053 struct tc_action a;
1054 int ret = 0;
1055 struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001056 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 if (kind == NULL) {
1059 printk("tc_dump_action: action bad kind\n");
1060 return 0;
1061 }
1062
Thomas Graf26dab892006-07-05 20:45:06 -07001063 a_o = tc_lookup_action(kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (a_o == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return 0;
1066 }
1067
1068 memset(&a, 0, sizeof(struct tc_action));
1069 a.ops = a_o;
1070
1071 if (a_o->walk == NULL) {
Thomas Graf26dab892006-07-05 20:45:06 -07001072 printk("tc_dump_action: %s !capable of dumping table\n", a_o->kind);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001073 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075
1076 nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001077 cb->nlh->nlmsg_type, sizeof(*t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 t = NLMSG_DATA(nlh);
1079 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001080 t->tca__pad1 = 0;
1081 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001083 nest = nla_nest_start(skb, TCA_ACT_TAB);
1084 if (nest == NULL)
1085 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1088 if (ret < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001089 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001092 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 ret = skb->len;
1094 } else
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001095 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001097 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (NETLINK_CB(cb->skb).pid && ret)
1099 nlh->nlmsg_flags |= NLM_F_MULTI;
1100 module_put(a_o->owner);
1101 return skb->len;
1102
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001103nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104nlmsg_failure:
1105 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001106 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return skb->len;
1108}
1109
1110static int __init tc_action_init(void)
1111{
Thomas Graf708914c2007-03-22 11:56:59 -07001112 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL);
1113 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL);
1114 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return 0;
1117}
1118
1119subsys_initcall(tc_action_init);