blob: d97419f35e7ea867993f63516f730c3dec5c93f6 [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 Conga85a9702016-07-25 16:09:41 -070041 struct tcf_common *p = (struct tcf_common *)a;
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 Conga85a9702016-07-25 16:09:41 -070057 struct tcf_common *p = (struct tcf_common *)a;
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 Conga85a9702016-07-25 16:09:41 -070070 list_del(&a->list);
WANG Congddf97cc2016-02-22 15:57:53 -080071 tcf_hash_destroy(a->hinfo, a);
WANG Cong1d4150c2016-02-22 15:57:52 -080072 ret = ACT_P_DELETED;
David S. Millere9ce1cd2006-08-21 23:54:55 -070073 }
74 }
Daniel Borkmann28e6b672015-07-29 23:35:25 +020075
David S. Millere9ce1cd2006-08-21 23:54:55 -070076 return ret;
77}
Daniel Borkmann28e6b672015-07-29 23:35:25 +020078EXPORT_SYMBOL(__tcf_hash_release);
David S. Millere9ce1cd2006-08-21 23:54:55 -070079
WANG Congddf97cc2016-02-22 15:57:53 -080080static int tcf_dump_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -070081 struct netlink_callback *cb)
David S. Millere9ce1cd2006-08-21 23:54:55 -070082{
Eric Dumazetcc7ec452011-01-19 19:26:56 +000083 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080084 struct nlattr *nest;
David S. Millere9ce1cd2006-08-21 23:54:55 -070085
WANG Cong89819dc2013-12-15 20:15:09 -080086 spin_lock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -070087
88 s_i = cb->args[0];
89
90 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Conga85a9702016-07-25 16:09:41 -070091 struct hlist_head *head;
92 struct tcf_common *p;
93
WANG Cong89819dc2013-12-15 20:15:09 -080094 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
David S. Millere9ce1cd2006-08-21 23:54:55 -070095
WANG Cong89819dc2013-12-15 20:15:09 -080096 hlist_for_each_entry_rcu(p, head, tcfc_head) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070097 index++;
98 if (index < s_i)
99 continue;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800100
WANG Conga85a9702016-07-25 16:09:41 -0700101 nest = nla_nest_start(skb, n_i);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800102 if (nest == NULL)
103 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700104 err = tcf_action_dump_1(skb, (struct tc_action *)p, 0, 0);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700105 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,
WANG Conga85a9702016-07-25 16:09:41 -0700128 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700129{
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800130 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000131 int i = 0, n_i = 0;
WANG Cong55334a52014-02-11 17:07:34 -0800132 int ret = -EINVAL;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700133
WANG Conga85a9702016-07-25 16:09:41 -0700134 nest = nla_nest_start(skb, 0);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800135 if (nest == NULL)
136 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700137 if (nla_put_string(skb, TCA_KIND, ops->kind))
David S. Miller1b34ec42012-03-29 05:11:39 -0400138 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700139 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Conga85a9702016-07-25 16:09:41 -0700140 struct hlist_head *head;
141 struct hlist_node *n;
142 struct tcf_common *p;
143
WANG Cong89819dc2013-12-15 20:15:09 -0800144 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
145 hlist_for_each_entry_safe(p, n, head, tcfc_head) {
WANG Conga85a9702016-07-25 16:09:41 -0700146 ret = __tcf_hash_release((struct tc_action *)p, false, true);
WANG Cong55334a52014-02-11 17:07:34 -0800147 if (ret == ACT_P_DELETED) {
WANG Conga85a9702016-07-25 16:09:41 -0700148 module_put(p->tcfc_act.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,
WANG Conga85a9702016-07-25 16:09:41 -0700166 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700167{
WANG Congddf97cc2016-02-22 15:57:53 -0800168 struct tcf_hashinfo *hinfo = tn->hinfo;
169
David S. Millere9ce1cd2006-08-21 23:54:55 -0700170 if (type == RTM_DELACTION) {
WANG Conga85a9702016-07-25 16:09:41 -0700171 return tcf_del_walker(hinfo, skb, ops);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700172 } else if (type == RTM_GETACTION) {
WANG Conga85a9702016-07-25 16:09:41 -0700173 return tcf_dump_walker(hinfo, skb, cb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700174 } else {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000175 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700176 return -EINVAL;
177 }
178}
WANG Congddf97cc2016-02-22 15:57:53 -0800179EXPORT_SYMBOL(tcf_generic_walker);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700180
WANG Cong6e6a50c2014-01-17 11:37:03 -0800181static struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700182{
WANG Cong89819dc2013-12-15 20:15:09 -0800183 struct tcf_common *p = NULL;
184 struct hlist_head *head;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700185
WANG Cong89819dc2013-12-15 20:15:09 -0800186 spin_lock_bh(&hinfo->lock);
187 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
188 hlist_for_each_entry_rcu(p, head, tcfc_head)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700189 if (p->tcfc_index == index)
190 break;
WANG Cong89819dc2013-12-15 20:15:09 -0800191 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700192
193 return p;
194}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700195
WANG Congddf97cc2016-02-22 15:57:53 -0800196u32 tcf_hash_new_index(struct tc_action_net *tn)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700197{
WANG Congddf97cc2016-02-22 15:57:53 -0800198 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congddafd342014-01-09 16:13:59 -0800199 u32 val = hinfo->index;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700200
201 do {
202 if (++val == 0)
203 val = 1;
204 } while (tcf_hash_lookup(val, hinfo));
205
WANG Congddafd342014-01-09 16:13:59 -0800206 hinfo->index = val;
Yang Yingliang17569fa2013-12-10 20:55:29 +0800207 return val;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700208}
209EXPORT_SYMBOL(tcf_hash_new_index);
210
WANG Conga85a9702016-07-25 16:09:41 -0700211int tcf_hash_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700212{
WANG Congddf97cc2016-02-22 15:57:53 -0800213 struct tcf_hashinfo *hinfo = tn->hinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700214 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
215
216 if (p) {
WANG Conga85a9702016-07-25 16:09:41 -0700217 *a = &p->tcfc_act;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700218 return 1;
219 }
220 return 0;
221}
WANG Cong6e6a50c2014-01-17 11:37:03 -0800222EXPORT_SYMBOL(tcf_hash_search);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700223
WANG Conga85a9702016-07-25 16:09:41 -0700224bool tcf_hash_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
WANG Congb2313072016-06-13 13:46:28 -0700225 int bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700226{
WANG Congddf97cc2016-02-22 15:57:53 -0800227 struct tcf_hashinfo *hinfo = tn->hinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700228 struct tcf_common *p = NULL;
229 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700230 if (bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700231 p->tcfc_bindcnt++;
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700232 p->tcfc_refcnt++;
WANG Conga85a9702016-07-25 16:09:41 -0700233 *a = &p->tcfc_act;
WANG Congb2313072016-06-13 13:46:28 -0700234 return true;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700235 }
WANG Congb2313072016-06-13 13:46:28 -0700236 return false;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700237}
238EXPORT_SYMBOL(tcf_hash_check);
239
WANG Cong86062032014-02-11 17:07:31 -0800240void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
241{
WANG Conga85a9702016-07-25 16:09:41 -0700242 struct tcf_common *pc = (struct tcf_common *)a;
WANG Cong86062032014-02-11 17:07:31 -0800243 if (est)
244 gen_kill_estimator(&pc->tcfc_bstats,
245 &pc->tcfc_rate_est);
Eric Dumazet519c8182015-07-06 05:18:04 -0700246 call_rcu(&pc->tcfc_rcu, free_tcf);
WANG Cong86062032014-02-11 17:07:31 -0800247}
248EXPORT_SYMBOL(tcf_hash_cleanup);
249
WANG Congddf97cc2016-02-22 15:57:53 -0800250int tcf_hash_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
WANG Conga85a9702016-07-25 16:09:41 -0700251 struct tc_action **a, const struct tc_action_ops *ops,
252 int bind, bool cpustats)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700253{
WANG Conga85a9702016-07-25 16:09:41 -0700254 struct tcf_common *p = kzalloc(ops->size, GFP_KERNEL);
WANG Congddf97cc2016-02-22 15:57:53 -0800255 struct tcf_hashinfo *hinfo = tn->hinfo;
Eric Dumazet519c8182015-07-06 05:18:04 -0700256 int err = -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700257
258 if (unlikely(!p))
WANG Cong86062032014-02-11 17:07:31 -0800259 return -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700260 p->tcfc_refcnt = 1;
261 if (bind)
262 p->tcfc_bindcnt = 1;
263
Eric Dumazet519c8182015-07-06 05:18:04 -0700264 if (cpustats) {
265 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
266 if (!p->cpu_bstats) {
267err1:
268 kfree(p);
269 return err;
270 }
271 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
272 if (!p->cpu_qstats) {
273err2:
274 free_percpu(p->cpu_bstats);
275 goto err1;
276 }
277 }
David S. Millere9ce1cd2006-08-21 23:54:55 -0700278 spin_lock_init(&p->tcfc_lock);
WANG Cong89819dc2013-12-15 20:15:09 -0800279 INIT_HLIST_NODE(&p->tcfc_head);
WANG Congddf97cc2016-02-22 15:57:53 -0800280 p->tcfc_index = index ? index : tcf_hash_new_index(tn);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700281 p->tcfc_tm.install = jiffies;
282 p->tcfc_tm.lastuse = jiffies;
Jamal Hadi Salim53eb4402016-06-06 06:32:54 -0400283 p->tcfc_tm.firstuse = 0;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800284 if (est) {
Eric Dumazet519c8182015-07-06 05:18:04 -0700285 err = gen_new_estimator(&p->tcfc_bstats, p->cpu_bstats,
286 &p->tcfc_rate_est,
Eric Dumazetedb09eb2016-06-06 09:37:16 -0700287 &p->tcfc_lock, NULL, est);
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800288 if (err) {
Eric Dumazet519c8182015-07-06 05:18:04 -0700289 free_percpu(p->cpu_qstats);
290 goto err2;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800291 }
292 }
293
WANG Conga85a9702016-07-25 16:09:41 -0700294 p->tcfc_act.hinfo = hinfo;
295 p->tcfc_act.ops = ops;
296 INIT_LIST_HEAD(&p->tcfc_act.list);
297 *a = &p->tcfc_act;
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 Conga85a9702016-07-25 16:09:41 -0700304 struct tcf_common *p = (struct tcf_common *)a;
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 int i;
318
319 for (i = 0; i < hinfo->hmask + 1; i++) {
320 struct tcf_common *p;
321 struct hlist_node *n;
322
323 hlist_for_each_entry_safe(p, n, &hinfo->htab[i], tcfc_head) {
324 int ret;
325
WANG Conga85a9702016-07-25 16:09:41 -0700326 ret = __tcf_hash_release((struct tc_action *)p, false, true);
WANG Cong1d4150c2016-02-22 15:57:52 -0800327 if (ret == ACT_P_DELETED)
328 module_put(ops->owner);
329 else if (ret < 0)
330 return;
331 }
332 }
333 kfree(hinfo->htab);
334}
WANG Congddf97cc2016-02-22 15:57:53 -0800335EXPORT_SYMBOL(tcf_hashinfo_destroy);
WANG Cong1d4150c2016-02-22 15:57:52 -0800336
WANG Cong1f747c22013-12-15 20:15:10 -0800337static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338static DEFINE_RWLOCK(act_mod_lock);
339
WANG Congddf97cc2016-02-22 15:57:53 -0800340int tcf_register_action(struct tc_action_ops *act,
341 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
WANG Cong1f747c22013-12-15 20:15:10 -0800343 struct tc_action_ops *a;
WANG Congddf97cc2016-02-22 15:57:53 -0800344 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
WANG Congddf97cc2016-02-22 15:57:53 -0800346 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500347 return -EINVAL;
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800350 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
352 write_unlock(&act_mod_lock);
353 return -EEXIST;
354 }
355 }
WANG Cong1f747c22013-12-15 20:15:10 -0800356 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 write_unlock(&act_mod_lock);
WANG Congddf97cc2016-02-22 15:57:53 -0800358
359 ret = register_pernet_subsys(ops);
360 if (ret) {
361 tcf_unregister_action(act, ops);
362 return ret;
363 }
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return 0;
366}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800367EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
WANG Congddf97cc2016-02-22 15:57:53 -0800369int tcf_unregister_action(struct tc_action_ops *act,
370 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
WANG Cong1f747c22013-12-15 20:15:10 -0800372 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 int err = -ENOENT;
374
WANG Congddf97cc2016-02-22 15:57:53 -0800375 unregister_pernet_subsys(ops);
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800378 list_for_each_entry(a, &act_base, head) {
379 if (a == act) {
380 list_del(&act->head);
381 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385 write_unlock(&act_mod_lock);
386 return err;
387}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800388EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390/* lookup by name */
391static struct tc_action_ops *tc_lookup_action_n(char *kind)
392{
Eric Dumazeta7928662013-12-20 12:32:32 -0800393 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 if (kind) {
396 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800397 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800399 if (try_module_get(a->owner))
400 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 break;
402 }
403 }
404 read_unlock(&act_mod_lock);
405 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800406 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800409/* lookup by nlattr */
410static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Eric Dumazeta7928662013-12-20 12:32:32 -0800412 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 if (kind) {
415 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800416 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800417 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800418 if (try_module_get(a->owner))
419 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 break;
421 }
422 }
423 read_unlock(&act_mod_lock);
424 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800425 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
WANG Cong33be6272013-12-15 20:15:05 -0800428int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900429 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000431 const struct tc_action *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 int ret = -1;
433
434 if (skb->tc_verd & TC_NCLS) {
435 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 ret = TC_ACT_OK;
437 goto exec_done;
438 }
WANG Cong33be6272013-12-15 20:15:05 -0800439 list_for_each_entry(a, actions, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500441 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500442 if (ret == TC_ACT_REPEAT)
443 goto repeat; /* we need a ttl - JHS */
444 if (ret != TC_ACT_PIPE)
445 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return ret;
449}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800450EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
WANG Cong55334a52014-02-11 17:07:34 -0800452int tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
WANG Cong33be6272013-12-15 20:15:05 -0800454 struct tc_action *a, *tmp;
WANG Cong55334a52014-02-11 17:07:34 -0800455 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
WANG Cong33be6272013-12-15 20:15:05 -0800457 list_for_each_entry_safe(a, tmp, actions, list) {
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200458 ret = __tcf_hash_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800459 if (ret == ACT_P_DELETED)
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500460 module_put(a->ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800461 else if (ret < 0)
462 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
WANG Cong55334a52014-02-11 17:07:34 -0800464 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
467int
468tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
469{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return a->ops->dump(skb, a, bind, ref);
471}
472
473int
474tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
475{
476 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700477 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800478 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
David S. Miller1b34ec42012-03-29 05:11:39 -0400480 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
481 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800483 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800484 nest = nla_nest_start(skb, TCA_OPTIONS);
485 if (nest == NULL)
486 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000487 err = tcf_action_dump_old(skb, a, bind, ref);
488 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800489 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return err;
491 }
492
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800493nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700494 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return -1;
496}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800497EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400499int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
500 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct tc_action *a;
503 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800504 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
WANG Cong33be6272013-12-15 20:15:05 -0800506 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800507 nest = nla_nest_start(skb, a->order);
508 if (nest == NULL)
509 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 err = tcf_action_dump_1(skb, a, bind, ref);
511 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700512 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800513 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515
516 return 0;
517
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800518nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700519 err = -EINVAL;
520errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800521 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700522 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000525struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
526 struct nlattr *est, char *name, int ovr,
527 int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 struct tc_action *a;
530 struct tc_action_ops *a_o;
531 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000532 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800533 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800534 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800537 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
538 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800540 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800541 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (kind == NULL)
543 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800544 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 goto err_out;
546 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800547 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
549 goto err_out;
550 }
551
552 a_o = tc_lookup_action_n(act_name);
553 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700554#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800556 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 rtnl_lock();
558
559 a_o = tc_lookup_action_n(act_name);
560
561 /* We dropped the RTNL semaphore in order to
562 * perform the module load. So, even if we
563 * succeeded in loading the module we have to
564 * tell the caller to replay the request. We
565 * indicate this using -EAGAIN.
566 */
567 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800568 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 goto err_mod;
570 }
571#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800572 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 goto err_out;
574 }
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 /* backward compatibility for policer */
577 if (name == NULL)
WANG Conga85a9702016-07-25 16:09:41 -0700578 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 else
WANG Conga85a9702016-07-25 16:09:41 -0700580 err = a_o->init(net, nla, est, &a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800581 if (err < 0)
WANG Conga85a9702016-07-25 16:09:41 -0700582 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000585 * if it exists and is only bound to in a_o->init() then
586 * ACT_P_CREATED is not returned (a zero is).
587 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800588 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return a;
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593err_mod:
594 module_put(a_o->owner);
595err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800596 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
WANG Cong33be6272013-12-15 20:15:05 -0800599int tcf_action_init(struct net *net, struct nlattr *nla,
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000600 struct nlattr *est, char *name, int ovr,
WANG Cong33be6272013-12-15 20:15:05 -0800601 int bind, struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000603 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800604 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800605 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 int i;
607
Patrick McHardycee63722008-01-23 20:33:32 -0800608 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
609 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800610 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800612 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000613 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800614 if (IS_ERR(act)) {
615 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800617 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800618 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800619 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
WANG Cong33be6272013-12-15 20:15:05 -0800621 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623err:
WANG Cong33be6272013-12-15 20:15:05 -0800624 tcf_action_destroy(actions, bind);
625 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
629 int compat_mode)
630{
631 int err = 0;
632 struct gnet_dump d;
WANG Conga85a9702016-07-25 16:09:41 -0700633 struct tcf_common *p = (struct tcf_common *)a;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900634
WANG Cong7eb88962014-01-09 16:14:05 -0800635 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 goto errout;
637
638 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400639 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 */
641 if (compat_mode) {
642 if (a->type == TCA_OLD_COMPAT)
643 err = gnet_stats_start_copy_compat(skb, 0,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200644 TCA_STATS,
645 TCA_XSTATS,
646 &p->tcfc_lock, &d,
647 TCA_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 else
649 return 0;
650 } else
651 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200652 &p->tcfc_lock, &d, TCA_ACT_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 if (err < 0)
655 goto errout;
656
Eric Dumazetedb09eb2016-06-06 09:37:16 -0700657 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfc_bstats) < 0 ||
WANG Cong7eb88962014-01-09 16:14:05 -0800658 gnet_stats_copy_rate_est(&d, &p->tcfc_bstats,
659 &p->tcfc_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -0700660 gnet_stats_copy_queue(&d, p->cpu_qstats,
John Fastabend64015852014-09-28 11:53:57 -0700661 &p->tcfc_qstats,
662 p->tcfc_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 goto errout;
664
665 if (gnet_stats_finish_copy(&d) < 0)
666 goto errout;
667
668 return 0;
669
670errout:
671 return -1;
672}
673
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400674static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
675 u32 portid, u32 seq, u16 flags, int event, int bind,
676 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 struct tcamsg *t;
679 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700680 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800681 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Eric W. Biederman15e47302012-09-07 20:12:54 +0000683 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700684 if (!nlh)
685 goto out_nlmsg_trim;
686 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700688 t->tca__pad1 = 0;
689 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900690
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800691 nest = nla_nest_start(skb, TCA_ACT_TAB);
692 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700693 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
WANG Cong33be6272013-12-15 20:15:05 -0800695 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700696 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800698 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900699
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700700 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return skb->len;
702
David S. Miller8b00a532012-06-26 21:39:32 -0700703out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700704 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return -1;
706}
707
708static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000709act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800710 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
715 if (!skb)
716 return -ENOBUFS;
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400717 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
718 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 kfree_skb(skb);
720 return -EINVAL;
721 }
Thomas Graf2942e902006-08-15 00:30:25 -0700722
Eric W. Biederman15e47302012-09-07 20:12:54 +0000723 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
WANG Congddf97cc2016-02-22 15:57:53 -0800726static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
727 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000729 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700730 const struct tc_action_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct tc_action *a;
732 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800733 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Patrick McHardycee63722008-01-23 20:33:32 -0800735 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
736 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800737 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Patrick McHardycee63722008-01-23 20:33:32 -0800739 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800740 if (tb[TCA_ACT_INDEX] == NULL ||
741 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800742 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800743 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800745 err = -EINVAL;
WANG Conga85a9702016-07-25 16:09:41 -0700746 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
747 if (!ops) /* could happen in batch of actions */
748 goto err_out;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800749 err = -ENOENT;
WANG Conga85a9702016-07-25 16:09:41 -0700750 if (ops->lookup(net, &a, index) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 goto err_mod;
752
WANG Conga85a9702016-07-25 16:09:41 -0700753 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756err_mod:
WANG Conga85a9702016-07-25 16:09:41 -0700757 module_put(ops->owner);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800758err_out:
759 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
WANG Cong33be6272013-12-15 20:15:05 -0800762static void cleanup_a(struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
WANG Cong33be6272013-12-15 20:15:05 -0800764 struct tc_action *a, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
WANG Cong33be6272013-12-15 20:15:05 -0800766 list_for_each_entry_safe(a, tmp, actions, list) {
767 list_del(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 kfree(a);
769 }
770}
771
Tom Goff7316ae82010-03-19 15:40:13 +0000772static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000773 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 struct sk_buff *skb;
776 unsigned char *b;
777 struct nlmsghdr *nlh;
778 struct tcamsg *t;
779 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800780 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000781 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700782 const struct tc_action_ops *ops;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800783 struct nlattr *kind;
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700784 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
787 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000788 pr_debug("tca_action_flush: failed skb alloc\n");
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700789 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700792 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Patrick McHardycee63722008-01-23 20:33:32 -0800794 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
795 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 goto err_out;
797
Patrick McHardycee63722008-01-23 20:33:32 -0800798 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800799 kind = tb[TCA_ACT_KIND];
WANG Conga85a9702016-07-25 16:09:41 -0700800 ops = tc_lookup_action(kind);
801 if (!ops) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 goto err_out;
803
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400804 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
805 sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700806 if (!nlh)
807 goto out_module_put;
808 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700810 t->tca__pad1 = 0;
811 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800813 nest = nla_nest_start(skb, TCA_ACT_TAB);
814 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700815 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
WANG Conga85a9702016-07-25 16:09:41 -0700817 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (err < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700819 goto out_module_put;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700820 if (err == 0)
821 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800823 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700825 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Conga85a9702016-07-25 16:09:41 -0700827 module_put(ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000828 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000829 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (err > 0)
831 return 0;
832
833 return err;
834
David S. Miller8b00a532012-06-26 21:39:32 -0700835out_module_put:
WANG Conga85a9702016-07-25 16:09:41 -0700836 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700838noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return err;
841}
842
843static int
WANG Conga56e1952014-01-09 16:14:00 -0800844tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
845 u32 portid)
846{
847 int ret;
848 struct sk_buff *skb;
849
850 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
851 if (!skb)
852 return -ENOBUFS;
853
854 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
855 0, 1) <= 0) {
856 kfree_skb(skb);
857 return -EINVAL;
858 }
859
860 /* now do the delete */
WANG Cong55334a52014-02-11 17:07:34 -0800861 ret = tcf_action_destroy(actions, 0);
862 if (ret < 0) {
863 kfree_skb(skb);
864 return ret;
865 }
WANG Conga56e1952014-01-09 16:14:00 -0800866
867 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
868 n->nlmsg_flags & NLM_F_ECHO);
869 if (ret > 0)
870 return 0;
871 return ret;
872}
873
874static int
Tom Goff7316ae82010-03-19 15:40:13 +0000875tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000876 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Patrick McHardycee63722008-01-23 20:33:32 -0800878 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000879 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800880 struct tc_action *act;
881 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Patrick McHardycee63722008-01-23 20:33:32 -0800883 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
884 if (ret < 0)
885 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000887 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700888 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000889 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700890 else
891 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800894 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
WANG Congddf97cc2016-02-22 15:57:53 -0800895 act = tcf_action_get_1(net, tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800896 if (IS_ERR(act)) {
897 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800899 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800900 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800901 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903
904 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800905 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -0800907 ret = tcf_del_notify(net, n, &actions, portid);
908 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return ret;
911 }
912err:
WANG Cong33be6272013-12-15 20:15:05 -0800913 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return ret;
915}
916
WANG Conga56e1952014-01-09 16:14:00 -0800917static int
918tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
919 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 int err = 0;
923
924 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
925 if (!skb)
926 return -ENOBUFS;
927
WANG Conga56e1952014-01-09 16:14:00 -0800928 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
929 RTM_NEWACTION, 0, 0) <= 0) {
930 kfree_skb(skb);
931 return -EINVAL;
932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
WANG Conga56e1952014-01-09 16:14:00 -0800934 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
935 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (err > 0)
937 err = 0;
938 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941static int
Tom Goff7316ae82010-03-19 15:40:13 +0000942tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000943 u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
945 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -0800946 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
WANG Cong33be6272013-12-15 20:15:05 -0800948 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
949 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 goto done;
951
952 /* dump then free all the actions after update; inserted policy
953 * stays intact
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000954 */
WANG Conga56e1952014-01-09 16:14:00 -0800955 ret = tcf_add_notify(net, n, &actions, portid);
WANG Cong33be6272013-12-15 20:15:05 -0800956 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957done:
958 return ret;
959}
960
Thomas Graf661d2962013-03-21 07:45:29 +0000961static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900963 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800964 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +0000965 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 int ret = 0, ovr = 0;
967
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400968 if ((n->nlmsg_type != RTM_GETACTION) &&
969 !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000970 return -EPERM;
971
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800972 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
973 if (ret < 0)
974 return ret;
975
976 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000977 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return -EINVAL;
979 }
980
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000981 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 switch (n->nlmsg_type) {
983 case RTM_NEWACTION:
984 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300985 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 * Note that CREATE | EXCL implies that
987 * but since we want avoid ambiguity (eg when flags
988 * is zero) then just set this
989 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000990 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 ovr = 1;
992replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000993 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (ret == -EAGAIN)
995 goto replay;
996 break;
997 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000998 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000999 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 break;
1001 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001002 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001003 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 break;
1005 default:
1006 BUG();
1007 }
1008
1009 return ret;
1010}
1011
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001012static struct nlattr *
Patrick McHardy3a6c2b42009-08-25 16:07:40 +02001013find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001015 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001016 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1017 struct nlattr *nla[TCAA_MAX + 1];
1018 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Patrick McHardyc96c9472008-01-23 20:32:58 -08001020 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001022 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (tb1 == NULL)
1024 return NULL;
1025
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001026 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1027 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Patrick McHardy6d834e02008-01-23 20:32:42 -08001030 if (tb[1] == NULL)
1031 return NULL;
1032 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1033 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001035 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Thomas Graf26dab892006-07-05 20:45:06 -07001037 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
1040static int
1041tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1042{
WANG Congddf97cc2016-02-22 15:57:53 -08001043 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001045 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001046 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 struct tc_action_ops *a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001049 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001050 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001053 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return 0;
1055 }
1056
Thomas Graf26dab892006-07-05 20:45:06 -07001057 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001058 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Eric W. Biederman15e47302012-09-07 20:12:54 +00001061 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001062 cb->nlh->nlmsg_type, sizeof(*t), 0);
1063 if (!nlh)
1064 goto out_module_put;
1065 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001067 t->tca__pad1 = 0;
1068 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001070 nest = nla_nest_start(skb, TCA_ACT_TAB);
1071 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001072 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
WANG Conga85a9702016-07-25 16:09:41 -07001074 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001076 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001079 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 ret = skb->len;
1081 } else
Jamal Hadi Salimebecaa62016-06-13 18:08:42 -04001082 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001084 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001085 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 nlh->nlmsg_flags |= NLM_F_MULTI;
1087 module_put(a_o->owner);
1088 return skb->len;
1089
David S. Miller8b00a532012-06-26 21:39:32 -07001090out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001092 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return skb->len;
1094}
1095
1096static int __init tc_action_init(void)
1097{
Greg Rosec7ac8672011-06-10 01:27:09 +00001098 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1099 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1100 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1101 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return 0;
1104}
1105
1106subsys_initcall(tc_action_init);