net: sched: introduce terse dump flag
[linux-block.git] / net / sched / act_api.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * net/sched/act_api.c Packet action API.
4 *
1da177e4 5 * Author: Jamal Hadi Salim
1da177e4
LT
6 */
7
1da177e4
LT
8#include <linux/types.h>
9#include <linux/kernel.h>
1da177e4 10#include <linux/string.h>
1da177e4 11#include <linux/errno.h>
5a0e3ad6 12#include <linux/slab.h>
1da177e4 13#include <linux/skbuff.h>
1da177e4
LT
14#include <linux/init.h>
15#include <linux/kmod.h>
ab27cfb8 16#include <linux/err.h>
3a9a231d 17#include <linux/module.h>
b854272b
DL
18#include <net/net_namespace.h>
19#include <net/sock.h>
1da177e4 20#include <net/sch_generic.h>
1045ba77 21#include <net/pkt_cls.h>
1da177e4 22#include <net/act_api.h>
dc5fc579 23#include <net/netlink.h>
1da177e4 24
db50514f
JP
25static void tcf_action_goto_chain_exec(const struct tc_action *a,
26 struct tcf_result *res)
27{
ee3bbfe8 28 const struct tcf_chain *chain = rcu_dereference_bh(a->goto_chain);
db50514f
JP
29
30 res->goto_tp = rcu_dereference_bh(chain->filter_chain);
31}
32
eec94fdb
VB
33static void tcf_free_cookie_rcu(struct rcu_head *p)
34{
35 struct tc_cookie *cookie = container_of(p, struct tc_cookie, rcu);
36
37 kfree(cookie->data);
38 kfree(cookie);
39}
40
41static void tcf_set_action_cookie(struct tc_cookie __rcu **old_cookie,
42 struct tc_cookie *new_cookie)
43{
44 struct tc_cookie *old;
45
0dbc81ea 46 old = xchg((__force struct tc_cookie **)old_cookie, new_cookie);
eec94fdb
VB
47 if (old)
48 call_rcu(&old->rcu, tcf_free_cookie_rcu);
49}
50
85d0966f
DC
51int tcf_action_check_ctrlact(int action, struct tcf_proto *tp,
52 struct tcf_chain **newchain,
53 struct netlink_ext_ack *extack)
54{
55 int opcode = TC_ACT_EXT_OPCODE(action), ret = -EINVAL;
56 u32 chain_index;
57
58 if (!opcode)
59 ret = action > TC_ACT_VALUE_MAX ? -EINVAL : 0;
60 else if (opcode <= TC_ACT_EXT_OPCODE_MAX || action == TC_ACT_UNSPEC)
61 ret = 0;
62 if (ret) {
63 NL_SET_ERR_MSG(extack, "invalid control action");
64 goto end;
65 }
66
67 if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN)) {
68 chain_index = action & TC_ACT_EXT_VAL_MASK;
69 if (!tp || !newchain) {
70 ret = -EINVAL;
71 NL_SET_ERR_MSG(extack,
72 "can't goto NULL proto/chain");
73 goto end;
74 }
75 *newchain = tcf_chain_get_by_act(tp->chain->block, chain_index);
76 if (!*newchain) {
77 ret = -ENOMEM;
78 NL_SET_ERR_MSG(extack,
79 "can't allocate goto_chain");
80 }
81 }
82end:
83 return ret;
84}
85EXPORT_SYMBOL(tcf_action_check_ctrlact);
86
87struct tcf_chain *tcf_action_set_ctrlact(struct tc_action *a, int action,
ee3bbfe8 88 struct tcf_chain *goto_chain)
85d0966f 89{
85d0966f 90 a->tcfa_action = action;
445d3749 91 goto_chain = rcu_replace_pointer(a->goto_chain, goto_chain, 1);
ee3bbfe8 92 return goto_chain;
85d0966f
DC
93}
94EXPORT_SYMBOL(tcf_action_set_ctrlact);
95
d7fb60b9
CW
96/* XXX: For standalone actions, we don't need a RCU grace period either, because
97 * actions are always connected to filters and filters are already destroyed in
98 * RCU callbacks, so after a RCU grace period actions are already disconnected
99 * from filters. Readers later can not find us.
100 */
101static void free_tcf(struct tc_action *p)
519c818e 102{
ee3bbfe8 103 struct tcf_chain *chain = rcu_dereference_protected(p->goto_chain, 1);
85d0966f 104
519c818e 105 free_percpu(p->cpu_bstats);
28169aba 106 free_percpu(p->cpu_bstats_hw);
519c818e 107 free_percpu(p->cpu_qstats);
1045ba77 108
eec94fdb 109 tcf_set_action_cookie(&p->act_cookie, NULL);
85d0966f
DC
110 if (chain)
111 tcf_chain_put_by_act(chain);
1045ba77 112
519c818e
ED
113 kfree(p);
114}
115
16af6067 116static void tcf_action_cleanup(struct tc_action *p)
e9ce1cd3 117{
16af6067
VB
118 if (p->ops->cleanup)
119 p->ops->cleanup(p);
120
1c0d32fd 121 gen_kill_estimator(&p->tcfa_rate_est);
d7fb60b9 122 free_tcf(p);
e9ce1cd3 123}
e9ce1cd3 124
16af6067
VB
125static int __tcf_action_put(struct tc_action *p, bool bind)
126{
127 struct tcf_idrinfo *idrinfo = p->idrinfo;
128
95278dda 129 if (refcount_dec_and_mutex_lock(&p->tcfa_refcnt, &idrinfo->lock)) {
16af6067
VB
130 if (bind)
131 atomic_dec(&p->tcfa_bindcnt);
132 idr_remove(&idrinfo->action_idr, p->tcfa_index);
95278dda 133 mutex_unlock(&idrinfo->lock);
16af6067
VB
134
135 tcf_action_cleanup(p);
136 return 1;
137 }
138
139 if (bind)
140 atomic_dec(&p->tcfa_bindcnt);
141
142 return 0;
143}
144
65a206c0 145int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
e9ce1cd3
DM
146{
147 int ret = 0;
148
036bb443
VB
149 /* Release with strict==1 and bind==0 is only called through act API
150 * interface (classifiers always bind). Only case when action with
151 * positive reference count and zero bind count can exist is when it was
152 * also created with act API (unbinding last classifier will destroy the
153 * action if it was created by classifier). So only case when bind count
154 * can be changed after initial check is when unbound action is
155 * destroyed by act API while classifier binds to action with same id
156 * concurrently. This result either creation of new action(same behavior
157 * as before), or reusing existing action if concurrent process
158 * increments reference count before action is deleted. Both scenarios
159 * are acceptable.
160 */
e9ce1cd3 161 if (p) {
16af6067 162 if (!bind && strict && atomic_read(&p->tcfa_bindcnt) > 0)
55334a5d 163 return -EPERM;
e9ce1cd3 164
16af6067 165 if (__tcf_action_put(p, bind))
1d4150c0 166 ret = ACT_P_DELETED;
e9ce1cd3 167 }
28e6b67f 168
e9ce1cd3
DM
169 return ret;
170}
65a206c0 171EXPORT_SYMBOL(__tcf_idr_release);
e9ce1cd3 172
4e76e75d
RM
173static size_t tcf_action_shared_attrs_size(const struct tc_action *act)
174{
e0479b67 175 struct tc_cookie *act_cookie;
4e76e75d
RM
176 u32 cookie_len = 0;
177
e0479b67
VB
178 rcu_read_lock();
179 act_cookie = rcu_dereference(act->act_cookie);
180
181 if (act_cookie)
182 cookie_len = nla_total_size(act_cookie->len);
183 rcu_read_unlock();
4e76e75d
RM
184
185 return nla_total_size(0) /* action number nested */
186 + nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */
187 + cookie_len /* TCA_ACT_COOKIE */
0dfb2d82 188 + nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_HW_STATS */
4e76e75d 189 + nla_total_size(0) /* TCA_ACT_STATS nested */
1521a67e 190 + nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_FLAGS */
4e76e75d
RM
191 /* TCA_STATS_BASIC */
192 + nla_total_size_64bit(sizeof(struct gnet_stats_basic))
b33e699f
ED
193 /* TCA_STATS_PKT64 */
194 + nla_total_size_64bit(sizeof(u64))
4e76e75d
RM
195 /* TCA_STATS_QUEUE */
196 + nla_total_size_64bit(sizeof(struct gnet_stats_queue))
197 + nla_total_size(0) /* TCA_OPTIONS nested */
198 + nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */
199}
200
201static size_t tcf_action_full_attrs_size(size_t sz)
202{
203 return NLMSG_HDRLEN /* struct nlmsghdr */
204 + sizeof(struct tcamsg)
205 + nla_total_size(0) /* TCA_ACT_TAB nested */
206 + sz;
207}
208
209static size_t tcf_action_fill_size(const struct tc_action *act)
210{
211 size_t sz = tcf_action_shared_attrs_size(act);
212
213 if (act->ops->get_fill_size)
214 return act->ops->get_fill_size(act) + sz;
215 return sz;
216}
217
65a206c0 218static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
a85a970a 219 struct netlink_callback *cb)
e9ce1cd3 220{
65a206c0 221 int err = 0, index = -1, s_i = 0, n_i = 0;
90825b23 222 u32 act_flags = cb->args[2];
e62e484d 223 unsigned long jiffy_since = cb->args[3];
4b3550ef 224 struct nlattr *nest;
65a206c0
CM
225 struct idr *idr = &idrinfo->action_idr;
226 struct tc_action *p;
227 unsigned long id = 1;
e33d2b74 228 unsigned long tmp;
e9ce1cd3 229
95278dda 230 mutex_lock(&idrinfo->lock);
e9ce1cd3
DM
231
232 s_i = cb->args[0];
233
e33d2b74 234 idr_for_each_entry_ul(idr, p, tmp, id) {
65a206c0
CM
235 index++;
236 if (index < s_i)
237 continue;
238
239 if (jiffy_since &&
240 time_after(jiffy_since,
241 (unsigned long)p->tcfa_tm.lastuse))
242 continue;
243
ae0be8de 244 nest = nla_nest_start_noflag(skb, n_i);
734549eb
CD
245 if (!nest) {
246 index--;
65a206c0 247 goto nla_put_failure;
734549eb 248 }
65a206c0
CM
249 err = tcf_action_dump_1(skb, p, 0, 0);
250 if (err < 0) {
251 index--;
252 nlmsg_trim(skb, nest);
253 goto done;
e9ce1cd3 254 }
65a206c0
CM
255 nla_nest_end(skb, nest);
256 n_i++;
257 if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) &&
258 n_i >= TCA_ACT_MAX_PRIO)
259 goto done;
e9ce1cd3
DM
260 }
261done:
e62e484d
JHS
262 if (index >= 0)
263 cb->args[0] = index + 1;
264
95278dda 265 mutex_unlock(&idrinfo->lock);
90825b23 266 if (n_i) {
90825b23
JHS
267 if (act_flags & TCA_FLAG_LARGE_DUMP_ON)
268 cb->args[1] = n_i;
269 }
e9ce1cd3
DM
270 return n_i;
271
7ba699c6 272nla_put_failure:
4b3550ef 273 nla_nest_cancel(skb, nest);
e9ce1cd3
DM
274 goto done;
275}
276
ec3ed293
VB
277static int tcf_idr_release_unsafe(struct tc_action *p)
278{
279 if (atomic_read(&p->tcfa_bindcnt) > 0)
280 return -EPERM;
281
282 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
283 idr_remove(&p->idrinfo->action_idr, p->tcfa_index);
284 tcf_action_cleanup(p);
285 return ACT_P_DELETED;
286 }
287
288 return 0;
289}
290
65a206c0 291static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
a85a970a 292 const struct tc_action_ops *ops)
e9ce1cd3 293{
4b3550ef 294 struct nlattr *nest;
65a206c0 295 int n_i = 0;
55334a5d 296 int ret = -EINVAL;
65a206c0
CM
297 struct idr *idr = &idrinfo->action_idr;
298 struct tc_action *p;
299 unsigned long id = 1;
e33d2b74 300 unsigned long tmp;
e9ce1cd3 301
ae0be8de 302 nest = nla_nest_start_noflag(skb, 0);
4b3550ef
PM
303 if (nest == NULL)
304 goto nla_put_failure;
a85a970a 305 if (nla_put_string(skb, TCA_KIND, ops->kind))
1b34ec43 306 goto nla_put_failure;
65a206c0 307
95278dda 308 mutex_lock(&idrinfo->lock);
e33d2b74 309 idr_for_each_entry_ul(idr, p, tmp, id) {
ec3ed293 310 ret = tcf_idr_release_unsafe(p);
65a206c0 311 if (ret == ACT_P_DELETED) {
255cd50f 312 module_put(ops->owner);
65a206c0
CM
313 n_i++;
314 } else if (ret < 0) {
95278dda 315 mutex_unlock(&idrinfo->lock);
65a206c0 316 goto nla_put_failure;
e9ce1cd3
DM
317 }
318 }
95278dda 319 mutex_unlock(&idrinfo->lock);
ec3ed293 320
1b34ec43
DM
321 if (nla_put_u32(skb, TCA_FCNT, n_i))
322 goto nla_put_failure;
4b3550ef 323 nla_nest_end(skb, nest);
e9ce1cd3
DM
324
325 return n_i;
7ba699c6 326nla_put_failure:
4b3550ef 327 nla_nest_cancel(skb, nest);
55334a5d 328 return ret;
e9ce1cd3
DM
329}
330
ddf97ccd
WC
331int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
332 struct netlink_callback *cb, int type,
b3620145
AA
333 const struct tc_action_ops *ops,
334 struct netlink_ext_ack *extack)
e9ce1cd3 335{
65a206c0 336 struct tcf_idrinfo *idrinfo = tn->idrinfo;
ddf97ccd 337
e9ce1cd3 338 if (type == RTM_DELACTION) {
65a206c0 339 return tcf_del_walker(idrinfo, skb, ops);
e9ce1cd3 340 } else if (type == RTM_GETACTION) {
65a206c0 341 return tcf_dump_walker(idrinfo, skb, cb);
e9ce1cd3 342 } else {
b3620145
AA
343 WARN(1, "tcf_generic_walker: unknown command %d\n", type);
344 NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command");
e9ce1cd3
DM
345 return -EINVAL;
346 }
347}
ddf97ccd 348EXPORT_SYMBOL(tcf_generic_walker);
e9ce1cd3 349
7d485c45 350int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
e9ce1cd3 351{
3f7c72bc
VB
352 struct tcf_idrinfo *idrinfo = tn->idrinfo;
353 struct tc_action *p;
e9ce1cd3 354
95278dda 355 mutex_lock(&idrinfo->lock);
322d884b 356 p = idr_find(&idrinfo->action_idr, index);
7d485c45 357 if (IS_ERR(p))
0190c1d4 358 p = NULL;
7d485c45 359 else if (p)
3f7c72bc 360 refcount_inc(&p->tcfa_refcnt);
95278dda 361 mutex_unlock(&idrinfo->lock);
e9ce1cd3 362
3f7c72bc
VB
363 if (p) {
364 *a = p;
365 return true;
366 }
367 return false;
e9ce1cd3 368}
65a206c0 369EXPORT_SYMBOL(tcf_idr_search);
e9ce1cd3 370
97a3f84f 371static int tcf_idr_delete_index(struct tcf_idrinfo *idrinfo, u32 index)
2a2ea349 372{
2a2ea349
VB
373 struct tc_action *p;
374 int ret = 0;
375
95278dda 376 mutex_lock(&idrinfo->lock);
2a2ea349
VB
377 p = idr_find(&idrinfo->action_idr, index);
378 if (!p) {
95278dda 379 mutex_unlock(&idrinfo->lock);
2a2ea349
VB
380 return -ENOENT;
381 }
382
383 if (!atomic_read(&p->tcfa_bindcnt)) {
384 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
385 struct module *owner = p->ops->owner;
386
387 WARN_ON(p != idr_remove(&idrinfo->action_idr,
388 p->tcfa_index));
95278dda 389 mutex_unlock(&idrinfo->lock);
2a2ea349 390
16af6067 391 tcf_action_cleanup(p);
2a2ea349
VB
392 module_put(owner);
393 return 0;
394 }
395 ret = 0;
396 } else {
397 ret = -EPERM;
398 }
399
95278dda 400 mutex_unlock(&idrinfo->lock);
2a2ea349
VB
401 return ret;
402}
2a2ea349 403
65a206c0
CM
404int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
405 struct tc_action **a, const struct tc_action_ops *ops,
e3822678 406 int bind, bool cpustats, u32 flags)
e9ce1cd3 407{
ec0595cc 408 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
65a206c0 409 struct tcf_idrinfo *idrinfo = tn->idrinfo;
519c818e 410 int err = -ENOMEM;
e9ce1cd3
DM
411
412 if (unlikely(!p))
86062033 413 return -ENOMEM;
036bb443 414 refcount_set(&p->tcfa_refcnt, 1);
e9ce1cd3 415 if (bind)
036bb443 416 atomic_set(&p->tcfa_bindcnt, 1);
e9ce1cd3 417
519c818e
ED
418 if (cpustats) {
419 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
339913a8 420 if (!p->cpu_bstats)
519c818e 421 goto err1;
28169aba
EC
422 p->cpu_bstats_hw = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
423 if (!p->cpu_bstats_hw)
424 goto err2;
339913a8
MW
425 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
426 if (!p->cpu_qstats)
28169aba 427 goto err3;
519c818e 428 }
ec0595cc 429 spin_lock_init(&p->tcfa_lock);
339913a8 430 p->tcfa_index = index;
ec0595cc
WC
431 p->tcfa_tm.install = jiffies;
432 p->tcfa_tm.lastuse = jiffies;
433 p->tcfa_tm.firstuse = 0;
e3822678 434 p->tcfa_flags = flags;
0e991ec6 435 if (est) {
ec0595cc
WC
436 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
437 &p->tcfa_rate_est,
438 &p->tcfa_lock, NULL, est);
339913a8 439 if (err)
28169aba 440 goto err4;
0e991ec6
SH
441 }
442
65a206c0 443 p->idrinfo = idrinfo;
ec0595cc 444 p->ops = ops;
ec0595cc 445 *a = p;
86062033 446 return 0;
28169aba 447err4:
339913a8 448 free_percpu(p->cpu_qstats);
28169aba
EC
449err3:
450 free_percpu(p->cpu_bstats_hw);
339913a8
MW
451err2:
452 free_percpu(p->cpu_bstats);
453err1:
454 kfree(p);
455 return err;
e9ce1cd3 456}
65a206c0 457EXPORT_SYMBOL(tcf_idr_create);
e9ce1cd3 458
e3822678
VB
459int tcf_idr_create_from_flags(struct tc_action_net *tn, u32 index,
460 struct nlattr *est, struct tc_action **a,
461 const struct tc_action_ops *ops, int bind,
462 u32 flags)
463{
464 /* Set cpustats according to actions flags. */
465 return tcf_idr_create(tn, index, est, a, ops, bind,
466 !(flags & TCA_ACT_FLAGS_NO_PERCPU_STATS), flags);
467}
468EXPORT_SYMBOL(tcf_idr_create_from_flags);
469
65a206c0 470void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a)
e9ce1cd3 471{
65a206c0 472 struct tcf_idrinfo *idrinfo = tn->idrinfo;
e9ce1cd3 473
95278dda 474 mutex_lock(&idrinfo->lock);
0190c1d4
VB
475 /* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
476 WARN_ON(!IS_ERR(idr_replace(&idrinfo->action_idr, a, a->tcfa_index)));
95278dda 477 mutex_unlock(&idrinfo->lock);
e9ce1cd3 478}
65a206c0 479EXPORT_SYMBOL(tcf_idr_insert);
1da177e4 480
0190c1d4
VB
481/* Cleanup idr index that was allocated but not initialized. */
482
483void tcf_idr_cleanup(struct tc_action_net *tn, u32 index)
484{
485 struct tcf_idrinfo *idrinfo = tn->idrinfo;
486
95278dda 487 mutex_lock(&idrinfo->lock);
0190c1d4
VB
488 /* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
489 WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index)));
95278dda 490 mutex_unlock(&idrinfo->lock);
0190c1d4
VB
491}
492EXPORT_SYMBOL(tcf_idr_cleanup);
493
494/* Check if action with specified index exists. If actions is found, increments
495 * its reference and bind counters, and return 1. Otherwise insert temporary
496 * error pointer (to prevent concurrent users from inserting actions with same
497 * index) and return 0.
498 */
499
500int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
501 struct tc_action **a, int bind)
502{
503 struct tcf_idrinfo *idrinfo = tn->idrinfo;
504 struct tc_action *p;
505 int ret;
506
507again:
95278dda 508 mutex_lock(&idrinfo->lock);
0190c1d4
VB
509 if (*index) {
510 p = idr_find(&idrinfo->action_idr, *index);
511 if (IS_ERR(p)) {
512 /* This means that another process allocated
513 * index but did not assign the pointer yet.
514 */
95278dda 515 mutex_unlock(&idrinfo->lock);
0190c1d4
VB
516 goto again;
517 }
518
519 if (p) {
520 refcount_inc(&p->tcfa_refcnt);
521 if (bind)
522 atomic_inc(&p->tcfa_bindcnt);
523 *a = p;
524 ret = 1;
525 } else {
526 *a = NULL;
527 ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
95278dda 528 *index, GFP_KERNEL);
0190c1d4
VB
529 if (!ret)
530 idr_replace(&idrinfo->action_idr,
531 ERR_PTR(-EBUSY), *index);
532 }
533 } else {
534 *index = 1;
535 *a = NULL;
536 ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
95278dda 537 UINT_MAX, GFP_KERNEL);
0190c1d4
VB
538 if (!ret)
539 idr_replace(&idrinfo->action_idr, ERR_PTR(-EBUSY),
540 *index);
541 }
95278dda 542 mutex_unlock(&idrinfo->lock);
0190c1d4
VB
543 return ret;
544}
545EXPORT_SYMBOL(tcf_idr_check_alloc);
546
65a206c0
CM
547void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
548 struct tcf_idrinfo *idrinfo)
1d4150c0 549{
65a206c0
CM
550 struct idr *idr = &idrinfo->action_idr;
551 struct tc_action *p;
552 int ret;
553 unsigned long id = 1;
e33d2b74 554 unsigned long tmp;
1d4150c0 555
e33d2b74 556 idr_for_each_entry_ul(idr, p, tmp, id) {
65a206c0
CM
557 ret = __tcf_idr_release(p, false, true);
558 if (ret == ACT_P_DELETED)
559 module_put(ops->owner);
560 else if (ret < 0)
561 return;
1d4150c0 562 }
65a206c0 563 idr_destroy(&idrinfo->action_idr);
1d4150c0 564}
65a206c0 565EXPORT_SYMBOL(tcf_idrinfo_destroy);
1d4150c0 566
1f747c26 567static LIST_HEAD(act_base);
1da177e4
LT
568static DEFINE_RWLOCK(act_mod_lock);
569
ddf97ccd
WC
570int tcf_register_action(struct tc_action_ops *act,
571 struct pernet_operations *ops)
1da177e4 572{
1f747c26 573 struct tc_action_ops *a;
ddf97ccd 574 int ret;
1da177e4 575
ddf97ccd 576 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
76c82d7a
JHS
577 return -EINVAL;
578
ab102b80
WC
579 /* We have to register pernet ops before making the action ops visible,
580 * otherwise tcf_action_init_1() could get a partially initialized
581 * netns.
582 */
583 ret = register_pernet_subsys(ops);
584 if (ret)
585 return ret;
586
1da177e4 587 write_lock(&act_mod_lock);
1f747c26 588 list_for_each_entry(a, &act_base, head) {
eddd2cf1 589 if (act->id == a->id || (strcmp(act->kind, a->kind) == 0)) {
1da177e4 590 write_unlock(&act_mod_lock);
ab102b80 591 unregister_pernet_subsys(ops);
1da177e4
LT
592 return -EEXIST;
593 }
594 }
1f747c26 595 list_add_tail(&act->head, &act_base);
1da177e4 596 write_unlock(&act_mod_lock);
ddf97ccd 597
1da177e4
LT
598 return 0;
599}
62e3ba1b 600EXPORT_SYMBOL(tcf_register_action);
1da177e4 601
ddf97ccd
WC
602int tcf_unregister_action(struct tc_action_ops *act,
603 struct pernet_operations *ops)
1da177e4 604{
1f747c26 605 struct tc_action_ops *a;
1da177e4
LT
606 int err = -ENOENT;
607
608 write_lock(&act_mod_lock);
a792866a
ED
609 list_for_each_entry(a, &act_base, head) {
610 if (a == act) {
611 list_del(&act->head);
612 err = 0;
1da177e4 613 break;
a792866a 614 }
1da177e4
LT
615 }
616 write_unlock(&act_mod_lock);
ab102b80
WC
617 if (!err)
618 unregister_pernet_subsys(ops);
1da177e4
LT
619 return err;
620}
62e3ba1b 621EXPORT_SYMBOL(tcf_unregister_action);
1da177e4
LT
622
623/* lookup by name */
624static struct tc_action_ops *tc_lookup_action_n(char *kind)
625{
a792866a 626 struct tc_action_ops *a, *res = NULL;
1da177e4
LT
627
628 if (kind) {
629 read_lock(&act_mod_lock);
1f747c26 630 list_for_each_entry(a, &act_base, head) {
1da177e4 631 if (strcmp(kind, a->kind) == 0) {
a792866a
ED
632 if (try_module_get(a->owner))
633 res = a;
1da177e4
LT
634 break;
635 }
636 }
637 read_unlock(&act_mod_lock);
638 }
a792866a 639 return res;
1da177e4
LT
640}
641
7ba699c6
PM
642/* lookup by nlattr */
643static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
1da177e4 644{
a792866a 645 struct tc_action_ops *a, *res = NULL;
1da177e4
LT
646
647 if (kind) {
648 read_lock(&act_mod_lock);
1f747c26 649 list_for_each_entry(a, &act_base, head) {
7ba699c6 650 if (nla_strcmp(kind, a->kind) == 0) {
a792866a
ED
651 if (try_module_get(a->owner))
652 res = a;
1da177e4
LT
653 break;
654 }
655 }
656 read_unlock(&act_mod_lock);
657 }
a792866a 658 return res;
1da177e4 659}
1da177e4 660
e0ee84de
JHS
661/*TCA_ACT_MAX_PRIO is 32, there count upto 32 */
662#define TCA_ACT_MAX_PRIO_MASK 0x1FF
22dc13c8
WC
663int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
664 int nr_actions, struct tcf_result *res)
1da177e4 665{
e0ee84de
JHS
666 u32 jmp_prgcnt = 0;
667 u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
ec1a9cca
JP
668 int i;
669 int ret = TC_ACT_OK;
1da177e4 670
e7246e12
WB
671 if (skb_skip_tc_classify(skb))
672 return TC_ACT_OK;
673
e0ee84de 674restart_act_graph:
22dc13c8
WC
675 for (i = 0; i < nr_actions; i++) {
676 const struct tc_action *a = actions[i];
677
e0ee84de
JHS
678 if (jmp_prgcnt > 0) {
679 jmp_prgcnt -= 1;
680 continue;
681 }
1da177e4 682repeat:
63acd680 683 ret = a->ops->act(skb, a, res);
63acd680
JHS
684 if (ret == TC_ACT_REPEAT)
685 goto repeat; /* we need a ttl - JHS */
e0ee84de 686
9da3242e 687 if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
e0ee84de
JHS
688 jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
689 if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
690 /* faulty opcode, stop pipeline */
691 return TC_ACT_OK;
692 } else {
693 jmp_ttl -= 1;
694 if (jmp_ttl > 0)
695 goto restart_act_graph;
696 else /* faulty graph, stop pipeline */
697 return TC_ACT_OK;
698 }
db50514f 699 } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
ee3bbfe8
DC
700 if (unlikely(!rcu_access_pointer(a->goto_chain))) {
701 net_warn_ratelimited("can't go to NULL chain!\n");
702 return TC_ACT_SHOT;
703 }
db50514f 704 tcf_action_goto_chain_exec(a, res);
e0ee84de
JHS
705 }
706
63acd680 707 if (ret != TC_ACT_PIPE)
e7246e12 708 break;
1da177e4 709 }
e0ee84de 710
1da177e4
LT
711 return ret;
712}
62e3ba1b 713EXPORT_SYMBOL(tcf_action_exec);
1da177e4 714
90b73b77 715int tcf_action_destroy(struct tc_action *actions[], int bind)
1da177e4 716{
255cd50f 717 const struct tc_action_ops *ops;
90b73b77
VB
718 struct tc_action *a;
719 int ret = 0, i;
1da177e4 720
90b73b77
VB
721 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
722 a = actions[i];
723 actions[i] = NULL;
255cd50f 724 ops = a->ops;
65a206c0 725 ret = __tcf_idr_release(a, bind, true);
55334a5d 726 if (ret == ACT_P_DELETED)
255cd50f 727 module_put(ops->owner);
55334a5d
WC
728 else if (ret < 0)
729 return ret;
1da177e4 730 }
55334a5d 731 return ret;
1da177e4
LT
732}
733
97763dc0
PA
734static int tcf_action_destroy_1(struct tc_action *a, int bind)
735{
736 struct tc_action *actions[] = { a, NULL };
737
738 return tcf_action_destroy(actions, bind);
739}
740
16af6067
VB
741static int tcf_action_put(struct tc_action *p)
742{
743 return __tcf_action_put(p, false);
744}
745
edfaf94f 746/* Put all actions in this array, skip those NULL's. */
90b73b77 747static void tcf_action_put_many(struct tc_action *actions[])
cae422f3 748{
90b73b77 749 int i;
cae422f3 750
edfaf94f 751 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
90b73b77 752 struct tc_action *a = actions[i];
edfaf94f 753 const struct tc_action_ops *ops;
cae422f3 754
edfaf94f
CW
755 if (!a)
756 continue;
757 ops = a->ops;
cae422f3
VB
758 if (tcf_action_put(a))
759 module_put(ops->owner);
760 }
761}
762
1da177e4
LT
763int
764tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
765{
1da177e4
LT
766 return a->ops->dump(skb, a, bind, ref);
767}
768
769int
770tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
771{
772 int err = -EINVAL;
27a884dc 773 unsigned char *b = skb_tail_pointer(skb);
4b3550ef 774 struct nlattr *nest;
eec94fdb 775 struct tc_cookie *cookie;
1da177e4 776
1b34ec43
DM
777 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
778 goto nla_put_failure;
1da177e4 779 if (tcf_action_copy_stats(skb, a, 0))
7ba699c6 780 goto nla_put_failure;
eec94fdb
VB
781
782 rcu_read_lock();
783 cookie = rcu_dereference(a->act_cookie);
784 if (cookie) {
785 if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) {
786 rcu_read_unlock();
1045ba77 787 goto nla_put_failure;
eec94fdb 788 }
1045ba77 789 }
eec94fdb 790 rcu_read_unlock();
1045ba77 791
8953b077
JP
792 if (a->hw_stats != TCA_ACT_HW_STATS_ANY &&
793 nla_put_bitfield32(skb, TCA_ACT_HW_STATS,
794 a->hw_stats, TCA_ACT_HW_STATS_ANY))
795 goto nla_put_failure;
e3822678 796
93a129eb
JP
797 if (a->used_hw_stats_valid &&
798 nla_put_bitfield32(skb, TCA_ACT_USED_HW_STATS,
799 a->used_hw_stats, TCA_ACT_HW_STATS_ANY))
800 goto nla_put_failure;
801
8953b077
JP
802 if (a->tcfa_flags &&
803 nla_put_bitfield32(skb, TCA_ACT_FLAGS,
804 a->tcfa_flags, a->tcfa_flags))
805 goto nla_put_failure;
e3822678 806
ae0be8de 807 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
4b3550ef
PM
808 if (nest == NULL)
809 goto nla_put_failure;
cc7ec456
ED
810 err = tcf_action_dump_old(skb, a, bind, ref);
811 if (err > 0) {
4b3550ef 812 nla_nest_end(skb, nest);
1da177e4
LT
813 return err;
814 }
815
7ba699c6 816nla_put_failure:
dc5fc579 817 nlmsg_trim(skb, b);
1da177e4
LT
818 return -1;
819}
62e3ba1b 820EXPORT_SYMBOL(tcf_action_dump_1);
1da177e4 821
90b73b77 822int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[],
0b0f43fe 823 int bind, int ref)
1da177e4
LT
824{
825 struct tc_action *a;
90b73b77 826 int err = -EINVAL, i;
4b3550ef 827 struct nlattr *nest;
1da177e4 828
90b73b77
VB
829 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
830 a = actions[i];
4097e9d2 831 nest = nla_nest_start_noflag(skb, i + 1);
4b3550ef
PM
832 if (nest == NULL)
833 goto nla_put_failure;
1da177e4
LT
834 err = tcf_action_dump_1(skb, a, bind, ref);
835 if (err < 0)
4fe683f5 836 goto errout;
4b3550ef 837 nla_nest_end(skb, nest);
1da177e4
LT
838 }
839
840 return 0;
841
7ba699c6 842nla_put_failure:
4fe683f5
TG
843 err = -EINVAL;
844errout:
4b3550ef 845 nla_nest_cancel(skb, nest);
4fe683f5 846 return err;
1da177e4
LT
847}
848
e0535ce5 849static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
1045ba77 850{
e0535ce5
WB
851 struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
852 if (!c)
853 return NULL;
854
855 c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
856 if (!c->data) {
857 kfree(c);
858 return NULL;
1045ba77 859 }
e0535ce5 860 c->len = nla_len(tb[TCA_ACT_COOKIE]);
1045ba77 861
e0535ce5 862 return c;
1045ba77
JHS
863}
864
0dfb2d82 865static u8 tcf_action_hw_stats_get(struct nlattr *hw_stats_attr)
44f86580 866{
0dfb2d82 867 struct nla_bitfield32 hw_stats_bf;
44f86580
JP
868
869 /* If the user did not pass the attr, that means he does
870 * not care about the type. Return "any" in that case
871 * which is setting on all supported types.
872 */
0dfb2d82
JK
873 if (!hw_stats_attr)
874 return TCA_ACT_HW_STATS_ANY;
875 hw_stats_bf = nla_get_bitfield32(hw_stats_attr);
876 return hw_stats_bf.value;
44f86580
JP
877}
878
199ce850 879static const struct nla_policy tcf_action_policy[TCA_ACT_MAX + 1] = {
4b793fec 880 [TCA_ACT_KIND] = { .type = NLA_STRING },
199ce850
CW
881 [TCA_ACT_INDEX] = { .type = NLA_U32 },
882 [TCA_ACT_COOKIE] = { .type = NLA_BINARY,
883 .len = TC_COOKIE_MAX_SIZE },
884 [TCA_ACT_OPTIONS] = { .type = NLA_NESTED },
47a1494b
JB
885 [TCA_ACT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_ACT_FLAGS_NO_PERCPU_STATS),
886 [TCA_ACT_HW_STATS] = NLA_POLICY_BITFIELD32(TCA_ACT_HW_STATS_ANY),
199ce850
CW
887};
888
9fb9f251
JP
889struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
890 struct nlattr *nla, struct nlattr *est,
aea0d727 891 char *name, int ovr, int bind,
789871bb 892 bool rtnl_held,
aea0d727 893 struct netlink_ext_ack *extack)
1da177e4 894{
abbb0d33 895 struct nla_bitfield32 flags = { 0, 0 };
0dfb2d82 896 u8 hw_stats = TCA_ACT_HW_STATS_ANY;
1da177e4
LT
897 struct tc_action *a;
898 struct tc_action_ops *a_o;
e0535ce5 899 struct tc_cookie *cookie = NULL;
1da177e4 900 char act_name[IFNAMSIZ];
cc7ec456 901 struct nlattr *tb[TCA_ACT_MAX + 1];
7ba699c6 902 struct nlattr *kind;
ab27cfb8 903 int err;
1da177e4 904
1da177e4 905 if (name == NULL) {
199ce850
CW
906 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
907 tcf_action_policy, extack);
cee63723 908 if (err < 0)
1da177e4 909 goto err_out;
cee63723 910 err = -EINVAL;
7ba699c6 911 kind = tb[TCA_ACT_KIND];
84ae017a
AA
912 if (!kind) {
913 NL_SET_ERR_MSG(extack, "TC action kind must be specified");
1da177e4 914 goto err_out;
84ae017a 915 }
4b793fec
CW
916 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) {
917 NL_SET_ERR_MSG(extack, "TC action name too long");
918 goto err_out;
919 }
199ce850 920 if (tb[TCA_ACT_COOKIE]) {
e0535ce5
WB
921 cookie = nla_memdup_cookie(tb);
922 if (!cookie) {
84ae017a 923 NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
e0535ce5
WB
924 err = -ENOMEM;
925 goto err_out;
926 }
927 }
0dfb2d82 928 hw_stats = tcf_action_hw_stats_get(tb[TCA_ACT_HW_STATS]);
abbb0d33
VB
929 if (tb[TCA_ACT_FLAGS])
930 flags = nla_get_bitfield32(tb[TCA_ACT_FLAGS]);
1da177e4 931 } else {
84ae017a
AA
932 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) {
933 NL_SET_ERR_MSG(extack, "TC action name too long");
934 err = -EINVAL;
1da177e4 935 goto err_out;
84ae017a 936 }
1da177e4
LT
937 }
938
939 a_o = tc_lookup_action_n(act_name);
940 if (a_o == NULL) {
95a5afca 941#ifdef CONFIG_MODULES
789871bb
VB
942 if (rtnl_held)
943 rtnl_unlock();
4bba3925 944 request_module("act_%s", act_name);
789871bb
VB
945 if (rtnl_held)
946 rtnl_lock();
1da177e4
LT
947
948 a_o = tc_lookup_action_n(act_name);
949
950 /* We dropped the RTNL semaphore in order to
951 * perform the module load. So, even if we
952 * succeeded in loading the module we have to
953 * tell the caller to replay the request. We
954 * indicate this using -EAGAIN.
955 */
956 if (a_o != NULL) {
ab27cfb8 957 err = -EAGAIN;
1da177e4
LT
958 goto err_mod;
959 }
960#endif
84ae017a 961 NL_SET_ERR_MSG(extack, "Failed to load TC action module");
ab27cfb8 962 err = -ENOENT;
1da177e4
LT
963 goto err_out;
964 }
965
1da177e4
LT
966 /* backward compatibility for policer */
967 if (name == NULL)
589dad6d 968 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind,
abbb0d33 969 rtnl_held, tp, flags.value, extack);
1da177e4 970 else
789871bb 971 err = a_o->init(net, nla, est, &a, ovr, bind, rtnl_held,
abbb0d33 972 tp, flags.value, extack);
ab27cfb8 973 if (err < 0)
a85a970a 974 goto err_mod;
1da177e4 975
eec94fdb
VB
976 if (!name && tb[TCA_ACT_COOKIE])
977 tcf_set_action_cookie(&a->act_cookie, cookie);
1045ba77 978
44f86580 979 if (!name)
0dfb2d82 980 a->hw_stats = hw_stats;
44f86580 981
1da177e4 982 /* module count goes up only when brand new policy is created
cc7ec456
ED
983 * if it exists and is only bound to in a_o->init() then
984 * ACT_P_CREATED is not returned (a zero is).
985 */
ab27cfb8 986 if (err != ACT_P_CREATED)
1da177e4 987 module_put(a_o->owner);
1da177e4 988
85d0966f 989 if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN) &&
ee3bbfe8 990 !rcu_access_pointer(a->goto_chain)) {
97763dc0 991 tcf_action_destroy_1(a, bind);
85d0966f 992 NL_SET_ERR_MSG(extack, "can't use goto chain with NULL chain");
97763dc0 993 return ERR_PTR(-EINVAL);
802bfb19
PA
994 }
995
1da177e4
LT
996 return a;
997
1da177e4
LT
998err_mod:
999 module_put(a_o->owner);
1000err_out:
e0535ce5
WB
1001 if (cookie) {
1002 kfree(cookie->data);
1003 kfree(cookie);
1004 }
ab27cfb8 1005 return ERR_PTR(err);
1da177e4
LT
1006}
1007
90b73b77
VB
1008/* Returns numbers of initialized actions or negative error. */
1009
9fb9f251
JP
1010int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
1011 struct nlattr *est, char *name, int ovr, int bind,
90b73b77 1012 struct tc_action *actions[], size_t *attr_size,
789871bb 1013 bool rtnl_held, struct netlink_ext_ack *extack)
1da177e4 1014{
cc7ec456 1015 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
33be6271 1016 struct tc_action *act;
4e76e75d 1017 size_t sz = 0;
cee63723 1018 int err;
1da177e4
LT
1019 int i;
1020
8cb08174
JB
1021 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
1022 extack);
cee63723 1023 if (err < 0)
33be6271 1024 return err;
1da177e4 1025
7ba699c6 1026 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
aea0d727 1027 act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind,
789871bb 1028 rtnl_held, extack);
33be6271
WC
1029 if (IS_ERR(act)) {
1030 err = PTR_ERR(act);
1da177e4 1031 goto err;
33be6271 1032 }
4e76e75d 1033 sz += tcf_action_fill_size(act);
90b73b77
VB
1034 /* Start from index 0 */
1035 actions[i - 1] = act;
1da177e4 1036 }
aecc5cef 1037
4e76e75d 1038 *attr_size = tcf_action_full_attrs_size(sz);
90b73b77 1039 return i - 1;
1da177e4
LT
1040
1041err:
33be6271
WC
1042 tcf_action_destroy(actions, bind);
1043 return err;
1da177e4
LT
1044}
1045
c8ecebd0
VB
1046void tcf_action_update_stats(struct tc_action *a, u64 bytes, u32 packets,
1047 bool drop, bool hw)
1048{
5e174d5e
VB
1049 if (a->cpu_bstats) {
1050 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
c8ecebd0 1051
5e174d5e
VB
1052 if (drop)
1053 this_cpu_ptr(a->cpu_qstats)->drops += packets;
1054
1055 if (hw)
1056 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats_hw),
1057 bytes, packets);
1058 return;
1059 }
c8ecebd0 1060
5e174d5e
VB
1061 _bstats_update(&a->tcfa_bstats, bytes, packets);
1062 if (drop)
1063 a->tcfa_qstats.drops += packets;
c8ecebd0 1064 if (hw)
5e174d5e 1065 _bstats_update(&a->tcfa_bstats_hw, bytes, packets);
c8ecebd0
VB
1066}
1067EXPORT_SYMBOL(tcf_action_update_stats);
1068
ec0595cc 1069int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
1da177e4
LT
1070 int compat_mode)
1071{
1072 int err = 0;
1073 struct gnet_dump d;
10297b99 1074
7eb8896d 1075 if (p == NULL)
1da177e4
LT
1076 goto errout;
1077
1078 /* compat_mode being true specifies a call that is supposed
06fe9fb4 1079 * to add additional backward compatibility statistic TLVs.
1da177e4
LT
1080 */
1081 if (compat_mode) {
ec0595cc 1082 if (p->type == TCA_OLD_COMPAT)
1da177e4 1083 err = gnet_stats_start_copy_compat(skb, 0,
9854518e
ND
1084 TCA_STATS,
1085 TCA_XSTATS,
ec0595cc 1086 &p->tcfa_lock, &d,
9854518e 1087 TCA_PAD);
1da177e4
LT
1088 else
1089 return 0;
1090 } else
1091 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
ec0595cc 1092 &p->tcfa_lock, &d, TCA_ACT_PAD);
1da177e4
LT
1093
1094 if (err < 0)
1095 goto errout;
1096
ec0595cc 1097 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
28169aba
EC
1098 gnet_stats_copy_basic_hw(NULL, &d, p->cpu_bstats_hw,
1099 &p->tcfa_bstats_hw) < 0 ||
1c0d32fd 1100 gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
519c818e 1101 gnet_stats_copy_queue(&d, p->cpu_qstats,
ec0595cc
WC
1102 &p->tcfa_qstats,
1103 p->tcfa_qstats.qlen) < 0)
1da177e4
LT
1104 goto errout;
1105
1106 if (gnet_stats_finish_copy(&d) < 0)
1107 goto errout;
1108
1109 return 0;
1110
1111errout:
1112 return -1;
1113}
1114
90b73b77 1115static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[],
0b0f43fe
JHS
1116 u32 portid, u32 seq, u16 flags, int event, int bind,
1117 int ref)
1da177e4
LT
1118{
1119 struct tcamsg *t;
1120 struct nlmsghdr *nlh;
27a884dc 1121 unsigned char *b = skb_tail_pointer(skb);
4b3550ef 1122 struct nlattr *nest;
1da177e4 1123
15e47304 1124 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
8b00a53c
DM
1125 if (!nlh)
1126 goto out_nlmsg_trim;
1127 t = nlmsg_data(nlh);
1da177e4 1128 t->tca_family = AF_UNSPEC;
9ef1d4c7
PM
1129 t->tca__pad1 = 0;
1130 t->tca__pad2 = 0;
10297b99 1131
ae0be8de 1132 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
1af85155 1133 if (!nest)
8b00a53c 1134 goto out_nlmsg_trim;
1da177e4 1135
33be6271 1136 if (tcf_action_dump(skb, actions, bind, ref) < 0)
8b00a53c 1137 goto out_nlmsg_trim;
1da177e4 1138
4b3550ef 1139 nla_nest_end(skb, nest);
10297b99 1140
27a884dc 1141 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1da177e4
LT
1142 return skb->len;
1143
8b00a53c 1144out_nlmsg_trim:
dc5fc579 1145 nlmsg_trim(skb, b);
1da177e4
LT
1146 return -1;
1147}
1148
1149static int
c4c4290c 1150tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
90b73b77 1151 struct tc_action *actions[], int event,
84ae017a 1152 struct netlink_ext_ack *extack)
1da177e4
LT
1153{
1154 struct sk_buff *skb;
1da177e4
LT
1155
1156 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1157 if (!skb)
1158 return -ENOBUFS;
0b0f43fe 1159 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
3f7c72bc 1160 0, 1) <= 0) {
84ae017a 1161 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
1da177e4
LT
1162 kfree_skb(skb);
1163 return -EINVAL;
1164 }
2942e900 1165
15e47304 1166 return rtnl_unicast(skb, net, portid);
1da177e4
LT
1167}
1168
ddf97ccd 1169static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
84ae017a
AA
1170 struct nlmsghdr *n, u32 portid,
1171 struct netlink_ext_ack *extack)
1da177e4 1172{
cc7ec456 1173 struct nlattr *tb[TCA_ACT_MAX + 1];
a85a970a 1174 const struct tc_action_ops *ops;
1da177e4
LT
1175 struct tc_action *a;
1176 int index;
ab27cfb8 1177 int err;
1da177e4 1178
199ce850
CW
1179 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1180 tcf_action_policy, extack);
cee63723 1181 if (err < 0)
ab27cfb8 1182 goto err_out;
1da177e4 1183
cee63723 1184 err = -EINVAL;
7ba699c6 1185 if (tb[TCA_ACT_INDEX] == NULL ||
84ae017a
AA
1186 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
1187 NL_SET_ERR_MSG(extack, "Invalid TC action index value");
ab27cfb8 1188 goto err_out;
84ae017a 1189 }
1587bac4 1190 index = nla_get_u32(tb[TCA_ACT_INDEX]);
1da177e4 1191
ab27cfb8 1192 err = -EINVAL;
a85a970a 1193 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
84ae017a 1194 if (!ops) { /* could happen in batch of actions */
f061b48c 1195 NL_SET_ERR_MSG(extack, "Specified TC action kind not found");
a85a970a 1196 goto err_out;
84ae017a 1197 }
ab27cfb8 1198 err = -ENOENT;
f061b48c
CW
1199 if (ops->lookup(net, &a, index) == 0) {
1200 NL_SET_ERR_MSG(extack, "TC action with specified index not found");
1da177e4 1201 goto err_mod;
f061b48c 1202 }
1da177e4 1203
a85a970a 1204 module_put(ops->owner);
1da177e4 1205 return a;
ab27cfb8 1206
1da177e4 1207err_mod:
a85a970a 1208 module_put(ops->owner);
ab27cfb8
PM
1209err_out:
1210 return ERR_PTR(err);
1da177e4
LT
1211}
1212
7316ae88 1213static int tca_action_flush(struct net *net, struct nlattr *nla,
84ae017a
AA
1214 struct nlmsghdr *n, u32 portid,
1215 struct netlink_ext_ack *extack)
1da177e4
LT
1216{
1217 struct sk_buff *skb;
1218 unsigned char *b;
1219 struct nlmsghdr *nlh;
1220 struct tcamsg *t;
1221 struct netlink_callback dcb;
4b3550ef 1222 struct nlattr *nest;
cc7ec456 1223 struct nlattr *tb[TCA_ACT_MAX + 1];
a85a970a 1224 const struct tc_action_ops *ops;
7ba699c6 1225 struct nlattr *kind;
36723873 1226 int err = -ENOMEM;
1da177e4 1227
1da177e4 1228 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
84ae017a 1229 if (!skb)
36723873 1230 return err;
1da177e4 1231
27a884dc 1232 b = skb_tail_pointer(skb);
1da177e4 1233
199ce850
CW
1234 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1235 tcf_action_policy, extack);
cee63723 1236 if (err < 0)
1da177e4
LT
1237 goto err_out;
1238
cee63723 1239 err = -EINVAL;
7ba699c6 1240 kind = tb[TCA_ACT_KIND];
a85a970a 1241 ops = tc_lookup_action(kind);
84ae017a
AA
1242 if (!ops) { /*some idjot trying to flush unknown action */
1243 NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
1da177e4 1244 goto err_out;
84ae017a 1245 }
1da177e4 1246
0b0f43fe
JHS
1247 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
1248 sizeof(*t), 0);
84ae017a
AA
1249 if (!nlh) {
1250 NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
8b00a53c 1251 goto out_module_put;
84ae017a 1252 }
8b00a53c 1253 t = nlmsg_data(nlh);
1da177e4 1254 t->tca_family = AF_UNSPEC;
9ef1d4c7
PM
1255 t->tca__pad1 = 0;
1256 t->tca__pad2 = 0;
1da177e4 1257
ae0be8de 1258 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
84ae017a
AA
1259 if (!nest) {
1260 NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
8b00a53c 1261 goto out_module_put;
84ae017a 1262 }
1da177e4 1263
41780105 1264 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack);
66dede2d
DC
1265 if (err <= 0) {
1266 nla_nest_cancel(skb, nest);
8b00a53c 1267 goto out_module_put;
66dede2d 1268 }
1da177e4 1269
4b3550ef 1270 nla_nest_end(skb, nest);
1da177e4 1271
27a884dc 1272 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1da177e4 1273 nlh->nlmsg_flags |= NLM_F_ROOT;
a85a970a 1274 module_put(ops->owner);
15e47304 1275 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
cc7ec456 1276 n->nlmsg_flags & NLM_F_ECHO);
1da177e4
LT
1277 if (err > 0)
1278 return 0;
84ae017a
AA
1279 if (err < 0)
1280 NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
1da177e4
LT
1281
1282 return err;
1283
8b00a53c 1284out_module_put:
a85a970a 1285 module_put(ops->owner);
1da177e4
LT
1286err_out:
1287 kfree_skb(skb);
1da177e4
LT
1288 return err;
1289}
1290
b144e7ec 1291static int tcf_action_delete(struct net *net, struct tc_action *actions[])
16af6067 1292{
97a3f84f 1293 int i;
16af6067 1294
90b73b77
VB
1295 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
1296 struct tc_action *a = actions[i];
16af6067 1297 const struct tc_action_ops *ops = a->ops;
16af6067
VB
1298 /* Actions can be deleted concurrently so we must save their
1299 * type and id to search again after reference is released.
1300 */
97a3f84f
CW
1301 struct tcf_idrinfo *idrinfo = a->idrinfo;
1302 u32 act_index = a->tcfa_index;
16af6067 1303
c10bbfae 1304 actions[i] = NULL;
16af6067
VB
1305 if (tcf_action_put(a)) {
1306 /* last reference, action was deleted concurrently */
1307 module_put(ops->owner);
1308 } else {
97a3f84f
CW
1309 int ret;
1310
16af6067 1311 /* now do the delete */
97a3f84f 1312 ret = tcf_idr_delete_index(idrinfo, act_index);
edfaf94f 1313 if (ret < 0)
16af6067
VB
1314 return ret;
1315 }
1316 }
1317 return 0;
1318}
1319
a56e1953 1320static int
90b73b77 1321tcf_del_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
edfaf94f 1322 u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
a56e1953
WC
1323{
1324 int ret;
1325 struct sk_buff *skb;
1326
d04e6990
RM
1327 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1328 GFP_KERNEL);
a56e1953
WC
1329 if (!skb)
1330 return -ENOBUFS;
1331
1332 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
3f7c72bc 1333 0, 2) <= 0) {
84ae017a 1334 NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
a56e1953
WC
1335 kfree_skb(skb);
1336 return -EINVAL;
1337 }
1338
1339 /* now do the delete */
b144e7ec 1340 ret = tcf_action_delete(net, actions);
55334a5d 1341 if (ret < 0) {
84ae017a 1342 NL_SET_ERR_MSG(extack, "Failed to delete TC action");
55334a5d
WC
1343 kfree_skb(skb);
1344 return ret;
1345 }
a56e1953
WC
1346
1347 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1348 n->nlmsg_flags & NLM_F_ECHO);
1349 if (ret > 0)
1350 return 0;
1351 return ret;
1352}
1353
1da177e4 1354static int
7316ae88 1355tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
84ae017a 1356 u32 portid, int event, struct netlink_ext_ack *extack)
1da177e4 1357{
cee63723 1358 int i, ret;
cc7ec456 1359 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
33be6271 1360 struct tc_action *act;
d04e6990 1361 size_t attr_size = 0;
edfaf94f 1362 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
1da177e4 1363
8cb08174
JB
1364 ret = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
1365 extack);
cee63723
PM
1366 if (ret < 0)
1367 return ret;
1da177e4 1368
cc7ec456 1369 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
1af85155 1370 if (tb[1])
84ae017a 1371 return tca_action_flush(net, tb[1], n, portid, extack);
1af85155 1372
84ae017a 1373 NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
1af85155 1374 return -EINVAL;
1da177e4
LT
1375 }
1376
7ba699c6 1377 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
84ae017a 1378 act = tcf_action_get_1(net, tb[i], n, portid, extack);
ab27cfb8
PM
1379 if (IS_ERR(act)) {
1380 ret = PTR_ERR(act);
1da177e4 1381 goto err;
ab27cfb8 1382 }
4e76e75d 1383 attr_size += tcf_action_fill_size(act);
90b73b77 1384 actions[i - 1] = act;
1da177e4 1385 }
4e76e75d
RM
1386
1387 attr_size = tcf_action_full_attrs_size(attr_size);
1da177e4
LT
1388
1389 if (event == RTM_GETACTION)
90b73b77 1390 ret = tcf_get_notify(net, portid, n, actions, event, extack);
1da177e4 1391 else { /* delete */
edfaf94f 1392 ret = tcf_del_notify(net, n, actions, portid, attr_size, extack);
a56e1953 1393 if (ret)
1da177e4 1394 goto err;
edfaf94f 1395 return 0;
1da177e4
LT
1396 }
1397err:
edfaf94f 1398 tcf_action_put_many(actions);
1da177e4
LT
1399 return ret;
1400}
1401
a56e1953 1402static int
90b73b77 1403tcf_add_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
d04e6990 1404 u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
1da177e4 1405{
1da177e4 1406 struct sk_buff *skb;
1da177e4
LT
1407 int err = 0;
1408
d04e6990
RM
1409 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1410 GFP_KERNEL);
1da177e4
LT
1411 if (!skb)
1412 return -ENOBUFS;
1413
a56e1953
WC
1414 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
1415 RTM_NEWACTION, 0, 0) <= 0) {
d143b9e3 1416 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
a56e1953
WC
1417 kfree_skb(skb);
1418 return -EINVAL;
1419 }
10297b99 1420
a56e1953
WC
1421 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1422 n->nlmsg_flags & NLM_F_ECHO);
1da177e4
LT
1423 if (err > 0)
1424 err = 0;
1425 return err;
1da177e4
LT
1426}
1427
5a7a5555 1428static int tcf_action_add(struct net *net, struct nlattr *nla,
aea0d727
AA
1429 struct nlmsghdr *n, u32 portid, int ovr,
1430 struct netlink_ext_ack *extack)
1da177e4 1431{
d04e6990 1432 size_t attr_size = 0;
39f13ea2 1433 int loop, ret;
90b73b77 1434 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
1da177e4 1435
39f13ea2
ED
1436 for (loop = 0; loop < 10; loop++) {
1437 ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0,
1438 actions, &attr_size, true, extack);
1439 if (ret != -EAGAIN)
1440 break;
1441 }
1442
90b73b77 1443 if (ret < 0)
f07fed82 1444 return ret;
90b73b77 1445 ret = tcf_add_notify(net, n, actions, portid, attr_size, extack);
cae422f3 1446 if (ovr)
90b73b77 1447 tcf_action_put_many(actions);
1da177e4 1448
cae422f3 1449 return ret;
1da177e4
LT
1450}
1451
90825b23 1452static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
47a1494b 1453 [TCA_ROOT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_FLAG_LARGE_DUMP_ON),
e62e484d 1454 [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
90825b23
JHS
1455};
1456
c21ef3e3
DA
1457static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
1458 struct netlink_ext_ack *extack)
1da177e4 1459{
3b1e0a65 1460 struct net *net = sock_net(skb->sk);
90825b23 1461 struct nlattr *tca[TCA_ROOT_MAX + 1];
15e47304 1462 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
1da177e4
LT
1463 int ret = 0, ovr = 0;
1464
0b0f43fe
JHS
1465 if ((n->nlmsg_type != RTM_GETACTION) &&
1466 !netlink_capable(skb, CAP_NET_ADMIN))
dfc47ef8
EB
1467 return -EPERM;
1468
8cb08174
JB
1469 ret = nlmsg_parse_deprecated(n, sizeof(struct tcamsg), tca,
1470 TCA_ROOT_MAX, NULL, extack);
7ba699c6
PM
1471 if (ret < 0)
1472 return ret;
1473
1474 if (tca[TCA_ACT_TAB] == NULL) {
84ae017a 1475 NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
1da177e4
LT
1476 return -EINVAL;
1477 }
1478
cc7ec456 1479 /* n->nlmsg_flags & NLM_F_CREATE */
1da177e4
LT
1480 switch (n->nlmsg_type) {
1481 case RTM_NEWACTION:
1482 /* we are going to assume all other flags
25985edc 1483 * imply create only if it doesn't exist
1da177e4
LT
1484 * Note that CREATE | EXCL implies that
1485 * but since we want avoid ambiguity (eg when flags
1486 * is zero) then just set this
1487 */
cc7ec456 1488 if (n->nlmsg_flags & NLM_F_REPLACE)
1da177e4 1489 ovr = 1;
aea0d727
AA
1490 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr,
1491 extack);
1da177e4
LT
1492 break;
1493 case RTM_DELACTION:
7316ae88 1494 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
84ae017a 1495 portid, RTM_DELACTION, extack);
1da177e4
LT
1496 break;
1497 case RTM_GETACTION:
7316ae88 1498 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
84ae017a 1499 portid, RTM_GETACTION, extack);
1da177e4
LT
1500 break;
1501 default:
1502 BUG();
1503 }
1504
1505 return ret;
1506}
1507
90825b23 1508static struct nlattr *find_dump_kind(struct nlattr **nla)
1da177e4 1509{
cc7ec456 1510 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
7ba699c6 1511 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
7ba699c6 1512 struct nlattr *kind;
1da177e4 1513
7ba699c6 1514 tb1 = nla[TCA_ACT_TAB];
1da177e4
LT
1515 if (tb1 == NULL)
1516 return NULL;
1517
8cb08174 1518 if (nla_parse_deprecated(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
1da177e4 1519 return NULL;
1da177e4 1520
6d834e04
PM
1521 if (tb[1] == NULL)
1522 return NULL;
199ce850 1523 if (nla_parse_nested_deprecated(tb2, TCA_ACT_MAX, tb[1], tcf_action_policy, NULL) < 0)
1da177e4 1524 return NULL;
7ba699c6 1525 kind = tb2[TCA_ACT_KIND];
1da177e4 1526
26dab893 1527 return kind;
1da177e4
LT
1528}
1529
5a7a5555 1530static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1da177e4 1531{
ddf97ccd 1532 struct net *net = sock_net(skb->sk);
1da177e4 1533 struct nlmsghdr *nlh;
27a884dc 1534 unsigned char *b = skb_tail_pointer(skb);
4b3550ef 1535 struct nlattr *nest;
1da177e4 1536 struct tc_action_ops *a_o;
1da177e4 1537 int ret = 0;
8b00a53c 1538 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
90825b23
JHS
1539 struct nlattr *tb[TCA_ROOT_MAX + 1];
1540 struct nlattr *count_attr = NULL;
e62e484d 1541 unsigned long jiffy_since = 0;
90825b23
JHS
1542 struct nlattr *kind = NULL;
1543 struct nla_bitfield32 bf;
e62e484d 1544 u32 msecs_since = 0;
90825b23
JHS
1545 u32 act_count = 0;
1546
8cb08174
JB
1547 ret = nlmsg_parse_deprecated(cb->nlh, sizeof(struct tcamsg), tb,
1548 TCA_ROOT_MAX, tcaa_policy, cb->extack);
90825b23
JHS
1549 if (ret < 0)
1550 return ret;
1da177e4 1551
90825b23 1552 kind = find_dump_kind(tb);
1da177e4 1553 if (kind == NULL) {
6ff9c364 1554 pr_info("tc_dump_action: action bad kind\n");
1da177e4
LT
1555 return 0;
1556 }
1557
26dab893 1558 a_o = tc_lookup_action(kind);
cc7ec456 1559 if (a_o == NULL)
1da177e4 1560 return 0;
1da177e4 1561
90825b23
JHS
1562 cb->args[2] = 0;
1563 if (tb[TCA_ROOT_FLAGS]) {
1564 bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
1565 cb->args[2] = bf.value;
1566 }
1567
e62e484d
JHS
1568 if (tb[TCA_ROOT_TIME_DELTA]) {
1569 msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
1570 }
1571
15e47304 1572 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
8b00a53c
DM
1573 cb->nlh->nlmsg_type, sizeof(*t), 0);
1574 if (!nlh)
1575 goto out_module_put;
90825b23 1576
e62e484d
JHS
1577 if (msecs_since)
1578 jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
1579
8b00a53c 1580 t = nlmsg_data(nlh);
1da177e4 1581 t->tca_family = AF_UNSPEC;
9ef1d4c7
PM
1582 t->tca__pad1 = 0;
1583 t->tca__pad2 = 0;
e62e484d 1584 cb->args[3] = jiffy_since;
90825b23
JHS
1585 count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
1586 if (!count_attr)
1587 goto out_module_put;
1da177e4 1588
ae0be8de 1589 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
4b3550ef 1590 if (nest == NULL)
8b00a53c 1591 goto out_module_put;
1da177e4 1592
41780105 1593 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL);
1da177e4 1594 if (ret < 0)
8b00a53c 1595 goto out_module_put;
1da177e4
LT
1596
1597 if (ret > 0) {
4b3550ef 1598 nla_nest_end(skb, nest);
1da177e4 1599 ret = skb->len;
90825b23
JHS
1600 act_count = cb->args[1];
1601 memcpy(nla_data(count_attr), &act_count, sizeof(u32));
1602 cb->args[1] = 0;
1da177e4 1603 } else
ebecaa66 1604 nlmsg_trim(skb, b);
1da177e4 1605
27a884dc 1606 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
15e47304 1607 if (NETLINK_CB(cb->skb).portid && ret)
1da177e4
LT
1608 nlh->nlmsg_flags |= NLM_F_MULTI;
1609 module_put(a_o->owner);
1610 return skb->len;
1611
8b00a53c 1612out_module_put:
1da177e4 1613 module_put(a_o->owner);
dc5fc579 1614 nlmsg_trim(skb, b);
1da177e4
LT
1615 return skb->len;
1616}
1617
1618static int __init tc_action_init(void)
1619{
b97bac64
FW
1620 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
1621 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
c7ac8679 1622 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
b97bac64 1623 0);
1da177e4 1624
1da177e4
LT
1625 return 0;
1626}
1627
1628subsys_initcall(tc_action_init);