blob: c88d382d3b098ffe13aefef5d753b601dcd17aee [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 Conga5b5c952014-02-11 17:07:32 -080059 if (a->ops->cleanup)
60 a->ops->cleanup(a, bind);
WANG Cong86062032014-02-11 17:07:31 -080061 tcf_hash_destroy(a);
David S. Millere9ce1cd2006-08-21 23:54:55 -070062 ret = 1;
63 }
64 }
65 return ret;
66}
67EXPORT_SYMBOL(tcf_hash_release);
68
69static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
WANG Congc779f7a2014-01-17 11:37:02 -080070 struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -070071{
WANG Congc779f7a2014-01-17 11:37:02 -080072 struct tcf_hashinfo *hinfo = a->ops->hinfo;
WANG Cong89819dc2013-12-15 20:15:09 -080073 struct hlist_head *head;
David S. Millere9ce1cd2006-08-21 23:54:55 -070074 struct tcf_common *p;
Eric Dumazetcc7ec452011-01-19 19:26:56 +000075 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080076 struct nlattr *nest;
David S. Millere9ce1cd2006-08-21 23:54:55 -070077
WANG Cong89819dc2013-12-15 20:15:09 -080078 spin_lock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -070079
80 s_i = cb->args[0];
81
82 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Cong89819dc2013-12-15 20:15:09 -080083 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
David S. Millere9ce1cd2006-08-21 23:54:55 -070084
WANG Cong89819dc2013-12-15 20:15:09 -080085 hlist_for_each_entry_rcu(p, head, tcfc_head) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070086 index++;
87 if (index < s_i)
88 continue;
89 a->priv = p;
90 a->order = n_i;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080091
92 nest = nla_nest_start(skb, a->order);
93 if (nest == NULL)
94 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -070095 err = tcf_action_dump_1(skb, a, 0, 0);
96 if (err < 0) {
97 index--;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080098 nlmsg_trim(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -070099 goto done;
100 }
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800101 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700102 n_i++;
103 if (n_i >= TCA_ACT_MAX_PRIO)
104 goto done;
105 }
106 }
107done:
WANG Cong89819dc2013-12-15 20:15:09 -0800108 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700109 if (n_i)
110 cb->args[0] += n_i;
111 return n_i;
112
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800113nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800114 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700115 goto done;
116}
117
WANG Congc779f7a2014-01-17 11:37:02 -0800118static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700119{
WANG Congc779f7a2014-01-17 11:37:02 -0800120 struct tcf_hashinfo *hinfo = a->ops->hinfo;
WANG Cong89819dc2013-12-15 20:15:09 -0800121 struct hlist_head *head;
122 struct hlist_node *n;
123 struct tcf_common *p;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800124 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000125 int i = 0, n_i = 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700126
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800127 nest = nla_nest_start(skb, a->order);
128 if (nest == NULL)
129 goto nla_put_failure;
David S. Miller1b34ec42012-03-29 05:11:39 -0400130 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
131 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700132 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Cong89819dc2013-12-15 20:15:09 -0800133 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
134 hlist_for_each_entry_safe(p, n, head, tcfc_head) {
WANG Cong86062032014-02-11 17:07:31 -0800135 a->priv = p;
136 if (ACT_P_DELETED == tcf_hash_release(a, 0)) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000137 module_put(a->ops->owner);
Jamal Hadi Salim805c1f42013-12-23 08:02:13 -0500138 n_i++;
139 }
David S. Millere9ce1cd2006-08-21 23:54:55 -0700140 }
141 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400142 if (nla_put_u32(skb, TCA_FCNT, n_i))
143 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800144 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700145
146 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800147nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800148 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700149 return -EINVAL;
150}
151
stephen hemminger9c75f402013-12-31 11:54:00 -0800152static int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
153 int type, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700154{
David S. Millere9ce1cd2006-08-21 23:54:55 -0700155 if (type == RTM_DELACTION) {
WANG Congc779f7a2014-01-17 11:37:02 -0800156 return tcf_del_walker(skb, a);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700157 } else if (type == RTM_GETACTION) {
WANG Congc779f7a2014-01-17 11:37:02 -0800158 return tcf_dump_walker(skb, cb, a);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700159 } else {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000160 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700161 return -EINVAL;
162 }
163}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700164
WANG Cong6e6a50c2014-01-17 11:37:03 -0800165static struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700166{
WANG Cong89819dc2013-12-15 20:15:09 -0800167 struct tcf_common *p = NULL;
168 struct hlist_head *head;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700169
WANG Cong89819dc2013-12-15 20:15:09 -0800170 spin_lock_bh(&hinfo->lock);
171 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
172 hlist_for_each_entry_rcu(p, head, tcfc_head)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700173 if (p->tcfc_index == index)
174 break;
WANG Cong89819dc2013-12-15 20:15:09 -0800175 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700176
177 return p;
178}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700179
WANG Congddafd342014-01-09 16:13:59 -0800180u32 tcf_hash_new_index(struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700181{
WANG Congddafd342014-01-09 16:13:59 -0800182 u32 val = hinfo->index;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700183
184 do {
185 if (++val == 0)
186 val = 1;
187 } while (tcf_hash_lookup(val, hinfo));
188
WANG Congddafd342014-01-09 16:13:59 -0800189 hinfo->index = val;
Yang Yingliang17569fa2013-12-10 20:55:29 +0800190 return val;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700191}
192EXPORT_SYMBOL(tcf_hash_new_index);
193
WANG Cong6e6a50c2014-01-17 11:37:03 -0800194int tcf_hash_search(struct tc_action *a, u32 index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700195{
196 struct tcf_hashinfo *hinfo = a->ops->hinfo;
197 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
198
199 if (p) {
200 a->priv = p;
201 return 1;
202 }
203 return 0;
204}
WANG Cong6e6a50c2014-01-17 11:37:03 -0800205EXPORT_SYMBOL(tcf_hash_search);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700206
WANG Cong86062032014-02-11 17:07:31 -0800207int tcf_hash_check(u32 index, struct tc_action *a, int bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700208{
WANG Congc779f7a2014-01-17 11:37:02 -0800209 struct tcf_hashinfo *hinfo = a->ops->hinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700210 struct tcf_common *p = NULL;
211 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700212 if (bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700213 p->tcfc_bindcnt++;
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700214 p->tcfc_refcnt++;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700215 a->priv = p;
WANG Cong86062032014-02-11 17:07:31 -0800216 return 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700217 }
WANG Cong86062032014-02-11 17:07:31 -0800218 return 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700219}
220EXPORT_SYMBOL(tcf_hash_check);
221
WANG Cong86062032014-02-11 17:07:31 -0800222void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
223{
224 struct tcf_common *pc = a->priv;
225 if (est)
226 gen_kill_estimator(&pc->tcfc_bstats,
227 &pc->tcfc_rate_est);
228 kfree_rcu(pc, tcfc_rcu);
229}
230EXPORT_SYMBOL(tcf_hash_cleanup);
231
232int tcf_hash_create(u32 index, struct nlattr *est, struct tc_action *a,
233 int size, int bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700234{
WANG Congc779f7a2014-01-17 11:37:02 -0800235 struct tcf_hashinfo *hinfo = a->ops->hinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700236 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
237
238 if (unlikely(!p))
WANG Cong86062032014-02-11 17:07:31 -0800239 return -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700240 p->tcfc_refcnt = 1;
241 if (bind)
242 p->tcfc_bindcnt = 1;
243
244 spin_lock_init(&p->tcfc_lock);
WANG Cong89819dc2013-12-15 20:15:09 -0800245 INIT_HLIST_NODE(&p->tcfc_head);
WANG Congddafd342014-01-09 16:13:59 -0800246 p->tcfc_index = index ? index : tcf_hash_new_index(hinfo);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700247 p->tcfc_tm.install = jiffies;
248 p->tcfc_tm.lastuse = jiffies;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800249 if (est) {
250 int err = gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
251 &p->tcfc_lock, est);
252 if (err) {
253 kfree(p);
WANG Cong86062032014-02-11 17:07:31 -0800254 return err;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800255 }
256 }
257
David S. Millere9ce1cd2006-08-21 23:54:55 -0700258 a->priv = (void *) p;
WANG Cong86062032014-02-11 17:07:31 -0800259 return 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700260}
261EXPORT_SYMBOL(tcf_hash_create);
262
WANG Cong86062032014-02-11 17:07:31 -0800263void tcf_hash_insert(struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700264{
WANG Cong86062032014-02-11 17:07:31 -0800265 struct tcf_common *p = a->priv;
266 struct tcf_hashinfo *hinfo = a->ops->hinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700267 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
268
WANG Cong89819dc2013-12-15 20:15:09 -0800269 spin_lock_bh(&hinfo->lock);
270 hlist_add_head(&p->tcfc_head, &hinfo->htab[h]);
271 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700272}
273EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
WANG Cong1f747c22013-12-15 20:15:10 -0800275static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276static DEFINE_RWLOCK(act_mod_lock);
277
WANG Cong4f1e9d82014-02-11 17:07:33 -0800278int tcf_register_action(struct tc_action_ops *act, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
WANG Cong1f747c22013-12-15 20:15:10 -0800280 struct tc_action_ops *a;
WANG Cong4f1e9d82014-02-11 17:07:33 -0800281 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
WANG Conga5b5c952014-02-11 17:07:32 -0800283 /* Must supply act, dump and init */
284 if (!act->act || !act->dump || !act->init)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500285 return -EINVAL;
286
Jamal Hadi Salim382ca8a2013-12-04 09:26:55 -0500287 /* Supply defaults */
Jamal Hadi Salim63ef6172013-12-04 09:26:53 -0500288 if (!act->lookup)
289 act->lookup = tcf_hash_search;
Jamal Hadi Salim382ca8a2013-12-04 09:26:55 -0500290 if (!act->walk)
291 act->walk = tcf_generic_walker;
Jamal Hadi Salim63ef6172013-12-04 09:26:53 -0500292
WANG Cong4f1e9d82014-02-11 17:07:33 -0800293 act->hinfo = kmalloc(sizeof(struct tcf_hashinfo), GFP_KERNEL);
294 if (!act->hinfo)
295 return -ENOMEM;
296 err = tcf_hashinfo_init(act->hinfo, mask);
297 if (err) {
298 kfree(act->hinfo);
299 return err;
300 }
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800303 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
305 write_unlock(&act_mod_lock);
WANG Cong4f1e9d82014-02-11 17:07:33 -0800306 tcf_hashinfo_destroy(act->hinfo);
307 kfree(act->hinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return -EEXIST;
309 }
310 }
WANG Cong1f747c22013-12-15 20:15:10 -0800311 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 write_unlock(&act_mod_lock);
313 return 0;
314}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800315EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317int tcf_unregister_action(struct tc_action_ops *act)
318{
WANG Cong1f747c22013-12-15 20:15:10 -0800319 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int err = -ENOENT;
321
322 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800323 list_for_each_entry(a, &act_base, head) {
324 if (a == act) {
325 list_del(&act->head);
WANG Cong4f1e9d82014-02-11 17:07:33 -0800326 tcf_hashinfo_destroy(act->hinfo);
327 kfree(act->hinfo);
Eric Dumazeta7928662013-12-20 12:32:32 -0800328 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332 write_unlock(&act_mod_lock);
333 return err;
334}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800335EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337/* lookup by name */
338static struct tc_action_ops *tc_lookup_action_n(char *kind)
339{
Eric Dumazeta7928662013-12-20 12:32:32 -0800340 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 if (kind) {
343 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800344 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800346 if (try_module_get(a->owner))
347 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 break;
349 }
350 }
351 read_unlock(&act_mod_lock);
352 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800353 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800356/* lookup by nlattr */
357static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Eric Dumazeta7928662013-12-20 12:32:32 -0800359 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 if (kind) {
362 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800363 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800364 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800365 if (try_module_get(a->owner))
366 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 break;
368 }
369 }
370 read_unlock(&act_mod_lock);
371 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800372 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
WANG Cong33be6272013-12-15 20:15:05 -0800375int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900376 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000378 const struct tc_action *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 int ret = -1;
380
381 if (skb->tc_verd & TC_NCLS) {
382 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 ret = TC_ACT_OK;
384 goto exec_done;
385 }
WANG Cong33be6272013-12-15 20:15:05 -0800386 list_for_each_entry(a, actions, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500388 ret = a->ops->act(skb, a, res);
389 if (TC_MUNGED & skb->tc_verd) {
390 /* copied already, allow trampling */
391 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
392 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500394 if (ret == TC_ACT_REPEAT)
395 goto repeat; /* we need a ttl - JHS */
396 if (ret != TC_ACT_PIPE)
397 goto exec_done;
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) {
WANG Conga5b5c952014-02-11 17:07:32 -0800409 if (tcf_hash_release(a, bind) == ACT_P_DELETED)
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500410 module_put(a->ops->owner);
411 list_del(&a->list);
412 kfree(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414}
415
416int
417tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
418{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return a->ops->dump(skb, a, bind, ref);
420}
421
422int
423tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
424{
425 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700426 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800427 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
David S. Miller1b34ec42012-03-29 05:11:39 -0400429 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
430 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800432 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800433 nest = nla_nest_start(skb, TCA_OPTIONS);
434 if (nest == NULL)
435 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000436 err = tcf_action_dump_old(skb, a, bind, ref);
437 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800438 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return err;
440 }
441
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800442nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700443 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return -1;
445}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800446EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448int
WANG Cong33be6272013-12-15 20:15:05 -0800449tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 struct tc_action *a;
452 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800453 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
WANG Cong33be6272013-12-15 20:15:05 -0800455 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800456 nest = nla_nest_start(skb, a->order);
457 if (nest == NULL)
458 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 err = tcf_action_dump_1(skb, a, bind, ref);
460 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700461 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800462 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464
465 return 0;
466
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800467nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700468 err = -EINVAL;
469errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800470 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700471 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000474struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
475 struct nlattr *est, char *name, int ovr,
476 int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 struct tc_action *a;
479 struct tc_action_ops *a_o;
480 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000481 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800482 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800483 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800486 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
487 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800489 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800490 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (kind == NULL)
492 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800493 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 goto err_out;
495 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800496 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
498 goto err_out;
499 }
500
501 a_o = tc_lookup_action_n(act_name);
502 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700503#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800505 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 rtnl_lock();
507
508 a_o = tc_lookup_action_n(act_name);
509
510 /* We dropped the RTNL semaphore in order to
511 * perform the module load. So, even if we
512 * succeeded in loading the module we have to
513 * tell the caller to replay the request. We
514 * indicate this using -EAGAIN.
515 */
516 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800517 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 goto err_mod;
519 }
520#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800521 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 goto err_out;
523 }
524
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800525 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700526 a = kzalloc(sizeof(*a), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (a == NULL)
528 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
WANG Congc779f7a2014-01-17 11:37:02 -0800530 a->ops = a_o;
WANG Cong33be6272013-12-15 20:15:05 -0800531 INIT_LIST_HEAD(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /* backward compatibility for policer */
533 if (name == NULL)
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000534 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 else
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000536 err = a_o->init(net, nla, est, a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800537 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 goto err_free;
539
540 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000541 * if it exists and is only bound to in a_o->init() then
542 * ACT_P_CREATED is not returned (a zero is).
543 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800544 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return a;
548
549err_free:
550 kfree(a);
551err_mod:
552 module_put(a_o->owner);
553err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800554 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
WANG Cong33be6272013-12-15 20:15:05 -0800557int tcf_action_init(struct net *net, struct nlattr *nla,
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000558 struct nlattr *est, char *name, int ovr,
WANG Cong33be6272013-12-15 20:15:05 -0800559 int bind, struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000561 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800562 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800563 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 int i;
565
Patrick McHardycee63722008-01-23 20:33:32 -0800566 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
567 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800568 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800570 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000571 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800572 if (IS_ERR(act)) {
573 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800575 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800576 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800577 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
WANG Cong33be6272013-12-15 20:15:05 -0800579 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581err:
WANG Cong33be6272013-12-15 20:15:05 -0800582 tcf_action_destroy(actions, bind);
583 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
586int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
587 int compat_mode)
588{
589 int err = 0;
590 struct gnet_dump d;
WANG Cong7eb88962014-01-09 16:14:05 -0800591 struct tcf_common *p = a->priv;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900592
WANG Cong7eb88962014-01-09 16:14:05 -0800593 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 goto errout;
595
596 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400597 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 */
599 if (compat_mode) {
600 if (a->type == TCA_OLD_COMPAT)
601 err = gnet_stats_start_copy_compat(skb, 0,
WANG Cong7eb88962014-01-09 16:14:05 -0800602 TCA_STATS, TCA_XSTATS, &p->tcfc_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 else
604 return 0;
605 } else
606 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Cong7eb88962014-01-09 16:14:05 -0800607 &p->tcfc_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 if (err < 0)
610 goto errout;
611
WANG Cong7eb88962014-01-09 16:14:05 -0800612 if (gnet_stats_copy_basic(&d, &p->tcfc_bstats) < 0 ||
613 gnet_stats_copy_rate_est(&d, &p->tcfc_bstats,
614 &p->tcfc_rate_est) < 0 ||
615 gnet_stats_copy_queue(&d, &p->tcfc_qstats) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 goto errout;
617
618 if (gnet_stats_finish_copy(&d) < 0)
619 goto errout;
620
621 return 0;
622
623errout:
624 return -1;
625}
626
627static int
WANG Cong33be6272013-12-15 20:15:05 -0800628tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900629 u16 flags, int event, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 struct tcamsg *t;
632 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700633 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800634 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Eric W. Biederman15e47302012-09-07 20:12:54 +0000636 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700637 if (!nlh)
638 goto out_nlmsg_trim;
639 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700641 t->tca__pad1 = 0;
642 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900643
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800644 nest = nla_nest_start(skb, TCA_ACT_TAB);
645 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700646 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
WANG Cong33be6272013-12-15 20:15:05 -0800648 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700649 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800651 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900652
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700653 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return skb->len;
655
David S. Miller8b00a532012-06-26 21:39:32 -0700656out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700657 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return -1;
659}
660
661static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000662act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800663 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
668 if (!skb)
669 return -ENOBUFS;
WANG Cong33be6272013-12-15 20:15:05 -0800670 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 kfree_skb(skb);
672 return -EINVAL;
673 }
Thomas Graf2942e902006-08-15 00:30:25 -0700674
Eric W. Biederman15e47302012-09-07 20:12:54 +0000675 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
678static struct tc_action *
Eric W. Biederman15e47302012-09-07 20:12:54 +0000679tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000681 struct nlattr *tb[TCA_ACT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 struct tc_action *a;
683 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800684 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Patrick McHardycee63722008-01-23 20:33:32 -0800686 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
687 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800688 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Patrick McHardycee63722008-01-23 20:33:32 -0800690 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800691 if (tb[TCA_ACT_INDEX] == NULL ||
692 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800693 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800694 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800696 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700697 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (a == NULL)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800699 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
WANG Cong33be6272013-12-15 20:15:05 -0800701 INIT_LIST_HEAD(&a->list);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800702 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800703 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500704 if (a->ops == NULL) /* could happen in batch of actions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 goto err_free;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800706 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (a->ops->lookup(a, index) == 0)
708 goto err_mod;
709
710 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713err_mod:
714 module_put(a->ops->owner);
715err_free:
716 kfree(a);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800717err_out:
718 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
WANG Cong33be6272013-12-15 20:15:05 -0800721static void cleanup_a(struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
WANG Cong33be6272013-12-15 20:15:05 -0800723 struct tc_action *a, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
WANG Cong33be6272013-12-15 20:15:05 -0800725 list_for_each_entry_safe(a, tmp, actions, list) {
726 list_del(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 kfree(a);
728 }
729}
730
731static struct tc_action *create_a(int i)
732{
733 struct tc_action *act;
734
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700735 act = kzalloc(sizeof(*act), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (act == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000737 pr_debug("create_a: failed to alloc!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return NULL;
739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800741 INIT_LIST_HEAD(&act->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return act;
743}
744
Tom Goff7316ae82010-03-19 15:40:13 +0000745static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000746 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 struct sk_buff *skb;
749 unsigned char *b;
750 struct nlmsghdr *nlh;
751 struct tcamsg *t;
752 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800753 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000754 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800755 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 struct tc_action *a = create_a(0);
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700757 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 if (a == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000760 pr_debug("tca_action_flush: couldnt create tc_action\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return err;
762 }
763
764 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
765 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000766 pr_debug("tca_action_flush: failed skb alloc\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 kfree(a);
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700768 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700771 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Patrick McHardycee63722008-01-23 20:33:32 -0800773 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
774 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 goto err_out;
776
Patrick McHardycee63722008-01-23 20:33:32 -0800777 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800778 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 a->ops = tc_lookup_action(kind);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500780 if (a->ops == NULL) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 goto err_out;
782
Eric W. Biederman15e47302012-09-07 20:12:54 +0000783 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700784 if (!nlh)
785 goto out_module_put;
786 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700788 t->tca__pad1 = 0;
789 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800791 nest = nla_nest_start(skb, TCA_ACT_TAB);
792 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700793 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
796 if (err < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700797 goto out_module_put;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700798 if (err == 0)
799 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800801 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700803 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 nlh->nlmsg_flags |= NLM_F_ROOT;
805 module_put(a->ops->owner);
806 kfree(a);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000807 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000808 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (err > 0)
810 return 0;
811
812 return err;
813
David S. Miller8b00a532012-06-26 21:39:32 -0700814out_module_put:
Thomas Grafebbaeab2006-07-09 11:36:23 -0700815 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700817noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 kfree_skb(skb);
819 kfree(a);
820 return err;
821}
822
823static int
WANG Conga56e1952014-01-09 16:14:00 -0800824tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
825 u32 portid)
826{
827 int ret;
828 struct sk_buff *skb;
829
830 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
831 if (!skb)
832 return -ENOBUFS;
833
834 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
835 0, 1) <= 0) {
836 kfree_skb(skb);
837 return -EINVAL;
838 }
839
840 /* now do the delete */
841 tcf_action_destroy(actions, 0);
842
843 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
844 n->nlmsg_flags & NLM_F_ECHO);
845 if (ret > 0)
846 return 0;
847 return ret;
848}
849
850static int
Tom Goff7316ae82010-03-19 15:40:13 +0000851tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000852 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Patrick McHardycee63722008-01-23 20:33:32 -0800854 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000855 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800856 struct tc_action *act;
857 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Patrick McHardycee63722008-01-23 20:33:32 -0800859 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
860 if (ret < 0)
861 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000863 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700864 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000865 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700866 else
867 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800870 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000871 act = tcf_action_get_1(tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800872 if (IS_ERR(act)) {
873 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800875 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800876 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800877 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879
880 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800881 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -0800883 ret = tcf_del_notify(net, n, &actions, portid);
884 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return ret;
887 }
888err:
WANG Cong33be6272013-12-15 20:15:05 -0800889 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return ret;
891}
892
WANG Conga56e1952014-01-09 16:14:00 -0800893static int
894tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
895 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 int err = 0;
899
900 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
901 if (!skb)
902 return -ENOBUFS;
903
WANG Conga56e1952014-01-09 16:14:00 -0800904 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
905 RTM_NEWACTION, 0, 0) <= 0) {
906 kfree_skb(skb);
907 return -EINVAL;
908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
WANG Conga56e1952014-01-09 16:14:00 -0800910 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
911 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (err > 0)
913 err = 0;
914 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917static int
Tom Goff7316ae82010-03-19 15:40:13 +0000918tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000919 u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -0800922 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
WANG Cong33be6272013-12-15 20:15:05 -0800924 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
925 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 goto done;
927
928 /* dump then free all the actions after update; inserted policy
929 * stays intact
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000930 */
WANG Conga56e1952014-01-09 16:14:00 -0800931 ret = tcf_add_notify(net, n, &actions, portid);
WANG Cong33be6272013-12-15 20:15:05 -0800932 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933done:
934 return ret;
935}
936
Thomas Graf661d2962013-03-21 07:45:29 +0000937static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900939 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800940 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +0000941 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 int ret = 0, ovr = 0;
943
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000944 if ((n->nlmsg_type != RTM_GETACTION) && !capable(CAP_NET_ADMIN))
945 return -EPERM;
946
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800947 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
948 if (ret < 0)
949 return ret;
950
951 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000952 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return -EINVAL;
954 }
955
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000956 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 switch (n->nlmsg_type) {
958 case RTM_NEWACTION:
959 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300960 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 * Note that CREATE | EXCL implies that
962 * but since we want avoid ambiguity (eg when flags
963 * is zero) then just set this
964 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000965 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 ovr = 1;
967replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000968 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (ret == -EAGAIN)
970 goto replay;
971 break;
972 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000973 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000974 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 break;
976 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000977 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000978 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 break;
980 default:
981 BUG();
982 }
983
984 return ret;
985}
986
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800987static struct nlattr *
Patrick McHardy3a6c2b42009-08-25 16:07:40 +0200988find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000990 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800991 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
992 struct nlattr *nla[TCAA_MAX + 1];
993 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Patrick McHardyc96c9472008-01-23 20:32:58 -0800995 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800997 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (tb1 == NULL)
999 return NULL;
1000
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001001 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1002 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Patrick McHardy6d834e02008-01-23 20:32:42 -08001005 if (tb[1] == NULL)
1006 return NULL;
1007 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1008 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001010 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Thomas Graf26dab892006-07-05 20:45:06 -07001012 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
1015static int
1016tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1017{
1018 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001019 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001020 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 struct tc_action_ops *a_o;
1022 struct tc_action a;
1023 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001024 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001025 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001028 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return 0;
1030 }
1031
Thomas Graf26dab892006-07-05 20:45:06 -07001032 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001033 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 memset(&a, 0, sizeof(struct tc_action));
1037 a.ops = a_o;
1038
Eric W. Biederman15e47302012-09-07 20:12:54 +00001039 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001040 cb->nlh->nlmsg_type, sizeof(*t), 0);
1041 if (!nlh)
1042 goto out_module_put;
1043 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001045 t->tca__pad1 = 0;
1046 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001048 nest = nla_nest_start(skb, TCA_ACT_TAB);
1049 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001050 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1053 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001054 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001057 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 ret = skb->len;
1059 } else
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001060 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001062 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001063 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 nlh->nlmsg_flags |= NLM_F_MULTI;
1065 module_put(a_o->owner);
1066 return skb->len;
1067
David S. Miller8b00a532012-06-26 21:39:32 -07001068out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001070 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return skb->len;
1072}
1073
1074static int __init tc_action_init(void)
1075{
Greg Rosec7ac8672011-06-10 01:27:09 +00001076 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1077 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1078 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1079 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return 0;
1082}
1083
1084subsys_initcall(tc_action_init);