blob: f8c61d2a79635d4951858341f02bb456446886f6 [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{
32 struct tcf_common *p = container_of(head, struct tcf_common, tcfc_rcu);
33
34 free_percpu(p->cpu_bstats);
35 free_percpu(p->cpu_qstats);
36 kfree(p);
37}
38
WANG Congddf97cc2016-02-22 15:57:53 -080039static void tcf_hash_destroy(struct tcf_hashinfo *hinfo, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -070040{
WANG Cong86062032014-02-11 17:07:31 -080041 struct tcf_common *p = a->priv;
WANG Cong86062032014-02-11 17:07:31 -080042
WANG Cong89819dc2013-12-15 20:15:09 -080043 spin_lock_bh(&hinfo->lock);
44 hlist_del(&p->tcfc_head);
45 spin_unlock_bh(&hinfo->lock);
46 gen_kill_estimator(&p->tcfc_bstats,
47 &p->tcfc_rate_est);
48 /*
49 * gen_estimator est_timer() might access p->tcfc_lock
50 * or bstats, wait a RCU grace period before freeing p
51 */
Eric Dumazet519c8182015-07-06 05:18:04 -070052 call_rcu(&p->tcfc_rcu, free_tcf);
David S. Millere9ce1cd2006-08-21 23:54:55 -070053}
David S. Millere9ce1cd2006-08-21 23:54:55 -070054
Daniel Borkmann28e6b672015-07-29 23:35:25 +020055int __tcf_hash_release(struct tc_action *a, bool bind, bool strict)
David S. Millere9ce1cd2006-08-21 23:54:55 -070056{
WANG Cong86062032014-02-11 17:07:31 -080057 struct tcf_common *p = a->priv;
David S. Millere9ce1cd2006-08-21 23:54:55 -070058 int ret = 0;
59
60 if (p) {
61 if (bind)
62 p->tcfc_bindcnt--;
Daniel Borkmann28e6b672015-07-29 23:35:25 +020063 else if (strict && p->tcfc_bindcnt > 0)
WANG Cong55334a52014-02-11 17:07:34 -080064 return -EPERM;
David S. Millere9ce1cd2006-08-21 23:54:55 -070065
66 p->tcfc_refcnt--;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090067 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
WANG Conga5b5c952014-02-11 17:07:32 -080068 if (a->ops->cleanup)
69 a->ops->cleanup(a, bind);
WANG Congddf97cc2016-02-22 15:57:53 -080070 tcf_hash_destroy(a->hinfo, a);
WANG Cong1d4150c2016-02-22 15:57:52 -080071 ret = ACT_P_DELETED;
David S. Millere9ce1cd2006-08-21 23:54:55 -070072 }
73 }
Daniel Borkmann28e6b672015-07-29 23:35:25 +020074
David S. Millere9ce1cd2006-08-21 23:54:55 -070075 return ret;
76}
Daniel Borkmann28e6b672015-07-29 23:35:25 +020077EXPORT_SYMBOL(__tcf_hash_release);
David S. Millere9ce1cd2006-08-21 23:54:55 -070078
WANG Congddf97cc2016-02-22 15:57:53 -080079static int tcf_dump_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
80 struct netlink_callback *cb, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -070081{
WANG Cong89819dc2013-12-15 20:15:09 -080082 struct hlist_head *head;
David S. Millere9ce1cd2006-08-21 23:54:55 -070083 struct tcf_common *p;
Eric Dumazetcc7ec452011-01-19 19:26:56 +000084 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080085 struct nlattr *nest;
David S. Millere9ce1cd2006-08-21 23:54:55 -070086
WANG Cong89819dc2013-12-15 20:15:09 -080087 spin_lock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -070088
89 s_i = cb->args[0];
90
91 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Cong89819dc2013-12-15 20:15:09 -080092 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
David S. Millere9ce1cd2006-08-21 23:54:55 -070093
WANG Cong89819dc2013-12-15 20:15:09 -080094 hlist_for_each_entry_rcu(p, head, tcfc_head) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070095 index++;
96 if (index < s_i)
97 continue;
98 a->priv = p;
99 a->order = n_i;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800100
101 nest = nla_nest_start(skb, a->order);
102 if (nest == NULL)
103 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700104 err = tcf_action_dump_1(skb, a, 0, 0);
105 if (err < 0) {
106 index--;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800107 nlmsg_trim(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700108 goto done;
109 }
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800110 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700111 n_i++;
112 if (n_i >= TCA_ACT_MAX_PRIO)
113 goto done;
114 }
115 }
116done:
WANG Cong89819dc2013-12-15 20:15:09 -0800117 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700118 if (n_i)
119 cb->args[0] += n_i;
120 return n_i;
121
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800122nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800123 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700124 goto done;
125}
126
WANG Congddf97cc2016-02-22 15:57:53 -0800127static int tcf_del_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
128 struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700129{
WANG Cong89819dc2013-12-15 20:15:09 -0800130 struct hlist_head *head;
131 struct hlist_node *n;
132 struct tcf_common *p;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800133 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000134 int i = 0, n_i = 0;
WANG Cong55334a52014-02-11 17:07:34 -0800135 int ret = -EINVAL;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700136
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800137 nest = nla_nest_start(skb, a->order);
138 if (nest == NULL)
139 goto nla_put_failure;
David S. Miller1b34ec42012-03-29 05:11:39 -0400140 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
141 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700142 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Cong89819dc2013-12-15 20:15:09 -0800143 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
144 hlist_for_each_entry_safe(p, n, head, tcfc_head) {
WANG Cong86062032014-02-11 17:07:31 -0800145 a->priv = p;
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200146 ret = __tcf_hash_release(a, false, true);
WANG Cong55334a52014-02-11 17:07:34 -0800147 if (ret == ACT_P_DELETED) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000148 module_put(a->ops->owner);
Jamal Hadi Salim805c1f42013-12-23 08:02:13 -0500149 n_i++;
WANG Cong55334a52014-02-11 17:07:34 -0800150 } else if (ret < 0)
151 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700152 }
153 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400154 if (nla_put_u32(skb, TCA_FCNT, n_i))
155 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800156 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700157
158 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800159nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800160 nla_nest_cancel(skb, nest);
WANG Cong55334a52014-02-11 17:07:34 -0800161 return ret;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700162}
163
WANG Congddf97cc2016-02-22 15:57:53 -0800164int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
165 struct netlink_callback *cb, int type,
166 struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700167{
WANG Congddf97cc2016-02-22 15:57:53 -0800168 struct tcf_hashinfo *hinfo = tn->hinfo;
169
170 a->hinfo = hinfo;
171
David S. Millere9ce1cd2006-08-21 23:54:55 -0700172 if (type == RTM_DELACTION) {
WANG Congddf97cc2016-02-22 15:57:53 -0800173 return tcf_del_walker(hinfo, skb, a);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700174 } else if (type == RTM_GETACTION) {
WANG Congddf97cc2016-02-22 15:57:53 -0800175 return tcf_dump_walker(hinfo, skb, cb, a);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700176 } else {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000177 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700178 return -EINVAL;
179 }
180}
WANG Congddf97cc2016-02-22 15:57:53 -0800181EXPORT_SYMBOL(tcf_generic_walker);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700182
WANG Cong6e6a50c2014-01-17 11:37:03 -0800183static struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700184{
WANG Cong89819dc2013-12-15 20:15:09 -0800185 struct tcf_common *p = NULL;
186 struct hlist_head *head;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700187
WANG Cong89819dc2013-12-15 20:15:09 -0800188 spin_lock_bh(&hinfo->lock);
189 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
190 hlist_for_each_entry_rcu(p, head, tcfc_head)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700191 if (p->tcfc_index == index)
192 break;
WANG Cong89819dc2013-12-15 20:15:09 -0800193 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700194
195 return p;
196}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700197
WANG Congddf97cc2016-02-22 15:57:53 -0800198u32 tcf_hash_new_index(struct tc_action_net *tn)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700199{
WANG Congddf97cc2016-02-22 15:57:53 -0800200 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congddafd342014-01-09 16:13:59 -0800201 u32 val = hinfo->index;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700202
203 do {
204 if (++val == 0)
205 val = 1;
206 } while (tcf_hash_lookup(val, hinfo));
207
WANG Congddafd342014-01-09 16:13:59 -0800208 hinfo->index = val;
Yang Yingliang17569fa2013-12-10 20:55:29 +0800209 return val;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700210}
211EXPORT_SYMBOL(tcf_hash_new_index);
212
WANG Congddf97cc2016-02-22 15:57:53 -0800213int tcf_hash_search(struct tc_action_net *tn, struct tc_action *a, u32 index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700214{
WANG Congddf97cc2016-02-22 15:57:53 -0800215 struct tcf_hashinfo *hinfo = tn->hinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700216 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
217
218 if (p) {
219 a->priv = p;
WANG Congddf97cc2016-02-22 15:57:53 -0800220 a->hinfo = hinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700221 return 1;
222 }
223 return 0;
224}
WANG Cong6e6a50c2014-01-17 11:37:03 -0800225EXPORT_SYMBOL(tcf_hash_search);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700226
WANG Congb2313072016-06-13 13:46:28 -0700227bool tcf_hash_check(struct tc_action_net *tn, u32 index, struct tc_action *a,
228 int bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700229{
WANG Congddf97cc2016-02-22 15:57:53 -0800230 struct tcf_hashinfo *hinfo = tn->hinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700231 struct tcf_common *p = NULL;
232 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700233 if (bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700234 p->tcfc_bindcnt++;
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700235 p->tcfc_refcnt++;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700236 a->priv = p;
WANG Congddf97cc2016-02-22 15:57:53 -0800237 a->hinfo = hinfo;
WANG Congb2313072016-06-13 13:46:28 -0700238 return true;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700239 }
WANG Congb2313072016-06-13 13:46:28 -0700240 return false;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700241}
242EXPORT_SYMBOL(tcf_hash_check);
243
WANG Cong86062032014-02-11 17:07:31 -0800244void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
245{
246 struct tcf_common *pc = a->priv;
247 if (est)
248 gen_kill_estimator(&pc->tcfc_bstats,
249 &pc->tcfc_rate_est);
Eric Dumazet519c8182015-07-06 05:18:04 -0700250 call_rcu(&pc->tcfc_rcu, free_tcf);
WANG Cong86062032014-02-11 17:07:31 -0800251}
252EXPORT_SYMBOL(tcf_hash_cleanup);
253
WANG Congddf97cc2016-02-22 15:57:53 -0800254int tcf_hash_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
255 struct tc_action *a, int size, int bind, bool cpustats)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700256{
257 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
WANG Congddf97cc2016-02-22 15:57:53 -0800258 struct tcf_hashinfo *hinfo = tn->hinfo;
Eric Dumazet519c8182015-07-06 05:18:04 -0700259 int err = -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700260
261 if (unlikely(!p))
WANG Cong86062032014-02-11 17:07:31 -0800262 return -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700263 p->tcfc_refcnt = 1;
264 if (bind)
265 p->tcfc_bindcnt = 1;
266
Eric Dumazet519c8182015-07-06 05:18:04 -0700267 if (cpustats) {
268 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
269 if (!p->cpu_bstats) {
270err1:
271 kfree(p);
272 return err;
273 }
274 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
275 if (!p->cpu_qstats) {
276err2:
277 free_percpu(p->cpu_bstats);
278 goto err1;
279 }
280 }
David S. Millere9ce1cd2006-08-21 23:54:55 -0700281 spin_lock_init(&p->tcfc_lock);
WANG Cong89819dc2013-12-15 20:15:09 -0800282 INIT_HLIST_NODE(&p->tcfc_head);
WANG Congddf97cc2016-02-22 15:57:53 -0800283 p->tcfc_index = index ? index : tcf_hash_new_index(tn);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700284 p->tcfc_tm.install = jiffies;
285 p->tcfc_tm.lastuse = jiffies;
Jamal Hadi Salim53eb4402016-06-06 06:32:54 -0400286 p->tcfc_tm.firstuse = 0;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800287 if (est) {
Eric Dumazet519c8182015-07-06 05:18:04 -0700288 err = gen_new_estimator(&p->tcfc_bstats, p->cpu_bstats,
289 &p->tcfc_rate_est,
Eric Dumazetedb09eb2016-06-06 09:37:16 -0700290 &p->tcfc_lock, NULL, est);
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800291 if (err) {
Eric Dumazet519c8182015-07-06 05:18:04 -0700292 free_percpu(p->cpu_qstats);
293 goto err2;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800294 }
295 }
296
David S. Millere9ce1cd2006-08-21 23:54:55 -0700297 a->priv = (void *) p;
WANG Congddf97cc2016-02-22 15:57:53 -0800298 a->hinfo = hinfo;
WANG Cong86062032014-02-11 17:07:31 -0800299 return 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700300}
301EXPORT_SYMBOL(tcf_hash_create);
302
WANG Congddf97cc2016-02-22 15:57:53 -0800303void tcf_hash_insert(struct tc_action_net *tn, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700304{
WANG Cong86062032014-02-11 17:07:31 -0800305 struct tcf_common *p = a->priv;
WANG Congddf97cc2016-02-22 15:57:53 -0800306 struct tcf_hashinfo *hinfo = tn->hinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700307 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
308
WANG Cong89819dc2013-12-15 20:15:09 -0800309 spin_lock_bh(&hinfo->lock);
310 hlist_add_head(&p->tcfc_head, &hinfo->htab[h]);
311 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700312}
313EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
WANG Congddf97cc2016-02-22 15:57:53 -0800315void tcf_hashinfo_destroy(const struct tc_action_ops *ops,
316 struct tcf_hashinfo *hinfo)
WANG Cong1d4150c2016-02-22 15:57:52 -0800317{
WANG Cong1d4150c2016-02-22 15:57:52 -0800318 struct tc_action a = {
319 .ops = ops,
WANG Congddf97cc2016-02-22 15:57:53 -0800320 .hinfo = hinfo,
WANG Cong1d4150c2016-02-22 15:57:52 -0800321 };
322 int i;
323
324 for (i = 0; i < hinfo->hmask + 1; i++) {
325 struct tcf_common *p;
326 struct hlist_node *n;
327
328 hlist_for_each_entry_safe(p, n, &hinfo->htab[i], tcfc_head) {
329 int ret;
330
331 a.priv = p;
332 ret = __tcf_hash_release(&a, false, true);
333 if (ret == ACT_P_DELETED)
334 module_put(ops->owner);
335 else if (ret < 0)
336 return;
337 }
338 }
339 kfree(hinfo->htab);
340}
WANG Congddf97cc2016-02-22 15:57:53 -0800341EXPORT_SYMBOL(tcf_hashinfo_destroy);
WANG Cong1d4150c2016-02-22 15:57:52 -0800342
WANG Cong1f747c22013-12-15 20:15:10 -0800343static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344static DEFINE_RWLOCK(act_mod_lock);
345
WANG Congddf97cc2016-02-22 15:57:53 -0800346int tcf_register_action(struct tc_action_ops *act,
347 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
WANG Cong1f747c22013-12-15 20:15:10 -0800349 struct tc_action_ops *a;
WANG Congddf97cc2016-02-22 15:57:53 -0800350 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
WANG Congddf97cc2016-02-22 15:57:53 -0800352 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500353 return -EINVAL;
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800356 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
358 write_unlock(&act_mod_lock);
359 return -EEXIST;
360 }
361 }
WANG Cong1f747c22013-12-15 20:15:10 -0800362 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 write_unlock(&act_mod_lock);
WANG Congddf97cc2016-02-22 15:57:53 -0800364
365 ret = register_pernet_subsys(ops);
366 if (ret) {
367 tcf_unregister_action(act, ops);
368 return ret;
369 }
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return 0;
372}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800373EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
WANG Congddf97cc2016-02-22 15:57:53 -0800375int tcf_unregister_action(struct tc_action_ops *act,
376 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
WANG Cong1f747c22013-12-15 20:15:10 -0800378 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 int err = -ENOENT;
380
WANG Congddf97cc2016-02-22 15:57:53 -0800381 unregister_pernet_subsys(ops);
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800384 list_for_each_entry(a, &act_base, head) {
385 if (a == act) {
386 list_del(&act->head);
387 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391 write_unlock(&act_mod_lock);
392 return err;
393}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800394EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396/* lookup by name */
397static struct tc_action_ops *tc_lookup_action_n(char *kind)
398{
Eric Dumazeta7928662013-12-20 12:32:32 -0800399 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 if (kind) {
402 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800403 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800405 if (try_module_get(a->owner))
406 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 break;
408 }
409 }
410 read_unlock(&act_mod_lock);
411 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800412 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800415/* lookup by nlattr */
416static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Eric Dumazeta7928662013-12-20 12:32:32 -0800418 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 if (kind) {
421 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800422 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800423 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800424 if (try_module_get(a->owner))
425 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 break;
427 }
428 }
429 read_unlock(&act_mod_lock);
430 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800431 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
WANG Cong33be6272013-12-15 20:15:05 -0800434int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900435 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000437 const struct tc_action *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 int ret = -1;
439
440 if (skb->tc_verd & TC_NCLS) {
441 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 ret = TC_ACT_OK;
443 goto exec_done;
444 }
WANG Cong33be6272013-12-15 20:15:05 -0800445 list_for_each_entry(a, actions, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500447 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500448 if (ret == TC_ACT_REPEAT)
449 goto repeat; /* we need a ttl - JHS */
450 if (ret != TC_ACT_PIPE)
451 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return ret;
455}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800456EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
WANG Cong55334a52014-02-11 17:07:34 -0800458int tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
WANG Cong33be6272013-12-15 20:15:05 -0800460 struct tc_action *a, *tmp;
WANG Cong55334a52014-02-11 17:07:34 -0800461 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
WANG Cong33be6272013-12-15 20:15:05 -0800463 list_for_each_entry_safe(a, tmp, actions, list) {
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200464 ret = __tcf_hash_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800465 if (ret == ACT_P_DELETED)
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500466 module_put(a->ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800467 else if (ret < 0)
468 return ret;
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500469 list_del(&a->list);
470 kfree(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
WANG Cong55334a52014-02-11 17:07:34 -0800472 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
475int
476tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
477{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return a->ops->dump(skb, a, bind, ref);
479}
480
481int
482tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
483{
484 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700485 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800486 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
David S. Miller1b34ec42012-03-29 05:11:39 -0400488 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
489 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800491 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800492 nest = nla_nest_start(skb, TCA_OPTIONS);
493 if (nest == NULL)
494 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000495 err = tcf_action_dump_old(skb, a, bind, ref);
496 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800497 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return err;
499 }
500
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800501nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700502 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return -1;
504}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800505EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400507int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
508 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 struct tc_action *a;
511 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800512 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
WANG Cong33be6272013-12-15 20:15:05 -0800514 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800515 nest = nla_nest_start(skb, a->order);
516 if (nest == NULL)
517 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 err = tcf_action_dump_1(skb, a, bind, ref);
519 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700520 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800521 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523
524 return 0;
525
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800526nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700527 err = -EINVAL;
528errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800529 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700530 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000533struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
534 struct nlattr *est, char *name, int ovr,
535 int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 struct tc_action *a;
538 struct tc_action_ops *a_o;
539 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000540 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800541 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800542 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800545 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
546 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800548 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800549 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (kind == NULL)
551 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800552 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 goto err_out;
554 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800555 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
557 goto err_out;
558 }
559
560 a_o = tc_lookup_action_n(act_name);
561 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700562#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800564 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 rtnl_lock();
566
567 a_o = tc_lookup_action_n(act_name);
568
569 /* We dropped the RTNL semaphore in order to
570 * perform the module load. So, even if we
571 * succeeded in loading the module we have to
572 * tell the caller to replay the request. We
573 * indicate this using -EAGAIN.
574 */
575 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800576 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 goto err_mod;
578 }
579#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800580 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 goto err_out;
582 }
583
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800584 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700585 a = kzalloc(sizeof(*a), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (a == NULL)
587 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
WANG Congc779f7a2014-01-17 11:37:02 -0800589 a->ops = a_o;
WANG Cong33be6272013-12-15 20:15:05 -0800590 INIT_LIST_HEAD(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /* backward compatibility for policer */
592 if (name == NULL)
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000593 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 else
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000595 err = a_o->init(net, nla, est, a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800596 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 goto err_free;
598
599 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000600 * if it exists and is only bound to in a_o->init() then
601 * ACT_P_CREATED is not returned (a zero is).
602 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800603 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return a;
607
608err_free:
609 kfree(a);
610err_mod:
611 module_put(a_o->owner);
612err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800613 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
WANG Cong33be6272013-12-15 20:15:05 -0800616int tcf_action_init(struct net *net, struct nlattr *nla,
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000617 struct nlattr *est, char *name, int ovr,
WANG Cong33be6272013-12-15 20:15:05 -0800618 int bind, struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000620 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800621 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800622 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 int i;
624
Patrick McHardycee63722008-01-23 20:33:32 -0800625 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
626 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800627 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800629 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000630 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800631 if (IS_ERR(act)) {
632 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800634 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800635 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800636 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
WANG Cong33be6272013-12-15 20:15:05 -0800638 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640err:
WANG Cong33be6272013-12-15 20:15:05 -0800641 tcf_action_destroy(actions, bind);
642 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
645int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
646 int compat_mode)
647{
648 int err = 0;
649 struct gnet_dump d;
WANG Cong7eb88962014-01-09 16:14:05 -0800650 struct tcf_common *p = a->priv;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900651
WANG Cong7eb88962014-01-09 16:14:05 -0800652 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 goto errout;
654
655 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400656 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
658 if (compat_mode) {
659 if (a->type == TCA_OLD_COMPAT)
660 err = gnet_stats_start_copy_compat(skb, 0,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200661 TCA_STATS,
662 TCA_XSTATS,
663 &p->tcfc_lock, &d,
664 TCA_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 else
666 return 0;
667 } else
668 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200669 &p->tcfc_lock, &d, TCA_ACT_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 if (err < 0)
672 goto errout;
673
Eric Dumazetedb09eb2016-06-06 09:37:16 -0700674 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfc_bstats) < 0 ||
WANG Cong7eb88962014-01-09 16:14:05 -0800675 gnet_stats_copy_rate_est(&d, &p->tcfc_bstats,
676 &p->tcfc_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -0700677 gnet_stats_copy_queue(&d, p->cpu_qstats,
John Fastabend64015852014-09-28 11:53:57 -0700678 &p->tcfc_qstats,
679 p->tcfc_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 goto errout;
681
682 if (gnet_stats_finish_copy(&d) < 0)
683 goto errout;
684
685 return 0;
686
687errout:
688 return -1;
689}
690
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400691static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
692 u32 portid, u32 seq, u16 flags, int event, int bind,
693 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
695 struct tcamsg *t;
696 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700697 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800698 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Eric W. Biederman15e47302012-09-07 20:12:54 +0000700 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700701 if (!nlh)
702 goto out_nlmsg_trim;
703 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700705 t->tca__pad1 = 0;
706 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900707
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800708 nest = nla_nest_start(skb, TCA_ACT_TAB);
709 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700710 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
WANG Cong33be6272013-12-15 20:15:05 -0800712 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700713 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800715 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900716
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700717 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return skb->len;
719
David S. Miller8b00a532012-06-26 21:39:32 -0700720out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700721 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return -1;
723}
724
725static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000726act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800727 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
729 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
732 if (!skb)
733 return -ENOBUFS;
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400734 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
735 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 kfree_skb(skb);
737 return -EINVAL;
738 }
Thomas Graf2942e902006-08-15 00:30:25 -0700739
Eric W. Biederman15e47302012-09-07 20:12:54 +0000740 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
WANG Cong03701d62014-02-11 17:07:35 -0800743static struct tc_action *create_a(int i)
744{
745 struct tc_action *act;
746
747 act = kzalloc(sizeof(*act), GFP_KERNEL);
748 if (act == NULL) {
749 pr_debug("create_a: failed to alloc!\n");
750 return NULL;
751 }
752 act->order = i;
753 INIT_LIST_HEAD(&act->list);
754 return act;
755}
756
WANG Congddf97cc2016-02-22 15:57:53 -0800757static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
758 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000760 struct nlattr *tb[TCA_ACT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 struct tc_action *a;
762 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800763 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Patrick McHardycee63722008-01-23 20:33:32 -0800765 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
766 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800767 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Patrick McHardycee63722008-01-23 20:33:32 -0800769 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800770 if (tb[TCA_ACT_INDEX] == NULL ||
771 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800772 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800773 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800775 err = -ENOMEM;
WANG Cong03701d62014-02-11 17:07:35 -0800776 a = create_a(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (a == NULL)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800778 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800780 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800781 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500782 if (a->ops == NULL) /* could happen in batch of actions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 goto err_free;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800784 err = -ENOENT;
WANG Congddf97cc2016-02-22 15:57:53 -0800785 if (a->ops->lookup(net, a, index) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 goto err_mod;
787
788 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791err_mod:
792 module_put(a->ops->owner);
793err_free:
794 kfree(a);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800795err_out:
796 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
WANG Cong33be6272013-12-15 20:15:05 -0800799static void cleanup_a(struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
WANG Cong33be6272013-12-15 20:15:05 -0800801 struct tc_action *a, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
WANG Cong33be6272013-12-15 20:15:05 -0800803 list_for_each_entry_safe(a, tmp, actions, list) {
804 list_del(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 kfree(a);
806 }
807}
808
Tom Goff7316ae82010-03-19 15:40:13 +0000809static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000810 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 struct sk_buff *skb;
813 unsigned char *b;
814 struct nlmsghdr *nlh;
815 struct tcamsg *t;
816 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800817 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000818 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800819 struct nlattr *kind;
WANG Cong03701d62014-02-11 17:07:35 -0800820 struct tc_action a;
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700821 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
824 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000825 pr_debug("tca_action_flush: failed skb alloc\n");
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700826 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700829 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Patrick McHardycee63722008-01-23 20:33:32 -0800831 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
832 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 goto err_out;
834
Patrick McHardycee63722008-01-23 20:33:32 -0800835 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800836 kind = tb[TCA_ACT_KIND];
WANG Cong03701d62014-02-11 17:07:35 -0800837 memset(&a, 0, sizeof(struct tc_action));
838 INIT_LIST_HEAD(&a.list);
839 a.ops = tc_lookup_action(kind);
840 if (a.ops == NULL) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 goto err_out;
842
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400843 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
844 sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700845 if (!nlh)
846 goto out_module_put;
847 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700849 t->tca__pad1 = 0;
850 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800852 nest = nla_nest_start(skb, TCA_ACT_TAB);
853 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700854 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
WANG Congddf97cc2016-02-22 15:57:53 -0800856 err = a.ops->walk(net, skb, &dcb, RTM_DELACTION, &a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (err < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700858 goto out_module_put;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700859 if (err == 0)
860 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800862 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700864 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Cong03701d62014-02-11 17:07:35 -0800866 module_put(a.ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000867 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000868 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (err > 0)
870 return 0;
871
872 return err;
873
David S. Miller8b00a532012-06-26 21:39:32 -0700874out_module_put:
WANG Cong03701d62014-02-11 17:07:35 -0800875 module_put(a.ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700877noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return err;
880}
881
882static int
WANG Conga56e1952014-01-09 16:14:00 -0800883tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
884 u32 portid)
885{
886 int ret;
887 struct sk_buff *skb;
888
889 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
890 if (!skb)
891 return -ENOBUFS;
892
893 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
894 0, 1) <= 0) {
895 kfree_skb(skb);
896 return -EINVAL;
897 }
898
899 /* now do the delete */
WANG Cong55334a52014-02-11 17:07:34 -0800900 ret = tcf_action_destroy(actions, 0);
901 if (ret < 0) {
902 kfree_skb(skb);
903 return ret;
904 }
WANG Conga56e1952014-01-09 16:14:00 -0800905
906 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
907 n->nlmsg_flags & NLM_F_ECHO);
908 if (ret > 0)
909 return 0;
910 return ret;
911}
912
913static int
Tom Goff7316ae82010-03-19 15:40:13 +0000914tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000915 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Patrick McHardycee63722008-01-23 20:33:32 -0800917 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000918 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800919 struct tc_action *act;
920 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Patrick McHardycee63722008-01-23 20:33:32 -0800922 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
923 if (ret < 0)
924 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000926 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700927 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000928 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700929 else
930 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800933 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
WANG Congddf97cc2016-02-22 15:57:53 -0800934 act = tcf_action_get_1(net, tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800935 if (IS_ERR(act)) {
936 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800938 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800939 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800940 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942
943 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800944 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -0800946 ret = tcf_del_notify(net, n, &actions, portid);
947 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return ret;
950 }
951err:
WANG Cong33be6272013-12-15 20:15:05 -0800952 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return ret;
954}
955
WANG Conga56e1952014-01-09 16:14:00 -0800956static int
957tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
958 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 int err = 0;
962
963 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
964 if (!skb)
965 return -ENOBUFS;
966
WANG Conga56e1952014-01-09 16:14:00 -0800967 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
968 RTM_NEWACTION, 0, 0) <= 0) {
969 kfree_skb(skb);
970 return -EINVAL;
971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
WANG Conga56e1952014-01-09 16:14:00 -0800973 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
974 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (err > 0)
976 err = 0;
977 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980static int
Tom Goff7316ae82010-03-19 15:40:13 +0000981tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000982 u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
984 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -0800985 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
WANG Cong33be6272013-12-15 20:15:05 -0800987 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
988 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 goto done;
990
991 /* dump then free all the actions after update; inserted policy
992 * stays intact
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000993 */
WANG Conga56e1952014-01-09 16:14:00 -0800994 ret = tcf_add_notify(net, n, &actions, portid);
WANG Cong33be6272013-12-15 20:15:05 -0800995 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996done:
997 return ret;
998}
999
Thomas Graf661d2962013-03-21 07:45:29 +00001000static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001002 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001003 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +00001004 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 int ret = 0, ovr = 0;
1006
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001007 if ((n->nlmsg_type != RTM_GETACTION) &&
1008 !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001009 return -EPERM;
1010
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001011 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
1012 if (ret < 0)
1013 return ret;
1014
1015 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001016 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return -EINVAL;
1018 }
1019
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001020 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 switch (n->nlmsg_type) {
1022 case RTM_NEWACTION:
1023 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001024 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 * Note that CREATE | EXCL implies that
1026 * but since we want avoid ambiguity (eg when flags
1027 * is zero) then just set this
1028 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001029 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 ovr = 1;
1031replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +00001032 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (ret == -EAGAIN)
1034 goto replay;
1035 break;
1036 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001037 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001038 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 break;
1040 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001041 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001042 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 break;
1044 default:
1045 BUG();
1046 }
1047
1048 return ret;
1049}
1050
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001051static struct nlattr *
Patrick McHardy3a6c2b42009-08-25 16:07:40 +02001052find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001054 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001055 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1056 struct nlattr *nla[TCAA_MAX + 1];
1057 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Patrick McHardyc96c9472008-01-23 20:32:58 -08001059 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001061 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (tb1 == NULL)
1063 return NULL;
1064
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001065 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1066 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Patrick McHardy6d834e02008-01-23 20:32:42 -08001069 if (tb[1] == NULL)
1070 return NULL;
1071 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1072 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001074 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Thomas Graf26dab892006-07-05 20:45:06 -07001076 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
1079static int
1080tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1081{
WANG Congddf97cc2016-02-22 15:57:53 -08001082 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001084 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001085 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 struct tc_action_ops *a_o;
1087 struct tc_action a;
1088 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001089 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001090 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001093 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return 0;
1095 }
1096
Thomas Graf26dab892006-07-05 20:45:06 -07001097 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001098 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 memset(&a, 0, sizeof(struct tc_action));
1102 a.ops = a_o;
1103
Eric W. Biederman15e47302012-09-07 20:12:54 +00001104 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001105 cb->nlh->nlmsg_type, sizeof(*t), 0);
1106 if (!nlh)
1107 goto out_module_put;
1108 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001110 t->tca__pad1 = 0;
1111 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001113 nest = nla_nest_start(skb, TCA_ACT_TAB);
1114 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001115 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
WANG Congddf97cc2016-02-22 15:57:53 -08001117 ret = a_o->walk(net, skb, cb, RTM_GETACTION, &a);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001119 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001122 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 ret = skb->len;
1124 } else
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001125 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001127 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001128 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 nlh->nlmsg_flags |= NLM_F_MULTI;
1130 module_put(a_o->owner);
1131 return skb->len;
1132
David S. Miller8b00a532012-06-26 21:39:32 -07001133out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001135 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return skb->len;
1137}
1138
1139static int __init tc_action_init(void)
1140{
Greg Rosec7ac8672011-06-10 01:27:09 +00001141 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1142 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1143 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1144 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return 0;
1147}
1148
1149subsys_initcall(tc_action_init);