blob: e4a5f2607ffa2edb6c9c3206c96b578946edfa57 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sched/act_api.c Packet action API.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Author: Jamal Hadi Salim
10 *
11 *
12 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/types.h>
15#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/init.h>
21#include <linux/kmod.h>
Patrick McHardyab27cfb2008-01-23 20:33:13 -080022#include <linux/err.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040023#include <linux/module.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110024#include <net/net_namespace.h>
25#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <net/sch_generic.h>
27#include <net/act_api.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070028#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Eric Dumazet519c8182015-07-06 05:18:04 -070030static void free_tcf(struct rcu_head *head)
31{
WANG Congec0595c2016-07-25 16:09:42 -070032 struct tc_action *p = container_of(head, struct tc_action, tcfa_rcu);
Eric Dumazet519c8182015-07-06 05:18:04 -070033
34 free_percpu(p->cpu_bstats);
35 free_percpu(p->cpu_qstats);
36 kfree(p);
37}
38
WANG Congec0595c2016-07-25 16:09:42 -070039static void tcf_hash_destroy(struct tcf_hashinfo *hinfo, struct tc_action *p)
David S. Millere9ce1cd2006-08-21 23:54:55 -070040{
WANG Cong89819dc2013-12-15 20:15:09 -080041 spin_lock_bh(&hinfo->lock);
WANG Congec0595c2016-07-25 16:09:42 -070042 hlist_del(&p->tcfa_head);
WANG Cong89819dc2013-12-15 20:15:09 -080043 spin_unlock_bh(&hinfo->lock);
WANG Congec0595c2016-07-25 16:09:42 -070044 gen_kill_estimator(&p->tcfa_bstats,
45 &p->tcfa_rate_est);
WANG Cong89819dc2013-12-15 20:15:09 -080046 /*
WANG Congec0595c2016-07-25 16:09:42 -070047 * gen_estimator est_timer() might access p->tcfa_lock
WANG Cong89819dc2013-12-15 20:15:09 -080048 * or bstats, wait a RCU grace period before freeing p
49 */
WANG Congec0595c2016-07-25 16:09:42 -070050 call_rcu(&p->tcfa_rcu, free_tcf);
David S. Millere9ce1cd2006-08-21 23:54:55 -070051}
David S. Millere9ce1cd2006-08-21 23:54:55 -070052
WANG Congec0595c2016-07-25 16:09:42 -070053int __tcf_hash_release(struct tc_action *p, bool bind, bool strict)
David S. Millere9ce1cd2006-08-21 23:54:55 -070054{
55 int ret = 0;
56
57 if (p) {
58 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -070059 p->tcfa_bindcnt--;
60 else if (strict && p->tcfa_bindcnt > 0)
WANG Cong55334a52014-02-11 17:07:34 -080061 return -EPERM;
David S. Millere9ce1cd2006-08-21 23:54:55 -070062
WANG Congec0595c2016-07-25 16:09:42 -070063 p->tcfa_refcnt--;
64 if (p->tcfa_bindcnt <= 0 && p->tcfa_refcnt <= 0) {
65 if (p->ops->cleanup)
66 p->ops->cleanup(p, bind);
67 list_del(&p->list);
68 tcf_hash_destroy(p->hinfo, p);
WANG Cong1d4150c2016-02-22 15:57:52 -080069 ret = ACT_P_DELETED;
David S. Millere9ce1cd2006-08-21 23:54:55 -070070 }
71 }
Daniel Borkmann28e6b672015-07-29 23:35:25 +020072
David S. Millere9ce1cd2006-08-21 23:54:55 -070073 return ret;
74}
Daniel Borkmann28e6b672015-07-29 23:35:25 +020075EXPORT_SYMBOL(__tcf_hash_release);
David S. Millere9ce1cd2006-08-21 23:54:55 -070076
WANG Congddf97cc2016-02-22 15:57:53 -080077static int tcf_dump_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -070078 struct netlink_callback *cb)
David S. Millere9ce1cd2006-08-21 23:54:55 -070079{
Eric Dumazetcc7ec452011-01-19 19:26:56 +000080 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080081 struct nlattr *nest;
David S. Millere9ce1cd2006-08-21 23:54:55 -070082
WANG Cong89819dc2013-12-15 20:15:09 -080083 spin_lock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -070084
85 s_i = cb->args[0];
86
87 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Conga85a9702016-07-25 16:09:41 -070088 struct hlist_head *head;
WANG Congec0595c2016-07-25 16:09:42 -070089 struct tc_action *p;
WANG Conga85a9702016-07-25 16:09:41 -070090
WANG Cong89819dc2013-12-15 20:15:09 -080091 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
David S. Millere9ce1cd2006-08-21 23:54:55 -070092
WANG Congec0595c2016-07-25 16:09:42 -070093 hlist_for_each_entry_rcu(p, head, tcfa_head) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070094 index++;
95 if (index < s_i)
96 continue;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080097
WANG Conga85a9702016-07-25 16:09:41 -070098 nest = nla_nest_start(skb, n_i);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080099 if (nest == NULL)
100 goto nla_put_failure;
WANG Congec0595c2016-07-25 16:09:42 -0700101 err = tcf_action_dump_1(skb, p, 0, 0);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700102 if (err < 0) {
103 index--;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800104 nlmsg_trim(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700105 goto done;
106 }
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800107 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700108 n_i++;
109 if (n_i >= TCA_ACT_MAX_PRIO)
110 goto done;
111 }
112 }
113done:
WANG Cong89819dc2013-12-15 20:15:09 -0800114 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700115 if (n_i)
116 cb->args[0] += n_i;
117 return n_i;
118
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800119nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800120 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700121 goto done;
122}
123
WANG Congddf97cc2016-02-22 15:57:53 -0800124static int tcf_del_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700125 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700126{
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800127 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000128 int i = 0, n_i = 0;
WANG Cong55334a52014-02-11 17:07:34 -0800129 int ret = -EINVAL;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700130
WANG Conga85a9702016-07-25 16:09:41 -0700131 nest = nla_nest_start(skb, 0);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800132 if (nest == NULL)
133 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700134 if (nla_put_string(skb, TCA_KIND, ops->kind))
David S. Miller1b34ec42012-03-29 05:11:39 -0400135 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700136 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Conga85a9702016-07-25 16:09:41 -0700137 struct hlist_head *head;
138 struct hlist_node *n;
WANG Congec0595c2016-07-25 16:09:42 -0700139 struct tc_action *p;
WANG Conga85a9702016-07-25 16:09:41 -0700140
WANG Cong89819dc2013-12-15 20:15:09 -0800141 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
WANG Congec0595c2016-07-25 16:09:42 -0700142 hlist_for_each_entry_safe(p, n, head, tcfa_head) {
143 ret = __tcf_hash_release(p, false, true);
WANG Cong55334a52014-02-11 17:07:34 -0800144 if (ret == ACT_P_DELETED) {
WANG Congec0595c2016-07-25 16:09:42 -0700145 module_put(p->ops->owner);
Jamal Hadi Salim805c1f42013-12-23 08:02:13 -0500146 n_i++;
WANG Cong55334a52014-02-11 17:07:34 -0800147 } else if (ret < 0)
148 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700149 }
150 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400151 if (nla_put_u32(skb, TCA_FCNT, n_i))
152 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800153 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700154
155 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800156nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800157 nla_nest_cancel(skb, nest);
WANG Cong55334a52014-02-11 17:07:34 -0800158 return ret;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700159}
160
WANG Congddf97cc2016-02-22 15:57:53 -0800161int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
162 struct netlink_callback *cb, int type,
WANG Conga85a9702016-07-25 16:09:41 -0700163 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700164{
WANG Congddf97cc2016-02-22 15:57:53 -0800165 struct tcf_hashinfo *hinfo = tn->hinfo;
166
David S. Millere9ce1cd2006-08-21 23:54:55 -0700167 if (type == RTM_DELACTION) {
WANG Conga85a9702016-07-25 16:09:41 -0700168 return tcf_del_walker(hinfo, skb, ops);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700169 } else if (type == RTM_GETACTION) {
WANG Conga85a9702016-07-25 16:09:41 -0700170 return tcf_dump_walker(hinfo, skb, cb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700171 } else {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000172 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700173 return -EINVAL;
174 }
175}
WANG Congddf97cc2016-02-22 15:57:53 -0800176EXPORT_SYMBOL(tcf_generic_walker);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700177
WANG Congec0595c2016-07-25 16:09:42 -0700178static struct tc_action *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700179{
WANG Congec0595c2016-07-25 16:09:42 -0700180 struct tc_action *p = NULL;
WANG Cong89819dc2013-12-15 20:15:09 -0800181 struct hlist_head *head;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700182
WANG Cong89819dc2013-12-15 20:15:09 -0800183 spin_lock_bh(&hinfo->lock);
184 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
WANG Congec0595c2016-07-25 16:09:42 -0700185 hlist_for_each_entry_rcu(p, head, tcfa_head)
186 if (p->tcfa_index == index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700187 break;
WANG Cong89819dc2013-12-15 20:15:09 -0800188 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700189
190 return p;
191}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700192
WANG Congddf97cc2016-02-22 15:57:53 -0800193u32 tcf_hash_new_index(struct tc_action_net *tn)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700194{
WANG Congddf97cc2016-02-22 15:57:53 -0800195 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congddafd342014-01-09 16:13:59 -0800196 u32 val = hinfo->index;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700197
198 do {
199 if (++val == 0)
200 val = 1;
201 } while (tcf_hash_lookup(val, hinfo));
202
WANG Congddafd342014-01-09 16:13:59 -0800203 hinfo->index = val;
Yang Yingliang17569fa2013-12-10 20:55:29 +0800204 return val;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700205}
206EXPORT_SYMBOL(tcf_hash_new_index);
207
WANG Conga85a9702016-07-25 16:09:41 -0700208int tcf_hash_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700209{
WANG Congddf97cc2016-02-22 15:57:53 -0800210 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700211 struct tc_action *p = tcf_hash_lookup(index, hinfo);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700212
213 if (p) {
WANG Congec0595c2016-07-25 16:09:42 -0700214 *a = p;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700215 return 1;
216 }
217 return 0;
218}
WANG Cong6e6a50c2014-01-17 11:37:03 -0800219EXPORT_SYMBOL(tcf_hash_search);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700220
WANG Conga85a9702016-07-25 16:09:41 -0700221bool tcf_hash_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
WANG Congb2313072016-06-13 13:46:28 -0700222 int bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700223{
WANG Congddf97cc2016-02-22 15:57:53 -0800224 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700225 struct tc_action *p = NULL;
226
David S. Millere9ce1cd2006-08-21 23:54:55 -0700227 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700228 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -0700229 p->tcfa_bindcnt++;
230 p->tcfa_refcnt++;
231 *a = p;
WANG Congb2313072016-06-13 13:46:28 -0700232 return true;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700233 }
WANG Congb2313072016-06-13 13:46:28 -0700234 return false;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700235}
236EXPORT_SYMBOL(tcf_hash_check);
237
WANG Cong86062032014-02-11 17:07:31 -0800238void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
239{
WANG Cong86062032014-02-11 17:07:31 -0800240 if (est)
WANG Congec0595c2016-07-25 16:09:42 -0700241 gen_kill_estimator(&a->tcfa_bstats,
242 &a->tcfa_rate_est);
243 call_rcu(&a->tcfa_rcu, free_tcf);
WANG Cong86062032014-02-11 17:07:31 -0800244}
245EXPORT_SYMBOL(tcf_hash_cleanup);
246
WANG Congddf97cc2016-02-22 15:57:53 -0800247int tcf_hash_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
WANG Conga85a9702016-07-25 16:09:41 -0700248 struct tc_action **a, const struct tc_action_ops *ops,
249 int bind, bool cpustats)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700250{
WANG Congec0595c2016-07-25 16:09:42 -0700251 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
WANG Congddf97cc2016-02-22 15:57:53 -0800252 struct tcf_hashinfo *hinfo = tn->hinfo;
Eric Dumazet519c8182015-07-06 05:18:04 -0700253 int err = -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700254
255 if (unlikely(!p))
WANG Cong86062032014-02-11 17:07:31 -0800256 return -ENOMEM;
WANG Congec0595c2016-07-25 16:09:42 -0700257 p->tcfa_refcnt = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700258 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -0700259 p->tcfa_bindcnt = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700260
Eric Dumazet519c8182015-07-06 05:18:04 -0700261 if (cpustats) {
262 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
263 if (!p->cpu_bstats) {
264err1:
265 kfree(p);
266 return err;
267 }
268 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
269 if (!p->cpu_qstats) {
270err2:
271 free_percpu(p->cpu_bstats);
272 goto err1;
273 }
274 }
WANG Congec0595c2016-07-25 16:09:42 -0700275 spin_lock_init(&p->tcfa_lock);
276 INIT_HLIST_NODE(&p->tcfa_head);
277 p->tcfa_index = index ? index : tcf_hash_new_index(tn);
278 p->tcfa_tm.install = jiffies;
279 p->tcfa_tm.lastuse = jiffies;
280 p->tcfa_tm.firstuse = 0;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800281 if (est) {
WANG Congec0595c2016-07-25 16:09:42 -0700282 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
283 &p->tcfa_rate_est,
284 &p->tcfa_lock, NULL, est);
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800285 if (err) {
Eric Dumazet519c8182015-07-06 05:18:04 -0700286 free_percpu(p->cpu_qstats);
287 goto err2;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800288 }
289 }
290
WANG Congec0595c2016-07-25 16:09:42 -0700291 p->hinfo = hinfo;
292 p->ops = ops;
293 INIT_LIST_HEAD(&p->list);
294 *a = p;
WANG Cong86062032014-02-11 17:07:31 -0800295 return 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700296}
297EXPORT_SYMBOL(tcf_hash_create);
298
WANG Congddf97cc2016-02-22 15:57:53 -0800299void tcf_hash_insert(struct tc_action_net *tn, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700300{
WANG Congddf97cc2016-02-22 15:57:53 -0800301 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700302 unsigned int h = tcf_hash(a->tcfa_index, hinfo->hmask);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700303
WANG Cong89819dc2013-12-15 20:15:09 -0800304 spin_lock_bh(&hinfo->lock);
WANG Congec0595c2016-07-25 16:09:42 -0700305 hlist_add_head(&a->tcfa_head, &hinfo->htab[h]);
WANG Cong89819dc2013-12-15 20:15:09 -0800306 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700307}
308EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
WANG Congddf97cc2016-02-22 15:57:53 -0800310void tcf_hashinfo_destroy(const struct tc_action_ops *ops,
311 struct tcf_hashinfo *hinfo)
WANG Cong1d4150c2016-02-22 15:57:52 -0800312{
WANG Cong1d4150c2016-02-22 15:57:52 -0800313 int i;
314
315 for (i = 0; i < hinfo->hmask + 1; i++) {
WANG Congec0595c2016-07-25 16:09:42 -0700316 struct tc_action *p;
WANG Cong1d4150c2016-02-22 15:57:52 -0800317 struct hlist_node *n;
318
WANG Congec0595c2016-07-25 16:09:42 -0700319 hlist_for_each_entry_safe(p, n, &hinfo->htab[i], tcfa_head) {
WANG Cong1d4150c2016-02-22 15:57:52 -0800320 int ret;
321
WANG Congec0595c2016-07-25 16:09:42 -0700322 ret = __tcf_hash_release(p, false, true);
WANG Cong1d4150c2016-02-22 15:57:52 -0800323 if (ret == ACT_P_DELETED)
324 module_put(ops->owner);
325 else if (ret < 0)
326 return;
327 }
328 }
329 kfree(hinfo->htab);
330}
WANG Congddf97cc2016-02-22 15:57:53 -0800331EXPORT_SYMBOL(tcf_hashinfo_destroy);
WANG Cong1d4150c2016-02-22 15:57:52 -0800332
WANG Cong1f747c22013-12-15 20:15:10 -0800333static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334static DEFINE_RWLOCK(act_mod_lock);
335
WANG Congddf97cc2016-02-22 15:57:53 -0800336int tcf_register_action(struct tc_action_ops *act,
337 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
WANG Cong1f747c22013-12-15 20:15:10 -0800339 struct tc_action_ops *a;
WANG Congddf97cc2016-02-22 15:57:53 -0800340 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
WANG Congddf97cc2016-02-22 15:57:53 -0800342 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500343 return -EINVAL;
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800346 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
348 write_unlock(&act_mod_lock);
349 return -EEXIST;
350 }
351 }
WANG Cong1f747c22013-12-15 20:15:10 -0800352 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 write_unlock(&act_mod_lock);
WANG Congddf97cc2016-02-22 15:57:53 -0800354
355 ret = register_pernet_subsys(ops);
356 if (ret) {
357 tcf_unregister_action(act, ops);
358 return ret;
359 }
360
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
WANG Congddf97cc2016-02-22 15:57:53 -0800371 unregister_pernet_subsys(ops);
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800374 list_for_each_entry(a, &act_base, head) {
375 if (a == act) {
376 list_del(&act->head);
377 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381 write_unlock(&act_mod_lock);
382 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 Cong33be6272013-12-15 20:15:05 -0800424int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900425 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000427 const struct tc_action *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 int ret = -1;
429
430 if (skb->tc_verd & TC_NCLS) {
431 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 ret = TC_ACT_OK;
433 goto exec_done;
434 }
WANG Cong33be6272013-12-15 20:15:05 -0800435 list_for_each_entry(a, actions, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500437 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500438 if (ret == TC_ACT_REPEAT)
439 goto repeat; /* we need a ttl - JHS */
440 if (ret != TC_ACT_PIPE)
441 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return ret;
445}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800446EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
WANG Cong55334a52014-02-11 17:07:34 -0800448int tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
WANG Cong33be6272013-12-15 20:15:05 -0800450 struct tc_action *a, *tmp;
WANG Cong55334a52014-02-11 17:07:34 -0800451 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
WANG Cong33be6272013-12-15 20:15:05 -0800453 list_for_each_entry_safe(a, tmp, actions, list) {
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200454 ret = __tcf_hash_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800455 if (ret == ACT_P_DELETED)
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500456 module_put(a->ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800457 else if (ret < 0)
458 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
WANG Cong55334a52014-02-11 17:07:34 -0800460 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
463int
464tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
465{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return a->ops->dump(skb, a, bind, ref);
467}
468
469int
470tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
471{
472 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700473 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800474 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
David S. Miller1b34ec42012-03-29 05:11:39 -0400476 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
477 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800479 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800480 nest = nla_nest_start(skb, TCA_OPTIONS);
481 if (nest == NULL)
482 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000483 err = tcf_action_dump_old(skb, a, bind, ref);
484 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800485 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return err;
487 }
488
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800489nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700490 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return -1;
492}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800493EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400495int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
496 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct tc_action *a;
499 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800500 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
WANG Cong33be6272013-12-15 20:15:05 -0800502 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800503 nest = nla_nest_start(skb, a->order);
504 if (nest == NULL)
505 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 err = tcf_action_dump_1(skb, a, bind, ref);
507 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700508 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800509 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511
512 return 0;
513
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800514nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700515 err = -EINVAL;
516errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800517 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700518 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000521struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
522 struct nlattr *est, char *name, int ovr,
523 int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 struct tc_action *a;
526 struct tc_action_ops *a_o;
527 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000528 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800529 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800530 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800533 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
534 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800536 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800537 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (kind == NULL)
539 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800540 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 goto err_out;
542 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800543 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
545 goto err_out;
546 }
547
548 a_o = tc_lookup_action_n(act_name);
549 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700550#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800552 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 rtnl_lock();
554
555 a_o = tc_lookup_action_n(act_name);
556
557 /* We dropped the RTNL semaphore in order to
558 * perform the module load. So, even if we
559 * succeeded in loading the module we have to
560 * tell the caller to replay the request. We
561 * indicate this using -EAGAIN.
562 */
563 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800564 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 goto err_mod;
566 }
567#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800568 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 goto err_out;
570 }
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /* backward compatibility for policer */
573 if (name == NULL)
WANG Conga85a9702016-07-25 16:09:41 -0700574 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 else
WANG Conga85a9702016-07-25 16:09:41 -0700576 err = a_o->init(net, nla, est, &a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800577 if (err < 0)
WANG Conga85a9702016-07-25 16:09:41 -0700578 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000581 * if it exists and is only bound to in a_o->init() then
582 * ACT_P_CREATED is not returned (a zero is).
583 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800584 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return a;
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589err_mod:
590 module_put(a_o->owner);
591err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800592 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
WANG Cong33be6272013-12-15 20:15:05 -0800595int tcf_action_init(struct net *net, struct nlattr *nla,
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000596 struct nlattr *est, char *name, int ovr,
WANG Cong33be6272013-12-15 20:15:05 -0800597 int bind, struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000599 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800600 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800601 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 int i;
603
Patrick McHardycee63722008-01-23 20:33:32 -0800604 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
605 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800606 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800608 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000609 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800610 if (IS_ERR(act)) {
611 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800613 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800614 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800615 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
WANG Cong33be6272013-12-15 20:15:05 -0800617 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619err:
WANG Cong33be6272013-12-15 20:15:05 -0800620 tcf_action_destroy(actions, bind);
621 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
WANG Congec0595c2016-07-25 16:09:42 -0700624int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 int compat_mode)
626{
627 int err = 0;
628 struct gnet_dump d;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900629
WANG Cong7eb88962014-01-09 16:14:05 -0800630 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 goto errout;
632
633 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400634 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 */
636 if (compat_mode) {
WANG Congec0595c2016-07-25 16:09:42 -0700637 if (p->type == TCA_OLD_COMPAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 err = gnet_stats_start_copy_compat(skb, 0,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200639 TCA_STATS,
640 TCA_XSTATS,
WANG Congec0595c2016-07-25 16:09:42 -0700641 &p->tcfa_lock, &d,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200642 TCA_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 else
644 return 0;
645 } else
646 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Congec0595c2016-07-25 16:09:42 -0700647 &p->tcfa_lock, &d, TCA_ACT_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 if (err < 0)
650 goto errout;
651
WANG Congec0595c2016-07-25 16:09:42 -0700652 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
653 gnet_stats_copy_rate_est(&d, &p->tcfa_bstats,
654 &p->tcfa_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -0700655 gnet_stats_copy_queue(&d, p->cpu_qstats,
WANG Congec0595c2016-07-25 16:09:42 -0700656 &p->tcfa_qstats,
657 p->tcfa_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 goto errout;
659
660 if (gnet_stats_finish_copy(&d) < 0)
661 goto errout;
662
663 return 0;
664
665errout:
666 return -1;
667}
668
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400669static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
670 u32 portid, u32 seq, u16 flags, int event, int bind,
671 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 struct tcamsg *t;
674 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700675 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800676 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Eric W. Biederman15e47302012-09-07 20:12:54 +0000678 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700679 if (!nlh)
680 goto out_nlmsg_trim;
681 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700683 t->tca__pad1 = 0;
684 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900685
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800686 nest = nla_nest_start(skb, TCA_ACT_TAB);
687 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700688 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
WANG Cong33be6272013-12-15 20:15:05 -0800690 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700691 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800693 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900694
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700695 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return skb->len;
697
David S. Miller8b00a532012-06-26 21:39:32 -0700698out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700699 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return -1;
701}
702
703static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000704act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800705 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
710 if (!skb)
711 return -ENOBUFS;
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400712 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
713 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 kfree_skb(skb);
715 return -EINVAL;
716 }
Thomas Graf2942e902006-08-15 00:30:25 -0700717
Eric W. Biederman15e47302012-09-07 20:12:54 +0000718 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
WANG Congddf97cc2016-02-22 15:57:53 -0800721static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
722 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000724 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700725 const struct tc_action_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct tc_action *a;
727 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800728 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Patrick McHardycee63722008-01-23 20:33:32 -0800730 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
731 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800732 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Patrick McHardycee63722008-01-23 20:33:32 -0800734 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800735 if (tb[TCA_ACT_INDEX] == NULL ||
736 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800737 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800738 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800740 err = -EINVAL;
WANG Conga85a9702016-07-25 16:09:41 -0700741 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
742 if (!ops) /* could happen in batch of actions */
743 goto err_out;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800744 err = -ENOENT;
WANG Conga85a9702016-07-25 16:09:41 -0700745 if (ops->lookup(net, &a, index) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 goto err_mod;
747
WANG Conga85a9702016-07-25 16:09:41 -0700748 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751err_mod:
WANG Conga85a9702016-07-25 16:09:41 -0700752 module_put(ops->owner);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800753err_out:
754 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
WANG Cong33be6272013-12-15 20:15:05 -0800757static void cleanup_a(struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
WANG Cong33be6272013-12-15 20:15:05 -0800759 struct tc_action *a, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
WANG Cong33be6272013-12-15 20:15:05 -0800761 list_for_each_entry_safe(a, tmp, actions, list) {
762 list_del(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 kfree(a);
764 }
765}
766
Tom Goff7316ae82010-03-19 15:40:13 +0000767static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000768 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 struct sk_buff *skb;
771 unsigned char *b;
772 struct nlmsghdr *nlh;
773 struct tcamsg *t;
774 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800775 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000776 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700777 const struct tc_action_ops *ops;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800778 struct nlattr *kind;
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700779 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
782 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000783 pr_debug("tca_action_flush: failed skb alloc\n");
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700784 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700787 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Patrick McHardycee63722008-01-23 20:33:32 -0800789 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
790 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 goto err_out;
792
Patrick McHardycee63722008-01-23 20:33:32 -0800793 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800794 kind = tb[TCA_ACT_KIND];
WANG Conga85a9702016-07-25 16:09:41 -0700795 ops = tc_lookup_action(kind);
796 if (!ops) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 goto err_out;
798
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400799 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
800 sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700801 if (!nlh)
802 goto out_module_put;
803 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700805 t->tca__pad1 = 0;
806 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800808 nest = nla_nest_start(skb, TCA_ACT_TAB);
809 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700810 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
WANG Conga85a9702016-07-25 16:09:41 -0700812 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (err < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700814 goto out_module_put;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700815 if (err == 0)
816 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800818 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700820 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Conga85a9702016-07-25 16:09:41 -0700822 module_put(ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000823 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000824 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (err > 0)
826 return 0;
827
828 return err;
829
David S. Miller8b00a532012-06-26 21:39:32 -0700830out_module_put:
WANG Conga85a9702016-07-25 16:09:41 -0700831 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700833noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return err;
836}
837
838static int
WANG Conga56e1952014-01-09 16:14:00 -0800839tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
840 u32 portid)
841{
842 int ret;
843 struct sk_buff *skb;
844
845 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
846 if (!skb)
847 return -ENOBUFS;
848
849 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
850 0, 1) <= 0) {
851 kfree_skb(skb);
852 return -EINVAL;
853 }
854
855 /* now do the delete */
WANG Cong55334a52014-02-11 17:07:34 -0800856 ret = tcf_action_destroy(actions, 0);
857 if (ret < 0) {
858 kfree_skb(skb);
859 return ret;
860 }
WANG Conga56e1952014-01-09 16:14:00 -0800861
862 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
863 n->nlmsg_flags & NLM_F_ECHO);
864 if (ret > 0)
865 return 0;
866 return ret;
867}
868
869static int
Tom Goff7316ae82010-03-19 15:40:13 +0000870tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000871 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Patrick McHardycee63722008-01-23 20:33:32 -0800873 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000874 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800875 struct tc_action *act;
876 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Patrick McHardycee63722008-01-23 20:33:32 -0800878 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
879 if (ret < 0)
880 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000882 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700883 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000884 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700885 else
886 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800889 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
WANG Congddf97cc2016-02-22 15:57:53 -0800890 act = tcf_action_get_1(net, tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800891 if (IS_ERR(act)) {
892 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800894 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800895 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800896 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898
899 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800900 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -0800902 ret = tcf_del_notify(net, n, &actions, portid);
903 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return ret;
906 }
907err:
WANG Cong33be6272013-12-15 20:15:05 -0800908 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return ret;
910}
911
WANG Conga56e1952014-01-09 16:14:00 -0800912static int
913tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
914 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 int err = 0;
918
919 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
920 if (!skb)
921 return -ENOBUFS;
922
WANG Conga56e1952014-01-09 16:14:00 -0800923 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
924 RTM_NEWACTION, 0, 0) <= 0) {
925 kfree_skb(skb);
926 return -EINVAL;
927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
WANG Conga56e1952014-01-09 16:14:00 -0800929 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
930 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (err > 0)
932 err = 0;
933 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936static int
Tom Goff7316ae82010-03-19 15:40:13 +0000937tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000938 u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -0800941 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
WANG Cong33be6272013-12-15 20:15:05 -0800943 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
944 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 goto done;
946
947 /* dump then free all the actions after update; inserted policy
948 * stays intact
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000949 */
WANG Conga56e1952014-01-09 16:14:00 -0800950 ret = tcf_add_notify(net, n, &actions, portid);
WANG Cong33be6272013-12-15 20:15:05 -0800951 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952done:
953 return ret;
954}
955
Thomas Graf661d2962013-03-21 07:45:29 +0000956static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900958 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800959 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +0000960 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 int ret = 0, ovr = 0;
962
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400963 if ((n->nlmsg_type != RTM_GETACTION) &&
964 !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000965 return -EPERM;
966
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800967 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
968 if (ret < 0)
969 return ret;
970
971 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000972 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return -EINVAL;
974 }
975
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000976 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 switch (n->nlmsg_type) {
978 case RTM_NEWACTION:
979 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300980 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 * Note that CREATE | EXCL implies that
982 * but since we want avoid ambiguity (eg when flags
983 * is zero) then just set this
984 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000985 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 ovr = 1;
987replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000988 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (ret == -EAGAIN)
990 goto replay;
991 break;
992 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000993 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000994 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 break;
996 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000997 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000998 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 break;
1000 default:
1001 BUG();
1002 }
1003
1004 return ret;
1005}
1006
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001007static struct nlattr *
Patrick McHardy3a6c2b42009-08-25 16:07:40 +02001008find_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;
1027 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1028 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001030 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Thomas Graf26dab892006-07-05 20:45:06 -07001032 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
1035static int
1036tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1037{
WANG Congddf97cc2016-02-22 15:57:53 -08001038 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001040 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001041 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 struct tc_action_ops *a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001044 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001045 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001048 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return 0;
1050 }
1051
Thomas Graf26dab892006-07-05 20:45:06 -07001052 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001053 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Eric W. Biederman15e47302012-09-07 20:12:54 +00001056 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001057 cb->nlh->nlmsg_type, sizeof(*t), 0);
1058 if (!nlh)
1059 goto out_module_put;
1060 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001062 t->tca__pad1 = 0;
1063 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001065 nest = nla_nest_start(skb, TCA_ACT_TAB);
1066 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001067 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
WANG Conga85a9702016-07-25 16:09:41 -07001069 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001071 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001074 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 ret = skb->len;
1076 } else
Jamal Hadi Salimebecaa62016-06-13 18:08:42 -04001077 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001079 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001080 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 nlh->nlmsg_flags |= NLM_F_MULTI;
1082 module_put(a_o->owner);
1083 return skb->len;
1084
David S. Miller8b00a532012-06-26 21:39:32 -07001085out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001087 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return skb->len;
1089}
1090
1091static int __init tc_action_init(void)
1092{
Greg Rosec7ac8672011-06-10 01:27:09 +00001093 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1094 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1095 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1096 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return 0;
1099}
1100
1101subsys_initcall(tc_action_init);