blob: d09d0687594b0b5ae04ad54a2b937044522bfbea [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{
WANG Congec0595c2016-07-25 16:09:42 -070032 struct tc_action *p = container_of(head, struct tc_action, tcfa_rcu);
Eric Dumazet519c8182015-07-06 05:18:04 -070033
34 free_percpu(p->cpu_bstats);
35 free_percpu(p->cpu_qstats);
36 kfree(p);
37}
38
WANG Congec0595c2016-07-25 16:09:42 -070039static void tcf_hash_destroy(struct tcf_hashinfo *hinfo, struct tc_action *p)
David S. Millere9ce1cd2006-08-21 23:54:55 -070040{
WANG Cong89819dc2013-12-15 20:15:09 -080041 spin_lock_bh(&hinfo->lock);
WANG Congec0595c2016-07-25 16:09:42 -070042 hlist_del(&p->tcfa_head);
WANG Cong89819dc2013-12-15 20:15:09 -080043 spin_unlock_bh(&hinfo->lock);
WANG Congec0595c2016-07-25 16:09:42 -070044 gen_kill_estimator(&p->tcfa_bstats,
45 &p->tcfa_rate_est);
WANG Cong89819dc2013-12-15 20:15:09 -080046 /*
WANG Congec0595c2016-07-25 16:09:42 -070047 * gen_estimator est_timer() might access p->tcfa_lock
WANG Cong89819dc2013-12-15 20:15:09 -080048 * or bstats, wait a RCU grace period before freeing p
49 */
WANG Congec0595c2016-07-25 16:09:42 -070050 call_rcu(&p->tcfa_rcu, free_tcf);
David S. Millere9ce1cd2006-08-21 23:54:55 -070051}
David S. Millere9ce1cd2006-08-21 23:54:55 -070052
WANG Congec0595c2016-07-25 16:09:42 -070053int __tcf_hash_release(struct tc_action *p, bool bind, bool strict)
David S. Millere9ce1cd2006-08-21 23:54:55 -070054{
55 int ret = 0;
56
57 if (p) {
58 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -070059 p->tcfa_bindcnt--;
60 else if (strict && p->tcfa_bindcnt > 0)
WANG Cong55334a52014-02-11 17:07:34 -080061 return -EPERM;
David S. Millere9ce1cd2006-08-21 23:54:55 -070062
WANG Congec0595c2016-07-25 16:09:42 -070063 p->tcfa_refcnt--;
64 if (p->tcfa_bindcnt <= 0 && p->tcfa_refcnt <= 0) {
65 if (p->ops->cleanup)
66 p->ops->cleanup(p, bind);
WANG Congec0595c2016-07-25 16:09:42 -070067 tcf_hash_destroy(p->hinfo, p);
WANG Cong1d4150c2016-02-22 15:57:52 -080068 ret = ACT_P_DELETED;
David S. Millere9ce1cd2006-08-21 23:54:55 -070069 }
70 }
Daniel Borkmann28e6b672015-07-29 23:35:25 +020071
David S. Millere9ce1cd2006-08-21 23:54:55 -070072 return ret;
73}
Daniel Borkmann28e6b672015-07-29 23:35:25 +020074EXPORT_SYMBOL(__tcf_hash_release);
David S. Millere9ce1cd2006-08-21 23:54:55 -070075
WANG Congddf97cc2016-02-22 15:57:53 -080076static int tcf_dump_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -070077 struct netlink_callback *cb)
David S. Millere9ce1cd2006-08-21 23:54:55 -070078{
Eric Dumazetcc7ec452011-01-19 19:26:56 +000079 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080080 struct nlattr *nest;
David S. Millere9ce1cd2006-08-21 23:54:55 -070081
WANG Cong89819dc2013-12-15 20:15:09 -080082 spin_lock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -070083
84 s_i = cb->args[0];
85
86 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Conga85a9702016-07-25 16:09:41 -070087 struct hlist_head *head;
WANG Congec0595c2016-07-25 16:09:42 -070088 struct tc_action *p;
WANG Conga85a9702016-07-25 16:09:41 -070089
WANG Cong89819dc2013-12-15 20:15:09 -080090 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
David S. Millere9ce1cd2006-08-21 23:54:55 -070091
WANG Congec0595c2016-07-25 16:09:42 -070092 hlist_for_each_entry_rcu(p, head, tcfa_head) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070093 index++;
94 if (index < s_i)
95 continue;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080096
WANG Conga85a9702016-07-25 16:09:41 -070097 nest = nla_nest_start(skb, n_i);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080098 if (nest == NULL)
99 goto nla_put_failure;
WANG Congec0595c2016-07-25 16:09:42 -0700100 err = tcf_action_dump_1(skb, p, 0, 0);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700101 if (err < 0) {
102 index--;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800103 nlmsg_trim(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700104 goto done;
105 }
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800106 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700107 n_i++;
108 if (n_i >= TCA_ACT_MAX_PRIO)
109 goto done;
110 }
111 }
112done:
WANG Cong89819dc2013-12-15 20:15:09 -0800113 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700114 if (n_i)
115 cb->args[0] += n_i;
116 return n_i;
117
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800118nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800119 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700120 goto done;
121}
122
WANG Congddf97cc2016-02-22 15:57:53 -0800123static int tcf_del_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700124 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700125{
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800126 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000127 int i = 0, n_i = 0;
WANG Cong55334a52014-02-11 17:07:34 -0800128 int ret = -EINVAL;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700129
WANG Conga85a9702016-07-25 16:09:41 -0700130 nest = nla_nest_start(skb, 0);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800131 if (nest == NULL)
132 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700133 if (nla_put_string(skb, TCA_KIND, ops->kind))
David S. Miller1b34ec42012-03-29 05:11:39 -0400134 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700135 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Conga85a9702016-07-25 16:09:41 -0700136 struct hlist_head *head;
137 struct hlist_node *n;
WANG Congec0595c2016-07-25 16:09:42 -0700138 struct tc_action *p;
WANG Conga85a9702016-07-25 16:09:41 -0700139
WANG Cong89819dc2013-12-15 20:15:09 -0800140 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
WANG Congec0595c2016-07-25 16:09:42 -0700141 hlist_for_each_entry_safe(p, n, head, tcfa_head) {
142 ret = __tcf_hash_release(p, false, true);
WANG Cong55334a52014-02-11 17:07:34 -0800143 if (ret == ACT_P_DELETED) {
WANG Congec0595c2016-07-25 16:09:42 -0700144 module_put(p->ops->owner);
Jamal Hadi Salim805c1f42013-12-23 08:02:13 -0500145 n_i++;
WANG Cong55334a52014-02-11 17:07:34 -0800146 } else if (ret < 0)
147 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700148 }
149 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400150 if (nla_put_u32(skb, TCA_FCNT, n_i))
151 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800152 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700153
154 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800155nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800156 nla_nest_cancel(skb, nest);
WANG Cong55334a52014-02-11 17:07:34 -0800157 return ret;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700158}
159
WANG Congddf97cc2016-02-22 15:57:53 -0800160int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
161 struct netlink_callback *cb, int type,
WANG Conga85a9702016-07-25 16:09:41 -0700162 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700163{
WANG Congddf97cc2016-02-22 15:57:53 -0800164 struct tcf_hashinfo *hinfo = tn->hinfo;
165
David S. Millere9ce1cd2006-08-21 23:54:55 -0700166 if (type == RTM_DELACTION) {
WANG Conga85a9702016-07-25 16:09:41 -0700167 return tcf_del_walker(hinfo, skb, ops);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700168 } else if (type == RTM_GETACTION) {
WANG Conga85a9702016-07-25 16:09:41 -0700169 return tcf_dump_walker(hinfo, skb, cb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700170 } else {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000171 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700172 return -EINVAL;
173 }
174}
WANG Congddf97cc2016-02-22 15:57:53 -0800175EXPORT_SYMBOL(tcf_generic_walker);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700176
WANG Congec0595c2016-07-25 16:09:42 -0700177static struct tc_action *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700178{
WANG Congec0595c2016-07-25 16:09:42 -0700179 struct tc_action *p = NULL;
WANG Cong89819dc2013-12-15 20:15:09 -0800180 struct hlist_head *head;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700181
WANG Cong89819dc2013-12-15 20:15:09 -0800182 spin_lock_bh(&hinfo->lock);
183 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
WANG Congec0595c2016-07-25 16:09:42 -0700184 hlist_for_each_entry_rcu(p, head, tcfa_head)
185 if (p->tcfa_index == index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700186 break;
WANG Cong89819dc2013-12-15 20:15:09 -0800187 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700188
189 return p;
190}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700191
WANG Congddf97cc2016-02-22 15:57:53 -0800192u32 tcf_hash_new_index(struct tc_action_net *tn)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700193{
WANG Congddf97cc2016-02-22 15:57:53 -0800194 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congddafd342014-01-09 16:13:59 -0800195 u32 val = hinfo->index;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700196
197 do {
198 if (++val == 0)
199 val = 1;
200 } while (tcf_hash_lookup(val, hinfo));
201
WANG Congddafd342014-01-09 16:13:59 -0800202 hinfo->index = val;
Yang Yingliang17569fa2013-12-10 20:55:29 +0800203 return val;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700204}
205EXPORT_SYMBOL(tcf_hash_new_index);
206
WANG Conga85a9702016-07-25 16:09:41 -0700207int tcf_hash_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700208{
WANG Congddf97cc2016-02-22 15:57:53 -0800209 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700210 struct tc_action *p = tcf_hash_lookup(index, hinfo);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700211
212 if (p) {
WANG Congec0595c2016-07-25 16:09:42 -0700213 *a = p;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700214 return 1;
215 }
216 return 0;
217}
WANG Cong6e6a50c2014-01-17 11:37:03 -0800218EXPORT_SYMBOL(tcf_hash_search);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700219
WANG Conga85a9702016-07-25 16:09:41 -0700220bool tcf_hash_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
WANG Congb2313072016-06-13 13:46:28 -0700221 int bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700222{
WANG Congddf97cc2016-02-22 15:57:53 -0800223 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700224 struct tc_action *p = NULL;
225
David S. Millere9ce1cd2006-08-21 23:54:55 -0700226 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700227 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -0700228 p->tcfa_bindcnt++;
229 p->tcfa_refcnt++;
230 *a = p;
WANG Congb2313072016-06-13 13:46:28 -0700231 return true;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700232 }
WANG Congb2313072016-06-13 13:46:28 -0700233 return false;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700234}
235EXPORT_SYMBOL(tcf_hash_check);
236
WANG Cong86062032014-02-11 17:07:31 -0800237void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
238{
WANG Cong86062032014-02-11 17:07:31 -0800239 if (est)
WANG Congec0595c2016-07-25 16:09:42 -0700240 gen_kill_estimator(&a->tcfa_bstats,
241 &a->tcfa_rate_est);
242 call_rcu(&a->tcfa_rcu, free_tcf);
WANG Cong86062032014-02-11 17:07:31 -0800243}
244EXPORT_SYMBOL(tcf_hash_cleanup);
245
WANG Congddf97cc2016-02-22 15:57:53 -0800246int tcf_hash_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
WANG Conga85a9702016-07-25 16:09:41 -0700247 struct tc_action **a, const struct tc_action_ops *ops,
248 int bind, bool cpustats)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700249{
WANG Congec0595c2016-07-25 16:09:42 -0700250 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
WANG Congddf97cc2016-02-22 15:57:53 -0800251 struct tcf_hashinfo *hinfo = tn->hinfo;
Eric Dumazet519c8182015-07-06 05:18:04 -0700252 int err = -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700253
254 if (unlikely(!p))
WANG Cong86062032014-02-11 17:07:31 -0800255 return -ENOMEM;
WANG Congec0595c2016-07-25 16:09:42 -0700256 p->tcfa_refcnt = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700257 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -0700258 p->tcfa_bindcnt = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700259
Eric Dumazet519c8182015-07-06 05:18:04 -0700260 if (cpustats) {
261 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
262 if (!p->cpu_bstats) {
263err1:
264 kfree(p);
265 return err;
266 }
267 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
268 if (!p->cpu_qstats) {
269err2:
270 free_percpu(p->cpu_bstats);
271 goto err1;
272 }
273 }
WANG Congec0595c2016-07-25 16:09:42 -0700274 spin_lock_init(&p->tcfa_lock);
275 INIT_HLIST_NODE(&p->tcfa_head);
276 p->tcfa_index = index ? index : tcf_hash_new_index(tn);
277 p->tcfa_tm.install = jiffies;
278 p->tcfa_tm.lastuse = jiffies;
279 p->tcfa_tm.firstuse = 0;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800280 if (est) {
WANG Congec0595c2016-07-25 16:09:42 -0700281 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
282 &p->tcfa_rate_est,
283 &p->tcfa_lock, NULL, est);
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800284 if (err) {
Eric Dumazet519c8182015-07-06 05:18:04 -0700285 free_percpu(p->cpu_qstats);
286 goto err2;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800287 }
288 }
289
WANG Congec0595c2016-07-25 16:09:42 -0700290 p->hinfo = hinfo;
291 p->ops = ops;
292 INIT_LIST_HEAD(&p->list);
293 *a = p;
WANG Cong86062032014-02-11 17:07:31 -0800294 return 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700295}
296EXPORT_SYMBOL(tcf_hash_create);
297
WANG Congddf97cc2016-02-22 15:57:53 -0800298void tcf_hash_insert(struct tc_action_net *tn, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700299{
WANG Congddf97cc2016-02-22 15:57:53 -0800300 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700301 unsigned int h = tcf_hash(a->tcfa_index, hinfo->hmask);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700302
WANG Cong89819dc2013-12-15 20:15:09 -0800303 spin_lock_bh(&hinfo->lock);
WANG Congec0595c2016-07-25 16:09:42 -0700304 hlist_add_head(&a->tcfa_head, &hinfo->htab[h]);
WANG Cong89819dc2013-12-15 20:15:09 -0800305 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700306}
307EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
WANG Congddf97cc2016-02-22 15:57:53 -0800309void tcf_hashinfo_destroy(const struct tc_action_ops *ops,
310 struct tcf_hashinfo *hinfo)
WANG Cong1d4150c2016-02-22 15:57:52 -0800311{
WANG Cong1d4150c2016-02-22 15:57:52 -0800312 int i;
313
314 for (i = 0; i < hinfo->hmask + 1; i++) {
WANG Congec0595c2016-07-25 16:09:42 -0700315 struct tc_action *p;
WANG Cong1d4150c2016-02-22 15:57:52 -0800316 struct hlist_node *n;
317
WANG Congec0595c2016-07-25 16:09:42 -0700318 hlist_for_each_entry_safe(p, n, &hinfo->htab[i], tcfa_head) {
WANG Cong1d4150c2016-02-22 15:57:52 -0800319 int ret;
320
WANG Congec0595c2016-07-25 16:09:42 -0700321 ret = __tcf_hash_release(p, false, true);
WANG Cong1d4150c2016-02-22 15:57:52 -0800322 if (ret == ACT_P_DELETED)
323 module_put(ops->owner);
324 else if (ret < 0)
325 return;
326 }
327 }
328 kfree(hinfo->htab);
329}
WANG Congddf97cc2016-02-22 15:57:53 -0800330EXPORT_SYMBOL(tcf_hashinfo_destroy);
WANG Cong1d4150c2016-02-22 15:57:52 -0800331
WANG Cong1f747c22013-12-15 20:15:10 -0800332static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333static DEFINE_RWLOCK(act_mod_lock);
334
WANG Congddf97cc2016-02-22 15:57:53 -0800335int tcf_register_action(struct tc_action_ops *act,
336 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
WANG Cong1f747c22013-12-15 20:15:10 -0800338 struct tc_action_ops *a;
WANG Congddf97cc2016-02-22 15:57:53 -0800339 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
WANG Congddf97cc2016-02-22 15:57:53 -0800341 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500342 return -EINVAL;
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800345 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
347 write_unlock(&act_mod_lock);
348 return -EEXIST;
349 }
350 }
WANG Cong1f747c22013-12-15 20:15:10 -0800351 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 write_unlock(&act_mod_lock);
WANG Congddf97cc2016-02-22 15:57:53 -0800353
354 ret = register_pernet_subsys(ops);
355 if (ret) {
356 tcf_unregister_action(act, ops);
357 return ret;
358 }
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
361}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800362EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
WANG Congddf97cc2016-02-22 15:57:53 -0800364int tcf_unregister_action(struct tc_action_ops *act,
365 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
WANG Cong1f747c22013-12-15 20:15:10 -0800367 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 int err = -ENOENT;
369
WANG Congddf97cc2016-02-22 15:57:53 -0800370 unregister_pernet_subsys(ops);
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800373 list_for_each_entry(a, &act_base, head) {
374 if (a == act) {
375 list_del(&act->head);
376 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380 write_unlock(&act_mod_lock);
381 return err;
382}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800383EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385/* lookup by name */
386static struct tc_action_ops *tc_lookup_action_n(char *kind)
387{
Eric Dumazeta7928662013-12-20 12:32:32 -0800388 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 if (kind) {
391 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800392 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800394 if (try_module_get(a->owner))
395 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 break;
397 }
398 }
399 read_unlock(&act_mod_lock);
400 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800401 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800404/* lookup by nlattr */
405static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Eric Dumazeta7928662013-12-20 12:32:32 -0800407 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 if (kind) {
410 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800411 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800412 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800413 if (try_module_get(a->owner))
414 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 break;
416 }
417 }
418 read_unlock(&act_mod_lock);
419 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800420 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
WANG Cong22dc13c2016-08-13 22:35:00 -0700423int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
424 int nr_actions, struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
WANG Cong22dc13c2016-08-13 22:35:00 -0700426 int ret = -1, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 if (skb->tc_verd & TC_NCLS) {
429 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 ret = TC_ACT_OK;
431 goto exec_done;
432 }
WANG Cong22dc13c2016-08-13 22:35:00 -0700433 for (i = 0; i < nr_actions; i++) {
434 const struct tc_action *a = actions[i];
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500437 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500438 if (ret == TC_ACT_REPEAT)
439 goto repeat; /* we need a ttl - JHS */
440 if (ret != TC_ACT_PIPE)
441 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return ret;
445}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800446EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
WANG Cong55334a52014-02-11 17:07:34 -0800448int tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
WANG Cong33be6272013-12-15 20:15:05 -0800450 struct tc_action *a, *tmp;
WANG Cong55334a52014-02-11 17:07:34 -0800451 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
WANG Cong33be6272013-12-15 20:15:05 -0800453 list_for_each_entry_safe(a, tmp, actions, list) {
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200454 ret = __tcf_hash_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800455 if (ret == ACT_P_DELETED)
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500456 module_put(a->ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800457 else if (ret < 0)
458 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
WANG Cong55334a52014-02-11 17:07:34 -0800460 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
463int
464tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
465{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return a->ops->dump(skb, a, bind, ref);
467}
468
469int
470tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
471{
472 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700473 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800474 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
David S. Miller1b34ec42012-03-29 05:11:39 -0400476 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
477 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800479 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800480 nest = nla_nest_start(skb, TCA_OPTIONS);
481 if (nest == NULL)
482 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000483 err = tcf_action_dump_old(skb, a, bind, ref);
484 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800485 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return err;
487 }
488
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800489nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700490 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return -1;
492}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800493EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400495int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
496 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct tc_action *a;
499 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800500 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
WANG Cong33be6272013-12-15 20:15:05 -0800502 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800503 nest = nla_nest_start(skb, a->order);
504 if (nest == NULL)
505 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 err = tcf_action_dump_1(skb, a, bind, ref);
507 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700508 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800509 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511
512 return 0;
513
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800514nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700515 err = -EINVAL;
516errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800517 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700518 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000521struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
522 struct nlattr *est, char *name, int ovr,
523 int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 struct tc_action *a;
526 struct tc_action_ops *a_o;
527 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000528 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800529 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800530 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800533 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
534 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800536 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800537 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (kind == NULL)
539 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800540 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 goto err_out;
542 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800543 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
545 goto err_out;
546 }
547
548 a_o = tc_lookup_action_n(act_name);
549 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700550#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800552 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 rtnl_lock();
554
555 a_o = tc_lookup_action_n(act_name);
556
557 /* We dropped the RTNL semaphore in order to
558 * perform the module load. So, even if we
559 * succeeded in loading the module we have to
560 * tell the caller to replay the request. We
561 * indicate this using -EAGAIN.
562 */
563 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800564 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 goto err_mod;
566 }
567#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800568 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 goto err_out;
570 }
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /* backward compatibility for policer */
573 if (name == NULL)
WANG Conga85a9702016-07-25 16:09:41 -0700574 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 else
WANG Conga85a9702016-07-25 16:09:41 -0700576 err = a_o->init(net, nla, est, &a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800577 if (err < 0)
WANG Conga85a9702016-07-25 16:09:41 -0700578 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000581 * if it exists and is only bound to in a_o->init() then
582 * ACT_P_CREATED is not returned (a zero is).
583 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800584 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return a;
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589err_mod:
590 module_put(a_o->owner);
591err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800592 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
WANG Cong33be6272013-12-15 20:15:05 -0800595int tcf_action_init(struct net *net, struct nlattr *nla,
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000596 struct nlattr *est, char *name, int ovr,
WANG Cong33be6272013-12-15 20:15:05 -0800597 int bind, struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000599 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800600 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800601 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 int i;
603
Patrick McHardycee63722008-01-23 20:33:32 -0800604 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
605 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800606 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800608 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000609 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800610 if (IS_ERR(act)) {
611 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800613 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800614 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800615 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
WANG Cong33be6272013-12-15 20:15:05 -0800617 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619err:
WANG Cong33be6272013-12-15 20:15:05 -0800620 tcf_action_destroy(actions, bind);
621 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
WANG Congec0595c2016-07-25 16:09:42 -0700624int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 int compat_mode)
626{
627 int err = 0;
628 struct gnet_dump d;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900629
WANG Cong7eb88962014-01-09 16:14:05 -0800630 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 goto errout;
632
633 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400634 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 */
636 if (compat_mode) {
WANG Congec0595c2016-07-25 16:09:42 -0700637 if (p->type == TCA_OLD_COMPAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 err = gnet_stats_start_copy_compat(skb, 0,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200639 TCA_STATS,
640 TCA_XSTATS,
WANG Congec0595c2016-07-25 16:09:42 -0700641 &p->tcfa_lock, &d,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200642 TCA_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 else
644 return 0;
645 } else
646 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Congec0595c2016-07-25 16:09:42 -0700647 &p->tcfa_lock, &d, TCA_ACT_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 if (err < 0)
650 goto errout;
651
WANG Congec0595c2016-07-25 16:09:42 -0700652 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
653 gnet_stats_copy_rate_est(&d, &p->tcfa_bstats,
654 &p->tcfa_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -0700655 gnet_stats_copy_queue(&d, p->cpu_qstats,
WANG Congec0595c2016-07-25 16:09:42 -0700656 &p->tcfa_qstats,
657 p->tcfa_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 goto errout;
659
660 if (gnet_stats_finish_copy(&d) < 0)
661 goto errout;
662
663 return 0;
664
665errout:
666 return -1;
667}
668
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400669static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
670 u32 portid, u32 seq, u16 flags, int event, int bind,
671 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 struct tcamsg *t;
674 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700675 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800676 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Eric W. Biederman15e47302012-09-07 20:12:54 +0000678 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700679 if (!nlh)
680 goto out_nlmsg_trim;
681 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700683 t->tca__pad1 = 0;
684 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900685
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800686 nest = nla_nest_start(skb, TCA_ACT_TAB);
687 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700688 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
WANG Cong33be6272013-12-15 20:15:05 -0800690 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700691 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800693 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900694
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700695 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return skb->len;
697
David S. Miller8b00a532012-06-26 21:39:32 -0700698out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700699 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return -1;
701}
702
703static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000704act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800705 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
710 if (!skb)
711 return -ENOBUFS;
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400712 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
713 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 kfree_skb(skb);
715 return -EINVAL;
716 }
Thomas Graf2942e902006-08-15 00:30:25 -0700717
Eric W. Biederman15e47302012-09-07 20:12:54 +0000718 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
WANG Congddf97cc2016-02-22 15:57:53 -0800721static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
722 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000724 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700725 const struct tc_action_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct tc_action *a;
727 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800728 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Patrick McHardycee63722008-01-23 20:33:32 -0800730 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
731 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800732 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Patrick McHardycee63722008-01-23 20:33:32 -0800734 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800735 if (tb[TCA_ACT_INDEX] == NULL ||
736 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800737 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800738 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800740 err = -EINVAL;
WANG Conga85a9702016-07-25 16:09:41 -0700741 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
742 if (!ops) /* could happen in batch of actions */
743 goto err_out;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800744 err = -ENOENT;
WANG Conga85a9702016-07-25 16:09:41 -0700745 if (ops->lookup(net, &a, index) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 goto err_mod;
747
WANG Conga85a9702016-07-25 16:09:41 -0700748 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751err_mod:
WANG Conga85a9702016-07-25 16:09:41 -0700752 module_put(ops->owner);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800753err_out:
754 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Tom Goff7316ae82010-03-19 15:40:13 +0000757static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000758 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
760 struct sk_buff *skb;
761 unsigned char *b;
762 struct nlmsghdr *nlh;
763 struct tcamsg *t;
764 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800765 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000766 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700767 const struct tc_action_ops *ops;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800768 struct nlattr *kind;
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700769 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
772 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000773 pr_debug("tca_action_flush: failed skb alloc\n");
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700774 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700777 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Patrick McHardycee63722008-01-23 20:33:32 -0800779 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
780 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 goto err_out;
782
Patrick McHardycee63722008-01-23 20:33:32 -0800783 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800784 kind = tb[TCA_ACT_KIND];
WANG Conga85a9702016-07-25 16:09:41 -0700785 ops = tc_lookup_action(kind);
786 if (!ops) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 goto err_out;
788
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400789 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
790 sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700791 if (!nlh)
792 goto out_module_put;
793 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700795 t->tca__pad1 = 0;
796 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800798 nest = nla_nest_start(skb, TCA_ACT_TAB);
799 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700800 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
WANG Conga85a9702016-07-25 16:09:41 -0700802 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (err < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700804 goto out_module_put;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700805 if (err == 0)
806 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800808 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700810 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Conga85a9702016-07-25 16:09:41 -0700812 module_put(ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000813 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000814 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (err > 0)
816 return 0;
817
818 return err;
819
David S. Miller8b00a532012-06-26 21:39:32 -0700820out_module_put:
WANG Conga85a9702016-07-25 16:09:41 -0700821 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700823noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return err;
826}
827
828static int
WANG Conga56e1952014-01-09 16:14:00 -0800829tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
830 u32 portid)
831{
832 int ret;
833 struct sk_buff *skb;
834
835 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
836 if (!skb)
837 return -ENOBUFS;
838
839 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
840 0, 1) <= 0) {
841 kfree_skb(skb);
842 return -EINVAL;
843 }
844
845 /* now do the delete */
WANG Cong55334a52014-02-11 17:07:34 -0800846 ret = tcf_action_destroy(actions, 0);
847 if (ret < 0) {
848 kfree_skb(skb);
849 return ret;
850 }
WANG Conga56e1952014-01-09 16:14:00 -0800851
852 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
853 n->nlmsg_flags & NLM_F_ECHO);
854 if (ret > 0)
855 return 0;
856 return ret;
857}
858
859static int
Tom Goff7316ae82010-03-19 15:40:13 +0000860tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000861 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Patrick McHardycee63722008-01-23 20:33:32 -0800863 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000864 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800865 struct tc_action *act;
866 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Patrick McHardycee63722008-01-23 20:33:32 -0800868 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
869 if (ret < 0)
870 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000872 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700873 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000874 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700875 else
876 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800879 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
WANG Congddf97cc2016-02-22 15:57:53 -0800880 act = tcf_action_get_1(net, tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800881 if (IS_ERR(act)) {
882 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800884 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800885 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800886 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888
889 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800890 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -0800892 ret = tcf_del_notify(net, n, &actions, portid);
893 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return ret;
896 }
897err:
WANG Congf07fed82016-08-13 22:34:56 -0700898 tcf_action_destroy(&actions, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return ret;
900}
901
WANG Conga56e1952014-01-09 16:14:00 -0800902static int
903tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
904 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 int err = 0;
908
909 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
910 if (!skb)
911 return -ENOBUFS;
912
WANG Conga56e1952014-01-09 16:14:00 -0800913 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
914 RTM_NEWACTION, 0, 0) <= 0) {
915 kfree_skb(skb);
916 return -EINVAL;
917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
WANG Conga56e1952014-01-09 16:14:00 -0800919 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
920 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (err > 0)
922 err = 0;
923 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926static int
Tom Goff7316ae82010-03-19 15:40:13 +0000927tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000928 u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
930 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -0800931 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
WANG Cong33be6272013-12-15 20:15:05 -0800933 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
934 if (ret)
WANG Congf07fed82016-08-13 22:34:56 -0700935 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
WANG Congf07fed82016-08-13 22:34:56 -0700937 return tcf_add_notify(net, n, &actions, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Thomas Graf661d2962013-03-21 07:45:29 +0000940static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900942 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800943 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +0000944 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 int ret = 0, ovr = 0;
946
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400947 if ((n->nlmsg_type != RTM_GETACTION) &&
948 !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000949 return -EPERM;
950
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800951 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
952 if (ret < 0)
953 return ret;
954
955 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000956 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return -EINVAL;
958 }
959
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000960 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 switch (n->nlmsg_type) {
962 case RTM_NEWACTION:
963 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300964 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 * Note that CREATE | EXCL implies that
966 * but since we want avoid ambiguity (eg when flags
967 * is zero) then just set this
968 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000969 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 ovr = 1;
971replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000972 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (ret == -EAGAIN)
974 goto replay;
975 break;
976 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000977 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000978 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 break;
980 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000981 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000982 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 break;
984 default:
985 BUG();
986 }
987
988 return ret;
989}
990
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800991static struct nlattr *
Patrick McHardy3a6c2b42009-08-25 16:07:40 +0200992find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000994 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800995 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
996 struct nlattr *nla[TCAA_MAX + 1];
997 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Patrick McHardyc96c9472008-01-23 20:32:58 -0800999 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001001 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (tb1 == NULL)
1003 return NULL;
1004
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001005 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1006 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Patrick McHardy6d834e02008-01-23 20:32:42 -08001009 if (tb[1] == NULL)
1010 return NULL;
1011 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1012 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001014 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Thomas Graf26dab892006-07-05 20:45:06 -07001016 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
1019static int
1020tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1021{
WANG Congddf97cc2016-02-22 15:57:53 -08001022 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001024 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001025 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 struct tc_action_ops *a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001028 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001029 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001032 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return 0;
1034 }
1035
Thomas Graf26dab892006-07-05 20:45:06 -07001036 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001037 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Eric W. Biederman15e47302012-09-07 20:12:54 +00001040 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001041 cb->nlh->nlmsg_type, sizeof(*t), 0);
1042 if (!nlh)
1043 goto out_module_put;
1044 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001046 t->tca__pad1 = 0;
1047 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001049 nest = nla_nest_start(skb, TCA_ACT_TAB);
1050 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001051 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
WANG Conga85a9702016-07-25 16:09:41 -07001053 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001055 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001058 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 ret = skb->len;
1060 } else
Jamal Hadi Salimebecaa62016-06-13 18:08:42 -04001061 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001063 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001064 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 nlh->nlmsg_flags |= NLM_F_MULTI;
1066 module_put(a_o->owner);
1067 return skb->len;
1068
David S. Miller8b00a532012-06-26 21:39:32 -07001069out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001071 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return skb->len;
1073}
1074
1075static int __init tc_action_init(void)
1076{
Greg Rosec7ac8672011-06-10 01:27:09 +00001077 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1078 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1079 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1080 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return 0;
1083}
1084
1085subsys_initcall(tc_action_init);