blob: a5bf9351ce5c2dc13d7da68d4fbd0c8153585605 [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
278int tcf_register_action(struct tc_action_ops *act)
279{
WANG Cong1f747c22013-12-15 20:15:10 -0800280 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
WANG Conga5b5c952014-02-11 17:07:32 -0800282 /* Must supply act, dump and init */
283 if (!act->act || !act->dump || !act->init)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500284 return -EINVAL;
285
Jamal Hadi Salim382ca8a2013-12-04 09:26:55 -0500286 /* Supply defaults */
Jamal Hadi Salim63ef6172013-12-04 09:26:53 -0500287 if (!act->lookup)
288 act->lookup = tcf_hash_search;
Jamal Hadi Salim382ca8a2013-12-04 09:26:55 -0500289 if (!act->walk)
290 act->walk = tcf_generic_walker;
Jamal Hadi Salim63ef6172013-12-04 09:26:53 -0500291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800293 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
295 write_unlock(&act_mod_lock);
296 return -EEXIST;
297 }
298 }
WANG Cong1f747c22013-12-15 20:15:10 -0800299 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 write_unlock(&act_mod_lock);
301 return 0;
302}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800303EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305int tcf_unregister_action(struct tc_action_ops *act)
306{
WANG Cong1f747c22013-12-15 20:15:10 -0800307 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 int err = -ENOENT;
309
310 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800311 list_for_each_entry(a, &act_base, head) {
312 if (a == act) {
313 list_del(&act->head);
314 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318 write_unlock(&act_mod_lock);
319 return err;
320}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800321EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323/* lookup by name */
324static struct tc_action_ops *tc_lookup_action_n(char *kind)
325{
Eric Dumazeta7928662013-12-20 12:32:32 -0800326 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 if (kind) {
329 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800330 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800332 if (try_module_get(a->owner))
333 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 break;
335 }
336 }
337 read_unlock(&act_mod_lock);
338 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800339 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800342/* lookup by nlattr */
343static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Eric Dumazeta7928662013-12-20 12:32:32 -0800345 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 if (kind) {
348 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800349 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800350 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800351 if (try_module_get(a->owner))
352 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 break;
354 }
355 }
356 read_unlock(&act_mod_lock);
357 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800358 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
WANG Cong33be6272013-12-15 20:15:05 -0800361int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900362 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000364 const struct tc_action *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int ret = -1;
366
367 if (skb->tc_verd & TC_NCLS) {
368 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 ret = TC_ACT_OK;
370 goto exec_done;
371 }
WANG Cong33be6272013-12-15 20:15:05 -0800372 list_for_each_entry(a, actions, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500374 ret = a->ops->act(skb, a, res);
375 if (TC_MUNGED & skb->tc_verd) {
376 /* copied already, allow trampling */
377 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
378 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500380 if (ret == TC_ACT_REPEAT)
381 goto repeat; /* we need a ttl - JHS */
382 if (ret != TC_ACT_PIPE)
383 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return ret;
387}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800388EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
WANG Cong33be6272013-12-15 20:15:05 -0800390void tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
WANG Cong33be6272013-12-15 20:15:05 -0800392 struct tc_action *a, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
WANG Cong33be6272013-12-15 20:15:05 -0800394 list_for_each_entry_safe(a, tmp, actions, list) {
WANG Conga5b5c952014-02-11 17:07:32 -0800395 if (tcf_hash_release(a, bind) == ACT_P_DELETED)
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500396 module_put(a->ops->owner);
397 list_del(&a->list);
398 kfree(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400}
401
402int
403tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
404{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return a->ops->dump(skb, a, bind, ref);
406}
407
408int
409tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
410{
411 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700412 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800413 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
David S. Miller1b34ec42012-03-29 05:11:39 -0400415 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
416 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800418 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800419 nest = nla_nest_start(skb, TCA_OPTIONS);
420 if (nest == NULL)
421 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000422 err = tcf_action_dump_old(skb, a, bind, ref);
423 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800424 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return err;
426 }
427
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800428nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700429 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return -1;
431}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800432EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434int
WANG Cong33be6272013-12-15 20:15:05 -0800435tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 struct tc_action *a;
438 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800439 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
WANG Cong33be6272013-12-15 20:15:05 -0800441 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800442 nest = nla_nest_start(skb, a->order);
443 if (nest == NULL)
444 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 err = tcf_action_dump_1(skb, a, bind, ref);
446 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700447 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800448 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
450
451 return 0;
452
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800453nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700454 err = -EINVAL;
455errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800456 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700457 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000460struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
461 struct nlattr *est, char *name, int ovr,
462 int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 struct tc_action *a;
465 struct tc_action_ops *a_o;
466 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000467 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800468 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800469 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800472 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
473 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800475 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800476 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (kind == NULL)
478 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800479 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 goto err_out;
481 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800482 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
484 goto err_out;
485 }
486
487 a_o = tc_lookup_action_n(act_name);
488 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700489#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800491 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 rtnl_lock();
493
494 a_o = tc_lookup_action_n(act_name);
495
496 /* We dropped the RTNL semaphore in order to
497 * perform the module load. So, even if we
498 * succeeded in loading the module we have to
499 * tell the caller to replay the request. We
500 * indicate this using -EAGAIN.
501 */
502 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800503 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 goto err_mod;
505 }
506#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800507 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 goto err_out;
509 }
510
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800511 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700512 a = kzalloc(sizeof(*a), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (a == NULL)
514 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
WANG Congc779f7a2014-01-17 11:37:02 -0800516 a->ops = a_o;
WANG Cong33be6272013-12-15 20:15:05 -0800517 INIT_LIST_HEAD(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /* backward compatibility for policer */
519 if (name == NULL)
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000520 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 else
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000522 err = a_o->init(net, nla, est, a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800523 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 goto err_free;
525
526 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000527 * if it exists and is only bound to in a_o->init() then
528 * ACT_P_CREATED is not returned (a zero is).
529 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800530 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return a;
534
535err_free:
536 kfree(a);
537err_mod:
538 module_put(a_o->owner);
539err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800540 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
WANG Cong33be6272013-12-15 20:15:05 -0800543int tcf_action_init(struct net *net, struct nlattr *nla,
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000544 struct nlattr *est, char *name, int ovr,
WANG Cong33be6272013-12-15 20:15:05 -0800545 int bind, struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000547 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800548 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800549 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 int i;
551
Patrick McHardycee63722008-01-23 20:33:32 -0800552 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
553 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800554 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800556 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000557 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800558 if (IS_ERR(act)) {
559 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800561 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800562 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800563 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
WANG Cong33be6272013-12-15 20:15:05 -0800565 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567err:
WANG Cong33be6272013-12-15 20:15:05 -0800568 tcf_action_destroy(actions, bind);
569 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
572int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
573 int compat_mode)
574{
575 int err = 0;
576 struct gnet_dump d;
WANG Cong7eb88962014-01-09 16:14:05 -0800577 struct tcf_common *p = a->priv;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900578
WANG Cong7eb88962014-01-09 16:14:05 -0800579 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 goto errout;
581
582 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400583 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 */
585 if (compat_mode) {
586 if (a->type == TCA_OLD_COMPAT)
587 err = gnet_stats_start_copy_compat(skb, 0,
WANG Cong7eb88962014-01-09 16:14:05 -0800588 TCA_STATS, TCA_XSTATS, &p->tcfc_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 else
590 return 0;
591 } else
592 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Cong7eb88962014-01-09 16:14:05 -0800593 &p->tcfc_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 if (err < 0)
596 goto errout;
597
WANG Cong7eb88962014-01-09 16:14:05 -0800598 if (gnet_stats_copy_basic(&d, &p->tcfc_bstats) < 0 ||
599 gnet_stats_copy_rate_est(&d, &p->tcfc_bstats,
600 &p->tcfc_rate_est) < 0 ||
601 gnet_stats_copy_queue(&d, &p->tcfc_qstats) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 goto errout;
603
604 if (gnet_stats_finish_copy(&d) < 0)
605 goto errout;
606
607 return 0;
608
609errout:
610 return -1;
611}
612
613static int
WANG Cong33be6272013-12-15 20:15:05 -0800614tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900615 u16 flags, int event, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 struct tcamsg *t;
618 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700619 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800620 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Eric W. Biederman15e47302012-09-07 20:12:54 +0000622 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700623 if (!nlh)
624 goto out_nlmsg_trim;
625 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700627 t->tca__pad1 = 0;
628 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900629
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800630 nest = nla_nest_start(skb, TCA_ACT_TAB);
631 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700632 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
WANG Cong33be6272013-12-15 20:15:05 -0800634 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700635 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800637 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900638
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700639 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return skb->len;
641
David S. Miller8b00a532012-06-26 21:39:32 -0700642out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700643 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return -1;
645}
646
647static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000648act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800649 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
654 if (!skb)
655 return -ENOBUFS;
WANG Cong33be6272013-12-15 20:15:05 -0800656 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 kfree_skb(skb);
658 return -EINVAL;
659 }
Thomas Graf2942e902006-08-15 00:30:25 -0700660
Eric W. Biederman15e47302012-09-07 20:12:54 +0000661 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
664static struct tc_action *
Eric W. Biederman15e47302012-09-07 20:12:54 +0000665tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000667 struct nlattr *tb[TCA_ACT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 struct tc_action *a;
669 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800670 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Patrick McHardycee63722008-01-23 20:33:32 -0800672 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
673 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800674 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Patrick McHardycee63722008-01-23 20:33:32 -0800676 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800677 if (tb[TCA_ACT_INDEX] == NULL ||
678 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800679 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800680 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800682 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700683 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (a == NULL)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800685 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
WANG Cong33be6272013-12-15 20:15:05 -0800687 INIT_LIST_HEAD(&a->list);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800688 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800689 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500690 if (a->ops == NULL) /* could happen in batch of actions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 goto err_free;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800692 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (a->ops->lookup(a, index) == 0)
694 goto err_mod;
695
696 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699err_mod:
700 module_put(a->ops->owner);
701err_free:
702 kfree(a);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800703err_out:
704 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
WANG Cong33be6272013-12-15 20:15:05 -0800707static void cleanup_a(struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
WANG Cong33be6272013-12-15 20:15:05 -0800709 struct tc_action *a, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
WANG Cong33be6272013-12-15 20:15:05 -0800711 list_for_each_entry_safe(a, tmp, actions, list) {
712 list_del(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 kfree(a);
714 }
715}
716
717static struct tc_action *create_a(int i)
718{
719 struct tc_action *act;
720
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700721 act = kzalloc(sizeof(*act), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (act == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000723 pr_debug("create_a: failed to alloc!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return NULL;
725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800727 INIT_LIST_HEAD(&act->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return act;
729}
730
Tom Goff7316ae82010-03-19 15:40:13 +0000731static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000732 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 struct sk_buff *skb;
735 unsigned char *b;
736 struct nlmsghdr *nlh;
737 struct tcamsg *t;
738 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800739 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000740 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800741 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 struct tc_action *a = create_a(0);
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700743 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 if (a == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000746 pr_debug("tca_action_flush: couldnt create tc_action\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return err;
748 }
749
750 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
751 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000752 pr_debug("tca_action_flush: failed skb alloc\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 kfree(a);
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700754 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700757 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Patrick McHardycee63722008-01-23 20:33:32 -0800759 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
760 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 goto err_out;
762
Patrick McHardycee63722008-01-23 20:33:32 -0800763 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800764 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 a->ops = tc_lookup_action(kind);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500766 if (a->ops == NULL) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 goto err_out;
768
Eric W. Biederman15e47302012-09-07 20:12:54 +0000769 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700770 if (!nlh)
771 goto out_module_put;
772 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700774 t->tca__pad1 = 0;
775 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800777 nest = nla_nest_start(skb, TCA_ACT_TAB);
778 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700779 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
782 if (err < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700783 goto out_module_put;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700784 if (err == 0)
785 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800787 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700789 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 nlh->nlmsg_flags |= NLM_F_ROOT;
791 module_put(a->ops->owner);
792 kfree(a);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000793 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000794 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (err > 0)
796 return 0;
797
798 return err;
799
David S. Miller8b00a532012-06-26 21:39:32 -0700800out_module_put:
Thomas Grafebbaeab2006-07-09 11:36:23 -0700801 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700803noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 kfree_skb(skb);
805 kfree(a);
806 return err;
807}
808
809static int
WANG Conga56e1952014-01-09 16:14:00 -0800810tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
811 u32 portid)
812{
813 int ret;
814 struct sk_buff *skb;
815
816 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
817 if (!skb)
818 return -ENOBUFS;
819
820 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
821 0, 1) <= 0) {
822 kfree_skb(skb);
823 return -EINVAL;
824 }
825
826 /* now do the delete */
827 tcf_action_destroy(actions, 0);
828
829 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
830 n->nlmsg_flags & NLM_F_ECHO);
831 if (ret > 0)
832 return 0;
833 return ret;
834}
835
836static int
Tom Goff7316ae82010-03-19 15:40:13 +0000837tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000838 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Patrick McHardycee63722008-01-23 20:33:32 -0800840 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000841 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800842 struct tc_action *act;
843 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Patrick McHardycee63722008-01-23 20:33:32 -0800845 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
846 if (ret < 0)
847 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000849 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700850 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000851 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700852 else
853 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800856 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000857 act = tcf_action_get_1(tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800858 if (IS_ERR(act)) {
859 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800861 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800862 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800863 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
865
866 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800867 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -0800869 ret = tcf_del_notify(net, n, &actions, portid);
870 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return ret;
873 }
874err:
WANG Cong33be6272013-12-15 20:15:05 -0800875 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return ret;
877}
878
WANG Conga56e1952014-01-09 16:14:00 -0800879static int
880tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
881 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 int err = 0;
885
886 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
887 if (!skb)
888 return -ENOBUFS;
889
WANG Conga56e1952014-01-09 16:14:00 -0800890 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
891 RTM_NEWACTION, 0, 0) <= 0) {
892 kfree_skb(skb);
893 return -EINVAL;
894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
WANG Conga56e1952014-01-09 16:14:00 -0800896 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
897 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (err > 0)
899 err = 0;
900 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903static int
Tom Goff7316ae82010-03-19 15:40:13 +0000904tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000905 u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -0800908 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
WANG Cong33be6272013-12-15 20:15:05 -0800910 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
911 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 goto done;
913
914 /* dump then free all the actions after update; inserted policy
915 * stays intact
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000916 */
WANG Conga56e1952014-01-09 16:14:00 -0800917 ret = tcf_add_notify(net, n, &actions, portid);
WANG Cong33be6272013-12-15 20:15:05 -0800918 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919done:
920 return ret;
921}
922
Thomas Graf661d2962013-03-21 07:45:29 +0000923static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900925 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800926 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +0000927 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 int ret = 0, ovr = 0;
929
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000930 if ((n->nlmsg_type != RTM_GETACTION) && !capable(CAP_NET_ADMIN))
931 return -EPERM;
932
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800933 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
934 if (ret < 0)
935 return ret;
936
937 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000938 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return -EINVAL;
940 }
941
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000942 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 switch (n->nlmsg_type) {
944 case RTM_NEWACTION:
945 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300946 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 * Note that CREATE | EXCL implies that
948 * but since we want avoid ambiguity (eg when flags
949 * is zero) then just set this
950 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000951 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 ovr = 1;
953replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000954 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (ret == -EAGAIN)
956 goto replay;
957 break;
958 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000959 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000960 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 break;
962 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000963 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000964 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 break;
966 default:
967 BUG();
968 }
969
970 return ret;
971}
972
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800973static struct nlattr *
Patrick McHardy3a6c2b42009-08-25 16:07:40 +0200974find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000976 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800977 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
978 struct nlattr *nla[TCAA_MAX + 1];
979 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Patrick McHardyc96c9472008-01-23 20:32:58 -0800981 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800983 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (tb1 == NULL)
985 return NULL;
986
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800987 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
988 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Patrick McHardy6d834e02008-01-23 20:32:42 -0800991 if (tb[1] == NULL)
992 return NULL;
993 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
994 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800996 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Thomas Graf26dab892006-07-05 20:45:06 -0700998 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
1001static int
1002tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1003{
1004 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001005 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001006 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 struct tc_action_ops *a_o;
1008 struct tc_action a;
1009 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001010 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001011 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001014 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return 0;
1016 }
1017
Thomas Graf26dab892006-07-05 20:45:06 -07001018 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001019 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 memset(&a, 0, sizeof(struct tc_action));
1023 a.ops = a_o;
1024
Eric W. Biederman15e47302012-09-07 20:12:54 +00001025 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001026 cb->nlh->nlmsg_type, sizeof(*t), 0);
1027 if (!nlh)
1028 goto out_module_put;
1029 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001031 t->tca__pad1 = 0;
1032 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001034 nest = nla_nest_start(skb, TCA_ACT_TAB);
1035 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001036 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1039 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001040 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001043 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 ret = skb->len;
1045 } else
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001046 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001048 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001049 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 nlh->nlmsg_flags |= NLM_F_MULTI;
1051 module_put(a_o->owner);
1052 return skb->len;
1053
David S. Miller8b00a532012-06-26 21:39:32 -07001054out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001056 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return skb->len;
1058}
1059
1060static int __init tc_action_init(void)
1061{
Greg Rosec7ac8672011-06-10 01:27:09 +00001062 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1063 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1064 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1065 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return 0;
1068}
1069
1070subsys_initcall(tc_action_init);