blob: cd69a6afcf880e8e5d709e4db951b35b1c7fc881 [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>
Jiri Pirkob3f55bd2017-10-11 09:41:08 +020024#include <linux/rhashtable.h>
25#include <linux/list.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110026#include <net/net_namespace.h>
27#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <net/sch_generic.h>
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -050029#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <net/act_api.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070031#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Jiri Pirkodb505142017-05-17 11:08:03 +020033static int tcf_action_goto_chain_init(struct tc_action *a, struct tcf_proto *tp)
34{
35 u32 chain_index = a->tcfa_action & TC_ACT_EXT_VAL_MASK;
36
37 if (!tp)
38 return -EINVAL;
Jiri Pirko1f3ed382018-07-27 09:45:05 +020039 a->goto_chain = tcf_chain_get_by_act(tp->chain->block, chain_index);
Jiri Pirkodb505142017-05-17 11:08:03 +020040 if (!a->goto_chain)
41 return -ENOMEM;
42 return 0;
43}
44
45static void tcf_action_goto_chain_fini(struct tc_action *a)
46{
Jiri Pirko1f3ed382018-07-27 09:45:05 +020047 tcf_chain_put_by_act(a->goto_chain);
Jiri Pirkodb505142017-05-17 11:08:03 +020048}
49
50static void tcf_action_goto_chain_exec(const struct tc_action *a,
51 struct tcf_result *res)
52{
53 const struct tcf_chain *chain = a->goto_chain;
54
55 res->goto_tp = rcu_dereference_bh(chain->filter_chain);
56}
57
Vlad Busloveec94fd2018-07-05 17:24:23 +030058static void tcf_free_cookie_rcu(struct rcu_head *p)
59{
60 struct tc_cookie *cookie = container_of(p, struct tc_cookie, rcu);
61
62 kfree(cookie->data);
63 kfree(cookie);
64}
65
66static void tcf_set_action_cookie(struct tc_cookie __rcu **old_cookie,
67 struct tc_cookie *new_cookie)
68{
69 struct tc_cookie *old;
70
David S. Miller0dbc81e2018-07-08 17:02:59 +090071 old = xchg((__force struct tc_cookie **)old_cookie, new_cookie);
Vlad Busloveec94fd2018-07-05 17:24:23 +030072 if (old)
73 call_rcu(&old->rcu, tcf_free_cookie_rcu);
74}
75
Cong Wangd7fb60b2017-09-11 16:33:30 -070076/* XXX: For standalone actions, we don't need a RCU grace period either, because
77 * actions are always connected to filters and filters are already destroyed in
78 * RCU callbacks, so after a RCU grace period actions are already disconnected
79 * from filters. Readers later can not find us.
80 */
81static void free_tcf(struct tc_action *p)
Eric Dumazet519c8182015-07-06 05:18:04 -070082{
Eric Dumazet519c8182015-07-06 05:18:04 -070083 free_percpu(p->cpu_bstats);
84 free_percpu(p->cpu_qstats);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -050085
Vlad Busloveec94fd2018-07-05 17:24:23 +030086 tcf_set_action_cookie(&p->act_cookie, NULL);
Jiri Pirkodb505142017-05-17 11:08:03 +020087 if (p->goto_chain)
88 tcf_action_goto_chain_fini(p);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -050089
Eric Dumazet519c8182015-07-06 05:18:04 -070090 kfree(p);
91}
92
Vlad Buslov16af6062018-07-05 17:24:29 +030093static void tcf_action_cleanup(struct tc_action *p)
David S. Millere9ce1cd2006-08-21 23:54:55 -070094{
Vlad Buslov16af6062018-07-05 17:24:29 +030095 if (p->ops->cleanup)
96 p->ops->cleanup(p);
97
Eric Dumazet1c0d32f2016-12-04 09:48:16 -080098 gen_kill_estimator(&p->tcfa_rate_est);
Cong Wangd7fb60b2017-09-11 16:33:30 -070099 free_tcf(p);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700100}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700101
Vlad Buslov16af6062018-07-05 17:24:29 +0300102static int __tcf_action_put(struct tc_action *p, bool bind)
103{
104 struct tcf_idrinfo *idrinfo = p->idrinfo;
105
106 if (refcount_dec_and_lock(&p->tcfa_refcnt, &idrinfo->lock)) {
107 if (bind)
108 atomic_dec(&p->tcfa_bindcnt);
109 idr_remove(&idrinfo->action_idr, p->tcfa_index);
110 spin_unlock(&idrinfo->lock);
111
112 tcf_action_cleanup(p);
113 return 1;
114 }
115
116 if (bind)
117 atomic_dec(&p->tcfa_bindcnt);
118
119 return 0;
120}
121
Chris Mi65a206c2017-08-30 02:31:59 -0400122int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700123{
124 int ret = 0;
125
Vlad Buslov036bb442018-07-05 17:24:24 +0300126 /* Release with strict==1 and bind==0 is only called through act API
127 * interface (classifiers always bind). Only case when action with
128 * positive reference count and zero bind count can exist is when it was
129 * also created with act API (unbinding last classifier will destroy the
130 * action if it was created by classifier). So only case when bind count
131 * can be changed after initial check is when unbound action is
132 * destroyed by act API while classifier binds to action with same id
133 * concurrently. This result either creation of new action(same behavior
134 * as before), or reusing existing action if concurrent process
135 * increments reference count before action is deleted. Both scenarios
136 * are acceptable.
137 */
David S. Millere9ce1cd2006-08-21 23:54:55 -0700138 if (p) {
Vlad Buslov16af6062018-07-05 17:24:29 +0300139 if (!bind && strict && atomic_read(&p->tcfa_bindcnt) > 0)
WANG Cong55334a52014-02-11 17:07:34 -0800140 return -EPERM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700141
Vlad Buslov16af6062018-07-05 17:24:29 +0300142 if (__tcf_action_put(p, bind))
WANG Cong1d4150c2016-02-22 15:57:52 -0800143 ret = ACT_P_DELETED;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700144 }
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200145
David S. Millere9ce1cd2006-08-21 23:54:55 -0700146 return ret;
147}
Chris Mi65a206c2017-08-30 02:31:59 -0400148EXPORT_SYMBOL(__tcf_idr_release);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700149
Roman Mashak4e76e752018-03-08 16:59:19 -0500150static size_t tcf_action_shared_attrs_size(const struct tc_action *act)
151{
Vlad Buslove0479b62018-07-09 20:26:47 +0300152 struct tc_cookie *act_cookie;
Roman Mashak4e76e752018-03-08 16:59:19 -0500153 u32 cookie_len = 0;
154
Vlad Buslove0479b62018-07-09 20:26:47 +0300155 rcu_read_lock();
156 act_cookie = rcu_dereference(act->act_cookie);
157
158 if (act_cookie)
159 cookie_len = nla_total_size(act_cookie->len);
160 rcu_read_unlock();
Roman Mashak4e76e752018-03-08 16:59:19 -0500161
162 return nla_total_size(0) /* action number nested */
163 + nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */
164 + cookie_len /* TCA_ACT_COOKIE */
165 + nla_total_size(0) /* TCA_ACT_STATS nested */
166 /* TCA_STATS_BASIC */
167 + nla_total_size_64bit(sizeof(struct gnet_stats_basic))
168 /* TCA_STATS_QUEUE */
169 + nla_total_size_64bit(sizeof(struct gnet_stats_queue))
170 + nla_total_size(0) /* TCA_OPTIONS nested */
171 + nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */
172}
173
174static size_t tcf_action_full_attrs_size(size_t sz)
175{
176 return NLMSG_HDRLEN /* struct nlmsghdr */
177 + sizeof(struct tcamsg)
178 + nla_total_size(0) /* TCA_ACT_TAB nested */
179 + sz;
180}
181
182static size_t tcf_action_fill_size(const struct tc_action *act)
183{
184 size_t sz = tcf_action_shared_attrs_size(act);
185
186 if (act->ops->get_fill_size)
187 return act->ops->get_fill_size(act) + sz;
188 return sz;
189}
190
Chris Mi65a206c2017-08-30 02:31:59 -0400191static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700192 struct netlink_callback *cb)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700193{
Chris Mi65a206c2017-08-30 02:31:59 -0400194 int err = 0, index = -1, s_i = 0, n_i = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400195 u32 act_flags = cb->args[2];
Jamal Hadi Salime62e4842017-07-30 13:24:52 -0400196 unsigned long jiffy_since = cb->args[3];
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800197 struct nlattr *nest;
Chris Mi65a206c2017-08-30 02:31:59 -0400198 struct idr *idr = &idrinfo->action_idr;
199 struct tc_action *p;
200 unsigned long id = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700201
Vlad Buslov290aa0a2018-05-21 23:03:04 +0300202 spin_lock(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700203
204 s_i = cb->args[0];
205
Matthew Wilcox7a457572017-11-28 15:39:51 -0500206 idr_for_each_entry_ul(idr, p, id) {
Chris Mi65a206c2017-08-30 02:31:59 -0400207 index++;
208 if (index < s_i)
209 continue;
WANG Conga85a9702016-07-25 16:09:41 -0700210
Chris Mi65a206c2017-08-30 02:31:59 -0400211 if (jiffy_since &&
212 time_after(jiffy_since,
213 (unsigned long)p->tcfa_tm.lastuse))
214 continue;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700215
Chris Mi65a206c2017-08-30 02:31:59 -0400216 nest = nla_nest_start(skb, n_i);
Craig Dillabaugh734549e2018-03-26 14:58:32 -0400217 if (!nest) {
218 index--;
Chris Mi65a206c2017-08-30 02:31:59 -0400219 goto nla_put_failure;
Craig Dillabaugh734549e2018-03-26 14:58:32 -0400220 }
Chris Mi65a206c2017-08-30 02:31:59 -0400221 err = tcf_action_dump_1(skb, p, 0, 0);
222 if (err < 0) {
223 index--;
224 nlmsg_trim(skb, nest);
225 goto done;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700226 }
Chris Mi65a206c2017-08-30 02:31:59 -0400227 nla_nest_end(skb, nest);
228 n_i++;
229 if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) &&
230 n_i >= TCA_ACT_MAX_PRIO)
231 goto done;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700232 }
233done:
Jamal Hadi Salime62e4842017-07-30 13:24:52 -0400234 if (index >= 0)
235 cb->args[0] = index + 1;
236
Vlad Buslov290aa0a2018-05-21 23:03:04 +0300237 spin_unlock(&idrinfo->lock);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400238 if (n_i) {
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400239 if (act_flags & TCA_FLAG_LARGE_DUMP_ON)
240 cb->args[1] = n_i;
241 }
David S. Millere9ce1cd2006-08-21 23:54:55 -0700242 return n_i;
243
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800244nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800245 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700246 goto done;
247}
248
Chris Mi65a206c2017-08-30 02:31:59 -0400249static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700250 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700251{
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800252 struct nlattr *nest;
Chris Mi65a206c2017-08-30 02:31:59 -0400253 int n_i = 0;
WANG Cong55334a52014-02-11 17:07:34 -0800254 int ret = -EINVAL;
Chris Mi65a206c2017-08-30 02:31:59 -0400255 struct idr *idr = &idrinfo->action_idr;
256 struct tc_action *p;
257 unsigned long id = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700258
WANG Conga85a9702016-07-25 16:09:41 -0700259 nest = nla_nest_start(skb, 0);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800260 if (nest == NULL)
261 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700262 if (nla_put_string(skb, TCA_KIND, ops->kind))
David S. Miller1b34ec42012-03-29 05:11:39 -0400263 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700264
Matthew Wilcox7a457572017-11-28 15:39:51 -0500265 idr_for_each_entry_ul(idr, p, id) {
Chris Mi65a206c2017-08-30 02:31:59 -0400266 ret = __tcf_idr_release(p, false, true);
267 if (ret == ACT_P_DELETED) {
Jiri Pirko255cd502017-09-13 17:32:37 +0200268 module_put(ops->owner);
Chris Mi65a206c2017-08-30 02:31:59 -0400269 n_i++;
270 } else if (ret < 0) {
271 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700272 }
273 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400274 if (nla_put_u32(skb, TCA_FCNT, n_i))
275 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800276 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700277
278 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800279nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800280 nla_nest_cancel(skb, nest);
WANG Cong55334a52014-02-11 17:07:34 -0800281 return ret;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700282}
283
WANG Congddf97cc2016-02-22 15:57:53 -0800284int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
285 struct netlink_callback *cb, int type,
Alexander Aringb3620142018-02-15 10:54:59 -0500286 const struct tc_action_ops *ops,
287 struct netlink_ext_ack *extack)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700288{
Chris Mi65a206c2017-08-30 02:31:59 -0400289 struct tcf_idrinfo *idrinfo = tn->idrinfo;
WANG Congddf97cc2016-02-22 15:57:53 -0800290
David S. Millere9ce1cd2006-08-21 23:54:55 -0700291 if (type == RTM_DELACTION) {
Chris Mi65a206c2017-08-30 02:31:59 -0400292 return tcf_del_walker(idrinfo, skb, ops);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700293 } else if (type == RTM_GETACTION) {
Chris Mi65a206c2017-08-30 02:31:59 -0400294 return tcf_dump_walker(idrinfo, skb, cb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700295 } else {
Alexander Aringb3620142018-02-15 10:54:59 -0500296 WARN(1, "tcf_generic_walker: unknown command %d\n", type);
297 NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command");
David S. Millere9ce1cd2006-08-21 23:54:55 -0700298 return -EINVAL;
299 }
300}
WANG Congddf97cc2016-02-22 15:57:53 -0800301EXPORT_SYMBOL(tcf_generic_walker);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700302
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300303static bool __tcf_idr_check(struct tc_action_net *tn, u32 index,
304 struct tc_action **a, int bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700305{
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300306 struct tcf_idrinfo *idrinfo = tn->idrinfo;
307 struct tc_action *p;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700308
Vlad Buslov290aa0a2018-05-21 23:03:04 +0300309 spin_lock(&idrinfo->lock);
Matthew Wilcox322d8842017-11-28 10:01:24 -0500310 p = idr_find(&idrinfo->action_idr, index);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300311 if (IS_ERR(p)) {
312 p = NULL;
313 } else if (p) {
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300314 refcount_inc(&p->tcfa_refcnt);
315 if (bind)
316 atomic_inc(&p->tcfa_bindcnt);
317 }
Vlad Buslov290aa0a2018-05-21 23:03:04 +0300318 spin_unlock(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700319
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300320 if (p) {
321 *a = p;
322 return true;
323 }
324 return false;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700325}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700326
Chris Mi65a206c2017-08-30 02:31:59 -0400327int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700328{
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300329 return __tcf_idr_check(tn, index, a, 0);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700330}
Chris Mi65a206c2017-08-30 02:31:59 -0400331EXPORT_SYMBOL(tcf_idr_search);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700332
Chris Mi65a206c2017-08-30 02:31:59 -0400333bool tcf_idr_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
334 int bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700335{
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300336 return __tcf_idr_check(tn, index, a, bind);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700337}
Chris Mi65a206c2017-08-30 02:31:59 -0400338EXPORT_SYMBOL(tcf_idr_check);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700339
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300340int tcf_idr_delete_index(struct tc_action_net *tn, u32 index)
341{
342 struct tcf_idrinfo *idrinfo = tn->idrinfo;
343 struct tc_action *p;
344 int ret = 0;
345
346 spin_lock(&idrinfo->lock);
347 p = idr_find(&idrinfo->action_idr, index);
348 if (!p) {
349 spin_unlock(&idrinfo->lock);
350 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));
359 spin_unlock(&idrinfo->lock);
360
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
370 spin_unlock(&idrinfo->lock);
371 return ret;
372}
373EXPORT_SYMBOL(tcf_idr_delete_index);
374
Chris Mi65a206c2017-08-30 02:31:59 -0400375int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
376 struct tc_action **a, const struct tc_action_ops *ops,
377 int bind, bool cpustats)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700378{
WANG Congec0595c2016-07-25 16:09:42 -0700379 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
Chris Mi65a206c2017-08-30 02:31:59 -0400380 struct tcf_idrinfo *idrinfo = tn->idrinfo;
Eric Dumazet519c8182015-07-06 05:18:04 -0700381 int err = -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700382
383 if (unlikely(!p))
WANG Cong86062032014-02-11 17:07:31 -0800384 return -ENOMEM;
Vlad Buslov036bb442018-07-05 17:24:24 +0300385 refcount_set(&p->tcfa_refcnt, 1);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700386 if (bind)
Vlad Buslov036bb442018-07-05 17:24:24 +0300387 atomic_set(&p->tcfa_bindcnt, 1);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700388
Eric Dumazet519c8182015-07-06 05:18:04 -0700389 if (cpustats) {
390 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500391 if (!p->cpu_bstats)
Eric Dumazet519c8182015-07-06 05:18:04 -0700392 goto err1;
Matthew Wilcox339913a2017-11-28 10:28:15 -0500393 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
394 if (!p->cpu_qstats)
395 goto err2;
Eric Dumazet519c8182015-07-06 05:18:04 -0700396 }
WANG Congec0595c2016-07-25 16:09:42 -0700397 spin_lock_init(&p->tcfa_lock);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500398 p->tcfa_index = index;
WANG Congec0595c2016-07-25 16:09:42 -0700399 p->tcfa_tm.install = jiffies;
400 p->tcfa_tm.lastuse = jiffies;
401 p->tcfa_tm.firstuse = 0;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800402 if (est) {
WANG Congec0595c2016-07-25 16:09:42 -0700403 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
404 &p->tcfa_rate_est,
405 &p->tcfa_lock, NULL, est);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500406 if (err)
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300407 goto err3;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800408 }
409
Chris Mi65a206c2017-08-30 02:31:59 -0400410 p->idrinfo = idrinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700411 p->ops = ops;
412 INIT_LIST_HEAD(&p->list);
413 *a = p;
WANG Cong86062032014-02-11 17:07:31 -0800414 return 0;
Matthew Wilcox339913a2017-11-28 10:28:15 -0500415err3:
416 free_percpu(p->cpu_qstats);
417err2:
418 free_percpu(p->cpu_bstats);
419err1:
420 kfree(p);
421 return err;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700422}
Chris Mi65a206c2017-08-30 02:31:59 -0400423EXPORT_SYMBOL(tcf_idr_create);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700424
Chris Mi65a206c2017-08-30 02:31:59 -0400425void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700426{
Chris Mi65a206c2017-08-30 02:31:59 -0400427 struct tcf_idrinfo *idrinfo = tn->idrinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700428
Vlad Buslov290aa0a2018-05-21 23:03:04 +0300429 spin_lock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300430 /* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
431 WARN_ON(!IS_ERR(idr_replace(&idrinfo->action_idr, a, a->tcfa_index)));
Vlad Buslov290aa0a2018-05-21 23:03:04 +0300432 spin_unlock(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700433}
Chris Mi65a206c2017-08-30 02:31:59 -0400434EXPORT_SYMBOL(tcf_idr_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300436/* Cleanup idr index that was allocated but not initialized. */
437
438void tcf_idr_cleanup(struct tc_action_net *tn, u32 index)
439{
440 struct tcf_idrinfo *idrinfo = tn->idrinfo;
441
442 spin_lock(&idrinfo->lock);
443 /* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
444 WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index)));
445 spin_unlock(&idrinfo->lock);
446}
447EXPORT_SYMBOL(tcf_idr_cleanup);
448
449/* Check if action with specified index exists. If actions is found, increments
450 * its reference and bind counters, and return 1. Otherwise insert temporary
451 * error pointer (to prevent concurrent users from inserting actions with same
452 * index) and return 0.
453 */
454
455int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
456 struct tc_action **a, int bind)
457{
458 struct tcf_idrinfo *idrinfo = tn->idrinfo;
459 struct tc_action *p;
460 int ret;
461
462again:
463 spin_lock(&idrinfo->lock);
464 if (*index) {
465 p = idr_find(&idrinfo->action_idr, *index);
466 if (IS_ERR(p)) {
467 /* This means that another process allocated
468 * index but did not assign the pointer yet.
469 */
470 spin_unlock(&idrinfo->lock);
471 goto again;
472 }
473
474 if (p) {
475 refcount_inc(&p->tcfa_refcnt);
476 if (bind)
477 atomic_inc(&p->tcfa_bindcnt);
478 *a = p;
479 ret = 1;
480 } else {
481 *a = NULL;
482 ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
483 *index, GFP_ATOMIC);
484 if (!ret)
485 idr_replace(&idrinfo->action_idr,
486 ERR_PTR(-EBUSY), *index);
487 }
488 } else {
489 *index = 1;
490 *a = NULL;
491 ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
492 UINT_MAX, GFP_ATOMIC);
493 if (!ret)
494 idr_replace(&idrinfo->action_idr, ERR_PTR(-EBUSY),
495 *index);
496 }
497 spin_unlock(&idrinfo->lock);
498 return ret;
499}
500EXPORT_SYMBOL(tcf_idr_check_alloc);
501
Chris Mi65a206c2017-08-30 02:31:59 -0400502void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
503 struct tcf_idrinfo *idrinfo)
WANG Cong1d4150c2016-02-22 15:57:52 -0800504{
Chris Mi65a206c2017-08-30 02:31:59 -0400505 struct idr *idr = &idrinfo->action_idr;
506 struct tc_action *p;
507 int ret;
508 unsigned long id = 1;
WANG Cong1d4150c2016-02-22 15:57:52 -0800509
Matthew Wilcox7a457572017-11-28 15:39:51 -0500510 idr_for_each_entry_ul(idr, p, id) {
Chris Mi65a206c2017-08-30 02:31:59 -0400511 ret = __tcf_idr_release(p, false, true);
512 if (ret == ACT_P_DELETED)
513 module_put(ops->owner);
514 else if (ret < 0)
515 return;
WANG Cong1d4150c2016-02-22 15:57:52 -0800516 }
Chris Mi65a206c2017-08-30 02:31:59 -0400517 idr_destroy(&idrinfo->action_idr);
WANG Cong1d4150c2016-02-22 15:57:52 -0800518}
Chris Mi65a206c2017-08-30 02:31:59 -0400519EXPORT_SYMBOL(tcf_idrinfo_destroy);
WANG Cong1d4150c2016-02-22 15:57:52 -0800520
WANG Cong1f747c22013-12-15 20:15:10 -0800521static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522static DEFINE_RWLOCK(act_mod_lock);
523
WANG Congddf97cc2016-02-22 15:57:53 -0800524int tcf_register_action(struct tc_action_ops *act,
525 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
WANG Cong1f747c22013-12-15 20:15:10 -0800527 struct tc_action_ops *a;
WANG Congddf97cc2016-02-22 15:57:53 -0800528 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
WANG Congddf97cc2016-02-22 15:57:53 -0800530 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500531 return -EINVAL;
532
WANG Congab102b82016-10-11 10:56:45 -0700533 /* We have to register pernet ops before making the action ops visible,
534 * otherwise tcf_action_init_1() could get a partially initialized
535 * netns.
536 */
537 ret = register_pernet_subsys(ops);
538 if (ret)
539 return ret;
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800542 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
544 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700545 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return -EEXIST;
547 }
548 }
WANG Cong1f747c22013-12-15 20:15:10 -0800549 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 write_unlock(&act_mod_lock);
WANG Congddf97cc2016-02-22 15:57:53 -0800551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return 0;
553}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800554EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
WANG Congddf97cc2016-02-22 15:57:53 -0800556int tcf_unregister_action(struct tc_action_ops *act,
557 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
WANG Cong1f747c22013-12-15 20:15:10 -0800559 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 int err = -ENOENT;
561
562 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800563 list_for_each_entry(a, &act_base, head) {
564 if (a == act) {
565 list_del(&act->head);
566 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700571 if (!err)
572 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return err;
574}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800575EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577/* lookup by name */
578static struct tc_action_ops *tc_lookup_action_n(char *kind)
579{
Eric Dumazeta7928662013-12-20 12:32:32 -0800580 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 if (kind) {
583 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800584 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800586 if (try_module_get(a->owner))
587 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 break;
589 }
590 }
591 read_unlock(&act_mod_lock);
592 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800593 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800596/* lookup by nlattr */
597static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Eric Dumazeta7928662013-12-20 12:32:32 -0800599 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 if (kind) {
602 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800603 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800604 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800605 if (try_module_get(a->owner))
606 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 break;
608 }
609 }
610 read_unlock(&act_mod_lock);
611 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800612 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400615/*TCA_ACT_MAX_PRIO is 32, there count upto 32 */
616#define TCA_ACT_MAX_PRIO_MASK 0x1FF
WANG Cong22dc13c2016-08-13 22:35:00 -0700617int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
618 int nr_actions, struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400620 u32 jmp_prgcnt = 0;
621 u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
Jiri Pirkoec1a9cc2017-08-04 14:29:02 +0200622 int i;
623 int ret = TC_ACT_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Willem de Bruijne7246e12017-01-07 17:06:35 -0500625 if (skb_skip_tc_classify(skb))
626 return TC_ACT_OK;
627
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400628restart_act_graph:
WANG Cong22dc13c2016-08-13 22:35:00 -0700629 for (i = 0; i < nr_actions; i++) {
630 const struct tc_action *a = actions[i];
631
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400632 if (jmp_prgcnt > 0) {
633 jmp_prgcnt -= 1;
634 continue;
635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500637 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500638 if (ret == TC_ACT_REPEAT)
639 goto repeat; /* we need a ttl - JHS */
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400640
Jiri Pirko9da32422017-05-02 10:12:00 +0200641 if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400642 jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
643 if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
644 /* faulty opcode, stop pipeline */
645 return TC_ACT_OK;
646 } else {
647 jmp_ttl -= 1;
648 if (jmp_ttl > 0)
649 goto restart_act_graph;
650 else /* faulty graph, stop pipeline */
651 return TC_ACT_OK;
652 }
Jiri Pirkodb505142017-05-17 11:08:03 +0200653 } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
654 tcf_action_goto_chain_exec(a, res);
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400655 }
656
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500657 if (ret != TC_ACT_PIPE)
Willem de Bruijne7246e12017-01-07 17:06:35 -0500658 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return ret;
662}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800663EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Vlad Buslov90b73b72018-07-05 17:24:33 +0300665int tcf_action_destroy(struct tc_action *actions[], int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Jiri Pirko255cd502017-09-13 17:32:37 +0200667 const struct tc_action_ops *ops;
Vlad Buslov90b73b72018-07-05 17:24:33 +0300668 struct tc_action *a;
669 int ret = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Vlad Buslov90b73b72018-07-05 17:24:33 +0300671 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
672 a = actions[i];
673 actions[i] = NULL;
Jiri Pirko255cd502017-09-13 17:32:37 +0200674 ops = a->ops;
Chris Mi65a206c2017-08-30 02:31:59 -0400675 ret = __tcf_idr_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800676 if (ret == ACT_P_DELETED)
Jiri Pirko255cd502017-09-13 17:32:37 +0200677 module_put(ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800678 else if (ret < 0)
679 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
WANG Cong55334a52014-02-11 17:07:34 -0800681 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
Vlad Buslov16af6062018-07-05 17:24:29 +0300684static int tcf_action_put(struct tc_action *p)
685{
686 return __tcf_action_put(p, false);
687}
688
Cong Wangedfaf942018-08-19 12:22:05 -0700689/* Put all actions in this array, skip those NULL's. */
Vlad Buslov90b73b72018-07-05 17:24:33 +0300690static void tcf_action_put_many(struct tc_action *actions[])
Vlad Buslovcae422f2018-07-05 17:24:31 +0300691{
Vlad Buslov90b73b72018-07-05 17:24:33 +0300692 int i;
Vlad Buslovcae422f2018-07-05 17:24:31 +0300693
Cong Wangedfaf942018-08-19 12:22:05 -0700694 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
Vlad Buslov90b73b72018-07-05 17:24:33 +0300695 struct tc_action *a = actions[i];
Cong Wangedfaf942018-08-19 12:22:05 -0700696 const struct tc_action_ops *ops;
Vlad Buslovcae422f2018-07-05 17:24:31 +0300697
Cong Wangedfaf942018-08-19 12:22:05 -0700698 if (!a)
699 continue;
700 ops = a->ops;
Vlad Buslovcae422f2018-07-05 17:24:31 +0300701 if (tcf_action_put(a))
702 module_put(ops->owner);
703 }
704}
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706int
707tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
708{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return a->ops->dump(skb, a, bind, ref);
710}
711
712int
713tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
714{
715 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700716 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800717 struct nlattr *nest;
Vlad Busloveec94fd2018-07-05 17:24:23 +0300718 struct tc_cookie *cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
David S. Miller1b34ec42012-03-29 05:11:39 -0400720 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
721 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800723 goto nla_put_failure;
Vlad Busloveec94fd2018-07-05 17:24:23 +0300724
725 rcu_read_lock();
726 cookie = rcu_dereference(a->act_cookie);
727 if (cookie) {
728 if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) {
729 rcu_read_unlock();
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500730 goto nla_put_failure;
Vlad Busloveec94fd2018-07-05 17:24:23 +0300731 }
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500732 }
Vlad Busloveec94fd2018-07-05 17:24:23 +0300733 rcu_read_unlock();
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500734
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800735 nest = nla_nest_start(skb, TCA_OPTIONS);
736 if (nest == NULL)
737 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000738 err = tcf_action_dump_old(skb, a, bind, ref);
739 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800740 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return err;
742 }
743
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800744nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700745 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return -1;
747}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800748EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Vlad Buslov90b73b72018-07-05 17:24:33 +0300750int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[],
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400751 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 struct tc_action *a;
Vlad Buslov90b73b72018-07-05 17:24:33 +0300754 int err = -EINVAL, i;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800755 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Vlad Buslov90b73b72018-07-05 17:24:33 +0300757 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
758 a = actions[i];
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800759 nest = nla_nest_start(skb, a->order);
760 if (nest == NULL)
761 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 err = tcf_action_dump_1(skb, a, bind, ref);
763 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700764 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800765 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767
768 return 0;
769
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800770nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700771 err = -EINVAL;
772errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800773 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700774 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200777static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500778{
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200779 struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
780 if (!c)
781 return NULL;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500782
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200783 c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
784 if (!c->data) {
785 kfree(c);
786 return NULL;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500787 }
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200788 c->len = nla_len(tb[TCA_ACT_COOKIE]);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500789
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200790 return c;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500791}
792
Paolo Abeni802bfb12018-07-30 14:30:42 +0200793static bool tcf_action_valid(int action)
794{
795 int opcode = TC_ACT_EXT_OPCODE(action);
796
797 if (!opcode)
798 return action <= TC_ACT_VALUE_MAX;
799 return opcode <= TC_ACT_EXT_OPCODE_MAX || action == TC_ACT_UNSPEC;
800}
801
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200802struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
803 struct nlattr *nla, struct nlattr *est,
Alexander Aringaea0d722018-02-15 10:54:54 -0500804 char *name, int ovr, int bind,
Vlad Buslov789871b2018-07-05 17:24:25 +0300805 bool rtnl_held,
Alexander Aringaea0d722018-02-15 10:54:54 -0500806 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 struct tc_action *a;
809 struct tc_action_ops *a_o;
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200810 struct tc_cookie *cookie = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000812 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800813 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800814 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (name == NULL) {
Alexander Aring84ae0172018-02-15 10:54:55 -0500817 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
Patrick McHardycee63722008-01-23 20:33:32 -0800818 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800820 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800821 kind = tb[TCA_ACT_KIND];
Alexander Aring84ae0172018-02-15 10:54:55 -0500822 if (!kind) {
823 NL_SET_ERR_MSG(extack, "TC action kind must be specified");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -0500825 }
826 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) {
827 NL_SET_ERR_MSG(extack, "TC action name too long");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -0500829 }
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200830 if (tb[TCA_ACT_COOKIE]) {
831 int cklen = nla_len(tb[TCA_ACT_COOKIE]);
832
Alexander Aring84ae0172018-02-15 10:54:55 -0500833 if (cklen > TC_COOKIE_MAX_SIZE) {
834 NL_SET_ERR_MSG(extack, "TC cookie size above the maximum");
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200835 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -0500836 }
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200837
838 cookie = nla_memdup_cookie(tb);
839 if (!cookie) {
Alexander Aring84ae0172018-02-15 10:54:55 -0500840 NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200841 err = -ENOMEM;
842 goto err_out;
843 }
844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 } else {
Alexander Aring84ae0172018-02-15 10:54:55 -0500846 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) {
847 NL_SET_ERR_MSG(extack, "TC action name too long");
848 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -0500850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852
853 a_o = tc_lookup_action_n(act_name);
854 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700855#ifdef CONFIG_MODULES
Vlad Buslov789871b2018-07-05 17:24:25 +0300856 if (rtnl_held)
857 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800858 request_module("act_%s", act_name);
Vlad Buslov789871b2018-07-05 17:24:25 +0300859 if (rtnl_held)
860 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 a_o = tc_lookup_action_n(act_name);
863
864 /* We dropped the RTNL semaphore in order to
865 * perform the module load. So, even if we
866 * succeeded in loading the module we have to
867 * tell the caller to replay the request. We
868 * indicate this using -EAGAIN.
869 */
870 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800871 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 goto err_mod;
873 }
874#endif
Alexander Aring84ae0172018-02-15 10:54:55 -0500875 NL_SET_ERR_MSG(extack, "Failed to load TC action module");
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800876 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 goto err_out;
878 }
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /* backward compatibility for policer */
881 if (name == NULL)
Alexander Aring589dad62018-02-15 10:54:56 -0500882 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind,
Vlad Buslov789871b2018-07-05 17:24:25 +0300883 rtnl_held, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 else
Vlad Buslov789871b2018-07-05 17:24:25 +0300885 err = a_o->init(net, nla, est, &a, ovr, bind, rtnl_held,
886 extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800887 if (err < 0)
WANG Conga85a9702016-07-25 16:09:41 -0700888 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Vlad Busloveec94fd2018-07-05 17:24:23 +0300890 if (!name && tb[TCA_ACT_COOKIE])
891 tcf_set_action_cookie(&a->act_cookie, cookie);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000894 * if it exists and is only bound to in a_o->init() then
895 * ACT_P_CREATED is not returned (a zero is).
896 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800897 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Jiri Pirkodb505142017-05-17 11:08:03 +0200900 if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) {
901 err = tcf_action_goto_chain_init(a, tp);
902 if (err) {
Vlad Buslov90b73b72018-07-05 17:24:33 +0300903 struct tc_action *actions[] = { a, NULL };
Jiri Pirkodb505142017-05-17 11:08:03 +0200904
Vlad Buslov90b73b72018-07-05 17:24:33 +0300905 tcf_action_destroy(actions, bind);
Alexander Aring84ae0172018-02-15 10:54:55 -0500906 NL_SET_ERR_MSG(extack, "Failed to init TC action chain");
Jiri Pirkodb505142017-05-17 11:08:03 +0200907 return ERR_PTR(err);
908 }
909 }
910
Paolo Abeni802bfb12018-07-30 14:30:42 +0200911 if (!tcf_action_valid(a->tcfa_action)) {
912 NL_SET_ERR_MSG(extack, "invalid action value, using TC_ACT_UNSPEC instead");
913 a->tcfa_action = TC_ACT_UNSPEC;
914 }
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return a;
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918err_mod:
919 module_put(a_o->owner);
920err_out:
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200921 if (cookie) {
922 kfree(cookie->data);
923 kfree(cookie);
924 }
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800925 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
Vlad Buslov90b73b72018-07-05 17:24:33 +0300928/* Returns numbers of initialized actions or negative error. */
929
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200930int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
931 struct nlattr *est, char *name, int ovr, int bind,
Vlad Buslov90b73b72018-07-05 17:24:33 +0300932 struct tc_action *actions[], size_t *attr_size,
Vlad Buslov789871b2018-07-05 17:24:25 +0300933 bool rtnl_held, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000935 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800936 struct tc_action *act;
Roman Mashak4e76e752018-03-08 16:59:19 -0500937 size_t sz = 0;
Patrick McHardycee63722008-01-23 20:33:32 -0800938 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 int i;
940
Alexander Aring84ae0172018-02-15 10:54:55 -0500941 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack);
Patrick McHardycee63722008-01-23 20:33:32 -0800942 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800943 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800945 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Alexander Aringaea0d722018-02-15 10:54:54 -0500946 act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind,
Vlad Buslov789871b2018-07-05 17:24:25 +0300947 rtnl_held, extack);
WANG Cong33be6272013-12-15 20:15:05 -0800948 if (IS_ERR(act)) {
949 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800951 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800952 act->order = i;
Roman Mashak4e76e752018-03-08 16:59:19 -0500953 sz += tcf_action_fill_size(act);
Vlad Buslov90b73b72018-07-05 17:24:33 +0300954 /* Start from index 0 */
955 actions[i - 1] = act;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400957
Roman Mashak4e76e752018-03-08 16:59:19 -0500958 *attr_size = tcf_action_full_attrs_size(sz);
Vlad Buslov90b73b72018-07-05 17:24:33 +0300959 return i - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961err:
WANG Cong33be6272013-12-15 20:15:05 -0800962 tcf_action_destroy(actions, bind);
963 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
WANG Congec0595c2016-07-25 16:09:42 -0700966int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 int compat_mode)
968{
969 int err = 0;
970 struct gnet_dump d;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900971
WANG Cong7eb88962014-01-09 16:14:05 -0800972 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 goto errout;
974
975 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400976 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 */
978 if (compat_mode) {
WANG Congec0595c2016-07-25 16:09:42 -0700979 if (p->type == TCA_OLD_COMPAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 err = gnet_stats_start_copy_compat(skb, 0,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200981 TCA_STATS,
982 TCA_XSTATS,
WANG Congec0595c2016-07-25 16:09:42 -0700983 &p->tcfa_lock, &d,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200984 TCA_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 else
986 return 0;
987 } else
988 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Congec0595c2016-07-25 16:09:42 -0700989 &p->tcfa_lock, &d, TCA_ACT_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 if (err < 0)
992 goto errout;
993
WANG Congec0595c2016-07-25 16:09:42 -0700994 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800995 gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -0700996 gnet_stats_copy_queue(&d, p->cpu_qstats,
WANG Congec0595c2016-07-25 16:09:42 -0700997 &p->tcfa_qstats,
998 p->tcfa_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 goto errout;
1000
1001 if (gnet_stats_finish_copy(&d) < 0)
1002 goto errout;
1003
1004 return 0;
1005
1006errout:
1007 return -1;
1008}
1009
Vlad Buslov90b73b72018-07-05 17:24:33 +03001010static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[],
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001011 u32 portid, u32 seq, u16 flags, int event, int bind,
1012 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 struct tcamsg *t;
1015 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001016 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001017 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Eric W. Biederman15e47302012-09-07 20:12:54 +00001019 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -07001020 if (!nlh)
1021 goto out_nlmsg_trim;
1022 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001024 t->tca__pad1 = 0;
1025 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001026
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001027 nest = nla_nest_start(skb, TCA_ACT_TAB);
Alexander Aring1af8515582018-02-15 10:54:53 -05001028 if (!nest)
David S. Miller8b00a532012-06-26 21:39:32 -07001029 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
WANG Cong33be6272013-12-15 20:15:05 -08001031 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001032 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001034 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001035
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001036 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return skb->len;
1038
David S. Miller8b00a532012-06-26 21:39:32 -07001039out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001040 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return -1;
1042}
1043
1044static int
Roman Mashakc4c42902017-07-13 13:12:18 -04001045tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
Vlad Buslov90b73b72018-07-05 17:24:33 +03001046 struct tc_action *actions[], int event,
Alexander Aring84ae0172018-02-15 10:54:55 -05001047 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
1049 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1052 if (!skb)
1053 return -ENOBUFS;
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001054 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
Vlad Buslov3f7c72b2018-07-05 17:24:26 +03001055 0, 1) <= 0) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001056 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 kfree_skb(skb);
1058 return -EINVAL;
1059 }
Thomas Graf2942e902006-08-15 00:30:25 -07001060
Eric W. Biederman15e47302012-09-07 20:12:54 +00001061 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
WANG Congddf97cc2016-02-22 15:57:53 -08001064static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
Alexander Aring84ae0172018-02-15 10:54:55 -05001065 struct nlmsghdr *n, u32 portid,
1066 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001068 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -07001069 const struct tc_action_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 struct tc_action *a;
1071 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001072 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Alexander Aring84ae0172018-02-15 10:54:55 -05001074 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001075 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001076 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Patrick McHardycee63722008-01-23 20:33:32 -08001078 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001079 if (tb[TCA_ACT_INDEX] == NULL ||
Alexander Aring84ae0172018-02-15 10:54:55 -05001080 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
1081 NL_SET_ERR_MSG(extack, "Invalid TC action index value");
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001082 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -05001083 }
Patrick McHardy1587bac2008-01-23 20:35:03 -08001084 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001086 err = -EINVAL;
WANG Conga85a9702016-07-25 16:09:41 -07001087 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Alexander Aring84ae0172018-02-15 10:54:55 -05001088 if (!ops) { /* could happen in batch of actions */
1089 NL_SET_ERR_MSG(extack, "Specified TC action not found");
WANG Conga85a9702016-07-25 16:09:41 -07001090 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -05001091 }
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001092 err = -ENOENT;
Alexander Aring331a9292018-02-15 10:54:57 -05001093 if (ops->lookup(net, &a, index, extack) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 goto err_mod;
1095
WANG Conga85a9702016-07-25 16:09:41 -07001096 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099err_mod:
WANG Conga85a9702016-07-25 16:09:41 -07001100 module_put(ops->owner);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001101err_out:
1102 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
Tom Goff7316ae82010-03-19 15:40:13 +00001105static int tca_action_flush(struct net *net, struct nlattr *nla,
Alexander Aring84ae0172018-02-15 10:54:55 -05001106 struct nlmsghdr *n, u32 portid,
1107 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
1109 struct sk_buff *skb;
1110 unsigned char *b;
1111 struct nlmsghdr *nlh;
1112 struct tcamsg *t;
1113 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001114 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001115 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -07001116 const struct tc_action_ops *ops;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001117 struct nlattr *kind;
Jamal Hadi Salim36723872008-08-13 02:41:45 -07001118 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
Alexander Aring84ae0172018-02-15 10:54:55 -05001121 if (!skb)
Jamal Hadi Salim36723872008-08-13 02:41:45 -07001122 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001124 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Alexander Aring84ae0172018-02-15 10:54:55 -05001126 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001127 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 goto err_out;
1129
Patrick McHardycee63722008-01-23 20:33:32 -08001130 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001131 kind = tb[TCA_ACT_KIND];
WANG Conga85a9702016-07-25 16:09:41 -07001132 ops = tc_lookup_action(kind);
Alexander Aring84ae0172018-02-15 10:54:55 -05001133 if (!ops) { /*some idjot trying to flush unknown action */
1134 NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -05001136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001138 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
1139 sizeof(*t), 0);
Alexander Aring84ae0172018-02-15 10:54:55 -05001140 if (!nlh) {
1141 NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
David S. Miller8b00a532012-06-26 21:39:32 -07001142 goto out_module_put;
Alexander Aring84ae0172018-02-15 10:54:55 -05001143 }
David S. Miller8b00a532012-06-26 21:39:32 -07001144 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001146 t->tca__pad1 = 0;
1147 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001149 nest = nla_nest_start(skb, TCA_ACT_TAB);
Alexander Aring84ae0172018-02-15 10:54:55 -05001150 if (!nest) {
1151 NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
David S. Miller8b00a532012-06-26 21:39:32 -07001152 goto out_module_put;
Alexander Aring84ae0172018-02-15 10:54:55 -05001153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Alexander Aring41780102018-02-15 10:54:58 -05001155 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack);
Davide Caratti66dede22018-02-15 15:50:57 +01001156 if (err <= 0) {
1157 nla_nest_cancel(skb, nest);
David S. Miller8b00a532012-06-26 21:39:32 -07001158 goto out_module_put;
Davide Caratti66dede22018-02-15 15:50:57 +01001159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001161 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001163 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Conga85a9702016-07-25 16:09:41 -07001165 module_put(ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001166 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001167 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (err > 0)
1169 return 0;
Alexander Aring84ae0172018-02-15 10:54:55 -05001170 if (err < 0)
1171 NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 return err;
1174
David S. Miller8b00a532012-06-26 21:39:32 -07001175out_module_put:
WANG Conga85a9702016-07-25 16:09:41 -07001176 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177err_out:
1178 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return err;
1180}
1181
Vlad Buslov90b73b72018-07-05 17:24:33 +03001182static int tcf_action_delete(struct net *net, struct tc_action *actions[],
Cong Wangedfaf942018-08-19 12:22:05 -07001183 struct netlink_ext_ack *extack)
Vlad Buslov16af6062018-07-05 17:24:29 +03001184{
Vlad Buslov16af6062018-07-05 17:24:29 +03001185 u32 act_index;
Vlad Buslov90b73b72018-07-05 17:24:33 +03001186 int ret, i;
Vlad Buslov16af6062018-07-05 17:24:29 +03001187
Vlad Buslov90b73b72018-07-05 17:24:33 +03001188 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
1189 struct tc_action *a = actions[i];
Vlad Buslov16af6062018-07-05 17:24:29 +03001190 const struct tc_action_ops *ops = a->ops;
1191
1192 /* Actions can be deleted concurrently so we must save their
1193 * type and id to search again after reference is released.
1194 */
1195 act_index = a->tcfa_index;
1196
Vlad Buslov16af6062018-07-05 17:24:29 +03001197 if (tcf_action_put(a)) {
1198 /* last reference, action was deleted concurrently */
1199 module_put(ops->owner);
1200 } else {
1201 /* now do the delete */
1202 ret = ops->delete(net, act_index);
Cong Wangedfaf942018-08-19 12:22:05 -07001203 if (ret < 0)
Vlad Buslov16af6062018-07-05 17:24:29 +03001204 return ret;
1205 }
Cong Wangedfaf942018-08-19 12:22:05 -07001206 actions[i] = NULL;
Vlad Buslov16af6062018-07-05 17:24:29 +03001207 }
1208 return 0;
1209}
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211static int
Vlad Buslov90b73b72018-07-05 17:24:33 +03001212tcf_del_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
Cong Wangedfaf942018-08-19 12:22:05 -07001213 u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
WANG Conga56e1952014-01-09 16:14:00 -08001214{
1215 int ret;
1216 struct sk_buff *skb;
1217
Roman Mashakd04e6992018-03-08 16:59:17 -05001218 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1219 GFP_KERNEL);
WANG Conga56e1952014-01-09 16:14:00 -08001220 if (!skb)
1221 return -ENOBUFS;
1222
1223 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
Vlad Buslov3f7c72b2018-07-05 17:24:26 +03001224 0, 2) <= 0) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001225 NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
WANG Conga56e1952014-01-09 16:14:00 -08001226 kfree_skb(skb);
1227 return -EINVAL;
1228 }
1229
1230 /* now do the delete */
Cong Wangedfaf942018-08-19 12:22:05 -07001231 ret = tcf_action_delete(net, actions, extack);
WANG Cong55334a52014-02-11 17:07:34 -08001232 if (ret < 0) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001233 NL_SET_ERR_MSG(extack, "Failed to delete TC action");
WANG Cong55334a52014-02-11 17:07:34 -08001234 kfree_skb(skb);
1235 return ret;
1236 }
WANG Conga56e1952014-01-09 16:14:00 -08001237
1238 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1239 n->nlmsg_flags & NLM_F_ECHO);
1240 if (ret > 0)
1241 return 0;
1242 return ret;
1243}
1244
1245static int
Tom Goff7316ae82010-03-19 15:40:13 +00001246tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Alexander Aring84ae0172018-02-15 10:54:55 -05001247 u32 portid, int event, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
Patrick McHardycee63722008-01-23 20:33:32 -08001249 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001250 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -08001251 struct tc_action *act;
Roman Mashakd04e6992018-03-08 16:59:17 -05001252 size_t attr_size = 0;
Cong Wangedfaf942018-08-19 12:22:05 -07001253 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Alexander Aring84ae0172018-02-15 10:54:55 -05001255 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001256 if (ret < 0)
1257 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001259 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Alexander Aring1af8515582018-02-15 10:54:53 -05001260 if (tb[1])
Alexander Aring84ae0172018-02-15 10:54:55 -05001261 return tca_action_flush(net, tb[1], n, portid, extack);
Alexander Aring1af8515582018-02-15 10:54:53 -05001262
Alexander Aring84ae0172018-02-15 10:54:55 -05001263 NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
Alexander Aring1af8515582018-02-15 10:54:53 -05001264 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001267 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001268 act = tcf_action_get_1(net, tb[i], n, portid, extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001269 if (IS_ERR(act)) {
1270 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001272 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001273 act->order = i;
Roman Mashak4e76e752018-03-08 16:59:19 -05001274 attr_size += tcf_action_fill_size(act);
Vlad Buslov90b73b72018-07-05 17:24:33 +03001275 actions[i - 1] = act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
1277
Roman Mashak4e76e752018-03-08 16:59:19 -05001278 attr_size = tcf_action_full_attrs_size(attr_size);
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (event == RTM_GETACTION)
Vlad Buslov90b73b72018-07-05 17:24:33 +03001281 ret = tcf_get_notify(net, portid, n, actions, event, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 else { /* delete */
Cong Wangedfaf942018-08-19 12:22:05 -07001283 ret = tcf_del_notify(net, n, actions, portid, attr_size, extack);
WANG Conga56e1952014-01-09 16:14:00 -08001284 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 goto err;
Cong Wangedfaf942018-08-19 12:22:05 -07001286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288err:
Cong Wangedfaf942018-08-19 12:22:05 -07001289 tcf_action_put_many(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return ret;
1291}
1292
WANG Conga56e1952014-01-09 16:14:00 -08001293static int
Vlad Buslov90b73b72018-07-05 17:24:33 +03001294tcf_add_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
Roman Mashakd04e6992018-03-08 16:59:17 -05001295 u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 int err = 0;
1299
Roman Mashakd04e6992018-03-08 16:59:17 -05001300 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1301 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (!skb)
1303 return -ENOBUFS;
1304
WANG Conga56e1952014-01-09 16:14:00 -08001305 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
1306 RTM_NEWACTION, 0, 0) <= 0) {
Roman Mashakd143b9e2018-03-02 20:52:01 -05001307 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
WANG Conga56e1952014-01-09 16:14:00 -08001308 kfree_skb(skb);
1309 return -EINVAL;
1310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
WANG Conga56e1952014-01-09 16:14:00 -08001312 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1313 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (err > 0)
1315 err = 0;
1316 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
1318
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001319static int tcf_action_add(struct net *net, struct nlattr *nla,
Alexander Aringaea0d722018-02-15 10:54:54 -05001320 struct nlmsghdr *n, u32 portid, int ovr,
1321 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Roman Mashakd04e6992018-03-08 16:59:17 -05001323 size_t attr_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 int ret = 0;
Vlad Buslov90b73b72018-07-05 17:24:33 +03001325 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Vlad Buslov90b73b72018-07-05 17:24:33 +03001327 ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0, actions,
Vlad Buslov789871b2018-07-05 17:24:25 +03001328 &attr_size, true, extack);
Vlad Buslov90b73b72018-07-05 17:24:33 +03001329 if (ret < 0)
WANG Congf07fed82016-08-13 22:34:56 -07001330 return ret;
Vlad Buslov90b73b72018-07-05 17:24:33 +03001331 ret = tcf_add_notify(net, n, actions, portid, attr_size, extack);
Vlad Buslovcae422f2018-07-05 17:24:31 +03001332 if (ovr)
Vlad Buslov90b73b72018-07-05 17:24:33 +03001333 tcf_action_put_many(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Vlad Buslovcae422f2018-07-05 17:24:31 +03001335 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001338static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON;
1339static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
1340 [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32,
1341 .validation_data = &tcaa_root_flags_allowed },
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001342 [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001343};
1344
David Ahernc21ef3e2017-04-16 09:48:24 -07001345static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
1346 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001348 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001349 struct nlattr *tca[TCA_ROOT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +00001350 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 int ret = 0, ovr = 0;
1352
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001353 if ((n->nlmsg_type != RTM_GETACTION) &&
1354 !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001355 return -EPERM;
1356
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001357 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ROOT_MAX, NULL,
David Ahernc21ef3e2017-04-16 09:48:24 -07001358 extack);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001359 if (ret < 0)
1360 return ret;
1361
1362 if (tca[TCA_ACT_TAB] == NULL) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001363 NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return -EINVAL;
1365 }
1366
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001367 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 switch (n->nlmsg_type) {
1369 case RTM_NEWACTION:
1370 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001371 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 * Note that CREATE | EXCL implies that
1373 * but since we want avoid ambiguity (eg when flags
1374 * is zero) then just set this
1375 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001376 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 ovr = 1;
1378replay:
Alexander Aringaea0d722018-02-15 10:54:54 -05001379 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr,
1380 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (ret == -EAGAIN)
1382 goto replay;
1383 break;
1384 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001385 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Alexander Aring84ae0172018-02-15 10:54:55 -05001386 portid, RTM_DELACTION, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 break;
1388 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001389 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Alexander Aring84ae0172018-02-15 10:54:55 -05001390 portid, RTM_GETACTION, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 break;
1392 default:
1393 BUG();
1394 }
1395
1396 return ret;
1397}
1398
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001399static struct nlattr *find_dump_kind(struct nlattr **nla)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001401 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001402 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001403 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001405 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (tb1 == NULL)
1407 return NULL;
1408
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001409 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
Johannes Bergfceb6432017-04-12 14:34:07 +02001410 NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Patrick McHardy6d834e02008-01-23 20:32:42 -08001413 if (tb[1] == NULL)
1414 return NULL;
Johannes Bergfceb6432017-04-12 14:34:07 +02001415 if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001417 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Thomas Graf26dab892006-07-05 20:45:06 -07001419 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001422static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
WANG Congddf97cc2016-02-22 15:57:53 -08001424 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001426 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001427 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 struct tc_action_ops *a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001430 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001431 struct nlattr *tb[TCA_ROOT_MAX + 1];
1432 struct nlattr *count_attr = NULL;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001433 unsigned long jiffy_since = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001434 struct nlattr *kind = NULL;
1435 struct nla_bitfield32 bf;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001436 u32 msecs_since = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001437 u32 act_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001439 ret = nlmsg_parse(cb->nlh, sizeof(struct tcamsg), tb, TCA_ROOT_MAX,
1440 tcaa_policy, NULL);
1441 if (ret < 0)
1442 return ret;
1443
1444 kind = find_dump_kind(tb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001446 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return 0;
1448 }
1449
Thomas Graf26dab892006-07-05 20:45:06 -07001450 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001451 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001454 cb->args[2] = 0;
1455 if (tb[TCA_ROOT_FLAGS]) {
1456 bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
1457 cb->args[2] = bf.value;
1458 }
1459
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001460 if (tb[TCA_ROOT_TIME_DELTA]) {
1461 msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
1462 }
1463
Eric W. Biederman15e47302012-09-07 20:12:54 +00001464 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001465 cb->nlh->nlmsg_type, sizeof(*t), 0);
1466 if (!nlh)
1467 goto out_module_put;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001468
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001469 if (msecs_since)
1470 jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
1471
David S. Miller8b00a532012-06-26 21:39:32 -07001472 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001474 t->tca__pad1 = 0;
1475 t->tca__pad2 = 0;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001476 cb->args[3] = jiffy_since;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001477 count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
1478 if (!count_attr)
1479 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001481 nest = nla_nest_start(skb, TCA_ACT_TAB);
1482 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001483 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Alexander Aring41780102018-02-15 10:54:58 -05001485 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001487 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001490 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 ret = skb->len;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001492 act_count = cb->args[1];
1493 memcpy(nla_data(count_attr), &act_count, sizeof(u32));
1494 cb->args[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 } else
Jamal Hadi Salimebecaa62016-06-13 18:08:42 -04001496 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001498 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001499 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 nlh->nlmsg_flags |= NLM_F_MULTI;
1501 module_put(a_o->owner);
1502 return skb->len;
1503
David S. Miller8b00a532012-06-26 21:39:32 -07001504out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001506 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return skb->len;
1508}
1509
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001510struct tcf_action_net {
1511 struct rhashtable egdev_ht;
1512};
1513
1514static unsigned int tcf_action_net_id;
1515
1516struct tcf_action_egdev_cb {
1517 struct list_head list;
1518 tc_setup_cb_t *cb;
1519 void *cb_priv;
1520};
1521
1522struct tcf_action_egdev {
1523 struct rhash_head ht_node;
1524 const struct net_device *dev;
1525 unsigned int refcnt;
1526 struct list_head cb_list;
1527};
1528
1529static const struct rhashtable_params tcf_action_egdev_ht_params = {
1530 .key_offset = offsetof(struct tcf_action_egdev, dev),
1531 .head_offset = offsetof(struct tcf_action_egdev, ht_node),
1532 .key_len = sizeof(const struct net_device *),
1533};
1534
1535static struct tcf_action_egdev *
1536tcf_action_egdev_lookup(const struct net_device *dev)
1537{
1538 struct net *net = dev_net(dev);
1539 struct tcf_action_net *tan = net_generic(net, tcf_action_net_id);
1540
1541 return rhashtable_lookup_fast(&tan->egdev_ht, &dev,
1542 tcf_action_egdev_ht_params);
1543}
1544
1545static struct tcf_action_egdev *
1546tcf_action_egdev_get(const struct net_device *dev)
1547{
1548 struct tcf_action_egdev *egdev;
1549 struct tcf_action_net *tan;
1550
1551 egdev = tcf_action_egdev_lookup(dev);
1552 if (egdev)
1553 goto inc_ref;
1554
1555 egdev = kzalloc(sizeof(*egdev), GFP_KERNEL);
1556 if (!egdev)
1557 return NULL;
1558 INIT_LIST_HEAD(&egdev->cb_list);
Or Gerlitz0843c092017-10-18 18:38:08 +03001559 egdev->dev = dev;
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001560 tan = net_generic(dev_net(dev), tcf_action_net_id);
1561 rhashtable_insert_fast(&tan->egdev_ht, &egdev->ht_node,
1562 tcf_action_egdev_ht_params);
1563
1564inc_ref:
1565 egdev->refcnt++;
1566 return egdev;
1567}
1568
1569static void tcf_action_egdev_put(struct tcf_action_egdev *egdev)
1570{
1571 struct tcf_action_net *tan;
1572
1573 if (--egdev->refcnt)
1574 return;
1575 tan = net_generic(dev_net(egdev->dev), tcf_action_net_id);
1576 rhashtable_remove_fast(&tan->egdev_ht, &egdev->ht_node,
1577 tcf_action_egdev_ht_params);
1578 kfree(egdev);
1579}
1580
1581static struct tcf_action_egdev_cb *
1582tcf_action_egdev_cb_lookup(struct tcf_action_egdev *egdev,
1583 tc_setup_cb_t *cb, void *cb_priv)
1584{
1585 struct tcf_action_egdev_cb *egdev_cb;
1586
1587 list_for_each_entry(egdev_cb, &egdev->cb_list, list)
1588 if (egdev_cb->cb == cb && egdev_cb->cb_priv == cb_priv)
1589 return egdev_cb;
1590 return NULL;
1591}
1592
1593static int tcf_action_egdev_cb_call(struct tcf_action_egdev *egdev,
1594 enum tc_setup_type type,
1595 void *type_data, bool err_stop)
1596{
1597 struct tcf_action_egdev_cb *egdev_cb;
1598 int ok_count = 0;
1599 int err;
1600
1601 list_for_each_entry(egdev_cb, &egdev->cb_list, list) {
1602 err = egdev_cb->cb(type, type_data, egdev_cb->cb_priv);
1603 if (err) {
1604 if (err_stop)
1605 return err;
1606 } else {
1607 ok_count++;
1608 }
1609 }
1610 return ok_count;
1611}
1612
1613static int tcf_action_egdev_cb_add(struct tcf_action_egdev *egdev,
1614 tc_setup_cb_t *cb, void *cb_priv)
1615{
1616 struct tcf_action_egdev_cb *egdev_cb;
1617
1618 egdev_cb = tcf_action_egdev_cb_lookup(egdev, cb, cb_priv);
1619 if (WARN_ON(egdev_cb))
1620 return -EEXIST;
1621 egdev_cb = kzalloc(sizeof(*egdev_cb), GFP_KERNEL);
1622 if (!egdev_cb)
1623 return -ENOMEM;
1624 egdev_cb->cb = cb;
1625 egdev_cb->cb_priv = cb_priv;
1626 list_add(&egdev_cb->list, &egdev->cb_list);
1627 return 0;
1628}
1629
1630static void tcf_action_egdev_cb_del(struct tcf_action_egdev *egdev,
1631 tc_setup_cb_t *cb, void *cb_priv)
1632{
1633 struct tcf_action_egdev_cb *egdev_cb;
1634
1635 egdev_cb = tcf_action_egdev_cb_lookup(egdev, cb, cb_priv);
1636 if (WARN_ON(!egdev_cb))
1637 return;
1638 list_del(&egdev_cb->list);
1639 kfree(egdev_cb);
1640}
1641
1642static int __tc_setup_cb_egdev_register(const struct net_device *dev,
1643 tc_setup_cb_t *cb, void *cb_priv)
1644{
1645 struct tcf_action_egdev *egdev = tcf_action_egdev_get(dev);
1646 int err;
1647
1648 if (!egdev)
1649 return -ENOMEM;
1650 err = tcf_action_egdev_cb_add(egdev, cb, cb_priv);
1651 if (err)
1652 goto err_cb_add;
1653 return 0;
1654
1655err_cb_add:
1656 tcf_action_egdev_put(egdev);
1657 return err;
1658}
1659int tc_setup_cb_egdev_register(const struct net_device *dev,
1660 tc_setup_cb_t *cb, void *cb_priv)
1661{
1662 int err;
1663
1664 rtnl_lock();
1665 err = __tc_setup_cb_egdev_register(dev, cb, cb_priv);
1666 rtnl_unlock();
1667 return err;
1668}
1669EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_register);
1670
1671static void __tc_setup_cb_egdev_unregister(const struct net_device *dev,
1672 tc_setup_cb_t *cb, void *cb_priv)
1673{
1674 struct tcf_action_egdev *egdev = tcf_action_egdev_lookup(dev);
1675
1676 if (WARN_ON(!egdev))
1677 return;
1678 tcf_action_egdev_cb_del(egdev, cb, cb_priv);
1679 tcf_action_egdev_put(egdev);
1680}
1681void tc_setup_cb_egdev_unregister(const struct net_device *dev,
1682 tc_setup_cb_t *cb, void *cb_priv)
1683{
1684 rtnl_lock();
1685 __tc_setup_cb_egdev_unregister(dev, cb, cb_priv);
1686 rtnl_unlock();
1687}
1688EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_unregister);
1689
1690int tc_setup_cb_egdev_call(const struct net_device *dev,
1691 enum tc_setup_type type, void *type_data,
1692 bool err_stop)
1693{
1694 struct tcf_action_egdev *egdev = tcf_action_egdev_lookup(dev);
1695
1696 if (!egdev)
1697 return 0;
1698 return tcf_action_egdev_cb_call(egdev, type, type_data, err_stop);
1699}
1700EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_call);
1701
1702static __net_init int tcf_action_net_init(struct net *net)
1703{
1704 struct tcf_action_net *tan = net_generic(net, tcf_action_net_id);
1705
1706 return rhashtable_init(&tan->egdev_ht, &tcf_action_egdev_ht_params);
1707}
1708
1709static void __net_exit tcf_action_net_exit(struct net *net)
1710{
1711 struct tcf_action_net *tan = net_generic(net, tcf_action_net_id);
1712
1713 rhashtable_destroy(&tan->egdev_ht);
1714}
1715
1716static struct pernet_operations tcf_action_net_ops = {
1717 .init = tcf_action_net_init,
1718 .exit = tcf_action_net_exit,
1719 .id = &tcf_action_net_id,
1720 .size = sizeof(struct tcf_action_net),
1721};
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723static int __init tc_action_init(void)
1724{
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001725 int err;
1726
1727 err = register_pernet_subsys(&tcf_action_net_ops);
1728 if (err)
1729 return err;
1730
Florian Westphalb97bac62017-08-09 20:41:48 +02001731 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
1732 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
Greg Rosec7ac8672011-06-10 01:27:09 +00001733 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
Florian Westphalb97bac62017-08-09 20:41:48 +02001734 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 return 0;
1737}
1738
1739subsys_initcall(tc_action_init);