blob: 7d84183b633e2cd3aacd76e65f238eb824b021fa [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;
David S. Miller1b34ec42012-03-29 05:11:39 -0400130 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
131 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700132 for (i = 0; i < (hinfo->hmask + 1); i++) {
133 p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
134
135 while (p != NULL) {
136 s_p = p->tcfc_next;
137 if (ACT_P_DELETED == tcf_hash_release(p, 0, hinfo))
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000138 module_put(a->ops->owner);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700139 n_i++;
140 p = s_p;
141 }
142 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400143 if (nla_put_u32(skb, TCA_FCNT, n_i))
144 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800145 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700146
147 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800148nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800149 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700150 return -EINVAL;
151}
152
153int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
154 int type, struct tc_action *a)
155{
156 struct tcf_hashinfo *hinfo = a->ops->hinfo;
157
158 if (type == RTM_DELACTION) {
159 return tcf_del_walker(skb, a, hinfo);
160 } else if (type == RTM_GETACTION) {
161 return tcf_dump_walker(skb, cb, a, hinfo);
162 } else {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000163 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700164 return -EINVAL;
165 }
166}
167EXPORT_SYMBOL(tcf_generic_walker);
168
169struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
170{
171 struct tcf_common *p;
172
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +0200173 read_lock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700174 for (p = hinfo->htab[tcf_hash(index, hinfo->hmask)]; p;
175 p = p->tcfc_next) {
176 if (p->tcfc_index == index)
177 break;
178 }
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +0200179 read_unlock_bh(hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700180
181 return p;
182}
183EXPORT_SYMBOL(tcf_hash_lookup);
184
185u32 tcf_hash_new_index(u32 *idx_gen, struct tcf_hashinfo *hinfo)
186{
187 u32 val = *idx_gen;
188
189 do {
190 if (++val == 0)
191 val = 1;
192 } while (tcf_hash_lookup(val, hinfo));
193
Yang Yingliang17569fa2013-12-10 20:55:29 +0800194 *idx_gen = val;
195 return val;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700196}
197EXPORT_SYMBOL(tcf_hash_new_index);
198
199int tcf_hash_search(struct tc_action *a, u32 index)
200{
201 struct tcf_hashinfo *hinfo = a->ops->hinfo;
202 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
203
204 if (p) {
205 a->priv = p;
206 return 1;
207 }
208 return 0;
209}
210EXPORT_SYMBOL(tcf_hash_search);
211
212struct tcf_common *tcf_hash_check(u32 index, struct tc_action *a, int bind,
213 struct tcf_hashinfo *hinfo)
214{
215 struct tcf_common *p = NULL;
216 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700217 if (bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700218 p->tcfc_bindcnt++;
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700219 p->tcfc_refcnt++;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700220 a->priv = p;
221 }
222 return p;
223}
224EXPORT_SYMBOL(tcf_hash_check);
225
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800226struct tcf_common *tcf_hash_create(u32 index, struct nlattr *est,
227 struct tc_action *a, int size, int bind,
228 u32 *idx_gen, struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700229{
230 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
231
232 if (unlikely(!p))
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800233 return ERR_PTR(-ENOMEM);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700234 p->tcfc_refcnt = 1;
235 if (bind)
236 p->tcfc_bindcnt = 1;
237
238 spin_lock_init(&p->tcfc_lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700239 p->tcfc_index = index ? index : tcf_hash_new_index(idx_gen, hinfo);
240 p->tcfc_tm.install = jiffies;
241 p->tcfc_tm.lastuse = jiffies;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800242 if (est) {
243 int err = gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
244 &p->tcfc_lock, est);
245 if (err) {
246 kfree(p);
247 return ERR_PTR(err);
248 }
249 }
250
David S. Millere9ce1cd2006-08-21 23:54:55 -0700251 a->priv = (void *) p;
252 return p;
253}
254EXPORT_SYMBOL(tcf_hash_create);
255
256void tcf_hash_insert(struct tcf_common *p, struct tcf_hashinfo *hinfo)
257{
258 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
259
260 write_lock_bh(hinfo->lock);
261 p->tcfc_next = hinfo->htab[h];
262 hinfo->htab[h] = p;
263 write_unlock_bh(hinfo->lock);
264}
265EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267static struct tc_action_ops *act_base = NULL;
268static DEFINE_RWLOCK(act_mod_lock);
269
270int tcf_register_action(struct tc_action_ops *act)
271{
272 struct tc_action_ops *a, **ap;
273
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500274 /* Must supply act, dump, cleanup and init */
275 if (!act->act || !act->dump || !act->cleanup || !act->init)
276 return -EINVAL;
277
Jamal Hadi Salim382ca8a2013-12-04 09:26:55 -0500278 /* Supply defaults */
Jamal Hadi Salim63ef6172013-12-04 09:26:53 -0500279 if (!act->lookup)
280 act->lookup = tcf_hash_search;
Jamal Hadi Salim382ca8a2013-12-04 09:26:55 -0500281 if (!act->walk)
282 act->walk = tcf_generic_walker;
Jamal Hadi Salim63ef6172013-12-04 09:26:53 -0500283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 write_lock(&act_mod_lock);
285 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next) {
286 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
287 write_unlock(&act_mod_lock);
288 return -EEXIST;
289 }
290 }
291 act->next = NULL;
292 *ap = act;
293 write_unlock(&act_mod_lock);
294 return 0;
295}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800296EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298int tcf_unregister_action(struct tc_action_ops *act)
299{
300 struct tc_action_ops *a, **ap;
301 int err = -ENOENT;
302
303 write_lock(&act_mod_lock);
304 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next)
305 if (a == act)
306 break;
307 if (a) {
308 *ap = a->next;
309 a->next = NULL;
310 err = 0;
311 }
312 write_unlock(&act_mod_lock);
313 return err;
314}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800315EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317/* lookup by name */
318static struct tc_action_ops *tc_lookup_action_n(char *kind)
319{
320 struct tc_action_ops *a = NULL;
321
322 if (kind) {
323 read_lock(&act_mod_lock);
324 for (a = act_base; a; a = a->next) {
325 if (strcmp(kind, a->kind) == 0) {
326 if (!try_module_get(a->owner)) {
327 read_unlock(&act_mod_lock);
328 return NULL;
329 }
330 break;
331 }
332 }
333 read_unlock(&act_mod_lock);
334 }
335 return a;
336}
337
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800338/* lookup by nlattr */
339static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 struct tc_action_ops *a = NULL;
342
343 if (kind) {
344 read_lock(&act_mod_lock);
345 for (a = act_base; a; a = a->next) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800346 if (nla_strcmp(kind, a->kind) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (!try_module_get(a->owner)) {
348 read_unlock(&act_mod_lock);
349 return NULL;
350 }
351 break;
352 }
353 }
354 read_unlock(&act_mod_lock);
355 }
356 return a;
357}
358
359#if 0
360/* lookup by id */
361static struct tc_action_ops *tc_lookup_action_id(u32 type)
362{
363 struct tc_action_ops *a = NULL;
364
365 if (type) {
366 read_lock(&act_mod_lock);
367 for (a = act_base; a; a = a->next) {
368 if (a->type == type) {
369 if (!try_module_get(a->owner)) {
370 read_unlock(&act_mod_lock);
371 return NULL;
372 }
373 break;
374 }
375 }
376 read_unlock(&act_mod_lock);
377 }
378 return a;
379}
380#endif
381
WANG Cong33be6272013-12-15 20:15:05 -0800382int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900383 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000385 const struct tc_action *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 int ret = -1;
387
388 if (skb->tc_verd & TC_NCLS) {
389 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 ret = TC_ACT_OK;
391 goto exec_done;
392 }
WANG Cong33be6272013-12-15 20:15:05 -0800393 list_for_each_entry(a, actions, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394repeat:
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500395 if (a->ops) {
Patrick McHardyf43c5a02006-01-08 22:15:34 -0800396 ret = a->ops->act(skb, a, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (TC_MUNGED & skb->tc_verd) {
398 /* copied already, allow trampling */
399 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
400 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (ret == TC_ACT_REPEAT)
403 goto repeat; /* we need a ttl - JHS */
J Hadi Salim14d50e72005-05-03 16:29:13 -0700404 if (ret != TC_ACT_PIPE)
405 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return ret;
410}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800411EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
WANG Cong33be6272013-12-15 20:15:05 -0800413void tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
WANG Cong33be6272013-12-15 20:15:05 -0800415 struct tc_action *a, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
WANG Cong33be6272013-12-15 20:15:05 -0800417 list_for_each_entry_safe(a, tmp, actions, list) {
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500418 if (a->ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
420 module_put(a->ops->owner);
WANG Cong33be6272013-12-15 20:15:05 -0800421 list_del(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 kfree(a);
stephen hemminger6ff9c362010-05-12 06:37:05 +0000423 } else {
424 /*FIXME: Remove later - catch insertion bugs*/
425 WARN(1, "tcf_action_destroy: BUG? destroying NULL ops\n");
WANG Cong33be6272013-12-15 20:15:05 -0800426 list_del(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 kfree(a);
428 }
429 }
430}
431
432int
433tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
434{
435 int err = -EINVAL;
436
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500437 if (a->ops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return err;
439 return a->ops->dump(skb, a, bind, ref);
440}
441
442int
443tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
444{
445 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700446 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800447 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500449 if (a->ops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return err;
451
David S. Miller1b34ec42012-03-29 05:11:39 -0400452 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
453 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800455 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800456 nest = nla_nest_start(skb, TCA_OPTIONS);
457 if (nest == NULL)
458 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000459 err = tcf_action_dump_old(skb, a, bind, ref);
460 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800461 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return err;
463 }
464
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800465nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700466 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return -1;
468}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800469EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471int
WANG Cong33be6272013-12-15 20:15:05 -0800472tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 struct tc_action *a;
475 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800476 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
WANG Cong33be6272013-12-15 20:15:05 -0800478 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800479 nest = nla_nest_start(skb, a->order);
480 if (nest == NULL)
481 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 err = tcf_action_dump_1(skb, a, bind, ref);
483 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700484 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800485 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487
488 return 0;
489
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800490nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700491 err = -EINVAL;
492errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800493 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700494 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000497struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
498 struct nlattr *est, char *name, int ovr,
499 int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 struct tc_action *a;
502 struct tc_action_ops *a_o;
503 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000504 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800505 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800506 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800509 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
510 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800512 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800513 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (kind == NULL)
515 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800516 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 goto err_out;
518 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800519 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
521 goto err_out;
522 }
523
524 a_o = tc_lookup_action_n(act_name);
525 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700526#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800528 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 rtnl_lock();
530
531 a_o = tc_lookup_action_n(act_name);
532
533 /* We dropped the RTNL semaphore in order to
534 * perform the module load. So, even if we
535 * succeeded in loading the module we have to
536 * tell the caller to replay the request. We
537 * indicate this using -EAGAIN.
538 */
539 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800540 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 goto err_mod;
542 }
543#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800544 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 goto err_out;
546 }
547
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800548 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700549 a = kzalloc(sizeof(*a), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (a == NULL)
551 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
WANG Cong33be6272013-12-15 20:15:05 -0800553 INIT_LIST_HEAD(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /* backward compatibility for policer */
555 if (name == NULL)
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000556 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 else
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000558 err = a_o->init(net, nla, est, a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800559 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 goto err_free;
561
562 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000563 * if it exists and is only bound to in a_o->init() then
564 * ACT_P_CREATED is not returned (a zero is).
565 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800566 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 module_put(a_o->owner);
568 a->ops = a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return a;
571
572err_free:
573 kfree(a);
574err_mod:
575 module_put(a_o->owner);
576err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800577 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
WANG Cong33be6272013-12-15 20:15:05 -0800580int tcf_action_init(struct net *net, struct nlattr *nla,
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000581 struct nlattr *est, char *name, int ovr,
WANG Cong33be6272013-12-15 20:15:05 -0800582 int bind, struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000584 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800585 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800586 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 int i;
588
Patrick McHardycee63722008-01-23 20:33:32 -0800589 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
590 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800591 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800593 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000594 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800595 if (IS_ERR(act)) {
596 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800598 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800599 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800600 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
WANG Cong33be6272013-12-15 20:15:05 -0800602 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604err:
WANG Cong33be6272013-12-15 20:15:05 -0800605 tcf_action_destroy(actions, bind);
606 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
609int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
610 int compat_mode)
611{
612 int err = 0;
613 struct gnet_dump d;
614 struct tcf_act_hdr *h = a->priv;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (h == NULL)
617 goto errout;
618
619 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400620 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 */
622 if (compat_mode) {
623 if (a->type == TCA_OLD_COMPAT)
624 err = gnet_stats_start_copy_compat(skb, 0,
Patrick McHardy4bdf3992007-07-02 22:47:37 -0700625 TCA_STATS, TCA_XSTATS, &h->tcf_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 else
627 return 0;
628 } else
629 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
Patrick McHardy4bdf3992007-07-02 22:47:37 -0700630 &h->tcf_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 if (err < 0)
633 goto errout;
634
David S. Millere9ce1cd2006-08-21 23:54:55 -0700635 if (gnet_stats_copy_basic(&d, &h->tcf_bstats) < 0 ||
Eric Dumazetd250a5f2009-10-02 10:32:18 +0000636 gnet_stats_copy_rate_est(&d, &h->tcf_bstats,
637 &h->tcf_rate_est) < 0 ||
David S. Millere9ce1cd2006-08-21 23:54:55 -0700638 gnet_stats_copy_queue(&d, &h->tcf_qstats) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 goto errout;
640
641 if (gnet_stats_finish_copy(&d) < 0)
642 goto errout;
643
644 return 0;
645
646errout:
647 return -1;
648}
649
650static int
WANG Cong33be6272013-12-15 20:15:05 -0800651tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900652 u16 flags, int event, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 struct tcamsg *t;
655 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700656 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800657 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Eric W. Biederman15e47302012-09-07 20:12:54 +0000659 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700660 if (!nlh)
661 goto out_nlmsg_trim;
662 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700664 t->tca__pad1 = 0;
665 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900666
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800667 nest = nla_nest_start(skb, TCA_ACT_TAB);
668 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700669 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
WANG Cong33be6272013-12-15 20:15:05 -0800671 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700672 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800674 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900675
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700676 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return skb->len;
678
David S. Miller8b00a532012-06-26 21:39:32 -0700679out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700680 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return -1;
682}
683
684static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000685act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800686 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
691 if (!skb)
692 return -ENOBUFS;
WANG Cong33be6272013-12-15 20:15:05 -0800693 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 kfree_skb(skb);
695 return -EINVAL;
696 }
Thomas Graf2942e902006-08-15 00:30:25 -0700697
Eric W. Biederman15e47302012-09-07 20:12:54 +0000698 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
701static struct tc_action *
Eric W. Biederman15e47302012-09-07 20:12:54 +0000702tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000704 struct nlattr *tb[TCA_ACT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 struct tc_action *a;
706 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800707 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Patrick McHardycee63722008-01-23 20:33:32 -0800709 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
710 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800711 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Patrick McHardycee63722008-01-23 20:33:32 -0800713 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800714 if (tb[TCA_ACT_INDEX] == NULL ||
715 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800716 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800717 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800719 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700720 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (a == NULL)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800722 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
WANG Cong33be6272013-12-15 20:15:05 -0800724 INIT_LIST_HEAD(&a->list);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800725 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800726 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (a->ops == NULL)
728 goto err_free;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800729 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (a->ops->lookup(a, index) == 0)
731 goto err_mod;
732
733 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736err_mod:
737 module_put(a->ops->owner);
738err_free:
739 kfree(a);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800740err_out:
741 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
WANG Cong33be6272013-12-15 20:15:05 -0800744static void cleanup_a(struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
WANG Cong33be6272013-12-15 20:15:05 -0800746 struct tc_action *a, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
WANG Cong33be6272013-12-15 20:15:05 -0800748 list_for_each_entry_safe(a, tmp, actions, list) {
749 list_del(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 kfree(a);
751 }
752}
753
754static struct tc_action *create_a(int i)
755{
756 struct tc_action *act;
757
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700758 act = kzalloc(sizeof(*act), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (act == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000760 pr_debug("create_a: failed to alloc!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return NULL;
762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800764 INIT_LIST_HEAD(&act->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return act;
766}
767
Tom Goff7316ae82010-03-19 15:40:13 +0000768static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000769 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 struct sk_buff *skb;
772 unsigned char *b;
773 struct nlmsghdr *nlh;
774 struct tcamsg *t;
775 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800776 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000777 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800778 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 struct tc_action *a = create_a(0);
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700780 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 if (a == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000783 pr_debug("tca_action_flush: couldnt create tc_action\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return err;
785 }
786
787 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
788 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000789 pr_debug("tca_action_flush: failed skb alloc\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 kfree(a);
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700791 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700794 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Patrick McHardycee63722008-01-23 20:33:32 -0800796 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
797 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 goto err_out;
799
Patrick McHardycee63722008-01-23 20:33:32 -0800800 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800801 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 a->ops = tc_lookup_action(kind);
803 if (a->ops == NULL)
804 goto err_out;
805
Eric W. Biederman15e47302012-09-07 20:12:54 +0000806 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700807 if (!nlh)
808 goto out_module_put;
809 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700811 t->tca__pad1 = 0;
812 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800814 nest = nla_nest_start(skb, TCA_ACT_TAB);
815 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700816 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
819 if (err < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700820 goto out_module_put;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700821 if (err == 0)
822 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800824 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700826 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 nlh->nlmsg_flags |= NLM_F_ROOT;
828 module_put(a->ops->owner);
829 kfree(a);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000830 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000831 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (err > 0)
833 return 0;
834
835 return err;
836
David S. Miller8b00a532012-06-26 21:39:32 -0700837out_module_put:
Thomas Grafebbaeab2006-07-09 11:36:23 -0700838 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700840noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 kfree_skb(skb);
842 kfree(a);
843 return err;
844}
845
846static int
Tom Goff7316ae82010-03-19 15:40:13 +0000847tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000848 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Patrick McHardycee63722008-01-23 20:33:32 -0800850 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000851 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800852 struct tc_action *act;
853 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Patrick McHardycee63722008-01-23 20:33:32 -0800855 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
856 if (ret < 0)
857 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000859 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700860 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000861 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700862 else
863 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
865
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800866 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000867 act = tcf_action_get_1(tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800868 if (IS_ERR(act)) {
869 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800871 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800872 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800873 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875
876 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800877 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 else { /* delete */
879 struct sk_buff *skb;
880
881 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
882 if (!skb) {
883 ret = -ENOBUFS;
884 goto err;
885 }
886
WANG Cong33be6272013-12-15 20:15:05 -0800887 if (tca_get_fill(skb, &actions, portid, n->nlmsg_seq, 0, event,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900888 0, 1) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 kfree_skb(skb);
890 ret = -EINVAL;
891 goto err;
892 }
893
894 /* now do the delete */
WANG Cong33be6272013-12-15 20:15:05 -0800895 tcf_action_destroy(&actions, 0);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000896 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000897 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (ret > 0)
899 return 0;
900 return ret;
901 }
902err:
WANG Cong33be6272013-12-15 20:15:05 -0800903 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return ret;
905}
906
WANG Cong33be6272013-12-15 20:15:05 -0800907static int tcf_add_notify(struct net *net, struct list_head *actions,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000908 u32 portid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910 struct tcamsg *t;
911 struct nlmsghdr *nlh;
912 struct sk_buff *skb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800913 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 unsigned char *b;
915 int err = 0;
916
917 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
918 if (!skb)
919 return -ENOBUFS;
920
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700921 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Eric W. Biederman15e47302012-09-07 20:12:54 +0000923 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700924 if (!nlh)
925 goto out_kfree_skb;
926 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700928 t->tca__pad1 = 0;
929 t->tca__pad2 = 0;
930
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800931 nest = nla_nest_start(skb, TCA_ACT_TAB);
932 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700933 goto out_kfree_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
WANG Cong33be6272013-12-15 20:15:05 -0800935 if (tcf_action_dump(skb, actions, 0, 0) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700936 goto out_kfree_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800938 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900939
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700940 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Patrick McHardyac6d4392005-08-14 19:29:52 -0700941 NETLINK_CB(skb).dst_group = RTNLGRP_TC;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900942
Eric W. Biederman15e47302012-09-07 20:12:54 +0000943 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 if (err > 0)
945 err = 0;
946 return err;
947
David S. Miller8b00a532012-06-26 21:39:32 -0700948out_kfree_skb:
Patrick McHardyf6e57462006-03-12 20:33:22 -0800949 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return -1;
951}
952
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954static int
Tom Goff7316ae82010-03-19 15:40:13 +0000955tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000956 u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -0800959 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 u32 seq = n->nlmsg_seq;
961
WANG Cong33be6272013-12-15 20:15:05 -0800962 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
963 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 goto done;
965
966 /* dump then free all the actions after update; inserted policy
967 * stays intact
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000968 */
WANG Cong33be6272013-12-15 20:15:05 -0800969 ret = tcf_add_notify(net, &actions, portid, seq, RTM_NEWACTION, n->nlmsg_flags);
970 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971done:
972 return ret;
973}
974
Thomas Graf661d2962013-03-21 07:45:29 +0000975static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900977 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800978 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +0000979 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 int ret = 0, ovr = 0;
981
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000982 if ((n->nlmsg_type != RTM_GETACTION) && !capable(CAP_NET_ADMIN))
983 return -EPERM;
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:
Eric W. Biederman15e47302012-09-07 20:12:54 +00001006 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, 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,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001012 portid, 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,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001016 portid, 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;
David S. Miller8b00a532012-06-26 21:39:32 -07001062 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
Eric W. Biederman15e47302012-09-07 20:12:54 +00001077 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001078 cb->nlh->nlmsg_type, sizeof(*t), 0);
1079 if (!nlh)
1080 goto out_module_put;
1081 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001083 t->tca__pad1 = 0;
1084 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001086 nest = nla_nest_start(skb, TCA_ACT_TAB);
1087 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001088 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1091 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001092 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001095 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 ret = skb->len;
1097 } else
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001098 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001100 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001101 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 nlh->nlmsg_flags |= NLM_F_MULTI;
1103 module_put(a_o->owner);
1104 return skb->len;
1105
David S. Miller8b00a532012-06-26 21:39:32 -07001106out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001108 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return skb->len;
1110}
1111
1112static int __init tc_action_init(void)
1113{
Greg Rosec7ac8672011-06-10 01:27:09 +00001114 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1115 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1116 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1117 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return 0;
1120}
1121
1122subsys_initcall(tc_action_init);