blob: 92c00207d5a122f6184e833f4053856f1f24484c [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * net/sched/act_api.c Packet action API.
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author: Jamal Hadi Salim
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/types.h>
9#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/init.h>
15#include <linux/kmod.h>
Patrick McHardyab27cfb2008-01-23 20:33:13 -080016#include <linux/err.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040017#include <linux/module.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110018#include <net/net_namespace.h>
19#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <net/sch_generic.h>
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -050021#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <net/act_api.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070023#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Jiri Pirkodb505142017-05-17 11:08:03 +020025static void tcf_action_goto_chain_exec(const struct tc_action *a,
26 struct tcf_result *res)
27{
Davide Carattiee3bbfe2019-03-20 15:00:16 +010028 const struct tcf_chain *chain = rcu_dereference_bh(a->goto_chain);
Jiri Pirkodb505142017-05-17 11:08:03 +020029
30 res->goto_tp = rcu_dereference_bh(chain->filter_chain);
31}
32
Vlad Busloveec94fd2018-07-05 17:24:23 +030033static void tcf_free_cookie_rcu(struct rcu_head *p)
34{
35 struct tc_cookie *cookie = container_of(p, struct tc_cookie, rcu);
36
37 kfree(cookie->data);
38 kfree(cookie);
39}
40
41static void tcf_set_action_cookie(struct tc_cookie __rcu **old_cookie,
42 struct tc_cookie *new_cookie)
43{
44 struct tc_cookie *old;
45
David S. Miller0dbc81e2018-07-08 17:02:59 +090046 old = xchg((__force struct tc_cookie **)old_cookie, new_cookie);
Vlad Busloveec94fd2018-07-05 17:24:23 +030047 if (old)
48 call_rcu(&old->rcu, tcf_free_cookie_rcu);
49}
50
Davide Caratti85d09662019-03-20 14:59:59 +010051int tcf_action_check_ctrlact(int action, struct tcf_proto *tp,
52 struct tcf_chain **newchain,
53 struct netlink_ext_ack *extack)
54{
55 int opcode = TC_ACT_EXT_OPCODE(action), ret = -EINVAL;
56 u32 chain_index;
57
58 if (!opcode)
59 ret = action > TC_ACT_VALUE_MAX ? -EINVAL : 0;
60 else if (opcode <= TC_ACT_EXT_OPCODE_MAX || action == TC_ACT_UNSPEC)
61 ret = 0;
62 if (ret) {
63 NL_SET_ERR_MSG(extack, "invalid control action");
64 goto end;
65 }
66
67 if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN)) {
68 chain_index = action & TC_ACT_EXT_VAL_MASK;
69 if (!tp || !newchain) {
70 ret = -EINVAL;
71 NL_SET_ERR_MSG(extack,
72 "can't goto NULL proto/chain");
73 goto end;
74 }
75 *newchain = tcf_chain_get_by_act(tp->chain->block, chain_index);
76 if (!*newchain) {
77 ret = -ENOMEM;
78 NL_SET_ERR_MSG(extack,
79 "can't allocate goto_chain");
80 }
81 }
82end:
83 return ret;
84}
85EXPORT_SYMBOL(tcf_action_check_ctrlact);
86
87struct tcf_chain *tcf_action_set_ctrlact(struct tc_action *a, int action,
Davide Carattiee3bbfe2019-03-20 15:00:16 +010088 struct tcf_chain *goto_chain)
Davide Caratti85d09662019-03-20 14:59:59 +010089{
Davide Caratti85d09662019-03-20 14:59:59 +010090 a->tcfa_action = action;
Davide Carattiee3bbfe2019-03-20 15:00:16 +010091 rcu_swap_protected(a->goto_chain, goto_chain, 1);
92 return goto_chain;
Davide Caratti85d09662019-03-20 14:59:59 +010093}
94EXPORT_SYMBOL(tcf_action_set_ctrlact);
95
Cong Wangd7fb60b2017-09-11 16:33:30 -070096/* XXX: For standalone actions, we don't need a RCU grace period either, because
97 * actions are always connected to filters and filters are already destroyed in
98 * RCU callbacks, so after a RCU grace period actions are already disconnected
99 * from filters. Readers later can not find us.
100 */
101static void free_tcf(struct tc_action *p)
Eric Dumazet519c8182015-07-06 05:18:04 -0700102{
Davide Carattiee3bbfe2019-03-20 15:00:16 +0100103 struct tcf_chain *chain = rcu_dereference_protected(p->goto_chain, 1);
Davide Caratti85d09662019-03-20 14:59:59 +0100104
Eric Dumazet519c8182015-07-06 05:18:04 -0700105 free_percpu(p->cpu_bstats);
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400106 free_percpu(p->cpu_bstats_hw);
Eric Dumazet519c8182015-07-06 05:18:04 -0700107 free_percpu(p->cpu_qstats);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500108
Vlad Busloveec94fd2018-07-05 17:24:23 +0300109 tcf_set_action_cookie(&p->act_cookie, NULL);
Davide Caratti85d09662019-03-20 14:59:59 +0100110 if (chain)
111 tcf_chain_put_by_act(chain);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500112
Eric Dumazet519c8182015-07-06 05:18:04 -0700113 kfree(p);
114}
115
Vlad Buslov16af6062018-07-05 17:24:29 +0300116static void tcf_action_cleanup(struct tc_action *p)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700117{
Vlad Buslov16af6062018-07-05 17:24:29 +0300118 if (p->ops->cleanup)
119 p->ops->cleanup(p);
120
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800121 gen_kill_estimator(&p->tcfa_rate_est);
Cong Wangd7fb60b2017-09-11 16:33:30 -0700122 free_tcf(p);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700123}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700124
Vlad Buslov16af6062018-07-05 17:24:29 +0300125static int __tcf_action_put(struct tc_action *p, bool bind)
126{
127 struct tcf_idrinfo *idrinfo = p->idrinfo;
128
Cong Wang95278dd2018-10-02 12:50:19 -0700129 if (refcount_dec_and_mutex_lock(&p->tcfa_refcnt, &idrinfo->lock)) {
Vlad Buslov16af6062018-07-05 17:24:29 +0300130 if (bind)
131 atomic_dec(&p->tcfa_bindcnt);
132 idr_remove(&idrinfo->action_idr, p->tcfa_index);
Cong Wang95278dd2018-10-02 12:50:19 -0700133 mutex_unlock(&idrinfo->lock);
Vlad Buslov16af6062018-07-05 17:24:29 +0300134
135 tcf_action_cleanup(p);
136 return 1;
137 }
138
139 if (bind)
140 atomic_dec(&p->tcfa_bindcnt);
141
142 return 0;
143}
144
Chris Mi65a206c2017-08-30 02:31:59 -0400145int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700146{
147 int ret = 0;
148
Vlad Buslov036bb442018-07-05 17:24:24 +0300149 /* Release with strict==1 and bind==0 is only called through act API
150 * interface (classifiers always bind). Only case when action with
151 * positive reference count and zero bind count can exist is when it was
152 * also created with act API (unbinding last classifier will destroy the
153 * action if it was created by classifier). So only case when bind count
154 * can be changed after initial check is when unbound action is
155 * destroyed by act API while classifier binds to action with same id
156 * concurrently. This result either creation of new action(same behavior
157 * as before), or reusing existing action if concurrent process
158 * increments reference count before action is deleted. Both scenarios
159 * are acceptable.
160 */
David S. Millere9ce1cd2006-08-21 23:54:55 -0700161 if (p) {
Vlad Buslov16af6062018-07-05 17:24:29 +0300162 if (!bind && strict && atomic_read(&p->tcfa_bindcnt) > 0)
WANG Cong55334a52014-02-11 17:07:34 -0800163 return -EPERM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700164
Vlad Buslov16af6062018-07-05 17:24:29 +0300165 if (__tcf_action_put(p, bind))
WANG Cong1d4150c2016-02-22 15:57:52 -0800166 ret = ACT_P_DELETED;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700167 }
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200168
David S. Millere9ce1cd2006-08-21 23:54:55 -0700169 return ret;
170}
Chris Mi65a206c2017-08-30 02:31:59 -0400171EXPORT_SYMBOL(__tcf_idr_release);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700172
Roman Mashak4e76e752018-03-08 16:59:19 -0500173static size_t tcf_action_shared_attrs_size(const struct tc_action *act)
174{
Vlad Buslove0479b62018-07-09 20:26:47 +0300175 struct tc_cookie *act_cookie;
Roman Mashak4e76e752018-03-08 16:59:19 -0500176 u32 cookie_len = 0;
177
Vlad Buslove0479b62018-07-09 20:26:47 +0300178 rcu_read_lock();
179 act_cookie = rcu_dereference(act->act_cookie);
180
181 if (act_cookie)
182 cookie_len = nla_total_size(act_cookie->len);
183 rcu_read_unlock();
Roman Mashak4e76e752018-03-08 16:59:19 -0500184
185 return nla_total_size(0) /* action number nested */
186 + nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */
187 + cookie_len /* TCA_ACT_COOKIE */
188 + nla_total_size(0) /* TCA_ACT_STATS nested */
189 /* TCA_STATS_BASIC */
190 + nla_total_size_64bit(sizeof(struct gnet_stats_basic))
191 /* TCA_STATS_QUEUE */
192 + nla_total_size_64bit(sizeof(struct gnet_stats_queue))
193 + nla_total_size(0) /* TCA_OPTIONS nested */
194 + nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */
195}
196
197static size_t tcf_action_full_attrs_size(size_t sz)
198{
199 return NLMSG_HDRLEN /* struct nlmsghdr */
200 + sizeof(struct tcamsg)
201 + nla_total_size(0) /* TCA_ACT_TAB nested */
202 + sz;
203}
204
205static size_t tcf_action_fill_size(const struct tc_action *act)
206{
207 size_t sz = tcf_action_shared_attrs_size(act);
208
209 if (act->ops->get_fill_size)
210 return act->ops->get_fill_size(act) + sz;
211 return sz;
212}
213
Chris Mi65a206c2017-08-30 02:31:59 -0400214static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700215 struct netlink_callback *cb)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700216{
Chris Mi65a206c2017-08-30 02:31:59 -0400217 int err = 0, index = -1, s_i = 0, n_i = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400218 u32 act_flags = cb->args[2];
Jamal Hadi Salime62e4842017-07-30 13:24:52 -0400219 unsigned long jiffy_since = cb->args[3];
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800220 struct nlattr *nest;
Chris Mi65a206c2017-08-30 02:31:59 -0400221 struct idr *idr = &idrinfo->action_idr;
222 struct tc_action *p;
223 unsigned long id = 1;
Cong Wange33d2b72019-06-28 11:03:41 -0700224 unsigned long tmp;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700225
Cong Wang95278dd2018-10-02 12:50:19 -0700226 mutex_lock(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700227
228 s_i = cb->args[0];
229
Cong Wange33d2b72019-06-28 11:03:41 -0700230 idr_for_each_entry_ul(idr, p, tmp, id) {
Chris Mi65a206c2017-08-30 02:31:59 -0400231 index++;
232 if (index < s_i)
233 continue;
WANG Conga85a9702016-07-25 16:09:41 -0700234
Chris Mi65a206c2017-08-30 02:31:59 -0400235 if (jiffy_since &&
236 time_after(jiffy_since,
237 (unsigned long)p->tcfa_tm.lastuse))
238 continue;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700239
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200240 nest = nla_nest_start_noflag(skb, n_i);
Craig Dillabaugh734549e2018-03-26 14:58:32 -0400241 if (!nest) {
242 index--;
Chris Mi65a206c2017-08-30 02:31:59 -0400243 goto nla_put_failure;
Craig Dillabaugh734549e2018-03-26 14:58:32 -0400244 }
Chris Mi65a206c2017-08-30 02:31:59 -0400245 err = tcf_action_dump_1(skb, p, 0, 0);
246 if (err < 0) {
247 index--;
248 nlmsg_trim(skb, nest);
249 goto done;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700250 }
Chris Mi65a206c2017-08-30 02:31:59 -0400251 nla_nest_end(skb, nest);
252 n_i++;
253 if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) &&
254 n_i >= TCA_ACT_MAX_PRIO)
255 goto done;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700256 }
257done:
Jamal Hadi Salime62e4842017-07-30 13:24:52 -0400258 if (index >= 0)
259 cb->args[0] = index + 1;
260
Cong Wang95278dd2018-10-02 12:50:19 -0700261 mutex_unlock(&idrinfo->lock);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400262 if (n_i) {
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400263 if (act_flags & TCA_FLAG_LARGE_DUMP_ON)
264 cb->args[1] = n_i;
265 }
David S. Millere9ce1cd2006-08-21 23:54:55 -0700266 return n_i;
267
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800268nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800269 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700270 goto done;
271}
272
Vlad Buslovec3ed292018-09-19 16:37:29 -0700273static int tcf_idr_release_unsafe(struct tc_action *p)
274{
275 if (atomic_read(&p->tcfa_bindcnt) > 0)
276 return -EPERM;
277
278 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
279 idr_remove(&p->idrinfo->action_idr, p->tcfa_index);
280 tcf_action_cleanup(p);
281 return ACT_P_DELETED;
282 }
283
284 return 0;
285}
286
Chris Mi65a206c2017-08-30 02:31:59 -0400287static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700288 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700289{
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800290 struct nlattr *nest;
Chris Mi65a206c2017-08-30 02:31:59 -0400291 int n_i = 0;
WANG Cong55334a52014-02-11 17:07:34 -0800292 int ret = -EINVAL;
Chris Mi65a206c2017-08-30 02:31:59 -0400293 struct idr *idr = &idrinfo->action_idr;
294 struct tc_action *p;
295 unsigned long id = 1;
Cong Wange33d2b72019-06-28 11:03:41 -0700296 unsigned long tmp;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700297
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200298 nest = nla_nest_start_noflag(skb, 0);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800299 if (nest == NULL)
300 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700301 if (nla_put_string(skb, TCA_KIND, ops->kind))
David S. Miller1b34ec42012-03-29 05:11:39 -0400302 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700303
Cong Wang95278dd2018-10-02 12:50:19 -0700304 mutex_lock(&idrinfo->lock);
Cong Wange33d2b72019-06-28 11:03:41 -0700305 idr_for_each_entry_ul(idr, p, tmp, id) {
Vlad Buslovec3ed292018-09-19 16:37:29 -0700306 ret = tcf_idr_release_unsafe(p);
Chris Mi65a206c2017-08-30 02:31:59 -0400307 if (ret == ACT_P_DELETED) {
Jiri Pirko255cd502017-09-13 17:32:37 +0200308 module_put(ops->owner);
Chris Mi65a206c2017-08-30 02:31:59 -0400309 n_i++;
310 } else if (ret < 0) {
Cong Wang95278dd2018-10-02 12:50:19 -0700311 mutex_unlock(&idrinfo->lock);
Chris Mi65a206c2017-08-30 02:31:59 -0400312 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700313 }
314 }
Cong Wang95278dd2018-10-02 12:50:19 -0700315 mutex_unlock(&idrinfo->lock);
Vlad Buslovec3ed292018-09-19 16:37:29 -0700316
David S. Miller1b34ec42012-03-29 05:11:39 -0400317 if (nla_put_u32(skb, TCA_FCNT, n_i))
318 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800319 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700320
321 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800322nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800323 nla_nest_cancel(skb, nest);
WANG Cong55334a52014-02-11 17:07:34 -0800324 return ret;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700325}
326
WANG Congddf97cc2016-02-22 15:57:53 -0800327int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
328 struct netlink_callback *cb, int type,
Alexander Aringb3620142018-02-15 10:54:59 -0500329 const struct tc_action_ops *ops,
330 struct netlink_ext_ack *extack)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700331{
Chris Mi65a206c2017-08-30 02:31:59 -0400332 struct tcf_idrinfo *idrinfo = tn->idrinfo;
WANG Congddf97cc2016-02-22 15:57:53 -0800333
David S. Millere9ce1cd2006-08-21 23:54:55 -0700334 if (type == RTM_DELACTION) {
Chris Mi65a206c2017-08-30 02:31:59 -0400335 return tcf_del_walker(idrinfo, skb, ops);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700336 } else if (type == RTM_GETACTION) {
Chris Mi65a206c2017-08-30 02:31:59 -0400337 return tcf_dump_walker(idrinfo, skb, cb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700338 } else {
Alexander Aringb3620142018-02-15 10:54:59 -0500339 WARN(1, "tcf_generic_walker: unknown command %d\n", type);
340 NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command");
David S. Millere9ce1cd2006-08-21 23:54:55 -0700341 return -EINVAL;
342 }
343}
WANG Congddf97cc2016-02-22 15:57:53 -0800344EXPORT_SYMBOL(tcf_generic_walker);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700345
Cong Wang7d485c42018-08-19 12:22:08 -0700346int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700347{
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300348 struct tcf_idrinfo *idrinfo = tn->idrinfo;
349 struct tc_action *p;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700350
Cong Wang95278dd2018-10-02 12:50:19 -0700351 mutex_lock(&idrinfo->lock);
Matthew Wilcox322d8842017-11-28 10:01:24 -0500352 p = idr_find(&idrinfo->action_idr, index);
Cong Wang7d485c42018-08-19 12:22:08 -0700353 if (IS_ERR(p))
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300354 p = NULL;
Cong Wang7d485c42018-08-19 12:22:08 -0700355 else if (p)
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300356 refcount_inc(&p->tcfa_refcnt);
Cong Wang95278dd2018-10-02 12:50:19 -0700357 mutex_unlock(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700358
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300359 if (p) {
360 *a = p;
361 return true;
362 }
363 return false;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700364}
Chris Mi65a206c2017-08-30 02:31:59 -0400365EXPORT_SYMBOL(tcf_idr_search);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700366
Cong Wang97a3f84f2018-08-19 12:22:06 -0700367static int tcf_idr_delete_index(struct tcf_idrinfo *idrinfo, u32 index)
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300368{
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300369 struct tc_action *p;
370 int ret = 0;
371
Cong Wang95278dd2018-10-02 12:50:19 -0700372 mutex_lock(&idrinfo->lock);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300373 p = idr_find(&idrinfo->action_idr, index);
374 if (!p) {
Cong Wang95278dd2018-10-02 12:50:19 -0700375 mutex_unlock(&idrinfo->lock);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300376 return -ENOENT;
377 }
378
379 if (!atomic_read(&p->tcfa_bindcnt)) {
380 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
381 struct module *owner = p->ops->owner;
382
383 WARN_ON(p != idr_remove(&idrinfo->action_idr,
384 p->tcfa_index));
Cong Wang95278dd2018-10-02 12:50:19 -0700385 mutex_unlock(&idrinfo->lock);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300386
Vlad Buslov16af6062018-07-05 17:24:29 +0300387 tcf_action_cleanup(p);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300388 module_put(owner);
389 return 0;
390 }
391 ret = 0;
392 } else {
393 ret = -EPERM;
394 }
395
Cong Wang95278dd2018-10-02 12:50:19 -0700396 mutex_unlock(&idrinfo->lock);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300397 return ret;
398}
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300399
Chris Mi65a206c2017-08-30 02:31:59 -0400400int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
401 struct tc_action **a, const struct tc_action_ops *ops,
402 int bind, bool cpustats)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700403{
WANG Congec0595c2016-07-25 16:09:42 -0700404 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
Chris Mi65a206c2017-08-30 02:31:59 -0400405 struct tcf_idrinfo *idrinfo = tn->idrinfo;
Eric Dumazet519c8182015-07-06 05:18:04 -0700406 int err = -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700407
408 if (unlikely(!p))
WANG Cong86062032014-02-11 17:07:31 -0800409 return -ENOMEM;
Vlad Buslov036bb442018-07-05 17:24:24 +0300410 refcount_set(&p->tcfa_refcnt, 1);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700411 if (bind)
Vlad Buslov036bb442018-07-05 17:24:24 +0300412 atomic_set(&p->tcfa_bindcnt, 1);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700413
Eric Dumazet519c8182015-07-06 05:18:04 -0700414 if (cpustats) {
415 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500416 if (!p->cpu_bstats)
Eric Dumazet519c8182015-07-06 05:18:04 -0700417 goto err1;
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400418 p->cpu_bstats_hw = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
419 if (!p->cpu_bstats_hw)
420 goto err2;
Matthew Wilcox339913a2017-11-28 10:28:15 -0500421 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
422 if (!p->cpu_qstats)
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400423 goto err3;
Eric Dumazet519c8182015-07-06 05:18:04 -0700424 }
WANG Congec0595c2016-07-25 16:09:42 -0700425 spin_lock_init(&p->tcfa_lock);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500426 p->tcfa_index = index;
WANG Congec0595c2016-07-25 16:09:42 -0700427 p->tcfa_tm.install = jiffies;
428 p->tcfa_tm.lastuse = jiffies;
429 p->tcfa_tm.firstuse = 0;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800430 if (est) {
WANG Congec0595c2016-07-25 16:09:42 -0700431 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
432 &p->tcfa_rate_est,
433 &p->tcfa_lock, NULL, est);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500434 if (err)
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400435 goto err4;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800436 }
437
Chris Mi65a206c2017-08-30 02:31:59 -0400438 p->idrinfo = idrinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700439 p->ops = ops;
WANG Congec0595c2016-07-25 16:09:42 -0700440 *a = p;
WANG Cong86062032014-02-11 17:07:31 -0800441 return 0;
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400442err4:
Matthew Wilcox339913a2017-11-28 10:28:15 -0500443 free_percpu(p->cpu_qstats);
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400444err3:
445 free_percpu(p->cpu_bstats_hw);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500446err2:
447 free_percpu(p->cpu_bstats);
448err1:
449 kfree(p);
450 return err;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700451}
Chris Mi65a206c2017-08-30 02:31:59 -0400452EXPORT_SYMBOL(tcf_idr_create);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700453
Chris Mi65a206c2017-08-30 02:31:59 -0400454void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700455{
Chris Mi65a206c2017-08-30 02:31:59 -0400456 struct tcf_idrinfo *idrinfo = tn->idrinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700457
Cong Wang95278dd2018-10-02 12:50:19 -0700458 mutex_lock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300459 /* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
460 WARN_ON(!IS_ERR(idr_replace(&idrinfo->action_idr, a, a->tcfa_index)));
Cong Wang95278dd2018-10-02 12:50:19 -0700461 mutex_unlock(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700462}
Chris Mi65a206c2017-08-30 02:31:59 -0400463EXPORT_SYMBOL(tcf_idr_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300465/* Cleanup idr index that was allocated but not initialized. */
466
467void tcf_idr_cleanup(struct tc_action_net *tn, u32 index)
468{
469 struct tcf_idrinfo *idrinfo = tn->idrinfo;
470
Cong Wang95278dd2018-10-02 12:50:19 -0700471 mutex_lock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300472 /* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
473 WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index)));
Cong Wang95278dd2018-10-02 12:50:19 -0700474 mutex_unlock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300475}
476EXPORT_SYMBOL(tcf_idr_cleanup);
477
478/* Check if action with specified index exists. If actions is found, increments
479 * its reference and bind counters, and return 1. Otherwise insert temporary
480 * error pointer (to prevent concurrent users from inserting actions with same
481 * index) and return 0.
482 */
483
484int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
485 struct tc_action **a, int bind)
486{
487 struct tcf_idrinfo *idrinfo = tn->idrinfo;
488 struct tc_action *p;
489 int ret;
490
491again:
Cong Wang95278dd2018-10-02 12:50:19 -0700492 mutex_lock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300493 if (*index) {
494 p = idr_find(&idrinfo->action_idr, *index);
495 if (IS_ERR(p)) {
496 /* This means that another process allocated
497 * index but did not assign the pointer yet.
498 */
Cong Wang95278dd2018-10-02 12:50:19 -0700499 mutex_unlock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300500 goto again;
501 }
502
503 if (p) {
504 refcount_inc(&p->tcfa_refcnt);
505 if (bind)
506 atomic_inc(&p->tcfa_bindcnt);
507 *a = p;
508 ret = 1;
509 } else {
510 *a = NULL;
511 ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
Cong Wang95278dd2018-10-02 12:50:19 -0700512 *index, GFP_KERNEL);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300513 if (!ret)
514 idr_replace(&idrinfo->action_idr,
515 ERR_PTR(-EBUSY), *index);
516 }
517 } else {
518 *index = 1;
519 *a = NULL;
520 ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
Cong Wang95278dd2018-10-02 12:50:19 -0700521 UINT_MAX, GFP_KERNEL);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300522 if (!ret)
523 idr_replace(&idrinfo->action_idr, ERR_PTR(-EBUSY),
524 *index);
525 }
Cong Wang95278dd2018-10-02 12:50:19 -0700526 mutex_unlock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300527 return ret;
528}
529EXPORT_SYMBOL(tcf_idr_check_alloc);
530
Chris Mi65a206c2017-08-30 02:31:59 -0400531void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
532 struct tcf_idrinfo *idrinfo)
WANG Cong1d4150c2016-02-22 15:57:52 -0800533{
Chris Mi65a206c2017-08-30 02:31:59 -0400534 struct idr *idr = &idrinfo->action_idr;
535 struct tc_action *p;
536 int ret;
537 unsigned long id = 1;
Cong Wange33d2b72019-06-28 11:03:41 -0700538 unsigned long tmp;
WANG Cong1d4150c2016-02-22 15:57:52 -0800539
Cong Wange33d2b72019-06-28 11:03:41 -0700540 idr_for_each_entry_ul(idr, p, tmp, id) {
Chris Mi65a206c2017-08-30 02:31:59 -0400541 ret = __tcf_idr_release(p, false, true);
542 if (ret == ACT_P_DELETED)
543 module_put(ops->owner);
544 else if (ret < 0)
545 return;
WANG Cong1d4150c2016-02-22 15:57:52 -0800546 }
Chris Mi65a206c2017-08-30 02:31:59 -0400547 idr_destroy(&idrinfo->action_idr);
WANG Cong1d4150c2016-02-22 15:57:52 -0800548}
Chris Mi65a206c2017-08-30 02:31:59 -0400549EXPORT_SYMBOL(tcf_idrinfo_destroy);
WANG Cong1d4150c2016-02-22 15:57:52 -0800550
WANG Cong1f747c22013-12-15 20:15:10 -0800551static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552static DEFINE_RWLOCK(act_mod_lock);
553
WANG Congddf97cc2016-02-22 15:57:53 -0800554int tcf_register_action(struct tc_action_ops *act,
555 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
WANG Cong1f747c22013-12-15 20:15:10 -0800557 struct tc_action_ops *a;
WANG Congddf97cc2016-02-22 15:57:53 -0800558 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
WANG Congddf97cc2016-02-22 15:57:53 -0800560 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500561 return -EINVAL;
562
WANG Congab102b82016-10-11 10:56:45 -0700563 /* We have to register pernet ops before making the action ops visible,
564 * otherwise tcf_action_init_1() could get a partially initialized
565 * netns.
566 */
567 ret = register_pernet_subsys(ops);
568 if (ret)
569 return ret;
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800572 list_for_each_entry(a, &act_base, head) {
Eli Coheneddd2cf2019-02-10 14:25:00 +0200573 if (act->id == a->id || (strcmp(act->kind, a->kind) == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700575 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return -EEXIST;
577 }
578 }
WANG Cong1f747c22013-12-15 20:15:10 -0800579 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 write_unlock(&act_mod_lock);
WANG Congddf97cc2016-02-22 15:57:53 -0800581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return 0;
583}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800584EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
WANG Congddf97cc2016-02-22 15:57:53 -0800586int tcf_unregister_action(struct tc_action_ops *act,
587 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
WANG Cong1f747c22013-12-15 20:15:10 -0800589 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 int err = -ENOENT;
591
592 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800593 list_for_each_entry(a, &act_base, head) {
594 if (a == act) {
595 list_del(&act->head);
596 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700601 if (!err)
602 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return err;
604}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800605EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607/* lookup by name */
608static struct tc_action_ops *tc_lookup_action_n(char *kind)
609{
Eric Dumazeta7928662013-12-20 12:32:32 -0800610 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 if (kind) {
613 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800614 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800616 if (try_module_get(a->owner))
617 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 break;
619 }
620 }
621 read_unlock(&act_mod_lock);
622 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800623 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800626/* lookup by nlattr */
627static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Eric Dumazeta7928662013-12-20 12:32:32 -0800629 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 if (kind) {
632 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800633 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800634 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800635 if (try_module_get(a->owner))
636 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 break;
638 }
639 }
640 read_unlock(&act_mod_lock);
641 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800642 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400645/*TCA_ACT_MAX_PRIO is 32, there count upto 32 */
646#define TCA_ACT_MAX_PRIO_MASK 0x1FF
WANG Cong22dc13c2016-08-13 22:35:00 -0700647int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
648 int nr_actions, struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400650 u32 jmp_prgcnt = 0;
651 u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
Jiri Pirkoec1a9cc2017-08-04 14:29:02 +0200652 int i;
653 int ret = TC_ACT_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Willem de Bruijne7246e12017-01-07 17:06:35 -0500655 if (skb_skip_tc_classify(skb))
656 return TC_ACT_OK;
657
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400658restart_act_graph:
WANG Cong22dc13c2016-08-13 22:35:00 -0700659 for (i = 0; i < nr_actions; i++) {
660 const struct tc_action *a = actions[i];
661
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400662 if (jmp_prgcnt > 0) {
663 jmp_prgcnt -= 1;
664 continue;
665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500667 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500668 if (ret == TC_ACT_REPEAT)
669 goto repeat; /* we need a ttl - JHS */
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400670
Jiri Pirko9da32422017-05-02 10:12:00 +0200671 if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400672 jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
673 if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
674 /* faulty opcode, stop pipeline */
675 return TC_ACT_OK;
676 } else {
677 jmp_ttl -= 1;
678 if (jmp_ttl > 0)
679 goto restart_act_graph;
680 else /* faulty graph, stop pipeline */
681 return TC_ACT_OK;
682 }
Jiri Pirkodb505142017-05-17 11:08:03 +0200683 } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
Davide Carattiee3bbfe2019-03-20 15:00:16 +0100684 if (unlikely(!rcu_access_pointer(a->goto_chain))) {
685 net_warn_ratelimited("can't go to NULL chain!\n");
686 return TC_ACT_SHOT;
687 }
Jiri Pirkodb505142017-05-17 11:08:03 +0200688 tcf_action_goto_chain_exec(a, res);
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400689 }
690
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500691 if (ret != TC_ACT_PIPE)
Willem de Bruijne7246e12017-01-07 17:06:35 -0500692 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return ret;
696}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800697EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Vlad Buslov90b73b72018-07-05 17:24:33 +0300699int tcf_action_destroy(struct tc_action *actions[], int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Jiri Pirko255cd502017-09-13 17:32:37 +0200701 const struct tc_action_ops *ops;
Vlad Buslov90b73b72018-07-05 17:24:33 +0300702 struct tc_action *a;
703 int ret = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Vlad Buslov90b73b72018-07-05 17:24:33 +0300705 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
706 a = actions[i];
707 actions[i] = NULL;
Jiri Pirko255cd502017-09-13 17:32:37 +0200708 ops = a->ops;
Chris Mi65a206c2017-08-30 02:31:59 -0400709 ret = __tcf_idr_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800710 if (ret == ACT_P_DELETED)
Jiri Pirko255cd502017-09-13 17:32:37 +0200711 module_put(ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800712 else if (ret < 0)
713 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
WANG Cong55334a52014-02-11 17:07:34 -0800715 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
Paolo Abeni97763dc2018-08-29 10:22:33 +0200718static int tcf_action_destroy_1(struct tc_action *a, int bind)
719{
720 struct tc_action *actions[] = { a, NULL };
721
722 return tcf_action_destroy(actions, bind);
723}
724
Vlad Buslov16af6062018-07-05 17:24:29 +0300725static int tcf_action_put(struct tc_action *p)
726{
727 return __tcf_action_put(p, false);
728}
729
Cong Wangedfaf942018-08-19 12:22:05 -0700730/* Put all actions in this array, skip those NULL's. */
Vlad Buslov90b73b72018-07-05 17:24:33 +0300731static void tcf_action_put_many(struct tc_action *actions[])
Vlad Buslovcae422f2018-07-05 17:24:31 +0300732{
Vlad Buslov90b73b72018-07-05 17:24:33 +0300733 int i;
Vlad Buslovcae422f2018-07-05 17:24:31 +0300734
Cong Wangedfaf942018-08-19 12:22:05 -0700735 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
Vlad Buslov90b73b72018-07-05 17:24:33 +0300736 struct tc_action *a = actions[i];
Cong Wangedfaf942018-08-19 12:22:05 -0700737 const struct tc_action_ops *ops;
Vlad Buslovcae422f2018-07-05 17:24:31 +0300738
Cong Wangedfaf942018-08-19 12:22:05 -0700739 if (!a)
740 continue;
741 ops = a->ops;
Vlad Buslovcae422f2018-07-05 17:24:31 +0300742 if (tcf_action_put(a))
743 module_put(ops->owner);
744 }
745}
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747int
748tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
749{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return a->ops->dump(skb, a, bind, ref);
751}
752
753int
754tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
755{
756 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700757 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800758 struct nlattr *nest;
Vlad Busloveec94fd2018-07-05 17:24:23 +0300759 struct tc_cookie *cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
David S. Miller1b34ec42012-03-29 05:11:39 -0400761 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
762 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800764 goto nla_put_failure;
Vlad Busloveec94fd2018-07-05 17:24:23 +0300765
766 rcu_read_lock();
767 cookie = rcu_dereference(a->act_cookie);
768 if (cookie) {
769 if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) {
770 rcu_read_unlock();
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500771 goto nla_put_failure;
Vlad Busloveec94fd2018-07-05 17:24:23 +0300772 }
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500773 }
Vlad Busloveec94fd2018-07-05 17:24:23 +0300774 rcu_read_unlock();
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500775
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200776 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800777 if (nest == NULL)
778 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000779 err = tcf_action_dump_old(skb, a, bind, ref);
780 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800781 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return err;
783 }
784
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800785nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700786 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return -1;
788}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800789EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Vlad Buslov90b73b72018-07-05 17:24:33 +0300791int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[],
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400792 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 struct tc_action *a;
Vlad Buslov90b73b72018-07-05 17:24:33 +0300795 int err = -EINVAL, i;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800796 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Vlad Buslov90b73b72018-07-05 17:24:33 +0300798 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
799 a = actions[i];
Vlad Buslov4097e9d22019-05-23 09:32:31 +0300800 nest = nla_nest_start_noflag(skb, i + 1);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800801 if (nest == NULL)
802 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 err = tcf_action_dump_1(skb, a, bind, ref);
804 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700805 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800806 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808
809 return 0;
810
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800811nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700812 err = -EINVAL;
813errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800814 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700815 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200818static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500819{
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200820 struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
821 if (!c)
822 return NULL;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500823
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200824 c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
825 if (!c->data) {
826 kfree(c);
827 return NULL;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500828 }
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200829 c->len = nla_len(tb[TCA_ACT_COOKIE]);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500830
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200831 return c;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500832}
833
Vlad Buslovabbb0d32019-10-30 16:09:05 +0200834static const u32 tca_act_flags_allowed = TCA_ACT_FLAGS_NO_PERCPU_STATS;
Cong Wang199ce852019-09-18 18:44:43 -0700835static const struct nla_policy tcf_action_policy[TCA_ACT_MAX + 1] = {
Cong Wang4b793fe2019-10-07 13:26:29 -0700836 [TCA_ACT_KIND] = { .type = NLA_STRING },
Cong Wang199ce852019-09-18 18:44:43 -0700837 [TCA_ACT_INDEX] = { .type = NLA_U32 },
838 [TCA_ACT_COOKIE] = { .type = NLA_BINARY,
839 .len = TC_COOKIE_MAX_SIZE },
840 [TCA_ACT_OPTIONS] = { .type = NLA_NESTED },
Vlad Buslovabbb0d32019-10-30 16:09:05 +0200841 [TCA_ACT_FLAGS] = { .type = NLA_BITFIELD32,
842 .validation_data = &tca_act_flags_allowed },
Cong Wang199ce852019-09-18 18:44:43 -0700843};
844
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200845struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
846 struct nlattr *nla, struct nlattr *est,
Alexander Aringaea0d722018-02-15 10:54:54 -0500847 char *name, int ovr, int bind,
Vlad Buslov789871b2018-07-05 17:24:25 +0300848 bool rtnl_held,
Alexander Aringaea0d722018-02-15 10:54:54 -0500849 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Vlad Buslovabbb0d32019-10-30 16:09:05 +0200851 struct nla_bitfield32 flags = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 struct tc_action *a;
853 struct tc_action_ops *a_o;
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200854 struct tc_cookie *cookie = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000856 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800857 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800858 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (name == NULL) {
Cong Wang199ce852019-09-18 18:44:43 -0700861 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
862 tcf_action_policy, extack);
Patrick McHardycee63722008-01-23 20:33:32 -0800863 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800865 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800866 kind = tb[TCA_ACT_KIND];
Alexander Aring84ae0172018-02-15 10:54:55 -0500867 if (!kind) {
868 NL_SET_ERR_MSG(extack, "TC action kind must be specified");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -0500870 }
Cong Wang4b793fe2019-10-07 13:26:29 -0700871 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) {
872 NL_SET_ERR_MSG(extack, "TC action name too long");
873 goto err_out;
874 }
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200875 if (tb[TCA_ACT_COOKIE]) {
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200876 cookie = nla_memdup_cookie(tb);
877 if (!cookie) {
Alexander Aring84ae0172018-02-15 10:54:55 -0500878 NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200879 err = -ENOMEM;
880 goto err_out;
881 }
882 }
Vlad Buslovabbb0d32019-10-30 16:09:05 +0200883 if (tb[TCA_ACT_FLAGS])
884 flags = nla_get_bitfield32(tb[TCA_ACT_FLAGS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 } else {
Alexander Aring84ae0172018-02-15 10:54:55 -0500886 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) {
887 NL_SET_ERR_MSG(extack, "TC action name too long");
888 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -0500890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892
893 a_o = tc_lookup_action_n(act_name);
894 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700895#ifdef CONFIG_MODULES
Vlad Buslov789871b2018-07-05 17:24:25 +0300896 if (rtnl_held)
897 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800898 request_module("act_%s", act_name);
Vlad Buslov789871b2018-07-05 17:24:25 +0300899 if (rtnl_held)
900 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 a_o = tc_lookup_action_n(act_name);
903
904 /* We dropped the RTNL semaphore in order to
905 * perform the module load. So, even if we
906 * succeeded in loading the module we have to
907 * tell the caller to replay the request. We
908 * indicate this using -EAGAIN.
909 */
910 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800911 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 goto err_mod;
913 }
914#endif
Alexander Aring84ae0172018-02-15 10:54:55 -0500915 NL_SET_ERR_MSG(extack, "Failed to load TC action module");
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800916 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 goto err_out;
918 }
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 /* backward compatibility for policer */
921 if (name == NULL)
Alexander Aring589dad62018-02-15 10:54:56 -0500922 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind,
Vlad Buslovabbb0d32019-10-30 16:09:05 +0200923 rtnl_held, tp, flags.value, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 else
Vlad Buslov789871b2018-07-05 17:24:25 +0300925 err = a_o->init(net, nla, est, &a, ovr, bind, rtnl_held,
Vlad Buslovabbb0d32019-10-30 16:09:05 +0200926 tp, flags.value, extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800927 if (err < 0)
WANG Conga85a9702016-07-25 16:09:41 -0700928 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Vlad Busloveec94fd2018-07-05 17:24:23 +0300930 if (!name && tb[TCA_ACT_COOKIE])
931 tcf_set_action_cookie(&a->act_cookie, cookie);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000934 * if it exists and is only bound to in a_o->init() then
935 * ACT_P_CREATED is not returned (a zero is).
936 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800937 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Davide Caratti85d09662019-03-20 14:59:59 +0100940 if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN) &&
Davide Carattiee3bbfe2019-03-20 15:00:16 +0100941 !rcu_access_pointer(a->goto_chain)) {
Paolo Abeni97763dc2018-08-29 10:22:33 +0200942 tcf_action_destroy_1(a, bind);
Davide Caratti85d09662019-03-20 14:59:59 +0100943 NL_SET_ERR_MSG(extack, "can't use goto chain with NULL chain");
Paolo Abeni97763dc2018-08-29 10:22:33 +0200944 return ERR_PTR(-EINVAL);
Paolo Abeni802bfb12018-07-30 14:30:42 +0200945 }
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return a;
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949err_mod:
950 module_put(a_o->owner);
951err_out:
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200952 if (cookie) {
953 kfree(cookie->data);
954 kfree(cookie);
955 }
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800956 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Vlad Buslov90b73b72018-07-05 17:24:33 +0300959/* Returns numbers of initialized actions or negative error. */
960
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200961int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
962 struct nlattr *est, char *name, int ovr, int bind,
Vlad Buslov90b73b72018-07-05 17:24:33 +0300963 struct tc_action *actions[], size_t *attr_size,
Vlad Buslov789871b2018-07-05 17:24:25 +0300964 bool rtnl_held, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000966 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800967 struct tc_action *act;
Roman Mashak4e76e752018-03-08 16:59:19 -0500968 size_t sz = 0;
Patrick McHardycee63722008-01-23 20:33:32 -0800969 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 int i;
971
Johannes Berg8cb08172019-04-26 14:07:28 +0200972 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
973 extack);
Patrick McHardycee63722008-01-23 20:33:32 -0800974 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800975 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800977 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Alexander Aringaea0d722018-02-15 10:54:54 -0500978 act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind,
Vlad Buslov789871b2018-07-05 17:24:25 +0300979 rtnl_held, extack);
WANG Cong33be6272013-12-15 20:15:05 -0800980 if (IS_ERR(act)) {
981 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800983 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800984 act->order = i;
Roman Mashak4e76e752018-03-08 16:59:19 -0500985 sz += tcf_action_fill_size(act);
Vlad Buslov90b73b72018-07-05 17:24:33 +0300986 /* Start from index 0 */
987 actions[i - 1] = act;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400989
Roman Mashak4e76e752018-03-08 16:59:19 -0500990 *attr_size = tcf_action_full_attrs_size(sz);
Vlad Buslov90b73b72018-07-05 17:24:33 +0300991 return i - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993err:
WANG Cong33be6272013-12-15 20:15:05 -0800994 tcf_action_destroy(actions, bind);
995 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
Vlad Buslovc8ecebd2019-10-30 16:09:00 +0200998void tcf_action_update_stats(struct tc_action *a, u64 bytes, u32 packets,
999 bool drop, bool hw)
1000{
Vlad Buslov5e174d52019-10-30 16:09:04 +02001001 if (a->cpu_bstats) {
1002 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
Vlad Buslovc8ecebd2019-10-30 16:09:00 +02001003
Vlad Buslov5e174d52019-10-30 16:09:04 +02001004 if (drop)
1005 this_cpu_ptr(a->cpu_qstats)->drops += packets;
1006
1007 if (hw)
1008 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats_hw),
1009 bytes, packets);
1010 return;
1011 }
1012
1013 _bstats_update(&a->tcfa_bstats, bytes, packets);
Vlad Buslovc8ecebd2019-10-30 16:09:00 +02001014 if (drop)
Vlad Buslov5e174d52019-10-30 16:09:04 +02001015 a->tcfa_qstats.drops += packets;
Vlad Buslovc8ecebd2019-10-30 16:09:00 +02001016 if (hw)
Vlad Buslov5e174d52019-10-30 16:09:04 +02001017 _bstats_update(&a->tcfa_bstats_hw, bytes, packets);
Vlad Buslovc8ecebd2019-10-30 16:09:00 +02001018}
1019EXPORT_SYMBOL(tcf_action_update_stats);
1020
WANG Congec0595c2016-07-25 16:09:42 -07001021int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 int compat_mode)
1023{
1024 int err = 0;
1025 struct gnet_dump d;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001026
WANG Cong7eb88962014-01-09 16:14:05 -08001027 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 goto errout;
1029
1030 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -04001031 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 */
1033 if (compat_mode) {
WANG Congec0595c2016-07-25 16:09:42 -07001034 if (p->type == TCA_OLD_COMPAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 err = gnet_stats_start_copy_compat(skb, 0,
Nicolas Dichtel9854518e2016-04-26 10:06:18 +02001036 TCA_STATS,
1037 TCA_XSTATS,
WANG Congec0595c2016-07-25 16:09:42 -07001038 &p->tcfa_lock, &d,
Nicolas Dichtel9854518e2016-04-26 10:06:18 +02001039 TCA_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 else
1041 return 0;
1042 } else
1043 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Congec0595c2016-07-25 16:09:42 -07001044 &p->tcfa_lock, &d, TCA_ACT_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 if (err < 0)
1047 goto errout;
1048
WANG Congec0595c2016-07-25 16:09:42 -07001049 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
Eelco Chaudron28169ab2018-09-21 07:14:02 -04001050 gnet_stats_copy_basic_hw(NULL, &d, p->cpu_bstats_hw,
1051 &p->tcfa_bstats_hw) < 0 ||
Eric Dumazet1c0d32f2016-12-04 09:48:16 -08001052 gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -07001053 gnet_stats_copy_queue(&d, p->cpu_qstats,
WANG Congec0595c2016-07-25 16:09:42 -07001054 &p->tcfa_qstats,
1055 p->tcfa_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 goto errout;
1057
1058 if (gnet_stats_finish_copy(&d) < 0)
1059 goto errout;
1060
1061 return 0;
1062
1063errout:
1064 return -1;
1065}
1066
Vlad Buslov90b73b72018-07-05 17:24:33 +03001067static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[],
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001068 u32 portid, u32 seq, u16 flags, int event, int bind,
1069 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
1071 struct tcamsg *t;
1072 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001073 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001074 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Eric W. Biederman15e47302012-09-07 20:12:54 +00001076 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -07001077 if (!nlh)
1078 goto out_nlmsg_trim;
1079 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001081 t->tca__pad1 = 0;
1082 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001083
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001084 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
Alexander Aring1af8515582018-02-15 10:54:53 -05001085 if (!nest)
David S. Miller8b00a532012-06-26 21:39:32 -07001086 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
WANG Cong33be6272013-12-15 20:15:05 -08001088 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001089 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001091 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001092
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001093 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return skb->len;
1095
David S. Miller8b00a532012-06-26 21:39:32 -07001096out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001097 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return -1;
1099}
1100
1101static int
Roman Mashakc4c42902017-07-13 13:12:18 -04001102tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
Vlad Buslov90b73b72018-07-05 17:24:33 +03001103 struct tc_action *actions[], int event,
Alexander Aring84ae0172018-02-15 10:54:55 -05001104 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
1106 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1109 if (!skb)
1110 return -ENOBUFS;
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001111 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
Vlad Buslov3f7c72b2018-07-05 17:24:26 +03001112 0, 1) <= 0) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001113 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 kfree_skb(skb);
1115 return -EINVAL;
1116 }
Thomas Graf2942e902006-08-15 00:30:25 -07001117
Eric W. Biederman15e47302012-09-07 20:12:54 +00001118 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
WANG Congddf97cc2016-02-22 15:57:53 -08001121static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
Alexander Aring84ae0172018-02-15 10:54:55 -05001122 struct nlmsghdr *n, u32 portid,
1123 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001125 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -07001126 const struct tc_action_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 struct tc_action *a;
1128 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001129 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Cong Wang199ce852019-09-18 18:44:43 -07001131 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1132 tcf_action_policy, extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001133 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001134 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Patrick McHardycee63722008-01-23 20:33:32 -08001136 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001137 if (tb[TCA_ACT_INDEX] == NULL ||
Alexander Aring84ae0172018-02-15 10:54:55 -05001138 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
1139 NL_SET_ERR_MSG(extack, "Invalid TC action index value");
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001140 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -05001141 }
Patrick McHardy1587bac2008-01-23 20:35:03 -08001142 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001144 err = -EINVAL;
WANG Conga85a9702016-07-25 16:09:41 -07001145 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Alexander Aring84ae0172018-02-15 10:54:55 -05001146 if (!ops) { /* could happen in batch of actions */
Cong Wangf061b482018-08-29 10:15:35 -07001147 NL_SET_ERR_MSG(extack, "Specified TC action kind not found");
WANG Conga85a9702016-07-25 16:09:41 -07001148 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -05001149 }
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001150 err = -ENOENT;
Cong Wangf061b482018-08-29 10:15:35 -07001151 if (ops->lookup(net, &a, index) == 0) {
1152 NL_SET_ERR_MSG(extack, "TC action with specified index not found");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 goto err_mod;
Cong Wangf061b482018-08-29 10:15:35 -07001154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
WANG Conga85a9702016-07-25 16:09:41 -07001156 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159err_mod:
WANG Conga85a9702016-07-25 16:09:41 -07001160 module_put(ops->owner);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001161err_out:
1162 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
Tom Goff7316ae82010-03-19 15:40:13 +00001165static int tca_action_flush(struct net *net, struct nlattr *nla,
Alexander Aring84ae0172018-02-15 10:54:55 -05001166 struct nlmsghdr *n, u32 portid,
1167 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 struct sk_buff *skb;
1170 unsigned char *b;
1171 struct nlmsghdr *nlh;
1172 struct tcamsg *t;
1173 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001174 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001175 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -07001176 const struct tc_action_ops *ops;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001177 struct nlattr *kind;
Jamal Hadi Salim36723872008-08-13 02:41:45 -07001178 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
Alexander Aring84ae0172018-02-15 10:54:55 -05001181 if (!skb)
Jamal Hadi Salim36723872008-08-13 02:41:45 -07001182 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001184 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Cong Wang199ce852019-09-18 18:44:43 -07001186 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1187 tcf_action_policy, extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001188 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 goto err_out;
1190
Patrick McHardycee63722008-01-23 20:33:32 -08001191 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001192 kind = tb[TCA_ACT_KIND];
WANG Conga85a9702016-07-25 16:09:41 -07001193 ops = tc_lookup_action(kind);
Alexander Aring84ae0172018-02-15 10:54:55 -05001194 if (!ops) { /*some idjot trying to flush unknown action */
1195 NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -05001197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001199 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
1200 sizeof(*t), 0);
Alexander Aring84ae0172018-02-15 10:54:55 -05001201 if (!nlh) {
1202 NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
David S. Miller8b00a532012-06-26 21:39:32 -07001203 goto out_module_put;
Alexander Aring84ae0172018-02-15 10:54:55 -05001204 }
David S. Miller8b00a532012-06-26 21:39:32 -07001205 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001207 t->tca__pad1 = 0;
1208 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001210 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
Alexander Aring84ae0172018-02-15 10:54:55 -05001211 if (!nest) {
1212 NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
David S. Miller8b00a532012-06-26 21:39:32 -07001213 goto out_module_put;
Alexander Aring84ae0172018-02-15 10:54:55 -05001214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Alexander Aring41780102018-02-15 10:54:58 -05001216 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack);
Davide Caratti66dede22018-02-15 15:50:57 +01001217 if (err <= 0) {
1218 nla_nest_cancel(skb, nest);
David S. Miller8b00a532012-06-26 21:39:32 -07001219 goto out_module_put;
Davide Caratti66dede22018-02-15 15:50:57 +01001220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001222 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001224 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Conga85a9702016-07-25 16:09:41 -07001226 module_put(ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001227 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001228 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (err > 0)
1230 return 0;
Alexander Aring84ae0172018-02-15 10:54:55 -05001231 if (err < 0)
1232 NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 return err;
1235
David S. Miller8b00a532012-06-26 21:39:32 -07001236out_module_put:
WANG Conga85a9702016-07-25 16:09:41 -07001237 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238err_out:
1239 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return err;
1241}
1242
Cong Wangb144e7e2018-08-19 12:22:07 -07001243static int tcf_action_delete(struct net *net, struct tc_action *actions[])
Vlad Buslov16af6062018-07-05 17:24:29 +03001244{
Cong Wang97a3f84f2018-08-19 12:22:06 -07001245 int i;
Vlad Buslov16af6062018-07-05 17:24:29 +03001246
Vlad Buslov90b73b72018-07-05 17:24:33 +03001247 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
1248 struct tc_action *a = actions[i];
Vlad Buslov16af6062018-07-05 17:24:29 +03001249 const struct tc_action_ops *ops = a->ops;
Vlad Buslov16af6062018-07-05 17:24:29 +03001250 /* Actions can be deleted concurrently so we must save their
1251 * type and id to search again after reference is released.
1252 */
Cong Wang97a3f84f2018-08-19 12:22:06 -07001253 struct tcf_idrinfo *idrinfo = a->idrinfo;
1254 u32 act_index = a->tcfa_index;
Vlad Buslov16af6062018-07-05 17:24:29 +03001255
Vlad Buslovc10bbfa2018-09-03 10:04:55 +03001256 actions[i] = NULL;
Vlad Buslov16af6062018-07-05 17:24:29 +03001257 if (tcf_action_put(a)) {
1258 /* last reference, action was deleted concurrently */
1259 module_put(ops->owner);
1260 } else {
Cong Wang97a3f84f2018-08-19 12:22:06 -07001261 int ret;
1262
Vlad Buslov16af6062018-07-05 17:24:29 +03001263 /* now do the delete */
Cong Wang97a3f84f2018-08-19 12:22:06 -07001264 ret = tcf_idr_delete_index(idrinfo, act_index);
Cong Wangedfaf942018-08-19 12:22:05 -07001265 if (ret < 0)
Vlad Buslov16af6062018-07-05 17:24:29 +03001266 return ret;
1267 }
1268 }
1269 return 0;
1270}
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272static int
Vlad Buslov90b73b72018-07-05 17:24:33 +03001273tcf_del_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
Cong Wangedfaf942018-08-19 12:22:05 -07001274 u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
WANG Conga56e1952014-01-09 16:14:00 -08001275{
1276 int ret;
1277 struct sk_buff *skb;
1278
Roman Mashakd04e6992018-03-08 16:59:17 -05001279 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1280 GFP_KERNEL);
WANG Conga56e1952014-01-09 16:14:00 -08001281 if (!skb)
1282 return -ENOBUFS;
1283
1284 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
Vlad Buslov3f7c72b2018-07-05 17:24:26 +03001285 0, 2) <= 0) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001286 NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
WANG Conga56e1952014-01-09 16:14:00 -08001287 kfree_skb(skb);
1288 return -EINVAL;
1289 }
1290
1291 /* now do the delete */
Cong Wangb144e7e2018-08-19 12:22:07 -07001292 ret = tcf_action_delete(net, actions);
WANG Cong55334a52014-02-11 17:07:34 -08001293 if (ret < 0) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001294 NL_SET_ERR_MSG(extack, "Failed to delete TC action");
WANG Cong55334a52014-02-11 17:07:34 -08001295 kfree_skb(skb);
1296 return ret;
1297 }
WANG Conga56e1952014-01-09 16:14:00 -08001298
1299 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1300 n->nlmsg_flags & NLM_F_ECHO);
1301 if (ret > 0)
1302 return 0;
1303 return ret;
1304}
1305
1306static int
Tom Goff7316ae82010-03-19 15:40:13 +00001307tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Alexander Aring84ae0172018-02-15 10:54:55 -05001308 u32 portid, int event, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Patrick McHardycee63722008-01-23 20:33:32 -08001310 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001311 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -08001312 struct tc_action *act;
Roman Mashakd04e6992018-03-08 16:59:17 -05001313 size_t attr_size = 0;
Cong Wangedfaf942018-08-19 12:22:05 -07001314 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Johannes Berg8cb08172019-04-26 14:07:28 +02001316 ret = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
1317 extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001318 if (ret < 0)
1319 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001321 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Alexander Aring1af8515582018-02-15 10:54:53 -05001322 if (tb[1])
Alexander Aring84ae0172018-02-15 10:54:55 -05001323 return tca_action_flush(net, tb[1], n, portid, extack);
Alexander Aring1af8515582018-02-15 10:54:53 -05001324
Alexander Aring84ae0172018-02-15 10:54:55 -05001325 NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
Alexander Aring1af8515582018-02-15 10:54:53 -05001326 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001329 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001330 act = tcf_action_get_1(net, tb[i], n, portid, extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001331 if (IS_ERR(act)) {
1332 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001334 }
Roman Mashak4e76e752018-03-08 16:59:19 -05001335 attr_size += tcf_action_fill_size(act);
Vlad Buslov90b73b72018-07-05 17:24:33 +03001336 actions[i - 1] = act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338
Roman Mashak4e76e752018-03-08 16:59:19 -05001339 attr_size = tcf_action_full_attrs_size(attr_size);
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (event == RTM_GETACTION)
Vlad Buslov90b73b72018-07-05 17:24:33 +03001342 ret = tcf_get_notify(net, portid, n, actions, event, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 else { /* delete */
Cong Wangedfaf942018-08-19 12:22:05 -07001344 ret = tcf_del_notify(net, n, actions, portid, attr_size, extack);
WANG Conga56e1952014-01-09 16:14:00 -08001345 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 goto err;
Cong Wangedfaf942018-08-19 12:22:05 -07001347 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349err:
Cong Wangedfaf942018-08-19 12:22:05 -07001350 tcf_action_put_many(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return ret;
1352}
1353
WANG Conga56e1952014-01-09 16:14:00 -08001354static int
Vlad Buslov90b73b72018-07-05 17:24:33 +03001355tcf_add_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
Roman Mashakd04e6992018-03-08 16:59:17 -05001356 u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 int err = 0;
1360
Roman Mashakd04e6992018-03-08 16:59:17 -05001361 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1362 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (!skb)
1364 return -ENOBUFS;
1365
WANG Conga56e1952014-01-09 16:14:00 -08001366 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
1367 RTM_NEWACTION, 0, 0) <= 0) {
Roman Mashakd143b9e2018-03-02 20:52:01 -05001368 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
WANG Conga56e1952014-01-09 16:14:00 -08001369 kfree_skb(skb);
1370 return -EINVAL;
1371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
WANG Conga56e1952014-01-09 16:14:00 -08001373 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1374 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (err > 0)
1376 err = 0;
1377 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001380static int tcf_action_add(struct net *net, struct nlattr *nla,
Alexander Aringaea0d722018-02-15 10:54:54 -05001381 struct nlmsghdr *n, u32 portid, int ovr,
1382 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
Roman Mashakd04e6992018-03-08 16:59:17 -05001384 size_t attr_size = 0;
Eric Dumazet39f13ea2019-10-14 11:22:30 -07001385 int loop, ret;
Vlad Buslov90b73b72018-07-05 17:24:33 +03001386 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Eric Dumazet39f13ea2019-10-14 11:22:30 -07001388 for (loop = 0; loop < 10; loop++) {
1389 ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0,
1390 actions, &attr_size, true, extack);
1391 if (ret != -EAGAIN)
1392 break;
1393 }
1394
Vlad Buslov90b73b72018-07-05 17:24:33 +03001395 if (ret < 0)
WANG Congf07fed82016-08-13 22:34:56 -07001396 return ret;
Vlad Buslov90b73b72018-07-05 17:24:33 +03001397 ret = tcf_add_notify(net, n, actions, portid, attr_size, extack);
Vlad Buslovcae422f2018-07-05 17:24:31 +03001398 if (ovr)
Vlad Buslov90b73b72018-07-05 17:24:33 +03001399 tcf_action_put_many(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Vlad Buslovcae422f2018-07-05 17:24:31 +03001401 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
1403
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001404static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON;
1405static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
1406 [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32,
1407 .validation_data = &tcaa_root_flags_allowed },
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001408 [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001409};
1410
David Ahernc21ef3e2017-04-16 09:48:24 -07001411static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
1412 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001414 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001415 struct nlattr *tca[TCA_ROOT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +00001416 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 int ret = 0, ovr = 0;
1418
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001419 if ((n->nlmsg_type != RTM_GETACTION) &&
1420 !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001421 return -EPERM;
1422
Johannes Berg8cb08172019-04-26 14:07:28 +02001423 ret = nlmsg_parse_deprecated(n, sizeof(struct tcamsg), tca,
1424 TCA_ROOT_MAX, NULL, extack);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001425 if (ret < 0)
1426 return ret;
1427
1428 if (tca[TCA_ACT_TAB] == NULL) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001429 NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return -EINVAL;
1431 }
1432
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001433 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 switch (n->nlmsg_type) {
1435 case RTM_NEWACTION:
1436 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001437 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 * Note that CREATE | EXCL implies that
1439 * but since we want avoid ambiguity (eg when flags
1440 * is zero) then just set this
1441 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001442 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 ovr = 1;
Alexander Aringaea0d722018-02-15 10:54:54 -05001444 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr,
1445 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 break;
1447 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001448 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Alexander Aring84ae0172018-02-15 10:54:55 -05001449 portid, RTM_DELACTION, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 break;
1451 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001452 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Alexander Aring84ae0172018-02-15 10:54:55 -05001453 portid, RTM_GETACTION, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 break;
1455 default:
1456 BUG();
1457 }
1458
1459 return ret;
1460}
1461
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001462static struct nlattr *find_dump_kind(struct nlattr **nla)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001464 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001465 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001466 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001468 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 if (tb1 == NULL)
1470 return NULL;
1471
Johannes Berg8cb08172019-04-26 14:07:28 +02001472 if (nla_parse_deprecated(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Patrick McHardy6d834e02008-01-23 20:32:42 -08001475 if (tb[1] == NULL)
1476 return NULL;
Cong Wang199ce852019-09-18 18:44:43 -07001477 if (nla_parse_nested_deprecated(tb2, TCA_ACT_MAX, tb[1], tcf_action_policy, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001479 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Thomas Graf26dab892006-07-05 20:45:06 -07001481 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001484static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
WANG Congddf97cc2016-02-22 15:57:53 -08001486 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001488 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001489 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 struct tc_action_ops *a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001492 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001493 struct nlattr *tb[TCA_ROOT_MAX + 1];
1494 struct nlattr *count_attr = NULL;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001495 unsigned long jiffy_since = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001496 struct nlattr *kind = NULL;
1497 struct nla_bitfield32 bf;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001498 u32 msecs_since = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001499 u32 act_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Johannes Berg8cb08172019-04-26 14:07:28 +02001501 ret = nlmsg_parse_deprecated(cb->nlh, sizeof(struct tcamsg), tb,
1502 TCA_ROOT_MAX, tcaa_policy, cb->extack);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001503 if (ret < 0)
1504 return ret;
1505
1506 kind = find_dump_kind(tb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001508 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return 0;
1510 }
1511
Thomas Graf26dab892006-07-05 20:45:06 -07001512 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001513 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001516 cb->args[2] = 0;
1517 if (tb[TCA_ROOT_FLAGS]) {
1518 bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
1519 cb->args[2] = bf.value;
1520 }
1521
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001522 if (tb[TCA_ROOT_TIME_DELTA]) {
1523 msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
1524 }
1525
Eric W. Biederman15e47302012-09-07 20:12:54 +00001526 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001527 cb->nlh->nlmsg_type, sizeof(*t), 0);
1528 if (!nlh)
1529 goto out_module_put;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001530
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001531 if (msecs_since)
1532 jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
1533
David S. Miller8b00a532012-06-26 21:39:32 -07001534 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001536 t->tca__pad1 = 0;
1537 t->tca__pad2 = 0;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001538 cb->args[3] = jiffy_since;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001539 count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
1540 if (!count_attr)
1541 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001543 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001544 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001545 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Alexander Aring41780102018-02-15 10:54:58 -05001547 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001549 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001552 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 ret = skb->len;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001554 act_count = cb->args[1];
1555 memcpy(nla_data(count_attr), &act_count, sizeof(u32));
1556 cb->args[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 } else
Jamal Hadi Salimebecaa62016-06-13 18:08:42 -04001558 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001560 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001561 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 nlh->nlmsg_flags |= NLM_F_MULTI;
1563 module_put(a_o->owner);
1564 return skb->len;
1565
David S. Miller8b00a532012-06-26 21:39:32 -07001566out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001568 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return skb->len;
1570}
1571
1572static int __init tc_action_init(void)
1573{
Florian Westphalb97bac62017-08-09 20:41:48 +02001574 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
1575 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
Greg Rosec7ac8672011-06-10 01:27:09 +00001576 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
Florian Westphalb97bac62017-08-09 20:41:48 +02001577 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 return 0;
1580}
1581
1582subsys_initcall(tc_action_init);