blob: 5b8f8b382a2e7aedc1ecc6f5ed999352ed59f52c [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);
Craig Dillabaugh4363b972018-03-26 14:58:32 -040098 if (nest == NULL) {
99 index--;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800100 goto nla_put_failure;
Craig Dillabaugh4363b972018-03-26 14:58:32 -0400101 }
WANG Congec0595c2016-07-25 16:09:42 -0700102 err = tcf_action_dump_1(skb, p, 0, 0);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700103 if (err < 0) {
104 index--;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800105 nlmsg_trim(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700106 goto done;
107 }
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800108 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700109 n_i++;
110 if (n_i >= TCA_ACT_MAX_PRIO)
111 goto done;
112 }
113 }
114done:
WANG Cong89819dc2013-12-15 20:15:09 -0800115 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700116 if (n_i)
117 cb->args[0] += n_i;
118 return n_i;
119
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800120nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800121 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700122 goto done;
123}
124
WANG Congddf97cc2016-02-22 15:57:53 -0800125static int tcf_del_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700126 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700127{
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800128 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000129 int i = 0, n_i = 0;
WANG Cong55334a52014-02-11 17:07:34 -0800130 int ret = -EINVAL;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700131
WANG Conga85a9702016-07-25 16:09:41 -0700132 nest = nla_nest_start(skb, 0);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800133 if (nest == NULL)
134 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700135 if (nla_put_string(skb, TCA_KIND, ops->kind))
David S. Miller1b34ec42012-03-29 05:11:39 -0400136 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700137 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Conga85a9702016-07-25 16:09:41 -0700138 struct hlist_head *head;
139 struct hlist_node *n;
WANG Congec0595c2016-07-25 16:09:42 -0700140 struct tc_action *p;
WANG Conga85a9702016-07-25 16:09:41 -0700141
WANG Cong89819dc2013-12-15 20:15:09 -0800142 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
WANG Congec0595c2016-07-25 16:09:42 -0700143 hlist_for_each_entry_safe(p, n, head, tcfa_head) {
144 ret = __tcf_hash_release(p, false, true);
WANG Cong55334a52014-02-11 17:07:34 -0800145 if (ret == ACT_P_DELETED) {
Jiri Pirkof86d3b12017-09-13 17:32:37 +0200146 module_put(ops->owner);
Jamal Hadi Salim805c1f42013-12-23 08:02:13 -0500147 n_i++;
WANG Cong55334a52014-02-11 17:07:34 -0800148 } else if (ret < 0)
149 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700150 }
151 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400152 if (nla_put_u32(skb, TCA_FCNT, n_i))
153 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800154 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700155
156 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800157nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800158 nla_nest_cancel(skb, nest);
WANG Cong55334a52014-02-11 17:07:34 -0800159 return ret;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700160}
161
WANG Congddf97cc2016-02-22 15:57:53 -0800162int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
163 struct netlink_callback *cb, int type,
WANG Conga85a9702016-07-25 16:09:41 -0700164 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700165{
WANG Congddf97cc2016-02-22 15:57:53 -0800166 struct tcf_hashinfo *hinfo = tn->hinfo;
167
David S. Millere9ce1cd2006-08-21 23:54:55 -0700168 if (type == RTM_DELACTION) {
WANG Conga85a9702016-07-25 16:09:41 -0700169 return tcf_del_walker(hinfo, skb, ops);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700170 } else if (type == RTM_GETACTION) {
WANG Conga85a9702016-07-25 16:09:41 -0700171 return tcf_dump_walker(hinfo, skb, cb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700172 } else {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000173 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700174 return -EINVAL;
175 }
176}
WANG Congddf97cc2016-02-22 15:57:53 -0800177EXPORT_SYMBOL(tcf_generic_walker);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700178
WANG Congec0595c2016-07-25 16:09:42 -0700179static struct tc_action *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700180{
WANG Congec0595c2016-07-25 16:09:42 -0700181 struct tc_action *p = NULL;
WANG Cong89819dc2013-12-15 20:15:09 -0800182 struct hlist_head *head;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700183
WANG Cong89819dc2013-12-15 20:15:09 -0800184 spin_lock_bh(&hinfo->lock);
185 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
WANG Congec0595c2016-07-25 16:09:42 -0700186 hlist_for_each_entry_rcu(p, head, tcfa_head)
187 if (p->tcfa_index == index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700188 break;
WANG Cong89819dc2013-12-15 20:15:09 -0800189 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700190
191 return p;
192}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700193
WANG Congddf97cc2016-02-22 15:57:53 -0800194u32 tcf_hash_new_index(struct tc_action_net *tn)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700195{
WANG Congddf97cc2016-02-22 15:57:53 -0800196 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congddafd342014-01-09 16:13:59 -0800197 u32 val = hinfo->index;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700198
199 do {
200 if (++val == 0)
201 val = 1;
202 } while (tcf_hash_lookup(val, hinfo));
203
WANG Congddafd342014-01-09 16:13:59 -0800204 hinfo->index = val;
Yang Yingliang17569fa2013-12-10 20:55:29 +0800205 return val;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700206}
207EXPORT_SYMBOL(tcf_hash_new_index);
208
WANG Conga85a9702016-07-25 16:09:41 -0700209int tcf_hash_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700210{
WANG Congddf97cc2016-02-22 15:57:53 -0800211 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700212 struct tc_action *p = tcf_hash_lookup(index, hinfo);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700213
214 if (p) {
WANG Congec0595c2016-07-25 16:09:42 -0700215 *a = p;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700216 return 1;
217 }
218 return 0;
219}
WANG Cong6e6a50c2014-01-17 11:37:03 -0800220EXPORT_SYMBOL(tcf_hash_search);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700221
WANG Conga85a9702016-07-25 16:09:41 -0700222bool tcf_hash_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
WANG Congb2313072016-06-13 13:46:28 -0700223 int bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700224{
WANG Congddf97cc2016-02-22 15:57:53 -0800225 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700226 struct tc_action *p = NULL;
227
David S. Millere9ce1cd2006-08-21 23:54:55 -0700228 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700229 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -0700230 p->tcfa_bindcnt++;
231 p->tcfa_refcnt++;
232 *a = p;
WANG Congb2313072016-06-13 13:46:28 -0700233 return true;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700234 }
WANG Congb2313072016-06-13 13:46:28 -0700235 return false;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700236}
237EXPORT_SYMBOL(tcf_hash_check);
238
WANG Cong86062032014-02-11 17:07:31 -0800239void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
240{
WANG Cong86062032014-02-11 17:07:31 -0800241 if (est)
WANG Congec0595c2016-07-25 16:09:42 -0700242 gen_kill_estimator(&a->tcfa_bstats,
243 &a->tcfa_rate_est);
244 call_rcu(&a->tcfa_rcu, free_tcf);
WANG Cong86062032014-02-11 17:07:31 -0800245}
246EXPORT_SYMBOL(tcf_hash_cleanup);
247
WANG Congddf97cc2016-02-22 15:57:53 -0800248int tcf_hash_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
WANG Conga85a9702016-07-25 16:09:41 -0700249 struct tc_action **a, const struct tc_action_ops *ops,
250 int bind, bool cpustats)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700251{
WANG Congec0595c2016-07-25 16:09:42 -0700252 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
WANG Congddf97cc2016-02-22 15:57:53 -0800253 struct tcf_hashinfo *hinfo = tn->hinfo;
Eric Dumazet519c8182015-07-06 05:18:04 -0700254 int err = -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700255
256 if (unlikely(!p))
WANG Cong86062032014-02-11 17:07:31 -0800257 return -ENOMEM;
WANG Congec0595c2016-07-25 16:09:42 -0700258 p->tcfa_refcnt = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700259 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -0700260 p->tcfa_bindcnt = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700261
Eric Dumazet519c8182015-07-06 05:18:04 -0700262 if (cpustats) {
263 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
264 if (!p->cpu_bstats) {
265err1:
266 kfree(p);
267 return err;
268 }
269 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
270 if (!p->cpu_qstats) {
271err2:
272 free_percpu(p->cpu_bstats);
273 goto err1;
274 }
275 }
WANG Congec0595c2016-07-25 16:09:42 -0700276 spin_lock_init(&p->tcfa_lock);
277 INIT_HLIST_NODE(&p->tcfa_head);
278 p->tcfa_index = index ? index : tcf_hash_new_index(tn);
279 p->tcfa_tm.install = jiffies;
280 p->tcfa_tm.lastuse = jiffies;
281 p->tcfa_tm.firstuse = 0;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800282 if (est) {
WANG Congec0595c2016-07-25 16:09:42 -0700283 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
284 &p->tcfa_rate_est,
285 &p->tcfa_lock, NULL, est);
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800286 if (err) {
Eric Dumazet519c8182015-07-06 05:18:04 -0700287 free_percpu(p->cpu_qstats);
288 goto err2;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800289 }
290 }
291
WANG Congec0595c2016-07-25 16:09:42 -0700292 p->hinfo = hinfo;
293 p->ops = ops;
294 INIT_LIST_HEAD(&p->list);
295 *a = p;
WANG Cong86062032014-02-11 17:07:31 -0800296 return 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700297}
298EXPORT_SYMBOL(tcf_hash_create);
299
WANG Congddf97cc2016-02-22 15:57:53 -0800300void tcf_hash_insert(struct tc_action_net *tn, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700301{
WANG Congddf97cc2016-02-22 15:57:53 -0800302 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700303 unsigned int h = tcf_hash(a->tcfa_index, hinfo->hmask);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700304
WANG Cong89819dc2013-12-15 20:15:09 -0800305 spin_lock_bh(&hinfo->lock);
WANG Congec0595c2016-07-25 16:09:42 -0700306 hlist_add_head(&a->tcfa_head, &hinfo->htab[h]);
WANG Cong89819dc2013-12-15 20:15:09 -0800307 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700308}
309EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
WANG Congddf97cc2016-02-22 15:57:53 -0800311void tcf_hashinfo_destroy(const struct tc_action_ops *ops,
312 struct tcf_hashinfo *hinfo)
WANG Cong1d4150c2016-02-22 15:57:52 -0800313{
WANG Cong1d4150c2016-02-22 15:57:52 -0800314 int i;
315
316 for (i = 0; i < hinfo->hmask + 1; i++) {
WANG Congec0595c2016-07-25 16:09:42 -0700317 struct tc_action *p;
WANG Cong1d4150c2016-02-22 15:57:52 -0800318 struct hlist_node *n;
319
WANG Congec0595c2016-07-25 16:09:42 -0700320 hlist_for_each_entry_safe(p, n, &hinfo->htab[i], tcfa_head) {
WANG Cong1d4150c2016-02-22 15:57:52 -0800321 int ret;
322
WANG Congec0595c2016-07-25 16:09:42 -0700323 ret = __tcf_hash_release(p, false, true);
WANG Cong1d4150c2016-02-22 15:57:52 -0800324 if (ret == ACT_P_DELETED)
325 module_put(ops->owner);
326 else if (ret < 0)
327 return;
328 }
329 }
330 kfree(hinfo->htab);
331}
WANG Congddf97cc2016-02-22 15:57:53 -0800332EXPORT_SYMBOL(tcf_hashinfo_destroy);
WANG Cong1d4150c2016-02-22 15:57:52 -0800333
WANG Cong1f747c22013-12-15 20:15:10 -0800334static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335static DEFINE_RWLOCK(act_mod_lock);
336
WANG Congddf97cc2016-02-22 15:57:53 -0800337int tcf_register_action(struct tc_action_ops *act,
338 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
WANG Cong1f747c22013-12-15 20:15:10 -0800340 struct tc_action_ops *a;
WANG Congddf97cc2016-02-22 15:57:53 -0800341 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
WANG Congddf97cc2016-02-22 15:57:53 -0800343 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500344 return -EINVAL;
345
WANG Congab102b82016-10-11 10:56:45 -0700346 /* We have to register pernet ops before making the action ops visible,
347 * otherwise tcf_action_init_1() could get a partially initialized
348 * netns.
349 */
350 ret = register_pernet_subsys(ops);
351 if (ret)
352 return ret;
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800355 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
357 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700358 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return -EEXIST;
360 }
361 }
WANG Cong1f747c22013-12-15 20:15:10 -0800362 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 write_unlock(&act_mod_lock);
WANG Congddf97cc2016-02-22 15:57:53 -0800364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return 0;
366}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800367EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
WANG Congddf97cc2016-02-22 15:57:53 -0800369int tcf_unregister_action(struct tc_action_ops *act,
370 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
WANG Cong1f747c22013-12-15 20:15:10 -0800372 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 int err = -ENOENT;
374
375 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800376 list_for_each_entry(a, &act_base, head) {
377 if (a == act) {
378 list_del(&act->head);
379 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700384 if (!err)
385 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return err;
387}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800388EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390/* lookup by name */
391static struct tc_action_ops *tc_lookup_action_n(char *kind)
392{
Eric Dumazeta7928662013-12-20 12:32:32 -0800393 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 if (kind) {
396 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800397 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800399 if (try_module_get(a->owner))
400 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 break;
402 }
403 }
404 read_unlock(&act_mod_lock);
405 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800406 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800409/* lookup by nlattr */
410static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Eric Dumazeta7928662013-12-20 12:32:32 -0800412 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 if (kind) {
415 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800416 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800417 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800418 if (try_module_get(a->owner))
419 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 break;
421 }
422 }
423 read_unlock(&act_mod_lock);
424 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800425 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
WANG Cong22dc13c2016-08-13 22:35:00 -0700428int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
429 int nr_actions, struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
WANG Cong22dc13c2016-08-13 22:35:00 -0700431 int ret = -1, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (skb->tc_verd & TC_NCLS) {
434 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 ret = TC_ACT_OK;
436 goto exec_done;
437 }
WANG Cong22dc13c2016-08-13 22:35:00 -0700438 for (i = 0; i < nr_actions; i++) {
439 const struct tc_action *a = actions[i];
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500442 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500443 if (ret == TC_ACT_REPEAT)
444 goto repeat; /* we need a ttl - JHS */
445 if (ret != TC_ACT_PIPE)
446 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return ret;
450}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800451EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
WANG Cong55334a52014-02-11 17:07:34 -0800453int tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Jiri Pirkof86d3b12017-09-13 17:32:37 +0200455 const struct tc_action_ops *ops;
WANG Cong33be6272013-12-15 20:15:05 -0800456 struct tc_action *a, *tmp;
WANG Cong55334a52014-02-11 17:07:34 -0800457 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
WANG Cong33be6272013-12-15 20:15:05 -0800459 list_for_each_entry_safe(a, tmp, actions, list) {
Jiri Pirkof86d3b12017-09-13 17:32:37 +0200460 ops = a->ops;
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200461 ret = __tcf_hash_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800462 if (ret == ACT_P_DELETED)
Jiri Pirkof86d3b12017-09-13 17:32:37 +0200463 module_put(ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800464 else if (ret < 0)
465 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
WANG Cong55334a52014-02-11 17:07:34 -0800467 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
470int
471tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
472{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return a->ops->dump(skb, a, bind, ref);
474}
475
476int
477tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
478{
479 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700480 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800481 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
David S. Miller1b34ec42012-03-29 05:11:39 -0400483 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
484 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800486 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800487 nest = nla_nest_start(skb, TCA_OPTIONS);
488 if (nest == NULL)
489 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000490 err = tcf_action_dump_old(skb, a, bind, ref);
491 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800492 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return err;
494 }
495
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800496nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700497 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return -1;
499}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800500EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400502int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
503 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 struct tc_action *a;
506 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800507 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
WANG Cong33be6272013-12-15 20:15:05 -0800509 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800510 nest = nla_nest_start(skb, a->order);
511 if (nest == NULL)
512 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 err = tcf_action_dump_1(skb, a, bind, ref);
514 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700515 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800516 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518
519 return 0;
520
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800521nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700522 err = -EINVAL;
523errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800524 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700525 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000528struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
529 struct nlattr *est, char *name, int ovr,
530 int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 struct tc_action *a;
533 struct tc_action_ops *a_o;
534 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000535 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800536 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800537 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800540 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
541 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800543 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800544 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (kind == NULL)
546 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800547 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 goto err_out;
549 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800550 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
552 goto err_out;
553 }
554
555 a_o = tc_lookup_action_n(act_name);
556 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700557#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800559 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 rtnl_lock();
561
562 a_o = tc_lookup_action_n(act_name);
563
564 /* We dropped the RTNL semaphore in order to
565 * perform the module load. So, even if we
566 * succeeded in loading the module we have to
567 * tell the caller to replay the request. We
568 * indicate this using -EAGAIN.
569 */
570 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800571 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 goto err_mod;
573 }
574#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800575 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 goto err_out;
577 }
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* backward compatibility for policer */
580 if (name == NULL)
WANG Conga85a9702016-07-25 16:09:41 -0700581 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 else
WANG Conga85a9702016-07-25 16:09:41 -0700583 err = a_o->init(net, nla, est, &a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800584 if (err < 0)
WANG Conga85a9702016-07-25 16:09:41 -0700585 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000588 * if it exists and is only bound to in a_o->init() then
589 * ACT_P_CREATED is not returned (a zero is).
590 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800591 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return a;
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596err_mod:
597 module_put(a_o->owner);
598err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800599 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400602static void cleanup_a(struct list_head *actions, int ovr)
603{
604 struct tc_action *a;
605
606 if (!ovr)
607 return;
608
609 list_for_each_entry(a, actions, list)
610 a->tcfa_refcnt--;
611}
612
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400613int tcf_action_init(struct net *net, struct nlattr *nla, struct nlattr *est,
614 char *name, int ovr, int bind, struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000616 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800617 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800618 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 int i;
620
Patrick McHardycee63722008-01-23 20:33:32 -0800621 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
622 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800623 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800625 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000626 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800627 if (IS_ERR(act)) {
628 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800630 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800631 act->order = i;
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400632 if (ovr)
633 act->tcfa_refcnt++;
WANG Cong33be6272013-12-15 20:15:05 -0800634 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400636
637 /* Remove the temp refcnt which was necessary to protect against
638 * destroying an existing action which was being replaced
639 */
640 cleanup_a(actions, ovr);
WANG Cong33be6272013-12-15 20:15:05 -0800641 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643err:
WANG Cong33be6272013-12-15 20:15:05 -0800644 tcf_action_destroy(actions, bind);
645 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
WANG Congec0595c2016-07-25 16:09:42 -0700648int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 int compat_mode)
650{
651 int err = 0;
652 struct gnet_dump d;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900653
WANG Cong7eb88962014-01-09 16:14:05 -0800654 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 goto errout;
656
657 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400658 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 */
660 if (compat_mode) {
WANG Congec0595c2016-07-25 16:09:42 -0700661 if (p->type == TCA_OLD_COMPAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 err = gnet_stats_start_copy_compat(skb, 0,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200663 TCA_STATS,
664 TCA_XSTATS,
WANG Congec0595c2016-07-25 16:09:42 -0700665 &p->tcfa_lock, &d,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200666 TCA_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 else
668 return 0;
669 } else
670 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Congec0595c2016-07-25 16:09:42 -0700671 &p->tcfa_lock, &d, TCA_ACT_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 if (err < 0)
674 goto errout;
675
WANG Congec0595c2016-07-25 16:09:42 -0700676 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
677 gnet_stats_copy_rate_est(&d, &p->tcfa_bstats,
678 &p->tcfa_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -0700679 gnet_stats_copy_queue(&d, p->cpu_qstats,
WANG Congec0595c2016-07-25 16:09:42 -0700680 &p->tcfa_qstats,
681 p->tcfa_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 goto errout;
683
684 if (gnet_stats_finish_copy(&d) < 0)
685 goto errout;
686
687 return 0;
688
689errout:
690 return -1;
691}
692
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400693static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
694 u32 portid, u32 seq, u16 flags, int event, int bind,
695 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 struct tcamsg *t;
698 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700699 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800700 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Eric W. Biederman15e47302012-09-07 20:12:54 +0000702 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700703 if (!nlh)
704 goto out_nlmsg_trim;
705 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700707 t->tca__pad1 = 0;
708 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900709
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800710 nest = nla_nest_start(skb, TCA_ACT_TAB);
711 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700712 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
WANG Cong33be6272013-12-15 20:15:05 -0800714 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700715 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800717 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900718
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700719 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return skb->len;
721
David S. Miller8b00a532012-06-26 21:39:32 -0700722out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700723 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return -1;
725}
726
727static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000728act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800729 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
734 if (!skb)
735 return -ENOBUFS;
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400736 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
737 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 kfree_skb(skb);
739 return -EINVAL;
740 }
Thomas Graf2942e902006-08-15 00:30:25 -0700741
Eric W. Biederman15e47302012-09-07 20:12:54 +0000742 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
WANG Congddf97cc2016-02-22 15:57:53 -0800745static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
746 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000748 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700749 const struct tc_action_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 struct tc_action *a;
751 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800752 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Patrick McHardycee63722008-01-23 20:33:32 -0800754 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
755 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800756 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Patrick McHardycee63722008-01-23 20:33:32 -0800758 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800759 if (tb[TCA_ACT_INDEX] == NULL ||
760 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800761 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800762 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800764 err = -EINVAL;
WANG Conga85a9702016-07-25 16:09:41 -0700765 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
766 if (!ops) /* could happen in batch of actions */
767 goto err_out;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800768 err = -ENOENT;
WANG Conga85a9702016-07-25 16:09:41 -0700769 if (ops->lookup(net, &a, index) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 goto err_mod;
771
WANG Conga85a9702016-07-25 16:09:41 -0700772 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775err_mod:
WANG Conga85a9702016-07-25 16:09:41 -0700776 module_put(ops->owner);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800777err_out:
778 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
Tom Goff7316ae82010-03-19 15:40:13 +0000781static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000782 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 struct sk_buff *skb;
785 unsigned char *b;
786 struct nlmsghdr *nlh;
787 struct tcamsg *t;
788 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800789 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000790 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700791 const struct tc_action_ops *ops;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800792 struct nlattr *kind;
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700793 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
796 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000797 pr_debug("tca_action_flush: failed skb alloc\n");
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700798 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700801 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Patrick McHardycee63722008-01-23 20:33:32 -0800803 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
804 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 goto err_out;
806
Patrick McHardycee63722008-01-23 20:33:32 -0800807 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800808 kind = tb[TCA_ACT_KIND];
WANG Conga85a9702016-07-25 16:09:41 -0700809 ops = tc_lookup_action(kind);
810 if (!ops) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 goto err_out;
812
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400813 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
814 sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700815 if (!nlh)
816 goto out_module_put;
817 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700819 t->tca__pad1 = 0;
820 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800822 nest = nla_nest_start(skb, TCA_ACT_TAB);
823 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700824 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
WANG Conga85a9702016-07-25 16:09:41 -0700826 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops);
Roman Mashak063893e2017-02-24 11:00:32 -0500827 if (err <= 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700828 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800830 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700832 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Conga85a9702016-07-25 16:09:41 -0700834 module_put(ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000835 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000836 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (err > 0)
838 return 0;
839
840 return err;
841
David S. Miller8b00a532012-06-26 21:39:32 -0700842out_module_put:
WANG Conga85a9702016-07-25 16:09:41 -0700843 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844err_out:
845 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return err;
847}
848
849static int
WANG Conga56e1952014-01-09 16:14:00 -0800850tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
851 u32 portid)
852{
853 int ret;
854 struct sk_buff *skb;
855
856 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
857 if (!skb)
858 return -ENOBUFS;
859
860 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
861 0, 1) <= 0) {
862 kfree_skb(skb);
863 return -EINVAL;
864 }
865
866 /* now do the delete */
WANG Cong55334a52014-02-11 17:07:34 -0800867 ret = tcf_action_destroy(actions, 0);
868 if (ret < 0) {
869 kfree_skb(skb);
870 return ret;
871 }
WANG Conga56e1952014-01-09 16:14:00 -0800872
873 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
874 n->nlmsg_flags & NLM_F_ECHO);
875 if (ret > 0)
876 return 0;
877 return ret;
878}
879
880static int
Tom Goff7316ae82010-03-19 15:40:13 +0000881tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000882 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Patrick McHardycee63722008-01-23 20:33:32 -0800884 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000885 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800886 struct tc_action *act;
887 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Patrick McHardycee63722008-01-23 20:33:32 -0800889 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
890 if (ret < 0)
891 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000893 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700894 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000895 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700896 else
897 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800900 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
WANG Congddf97cc2016-02-22 15:57:53 -0800901 act = tcf_action_get_1(net, tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800902 if (IS_ERR(act)) {
903 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800905 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800906 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800907 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909
910 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800911 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -0800913 ret = tcf_del_notify(net, n, &actions, portid);
914 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return ret;
917 }
918err:
Jamal Hadi Salimb260a712017-01-15 10:14:06 -0500919 if (event != RTM_GETACTION)
920 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{
Eric Dumazetd1b2bdb2019-10-14 11:22:30 -0700951 int loop, ret;
WANG Cong33be6272013-12-15 20:15:05 -0800952 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Eric Dumazetd1b2bdb2019-10-14 11:22:30 -0700954 for (loop = 0; loop < 10; loop++) {
955 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
956 if (ret != -EAGAIN)
957 break;
958 }
959
WANG Cong33be6272013-12-15 20:15:05 -0800960 if (ret)
WANG Congf07fed82016-08-13 22:34:56 -0700961 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
WANG Congf07fed82016-08-13 22:34:56 -0700963 return tcf_add_notify(net, n, &actions, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
Thomas Graf661d2962013-03-21 07:45:29 +0000966static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900968 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800969 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +0000970 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 int ret = 0, ovr = 0;
972
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400973 if ((n->nlmsg_type != RTM_GETACTION) &&
974 !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000975 return -EPERM;
976
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800977 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
978 if (ret < 0)
979 return ret;
980
981 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000982 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return -EINVAL;
984 }
985
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000986 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 switch (n->nlmsg_type) {
988 case RTM_NEWACTION:
989 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300990 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 * Note that CREATE | EXCL implies that
992 * but since we want avoid ambiguity (eg when flags
993 * is zero) then just set this
994 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000995 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 ovr = 1;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000997 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 break;
999 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001000 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001001 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 break;
1003 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001004 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001005 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 break;
1007 default:
1008 BUG();
1009 }
1010
1011 return ret;
1012}
1013
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001014static struct nlattr *find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001016 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001017 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1018 struct nlattr *nla[TCAA_MAX + 1];
1019 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Patrick McHardyc96c9472008-01-23 20:32:58 -08001021 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001023 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (tb1 == NULL)
1025 return NULL;
1026
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001027 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1028 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Patrick McHardy6d834e02008-01-23 20:32:42 -08001031 if (tb[1] == NULL)
1032 return NULL;
Johannes Berg4700e9c2016-10-26 14:44:33 +02001033 if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001035 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Thomas Graf26dab892006-07-05 20:45:06 -07001037 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001040static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
WANG Congddf97cc2016-02-22 15:57:53 -08001042 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001044 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001045 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 struct tc_action_ops *a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001048 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001049 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001052 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return 0;
1054 }
1055
Thomas Graf26dab892006-07-05 20:45:06 -07001056 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001057 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Eric W. Biederman15e47302012-09-07 20:12:54 +00001060 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001061 cb->nlh->nlmsg_type, sizeof(*t), 0);
1062 if (!nlh)
1063 goto out_module_put;
1064 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001066 t->tca__pad1 = 0;
1067 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001069 nest = nla_nest_start(skb, TCA_ACT_TAB);
1070 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001071 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
WANG Conga85a9702016-07-25 16:09:41 -07001073 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001075 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001078 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 ret = skb->len;
1080 } else
Jamal Hadi Salimebecaa62016-06-13 18:08:42 -04001081 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001083 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001084 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 nlh->nlmsg_flags |= NLM_F_MULTI;
1086 module_put(a_o->owner);
1087 return skb->len;
1088
David S. Miller8b00a532012-06-26 21:39:32 -07001089out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001091 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return skb->len;
1093}
1094
1095static int __init tc_action_init(void)
1096{
Greg Rosec7ac8672011-06-10 01:27:09 +00001097 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1098 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1099 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1100 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return 0;
1103}
1104
1105subsys_initcall(tc_action_init);