blob: f2e9ed34a963e1a06dbefb792836919d92bf6733 [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>
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -050027#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <net/act_api.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070029#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Jiri Pirkodb505142017-05-17 11:08:03 +020031static int tcf_action_goto_chain_init(struct tc_action *a, struct tcf_proto *tp)
32{
33 u32 chain_index = a->tcfa_action & TC_ACT_EXT_VAL_MASK;
34
35 if (!tp)
36 return -EINVAL;
WANG Cong367a8ce2017-05-23 09:42:37 -070037 a->goto_chain = tcf_chain_get(tp->chain->block, chain_index, true);
Jiri Pirkodb505142017-05-17 11:08:03 +020038 if (!a->goto_chain)
39 return -ENOMEM;
40 return 0;
41}
42
43static void tcf_action_goto_chain_fini(struct tc_action *a)
44{
45 tcf_chain_put(a->goto_chain);
46}
47
48static void tcf_action_goto_chain_exec(const struct tc_action *a,
49 struct tcf_result *res)
50{
51 const struct tcf_chain *chain = a->goto_chain;
52
53 res->goto_tp = rcu_dereference_bh(chain->filter_chain);
54}
55
Eric Dumazet519c8182015-07-06 05:18:04 -070056static void free_tcf(struct rcu_head *head)
57{
WANG Congec0595c2016-07-25 16:09:42 -070058 struct tc_action *p = container_of(head, struct tc_action, tcfa_rcu);
Eric Dumazet519c8182015-07-06 05:18:04 -070059
60 free_percpu(p->cpu_bstats);
61 free_percpu(p->cpu_qstats);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -050062
63 if (p->act_cookie) {
64 kfree(p->act_cookie->data);
65 kfree(p->act_cookie);
66 }
Jiri Pirkodb505142017-05-17 11:08:03 +020067 if (p->goto_chain)
68 tcf_action_goto_chain_fini(p);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -050069
Eric Dumazet519c8182015-07-06 05:18:04 -070070 kfree(p);
71}
72
WANG Congec0595c2016-07-25 16:09:42 -070073static void tcf_hash_destroy(struct tcf_hashinfo *hinfo, struct tc_action *p)
David S. Millere9ce1cd2006-08-21 23:54:55 -070074{
WANG Cong89819dc2013-12-15 20:15:09 -080075 spin_lock_bh(&hinfo->lock);
WANG Congec0595c2016-07-25 16:09:42 -070076 hlist_del(&p->tcfa_head);
WANG Cong89819dc2013-12-15 20:15:09 -080077 spin_unlock_bh(&hinfo->lock);
Eric Dumazet1c0d32f2016-12-04 09:48:16 -080078 gen_kill_estimator(&p->tcfa_rate_est);
WANG Cong89819dc2013-12-15 20:15:09 -080079 /*
WANG Congec0595c2016-07-25 16:09:42 -070080 * gen_estimator est_timer() might access p->tcfa_lock
WANG Cong89819dc2013-12-15 20:15:09 -080081 * or bstats, wait a RCU grace period before freeing p
82 */
WANG Congec0595c2016-07-25 16:09:42 -070083 call_rcu(&p->tcfa_rcu, free_tcf);
David S. Millere9ce1cd2006-08-21 23:54:55 -070084}
David S. Millere9ce1cd2006-08-21 23:54:55 -070085
WANG Congec0595c2016-07-25 16:09:42 -070086int __tcf_hash_release(struct tc_action *p, bool bind, bool strict)
David S. Millere9ce1cd2006-08-21 23:54:55 -070087{
88 int ret = 0;
89
90 if (p) {
91 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -070092 p->tcfa_bindcnt--;
93 else if (strict && p->tcfa_bindcnt > 0)
WANG Cong55334a52014-02-11 17:07:34 -080094 return -EPERM;
David S. Millere9ce1cd2006-08-21 23:54:55 -070095
WANG Congec0595c2016-07-25 16:09:42 -070096 p->tcfa_refcnt--;
97 if (p->tcfa_bindcnt <= 0 && p->tcfa_refcnt <= 0) {
98 if (p->ops->cleanup)
99 p->ops->cleanup(p, bind);
WANG Congec0595c2016-07-25 16:09:42 -0700100 tcf_hash_destroy(p->hinfo, p);
WANG Cong1d4150c2016-02-22 15:57:52 -0800101 ret = ACT_P_DELETED;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700102 }
103 }
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200104
David S. Millere9ce1cd2006-08-21 23:54:55 -0700105 return ret;
106}
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200107EXPORT_SYMBOL(__tcf_hash_release);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700108
WANG Congddf97cc2016-02-22 15:57:53 -0800109static int tcf_dump_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700110 struct netlink_callback *cb)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700111{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000112 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800113 struct nlattr *nest;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700114
WANG Cong89819dc2013-12-15 20:15:09 -0800115 spin_lock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700116
117 s_i = cb->args[0];
118
119 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Conga85a9702016-07-25 16:09:41 -0700120 struct hlist_head *head;
WANG Congec0595c2016-07-25 16:09:42 -0700121 struct tc_action *p;
WANG Conga85a9702016-07-25 16:09:41 -0700122
WANG Cong89819dc2013-12-15 20:15:09 -0800123 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
David S. Millere9ce1cd2006-08-21 23:54:55 -0700124
WANG Congec0595c2016-07-25 16:09:42 -0700125 hlist_for_each_entry_rcu(p, head, tcfa_head) {
David S. Millere9ce1cd2006-08-21 23:54:55 -0700126 index++;
127 if (index < s_i)
128 continue;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800129
WANG Conga85a9702016-07-25 16:09:41 -0700130 nest = nla_nest_start(skb, n_i);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800131 if (nest == NULL)
132 goto nla_put_failure;
WANG Congec0595c2016-07-25 16:09:42 -0700133 err = tcf_action_dump_1(skb, p, 0, 0);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700134 if (err < 0) {
135 index--;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800136 nlmsg_trim(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700137 goto done;
138 }
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800139 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700140 n_i++;
141 if (n_i >= TCA_ACT_MAX_PRIO)
142 goto done;
143 }
144 }
145done:
WANG Cong89819dc2013-12-15 20:15:09 -0800146 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700147 if (n_i)
148 cb->args[0] += n_i;
149 return n_i;
150
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800151nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800152 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700153 goto done;
154}
155
WANG Congddf97cc2016-02-22 15:57:53 -0800156static int tcf_del_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700157 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700158{
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800159 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000160 int i = 0, n_i = 0;
WANG Cong55334a52014-02-11 17:07:34 -0800161 int ret = -EINVAL;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700162
WANG Conga85a9702016-07-25 16:09:41 -0700163 nest = nla_nest_start(skb, 0);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800164 if (nest == NULL)
165 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700166 if (nla_put_string(skb, TCA_KIND, ops->kind))
David S. Miller1b34ec42012-03-29 05:11:39 -0400167 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700168 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Conga85a9702016-07-25 16:09:41 -0700169 struct hlist_head *head;
170 struct hlist_node *n;
WANG Congec0595c2016-07-25 16:09:42 -0700171 struct tc_action *p;
WANG Conga85a9702016-07-25 16:09:41 -0700172
WANG Cong89819dc2013-12-15 20:15:09 -0800173 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
WANG Congec0595c2016-07-25 16:09:42 -0700174 hlist_for_each_entry_safe(p, n, head, tcfa_head) {
175 ret = __tcf_hash_release(p, false, true);
WANG Cong55334a52014-02-11 17:07:34 -0800176 if (ret == ACT_P_DELETED) {
WANG Congec0595c2016-07-25 16:09:42 -0700177 module_put(p->ops->owner);
Jamal Hadi Salim805c1f42013-12-23 08:02:13 -0500178 n_i++;
WANG Cong55334a52014-02-11 17:07:34 -0800179 } else if (ret < 0)
180 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700181 }
182 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400183 if (nla_put_u32(skb, TCA_FCNT, n_i))
184 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800185 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700186
187 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800188nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800189 nla_nest_cancel(skb, nest);
WANG Cong55334a52014-02-11 17:07:34 -0800190 return ret;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700191}
192
WANG Congddf97cc2016-02-22 15:57:53 -0800193int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
194 struct netlink_callback *cb, int type,
WANG Conga85a9702016-07-25 16:09:41 -0700195 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700196{
WANG Congddf97cc2016-02-22 15:57:53 -0800197 struct tcf_hashinfo *hinfo = tn->hinfo;
198
David S. Millere9ce1cd2006-08-21 23:54:55 -0700199 if (type == RTM_DELACTION) {
WANG Conga85a9702016-07-25 16:09:41 -0700200 return tcf_del_walker(hinfo, skb, ops);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700201 } else if (type == RTM_GETACTION) {
WANG Conga85a9702016-07-25 16:09:41 -0700202 return tcf_dump_walker(hinfo, skb, cb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700203 } else {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000204 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700205 return -EINVAL;
206 }
207}
WANG Congddf97cc2016-02-22 15:57:53 -0800208EXPORT_SYMBOL(tcf_generic_walker);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700209
WANG Congec0595c2016-07-25 16:09:42 -0700210static struct tc_action *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700211{
WANG Congec0595c2016-07-25 16:09:42 -0700212 struct tc_action *p = NULL;
WANG Cong89819dc2013-12-15 20:15:09 -0800213 struct hlist_head *head;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700214
WANG Cong89819dc2013-12-15 20:15:09 -0800215 spin_lock_bh(&hinfo->lock);
216 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
WANG Congec0595c2016-07-25 16:09:42 -0700217 hlist_for_each_entry_rcu(p, head, tcfa_head)
218 if (p->tcfa_index == index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700219 break;
WANG Cong89819dc2013-12-15 20:15:09 -0800220 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700221
222 return p;
223}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700224
WANG Congddf97cc2016-02-22 15:57:53 -0800225u32 tcf_hash_new_index(struct tc_action_net *tn)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700226{
WANG Congddf97cc2016-02-22 15:57:53 -0800227 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congddafd342014-01-09 16:13:59 -0800228 u32 val = hinfo->index;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700229
230 do {
231 if (++val == 0)
232 val = 1;
233 } while (tcf_hash_lookup(val, hinfo));
234
WANG Congddafd342014-01-09 16:13:59 -0800235 hinfo->index = val;
Yang Yingliang17569fa2013-12-10 20:55:29 +0800236 return val;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700237}
238EXPORT_SYMBOL(tcf_hash_new_index);
239
WANG Conga85a9702016-07-25 16:09:41 -0700240int tcf_hash_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700241{
WANG Congddf97cc2016-02-22 15:57:53 -0800242 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700243 struct tc_action *p = tcf_hash_lookup(index, hinfo);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700244
245 if (p) {
WANG Congec0595c2016-07-25 16:09:42 -0700246 *a = p;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700247 return 1;
248 }
249 return 0;
250}
WANG Cong6e6a50c2014-01-17 11:37:03 -0800251EXPORT_SYMBOL(tcf_hash_search);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700252
WANG Conga85a9702016-07-25 16:09:41 -0700253bool tcf_hash_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
WANG Congb2313072016-06-13 13:46:28 -0700254 int bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700255{
WANG Congddf97cc2016-02-22 15:57:53 -0800256 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700257 struct tc_action *p = NULL;
258
David S. Millere9ce1cd2006-08-21 23:54:55 -0700259 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700260 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -0700261 p->tcfa_bindcnt++;
262 p->tcfa_refcnt++;
263 *a = p;
WANG Congb2313072016-06-13 13:46:28 -0700264 return true;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700265 }
WANG Congb2313072016-06-13 13:46:28 -0700266 return false;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700267}
268EXPORT_SYMBOL(tcf_hash_check);
269
WANG Cong86062032014-02-11 17:07:31 -0800270void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
271{
WANG Cong86062032014-02-11 17:07:31 -0800272 if (est)
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800273 gen_kill_estimator(&a->tcfa_rate_est);
WANG Congec0595c2016-07-25 16:09:42 -0700274 call_rcu(&a->tcfa_rcu, free_tcf);
WANG Cong86062032014-02-11 17:07:31 -0800275}
276EXPORT_SYMBOL(tcf_hash_cleanup);
277
WANG Congddf97cc2016-02-22 15:57:53 -0800278int tcf_hash_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
WANG Conga85a9702016-07-25 16:09:41 -0700279 struct tc_action **a, const struct tc_action_ops *ops,
280 int bind, bool cpustats)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700281{
WANG Congec0595c2016-07-25 16:09:42 -0700282 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
WANG Congddf97cc2016-02-22 15:57:53 -0800283 struct tcf_hashinfo *hinfo = tn->hinfo;
Eric Dumazet519c8182015-07-06 05:18:04 -0700284 int err = -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700285
286 if (unlikely(!p))
WANG Cong86062032014-02-11 17:07:31 -0800287 return -ENOMEM;
WANG Congec0595c2016-07-25 16:09:42 -0700288 p->tcfa_refcnt = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700289 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -0700290 p->tcfa_bindcnt = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700291
Eric Dumazet519c8182015-07-06 05:18:04 -0700292 if (cpustats) {
293 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
294 if (!p->cpu_bstats) {
295err1:
296 kfree(p);
297 return err;
298 }
299 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
300 if (!p->cpu_qstats) {
301err2:
302 free_percpu(p->cpu_bstats);
303 goto err1;
304 }
305 }
WANG Congec0595c2016-07-25 16:09:42 -0700306 spin_lock_init(&p->tcfa_lock);
307 INIT_HLIST_NODE(&p->tcfa_head);
308 p->tcfa_index = index ? index : tcf_hash_new_index(tn);
309 p->tcfa_tm.install = jiffies;
310 p->tcfa_tm.lastuse = jiffies;
311 p->tcfa_tm.firstuse = 0;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800312 if (est) {
WANG Congec0595c2016-07-25 16:09:42 -0700313 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
314 &p->tcfa_rate_est,
315 &p->tcfa_lock, NULL, est);
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800316 if (err) {
Eric Dumazet519c8182015-07-06 05:18:04 -0700317 free_percpu(p->cpu_qstats);
318 goto err2;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800319 }
320 }
321
WANG Congec0595c2016-07-25 16:09:42 -0700322 p->hinfo = hinfo;
323 p->ops = ops;
324 INIT_LIST_HEAD(&p->list);
325 *a = p;
WANG Cong86062032014-02-11 17:07:31 -0800326 return 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700327}
328EXPORT_SYMBOL(tcf_hash_create);
329
WANG Congddf97cc2016-02-22 15:57:53 -0800330void tcf_hash_insert(struct tc_action_net *tn, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700331{
WANG Congddf97cc2016-02-22 15:57:53 -0800332 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700333 unsigned int h = tcf_hash(a->tcfa_index, hinfo->hmask);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700334
WANG Cong89819dc2013-12-15 20:15:09 -0800335 spin_lock_bh(&hinfo->lock);
WANG Congec0595c2016-07-25 16:09:42 -0700336 hlist_add_head(&a->tcfa_head, &hinfo->htab[h]);
WANG Cong89819dc2013-12-15 20:15:09 -0800337 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700338}
339EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
WANG Congddf97cc2016-02-22 15:57:53 -0800341void tcf_hashinfo_destroy(const struct tc_action_ops *ops,
342 struct tcf_hashinfo *hinfo)
WANG Cong1d4150c2016-02-22 15:57:52 -0800343{
WANG Cong1d4150c2016-02-22 15:57:52 -0800344 int i;
345
346 for (i = 0; i < hinfo->hmask + 1; i++) {
WANG Congec0595c2016-07-25 16:09:42 -0700347 struct tc_action *p;
WANG Cong1d4150c2016-02-22 15:57:52 -0800348 struct hlist_node *n;
349
WANG Congec0595c2016-07-25 16:09:42 -0700350 hlist_for_each_entry_safe(p, n, &hinfo->htab[i], tcfa_head) {
WANG Cong1d4150c2016-02-22 15:57:52 -0800351 int ret;
352
WANG Congec0595c2016-07-25 16:09:42 -0700353 ret = __tcf_hash_release(p, false, true);
WANG Cong1d4150c2016-02-22 15:57:52 -0800354 if (ret == ACT_P_DELETED)
355 module_put(ops->owner);
356 else if (ret < 0)
357 return;
358 }
359 }
360 kfree(hinfo->htab);
361}
WANG Congddf97cc2016-02-22 15:57:53 -0800362EXPORT_SYMBOL(tcf_hashinfo_destroy);
WANG Cong1d4150c2016-02-22 15:57:52 -0800363
WANG Cong1f747c22013-12-15 20:15:10 -0800364static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365static DEFINE_RWLOCK(act_mod_lock);
366
WANG Congddf97cc2016-02-22 15:57:53 -0800367int tcf_register_action(struct tc_action_ops *act,
368 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
WANG Cong1f747c22013-12-15 20:15:10 -0800370 struct tc_action_ops *a;
WANG Congddf97cc2016-02-22 15:57:53 -0800371 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
WANG Congddf97cc2016-02-22 15:57:53 -0800373 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500374 return -EINVAL;
375
WANG Congab102b82016-10-11 10:56:45 -0700376 /* We have to register pernet ops before making the action ops visible,
377 * otherwise tcf_action_init_1() could get a partially initialized
378 * netns.
379 */
380 ret = register_pernet_subsys(ops);
381 if (ret)
382 return ret;
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800385 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
387 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700388 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return -EEXIST;
390 }
391 }
WANG Cong1f747c22013-12-15 20:15:10 -0800392 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 write_unlock(&act_mod_lock);
WANG Congddf97cc2016-02-22 15:57:53 -0800394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return 0;
396}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800397EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
WANG Congddf97cc2016-02-22 15:57:53 -0800399int tcf_unregister_action(struct tc_action_ops *act,
400 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
WANG Cong1f747c22013-12-15 20:15:10 -0800402 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 int err = -ENOENT;
404
405 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800406 list_for_each_entry(a, &act_base, head) {
407 if (a == act) {
408 list_del(&act->head);
409 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700414 if (!err)
415 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return err;
417}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800418EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420/* lookup by name */
421static struct tc_action_ops *tc_lookup_action_n(char *kind)
422{
Eric Dumazeta7928662013-12-20 12:32:32 -0800423 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 if (kind) {
426 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800427 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800429 if (try_module_get(a->owner))
430 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 break;
432 }
433 }
434 read_unlock(&act_mod_lock);
435 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800436 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800439/* lookup by nlattr */
440static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Eric Dumazeta7928662013-12-20 12:32:32 -0800442 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 if (kind) {
445 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800446 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800447 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800448 if (try_module_get(a->owner))
449 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 break;
451 }
452 }
453 read_unlock(&act_mod_lock);
454 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800455 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400458/*TCA_ACT_MAX_PRIO is 32, there count upto 32 */
459#define TCA_ACT_MAX_PRIO_MASK 0x1FF
WANG Cong22dc13c2016-08-13 22:35:00 -0700460int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
461 int nr_actions, struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
WANG Cong22dc13c2016-08-13 22:35:00 -0700463 int ret = -1, i;
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400464 u32 jmp_prgcnt = 0;
465 u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Willem de Bruijne7246e12017-01-07 17:06:35 -0500467 if (skb_skip_tc_classify(skb))
468 return TC_ACT_OK;
469
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400470restart_act_graph:
WANG Cong22dc13c2016-08-13 22:35:00 -0700471 for (i = 0; i < nr_actions; i++) {
472 const struct tc_action *a = actions[i];
473
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400474 if (jmp_prgcnt > 0) {
475 jmp_prgcnt -= 1;
476 continue;
477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500479 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500480 if (ret == TC_ACT_REPEAT)
481 goto repeat; /* we need a ttl - JHS */
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400482
Jiri Pirko9da32422017-05-02 10:12:00 +0200483 if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400484 jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
485 if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
486 /* faulty opcode, stop pipeline */
487 return TC_ACT_OK;
488 } else {
489 jmp_ttl -= 1;
490 if (jmp_ttl > 0)
491 goto restart_act_graph;
492 else /* faulty graph, stop pipeline */
493 return TC_ACT_OK;
494 }
Jiri Pirkodb505142017-05-17 11:08:03 +0200495 } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
496 tcf_action_goto_chain_exec(a, res);
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400497 }
498
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500499 if (ret != TC_ACT_PIPE)
Willem de Bruijne7246e12017-01-07 17:06:35 -0500500 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return ret;
504}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800505EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
WANG Cong55334a52014-02-11 17:07:34 -0800507int tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
WANG Cong33be6272013-12-15 20:15:05 -0800509 struct tc_action *a, *tmp;
WANG Cong55334a52014-02-11 17:07:34 -0800510 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
WANG Cong33be6272013-12-15 20:15:05 -0800512 list_for_each_entry_safe(a, tmp, actions, list) {
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200513 ret = __tcf_hash_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800514 if (ret == ACT_P_DELETED)
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500515 module_put(a->ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800516 else if (ret < 0)
517 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
WANG Cong55334a52014-02-11 17:07:34 -0800519 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522int
523tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
524{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return a->ops->dump(skb, a, bind, ref);
526}
527
528int
529tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
530{
531 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700532 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800533 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
David S. Miller1b34ec42012-03-29 05:11:39 -0400535 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
536 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800538 goto nla_put_failure;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500539 if (a->act_cookie) {
540 if (nla_put(skb, TCA_ACT_COOKIE, a->act_cookie->len,
541 a->act_cookie->data))
542 goto nla_put_failure;
543 }
544
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800545 nest = nla_nest_start(skb, TCA_OPTIONS);
546 if (nest == NULL)
547 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000548 err = tcf_action_dump_old(skb, a, bind, ref);
549 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800550 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return err;
552 }
553
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800554nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700555 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return -1;
557}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800558EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400560int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
561 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 struct tc_action *a;
564 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800565 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
WANG Cong33be6272013-12-15 20:15:05 -0800567 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800568 nest = nla_nest_start(skb, a->order);
569 if (nest == NULL)
570 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 err = tcf_action_dump_1(skb, a, bind, ref);
572 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700573 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800574 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
577 return 0;
578
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800579nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700580 err = -EINVAL;
581errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800582 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700583 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200586static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500587{
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200588 struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
589 if (!c)
590 return NULL;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500591
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200592 c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
593 if (!c->data) {
594 kfree(c);
595 return NULL;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500596 }
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200597 c->len = nla_len(tb[TCA_ACT_COOKIE]);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500598
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200599 return c;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500600}
601
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200602struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
603 struct nlattr *nla, struct nlattr *est,
604 char *name, int ovr, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 struct tc_action *a;
607 struct tc_action_ops *a_o;
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200608 struct tc_cookie *cookie = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000610 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800611 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800612 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (name == NULL) {
Johannes Bergfceb6432017-04-12 14:34:07 +0200615 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800616 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800618 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800619 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (kind == NULL)
621 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800622 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 goto err_out;
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200624 if (tb[TCA_ACT_COOKIE]) {
625 int cklen = nla_len(tb[TCA_ACT_COOKIE]);
626
627 if (cklen > TC_COOKIE_MAX_SIZE)
628 goto err_out;
629
630 cookie = nla_memdup_cookie(tb);
631 if (!cookie) {
632 err = -ENOMEM;
633 goto err_out;
634 }
635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800637 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
639 goto err_out;
640 }
641
642 a_o = tc_lookup_action_n(act_name);
643 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700644#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800646 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 rtnl_lock();
648
649 a_o = tc_lookup_action_n(act_name);
650
651 /* We dropped the RTNL semaphore in order to
652 * perform the module load. So, even if we
653 * succeeded in loading the module we have to
654 * tell the caller to replay the request. We
655 * indicate this using -EAGAIN.
656 */
657 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800658 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 goto err_mod;
660 }
661#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800662 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 goto err_out;
664 }
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 /* backward compatibility for policer */
667 if (name == NULL)
WANG Conga85a9702016-07-25 16:09:41 -0700668 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 else
WANG Conga85a9702016-07-25 16:09:41 -0700670 err = a_o->init(net, nla, est, &a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800671 if (err < 0)
WANG Conga85a9702016-07-25 16:09:41 -0700672 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200674 if (name == NULL && tb[TCA_ACT_COOKIE]) {
675 if (a->act_cookie) {
676 kfree(a->act_cookie->data);
677 kfree(a->act_cookie);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500678 }
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200679 a->act_cookie = cookie;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500680 }
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000683 * if it exists and is only bound to in a_o->init() then
684 * ACT_P_CREATED is not returned (a zero is).
685 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800686 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Jiri Pirkodb505142017-05-17 11:08:03 +0200689 if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) {
690 err = tcf_action_goto_chain_init(a, tp);
691 if (err) {
692 LIST_HEAD(actions);
693
694 list_add_tail(&a->list, &actions);
695 tcf_action_destroy(&actions, bind);
696 return ERR_PTR(err);
697 }
698 }
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return a;
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702err_mod:
703 module_put(a_o->owner);
704err_out:
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200705 if (cookie) {
706 kfree(cookie->data);
707 kfree(cookie);
708 }
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800709 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400712static void cleanup_a(struct list_head *actions, int ovr)
713{
714 struct tc_action *a;
715
716 if (!ovr)
717 return;
718
719 list_for_each_entry(a, actions, list)
720 a->tcfa_refcnt--;
721}
722
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200723int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
724 struct nlattr *est, char *name, int ovr, int bind,
725 struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000727 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800728 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800729 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 int i;
731
Johannes Bergfceb6432017-04-12 14:34:07 +0200732 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800733 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800734 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800736 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200737 act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800738 if (IS_ERR(act)) {
739 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800741 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800742 act->order = i;
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400743 if (ovr)
744 act->tcfa_refcnt++;
WANG Cong33be6272013-12-15 20:15:05 -0800745 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400747
748 /* Remove the temp refcnt which was necessary to protect against
749 * destroying an existing action which was being replaced
750 */
751 cleanup_a(actions, ovr);
WANG Cong33be6272013-12-15 20:15:05 -0800752 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754err:
WANG Cong33be6272013-12-15 20:15:05 -0800755 tcf_action_destroy(actions, bind);
756 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
WANG Congec0595c2016-07-25 16:09:42 -0700759int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 int compat_mode)
761{
762 int err = 0;
763 struct gnet_dump d;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900764
WANG Cong7eb88962014-01-09 16:14:05 -0800765 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 goto errout;
767
768 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400769 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 */
771 if (compat_mode) {
WANG Congec0595c2016-07-25 16:09:42 -0700772 if (p->type == TCA_OLD_COMPAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 err = gnet_stats_start_copy_compat(skb, 0,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200774 TCA_STATS,
775 TCA_XSTATS,
WANG Congec0595c2016-07-25 16:09:42 -0700776 &p->tcfa_lock, &d,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200777 TCA_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 else
779 return 0;
780 } else
781 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Congec0595c2016-07-25 16:09:42 -0700782 &p->tcfa_lock, &d, TCA_ACT_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 if (err < 0)
785 goto errout;
786
WANG Congec0595c2016-07-25 16:09:42 -0700787 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800788 gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -0700789 gnet_stats_copy_queue(&d, p->cpu_qstats,
WANG Congec0595c2016-07-25 16:09:42 -0700790 &p->tcfa_qstats,
791 p->tcfa_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 goto errout;
793
794 if (gnet_stats_finish_copy(&d) < 0)
795 goto errout;
796
797 return 0;
798
799errout:
800 return -1;
801}
802
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400803static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
804 u32 portid, u32 seq, u16 flags, int event, int bind,
805 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 struct tcamsg *t;
808 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700809 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800810 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Eric W. Biederman15e47302012-09-07 20:12:54 +0000812 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700813 if (!nlh)
814 goto out_nlmsg_trim;
815 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700817 t->tca__pad1 = 0;
818 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900819
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800820 nest = nla_nest_start(skb, TCA_ACT_TAB);
821 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700822 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
WANG Cong33be6272013-12-15 20:15:05 -0800824 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700825 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800827 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900828
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700829 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return skb->len;
831
David S. Miller8b00a532012-06-26 21:39:32 -0700832out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700833 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return -1;
835}
836
837static int
Roman Mashakc4c42902017-07-13 13:12:18 -0400838tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800839 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
844 if (!skb)
845 return -ENOBUFS;
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400846 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
847 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 kfree_skb(skb);
849 return -EINVAL;
850 }
Thomas Graf2942e902006-08-15 00:30:25 -0700851
Eric W. Biederman15e47302012-09-07 20:12:54 +0000852 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
WANG Congddf97cc2016-02-22 15:57:53 -0800855static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
856 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000858 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700859 const struct tc_action_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 struct tc_action *a;
861 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800862 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Johannes Bergfceb6432017-04-12 14:34:07 +0200864 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800865 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800866 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Patrick McHardycee63722008-01-23 20:33:32 -0800868 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800869 if (tb[TCA_ACT_INDEX] == NULL ||
870 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800871 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800872 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800874 err = -EINVAL;
WANG Conga85a9702016-07-25 16:09:41 -0700875 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
876 if (!ops) /* could happen in batch of actions */
877 goto err_out;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800878 err = -ENOENT;
WANG Conga85a9702016-07-25 16:09:41 -0700879 if (ops->lookup(net, &a, index) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 goto err_mod;
881
WANG Conga85a9702016-07-25 16:09:41 -0700882 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885err_mod:
WANG Conga85a9702016-07-25 16:09:41 -0700886 module_put(ops->owner);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800887err_out:
888 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
Tom Goff7316ae82010-03-19 15:40:13 +0000891static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000892 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 struct sk_buff *skb;
895 unsigned char *b;
896 struct nlmsghdr *nlh;
897 struct tcamsg *t;
898 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800899 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000900 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700901 const struct tc_action_ops *ops;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800902 struct nlattr *kind;
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700903 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
906 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000907 pr_debug("tca_action_flush: failed skb alloc\n");
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700908 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700911 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Johannes Bergfceb6432017-04-12 14:34:07 +0200913 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800914 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 goto err_out;
916
Patrick McHardycee63722008-01-23 20:33:32 -0800917 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800918 kind = tb[TCA_ACT_KIND];
WANG Conga85a9702016-07-25 16:09:41 -0700919 ops = tc_lookup_action(kind);
920 if (!ops) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 goto err_out;
922
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400923 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
924 sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700925 if (!nlh)
926 goto out_module_put;
927 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700929 t->tca__pad1 = 0;
930 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800932 nest = nla_nest_start(skb, TCA_ACT_TAB);
933 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700934 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
WANG Conga85a9702016-07-25 16:09:41 -0700936 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops);
Roman Mashakedb9d1b2017-02-24 11:00:32 -0500937 if (err <= 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700938 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800940 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700942 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Conga85a9702016-07-25 16:09:41 -0700944 module_put(ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000945 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000946 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (err > 0)
948 return 0;
949
950 return err;
951
David S. Miller8b00a532012-06-26 21:39:32 -0700952out_module_put:
WANG Conga85a9702016-07-25 16:09:41 -0700953 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954err_out:
955 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return err;
957}
958
959static int
WANG Conga56e1952014-01-09 16:14:00 -0800960tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
961 u32 portid)
962{
963 int ret;
964 struct sk_buff *skb;
965
966 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
967 if (!skb)
968 return -ENOBUFS;
969
970 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
971 0, 1) <= 0) {
972 kfree_skb(skb);
973 return -EINVAL;
974 }
975
976 /* now do the delete */
WANG Cong55334a52014-02-11 17:07:34 -0800977 ret = tcf_action_destroy(actions, 0);
978 if (ret < 0) {
979 kfree_skb(skb);
980 return ret;
981 }
WANG Conga56e1952014-01-09 16:14:00 -0800982
983 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
984 n->nlmsg_flags & NLM_F_ECHO);
985 if (ret > 0)
986 return 0;
987 return ret;
988}
989
990static int
Tom Goff7316ae82010-03-19 15:40:13 +0000991tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000992 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Patrick McHardycee63722008-01-23 20:33:32 -0800994 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000995 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800996 struct tc_action *act;
997 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Johannes Bergfceb6432017-04-12 14:34:07 +0200999 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -08001000 if (ret < 0)
1001 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001003 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -07001004 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +00001005 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -07001006 else
1007 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001010 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
WANG Congddf97cc2016-02-22 15:57:53 -08001011 act = tcf_action_get_1(net, tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001012 if (IS_ERR(act)) {
1013 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001015 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001016 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -08001017 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
1019
1020 if (event == RTM_GETACTION)
Roman Mashakc4c42902017-07-13 13:12:18 -04001021 ret = tcf_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -08001023 ret = tcf_del_notify(net, n, &actions, portid);
1024 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return ret;
1027 }
1028err:
Jamal Hadi Salim0faa9cb2017-01-15 10:14:06 -05001029 if (event != RTM_GETACTION)
1030 tcf_action_destroy(&actions, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return ret;
1032}
1033
WANG Conga56e1952014-01-09 16:14:00 -08001034static int
1035tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
1036 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 int err = 0;
1040
1041 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1042 if (!skb)
1043 return -ENOBUFS;
1044
WANG Conga56e1952014-01-09 16:14:00 -08001045 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
1046 RTM_NEWACTION, 0, 0) <= 0) {
1047 kfree_skb(skb);
1048 return -EINVAL;
1049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
WANG Conga56e1952014-01-09 16:14:00 -08001051 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1052 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (err > 0)
1054 err = 0;
1055 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001058static int tcf_action_add(struct net *net, struct nlattr *nla,
1059 struct nlmsghdr *n, u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -08001062 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001064 ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0, &actions);
WANG Cong33be6272013-12-15 20:15:05 -08001065 if (ret)
WANG Congf07fed82016-08-13 22:34:56 -07001066 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
WANG Congf07fed82016-08-13 22:34:56 -07001068 return tcf_add_notify(net, n, &actions, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
David Ahernc21ef3e2017-04-16 09:48:24 -07001071static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
1072 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001074 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001075 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +00001076 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 int ret = 0, ovr = 0;
1078
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001079 if ((n->nlmsg_type != RTM_GETACTION) &&
1080 !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001081 return -EPERM;
1082
Johannes Bergfceb6432017-04-12 14:34:07 +02001083 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL,
David Ahernc21ef3e2017-04-16 09:48:24 -07001084 extack);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001085 if (ret < 0)
1086 return ret;
1087
1088 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001089 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return -EINVAL;
1091 }
1092
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001093 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 switch (n->nlmsg_type) {
1095 case RTM_NEWACTION:
1096 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001097 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 * Note that CREATE | EXCL implies that
1099 * but since we want avoid ambiguity (eg when flags
1100 * is zero) then just set this
1101 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001102 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 ovr = 1;
1104replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +00001105 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (ret == -EAGAIN)
1107 goto replay;
1108 break;
1109 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001110 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001111 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 break;
1113 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001114 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001115 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 break;
1117 default:
1118 BUG();
1119 }
1120
1121 return ret;
1122}
1123
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001124static struct nlattr *find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001126 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001127 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1128 struct nlattr *nla[TCAA_MAX + 1];
1129 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Johannes Bergfceb6432017-04-12 14:34:07 +02001131 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX,
1132 NULL, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001134 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (tb1 == NULL)
1136 return NULL;
1137
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001138 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
Johannes Bergfceb6432017-04-12 14:34:07 +02001139 NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Patrick McHardy6d834e02008-01-23 20:32:42 -08001142 if (tb[1] == NULL)
1143 return NULL;
Johannes Bergfceb6432017-04-12 14:34:07 +02001144 if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001146 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Thomas Graf26dab892006-07-05 20:45:06 -07001148 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001151static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
WANG Congddf97cc2016-02-22 15:57:53 -08001153 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001155 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001156 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 struct tc_action_ops *a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001159 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001160 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001163 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return 0;
1165 }
1166
Thomas Graf26dab892006-07-05 20:45:06 -07001167 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001168 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Eric W. Biederman15e47302012-09-07 20:12:54 +00001171 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001172 cb->nlh->nlmsg_type, sizeof(*t), 0);
1173 if (!nlh)
1174 goto out_module_put;
1175 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001177 t->tca__pad1 = 0;
1178 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001180 nest = nla_nest_start(skb, TCA_ACT_TAB);
1181 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001182 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
WANG Conga85a9702016-07-25 16:09:41 -07001184 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001186 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001189 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 ret = skb->len;
1191 } else
Jamal Hadi Salimebecaa62016-06-13 18:08:42 -04001192 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001194 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001195 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 nlh->nlmsg_flags |= NLM_F_MULTI;
1197 module_put(a_o->owner);
1198 return skb->len;
1199
David S. Miller8b00a532012-06-26 21:39:32 -07001200out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001202 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return skb->len;
1204}
1205
1206static int __init tc_action_init(void)
1207{
Greg Rosec7ac8672011-06-10 01:27:09 +00001208 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1209 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1210 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1211 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return 0;
1214}
1215
1216subsys_initcall(tc_action_init);