blob: 35f89e9ce49c2f9300c38a03ca0348e726331d29 [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) {
Jamal Hadi Salim805c1f42013-12-23 08:02:13 -0500129 if (ACT_P_DELETED == tcf_hash_release(p, 0, hinfo)) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000130 module_put(a->ops->owner);
Jamal Hadi Salim805c1f42013-12-23 08:02:13 -0500131 n_i++;
132 }
David S. Millere9ce1cd2006-08-21 23:54:55 -0700133 }
134 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400135 if (nla_put_u32(skb, TCA_FCNT, n_i))
136 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800137 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700138
139 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800140nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800141 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700142 return -EINVAL;
143}
144
stephen hemminger9c75f402013-12-31 11:54:00 -0800145static int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
146 int type, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700147{
148 struct tcf_hashinfo *hinfo = a->ops->hinfo;
149
150 if (type == RTM_DELACTION) {
151 return tcf_del_walker(skb, a, hinfo);
152 } else if (type == RTM_GETACTION) {
153 return tcf_dump_walker(skb, cb, a, hinfo);
154 } else {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000155 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700156 return -EINVAL;
157 }
158}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700159
160struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
161{
WANG Cong89819dc2013-12-15 20:15:09 -0800162 struct tcf_common *p = NULL;
163 struct hlist_head *head;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700164
WANG Cong89819dc2013-12-15 20:15:09 -0800165 spin_lock_bh(&hinfo->lock);
166 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
167 hlist_for_each_entry_rcu(p, head, tcfc_head)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700168 if (p->tcfc_index == index)
169 break;
WANG Cong89819dc2013-12-15 20:15:09 -0800170 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700171
172 return p;
173}
174EXPORT_SYMBOL(tcf_hash_lookup);
175
WANG Congddafd342014-01-09 16:13:59 -0800176u32 tcf_hash_new_index(struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700177{
WANG Congddafd342014-01-09 16:13:59 -0800178 u32 val = hinfo->index;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700179
180 do {
181 if (++val == 0)
182 val = 1;
183 } while (tcf_hash_lookup(val, hinfo));
184
WANG Congddafd342014-01-09 16:13:59 -0800185 hinfo->index = val;
Yang Yingliang17569fa2013-12-10 20:55:29 +0800186 return val;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700187}
188EXPORT_SYMBOL(tcf_hash_new_index);
189
stephen hemminger9c75f402013-12-31 11:54:00 -0800190static int tcf_hash_search(struct tc_action *a, u32 index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700191{
192 struct tcf_hashinfo *hinfo = a->ops->hinfo;
193 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
194
195 if (p) {
196 a->priv = p;
197 return 1;
198 }
199 return 0;
200}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700201
202struct tcf_common *tcf_hash_check(u32 index, struct tc_action *a, int bind,
203 struct tcf_hashinfo *hinfo)
204{
205 struct tcf_common *p = NULL;
206 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700207 if (bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700208 p->tcfc_bindcnt++;
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700209 p->tcfc_refcnt++;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700210 a->priv = p;
211 }
212 return p;
213}
214EXPORT_SYMBOL(tcf_hash_check);
215
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800216struct tcf_common *tcf_hash_create(u32 index, struct nlattr *est,
217 struct tc_action *a, int size, int bind,
WANG Congddafd342014-01-09 16:13:59 -0800218 struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700219{
220 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
221
222 if (unlikely(!p))
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800223 return ERR_PTR(-ENOMEM);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700224 p->tcfc_refcnt = 1;
225 if (bind)
226 p->tcfc_bindcnt = 1;
227
228 spin_lock_init(&p->tcfc_lock);
WANG Cong89819dc2013-12-15 20:15:09 -0800229 INIT_HLIST_NODE(&p->tcfc_head);
WANG Congddafd342014-01-09 16:13:59 -0800230 p->tcfc_index = index ? index : tcf_hash_new_index(hinfo);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700231 p->tcfc_tm.install = jiffies;
232 p->tcfc_tm.lastuse = jiffies;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800233 if (est) {
234 int err = gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
235 &p->tcfc_lock, est);
236 if (err) {
237 kfree(p);
238 return ERR_PTR(err);
239 }
240 }
241
David S. Millere9ce1cd2006-08-21 23:54:55 -0700242 a->priv = (void *) p;
243 return p;
244}
245EXPORT_SYMBOL(tcf_hash_create);
246
247void tcf_hash_insert(struct tcf_common *p, struct tcf_hashinfo *hinfo)
248{
249 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
250
WANG Cong89819dc2013-12-15 20:15:09 -0800251 spin_lock_bh(&hinfo->lock);
252 hlist_add_head(&p->tcfc_head, &hinfo->htab[h]);
253 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700254}
255EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
WANG Cong1f747c22013-12-15 20:15:10 -0800257static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258static DEFINE_RWLOCK(act_mod_lock);
259
260int tcf_register_action(struct tc_action_ops *act)
261{
WANG Cong1f747c22013-12-15 20:15:10 -0800262 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500264 /* Must supply act, dump, cleanup and init */
265 if (!act->act || !act->dump || !act->cleanup || !act->init)
266 return -EINVAL;
267
Jamal Hadi Salim382ca8a2013-12-04 09:26:55 -0500268 /* Supply defaults */
Jamal Hadi Salim63ef6172013-12-04 09:26:53 -0500269 if (!act->lookup)
270 act->lookup = tcf_hash_search;
Jamal Hadi Salim382ca8a2013-12-04 09:26:55 -0500271 if (!act->walk)
272 act->walk = tcf_generic_walker;
Jamal Hadi Salim63ef6172013-12-04 09:26:53 -0500273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800275 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
277 write_unlock(&act_mod_lock);
278 return -EEXIST;
279 }
280 }
WANG Cong1f747c22013-12-15 20:15:10 -0800281 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 write_unlock(&act_mod_lock);
283 return 0;
284}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800285EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287int tcf_unregister_action(struct tc_action_ops *act)
288{
WANG Cong1f747c22013-12-15 20:15:10 -0800289 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 int err = -ENOENT;
291
292 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800293 list_for_each_entry(a, &act_base, head) {
294 if (a == act) {
295 list_del(&act->head);
296 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300 write_unlock(&act_mod_lock);
301 return err;
302}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800303EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305/* lookup by name */
306static struct tc_action_ops *tc_lookup_action_n(char *kind)
307{
Eric Dumazeta7928662013-12-20 12:32:32 -0800308 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 if (kind) {
311 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800312 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800314 if (try_module_get(a->owner))
315 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 break;
317 }
318 }
319 read_unlock(&act_mod_lock);
320 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800321 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800324/* lookup by nlattr */
325static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Eric Dumazeta7928662013-12-20 12:32:32 -0800327 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 if (kind) {
330 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800331 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800332 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800333 if (try_module_get(a->owner))
334 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 break;
336 }
337 }
338 read_unlock(&act_mod_lock);
339 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800340 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
WANG Cong33be6272013-12-15 20:15:05 -0800343int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900344 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000346 const struct tc_action *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 int ret = -1;
348
349 if (skb->tc_verd & TC_NCLS) {
350 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 ret = TC_ACT_OK;
352 goto exec_done;
353 }
WANG Cong33be6272013-12-15 20:15:05 -0800354 list_for_each_entry(a, actions, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500356 ret = a->ops->act(skb, a, res);
357 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500362 if (ret == TC_ACT_REPEAT)
363 goto repeat; /* we need a ttl - JHS */
364 if (ret != TC_ACT_PIPE)
365 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return ret;
369}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800370EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
WANG Cong33be6272013-12-15 20:15:05 -0800372void tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
WANG Cong33be6272013-12-15 20:15:05 -0800374 struct tc_action *a, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
WANG Cong33be6272013-12-15 20:15:05 -0800376 list_for_each_entry_safe(a, tmp, actions, list) {
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500377 if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
378 module_put(a->ops->owner);
379 list_del(&a->list);
380 kfree(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382}
383
384int
385tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
386{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return a->ops->dump(skb, a, bind, ref);
388}
389
390int
391tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
392{
393 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700394 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800395 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
David S. Miller1b34ec42012-03-29 05:11:39 -0400397 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
398 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800400 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800401 nest = nla_nest_start(skb, TCA_OPTIONS);
402 if (nest == NULL)
403 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000404 err = tcf_action_dump_old(skb, a, bind, ref);
405 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800406 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return err;
408 }
409
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800410nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700411 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return -1;
413}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800414EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416int
WANG Cong33be6272013-12-15 20:15:05 -0800417tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 struct tc_action *a;
420 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800421 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
WANG Cong33be6272013-12-15 20:15:05 -0800423 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800424 nest = nla_nest_start(skb, a->order);
425 if (nest == NULL)
426 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 err = tcf_action_dump_1(skb, a, bind, ref);
428 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700429 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800430 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432
433 return 0;
434
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800435nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700436 err = -EINVAL;
437errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800438 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700439 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000442struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
443 struct nlattr *est, char *name, int ovr,
444 int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 struct tc_action *a;
447 struct tc_action_ops *a_o;
448 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000449 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800450 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800451 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800454 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
455 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800457 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800458 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (kind == NULL)
460 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800461 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 goto err_out;
463 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800464 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
466 goto err_out;
467 }
468
469 a_o = tc_lookup_action_n(act_name);
470 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700471#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800473 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 rtnl_lock();
475
476 a_o = tc_lookup_action_n(act_name);
477
478 /* We dropped the RTNL semaphore in order to
479 * perform the module load. So, even if we
480 * succeeded in loading the module we have to
481 * tell the caller to replay the request. We
482 * indicate this using -EAGAIN.
483 */
484 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800485 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 goto err_mod;
487 }
488#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800489 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 goto err_out;
491 }
492
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800493 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700494 a = kzalloc(sizeof(*a), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (a == NULL)
496 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
WANG Cong33be6272013-12-15 20:15:05 -0800498 INIT_LIST_HEAD(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /* backward compatibility for policer */
500 if (name == NULL)
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000501 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 else
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000503 err = a_o->init(net, nla, est, a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800504 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 goto err_free;
506
507 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000508 * if it exists and is only bound to in a_o->init() then
509 * ACT_P_CREATED is not returned (a zero is).
510 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800511 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 module_put(a_o->owner);
513 a->ops = a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return a;
516
517err_free:
518 kfree(a);
519err_mod:
520 module_put(a_o->owner);
521err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800522 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
WANG Cong33be6272013-12-15 20:15:05 -0800525int tcf_action_init(struct net *net, struct nlattr *nla,
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000526 struct nlattr *est, char *name, int ovr,
WANG Cong33be6272013-12-15 20:15:05 -0800527 int bind, struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000529 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800530 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800531 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 int i;
533
Patrick McHardycee63722008-01-23 20:33:32 -0800534 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
535 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800536 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800538 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000539 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800540 if (IS_ERR(act)) {
541 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800543 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800544 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800545 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
WANG Cong33be6272013-12-15 20:15:05 -0800547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549err:
WANG Cong33be6272013-12-15 20:15:05 -0800550 tcf_action_destroy(actions, bind);
551 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
554int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
555 int compat_mode)
556{
557 int err = 0;
558 struct gnet_dump d;
WANG Cong7eb88962014-01-09 16:14:05 -0800559 struct tcf_common *p = a->priv;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900560
WANG Cong7eb88962014-01-09 16:14:05 -0800561 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto errout;
563
564 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400565 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 */
567 if (compat_mode) {
568 if (a->type == TCA_OLD_COMPAT)
569 err = gnet_stats_start_copy_compat(skb, 0,
WANG Cong7eb88962014-01-09 16:14:05 -0800570 TCA_STATS, TCA_XSTATS, &p->tcfc_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 else
572 return 0;
573 } else
574 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Cong7eb88962014-01-09 16:14:05 -0800575 &p->tcfc_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 if (err < 0)
578 goto errout;
579
WANG Cong7eb88962014-01-09 16:14:05 -0800580 if (gnet_stats_copy_basic(&d, &p->tcfc_bstats) < 0 ||
581 gnet_stats_copy_rate_est(&d, &p->tcfc_bstats,
582 &p->tcfc_rate_est) < 0 ||
583 gnet_stats_copy_queue(&d, &p->tcfc_qstats) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 goto errout;
585
586 if (gnet_stats_finish_copy(&d) < 0)
587 goto errout;
588
589 return 0;
590
591errout:
592 return -1;
593}
594
595static int
WANG Cong33be6272013-12-15 20:15:05 -0800596tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900597 u16 flags, int event, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 struct tcamsg *t;
600 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700601 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800602 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Eric W. Biederman15e47302012-09-07 20:12:54 +0000604 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700605 if (!nlh)
606 goto out_nlmsg_trim;
607 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700609 t->tca__pad1 = 0;
610 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900611
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800612 nest = nla_nest_start(skb, TCA_ACT_TAB);
613 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700614 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
WANG Cong33be6272013-12-15 20:15:05 -0800616 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700617 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800619 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900620
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700621 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return skb->len;
623
David S. Miller8b00a532012-06-26 21:39:32 -0700624out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700625 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return -1;
627}
628
629static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000630act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800631 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
636 if (!skb)
637 return -ENOBUFS;
WANG Cong33be6272013-12-15 20:15:05 -0800638 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 kfree_skb(skb);
640 return -EINVAL;
641 }
Thomas Graf2942e902006-08-15 00:30:25 -0700642
Eric W. Biederman15e47302012-09-07 20:12:54 +0000643 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
646static struct tc_action *
Eric W. Biederman15e47302012-09-07 20:12:54 +0000647tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000649 struct nlattr *tb[TCA_ACT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 struct tc_action *a;
651 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800652 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Patrick McHardycee63722008-01-23 20:33:32 -0800654 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
655 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800656 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Patrick McHardycee63722008-01-23 20:33:32 -0800658 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800659 if (tb[TCA_ACT_INDEX] == NULL ||
660 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800661 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800662 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800664 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700665 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (a == NULL)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800667 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
WANG Cong33be6272013-12-15 20:15:05 -0800669 INIT_LIST_HEAD(&a->list);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800670 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800671 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500672 if (a->ops == NULL) /* could happen in batch of actions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 goto err_free;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800674 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (a->ops->lookup(a, index) == 0)
676 goto err_mod;
677
678 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681err_mod:
682 module_put(a->ops->owner);
683err_free:
684 kfree(a);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800685err_out:
686 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
WANG Cong33be6272013-12-15 20:15:05 -0800689static void cleanup_a(struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
WANG Cong33be6272013-12-15 20:15:05 -0800691 struct tc_action *a, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
WANG Cong33be6272013-12-15 20:15:05 -0800693 list_for_each_entry_safe(a, tmp, actions, list) {
694 list_del(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 kfree(a);
696 }
697}
698
699static struct tc_action *create_a(int i)
700{
701 struct tc_action *act;
702
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700703 act = kzalloc(sizeof(*act), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (act == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000705 pr_debug("create_a: failed to alloc!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return NULL;
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800709 INIT_LIST_HEAD(&act->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return act;
711}
712
Tom Goff7316ae82010-03-19 15:40:13 +0000713static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000714 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 struct sk_buff *skb;
717 unsigned char *b;
718 struct nlmsghdr *nlh;
719 struct tcamsg *t;
720 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800721 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000722 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800723 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 struct tc_action *a = create_a(0);
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700725 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 if (a == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000728 pr_debug("tca_action_flush: couldnt create tc_action\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return err;
730 }
731
732 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
733 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000734 pr_debug("tca_action_flush: failed skb alloc\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 kfree(a);
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700736 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700739 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Patrick McHardycee63722008-01-23 20:33:32 -0800741 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
742 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 goto err_out;
744
Patrick McHardycee63722008-01-23 20:33:32 -0800745 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800746 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 a->ops = tc_lookup_action(kind);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500748 if (a->ops == NULL) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 goto err_out;
750
Eric W. Biederman15e47302012-09-07 20:12:54 +0000751 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700752 if (!nlh)
753 goto out_module_put;
754 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700756 t->tca__pad1 = 0;
757 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800759 nest = nla_nest_start(skb, TCA_ACT_TAB);
760 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700761 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
764 if (err < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700765 goto out_module_put;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700766 if (err == 0)
767 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800769 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700771 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 nlh->nlmsg_flags |= NLM_F_ROOT;
773 module_put(a->ops->owner);
774 kfree(a);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000775 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000776 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (err > 0)
778 return 0;
779
780 return err;
781
David S. Miller8b00a532012-06-26 21:39:32 -0700782out_module_put:
Thomas Grafebbaeab2006-07-09 11:36:23 -0700783 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700785noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 kfree_skb(skb);
787 kfree(a);
788 return err;
789}
790
791static int
WANG Conga56e1952014-01-09 16:14:00 -0800792tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
793 u32 portid)
794{
795 int ret;
796 struct sk_buff *skb;
797
798 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
799 if (!skb)
800 return -ENOBUFS;
801
802 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
803 0, 1) <= 0) {
804 kfree_skb(skb);
805 return -EINVAL;
806 }
807
808 /* now do the delete */
809 tcf_action_destroy(actions, 0);
810
811 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
812 n->nlmsg_flags & NLM_F_ECHO);
813 if (ret > 0)
814 return 0;
815 return ret;
816}
817
818static int
Tom Goff7316ae82010-03-19 15:40:13 +0000819tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000820 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Patrick McHardycee63722008-01-23 20:33:32 -0800822 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000823 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800824 struct tc_action *act;
825 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Patrick McHardycee63722008-01-23 20:33:32 -0800827 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
828 if (ret < 0)
829 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000831 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700832 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000833 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700834 else
835 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800838 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000839 act = tcf_action_get_1(tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800840 if (IS_ERR(act)) {
841 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800843 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800844 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800845 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847
848 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800849 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -0800851 ret = tcf_del_notify(net, n, &actions, portid);
852 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return ret;
855 }
856err:
WANG Cong33be6272013-12-15 20:15:05 -0800857 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return ret;
859}
860
WANG Conga56e1952014-01-09 16:14:00 -0800861static int
862tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
863 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 int err = 0;
867
868 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
869 if (!skb)
870 return -ENOBUFS;
871
WANG Conga56e1952014-01-09 16:14:00 -0800872 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
873 RTM_NEWACTION, 0, 0) <= 0) {
874 kfree_skb(skb);
875 return -EINVAL;
876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
WANG Conga56e1952014-01-09 16:14:00 -0800878 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
879 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (err > 0)
881 err = 0;
882 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885static int
Tom Goff7316ae82010-03-19 15:40:13 +0000886tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000887 u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -0800890 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
WANG Cong33be6272013-12-15 20:15:05 -0800892 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
893 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 goto done;
895
896 /* dump then free all the actions after update; inserted policy
897 * stays intact
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000898 */
WANG Conga56e1952014-01-09 16:14:00 -0800899 ret = tcf_add_notify(net, n, &actions, portid);
WANG Cong33be6272013-12-15 20:15:05 -0800900 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901done:
902 return ret;
903}
904
Thomas Graf661d2962013-03-21 07:45:29 +0000905static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900907 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800908 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +0000909 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 int ret = 0, ovr = 0;
911
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000912 if ((n->nlmsg_type != RTM_GETACTION) && !capable(CAP_NET_ADMIN))
913 return -EPERM;
914
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800915 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
916 if (ret < 0)
917 return ret;
918
919 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000920 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return -EINVAL;
922 }
923
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000924 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 switch (n->nlmsg_type) {
926 case RTM_NEWACTION:
927 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300928 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 * Note that CREATE | EXCL implies that
930 * but since we want avoid ambiguity (eg when flags
931 * is zero) then just set this
932 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000933 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 ovr = 1;
935replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000936 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (ret == -EAGAIN)
938 goto replay;
939 break;
940 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000941 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000942 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 break;
944 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000945 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000946 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 break;
948 default:
949 BUG();
950 }
951
952 return ret;
953}
954
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800955static struct nlattr *
Patrick McHardy3a6c2b42009-08-25 16:07:40 +0200956find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000958 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800959 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
960 struct nlattr *nla[TCAA_MAX + 1];
961 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Patrick McHardyc96c9472008-01-23 20:32:58 -0800963 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800965 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (tb1 == NULL)
967 return NULL;
968
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800969 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
970 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Patrick McHardy6d834e02008-01-23 20:32:42 -0800973 if (tb[1] == NULL)
974 return NULL;
975 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
976 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800978 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Thomas Graf26dab892006-07-05 20:45:06 -0700980 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
983static int
984tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
985{
986 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700987 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800988 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 struct tc_action_ops *a_o;
990 struct tc_action a;
991 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -0700992 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800993 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000996 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return 0;
998 }
999
Thomas Graf26dab892006-07-05 20:45:06 -07001000 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001001 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 memset(&a, 0, sizeof(struct tc_action));
1005 a.ops = a_o;
1006
Eric W. Biederman15e47302012-09-07 20:12:54 +00001007 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001008 cb->nlh->nlmsg_type, sizeof(*t), 0);
1009 if (!nlh)
1010 goto out_module_put;
1011 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001013 t->tca__pad1 = 0;
1014 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001016 nest = nla_nest_start(skb, TCA_ACT_TAB);
1017 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001018 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1021 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001022 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001025 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 ret = skb->len;
1027 } else
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001028 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001030 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001031 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 nlh->nlmsg_flags |= NLM_F_MULTI;
1033 module_put(a_o->owner);
1034 return skb->len;
1035
David S. Miller8b00a532012-06-26 21:39:32 -07001036out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001038 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return skb->len;
1040}
1041
1042static int __init tc_action_init(void)
1043{
Greg Rosec7ac8672011-06-10 01:27:09 +00001044 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1045 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1046 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1047 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return 0;
1050}
1051
1052subsys_initcall(tc_action_init);