blob: 6f103fd76c17c3d8592dca82d50fa38b6075766a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sched/act_api.c Packet action API.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Author: Jamal Hadi Salim
10 *
11 *
12 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/types.h>
15#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/init.h>
21#include <linux/kmod.h>
Patrick McHardyab27cfb2008-01-23 20:33:13 -080022#include <linux/err.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040023#include <linux/module.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110024#include <net/net_namespace.h>
25#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <net/sch_generic.h>
27#include <net/act_api.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070028#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
David S. Millere9ce1cd2006-08-21 23:54:55 -070030void tcf_hash_destroy(struct tcf_common *p, struct tcf_hashinfo *hinfo)
31{
WANG Cong89819dc2013-12-15 20:15:09 -080032 spin_lock_bh(&hinfo->lock);
33 hlist_del(&p->tcfc_head);
34 spin_unlock_bh(&hinfo->lock);
35 gen_kill_estimator(&p->tcfc_bstats,
36 &p->tcfc_rate_est);
37 /*
38 * gen_estimator est_timer() might access p->tcfc_lock
39 * or bstats, wait a RCU grace period before freeing p
40 */
41 kfree_rcu(p, tcfc_rcu);
David S. Millere9ce1cd2006-08-21 23:54:55 -070042}
43EXPORT_SYMBOL(tcf_hash_destroy);
44
45int tcf_hash_release(struct tcf_common *p, int bind,
46 struct tcf_hashinfo *hinfo)
47{
48 int ret = 0;
49
50 if (p) {
51 if (bind)
52 p->tcfc_bindcnt--;
53
54 p->tcfc_refcnt--;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090055 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070056 tcf_hash_destroy(p, hinfo);
57 ret = 1;
58 }
59 }
60 return ret;
61}
62EXPORT_SYMBOL(tcf_hash_release);
63
64static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
65 struct tc_action *a, struct tcf_hashinfo *hinfo)
66{
WANG Cong89819dc2013-12-15 20:15:09 -080067 struct hlist_head *head;
David S. Millere9ce1cd2006-08-21 23:54:55 -070068 struct tcf_common *p;
Eric Dumazetcc7ec452011-01-19 19:26:56 +000069 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080070 struct nlattr *nest;
David S. Millere9ce1cd2006-08-21 23:54:55 -070071
WANG Cong89819dc2013-12-15 20:15:09 -080072 spin_lock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -070073
74 s_i = cb->args[0];
75
76 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Cong89819dc2013-12-15 20:15:09 -080077 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
David S. Millere9ce1cd2006-08-21 23:54:55 -070078
WANG Cong89819dc2013-12-15 20:15:09 -080079 hlist_for_each_entry_rcu(p, head, tcfc_head) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070080 index++;
81 if (index < s_i)
82 continue;
83 a->priv = p;
84 a->order = n_i;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080085
86 nest = nla_nest_start(skb, a->order);
87 if (nest == NULL)
88 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -070089 err = tcf_action_dump_1(skb, a, 0, 0);
90 if (err < 0) {
91 index--;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080092 nlmsg_trim(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -070093 goto done;
94 }
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080095 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -070096 n_i++;
97 if (n_i >= TCA_ACT_MAX_PRIO)
98 goto done;
99 }
100 }
101done:
WANG Cong89819dc2013-12-15 20:15:09 -0800102 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700103 if (n_i)
104 cb->args[0] += n_i;
105 return n_i;
106
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800107nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800108 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700109 goto done;
110}
111
112static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a,
113 struct tcf_hashinfo *hinfo)
114{
WANG Cong89819dc2013-12-15 20:15:09 -0800115 struct hlist_head *head;
116 struct hlist_node *n;
117 struct tcf_common *p;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800118 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000119 int i = 0, n_i = 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700120
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800121 nest = nla_nest_start(skb, a->order);
122 if (nest == NULL)
123 goto nla_put_failure;
David S. Miller1b34ec42012-03-29 05:11:39 -0400124 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
125 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700126 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Cong89819dc2013-12-15 20:15:09 -0800127 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
128 hlist_for_each_entry_safe(p, n, head, tcfc_head) {
David S. Millere9ce1cd2006-08-21 23:54:55 -0700129 if (ACT_P_DELETED == tcf_hash_release(p, 0, hinfo))
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000130 module_put(a->ops->owner);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700131 n_i++;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700132 }
133 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400134 if (nla_put_u32(skb, TCA_FCNT, n_i))
135 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800136 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700137
138 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800139nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800140 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700141 return -EINVAL;
142}
143
stephen hemminger9c75f402013-12-31 11:54:00 -0800144static int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
145 int type, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700146{
147 struct tcf_hashinfo *hinfo = a->ops->hinfo;
148
149 if (type == RTM_DELACTION) {
150 return tcf_del_walker(skb, a, hinfo);
151 } else if (type == RTM_GETACTION) {
152 return tcf_dump_walker(skb, cb, a, hinfo);
153 } else {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000154 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700155 return -EINVAL;
156 }
157}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700158
159struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
160{
WANG Cong89819dc2013-12-15 20:15:09 -0800161 struct tcf_common *p = NULL;
162 struct hlist_head *head;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700163
WANG Cong89819dc2013-12-15 20:15:09 -0800164 spin_lock_bh(&hinfo->lock);
165 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
166 hlist_for_each_entry_rcu(p, head, tcfc_head)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700167 if (p->tcfc_index == index)
168 break;
WANG Cong89819dc2013-12-15 20:15:09 -0800169 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700170
171 return p;
172}
173EXPORT_SYMBOL(tcf_hash_lookup);
174
175u32 tcf_hash_new_index(u32 *idx_gen, struct tcf_hashinfo *hinfo)
176{
177 u32 val = *idx_gen;
178
179 do {
180 if (++val == 0)
181 val = 1;
182 } while (tcf_hash_lookup(val, hinfo));
183
Yang Yingliang17569fa2013-12-10 20:55:29 +0800184 *idx_gen = val;
185 return val;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700186}
187EXPORT_SYMBOL(tcf_hash_new_index);
188
stephen hemminger9c75f402013-12-31 11:54:00 -0800189static int tcf_hash_search(struct tc_action *a, u32 index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700190{
191 struct tcf_hashinfo *hinfo = a->ops->hinfo;
192 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
193
194 if (p) {
195 a->priv = p;
196 return 1;
197 }
198 return 0;
199}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700200
201struct tcf_common *tcf_hash_check(u32 index, struct tc_action *a, int bind,
202 struct tcf_hashinfo *hinfo)
203{
204 struct tcf_common *p = NULL;
205 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700206 if (bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700207 p->tcfc_bindcnt++;
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700208 p->tcfc_refcnt++;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700209 a->priv = p;
210 }
211 return p;
212}
213EXPORT_SYMBOL(tcf_hash_check);
214
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800215struct tcf_common *tcf_hash_create(u32 index, struct nlattr *est,
216 struct tc_action *a, int size, int bind,
217 u32 *idx_gen, struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700218{
219 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
220
221 if (unlikely(!p))
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800222 return ERR_PTR(-ENOMEM);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700223 p->tcfc_refcnt = 1;
224 if (bind)
225 p->tcfc_bindcnt = 1;
226
227 spin_lock_init(&p->tcfc_lock);
WANG Cong89819dc2013-12-15 20:15:09 -0800228 INIT_HLIST_NODE(&p->tcfc_head);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700229 p->tcfc_index = index ? index : tcf_hash_new_index(idx_gen, hinfo);
230 p->tcfc_tm.install = jiffies;
231 p->tcfc_tm.lastuse = jiffies;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800232 if (est) {
233 int err = gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
234 &p->tcfc_lock, est);
235 if (err) {
236 kfree(p);
237 return ERR_PTR(err);
238 }
239 }
240
David S. Millere9ce1cd2006-08-21 23:54:55 -0700241 a->priv = (void *) p;
242 return p;
243}
244EXPORT_SYMBOL(tcf_hash_create);
245
246void tcf_hash_insert(struct tcf_common *p, struct tcf_hashinfo *hinfo)
247{
248 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
249
WANG Cong89819dc2013-12-15 20:15:09 -0800250 spin_lock_bh(&hinfo->lock);
251 hlist_add_head(&p->tcfc_head, &hinfo->htab[h]);
252 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700253}
254EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
WANG Cong1f747c22013-12-15 20:15:10 -0800256static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257static DEFINE_RWLOCK(act_mod_lock);
258
259int tcf_register_action(struct tc_action_ops *act)
260{
WANG Cong1f747c22013-12-15 20:15:10 -0800261 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500263 /* Must supply act, dump, cleanup and init */
264 if (!act->act || !act->dump || !act->cleanup || !act->init)
265 return -EINVAL;
266
Jamal Hadi Salim382ca8a2013-12-04 09:26:55 -0500267 /* Supply defaults */
Jamal Hadi Salim63ef6172013-12-04 09:26:53 -0500268 if (!act->lookup)
269 act->lookup = tcf_hash_search;
Jamal Hadi Salim382ca8a2013-12-04 09:26:55 -0500270 if (!act->walk)
271 act->walk = tcf_generic_walker;
Jamal Hadi Salim63ef6172013-12-04 09:26:53 -0500272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800274 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
276 write_unlock(&act_mod_lock);
277 return -EEXIST;
278 }
279 }
WANG Cong1f747c22013-12-15 20:15:10 -0800280 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 write_unlock(&act_mod_lock);
282 return 0;
283}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800284EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286int tcf_unregister_action(struct tc_action_ops *act)
287{
WANG Cong1f747c22013-12-15 20:15:10 -0800288 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 int err = -ENOENT;
290
291 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800292 list_for_each_entry(a, &act_base, head) {
293 if (a == act) {
294 list_del(&act->head);
295 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299 write_unlock(&act_mod_lock);
300 return err;
301}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800302EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304/* lookup by name */
305static struct tc_action_ops *tc_lookup_action_n(char *kind)
306{
Eric Dumazeta7928662013-12-20 12:32:32 -0800307 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 if (kind) {
310 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800311 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800313 if (try_module_get(a->owner))
314 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 break;
316 }
317 }
318 read_unlock(&act_mod_lock);
319 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800320 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800323/* lookup by nlattr */
324static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
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) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800331 if (nla_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
WANG Cong33be6272013-12-15 20:15:05 -0800342int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900343 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000345 const struct tc_action *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 int ret = -1;
347
348 if (skb->tc_verd & TC_NCLS) {
349 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 ret = TC_ACT_OK;
351 goto exec_done;
352 }
WANG Cong33be6272013-12-15 20:15:05 -0800353 list_for_each_entry(a, actions, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354repeat:
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500355 if (a->ops) {
Patrick McHardyf43c5a02006-01-08 22:15:34 -0800356 ret = a->ops->act(skb, a, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (TC_MUNGED & skb->tc_verd) {
358 /* copied already, allow trampling */
359 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
360 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (ret == TC_ACT_REPEAT)
363 goto repeat; /* we need a ttl - JHS */
J Hadi Salim14d50e72005-05-03 16:29:13 -0700364 if (ret != TC_ACT_PIPE)
365 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return ret;
370}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800371EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
WANG Cong33be6272013-12-15 20:15:05 -0800373void tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
WANG Cong33be6272013-12-15 20:15:05 -0800375 struct tc_action *a, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
WANG Cong33be6272013-12-15 20:15:05 -0800377 list_for_each_entry_safe(a, tmp, actions, list) {
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500378 if (a->ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
380 module_put(a->ops->owner);
WANG Cong33be6272013-12-15 20:15:05 -0800381 list_del(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 kfree(a);
stephen hemminger6ff9c362010-05-12 06:37:05 +0000383 } else {
384 /*FIXME: Remove later - catch insertion bugs*/
385 WARN(1, "tcf_action_destroy: BUG? destroying NULL ops\n");
WANG Cong33be6272013-12-15 20:15:05 -0800386 list_del(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 kfree(a);
388 }
389 }
390}
391
392int
393tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
394{
395 int err = -EINVAL;
396
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500397 if (a->ops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return err;
399 return a->ops->dump(skb, a, bind, ref);
400}
401
402int
403tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
404{
405 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700406 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800407 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500409 if (a->ops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return err;
411
David S. Miller1b34ec42012-03-29 05:11:39 -0400412 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
413 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800415 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800416 nest = nla_nest_start(skb, TCA_OPTIONS);
417 if (nest == NULL)
418 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000419 err = tcf_action_dump_old(skb, a, bind, ref);
420 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800421 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return err;
423 }
424
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800425nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700426 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return -1;
428}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800429EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431int
WANG Cong33be6272013-12-15 20:15:05 -0800432tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 struct tc_action *a;
435 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800436 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
WANG Cong33be6272013-12-15 20:15:05 -0800438 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800439 nest = nla_nest_start(skb, a->order);
440 if (nest == NULL)
441 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 err = tcf_action_dump_1(skb, a, bind, ref);
443 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700444 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800445 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447
448 return 0;
449
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800450nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700451 err = -EINVAL;
452errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800453 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700454 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000457struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
458 struct nlattr *est, char *name, int ovr,
459 int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 struct tc_action *a;
462 struct tc_action_ops *a_o;
463 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000464 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800465 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800466 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800469 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
470 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800472 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800473 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (kind == NULL)
475 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800476 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 goto err_out;
478 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800479 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
481 goto err_out;
482 }
483
484 a_o = tc_lookup_action_n(act_name);
485 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700486#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800488 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 rtnl_lock();
490
491 a_o = tc_lookup_action_n(act_name);
492
493 /* We dropped the RTNL semaphore in order to
494 * perform the module load. So, even if we
495 * succeeded in loading the module we have to
496 * tell the caller to replay the request. We
497 * indicate this using -EAGAIN.
498 */
499 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800500 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 goto err_mod;
502 }
503#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800504 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 goto err_out;
506 }
507
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800508 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700509 a = kzalloc(sizeof(*a), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (a == NULL)
511 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
WANG Cong33be6272013-12-15 20:15:05 -0800513 INIT_LIST_HEAD(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /* backward compatibility for policer */
515 if (name == NULL)
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000516 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 else
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000518 err = a_o->init(net, nla, est, a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800519 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 goto err_free;
521
522 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000523 * if it exists and is only bound to in a_o->init() then
524 * ACT_P_CREATED is not returned (a zero is).
525 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800526 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 module_put(a_o->owner);
528 a->ops = a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return a;
531
532err_free:
533 kfree(a);
534err_mod:
535 module_put(a_o->owner);
536err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800537 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
WANG Cong33be6272013-12-15 20:15:05 -0800540int tcf_action_init(struct net *net, struct nlattr *nla,
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000541 struct nlattr *est, char *name, int ovr,
WANG Cong33be6272013-12-15 20:15:05 -0800542 int bind, struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000544 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800545 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800546 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 int i;
548
Patrick McHardycee63722008-01-23 20:33:32 -0800549 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
550 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800551 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800553 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000554 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800555 if (IS_ERR(act)) {
556 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800558 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800559 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800560 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
WANG Cong33be6272013-12-15 20:15:05 -0800562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564err:
WANG Cong33be6272013-12-15 20:15:05 -0800565 tcf_action_destroy(actions, bind);
566 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
569int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
570 int compat_mode)
571{
572 int err = 0;
573 struct gnet_dump d;
574 struct tcf_act_hdr *h = a->priv;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (h == NULL)
577 goto errout;
578
579 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400580 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 */
582 if (compat_mode) {
583 if (a->type == TCA_OLD_COMPAT)
584 err = gnet_stats_start_copy_compat(skb, 0,
Patrick McHardy4bdf3992007-07-02 22:47:37 -0700585 TCA_STATS, TCA_XSTATS, &h->tcf_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 else
587 return 0;
588 } else
589 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
Patrick McHardy4bdf3992007-07-02 22:47:37 -0700590 &h->tcf_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 if (err < 0)
593 goto errout;
594
David S. Millere9ce1cd2006-08-21 23:54:55 -0700595 if (gnet_stats_copy_basic(&d, &h->tcf_bstats) < 0 ||
Eric Dumazetd250a5f2009-10-02 10:32:18 +0000596 gnet_stats_copy_rate_est(&d, &h->tcf_bstats,
597 &h->tcf_rate_est) < 0 ||
David S. Millere9ce1cd2006-08-21 23:54:55 -0700598 gnet_stats_copy_queue(&d, &h->tcf_qstats) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 goto errout;
600
601 if (gnet_stats_finish_copy(&d) < 0)
602 goto errout;
603
604 return 0;
605
606errout:
607 return -1;
608}
609
610static int
WANG Cong33be6272013-12-15 20:15:05 -0800611tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900612 u16 flags, int event, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 struct tcamsg *t;
615 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700616 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800617 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Eric W. Biederman15e47302012-09-07 20:12:54 +0000619 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700620 if (!nlh)
621 goto out_nlmsg_trim;
622 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700624 t->tca__pad1 = 0;
625 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900626
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800627 nest = nla_nest_start(skb, TCA_ACT_TAB);
628 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700629 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
WANG Cong33be6272013-12-15 20:15:05 -0800631 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700632 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800634 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900635
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700636 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return skb->len;
638
David S. Miller8b00a532012-06-26 21:39:32 -0700639out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700640 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return -1;
642}
643
644static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000645act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800646 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
651 if (!skb)
652 return -ENOBUFS;
WANG Cong33be6272013-12-15 20:15:05 -0800653 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 kfree_skb(skb);
655 return -EINVAL;
656 }
Thomas Graf2942e902006-08-15 00:30:25 -0700657
Eric W. Biederman15e47302012-09-07 20:12:54 +0000658 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
661static struct tc_action *
Eric W. Biederman15e47302012-09-07 20:12:54 +0000662tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000664 struct nlattr *tb[TCA_ACT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 struct tc_action *a;
666 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800667 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Patrick McHardycee63722008-01-23 20:33:32 -0800669 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
670 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800671 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Patrick McHardycee63722008-01-23 20:33:32 -0800673 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800674 if (tb[TCA_ACT_INDEX] == NULL ||
675 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800676 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800677 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800679 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700680 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (a == NULL)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800682 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
WANG Cong33be6272013-12-15 20:15:05 -0800684 INIT_LIST_HEAD(&a->list);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800685 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800686 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (a->ops == NULL)
688 goto err_free;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800689 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (a->ops->lookup(a, index) == 0)
691 goto err_mod;
692
693 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696err_mod:
697 module_put(a->ops->owner);
698err_free:
699 kfree(a);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800700err_out:
701 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
WANG Cong33be6272013-12-15 20:15:05 -0800704static void cleanup_a(struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
WANG Cong33be6272013-12-15 20:15:05 -0800706 struct tc_action *a, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
WANG Cong33be6272013-12-15 20:15:05 -0800708 list_for_each_entry_safe(a, tmp, actions, list) {
709 list_del(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 kfree(a);
711 }
712}
713
714static struct tc_action *create_a(int i)
715{
716 struct tc_action *act;
717
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700718 act = kzalloc(sizeof(*act), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (act == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000720 pr_debug("create_a: failed to alloc!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return NULL;
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800724 INIT_LIST_HEAD(&act->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return act;
726}
727
Tom Goff7316ae82010-03-19 15:40:13 +0000728static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000729 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 struct sk_buff *skb;
732 unsigned char *b;
733 struct nlmsghdr *nlh;
734 struct tcamsg *t;
735 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800736 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000737 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800738 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 struct tc_action *a = create_a(0);
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700740 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 if (a == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000743 pr_debug("tca_action_flush: couldnt create tc_action\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return err;
745 }
746
747 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
748 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000749 pr_debug("tca_action_flush: failed skb alloc\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 kfree(a);
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700751 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700754 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Patrick McHardycee63722008-01-23 20:33:32 -0800756 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
757 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 goto err_out;
759
Patrick McHardycee63722008-01-23 20:33:32 -0800760 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800761 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 a->ops = tc_lookup_action(kind);
763 if (a->ops == NULL)
764 goto err_out;
765
Eric W. Biederman15e47302012-09-07 20:12:54 +0000766 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700767 if (!nlh)
768 goto out_module_put;
769 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700771 t->tca__pad1 = 0;
772 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800774 nest = nla_nest_start(skb, TCA_ACT_TAB);
775 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700776 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
779 if (err < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700780 goto out_module_put;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700781 if (err == 0)
782 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800784 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700786 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 nlh->nlmsg_flags |= NLM_F_ROOT;
788 module_put(a->ops->owner);
789 kfree(a);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000790 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000791 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (err > 0)
793 return 0;
794
795 return err;
796
David S. Miller8b00a532012-06-26 21:39:32 -0700797out_module_put:
Thomas Grafebbaeab2006-07-09 11:36:23 -0700798 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700800noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 kfree_skb(skb);
802 kfree(a);
803 return err;
804}
805
806static int
Tom Goff7316ae82010-03-19 15:40:13 +0000807tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000808 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Patrick McHardycee63722008-01-23 20:33:32 -0800810 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000811 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800812 struct tc_action *act;
813 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Patrick McHardycee63722008-01-23 20:33:32 -0800815 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
816 if (ret < 0)
817 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000819 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700820 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000821 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700822 else
823 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800826 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000827 act = tcf_action_get_1(tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800828 if (IS_ERR(act)) {
829 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800831 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800832 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800833 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
836 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800837 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 else { /* delete */
839 struct sk_buff *skb;
840
841 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
842 if (!skb) {
843 ret = -ENOBUFS;
844 goto err;
845 }
846
WANG Cong33be6272013-12-15 20:15:05 -0800847 if (tca_get_fill(skb, &actions, portid, n->nlmsg_seq, 0, event,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900848 0, 1) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 kfree_skb(skb);
850 ret = -EINVAL;
851 goto err;
852 }
853
854 /* now do the delete */
WANG Cong33be6272013-12-15 20:15:05 -0800855 tcf_action_destroy(&actions, 0);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000856 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000857 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (ret > 0)
859 return 0;
860 return ret;
861 }
862err:
WANG Cong33be6272013-12-15 20:15:05 -0800863 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return ret;
865}
866
WANG Cong33be6272013-12-15 20:15:05 -0800867static int tcf_add_notify(struct net *net, struct list_head *actions,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000868 u32 portid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 struct tcamsg *t;
871 struct nlmsghdr *nlh;
872 struct sk_buff *skb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800873 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 unsigned char *b;
875 int err = 0;
876
877 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
878 if (!skb)
879 return -ENOBUFS;
880
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700881 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Eric W. Biederman15e47302012-09-07 20:12:54 +0000883 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700884 if (!nlh)
885 goto out_kfree_skb;
886 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700888 t->tca__pad1 = 0;
889 t->tca__pad2 = 0;
890
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800891 nest = nla_nest_start(skb, TCA_ACT_TAB);
892 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700893 goto out_kfree_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
WANG Cong33be6272013-12-15 20:15:05 -0800895 if (tcf_action_dump(skb, actions, 0, 0) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700896 goto out_kfree_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800898 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900899
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700900 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Patrick McHardyac6d4392005-08-14 19:29:52 -0700901 NETLINK_CB(skb).dst_group = RTNLGRP_TC;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900902
Eric W. Biederman15e47302012-09-07 20:12:54 +0000903 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (err > 0)
905 err = 0;
906 return err;
907
David S. Miller8b00a532012-06-26 21:39:32 -0700908out_kfree_skb:
Patrick McHardyf6e57462006-03-12 20:33:22 -0800909 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return -1;
911}
912
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914static int
Tom Goff7316ae82010-03-19 15:40:13 +0000915tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000916 u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -0800919 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 u32 seq = n->nlmsg_seq;
921
WANG Cong33be6272013-12-15 20:15:05 -0800922 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
923 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 goto done;
925
926 /* dump then free all the actions after update; inserted policy
927 * stays intact
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000928 */
WANG Cong33be6272013-12-15 20:15:05 -0800929 ret = tcf_add_notify(net, &actions, portid, seq, RTM_NEWACTION, n->nlmsg_flags);
930 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931done:
932 return ret;
933}
934
Thomas Graf661d2962013-03-21 07:45:29 +0000935static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900937 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800938 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +0000939 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 int ret = 0, ovr = 0;
941
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000942 if ((n->nlmsg_type != RTM_GETACTION) && !capable(CAP_NET_ADMIN))
943 return -EPERM;
944
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800945 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
946 if (ret < 0)
947 return ret;
948
949 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000950 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return -EINVAL;
952 }
953
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000954 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 switch (n->nlmsg_type) {
956 case RTM_NEWACTION:
957 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300958 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 * Note that CREATE | EXCL implies that
960 * but since we want avoid ambiguity (eg when flags
961 * is zero) then just set this
962 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000963 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 ovr = 1;
965replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000966 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (ret == -EAGAIN)
968 goto replay;
969 break;
970 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000971 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000972 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 break;
974 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000975 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000976 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 break;
978 default:
979 BUG();
980 }
981
982 return ret;
983}
984
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800985static struct nlattr *
Patrick McHardy3a6c2b42009-08-25 16:07:40 +0200986find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000988 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800989 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
990 struct nlattr *nla[TCAA_MAX + 1];
991 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Patrick McHardyc96c9472008-01-23 20:32:58 -0800993 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800995 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (tb1 == NULL)
997 return NULL;
998
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800999 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1000 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Patrick McHardy6d834e02008-01-23 20:32:42 -08001003 if (tb[1] == NULL)
1004 return NULL;
1005 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1006 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001008 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Thomas Graf26dab892006-07-05 20:45:06 -07001010 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
1013static int
1014tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1015{
1016 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001017 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001018 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 struct tc_action_ops *a_o;
1020 struct tc_action a;
1021 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001022 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001023 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001026 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return 0;
1028 }
1029
Thomas Graf26dab892006-07-05 20:45:06 -07001030 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001031 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 memset(&a, 0, sizeof(struct tc_action));
1035 a.ops = a_o;
1036
Eric W. Biederman15e47302012-09-07 20:12:54 +00001037 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001038 cb->nlh->nlmsg_type, sizeof(*t), 0);
1039 if (!nlh)
1040 goto out_module_put;
1041 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001043 t->tca__pad1 = 0;
1044 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001046 nest = nla_nest_start(skb, TCA_ACT_TAB);
1047 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001048 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1051 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001052 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001055 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 ret = skb->len;
1057 } else
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001058 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001060 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001061 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 nlh->nlmsg_flags |= NLM_F_MULTI;
1063 module_put(a_o->owner);
1064 return skb->len;
1065
David S. Miller8b00a532012-06-26 21:39:32 -07001066out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001068 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return skb->len;
1070}
1071
1072static int __init tc_action_init(void)
1073{
Greg Rosec7ac8672011-06-10 01:27:09 +00001074 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1075 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1076 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1077 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return 0;
1080}
1081
1082subsys_initcall(tc_action_init);