blob: 06e7c4a372451916b77d81d32d94e2a20d96a527 [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);
David S. Millere9ce1cd2006-08-21 23:54:55 -070072 ret = 1;
73 }
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 Cong1f747c22013-12-15 20:15:10 -0800305static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static DEFINE_RWLOCK(act_mod_lock);
307
WANG Cong4f1e9d82014-02-11 17:07:33 -0800308int tcf_register_action(struct tc_action_ops *act, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
WANG Cong1f747c22013-12-15 20:15:10 -0800310 struct tc_action_ops *a;
WANG Cong4f1e9d82014-02-11 17:07:33 -0800311 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
WANG Conga5b5c952014-02-11 17:07:32 -0800313 /* Must supply act, dump and init */
314 if (!act->act || !act->dump || !act->init)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500315 return -EINVAL;
316
Jamal Hadi Salim382ca8a2013-12-04 09:26:55 -0500317 /* Supply defaults */
Jamal Hadi Salim63ef6172013-12-04 09:26:53 -0500318 if (!act->lookup)
319 act->lookup = tcf_hash_search;
Jamal Hadi Salim382ca8a2013-12-04 09:26:55 -0500320 if (!act->walk)
321 act->walk = tcf_generic_walker;
Jamal Hadi Salim63ef6172013-12-04 09:26:53 -0500322
WANG Cong4f1e9d82014-02-11 17:07:33 -0800323 act->hinfo = kmalloc(sizeof(struct tcf_hashinfo), GFP_KERNEL);
324 if (!act->hinfo)
325 return -ENOMEM;
326 err = tcf_hashinfo_init(act->hinfo, mask);
327 if (err) {
328 kfree(act->hinfo);
329 return err;
330 }
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800333 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
335 write_unlock(&act_mod_lock);
WANG Cong4f1e9d82014-02-11 17:07:33 -0800336 tcf_hashinfo_destroy(act->hinfo);
337 kfree(act->hinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return -EEXIST;
339 }
340 }
WANG Cong1f747c22013-12-15 20:15:10 -0800341 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 write_unlock(&act_mod_lock);
343 return 0;
344}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800345EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347int tcf_unregister_action(struct tc_action_ops *act)
348{
WANG Cong1f747c22013-12-15 20:15:10 -0800349 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int err = -ENOENT;
351
352 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800353 list_for_each_entry(a, &act_base, head) {
354 if (a == act) {
355 list_del(&act->head);
WANG Cong4f1e9d82014-02-11 17:07:33 -0800356 tcf_hashinfo_destroy(act->hinfo);
357 kfree(act->hinfo);
Eric Dumazeta7928662013-12-20 12:32:32 -0800358 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362 write_unlock(&act_mod_lock);
363 return err;
364}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800365EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367/* lookup by name */
368static struct tc_action_ops *tc_lookup_action_n(char *kind)
369{
Eric Dumazeta7928662013-12-20 12:32:32 -0800370 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 if (kind) {
373 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800374 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800376 if (try_module_get(a->owner))
377 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 break;
379 }
380 }
381 read_unlock(&act_mod_lock);
382 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800383 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800386/* lookup by nlattr */
387static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Eric Dumazeta7928662013-12-20 12:32:32 -0800389 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 if (kind) {
392 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800393 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800394 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800395 if (try_module_get(a->owner))
396 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 break;
398 }
399 }
400 read_unlock(&act_mod_lock);
401 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800402 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
WANG Cong33be6272013-12-15 20:15:05 -0800405int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900406 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000408 const struct tc_action *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 int ret = -1;
410
411 if (skb->tc_verd & TC_NCLS) {
412 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 ret = TC_ACT_OK;
414 goto exec_done;
415 }
WANG Cong33be6272013-12-15 20:15:05 -0800416 list_for_each_entry(a, actions, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500418 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500419 if (ret == TC_ACT_REPEAT)
420 goto repeat; /* we need a ttl - JHS */
421 if (ret != TC_ACT_PIPE)
422 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return ret;
426}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800427EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
WANG Cong55334a52014-02-11 17:07:34 -0800429int tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
WANG Cong33be6272013-12-15 20:15:05 -0800431 struct tc_action *a, *tmp;
WANG Cong55334a52014-02-11 17:07:34 -0800432 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
WANG Cong33be6272013-12-15 20:15:05 -0800434 list_for_each_entry_safe(a, tmp, actions, list) {
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200435 ret = __tcf_hash_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800436 if (ret == ACT_P_DELETED)
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500437 module_put(a->ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800438 else if (ret < 0)
439 return ret;
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500440 list_del(&a->list);
441 kfree(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
WANG Cong55334a52014-02-11 17:07:34 -0800443 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446int
447tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
448{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return a->ops->dump(skb, a, bind, ref);
450}
451
452int
453tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
454{
455 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700456 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800457 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
David S. Miller1b34ec42012-03-29 05:11:39 -0400459 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
460 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800462 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800463 nest = nla_nest_start(skb, TCA_OPTIONS);
464 if (nest == NULL)
465 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000466 err = tcf_action_dump_old(skb, a, bind, ref);
467 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800468 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return err;
470 }
471
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800472nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700473 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return -1;
475}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800476EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478int
WANG Cong33be6272013-12-15 20:15:05 -0800479tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 struct tc_action *a;
482 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800483 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
WANG Cong33be6272013-12-15 20:15:05 -0800485 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800486 nest = nla_nest_start(skb, a->order);
487 if (nest == NULL)
488 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 err = tcf_action_dump_1(skb, a, bind, ref);
490 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700491 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800492 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494
495 return 0;
496
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800497nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700498 err = -EINVAL;
499errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800500 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700501 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000504struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
505 struct nlattr *est, char *name, int ovr,
506 int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 struct tc_action *a;
509 struct tc_action_ops *a_o;
510 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000511 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800512 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800513 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800516 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
517 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800519 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800520 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (kind == NULL)
522 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800523 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 goto err_out;
525 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800526 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
528 goto err_out;
529 }
530
531 a_o = tc_lookup_action_n(act_name);
532 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700533#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800535 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 rtnl_lock();
537
538 a_o = tc_lookup_action_n(act_name);
539
540 /* We dropped the RTNL semaphore in order to
541 * perform the module load. So, even if we
542 * succeeded in loading the module we have to
543 * tell the caller to replay the request. We
544 * indicate this using -EAGAIN.
545 */
546 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800547 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 goto err_mod;
549 }
550#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800551 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 goto err_out;
553 }
554
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800555 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700556 a = kzalloc(sizeof(*a), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (a == NULL)
558 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
WANG Congc779f7a2014-01-17 11:37:02 -0800560 a->ops = a_o;
WANG Cong33be6272013-12-15 20:15:05 -0800561 INIT_LIST_HEAD(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /* backward compatibility for policer */
563 if (name == NULL)
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000564 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 else
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000566 err = a_o->init(net, nla, est, a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800567 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 goto err_free;
569
570 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000571 * if it exists and is only bound to in a_o->init() then
572 * ACT_P_CREATED is not returned (a zero is).
573 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800574 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return a;
578
579err_free:
580 kfree(a);
581err_mod:
582 module_put(a_o->owner);
583err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800584 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
WANG Cong33be6272013-12-15 20:15:05 -0800587int tcf_action_init(struct net *net, struct nlattr *nla,
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000588 struct nlattr *est, char *name, int ovr,
WANG Cong33be6272013-12-15 20:15:05 -0800589 int bind, struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000591 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800592 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800593 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 int i;
595
Patrick McHardycee63722008-01-23 20:33:32 -0800596 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
597 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800598 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800600 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000601 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800602 if (IS_ERR(act)) {
603 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800605 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800606 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800607 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
WANG Cong33be6272013-12-15 20:15:05 -0800609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611err:
WANG Cong33be6272013-12-15 20:15:05 -0800612 tcf_action_destroy(actions, bind);
613 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
617 int compat_mode)
618{
619 int err = 0;
620 struct gnet_dump d;
WANG Cong7eb88962014-01-09 16:14:05 -0800621 struct tcf_common *p = a->priv;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900622
WANG Cong7eb88962014-01-09 16:14:05 -0800623 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 goto errout;
625
626 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400627 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 */
629 if (compat_mode) {
630 if (a->type == TCA_OLD_COMPAT)
631 err = gnet_stats_start_copy_compat(skb, 0,
WANG Cong7eb88962014-01-09 16:14:05 -0800632 TCA_STATS, TCA_XSTATS, &p->tcfc_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 else
634 return 0;
635 } else
636 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Cong7eb88962014-01-09 16:14:05 -0800637 &p->tcfc_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if (err < 0)
640 goto errout;
641
Eric Dumazet519c8182015-07-06 05:18:04 -0700642 if (gnet_stats_copy_basic(&d, p->cpu_bstats, &p->tcfc_bstats) < 0 ||
WANG Cong7eb88962014-01-09 16:14:05 -0800643 gnet_stats_copy_rate_est(&d, &p->tcfc_bstats,
644 &p->tcfc_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -0700645 gnet_stats_copy_queue(&d, p->cpu_qstats,
John Fastabend64015852014-09-28 11:53:57 -0700646 &p->tcfc_qstats,
647 p->tcfc_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 goto errout;
649
650 if (gnet_stats_finish_copy(&d) < 0)
651 goto errout;
652
653 return 0;
654
655errout:
656 return -1;
657}
658
659static int
WANG Cong33be6272013-12-15 20:15:05 -0800660tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900661 u16 flags, int event, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 struct tcamsg *t;
664 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700665 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800666 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Eric W. Biederman15e47302012-09-07 20:12:54 +0000668 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700669 if (!nlh)
670 goto out_nlmsg_trim;
671 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700673 t->tca__pad1 = 0;
674 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900675
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800676 nest = nla_nest_start(skb, TCA_ACT_TAB);
677 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700678 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
WANG Cong33be6272013-12-15 20:15:05 -0800680 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700681 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800683 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900684
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700685 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return skb->len;
687
David S. Miller8b00a532012-06-26 21:39:32 -0700688out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700689 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return -1;
691}
692
693static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000694act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800695 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
700 if (!skb)
701 return -ENOBUFS;
WANG Cong33be6272013-12-15 20:15:05 -0800702 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 kfree_skb(skb);
704 return -EINVAL;
705 }
Thomas Graf2942e902006-08-15 00:30:25 -0700706
Eric W. Biederman15e47302012-09-07 20:12:54 +0000707 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
WANG Cong03701d62014-02-11 17:07:35 -0800710static struct tc_action *create_a(int i)
711{
712 struct tc_action *act;
713
714 act = kzalloc(sizeof(*act), GFP_KERNEL);
715 if (act == NULL) {
716 pr_debug("create_a: failed to alloc!\n");
717 return NULL;
718 }
719 act->order = i;
720 INIT_LIST_HEAD(&act->list);
721 return act;
722}
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724static struct tc_action *
Eric W. Biederman15e47302012-09-07 20:12:54 +0000725tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000727 struct nlattr *tb[TCA_ACT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 struct tc_action *a;
729 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800730 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Patrick McHardycee63722008-01-23 20:33:32 -0800732 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
733 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800734 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Patrick McHardycee63722008-01-23 20:33:32 -0800736 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800737 if (tb[TCA_ACT_INDEX] == NULL ||
738 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800739 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800740 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800742 err = -ENOMEM;
WANG Cong03701d62014-02-11 17:07:35 -0800743 a = create_a(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (a == NULL)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800745 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800747 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800748 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500749 if (a->ops == NULL) /* could happen in batch of actions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 goto err_free;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800751 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (a->ops->lookup(a, index) == 0)
753 goto err_mod;
754
755 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758err_mod:
759 module_put(a->ops->owner);
760err_free:
761 kfree(a);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800762err_out:
763 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
WANG Cong33be6272013-12-15 20:15:05 -0800766static void cleanup_a(struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
WANG Cong33be6272013-12-15 20:15:05 -0800768 struct tc_action *a, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
WANG Cong33be6272013-12-15 20:15:05 -0800770 list_for_each_entry_safe(a, tmp, actions, list) {
771 list_del(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 kfree(a);
773 }
774}
775
Tom Goff7316ae82010-03-19 15:40:13 +0000776static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000777 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 struct sk_buff *skb;
780 unsigned char *b;
781 struct nlmsghdr *nlh;
782 struct tcamsg *t;
783 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800784 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000785 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800786 struct nlattr *kind;
WANG Cong03701d62014-02-11 17:07:35 -0800787 struct tc_action a;
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700788 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
791 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000792 pr_debug("tca_action_flush: failed skb alloc\n");
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700793 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700796 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Patrick McHardycee63722008-01-23 20:33:32 -0800798 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
799 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 goto err_out;
801
Patrick McHardycee63722008-01-23 20:33:32 -0800802 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800803 kind = tb[TCA_ACT_KIND];
WANG Cong03701d62014-02-11 17:07:35 -0800804 memset(&a, 0, sizeof(struct tc_action));
805 INIT_LIST_HEAD(&a.list);
806 a.ops = tc_lookup_action(kind);
807 if (a.ops == NULL) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 goto err_out;
809
Eric W. Biederman15e47302012-09-07 20:12:54 +0000810 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700811 if (!nlh)
812 goto out_module_put;
813 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700815 t->tca__pad1 = 0;
816 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800818 nest = nla_nest_start(skb, TCA_ACT_TAB);
819 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700820 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
WANG Cong03701d62014-02-11 17:07:35 -0800822 err = a.ops->walk(skb, &dcb, RTM_DELACTION, &a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (err < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700824 goto out_module_put;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700825 if (err == 0)
826 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800828 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700830 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Cong03701d62014-02-11 17:07:35 -0800832 module_put(a.ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000833 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000834 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (err > 0)
836 return 0;
837
838 return err;
839
David S. Miller8b00a532012-06-26 21:39:32 -0700840out_module_put:
WANG Cong03701d62014-02-11 17:07:35 -0800841 module_put(a.ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700843noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return err;
846}
847
848static int
WANG Conga56e1952014-01-09 16:14:00 -0800849tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
850 u32 portid)
851{
852 int ret;
853 struct sk_buff *skb;
854
855 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
856 if (!skb)
857 return -ENOBUFS;
858
859 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
860 0, 1) <= 0) {
861 kfree_skb(skb);
862 return -EINVAL;
863 }
864
865 /* now do the delete */
WANG Cong55334a52014-02-11 17:07:34 -0800866 ret = tcf_action_destroy(actions, 0);
867 if (ret < 0) {
868 kfree_skb(skb);
869 return ret;
870 }
WANG Conga56e1952014-01-09 16:14:00 -0800871
872 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
873 n->nlmsg_flags & NLM_F_ECHO);
874 if (ret > 0)
875 return 0;
876 return ret;
877}
878
879static int
Tom Goff7316ae82010-03-19 15:40:13 +0000880tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000881 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Patrick McHardycee63722008-01-23 20:33:32 -0800883 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000884 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800885 struct tc_action *act;
886 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Patrick McHardycee63722008-01-23 20:33:32 -0800888 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
889 if (ret < 0)
890 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000892 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700893 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000894 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700895 else
896 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800899 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000900 act = tcf_action_get_1(tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800901 if (IS_ERR(act)) {
902 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800904 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800905 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800906 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908
909 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800910 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -0800912 ret = tcf_del_notify(net, n, &actions, portid);
913 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return ret;
916 }
917err:
WANG Cong33be6272013-12-15 20:15:05 -0800918 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return ret;
920}
921
WANG Conga56e1952014-01-09 16:14:00 -0800922static int
923tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
924 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 int err = 0;
928
929 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
930 if (!skb)
931 return -ENOBUFS;
932
WANG Conga56e1952014-01-09 16:14:00 -0800933 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
934 RTM_NEWACTION, 0, 0) <= 0) {
935 kfree_skb(skb);
936 return -EINVAL;
937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
WANG Conga56e1952014-01-09 16:14:00 -0800939 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
940 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (err > 0)
942 err = 0;
943 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946static int
Tom Goff7316ae82010-03-19 15:40:13 +0000947tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000948 u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -0800951 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
WANG Cong33be6272013-12-15 20:15:05 -0800953 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
954 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 goto done;
956
957 /* dump then free all the actions after update; inserted policy
958 * stays intact
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000959 */
WANG Conga56e1952014-01-09 16:14:00 -0800960 ret = tcf_add_notify(net, n, &actions, portid);
WANG Cong33be6272013-12-15 20:15:05 -0800961 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962done:
963 return ret;
964}
965
Thomas Graf661d2962013-03-21 07:45:29 +0000966static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900968 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800969 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +0000970 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 int ret = 0, ovr = 0;
972
Eric W. Biederman90f62cf2014-04-23 14:29:27 -0700973 if ((n->nlmsg_type != RTM_GETACTION) && !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000974 return -EPERM;
975
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800976 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
977 if (ret < 0)
978 return ret;
979
980 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000981 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return -EINVAL;
983 }
984
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000985 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 switch (n->nlmsg_type) {
987 case RTM_NEWACTION:
988 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300989 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 * Note that CREATE | EXCL implies that
991 * but since we want avoid ambiguity (eg when flags
992 * is zero) then just set this
993 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000994 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 ovr = 1;
996replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000997 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (ret == -EAGAIN)
999 goto replay;
1000 break;
1001 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001002 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001003 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 break;
1005 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001006 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001007 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 break;
1009 default:
1010 BUG();
1011 }
1012
1013 return ret;
1014}
1015
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001016static struct nlattr *
Patrick McHardy3a6c2b42009-08-25 16:07:40 +02001017find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001019 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001020 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1021 struct nlattr *nla[TCAA_MAX + 1];
1022 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Patrick McHardyc96c9472008-01-23 20:32:58 -08001024 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001026 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (tb1 == NULL)
1028 return NULL;
1029
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001030 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1031 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Patrick McHardy6d834e02008-01-23 20:32:42 -08001034 if (tb[1] == NULL)
1035 return NULL;
1036 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1037 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001039 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Thomas Graf26dab892006-07-05 20:45:06 -07001041 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
1044static int
1045tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1046{
1047 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001048 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001049 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 struct tc_action_ops *a_o;
1051 struct tc_action a;
1052 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001053 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001054 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001057 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return 0;
1059 }
1060
Thomas Graf26dab892006-07-05 20:45:06 -07001061 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001062 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 memset(&a, 0, sizeof(struct tc_action));
1066 a.ops = a_o;
1067
Eric W. Biederman15e47302012-09-07 20:12:54 +00001068 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001069 cb->nlh->nlmsg_type, sizeof(*t), 0);
1070 if (!nlh)
1071 goto out_module_put;
1072 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001074 t->tca__pad1 = 0;
1075 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001077 nest = nla_nest_start(skb, TCA_ACT_TAB);
1078 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001079 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1082 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001083 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001086 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 ret = skb->len;
1088 } else
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001089 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001091 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001092 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 nlh->nlmsg_flags |= NLM_F_MULTI;
1094 module_put(a_o->owner);
1095 return skb->len;
1096
David S. Miller8b00a532012-06-26 21:39:32 -07001097out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001099 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return skb->len;
1101}
1102
1103static int __init tc_action_init(void)
1104{
Greg Rosec7ac8672011-06-10 01:27:09 +00001105 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1106 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1107 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1108 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return 0;
1111}
1112
1113subsys_initcall(tc_action_init);