blob: 336774a535c3959f4f25b05d1732c014d0d1763c [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 Congddf97cc2016-02-22 15:57:53 -0800227int 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 Cong86062032014-02-11 17:07:31 -0800238 return 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700239 }
WANG Cong86062032014-02-11 17:07:31 -0800240 return 0;
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;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800286 if (est) {
Eric Dumazet519c8182015-07-06 05:18:04 -0700287 err = gen_new_estimator(&p->tcfc_bstats, p->cpu_bstats,
288 &p->tcfc_rate_est,
289 &p->tcfc_lock, est);
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800290 if (err) {
Eric Dumazet519c8182015-07-06 05:18:04 -0700291 free_percpu(p->cpu_qstats);
292 goto err2;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800293 }
294 }
295
David S. Millere9ce1cd2006-08-21 23:54:55 -0700296 a->priv = (void *) p;
WANG Congddf97cc2016-02-22 15:57:53 -0800297 a->hinfo = hinfo;
WANG Cong86062032014-02-11 17:07:31 -0800298 return 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700299}
300EXPORT_SYMBOL(tcf_hash_create);
301
WANG Congddf97cc2016-02-22 15:57:53 -0800302void tcf_hash_insert(struct tc_action_net *tn, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700303{
WANG Cong86062032014-02-11 17:07:31 -0800304 struct tcf_common *p = a->priv;
WANG Congddf97cc2016-02-22 15:57:53 -0800305 struct tcf_hashinfo *hinfo = tn->hinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700306 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
307
WANG Cong89819dc2013-12-15 20:15:09 -0800308 spin_lock_bh(&hinfo->lock);
309 hlist_add_head(&p->tcfc_head, &hinfo->htab[h]);
310 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700311}
312EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
WANG Congddf97cc2016-02-22 15:57:53 -0800314void tcf_hashinfo_destroy(const struct tc_action_ops *ops,
315 struct tcf_hashinfo *hinfo)
WANG Cong1d4150c2016-02-22 15:57:52 -0800316{
WANG Cong1d4150c2016-02-22 15:57:52 -0800317 struct tc_action a = {
318 .ops = ops,
WANG Congddf97cc2016-02-22 15:57:53 -0800319 .hinfo = hinfo,
WANG Cong1d4150c2016-02-22 15:57:52 -0800320 };
321 int i;
322
323 for (i = 0; i < hinfo->hmask + 1; i++) {
324 struct tcf_common *p;
325 struct hlist_node *n;
326
327 hlist_for_each_entry_safe(p, n, &hinfo->htab[i], tcfc_head) {
328 int ret;
329
330 a.priv = p;
331 ret = __tcf_hash_release(&a, false, true);
332 if (ret == ACT_P_DELETED)
333 module_put(ops->owner);
334 else if (ret < 0)
335 return;
336 }
337 }
338 kfree(hinfo->htab);
339}
WANG Congddf97cc2016-02-22 15:57:53 -0800340EXPORT_SYMBOL(tcf_hashinfo_destroy);
WANG Cong1d4150c2016-02-22 15:57:52 -0800341
WANG Cong1f747c22013-12-15 20:15:10 -0800342static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343static DEFINE_RWLOCK(act_mod_lock);
344
WANG Congddf97cc2016-02-22 15:57:53 -0800345int tcf_register_action(struct tc_action_ops *act,
346 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
WANG Cong1f747c22013-12-15 20:15:10 -0800348 struct tc_action_ops *a;
WANG Congddf97cc2016-02-22 15:57:53 -0800349 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
WANG Congddf97cc2016-02-22 15:57:53 -0800351 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500352 return -EINVAL;
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800355 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
357 write_unlock(&act_mod_lock);
358 return -EEXIST;
359 }
360 }
WANG Cong1f747c22013-12-15 20:15:10 -0800361 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 write_unlock(&act_mod_lock);
WANG Congddf97cc2016-02-22 15:57:53 -0800363
364 ret = register_pernet_subsys(ops);
365 if (ret) {
366 tcf_unregister_action(act, ops);
367 return ret;
368 }
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return 0;
371}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800372EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
WANG Congddf97cc2016-02-22 15:57:53 -0800374int tcf_unregister_action(struct tc_action_ops *act,
375 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
WANG Cong1f747c22013-12-15 20:15:10 -0800377 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 int err = -ENOENT;
379
WANG Congddf97cc2016-02-22 15:57:53 -0800380 unregister_pernet_subsys(ops);
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800383 list_for_each_entry(a, &act_base, head) {
384 if (a == act) {
385 list_del(&act->head);
386 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390 write_unlock(&act_mod_lock);
391 return err;
392}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800393EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395/* lookup by name */
396static struct tc_action_ops *tc_lookup_action_n(char *kind)
397{
Eric Dumazeta7928662013-12-20 12:32:32 -0800398 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 if (kind) {
401 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800402 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800404 if (try_module_get(a->owner))
405 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 break;
407 }
408 }
409 read_unlock(&act_mod_lock);
410 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800411 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800414/* lookup by nlattr */
415static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Eric Dumazeta7928662013-12-20 12:32:32 -0800417 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 if (kind) {
420 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800421 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800422 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800423 if (try_module_get(a->owner))
424 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 break;
426 }
427 }
428 read_unlock(&act_mod_lock);
429 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800430 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
WANG Cong33be6272013-12-15 20:15:05 -0800433int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900434 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000436 const struct tc_action *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 int ret = -1;
438
439 if (skb->tc_verd & TC_NCLS) {
440 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 ret = TC_ACT_OK;
442 goto exec_done;
443 }
WANG Cong33be6272013-12-15 20:15:05 -0800444 list_for_each_entry(a, actions, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500446 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500447 if (ret == TC_ACT_REPEAT)
448 goto repeat; /* we need a ttl - JHS */
449 if (ret != TC_ACT_PIPE)
450 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return ret;
454}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800455EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
WANG Cong55334a52014-02-11 17:07:34 -0800457int tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
WANG Cong33be6272013-12-15 20:15:05 -0800459 struct tc_action *a, *tmp;
WANG Cong55334a52014-02-11 17:07:34 -0800460 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
WANG Cong33be6272013-12-15 20:15:05 -0800462 list_for_each_entry_safe(a, tmp, actions, list) {
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200463 ret = __tcf_hash_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800464 if (ret == ACT_P_DELETED)
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500465 module_put(a->ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800466 else if (ret < 0)
467 return ret;
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500468 list_del(&a->list);
469 kfree(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
WANG Cong55334a52014-02-11 17:07:34 -0800471 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
474int
475tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
476{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return a->ops->dump(skb, a, bind, ref);
478}
479
480int
481tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
482{
483 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700484 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800485 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
David S. Miller1b34ec42012-03-29 05:11:39 -0400487 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
488 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800490 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800491 nest = nla_nest_start(skb, TCA_OPTIONS);
492 if (nest == NULL)
493 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000494 err = tcf_action_dump_old(skb, a, bind, ref);
495 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800496 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return err;
498 }
499
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800500nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700501 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return -1;
503}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800504EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506int
WANG Cong33be6272013-12-15 20:15:05 -0800507tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 struct tc_action *a;
510 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800511 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
WANG Cong33be6272013-12-15 20:15:05 -0800513 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800514 nest = nla_nest_start(skb, a->order);
515 if (nest == NULL)
516 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 err = tcf_action_dump_1(skb, a, bind, ref);
518 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700519 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800520 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522
523 return 0;
524
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800525nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700526 err = -EINVAL;
527errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800528 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700529 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000532struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
533 struct nlattr *est, char *name, int ovr,
534 int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 struct tc_action *a;
537 struct tc_action_ops *a_o;
538 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000539 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800540 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800541 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800544 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
545 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800547 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800548 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (kind == NULL)
550 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800551 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 goto err_out;
553 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800554 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
556 goto err_out;
557 }
558
559 a_o = tc_lookup_action_n(act_name);
560 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700561#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800563 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 rtnl_lock();
565
566 a_o = tc_lookup_action_n(act_name);
567
568 /* We dropped the RTNL semaphore in order to
569 * perform the module load. So, even if we
570 * succeeded in loading the module we have to
571 * tell the caller to replay the request. We
572 * indicate this using -EAGAIN.
573 */
574 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800575 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 goto err_mod;
577 }
578#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800579 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 goto err_out;
581 }
582
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800583 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700584 a = kzalloc(sizeof(*a), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (a == NULL)
586 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
WANG Congc779f7a2014-01-17 11:37:02 -0800588 a->ops = a_o;
WANG Cong33be6272013-12-15 20:15:05 -0800589 INIT_LIST_HEAD(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 /* backward compatibility for policer */
591 if (name == NULL)
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000592 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 else
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000594 err = a_o->init(net, nla, est, a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800595 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 goto err_free;
597
598 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000599 * if it exists and is only bound to in a_o->init() then
600 * ACT_P_CREATED is not returned (a zero is).
601 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800602 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return a;
606
607err_free:
608 kfree(a);
609err_mod:
610 module_put(a_o->owner);
611err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800612 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
WANG Cong33be6272013-12-15 20:15:05 -0800615int tcf_action_init(struct net *net, struct nlattr *nla,
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000616 struct nlattr *est, char *name, int ovr,
WANG Cong33be6272013-12-15 20:15:05 -0800617 int bind, struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000619 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800620 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800621 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 int i;
623
Patrick McHardycee63722008-01-23 20:33:32 -0800624 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
625 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800626 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800628 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000629 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800630 if (IS_ERR(act)) {
631 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800633 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800634 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800635 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
WANG Cong33be6272013-12-15 20:15:05 -0800637 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639err:
WANG Cong33be6272013-12-15 20:15:05 -0800640 tcf_action_destroy(actions, bind);
641 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
645 int compat_mode)
646{
647 int err = 0;
648 struct gnet_dump d;
WANG Cong7eb88962014-01-09 16:14:05 -0800649 struct tcf_common *p = a->priv;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900650
WANG Cong7eb88962014-01-09 16:14:05 -0800651 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 goto errout;
653
654 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400655 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 */
657 if (compat_mode) {
658 if (a->type == TCA_OLD_COMPAT)
659 err = gnet_stats_start_copy_compat(skb, 0,
Nicolas Dichtel9854518e2016-04-26 10:06:18 +0200660 TCA_STATS,
661 TCA_XSTATS,
662 &p->tcfc_lock, &d,
663 TCA_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 else
665 return 0;
666 } else
667 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
Nicolas Dichtel9854518e2016-04-26 10:06:18 +0200668 &p->tcfc_lock, &d, TCA_ACT_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 if (err < 0)
671 goto errout;
672
Eric Dumazet519c8182015-07-06 05:18:04 -0700673 if (gnet_stats_copy_basic(&d, p->cpu_bstats, &p->tcfc_bstats) < 0 ||
WANG Cong7eb88962014-01-09 16:14:05 -0800674 gnet_stats_copy_rate_est(&d, &p->tcfc_bstats,
675 &p->tcfc_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -0700676 gnet_stats_copy_queue(&d, p->cpu_qstats,
John Fastabend64015852014-09-28 11:53:57 -0700677 &p->tcfc_qstats,
678 p->tcfc_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 goto errout;
680
681 if (gnet_stats_finish_copy(&d) < 0)
682 goto errout;
683
684 return 0;
685
686errout:
687 return -1;
688}
689
690static int
WANG Cong33be6272013-12-15 20:15:05 -0800691tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900692 u16 flags, int event, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 struct tcamsg *t;
695 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700696 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800697 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Eric W. Biederman15e47302012-09-07 20:12:54 +0000699 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700700 if (!nlh)
701 goto out_nlmsg_trim;
702 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700704 t->tca__pad1 = 0;
705 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900706
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800707 nest = nla_nest_start(skb, TCA_ACT_TAB);
708 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700709 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
WANG Cong33be6272013-12-15 20:15:05 -0800711 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700712 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800714 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900715
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700716 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return skb->len;
718
David S. Miller8b00a532012-06-26 21:39:32 -0700719out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700720 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return -1;
722}
723
724static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000725act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800726 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
731 if (!skb)
732 return -ENOBUFS;
WANG Cong33be6272013-12-15 20:15:05 -0800733 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 kfree_skb(skb);
735 return -EINVAL;
736 }
Thomas Graf2942e902006-08-15 00:30:25 -0700737
Eric W. Biederman15e47302012-09-07 20:12:54 +0000738 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
WANG Cong03701d62014-02-11 17:07:35 -0800741static struct tc_action *create_a(int i)
742{
743 struct tc_action *act;
744
745 act = kzalloc(sizeof(*act), GFP_KERNEL);
746 if (act == NULL) {
747 pr_debug("create_a: failed to alloc!\n");
748 return NULL;
749 }
750 act->order = i;
751 INIT_LIST_HEAD(&act->list);
752 return act;
753}
754
WANG Congddf97cc2016-02-22 15:57:53 -0800755static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
756 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000758 struct nlattr *tb[TCA_ACT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 struct tc_action *a;
760 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800761 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Patrick McHardycee63722008-01-23 20:33:32 -0800763 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
764 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800765 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Patrick McHardycee63722008-01-23 20:33:32 -0800767 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800768 if (tb[TCA_ACT_INDEX] == NULL ||
769 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800770 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800771 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800773 err = -ENOMEM;
WANG Cong03701d62014-02-11 17:07:35 -0800774 a = create_a(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (a == NULL)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800776 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800778 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800779 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500780 if (a->ops == NULL) /* could happen in batch of actions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 goto err_free;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800782 err = -ENOENT;
WANG Congddf97cc2016-02-22 15:57:53 -0800783 if (a->ops->lookup(net, a, index) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 goto err_mod;
785
786 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789err_mod:
790 module_put(a->ops->owner);
791err_free:
792 kfree(a);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800793err_out:
794 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
WANG Cong33be6272013-12-15 20:15:05 -0800797static void cleanup_a(struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
WANG Cong33be6272013-12-15 20:15:05 -0800799 struct tc_action *a, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
WANG Cong33be6272013-12-15 20:15:05 -0800801 list_for_each_entry_safe(a, tmp, actions, list) {
802 list_del(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 kfree(a);
804 }
805}
806
Tom Goff7316ae82010-03-19 15:40:13 +0000807static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000808 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 struct sk_buff *skb;
811 unsigned char *b;
812 struct nlmsghdr *nlh;
813 struct tcamsg *t;
814 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800815 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000816 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800817 struct nlattr *kind;
WANG Cong03701d62014-02-11 17:07:35 -0800818 struct tc_action a;
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700819 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
822 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000823 pr_debug("tca_action_flush: failed skb alloc\n");
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700824 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700827 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Patrick McHardycee63722008-01-23 20:33:32 -0800829 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
830 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 goto err_out;
832
Patrick McHardycee63722008-01-23 20:33:32 -0800833 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800834 kind = tb[TCA_ACT_KIND];
WANG Cong03701d62014-02-11 17:07:35 -0800835 memset(&a, 0, sizeof(struct tc_action));
836 INIT_LIST_HEAD(&a.list);
837 a.ops = tc_lookup_action(kind);
838 if (a.ops == NULL) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 goto err_out;
840
Eric W. Biederman15e47302012-09-07 20:12:54 +0000841 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700842 if (!nlh)
843 goto out_module_put;
844 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700846 t->tca__pad1 = 0;
847 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800849 nest = nla_nest_start(skb, TCA_ACT_TAB);
850 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700851 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
WANG Congddf97cc2016-02-22 15:57:53 -0800853 err = a.ops->walk(net, skb, &dcb, RTM_DELACTION, &a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (err < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700855 goto out_module_put;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700856 if (err == 0)
857 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800859 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700861 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Cong03701d62014-02-11 17:07:35 -0800863 module_put(a.ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000864 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000865 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (err > 0)
867 return 0;
868
869 return err;
870
David S. Miller8b00a532012-06-26 21:39:32 -0700871out_module_put:
WANG Cong03701d62014-02-11 17:07:35 -0800872 module_put(a.ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700874noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return err;
877}
878
879static int
WANG Conga56e1952014-01-09 16:14:00 -0800880tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
881 u32 portid)
882{
883 int ret;
884 struct sk_buff *skb;
885
886 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
887 if (!skb)
888 return -ENOBUFS;
889
890 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
891 0, 1) <= 0) {
892 kfree_skb(skb);
893 return -EINVAL;
894 }
895
896 /* now do the delete */
WANG Cong55334a52014-02-11 17:07:34 -0800897 ret = tcf_action_destroy(actions, 0);
898 if (ret < 0) {
899 kfree_skb(skb);
900 return ret;
901 }
WANG Conga56e1952014-01-09 16:14:00 -0800902
903 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
904 n->nlmsg_flags & NLM_F_ECHO);
905 if (ret > 0)
906 return 0;
907 return ret;
908}
909
910static int
Tom Goff7316ae82010-03-19 15:40:13 +0000911tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000912 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Patrick McHardycee63722008-01-23 20:33:32 -0800914 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000915 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800916 struct tc_action *act;
917 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Patrick McHardycee63722008-01-23 20:33:32 -0800919 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
920 if (ret < 0)
921 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000923 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700924 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000925 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700926 else
927 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800930 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
WANG Congddf97cc2016-02-22 15:57:53 -0800931 act = tcf_action_get_1(net, tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800932 if (IS_ERR(act)) {
933 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800935 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800936 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800937 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939
940 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800941 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -0800943 ret = tcf_del_notify(net, n, &actions, portid);
944 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return ret;
947 }
948err:
WANG Cong33be6272013-12-15 20:15:05 -0800949 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return ret;
951}
952
WANG Conga56e1952014-01-09 16:14:00 -0800953static int
954tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
955 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 int err = 0;
959
960 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
961 if (!skb)
962 return -ENOBUFS;
963
WANG Conga56e1952014-01-09 16:14:00 -0800964 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
965 RTM_NEWACTION, 0, 0) <= 0) {
966 kfree_skb(skb);
967 return -EINVAL;
968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
WANG Conga56e1952014-01-09 16:14:00 -0800970 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
971 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (err > 0)
973 err = 0;
974 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977static int
Tom Goff7316ae82010-03-19 15:40:13 +0000978tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000979 u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -0800982 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
WANG Cong33be6272013-12-15 20:15:05 -0800984 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
985 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 goto done;
987
988 /* dump then free all the actions after update; inserted policy
989 * stays intact
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000990 */
WANG Conga56e1952014-01-09 16:14:00 -0800991 ret = tcf_add_notify(net, n, &actions, portid);
WANG Cong33be6272013-12-15 20:15:05 -0800992 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993done:
994 return ret;
995}
996
Thomas Graf661d2962013-03-21 07:45:29 +0000997static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900999 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001000 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +00001001 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 int ret = 0, ovr = 0;
1003
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001004 if ((n->nlmsg_type != RTM_GETACTION) && !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001005 return -EPERM;
1006
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001007 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
1008 if (ret < 0)
1009 return ret;
1010
1011 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001012 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return -EINVAL;
1014 }
1015
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001016 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 switch (n->nlmsg_type) {
1018 case RTM_NEWACTION:
1019 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001020 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 * Note that CREATE | EXCL implies that
1022 * but since we want avoid ambiguity (eg when flags
1023 * is zero) then just set this
1024 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001025 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 ovr = 1;
1027replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +00001028 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (ret == -EAGAIN)
1030 goto replay;
1031 break;
1032 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001033 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001034 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 break;
1036 case RTM_GETACTION:
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_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 break;
1040 default:
1041 BUG();
1042 }
1043
1044 return ret;
1045}
1046
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001047static struct nlattr *
Patrick McHardy3a6c2b42009-08-25 16:07:40 +02001048find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001050 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001051 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1052 struct nlattr *nla[TCAA_MAX + 1];
1053 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Patrick McHardyc96c9472008-01-23 20:32:58 -08001055 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001057 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (tb1 == NULL)
1059 return NULL;
1060
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001061 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1062 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Patrick McHardy6d834e02008-01-23 20:32:42 -08001065 if (tb[1] == NULL)
1066 return NULL;
1067 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1068 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001070 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Thomas Graf26dab892006-07-05 20:45:06 -07001072 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
1075static int
1076tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1077{
WANG Congddf97cc2016-02-22 15:57:53 -08001078 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001080 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001081 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 struct tc_action_ops *a_o;
1083 struct tc_action a;
1084 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001085 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001086 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001089 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return 0;
1091 }
1092
Thomas Graf26dab892006-07-05 20:45:06 -07001093 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001094 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 memset(&a, 0, sizeof(struct tc_action));
1098 a.ops = a_o;
1099
Eric W. Biederman15e47302012-09-07 20:12:54 +00001100 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001101 cb->nlh->nlmsg_type, sizeof(*t), 0);
1102 if (!nlh)
1103 goto out_module_put;
1104 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001106 t->tca__pad1 = 0;
1107 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001109 nest = nla_nest_start(skb, TCA_ACT_TAB);
1110 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001111 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
WANG Congddf97cc2016-02-22 15:57:53 -08001113 ret = a_o->walk(net, skb, cb, RTM_GETACTION, &a);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001115 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001118 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 ret = skb->len;
1120 } else
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001121 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001123 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001124 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 nlh->nlmsg_flags |= NLM_F_MULTI;
1126 module_put(a_o->owner);
1127 return skb->len;
1128
David S. Miller8b00a532012-06-26 21:39:32 -07001129out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001131 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return skb->len;
1133}
1134
1135static int __init tc_action_init(void)
1136{
Greg Rosec7ac8672011-06-10 01:27:09 +00001137 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1138 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1139 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1140 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return 0;
1143}
1144
1145subsys_initcall(tc_action_init);