blob: d4b8355737d8f80b5304aa7aa6d73949a7b52c11 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sched/act_api.c Packet action API.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Author: Jamal Hadi Salim
10 *
11 *
12 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/types.h>
15#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/init.h>
21#include <linux/kmod.h>
Patrick McHardyab27cfb2008-01-23 20:33:13 -080022#include <linux/err.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040023#include <linux/module.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110024#include <net/net_namespace.h>
25#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <net/sch_generic.h>
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -050027#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <net/act_api.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070029#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Jiri Pirkodb505142017-05-17 11:08:03 +020031static int tcf_action_goto_chain_init(struct tc_action *a, struct tcf_proto *tp)
32{
33 u32 chain_index = a->tcfa_action & TC_ACT_EXT_VAL_MASK;
34
35 if (!tp)
36 return -EINVAL;
Jiri Pirko1f3ed382018-07-27 09:45:05 +020037 a->goto_chain = tcf_chain_get_by_act(tp->chain->block, chain_index);
Jiri Pirkodb505142017-05-17 11:08:03 +020038 if (!a->goto_chain)
39 return -ENOMEM;
40 return 0;
41}
42
43static void tcf_action_goto_chain_fini(struct tc_action *a)
44{
Jiri Pirko1f3ed382018-07-27 09:45:05 +020045 tcf_chain_put_by_act(a->goto_chain);
Jiri Pirkodb505142017-05-17 11:08:03 +020046}
47
48static void tcf_action_goto_chain_exec(const struct tc_action *a,
49 struct tcf_result *res)
50{
51 const struct tcf_chain *chain = a->goto_chain;
52
53 res->goto_tp = rcu_dereference_bh(chain->filter_chain);
54}
55
Vlad Busloveec94fd2018-07-05 17:24:23 +030056static void tcf_free_cookie_rcu(struct rcu_head *p)
57{
58 struct tc_cookie *cookie = container_of(p, struct tc_cookie, rcu);
59
60 kfree(cookie->data);
61 kfree(cookie);
62}
63
64static void tcf_set_action_cookie(struct tc_cookie __rcu **old_cookie,
65 struct tc_cookie *new_cookie)
66{
67 struct tc_cookie *old;
68
David S. Miller0dbc81e2018-07-08 17:02:59 +090069 old = xchg((__force struct tc_cookie **)old_cookie, new_cookie);
Vlad Busloveec94fd2018-07-05 17:24:23 +030070 if (old)
71 call_rcu(&old->rcu, tcf_free_cookie_rcu);
72}
73
Cong Wangd7fb60b2017-09-11 16:33:30 -070074/* XXX: For standalone actions, we don't need a RCU grace period either, because
75 * actions are always connected to filters and filters are already destroyed in
76 * RCU callbacks, so after a RCU grace period actions are already disconnected
77 * from filters. Readers later can not find us.
78 */
79static void free_tcf(struct tc_action *p)
Eric Dumazet519c8182015-07-06 05:18:04 -070080{
Eric Dumazet519c8182015-07-06 05:18:04 -070081 free_percpu(p->cpu_bstats);
Eelco Chaudron28169ab2018-09-21 07:14:02 -040082 free_percpu(p->cpu_bstats_hw);
Eric Dumazet519c8182015-07-06 05:18:04 -070083 free_percpu(p->cpu_qstats);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -050084
Vlad Busloveec94fd2018-07-05 17:24:23 +030085 tcf_set_action_cookie(&p->act_cookie, NULL);
Jiri Pirkodb505142017-05-17 11:08:03 +020086 if (p->goto_chain)
87 tcf_action_goto_chain_fini(p);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -050088
Eric Dumazet519c8182015-07-06 05:18:04 -070089 kfree(p);
90}
91
Vlad Buslov16af6062018-07-05 17:24:29 +030092static void tcf_action_cleanup(struct tc_action *p)
David S. Millere9ce1cd2006-08-21 23:54:55 -070093{
Vlad Buslov16af6062018-07-05 17:24:29 +030094 if (p->ops->cleanup)
95 p->ops->cleanup(p);
96
Eric Dumazet1c0d32f2016-12-04 09:48:16 -080097 gen_kill_estimator(&p->tcfa_rate_est);
Cong Wangd7fb60b2017-09-11 16:33:30 -070098 free_tcf(p);
David S. Millere9ce1cd2006-08-21 23:54:55 -070099}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700100
Vlad Buslov16af6062018-07-05 17:24:29 +0300101static int __tcf_action_put(struct tc_action *p, bool bind)
102{
103 struct tcf_idrinfo *idrinfo = p->idrinfo;
104
Cong Wang95278dd2018-10-02 12:50:19 -0700105 if (refcount_dec_and_mutex_lock(&p->tcfa_refcnt, &idrinfo->lock)) {
Vlad Buslov16af6062018-07-05 17:24:29 +0300106 if (bind)
107 atomic_dec(&p->tcfa_bindcnt);
108 idr_remove(&idrinfo->action_idr, p->tcfa_index);
Cong Wang95278dd2018-10-02 12:50:19 -0700109 mutex_unlock(&idrinfo->lock);
Vlad Buslov16af6062018-07-05 17:24:29 +0300110
111 tcf_action_cleanup(p);
112 return 1;
113 }
114
115 if (bind)
116 atomic_dec(&p->tcfa_bindcnt);
117
118 return 0;
119}
120
Chris Mi65a206c2017-08-30 02:31:59 -0400121int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700122{
123 int ret = 0;
124
Vlad Buslov036bb442018-07-05 17:24:24 +0300125 /* Release with strict==1 and bind==0 is only called through act API
126 * interface (classifiers always bind). Only case when action with
127 * positive reference count and zero bind count can exist is when it was
128 * also created with act API (unbinding last classifier will destroy the
129 * action if it was created by classifier). So only case when bind count
130 * can be changed after initial check is when unbound action is
131 * destroyed by act API while classifier binds to action with same id
132 * concurrently. This result either creation of new action(same behavior
133 * as before), or reusing existing action if concurrent process
134 * increments reference count before action is deleted. Both scenarios
135 * are acceptable.
136 */
David S. Millere9ce1cd2006-08-21 23:54:55 -0700137 if (p) {
Vlad Buslov16af6062018-07-05 17:24:29 +0300138 if (!bind && strict && atomic_read(&p->tcfa_bindcnt) > 0)
WANG Cong55334a52014-02-11 17:07:34 -0800139 return -EPERM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700140
Vlad Buslov16af6062018-07-05 17:24:29 +0300141 if (__tcf_action_put(p, bind))
WANG Cong1d4150c2016-02-22 15:57:52 -0800142 ret = ACT_P_DELETED;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700143 }
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200144
David S. Millere9ce1cd2006-08-21 23:54:55 -0700145 return ret;
146}
Chris Mi65a206c2017-08-30 02:31:59 -0400147EXPORT_SYMBOL(__tcf_idr_release);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700148
Roman Mashak4e76e752018-03-08 16:59:19 -0500149static size_t tcf_action_shared_attrs_size(const struct tc_action *act)
150{
Vlad Buslove0479b62018-07-09 20:26:47 +0300151 struct tc_cookie *act_cookie;
Roman Mashak4e76e752018-03-08 16:59:19 -0500152 u32 cookie_len = 0;
153
Vlad Buslove0479b62018-07-09 20:26:47 +0300154 rcu_read_lock();
155 act_cookie = rcu_dereference(act->act_cookie);
156
157 if (act_cookie)
158 cookie_len = nla_total_size(act_cookie->len);
159 rcu_read_unlock();
Roman Mashak4e76e752018-03-08 16:59:19 -0500160
161 return nla_total_size(0) /* action number nested */
162 + nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */
163 + cookie_len /* TCA_ACT_COOKIE */
164 + nla_total_size(0) /* TCA_ACT_STATS nested */
165 /* TCA_STATS_BASIC */
166 + nla_total_size_64bit(sizeof(struct gnet_stats_basic))
167 /* TCA_STATS_QUEUE */
168 + nla_total_size_64bit(sizeof(struct gnet_stats_queue))
169 + nla_total_size(0) /* TCA_OPTIONS nested */
170 + nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */
171}
172
173static size_t tcf_action_full_attrs_size(size_t sz)
174{
175 return NLMSG_HDRLEN /* struct nlmsghdr */
176 + sizeof(struct tcamsg)
177 + nla_total_size(0) /* TCA_ACT_TAB nested */
178 + sz;
179}
180
181static size_t tcf_action_fill_size(const struct tc_action *act)
182{
183 size_t sz = tcf_action_shared_attrs_size(act);
184
185 if (act->ops->get_fill_size)
186 return act->ops->get_fill_size(act) + sz;
187 return sz;
188}
189
Chris Mi65a206c2017-08-30 02:31:59 -0400190static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700191 struct netlink_callback *cb)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700192{
Chris Mi65a206c2017-08-30 02:31:59 -0400193 int err = 0, index = -1, s_i = 0, n_i = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400194 u32 act_flags = cb->args[2];
Jamal Hadi Salime62e4842017-07-30 13:24:52 -0400195 unsigned long jiffy_since = cb->args[3];
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800196 struct nlattr *nest;
Chris Mi65a206c2017-08-30 02:31:59 -0400197 struct idr *idr = &idrinfo->action_idr;
198 struct tc_action *p;
199 unsigned long id = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700200
Cong Wang95278dd2018-10-02 12:50:19 -0700201 mutex_lock(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700202
203 s_i = cb->args[0];
204
Matthew Wilcox7a457572017-11-28 15:39:51 -0500205 idr_for_each_entry_ul(idr, p, id) {
Chris Mi65a206c2017-08-30 02:31:59 -0400206 index++;
207 if (index < s_i)
208 continue;
WANG Conga85a9702016-07-25 16:09:41 -0700209
Chris Mi65a206c2017-08-30 02:31:59 -0400210 if (jiffy_since &&
211 time_after(jiffy_since,
212 (unsigned long)p->tcfa_tm.lastuse))
213 continue;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700214
Chris Mi65a206c2017-08-30 02:31:59 -0400215 nest = nla_nest_start(skb, n_i);
Craig Dillabaugh734549e2018-03-26 14:58:32 -0400216 if (!nest) {
217 index--;
Chris Mi65a206c2017-08-30 02:31:59 -0400218 goto nla_put_failure;
Craig Dillabaugh734549e2018-03-26 14:58:32 -0400219 }
Chris Mi65a206c2017-08-30 02:31:59 -0400220 err = tcf_action_dump_1(skb, p, 0, 0);
221 if (err < 0) {
222 index--;
223 nlmsg_trim(skb, nest);
224 goto done;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700225 }
Chris Mi65a206c2017-08-30 02:31:59 -0400226 nla_nest_end(skb, nest);
227 n_i++;
228 if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) &&
229 n_i >= TCA_ACT_MAX_PRIO)
230 goto done;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700231 }
232done:
Jamal Hadi Salime62e4842017-07-30 13:24:52 -0400233 if (index >= 0)
234 cb->args[0] = index + 1;
235
Cong Wang95278dd2018-10-02 12:50:19 -0700236 mutex_unlock(&idrinfo->lock);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400237 if (n_i) {
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400238 if (act_flags & TCA_FLAG_LARGE_DUMP_ON)
239 cb->args[1] = n_i;
240 }
David S. Millere9ce1cd2006-08-21 23:54:55 -0700241 return n_i;
242
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800243nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800244 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700245 goto done;
246}
247
Vlad Buslovec3ed292018-09-19 16:37:29 -0700248static int tcf_idr_release_unsafe(struct tc_action *p)
249{
250 if (atomic_read(&p->tcfa_bindcnt) > 0)
251 return -EPERM;
252
253 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
254 idr_remove(&p->idrinfo->action_idr, p->tcfa_index);
255 tcf_action_cleanup(p);
256 return ACT_P_DELETED;
257 }
258
259 return 0;
260}
261
Chris Mi65a206c2017-08-30 02:31:59 -0400262static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700263 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700264{
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800265 struct nlattr *nest;
Chris Mi65a206c2017-08-30 02:31:59 -0400266 int n_i = 0;
WANG Cong55334a52014-02-11 17:07:34 -0800267 int ret = -EINVAL;
Chris Mi65a206c2017-08-30 02:31:59 -0400268 struct idr *idr = &idrinfo->action_idr;
269 struct tc_action *p;
270 unsigned long id = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700271
WANG Conga85a9702016-07-25 16:09:41 -0700272 nest = nla_nest_start(skb, 0);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800273 if (nest == NULL)
274 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700275 if (nla_put_string(skb, TCA_KIND, ops->kind))
David S. Miller1b34ec42012-03-29 05:11:39 -0400276 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700277
Cong Wang95278dd2018-10-02 12:50:19 -0700278 mutex_lock(&idrinfo->lock);
Matthew Wilcox7a457572017-11-28 15:39:51 -0500279 idr_for_each_entry_ul(idr, p, id) {
Vlad Buslovec3ed292018-09-19 16:37:29 -0700280 ret = tcf_idr_release_unsafe(p);
Chris Mi65a206c2017-08-30 02:31:59 -0400281 if (ret == ACT_P_DELETED) {
Jiri Pirko255cd502017-09-13 17:32:37 +0200282 module_put(ops->owner);
Chris Mi65a206c2017-08-30 02:31:59 -0400283 n_i++;
284 } else if (ret < 0) {
Cong Wang95278dd2018-10-02 12:50:19 -0700285 mutex_unlock(&idrinfo->lock);
Chris Mi65a206c2017-08-30 02:31:59 -0400286 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700287 }
288 }
Cong Wang95278dd2018-10-02 12:50:19 -0700289 mutex_unlock(&idrinfo->lock);
Vlad Buslovec3ed292018-09-19 16:37:29 -0700290
David S. Miller1b34ec42012-03-29 05:11:39 -0400291 if (nla_put_u32(skb, TCA_FCNT, n_i))
292 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800293 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700294
295 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800296nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800297 nla_nest_cancel(skb, nest);
WANG Cong55334a52014-02-11 17:07:34 -0800298 return ret;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700299}
300
WANG Congddf97cc2016-02-22 15:57:53 -0800301int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
302 struct netlink_callback *cb, int type,
Alexander Aringb3620142018-02-15 10:54:59 -0500303 const struct tc_action_ops *ops,
304 struct netlink_ext_ack *extack)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700305{
Chris Mi65a206c2017-08-30 02:31:59 -0400306 struct tcf_idrinfo *idrinfo = tn->idrinfo;
WANG Congddf97cc2016-02-22 15:57:53 -0800307
David S. Millere9ce1cd2006-08-21 23:54:55 -0700308 if (type == RTM_DELACTION) {
Chris Mi65a206c2017-08-30 02:31:59 -0400309 return tcf_del_walker(idrinfo, skb, ops);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700310 } else if (type == RTM_GETACTION) {
Chris Mi65a206c2017-08-30 02:31:59 -0400311 return tcf_dump_walker(idrinfo, skb, cb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700312 } else {
Alexander Aringb3620142018-02-15 10:54:59 -0500313 WARN(1, "tcf_generic_walker: unknown command %d\n", type);
314 NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command");
David S. Millere9ce1cd2006-08-21 23:54:55 -0700315 return -EINVAL;
316 }
317}
WANG Congddf97cc2016-02-22 15:57:53 -0800318EXPORT_SYMBOL(tcf_generic_walker);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700319
Cong Wang7d485c42018-08-19 12:22:08 -0700320int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700321{
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300322 struct tcf_idrinfo *idrinfo = tn->idrinfo;
323 struct tc_action *p;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700324
Cong Wang95278dd2018-10-02 12:50:19 -0700325 mutex_lock(&idrinfo->lock);
Matthew Wilcox322d8842017-11-28 10:01:24 -0500326 p = idr_find(&idrinfo->action_idr, index);
Cong Wang7d485c42018-08-19 12:22:08 -0700327 if (IS_ERR(p))
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300328 p = NULL;
Cong Wang7d485c42018-08-19 12:22:08 -0700329 else if (p)
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300330 refcount_inc(&p->tcfa_refcnt);
Cong Wang95278dd2018-10-02 12:50:19 -0700331 mutex_unlock(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700332
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300333 if (p) {
334 *a = p;
335 return true;
336 }
337 return false;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700338}
Chris Mi65a206c2017-08-30 02:31:59 -0400339EXPORT_SYMBOL(tcf_idr_search);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700340
Cong Wang97a3f84f2018-08-19 12:22:06 -0700341static int tcf_idr_delete_index(struct tcf_idrinfo *idrinfo, u32 index)
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300342{
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300343 struct tc_action *p;
344 int ret = 0;
345
Cong Wang95278dd2018-10-02 12:50:19 -0700346 mutex_lock(&idrinfo->lock);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300347 p = idr_find(&idrinfo->action_idr, index);
348 if (!p) {
Cong Wang95278dd2018-10-02 12:50:19 -0700349 mutex_unlock(&idrinfo->lock);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300350 return -ENOENT;
351 }
352
353 if (!atomic_read(&p->tcfa_bindcnt)) {
354 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
355 struct module *owner = p->ops->owner;
356
357 WARN_ON(p != idr_remove(&idrinfo->action_idr,
358 p->tcfa_index));
Cong Wang95278dd2018-10-02 12:50:19 -0700359 mutex_unlock(&idrinfo->lock);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300360
Vlad Buslov16af6062018-07-05 17:24:29 +0300361 tcf_action_cleanup(p);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300362 module_put(owner);
363 return 0;
364 }
365 ret = 0;
366 } else {
367 ret = -EPERM;
368 }
369
Cong Wang95278dd2018-10-02 12:50:19 -0700370 mutex_unlock(&idrinfo->lock);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300371 return ret;
372}
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300373
Chris Mi65a206c2017-08-30 02:31:59 -0400374int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
375 struct tc_action **a, const struct tc_action_ops *ops,
376 int bind, bool cpustats)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700377{
WANG Congec0595c2016-07-25 16:09:42 -0700378 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
Chris Mi65a206c2017-08-30 02:31:59 -0400379 struct tcf_idrinfo *idrinfo = tn->idrinfo;
Eric Dumazet519c8182015-07-06 05:18:04 -0700380 int err = -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700381
382 if (unlikely(!p))
WANG Cong86062032014-02-11 17:07:31 -0800383 return -ENOMEM;
Vlad Buslov036bb442018-07-05 17:24:24 +0300384 refcount_set(&p->tcfa_refcnt, 1);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700385 if (bind)
Vlad Buslov036bb442018-07-05 17:24:24 +0300386 atomic_set(&p->tcfa_bindcnt, 1);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700387
Eric Dumazet519c8182015-07-06 05:18:04 -0700388 if (cpustats) {
389 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500390 if (!p->cpu_bstats)
Eric Dumazet519c8182015-07-06 05:18:04 -0700391 goto err1;
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400392 p->cpu_bstats_hw = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
393 if (!p->cpu_bstats_hw)
394 goto err2;
Matthew Wilcox339913a2017-11-28 10:28:15 -0500395 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
396 if (!p->cpu_qstats)
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400397 goto err3;
Eric Dumazet519c8182015-07-06 05:18:04 -0700398 }
WANG Congec0595c2016-07-25 16:09:42 -0700399 spin_lock_init(&p->tcfa_lock);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500400 p->tcfa_index = index;
WANG Congec0595c2016-07-25 16:09:42 -0700401 p->tcfa_tm.install = jiffies;
402 p->tcfa_tm.lastuse = jiffies;
403 p->tcfa_tm.firstuse = 0;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800404 if (est) {
WANG Congec0595c2016-07-25 16:09:42 -0700405 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
406 &p->tcfa_rate_est,
407 &p->tcfa_lock, NULL, est);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500408 if (err)
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400409 goto err4;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800410 }
411
Chris Mi65a206c2017-08-30 02:31:59 -0400412 p->idrinfo = idrinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700413 p->ops = ops;
WANG Congec0595c2016-07-25 16:09:42 -0700414 *a = p;
WANG Cong86062032014-02-11 17:07:31 -0800415 return 0;
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400416err4:
Matthew Wilcox339913a2017-11-28 10:28:15 -0500417 free_percpu(p->cpu_qstats);
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400418err3:
419 free_percpu(p->cpu_bstats_hw);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500420err2:
421 free_percpu(p->cpu_bstats);
422err1:
423 kfree(p);
424 return err;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700425}
Chris Mi65a206c2017-08-30 02:31:59 -0400426EXPORT_SYMBOL(tcf_idr_create);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700427
Chris Mi65a206c2017-08-30 02:31:59 -0400428void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700429{
Chris Mi65a206c2017-08-30 02:31:59 -0400430 struct tcf_idrinfo *idrinfo = tn->idrinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700431
Cong Wang95278dd2018-10-02 12:50:19 -0700432 mutex_lock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300433 /* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
434 WARN_ON(!IS_ERR(idr_replace(&idrinfo->action_idr, a, a->tcfa_index)));
Cong Wang95278dd2018-10-02 12:50:19 -0700435 mutex_unlock(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700436}
Chris Mi65a206c2017-08-30 02:31:59 -0400437EXPORT_SYMBOL(tcf_idr_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300439/* Cleanup idr index that was allocated but not initialized. */
440
441void tcf_idr_cleanup(struct tc_action_net *tn, u32 index)
442{
443 struct tcf_idrinfo *idrinfo = tn->idrinfo;
444
Cong Wang95278dd2018-10-02 12:50:19 -0700445 mutex_lock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300446 /* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
447 WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index)));
Cong Wang95278dd2018-10-02 12:50:19 -0700448 mutex_unlock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300449}
450EXPORT_SYMBOL(tcf_idr_cleanup);
451
452/* Check if action with specified index exists. If actions is found, increments
453 * its reference and bind counters, and return 1. Otherwise insert temporary
454 * error pointer (to prevent concurrent users from inserting actions with same
455 * index) and return 0.
456 */
457
458int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
459 struct tc_action **a, int bind)
460{
461 struct tcf_idrinfo *idrinfo = tn->idrinfo;
462 struct tc_action *p;
463 int ret;
464
465again:
Cong Wang95278dd2018-10-02 12:50:19 -0700466 mutex_lock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300467 if (*index) {
468 p = idr_find(&idrinfo->action_idr, *index);
469 if (IS_ERR(p)) {
470 /* This means that another process allocated
471 * index but did not assign the pointer yet.
472 */
Cong Wang95278dd2018-10-02 12:50:19 -0700473 mutex_unlock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300474 goto again;
475 }
476
477 if (p) {
478 refcount_inc(&p->tcfa_refcnt);
479 if (bind)
480 atomic_inc(&p->tcfa_bindcnt);
481 *a = p;
482 ret = 1;
483 } else {
484 *a = NULL;
485 ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
Cong Wang95278dd2018-10-02 12:50:19 -0700486 *index, GFP_KERNEL);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300487 if (!ret)
488 idr_replace(&idrinfo->action_idr,
489 ERR_PTR(-EBUSY), *index);
490 }
491 } else {
492 *index = 1;
493 *a = NULL;
494 ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
Cong Wang95278dd2018-10-02 12:50:19 -0700495 UINT_MAX, GFP_KERNEL);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300496 if (!ret)
497 idr_replace(&idrinfo->action_idr, ERR_PTR(-EBUSY),
498 *index);
499 }
Cong Wang95278dd2018-10-02 12:50:19 -0700500 mutex_unlock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300501 return ret;
502}
503EXPORT_SYMBOL(tcf_idr_check_alloc);
504
Chris Mi65a206c2017-08-30 02:31:59 -0400505void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
506 struct tcf_idrinfo *idrinfo)
WANG Cong1d4150c2016-02-22 15:57:52 -0800507{
Chris Mi65a206c2017-08-30 02:31:59 -0400508 struct idr *idr = &idrinfo->action_idr;
509 struct tc_action *p;
510 int ret;
511 unsigned long id = 1;
WANG Cong1d4150c2016-02-22 15:57:52 -0800512
Matthew Wilcox7a457572017-11-28 15:39:51 -0500513 idr_for_each_entry_ul(idr, p, id) {
Chris Mi65a206c2017-08-30 02:31:59 -0400514 ret = __tcf_idr_release(p, false, true);
515 if (ret == ACT_P_DELETED)
516 module_put(ops->owner);
517 else if (ret < 0)
518 return;
WANG Cong1d4150c2016-02-22 15:57:52 -0800519 }
Chris Mi65a206c2017-08-30 02:31:59 -0400520 idr_destroy(&idrinfo->action_idr);
WANG Cong1d4150c2016-02-22 15:57:52 -0800521}
Chris Mi65a206c2017-08-30 02:31:59 -0400522EXPORT_SYMBOL(tcf_idrinfo_destroy);
WANG Cong1d4150c2016-02-22 15:57:52 -0800523
WANG Cong1f747c22013-12-15 20:15:10 -0800524static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525static DEFINE_RWLOCK(act_mod_lock);
526
WANG Congddf97cc2016-02-22 15:57:53 -0800527int tcf_register_action(struct tc_action_ops *act,
528 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
WANG Cong1f747c22013-12-15 20:15:10 -0800530 struct tc_action_ops *a;
WANG Congddf97cc2016-02-22 15:57:53 -0800531 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
WANG Congddf97cc2016-02-22 15:57:53 -0800533 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500534 return -EINVAL;
535
WANG Congab102b82016-10-11 10:56:45 -0700536 /* We have to register pernet ops before making the action ops visible,
537 * otherwise tcf_action_init_1() could get a partially initialized
538 * netns.
539 */
540 ret = register_pernet_subsys(ops);
541 if (ret)
542 return ret;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800545 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
547 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700548 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return -EEXIST;
550 }
551 }
WANG Cong1f747c22013-12-15 20:15:10 -0800552 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 write_unlock(&act_mod_lock);
WANG Congddf97cc2016-02-22 15:57:53 -0800554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return 0;
556}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800557EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
WANG Congddf97cc2016-02-22 15:57:53 -0800559int tcf_unregister_action(struct tc_action_ops *act,
560 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
WANG Cong1f747c22013-12-15 20:15:10 -0800562 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 int err = -ENOENT;
564
565 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800566 list_for_each_entry(a, &act_base, head) {
567 if (a == act) {
568 list_del(&act->head);
569 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700574 if (!err)
575 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return err;
577}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800578EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580/* lookup by name */
581static struct tc_action_ops *tc_lookup_action_n(char *kind)
582{
Eric Dumazeta7928662013-12-20 12:32:32 -0800583 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 if (kind) {
586 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800587 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800589 if (try_module_get(a->owner))
590 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 break;
592 }
593 }
594 read_unlock(&act_mod_lock);
595 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800596 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800599/* lookup by nlattr */
600static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Eric Dumazeta7928662013-12-20 12:32:32 -0800602 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 if (kind) {
605 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800606 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800607 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800608 if (try_module_get(a->owner))
609 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 break;
611 }
612 }
613 read_unlock(&act_mod_lock);
614 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800615 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400618/*TCA_ACT_MAX_PRIO is 32, there count upto 32 */
619#define TCA_ACT_MAX_PRIO_MASK 0x1FF
WANG Cong22dc13c2016-08-13 22:35:00 -0700620int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
621 int nr_actions, struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400623 u32 jmp_prgcnt = 0;
624 u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
Jiri Pirkoec1a9cc2017-08-04 14:29:02 +0200625 int i;
626 int ret = TC_ACT_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Willem de Bruijne7246e12017-01-07 17:06:35 -0500628 if (skb_skip_tc_classify(skb))
629 return TC_ACT_OK;
630
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400631restart_act_graph:
WANG Cong22dc13c2016-08-13 22:35:00 -0700632 for (i = 0; i < nr_actions; i++) {
633 const struct tc_action *a = actions[i];
634
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400635 if (jmp_prgcnt > 0) {
636 jmp_prgcnt -= 1;
637 continue;
638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500640 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500641 if (ret == TC_ACT_REPEAT)
642 goto repeat; /* we need a ttl - JHS */
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400643
Jiri Pirko9da32422017-05-02 10:12:00 +0200644 if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400645 jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
646 if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
647 /* faulty opcode, stop pipeline */
648 return TC_ACT_OK;
649 } else {
650 jmp_ttl -= 1;
651 if (jmp_ttl > 0)
652 goto restart_act_graph;
653 else /* faulty graph, stop pipeline */
654 return TC_ACT_OK;
655 }
Jiri Pirkodb505142017-05-17 11:08:03 +0200656 } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
657 tcf_action_goto_chain_exec(a, res);
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400658 }
659
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500660 if (ret != TC_ACT_PIPE)
Willem de Bruijne7246e12017-01-07 17:06:35 -0500661 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return ret;
665}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800666EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Vlad Buslov90b73b72018-07-05 17:24:33 +0300668int tcf_action_destroy(struct tc_action *actions[], int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Jiri Pirko255cd502017-09-13 17:32:37 +0200670 const struct tc_action_ops *ops;
Vlad Buslov90b73b72018-07-05 17:24:33 +0300671 struct tc_action *a;
672 int ret = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Vlad Buslov90b73b72018-07-05 17:24:33 +0300674 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
675 a = actions[i];
676 actions[i] = NULL;
Jiri Pirko255cd502017-09-13 17:32:37 +0200677 ops = a->ops;
Chris Mi65a206c2017-08-30 02:31:59 -0400678 ret = __tcf_idr_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800679 if (ret == ACT_P_DELETED)
Jiri Pirko255cd502017-09-13 17:32:37 +0200680 module_put(ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800681 else if (ret < 0)
682 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
WANG Cong55334a52014-02-11 17:07:34 -0800684 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Paolo Abeni97763dc2018-08-29 10:22:33 +0200687static int tcf_action_destroy_1(struct tc_action *a, int bind)
688{
689 struct tc_action *actions[] = { a, NULL };
690
691 return tcf_action_destroy(actions, bind);
692}
693
Vlad Buslov16af6062018-07-05 17:24:29 +0300694static int tcf_action_put(struct tc_action *p)
695{
696 return __tcf_action_put(p, false);
697}
698
Cong Wangedfaf942018-08-19 12:22:05 -0700699/* Put all actions in this array, skip those NULL's. */
Vlad Buslov90b73b72018-07-05 17:24:33 +0300700static void tcf_action_put_many(struct tc_action *actions[])
Vlad Buslovcae422f2018-07-05 17:24:31 +0300701{
Vlad Buslov90b73b72018-07-05 17:24:33 +0300702 int i;
Vlad Buslovcae422f2018-07-05 17:24:31 +0300703
Cong Wangedfaf942018-08-19 12:22:05 -0700704 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
Vlad Buslov90b73b72018-07-05 17:24:33 +0300705 struct tc_action *a = actions[i];
Cong Wangedfaf942018-08-19 12:22:05 -0700706 const struct tc_action_ops *ops;
Vlad Buslovcae422f2018-07-05 17:24:31 +0300707
Cong Wangedfaf942018-08-19 12:22:05 -0700708 if (!a)
709 continue;
710 ops = a->ops;
Vlad Buslovcae422f2018-07-05 17:24:31 +0300711 if (tcf_action_put(a))
712 module_put(ops->owner);
713 }
714}
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716int
717tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
718{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return a->ops->dump(skb, a, bind, ref);
720}
721
722int
723tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
724{
725 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700726 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800727 struct nlattr *nest;
Vlad Busloveec94fd2018-07-05 17:24:23 +0300728 struct tc_cookie *cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
David S. Miller1b34ec42012-03-29 05:11:39 -0400730 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
731 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800733 goto nla_put_failure;
Vlad Busloveec94fd2018-07-05 17:24:23 +0300734
735 rcu_read_lock();
736 cookie = rcu_dereference(a->act_cookie);
737 if (cookie) {
738 if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) {
739 rcu_read_unlock();
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500740 goto nla_put_failure;
Vlad Busloveec94fd2018-07-05 17:24:23 +0300741 }
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500742 }
Vlad Busloveec94fd2018-07-05 17:24:23 +0300743 rcu_read_unlock();
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500744
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800745 nest = nla_nest_start(skb, TCA_OPTIONS);
746 if (nest == NULL)
747 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000748 err = tcf_action_dump_old(skb, a, bind, ref);
749 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800750 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return err;
752 }
753
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800754nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700755 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return -1;
757}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800758EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Vlad Buslov90b73b72018-07-05 17:24:33 +0300760int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[],
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400761 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
763 struct tc_action *a;
Vlad Buslov90b73b72018-07-05 17:24:33 +0300764 int err = -EINVAL, i;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800765 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Vlad Buslov90b73b72018-07-05 17:24:33 +0300767 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
768 a = actions[i];
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800769 nest = nla_nest_start(skb, a->order);
770 if (nest == NULL)
771 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 err = tcf_action_dump_1(skb, a, bind, ref);
773 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700774 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800775 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777
778 return 0;
779
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800780nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700781 err = -EINVAL;
782errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800783 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700784 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200787static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500788{
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200789 struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
790 if (!c)
791 return NULL;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500792
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200793 c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
794 if (!c->data) {
795 kfree(c);
796 return NULL;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500797 }
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200798 c->len = nla_len(tb[TCA_ACT_COOKIE]);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500799
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200800 return c;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500801}
802
Paolo Abeni802bfb12018-07-30 14:30:42 +0200803static bool tcf_action_valid(int action)
804{
805 int opcode = TC_ACT_EXT_OPCODE(action);
806
807 if (!opcode)
808 return action <= TC_ACT_VALUE_MAX;
809 return opcode <= TC_ACT_EXT_OPCODE_MAX || action == TC_ACT_UNSPEC;
810}
811
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200812struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
813 struct nlattr *nla, struct nlattr *est,
Alexander Aringaea0d722018-02-15 10:54:54 -0500814 char *name, int ovr, int bind,
Vlad Buslov789871b2018-07-05 17:24:25 +0300815 bool rtnl_held,
Alexander Aringaea0d722018-02-15 10:54:54 -0500816 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
818 struct tc_action *a;
819 struct tc_action_ops *a_o;
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200820 struct tc_cookie *cookie = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000822 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800823 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800824 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (name == NULL) {
Alexander Aring84ae0172018-02-15 10:54:55 -0500827 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
Patrick McHardycee63722008-01-23 20:33:32 -0800828 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800830 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800831 kind = tb[TCA_ACT_KIND];
Alexander Aring84ae0172018-02-15 10:54:55 -0500832 if (!kind) {
833 NL_SET_ERR_MSG(extack, "TC action kind must be specified");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -0500835 }
836 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) {
837 NL_SET_ERR_MSG(extack, "TC action name too long");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -0500839 }
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200840 if (tb[TCA_ACT_COOKIE]) {
841 int cklen = nla_len(tb[TCA_ACT_COOKIE]);
842
Alexander Aring84ae0172018-02-15 10:54:55 -0500843 if (cklen > TC_COOKIE_MAX_SIZE) {
844 NL_SET_ERR_MSG(extack, "TC cookie size above the maximum");
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200845 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -0500846 }
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200847
848 cookie = nla_memdup_cookie(tb);
849 if (!cookie) {
Alexander Aring84ae0172018-02-15 10:54:55 -0500850 NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200851 err = -ENOMEM;
852 goto err_out;
853 }
854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 } else {
Alexander Aring84ae0172018-02-15 10:54:55 -0500856 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) {
857 NL_SET_ERR_MSG(extack, "TC action name too long");
858 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -0500860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862
863 a_o = tc_lookup_action_n(act_name);
864 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700865#ifdef CONFIG_MODULES
Vlad Buslov789871b2018-07-05 17:24:25 +0300866 if (rtnl_held)
867 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800868 request_module("act_%s", act_name);
Vlad Buslov789871b2018-07-05 17:24:25 +0300869 if (rtnl_held)
870 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 a_o = tc_lookup_action_n(act_name);
873
874 /* We dropped the RTNL semaphore in order to
875 * perform the module load. So, even if we
876 * succeeded in loading the module we have to
877 * tell the caller to replay the request. We
878 * indicate this using -EAGAIN.
879 */
880 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800881 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 goto err_mod;
883 }
884#endif
Alexander Aring84ae0172018-02-15 10:54:55 -0500885 NL_SET_ERR_MSG(extack, "Failed to load TC action module");
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800886 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 goto err_out;
888 }
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /* backward compatibility for policer */
891 if (name == NULL)
Alexander Aring589dad62018-02-15 10:54:56 -0500892 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind,
Vlad Buslov789871b2018-07-05 17:24:25 +0300893 rtnl_held, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 else
Vlad Buslov789871b2018-07-05 17:24:25 +0300895 err = a_o->init(net, nla, est, &a, ovr, bind, rtnl_held,
896 extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800897 if (err < 0)
WANG Conga85a9702016-07-25 16:09:41 -0700898 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Vlad Busloveec94fd2018-07-05 17:24:23 +0300900 if (!name && tb[TCA_ACT_COOKIE])
901 tcf_set_action_cookie(&a->act_cookie, cookie);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000904 * if it exists and is only bound to in a_o->init() then
905 * ACT_P_CREATED is not returned (a zero is).
906 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800907 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Jiri Pirkodb505142017-05-17 11:08:03 +0200910 if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) {
911 err = tcf_action_goto_chain_init(a, tp);
912 if (err) {
Paolo Abeni97763dc2018-08-29 10:22:33 +0200913 tcf_action_destroy_1(a, bind);
Alexander Aring84ae0172018-02-15 10:54:55 -0500914 NL_SET_ERR_MSG(extack, "Failed to init TC action chain");
Jiri Pirkodb505142017-05-17 11:08:03 +0200915 return ERR_PTR(err);
916 }
917 }
918
Paolo Abeni802bfb12018-07-30 14:30:42 +0200919 if (!tcf_action_valid(a->tcfa_action)) {
Paolo Abeni97763dc2018-08-29 10:22:33 +0200920 tcf_action_destroy_1(a, bind);
921 NL_SET_ERR_MSG(extack, "Invalid control action value");
922 return ERR_PTR(-EINVAL);
Paolo Abeni802bfb12018-07-30 14:30:42 +0200923 }
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return a;
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927err_mod:
928 module_put(a_o->owner);
929err_out:
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200930 if (cookie) {
931 kfree(cookie->data);
932 kfree(cookie);
933 }
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800934 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Vlad Buslov90b73b72018-07-05 17:24:33 +0300937/* Returns numbers of initialized actions or negative error. */
938
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200939int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
940 struct nlattr *est, char *name, int ovr, int bind,
Vlad Buslov90b73b72018-07-05 17:24:33 +0300941 struct tc_action *actions[], size_t *attr_size,
Vlad Buslov789871b2018-07-05 17:24:25 +0300942 bool rtnl_held, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000944 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800945 struct tc_action *act;
Roman Mashak4e76e752018-03-08 16:59:19 -0500946 size_t sz = 0;
Patrick McHardycee63722008-01-23 20:33:32 -0800947 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 int i;
949
Alexander Aring84ae0172018-02-15 10:54:55 -0500950 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack);
Patrick McHardycee63722008-01-23 20:33:32 -0800951 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800952 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800954 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Alexander Aringaea0d722018-02-15 10:54:54 -0500955 act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind,
Vlad Buslov789871b2018-07-05 17:24:25 +0300956 rtnl_held, extack);
WANG Cong33be6272013-12-15 20:15:05 -0800957 if (IS_ERR(act)) {
958 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800960 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800961 act->order = i;
Roman Mashak4e76e752018-03-08 16:59:19 -0500962 sz += tcf_action_fill_size(act);
Vlad Buslov90b73b72018-07-05 17:24:33 +0300963 /* Start from index 0 */
964 actions[i - 1] = act;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400966
Roman Mashak4e76e752018-03-08 16:59:19 -0500967 *attr_size = tcf_action_full_attrs_size(sz);
Vlad Buslov90b73b72018-07-05 17:24:33 +0300968 return i - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970err:
WANG Cong33be6272013-12-15 20:15:05 -0800971 tcf_action_destroy(actions, bind);
972 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
WANG Congec0595c2016-07-25 16:09:42 -0700975int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 int compat_mode)
977{
978 int err = 0;
979 struct gnet_dump d;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900980
WANG Cong7eb88962014-01-09 16:14:05 -0800981 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 goto errout;
983
984 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400985 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 */
987 if (compat_mode) {
WANG Congec0595c2016-07-25 16:09:42 -0700988 if (p->type == TCA_OLD_COMPAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 err = gnet_stats_start_copy_compat(skb, 0,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200990 TCA_STATS,
991 TCA_XSTATS,
WANG Congec0595c2016-07-25 16:09:42 -0700992 &p->tcfa_lock, &d,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200993 TCA_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 else
995 return 0;
996 } else
997 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Congec0595c2016-07-25 16:09:42 -0700998 &p->tcfa_lock, &d, TCA_ACT_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 if (err < 0)
1001 goto errout;
1002
WANG Congec0595c2016-07-25 16:09:42 -07001003 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
Eelco Chaudron28169ab2018-09-21 07:14:02 -04001004 gnet_stats_copy_basic_hw(NULL, &d, p->cpu_bstats_hw,
1005 &p->tcfa_bstats_hw) < 0 ||
Eric Dumazet1c0d32f2016-12-04 09:48:16 -08001006 gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -07001007 gnet_stats_copy_queue(&d, p->cpu_qstats,
WANG Congec0595c2016-07-25 16:09:42 -07001008 &p->tcfa_qstats,
1009 p->tcfa_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 goto errout;
1011
1012 if (gnet_stats_finish_copy(&d) < 0)
1013 goto errout;
1014
1015 return 0;
1016
1017errout:
1018 return -1;
1019}
1020
Vlad Buslov90b73b72018-07-05 17:24:33 +03001021static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[],
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001022 u32 portid, u32 seq, u16 flags, int event, int bind,
1023 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 struct tcamsg *t;
1026 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001027 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001028 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Eric W. Biederman15e47302012-09-07 20:12:54 +00001030 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -07001031 if (!nlh)
1032 goto out_nlmsg_trim;
1033 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001035 t->tca__pad1 = 0;
1036 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001037
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001038 nest = nla_nest_start(skb, TCA_ACT_TAB);
Alexander Aring1af8515582018-02-15 10:54:53 -05001039 if (!nest)
David S. Miller8b00a532012-06-26 21:39:32 -07001040 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
WANG Cong33be6272013-12-15 20:15:05 -08001042 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001043 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001045 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001046
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001047 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return skb->len;
1049
David S. Miller8b00a532012-06-26 21:39:32 -07001050out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001051 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return -1;
1053}
1054
1055static int
Roman Mashakc4c42902017-07-13 13:12:18 -04001056tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
Vlad Buslov90b73b72018-07-05 17:24:33 +03001057 struct tc_action *actions[], int event,
Alexander Aring84ae0172018-02-15 10:54:55 -05001058 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
1060 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1063 if (!skb)
1064 return -ENOBUFS;
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001065 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
Vlad Buslov3f7c72b2018-07-05 17:24:26 +03001066 0, 1) <= 0) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001067 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 kfree_skb(skb);
1069 return -EINVAL;
1070 }
Thomas Graf2942e902006-08-15 00:30:25 -07001071
Eric W. Biederman15e47302012-09-07 20:12:54 +00001072 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
WANG Congddf97cc2016-02-22 15:57:53 -08001075static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
Alexander Aring84ae0172018-02-15 10:54:55 -05001076 struct nlmsghdr *n, u32 portid,
1077 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001079 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -07001080 const struct tc_action_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 struct tc_action *a;
1082 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001083 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Alexander Aring84ae0172018-02-15 10:54:55 -05001085 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001086 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001087 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Patrick McHardycee63722008-01-23 20:33:32 -08001089 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001090 if (tb[TCA_ACT_INDEX] == NULL ||
Alexander Aring84ae0172018-02-15 10:54:55 -05001091 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
1092 NL_SET_ERR_MSG(extack, "Invalid TC action index value");
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001093 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -05001094 }
Patrick McHardy1587bac2008-01-23 20:35:03 -08001095 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001097 err = -EINVAL;
WANG Conga85a9702016-07-25 16:09:41 -07001098 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Alexander Aring84ae0172018-02-15 10:54:55 -05001099 if (!ops) { /* could happen in batch of actions */
Cong Wangf061b482018-08-29 10:15:35 -07001100 NL_SET_ERR_MSG(extack, "Specified TC action kind not found");
WANG Conga85a9702016-07-25 16:09:41 -07001101 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -05001102 }
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001103 err = -ENOENT;
Cong Wangf061b482018-08-29 10:15:35 -07001104 if (ops->lookup(net, &a, index) == 0) {
1105 NL_SET_ERR_MSG(extack, "TC action with specified index not found");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 goto err_mod;
Cong Wangf061b482018-08-29 10:15:35 -07001107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
WANG Conga85a9702016-07-25 16:09:41 -07001109 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112err_mod:
WANG Conga85a9702016-07-25 16:09:41 -07001113 module_put(ops->owner);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001114err_out:
1115 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
Tom Goff7316ae82010-03-19 15:40:13 +00001118static int tca_action_flush(struct net *net, struct nlattr *nla,
Alexander Aring84ae0172018-02-15 10:54:55 -05001119 struct nlmsghdr *n, u32 portid,
1120 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122 struct sk_buff *skb;
1123 unsigned char *b;
1124 struct nlmsghdr *nlh;
1125 struct tcamsg *t;
1126 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001127 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001128 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -07001129 const struct tc_action_ops *ops;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001130 struct nlattr *kind;
Jamal Hadi Salim36723872008-08-13 02:41:45 -07001131 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
Alexander Aring84ae0172018-02-15 10:54:55 -05001134 if (!skb)
Jamal Hadi Salim36723872008-08-13 02:41:45 -07001135 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001137 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Alexander Aring84ae0172018-02-15 10:54:55 -05001139 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001140 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 goto err_out;
1142
Patrick McHardycee63722008-01-23 20:33:32 -08001143 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001144 kind = tb[TCA_ACT_KIND];
WANG Conga85a9702016-07-25 16:09:41 -07001145 ops = tc_lookup_action(kind);
Alexander Aring84ae0172018-02-15 10:54:55 -05001146 if (!ops) { /*some idjot trying to flush unknown action */
1147 NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -05001149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001151 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
1152 sizeof(*t), 0);
Alexander Aring84ae0172018-02-15 10:54:55 -05001153 if (!nlh) {
1154 NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
David S. Miller8b00a532012-06-26 21:39:32 -07001155 goto out_module_put;
Alexander Aring84ae0172018-02-15 10:54:55 -05001156 }
David S. Miller8b00a532012-06-26 21:39:32 -07001157 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001159 t->tca__pad1 = 0;
1160 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001162 nest = nla_nest_start(skb, TCA_ACT_TAB);
Alexander Aring84ae0172018-02-15 10:54:55 -05001163 if (!nest) {
1164 NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
David S. Miller8b00a532012-06-26 21:39:32 -07001165 goto out_module_put;
Alexander Aring84ae0172018-02-15 10:54:55 -05001166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Alexander Aring41780102018-02-15 10:54:58 -05001168 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack);
Davide Caratti66dede22018-02-15 15:50:57 +01001169 if (err <= 0) {
1170 nla_nest_cancel(skb, nest);
David S. Miller8b00a532012-06-26 21:39:32 -07001171 goto out_module_put;
Davide Caratti66dede22018-02-15 15:50:57 +01001172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001174 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001176 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Conga85a9702016-07-25 16:09:41 -07001178 module_put(ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001179 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001180 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (err > 0)
1182 return 0;
Alexander Aring84ae0172018-02-15 10:54:55 -05001183 if (err < 0)
1184 NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 return err;
1187
David S. Miller8b00a532012-06-26 21:39:32 -07001188out_module_put:
WANG Conga85a9702016-07-25 16:09:41 -07001189 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190err_out:
1191 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return err;
1193}
1194
Cong Wangb144e7e2018-08-19 12:22:07 -07001195static int tcf_action_delete(struct net *net, struct tc_action *actions[])
Vlad Buslov16af6062018-07-05 17:24:29 +03001196{
Cong Wang97a3f84f2018-08-19 12:22:06 -07001197 int i;
Vlad Buslov16af6062018-07-05 17:24:29 +03001198
Vlad Buslov90b73b72018-07-05 17:24:33 +03001199 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
1200 struct tc_action *a = actions[i];
Vlad Buslov16af6062018-07-05 17:24:29 +03001201 const struct tc_action_ops *ops = a->ops;
Vlad Buslov16af6062018-07-05 17:24:29 +03001202 /* Actions can be deleted concurrently so we must save their
1203 * type and id to search again after reference is released.
1204 */
Cong Wang97a3f84f2018-08-19 12:22:06 -07001205 struct tcf_idrinfo *idrinfo = a->idrinfo;
1206 u32 act_index = a->tcfa_index;
Vlad Buslov16af6062018-07-05 17:24:29 +03001207
Vlad Buslovc10bbfa2018-09-03 10:04:55 +03001208 actions[i] = NULL;
Vlad Buslov16af6062018-07-05 17:24:29 +03001209 if (tcf_action_put(a)) {
1210 /* last reference, action was deleted concurrently */
1211 module_put(ops->owner);
1212 } else {
Cong Wang97a3f84f2018-08-19 12:22:06 -07001213 int ret;
1214
Vlad Buslov16af6062018-07-05 17:24:29 +03001215 /* now do the delete */
Cong Wang97a3f84f2018-08-19 12:22:06 -07001216 ret = tcf_idr_delete_index(idrinfo, act_index);
Cong Wangedfaf942018-08-19 12:22:05 -07001217 if (ret < 0)
Vlad Buslov16af6062018-07-05 17:24:29 +03001218 return ret;
1219 }
1220 }
1221 return 0;
1222}
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224static int
Vlad Buslov90b73b72018-07-05 17:24:33 +03001225tcf_del_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
Cong Wangedfaf942018-08-19 12:22:05 -07001226 u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
WANG Conga56e1952014-01-09 16:14:00 -08001227{
1228 int ret;
1229 struct sk_buff *skb;
1230
Roman Mashakd04e6992018-03-08 16:59:17 -05001231 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1232 GFP_KERNEL);
WANG Conga56e1952014-01-09 16:14:00 -08001233 if (!skb)
1234 return -ENOBUFS;
1235
1236 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
Vlad Buslov3f7c72b2018-07-05 17:24:26 +03001237 0, 2) <= 0) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001238 NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
WANG Conga56e1952014-01-09 16:14:00 -08001239 kfree_skb(skb);
1240 return -EINVAL;
1241 }
1242
1243 /* now do the delete */
Cong Wangb144e7e2018-08-19 12:22:07 -07001244 ret = tcf_action_delete(net, actions);
WANG Cong55334a52014-02-11 17:07:34 -08001245 if (ret < 0) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001246 NL_SET_ERR_MSG(extack, "Failed to delete TC action");
WANG Cong55334a52014-02-11 17:07:34 -08001247 kfree_skb(skb);
1248 return ret;
1249 }
WANG Conga56e1952014-01-09 16:14:00 -08001250
1251 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1252 n->nlmsg_flags & NLM_F_ECHO);
1253 if (ret > 0)
1254 return 0;
1255 return ret;
1256}
1257
1258static int
Tom Goff7316ae82010-03-19 15:40:13 +00001259tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Alexander Aring84ae0172018-02-15 10:54:55 -05001260 u32 portid, int event, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Patrick McHardycee63722008-01-23 20:33:32 -08001262 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001263 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -08001264 struct tc_action *act;
Roman Mashakd04e6992018-03-08 16:59:17 -05001265 size_t attr_size = 0;
Cong Wangedfaf942018-08-19 12:22:05 -07001266 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Alexander Aring84ae0172018-02-15 10:54:55 -05001268 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001269 if (ret < 0)
1270 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001272 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Alexander Aring1af8515582018-02-15 10:54:53 -05001273 if (tb[1])
Alexander Aring84ae0172018-02-15 10:54:55 -05001274 return tca_action_flush(net, tb[1], n, portid, extack);
Alexander Aring1af8515582018-02-15 10:54:53 -05001275
Alexander Aring84ae0172018-02-15 10:54:55 -05001276 NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
Alexander Aring1af8515582018-02-15 10:54:53 -05001277 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
1279
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001280 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001281 act = tcf_action_get_1(net, tb[i], n, portid, extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001282 if (IS_ERR(act)) {
1283 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001285 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001286 act->order = i;
Roman Mashak4e76e752018-03-08 16:59:19 -05001287 attr_size += tcf_action_fill_size(act);
Vlad Buslov90b73b72018-07-05 17:24:33 +03001288 actions[i - 1] = act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290
Roman Mashak4e76e752018-03-08 16:59:19 -05001291 attr_size = tcf_action_full_attrs_size(attr_size);
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (event == RTM_GETACTION)
Vlad Buslov90b73b72018-07-05 17:24:33 +03001294 ret = tcf_get_notify(net, portid, n, actions, event, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 else { /* delete */
Cong Wangedfaf942018-08-19 12:22:05 -07001296 ret = tcf_del_notify(net, n, actions, portid, attr_size, extack);
WANG Conga56e1952014-01-09 16:14:00 -08001297 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 goto err;
Cong Wangedfaf942018-08-19 12:22:05 -07001299 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301err:
Cong Wangedfaf942018-08-19 12:22:05 -07001302 tcf_action_put_many(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return ret;
1304}
1305
WANG Conga56e1952014-01-09 16:14:00 -08001306static int
Vlad Buslov90b73b72018-07-05 17:24:33 +03001307tcf_add_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
Roman Mashakd04e6992018-03-08 16:59:17 -05001308 u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 int err = 0;
1312
Roman Mashakd04e6992018-03-08 16:59:17 -05001313 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1314 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (!skb)
1316 return -ENOBUFS;
1317
WANG Conga56e1952014-01-09 16:14:00 -08001318 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
1319 RTM_NEWACTION, 0, 0) <= 0) {
Roman Mashakd143b9e2018-03-02 20:52:01 -05001320 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
WANG Conga56e1952014-01-09 16:14:00 -08001321 kfree_skb(skb);
1322 return -EINVAL;
1323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
WANG Conga56e1952014-01-09 16:14:00 -08001325 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1326 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (err > 0)
1328 err = 0;
1329 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
1331
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001332static int tcf_action_add(struct net *net, struct nlattr *nla,
Alexander Aringaea0d722018-02-15 10:54:54 -05001333 struct nlmsghdr *n, u32 portid, int ovr,
1334 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Roman Mashakd04e6992018-03-08 16:59:17 -05001336 size_t attr_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 int ret = 0;
Vlad Buslov90b73b72018-07-05 17:24:33 +03001338 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Vlad Buslov90b73b72018-07-05 17:24:33 +03001340 ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0, actions,
Vlad Buslov789871b2018-07-05 17:24:25 +03001341 &attr_size, true, extack);
Vlad Buslov90b73b72018-07-05 17:24:33 +03001342 if (ret < 0)
WANG Congf07fed82016-08-13 22:34:56 -07001343 return ret;
Vlad Buslov90b73b72018-07-05 17:24:33 +03001344 ret = tcf_add_notify(net, n, actions, portid, attr_size, extack);
Vlad Buslovcae422f2018-07-05 17:24:31 +03001345 if (ovr)
Vlad Buslov90b73b72018-07-05 17:24:33 +03001346 tcf_action_put_many(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Vlad Buslovcae422f2018-07-05 17:24:31 +03001348 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001351static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON;
1352static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
1353 [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32,
1354 .validation_data = &tcaa_root_flags_allowed },
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001355 [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001356};
1357
David Ahernc21ef3e2017-04-16 09:48:24 -07001358static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
1359 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001361 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001362 struct nlattr *tca[TCA_ROOT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +00001363 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 int ret = 0, ovr = 0;
1365
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001366 if ((n->nlmsg_type != RTM_GETACTION) &&
1367 !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001368 return -EPERM;
1369
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001370 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ROOT_MAX, NULL,
David Ahernc21ef3e2017-04-16 09:48:24 -07001371 extack);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001372 if (ret < 0)
1373 return ret;
1374
1375 if (tca[TCA_ACT_TAB] == NULL) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001376 NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return -EINVAL;
1378 }
1379
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001380 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 switch (n->nlmsg_type) {
1382 case RTM_NEWACTION:
1383 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001384 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 * Note that CREATE | EXCL implies that
1386 * but since we want avoid ambiguity (eg when flags
1387 * is zero) then just set this
1388 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001389 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 ovr = 1;
1391replay:
Alexander Aringaea0d722018-02-15 10:54:54 -05001392 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr,
1393 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (ret == -EAGAIN)
1395 goto replay;
1396 break;
1397 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001398 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Alexander Aring84ae0172018-02-15 10:54:55 -05001399 portid, RTM_DELACTION, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 break;
1401 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001402 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Alexander Aring84ae0172018-02-15 10:54:55 -05001403 portid, RTM_GETACTION, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 break;
1405 default:
1406 BUG();
1407 }
1408
1409 return ret;
1410}
1411
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001412static struct nlattr *find_dump_kind(struct nlattr **nla)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001414 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001415 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001416 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001418 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (tb1 == NULL)
1420 return NULL;
1421
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001422 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
Johannes Bergfceb6432017-04-12 14:34:07 +02001423 NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Patrick McHardy6d834e02008-01-23 20:32:42 -08001426 if (tb[1] == NULL)
1427 return NULL;
Johannes Bergfceb6432017-04-12 14:34:07 +02001428 if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001430 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Thomas Graf26dab892006-07-05 20:45:06 -07001432 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001435static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
WANG Congddf97cc2016-02-22 15:57:53 -08001437 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001439 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001440 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 struct tc_action_ops *a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001443 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001444 struct nlattr *tb[TCA_ROOT_MAX + 1];
1445 struct nlattr *count_attr = NULL;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001446 unsigned long jiffy_since = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001447 struct nlattr *kind = NULL;
1448 struct nla_bitfield32 bf;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001449 u32 msecs_since = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001450 u32 act_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001452 ret = nlmsg_parse(cb->nlh, sizeof(struct tcamsg), tb, TCA_ROOT_MAX,
David Aherndac9c972018-10-07 20:16:24 -07001453 tcaa_policy, cb->extack);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001454 if (ret < 0)
1455 return ret;
1456
1457 kind = find_dump_kind(tb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001459 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return 0;
1461 }
1462
Thomas Graf26dab892006-07-05 20:45:06 -07001463 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001464 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001467 cb->args[2] = 0;
1468 if (tb[TCA_ROOT_FLAGS]) {
1469 bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
1470 cb->args[2] = bf.value;
1471 }
1472
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001473 if (tb[TCA_ROOT_TIME_DELTA]) {
1474 msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
1475 }
1476
Eric W. Biederman15e47302012-09-07 20:12:54 +00001477 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001478 cb->nlh->nlmsg_type, sizeof(*t), 0);
1479 if (!nlh)
1480 goto out_module_put;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001481
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001482 if (msecs_since)
1483 jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
1484
David S. Miller8b00a532012-06-26 21:39:32 -07001485 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001487 t->tca__pad1 = 0;
1488 t->tca__pad2 = 0;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001489 cb->args[3] = jiffy_since;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001490 count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
1491 if (!count_attr)
1492 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001494 nest = nla_nest_start(skb, TCA_ACT_TAB);
1495 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001496 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Alexander Aring41780102018-02-15 10:54:58 -05001498 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001500 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001503 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 ret = skb->len;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001505 act_count = cb->args[1];
1506 memcpy(nla_data(count_attr), &act_count, sizeof(u32));
1507 cb->args[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 } else
Jamal Hadi Salimebecaa62016-06-13 18:08:42 -04001509 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001511 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001512 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 nlh->nlmsg_flags |= NLM_F_MULTI;
1514 module_put(a_o->owner);
1515 return skb->len;
1516
David S. Miller8b00a532012-06-26 21:39:32 -07001517out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001519 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return skb->len;
1521}
1522
1523static int __init tc_action_init(void)
1524{
Florian Westphalb97bac62017-08-09 20:41:48 +02001525 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
1526 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
Greg Rosec7ac8672011-06-10 01:27:09 +00001527 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
Florian Westphalb97bac62017-08-09 20:41:48 +02001528 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 return 0;
1531}
1532
1533subsys_initcall(tc_action_init);