blob: 96066665e3765d342fd1685ab32f0019c7455564 [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,
WANG Cong7eb88962014-01-09 16:14:05 -0800660 TCA_STATS, TCA_XSTATS, &p->tcfc_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 else
662 return 0;
663 } else
664 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Cong7eb88962014-01-09 16:14:05 -0800665 &p->tcfc_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 if (err < 0)
668 goto errout;
669
Eric Dumazet519c8182015-07-06 05:18:04 -0700670 if (gnet_stats_copy_basic(&d, p->cpu_bstats, &p->tcfc_bstats) < 0 ||
WANG Cong7eb88962014-01-09 16:14:05 -0800671 gnet_stats_copy_rate_est(&d, &p->tcfc_bstats,
672 &p->tcfc_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -0700673 gnet_stats_copy_queue(&d, p->cpu_qstats,
John Fastabend64015852014-09-28 11:53:57 -0700674 &p->tcfc_qstats,
675 p->tcfc_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 goto errout;
677
678 if (gnet_stats_finish_copy(&d) < 0)
679 goto errout;
680
681 return 0;
682
683errout:
684 return -1;
685}
686
687static int
WANG Cong33be6272013-12-15 20:15:05 -0800688tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900689 u16 flags, int event, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 struct tcamsg *t;
692 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700693 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800694 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Eric W. Biederman15e47302012-09-07 20:12:54 +0000696 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700697 if (!nlh)
698 goto out_nlmsg_trim;
699 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700701 t->tca__pad1 = 0;
702 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900703
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800704 nest = nla_nest_start(skb, TCA_ACT_TAB);
705 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700706 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
WANG Cong33be6272013-12-15 20:15:05 -0800708 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700709 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800711 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900712
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700713 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return skb->len;
715
David S. Miller8b00a532012-06-26 21:39:32 -0700716out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700717 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return -1;
719}
720
721static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000722act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800723 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
728 if (!skb)
729 return -ENOBUFS;
WANG Cong33be6272013-12-15 20:15:05 -0800730 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 kfree_skb(skb);
732 return -EINVAL;
733 }
Thomas Graf2942e902006-08-15 00:30:25 -0700734
Eric W. Biederman15e47302012-09-07 20:12:54 +0000735 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
WANG Cong03701d62014-02-11 17:07:35 -0800738static struct tc_action *create_a(int i)
739{
740 struct tc_action *act;
741
742 act = kzalloc(sizeof(*act), GFP_KERNEL);
743 if (act == NULL) {
744 pr_debug("create_a: failed to alloc!\n");
745 return NULL;
746 }
747 act->order = i;
748 INIT_LIST_HEAD(&act->list);
749 return act;
750}
751
WANG Congddf97cc2016-02-22 15:57:53 -0800752static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
753 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000755 struct nlattr *tb[TCA_ACT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 struct tc_action *a;
757 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800758 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Patrick McHardycee63722008-01-23 20:33:32 -0800760 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
761 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800762 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Patrick McHardycee63722008-01-23 20:33:32 -0800764 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800765 if (tb[TCA_ACT_INDEX] == NULL ||
766 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800767 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800768 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800770 err = -ENOMEM;
WANG Cong03701d62014-02-11 17:07:35 -0800771 a = create_a(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (a == NULL)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800773 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800775 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800776 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500777 if (a->ops == NULL) /* could happen in batch of actions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 goto err_free;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800779 err = -ENOENT;
WANG Congddf97cc2016-02-22 15:57:53 -0800780 if (a->ops->lookup(net, a, index) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 goto err_mod;
782
783 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786err_mod:
787 module_put(a->ops->owner);
788err_free:
789 kfree(a);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800790err_out:
791 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
WANG Cong33be6272013-12-15 20:15:05 -0800794static void cleanup_a(struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
WANG Cong33be6272013-12-15 20:15:05 -0800796 struct tc_action *a, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
WANG Cong33be6272013-12-15 20:15:05 -0800798 list_for_each_entry_safe(a, tmp, actions, list) {
799 list_del(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 kfree(a);
801 }
802}
803
Tom Goff7316ae82010-03-19 15:40:13 +0000804static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000805 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 struct sk_buff *skb;
808 unsigned char *b;
809 struct nlmsghdr *nlh;
810 struct tcamsg *t;
811 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800812 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000813 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800814 struct nlattr *kind;
WANG Cong03701d62014-02-11 17:07:35 -0800815 struct tc_action a;
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700816 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
819 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000820 pr_debug("tca_action_flush: failed skb alloc\n");
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700821 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700824 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Patrick McHardycee63722008-01-23 20:33:32 -0800826 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
827 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 goto err_out;
829
Patrick McHardycee63722008-01-23 20:33:32 -0800830 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800831 kind = tb[TCA_ACT_KIND];
WANG Cong03701d62014-02-11 17:07:35 -0800832 memset(&a, 0, sizeof(struct tc_action));
833 INIT_LIST_HEAD(&a.list);
834 a.ops = tc_lookup_action(kind);
835 if (a.ops == NULL) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 goto err_out;
837
Eric W. Biederman15e47302012-09-07 20:12:54 +0000838 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700839 if (!nlh)
840 goto out_module_put;
841 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700843 t->tca__pad1 = 0;
844 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800846 nest = nla_nest_start(skb, TCA_ACT_TAB);
847 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700848 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
WANG Congddf97cc2016-02-22 15:57:53 -0800850 err = a.ops->walk(net, skb, &dcb, RTM_DELACTION, &a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (err < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700852 goto out_module_put;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700853 if (err == 0)
854 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800856 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700858 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Cong03701d62014-02-11 17:07:35 -0800860 module_put(a.ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000861 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000862 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (err > 0)
864 return 0;
865
866 return err;
867
David S. Miller8b00a532012-06-26 21:39:32 -0700868out_module_put:
WANG Cong03701d62014-02-11 17:07:35 -0800869 module_put(a.ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700871noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return err;
874}
875
876static int
WANG Conga56e1952014-01-09 16:14:00 -0800877tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
878 u32 portid)
879{
880 int ret;
881 struct sk_buff *skb;
882
883 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
884 if (!skb)
885 return -ENOBUFS;
886
887 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
888 0, 1) <= 0) {
889 kfree_skb(skb);
890 return -EINVAL;
891 }
892
893 /* now do the delete */
WANG Cong55334a52014-02-11 17:07:34 -0800894 ret = tcf_action_destroy(actions, 0);
895 if (ret < 0) {
896 kfree_skb(skb);
897 return ret;
898 }
WANG Conga56e1952014-01-09 16:14:00 -0800899
900 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
901 n->nlmsg_flags & NLM_F_ECHO);
902 if (ret > 0)
903 return 0;
904 return ret;
905}
906
907static int
Tom Goff7316ae82010-03-19 15:40:13 +0000908tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000909 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Patrick McHardycee63722008-01-23 20:33:32 -0800911 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000912 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800913 struct tc_action *act;
914 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Patrick McHardycee63722008-01-23 20:33:32 -0800916 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
917 if (ret < 0)
918 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000920 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700921 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000922 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700923 else
924 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800927 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
WANG Congddf97cc2016-02-22 15:57:53 -0800928 act = tcf_action_get_1(net, tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800929 if (IS_ERR(act)) {
930 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800932 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800933 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800934 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936
937 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800938 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -0800940 ret = tcf_del_notify(net, n, &actions, portid);
941 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return ret;
944 }
945err:
WANG Cong33be6272013-12-15 20:15:05 -0800946 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return ret;
948}
949
WANG Conga56e1952014-01-09 16:14:00 -0800950static int
951tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
952 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 int err = 0;
956
957 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
958 if (!skb)
959 return -ENOBUFS;
960
WANG Conga56e1952014-01-09 16:14:00 -0800961 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
962 RTM_NEWACTION, 0, 0) <= 0) {
963 kfree_skb(skb);
964 return -EINVAL;
965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
WANG Conga56e1952014-01-09 16:14:00 -0800967 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
968 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (err > 0)
970 err = 0;
971 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974static int
Tom Goff7316ae82010-03-19 15:40:13 +0000975tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000976 u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
978 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -0800979 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
WANG Cong33be6272013-12-15 20:15:05 -0800981 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
982 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 goto done;
984
985 /* dump then free all the actions after update; inserted policy
986 * stays intact
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000987 */
WANG Conga56e1952014-01-09 16:14:00 -0800988 ret = tcf_add_notify(net, n, &actions, portid);
WANG Cong33be6272013-12-15 20:15:05 -0800989 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990done:
991 return ret;
992}
993
Thomas Graf661d2962013-03-21 07:45:29 +0000994static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900996 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800997 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +0000998 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 int ret = 0, ovr = 0;
1000
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001001 if ((n->nlmsg_type != RTM_GETACTION) && !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001002 return -EPERM;
1003
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001004 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
1005 if (ret < 0)
1006 return ret;
1007
1008 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001009 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return -EINVAL;
1011 }
1012
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001013 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 switch (n->nlmsg_type) {
1015 case RTM_NEWACTION:
1016 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001017 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 * Note that CREATE | EXCL implies that
1019 * but since we want avoid ambiguity (eg when flags
1020 * is zero) then just set this
1021 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001022 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 ovr = 1;
1024replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +00001025 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (ret == -EAGAIN)
1027 goto replay;
1028 break;
1029 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001030 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001031 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 break;
1033 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001034 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001035 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 break;
1037 default:
1038 BUG();
1039 }
1040
1041 return ret;
1042}
1043
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001044static struct nlattr *
Patrick McHardy3a6c2b42009-08-25 16:07:40 +02001045find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001047 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001048 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1049 struct nlattr *nla[TCAA_MAX + 1];
1050 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Patrick McHardyc96c9472008-01-23 20:32:58 -08001052 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001054 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (tb1 == NULL)
1056 return NULL;
1057
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001058 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1059 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Patrick McHardy6d834e02008-01-23 20:32:42 -08001062 if (tb[1] == NULL)
1063 return NULL;
1064 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1065 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001067 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Thomas Graf26dab892006-07-05 20:45:06 -07001069 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070}
1071
1072static int
1073tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1074{
WANG Congddf97cc2016-02-22 15:57:53 -08001075 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001077 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001078 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 struct tc_action_ops *a_o;
1080 struct tc_action a;
1081 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001082 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001083 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001086 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return 0;
1088 }
1089
Thomas Graf26dab892006-07-05 20:45:06 -07001090 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001091 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 memset(&a, 0, sizeof(struct tc_action));
1095 a.ops = a_o;
1096
Eric W. Biederman15e47302012-09-07 20:12:54 +00001097 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001098 cb->nlh->nlmsg_type, sizeof(*t), 0);
1099 if (!nlh)
1100 goto out_module_put;
1101 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001103 t->tca__pad1 = 0;
1104 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001106 nest = nla_nest_start(skb, TCA_ACT_TAB);
1107 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001108 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
WANG Congddf97cc2016-02-22 15:57:53 -08001110 ret = a_o->walk(net, skb, cb, RTM_GETACTION, &a);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001112 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001115 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 ret = skb->len;
1117 } else
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001118 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001120 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001121 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 nlh->nlmsg_flags |= NLM_F_MULTI;
1123 module_put(a_o->owner);
1124 return skb->len;
1125
David S. Miller8b00a532012-06-26 21:39:32 -07001126out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001128 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 return skb->len;
1130}
1131
1132static int __init tc_action_init(void)
1133{
Greg Rosec7ac8672011-06-10 01:27:09 +00001134 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1135 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1136 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1137 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return 0;
1140}
1141
1142subsys_initcall(tc_action_init);