blob: e10456ef6f7a43c1b1a3c153012805015c51e9f5 [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);
Eric Dumazet1c0d32f2016-12-04 09:48:16 -080044 gen_kill_estimator(&p->tcfa_rate_est);
WANG Cong89819dc2013-12-15 20:15:09 -080045 /*
WANG Congec0595c2016-07-25 16:09:42 -070046 * gen_estimator est_timer() might access p->tcfa_lock
WANG Cong89819dc2013-12-15 20:15:09 -080047 * or bstats, wait a RCU grace period before freeing p
48 */
WANG Congec0595c2016-07-25 16:09:42 -070049 call_rcu(&p->tcfa_rcu, free_tcf);
David S. Millere9ce1cd2006-08-21 23:54:55 -070050}
David S. Millere9ce1cd2006-08-21 23:54:55 -070051
WANG Congec0595c2016-07-25 16:09:42 -070052int __tcf_hash_release(struct tc_action *p, bool bind, bool strict)
David S. Millere9ce1cd2006-08-21 23:54:55 -070053{
54 int ret = 0;
55
56 if (p) {
57 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -070058 p->tcfa_bindcnt--;
59 else if (strict && p->tcfa_bindcnt > 0)
WANG Cong55334a52014-02-11 17:07:34 -080060 return -EPERM;
David S. Millere9ce1cd2006-08-21 23:54:55 -070061
WANG Congec0595c2016-07-25 16:09:42 -070062 p->tcfa_refcnt--;
63 if (p->tcfa_bindcnt <= 0 && p->tcfa_refcnt <= 0) {
64 if (p->ops->cleanup)
65 p->ops->cleanup(p, bind);
WANG Congec0595c2016-07-25 16:09:42 -070066 tcf_hash_destroy(p->hinfo, p);
WANG Cong1d4150c2016-02-22 15:57:52 -080067 ret = ACT_P_DELETED;
David S. Millere9ce1cd2006-08-21 23:54:55 -070068 }
69 }
Daniel Borkmann28e6b672015-07-29 23:35:25 +020070
David S. Millere9ce1cd2006-08-21 23:54:55 -070071 return ret;
72}
Daniel Borkmann28e6b672015-07-29 23:35:25 +020073EXPORT_SYMBOL(__tcf_hash_release);
David S. Millere9ce1cd2006-08-21 23:54:55 -070074
WANG Congddf97cc2016-02-22 15:57:53 -080075static int tcf_dump_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -070076 struct netlink_callback *cb)
David S. Millere9ce1cd2006-08-21 23:54:55 -070077{
Eric Dumazetcc7ec452011-01-19 19:26:56 +000078 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080079 struct nlattr *nest;
David S. Millere9ce1cd2006-08-21 23:54:55 -070080
WANG Cong89819dc2013-12-15 20:15:09 -080081 spin_lock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -070082
83 s_i = cb->args[0];
84
85 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Conga85a9702016-07-25 16:09:41 -070086 struct hlist_head *head;
WANG Congec0595c2016-07-25 16:09:42 -070087 struct tc_action *p;
WANG Conga85a9702016-07-25 16:09:41 -070088
WANG Cong89819dc2013-12-15 20:15:09 -080089 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
David S. Millere9ce1cd2006-08-21 23:54:55 -070090
WANG Congec0595c2016-07-25 16:09:42 -070091 hlist_for_each_entry_rcu(p, head, tcfa_head) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070092 index++;
93 if (index < s_i)
94 continue;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080095
WANG Conga85a9702016-07-25 16:09:41 -070096 nest = nla_nest_start(skb, n_i);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080097 if (nest == NULL)
98 goto nla_put_failure;
WANG Congec0595c2016-07-25 16:09:42 -070099 err = tcf_action_dump_1(skb, p, 0, 0);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700100 if (err < 0) {
101 index--;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800102 nlmsg_trim(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700103 goto done;
104 }
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800105 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700106 n_i++;
107 if (n_i >= TCA_ACT_MAX_PRIO)
108 goto done;
109 }
110 }
111done:
WANG Cong89819dc2013-12-15 20:15:09 -0800112 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700113 if (n_i)
114 cb->args[0] += n_i;
115 return n_i;
116
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800117nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800118 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700119 goto done;
120}
121
WANG Congddf97cc2016-02-22 15:57:53 -0800122static int tcf_del_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700123 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700124{
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800125 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000126 int i = 0, n_i = 0;
WANG Cong55334a52014-02-11 17:07:34 -0800127 int ret = -EINVAL;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700128
WANG Conga85a9702016-07-25 16:09:41 -0700129 nest = nla_nest_start(skb, 0);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800130 if (nest == NULL)
131 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700132 if (nla_put_string(skb, TCA_KIND, ops->kind))
David S. Miller1b34ec42012-03-29 05:11:39 -0400133 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700134 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Conga85a9702016-07-25 16:09:41 -0700135 struct hlist_head *head;
136 struct hlist_node *n;
WANG Congec0595c2016-07-25 16:09:42 -0700137 struct tc_action *p;
WANG Conga85a9702016-07-25 16:09:41 -0700138
WANG Cong89819dc2013-12-15 20:15:09 -0800139 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
WANG Congec0595c2016-07-25 16:09:42 -0700140 hlist_for_each_entry_safe(p, n, head, tcfa_head) {
141 ret = __tcf_hash_release(p, false, true);
WANG Cong55334a52014-02-11 17:07:34 -0800142 if (ret == ACT_P_DELETED) {
WANG Congec0595c2016-07-25 16:09:42 -0700143 module_put(p->ops->owner);
Jamal Hadi Salim805c1f42013-12-23 08:02:13 -0500144 n_i++;
WANG Cong55334a52014-02-11 17:07:34 -0800145 } else if (ret < 0)
146 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700147 }
148 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400149 if (nla_put_u32(skb, TCA_FCNT, n_i))
150 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800151 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700152
153 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800154nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800155 nla_nest_cancel(skb, nest);
WANG Cong55334a52014-02-11 17:07:34 -0800156 return ret;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700157}
158
WANG Congddf97cc2016-02-22 15:57:53 -0800159int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
160 struct netlink_callback *cb, int type,
WANG Conga85a9702016-07-25 16:09:41 -0700161 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700162{
WANG Congddf97cc2016-02-22 15:57:53 -0800163 struct tcf_hashinfo *hinfo = tn->hinfo;
164
David S. Millere9ce1cd2006-08-21 23:54:55 -0700165 if (type == RTM_DELACTION) {
WANG Conga85a9702016-07-25 16:09:41 -0700166 return tcf_del_walker(hinfo, skb, ops);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700167 } else if (type == RTM_GETACTION) {
WANG Conga85a9702016-07-25 16:09:41 -0700168 return tcf_dump_walker(hinfo, skb, cb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700169 } else {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000170 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700171 return -EINVAL;
172 }
173}
WANG Congddf97cc2016-02-22 15:57:53 -0800174EXPORT_SYMBOL(tcf_generic_walker);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700175
WANG Congec0595c2016-07-25 16:09:42 -0700176static struct tc_action *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700177{
WANG Congec0595c2016-07-25 16:09:42 -0700178 struct tc_action *p = NULL;
WANG Cong89819dc2013-12-15 20:15:09 -0800179 struct hlist_head *head;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700180
WANG Cong89819dc2013-12-15 20:15:09 -0800181 spin_lock_bh(&hinfo->lock);
182 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
WANG Congec0595c2016-07-25 16:09:42 -0700183 hlist_for_each_entry_rcu(p, head, tcfa_head)
184 if (p->tcfa_index == index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700185 break;
WANG Cong89819dc2013-12-15 20:15:09 -0800186 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700187
188 return p;
189}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700190
WANG Congddf97cc2016-02-22 15:57:53 -0800191u32 tcf_hash_new_index(struct tc_action_net *tn)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700192{
WANG Congddf97cc2016-02-22 15:57:53 -0800193 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congddafd342014-01-09 16:13:59 -0800194 u32 val = hinfo->index;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700195
196 do {
197 if (++val == 0)
198 val = 1;
199 } while (tcf_hash_lookup(val, hinfo));
200
WANG Congddafd342014-01-09 16:13:59 -0800201 hinfo->index = val;
Yang Yingliang17569fa2013-12-10 20:55:29 +0800202 return val;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700203}
204EXPORT_SYMBOL(tcf_hash_new_index);
205
WANG Conga85a9702016-07-25 16:09:41 -0700206int tcf_hash_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700207{
WANG Congddf97cc2016-02-22 15:57:53 -0800208 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700209 struct tc_action *p = tcf_hash_lookup(index, hinfo);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700210
211 if (p) {
WANG Congec0595c2016-07-25 16:09:42 -0700212 *a = p;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700213 return 1;
214 }
215 return 0;
216}
WANG Cong6e6a50c2014-01-17 11:37:03 -0800217EXPORT_SYMBOL(tcf_hash_search);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700218
WANG Conga85a9702016-07-25 16:09:41 -0700219bool tcf_hash_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
WANG Congb2313072016-06-13 13:46:28 -0700220 int bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700221{
WANG Congddf97cc2016-02-22 15:57:53 -0800222 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700223 struct tc_action *p = NULL;
224
David S. Millere9ce1cd2006-08-21 23:54:55 -0700225 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700226 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -0700227 p->tcfa_bindcnt++;
228 p->tcfa_refcnt++;
229 *a = p;
WANG Congb2313072016-06-13 13:46:28 -0700230 return true;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700231 }
WANG Congb2313072016-06-13 13:46:28 -0700232 return false;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700233}
234EXPORT_SYMBOL(tcf_hash_check);
235
WANG Cong86062032014-02-11 17:07:31 -0800236void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
237{
WANG Cong86062032014-02-11 17:07:31 -0800238 if (est)
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800239 gen_kill_estimator(&a->tcfa_rate_est);
WANG Congec0595c2016-07-25 16:09:42 -0700240 call_rcu(&a->tcfa_rcu, free_tcf);
WANG Cong86062032014-02-11 17:07:31 -0800241}
242EXPORT_SYMBOL(tcf_hash_cleanup);
243
WANG Congddf97cc2016-02-22 15:57:53 -0800244int tcf_hash_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
WANG Conga85a9702016-07-25 16:09:41 -0700245 struct tc_action **a, const struct tc_action_ops *ops,
246 int bind, bool cpustats)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700247{
WANG Congec0595c2016-07-25 16:09:42 -0700248 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
WANG Congddf97cc2016-02-22 15:57:53 -0800249 struct tcf_hashinfo *hinfo = tn->hinfo;
Eric Dumazet519c8182015-07-06 05:18:04 -0700250 int err = -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700251
252 if (unlikely(!p))
WANG Cong86062032014-02-11 17:07:31 -0800253 return -ENOMEM;
WANG Congec0595c2016-07-25 16:09:42 -0700254 p->tcfa_refcnt = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700255 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -0700256 p->tcfa_bindcnt = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700257
Eric Dumazet519c8182015-07-06 05:18:04 -0700258 if (cpustats) {
259 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
260 if (!p->cpu_bstats) {
261err1:
262 kfree(p);
263 return err;
264 }
265 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
266 if (!p->cpu_qstats) {
267err2:
268 free_percpu(p->cpu_bstats);
269 goto err1;
270 }
271 }
WANG Congec0595c2016-07-25 16:09:42 -0700272 spin_lock_init(&p->tcfa_lock);
273 INIT_HLIST_NODE(&p->tcfa_head);
274 p->tcfa_index = index ? index : tcf_hash_new_index(tn);
275 p->tcfa_tm.install = jiffies;
276 p->tcfa_tm.lastuse = jiffies;
277 p->tcfa_tm.firstuse = 0;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800278 if (est) {
WANG Congec0595c2016-07-25 16:09:42 -0700279 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
280 &p->tcfa_rate_est,
281 &p->tcfa_lock, NULL, est);
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800282 if (err) {
Eric Dumazet519c8182015-07-06 05:18:04 -0700283 free_percpu(p->cpu_qstats);
284 goto err2;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800285 }
286 }
287
WANG Congec0595c2016-07-25 16:09:42 -0700288 p->hinfo = hinfo;
289 p->ops = ops;
290 INIT_LIST_HEAD(&p->list);
291 *a = p;
WANG Cong86062032014-02-11 17:07:31 -0800292 return 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700293}
294EXPORT_SYMBOL(tcf_hash_create);
295
WANG Congddf97cc2016-02-22 15:57:53 -0800296void tcf_hash_insert(struct tc_action_net *tn, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700297{
WANG Congddf97cc2016-02-22 15:57:53 -0800298 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700299 unsigned int h = tcf_hash(a->tcfa_index, hinfo->hmask);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700300
WANG Cong89819dc2013-12-15 20:15:09 -0800301 spin_lock_bh(&hinfo->lock);
WANG Congec0595c2016-07-25 16:09:42 -0700302 hlist_add_head(&a->tcfa_head, &hinfo->htab[h]);
WANG Cong89819dc2013-12-15 20:15:09 -0800303 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700304}
305EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
WANG Congddf97cc2016-02-22 15:57:53 -0800307void tcf_hashinfo_destroy(const struct tc_action_ops *ops,
308 struct tcf_hashinfo *hinfo)
WANG Cong1d4150c2016-02-22 15:57:52 -0800309{
WANG Cong1d4150c2016-02-22 15:57:52 -0800310 int i;
311
312 for (i = 0; i < hinfo->hmask + 1; i++) {
WANG Congec0595c2016-07-25 16:09:42 -0700313 struct tc_action *p;
WANG Cong1d4150c2016-02-22 15:57:52 -0800314 struct hlist_node *n;
315
WANG Congec0595c2016-07-25 16:09:42 -0700316 hlist_for_each_entry_safe(p, n, &hinfo->htab[i], tcfa_head) {
WANG Cong1d4150c2016-02-22 15:57:52 -0800317 int ret;
318
WANG Congec0595c2016-07-25 16:09:42 -0700319 ret = __tcf_hash_release(p, false, true);
WANG Cong1d4150c2016-02-22 15:57:52 -0800320 if (ret == ACT_P_DELETED)
321 module_put(ops->owner);
322 else if (ret < 0)
323 return;
324 }
325 }
326 kfree(hinfo->htab);
327}
WANG Congddf97cc2016-02-22 15:57:53 -0800328EXPORT_SYMBOL(tcf_hashinfo_destroy);
WANG Cong1d4150c2016-02-22 15:57:52 -0800329
WANG Cong1f747c22013-12-15 20:15:10 -0800330static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331static DEFINE_RWLOCK(act_mod_lock);
332
WANG Congddf97cc2016-02-22 15:57:53 -0800333int tcf_register_action(struct tc_action_ops *act,
334 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
WANG Cong1f747c22013-12-15 20:15:10 -0800336 struct tc_action_ops *a;
WANG Congddf97cc2016-02-22 15:57:53 -0800337 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
WANG Congddf97cc2016-02-22 15:57:53 -0800339 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500340 return -EINVAL;
341
WANG Congab102b82016-10-11 10:56:45 -0700342 /* We have to register pernet ops before making the action ops visible,
343 * otherwise tcf_action_init_1() could get a partially initialized
344 * netns.
345 */
346 ret = register_pernet_subsys(ops);
347 if (ret)
348 return ret;
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800351 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
353 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700354 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return -EEXIST;
356 }
357 }
WANG Cong1f747c22013-12-15 20:15:10 -0800358 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 write_unlock(&act_mod_lock);
WANG Congddf97cc2016-02-22 15:57:53 -0800360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return 0;
362}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800363EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
WANG Congddf97cc2016-02-22 15:57:53 -0800365int tcf_unregister_action(struct tc_action_ops *act,
366 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
WANG Cong1f747c22013-12-15 20:15:10 -0800368 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 int err = -ENOENT;
370
371 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800372 list_for_each_entry(a, &act_base, head) {
373 if (a == act) {
374 list_del(&act->head);
375 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700380 if (!err)
381 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return err;
383}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800384EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386/* lookup by name */
387static struct tc_action_ops *tc_lookup_action_n(char *kind)
388{
Eric Dumazeta7928662013-12-20 12:32:32 -0800389 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 if (kind) {
392 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800393 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800395 if (try_module_get(a->owner))
396 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 break;
398 }
399 }
400 read_unlock(&act_mod_lock);
401 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800402 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800405/* lookup by nlattr */
406static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Eric Dumazeta7928662013-12-20 12:32:32 -0800408 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 if (kind) {
411 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800412 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800413 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800414 if (try_module_get(a->owner))
415 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 break;
417 }
418 }
419 read_unlock(&act_mod_lock);
420 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800421 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
WANG Cong22dc13c2016-08-13 22:35:00 -0700424int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
425 int nr_actions, struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
WANG Cong22dc13c2016-08-13 22:35:00 -0700427 int ret = -1, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 if (skb->tc_verd & TC_NCLS) {
430 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 ret = TC_ACT_OK;
432 goto exec_done;
433 }
WANG Cong22dc13c2016-08-13 22:35:00 -0700434 for (i = 0; i < nr_actions; i++) {
435 const struct tc_action *a = actions[i];
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500438 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500439 if (ret == TC_ACT_REPEAT)
440 goto repeat; /* we need a ttl - JHS */
441 if (ret != TC_ACT_PIPE)
442 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return ret;
446}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800447EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
WANG Cong55334a52014-02-11 17:07:34 -0800449int tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
WANG Cong33be6272013-12-15 20:15:05 -0800451 struct tc_action *a, *tmp;
WANG Cong55334a52014-02-11 17:07:34 -0800452 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
WANG Cong33be6272013-12-15 20:15:05 -0800454 list_for_each_entry_safe(a, tmp, actions, list) {
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200455 ret = __tcf_hash_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800456 if (ret == ACT_P_DELETED)
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500457 module_put(a->ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800458 else if (ret < 0)
459 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
WANG Cong55334a52014-02-11 17:07:34 -0800461 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
464int
465tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
466{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return a->ops->dump(skb, a, bind, ref);
468}
469
470int
471tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
472{
473 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700474 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800475 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
David S. Miller1b34ec42012-03-29 05:11:39 -0400477 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
478 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800480 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800481 nest = nla_nest_start(skb, TCA_OPTIONS);
482 if (nest == NULL)
483 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000484 err = tcf_action_dump_old(skb, a, bind, ref);
485 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800486 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return err;
488 }
489
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800490nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700491 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return -1;
493}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800494EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400496int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
497 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 struct tc_action *a;
500 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800501 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
WANG Cong33be6272013-12-15 20:15:05 -0800503 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800504 nest = nla_nest_start(skb, a->order);
505 if (nest == NULL)
506 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 err = tcf_action_dump_1(skb, a, bind, ref);
508 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700509 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800510 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512
513 return 0;
514
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800515nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700516 err = -EINVAL;
517errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800518 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700519 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000522struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
523 struct nlattr *est, char *name, int ovr,
524 int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 struct tc_action *a;
527 struct tc_action_ops *a_o;
528 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000529 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800530 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800531 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800534 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
535 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800537 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800538 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (kind == NULL)
540 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800541 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 goto err_out;
543 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800544 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
546 goto err_out;
547 }
548
549 a_o = tc_lookup_action_n(act_name);
550 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700551#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800553 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 rtnl_lock();
555
556 a_o = tc_lookup_action_n(act_name);
557
558 /* We dropped the RTNL semaphore in order to
559 * perform the module load. So, even if we
560 * succeeded in loading the module we have to
561 * tell the caller to replay the request. We
562 * indicate this using -EAGAIN.
563 */
564 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800565 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 goto err_mod;
567 }
568#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800569 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 goto err_out;
571 }
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /* backward compatibility for policer */
574 if (name == NULL)
WANG Conga85a9702016-07-25 16:09:41 -0700575 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 else
WANG Conga85a9702016-07-25 16:09:41 -0700577 err = a_o->init(net, nla, est, &a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800578 if (err < 0)
WANG Conga85a9702016-07-25 16:09:41 -0700579 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000582 * if it exists and is only bound to in a_o->init() then
583 * ACT_P_CREATED is not returned (a zero is).
584 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800585 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return a;
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590err_mod:
591 module_put(a_o->owner);
592err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800593 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400596static void cleanup_a(struct list_head *actions, int ovr)
597{
598 struct tc_action *a;
599
600 if (!ovr)
601 return;
602
603 list_for_each_entry(a, actions, list)
604 a->tcfa_refcnt--;
605}
606
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400607int tcf_action_init(struct net *net, struct nlattr *nla, struct nlattr *est,
608 char *name, int ovr, int bind, struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000610 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800611 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800612 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 int i;
614
Patrick McHardycee63722008-01-23 20:33:32 -0800615 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
616 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800617 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800619 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000620 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800621 if (IS_ERR(act)) {
622 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800624 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800625 act->order = i;
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400626 if (ovr)
627 act->tcfa_refcnt++;
WANG Cong33be6272013-12-15 20:15:05 -0800628 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400630
631 /* Remove the temp refcnt which was necessary to protect against
632 * destroying an existing action which was being replaced
633 */
634 cleanup_a(actions, ovr);
WANG Cong33be6272013-12-15 20:15:05 -0800635 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637err:
WANG Cong33be6272013-12-15 20:15:05 -0800638 tcf_action_destroy(actions, bind);
639 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
WANG Congec0595c2016-07-25 16:09:42 -0700642int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 int compat_mode)
644{
645 int err = 0;
646 struct gnet_dump d;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900647
WANG Cong7eb88962014-01-09 16:14:05 -0800648 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 goto errout;
650
651 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400652 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 */
654 if (compat_mode) {
WANG Congec0595c2016-07-25 16:09:42 -0700655 if (p->type == TCA_OLD_COMPAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 err = gnet_stats_start_copy_compat(skb, 0,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200657 TCA_STATS,
658 TCA_XSTATS,
WANG Congec0595c2016-07-25 16:09:42 -0700659 &p->tcfa_lock, &d,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200660 TCA_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 else
662 return 0;
663 } else
664 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Congec0595c2016-07-25 16:09:42 -0700665 &p->tcfa_lock, &d, TCA_ACT_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 if (err < 0)
668 goto errout;
669
WANG Congec0595c2016-07-25 16:09:42 -0700670 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800671 gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -0700672 gnet_stats_copy_queue(&d, p->cpu_qstats,
WANG Congec0595c2016-07-25 16:09:42 -0700673 &p->tcfa_qstats,
674 p->tcfa_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 goto errout;
676
677 if (gnet_stats_finish_copy(&d) < 0)
678 goto errout;
679
680 return 0;
681
682errout:
683 return -1;
684}
685
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400686static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
687 u32 portid, u32 seq, u16 flags, int event, int bind,
688 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 struct tcamsg *t;
691 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700692 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800693 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Eric W. Biederman15e47302012-09-07 20:12:54 +0000695 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700696 if (!nlh)
697 goto out_nlmsg_trim;
698 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700700 t->tca__pad1 = 0;
701 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900702
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800703 nest = nla_nest_start(skb, TCA_ACT_TAB);
704 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700705 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
WANG Cong33be6272013-12-15 20:15:05 -0800707 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700708 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800710 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900711
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700712 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return skb->len;
714
David S. Miller8b00a532012-06-26 21:39:32 -0700715out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700716 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return -1;
718}
719
720static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000721act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800722 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
727 if (!skb)
728 return -ENOBUFS;
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400729 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
730 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 kfree_skb(skb);
732 return -EINVAL;
733 }
Thomas Graf2942e902006-08-15 00:30:25 -0700734
Eric W. Biederman15e47302012-09-07 20:12:54 +0000735 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
WANG Congddf97cc2016-02-22 15:57:53 -0800738static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
739 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000741 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700742 const struct tc_action_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 struct tc_action *a;
744 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800745 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Patrick McHardycee63722008-01-23 20:33:32 -0800747 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
748 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800749 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Patrick McHardycee63722008-01-23 20:33:32 -0800751 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800752 if (tb[TCA_ACT_INDEX] == NULL ||
753 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800754 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800755 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800757 err = -EINVAL;
WANG Conga85a9702016-07-25 16:09:41 -0700758 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
759 if (!ops) /* could happen in batch of actions */
760 goto err_out;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800761 err = -ENOENT;
WANG Conga85a9702016-07-25 16:09:41 -0700762 if (ops->lookup(net, &a, index) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 goto err_mod;
764
WANG Conga85a9702016-07-25 16:09:41 -0700765 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768err_mod:
WANG Conga85a9702016-07-25 16:09:41 -0700769 module_put(ops->owner);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800770err_out:
771 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Tom Goff7316ae82010-03-19 15:40:13 +0000774static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000775 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 struct sk_buff *skb;
778 unsigned char *b;
779 struct nlmsghdr *nlh;
780 struct tcamsg *t;
781 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800782 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000783 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700784 const struct tc_action_ops *ops;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800785 struct nlattr *kind;
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700786 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
789 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000790 pr_debug("tca_action_flush: failed skb alloc\n");
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700791 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700794 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Patrick McHardycee63722008-01-23 20:33:32 -0800796 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
797 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 goto err_out;
799
Patrick McHardycee63722008-01-23 20:33:32 -0800800 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800801 kind = tb[TCA_ACT_KIND];
WANG Conga85a9702016-07-25 16:09:41 -0700802 ops = tc_lookup_action(kind);
803 if (!ops) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 goto err_out;
805
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400806 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
807 sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700808 if (!nlh)
809 goto out_module_put;
810 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700812 t->tca__pad1 = 0;
813 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800815 nest = nla_nest_start(skb, TCA_ACT_TAB);
816 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700817 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
WANG Conga85a9702016-07-25 16:09:41 -0700819 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (err < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700821 goto out_module_put;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700822 if (err == 0)
823 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800825 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700827 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Conga85a9702016-07-25 16:09:41 -0700829 module_put(ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000830 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000831 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (err > 0)
833 return 0;
834
835 return err;
836
David S. Miller8b00a532012-06-26 21:39:32 -0700837out_module_put:
WANG Conga85a9702016-07-25 16:09:41 -0700838 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700840noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return err;
843}
844
845static int
WANG Conga56e1952014-01-09 16:14:00 -0800846tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
847 u32 portid)
848{
849 int ret;
850 struct sk_buff *skb;
851
852 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
853 if (!skb)
854 return -ENOBUFS;
855
856 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
857 0, 1) <= 0) {
858 kfree_skb(skb);
859 return -EINVAL;
860 }
861
862 /* now do the delete */
WANG Cong55334a52014-02-11 17:07:34 -0800863 ret = tcf_action_destroy(actions, 0);
864 if (ret < 0) {
865 kfree_skb(skb);
866 return ret;
867 }
WANG Conga56e1952014-01-09 16:14:00 -0800868
869 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
870 n->nlmsg_flags & NLM_F_ECHO);
871 if (ret > 0)
872 return 0;
873 return ret;
874}
875
876static int
Tom Goff7316ae82010-03-19 15:40:13 +0000877tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000878 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Patrick McHardycee63722008-01-23 20:33:32 -0800880 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000881 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800882 struct tc_action *act;
883 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Patrick McHardycee63722008-01-23 20:33:32 -0800885 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
886 if (ret < 0)
887 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000889 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700890 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000891 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700892 else
893 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800896 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
WANG Congddf97cc2016-02-22 15:57:53 -0800897 act = tcf_action_get_1(net, tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800898 if (IS_ERR(act)) {
899 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800901 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800902 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800903 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905
906 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800907 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -0800909 ret = tcf_del_notify(net, n, &actions, portid);
910 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return ret;
913 }
914err:
Jamal Hadi Salim0faa9cb2017-01-15 10:14:06 -0500915 if (event != RTM_GETACTION)
916 tcf_action_destroy(&actions, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return ret;
918}
919
WANG Conga56e1952014-01-09 16:14:00 -0800920static int
921tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
922 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 int err = 0;
926
927 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
928 if (!skb)
929 return -ENOBUFS;
930
WANG Conga56e1952014-01-09 16:14:00 -0800931 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
932 RTM_NEWACTION, 0, 0) <= 0) {
933 kfree_skb(skb);
934 return -EINVAL;
935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
WANG Conga56e1952014-01-09 16:14:00 -0800937 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
938 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (err > 0)
940 err = 0;
941 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400944static int tcf_action_add(struct net *net, struct nlattr *nla,
945 struct nlmsghdr *n, u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -0800948 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
WANG Cong33be6272013-12-15 20:15:05 -0800950 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
951 if (ret)
WANG Congf07fed82016-08-13 22:34:56 -0700952 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
WANG Congf07fed82016-08-13 22:34:56 -0700954 return tcf_add_notify(net, n, &actions, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
Thomas Graf661d2962013-03-21 07:45:29 +0000957static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900959 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800960 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +0000961 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 int ret = 0, ovr = 0;
963
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400964 if ((n->nlmsg_type != RTM_GETACTION) &&
965 !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000966 return -EPERM;
967
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800968 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
969 if (ret < 0)
970 return ret;
971
972 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000973 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return -EINVAL;
975 }
976
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000977 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 switch (n->nlmsg_type) {
979 case RTM_NEWACTION:
980 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300981 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 * Note that CREATE | EXCL implies that
983 * but since we want avoid ambiguity (eg when flags
984 * is zero) then just set this
985 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000986 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 ovr = 1;
988replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000989 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (ret == -EAGAIN)
991 goto replay;
992 break;
993 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000994 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000995 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 break;
997 case RTM_GETACTION:
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_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 break;
1001 default:
1002 BUG();
1003 }
1004
1005 return ret;
1006}
1007
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001008static struct nlattr *find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001010 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001011 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1012 struct nlattr *nla[TCAA_MAX + 1];
1013 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Patrick McHardyc96c9472008-01-23 20:32:58 -08001015 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001017 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (tb1 == NULL)
1019 return NULL;
1020
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001021 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1022 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Patrick McHardy6d834e02008-01-23 20:32:42 -08001025 if (tb[1] == NULL)
1026 return NULL;
Johannes Berg4700e9c2016-10-26 14:44:33 +02001027 if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001029 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Thomas Graf26dab892006-07-05 20:45:06 -07001031 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001034static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
WANG Congddf97cc2016-02-22 15:57:53 -08001036 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001038 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001039 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 struct tc_action_ops *a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001042 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001043 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001046 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return 0;
1048 }
1049
Thomas Graf26dab892006-07-05 20:45:06 -07001050 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001051 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Eric W. Biederman15e47302012-09-07 20:12:54 +00001054 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001055 cb->nlh->nlmsg_type, sizeof(*t), 0);
1056 if (!nlh)
1057 goto out_module_put;
1058 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001060 t->tca__pad1 = 0;
1061 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001063 nest = nla_nest_start(skb, TCA_ACT_TAB);
1064 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001065 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
WANG Conga85a9702016-07-25 16:09:41 -07001067 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001069 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001072 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 ret = skb->len;
1074 } else
Jamal Hadi Salimebecaa62016-06-13 18:08:42 -04001075 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001077 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001078 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 nlh->nlmsg_flags |= NLM_F_MULTI;
1080 module_put(a_o->owner);
1081 return skb->len;
1082
David S. Miller8b00a532012-06-26 21:39:32 -07001083out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001085 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return skb->len;
1087}
1088
1089static int __init tc_action_init(void)
1090{
Greg Rosec7ac8672011-06-10 01:27:09 +00001091 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1092 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1093 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1094 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return 0;
1097}
1098
1099subsys_initcall(tc_action_init);