blob: 99db1c77426b385e34addbc567509096e0aa2768 [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;
Paul E. McKenney445d3742019-09-23 16:09:18 -070091 goto_chain = rcu_replace_pointer(a->goto_chain, goto_chain, 1);
Davide Carattiee3bbfe2019-03-20 15:00:16 +010092 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 */
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -0700188 + nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_HW_STATS */
Roman Mashak4e76e752018-03-08 16:59:19 -0500189 + nla_total_size(0) /* TCA_ACT_STATS nested */
Jiri Pirko1521a672020-02-25 13:54:12 +0100190 + nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_FLAGS */
Roman Mashak4e76e752018-03-08 16:59:19 -0500191 /* TCA_STATS_BASIC */
192 + nla_total_size_64bit(sizeof(struct gnet_stats_basic))
Eric Dumazetb33e6992019-11-04 19:13:15 -0800193 /* TCA_STATS_PKT64 */
194 + nla_total_size_64bit(sizeof(u64))
Roman Mashak4e76e752018-03-08 16:59:19 -0500195 /* TCA_STATS_QUEUE */
196 + nla_total_size_64bit(sizeof(struct gnet_stats_queue))
197 + nla_total_size(0) /* TCA_OPTIONS nested */
198 + nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */
199}
200
201static size_t tcf_action_full_attrs_size(size_t sz)
202{
203 return NLMSG_HDRLEN /* struct nlmsghdr */
204 + sizeof(struct tcamsg)
205 + nla_total_size(0) /* TCA_ACT_TAB nested */
206 + sz;
207}
208
209static size_t tcf_action_fill_size(const struct tc_action *act)
210{
211 size_t sz = tcf_action_shared_attrs_size(act);
212
213 if (act->ops->get_fill_size)
214 return act->ops->get_fill_size(act) + sz;
215 return sz;
216}
217
Vlad Buslov94f44f22020-11-02 22:12:43 +0200218static int
219tcf_action_dump_terse(struct sk_buff *skb, struct tc_action *a, bool from_act)
220{
221 unsigned char *b = skb_tail_pointer(skb);
222 struct tc_cookie *cookie;
223
224 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
225 goto nla_put_failure;
226 if (tcf_action_copy_stats(skb, a, 0))
227 goto nla_put_failure;
228 if (from_act && nla_put_u32(skb, TCA_ACT_INDEX, a->tcfa_index))
229 goto nla_put_failure;
230
231 rcu_read_lock();
232 cookie = rcu_dereference(a->act_cookie);
233 if (cookie) {
234 if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) {
235 rcu_read_unlock();
236 goto nla_put_failure;
237 }
238 }
239 rcu_read_unlock();
240
241 return 0;
242
243nla_put_failure:
244 nlmsg_trim(skb, b);
245 return -1;
246}
247
Chris Mi65a206c2017-08-30 02:31:59 -0400248static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700249 struct netlink_callback *cb)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700250{
Chris Mi65a206c2017-08-30 02:31:59 -0400251 int err = 0, index = -1, s_i = 0, n_i = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400252 u32 act_flags = cb->args[2];
Jamal Hadi Salime62e4842017-07-30 13:24:52 -0400253 unsigned long jiffy_since = cb->args[3];
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800254 struct nlattr *nest;
Chris Mi65a206c2017-08-30 02:31:59 -0400255 struct idr *idr = &idrinfo->action_idr;
256 struct tc_action *p;
257 unsigned long id = 1;
Cong Wange33d2b72019-06-28 11:03:41 -0700258 unsigned long tmp;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700259
Cong Wang95278dd2018-10-02 12:50:19 -0700260 mutex_lock(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700261
262 s_i = cb->args[0];
263
Cong Wange33d2b72019-06-28 11:03:41 -0700264 idr_for_each_entry_ul(idr, p, tmp, id) {
Chris Mi65a206c2017-08-30 02:31:59 -0400265 index++;
266 if (index < s_i)
267 continue;
Cong Wang580e4272020-10-02 12:13:34 -0700268 if (IS_ERR(p))
269 continue;
WANG Conga85a9702016-07-25 16:09:41 -0700270
Chris Mi65a206c2017-08-30 02:31:59 -0400271 if (jiffy_since &&
272 time_after(jiffy_since,
273 (unsigned long)p->tcfa_tm.lastuse))
274 continue;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700275
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200276 nest = nla_nest_start_noflag(skb, n_i);
Craig Dillabaugh734549e2018-03-26 14:58:32 -0400277 if (!nest) {
278 index--;
Chris Mi65a206c2017-08-30 02:31:59 -0400279 goto nla_put_failure;
Craig Dillabaugh734549e2018-03-26 14:58:32 -0400280 }
Vlad Buslovf4600192020-11-24 18:40:54 +0200281 err = (act_flags & TCA_ACT_FLAG_TERSE_DUMP) ?
Vlad Buslov94f44f22020-11-02 22:12:43 +0200282 tcf_action_dump_terse(skb, p, true) :
283 tcf_action_dump_1(skb, p, 0, 0);
Chris Mi65a206c2017-08-30 02:31:59 -0400284 if (err < 0) {
285 index--;
286 nlmsg_trim(skb, nest);
287 goto done;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700288 }
Chris Mi65a206c2017-08-30 02:31:59 -0400289 nla_nest_end(skb, nest);
290 n_i++;
Vlad Buslovf4600192020-11-24 18:40:54 +0200291 if (!(act_flags & TCA_ACT_FLAG_LARGE_DUMP_ON) &&
Chris Mi65a206c2017-08-30 02:31:59 -0400292 n_i >= TCA_ACT_MAX_PRIO)
293 goto done;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700294 }
295done:
Jamal Hadi Salime62e4842017-07-30 13:24:52 -0400296 if (index >= 0)
297 cb->args[0] = index + 1;
298
Cong Wang95278dd2018-10-02 12:50:19 -0700299 mutex_unlock(&idrinfo->lock);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400300 if (n_i) {
Vlad Buslovf4600192020-11-24 18:40:54 +0200301 if (act_flags & TCA_ACT_FLAG_LARGE_DUMP_ON)
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400302 cb->args[1] = n_i;
303 }
David S. Millere9ce1cd2006-08-21 23:54:55 -0700304 return n_i;
305
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800306nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800307 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700308 goto done;
309}
310
Vlad Buslovec3ed292018-09-19 16:37:29 -0700311static int tcf_idr_release_unsafe(struct tc_action *p)
312{
313 if (atomic_read(&p->tcfa_bindcnt) > 0)
314 return -EPERM;
315
316 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
317 idr_remove(&p->idrinfo->action_idr, p->tcfa_index);
318 tcf_action_cleanup(p);
319 return ACT_P_DELETED;
320 }
321
322 return 0;
323}
324
Chris Mi65a206c2017-08-30 02:31:59 -0400325static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700326 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700327{
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800328 struct nlattr *nest;
Chris Mi65a206c2017-08-30 02:31:59 -0400329 int n_i = 0;
WANG Cong55334a52014-02-11 17:07:34 -0800330 int ret = -EINVAL;
Chris Mi65a206c2017-08-30 02:31:59 -0400331 struct idr *idr = &idrinfo->action_idr;
332 struct tc_action *p;
333 unsigned long id = 1;
Cong Wange33d2b72019-06-28 11:03:41 -0700334 unsigned long tmp;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700335
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200336 nest = nla_nest_start_noflag(skb, 0);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800337 if (nest == NULL)
338 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700339 if (nla_put_string(skb, TCA_KIND, ops->kind))
David S. Miller1b34ec42012-03-29 05:11:39 -0400340 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700341
Cong Wang95278dd2018-10-02 12:50:19 -0700342 mutex_lock(&idrinfo->lock);
Cong Wange33d2b72019-06-28 11:03:41 -0700343 idr_for_each_entry_ul(idr, p, tmp, id) {
Cong Wang0fedc632020-09-22 20:56:24 -0700344 if (IS_ERR(p))
345 continue;
Vlad Buslovec3ed292018-09-19 16:37:29 -0700346 ret = tcf_idr_release_unsafe(p);
Chris Mi65a206c2017-08-30 02:31:59 -0400347 if (ret == ACT_P_DELETED) {
Jiri Pirko255cd502017-09-13 17:32:37 +0200348 module_put(ops->owner);
Chris Mi65a206c2017-08-30 02:31:59 -0400349 n_i++;
350 } else if (ret < 0) {
Cong Wang95278dd2018-10-02 12:50:19 -0700351 mutex_unlock(&idrinfo->lock);
Chris Mi65a206c2017-08-30 02:31:59 -0400352 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700353 }
354 }
Cong Wang95278dd2018-10-02 12:50:19 -0700355 mutex_unlock(&idrinfo->lock);
Vlad Buslovec3ed292018-09-19 16:37:29 -0700356
David S. Miller1b34ec42012-03-29 05:11:39 -0400357 if (nla_put_u32(skb, TCA_FCNT, n_i))
358 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800359 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700360
361 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800362nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800363 nla_nest_cancel(skb, nest);
WANG Cong55334a52014-02-11 17:07:34 -0800364 return ret;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700365}
366
WANG Congddf97cc2016-02-22 15:57:53 -0800367int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
368 struct netlink_callback *cb, int type,
Alexander Aringb3620142018-02-15 10:54:59 -0500369 const struct tc_action_ops *ops,
370 struct netlink_ext_ack *extack)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700371{
Chris Mi65a206c2017-08-30 02:31:59 -0400372 struct tcf_idrinfo *idrinfo = tn->idrinfo;
WANG Congddf97cc2016-02-22 15:57:53 -0800373
David S. Millere9ce1cd2006-08-21 23:54:55 -0700374 if (type == RTM_DELACTION) {
Chris Mi65a206c2017-08-30 02:31:59 -0400375 return tcf_del_walker(idrinfo, skb, ops);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700376 } else if (type == RTM_GETACTION) {
Chris Mi65a206c2017-08-30 02:31:59 -0400377 return tcf_dump_walker(idrinfo, skb, cb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700378 } else {
Alexander Aringb3620142018-02-15 10:54:59 -0500379 WARN(1, "tcf_generic_walker: unknown command %d\n", type);
380 NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command");
David S. Millere9ce1cd2006-08-21 23:54:55 -0700381 return -EINVAL;
382 }
383}
WANG Congddf97cc2016-02-22 15:57:53 -0800384EXPORT_SYMBOL(tcf_generic_walker);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700385
Cong Wang7d485c42018-08-19 12:22:08 -0700386int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700387{
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300388 struct tcf_idrinfo *idrinfo = tn->idrinfo;
389 struct tc_action *p;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700390
Cong Wang95278dd2018-10-02 12:50:19 -0700391 mutex_lock(&idrinfo->lock);
Matthew Wilcox322d8842017-11-28 10:01:24 -0500392 p = idr_find(&idrinfo->action_idr, index);
Cong Wang7d485c42018-08-19 12:22:08 -0700393 if (IS_ERR(p))
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300394 p = NULL;
Cong Wang7d485c42018-08-19 12:22:08 -0700395 else if (p)
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300396 refcount_inc(&p->tcfa_refcnt);
Cong Wang95278dd2018-10-02 12:50:19 -0700397 mutex_unlock(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700398
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300399 if (p) {
400 *a = p;
401 return true;
402 }
403 return false;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700404}
Chris Mi65a206c2017-08-30 02:31:59 -0400405EXPORT_SYMBOL(tcf_idr_search);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700406
Cong Wang97a3f84f2018-08-19 12:22:06 -0700407static int tcf_idr_delete_index(struct tcf_idrinfo *idrinfo, u32 index)
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300408{
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300409 struct tc_action *p;
410 int ret = 0;
411
Cong Wang95278dd2018-10-02 12:50:19 -0700412 mutex_lock(&idrinfo->lock);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300413 p = idr_find(&idrinfo->action_idr, index);
414 if (!p) {
Cong Wang95278dd2018-10-02 12:50:19 -0700415 mutex_unlock(&idrinfo->lock);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300416 return -ENOENT;
417 }
418
419 if (!atomic_read(&p->tcfa_bindcnt)) {
420 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
421 struct module *owner = p->ops->owner;
422
423 WARN_ON(p != idr_remove(&idrinfo->action_idr,
424 p->tcfa_index));
Cong Wang95278dd2018-10-02 12:50:19 -0700425 mutex_unlock(&idrinfo->lock);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300426
Vlad Buslov16af6062018-07-05 17:24:29 +0300427 tcf_action_cleanup(p);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300428 module_put(owner);
429 return 0;
430 }
431 ret = 0;
432 } else {
433 ret = -EPERM;
434 }
435
Cong Wang95278dd2018-10-02 12:50:19 -0700436 mutex_unlock(&idrinfo->lock);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300437 return ret;
438}
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300439
Chris Mi65a206c2017-08-30 02:31:59 -0400440int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
441 struct tc_action **a, const struct tc_action_ops *ops,
Vlad Buslove3822672019-10-30 16:09:06 +0200442 int bind, bool cpustats, u32 flags)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700443{
WANG Congec0595c2016-07-25 16:09:42 -0700444 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
Chris Mi65a206c2017-08-30 02:31:59 -0400445 struct tcf_idrinfo *idrinfo = tn->idrinfo;
Eric Dumazet519c8182015-07-06 05:18:04 -0700446 int err = -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700447
448 if (unlikely(!p))
WANG Cong86062032014-02-11 17:07:31 -0800449 return -ENOMEM;
Vlad Buslov036bb442018-07-05 17:24:24 +0300450 refcount_set(&p->tcfa_refcnt, 1);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700451 if (bind)
Vlad Buslov036bb442018-07-05 17:24:24 +0300452 atomic_set(&p->tcfa_bindcnt, 1);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700453
Eric Dumazet519c8182015-07-06 05:18:04 -0700454 if (cpustats) {
455 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500456 if (!p->cpu_bstats)
Eric Dumazet519c8182015-07-06 05:18:04 -0700457 goto err1;
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400458 p->cpu_bstats_hw = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
459 if (!p->cpu_bstats_hw)
460 goto err2;
Matthew Wilcox339913a2017-11-28 10:28:15 -0500461 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
462 if (!p->cpu_qstats)
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400463 goto err3;
Eric Dumazet519c8182015-07-06 05:18:04 -0700464 }
WANG Congec0595c2016-07-25 16:09:42 -0700465 spin_lock_init(&p->tcfa_lock);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500466 p->tcfa_index = index;
WANG Congec0595c2016-07-25 16:09:42 -0700467 p->tcfa_tm.install = jiffies;
468 p->tcfa_tm.lastuse = jiffies;
469 p->tcfa_tm.firstuse = 0;
Vlad Buslove3822672019-10-30 16:09:06 +0200470 p->tcfa_flags = flags;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800471 if (est) {
WANG Congec0595c2016-07-25 16:09:42 -0700472 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
473 &p->tcfa_rate_est,
474 &p->tcfa_lock, NULL, est);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500475 if (err)
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400476 goto err4;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800477 }
478
Chris Mi65a206c2017-08-30 02:31:59 -0400479 p->idrinfo = idrinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700480 p->ops = ops;
WANG Congec0595c2016-07-25 16:09:42 -0700481 *a = p;
WANG Cong86062032014-02-11 17:07:31 -0800482 return 0;
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400483err4:
Matthew Wilcox339913a2017-11-28 10:28:15 -0500484 free_percpu(p->cpu_qstats);
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400485err3:
486 free_percpu(p->cpu_bstats_hw);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500487err2:
488 free_percpu(p->cpu_bstats);
489err1:
490 kfree(p);
491 return err;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700492}
Chris Mi65a206c2017-08-30 02:31:59 -0400493EXPORT_SYMBOL(tcf_idr_create);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700494
Vlad Buslove3822672019-10-30 16:09:06 +0200495int tcf_idr_create_from_flags(struct tc_action_net *tn, u32 index,
496 struct nlattr *est, struct tc_action **a,
497 const struct tc_action_ops *ops, int bind,
498 u32 flags)
499{
500 /* Set cpustats according to actions flags. */
501 return tcf_idr_create(tn, index, est, a, ops, bind,
502 !(flags & TCA_ACT_FLAGS_NO_PERCPU_STATS), flags);
503}
504EXPORT_SYMBOL(tcf_idr_create_from_flags);
505
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300506/* Cleanup idr index that was allocated but not initialized. */
507
508void tcf_idr_cleanup(struct tc_action_net *tn, u32 index)
509{
510 struct tcf_idrinfo *idrinfo = tn->idrinfo;
511
Cong Wang95278dd2018-10-02 12:50:19 -0700512 mutex_lock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300513 /* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
514 WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index)));
Cong Wang95278dd2018-10-02 12:50:19 -0700515 mutex_unlock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300516}
517EXPORT_SYMBOL(tcf_idr_cleanup);
518
519/* Check if action with specified index exists. If actions is found, increments
520 * its reference and bind counters, and return 1. Otherwise insert temporary
521 * error pointer (to prevent concurrent users from inserting actions with same
522 * index) and return 0.
523 */
524
525int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
526 struct tc_action **a, int bind)
527{
528 struct tcf_idrinfo *idrinfo = tn->idrinfo;
529 struct tc_action *p;
530 int ret;
531
532again:
Cong Wang95278dd2018-10-02 12:50:19 -0700533 mutex_lock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300534 if (*index) {
535 p = idr_find(&idrinfo->action_idr, *index);
536 if (IS_ERR(p)) {
537 /* This means that another process allocated
538 * index but did not assign the pointer yet.
539 */
Cong Wang95278dd2018-10-02 12:50:19 -0700540 mutex_unlock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300541 goto again;
542 }
543
544 if (p) {
545 refcount_inc(&p->tcfa_refcnt);
546 if (bind)
547 atomic_inc(&p->tcfa_bindcnt);
548 *a = p;
549 ret = 1;
550 } else {
551 *a = NULL;
552 ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
Cong Wang95278dd2018-10-02 12:50:19 -0700553 *index, GFP_KERNEL);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300554 if (!ret)
555 idr_replace(&idrinfo->action_idr,
556 ERR_PTR(-EBUSY), *index);
557 }
558 } else {
559 *index = 1;
560 *a = NULL;
561 ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
Cong Wang95278dd2018-10-02 12:50:19 -0700562 UINT_MAX, GFP_KERNEL);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300563 if (!ret)
564 idr_replace(&idrinfo->action_idr, ERR_PTR(-EBUSY),
565 *index);
566 }
Cong Wang95278dd2018-10-02 12:50:19 -0700567 mutex_unlock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300568 return ret;
569}
570EXPORT_SYMBOL(tcf_idr_check_alloc);
571
Chris Mi65a206c2017-08-30 02:31:59 -0400572void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
573 struct tcf_idrinfo *idrinfo)
WANG Cong1d4150c2016-02-22 15:57:52 -0800574{
Chris Mi65a206c2017-08-30 02:31:59 -0400575 struct idr *idr = &idrinfo->action_idr;
576 struct tc_action *p;
577 int ret;
578 unsigned long id = 1;
Cong Wange33d2b72019-06-28 11:03:41 -0700579 unsigned long tmp;
WANG Cong1d4150c2016-02-22 15:57:52 -0800580
Cong Wange33d2b72019-06-28 11:03:41 -0700581 idr_for_each_entry_ul(idr, p, tmp, id) {
Chris Mi65a206c2017-08-30 02:31:59 -0400582 ret = __tcf_idr_release(p, false, true);
583 if (ret == ACT_P_DELETED)
584 module_put(ops->owner);
585 else if (ret < 0)
586 return;
WANG Cong1d4150c2016-02-22 15:57:52 -0800587 }
Chris Mi65a206c2017-08-30 02:31:59 -0400588 idr_destroy(&idrinfo->action_idr);
WANG Cong1d4150c2016-02-22 15:57:52 -0800589}
Chris Mi65a206c2017-08-30 02:31:59 -0400590EXPORT_SYMBOL(tcf_idrinfo_destroy);
WANG Cong1d4150c2016-02-22 15:57:52 -0800591
WANG Cong1f747c22013-12-15 20:15:10 -0800592static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593static DEFINE_RWLOCK(act_mod_lock);
594
WANG Congddf97cc2016-02-22 15:57:53 -0800595int tcf_register_action(struct tc_action_ops *act,
596 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
WANG Cong1f747c22013-12-15 20:15:10 -0800598 struct tc_action_ops *a;
WANG Congddf97cc2016-02-22 15:57:53 -0800599 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
WANG Congddf97cc2016-02-22 15:57:53 -0800601 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500602 return -EINVAL;
603
WANG Congab102b82016-10-11 10:56:45 -0700604 /* We have to register pernet ops before making the action ops visible,
605 * otherwise tcf_action_init_1() could get a partially initialized
606 * netns.
607 */
608 ret = register_pernet_subsys(ops);
609 if (ret)
610 return ret;
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800613 list_for_each_entry(a, &act_base, head) {
Eli Coheneddd2cf2019-02-10 14:25:00 +0200614 if (act->id == a->id || (strcmp(act->kind, a->kind) == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700616 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return -EEXIST;
618 }
619 }
WANG Cong1f747c22013-12-15 20:15:10 -0800620 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 write_unlock(&act_mod_lock);
WANG Congddf97cc2016-02-22 15:57:53 -0800622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return 0;
624}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800625EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
WANG Congddf97cc2016-02-22 15:57:53 -0800627int tcf_unregister_action(struct tc_action_ops *act,
628 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
WANG Cong1f747c22013-12-15 20:15:10 -0800630 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int err = -ENOENT;
632
633 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800634 list_for_each_entry(a, &act_base, head) {
635 if (a == act) {
636 list_del(&act->head);
637 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700642 if (!err)
643 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return err;
645}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800646EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648/* lookup by name */
649static struct tc_action_ops *tc_lookup_action_n(char *kind)
650{
Eric Dumazeta7928662013-12-20 12:32:32 -0800651 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 if (kind) {
654 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800655 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800657 if (try_module_get(a->owner))
658 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 break;
660 }
661 }
662 read_unlock(&act_mod_lock);
663 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800664 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800667/* lookup by nlattr */
668static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Eric Dumazeta7928662013-12-20 12:32:32 -0800670 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 if (kind) {
673 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800674 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800675 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800676 if (try_module_get(a->owner))
677 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 break;
679 }
680 }
681 read_unlock(&act_mod_lock);
682 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800683 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
Menglong Donge5a4b172020-11-09 02:02:17 -0500686/*TCA_ACT_MAX_PRIO is 32, there count up to 32 */
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400687#define TCA_ACT_MAX_PRIO_MASK 0x1FF
WANG Cong22dc13c2016-08-13 22:35:00 -0700688int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
689 int nr_actions, struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400691 u32 jmp_prgcnt = 0;
692 u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
Jiri Pirkoec1a9cc2017-08-04 14:29:02 +0200693 int i;
694 int ret = TC_ACT_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Willem de Bruijne7246e12017-01-07 17:06:35 -0500696 if (skb_skip_tc_classify(skb))
697 return TC_ACT_OK;
698
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400699restart_act_graph:
WANG Cong22dc13c2016-08-13 22:35:00 -0700700 for (i = 0; i < nr_actions; i++) {
701 const struct tc_action *a = actions[i];
702
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400703 if (jmp_prgcnt > 0) {
704 jmp_prgcnt -= 1;
705 continue;
706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500708 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500709 if (ret == TC_ACT_REPEAT)
710 goto repeat; /* we need a ttl - JHS */
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400711
Jiri Pirko9da32422017-05-02 10:12:00 +0200712 if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400713 jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
714 if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
715 /* faulty opcode, stop pipeline */
716 return TC_ACT_OK;
717 } else {
718 jmp_ttl -= 1;
719 if (jmp_ttl > 0)
720 goto restart_act_graph;
721 else /* faulty graph, stop pipeline */
722 return TC_ACT_OK;
723 }
Jiri Pirkodb505142017-05-17 11:08:03 +0200724 } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
Davide Carattiee3bbfe2019-03-20 15:00:16 +0100725 if (unlikely(!rcu_access_pointer(a->goto_chain))) {
726 net_warn_ratelimited("can't go to NULL chain!\n");
727 return TC_ACT_SHOT;
728 }
Jiri Pirkodb505142017-05-17 11:08:03 +0200729 tcf_action_goto_chain_exec(a, res);
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400730 }
731
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500732 if (ret != TC_ACT_PIPE)
Willem de Bruijne7246e12017-01-07 17:06:35 -0500733 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return ret;
737}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800738EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Vlad Buslov90b73b72018-07-05 17:24:33 +0300740int tcf_action_destroy(struct tc_action *actions[], int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Jiri Pirko255cd502017-09-13 17:32:37 +0200742 const struct tc_action_ops *ops;
Vlad Buslov90b73b72018-07-05 17:24:33 +0300743 struct tc_action *a;
744 int ret = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Vlad Buslov90b73b72018-07-05 17:24:33 +0300746 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
747 a = actions[i];
748 actions[i] = NULL;
Jiri Pirko255cd502017-09-13 17:32:37 +0200749 ops = a->ops;
Chris Mi65a206c2017-08-30 02:31:59 -0400750 ret = __tcf_idr_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800751 if (ret == ACT_P_DELETED)
Jiri Pirko255cd502017-09-13 17:32:37 +0200752 module_put(ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800753 else if (ret < 0)
754 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
WANG Cong55334a52014-02-11 17:07:34 -0800756 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Vlad Buslov16af6062018-07-05 17:24:29 +0300759static int tcf_action_put(struct tc_action *p)
760{
761 return __tcf_action_put(p, false);
762}
763
Cong Wangedfaf942018-08-19 12:22:05 -0700764/* Put all actions in this array, skip those NULL's. */
Vlad Buslov90b73b72018-07-05 17:24:33 +0300765static void tcf_action_put_many(struct tc_action *actions[])
Vlad Buslovcae422f2018-07-05 17:24:31 +0300766{
Vlad Buslov90b73b72018-07-05 17:24:33 +0300767 int i;
Vlad Buslovcae422f2018-07-05 17:24:31 +0300768
Cong Wangedfaf942018-08-19 12:22:05 -0700769 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
Vlad Buslov90b73b72018-07-05 17:24:33 +0300770 struct tc_action *a = actions[i];
Cong Wangedfaf942018-08-19 12:22:05 -0700771 const struct tc_action_ops *ops;
Vlad Buslovcae422f2018-07-05 17:24:31 +0300772
Cong Wangedfaf942018-08-19 12:22:05 -0700773 if (!a)
774 continue;
775 ops = a->ops;
Vlad Buslovcae422f2018-07-05 17:24:31 +0300776 if (tcf_action_put(a))
777 module_put(ops->owner);
778 }
779}
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781int
782tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
783{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return a->ops->dump(skb, a, bind, ref);
785}
786
Vlad Buslovca44b732020-05-15 14:40:12 +0300787int
788tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
789{
790 int err = -EINVAL;
791 unsigned char *b = skb_tail_pointer(skb);
792 struct nlattr *nest;
793
Vlad Buslov94f44f22020-11-02 22:12:43 +0200794 if (tcf_action_dump_terse(skb, a, false))
Vlad Buslovca44b732020-05-15 14:40:12 +0300795 goto nla_put_failure;
796
Jiri Pirko8953b072020-03-28 16:37:42 +0100797 if (a->hw_stats != TCA_ACT_HW_STATS_ANY &&
798 nla_put_bitfield32(skb, TCA_ACT_HW_STATS,
799 a->hw_stats, TCA_ACT_HW_STATS_ANY))
800 goto nla_put_failure;
Jiri Pirko44f86582020-03-07 12:40:20 +0100801
Jiri Pirko93a129e2020-03-28 16:37:43 +0100802 if (a->used_hw_stats_valid &&
803 nla_put_bitfield32(skb, TCA_ACT_USED_HW_STATS,
804 a->used_hw_stats, TCA_ACT_HW_STATS_ANY))
805 goto nla_put_failure;
806
Jiri Pirko8953b072020-03-28 16:37:42 +0100807 if (a->tcfa_flags &&
808 nla_put_bitfield32(skb, TCA_ACT_FLAGS,
809 a->tcfa_flags, a->tcfa_flags))
810 goto nla_put_failure;
Vlad Buslove3822672019-10-30 16:09:06 +0200811
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200812 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800813 if (nest == NULL)
814 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000815 err = tcf_action_dump_old(skb, a, bind, ref);
816 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800817 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return err;
819 }
820
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800821nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700822 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return -1;
824}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800825EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Vlad Buslov90b73b72018-07-05 17:24:33 +0300827int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[],
Vlad Buslovca44b732020-05-15 14:40:12 +0300828 int bind, int ref, bool terse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 struct tc_action *a;
Vlad Buslov90b73b72018-07-05 17:24:33 +0300831 int err = -EINVAL, i;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800832 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Vlad Buslov90b73b72018-07-05 17:24:33 +0300834 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
835 a = actions[i];
Vlad Buslov4097e9d22019-05-23 09:32:31 +0300836 nest = nla_nest_start_noflag(skb, i + 1);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800837 if (nest == NULL)
838 goto nla_put_failure;
Vlad Buslov94f44f22020-11-02 22:12:43 +0200839 err = terse ? tcf_action_dump_terse(skb, a, false) :
Vlad Buslovca44b732020-05-15 14:40:12 +0300840 tcf_action_dump_1(skb, a, bind, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700842 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800843 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845
846 return 0;
847
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800848nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700849 err = -EINVAL;
850errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800851 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700852 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200855static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500856{
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200857 struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
858 if (!c)
859 return NULL;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500860
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200861 c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
862 if (!c->data) {
863 kfree(c);
864 return NULL;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500865 }
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200866 c->len = nla_len(tb[TCA_ACT_COOKIE]);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500867
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200868 return c;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500869}
870
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -0700871static u8 tcf_action_hw_stats_get(struct nlattr *hw_stats_attr)
Jiri Pirko44f86582020-03-07 12:40:20 +0100872{
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -0700873 struct nla_bitfield32 hw_stats_bf;
Jiri Pirko44f86582020-03-07 12:40:20 +0100874
875 /* If the user did not pass the attr, that means he does
876 * not care about the type. Return "any" in that case
877 * which is setting on all supported types.
878 */
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -0700879 if (!hw_stats_attr)
880 return TCA_ACT_HW_STATS_ANY;
881 hw_stats_bf = nla_get_bitfield32(hw_stats_attr);
882 return hw_stats_bf.value;
Jiri Pirko44f86582020-03-07 12:40:20 +0100883}
884
Cong Wang199ce852019-09-18 18:44:43 -0700885static const struct nla_policy tcf_action_policy[TCA_ACT_MAX + 1] = {
Cong Wang4b793fe2019-10-07 13:26:29 -0700886 [TCA_ACT_KIND] = { .type = NLA_STRING },
Cong Wang199ce852019-09-18 18:44:43 -0700887 [TCA_ACT_INDEX] = { .type = NLA_U32 },
888 [TCA_ACT_COOKIE] = { .type = NLA_BINARY,
889 .len = TC_COOKIE_MAX_SIZE },
890 [TCA_ACT_OPTIONS] = { .type = NLA_NESTED },
Johannes Berg47a14942020-04-30 22:13:05 +0200891 [TCA_ACT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_ACT_FLAGS_NO_PERCPU_STATS),
892 [TCA_ACT_HW_STATS] = NLA_POLICY_BITFIELD32(TCA_ACT_HW_STATS_ANY),
Cong Wang199ce852019-09-18 18:44:43 -0700893};
894
Cong Wang0fedc632020-09-22 20:56:24 -0700895static void tcf_idr_insert_many(struct tc_action *actions[])
Cong Wange49d8c22020-09-22 20:56:23 -0700896{
Cong Wang0fedc632020-09-22 20:56:24 -0700897 int i;
Cong Wange49d8c22020-09-22 20:56:23 -0700898
Cong Wang0fedc632020-09-22 20:56:24 -0700899 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
900 struct tc_action *a = actions[i];
901 struct tcf_idrinfo *idrinfo;
902
903 if (!a)
904 continue;
905 idrinfo = a->idrinfo;
906 mutex_lock(&idrinfo->lock);
907 /* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc if
908 * it is just created, otherwise this is just a nop.
909 */
910 idr_replace(&idrinfo->action_idr, a, a->tcfa_index);
911 mutex_unlock(&idrinfo->lock);
912 }
Cong Wange49d8c22020-09-22 20:56:23 -0700913}
914
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200915struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
916 struct nlattr *nla, struct nlattr *est,
Alexander Aringaea0d722018-02-15 10:54:54 -0500917 char *name, int ovr, int bind,
Vlad Buslov789871b2018-07-05 17:24:25 +0300918 bool rtnl_held,
Alexander Aringaea0d722018-02-15 10:54:54 -0500919 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Vlad Buslovabbb0d32019-10-30 16:09:05 +0200921 struct nla_bitfield32 flags = { 0, 0 };
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -0700922 u8 hw_stats = TCA_ACT_HW_STATS_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 struct tc_action *a;
924 struct tc_action_ops *a_o;
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200925 struct tc_cookie *cookie = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000927 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800928 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800929 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (name == NULL) {
Cong Wang199ce852019-09-18 18:44:43 -0700932 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
933 tcf_action_policy, extack);
Patrick McHardycee63722008-01-23 20:33:32 -0800934 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800936 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800937 kind = tb[TCA_ACT_KIND];
Alexander Aring84ae0172018-02-15 10:54:55 -0500938 if (!kind) {
939 NL_SET_ERR_MSG(extack, "TC action kind must be specified");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -0500941 }
Francis Laniel872f6902020-11-15 18:08:06 +0100942 if (nla_strscpy(act_name, kind, IFNAMSIZ) < 0) {
Cong Wang4b793fe2019-10-07 13:26:29 -0700943 NL_SET_ERR_MSG(extack, "TC action name too long");
944 goto err_out;
945 }
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200946 if (tb[TCA_ACT_COOKIE]) {
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200947 cookie = nla_memdup_cookie(tb);
948 if (!cookie) {
Alexander Aring84ae0172018-02-15 10:54:55 -0500949 NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200950 err = -ENOMEM;
951 goto err_out;
952 }
953 }
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -0700954 hw_stats = tcf_action_hw_stats_get(tb[TCA_ACT_HW_STATS]);
Vlad Buslovabbb0d32019-10-30 16:09:05 +0200955 if (tb[TCA_ACT_FLAGS])
956 flags = nla_get_bitfield32(tb[TCA_ACT_FLAGS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 } else {
Alexander Aring84ae0172018-02-15 10:54:55 -0500958 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) {
959 NL_SET_ERR_MSG(extack, "TC action name too long");
960 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -0500962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964
965 a_o = tc_lookup_action_n(act_name);
966 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700967#ifdef CONFIG_MODULES
Vlad Buslov789871b2018-07-05 17:24:25 +0300968 if (rtnl_held)
969 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800970 request_module("act_%s", act_name);
Vlad Buslov789871b2018-07-05 17:24:25 +0300971 if (rtnl_held)
972 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 a_o = tc_lookup_action_n(act_name);
975
976 /* We dropped the RTNL semaphore in order to
977 * perform the module load. So, even if we
978 * succeeded in loading the module we have to
979 * tell the caller to replay the request. We
980 * indicate this using -EAGAIN.
981 */
982 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800983 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 goto err_mod;
985 }
986#endif
Alexander Aring84ae0172018-02-15 10:54:55 -0500987 NL_SET_ERR_MSG(extack, "Failed to load TC action module");
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800988 err = -ENOENT;
Tom Rixc1f1f162020-09-07 11:04:38 -0700989 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 /* backward compatibility for policer */
993 if (name == NULL)
Alexander Aring589dad62018-02-15 10:54:56 -0500994 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind,
Vlad Buslovabbb0d32019-10-30 16:09:05 +0200995 rtnl_held, tp, flags.value, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 else
Vlad Buslov789871b2018-07-05 17:24:25 +0300997 err = a_o->init(net, nla, est, &a, ovr, bind, rtnl_held,
Vlad Buslovabbb0d32019-10-30 16:09:05 +0200998 tp, flags.value, extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800999 if (err < 0)
WANG Conga85a9702016-07-25 16:09:41 -07001000 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Vlad Busloveec94fd2018-07-05 17:24:23 +03001002 if (!name && tb[TCA_ACT_COOKIE])
1003 tcf_set_action_cookie(&a->act_cookie, cookie);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -05001004
Jiri Pirko44f86582020-03-07 12:40:20 +01001005 if (!name)
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -07001006 a->hw_stats = hw_stats;
Jiri Pirko44f86582020-03-07 12:40:20 +01001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001009 * if it exists and is only bound to in a_o->init() then
1010 * ACT_P_CREATED is not returned (a zero is).
1011 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001012 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return a;
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017err_mod:
1018 module_put(a_o->owner);
Tom Rixc1f1f162020-09-07 11:04:38 -07001019err_free:
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +02001020 if (cookie) {
1021 kfree(cookie->data);
1022 kfree(cookie);
1023 }
Tom Rixc1f1f162020-09-07 11:04:38 -07001024err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001025 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
Vlad Buslov90b73b72018-07-05 17:24:33 +03001028/* Returns numbers of initialized actions or negative error. */
1029
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001030int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
1031 struct nlattr *est, char *name, int ovr, int bind,
Vlad Buslov90b73b72018-07-05 17:24:33 +03001032 struct tc_action *actions[], size_t *attr_size,
Vlad Buslov789871b2018-07-05 17:24:25 +03001033 bool rtnl_held, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001035 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -08001036 struct tc_action *act;
Roman Mashak4e76e752018-03-08 16:59:19 -05001037 size_t sz = 0;
Patrick McHardycee63722008-01-23 20:33:32 -08001038 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 int i;
1040
Johannes Berg8cb08172019-04-26 14:07:28 +02001041 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
1042 extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001043 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -08001044 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001046 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Alexander Aringaea0d722018-02-15 10:54:54 -05001047 act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind,
Vlad Buslov789871b2018-07-05 17:24:25 +03001048 rtnl_held, extack);
WANG Cong33be6272013-12-15 20:15:05 -08001049 if (IS_ERR(act)) {
1050 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 goto err;
WANG Cong33be6272013-12-15 20:15:05 -08001052 }
Roman Mashak4e76e752018-03-08 16:59:19 -05001053 sz += tcf_action_fill_size(act);
Vlad Buslov90b73b72018-07-05 17:24:33 +03001054 /* Start from index 0 */
1055 actions[i - 1] = act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -04001057
Cong Wang0fedc632020-09-22 20:56:24 -07001058 /* We have to commit them all together, because if any error happened in
1059 * between, we could not handle the failure gracefully.
1060 */
1061 tcf_idr_insert_many(actions);
1062
Roman Mashak4e76e752018-03-08 16:59:19 -05001063 *attr_size = tcf_action_full_attrs_size(sz);
Vlad Buslov90b73b72018-07-05 17:24:33 +03001064 return i - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066err:
WANG Cong33be6272013-12-15 20:15:05 -08001067 tcf_action_destroy(actions, bind);
1068 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
Po Liu4b61d3e2020-06-19 14:01:07 +08001071void tcf_action_update_stats(struct tc_action *a, u64 bytes, u64 packets,
1072 u64 drops, bool hw)
Vlad Buslovc8ecebd2019-10-30 16:09:00 +02001073{
Vlad Buslov5e174d52019-10-30 16:09:04 +02001074 if (a->cpu_bstats) {
1075 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
Vlad Buslovc8ecebd2019-10-30 16:09:00 +02001076
Po Liu4b61d3e2020-06-19 14:01:07 +08001077 this_cpu_ptr(a->cpu_qstats)->drops += drops;
Vlad Buslov5e174d52019-10-30 16:09:04 +02001078
1079 if (hw)
1080 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats_hw),
1081 bytes, packets);
1082 return;
1083 }
1084
1085 _bstats_update(&a->tcfa_bstats, bytes, packets);
Po Liu4b61d3e2020-06-19 14:01:07 +08001086 a->tcfa_qstats.drops += drops;
Vlad Buslovc8ecebd2019-10-30 16:09:00 +02001087 if (hw)
Vlad Buslov5e174d52019-10-30 16:09:04 +02001088 _bstats_update(&a->tcfa_bstats_hw, bytes, packets);
Vlad Buslovc8ecebd2019-10-30 16:09:00 +02001089}
1090EXPORT_SYMBOL(tcf_action_update_stats);
1091
WANG Congec0595c2016-07-25 16:09:42 -07001092int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 int compat_mode)
1094{
1095 int err = 0;
1096 struct gnet_dump d;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001097
WANG Cong7eb88962014-01-09 16:14:05 -08001098 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 goto errout;
1100
1101 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -04001102 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 */
1104 if (compat_mode) {
WANG Congec0595c2016-07-25 16:09:42 -07001105 if (p->type == TCA_OLD_COMPAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 err = gnet_stats_start_copy_compat(skb, 0,
Nicolas Dichtel9854518e2016-04-26 10:06:18 +02001107 TCA_STATS,
1108 TCA_XSTATS,
WANG Congec0595c2016-07-25 16:09:42 -07001109 &p->tcfa_lock, &d,
Nicolas Dichtel9854518e2016-04-26 10:06:18 +02001110 TCA_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 else
1112 return 0;
1113 } else
1114 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Congec0595c2016-07-25 16:09:42 -07001115 &p->tcfa_lock, &d, TCA_ACT_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 if (err < 0)
1118 goto errout;
1119
WANG Congec0595c2016-07-25 16:09:42 -07001120 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
Eelco Chaudron28169ab2018-09-21 07:14:02 -04001121 gnet_stats_copy_basic_hw(NULL, &d, p->cpu_bstats_hw,
1122 &p->tcfa_bstats_hw) < 0 ||
Eric Dumazet1c0d32f2016-12-04 09:48:16 -08001123 gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -07001124 gnet_stats_copy_queue(&d, p->cpu_qstats,
WANG Congec0595c2016-07-25 16:09:42 -07001125 &p->tcfa_qstats,
1126 p->tcfa_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 goto errout;
1128
1129 if (gnet_stats_finish_copy(&d) < 0)
1130 goto errout;
1131
1132 return 0;
1133
1134errout:
1135 return -1;
1136}
1137
Vlad Buslov90b73b72018-07-05 17:24:33 +03001138static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[],
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001139 u32 portid, u32 seq, u16 flags, int event, int bind,
1140 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
1142 struct tcamsg *t;
1143 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001144 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001145 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Eric W. Biederman15e47302012-09-07 20:12:54 +00001147 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -07001148 if (!nlh)
1149 goto out_nlmsg_trim;
1150 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001152 t->tca__pad1 = 0;
1153 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001154
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001155 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
Alexander Aring1af8515582018-02-15 10:54:53 -05001156 if (!nest)
David S. Miller8b00a532012-06-26 21:39:32 -07001157 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Vlad Buslovca44b732020-05-15 14:40:12 +03001159 if (tcf_action_dump(skb, actions, bind, ref, false) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001160 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001162 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001163
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001164 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return skb->len;
1166
David S. Miller8b00a532012-06-26 21:39:32 -07001167out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001168 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return -1;
1170}
1171
1172static int
Roman Mashakc4c42902017-07-13 13:12:18 -04001173tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
Vlad Buslov90b73b72018-07-05 17:24:33 +03001174 struct tc_action *actions[], int event,
Alexander Aring84ae0172018-02-15 10:54:55 -05001175 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1180 if (!skb)
1181 return -ENOBUFS;
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001182 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
Vlad Buslov3f7c72b2018-07-05 17:24:26 +03001183 0, 1) <= 0) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001184 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 kfree_skb(skb);
1186 return -EINVAL;
1187 }
Thomas Graf2942e902006-08-15 00:30:25 -07001188
Eric W. Biederman15e47302012-09-07 20:12:54 +00001189 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
WANG Congddf97cc2016-02-22 15:57:53 -08001192static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
Alexander Aring84ae0172018-02-15 10:54:55 -05001193 struct nlmsghdr *n, u32 portid,
1194 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001196 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -07001197 const struct tc_action_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 struct tc_action *a;
1199 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001200 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Cong Wang199ce852019-09-18 18:44:43 -07001202 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1203 tcf_action_policy, extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001204 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001205 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Patrick McHardycee63722008-01-23 20:33:32 -08001207 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001208 if (tb[TCA_ACT_INDEX] == NULL ||
Alexander Aring84ae0172018-02-15 10:54:55 -05001209 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
1210 NL_SET_ERR_MSG(extack, "Invalid TC action index value");
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001211 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -05001212 }
Patrick McHardy1587bac2008-01-23 20:35:03 -08001213 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001215 err = -EINVAL;
WANG Conga85a9702016-07-25 16:09:41 -07001216 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Alexander Aring84ae0172018-02-15 10:54:55 -05001217 if (!ops) { /* could happen in batch of actions */
Cong Wangf061b482018-08-29 10:15:35 -07001218 NL_SET_ERR_MSG(extack, "Specified TC action kind not found");
WANG Conga85a9702016-07-25 16:09:41 -07001219 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -05001220 }
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001221 err = -ENOENT;
Cong Wangf061b482018-08-29 10:15:35 -07001222 if (ops->lookup(net, &a, index) == 0) {
1223 NL_SET_ERR_MSG(extack, "TC action with specified index not found");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 goto err_mod;
Cong Wangf061b482018-08-29 10:15:35 -07001225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
WANG Conga85a9702016-07-25 16:09:41 -07001227 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230err_mod:
WANG Conga85a9702016-07-25 16:09:41 -07001231 module_put(ops->owner);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001232err_out:
1233 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
Tom Goff7316ae82010-03-19 15:40:13 +00001236static int tca_action_flush(struct net *net, struct nlattr *nla,
Alexander Aring84ae0172018-02-15 10:54:55 -05001237 struct nlmsghdr *n, u32 portid,
1238 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 struct sk_buff *skb;
1241 unsigned char *b;
1242 struct nlmsghdr *nlh;
1243 struct tcamsg *t;
1244 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001245 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001246 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -07001247 const struct tc_action_ops *ops;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001248 struct nlattr *kind;
Jamal Hadi Salim36723872008-08-13 02:41:45 -07001249 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
Alexander Aring84ae0172018-02-15 10:54:55 -05001252 if (!skb)
Jamal Hadi Salim36723872008-08-13 02:41:45 -07001253 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001255 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Cong Wang199ce852019-09-18 18:44:43 -07001257 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1258 tcf_action_policy, extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001259 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 goto err_out;
1261
Patrick McHardycee63722008-01-23 20:33:32 -08001262 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001263 kind = tb[TCA_ACT_KIND];
WANG Conga85a9702016-07-25 16:09:41 -07001264 ops = tc_lookup_action(kind);
Alexander Aring84ae0172018-02-15 10:54:55 -05001265 if (!ops) { /*some idjot trying to flush unknown action */
1266 NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -05001268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001270 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
1271 sizeof(*t), 0);
Alexander Aring84ae0172018-02-15 10:54:55 -05001272 if (!nlh) {
1273 NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
David S. Miller8b00a532012-06-26 21:39:32 -07001274 goto out_module_put;
Alexander Aring84ae0172018-02-15 10:54:55 -05001275 }
David S. Miller8b00a532012-06-26 21:39:32 -07001276 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001278 t->tca__pad1 = 0;
1279 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001281 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
Alexander Aring84ae0172018-02-15 10:54:55 -05001282 if (!nest) {
1283 NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
David S. Miller8b00a532012-06-26 21:39:32 -07001284 goto out_module_put;
Alexander Aring84ae0172018-02-15 10:54:55 -05001285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Alexander Aring41780102018-02-15 10:54:58 -05001287 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack);
Davide Caratti66dede22018-02-15 15:50:57 +01001288 if (err <= 0) {
1289 nla_nest_cancel(skb, nest);
David S. Miller8b00a532012-06-26 21:39:32 -07001290 goto out_module_put;
Davide Caratti66dede22018-02-15 15:50:57 +01001291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001293 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001295 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Conga85a9702016-07-25 16:09:41 -07001297 module_put(ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001298 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001299 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (err > 0)
1301 return 0;
Alexander Aring84ae0172018-02-15 10:54:55 -05001302 if (err < 0)
1303 NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 return err;
1306
David S. Miller8b00a532012-06-26 21:39:32 -07001307out_module_put:
WANG Conga85a9702016-07-25 16:09:41 -07001308 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309err_out:
1310 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return err;
1312}
1313
Cong Wangb144e7e2018-08-19 12:22:07 -07001314static int tcf_action_delete(struct net *net, struct tc_action *actions[])
Vlad Buslov16af6062018-07-05 17:24:29 +03001315{
Cong Wang97a3f84f2018-08-19 12:22:06 -07001316 int i;
Vlad Buslov16af6062018-07-05 17:24:29 +03001317
Vlad Buslov90b73b72018-07-05 17:24:33 +03001318 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
1319 struct tc_action *a = actions[i];
Vlad Buslov16af6062018-07-05 17:24:29 +03001320 const struct tc_action_ops *ops = a->ops;
Vlad Buslov16af6062018-07-05 17:24:29 +03001321 /* Actions can be deleted concurrently so we must save their
1322 * type and id to search again after reference is released.
1323 */
Cong Wang97a3f84f2018-08-19 12:22:06 -07001324 struct tcf_idrinfo *idrinfo = a->idrinfo;
1325 u32 act_index = a->tcfa_index;
Vlad Buslov16af6062018-07-05 17:24:29 +03001326
Vlad Buslovc10bbfa2018-09-03 10:04:55 +03001327 actions[i] = NULL;
Vlad Buslov16af6062018-07-05 17:24:29 +03001328 if (tcf_action_put(a)) {
1329 /* last reference, action was deleted concurrently */
1330 module_put(ops->owner);
1331 } else {
Cong Wang97a3f84f2018-08-19 12:22:06 -07001332 int ret;
1333
Vlad Buslov16af6062018-07-05 17:24:29 +03001334 /* now do the delete */
Cong Wang97a3f84f2018-08-19 12:22:06 -07001335 ret = tcf_idr_delete_index(idrinfo, act_index);
Cong Wangedfaf942018-08-19 12:22:05 -07001336 if (ret < 0)
Vlad Buslov16af6062018-07-05 17:24:29 +03001337 return ret;
1338 }
1339 }
1340 return 0;
1341}
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343static int
Vlad Buslov90b73b72018-07-05 17:24:33 +03001344tcf_del_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
Cong Wangedfaf942018-08-19 12:22:05 -07001345 u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
WANG Conga56e1952014-01-09 16:14:00 -08001346{
1347 int ret;
1348 struct sk_buff *skb;
1349
Roman Mashakd04e6992018-03-08 16:59:17 -05001350 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1351 GFP_KERNEL);
WANG Conga56e1952014-01-09 16:14:00 -08001352 if (!skb)
1353 return -ENOBUFS;
1354
1355 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
Vlad Buslov3f7c72b2018-07-05 17:24:26 +03001356 0, 2) <= 0) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001357 NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
WANG Conga56e1952014-01-09 16:14:00 -08001358 kfree_skb(skb);
1359 return -EINVAL;
1360 }
1361
1362 /* now do the delete */
Cong Wangb144e7e2018-08-19 12:22:07 -07001363 ret = tcf_action_delete(net, actions);
WANG Cong55334a52014-02-11 17:07:34 -08001364 if (ret < 0) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001365 NL_SET_ERR_MSG(extack, "Failed to delete TC action");
WANG Cong55334a52014-02-11 17:07:34 -08001366 kfree_skb(skb);
1367 return ret;
1368 }
WANG Conga56e1952014-01-09 16:14:00 -08001369
1370 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1371 n->nlmsg_flags & NLM_F_ECHO);
1372 if (ret > 0)
1373 return 0;
1374 return ret;
1375}
1376
1377static int
Tom Goff7316ae82010-03-19 15:40:13 +00001378tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Alexander Aring84ae0172018-02-15 10:54:55 -05001379 u32 portid, int event, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Patrick McHardycee63722008-01-23 20:33:32 -08001381 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001382 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -08001383 struct tc_action *act;
Roman Mashakd04e6992018-03-08 16:59:17 -05001384 size_t attr_size = 0;
Cong Wangedfaf942018-08-19 12:22:05 -07001385 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Johannes Berg8cb08172019-04-26 14:07:28 +02001387 ret = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
1388 extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001389 if (ret < 0)
1390 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001392 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Alexander Aring1af8515582018-02-15 10:54:53 -05001393 if (tb[1])
Alexander Aring84ae0172018-02-15 10:54:55 -05001394 return tca_action_flush(net, tb[1], n, portid, extack);
Alexander Aring1af8515582018-02-15 10:54:53 -05001395
Alexander Aring84ae0172018-02-15 10:54:55 -05001396 NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
Alexander Aring1af8515582018-02-15 10:54:53 -05001397 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001400 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001401 act = tcf_action_get_1(net, tb[i], n, portid, extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001402 if (IS_ERR(act)) {
1403 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001405 }
Roman Mashak4e76e752018-03-08 16:59:19 -05001406 attr_size += tcf_action_fill_size(act);
Vlad Buslov90b73b72018-07-05 17:24:33 +03001407 actions[i - 1] = act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409
Roman Mashak4e76e752018-03-08 16:59:19 -05001410 attr_size = tcf_action_full_attrs_size(attr_size);
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (event == RTM_GETACTION)
Vlad Buslov90b73b72018-07-05 17:24:33 +03001413 ret = tcf_get_notify(net, portid, n, actions, event, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 else { /* delete */
Cong Wangedfaf942018-08-19 12:22:05 -07001415 ret = tcf_del_notify(net, n, actions, portid, attr_size, extack);
WANG Conga56e1952014-01-09 16:14:00 -08001416 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 goto err;
Cong Wangedfaf942018-08-19 12:22:05 -07001418 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420err:
Cong Wangedfaf942018-08-19 12:22:05 -07001421 tcf_action_put_many(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return ret;
1423}
1424
WANG Conga56e1952014-01-09 16:14:00 -08001425static int
Vlad Buslov90b73b72018-07-05 17:24:33 +03001426tcf_add_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
Roman Mashakd04e6992018-03-08 16:59:17 -05001427 u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 int err = 0;
1431
Roman Mashakd04e6992018-03-08 16:59:17 -05001432 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1433 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (!skb)
1435 return -ENOBUFS;
1436
WANG Conga56e1952014-01-09 16:14:00 -08001437 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
1438 RTM_NEWACTION, 0, 0) <= 0) {
Roman Mashakd143b9e2018-03-02 20:52:01 -05001439 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
WANG Conga56e1952014-01-09 16:14:00 -08001440 kfree_skb(skb);
1441 return -EINVAL;
1442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
WANG Conga56e1952014-01-09 16:14:00 -08001444 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1445 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (err > 0)
1447 err = 0;
1448 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001451static int tcf_action_add(struct net *net, struct nlattr *nla,
Alexander Aringaea0d722018-02-15 10:54:54 -05001452 struct nlmsghdr *n, u32 portid, int ovr,
1453 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
Roman Mashakd04e6992018-03-08 16:59:17 -05001455 size_t attr_size = 0;
Eric Dumazet39f13ea2019-10-14 11:22:30 -07001456 int loop, ret;
Vlad Buslov90b73b72018-07-05 17:24:33 +03001457 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Eric Dumazet39f13ea2019-10-14 11:22:30 -07001459 for (loop = 0; loop < 10; loop++) {
1460 ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0,
1461 actions, &attr_size, true, extack);
1462 if (ret != -EAGAIN)
1463 break;
1464 }
1465
Vlad Buslov90b73b72018-07-05 17:24:33 +03001466 if (ret < 0)
WANG Congf07fed82016-08-13 22:34:56 -07001467 return ret;
Vlad Buslov90b73b72018-07-05 17:24:33 +03001468 ret = tcf_add_notify(net, n, actions, portid, attr_size, extack);
Vlad Buslovcae422f2018-07-05 17:24:31 +03001469 if (ovr)
Vlad Buslov90b73b72018-07-05 17:24:33 +03001470 tcf_action_put_many(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Vlad Buslovcae422f2018-07-05 17:24:31 +03001472 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
1474
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001475static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
Vlad Buslovf4600192020-11-24 18:40:54 +02001476 [TCA_ROOT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_ACT_FLAG_LARGE_DUMP_ON |
1477 TCA_ACT_FLAG_TERSE_DUMP),
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001478 [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001479};
1480
David Ahernc21ef3e2017-04-16 09:48:24 -07001481static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
1482 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001484 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001485 struct nlattr *tca[TCA_ROOT_MAX + 1];
Gaurav Singh8bf15392020-06-19 15:24:13 -04001486 u32 portid = NETLINK_CB(skb).portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 int ret = 0, ovr = 0;
1488
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001489 if ((n->nlmsg_type != RTM_GETACTION) &&
1490 !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001491 return -EPERM;
1492
Johannes Berg8cb08172019-04-26 14:07:28 +02001493 ret = nlmsg_parse_deprecated(n, sizeof(struct tcamsg), tca,
1494 TCA_ROOT_MAX, NULL, extack);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001495 if (ret < 0)
1496 return ret;
1497
1498 if (tca[TCA_ACT_TAB] == NULL) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001499 NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return -EINVAL;
1501 }
1502
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001503 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 switch (n->nlmsg_type) {
1505 case RTM_NEWACTION:
1506 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001507 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 * Note that CREATE | EXCL implies that
1509 * but since we want avoid ambiguity (eg when flags
1510 * is zero) then just set this
1511 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001512 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 ovr = 1;
Alexander Aringaea0d722018-02-15 10:54:54 -05001514 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr,
1515 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 break;
1517 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001518 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Alexander Aring84ae0172018-02-15 10:54:55 -05001519 portid, RTM_DELACTION, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 break;
1521 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001522 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Alexander Aring84ae0172018-02-15 10:54:55 -05001523 portid, RTM_GETACTION, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 break;
1525 default:
1526 BUG();
1527 }
1528
1529 return ret;
1530}
1531
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001532static struct nlattr *find_dump_kind(struct nlattr **nla)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001534 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001535 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001536 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001538 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (tb1 == NULL)
1540 return NULL;
1541
Johannes Berg8cb08172019-04-26 14:07:28 +02001542 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 -07001543 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Patrick McHardy6d834e02008-01-23 20:32:42 -08001545 if (tb[1] == NULL)
1546 return NULL;
Cong Wang199ce852019-09-18 18:44:43 -07001547 if (nla_parse_nested_deprecated(tb2, TCA_ACT_MAX, tb[1], tcf_action_policy, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001549 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Thomas Graf26dab892006-07-05 20:45:06 -07001551 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
1553
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001554static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
WANG Congddf97cc2016-02-22 15:57:53 -08001556 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001558 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001559 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 struct tc_action_ops *a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001562 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001563 struct nlattr *tb[TCA_ROOT_MAX + 1];
1564 struct nlattr *count_attr = NULL;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001565 unsigned long jiffy_since = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001566 struct nlattr *kind = NULL;
1567 struct nla_bitfield32 bf;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001568 u32 msecs_since = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001569 u32 act_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Johannes Berg8cb08172019-04-26 14:07:28 +02001571 ret = nlmsg_parse_deprecated(cb->nlh, sizeof(struct tcamsg), tb,
1572 TCA_ROOT_MAX, tcaa_policy, cb->extack);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001573 if (ret < 0)
1574 return ret;
1575
1576 kind = find_dump_kind(tb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001578 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 return 0;
1580 }
1581
Thomas Graf26dab892006-07-05 20:45:06 -07001582 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001583 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001586 cb->args[2] = 0;
1587 if (tb[TCA_ROOT_FLAGS]) {
1588 bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
1589 cb->args[2] = bf.value;
1590 }
1591
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001592 if (tb[TCA_ROOT_TIME_DELTA]) {
1593 msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
1594 }
1595
Eric W. Biederman15e47302012-09-07 20:12:54 +00001596 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001597 cb->nlh->nlmsg_type, sizeof(*t), 0);
1598 if (!nlh)
1599 goto out_module_put;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001600
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001601 if (msecs_since)
1602 jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
1603
David S. Miller8b00a532012-06-26 21:39:32 -07001604 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001606 t->tca__pad1 = 0;
1607 t->tca__pad2 = 0;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001608 cb->args[3] = jiffy_since;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001609 count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
1610 if (!count_attr)
1611 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001613 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001614 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001615 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Alexander Aring41780102018-02-15 10:54:58 -05001617 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001619 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001622 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 ret = skb->len;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001624 act_count = cb->args[1];
1625 memcpy(nla_data(count_attr), &act_count, sizeof(u32));
1626 cb->args[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 } else
Jamal Hadi Salimebecaa62016-06-13 18:08:42 -04001628 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001630 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001631 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 nlh->nlmsg_flags |= NLM_F_MULTI;
1633 module_put(a_o->owner);
1634 return skb->len;
1635
David S. Miller8b00a532012-06-26 21:39:32 -07001636out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001638 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 return skb->len;
1640}
1641
1642static int __init tc_action_init(void)
1643{
Florian Westphalb97bac62017-08-09 20:41:48 +02001644 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
1645 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
Greg Rosec7ac8672011-06-10 01:27:09 +00001646 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
Florian Westphalb97bac62017-08-09 20:41:48 +02001647 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 return 0;
1650}
1651
1652subsys_initcall(tc_action_init);