blob: dc457c9576568ebab3ed62ed8ead66241328dfea [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{
WANG Cong89819dc2013-12-15 20:15:09 -080032 spin_lock_bh(&hinfo->lock);
33 hlist_del(&p->tcfc_head);
34 spin_unlock_bh(&hinfo->lock);
35 gen_kill_estimator(&p->tcfc_bstats,
36 &p->tcfc_rate_est);
37 /*
38 * gen_estimator est_timer() might access p->tcfc_lock
39 * or bstats, wait a RCU grace period before freeing p
40 */
41 kfree_rcu(p, tcfc_rcu);
David S. Millere9ce1cd2006-08-21 23:54:55 -070042}
43EXPORT_SYMBOL(tcf_hash_destroy);
44
45int tcf_hash_release(struct tcf_common *p, int bind,
46 struct tcf_hashinfo *hinfo)
47{
48 int ret = 0;
49
50 if (p) {
51 if (bind)
52 p->tcfc_bindcnt--;
53
54 p->tcfc_refcnt--;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090055 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070056 tcf_hash_destroy(p, hinfo);
57 ret = 1;
58 }
59 }
60 return ret;
61}
62EXPORT_SYMBOL(tcf_hash_release);
63
64static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
65 struct tc_action *a, struct tcf_hashinfo *hinfo)
66{
WANG Cong89819dc2013-12-15 20:15:09 -080067 struct hlist_head *head;
David S. Millere9ce1cd2006-08-21 23:54:55 -070068 struct tcf_common *p;
Eric Dumazetcc7ec452011-01-19 19:26:56 +000069 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080070 struct nlattr *nest;
David S. Millere9ce1cd2006-08-21 23:54:55 -070071
WANG Cong89819dc2013-12-15 20:15:09 -080072 spin_lock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -070073
74 s_i = cb->args[0];
75
76 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Cong89819dc2013-12-15 20:15:09 -080077 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
David S. Millere9ce1cd2006-08-21 23:54:55 -070078
WANG Cong89819dc2013-12-15 20:15:09 -080079 hlist_for_each_entry_rcu(p, head, tcfc_head) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070080 index++;
81 if (index < s_i)
82 continue;
83 a->priv = p;
84 a->order = n_i;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080085
86 nest = nla_nest_start(skb, a->order);
87 if (nest == NULL)
88 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -070089 err = tcf_action_dump_1(skb, a, 0, 0);
90 if (err < 0) {
91 index--;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080092 nlmsg_trim(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -070093 goto done;
94 }
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080095 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -070096 n_i++;
97 if (n_i >= TCA_ACT_MAX_PRIO)
98 goto done;
99 }
100 }
101done:
WANG Cong89819dc2013-12-15 20:15:09 -0800102 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700103 if (n_i)
104 cb->args[0] += n_i;
105 return n_i;
106
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800107nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800108 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700109 goto done;
110}
111
112static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a,
113 struct tcf_hashinfo *hinfo)
114{
WANG Cong89819dc2013-12-15 20:15:09 -0800115 struct hlist_head *head;
116 struct hlist_node *n;
117 struct tcf_common *p;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800118 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000119 int i = 0, n_i = 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700120
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800121 nest = nla_nest_start(skb, a->order);
122 if (nest == NULL)
123 goto nla_put_failure;
David S. Miller1b34ec42012-03-29 05:11:39 -0400124 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
125 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700126 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Cong89819dc2013-12-15 20:15:09 -0800127 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
128 hlist_for_each_entry_safe(p, n, head, tcfc_head) {
David S. Millere9ce1cd2006-08-21 23:54:55 -0700129 if (ACT_P_DELETED == tcf_hash_release(p, 0, hinfo))
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000130 module_put(a->ops->owner);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700131 n_i++;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700132 }
133 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400134 if (nla_put_u32(skb, TCA_FCNT, n_i))
135 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800136 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700137
138 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800139nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800140 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700141 return -EINVAL;
142}
143
144int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
145 int type, struct tc_action *a)
146{
147 struct tcf_hashinfo *hinfo = a->ops->hinfo;
148
149 if (type == RTM_DELACTION) {
150 return tcf_del_walker(skb, a, hinfo);
151 } else if (type == RTM_GETACTION) {
152 return tcf_dump_walker(skb, cb, a, hinfo);
153 } else {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000154 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700155 return -EINVAL;
156 }
157}
158EXPORT_SYMBOL(tcf_generic_walker);
159
160struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
161{
WANG Cong89819dc2013-12-15 20:15:09 -0800162 struct tcf_common *p = NULL;
163 struct hlist_head *head;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700164
WANG Cong89819dc2013-12-15 20:15:09 -0800165 spin_lock_bh(&hinfo->lock);
166 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
167 hlist_for_each_entry_rcu(p, head, tcfc_head)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700168 if (p->tcfc_index == index)
169 break;
WANG Cong89819dc2013-12-15 20:15:09 -0800170 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700171
172 return p;
173}
174EXPORT_SYMBOL(tcf_hash_lookup);
175
176u32 tcf_hash_new_index(u32 *idx_gen, struct tcf_hashinfo *hinfo)
177{
178 u32 val = *idx_gen;
179
180 do {
181 if (++val == 0)
182 val = 1;
183 } while (tcf_hash_lookup(val, hinfo));
184
Yang Yingliang17569fa2013-12-10 20:55:29 +0800185 *idx_gen = val;
186 return val;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700187}
188EXPORT_SYMBOL(tcf_hash_new_index);
189
190int tcf_hash_search(struct tc_action *a, u32 index)
191{
192 struct tcf_hashinfo *hinfo = a->ops->hinfo;
193 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
194
195 if (p) {
196 a->priv = p;
197 return 1;
198 }
199 return 0;
200}
201EXPORT_SYMBOL(tcf_hash_search);
202
203struct tcf_common *tcf_hash_check(u32 index, struct tc_action *a, int bind,
204 struct tcf_hashinfo *hinfo)
205{
206 struct tcf_common *p = NULL;
207 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700208 if (bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700209 p->tcfc_bindcnt++;
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700210 p->tcfc_refcnt++;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700211 a->priv = p;
212 }
213 return p;
214}
215EXPORT_SYMBOL(tcf_hash_check);
216
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800217struct tcf_common *tcf_hash_create(u32 index, struct nlattr *est,
218 struct tc_action *a, int size, int bind,
219 u32 *idx_gen, struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700220{
221 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
222
223 if (unlikely(!p))
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800224 return ERR_PTR(-ENOMEM);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700225 p->tcfc_refcnt = 1;
226 if (bind)
227 p->tcfc_bindcnt = 1;
228
229 spin_lock_init(&p->tcfc_lock);
WANG Cong89819dc2013-12-15 20:15:09 -0800230 INIT_HLIST_NODE(&p->tcfc_head);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700231 p->tcfc_index = index ? index : tcf_hash_new_index(idx_gen, hinfo);
232 p->tcfc_tm.install = jiffies;
233 p->tcfc_tm.lastuse = jiffies;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800234 if (est) {
235 int err = gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
236 &p->tcfc_lock, est);
237 if (err) {
238 kfree(p);
239 return ERR_PTR(err);
240 }
241 }
242
David S. Millere9ce1cd2006-08-21 23:54:55 -0700243 a->priv = (void *) p;
244 return p;
245}
246EXPORT_SYMBOL(tcf_hash_create);
247
248void tcf_hash_insert(struct tcf_common *p, struct tcf_hashinfo *hinfo)
249{
250 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
251
WANG Cong89819dc2013-12-15 20:15:09 -0800252 spin_lock_bh(&hinfo->lock);
253 hlist_add_head(&p->tcfc_head, &hinfo->htab[h]);
254 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700255}
256EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258static struct tc_action_ops *act_base = NULL;
259static DEFINE_RWLOCK(act_mod_lock);
260
261int tcf_register_action(struct tc_action_ops *act)
262{
263 struct tc_action_ops *a, **ap;
264
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500265 /* Must supply act, dump, cleanup and init */
266 if (!act->act || !act->dump || !act->cleanup || !act->init)
267 return -EINVAL;
268
Jamal Hadi Salim382ca8a2013-12-04 09:26:55 -0500269 /* Supply defaults */
Jamal Hadi Salim63ef6172013-12-04 09:26:53 -0500270 if (!act->lookup)
271 act->lookup = tcf_hash_search;
Jamal Hadi Salim382ca8a2013-12-04 09:26:55 -0500272 if (!act->walk)
273 act->walk = tcf_generic_walker;
Jamal Hadi Salim63ef6172013-12-04 09:26:53 -0500274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 write_lock(&act_mod_lock);
276 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next) {
277 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
278 write_unlock(&act_mod_lock);
279 return -EEXIST;
280 }
281 }
282 act->next = NULL;
283 *ap = act;
284 write_unlock(&act_mod_lock);
285 return 0;
286}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800287EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289int tcf_unregister_action(struct tc_action_ops *act)
290{
291 struct tc_action_ops *a, **ap;
292 int err = -ENOENT;
293
294 write_lock(&act_mod_lock);
295 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next)
296 if (a == act)
297 break;
298 if (a) {
299 *ap = a->next;
300 a->next = NULL;
301 err = 0;
302 }
303 write_unlock(&act_mod_lock);
304 return err;
305}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800306EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308/* lookup by name */
309static struct tc_action_ops *tc_lookup_action_n(char *kind)
310{
311 struct tc_action_ops *a = NULL;
312
313 if (kind) {
314 read_lock(&act_mod_lock);
315 for (a = act_base; a; a = a->next) {
316 if (strcmp(kind, a->kind) == 0) {
317 if (!try_module_get(a->owner)) {
318 read_unlock(&act_mod_lock);
319 return NULL;
320 }
321 break;
322 }
323 }
324 read_unlock(&act_mod_lock);
325 }
326 return a;
327}
328
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800329/* lookup by nlattr */
330static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 struct tc_action_ops *a = NULL;
333
334 if (kind) {
335 read_lock(&act_mod_lock);
336 for (a = act_base; a; a = a->next) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800337 if (nla_strcmp(kind, a->kind) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (!try_module_get(a->owner)) {
339 read_unlock(&act_mod_lock);
340 return NULL;
341 }
342 break;
343 }
344 }
345 read_unlock(&act_mod_lock);
346 }
347 return a;
348}
349
350#if 0
351/* lookup by id */
352static struct tc_action_ops *tc_lookup_action_id(u32 type)
353{
354 struct tc_action_ops *a = NULL;
355
356 if (type) {
357 read_lock(&act_mod_lock);
358 for (a = act_base; a; a = a->next) {
359 if (a->type == type) {
360 if (!try_module_get(a->owner)) {
361 read_unlock(&act_mod_lock);
362 return NULL;
363 }
364 break;
365 }
366 }
367 read_unlock(&act_mod_lock);
368 }
369 return a;
370}
371#endif
372
WANG Cong33be6272013-12-15 20:15:05 -0800373int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900374 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000376 const struct tc_action *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 int ret = -1;
378
379 if (skb->tc_verd & TC_NCLS) {
380 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 ret = TC_ACT_OK;
382 goto exec_done;
383 }
WANG Cong33be6272013-12-15 20:15:05 -0800384 list_for_each_entry(a, actions, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385repeat:
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500386 if (a->ops) {
Patrick McHardyf43c5a02006-01-08 22:15:34 -0800387 ret = a->ops->act(skb, a, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (TC_MUNGED & skb->tc_verd) {
389 /* copied already, allow trampling */
390 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
391 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (ret == TC_ACT_REPEAT)
394 goto repeat; /* we need a ttl - JHS */
J Hadi Salim14d50e72005-05-03 16:29:13 -0700395 if (ret != TC_ACT_PIPE)
396 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return ret;
401}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800402EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
WANG Cong33be6272013-12-15 20:15:05 -0800404void tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
WANG Cong33be6272013-12-15 20:15:05 -0800406 struct tc_action *a, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
WANG Cong33be6272013-12-15 20:15:05 -0800408 list_for_each_entry_safe(a, tmp, actions, list) {
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500409 if (a->ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
411 module_put(a->ops->owner);
WANG Cong33be6272013-12-15 20:15:05 -0800412 list_del(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 kfree(a);
stephen hemminger6ff9c362010-05-12 06:37:05 +0000414 } else {
415 /*FIXME: Remove later - catch insertion bugs*/
416 WARN(1, "tcf_action_destroy: BUG? destroying NULL ops\n");
WANG Cong33be6272013-12-15 20:15:05 -0800417 list_del(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 kfree(a);
419 }
420 }
421}
422
423int
424tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
425{
426 int err = -EINVAL;
427
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500428 if (a->ops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return err;
430 return a->ops->dump(skb, a, bind, ref);
431}
432
433int
434tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
435{
436 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700437 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800438 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500440 if (a->ops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return err;
442
David S. Miller1b34ec42012-03-29 05:11:39 -0400443 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
444 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800446 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800447 nest = nla_nest_start(skb, TCA_OPTIONS);
448 if (nest == NULL)
449 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000450 err = tcf_action_dump_old(skb, a, bind, ref);
451 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800452 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return err;
454 }
455
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800456nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700457 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return -1;
459}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800460EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462int
WANG Cong33be6272013-12-15 20:15:05 -0800463tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 struct tc_action *a;
466 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800467 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
WANG Cong33be6272013-12-15 20:15:05 -0800469 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800470 nest = nla_nest_start(skb, a->order);
471 if (nest == NULL)
472 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 err = tcf_action_dump_1(skb, a, bind, ref);
474 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700475 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800476 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478
479 return 0;
480
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800481nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700482 err = -EINVAL;
483errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800484 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700485 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000488struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
489 struct nlattr *est, char *name, int ovr,
490 int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 struct tc_action *a;
493 struct tc_action_ops *a_o;
494 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000495 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800496 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800497 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800500 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
501 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800503 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800504 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (kind == NULL)
506 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800507 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 goto err_out;
509 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800510 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
512 goto err_out;
513 }
514
515 a_o = tc_lookup_action_n(act_name);
516 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700517#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800519 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 rtnl_lock();
521
522 a_o = tc_lookup_action_n(act_name);
523
524 /* We dropped the RTNL semaphore in order to
525 * perform the module load. So, even if we
526 * succeeded in loading the module we have to
527 * tell the caller to replay the request. We
528 * indicate this using -EAGAIN.
529 */
530 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800531 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 goto err_mod;
533 }
534#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800535 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 goto err_out;
537 }
538
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800539 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700540 a = kzalloc(sizeof(*a), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (a == NULL)
542 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
WANG Cong33be6272013-12-15 20:15:05 -0800544 INIT_LIST_HEAD(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 /* backward compatibility for policer */
546 if (name == NULL)
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000547 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 else
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000549 err = a_o->init(net, nla, est, a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800550 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 goto err_free;
552
553 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000554 * if it exists and is only bound to in a_o->init() then
555 * ACT_P_CREATED is not returned (a zero is).
556 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800557 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 module_put(a_o->owner);
559 a->ops = a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return a;
562
563err_free:
564 kfree(a);
565err_mod:
566 module_put(a_o->owner);
567err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800568 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
WANG Cong33be6272013-12-15 20:15:05 -0800571int tcf_action_init(struct net *net, struct nlattr *nla,
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000572 struct nlattr *est, char *name, int ovr,
WANG Cong33be6272013-12-15 20:15:05 -0800573 int bind, struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000575 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800576 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800577 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 int i;
579
Patrick McHardycee63722008-01-23 20:33:32 -0800580 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
581 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800582 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800584 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000585 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800586 if (IS_ERR(act)) {
587 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800589 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800590 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800591 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
WANG Cong33be6272013-12-15 20:15:05 -0800593 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595err:
WANG Cong33be6272013-12-15 20:15:05 -0800596 tcf_action_destroy(actions, bind);
597 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
600int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
601 int compat_mode)
602{
603 int err = 0;
604 struct gnet_dump d;
605 struct tcf_act_hdr *h = a->priv;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (h == NULL)
608 goto errout;
609
610 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400611 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 */
613 if (compat_mode) {
614 if (a->type == TCA_OLD_COMPAT)
615 err = gnet_stats_start_copy_compat(skb, 0,
Patrick McHardy4bdf3992007-07-02 22:47:37 -0700616 TCA_STATS, TCA_XSTATS, &h->tcf_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 else
618 return 0;
619 } else
620 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
Patrick McHardy4bdf3992007-07-02 22:47:37 -0700621 &h->tcf_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 if (err < 0)
624 goto errout;
625
David S. Millere9ce1cd2006-08-21 23:54:55 -0700626 if (gnet_stats_copy_basic(&d, &h->tcf_bstats) < 0 ||
Eric Dumazetd250a5f2009-10-02 10:32:18 +0000627 gnet_stats_copy_rate_est(&d, &h->tcf_bstats,
628 &h->tcf_rate_est) < 0 ||
David S. Millere9ce1cd2006-08-21 23:54:55 -0700629 gnet_stats_copy_queue(&d, &h->tcf_qstats) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 goto errout;
631
632 if (gnet_stats_finish_copy(&d) < 0)
633 goto errout;
634
635 return 0;
636
637errout:
638 return -1;
639}
640
641static int
WANG Cong33be6272013-12-15 20:15:05 -0800642tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900643 u16 flags, int event, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 struct tcamsg *t;
646 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700647 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800648 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Eric W. Biederman15e47302012-09-07 20:12:54 +0000650 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700651 if (!nlh)
652 goto out_nlmsg_trim;
653 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700655 t->tca__pad1 = 0;
656 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900657
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800658 nest = nla_nest_start(skb, TCA_ACT_TAB);
659 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700660 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
WANG Cong33be6272013-12-15 20:15:05 -0800662 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700663 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800665 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900666
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700667 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return skb->len;
669
David S. Miller8b00a532012-06-26 21:39:32 -0700670out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700671 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return -1;
673}
674
675static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000676act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800677 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
682 if (!skb)
683 return -ENOBUFS;
WANG Cong33be6272013-12-15 20:15:05 -0800684 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 kfree_skb(skb);
686 return -EINVAL;
687 }
Thomas Graf2942e902006-08-15 00:30:25 -0700688
Eric W. Biederman15e47302012-09-07 20:12:54 +0000689 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
692static struct tc_action *
Eric W. Biederman15e47302012-09-07 20:12:54 +0000693tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000695 struct nlattr *tb[TCA_ACT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 struct tc_action *a;
697 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800698 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Patrick McHardycee63722008-01-23 20:33:32 -0800700 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
701 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800702 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Patrick McHardycee63722008-01-23 20:33:32 -0800704 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800705 if (tb[TCA_ACT_INDEX] == NULL ||
706 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800707 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800708 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800710 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700711 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (a == NULL)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800713 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
WANG Cong33be6272013-12-15 20:15:05 -0800715 INIT_LIST_HEAD(&a->list);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800716 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800717 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (a->ops == NULL)
719 goto err_free;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800720 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (a->ops->lookup(a, index) == 0)
722 goto err_mod;
723
724 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727err_mod:
728 module_put(a->ops->owner);
729err_free:
730 kfree(a);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800731err_out:
732 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
WANG Cong33be6272013-12-15 20:15:05 -0800735static void cleanup_a(struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
WANG Cong33be6272013-12-15 20:15:05 -0800737 struct tc_action *a, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
WANG Cong33be6272013-12-15 20:15:05 -0800739 list_for_each_entry_safe(a, tmp, actions, list) {
740 list_del(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 kfree(a);
742 }
743}
744
745static struct tc_action *create_a(int i)
746{
747 struct tc_action *act;
748
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700749 act = kzalloc(sizeof(*act), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (act == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000751 pr_debug("create_a: failed to alloc!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return NULL;
753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800755 INIT_LIST_HEAD(&act->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return act;
757}
758
Tom Goff7316ae82010-03-19 15:40:13 +0000759static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000760 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 struct sk_buff *skb;
763 unsigned char *b;
764 struct nlmsghdr *nlh;
765 struct tcamsg *t;
766 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800767 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000768 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800769 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 struct tc_action *a = create_a(0);
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700771 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 if (a == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000774 pr_debug("tca_action_flush: couldnt create tc_action\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return err;
776 }
777
778 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
779 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000780 pr_debug("tca_action_flush: failed skb alloc\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 kfree(a);
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700782 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700785 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Patrick McHardycee63722008-01-23 20:33:32 -0800787 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
788 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 goto err_out;
790
Patrick McHardycee63722008-01-23 20:33:32 -0800791 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800792 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 a->ops = tc_lookup_action(kind);
794 if (a->ops == NULL)
795 goto err_out;
796
Eric W. Biederman15e47302012-09-07 20:12:54 +0000797 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700798 if (!nlh)
799 goto out_module_put;
800 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 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)
David S. Miller8b00a532012-06-26 21:39:32 -0700807 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
810 if (err < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700811 goto out_module_put;
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 W. Biederman15e47302012-09-07 20:12:54 +0000821 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000822 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
David S. Miller8b00a532012-06-26 21:39:32 -0700828out_module_put:
Thomas Grafebbaeab2006-07-09 11:36:23 -0700829 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700831noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 kfree_skb(skb);
833 kfree(a);
834 return err;
835}
836
837static int
Tom Goff7316ae82010-03-19 15:40:13 +0000838tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000839 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Patrick McHardycee63722008-01-23 20:33:32 -0800841 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000842 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800843 struct tc_action *act;
844 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
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)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000852 return tca_action_flush(net, tb[1], n, portid);
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++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000858 act = tcf_action_get_1(tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800859 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;
WANG Cong33be6272013-12-15 20:15:05 -0800864 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866
867 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800868 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 else { /* delete */
870 struct sk_buff *skb;
871
872 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
873 if (!skb) {
874 ret = -ENOBUFS;
875 goto err;
876 }
877
WANG Cong33be6272013-12-15 20:15:05 -0800878 if (tca_get_fill(skb, &actions, portid, n->nlmsg_seq, 0, event,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900879 0, 1) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 kfree_skb(skb);
881 ret = -EINVAL;
882 goto err;
883 }
884
885 /* now do the delete */
WANG Cong33be6272013-12-15 20:15:05 -0800886 tcf_action_destroy(&actions, 0);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000887 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000888 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (ret > 0)
890 return 0;
891 return ret;
892 }
893err:
WANG Cong33be6272013-12-15 20:15:05 -0800894 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return ret;
896}
897
WANG Cong33be6272013-12-15 20:15:05 -0800898static int tcf_add_notify(struct net *net, struct list_head *actions,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000899 u32 portid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 struct tcamsg *t;
902 struct nlmsghdr *nlh;
903 struct sk_buff *skb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800904 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 unsigned char *b;
906 int err = 0;
907
908 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
909 if (!skb)
910 return -ENOBUFS;
911
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700912 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Eric W. Biederman15e47302012-09-07 20:12:54 +0000914 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700915 if (!nlh)
916 goto out_kfree_skb;
917 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700919 t->tca__pad1 = 0;
920 t->tca__pad2 = 0;
921
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800922 nest = nla_nest_start(skb, TCA_ACT_TAB);
923 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700924 goto out_kfree_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
WANG Cong33be6272013-12-15 20:15:05 -0800926 if (tcf_action_dump(skb, actions, 0, 0) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700927 goto out_kfree_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800929 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900930
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700931 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Patrick McHardyac6d4392005-08-14 19:29:52 -0700932 NETLINK_CB(skb).dst_group = RTNLGRP_TC;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900933
Eric W. Biederman15e47302012-09-07 20:12:54 +0000934 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (err > 0)
936 err = 0;
937 return err;
938
David S. Miller8b00a532012-06-26 21:39:32 -0700939out_kfree_skb:
Patrick McHardyf6e57462006-03-12 20:33:22 -0800940 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return -1;
942}
943
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945static int
Tom Goff7316ae82010-03-19 15:40:13 +0000946tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000947 u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
949 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -0800950 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 u32 seq = n->nlmsg_seq;
952
WANG Cong33be6272013-12-15 20:15:05 -0800953 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
954 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 goto done;
956
957 /* dump then free all the actions after update; inserted policy
958 * stays intact
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000959 */
WANG Cong33be6272013-12-15 20:15:05 -0800960 ret = tcf_add_notify(net, &actions, portid, seq, RTM_NEWACTION, n->nlmsg_flags);
961 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962done:
963 return ret;
964}
965
Thomas Graf661d2962013-03-21 07:45:29 +0000966static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900968 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800969 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +0000970 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 int ret = 0, ovr = 0;
972
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000973 if ((n->nlmsg_type != RTM_GETACTION) && !capable(CAP_NET_ADMIN))
974 return -EPERM;
975
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800976 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
977 if (ret < 0)
978 return ret;
979
980 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000981 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return -EINVAL;
983 }
984
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000985 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 switch (n->nlmsg_type) {
987 case RTM_NEWACTION:
988 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300989 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 * Note that CREATE | EXCL implies that
991 * but since we want avoid ambiguity (eg when flags
992 * is zero) then just set this
993 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000994 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 ovr = 1;
996replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000997 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (ret == -EAGAIN)
999 goto replay;
1000 break;
1001 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001002 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001003 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 break;
1005 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001006 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001007 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 break;
1009 default:
1010 BUG();
1011 }
1012
1013 return ret;
1014}
1015
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001016static struct nlattr *
Patrick McHardy3a6c2b42009-08-25 16:07:40 +02001017find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001019 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001020 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1021 struct nlattr *nla[TCAA_MAX + 1];
1022 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Patrick McHardyc96c9472008-01-23 20:32:58 -08001024 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001026 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (tb1 == NULL)
1028 return NULL;
1029
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001030 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1031 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Patrick McHardy6d834e02008-01-23 20:32:42 -08001034 if (tb[1] == NULL)
1035 return NULL;
1036 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1037 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001039 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Thomas Graf26dab892006-07-05 20:45:06 -07001041 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
1044static int
1045tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1046{
1047 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001048 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001049 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 struct tc_action_ops *a_o;
1051 struct tc_action a;
1052 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001053 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001054 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001057 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return 0;
1059 }
1060
Thomas Graf26dab892006-07-05 20:45:06 -07001061 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001062 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 memset(&a, 0, sizeof(struct tc_action));
1066 a.ops = a_o;
1067
Eric W. Biederman15e47302012-09-07 20:12:54 +00001068 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001069 cb->nlh->nlmsg_type, sizeof(*t), 0);
1070 if (!nlh)
1071 goto out_module_put;
1072 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001074 t->tca__pad1 = 0;
1075 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001077 nest = nla_nest_start(skb, TCA_ACT_TAB);
1078 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001079 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1082 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001083 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001086 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 ret = skb->len;
1088 } else
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001089 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001091 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001092 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 nlh->nlmsg_flags |= NLM_F_MULTI;
1094 module_put(a_o->owner);
1095 return skb->len;
1096
David S. Miller8b00a532012-06-26 21:39:32 -07001097out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001099 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return skb->len;
1101}
1102
1103static int __init tc_action_init(void)
1104{
Greg Rosec7ac8672011-06-10 01:27:09 +00001105 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1106 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1107 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1108 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return 0;
1111}
1112
1113subsys_initcall(tc_action_init);