blob: a023873db7137a708222869c33084267dbe1b694 [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;
WANG Cong367a8ce2017-05-23 09:42:37 -070039 a->goto_chain = tcf_chain_get(tp->chain->block, chain_index, true);
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{
47 tcf_chain_put(a->goto_chain);
48}
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
71 old = xchg(old_cookie, new_cookie);
72 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{
152 u32 cookie_len = 0;
153
154 if (act->act_cookie)
155 cookie_len = nla_total_size(act->act_cookie->len);
156
157 return nla_total_size(0) /* action number nested */
158 + nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */
159 + cookie_len /* TCA_ACT_COOKIE */
160 + nla_total_size(0) /* TCA_ACT_STATS nested */
161 /* TCA_STATS_BASIC */
162 + nla_total_size_64bit(sizeof(struct gnet_stats_basic))
163 /* TCA_STATS_QUEUE */
164 + nla_total_size_64bit(sizeof(struct gnet_stats_queue))
165 + nla_total_size(0) /* TCA_OPTIONS nested */
166 + nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */
167}
168
169static size_t tcf_action_full_attrs_size(size_t sz)
170{
171 return NLMSG_HDRLEN /* struct nlmsghdr */
172 + sizeof(struct tcamsg)
173 + nla_total_size(0) /* TCA_ACT_TAB nested */
174 + sz;
175}
176
177static size_t tcf_action_fill_size(const struct tc_action *act)
178{
179 size_t sz = tcf_action_shared_attrs_size(act);
180
181 if (act->ops->get_fill_size)
182 return act->ops->get_fill_size(act) + sz;
183 return sz;
184}
185
Chris Mi65a206c2017-08-30 02:31:59 -0400186static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700187 struct netlink_callback *cb)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700188{
Chris Mi65a206c2017-08-30 02:31:59 -0400189 int err = 0, index = -1, s_i = 0, n_i = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400190 u32 act_flags = cb->args[2];
Jamal Hadi Salime62e4842017-07-30 13:24:52 -0400191 unsigned long jiffy_since = cb->args[3];
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800192 struct nlattr *nest;
Chris Mi65a206c2017-08-30 02:31:59 -0400193 struct idr *idr = &idrinfo->action_idr;
194 struct tc_action *p;
195 unsigned long id = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700196
Vlad Buslov290aa0a2018-05-21 23:03:04 +0300197 spin_lock(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700198
199 s_i = cb->args[0];
200
Matthew Wilcox7a457572017-11-28 15:39:51 -0500201 idr_for_each_entry_ul(idr, p, id) {
Chris Mi65a206c2017-08-30 02:31:59 -0400202 index++;
203 if (index < s_i)
204 continue;
WANG Conga85a9702016-07-25 16:09:41 -0700205
Chris Mi65a206c2017-08-30 02:31:59 -0400206 if (jiffy_since &&
207 time_after(jiffy_since,
208 (unsigned long)p->tcfa_tm.lastuse))
209 continue;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700210
Chris Mi65a206c2017-08-30 02:31:59 -0400211 nest = nla_nest_start(skb, n_i);
Craig Dillabaugh734549e2018-03-26 14:58:32 -0400212 if (!nest) {
213 index--;
Chris Mi65a206c2017-08-30 02:31:59 -0400214 goto nla_put_failure;
Craig Dillabaugh734549e2018-03-26 14:58:32 -0400215 }
Chris Mi65a206c2017-08-30 02:31:59 -0400216 err = tcf_action_dump_1(skb, p, 0, 0);
217 if (err < 0) {
218 index--;
219 nlmsg_trim(skb, nest);
220 goto done;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700221 }
Chris Mi65a206c2017-08-30 02:31:59 -0400222 nla_nest_end(skb, nest);
223 n_i++;
224 if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) &&
225 n_i >= TCA_ACT_MAX_PRIO)
226 goto done;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700227 }
228done:
Jamal Hadi Salime62e4842017-07-30 13:24:52 -0400229 if (index >= 0)
230 cb->args[0] = index + 1;
231
Vlad Buslov290aa0a2018-05-21 23:03:04 +0300232 spin_unlock(&idrinfo->lock);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400233 if (n_i) {
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400234 if (act_flags & TCA_FLAG_LARGE_DUMP_ON)
235 cb->args[1] = n_i;
236 }
David S. Millere9ce1cd2006-08-21 23:54:55 -0700237 return n_i;
238
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800239nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800240 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700241 goto done;
242}
243
Chris Mi65a206c2017-08-30 02:31:59 -0400244static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700245 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700246{
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800247 struct nlattr *nest;
Chris Mi65a206c2017-08-30 02:31:59 -0400248 int n_i = 0;
WANG Cong55334a52014-02-11 17:07:34 -0800249 int ret = -EINVAL;
Chris Mi65a206c2017-08-30 02:31:59 -0400250 struct idr *idr = &idrinfo->action_idr;
251 struct tc_action *p;
252 unsigned long id = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700253
WANG Conga85a9702016-07-25 16:09:41 -0700254 nest = nla_nest_start(skb, 0);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800255 if (nest == NULL)
256 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700257 if (nla_put_string(skb, TCA_KIND, ops->kind))
David S. Miller1b34ec42012-03-29 05:11:39 -0400258 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700259
Matthew Wilcox7a457572017-11-28 15:39:51 -0500260 idr_for_each_entry_ul(idr, p, id) {
Chris Mi65a206c2017-08-30 02:31:59 -0400261 ret = __tcf_idr_release(p, false, true);
262 if (ret == ACT_P_DELETED) {
Jiri Pirko255cd502017-09-13 17:32:37 +0200263 module_put(ops->owner);
Chris Mi65a206c2017-08-30 02:31:59 -0400264 n_i++;
265 } else if (ret < 0) {
266 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700267 }
268 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400269 if (nla_put_u32(skb, TCA_FCNT, n_i))
270 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800271 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700272
273 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800274nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800275 nla_nest_cancel(skb, nest);
WANG Cong55334a52014-02-11 17:07:34 -0800276 return ret;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700277}
278
WANG Congddf97cc2016-02-22 15:57:53 -0800279int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
280 struct netlink_callback *cb, int type,
Alexander Aringb3620142018-02-15 10:54:59 -0500281 const struct tc_action_ops *ops,
282 struct netlink_ext_ack *extack)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700283{
Chris Mi65a206c2017-08-30 02:31:59 -0400284 struct tcf_idrinfo *idrinfo = tn->idrinfo;
WANG Congddf97cc2016-02-22 15:57:53 -0800285
David S. Millere9ce1cd2006-08-21 23:54:55 -0700286 if (type == RTM_DELACTION) {
Chris Mi65a206c2017-08-30 02:31:59 -0400287 return tcf_del_walker(idrinfo, skb, ops);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700288 } else if (type == RTM_GETACTION) {
Chris Mi65a206c2017-08-30 02:31:59 -0400289 return tcf_dump_walker(idrinfo, skb, cb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700290 } else {
Alexander Aringb3620142018-02-15 10:54:59 -0500291 WARN(1, "tcf_generic_walker: unknown command %d\n", type);
292 NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command");
David S. Millere9ce1cd2006-08-21 23:54:55 -0700293 return -EINVAL;
294 }
295}
WANG Congddf97cc2016-02-22 15:57:53 -0800296EXPORT_SYMBOL(tcf_generic_walker);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700297
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300298static bool __tcf_idr_check(struct tc_action_net *tn, u32 index,
299 struct tc_action **a, int bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700300{
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300301 struct tcf_idrinfo *idrinfo = tn->idrinfo;
302 struct tc_action *p;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700303
Vlad Buslov290aa0a2018-05-21 23:03:04 +0300304 spin_lock(&idrinfo->lock);
Matthew Wilcox322d8842017-11-28 10:01:24 -0500305 p = idr_find(&idrinfo->action_idr, index);
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300306 if (p) {
307 refcount_inc(&p->tcfa_refcnt);
308 if (bind)
309 atomic_inc(&p->tcfa_bindcnt);
310 }
Vlad Buslov290aa0a2018-05-21 23:03:04 +0300311 spin_unlock(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700312
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300313 if (p) {
314 *a = p;
315 return true;
316 }
317 return false;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700318}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700319
Chris Mi65a206c2017-08-30 02:31:59 -0400320int 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 return __tcf_idr_check(tn, index, a, 0);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700323}
Chris Mi65a206c2017-08-30 02:31:59 -0400324EXPORT_SYMBOL(tcf_idr_search);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700325
Chris Mi65a206c2017-08-30 02:31:59 -0400326bool tcf_idr_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
327 int bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700328{
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300329 return __tcf_idr_check(tn, index, a, bind);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700330}
Chris Mi65a206c2017-08-30 02:31:59 -0400331EXPORT_SYMBOL(tcf_idr_check);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700332
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300333int tcf_idr_delete_index(struct tc_action_net *tn, u32 index)
334{
335 struct tcf_idrinfo *idrinfo = tn->idrinfo;
336 struct tc_action *p;
337 int ret = 0;
338
339 spin_lock(&idrinfo->lock);
340 p = idr_find(&idrinfo->action_idr, index);
341 if (!p) {
342 spin_unlock(&idrinfo->lock);
343 return -ENOENT;
344 }
345
346 if (!atomic_read(&p->tcfa_bindcnt)) {
347 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
348 struct module *owner = p->ops->owner;
349
350 WARN_ON(p != idr_remove(&idrinfo->action_idr,
351 p->tcfa_index));
352 spin_unlock(&idrinfo->lock);
353
Vlad Buslov16af6062018-07-05 17:24:29 +0300354 tcf_action_cleanup(p);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300355 module_put(owner);
356 return 0;
357 }
358 ret = 0;
359 } else {
360 ret = -EPERM;
361 }
362
363 spin_unlock(&idrinfo->lock);
364 return ret;
365}
366EXPORT_SYMBOL(tcf_idr_delete_index);
367
Chris Mi65a206c2017-08-30 02:31:59 -0400368int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
369 struct tc_action **a, const struct tc_action_ops *ops,
370 int bind, bool cpustats)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700371{
WANG Congec0595c2016-07-25 16:09:42 -0700372 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
Chris Mi65a206c2017-08-30 02:31:59 -0400373 struct tcf_idrinfo *idrinfo = tn->idrinfo;
374 struct idr *idr = &idrinfo->action_idr;
Eric Dumazet519c8182015-07-06 05:18:04 -0700375 int err = -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700376
377 if (unlikely(!p))
WANG Cong86062032014-02-11 17:07:31 -0800378 return -ENOMEM;
Vlad Buslov036bb442018-07-05 17:24:24 +0300379 refcount_set(&p->tcfa_refcnt, 1);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700380 if (bind)
Vlad Buslov036bb442018-07-05 17:24:24 +0300381 atomic_set(&p->tcfa_bindcnt, 1);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700382
Eric Dumazet519c8182015-07-06 05:18:04 -0700383 if (cpustats) {
384 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500385 if (!p->cpu_bstats)
Eric Dumazet519c8182015-07-06 05:18:04 -0700386 goto err1;
Matthew Wilcox339913a2017-11-28 10:28:15 -0500387 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
388 if (!p->cpu_qstats)
389 goto err2;
Eric Dumazet519c8182015-07-06 05:18:04 -0700390 }
WANG Congec0595c2016-07-25 16:09:42 -0700391 spin_lock_init(&p->tcfa_lock);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500392 idr_preload(GFP_KERNEL);
Vlad Buslov290aa0a2018-05-21 23:03:04 +0300393 spin_lock(&idrinfo->lock);
Chris Mi65a206c2017-08-30 02:31:59 -0400394 /* user doesn't specify an index */
395 if (!index) {
Matthew Wilcox339913a2017-11-28 10:28:15 -0500396 index = 1;
397 err = idr_alloc_u32(idr, NULL, &index, UINT_MAX, GFP_ATOMIC);
Chris Mi65a206c2017-08-30 02:31:59 -0400398 } else {
Matthew Wilcox339913a2017-11-28 10:28:15 -0500399 err = idr_alloc_u32(idr, NULL, &index, index, GFP_ATOMIC);
Chris Mi65a206c2017-08-30 02:31:59 -0400400 }
Vlad Buslov290aa0a2018-05-21 23:03:04 +0300401 spin_unlock(&idrinfo->lock);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500402 idr_preload_end();
403 if (err)
404 goto err3;
Chris Mi65a206c2017-08-30 02:31:59 -0400405
Matthew Wilcox339913a2017-11-28 10:28:15 -0500406 p->tcfa_index = index;
WANG Congec0595c2016-07-25 16:09:42 -0700407 p->tcfa_tm.install = jiffies;
408 p->tcfa_tm.lastuse = jiffies;
409 p->tcfa_tm.firstuse = 0;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800410 if (est) {
WANG Congec0595c2016-07-25 16:09:42 -0700411 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
412 &p->tcfa_rate_est,
413 &p->tcfa_lock, NULL, est);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500414 if (err)
415 goto err4;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800416 }
417
Chris Mi65a206c2017-08-30 02:31:59 -0400418 p->idrinfo = idrinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700419 p->ops = ops;
420 INIT_LIST_HEAD(&p->list);
421 *a = p;
WANG Cong86062032014-02-11 17:07:31 -0800422 return 0;
Matthew Wilcox339913a2017-11-28 10:28:15 -0500423err4:
424 idr_remove(idr, index);
425err3:
426 free_percpu(p->cpu_qstats);
427err2:
428 free_percpu(p->cpu_bstats);
429err1:
430 kfree(p);
431 return err;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700432}
Chris Mi65a206c2017-08-30 02:31:59 -0400433EXPORT_SYMBOL(tcf_idr_create);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700434
Chris Mi65a206c2017-08-30 02:31:59 -0400435void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700436{
Chris Mi65a206c2017-08-30 02:31:59 -0400437 struct tcf_idrinfo *idrinfo = tn->idrinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700438
Vlad Buslov290aa0a2018-05-21 23:03:04 +0300439 spin_lock(&idrinfo->lock);
Matthew Wilcox234a4622017-11-28 09:56:36 -0500440 idr_replace(&idrinfo->action_idr, a, a->tcfa_index);
Vlad Buslov290aa0a2018-05-21 23:03:04 +0300441 spin_unlock(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700442}
Chris Mi65a206c2017-08-30 02:31:59 -0400443EXPORT_SYMBOL(tcf_idr_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Chris Mi65a206c2017-08-30 02:31:59 -0400445void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
446 struct tcf_idrinfo *idrinfo)
WANG Cong1d4150c2016-02-22 15:57:52 -0800447{
Chris Mi65a206c2017-08-30 02:31:59 -0400448 struct idr *idr = &idrinfo->action_idr;
449 struct tc_action *p;
450 int ret;
451 unsigned long id = 1;
WANG Cong1d4150c2016-02-22 15:57:52 -0800452
Matthew Wilcox7a457572017-11-28 15:39:51 -0500453 idr_for_each_entry_ul(idr, p, id) {
Chris Mi65a206c2017-08-30 02:31:59 -0400454 ret = __tcf_idr_release(p, false, true);
455 if (ret == ACT_P_DELETED)
456 module_put(ops->owner);
457 else if (ret < 0)
458 return;
WANG Cong1d4150c2016-02-22 15:57:52 -0800459 }
Chris Mi65a206c2017-08-30 02:31:59 -0400460 idr_destroy(&idrinfo->action_idr);
WANG Cong1d4150c2016-02-22 15:57:52 -0800461}
Chris Mi65a206c2017-08-30 02:31:59 -0400462EXPORT_SYMBOL(tcf_idrinfo_destroy);
WANG Cong1d4150c2016-02-22 15:57:52 -0800463
WANG Cong1f747c22013-12-15 20:15:10 -0800464static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465static DEFINE_RWLOCK(act_mod_lock);
466
WANG Congddf97cc2016-02-22 15:57:53 -0800467int tcf_register_action(struct tc_action_ops *act,
468 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
WANG Cong1f747c22013-12-15 20:15:10 -0800470 struct tc_action_ops *a;
WANG Congddf97cc2016-02-22 15:57:53 -0800471 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
WANG Congddf97cc2016-02-22 15:57:53 -0800473 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500474 return -EINVAL;
475
WANG Congab102b82016-10-11 10:56:45 -0700476 /* We have to register pernet ops before making the action ops visible,
477 * otherwise tcf_action_init_1() could get a partially initialized
478 * netns.
479 */
480 ret = register_pernet_subsys(ops);
481 if (ret)
482 return ret;
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800485 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
487 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700488 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return -EEXIST;
490 }
491 }
WANG Cong1f747c22013-12-15 20:15:10 -0800492 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 write_unlock(&act_mod_lock);
WANG Congddf97cc2016-02-22 15:57:53 -0800494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return 0;
496}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800497EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
WANG Congddf97cc2016-02-22 15:57:53 -0800499int tcf_unregister_action(struct tc_action_ops *act,
500 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
WANG Cong1f747c22013-12-15 20:15:10 -0800502 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 int err = -ENOENT;
504
505 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800506 list_for_each_entry(a, &act_base, head) {
507 if (a == act) {
508 list_del(&act->head);
509 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700514 if (!err)
515 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return err;
517}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800518EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520/* lookup by name */
521static struct tc_action_ops *tc_lookup_action_n(char *kind)
522{
Eric Dumazeta7928662013-12-20 12:32:32 -0800523 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 if (kind) {
526 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800527 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800529 if (try_module_get(a->owner))
530 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 break;
532 }
533 }
534 read_unlock(&act_mod_lock);
535 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800536 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800539/* lookup by nlattr */
540static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Eric Dumazeta7928662013-12-20 12:32:32 -0800542 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 if (kind) {
545 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800546 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800547 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800548 if (try_module_get(a->owner))
549 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 break;
551 }
552 }
553 read_unlock(&act_mod_lock);
554 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800555 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400558/*TCA_ACT_MAX_PRIO is 32, there count upto 32 */
559#define TCA_ACT_MAX_PRIO_MASK 0x1FF
WANG Cong22dc13c2016-08-13 22:35:00 -0700560int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
561 int nr_actions, struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400563 u32 jmp_prgcnt = 0;
564 u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
Jiri Pirkoec1a9cc2017-08-04 14:29:02 +0200565 int i;
566 int ret = TC_ACT_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Willem de Bruijne7246e12017-01-07 17:06:35 -0500568 if (skb_skip_tc_classify(skb))
569 return TC_ACT_OK;
570
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400571restart_act_graph:
WANG Cong22dc13c2016-08-13 22:35:00 -0700572 for (i = 0; i < nr_actions; i++) {
573 const struct tc_action *a = actions[i];
574
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400575 if (jmp_prgcnt > 0) {
576 jmp_prgcnt -= 1;
577 continue;
578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500580 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500581 if (ret == TC_ACT_REPEAT)
582 goto repeat; /* we need a ttl - JHS */
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400583
Jiri Pirko9da32422017-05-02 10:12:00 +0200584 if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400585 jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
586 if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
587 /* faulty opcode, stop pipeline */
588 return TC_ACT_OK;
589 } else {
590 jmp_ttl -= 1;
591 if (jmp_ttl > 0)
592 goto restart_act_graph;
593 else /* faulty graph, stop pipeline */
594 return TC_ACT_OK;
595 }
Jiri Pirkodb505142017-05-17 11:08:03 +0200596 } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
597 tcf_action_goto_chain_exec(a, res);
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400598 }
599
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500600 if (ret != TC_ACT_PIPE)
Willem de Bruijne7246e12017-01-07 17:06:35 -0500601 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return ret;
605}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800606EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
WANG Cong55334a52014-02-11 17:07:34 -0800608int tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Jiri Pirko255cd502017-09-13 17:32:37 +0200610 const struct tc_action_ops *ops;
WANG Cong33be6272013-12-15 20:15:05 -0800611 struct tc_action *a, *tmp;
WANG Cong55334a52014-02-11 17:07:34 -0800612 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
WANG Cong33be6272013-12-15 20:15:05 -0800614 list_for_each_entry_safe(a, tmp, actions, list) {
Jiri Pirko255cd502017-09-13 17:32:37 +0200615 ops = a->ops;
Chris Mi65a206c2017-08-30 02:31:59 -0400616 ret = __tcf_idr_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800617 if (ret == ACT_P_DELETED)
Jiri Pirko255cd502017-09-13 17:32:37 +0200618 module_put(ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800619 else if (ret < 0)
620 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
WANG Cong55334a52014-02-11 17:07:34 -0800622 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Vlad Buslov16af6062018-07-05 17:24:29 +0300625static int tcf_action_put(struct tc_action *p)
626{
627 return __tcf_action_put(p, false);
628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630int
631tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
632{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return a->ops->dump(skb, a, bind, ref);
634}
635
636int
637tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
638{
639 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700640 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800641 struct nlattr *nest;
Vlad Busloveec94fd2018-07-05 17:24:23 +0300642 struct tc_cookie *cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
David S. Miller1b34ec42012-03-29 05:11:39 -0400644 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
645 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800647 goto nla_put_failure;
Vlad Busloveec94fd2018-07-05 17:24:23 +0300648
649 rcu_read_lock();
650 cookie = rcu_dereference(a->act_cookie);
651 if (cookie) {
652 if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) {
653 rcu_read_unlock();
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500654 goto nla_put_failure;
Vlad Busloveec94fd2018-07-05 17:24:23 +0300655 }
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500656 }
Vlad Busloveec94fd2018-07-05 17:24:23 +0300657 rcu_read_unlock();
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500658
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800659 nest = nla_nest_start(skb, TCA_OPTIONS);
660 if (nest == NULL)
661 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000662 err = tcf_action_dump_old(skb, a, bind, ref);
663 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800664 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return err;
666 }
667
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800668nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700669 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return -1;
671}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800672EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400674int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
675 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 struct tc_action *a;
678 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800679 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
WANG Cong33be6272013-12-15 20:15:05 -0800681 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800682 nest = nla_nest_start(skb, a->order);
683 if (nest == NULL)
684 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 err = tcf_action_dump_1(skb, a, bind, ref);
686 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700687 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800688 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690
691 return 0;
692
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800693nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700694 err = -EINVAL;
695errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800696 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700697 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200700static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500701{
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200702 struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
703 if (!c)
704 return NULL;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500705
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200706 c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
707 if (!c->data) {
708 kfree(c);
709 return NULL;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500710 }
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200711 c->len = nla_len(tb[TCA_ACT_COOKIE]);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500712
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200713 return c;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500714}
715
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200716struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
717 struct nlattr *nla, struct nlattr *est,
Alexander Aringaea0d722018-02-15 10:54:54 -0500718 char *name, int ovr, int bind,
Vlad Buslov789871b2018-07-05 17:24:25 +0300719 bool rtnl_held,
Alexander Aringaea0d722018-02-15 10:54:54 -0500720 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 struct tc_action *a;
723 struct tc_action_ops *a_o;
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200724 struct tc_cookie *cookie = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000726 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800727 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800728 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (name == NULL) {
Alexander Aring84ae0172018-02-15 10:54:55 -0500731 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
Patrick McHardycee63722008-01-23 20:33:32 -0800732 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800734 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800735 kind = tb[TCA_ACT_KIND];
Alexander Aring84ae0172018-02-15 10:54:55 -0500736 if (!kind) {
737 NL_SET_ERR_MSG(extack, "TC action kind must be specified");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -0500739 }
740 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) {
741 NL_SET_ERR_MSG(extack, "TC action name too long");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -0500743 }
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200744 if (tb[TCA_ACT_COOKIE]) {
745 int cklen = nla_len(tb[TCA_ACT_COOKIE]);
746
Alexander Aring84ae0172018-02-15 10:54:55 -0500747 if (cklen > TC_COOKIE_MAX_SIZE) {
748 NL_SET_ERR_MSG(extack, "TC cookie size above the maximum");
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200749 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -0500750 }
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200751
752 cookie = nla_memdup_cookie(tb);
753 if (!cookie) {
Alexander Aring84ae0172018-02-15 10:54:55 -0500754 NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200755 err = -ENOMEM;
756 goto err_out;
757 }
758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 } else {
Alexander Aring84ae0172018-02-15 10:54:55 -0500760 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) {
761 NL_SET_ERR_MSG(extack, "TC action name too long");
762 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -0500764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766
767 a_o = tc_lookup_action_n(act_name);
768 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700769#ifdef CONFIG_MODULES
Vlad Buslov789871b2018-07-05 17:24:25 +0300770 if (rtnl_held)
771 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800772 request_module("act_%s", act_name);
Vlad Buslov789871b2018-07-05 17:24:25 +0300773 if (rtnl_held)
774 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 a_o = tc_lookup_action_n(act_name);
777
778 /* We dropped the RTNL semaphore in order to
779 * perform the module load. So, even if we
780 * succeeded in loading the module we have to
781 * tell the caller to replay the request. We
782 * indicate this using -EAGAIN.
783 */
784 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800785 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 goto err_mod;
787 }
788#endif
Alexander Aring84ae0172018-02-15 10:54:55 -0500789 NL_SET_ERR_MSG(extack, "Failed to load TC action module");
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800790 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 goto err_out;
792 }
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 /* backward compatibility for policer */
795 if (name == NULL)
Alexander Aring589dad62018-02-15 10:54:56 -0500796 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind,
Vlad Buslov789871b2018-07-05 17:24:25 +0300797 rtnl_held, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 else
Vlad Buslov789871b2018-07-05 17:24:25 +0300799 err = a_o->init(net, nla, est, &a, ovr, bind, rtnl_held,
800 extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800801 if (err < 0)
WANG Conga85a9702016-07-25 16:09:41 -0700802 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Vlad Busloveec94fd2018-07-05 17:24:23 +0300804 if (!name && tb[TCA_ACT_COOKIE])
805 tcf_set_action_cookie(&a->act_cookie, cookie);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000808 * if it exists and is only bound to in a_o->init() then
809 * ACT_P_CREATED is not returned (a zero is).
810 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800811 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Jiri Pirkodb505142017-05-17 11:08:03 +0200814 if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) {
815 err = tcf_action_goto_chain_init(a, tp);
816 if (err) {
817 LIST_HEAD(actions);
818
819 list_add_tail(&a->list, &actions);
820 tcf_action_destroy(&actions, bind);
Alexander Aring84ae0172018-02-15 10:54:55 -0500821 NL_SET_ERR_MSG(extack, "Failed to init TC action chain");
Jiri Pirkodb505142017-05-17 11:08:03 +0200822 return ERR_PTR(err);
823 }
824 }
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return a;
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828err_mod:
829 module_put(a_o->owner);
830err_out:
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200831 if (cookie) {
832 kfree(cookie->data);
833 kfree(cookie);
834 }
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800835 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400838static void cleanup_a(struct list_head *actions, int ovr)
839{
840 struct tc_action *a;
841
842 if (!ovr)
843 return;
844
845 list_for_each_entry(a, actions, list)
Vlad Buslov036bb442018-07-05 17:24:24 +0300846 refcount_dec(&a->tcfa_refcnt);
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400847}
848
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200849int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
850 struct nlattr *est, char *name, int ovr, int bind,
Roman Mashakd04e6992018-03-08 16:59:17 -0500851 struct list_head *actions, size_t *attr_size,
Vlad Buslov789871b2018-07-05 17:24:25 +0300852 bool rtnl_held, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000854 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800855 struct tc_action *act;
Roman Mashak4e76e752018-03-08 16:59:19 -0500856 size_t sz = 0;
Patrick McHardycee63722008-01-23 20:33:32 -0800857 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 int i;
859
Alexander Aring84ae0172018-02-15 10:54:55 -0500860 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack);
Patrick McHardycee63722008-01-23 20:33:32 -0800861 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800862 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800864 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Alexander Aringaea0d722018-02-15 10:54:54 -0500865 act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind,
Vlad Buslov789871b2018-07-05 17:24:25 +0300866 rtnl_held, extack);
WANG Cong33be6272013-12-15 20:15:05 -0800867 if (IS_ERR(act)) {
868 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800870 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800871 act->order = i;
Roman Mashak4e76e752018-03-08 16:59:19 -0500872 sz += tcf_action_fill_size(act);
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400873 if (ovr)
Vlad Buslov036bb442018-07-05 17:24:24 +0300874 refcount_inc(&act->tcfa_refcnt);
WANG Cong33be6272013-12-15 20:15:05 -0800875 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400877
Roman Mashak4e76e752018-03-08 16:59:19 -0500878 *attr_size = tcf_action_full_attrs_size(sz);
879
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400880 /* Remove the temp refcnt which was necessary to protect against
881 * destroying an existing action which was being replaced
882 */
883 cleanup_a(actions, ovr);
WANG Cong33be6272013-12-15 20:15:05 -0800884 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886err:
WANG Cong33be6272013-12-15 20:15:05 -0800887 tcf_action_destroy(actions, bind);
888 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
WANG Congec0595c2016-07-25 16:09:42 -0700891int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 int compat_mode)
893{
894 int err = 0;
895 struct gnet_dump d;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900896
WANG Cong7eb88962014-01-09 16:14:05 -0800897 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 goto errout;
899
900 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400901 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 */
903 if (compat_mode) {
WANG Congec0595c2016-07-25 16:09:42 -0700904 if (p->type == TCA_OLD_COMPAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 err = gnet_stats_start_copy_compat(skb, 0,
Nicolas Dichtel9854518e2016-04-26 10:06:18 +0200906 TCA_STATS,
907 TCA_XSTATS,
WANG Congec0595c2016-07-25 16:09:42 -0700908 &p->tcfa_lock, &d,
Nicolas Dichtel9854518e2016-04-26 10:06:18 +0200909 TCA_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 else
911 return 0;
912 } else
913 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Congec0595c2016-07-25 16:09:42 -0700914 &p->tcfa_lock, &d, TCA_ACT_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 if (err < 0)
917 goto errout;
918
WANG Congec0595c2016-07-25 16:09:42 -0700919 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800920 gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -0700921 gnet_stats_copy_queue(&d, p->cpu_qstats,
WANG Congec0595c2016-07-25 16:09:42 -0700922 &p->tcfa_qstats,
923 p->tcfa_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 goto errout;
925
926 if (gnet_stats_finish_copy(&d) < 0)
927 goto errout;
928
929 return 0;
930
931errout:
932 return -1;
933}
934
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400935static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
936 u32 portid, u32 seq, u16 flags, int event, int bind,
937 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 struct tcamsg *t;
940 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700941 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800942 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Eric W. Biederman15e47302012-09-07 20:12:54 +0000944 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700945 if (!nlh)
946 goto out_nlmsg_trim;
947 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700949 t->tca__pad1 = 0;
950 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900951
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800952 nest = nla_nest_start(skb, TCA_ACT_TAB);
Alexander Aring1af8515582018-02-15 10:54:53 -0500953 if (!nest)
David S. Miller8b00a532012-06-26 21:39:32 -0700954 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
WANG Cong33be6272013-12-15 20:15:05 -0800956 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700957 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800959 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900960
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700961 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return skb->len;
963
David S. Miller8b00a532012-06-26 21:39:32 -0700964out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700965 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return -1;
967}
968
969static int
Roman Mashakc4c42902017-07-13 13:12:18 -0400970tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
Alexander Aring84ae0172018-02-15 10:54:55 -0500971 struct list_head *actions, int event,
972 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
974 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
977 if (!skb)
978 return -ENOBUFS;
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400979 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300980 0, 1) <= 0) {
Alexander Aring84ae0172018-02-15 10:54:55 -0500981 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 kfree_skb(skb);
983 return -EINVAL;
984 }
Thomas Graf2942e902006-08-15 00:30:25 -0700985
Eric W. Biederman15e47302012-09-07 20:12:54 +0000986 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
WANG Congddf97cc2016-02-22 15:57:53 -0800989static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
Alexander Aring84ae0172018-02-15 10:54:55 -0500990 struct nlmsghdr *n, u32 portid,
991 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000993 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700994 const struct tc_action_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 struct tc_action *a;
996 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800997 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Alexander Aring84ae0172018-02-15 10:54:55 -0500999 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001000 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001001 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Patrick McHardycee63722008-01-23 20:33:32 -08001003 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001004 if (tb[TCA_ACT_INDEX] == NULL ||
Alexander Aring84ae0172018-02-15 10:54:55 -05001005 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
1006 NL_SET_ERR_MSG(extack, "Invalid TC action index value");
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001007 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -05001008 }
Patrick McHardy1587bac2008-01-23 20:35:03 -08001009 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001011 err = -EINVAL;
WANG Conga85a9702016-07-25 16:09:41 -07001012 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Alexander Aring84ae0172018-02-15 10:54:55 -05001013 if (!ops) { /* could happen in batch of actions */
1014 NL_SET_ERR_MSG(extack, "Specified TC action not found");
WANG Conga85a9702016-07-25 16:09:41 -07001015 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -05001016 }
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001017 err = -ENOENT;
Alexander Aring331a9292018-02-15 10:54:57 -05001018 if (ops->lookup(net, &a, index, extack) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 goto err_mod;
1020
WANG Conga85a9702016-07-25 16:09:41 -07001021 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024err_mod:
WANG Conga85a9702016-07-25 16:09:41 -07001025 module_put(ops->owner);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001026err_out:
1027 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
Tom Goff7316ae82010-03-19 15:40:13 +00001030static int tca_action_flush(struct net *net, struct nlattr *nla,
Alexander Aring84ae0172018-02-15 10:54:55 -05001031 struct nlmsghdr *n, u32 portid,
1032 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
1034 struct sk_buff *skb;
1035 unsigned char *b;
1036 struct nlmsghdr *nlh;
1037 struct tcamsg *t;
1038 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001039 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001040 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -07001041 const struct tc_action_ops *ops;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001042 struct nlattr *kind;
Jamal Hadi Salim36723872008-08-13 02:41:45 -07001043 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
Alexander Aring84ae0172018-02-15 10:54:55 -05001046 if (!skb)
Jamal Hadi Salim36723872008-08-13 02:41:45 -07001047 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001049 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Alexander Aring84ae0172018-02-15 10:54:55 -05001051 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001052 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 goto err_out;
1054
Patrick McHardycee63722008-01-23 20:33:32 -08001055 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001056 kind = tb[TCA_ACT_KIND];
WANG Conga85a9702016-07-25 16:09:41 -07001057 ops = tc_lookup_action(kind);
Alexander Aring84ae0172018-02-15 10:54:55 -05001058 if (!ops) { /*some idjot trying to flush unknown action */
1059 NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -05001061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001063 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
1064 sizeof(*t), 0);
Alexander Aring84ae0172018-02-15 10:54:55 -05001065 if (!nlh) {
1066 NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
David S. Miller8b00a532012-06-26 21:39:32 -07001067 goto out_module_put;
Alexander Aring84ae0172018-02-15 10:54:55 -05001068 }
David S. Miller8b00a532012-06-26 21:39:32 -07001069 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001071 t->tca__pad1 = 0;
1072 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001074 nest = nla_nest_start(skb, TCA_ACT_TAB);
Alexander Aring84ae0172018-02-15 10:54:55 -05001075 if (!nest) {
1076 NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
David S. Miller8b00a532012-06-26 21:39:32 -07001077 goto out_module_put;
Alexander Aring84ae0172018-02-15 10:54:55 -05001078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Alexander Aring41780102018-02-15 10:54:58 -05001080 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack);
Davide Caratti66dede22018-02-15 15:50:57 +01001081 if (err <= 0) {
1082 nla_nest_cancel(skb, nest);
David S. Miller8b00a532012-06-26 21:39:32 -07001083 goto out_module_put;
Davide Caratti66dede22018-02-15 15:50:57 +01001084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001086 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001088 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Conga85a9702016-07-25 16:09:41 -07001090 module_put(ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001091 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001092 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (err > 0)
1094 return 0;
Alexander Aring84ae0172018-02-15 10:54:55 -05001095 if (err < 0)
1096 NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 return err;
1099
David S. Miller8b00a532012-06-26 21:39:32 -07001100out_module_put:
WANG Conga85a9702016-07-25 16:09:41 -07001101 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102err_out:
1103 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return err;
1105}
1106
Vlad Buslov16af6062018-07-05 17:24:29 +03001107static int tcf_action_delete(struct net *net, struct list_head *actions,
1108 struct netlink_ext_ack *extack)
1109{
1110 struct tc_action *a, *tmp;
1111 u32 act_index;
1112 int ret;
1113
1114 list_for_each_entry_safe(a, tmp, actions, list) {
1115 const struct tc_action_ops *ops = a->ops;
1116
1117 /* Actions can be deleted concurrently so we must save their
1118 * type and id to search again after reference is released.
1119 */
1120 act_index = a->tcfa_index;
1121
1122 list_del(&a->list);
1123 if (tcf_action_put(a)) {
1124 /* last reference, action was deleted concurrently */
1125 module_put(ops->owner);
1126 } else {
1127 /* now do the delete */
1128 ret = ops->delete(net, act_index);
1129 if (ret < 0)
1130 return ret;
1131 }
1132 }
1133 return 0;
1134}
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136static int
WANG Conga56e1952014-01-09 16:14:00 -08001137tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
Roman Mashakd04e6992018-03-08 16:59:17 -05001138 u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
WANG Conga56e1952014-01-09 16:14:00 -08001139{
1140 int ret;
1141 struct sk_buff *skb;
1142
Roman Mashakd04e6992018-03-08 16:59:17 -05001143 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1144 GFP_KERNEL);
WANG Conga56e1952014-01-09 16:14:00 -08001145 if (!skb)
1146 return -ENOBUFS;
1147
1148 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
Vlad Buslov3f7c72b2018-07-05 17:24:26 +03001149 0, 2) <= 0) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001150 NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
WANG Conga56e1952014-01-09 16:14:00 -08001151 kfree_skb(skb);
1152 return -EINVAL;
1153 }
1154
1155 /* now do the delete */
Vlad Buslov16af6062018-07-05 17:24:29 +03001156 ret = tcf_action_delete(net, actions, extack);
WANG Cong55334a52014-02-11 17:07:34 -08001157 if (ret < 0) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001158 NL_SET_ERR_MSG(extack, "Failed to delete TC action");
WANG Cong55334a52014-02-11 17:07:34 -08001159 kfree_skb(skb);
1160 return ret;
1161 }
WANG Conga56e1952014-01-09 16:14:00 -08001162
1163 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1164 n->nlmsg_flags & NLM_F_ECHO);
1165 if (ret > 0)
1166 return 0;
1167 return ret;
1168}
1169
1170static int
Tom Goff7316ae82010-03-19 15:40:13 +00001171tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Alexander Aring84ae0172018-02-15 10:54:55 -05001172 u32 portid, int event, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Patrick McHardycee63722008-01-23 20:33:32 -08001174 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001175 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -08001176 struct tc_action *act;
Roman Mashakd04e6992018-03-08 16:59:17 -05001177 size_t attr_size = 0;
WANG Cong33be6272013-12-15 20:15:05 -08001178 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Alexander Aring84ae0172018-02-15 10:54:55 -05001180 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001181 if (ret < 0)
1182 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001184 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Alexander Aring1af8515582018-02-15 10:54:53 -05001185 if (tb[1])
Alexander Aring84ae0172018-02-15 10:54:55 -05001186 return tca_action_flush(net, tb[1], n, portid, extack);
Alexander Aring1af8515582018-02-15 10:54:53 -05001187
Alexander Aring84ae0172018-02-15 10:54:55 -05001188 NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
Alexander Aring1af8515582018-02-15 10:54:53 -05001189 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
1191
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001192 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001193 act = tcf_action_get_1(net, tb[i], n, portid, extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001194 if (IS_ERR(act)) {
1195 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001197 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001198 act->order = i;
Roman Mashak4e76e752018-03-08 16:59:19 -05001199 attr_size += tcf_action_fill_size(act);
WANG Cong33be6272013-12-15 20:15:05 -08001200 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
1202
Roman Mashak4e76e752018-03-08 16:59:19 -05001203 attr_size = tcf_action_full_attrs_size(attr_size);
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 if (event == RTM_GETACTION)
Alexander Aring84ae0172018-02-15 10:54:55 -05001206 ret = tcf_get_notify(net, portid, n, &actions, event, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 else { /* delete */
Roman Mashakd04e6992018-03-08 16:59:17 -05001208 ret = tcf_del_notify(net, n, &actions, portid, attr_size, extack);
WANG Conga56e1952014-01-09 16:14:00 -08001209 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return ret;
1212 }
1213err:
Vlad Buslov3f7c72b2018-07-05 17:24:26 +03001214 tcf_action_destroy(&actions, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return ret;
1216}
1217
WANG Conga56e1952014-01-09 16:14:00 -08001218static int
1219tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
Roman Mashakd04e6992018-03-08 16:59:17 -05001220 u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 int err = 0;
1224
Roman Mashakd04e6992018-03-08 16:59:17 -05001225 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1226 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (!skb)
1228 return -ENOBUFS;
1229
WANG Conga56e1952014-01-09 16:14:00 -08001230 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
1231 RTM_NEWACTION, 0, 0) <= 0) {
Roman Mashakd143b9e2018-03-02 20:52:01 -05001232 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
WANG Conga56e1952014-01-09 16:14:00 -08001233 kfree_skb(skb);
1234 return -EINVAL;
1235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
WANG Conga56e1952014-01-09 16:14:00 -08001237 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1238 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (err > 0)
1240 err = 0;
1241 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001244static int tcf_action_add(struct net *net, struct nlattr *nla,
Alexander Aringaea0d722018-02-15 10:54:54 -05001245 struct nlmsghdr *n, u32 portid, int ovr,
1246 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Roman Mashakd04e6992018-03-08 16:59:17 -05001248 size_t attr_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -08001250 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Alexander Aringaea0d722018-02-15 10:54:54 -05001252 ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0, &actions,
Vlad Buslov789871b2018-07-05 17:24:25 +03001253 &attr_size, true, extack);
WANG Cong33be6272013-12-15 20:15:05 -08001254 if (ret)
WANG Congf07fed82016-08-13 22:34:56 -07001255 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Roman Mashakd04e6992018-03-08 16:59:17 -05001257 return tcf_add_notify(net, n, &actions, portid, attr_size, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001260static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON;
1261static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
1262 [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32,
1263 .validation_data = &tcaa_root_flags_allowed },
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001264 [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001265};
1266
David Ahernc21ef3e2017-04-16 09:48:24 -07001267static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
1268 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001270 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001271 struct nlattr *tca[TCA_ROOT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +00001272 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 int ret = 0, ovr = 0;
1274
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001275 if ((n->nlmsg_type != RTM_GETACTION) &&
1276 !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001277 return -EPERM;
1278
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001279 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ROOT_MAX, NULL,
David Ahernc21ef3e2017-04-16 09:48:24 -07001280 extack);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001281 if (ret < 0)
1282 return ret;
1283
1284 if (tca[TCA_ACT_TAB] == NULL) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001285 NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return -EINVAL;
1287 }
1288
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001289 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 switch (n->nlmsg_type) {
1291 case RTM_NEWACTION:
1292 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001293 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 * Note that CREATE | EXCL implies that
1295 * but since we want avoid ambiguity (eg when flags
1296 * is zero) then just set this
1297 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001298 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 ovr = 1;
1300replay:
Alexander Aringaea0d722018-02-15 10:54:54 -05001301 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr,
1302 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (ret == -EAGAIN)
1304 goto replay;
1305 break;
1306 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001307 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Alexander Aring84ae0172018-02-15 10:54:55 -05001308 portid, RTM_DELACTION, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 break;
1310 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001311 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Alexander Aring84ae0172018-02-15 10:54:55 -05001312 portid, RTM_GETACTION, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 break;
1314 default:
1315 BUG();
1316 }
1317
1318 return ret;
1319}
1320
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001321static struct nlattr *find_dump_kind(struct nlattr **nla)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001323 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001324 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001325 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001327 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (tb1 == NULL)
1329 return NULL;
1330
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001331 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
Johannes Bergfceb6432017-04-12 14:34:07 +02001332 NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Patrick McHardy6d834e02008-01-23 20:32:42 -08001335 if (tb[1] == NULL)
1336 return NULL;
Johannes Bergfceb6432017-04-12 14:34:07 +02001337 if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001339 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Thomas Graf26dab892006-07-05 20:45:06 -07001341 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001344static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
WANG Congddf97cc2016-02-22 15:57:53 -08001346 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001348 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001349 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 struct tc_action_ops *a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001352 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001353 struct nlattr *tb[TCA_ROOT_MAX + 1];
1354 struct nlattr *count_attr = NULL;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001355 unsigned long jiffy_since = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001356 struct nlattr *kind = NULL;
1357 struct nla_bitfield32 bf;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001358 u32 msecs_since = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001359 u32 act_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001361 ret = nlmsg_parse(cb->nlh, sizeof(struct tcamsg), tb, TCA_ROOT_MAX,
1362 tcaa_policy, NULL);
1363 if (ret < 0)
1364 return ret;
1365
1366 kind = find_dump_kind(tb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001368 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return 0;
1370 }
1371
Thomas Graf26dab892006-07-05 20:45:06 -07001372 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001373 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001376 cb->args[2] = 0;
1377 if (tb[TCA_ROOT_FLAGS]) {
1378 bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
1379 cb->args[2] = bf.value;
1380 }
1381
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001382 if (tb[TCA_ROOT_TIME_DELTA]) {
1383 msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
1384 }
1385
Eric W. Biederman15e47302012-09-07 20:12:54 +00001386 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001387 cb->nlh->nlmsg_type, sizeof(*t), 0);
1388 if (!nlh)
1389 goto out_module_put;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001390
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001391 if (msecs_since)
1392 jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
1393
David S. Miller8b00a532012-06-26 21:39:32 -07001394 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001396 t->tca__pad1 = 0;
1397 t->tca__pad2 = 0;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001398 cb->args[3] = jiffy_since;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001399 count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
1400 if (!count_attr)
1401 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001403 nest = nla_nest_start(skb, TCA_ACT_TAB);
1404 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001405 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Alexander Aring41780102018-02-15 10:54:58 -05001407 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001409 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001412 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 ret = skb->len;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001414 act_count = cb->args[1];
1415 memcpy(nla_data(count_attr), &act_count, sizeof(u32));
1416 cb->args[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 } else
Jamal Hadi Salimebecaa62016-06-13 18:08:42 -04001418 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001420 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001421 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 nlh->nlmsg_flags |= NLM_F_MULTI;
1423 module_put(a_o->owner);
1424 return skb->len;
1425
David S. Miller8b00a532012-06-26 21:39:32 -07001426out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001428 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return skb->len;
1430}
1431
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001432struct tcf_action_net {
1433 struct rhashtable egdev_ht;
1434};
1435
1436static unsigned int tcf_action_net_id;
1437
1438struct tcf_action_egdev_cb {
1439 struct list_head list;
1440 tc_setup_cb_t *cb;
1441 void *cb_priv;
1442};
1443
1444struct tcf_action_egdev {
1445 struct rhash_head ht_node;
1446 const struct net_device *dev;
1447 unsigned int refcnt;
1448 struct list_head cb_list;
1449};
1450
1451static const struct rhashtable_params tcf_action_egdev_ht_params = {
1452 .key_offset = offsetof(struct tcf_action_egdev, dev),
1453 .head_offset = offsetof(struct tcf_action_egdev, ht_node),
1454 .key_len = sizeof(const struct net_device *),
1455};
1456
1457static struct tcf_action_egdev *
1458tcf_action_egdev_lookup(const struct net_device *dev)
1459{
1460 struct net *net = dev_net(dev);
1461 struct tcf_action_net *tan = net_generic(net, tcf_action_net_id);
1462
1463 return rhashtable_lookup_fast(&tan->egdev_ht, &dev,
1464 tcf_action_egdev_ht_params);
1465}
1466
1467static struct tcf_action_egdev *
1468tcf_action_egdev_get(const struct net_device *dev)
1469{
1470 struct tcf_action_egdev *egdev;
1471 struct tcf_action_net *tan;
1472
1473 egdev = tcf_action_egdev_lookup(dev);
1474 if (egdev)
1475 goto inc_ref;
1476
1477 egdev = kzalloc(sizeof(*egdev), GFP_KERNEL);
1478 if (!egdev)
1479 return NULL;
1480 INIT_LIST_HEAD(&egdev->cb_list);
Or Gerlitz0843c092017-10-18 18:38:08 +03001481 egdev->dev = dev;
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001482 tan = net_generic(dev_net(dev), tcf_action_net_id);
1483 rhashtable_insert_fast(&tan->egdev_ht, &egdev->ht_node,
1484 tcf_action_egdev_ht_params);
1485
1486inc_ref:
1487 egdev->refcnt++;
1488 return egdev;
1489}
1490
1491static void tcf_action_egdev_put(struct tcf_action_egdev *egdev)
1492{
1493 struct tcf_action_net *tan;
1494
1495 if (--egdev->refcnt)
1496 return;
1497 tan = net_generic(dev_net(egdev->dev), tcf_action_net_id);
1498 rhashtable_remove_fast(&tan->egdev_ht, &egdev->ht_node,
1499 tcf_action_egdev_ht_params);
1500 kfree(egdev);
1501}
1502
1503static struct tcf_action_egdev_cb *
1504tcf_action_egdev_cb_lookup(struct tcf_action_egdev *egdev,
1505 tc_setup_cb_t *cb, void *cb_priv)
1506{
1507 struct tcf_action_egdev_cb *egdev_cb;
1508
1509 list_for_each_entry(egdev_cb, &egdev->cb_list, list)
1510 if (egdev_cb->cb == cb && egdev_cb->cb_priv == cb_priv)
1511 return egdev_cb;
1512 return NULL;
1513}
1514
1515static int tcf_action_egdev_cb_call(struct tcf_action_egdev *egdev,
1516 enum tc_setup_type type,
1517 void *type_data, bool err_stop)
1518{
1519 struct tcf_action_egdev_cb *egdev_cb;
1520 int ok_count = 0;
1521 int err;
1522
1523 list_for_each_entry(egdev_cb, &egdev->cb_list, list) {
1524 err = egdev_cb->cb(type, type_data, egdev_cb->cb_priv);
1525 if (err) {
1526 if (err_stop)
1527 return err;
1528 } else {
1529 ok_count++;
1530 }
1531 }
1532 return ok_count;
1533}
1534
1535static int tcf_action_egdev_cb_add(struct tcf_action_egdev *egdev,
1536 tc_setup_cb_t *cb, void *cb_priv)
1537{
1538 struct tcf_action_egdev_cb *egdev_cb;
1539
1540 egdev_cb = tcf_action_egdev_cb_lookup(egdev, cb, cb_priv);
1541 if (WARN_ON(egdev_cb))
1542 return -EEXIST;
1543 egdev_cb = kzalloc(sizeof(*egdev_cb), GFP_KERNEL);
1544 if (!egdev_cb)
1545 return -ENOMEM;
1546 egdev_cb->cb = cb;
1547 egdev_cb->cb_priv = cb_priv;
1548 list_add(&egdev_cb->list, &egdev->cb_list);
1549 return 0;
1550}
1551
1552static void tcf_action_egdev_cb_del(struct tcf_action_egdev *egdev,
1553 tc_setup_cb_t *cb, void *cb_priv)
1554{
1555 struct tcf_action_egdev_cb *egdev_cb;
1556
1557 egdev_cb = tcf_action_egdev_cb_lookup(egdev, cb, cb_priv);
1558 if (WARN_ON(!egdev_cb))
1559 return;
1560 list_del(&egdev_cb->list);
1561 kfree(egdev_cb);
1562}
1563
1564static int __tc_setup_cb_egdev_register(const struct net_device *dev,
1565 tc_setup_cb_t *cb, void *cb_priv)
1566{
1567 struct tcf_action_egdev *egdev = tcf_action_egdev_get(dev);
1568 int err;
1569
1570 if (!egdev)
1571 return -ENOMEM;
1572 err = tcf_action_egdev_cb_add(egdev, cb, cb_priv);
1573 if (err)
1574 goto err_cb_add;
1575 return 0;
1576
1577err_cb_add:
1578 tcf_action_egdev_put(egdev);
1579 return err;
1580}
1581int tc_setup_cb_egdev_register(const struct net_device *dev,
1582 tc_setup_cb_t *cb, void *cb_priv)
1583{
1584 int err;
1585
1586 rtnl_lock();
1587 err = __tc_setup_cb_egdev_register(dev, cb, cb_priv);
1588 rtnl_unlock();
1589 return err;
1590}
1591EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_register);
1592
1593static void __tc_setup_cb_egdev_unregister(const struct net_device *dev,
1594 tc_setup_cb_t *cb, void *cb_priv)
1595{
1596 struct tcf_action_egdev *egdev = tcf_action_egdev_lookup(dev);
1597
1598 if (WARN_ON(!egdev))
1599 return;
1600 tcf_action_egdev_cb_del(egdev, cb, cb_priv);
1601 tcf_action_egdev_put(egdev);
1602}
1603void tc_setup_cb_egdev_unregister(const struct net_device *dev,
1604 tc_setup_cb_t *cb, void *cb_priv)
1605{
1606 rtnl_lock();
1607 __tc_setup_cb_egdev_unregister(dev, cb, cb_priv);
1608 rtnl_unlock();
1609}
1610EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_unregister);
1611
1612int tc_setup_cb_egdev_call(const struct net_device *dev,
1613 enum tc_setup_type type, void *type_data,
1614 bool err_stop)
1615{
1616 struct tcf_action_egdev *egdev = tcf_action_egdev_lookup(dev);
1617
1618 if (!egdev)
1619 return 0;
1620 return tcf_action_egdev_cb_call(egdev, type, type_data, err_stop);
1621}
1622EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_call);
1623
1624static __net_init int tcf_action_net_init(struct net *net)
1625{
1626 struct tcf_action_net *tan = net_generic(net, tcf_action_net_id);
1627
1628 return rhashtable_init(&tan->egdev_ht, &tcf_action_egdev_ht_params);
1629}
1630
1631static void __net_exit tcf_action_net_exit(struct net *net)
1632{
1633 struct tcf_action_net *tan = net_generic(net, tcf_action_net_id);
1634
1635 rhashtable_destroy(&tan->egdev_ht);
1636}
1637
1638static struct pernet_operations tcf_action_net_ops = {
1639 .init = tcf_action_net_init,
1640 .exit = tcf_action_net_exit,
1641 .id = &tcf_action_net_id,
1642 .size = sizeof(struct tcf_action_net),
1643};
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645static int __init tc_action_init(void)
1646{
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001647 int err;
1648
1649 err = register_pernet_subsys(&tcf_action_net_ops);
1650 if (err)
1651 return err;
1652
Florian Westphalb97bac62017-08-09 20:41:48 +02001653 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
1654 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
Greg Rosec7ac8672011-06-10 01:27:09 +00001655 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
Florian Westphalb97bac62017-08-09 20:41:48 +02001656 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 return 0;
1659}
1660
1661subsys_initcall(tc_action_init);