blob: c9102172ce3b0fa72ceabacc8475ae30298d0ec4 [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
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400595static void cleanup_a(struct list_head *actions, int ovr)
596{
597 struct tc_action *a;
598
599 if (!ovr)
600 return;
601
602 list_for_each_entry(a, actions, list)
603 a->tcfa_refcnt--;
604}
605
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400606int tcf_action_init(struct net *net, struct nlattr *nla, struct nlattr *est,
607 char *name, int ovr, int bind, struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000609 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800610 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800611 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 int i;
613
Patrick McHardycee63722008-01-23 20:33:32 -0800614 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
615 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800616 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800618 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000619 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800620 if (IS_ERR(act)) {
621 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800623 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800624 act->order = i;
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400625 if (ovr)
626 act->tcfa_refcnt++;
WANG Cong33be6272013-12-15 20:15:05 -0800627 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400629
630 /* Remove the temp refcnt which was necessary to protect against
631 * destroying an existing action which was being replaced
632 */
633 cleanup_a(actions, ovr);
WANG Cong33be6272013-12-15 20:15:05 -0800634 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636err:
WANG Cong33be6272013-12-15 20:15:05 -0800637 tcf_action_destroy(actions, bind);
638 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
WANG Congec0595c2016-07-25 16:09:42 -0700641int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 int compat_mode)
643{
644 int err = 0;
645 struct gnet_dump d;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900646
WANG Cong7eb88962014-01-09 16:14:05 -0800647 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 goto errout;
649
650 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400651 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 */
653 if (compat_mode) {
WANG Congec0595c2016-07-25 16:09:42 -0700654 if (p->type == TCA_OLD_COMPAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 err = gnet_stats_start_copy_compat(skb, 0,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200656 TCA_STATS,
657 TCA_XSTATS,
WANG Congec0595c2016-07-25 16:09:42 -0700658 &p->tcfa_lock, &d,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200659 TCA_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 else
661 return 0;
662 } else
663 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Congec0595c2016-07-25 16:09:42 -0700664 &p->tcfa_lock, &d, TCA_ACT_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 if (err < 0)
667 goto errout;
668
WANG Congec0595c2016-07-25 16:09:42 -0700669 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
670 gnet_stats_copy_rate_est(&d, &p->tcfa_bstats,
671 &p->tcfa_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -0700672 gnet_stats_copy_queue(&d, p->cpu_qstats,
WANG Congec0595c2016-07-25 16:09:42 -0700673 &p->tcfa_qstats,
674 p->tcfa_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 goto errout;
676
677 if (gnet_stats_finish_copy(&d) < 0)
678 goto errout;
679
680 return 0;
681
682errout:
683 return -1;
684}
685
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400686static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
687 u32 portid, u32 seq, u16 flags, int event, int bind,
688 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 struct tcamsg *t;
691 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700692 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800693 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Eric W. Biederman15e47302012-09-07 20:12:54 +0000695 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700696 if (!nlh)
697 goto out_nlmsg_trim;
698 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700700 t->tca__pad1 = 0;
701 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900702
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800703 nest = nla_nest_start(skb, TCA_ACT_TAB);
704 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700705 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
WANG Cong33be6272013-12-15 20:15:05 -0800707 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700708 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800710 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900711
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700712 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return skb->len;
714
David S. Miller8b00a532012-06-26 21:39:32 -0700715out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700716 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return -1;
718}
719
720static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000721act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800722 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
727 if (!skb)
728 return -ENOBUFS;
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400729 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
730 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 kfree_skb(skb);
732 return -EINVAL;
733 }
Thomas Graf2942e902006-08-15 00:30:25 -0700734
Eric W. Biederman15e47302012-09-07 20:12:54 +0000735 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
WANG Congddf97cc2016-02-22 15:57:53 -0800738static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
739 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000741 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700742 const struct tc_action_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 struct tc_action *a;
744 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800745 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Patrick McHardycee63722008-01-23 20:33:32 -0800747 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
748 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800749 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Patrick McHardycee63722008-01-23 20:33:32 -0800751 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800752 if (tb[TCA_ACT_INDEX] == NULL ||
753 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800754 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800755 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800757 err = -EINVAL;
WANG Conga85a9702016-07-25 16:09:41 -0700758 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
759 if (!ops) /* could happen in batch of actions */
760 goto err_out;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800761 err = -ENOENT;
WANG Conga85a9702016-07-25 16:09:41 -0700762 if (ops->lookup(net, &a, index) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 goto err_mod;
764
WANG Conga85a9702016-07-25 16:09:41 -0700765 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768err_mod:
WANG Conga85a9702016-07-25 16:09:41 -0700769 module_put(ops->owner);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800770err_out:
771 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Tom Goff7316ae82010-03-19 15:40:13 +0000774static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000775 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 struct sk_buff *skb;
778 unsigned char *b;
779 struct nlmsghdr *nlh;
780 struct tcamsg *t;
781 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800782 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000783 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700784 const struct tc_action_ops *ops;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800785 struct nlattr *kind;
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700786 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
789 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000790 pr_debug("tca_action_flush: failed skb alloc\n");
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700791 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700794 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Patrick McHardycee63722008-01-23 20:33:32 -0800796 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
797 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 goto err_out;
799
Patrick McHardycee63722008-01-23 20:33:32 -0800800 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800801 kind = tb[TCA_ACT_KIND];
WANG Conga85a9702016-07-25 16:09:41 -0700802 ops = tc_lookup_action(kind);
803 if (!ops) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 goto err_out;
805
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400806 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
807 sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700808 if (!nlh)
809 goto out_module_put;
810 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700812 t->tca__pad1 = 0;
813 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800815 nest = nla_nest_start(skb, TCA_ACT_TAB);
816 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700817 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
WANG Conga85a9702016-07-25 16:09:41 -0700819 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (err < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700821 goto out_module_put;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700822 if (err == 0)
823 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800825 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700827 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Conga85a9702016-07-25 16:09:41 -0700829 module_put(ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000830 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000831 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (err > 0)
833 return 0;
834
835 return err;
836
David S. Miller8b00a532012-06-26 21:39:32 -0700837out_module_put:
WANG Conga85a9702016-07-25 16:09:41 -0700838 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700840noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return err;
843}
844
845static int
WANG Conga56e1952014-01-09 16:14:00 -0800846tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
847 u32 portid)
848{
849 int ret;
850 struct sk_buff *skb;
851
852 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
853 if (!skb)
854 return -ENOBUFS;
855
856 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
857 0, 1) <= 0) {
858 kfree_skb(skb);
859 return -EINVAL;
860 }
861
862 /* now do the delete */
WANG Cong55334a52014-02-11 17:07:34 -0800863 ret = tcf_action_destroy(actions, 0);
864 if (ret < 0) {
865 kfree_skb(skb);
866 return ret;
867 }
WANG Conga56e1952014-01-09 16:14:00 -0800868
869 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
870 n->nlmsg_flags & NLM_F_ECHO);
871 if (ret > 0)
872 return 0;
873 return ret;
874}
875
876static int
Tom Goff7316ae82010-03-19 15:40:13 +0000877tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000878 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Patrick McHardycee63722008-01-23 20:33:32 -0800880 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000881 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800882 struct tc_action *act;
883 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Patrick McHardycee63722008-01-23 20:33:32 -0800885 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
886 if (ret < 0)
887 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000889 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700890 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000891 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700892 else
893 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800896 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
WANG Congddf97cc2016-02-22 15:57:53 -0800897 act = tcf_action_get_1(net, tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800898 if (IS_ERR(act)) {
899 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800901 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800902 act->order = i;
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400903 if (event == RTM_GETACTION)
904 act->tcfa_refcnt++;
WANG Cong33be6272013-12-15 20:15:05 -0800905 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907
908 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800909 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -0800911 ret = tcf_del_notify(net, n, &actions, portid);
912 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return ret;
915 }
916err:
WANG Congf07fed82016-08-13 22:34:56 -0700917 tcf_action_destroy(&actions, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return ret;
919}
920
WANG Conga56e1952014-01-09 16:14:00 -0800921static int
922tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
923 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 int err = 0;
927
928 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
929 if (!skb)
930 return -ENOBUFS;
931
WANG Conga56e1952014-01-09 16:14:00 -0800932 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
933 RTM_NEWACTION, 0, 0) <= 0) {
934 kfree_skb(skb);
935 return -EINVAL;
936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
WANG Conga56e1952014-01-09 16:14:00 -0800938 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
939 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (err > 0)
941 err = 0;
942 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400945static int tcf_action_add(struct net *net, struct nlattr *nla,
946 struct nlmsghdr *n, u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
948 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -0800949 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
WANG Cong33be6272013-12-15 20:15:05 -0800951 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
952 if (ret)
WANG Congf07fed82016-08-13 22:34:56 -0700953 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
WANG Congf07fed82016-08-13 22:34:56 -0700955 return tcf_add_notify(net, n, &actions, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
Thomas Graf661d2962013-03-21 07:45:29 +0000958static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900960 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800961 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +0000962 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 int ret = 0, ovr = 0;
964
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400965 if ((n->nlmsg_type != RTM_GETACTION) &&
966 !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000967 return -EPERM;
968
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800969 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
970 if (ret < 0)
971 return ret;
972
973 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000974 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return -EINVAL;
976 }
977
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000978 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 switch (n->nlmsg_type) {
980 case RTM_NEWACTION:
981 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300982 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 * Note that CREATE | EXCL implies that
984 * but since we want avoid ambiguity (eg when flags
985 * is zero) then just set this
986 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000987 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 ovr = 1;
989replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000990 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (ret == -EAGAIN)
992 goto replay;
993 break;
994 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000995 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000996 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 break;
998 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000999 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001000 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 break;
1002 default:
1003 BUG();
1004 }
1005
1006 return ret;
1007}
1008
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001009static struct nlattr *find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001011 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001012 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1013 struct nlattr *nla[TCAA_MAX + 1];
1014 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Patrick McHardyc96c9472008-01-23 20:32:58 -08001016 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001018 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (tb1 == NULL)
1020 return NULL;
1021
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001022 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1023 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Patrick McHardy6d834e02008-01-23 20:32:42 -08001026 if (tb[1] == NULL)
1027 return NULL;
1028 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1029 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001031 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Thomas Graf26dab892006-07-05 20:45:06 -07001033 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001036static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
WANG Congddf97cc2016-02-22 15:57:53 -08001038 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001040 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001041 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 struct tc_action_ops *a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001044 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001045 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001048 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return 0;
1050 }
1051
Thomas Graf26dab892006-07-05 20:45:06 -07001052 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001053 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Eric W. Biederman15e47302012-09-07 20:12:54 +00001056 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001057 cb->nlh->nlmsg_type, sizeof(*t), 0);
1058 if (!nlh)
1059 goto out_module_put;
1060 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001062 t->tca__pad1 = 0;
1063 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001065 nest = nla_nest_start(skb, TCA_ACT_TAB);
1066 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001067 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
WANG Conga85a9702016-07-25 16:09:41 -07001069 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001071 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001074 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 ret = skb->len;
1076 } else
Jamal Hadi Salimebecaa62016-06-13 18:08:42 -04001077 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001079 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001080 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 nlh->nlmsg_flags |= NLM_F_MULTI;
1082 module_put(a_o->owner);
1083 return skb->len;
1084
David S. Miller8b00a532012-06-26 21:39:32 -07001085out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001087 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return skb->len;
1089}
1090
1091static int __init tc_action_init(void)
1092{
Greg Rosec7ac8672011-06-10 01:27:09 +00001093 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1094 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1095 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1096 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return 0;
1099}
1100
1101subsys_initcall(tc_action_init);