blob: 4f2b807b3621b58aff1cfc0dd7b402b6026ecd99 [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
WANG Cong86062032014-02-11 17:07:31 -080030void tcf_hash_destroy(struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -070031{
WANG Cong86062032014-02-11 17:07:31 -080032 struct tcf_common *p = a->priv;
33 struct tcf_hashinfo *hinfo = a->ops->hinfo;
34
WANG Cong89819dc2013-12-15 20:15:09 -080035 spin_lock_bh(&hinfo->lock);
36 hlist_del(&p->tcfc_head);
37 spin_unlock_bh(&hinfo->lock);
38 gen_kill_estimator(&p->tcfc_bstats,
39 &p->tcfc_rate_est);
40 /*
41 * gen_estimator est_timer() might access p->tcfc_lock
42 * or bstats, wait a RCU grace period before freeing p
43 */
44 kfree_rcu(p, tcfc_rcu);
David S. Millere9ce1cd2006-08-21 23:54:55 -070045}
46EXPORT_SYMBOL(tcf_hash_destroy);
47
WANG Cong86062032014-02-11 17:07:31 -080048int tcf_hash_release(struct tc_action *a, int bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -070049{
WANG Cong86062032014-02-11 17:07:31 -080050 struct tcf_common *p = a->priv;
David S. Millere9ce1cd2006-08-21 23:54:55 -070051 int ret = 0;
52
53 if (p) {
54 if (bind)
55 p->tcfc_bindcnt--;
56
57 p->tcfc_refcnt--;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090058 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
WANG Cong86062032014-02-11 17:07:31 -080059 tcf_hash_destroy(a);
David S. Millere9ce1cd2006-08-21 23:54:55 -070060 ret = 1;
61 }
62 }
63 return ret;
64}
65EXPORT_SYMBOL(tcf_hash_release);
66
67static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
WANG Congc779f7a2014-01-17 11:37:02 -080068 struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -070069{
WANG Congc779f7a2014-01-17 11:37:02 -080070 struct tcf_hashinfo *hinfo = a->ops->hinfo;
WANG Cong89819dc2013-12-15 20:15:09 -080071 struct hlist_head *head;
David S. Millere9ce1cd2006-08-21 23:54:55 -070072 struct tcf_common *p;
Eric Dumazetcc7ec452011-01-19 19:26:56 +000073 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080074 struct nlattr *nest;
David S. Millere9ce1cd2006-08-21 23:54:55 -070075
WANG Cong89819dc2013-12-15 20:15:09 -080076 spin_lock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -070077
78 s_i = cb->args[0];
79
80 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Cong89819dc2013-12-15 20:15:09 -080081 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
David S. Millere9ce1cd2006-08-21 23:54:55 -070082
WANG Cong89819dc2013-12-15 20:15:09 -080083 hlist_for_each_entry_rcu(p, head, tcfc_head) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070084 index++;
85 if (index < s_i)
86 continue;
87 a->priv = p;
88 a->order = n_i;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080089
90 nest = nla_nest_start(skb, a->order);
91 if (nest == NULL)
92 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -070093 err = tcf_action_dump_1(skb, a, 0, 0);
94 if (err < 0) {
95 index--;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080096 nlmsg_trim(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -070097 goto done;
98 }
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080099 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700100 n_i++;
101 if (n_i >= TCA_ACT_MAX_PRIO)
102 goto done;
103 }
104 }
105done:
WANG Cong89819dc2013-12-15 20:15:09 -0800106 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700107 if (n_i)
108 cb->args[0] += n_i;
109 return n_i;
110
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800111nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800112 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700113 goto done;
114}
115
WANG Congc779f7a2014-01-17 11:37:02 -0800116static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700117{
WANG Congc779f7a2014-01-17 11:37:02 -0800118 struct tcf_hashinfo *hinfo = a->ops->hinfo;
WANG Cong89819dc2013-12-15 20:15:09 -0800119 struct hlist_head *head;
120 struct hlist_node *n;
121 struct tcf_common *p;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800122 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000123 int i = 0, n_i = 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700124
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800125 nest = nla_nest_start(skb, a->order);
126 if (nest == NULL)
127 goto nla_put_failure;
David S. Miller1b34ec42012-03-29 05:11:39 -0400128 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
129 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700130 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Cong89819dc2013-12-15 20:15:09 -0800131 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
132 hlist_for_each_entry_safe(p, n, head, tcfc_head) {
WANG Cong86062032014-02-11 17:07:31 -0800133 a->priv = p;
134 if (ACT_P_DELETED == tcf_hash_release(a, 0)) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000135 module_put(a->ops->owner);
Jamal Hadi Salim805c1f42013-12-23 08:02:13 -0500136 n_i++;
137 }
David S. Millere9ce1cd2006-08-21 23:54:55 -0700138 }
139 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400140 if (nla_put_u32(skb, TCA_FCNT, n_i))
141 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800142 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700143
144 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800145nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800146 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700147 return -EINVAL;
148}
149
stephen hemminger9c75f402013-12-31 11:54:00 -0800150static int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
151 int type, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700152{
David S. Millere9ce1cd2006-08-21 23:54:55 -0700153 if (type == RTM_DELACTION) {
WANG Congc779f7a2014-01-17 11:37:02 -0800154 return tcf_del_walker(skb, a);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700155 } else if (type == RTM_GETACTION) {
WANG Congc779f7a2014-01-17 11:37:02 -0800156 return tcf_dump_walker(skb, cb, a);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700157 } else {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000158 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700159 return -EINVAL;
160 }
161}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700162
WANG Cong6e6a50c2014-01-17 11:37:03 -0800163static struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700164{
WANG Cong89819dc2013-12-15 20:15:09 -0800165 struct tcf_common *p = NULL;
166 struct hlist_head *head;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700167
WANG Cong89819dc2013-12-15 20:15:09 -0800168 spin_lock_bh(&hinfo->lock);
169 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
170 hlist_for_each_entry_rcu(p, head, tcfc_head)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700171 if (p->tcfc_index == index)
172 break;
WANG Cong89819dc2013-12-15 20:15:09 -0800173 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700174
175 return p;
176}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700177
WANG Congddafd342014-01-09 16:13:59 -0800178u32 tcf_hash_new_index(struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700179{
WANG Congddafd342014-01-09 16:13:59 -0800180 u32 val = hinfo->index;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700181
182 do {
183 if (++val == 0)
184 val = 1;
185 } while (tcf_hash_lookup(val, hinfo));
186
WANG Congddafd342014-01-09 16:13:59 -0800187 hinfo->index = val;
Yang Yingliang17569fa2013-12-10 20:55:29 +0800188 return val;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700189}
190EXPORT_SYMBOL(tcf_hash_new_index);
191
WANG Cong6e6a50c2014-01-17 11:37:03 -0800192int tcf_hash_search(struct tc_action *a, u32 index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700193{
194 struct tcf_hashinfo *hinfo = a->ops->hinfo;
195 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
196
197 if (p) {
198 a->priv = p;
199 return 1;
200 }
201 return 0;
202}
WANG Cong6e6a50c2014-01-17 11:37:03 -0800203EXPORT_SYMBOL(tcf_hash_search);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700204
WANG Cong86062032014-02-11 17:07:31 -0800205int tcf_hash_check(u32 index, struct tc_action *a, int bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700206{
WANG Congc779f7a2014-01-17 11:37:02 -0800207 struct tcf_hashinfo *hinfo = a->ops->hinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700208 struct tcf_common *p = NULL;
209 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700210 if (bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700211 p->tcfc_bindcnt++;
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700212 p->tcfc_refcnt++;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700213 a->priv = p;
WANG Cong86062032014-02-11 17:07:31 -0800214 return 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700215 }
WANG Cong86062032014-02-11 17:07:31 -0800216 return 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700217}
218EXPORT_SYMBOL(tcf_hash_check);
219
WANG Cong86062032014-02-11 17:07:31 -0800220void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
221{
222 struct tcf_common *pc = a->priv;
223 if (est)
224 gen_kill_estimator(&pc->tcfc_bstats,
225 &pc->tcfc_rate_est);
226 kfree_rcu(pc, tcfc_rcu);
227}
228EXPORT_SYMBOL(tcf_hash_cleanup);
229
230int tcf_hash_create(u32 index, struct nlattr *est, struct tc_action *a,
231 int size, int bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700232{
WANG Congc779f7a2014-01-17 11:37:02 -0800233 struct tcf_hashinfo *hinfo = a->ops->hinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700234 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
235
236 if (unlikely(!p))
WANG Cong86062032014-02-11 17:07:31 -0800237 return -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700238 p->tcfc_refcnt = 1;
239 if (bind)
240 p->tcfc_bindcnt = 1;
241
242 spin_lock_init(&p->tcfc_lock);
WANG Cong89819dc2013-12-15 20:15:09 -0800243 INIT_HLIST_NODE(&p->tcfc_head);
WANG Congddafd342014-01-09 16:13:59 -0800244 p->tcfc_index = index ? index : tcf_hash_new_index(hinfo);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700245 p->tcfc_tm.install = jiffies;
246 p->tcfc_tm.lastuse = jiffies;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800247 if (est) {
248 int err = gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
249 &p->tcfc_lock, est);
250 if (err) {
251 kfree(p);
WANG Cong86062032014-02-11 17:07:31 -0800252 return err;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800253 }
254 }
255
David S. Millere9ce1cd2006-08-21 23:54:55 -0700256 a->priv = (void *) p;
WANG Cong86062032014-02-11 17:07:31 -0800257 return 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700258}
259EXPORT_SYMBOL(tcf_hash_create);
260
WANG Cong86062032014-02-11 17:07:31 -0800261void tcf_hash_insert(struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700262{
WANG Cong86062032014-02-11 17:07:31 -0800263 struct tcf_common *p = a->priv;
264 struct tcf_hashinfo *hinfo = a->ops->hinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700265 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
266
WANG Cong89819dc2013-12-15 20:15:09 -0800267 spin_lock_bh(&hinfo->lock);
268 hlist_add_head(&p->tcfc_head, &hinfo->htab[h]);
269 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700270}
271EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
WANG Cong1f747c22013-12-15 20:15:10 -0800273static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274static DEFINE_RWLOCK(act_mod_lock);
275
276int tcf_register_action(struct tc_action_ops *act)
277{
WANG Cong1f747c22013-12-15 20:15:10 -0800278 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500280 /* Must supply act, dump, cleanup and init */
281 if (!act->act || !act->dump || !act->cleanup || !act->init)
282 return -EINVAL;
283
Jamal Hadi Salim382ca8a2013-12-04 09:26:55 -0500284 /* Supply defaults */
Jamal Hadi Salim63ef6172013-12-04 09:26:53 -0500285 if (!act->lookup)
286 act->lookup = tcf_hash_search;
Jamal Hadi Salim382ca8a2013-12-04 09:26:55 -0500287 if (!act->walk)
288 act->walk = tcf_generic_walker;
Jamal Hadi Salim63ef6172013-12-04 09:26:53 -0500289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800291 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
293 write_unlock(&act_mod_lock);
294 return -EEXIST;
295 }
296 }
WANG Cong1f747c22013-12-15 20:15:10 -0800297 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 write_unlock(&act_mod_lock);
299 return 0;
300}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800301EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303int tcf_unregister_action(struct tc_action_ops *act)
304{
WANG Cong1f747c22013-12-15 20:15:10 -0800305 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 int err = -ENOENT;
307
308 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800309 list_for_each_entry(a, &act_base, head) {
310 if (a == act) {
311 list_del(&act->head);
312 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316 write_unlock(&act_mod_lock);
317 return err;
318}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800319EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321/* lookup by name */
322static struct tc_action_ops *tc_lookup_action_n(char *kind)
323{
Eric Dumazeta7928662013-12-20 12:32:32 -0800324 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 if (kind) {
327 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800328 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800330 if (try_module_get(a->owner))
331 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 break;
333 }
334 }
335 read_unlock(&act_mod_lock);
336 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800337 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800340/* lookup by nlattr */
341static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Eric Dumazeta7928662013-12-20 12:32:32 -0800343 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 if (kind) {
346 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800347 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800348 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800349 if (try_module_get(a->owner))
350 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 break;
352 }
353 }
354 read_unlock(&act_mod_lock);
355 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800356 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
WANG Cong33be6272013-12-15 20:15:05 -0800359int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900360 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000362 const struct tc_action *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 int ret = -1;
364
365 if (skb->tc_verd & TC_NCLS) {
366 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 ret = TC_ACT_OK;
368 goto exec_done;
369 }
WANG Cong33be6272013-12-15 20:15:05 -0800370 list_for_each_entry(a, actions, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500372 ret = a->ops->act(skb, a, res);
373 if (TC_MUNGED & skb->tc_verd) {
374 /* copied already, allow trampling */
375 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
376 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500378 if (ret == TC_ACT_REPEAT)
379 goto repeat; /* we need a ttl - JHS */
380 if (ret != TC_ACT_PIPE)
381 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return ret;
385}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800386EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
WANG Cong33be6272013-12-15 20:15:05 -0800388void tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
WANG Cong33be6272013-12-15 20:15:05 -0800390 struct tc_action *a, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
WANG Cong33be6272013-12-15 20:15:05 -0800392 list_for_each_entry_safe(a, tmp, actions, list) {
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500393 if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
394 module_put(a->ops->owner);
395 list_del(&a->list);
396 kfree(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398}
399
400int
401tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
402{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return a->ops->dump(skb, a, bind, ref);
404}
405
406int
407tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
408{
409 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700410 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800411 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
David S. Miller1b34ec42012-03-29 05:11:39 -0400413 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
414 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800416 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800417 nest = nla_nest_start(skb, TCA_OPTIONS);
418 if (nest == NULL)
419 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000420 err = tcf_action_dump_old(skb, a, bind, ref);
421 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800422 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return err;
424 }
425
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800426nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700427 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return -1;
429}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800430EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432int
WANG Cong33be6272013-12-15 20:15:05 -0800433tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 struct tc_action *a;
436 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800437 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
WANG Cong33be6272013-12-15 20:15:05 -0800439 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800440 nest = nla_nest_start(skb, a->order);
441 if (nest == NULL)
442 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 err = tcf_action_dump_1(skb, a, bind, ref);
444 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700445 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800446 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448
449 return 0;
450
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800451nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700452 err = -EINVAL;
453errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800454 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700455 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000458struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
459 struct nlattr *est, char *name, int ovr,
460 int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 struct tc_action *a;
463 struct tc_action_ops *a_o;
464 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000465 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800466 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800467 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800470 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
471 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800473 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800474 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (kind == NULL)
476 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800477 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 goto err_out;
479 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800480 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
482 goto err_out;
483 }
484
485 a_o = tc_lookup_action_n(act_name);
486 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700487#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800489 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 rtnl_lock();
491
492 a_o = tc_lookup_action_n(act_name);
493
494 /* We dropped the RTNL semaphore in order to
495 * perform the module load. So, even if we
496 * succeeded in loading the module we have to
497 * tell the caller to replay the request. We
498 * indicate this using -EAGAIN.
499 */
500 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800501 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 goto err_mod;
503 }
504#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800505 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 goto err_out;
507 }
508
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800509 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700510 a = kzalloc(sizeof(*a), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (a == NULL)
512 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
WANG Congc779f7a2014-01-17 11:37:02 -0800514 a->ops = a_o;
WANG Cong33be6272013-12-15 20:15:05 -0800515 INIT_LIST_HEAD(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 /* backward compatibility for policer */
517 if (name == NULL)
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000518 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 else
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000520 err = a_o->init(net, nla, est, a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800521 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 goto err_free;
523
524 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000525 * if it exists and is only bound to in a_o->init() then
526 * ACT_P_CREATED is not returned (a zero is).
527 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800528 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return a;
532
533err_free:
534 kfree(a);
535err_mod:
536 module_put(a_o->owner);
537err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800538 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
WANG Cong33be6272013-12-15 20:15:05 -0800541int tcf_action_init(struct net *net, struct nlattr *nla,
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000542 struct nlattr *est, char *name, int ovr,
WANG Cong33be6272013-12-15 20:15:05 -0800543 int bind, struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000545 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800546 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800547 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 int i;
549
Patrick McHardycee63722008-01-23 20:33:32 -0800550 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
551 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800552 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800554 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000555 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800556 if (IS_ERR(act)) {
557 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800559 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800560 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800561 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
WANG Cong33be6272013-12-15 20:15:05 -0800563 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565err:
WANG Cong33be6272013-12-15 20:15:05 -0800566 tcf_action_destroy(actions, bind);
567 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
570int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
571 int compat_mode)
572{
573 int err = 0;
574 struct gnet_dump d;
WANG Cong7eb88962014-01-09 16:14:05 -0800575 struct tcf_common *p = a->priv;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900576
WANG Cong7eb88962014-01-09 16:14:05 -0800577 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 goto errout;
579
580 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400581 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 */
583 if (compat_mode) {
584 if (a->type == TCA_OLD_COMPAT)
585 err = gnet_stats_start_copy_compat(skb, 0,
WANG Cong7eb88962014-01-09 16:14:05 -0800586 TCA_STATS, TCA_XSTATS, &p->tcfc_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 else
588 return 0;
589 } else
590 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Cong7eb88962014-01-09 16:14:05 -0800591 &p->tcfc_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 if (err < 0)
594 goto errout;
595
WANG Cong7eb88962014-01-09 16:14:05 -0800596 if (gnet_stats_copy_basic(&d, &p->tcfc_bstats) < 0 ||
597 gnet_stats_copy_rate_est(&d, &p->tcfc_bstats,
598 &p->tcfc_rate_est) < 0 ||
599 gnet_stats_copy_queue(&d, &p->tcfc_qstats) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 goto errout;
601
602 if (gnet_stats_finish_copy(&d) < 0)
603 goto errout;
604
605 return 0;
606
607errout:
608 return -1;
609}
610
611static int
WANG Cong33be6272013-12-15 20:15:05 -0800612tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900613 u16 flags, int event, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 struct tcamsg *t;
616 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700617 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800618 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Eric W. Biederman15e47302012-09-07 20:12:54 +0000620 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700621 if (!nlh)
622 goto out_nlmsg_trim;
623 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700625 t->tca__pad1 = 0;
626 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900627
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800628 nest = nla_nest_start(skb, TCA_ACT_TAB);
629 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700630 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
WANG Cong33be6272013-12-15 20:15:05 -0800632 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700633 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800635 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900636
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700637 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return skb->len;
639
David S. Miller8b00a532012-06-26 21:39:32 -0700640out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700641 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return -1;
643}
644
645static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000646act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800647 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
652 if (!skb)
653 return -ENOBUFS;
WANG Cong33be6272013-12-15 20:15:05 -0800654 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 kfree_skb(skb);
656 return -EINVAL;
657 }
Thomas Graf2942e902006-08-15 00:30:25 -0700658
Eric W. Biederman15e47302012-09-07 20:12:54 +0000659 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
662static struct tc_action *
Eric W. Biederman15e47302012-09-07 20:12:54 +0000663tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000665 struct nlattr *tb[TCA_ACT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 struct tc_action *a;
667 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800668 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Patrick McHardycee63722008-01-23 20:33:32 -0800670 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
671 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800672 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Patrick McHardycee63722008-01-23 20:33:32 -0800674 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800675 if (tb[TCA_ACT_INDEX] == NULL ||
676 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800677 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800678 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800680 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700681 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (a == NULL)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800683 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
WANG Cong33be6272013-12-15 20:15:05 -0800685 INIT_LIST_HEAD(&a->list);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800686 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800687 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500688 if (a->ops == NULL) /* could happen in batch of actions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 goto err_free;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800690 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (a->ops->lookup(a, index) == 0)
692 goto err_mod;
693
694 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697err_mod:
698 module_put(a->ops->owner);
699err_free:
700 kfree(a);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800701err_out:
702 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
WANG Cong33be6272013-12-15 20:15:05 -0800705static void cleanup_a(struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
WANG Cong33be6272013-12-15 20:15:05 -0800707 struct tc_action *a, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
WANG Cong33be6272013-12-15 20:15:05 -0800709 list_for_each_entry_safe(a, tmp, actions, list) {
710 list_del(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 kfree(a);
712 }
713}
714
715static struct tc_action *create_a(int i)
716{
717 struct tc_action *act;
718
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700719 act = kzalloc(sizeof(*act), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (act == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000721 pr_debug("create_a: failed to alloc!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return NULL;
723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800725 INIT_LIST_HEAD(&act->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return act;
727}
728
Tom Goff7316ae82010-03-19 15:40:13 +0000729static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000730 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 struct sk_buff *skb;
733 unsigned char *b;
734 struct nlmsghdr *nlh;
735 struct tcamsg *t;
736 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800737 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000738 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800739 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 struct tc_action *a = create_a(0);
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700741 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 if (a == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000744 pr_debug("tca_action_flush: couldnt create tc_action\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return err;
746 }
747
748 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
749 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000750 pr_debug("tca_action_flush: failed skb alloc\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 kfree(a);
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700752 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700755 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Patrick McHardycee63722008-01-23 20:33:32 -0800757 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
758 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 goto err_out;
760
Patrick McHardycee63722008-01-23 20:33:32 -0800761 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800762 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 a->ops = tc_lookup_action(kind);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500764 if (a->ops == NULL) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 goto err_out;
766
Eric W. Biederman15e47302012-09-07 20:12:54 +0000767 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700768 if (!nlh)
769 goto out_module_put;
770 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700772 t->tca__pad1 = 0;
773 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800775 nest = nla_nest_start(skb, TCA_ACT_TAB);
776 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700777 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
780 if (err < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700781 goto out_module_put;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700782 if (err == 0)
783 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800785 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700787 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 nlh->nlmsg_flags |= NLM_F_ROOT;
789 module_put(a->ops->owner);
790 kfree(a);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000791 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000792 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (err > 0)
794 return 0;
795
796 return err;
797
David S. Miller8b00a532012-06-26 21:39:32 -0700798out_module_put:
Thomas Grafebbaeab2006-07-09 11:36:23 -0700799 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700801noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 kfree_skb(skb);
803 kfree(a);
804 return err;
805}
806
807static int
WANG Conga56e1952014-01-09 16:14:00 -0800808tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
809 u32 portid)
810{
811 int ret;
812 struct sk_buff *skb;
813
814 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
815 if (!skb)
816 return -ENOBUFS;
817
818 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
819 0, 1) <= 0) {
820 kfree_skb(skb);
821 return -EINVAL;
822 }
823
824 /* now do the delete */
825 tcf_action_destroy(actions, 0);
826
827 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
828 n->nlmsg_flags & NLM_F_ECHO);
829 if (ret > 0)
830 return 0;
831 return ret;
832}
833
834static int
Tom Goff7316ae82010-03-19 15:40:13 +0000835tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000836 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Patrick McHardycee63722008-01-23 20:33:32 -0800838 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000839 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800840 struct tc_action *act;
841 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Patrick McHardycee63722008-01-23 20:33:32 -0800843 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
844 if (ret < 0)
845 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000847 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700848 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000849 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700850 else
851 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800854 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000855 act = tcf_action_get_1(tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800856 if (IS_ERR(act)) {
857 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800859 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800860 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800861 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863
864 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800865 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -0800867 ret = tcf_del_notify(net, n, &actions, portid);
868 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return ret;
871 }
872err:
WANG Cong33be6272013-12-15 20:15:05 -0800873 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return ret;
875}
876
WANG Conga56e1952014-01-09 16:14:00 -0800877static int
878tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
879 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 int err = 0;
883
884 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
885 if (!skb)
886 return -ENOBUFS;
887
WANG Conga56e1952014-01-09 16:14:00 -0800888 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
889 RTM_NEWACTION, 0, 0) <= 0) {
890 kfree_skb(skb);
891 return -EINVAL;
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
WANG Conga56e1952014-01-09 16:14:00 -0800894 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
895 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (err > 0)
897 err = 0;
898 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901static int
Tom Goff7316ae82010-03-19 15:40:13 +0000902tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000903 u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
905 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -0800906 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
WANG Cong33be6272013-12-15 20:15:05 -0800908 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
909 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 goto done;
911
912 /* dump then free all the actions after update; inserted policy
913 * stays intact
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000914 */
WANG Conga56e1952014-01-09 16:14:00 -0800915 ret = tcf_add_notify(net, n, &actions, portid);
WANG Cong33be6272013-12-15 20:15:05 -0800916 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917done:
918 return ret;
919}
920
Thomas Graf661d2962013-03-21 07:45:29 +0000921static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900923 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800924 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +0000925 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 int ret = 0, ovr = 0;
927
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000928 if ((n->nlmsg_type != RTM_GETACTION) && !capable(CAP_NET_ADMIN))
929 return -EPERM;
930
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800931 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
932 if (ret < 0)
933 return ret;
934
935 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000936 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return -EINVAL;
938 }
939
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000940 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 switch (n->nlmsg_type) {
942 case RTM_NEWACTION:
943 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300944 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 * Note that CREATE | EXCL implies that
946 * but since we want avoid ambiguity (eg when flags
947 * is zero) then just set this
948 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000949 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 ovr = 1;
951replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000952 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (ret == -EAGAIN)
954 goto replay;
955 break;
956 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000957 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000958 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 break;
960 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000961 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000962 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 break;
964 default:
965 BUG();
966 }
967
968 return ret;
969}
970
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800971static struct nlattr *
Patrick McHardy3a6c2b42009-08-25 16:07:40 +0200972find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000974 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800975 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
976 struct nlattr *nla[TCAA_MAX + 1];
977 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Patrick McHardyc96c9472008-01-23 20:32:58 -0800979 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800981 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (tb1 == NULL)
983 return NULL;
984
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800985 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
986 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Patrick McHardy6d834e02008-01-23 20:32:42 -0800989 if (tb[1] == NULL)
990 return NULL;
991 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
992 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800994 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Thomas Graf26dab892006-07-05 20:45:06 -0700996 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
999static int
1000tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1001{
1002 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001003 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001004 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 struct tc_action_ops *a_o;
1006 struct tc_action a;
1007 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001008 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001009 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001012 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return 0;
1014 }
1015
Thomas Graf26dab892006-07-05 20:45:06 -07001016 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001017 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 memset(&a, 0, sizeof(struct tc_action));
1021 a.ops = a_o;
1022
Eric W. Biederman15e47302012-09-07 20:12:54 +00001023 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001024 cb->nlh->nlmsg_type, sizeof(*t), 0);
1025 if (!nlh)
1026 goto out_module_put;
1027 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001029 t->tca__pad1 = 0;
1030 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001032 nest = nla_nest_start(skb, TCA_ACT_TAB);
1033 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001034 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1037 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001038 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001041 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 ret = skb->len;
1043 } else
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001044 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001046 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001047 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 nlh->nlmsg_flags |= NLM_F_MULTI;
1049 module_put(a_o->owner);
1050 return skb->len;
1051
David S. Miller8b00a532012-06-26 21:39:32 -07001052out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001054 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return skb->len;
1056}
1057
1058static int __init tc_action_init(void)
1059{
Greg Rosec7ac8672011-06-10 01:27:09 +00001060 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1061 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1062 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1063 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return 0;
1066}
1067
1068subsys_initcall(tc_action_init);