blob: a512b18c0088506bc577d8b3a1113871206c6d47 [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
WANG Congab102b82016-10-11 10:56:45 -0700344 /* We have to register pernet ops before making the action ops visible,
345 * otherwise tcf_action_init_1() could get a partially initialized
346 * netns.
347 */
348 ret = register_pernet_subsys(ops);
349 if (ret)
350 return ret;
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800353 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
355 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700356 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return -EEXIST;
358 }
359 }
WANG Cong1f747c22013-12-15 20:15:10 -0800360 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 write_unlock(&act_mod_lock);
WANG Congddf97cc2016-02-22 15:57:53 -0800362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return 0;
364}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800365EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
WANG Congddf97cc2016-02-22 15:57:53 -0800367int tcf_unregister_action(struct tc_action_ops *act,
368 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
WANG Cong1f747c22013-12-15 20:15:10 -0800370 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 int err = -ENOENT;
372
373 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800374 list_for_each_entry(a, &act_base, head) {
375 if (a == act) {
376 list_del(&act->head);
377 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700382 if (!err)
383 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return err;
385}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800386EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388/* lookup by name */
389static struct tc_action_ops *tc_lookup_action_n(char *kind)
390{
Eric Dumazeta7928662013-12-20 12:32:32 -0800391 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 if (kind) {
394 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800395 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800397 if (try_module_get(a->owner))
398 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 break;
400 }
401 }
402 read_unlock(&act_mod_lock);
403 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800404 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800407/* lookup by nlattr */
408static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Eric Dumazeta7928662013-12-20 12:32:32 -0800410 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 if (kind) {
413 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800414 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800415 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800416 if (try_module_get(a->owner))
417 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 break;
419 }
420 }
421 read_unlock(&act_mod_lock);
422 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800423 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
WANG Cong22dc13c2016-08-13 22:35:00 -0700426int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
427 int nr_actions, struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
WANG Cong22dc13c2016-08-13 22:35:00 -0700429 int ret = -1, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 if (skb->tc_verd & TC_NCLS) {
432 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 ret = TC_ACT_OK;
434 goto exec_done;
435 }
WANG Cong22dc13c2016-08-13 22:35:00 -0700436 for (i = 0; i < nr_actions; i++) {
437 const struct tc_action *a = actions[i];
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500440 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500441 if (ret == TC_ACT_REPEAT)
442 goto repeat; /* we need a ttl - JHS */
443 if (ret != TC_ACT_PIPE)
444 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return ret;
448}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800449EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
WANG Cong55334a52014-02-11 17:07:34 -0800451int tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
WANG Cong33be6272013-12-15 20:15:05 -0800453 struct tc_action *a, *tmp;
WANG Cong55334a52014-02-11 17:07:34 -0800454 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
WANG Cong33be6272013-12-15 20:15:05 -0800456 list_for_each_entry_safe(a, tmp, actions, list) {
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200457 ret = __tcf_hash_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800458 if (ret == ACT_P_DELETED)
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500459 module_put(a->ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800460 else if (ret < 0)
461 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
WANG Cong55334a52014-02-11 17:07:34 -0800463 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466int
467tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
468{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return a->ops->dump(skb, a, bind, ref);
470}
471
472int
473tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
474{
475 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700476 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800477 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
David S. Miller1b34ec42012-03-29 05:11:39 -0400479 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
480 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800482 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800483 nest = nla_nest_start(skb, TCA_OPTIONS);
484 if (nest == NULL)
485 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000486 err = tcf_action_dump_old(skb, a, bind, ref);
487 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800488 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return err;
490 }
491
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800492nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700493 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return -1;
495}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800496EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400498int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
499 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 struct tc_action *a;
502 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800503 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
WANG Cong33be6272013-12-15 20:15:05 -0800505 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800506 nest = nla_nest_start(skb, a->order);
507 if (nest == NULL)
508 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 err = tcf_action_dump_1(skb, a, bind, ref);
510 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700511 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800512 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514
515 return 0;
516
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800517nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700518 err = -EINVAL;
519errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800520 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700521 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000524struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
525 struct nlattr *est, char *name, int ovr,
526 int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 struct tc_action *a;
529 struct tc_action_ops *a_o;
530 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000531 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800532 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800533 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800536 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
537 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800539 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800540 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (kind == NULL)
542 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800543 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 goto err_out;
545 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800546 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
548 goto err_out;
549 }
550
551 a_o = tc_lookup_action_n(act_name);
552 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700553#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800555 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 rtnl_lock();
557
558 a_o = tc_lookup_action_n(act_name);
559
560 /* We dropped the RTNL semaphore in order to
561 * perform the module load. So, even if we
562 * succeeded in loading the module we have to
563 * tell the caller to replay the request. We
564 * indicate this using -EAGAIN.
565 */
566 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800567 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 goto err_mod;
569 }
570#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800571 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 goto err_out;
573 }
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /* backward compatibility for policer */
576 if (name == NULL)
WANG Conga85a9702016-07-25 16:09:41 -0700577 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 else
WANG Conga85a9702016-07-25 16:09:41 -0700579 err = a_o->init(net, nla, est, &a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800580 if (err < 0)
WANG Conga85a9702016-07-25 16:09:41 -0700581 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000584 * if it exists and is only bound to in a_o->init() then
585 * ACT_P_CREATED is not returned (a zero is).
586 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800587 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return a;
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592err_mod:
593 module_put(a_o->owner);
594err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800595 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400598static void cleanup_a(struct list_head *actions, int ovr)
599{
600 struct tc_action *a;
601
602 if (!ovr)
603 return;
604
605 list_for_each_entry(a, actions, list)
606 a->tcfa_refcnt--;
607}
608
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400609int tcf_action_init(struct net *net, struct nlattr *nla, struct nlattr *est,
610 char *name, int ovr, int bind, struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000612 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800613 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800614 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 int i;
616
Patrick McHardycee63722008-01-23 20:33:32 -0800617 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
618 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800619 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800621 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000622 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800623 if (IS_ERR(act)) {
624 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800626 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800627 act->order = i;
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400628 if (ovr)
629 act->tcfa_refcnt++;
WANG Cong33be6272013-12-15 20:15:05 -0800630 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400632
633 /* Remove the temp refcnt which was necessary to protect against
634 * destroying an existing action which was being replaced
635 */
636 cleanup_a(actions, ovr);
WANG Cong33be6272013-12-15 20:15:05 -0800637 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639err:
WANG Cong33be6272013-12-15 20:15:05 -0800640 tcf_action_destroy(actions, bind);
641 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
WANG Congec0595c2016-07-25 16:09:42 -0700644int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 int compat_mode)
646{
647 int err = 0;
648 struct gnet_dump d;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900649
WANG Cong7eb88962014-01-09 16:14:05 -0800650 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 goto errout;
652
653 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400654 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 */
656 if (compat_mode) {
WANG Congec0595c2016-07-25 16:09:42 -0700657 if (p->type == TCA_OLD_COMPAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 err = gnet_stats_start_copy_compat(skb, 0,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200659 TCA_STATS,
660 TCA_XSTATS,
WANG Congec0595c2016-07-25 16:09:42 -0700661 &p->tcfa_lock, &d,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200662 TCA_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 else
664 return 0;
665 } else
666 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Congec0595c2016-07-25 16:09:42 -0700667 &p->tcfa_lock, &d, TCA_ACT_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 if (err < 0)
670 goto errout;
671
WANG Congec0595c2016-07-25 16:09:42 -0700672 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
673 gnet_stats_copy_rate_est(&d, &p->tcfa_bstats,
674 &p->tcfa_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -0700675 gnet_stats_copy_queue(&d, p->cpu_qstats,
WANG Congec0595c2016-07-25 16:09:42 -0700676 &p->tcfa_qstats,
677 p->tcfa_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 goto errout;
679
680 if (gnet_stats_finish_copy(&d) < 0)
681 goto errout;
682
683 return 0;
684
685errout:
686 return -1;
687}
688
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400689static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
690 u32 portid, u32 seq, u16 flags, int event, int bind,
691 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 struct tcamsg *t;
694 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700695 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800696 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Eric W. Biederman15e47302012-09-07 20:12:54 +0000698 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700699 if (!nlh)
700 goto out_nlmsg_trim;
701 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700703 t->tca__pad1 = 0;
704 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900705
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800706 nest = nla_nest_start(skb, TCA_ACT_TAB);
707 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700708 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
WANG Cong33be6272013-12-15 20:15:05 -0800710 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700711 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800713 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900714
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700715 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return skb->len;
717
David S. Miller8b00a532012-06-26 21:39:32 -0700718out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700719 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return -1;
721}
722
723static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000724act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800725 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
730 if (!skb)
731 return -ENOBUFS;
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400732 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
733 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 kfree_skb(skb);
735 return -EINVAL;
736 }
Thomas Graf2942e902006-08-15 00:30:25 -0700737
Eric W. Biederman15e47302012-09-07 20:12:54 +0000738 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
WANG Congddf97cc2016-02-22 15:57:53 -0800741static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
742 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000744 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700745 const struct tc_action_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 struct tc_action *a;
747 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800748 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Patrick McHardycee63722008-01-23 20:33:32 -0800750 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
751 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800752 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Patrick McHardycee63722008-01-23 20:33:32 -0800754 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800755 if (tb[TCA_ACT_INDEX] == NULL ||
756 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800757 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800758 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800760 err = -EINVAL;
WANG Conga85a9702016-07-25 16:09:41 -0700761 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
762 if (!ops) /* could happen in batch of actions */
763 goto err_out;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800764 err = -ENOENT;
WANG Conga85a9702016-07-25 16:09:41 -0700765 if (ops->lookup(net, &a, index) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 goto err_mod;
767
WANG Conga85a9702016-07-25 16:09:41 -0700768 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771err_mod:
WANG Conga85a9702016-07-25 16:09:41 -0700772 module_put(ops->owner);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800773err_out:
774 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Tom Goff7316ae82010-03-19 15:40:13 +0000777static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000778 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 struct sk_buff *skb;
781 unsigned char *b;
782 struct nlmsghdr *nlh;
783 struct tcamsg *t;
784 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800785 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000786 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700787 const struct tc_action_ops *ops;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800788 struct nlattr *kind;
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700789 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
792 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000793 pr_debug("tca_action_flush: failed skb alloc\n");
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700794 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700797 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Patrick McHardycee63722008-01-23 20:33:32 -0800799 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
800 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 goto err_out;
802
Patrick McHardycee63722008-01-23 20:33:32 -0800803 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800804 kind = tb[TCA_ACT_KIND];
WANG Conga85a9702016-07-25 16:09:41 -0700805 ops = tc_lookup_action(kind);
806 if (!ops) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 goto err_out;
808
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400809 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
810 sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700811 if (!nlh)
812 goto out_module_put;
813 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700815 t->tca__pad1 = 0;
816 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800818 nest = nla_nest_start(skb, TCA_ACT_TAB);
819 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700820 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
WANG Conga85a9702016-07-25 16:09:41 -0700822 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (err < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700824 goto out_module_put;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700825 if (err == 0)
826 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800828 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700830 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Conga85a9702016-07-25 16:09:41 -0700832 module_put(ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000833 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000834 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (err > 0)
836 return 0;
837
838 return err;
839
David S. Miller8b00a532012-06-26 21:39:32 -0700840out_module_put:
WANG Conga85a9702016-07-25 16:09:41 -0700841 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700843noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return err;
846}
847
848static int
WANG Conga56e1952014-01-09 16:14:00 -0800849tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
850 u32 portid)
851{
852 int ret;
853 struct sk_buff *skb;
854
855 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
856 if (!skb)
857 return -ENOBUFS;
858
859 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
860 0, 1) <= 0) {
861 kfree_skb(skb);
862 return -EINVAL;
863 }
864
865 /* now do the delete */
WANG Cong55334a52014-02-11 17:07:34 -0800866 ret = tcf_action_destroy(actions, 0);
867 if (ret < 0) {
868 kfree_skb(skb);
869 return ret;
870 }
WANG Conga56e1952014-01-09 16:14:00 -0800871
872 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
873 n->nlmsg_flags & NLM_F_ECHO);
874 if (ret > 0)
875 return 0;
876 return ret;
877}
878
879static int
Tom Goff7316ae82010-03-19 15:40:13 +0000880tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000881 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Patrick McHardycee63722008-01-23 20:33:32 -0800883 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000884 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800885 struct tc_action *act;
886 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Patrick McHardycee63722008-01-23 20:33:32 -0800888 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
889 if (ret < 0)
890 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000892 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700893 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000894 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700895 else
896 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800899 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
WANG Congddf97cc2016-02-22 15:57:53 -0800900 act = tcf_action_get_1(net, tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800901 if (IS_ERR(act)) {
902 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800904 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800905 act->order = i;
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400906 if (event == RTM_GETACTION)
907 act->tcfa_refcnt++;
WANG Cong33be6272013-12-15 20:15:05 -0800908 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910
911 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800912 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -0800914 ret = tcf_del_notify(net, n, &actions, portid);
915 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return ret;
918 }
919err:
WANG Congf07fed82016-08-13 22:34:56 -0700920 tcf_action_destroy(&actions, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return ret;
922}
923
WANG Conga56e1952014-01-09 16:14:00 -0800924static int
925tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
926 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 int err = 0;
930
931 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
932 if (!skb)
933 return -ENOBUFS;
934
WANG Conga56e1952014-01-09 16:14:00 -0800935 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
936 RTM_NEWACTION, 0, 0) <= 0) {
937 kfree_skb(skb);
938 return -EINVAL;
939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
WANG Conga56e1952014-01-09 16:14:00 -0800941 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
942 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (err > 0)
944 err = 0;
945 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400948static int tcf_action_add(struct net *net, struct nlattr *nla,
949 struct nlmsghdr *n, u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
951 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -0800952 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
WANG Cong33be6272013-12-15 20:15:05 -0800954 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
955 if (ret)
WANG Congf07fed82016-08-13 22:34:56 -0700956 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
WANG Congf07fed82016-08-13 22:34:56 -0700958 return tcf_add_notify(net, n, &actions, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
Thomas Graf661d2962013-03-21 07:45:29 +0000961static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900963 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800964 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +0000965 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 int ret = 0, ovr = 0;
967
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400968 if ((n->nlmsg_type != RTM_GETACTION) &&
969 !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000970 return -EPERM;
971
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800972 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
973 if (ret < 0)
974 return ret;
975
976 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000977 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return -EINVAL;
979 }
980
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000981 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 switch (n->nlmsg_type) {
983 case RTM_NEWACTION:
984 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300985 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 * Note that CREATE | EXCL implies that
987 * but since we want avoid ambiguity (eg when flags
988 * is zero) then just set this
989 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000990 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 ovr = 1;
992replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000993 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (ret == -EAGAIN)
995 goto replay;
996 break;
997 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000998 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000999 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 break;
1001 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001002 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001003 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 break;
1005 default:
1006 BUG();
1007 }
1008
1009 return ret;
1010}
1011
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001012static struct nlattr *find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001014 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001015 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1016 struct nlattr *nla[TCAA_MAX + 1];
1017 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Patrick McHardyc96c9472008-01-23 20:32:58 -08001019 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001021 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (tb1 == NULL)
1023 return NULL;
1024
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001025 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1026 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Patrick McHardy6d834e02008-01-23 20:32:42 -08001029 if (tb[1] == NULL)
1030 return NULL;
1031 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1032 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001034 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Thomas Graf26dab892006-07-05 20:45:06 -07001036 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001039static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
WANG Congddf97cc2016-02-22 15:57:53 -08001041 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001043 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001044 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 struct tc_action_ops *a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001047 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001048 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001051 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return 0;
1053 }
1054
Thomas Graf26dab892006-07-05 20:45:06 -07001055 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001056 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Eric W. Biederman15e47302012-09-07 20:12:54 +00001059 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001060 cb->nlh->nlmsg_type, sizeof(*t), 0);
1061 if (!nlh)
1062 goto out_module_put;
1063 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001065 t->tca__pad1 = 0;
1066 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001068 nest = nla_nest_start(skb, TCA_ACT_TAB);
1069 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001070 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
WANG Conga85a9702016-07-25 16:09:41 -07001072 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001074 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001077 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 ret = skb->len;
1079 } else
Jamal Hadi Salimebecaa62016-06-13 18:08:42 -04001080 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001082 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001083 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 nlh->nlmsg_flags |= NLM_F_MULTI;
1085 module_put(a_o->owner);
1086 return skb->len;
1087
David S. Miller8b00a532012-06-26 21:39:32 -07001088out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001090 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return skb->len;
1092}
1093
1094static int __init tc_action_init(void)
1095{
Greg Rosec7ac8672011-06-10 01:27:09 +00001096 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1097 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1098 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1099 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 return 0;
1102}
1103
1104subsys_initcall(tc_action_init);