blob: 93fdf131bd75e3e8e1969f1889c9d2a2cff90c67 [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>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040023#include <linux/module.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110024#include <net/net_namespace.h>
25#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <net/sch_generic.h>
27#include <net/act_api.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070028#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
David S. Millere9ce1cd2006-08-21 23:54:55 -070030void tcf_hash_destroy(struct tcf_common *p, struct tcf_hashinfo *hinfo)
31{
32 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
33 struct tcf_common **p1p;
34
35 for (p1p = &hinfo->htab[h]; *p1p; p1p = &(*p1p)->tcfc_next) {
36 if (*p1p == p) {
37 write_lock_bh(hinfo->lock);
38 *p1p = p->tcfc_next;
39 write_unlock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -070040 gen_kill_estimator(&p->tcfc_bstats,
41 &p->tcfc_rate_est);
Eric Dumazetc7de2cf2010-06-09 02:09:23 +000042 /*
43 * gen_estimator est_timer() might access p->tcfc_lock
44 * or bstats, wait a RCU grace period before freeing p
45 */
Lai Jiangshanf5c8593c2011-03-15 17:57:04 +080046 kfree_rcu(p, tcfc_rcu);
David S. Millere9ce1cd2006-08-21 23:54:55 -070047 return;
48 }
49 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -070050 WARN_ON(1);
David S. Millere9ce1cd2006-08-21 23:54:55 -070051}
52EXPORT_SYMBOL(tcf_hash_destroy);
53
54int tcf_hash_release(struct tcf_common *p, int bind,
55 struct tcf_hashinfo *hinfo)
56{
57 int ret = 0;
58
59 if (p) {
60 if (bind)
61 p->tcfc_bindcnt--;
62
63 p->tcfc_refcnt--;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090064 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070065 tcf_hash_destroy(p, hinfo);
66 ret = 1;
67 }
68 }
69 return ret;
70}
71EXPORT_SYMBOL(tcf_hash_release);
72
73static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
74 struct tc_action *a, struct tcf_hashinfo *hinfo)
75{
76 struct tcf_common *p;
Eric Dumazetcc7ec452011-01-19 19:26:56 +000077 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080078 struct nlattr *nest;
David S. Millere9ce1cd2006-08-21 23:54:55 -070079
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +020080 read_lock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -070081
82 s_i = cb->args[0];
83
84 for (i = 0; i < (hinfo->hmask + 1); i++) {
85 p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
86
87 for (; p; p = p->tcfc_next) {
88 index++;
89 if (index < s_i)
90 continue;
91 a->priv = p;
92 a->order = n_i;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080093
94 nest = nla_nest_start(skb, a->order);
95 if (nest == NULL)
96 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -070097 err = tcf_action_dump_1(skb, a, 0, 0);
98 if (err < 0) {
99 index--;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800100 nlmsg_trim(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700101 goto done;
102 }
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800103 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700104 n_i++;
105 if (n_i >= TCA_ACT_MAX_PRIO)
106 goto done;
107 }
108 }
109done:
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +0200110 read_unlock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700111 if (n_i)
112 cb->args[0] += n_i;
113 return n_i;
114
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800115nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800116 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700117 goto done;
118}
119
120static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a,
121 struct tcf_hashinfo *hinfo)
122{
123 struct tcf_common *p, *s_p;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800124 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000125 int i = 0, n_i = 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700126
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800127 nest = nla_nest_start(skb, a->order);
128 if (nest == NULL)
129 goto nla_put_failure;
Patrick McHardy57e1c482008-01-23 20:34:28 -0800130 NLA_PUT_STRING(skb, TCA_KIND, a->ops->kind);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700131 for (i = 0; i < (hinfo->hmask + 1); i++) {
132 p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
133
134 while (p != NULL) {
135 s_p = p->tcfc_next;
136 if (ACT_P_DELETED == tcf_hash_release(p, 0, hinfo))
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000137 module_put(a->ops->owner);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700138 n_i++;
139 p = s_p;
140 }
141 }
Patrick McHardy24beeab2008-01-23 20:34:48 -0800142 NLA_PUT_U32(skb, TCA_FCNT, n_i);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800143 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700144
145 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800146nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800147 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700148 return -EINVAL;
149}
150
151int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
152 int type, struct tc_action *a)
153{
154 struct tcf_hashinfo *hinfo = a->ops->hinfo;
155
156 if (type == RTM_DELACTION) {
157 return tcf_del_walker(skb, a, hinfo);
158 } else if (type == RTM_GETACTION) {
159 return tcf_dump_walker(skb, cb, a, hinfo);
160 } else {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000161 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700162 return -EINVAL;
163 }
164}
165EXPORT_SYMBOL(tcf_generic_walker);
166
167struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
168{
169 struct tcf_common *p;
170
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +0200171 read_lock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700172 for (p = hinfo->htab[tcf_hash(index, hinfo->hmask)]; p;
173 p = p->tcfc_next) {
174 if (p->tcfc_index == index)
175 break;
176 }
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +0200177 read_unlock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700178
179 return p;
180}
181EXPORT_SYMBOL(tcf_hash_lookup);
182
183u32 tcf_hash_new_index(u32 *idx_gen, struct tcf_hashinfo *hinfo)
184{
185 u32 val = *idx_gen;
186
187 do {
188 if (++val == 0)
189 val = 1;
190 } while (tcf_hash_lookup(val, hinfo));
191
192 return (*idx_gen = val);
193}
194EXPORT_SYMBOL(tcf_hash_new_index);
195
196int tcf_hash_search(struct tc_action *a, u32 index)
197{
198 struct tcf_hashinfo *hinfo = a->ops->hinfo;
199 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
200
201 if (p) {
202 a->priv = p;
203 return 1;
204 }
205 return 0;
206}
207EXPORT_SYMBOL(tcf_hash_search);
208
209struct tcf_common *tcf_hash_check(u32 index, struct tc_action *a, int bind,
210 struct tcf_hashinfo *hinfo)
211{
212 struct tcf_common *p = NULL;
213 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700214 if (bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700215 p->tcfc_bindcnt++;
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700216 p->tcfc_refcnt++;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700217 a->priv = p;
218 }
219 return p;
220}
221EXPORT_SYMBOL(tcf_hash_check);
222
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800223struct tcf_common *tcf_hash_create(u32 index, struct nlattr *est,
224 struct tc_action *a, int size, int bind,
225 u32 *idx_gen, struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700226{
227 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
228
229 if (unlikely(!p))
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800230 return ERR_PTR(-ENOMEM);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700231 p->tcfc_refcnt = 1;
232 if (bind)
233 p->tcfc_bindcnt = 1;
234
235 spin_lock_init(&p->tcfc_lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700236 p->tcfc_index = index ? index : tcf_hash_new_index(idx_gen, hinfo);
237 p->tcfc_tm.install = jiffies;
238 p->tcfc_tm.lastuse = jiffies;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800239 if (est) {
240 int err = gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
241 &p->tcfc_lock, est);
242 if (err) {
243 kfree(p);
244 return ERR_PTR(err);
245 }
246 }
247
David S. Millere9ce1cd2006-08-21 23:54:55 -0700248 a->priv = (void *) p;
249 return p;
250}
251EXPORT_SYMBOL(tcf_hash_create);
252
253void tcf_hash_insert(struct tcf_common *p, struct tcf_hashinfo *hinfo)
254{
255 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
256
257 write_lock_bh(hinfo->lock);
258 p->tcfc_next = hinfo->htab[h];
259 hinfo->htab[h] = p;
260 write_unlock_bh(hinfo->lock);
261}
262EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264static struct tc_action_ops *act_base = NULL;
265static DEFINE_RWLOCK(act_mod_lock);
266
267int tcf_register_action(struct tc_action_ops *act)
268{
269 struct tc_action_ops *a, **ap;
270
271 write_lock(&act_mod_lock);
272 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next) {
273 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
274 write_unlock(&act_mod_lock);
275 return -EEXIST;
276 }
277 }
278 act->next = NULL;
279 *ap = act;
280 write_unlock(&act_mod_lock);
281 return 0;
282}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800283EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285int tcf_unregister_action(struct tc_action_ops *act)
286{
287 struct tc_action_ops *a, **ap;
288 int err = -ENOENT;
289
290 write_lock(&act_mod_lock);
291 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next)
292 if (a == act)
293 break;
294 if (a) {
295 *ap = a->next;
296 a->next = NULL;
297 err = 0;
298 }
299 write_unlock(&act_mod_lock);
300 return err;
301}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800302EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304/* lookup by name */
305static struct tc_action_ops *tc_lookup_action_n(char *kind)
306{
307 struct tc_action_ops *a = NULL;
308
309 if (kind) {
310 read_lock(&act_mod_lock);
311 for (a = act_base; a; a = a->next) {
312 if (strcmp(kind, a->kind) == 0) {
313 if (!try_module_get(a->owner)) {
314 read_unlock(&act_mod_lock);
315 return NULL;
316 }
317 break;
318 }
319 }
320 read_unlock(&act_mod_lock);
321 }
322 return a;
323}
324
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800325/* lookup by nlattr */
326static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 struct tc_action_ops *a = NULL;
329
330 if (kind) {
331 read_lock(&act_mod_lock);
332 for (a = act_base; a; a = a->next) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800333 if (nla_strcmp(kind, a->kind) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (!try_module_get(a->owner)) {
335 read_unlock(&act_mod_lock);
336 return NULL;
337 }
338 break;
339 }
340 }
341 read_unlock(&act_mod_lock);
342 }
343 return a;
344}
345
346#if 0
347/* lookup by id */
348static struct tc_action_ops *tc_lookup_action_id(u32 type)
349{
350 struct tc_action_ops *a = NULL;
351
352 if (type) {
353 read_lock(&act_mod_lock);
354 for (a = act_base; a; a = a->next) {
355 if (a->type == type) {
356 if (!try_module_get(a->owner)) {
357 read_unlock(&act_mod_lock);
358 return NULL;
359 }
360 break;
361 }
362 }
363 read_unlock(&act_mod_lock);
364 }
365 return a;
366}
367#endif
368
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000369int tcf_action_exec(struct sk_buff *skb, const struct tc_action *act,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900370 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000372 const struct tc_action *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 int ret = -1;
374
375 if (skb->tc_verd & TC_NCLS) {
376 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 ret = TC_ACT_OK;
378 goto exec_done;
379 }
380 while ((a = act) != NULL) {
381repeat:
382 if (a->ops && a->ops->act) {
Patrick McHardyf43c5a02006-01-08 22:15:34 -0800383 ret = a->ops->act(skb, a, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (TC_MUNGED & skb->tc_verd) {
385 /* copied already, allow trampling */
386 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
387 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (ret == TC_ACT_REPEAT)
390 goto repeat; /* we need a ttl - JHS */
J Hadi Salim14d50e72005-05-03 16:29:13 -0700391 if (ret != TC_ACT_PIPE)
392 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394 act = a->next;
395 }
396exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return ret;
398}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800399EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401void tcf_action_destroy(struct tc_action *act, int bind)
402{
403 struct tc_action *a;
404
405 for (a = act; a; a = act) {
406 if (a->ops && a->ops->cleanup) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
408 module_put(a->ops->owner);
409 act = act->next;
410 kfree(a);
stephen hemminger6ff9c362010-05-12 06:37:05 +0000411 } else {
412 /*FIXME: Remove later - catch insertion bugs*/
413 WARN(1, "tcf_action_destroy: BUG? destroying NULL ops\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 act = act->next;
415 kfree(a);
416 }
417 }
418}
419
420int
421tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
422{
423 int err = -EINVAL;
424
425 if (a->ops == NULL || a->ops->dump == NULL)
426 return err;
427 return a->ops->dump(skb, a, bind, ref);
428}
429
430int
431tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
432{
433 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700434 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800435 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 if (a->ops == NULL || a->ops->dump == NULL)
438 return err;
439
Patrick McHardy57e1c482008-01-23 20:34:28 -0800440 NLA_PUT_STRING(skb, TCA_KIND, a->ops->kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800442 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800443 nest = nla_nest_start(skb, TCA_OPTIONS);
444 if (nest == NULL)
445 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000446 err = tcf_action_dump_old(skb, a, bind, ref);
447 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800448 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return err;
450 }
451
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800452nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700453 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return -1;
455}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800456EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458int
459tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref)
460{
461 struct tc_action *a;
462 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800463 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 while ((a = act) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 act = a->next;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800467 nest = nla_nest_start(skb, a->order);
468 if (nest == NULL)
469 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 err = tcf_action_dump_1(skb, a, bind, ref);
471 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700472 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800473 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475
476 return 0;
477
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800478nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700479 err = -EINVAL;
480errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800481 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700482 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800485struct tc_action *tcf_action_init_1(struct nlattr *nla, struct nlattr *est,
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800486 char *name, int ovr, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 struct tc_action *a;
489 struct tc_action_ops *a_o;
490 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000491 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800492 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800493 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800496 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
497 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800499 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800500 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (kind == NULL)
502 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800503 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 goto err_out;
505 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800506 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
508 goto err_out;
509 }
510
511 a_o = tc_lookup_action_n(act_name);
512 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700513#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800515 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 rtnl_lock();
517
518 a_o = tc_lookup_action_n(act_name);
519
520 /* We dropped the RTNL semaphore in order to
521 * perform the module load. So, even if we
522 * succeeded in loading the module we have to
523 * tell the caller to replay the request. We
524 * indicate this using -EAGAIN.
525 */
526 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800527 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 goto err_mod;
529 }
530#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800531 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 goto err_out;
533 }
534
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800535 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700536 a = kzalloc(sizeof(*a), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (a == NULL)
538 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 /* backward compatibility for policer */
541 if (name == NULL)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800542 err = a_o->init(tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 else
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800544 err = a_o->init(nla, est, a, ovr, bind);
545 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 goto err_free;
547
548 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000549 * if it exists and is only bound to in a_o->init() then
550 * ACT_P_CREATED is not returned (a zero is).
551 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800552 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 module_put(a_o->owner);
554 a->ops = a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return a;
557
558err_free:
559 kfree(a);
560err_mod:
561 module_put(a_o->owner);
562err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800563 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800566struct tc_action *tcf_action_init(struct nlattr *nla, struct nlattr *est,
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800567 char *name, int ovr, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000569 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 struct tc_action *head = NULL, *act, *act_prev = NULL;
Patrick McHardycee63722008-01-23 20:33:32 -0800571 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 int i;
573
Patrick McHardycee63722008-01-23 20:33:32 -0800574 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
575 if (err < 0)
576 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800578 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800579 act = tcf_action_init_1(tb[i], est, name, ovr, bind);
580 if (IS_ERR(act))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 goto err;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800582 act->order = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 if (head == NULL)
585 head = act;
586 else
587 act_prev->next = act;
588 act_prev = act;
589 }
590 return head;
591
592err:
593 if (head != NULL)
594 tcf_action_destroy(head, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800595 return act;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
598int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
599 int compat_mode)
600{
601 int err = 0;
602 struct gnet_dump d;
603 struct tcf_act_hdr *h = a->priv;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (h == NULL)
606 goto errout;
607
608 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400609 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
611 if (compat_mode) {
612 if (a->type == TCA_OLD_COMPAT)
613 err = gnet_stats_start_copy_compat(skb, 0,
Patrick McHardy4bdf3992007-07-02 22:47:37 -0700614 TCA_STATS, TCA_XSTATS, &h->tcf_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 else
616 return 0;
617 } else
618 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
Patrick McHardy4bdf3992007-07-02 22:47:37 -0700619 &h->tcf_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 if (err < 0)
622 goto errout;
623
624 if (a->ops != NULL && a->ops->get_stats != NULL)
625 if (a->ops->get_stats(skb, a) < 0)
626 goto errout;
627
David S. Millere9ce1cd2006-08-21 23:54:55 -0700628 if (gnet_stats_copy_basic(&d, &h->tcf_bstats) < 0 ||
Eric Dumazetd250a5f2009-10-02 10:32:18 +0000629 gnet_stats_copy_rate_est(&d, &h->tcf_bstats,
630 &h->tcf_rate_est) < 0 ||
David S. Millere9ce1cd2006-08-21 23:54:55 -0700631 gnet_stats_copy_queue(&d, &h->tcf_qstats) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 goto errout;
633
634 if (gnet_stats_finish_copy(&d) < 0)
635 goto errout;
636
637 return 0;
638
639errout:
640 return -1;
641}
642
643static int
644tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, u32 seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900645 u16 flags, int event, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 struct tcamsg *t;
648 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700649 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800650 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700652 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 t = NLMSG_DATA(nlh);
655 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700656 t->tca__pad1 = 0;
657 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900658
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800659 nest = nla_nest_start(skb, TCA_ACT_TAB);
660 if (nest == NULL)
661 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 if (tcf_action_dump(skb, a, bind, ref) < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800664 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800666 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900667
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700668 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return skb->len;
670
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800671nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700673 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return -1;
675}
676
677static int
Tom Goff7316ae82010-03-19 15:40:13 +0000678act_get_notify(struct net *net, u32 pid, struct nlmsghdr *n,
679 struct tc_action *a, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
684 if (!skb)
685 return -ENOBUFS;
686 if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
687 kfree_skb(skb);
688 return -EINVAL;
689 }
Thomas Graf2942e902006-08-15 00:30:25 -0700690
Tom Goff7316ae82010-03-19 15:40:13 +0000691 return rtnl_unicast(skb, net, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
694static struct tc_action *
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800695tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000697 struct nlattr *tb[TCA_ACT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct tc_action *a;
699 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800700 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Patrick McHardycee63722008-01-23 20:33:32 -0800702 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
703 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800704 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Patrick McHardycee63722008-01-23 20:33:32 -0800706 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800707 if (tb[TCA_ACT_INDEX] == NULL ||
708 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800709 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800710 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800712 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700713 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (a == NULL)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800715 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800717 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800718 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (a->ops == NULL)
720 goto err_free;
721 if (a->ops->lookup == NULL)
722 goto err_mod;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800723 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (a->ops->lookup(a, index) == 0)
725 goto err_mod;
726
727 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730err_mod:
731 module_put(a->ops->owner);
732err_free:
733 kfree(a);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800734err_out:
735 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
738static void cleanup_a(struct tc_action *act)
739{
740 struct tc_action *a;
741
742 for (a = act; a; a = act) {
743 act = a->next;
744 kfree(a);
745 }
746}
747
748static struct tc_action *create_a(int i)
749{
750 struct tc_action *act;
751
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700752 act = kzalloc(sizeof(*act), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (act == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000754 pr_debug("create_a: failed to alloc!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return NULL;
756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 act->order = i;
758 return act;
759}
760
Tom Goff7316ae82010-03-19 15:40:13 +0000761static int tca_action_flush(struct net *net, struct nlattr *nla,
762 struct nlmsghdr *n, u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 struct sk_buff *skb;
765 unsigned char *b;
766 struct nlmsghdr *nlh;
767 struct tcamsg *t;
768 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800769 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000770 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800771 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 struct tc_action *a = create_a(0);
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700773 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 if (a == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000776 pr_debug("tca_action_flush: couldnt create tc_action\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return err;
778 }
779
780 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
781 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000782 pr_debug("tca_action_flush: failed skb alloc\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 kfree(a);
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700784 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700787 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Patrick McHardycee63722008-01-23 20:33:32 -0800789 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
790 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 goto err_out;
792
Patrick McHardycee63722008-01-23 20:33:32 -0800793 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800794 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 a->ops = tc_lookup_action(kind);
796 if (a->ops == NULL)
797 goto err_out;
798
799 nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t));
800 t = NLMSG_DATA(nlh);
801 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700802 t->tca__pad1 = 0;
803 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800805 nest = nla_nest_start(skb, TCA_ACT_TAB);
806 if (nest == NULL)
807 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
810 if (err < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800811 goto nla_put_failure;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700812 if (err == 0)
813 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800815 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700817 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 nlh->nlmsg_flags |= NLM_F_ROOT;
819 module_put(a->ops->owner);
820 kfree(a);
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000821 err = rtnetlink_send(skb, net, pid, RTNLGRP_TC,
822 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (err > 0)
824 return 0;
825
826 return err;
827
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800828nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829nlmsg_failure:
Thomas Grafebbaeab2006-07-09 11:36:23 -0700830 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700832noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 kfree_skb(skb);
834 kfree(a);
835 return err;
836}
837
838static int
Tom Goff7316ae82010-03-19 15:40:13 +0000839tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
840 u32 pid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Patrick McHardycee63722008-01-23 20:33:32 -0800842 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000843 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 struct tc_action *head = NULL, *act, *act_prev = NULL;
845
Patrick McHardycee63722008-01-23 20:33:32 -0800846 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
847 if (ret < 0)
848 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000850 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700851 if (tb[1] != NULL)
Tom Goff7316ae82010-03-19 15:40:13 +0000852 return tca_action_flush(net, tb[1], n, pid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700853 else
854 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800857 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800858 act = tcf_action_get_1(tb[i], n, pid);
859 if (IS_ERR(act)) {
860 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800862 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800863 act->order = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 if (head == NULL)
866 head = act;
867 else
868 act_prev->next = act;
869 act_prev = act;
870 }
871
872 if (event == RTM_GETACTION)
Tom Goff7316ae82010-03-19 15:40:13 +0000873 ret = act_get_notify(net, pid, n, head, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 else { /* delete */
875 struct sk_buff *skb;
876
877 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
878 if (!skb) {
879 ret = -ENOBUFS;
880 goto err;
881 }
882
883 if (tca_get_fill(skb, head, pid, n->nlmsg_seq, 0, event,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900884 0, 1) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 kfree_skb(skb);
886 ret = -EINVAL;
887 goto err;
888 }
889
890 /* now do the delete */
891 tcf_action_destroy(head, 0);
Tom Goff7316ae82010-03-19 15:40:13 +0000892 ret = rtnetlink_send(skb, net, pid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000893 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (ret > 0)
895 return 0;
896 return ret;
897 }
898err:
899 cleanup_a(head);
900 return ret;
901}
902
Tom Goff7316ae82010-03-19 15:40:13 +0000903static int tcf_add_notify(struct net *net, struct tc_action *a,
904 u32 pid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 struct tcamsg *t;
907 struct nlmsghdr *nlh;
908 struct sk_buff *skb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800909 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 unsigned char *b;
911 int err = 0;
912
913 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
914 if (!skb)
915 return -ENOBUFS;
916
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700917 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700919 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 t = NLMSG_DATA(nlh);
921 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700922 t->tca__pad1 = 0;
923 t->tca__pad2 = 0;
924
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800925 nest = nla_nest_start(skb, TCA_ACT_TAB);
926 if (nest == NULL)
927 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 if (tcf_action_dump(skb, a, 0, 0) < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800930 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800932 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900933
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700934 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Patrick McHardyac6d4392005-08-14 19:29:52 -0700935 NETLINK_CB(skb).dst_group = RTNLGRP_TC;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900936
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000937 err = rtnetlink_send(skb, net, pid, RTNLGRP_TC, flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (err > 0)
939 err = 0;
940 return err;
941
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800942nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943nlmsg_failure:
Patrick McHardyf6e57462006-03-12 20:33:22 -0800944 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return -1;
946}
947
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949static int
Tom Goff7316ae82010-03-19 15:40:13 +0000950tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
951 u32 pid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 int ret = 0;
954 struct tc_action *act;
955 struct tc_action *a;
956 u32 seq = n->nlmsg_seq;
957
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800958 act = tcf_action_init(nla, NULL, NULL, ovr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (act == NULL)
960 goto done;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800961 if (IS_ERR(act)) {
962 ret = PTR_ERR(act);
963 goto done;
964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 /* dump then free all the actions after update; inserted policy
967 * stays intact
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000968 */
Tom Goff7316ae82010-03-19 15:40:13 +0000969 ret = tcf_add_notify(net, act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 for (a = act; a; a = act) {
971 act = a->next;
972 kfree(a);
973 }
974done:
975 return ret;
976}
977
978static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
979{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900980 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800981 struct nlattr *tca[TCA_ACT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 u32 pid = skb ? NETLINK_CB(skb).pid : 0;
983 int ret = 0, ovr = 0;
984
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800985 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
986 if (ret < 0)
987 return ret;
988
989 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000990 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return -EINVAL;
992 }
993
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000994 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 switch (n->nlmsg_type) {
996 case RTM_NEWACTION:
997 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300998 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 * Note that CREATE | EXCL implies that
1000 * but since we want avoid ambiguity (eg when flags
1001 * is zero) then just set this
1002 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001003 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 ovr = 1;
1005replay:
Tom Goff7316ae82010-03-19 15:40:13 +00001006 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, pid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (ret == -EAGAIN)
1008 goto replay;
1009 break;
1010 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001011 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1012 pid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 break;
1014 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001015 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1016 pid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 break;
1018 default:
1019 BUG();
1020 }
1021
1022 return ret;
1023}
1024
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001025static struct nlattr *
Patrick McHardy3a6c2b42009-08-25 16:07:40 +02001026find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001028 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001029 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1030 struct nlattr *nla[TCAA_MAX + 1];
1031 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Patrick McHardyc96c9472008-01-23 20:32:58 -08001033 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001035 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (tb1 == NULL)
1037 return NULL;
1038
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001039 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1040 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Patrick McHardy6d834e02008-01-23 20:32:42 -08001043 if (tb[1] == NULL)
1044 return NULL;
1045 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1046 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001048 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Thomas Graf26dab892006-07-05 20:45:06 -07001050 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053static int
1054tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1055{
1056 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001057 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001058 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 struct tc_action_ops *a_o;
1060 struct tc_action a;
1061 int ret = 0;
1062 struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001063 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001066 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return 0;
1068 }
1069
Thomas Graf26dab892006-07-05 20:45:06 -07001070 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001071 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 memset(&a, 0, sizeof(struct tc_action));
1075 a.ops = a_o;
1076
1077 if (a_o->walk == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001078 WARN(1, "tc_dump_action: %s !capable of dumping table\n",
1079 a_o->kind);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001080 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
1082
1083 nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001084 cb->nlh->nlmsg_type, sizeof(*t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 t = NLMSG_DATA(nlh);
1086 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001087 t->tca__pad1 = 0;
1088 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001090 nest = nla_nest_start(skb, TCA_ACT_TAB);
1091 if (nest == NULL)
1092 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1095 if (ret < 0)
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001096 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001099 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 ret = skb->len;
1101 } else
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001102 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001104 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (NETLINK_CB(cb->skb).pid && ret)
1106 nlh->nlmsg_flags |= NLM_F_MULTI;
1107 module_put(a_o->owner);
1108 return skb->len;
1109
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001110nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111nlmsg_failure:
1112 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001113 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return skb->len;
1115}
1116
1117static int __init tc_action_init(void)
1118{
Greg Rosec7ac8672011-06-10 01:27:09 +00001119 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1120 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1121 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1122 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return 0;
1125}
1126
1127subsys_initcall(tc_action_init);