blob: f04715a57300cc3e5ffc441ccac247ac0942e8ad [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
Willem de Bruijne7246e12017-01-07 17:06:35 -0500429 if (skb_skip_tc_classify(skb))
430 return TC_ACT_OK;
431
WANG Cong22dc13c2016-08-13 22:35:00 -0700432 for (i = 0; i < nr_actions; i++) {
433 const struct tc_action *a = actions[i];
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500436 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500437 if (ret == TC_ACT_REPEAT)
438 goto repeat; /* we need a ttl - JHS */
439 if (ret != TC_ACT_PIPE)
Willem de Bruijne7246e12017-01-07 17:06:35 -0500440 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return ret;
443}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800444EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
WANG Cong55334a52014-02-11 17:07:34 -0800446int tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
WANG Cong33be6272013-12-15 20:15:05 -0800448 struct tc_action *a, *tmp;
WANG Cong55334a52014-02-11 17:07:34 -0800449 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
WANG Cong33be6272013-12-15 20:15:05 -0800451 list_for_each_entry_safe(a, tmp, actions, list) {
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200452 ret = __tcf_hash_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800453 if (ret == ACT_P_DELETED)
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500454 module_put(a->ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800455 else if (ret < 0)
456 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
WANG Cong55334a52014-02-11 17:07:34 -0800458 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461int
462tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
463{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return a->ops->dump(skb, a, bind, ref);
465}
466
467int
468tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
469{
470 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700471 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800472 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
David S. Miller1b34ec42012-03-29 05:11:39 -0400474 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
475 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800477 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800478 nest = nla_nest_start(skb, TCA_OPTIONS);
479 if (nest == NULL)
480 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000481 err = tcf_action_dump_old(skb, a, bind, ref);
482 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800483 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return err;
485 }
486
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800487nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700488 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return -1;
490}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800491EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400493int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
494 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 struct tc_action *a;
497 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800498 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
WANG Cong33be6272013-12-15 20:15:05 -0800500 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800501 nest = nla_nest_start(skb, a->order);
502 if (nest == NULL)
503 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 err = tcf_action_dump_1(skb, a, bind, ref);
505 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700506 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800507 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509
510 return 0;
511
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800512nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700513 err = -EINVAL;
514errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800515 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700516 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000519struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
520 struct nlattr *est, char *name, int ovr,
521 int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 struct tc_action *a;
524 struct tc_action_ops *a_o;
525 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000526 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800527 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800528 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800531 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
532 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800534 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800535 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (kind == NULL)
537 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800538 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 goto err_out;
540 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800541 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
543 goto err_out;
544 }
545
546 a_o = tc_lookup_action_n(act_name);
547 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700548#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800550 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 rtnl_lock();
552
553 a_o = tc_lookup_action_n(act_name);
554
555 /* We dropped the RTNL semaphore in order to
556 * perform the module load. So, even if we
557 * succeeded in loading the module we have to
558 * tell the caller to replay the request. We
559 * indicate this using -EAGAIN.
560 */
561 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800562 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 goto err_mod;
564 }
565#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800566 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 goto err_out;
568 }
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /* backward compatibility for policer */
571 if (name == NULL)
WANG Conga85a9702016-07-25 16:09:41 -0700572 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 else
WANG Conga85a9702016-07-25 16:09:41 -0700574 err = a_o->init(net, nla, est, &a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800575 if (err < 0)
WANG Conga85a9702016-07-25 16:09:41 -0700576 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000579 * if it exists and is only bound to in a_o->init() then
580 * ACT_P_CREATED is not returned (a zero is).
581 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800582 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return a;
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587err_mod:
588 module_put(a_o->owner);
589err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800590 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400593static void cleanup_a(struct list_head *actions, int ovr)
594{
595 struct tc_action *a;
596
597 if (!ovr)
598 return;
599
600 list_for_each_entry(a, actions, list)
601 a->tcfa_refcnt--;
602}
603
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400604int tcf_action_init(struct net *net, struct nlattr *nla, struct nlattr *est,
605 char *name, int ovr, int bind, struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000607 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800608 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800609 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 int i;
611
Patrick McHardycee63722008-01-23 20:33:32 -0800612 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
613 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800614 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800616 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000617 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800618 if (IS_ERR(act)) {
619 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800621 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800622 act->order = i;
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400623 if (ovr)
624 act->tcfa_refcnt++;
WANG Cong33be6272013-12-15 20:15:05 -0800625 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400627
628 /* Remove the temp refcnt which was necessary to protect against
629 * destroying an existing action which was being replaced
630 */
631 cleanup_a(actions, ovr);
WANG Cong33be6272013-12-15 20:15:05 -0800632 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634err:
WANG Cong33be6272013-12-15 20:15:05 -0800635 tcf_action_destroy(actions, bind);
636 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
WANG Congec0595c2016-07-25 16:09:42 -0700639int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 int compat_mode)
641{
642 int err = 0;
643 struct gnet_dump d;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900644
WANG Cong7eb88962014-01-09 16:14:05 -0800645 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 goto errout;
647
648 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400649 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 */
651 if (compat_mode) {
WANG Congec0595c2016-07-25 16:09:42 -0700652 if (p->type == TCA_OLD_COMPAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 err = gnet_stats_start_copy_compat(skb, 0,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200654 TCA_STATS,
655 TCA_XSTATS,
WANG Congec0595c2016-07-25 16:09:42 -0700656 &p->tcfa_lock, &d,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200657 TCA_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 else
659 return 0;
660 } else
661 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Congec0595c2016-07-25 16:09:42 -0700662 &p->tcfa_lock, &d, TCA_ACT_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 if (err < 0)
665 goto errout;
666
WANG Congec0595c2016-07-25 16:09:42 -0700667 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800668 gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -0700669 gnet_stats_copy_queue(&d, p->cpu_qstats,
WANG Congec0595c2016-07-25 16:09:42 -0700670 &p->tcfa_qstats,
671 p->tcfa_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 goto errout;
673
674 if (gnet_stats_finish_copy(&d) < 0)
675 goto errout;
676
677 return 0;
678
679errout:
680 return -1;
681}
682
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400683static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
684 u32 portid, u32 seq, u16 flags, int event, int bind,
685 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 struct tcamsg *t;
688 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700689 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800690 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Eric W. Biederman15e47302012-09-07 20:12:54 +0000692 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700693 if (!nlh)
694 goto out_nlmsg_trim;
695 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700697 t->tca__pad1 = 0;
698 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900699
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800700 nest = nla_nest_start(skb, TCA_ACT_TAB);
701 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700702 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
WANG Cong33be6272013-12-15 20:15:05 -0800704 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700705 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800707 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900708
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700709 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return skb->len;
711
David S. Miller8b00a532012-06-26 21:39:32 -0700712out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700713 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return -1;
715}
716
717static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000718act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800719 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
724 if (!skb)
725 return -ENOBUFS;
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400726 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
727 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 kfree_skb(skb);
729 return -EINVAL;
730 }
Thomas Graf2942e902006-08-15 00:30:25 -0700731
Eric W. Biederman15e47302012-09-07 20:12:54 +0000732 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
WANG Congddf97cc2016-02-22 15:57:53 -0800735static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
736 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000738 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700739 const struct tc_action_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 struct tc_action *a;
741 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800742 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Patrick McHardycee63722008-01-23 20:33:32 -0800744 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
745 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800746 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Patrick McHardycee63722008-01-23 20:33:32 -0800748 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800749 if (tb[TCA_ACT_INDEX] == NULL ||
750 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800751 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800752 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800754 err = -EINVAL;
WANG Conga85a9702016-07-25 16:09:41 -0700755 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
756 if (!ops) /* could happen in batch of actions */
757 goto err_out;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800758 err = -ENOENT;
WANG Conga85a9702016-07-25 16:09:41 -0700759 if (ops->lookup(net, &a, index) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 goto err_mod;
761
WANG Conga85a9702016-07-25 16:09:41 -0700762 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765err_mod:
WANG Conga85a9702016-07-25 16:09:41 -0700766 module_put(ops->owner);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800767err_out:
768 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
Tom Goff7316ae82010-03-19 15:40:13 +0000771static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000772 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 struct sk_buff *skb;
775 unsigned char *b;
776 struct nlmsghdr *nlh;
777 struct tcamsg *t;
778 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800779 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000780 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700781 const struct tc_action_ops *ops;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800782 struct nlattr *kind;
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700783 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
786 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000787 pr_debug("tca_action_flush: failed skb alloc\n");
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700788 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700791 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Patrick McHardycee63722008-01-23 20:33:32 -0800793 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
794 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 goto err_out;
796
Patrick McHardycee63722008-01-23 20:33:32 -0800797 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800798 kind = tb[TCA_ACT_KIND];
WANG Conga85a9702016-07-25 16:09:41 -0700799 ops = tc_lookup_action(kind);
800 if (!ops) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 goto err_out;
802
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400803 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
804 sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700805 if (!nlh)
806 goto out_module_put;
807 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700809 t->tca__pad1 = 0;
810 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800812 nest = nla_nest_start(skb, TCA_ACT_TAB);
813 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700814 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
WANG Conga85a9702016-07-25 16:09:41 -0700816 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (err < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700818 goto out_module_put;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700819 if (err == 0)
820 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800822 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700824 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Conga85a9702016-07-25 16:09:41 -0700826 module_put(ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000827 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000828 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (err > 0)
830 return 0;
831
832 return err;
833
David S. Miller8b00a532012-06-26 21:39:32 -0700834out_module_put:
WANG Conga85a9702016-07-25 16:09:41 -0700835 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700837noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return err;
840}
841
842static int
WANG Conga56e1952014-01-09 16:14:00 -0800843tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
844 u32 portid)
845{
846 int ret;
847 struct sk_buff *skb;
848
849 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
850 if (!skb)
851 return -ENOBUFS;
852
853 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
854 0, 1) <= 0) {
855 kfree_skb(skb);
856 return -EINVAL;
857 }
858
859 /* now do the delete */
WANG Cong55334a52014-02-11 17:07:34 -0800860 ret = tcf_action_destroy(actions, 0);
861 if (ret < 0) {
862 kfree_skb(skb);
863 return ret;
864 }
WANG Conga56e1952014-01-09 16:14:00 -0800865
866 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
867 n->nlmsg_flags & NLM_F_ECHO);
868 if (ret > 0)
869 return 0;
870 return ret;
871}
872
873static int
Tom Goff7316ae82010-03-19 15:40:13 +0000874tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000875 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Patrick McHardycee63722008-01-23 20:33:32 -0800877 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000878 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800879 struct tc_action *act;
880 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Patrick McHardycee63722008-01-23 20:33:32 -0800882 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
883 if (ret < 0)
884 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000886 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700887 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000888 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700889 else
890 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800893 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
WANG Congddf97cc2016-02-22 15:57:53 -0800894 act = tcf_action_get_1(net, tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800895 if (IS_ERR(act)) {
896 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800898 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800899 act->order = i;
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400900 if (event == RTM_GETACTION)
901 act->tcfa_refcnt++;
WANG Cong33be6272013-12-15 20:15:05 -0800902 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904
905 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800906 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -0800908 ret = tcf_del_notify(net, n, &actions, portid);
909 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return ret;
912 }
913err:
WANG Congf07fed82016-08-13 22:34:56 -0700914 tcf_action_destroy(&actions, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return ret;
916}
917
WANG Conga56e1952014-01-09 16:14:00 -0800918static int
919tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
920 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 int err = 0;
924
925 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
926 if (!skb)
927 return -ENOBUFS;
928
WANG Conga56e1952014-01-09 16:14:00 -0800929 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
930 RTM_NEWACTION, 0, 0) <= 0) {
931 kfree_skb(skb);
932 return -EINVAL;
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
WANG Conga56e1952014-01-09 16:14:00 -0800935 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
936 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (err > 0)
938 err = 0;
939 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400942static int tcf_action_add(struct net *net, struct nlattr *nla,
943 struct nlmsghdr *n, u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
945 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -0800946 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
WANG Cong33be6272013-12-15 20:15:05 -0800948 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
949 if (ret)
WANG Congf07fed82016-08-13 22:34:56 -0700950 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
WANG Congf07fed82016-08-13 22:34:56 -0700952 return tcf_add_notify(net, n, &actions, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
Thomas Graf661d2962013-03-21 07:45:29 +0000955static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900957 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800958 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +0000959 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 int ret = 0, ovr = 0;
961
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400962 if ((n->nlmsg_type != RTM_GETACTION) &&
963 !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000964 return -EPERM;
965
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800966 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
967 if (ret < 0)
968 return ret;
969
970 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000971 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return -EINVAL;
973 }
974
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000975 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 switch (n->nlmsg_type) {
977 case RTM_NEWACTION:
978 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300979 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 * Note that CREATE | EXCL implies that
981 * but since we want avoid ambiguity (eg when flags
982 * is zero) then just set this
983 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000984 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 ovr = 1;
986replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000987 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (ret == -EAGAIN)
989 goto replay;
990 break;
991 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000992 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000993 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 break;
995 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000996 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000997 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 break;
999 default:
1000 BUG();
1001 }
1002
1003 return ret;
1004}
1005
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001006static struct nlattr *find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001008 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001009 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1010 struct nlattr *nla[TCAA_MAX + 1];
1011 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Patrick McHardyc96c9472008-01-23 20:32:58 -08001013 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001015 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (tb1 == NULL)
1017 return NULL;
1018
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001019 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1020 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Patrick McHardy6d834e02008-01-23 20:32:42 -08001023 if (tb[1] == NULL)
1024 return NULL;
Johannes Berg4700e9c2016-10-26 14:44:33 +02001025 if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001027 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Thomas Graf26dab892006-07-05 20:45:06 -07001029 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001032static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
WANG Congddf97cc2016-02-22 15:57:53 -08001034 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001036 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001037 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 struct tc_action_ops *a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001040 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001041 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001044 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return 0;
1046 }
1047
Thomas Graf26dab892006-07-05 20:45:06 -07001048 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001049 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Eric W. Biederman15e47302012-09-07 20:12:54 +00001052 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001053 cb->nlh->nlmsg_type, sizeof(*t), 0);
1054 if (!nlh)
1055 goto out_module_put;
1056 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001058 t->tca__pad1 = 0;
1059 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001061 nest = nla_nest_start(skb, TCA_ACT_TAB);
1062 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001063 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
WANG Conga85a9702016-07-25 16:09:41 -07001065 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001067 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001070 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 ret = skb->len;
1072 } else
Jamal Hadi Salimebecaa62016-06-13 18:08:42 -04001073 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001075 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001076 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 nlh->nlmsg_flags |= NLM_F_MULTI;
1078 module_put(a_o->owner);
1079 return skb->len;
1080
David S. Miller8b00a532012-06-26 21:39:32 -07001081out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001083 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return skb->len;
1085}
1086
1087static int __init tc_action_init(void)
1088{
Greg Rosec7ac8672011-06-10 01:27:09 +00001089 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1090 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1091 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1092 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return 0;
1095}
1096
1097subsys_initcall(tc_action_init);