blob: acafaf7434fc2267ddf7b8829d22f411591fdb00 [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
Eric Dumazet519c8182015-07-06 05:18:04 -070030static void free_tcf(struct rcu_head *head)
31{
32 struct tcf_common *p = container_of(head, struct tcf_common, tcfc_rcu);
33
34 free_percpu(p->cpu_bstats);
35 free_percpu(p->cpu_qstats);
36 kfree(p);
37}
38
Alexei Starovoitov3c645622015-08-25 20:06:31 -070039static void tcf_hash_destroy(struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -070040{
WANG Cong86062032014-02-11 17:07:31 -080041 struct tcf_common *p = a->priv;
42 struct tcf_hashinfo *hinfo = a->ops->hinfo;
43
WANG Cong89819dc2013-12-15 20:15:09 -080044 spin_lock_bh(&hinfo->lock);
45 hlist_del(&p->tcfc_head);
46 spin_unlock_bh(&hinfo->lock);
47 gen_kill_estimator(&p->tcfc_bstats,
48 &p->tcfc_rate_est);
49 /*
50 * gen_estimator est_timer() might access p->tcfc_lock
51 * or bstats, wait a RCU grace period before freeing p
52 */
Eric Dumazet519c8182015-07-06 05:18:04 -070053 call_rcu(&p->tcfc_rcu, free_tcf);
David S. Millere9ce1cd2006-08-21 23:54:55 -070054}
David S. Millere9ce1cd2006-08-21 23:54:55 -070055
Daniel Borkmann28e6b672015-07-29 23:35:25 +020056int __tcf_hash_release(struct tc_action *a, bool bind, bool strict)
David S. Millere9ce1cd2006-08-21 23:54:55 -070057{
WANG Cong86062032014-02-11 17:07:31 -080058 struct tcf_common *p = a->priv;
David S. Millere9ce1cd2006-08-21 23:54:55 -070059 int ret = 0;
60
61 if (p) {
62 if (bind)
63 p->tcfc_bindcnt--;
Daniel Borkmann28e6b672015-07-29 23:35:25 +020064 else if (strict && p->tcfc_bindcnt > 0)
WANG Cong55334a52014-02-11 17:07:34 -080065 return -EPERM;
David S. Millere9ce1cd2006-08-21 23:54:55 -070066
67 p->tcfc_refcnt--;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090068 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
WANG Conga5b5c952014-02-11 17:07:32 -080069 if (a->ops->cleanup)
70 a->ops->cleanup(a, bind);
WANG Cong86062032014-02-11 17:07:31 -080071 tcf_hash_destroy(a);
WANG Cong1d4150c2016-02-22 15:57:52 -080072 ret = ACT_P_DELETED;
David S. Millere9ce1cd2006-08-21 23:54:55 -070073 }
74 }
Daniel Borkmann28e6b672015-07-29 23:35:25 +020075
David S. Millere9ce1cd2006-08-21 23:54:55 -070076 return ret;
77}
Daniel Borkmann28e6b672015-07-29 23:35:25 +020078EXPORT_SYMBOL(__tcf_hash_release);
David S. Millere9ce1cd2006-08-21 23:54:55 -070079
80static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
WANG Congc779f7a2014-01-17 11:37:02 -080081 struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -070082{
WANG Congc779f7a2014-01-17 11:37:02 -080083 struct tcf_hashinfo *hinfo = a->ops->hinfo;
WANG Cong89819dc2013-12-15 20:15:09 -080084 struct hlist_head *head;
David S. Millere9ce1cd2006-08-21 23:54:55 -070085 struct tcf_common *p;
Eric Dumazetcc7ec452011-01-19 19:26:56 +000086 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080087 struct nlattr *nest;
David S. Millere9ce1cd2006-08-21 23:54:55 -070088
WANG Cong89819dc2013-12-15 20:15:09 -080089 spin_lock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -070090
91 s_i = cb->args[0];
92
93 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Cong89819dc2013-12-15 20:15:09 -080094 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
David S. Millere9ce1cd2006-08-21 23:54:55 -070095
WANG Cong89819dc2013-12-15 20:15:09 -080096 hlist_for_each_entry_rcu(p, head, tcfc_head) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070097 index++;
98 if (index < s_i)
99 continue;
100 a->priv = p;
101 a->order = n_i;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800102
103 nest = nla_nest_start(skb, a->order);
104 if (nest == NULL)
105 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700106 err = tcf_action_dump_1(skb, a, 0, 0);
107 if (err < 0) {
108 index--;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800109 nlmsg_trim(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700110 goto done;
111 }
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800112 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700113 n_i++;
114 if (n_i >= TCA_ACT_MAX_PRIO)
115 goto done;
116 }
117 }
118done:
WANG Cong89819dc2013-12-15 20:15:09 -0800119 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700120 if (n_i)
121 cb->args[0] += n_i;
122 return n_i;
123
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800124nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800125 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700126 goto done;
127}
128
WANG Congc779f7a2014-01-17 11:37:02 -0800129static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700130{
WANG Congc779f7a2014-01-17 11:37:02 -0800131 struct tcf_hashinfo *hinfo = a->ops->hinfo;
WANG Cong89819dc2013-12-15 20:15:09 -0800132 struct hlist_head *head;
133 struct hlist_node *n;
134 struct tcf_common *p;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800135 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000136 int i = 0, n_i = 0;
WANG Cong55334a52014-02-11 17:07:34 -0800137 int ret = -EINVAL;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700138
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800139 nest = nla_nest_start(skb, a->order);
140 if (nest == NULL)
141 goto nla_put_failure;
David S. Miller1b34ec42012-03-29 05:11:39 -0400142 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
143 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700144 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Cong89819dc2013-12-15 20:15:09 -0800145 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
146 hlist_for_each_entry_safe(p, n, head, tcfc_head) {
WANG Cong86062032014-02-11 17:07:31 -0800147 a->priv = p;
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200148 ret = __tcf_hash_release(a, false, true);
WANG Cong55334a52014-02-11 17:07:34 -0800149 if (ret == ACT_P_DELETED) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000150 module_put(a->ops->owner);
Jamal Hadi Salim805c1f42013-12-23 08:02:13 -0500151 n_i++;
WANG Cong55334a52014-02-11 17:07:34 -0800152 } else if (ret < 0)
153 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700154 }
155 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400156 if (nla_put_u32(skb, TCA_FCNT, n_i))
157 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800158 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700159
160 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800161nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800162 nla_nest_cancel(skb, nest);
WANG Cong55334a52014-02-11 17:07:34 -0800163 return ret;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700164}
165
stephen hemminger9c75f402013-12-31 11:54:00 -0800166static int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
167 int type, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700168{
David S. Millere9ce1cd2006-08-21 23:54:55 -0700169 if (type == RTM_DELACTION) {
WANG Congc779f7a2014-01-17 11:37:02 -0800170 return tcf_del_walker(skb, a);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700171 } else if (type == RTM_GETACTION) {
WANG Congc779f7a2014-01-17 11:37:02 -0800172 return tcf_dump_walker(skb, cb, a);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700173 } else {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000174 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700175 return -EINVAL;
176 }
177}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700178
WANG Cong6e6a50c2014-01-17 11:37:03 -0800179static struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700180{
WANG Cong89819dc2013-12-15 20:15:09 -0800181 struct tcf_common *p = NULL;
182 struct hlist_head *head;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700183
WANG Cong89819dc2013-12-15 20:15:09 -0800184 spin_lock_bh(&hinfo->lock);
185 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
186 hlist_for_each_entry_rcu(p, head, tcfc_head)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700187 if (p->tcfc_index == index)
188 break;
WANG Cong89819dc2013-12-15 20:15:09 -0800189 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700190
191 return p;
192}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700193
WANG Congddafd342014-01-09 16:13:59 -0800194u32 tcf_hash_new_index(struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700195{
WANG Congddafd342014-01-09 16:13:59 -0800196 u32 val = hinfo->index;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700197
198 do {
199 if (++val == 0)
200 val = 1;
201 } while (tcf_hash_lookup(val, hinfo));
202
WANG Congddafd342014-01-09 16:13:59 -0800203 hinfo->index = val;
Yang Yingliang17569fa2013-12-10 20:55:29 +0800204 return val;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700205}
206EXPORT_SYMBOL(tcf_hash_new_index);
207
WANG Cong6e6a50c2014-01-17 11:37:03 -0800208int tcf_hash_search(struct tc_action *a, u32 index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700209{
210 struct tcf_hashinfo *hinfo = a->ops->hinfo;
211 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
212
213 if (p) {
214 a->priv = p;
215 return 1;
216 }
217 return 0;
218}
WANG Cong6e6a50c2014-01-17 11:37:03 -0800219EXPORT_SYMBOL(tcf_hash_search);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700220
WANG Cong86062032014-02-11 17:07:31 -0800221int tcf_hash_check(u32 index, struct tc_action *a, int bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700222{
WANG Congc779f7a2014-01-17 11:37:02 -0800223 struct tcf_hashinfo *hinfo = a->ops->hinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700224 struct tcf_common *p = NULL;
225 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700226 if (bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700227 p->tcfc_bindcnt++;
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700228 p->tcfc_refcnt++;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700229 a->priv = p;
WANG Cong86062032014-02-11 17:07:31 -0800230 return 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700231 }
WANG Cong86062032014-02-11 17:07:31 -0800232 return 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700233}
234EXPORT_SYMBOL(tcf_hash_check);
235
WANG Cong86062032014-02-11 17:07:31 -0800236void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
237{
238 struct tcf_common *pc = a->priv;
239 if (est)
240 gen_kill_estimator(&pc->tcfc_bstats,
241 &pc->tcfc_rate_est);
Eric Dumazet519c8182015-07-06 05:18:04 -0700242 call_rcu(&pc->tcfc_rcu, free_tcf);
WANG Cong86062032014-02-11 17:07:31 -0800243}
244EXPORT_SYMBOL(tcf_hash_cleanup);
245
246int tcf_hash_create(u32 index, struct nlattr *est, struct tc_action *a,
Eric Dumazet519c8182015-07-06 05:18:04 -0700247 int size, int bind, bool cpustats)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700248{
WANG Congc779f7a2014-01-17 11:37:02 -0800249 struct tcf_hashinfo *hinfo = a->ops->hinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700250 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
Eric Dumazet519c8182015-07-06 05:18:04 -0700251 int err = -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700252
253 if (unlikely(!p))
WANG Cong86062032014-02-11 17:07:31 -0800254 return -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700255 p->tcfc_refcnt = 1;
256 if (bind)
257 p->tcfc_bindcnt = 1;
258
Eric Dumazet519c8182015-07-06 05:18:04 -0700259 if (cpustats) {
260 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
261 if (!p->cpu_bstats) {
262err1:
263 kfree(p);
264 return err;
265 }
266 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
267 if (!p->cpu_qstats) {
268err2:
269 free_percpu(p->cpu_bstats);
270 goto err1;
271 }
272 }
David S. Millere9ce1cd2006-08-21 23:54:55 -0700273 spin_lock_init(&p->tcfc_lock);
WANG Cong89819dc2013-12-15 20:15:09 -0800274 INIT_HLIST_NODE(&p->tcfc_head);
WANG Congddafd342014-01-09 16:13:59 -0800275 p->tcfc_index = index ? index : tcf_hash_new_index(hinfo);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700276 p->tcfc_tm.install = jiffies;
277 p->tcfc_tm.lastuse = jiffies;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800278 if (est) {
Eric Dumazet519c8182015-07-06 05:18:04 -0700279 err = gen_new_estimator(&p->tcfc_bstats, p->cpu_bstats,
280 &p->tcfc_rate_est,
281 &p->tcfc_lock, est);
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800282 if (err) {
Eric Dumazet519c8182015-07-06 05:18:04 -0700283 free_percpu(p->cpu_qstats);
284 goto err2;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800285 }
286 }
287
David S. Millere9ce1cd2006-08-21 23:54:55 -0700288 a->priv = (void *) p;
WANG Cong86062032014-02-11 17:07:31 -0800289 return 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700290}
291EXPORT_SYMBOL(tcf_hash_create);
292
WANG Cong86062032014-02-11 17:07:31 -0800293void tcf_hash_insert(struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700294{
WANG Cong86062032014-02-11 17:07:31 -0800295 struct tcf_common *p = a->priv;
296 struct tcf_hashinfo *hinfo = a->ops->hinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700297 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
298
WANG Cong89819dc2013-12-15 20:15:09 -0800299 spin_lock_bh(&hinfo->lock);
300 hlist_add_head(&p->tcfc_head, &hinfo->htab[h]);
301 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700302}
303EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
WANG Cong1d4150c2016-02-22 15:57:52 -0800305static void tcf_hashinfo_destroy(const struct tc_action_ops *ops)
306{
307 struct tcf_hashinfo *hinfo = ops->hinfo;
308 struct tc_action a = {
309 .ops = ops,
310 };
311 int i;
312
313 for (i = 0; i < hinfo->hmask + 1; i++) {
314 struct tcf_common *p;
315 struct hlist_node *n;
316
317 hlist_for_each_entry_safe(p, n, &hinfo->htab[i], tcfc_head) {
318 int ret;
319
320 a.priv = p;
321 ret = __tcf_hash_release(&a, false, true);
322 if (ret == ACT_P_DELETED)
323 module_put(ops->owner);
324 else if (ret < 0)
325 return;
326 }
327 }
328 kfree(hinfo->htab);
329}
330
WANG Cong1f747c22013-12-15 20:15:10 -0800331static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static DEFINE_RWLOCK(act_mod_lock);
333
WANG Cong4f1e9d82014-02-11 17:07:33 -0800334int tcf_register_action(struct tc_action_ops *act, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
WANG Cong1f747c22013-12-15 20:15:10 -0800336 struct tc_action_ops *a;
WANG Cong4f1e9d82014-02-11 17:07:33 -0800337 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
WANG Conga5b5c952014-02-11 17:07:32 -0800339 /* Must supply act, dump and init */
340 if (!act->act || !act->dump || !act->init)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500341 return -EINVAL;
342
Jamal Hadi Salim382ca8a2013-12-04 09:26:55 -0500343 /* Supply defaults */
Jamal Hadi Salim63ef6172013-12-04 09:26:53 -0500344 if (!act->lookup)
345 act->lookup = tcf_hash_search;
Jamal Hadi Salim382ca8a2013-12-04 09:26:55 -0500346 if (!act->walk)
347 act->walk = tcf_generic_walker;
Jamal Hadi Salim63ef6172013-12-04 09:26:53 -0500348
WANG Cong4f1e9d82014-02-11 17:07:33 -0800349 act->hinfo = kmalloc(sizeof(struct tcf_hashinfo), GFP_KERNEL);
350 if (!act->hinfo)
351 return -ENOMEM;
352 err = tcf_hashinfo_init(act->hinfo, mask);
353 if (err) {
354 kfree(act->hinfo);
355 return err;
356 }
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800359 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
361 write_unlock(&act_mod_lock);
WANG Cong1d4150c2016-02-22 15:57:52 -0800362 tcf_hashinfo_destroy(act);
WANG Cong4f1e9d82014-02-11 17:07:33 -0800363 kfree(act->hinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return -EEXIST;
365 }
366 }
WANG Cong1f747c22013-12-15 20:15:10 -0800367 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 write_unlock(&act_mod_lock);
369 return 0;
370}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800371EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373int tcf_unregister_action(struct tc_action_ops *act)
374{
WANG Cong1f747c22013-12-15 20:15:10 -0800375 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 int err = -ENOENT;
377
378 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800379 list_for_each_entry(a, &act_base, head) {
380 if (a == act) {
381 list_del(&act->head);
WANG Cong1d4150c2016-02-22 15:57:52 -0800382 tcf_hashinfo_destroy(act);
WANG Cong4f1e9d82014-02-11 17:07:33 -0800383 kfree(act->hinfo);
Eric Dumazeta7928662013-12-20 12:32:32 -0800384 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388 write_unlock(&act_mod_lock);
389 return err;
390}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800391EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393/* lookup by name */
394static struct tc_action_ops *tc_lookup_action_n(char *kind)
395{
Eric Dumazeta7928662013-12-20 12:32:32 -0800396 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 if (kind) {
399 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800400 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800402 if (try_module_get(a->owner))
403 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 break;
405 }
406 }
407 read_unlock(&act_mod_lock);
408 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800409 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800412/* lookup by nlattr */
413static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Eric Dumazeta7928662013-12-20 12:32:32 -0800415 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 if (kind) {
418 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800419 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800420 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800421 if (try_module_get(a->owner))
422 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 break;
424 }
425 }
426 read_unlock(&act_mod_lock);
427 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800428 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
WANG Cong33be6272013-12-15 20:15:05 -0800431int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900432 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000434 const struct tc_action *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 int ret = -1;
436
437 if (skb->tc_verd & TC_NCLS) {
438 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 ret = TC_ACT_OK;
440 goto exec_done;
441 }
WANG Cong33be6272013-12-15 20:15:05 -0800442 list_for_each_entry(a, actions, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500444 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500445 if (ret == TC_ACT_REPEAT)
446 goto repeat; /* we need a ttl - JHS */
447 if (ret != TC_ACT_PIPE)
448 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
450exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return ret;
452}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800453EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
WANG Cong55334a52014-02-11 17:07:34 -0800455int tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
WANG Cong33be6272013-12-15 20:15:05 -0800457 struct tc_action *a, *tmp;
WANG Cong55334a52014-02-11 17:07:34 -0800458 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
WANG Cong33be6272013-12-15 20:15:05 -0800460 list_for_each_entry_safe(a, tmp, actions, list) {
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200461 ret = __tcf_hash_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800462 if (ret == ACT_P_DELETED)
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500463 module_put(a->ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800464 else if (ret < 0)
465 return ret;
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500466 list_del(&a->list);
467 kfree(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
WANG Cong55334a52014-02-11 17:07:34 -0800469 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
472int
473tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
474{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return a->ops->dump(skb, a, bind, ref);
476}
477
478int
479tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
480{
481 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700482 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800483 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
David S. Miller1b34ec42012-03-29 05:11:39 -0400485 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
486 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800488 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800489 nest = nla_nest_start(skb, TCA_OPTIONS);
490 if (nest == NULL)
491 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000492 err = tcf_action_dump_old(skb, a, bind, ref);
493 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800494 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return err;
496 }
497
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800498nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700499 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return -1;
501}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800502EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504int
WANG Cong33be6272013-12-15 20:15:05 -0800505tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 struct tc_action *a;
508 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800509 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
WANG Cong33be6272013-12-15 20:15:05 -0800511 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800512 nest = nla_nest_start(skb, a->order);
513 if (nest == NULL)
514 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 err = tcf_action_dump_1(skb, a, bind, ref);
516 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700517 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800518 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520
521 return 0;
522
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800523nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700524 err = -EINVAL;
525errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800526 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700527 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000530struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
531 struct nlattr *est, char *name, int ovr,
532 int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 struct tc_action *a;
535 struct tc_action_ops *a_o;
536 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000537 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800538 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800539 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800542 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
543 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800545 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800546 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (kind == NULL)
548 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800549 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 goto err_out;
551 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800552 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
554 goto err_out;
555 }
556
557 a_o = tc_lookup_action_n(act_name);
558 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700559#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800561 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 rtnl_lock();
563
564 a_o = tc_lookup_action_n(act_name);
565
566 /* We dropped the RTNL semaphore in order to
567 * perform the module load. So, even if we
568 * succeeded in loading the module we have to
569 * tell the caller to replay the request. We
570 * indicate this using -EAGAIN.
571 */
572 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800573 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 goto err_mod;
575 }
576#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800577 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 goto err_out;
579 }
580
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800581 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700582 a = kzalloc(sizeof(*a), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (a == NULL)
584 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
WANG Congc779f7a2014-01-17 11:37:02 -0800586 a->ops = a_o;
WANG Cong33be6272013-12-15 20:15:05 -0800587 INIT_LIST_HEAD(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /* backward compatibility for policer */
589 if (name == NULL)
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000590 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 else
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000592 err = a_o->init(net, nla, est, a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800593 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 goto err_free;
595
596 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000597 * if it exists and is only bound to in a_o->init() then
598 * ACT_P_CREATED is not returned (a zero is).
599 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800600 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return a;
604
605err_free:
606 kfree(a);
607err_mod:
608 module_put(a_o->owner);
609err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800610 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
WANG Cong33be6272013-12-15 20:15:05 -0800613int tcf_action_init(struct net *net, struct nlattr *nla,
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000614 struct nlattr *est, char *name, int ovr,
WANG Cong33be6272013-12-15 20:15:05 -0800615 int bind, struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000617 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800618 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800619 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 int i;
621
Patrick McHardycee63722008-01-23 20:33:32 -0800622 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
623 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800624 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800626 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000627 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800628 if (IS_ERR(act)) {
629 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800631 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800632 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800633 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
WANG Cong33be6272013-12-15 20:15:05 -0800635 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637err:
WANG Cong33be6272013-12-15 20:15:05 -0800638 tcf_action_destroy(actions, bind);
639 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
643 int compat_mode)
644{
645 int err = 0;
646 struct gnet_dump d;
WANG Cong7eb88962014-01-09 16:14:05 -0800647 struct tcf_common *p = a->priv;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900648
WANG Cong7eb88962014-01-09 16:14:05 -0800649 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 goto errout;
651
652 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400653 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 */
655 if (compat_mode) {
656 if (a->type == TCA_OLD_COMPAT)
657 err = gnet_stats_start_copy_compat(skb, 0,
WANG Cong7eb88962014-01-09 16:14:05 -0800658 TCA_STATS, TCA_XSTATS, &p->tcfc_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 else
660 return 0;
661 } else
662 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Cong7eb88962014-01-09 16:14:05 -0800663 &p->tcfc_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 if (err < 0)
666 goto errout;
667
Eric Dumazet519c8182015-07-06 05:18:04 -0700668 if (gnet_stats_copy_basic(&d, p->cpu_bstats, &p->tcfc_bstats) < 0 ||
WANG Cong7eb88962014-01-09 16:14:05 -0800669 gnet_stats_copy_rate_est(&d, &p->tcfc_bstats,
670 &p->tcfc_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -0700671 gnet_stats_copy_queue(&d, p->cpu_qstats,
John Fastabend64015852014-09-28 11:53:57 -0700672 &p->tcfc_qstats,
673 p->tcfc_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 goto errout;
675
676 if (gnet_stats_finish_copy(&d) < 0)
677 goto errout;
678
679 return 0;
680
681errout:
682 return -1;
683}
684
685static int
WANG Cong33be6272013-12-15 20:15:05 -0800686tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900687 u16 flags, int event, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 struct tcamsg *t;
690 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700691 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800692 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Eric W. Biederman15e47302012-09-07 20:12:54 +0000694 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700695 if (!nlh)
696 goto out_nlmsg_trim;
697 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700699 t->tca__pad1 = 0;
700 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900701
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800702 nest = nla_nest_start(skb, TCA_ACT_TAB);
703 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700704 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
WANG Cong33be6272013-12-15 20:15:05 -0800706 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700707 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800709 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900710
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700711 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return skb->len;
713
David S. Miller8b00a532012-06-26 21:39:32 -0700714out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700715 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return -1;
717}
718
719static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000720act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800721 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
726 if (!skb)
727 return -ENOBUFS;
WANG Cong33be6272013-12-15 20:15:05 -0800728 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 kfree_skb(skb);
730 return -EINVAL;
731 }
Thomas Graf2942e902006-08-15 00:30:25 -0700732
Eric W. Biederman15e47302012-09-07 20:12:54 +0000733 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
WANG Cong03701d62014-02-11 17:07:35 -0800736static struct tc_action *create_a(int i)
737{
738 struct tc_action *act;
739
740 act = kzalloc(sizeof(*act), GFP_KERNEL);
741 if (act == NULL) {
742 pr_debug("create_a: failed to alloc!\n");
743 return NULL;
744 }
745 act->order = i;
746 INIT_LIST_HEAD(&act->list);
747 return act;
748}
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750static struct tc_action *
Eric W. Biederman15e47302012-09-07 20:12:54 +0000751tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000753 struct nlattr *tb[TCA_ACT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 struct tc_action *a;
755 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800756 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Patrick McHardycee63722008-01-23 20:33:32 -0800758 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
759 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800760 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Patrick McHardycee63722008-01-23 20:33:32 -0800762 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800763 if (tb[TCA_ACT_INDEX] == NULL ||
764 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800765 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800766 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800768 err = -ENOMEM;
WANG Cong03701d62014-02-11 17:07:35 -0800769 a = create_a(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (a == NULL)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800771 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800773 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800774 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500775 if (a->ops == NULL) /* could happen in batch of actions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 goto err_free;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800777 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (a->ops->lookup(a, index) == 0)
779 goto err_mod;
780
781 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784err_mod:
785 module_put(a->ops->owner);
786err_free:
787 kfree(a);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800788err_out:
789 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
WANG Cong33be6272013-12-15 20:15:05 -0800792static void cleanup_a(struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
WANG Cong33be6272013-12-15 20:15:05 -0800794 struct tc_action *a, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
WANG Cong33be6272013-12-15 20:15:05 -0800796 list_for_each_entry_safe(a, tmp, actions, list) {
797 list_del(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 kfree(a);
799 }
800}
801
Tom Goff7316ae82010-03-19 15:40:13 +0000802static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000803 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 struct sk_buff *skb;
806 unsigned char *b;
807 struct nlmsghdr *nlh;
808 struct tcamsg *t;
809 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800810 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000811 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800812 struct nlattr *kind;
WANG Cong03701d62014-02-11 17:07:35 -0800813 struct tc_action a;
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700814 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
817 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000818 pr_debug("tca_action_flush: failed skb alloc\n");
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700819 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700822 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Patrick McHardycee63722008-01-23 20:33:32 -0800824 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
825 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 goto err_out;
827
Patrick McHardycee63722008-01-23 20:33:32 -0800828 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800829 kind = tb[TCA_ACT_KIND];
WANG Cong03701d62014-02-11 17:07:35 -0800830 memset(&a, 0, sizeof(struct tc_action));
831 INIT_LIST_HEAD(&a.list);
832 a.ops = tc_lookup_action(kind);
833 if (a.ops == NULL) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 goto err_out;
835
Eric W. Biederman15e47302012-09-07 20:12:54 +0000836 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700837 if (!nlh)
838 goto out_module_put;
839 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700841 t->tca__pad1 = 0;
842 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800844 nest = nla_nest_start(skb, TCA_ACT_TAB);
845 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700846 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
WANG Cong03701d62014-02-11 17:07:35 -0800848 err = a.ops->walk(skb, &dcb, RTM_DELACTION, &a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (err < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700850 goto out_module_put;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700851 if (err == 0)
852 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800854 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700856 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Cong03701d62014-02-11 17:07:35 -0800858 module_put(a.ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000859 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000860 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (err > 0)
862 return 0;
863
864 return err;
865
David S. Miller8b00a532012-06-26 21:39:32 -0700866out_module_put:
WANG Cong03701d62014-02-11 17:07:35 -0800867 module_put(a.ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700869noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return err;
872}
873
874static int
WANG Conga56e1952014-01-09 16:14:00 -0800875tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
876 u32 portid)
877{
878 int ret;
879 struct sk_buff *skb;
880
881 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
882 if (!skb)
883 return -ENOBUFS;
884
885 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
886 0, 1) <= 0) {
887 kfree_skb(skb);
888 return -EINVAL;
889 }
890
891 /* now do the delete */
WANG Cong55334a52014-02-11 17:07:34 -0800892 ret = tcf_action_destroy(actions, 0);
893 if (ret < 0) {
894 kfree_skb(skb);
895 return ret;
896 }
WANG Conga56e1952014-01-09 16:14:00 -0800897
898 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
899 n->nlmsg_flags & NLM_F_ECHO);
900 if (ret > 0)
901 return 0;
902 return ret;
903}
904
905static int
Tom Goff7316ae82010-03-19 15:40:13 +0000906tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000907 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Patrick McHardycee63722008-01-23 20:33:32 -0800909 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000910 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800911 struct tc_action *act;
912 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Patrick McHardycee63722008-01-23 20:33:32 -0800914 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
915 if (ret < 0)
916 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000918 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700919 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000920 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700921 else
922 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800925 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000926 act = tcf_action_get_1(tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800927 if (IS_ERR(act)) {
928 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800930 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800931 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800932 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934
935 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800936 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -0800938 ret = tcf_del_notify(net, n, &actions, portid);
939 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return ret;
942 }
943err:
WANG Cong33be6272013-12-15 20:15:05 -0800944 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return ret;
946}
947
WANG Conga56e1952014-01-09 16:14:00 -0800948static int
949tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
950 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 int err = 0;
954
955 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
956 if (!skb)
957 return -ENOBUFS;
958
WANG Conga56e1952014-01-09 16:14:00 -0800959 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
960 RTM_NEWACTION, 0, 0) <= 0) {
961 kfree_skb(skb);
962 return -EINVAL;
963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
WANG Conga56e1952014-01-09 16:14:00 -0800965 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
966 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (err > 0)
968 err = 0;
969 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972static int
Tom Goff7316ae82010-03-19 15:40:13 +0000973tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000974 u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -0800977 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
WANG Cong33be6272013-12-15 20:15:05 -0800979 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
980 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 goto done;
982
983 /* dump then free all the actions after update; inserted policy
984 * stays intact
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000985 */
WANG Conga56e1952014-01-09 16:14:00 -0800986 ret = tcf_add_notify(net, n, &actions, portid);
WANG Cong33be6272013-12-15 20:15:05 -0800987 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988done:
989 return ret;
990}
991
Thomas Graf661d2962013-03-21 07:45:29 +0000992static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900994 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800995 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +0000996 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 int ret = 0, ovr = 0;
998
Eric W. Biederman90f62cf2014-04-23 14:29:27 -0700999 if ((n->nlmsg_type != RTM_GETACTION) && !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001000 return -EPERM;
1001
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001002 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
1003 if (ret < 0)
1004 return ret;
1005
1006 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001007 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return -EINVAL;
1009 }
1010
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001011 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 switch (n->nlmsg_type) {
1013 case RTM_NEWACTION:
1014 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001015 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 * Note that CREATE | EXCL implies that
1017 * but since we want avoid ambiguity (eg when flags
1018 * is zero) then just set this
1019 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001020 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 ovr = 1;
1022replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +00001023 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (ret == -EAGAIN)
1025 goto replay;
1026 break;
1027 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001028 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001029 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 break;
1031 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001032 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001033 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 break;
1035 default:
1036 BUG();
1037 }
1038
1039 return ret;
1040}
1041
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001042static struct nlattr *
Patrick McHardy3a6c2b42009-08-25 16:07:40 +02001043find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001045 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001046 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1047 struct nlattr *nla[TCAA_MAX + 1];
1048 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Patrick McHardyc96c9472008-01-23 20:32:58 -08001050 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001052 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (tb1 == NULL)
1054 return NULL;
1055
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001056 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1057 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Patrick McHardy6d834e02008-01-23 20:32:42 -08001060 if (tb[1] == NULL)
1061 return NULL;
1062 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1063 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001065 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Thomas Graf26dab892006-07-05 20:45:06 -07001067 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
1070static int
1071tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1072{
1073 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001074 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001075 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 struct tc_action_ops *a_o;
1077 struct tc_action a;
1078 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001079 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001080 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001083 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return 0;
1085 }
1086
Thomas Graf26dab892006-07-05 20:45:06 -07001087 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001088 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 memset(&a, 0, sizeof(struct tc_action));
1092 a.ops = a_o;
1093
Eric W. Biederman15e47302012-09-07 20:12:54 +00001094 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001095 cb->nlh->nlmsg_type, sizeof(*t), 0);
1096 if (!nlh)
1097 goto out_module_put;
1098 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001100 t->tca__pad1 = 0;
1101 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001103 nest = nla_nest_start(skb, TCA_ACT_TAB);
1104 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001105 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1108 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001109 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001112 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 ret = skb->len;
1114 } else
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001115 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001117 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001118 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 nlh->nlmsg_flags |= NLM_F_MULTI;
1120 module_put(a_o->owner);
1121 return skb->len;
1122
David S. Miller8b00a532012-06-26 21:39:32 -07001123out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001125 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return skb->len;
1127}
1128
1129static int __init tc_action_init(void)
1130{
Greg Rosec7ac8672011-06-10 01:27:09 +00001131 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1132 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1133 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1134 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return 0;
1137}
1138
1139subsys_initcall(tc_action_init);