1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * net/sched/act_api.c Packet action API.
4 *
5 * Author: Jamal Hadi Salim
6 */
7
8 #include <linux/types.h>
9 #include <linux/kernel.h>
10 #include <linux/string.h>
11 #include <linux/errno.h>
12 #include <linux/slab.h>
13 #include <linux/skbuff.h>
14 #include <linux/init.h>
15 #include <linux/kmod.h>
16 #include <linux/err.h>
17 #include <linux/module.h>
18 #include <net/net_namespace.h>
19 #include <net/sock.h>
20 #include <net/sch_generic.h>
21 #include <net/pkt_cls.h>
22 #include <net/tc_act/tc_pedit.h>
23 #include <net/act_api.h>
24 #include <net/netlink.h>
25 #include <net/flow_offload.h>
26 #include <net/tc_wrapper.h>
27
28 #ifdef CONFIG_INET
29 DEFINE_STATIC_KEY_FALSE(tcf_frag_xmit_count);
30 EXPORT_SYMBOL_GPL(tcf_frag_xmit_count);
31 #endif
32
tcf_dev_queue_xmit(struct sk_buff * skb,int (* xmit)(struct sk_buff * skb))33 int tcf_dev_queue_xmit(struct sk_buff *skb, int (*xmit)(struct sk_buff *skb))
34 {
35 #ifdef CONFIG_INET
36 if (static_branch_unlikely(&tcf_frag_xmit_count))
37 return sch_frag_xmit_hook(skb, xmit);
38 #endif
39
40 return xmit(skb);
41 }
42 EXPORT_SYMBOL_GPL(tcf_dev_queue_xmit);
43
tcf_action_goto_chain_exec(const struct tcf_chain * chain,struct tcf_result * res)44 static void tcf_action_goto_chain_exec(const struct tcf_chain *chain,
45 struct tcf_result *res)
46 {
47 res->goto_tp = rcu_dereference_bh(chain->filter_chain);
48 }
49
tcf_free_cookie_rcu(struct rcu_head * p)50 static void tcf_free_cookie_rcu(struct rcu_head *p)
51 {
52 struct tc_cookie *cookie = container_of(p, struct tc_cookie, rcu);
53
54 kfree(cookie->data);
55 kfree(cookie);
56 }
57
tcf_set_action_cookie(struct tc_cookie __rcu ** old_cookie,struct tc_cookie * new_cookie)58 static void tcf_set_action_cookie(struct tc_cookie __rcu **old_cookie,
59 struct tc_cookie *new_cookie)
60 {
61 struct tc_cookie *old;
62
63 old = unrcu_pointer(xchg(old_cookie, RCU_INITIALIZER(new_cookie)));
64 if (old)
65 call_rcu(&old->rcu, tcf_free_cookie_rcu);
66 }
67
tcf_action_check_ctrlact(int action,struct tcf_proto * tp,struct tcf_chain ** newchain,struct netlink_ext_ack * extack)68 int tcf_action_check_ctrlact(int action, struct tcf_proto *tp,
69 struct tcf_chain **newchain,
70 struct netlink_ext_ack *extack)
71 {
72 int opcode = TC_ACT_EXT_OPCODE(action), ret = -EINVAL;
73 u32 chain_index;
74
75 if (!opcode)
76 ret = action > TC_ACT_VALUE_MAX ? -EINVAL : 0;
77 else if (opcode <= TC_ACT_EXT_OPCODE_MAX || action == TC_ACT_UNSPEC)
78 ret = 0;
79 if (ret) {
80 NL_SET_ERR_MSG(extack, "invalid control action");
81 goto end;
82 }
83
84 if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN)) {
85 chain_index = action & TC_ACT_EXT_VAL_MASK;
86 if (!tp || !newchain) {
87 ret = -EINVAL;
88 NL_SET_ERR_MSG(extack,
89 "can't goto NULL proto/chain");
90 goto end;
91 }
92 *newchain = tcf_chain_get_by_act(tp->chain->block, chain_index);
93 if (!*newchain) {
94 ret = -ENOMEM;
95 NL_SET_ERR_MSG(extack,
96 "can't allocate goto_chain");
97 }
98 }
99 end:
100 return ret;
101 }
102 EXPORT_SYMBOL(tcf_action_check_ctrlact);
103
tcf_action_set_ctrlact(struct tc_action * a,int action,struct tcf_chain * goto_chain)104 struct tcf_chain *tcf_action_set_ctrlact(struct tc_action *a, int action,
105 struct tcf_chain *goto_chain)
106 {
107 a->tcfa_action = action;
108 goto_chain = rcu_replace_pointer(a->goto_chain, goto_chain, 1);
109 return goto_chain;
110 }
111 EXPORT_SYMBOL(tcf_action_set_ctrlact);
112
free_tcf(struct tc_action * p)113 static void free_tcf(struct tc_action *p)
114 {
115 struct tcf_chain *chain = rcu_dereference_protected(p->goto_chain, 1);
116
117 free_percpu(p->cpu_bstats);
118 free_percpu(p->cpu_bstats_hw);
119 free_percpu(p->cpu_qstats);
120
121 tcf_set_action_cookie(&p->user_cookie, NULL);
122 if (chain)
123 tcf_chain_put_by_act(chain);
124
125 kfree_rcu(p, tcfa_rcu);
126 }
127
offload_action_hw_count_set(struct tc_action * act,u32 hw_count)128 static void offload_action_hw_count_set(struct tc_action *act,
129 u32 hw_count)
130 {
131 act->in_hw_count = hw_count;
132 }
133
offload_action_hw_count_inc(struct tc_action * act,u32 hw_count)134 static void offload_action_hw_count_inc(struct tc_action *act,
135 u32 hw_count)
136 {
137 act->in_hw_count += hw_count;
138 }
139
offload_action_hw_count_dec(struct tc_action * act,u32 hw_count)140 static void offload_action_hw_count_dec(struct tc_action *act,
141 u32 hw_count)
142 {
143 act->in_hw_count = act->in_hw_count > hw_count ?
144 act->in_hw_count - hw_count : 0;
145 }
146
tcf_offload_act_num_actions_single(struct tc_action * act)147 static unsigned int tcf_offload_act_num_actions_single(struct tc_action *act)
148 {
149 unsigned int count;
150
151 if (is_tcf_pedit(act)) {
152 spin_lock_bh(&act->tcfa_lock);
153 count = tcf_pedit_nkeys_locked(act);
154 spin_unlock_bh(&act->tcfa_lock);
155 return count;
156 }
157 return 1;
158 }
159
tc_act_skip_hw(u32 flags)160 static bool tc_act_skip_hw(u32 flags)
161 {
162 return (flags & TCA_ACT_FLAGS_SKIP_HW) ? true : false;
163 }
164
tc_act_skip_sw(u32 flags)165 static bool tc_act_skip_sw(u32 flags)
166 {
167 return (flags & TCA_ACT_FLAGS_SKIP_SW) ? true : false;
168 }
169
170 /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
tc_act_flags_valid(u32 flags)171 static bool tc_act_flags_valid(u32 flags)
172 {
173 flags &= TCA_ACT_FLAGS_SKIP_HW | TCA_ACT_FLAGS_SKIP_SW;
174
175 return flags ^ (TCA_ACT_FLAGS_SKIP_HW | TCA_ACT_FLAGS_SKIP_SW);
176 }
177
offload_action_init(struct flow_offload_action * fl_action,struct tc_action * act,enum offload_act_command cmd,struct netlink_ext_ack * extack)178 static int offload_action_init(struct flow_offload_action *fl_action,
179 struct tc_action *act,
180 enum offload_act_command cmd,
181 struct netlink_ext_ack *extack)
182 {
183 int err;
184
185 fl_action->extack = extack;
186 fl_action->command = cmd;
187 fl_action->index = act->tcfa_index;
188 fl_action->cookie = (unsigned long)act;
189
190 if (act->ops->offload_act_setup) {
191 spin_lock_bh(&act->tcfa_lock);
192 err = act->ops->offload_act_setup(act, fl_action, NULL,
193 false, extack);
194 spin_unlock_bh(&act->tcfa_lock);
195 return err;
196 }
197
198 return -EOPNOTSUPP;
199 }
200
tcf_action_offload_cmd_ex(struct flow_offload_action * fl_act,u32 * hw_count)201 static int tcf_action_offload_cmd_ex(struct flow_offload_action *fl_act,
202 u32 *hw_count)
203 {
204 int err;
205
206 err = flow_indr_dev_setup_offload(NULL, NULL, TC_SETUP_ACT,
207 fl_act, NULL, NULL);
208 if (err < 0)
209 return err;
210
211 if (hw_count)
212 *hw_count = err;
213
214 return 0;
215 }
216
tcf_action_offload_cmd_cb_ex(struct flow_offload_action * fl_act,u32 * hw_count,flow_indr_block_bind_cb_t * cb,void * cb_priv)217 static int tcf_action_offload_cmd_cb_ex(struct flow_offload_action *fl_act,
218 u32 *hw_count,
219 flow_indr_block_bind_cb_t *cb,
220 void *cb_priv)
221 {
222 int err;
223
224 err = cb(NULL, NULL, cb_priv, TC_SETUP_ACT, NULL, fl_act, NULL);
225 if (err < 0)
226 return err;
227
228 if (hw_count)
229 *hw_count = 1;
230
231 return 0;
232 }
233
tcf_action_offload_cmd(struct flow_offload_action * fl_act,u32 * hw_count,flow_indr_block_bind_cb_t * cb,void * cb_priv)234 static int tcf_action_offload_cmd(struct flow_offload_action *fl_act,
235 u32 *hw_count,
236 flow_indr_block_bind_cb_t *cb,
237 void *cb_priv)
238 {
239 return cb ? tcf_action_offload_cmd_cb_ex(fl_act, hw_count,
240 cb, cb_priv) :
241 tcf_action_offload_cmd_ex(fl_act, hw_count);
242 }
243
tcf_action_offload_add_ex(struct tc_action * action,struct netlink_ext_ack * extack,flow_indr_block_bind_cb_t * cb,void * cb_priv)244 static int tcf_action_offload_add_ex(struct tc_action *action,
245 struct netlink_ext_ack *extack,
246 flow_indr_block_bind_cb_t *cb,
247 void *cb_priv)
248 {
249 bool skip_sw = tc_act_skip_sw(action->tcfa_flags);
250 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {
251 [0] = action,
252 };
253 struct flow_offload_action *fl_action;
254 u32 in_hw_count = 0;
255 int num, err = 0;
256
257 if (tc_act_skip_hw(action->tcfa_flags))
258 return 0;
259
260 num = tcf_offload_act_num_actions_single(action);
261 fl_action = offload_action_alloc(num);
262 if (!fl_action)
263 return -ENOMEM;
264
265 err = offload_action_init(fl_action, action, FLOW_ACT_REPLACE, extack);
266 if (err)
267 goto fl_err;
268
269 err = tc_setup_action(&fl_action->action, actions, 0, extack);
270 if (err) {
271 NL_SET_ERR_MSG_MOD(extack,
272 "Failed to setup tc actions for offload");
273 goto fl_err;
274 }
275
276 err = tcf_action_offload_cmd(fl_action, &in_hw_count, cb, cb_priv);
277 if (!err)
278 cb ? offload_action_hw_count_inc(action, in_hw_count) :
279 offload_action_hw_count_set(action, in_hw_count);
280
281 if (skip_sw && !tc_act_in_hw(action))
282 err = -EINVAL;
283
284 tc_cleanup_offload_action(&fl_action->action);
285
286 fl_err:
287 kfree(fl_action);
288
289 return err;
290 }
291
292 /* offload the tc action after it is inserted */
tcf_action_offload_add(struct tc_action * action,struct netlink_ext_ack * extack)293 static int tcf_action_offload_add(struct tc_action *action,
294 struct netlink_ext_ack *extack)
295 {
296 return tcf_action_offload_add_ex(action, extack, NULL, NULL);
297 }
298
tcf_action_update_hw_stats(struct tc_action * action)299 int tcf_action_update_hw_stats(struct tc_action *action)
300 {
301 struct flow_offload_action fl_act = {};
302 int err;
303
304 err = offload_action_init(&fl_act, action, FLOW_ACT_STATS, NULL);
305 if (err)
306 return err;
307
308 err = tcf_action_offload_cmd(&fl_act, NULL, NULL, NULL);
309 if (!err) {
310 preempt_disable();
311 tcf_action_stats_update(action, fl_act.stats.bytes,
312 fl_act.stats.pkts,
313 fl_act.stats.drops,
314 fl_act.stats.lastused,
315 true);
316 preempt_enable();
317 action->used_hw_stats = fl_act.stats.used_hw_stats;
318 action->used_hw_stats_valid = true;
319 } else {
320 return -EOPNOTSUPP;
321 }
322
323 return 0;
324 }
325 EXPORT_SYMBOL(tcf_action_update_hw_stats);
326
tcf_action_offload_del_ex(struct tc_action * action,flow_indr_block_bind_cb_t * cb,void * cb_priv)327 static int tcf_action_offload_del_ex(struct tc_action *action,
328 flow_indr_block_bind_cb_t *cb,
329 void *cb_priv)
330 {
331 struct flow_offload_action fl_act = {};
332 u32 in_hw_count = 0;
333 int err = 0;
334
335 if (!tc_act_in_hw(action))
336 return 0;
337
338 err = offload_action_init(&fl_act, action, FLOW_ACT_DESTROY, NULL);
339 if (err)
340 return err;
341
342 err = tcf_action_offload_cmd(&fl_act, &in_hw_count, cb, cb_priv);
343 if (err < 0)
344 return err;
345
346 if (!cb && action->in_hw_count != in_hw_count)
347 return -EINVAL;
348
349 /* do not need to update hw state when deleting action */
350 if (cb && in_hw_count)
351 offload_action_hw_count_dec(action, in_hw_count);
352
353 return 0;
354 }
355
tcf_action_offload_del(struct tc_action * action)356 static int tcf_action_offload_del(struct tc_action *action)
357 {
358 return tcf_action_offload_del_ex(action, NULL, NULL);
359 }
360
tcf_action_cleanup(struct tc_action * p)361 static void tcf_action_cleanup(struct tc_action *p)
362 {
363 tcf_action_offload_del(p);
364 if (p->ops->cleanup)
365 p->ops->cleanup(p);
366
367 gen_kill_estimator(&p->tcfa_rate_est);
368 free_tcf(p);
369 }
370
__tcf_action_put(struct tc_action * p,bool bind)371 static int __tcf_action_put(struct tc_action *p, bool bind)
372 {
373 struct tcf_idrinfo *idrinfo = p->idrinfo;
374
375 if (refcount_dec_and_mutex_lock(&p->tcfa_refcnt, &idrinfo->lock)) {
376 if (bind)
377 atomic_dec(&p->tcfa_bindcnt);
378 idr_remove(&idrinfo->action_idr, p->tcfa_index);
379 mutex_unlock(&idrinfo->lock);
380
381 tcf_action_cleanup(p);
382 return 1;
383 }
384
385 if (bind)
386 atomic_dec(&p->tcfa_bindcnt);
387
388 return 0;
389 }
390
__tcf_idr_release(struct tc_action * p,bool bind,bool strict)391 static int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
392 {
393 int ret = 0;
394
395 /* Release with strict==1 and bind==0 is only called through act API
396 * interface (classifiers always bind). Only case when action with
397 * positive reference count and zero bind count can exist is when it was
398 * also created with act API (unbinding last classifier will destroy the
399 * action if it was created by classifier). So only case when bind count
400 * can be changed after initial check is when unbound action is
401 * destroyed by act API while classifier binds to action with same id
402 * concurrently. This result either creation of new action(same behavior
403 * as before), or reusing existing action if concurrent process
404 * increments reference count before action is deleted. Both scenarios
405 * are acceptable.
406 */
407 if (p) {
408 if (!bind && strict && atomic_read(&p->tcfa_bindcnt) > 0)
409 return -EPERM;
410
411 if (__tcf_action_put(p, bind))
412 ret = ACT_P_DELETED;
413 }
414
415 return ret;
416 }
417
tcf_idr_release(struct tc_action * a,bool bind)418 int tcf_idr_release(struct tc_action *a, bool bind)
419 {
420 const struct tc_action_ops *ops = a->ops;
421 int ret;
422
423 ret = __tcf_idr_release(a, bind, false);
424 if (ret == ACT_P_DELETED)
425 module_put(ops->owner);
426 return ret;
427 }
428 EXPORT_SYMBOL(tcf_idr_release);
429
tcf_action_shared_attrs_size(const struct tc_action * act)430 static size_t tcf_action_shared_attrs_size(const struct tc_action *act)
431 {
432 struct tc_cookie *user_cookie;
433 u32 cookie_len = 0;
434
435 rcu_read_lock();
436 user_cookie = rcu_dereference(act->user_cookie);
437
438 if (user_cookie)
439 cookie_len = nla_total_size(user_cookie->len);
440 rcu_read_unlock();
441
442 return nla_total_size(0) /* action number nested */
443 + nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */
444 + cookie_len /* TCA_ACT_COOKIE */
445 + nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_HW_STATS */
446 /* TCA_ACT_USED_HW_STATS */
447 + nla_total_size(sizeof(struct nla_bitfield32))
448 + nla_total_size(sizeof(u32)) /* TCA_ACT_IN_HW_COUNT */
449 + nla_total_size(0) /* TCA_ACT_STATS nested */
450 + nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_FLAGS */
451 /* TCA_STATS_BASIC */
452 + nla_total_size_64bit(sizeof(struct gnet_stats_basic))
453 /* TCA_STATS_BASIC_HW */
454 + nla_total_size_64bit(sizeof(struct gnet_stats_basic))
455 /* TCA_STATS_PKT64, emitted by both of the basic copies above */
456 + 2 * nla_total_size_64bit(sizeof(u64))
457 /* TCA_STATS_RATE_EST */
458 + nla_total_size_64bit(sizeof(struct gnet_stats_rate_est))
459 /* TCA_STATS_RATE_EST64 */
460 + nla_total_size_64bit(sizeof(struct gnet_stats_rate_est64))
461 /* TCA_STATS_QUEUE */
462 + nla_total_size_64bit(sizeof(struct gnet_stats_queue))
463 + nla_total_size(0) /* TCA_ACT_OPTIONS nested */
464 /* TCA_GACT_TM; actions dump their tcf_t with nla_put_64bit(),
465 * which may emit an extra NLA_PAD attribute.
466 */
467 + nla_total_size_64bit(sizeof(struct tcf_t));
468 }
469
tcf_action_full_attrs_size(size_t sz)470 static size_t tcf_action_full_attrs_size(size_t sz)
471 {
472 return NLMSG_HDRLEN /* struct nlmsghdr */
473 + sizeof(struct tcamsg)
474 + nla_total_size(0) /* TCA_ACT_TAB nested */
475 + sz;
476 }
477
tcf_action_fill_size(const struct tc_action * act)478 static size_t tcf_action_fill_size(const struct tc_action *act)
479 {
480 size_t sz = tcf_action_shared_attrs_size(act);
481
482 if (act->ops->get_fill_size)
483 return act->ops->get_fill_size(act) + sz;
484 return sz;
485 }
486
487 static int
tcf_action_dump_terse(struct sk_buff * skb,struct tc_action * a,bool from_act)488 tcf_action_dump_terse(struct sk_buff *skb, struct tc_action *a, bool from_act)
489 {
490 unsigned char *b = skb_tail_pointer(skb);
491 struct tc_cookie *cookie;
492
493 if (nla_put_string(skb, TCA_ACT_KIND, a->ops->kind))
494 goto nla_put_failure;
495 if (tcf_action_copy_stats(skb, a, 0))
496 goto nla_put_failure;
497 if (from_act && nla_put_u32(skb, TCA_ACT_INDEX, a->tcfa_index))
498 goto nla_put_failure;
499
500 rcu_read_lock();
501 cookie = rcu_dereference(a->user_cookie);
502 if (cookie) {
503 if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) {
504 rcu_read_unlock();
505 goto nla_put_failure;
506 }
507 }
508 rcu_read_unlock();
509
510 return 0;
511
512 nla_put_failure:
513 nlmsg_trim(skb, b);
514 return -1;
515 }
516
517 static int
tcf_action_dump_1(struct sk_buff * skb,struct tc_action * a,int bind,int ref)518 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
519 {
520 unsigned char *b = skb_tail_pointer(skb);
521 struct nlattr *nest;
522 int err = -EINVAL;
523 u32 flags;
524
525 if (tcf_action_dump_terse(skb, a, false))
526 goto nla_put_failure;
527
528 if (a->hw_stats != TCA_ACT_HW_STATS_ANY &&
529 nla_put_bitfield32(skb, TCA_ACT_HW_STATS,
530 a->hw_stats, TCA_ACT_HW_STATS_ANY))
531 goto nla_put_failure;
532
533 if (a->used_hw_stats_valid &&
534 nla_put_bitfield32(skb, TCA_ACT_USED_HW_STATS,
535 a->used_hw_stats, TCA_ACT_HW_STATS_ANY))
536 goto nla_put_failure;
537
538 flags = a->tcfa_flags & TCA_ACT_FLAGS_USER_MASK;
539 if (flags &&
540 nla_put_bitfield32(skb, TCA_ACT_FLAGS,
541 flags, flags))
542 goto nla_put_failure;
543
544 if (nla_put_u32(skb, TCA_ACT_IN_HW_COUNT, a->in_hw_count))
545 goto nla_put_failure;
546
547 nest = nla_nest_start_noflag(skb, TCA_ACT_OPTIONS);
548 if (nest == NULL)
549 goto nla_put_failure;
550 err = tcf_action_dump_old(skb, a, bind, ref);
551 if (err > 0) {
552 nla_nest_end(skb, nest);
553 return err;
554 }
555
556 nla_put_failure:
557 nlmsg_trim(skb, b);
558 return -1;
559 }
560
tcf_dump_walker(struct tcf_idrinfo * idrinfo,struct sk_buff * skb,struct netlink_callback * cb)561 static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
562 struct netlink_callback *cb)
563 {
564 int err = 0, index = -1, s_i = 0, n_i = 0;
565 u32 act_flags = cb->args[2];
566 unsigned long jiffy_since = cb->args[3];
567 struct nlattr *nest;
568 struct idr *idr = &idrinfo->action_idr;
569 struct tc_action *p;
570 unsigned long id = 1;
571 unsigned long tmp;
572
573 mutex_lock(&idrinfo->lock);
574
575 s_i = cb->args[0];
576
577 idr_for_each_entry_ul(idr, p, tmp, id) {
578 index++;
579 if (index < s_i)
580 continue;
581 if (IS_ERR(p))
582 continue;
583
584 if (jiffy_since &&
585 time_after(jiffy_since,
586 (unsigned long)p->tcfa_tm.lastuse))
587 continue;
588
589 tcf_action_update_hw_stats(p);
590
591 nest = nla_nest_start_noflag(skb, n_i);
592 if (!nest) {
593 index--;
594 goto nla_put_failure;
595 }
596 err = (act_flags & TCA_ACT_FLAG_TERSE_DUMP) ?
597 tcf_action_dump_terse(skb, p, true) :
598 tcf_action_dump_1(skb, p, 0, 0);
599 if (err < 0) {
600 index--;
601 nlmsg_trim(skb, nest);
602 goto done;
603 }
604 nla_nest_end(skb, nest);
605 n_i++;
606 if (!(act_flags & TCA_ACT_FLAG_LARGE_DUMP_ON) &&
607 n_i >= TCA_ACT_MAX_PRIO)
608 goto done;
609 }
610 done:
611 if (index >= 0)
612 cb->args[0] = index + 1;
613
614 mutex_unlock(&idrinfo->lock);
615 if (n_i) {
616 if (act_flags & TCA_ACT_FLAG_LARGE_DUMP_ON)
617 cb->args[1] = n_i;
618 }
619 return n_i;
620
621 nla_put_failure:
622 nla_nest_cancel(skb, nest);
623 goto done;
624 }
625
tcf_idr_release_unsafe(struct tc_action * p)626 static int tcf_idr_release_unsafe(struct tc_action *p)
627 {
628 if (atomic_read(&p->tcfa_bindcnt) > 0)
629 return -EPERM;
630
631 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
632 idr_remove(&p->idrinfo->action_idr, p->tcfa_index);
633 tcf_action_cleanup(p);
634 return ACT_P_DELETED;
635 }
636
637 return 0;
638 }
639
tcf_del_walker(struct tcf_idrinfo * idrinfo,struct sk_buff * skb,const struct tc_action_ops * ops,struct netlink_ext_ack * extack)640 static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
641 const struct tc_action_ops *ops,
642 struct netlink_ext_ack *extack)
643 {
644 struct nlattr *nest;
645 int n_i = 0;
646 int ret = -EINVAL;
647 struct idr *idr = &idrinfo->action_idr;
648 struct tc_action *p;
649 unsigned long id = 1;
650 unsigned long tmp;
651
652 nest = nla_nest_start_noflag(skb, 0);
653 if (nest == NULL)
654 goto nla_put_failure;
655 if (nla_put_string(skb, TCA_ACT_KIND, ops->kind))
656 goto nla_put_failure;
657
658 ret = 0;
659 mutex_lock(&idrinfo->lock);
660 idr_for_each_entry_ul(idr, p, tmp, id) {
661 if (IS_ERR(p))
662 continue;
663 ret = tcf_idr_release_unsafe(p);
664 if (ret == ACT_P_DELETED)
665 module_put(ops->owner);
666 else if (ret < 0)
667 break;
668 n_i++;
669 }
670 mutex_unlock(&idrinfo->lock);
671 if (ret < 0) {
672 if (n_i)
673 NL_SET_ERR_MSG(extack, "Unable to flush all TC actions");
674 else
675 goto nla_put_failure;
676 }
677
678 ret = nla_put_u32(skb, TCA_FCNT, n_i);
679 if (ret)
680 goto nla_put_failure;
681 nla_nest_end(skb, nest);
682
683 return n_i;
684 nla_put_failure:
685 nla_nest_cancel(skb, nest);
686 return ret;
687 }
688
tcf_generic_walker(struct tc_action_net * tn,struct sk_buff * skb,struct netlink_callback * cb,int type,const struct tc_action_ops * ops,struct netlink_ext_ack * extack)689 int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
690 struct netlink_callback *cb, int type,
691 const struct tc_action_ops *ops,
692 struct netlink_ext_ack *extack)
693 {
694 struct tcf_idrinfo *idrinfo = tn->idrinfo;
695
696 if (type == RTM_DELACTION) {
697 return tcf_del_walker(idrinfo, skb, ops, extack);
698 } else if (type == RTM_GETACTION) {
699 return tcf_dump_walker(idrinfo, skb, cb);
700 } else {
701 WARN(1, "tcf_generic_walker: unknown command %d\n", type);
702 NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command");
703 return -EINVAL;
704 }
705 }
706 EXPORT_SYMBOL(tcf_generic_walker);
707
tcf_idr_search(struct tc_action_net * tn,struct tc_action ** a,u32 index)708 int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
709 {
710 struct tcf_idrinfo *idrinfo = tn->idrinfo;
711 struct tc_action *p;
712
713 mutex_lock(&idrinfo->lock);
714 p = idr_find(&idrinfo->action_idr, index);
715 if (IS_ERR(p))
716 p = NULL;
717 else if (p)
718 refcount_inc(&p->tcfa_refcnt);
719 mutex_unlock(&idrinfo->lock);
720
721 if (p) {
722 *a = p;
723 return true;
724 }
725 return false;
726 }
727 EXPORT_SYMBOL(tcf_idr_search);
728
__tcf_generic_walker(struct net * net,struct sk_buff * skb,struct netlink_callback * cb,int type,const struct tc_action_ops * ops,struct netlink_ext_ack * extack)729 static int __tcf_generic_walker(struct net *net, struct sk_buff *skb,
730 struct netlink_callback *cb, int type,
731 const struct tc_action_ops *ops,
732 struct netlink_ext_ack *extack)
733 {
734 struct tc_action_net *tn = net_generic(net, ops->net_id);
735
736 if (unlikely(ops->walk))
737 return ops->walk(net, skb, cb, type, ops, extack);
738
739 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
740 }
741
__tcf_idr_search(struct net * net,const struct tc_action_ops * ops,struct tc_action ** a,u32 index)742 static int __tcf_idr_search(struct net *net,
743 const struct tc_action_ops *ops,
744 struct tc_action **a, u32 index)
745 {
746 struct tc_action_net *tn = net_generic(net, ops->net_id);
747
748 if (unlikely(ops->lookup))
749 return ops->lookup(net, a, index);
750
751 return tcf_idr_search(tn, a, index);
752 }
753
tcf_idr_delete_index(struct tcf_idrinfo * idrinfo,u32 index)754 static int tcf_idr_delete_index(struct tcf_idrinfo *idrinfo, u32 index)
755 {
756 struct tc_action *p;
757 int ret = 0;
758
759 mutex_lock(&idrinfo->lock);
760 p = idr_find(&idrinfo->action_idr, index);
761 if (!p) {
762 mutex_unlock(&idrinfo->lock);
763 return -ENOENT;
764 }
765
766 if (!atomic_read(&p->tcfa_bindcnt)) {
767 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
768 struct module *owner = p->ops->owner;
769
770 WARN_ON(p != idr_remove(&idrinfo->action_idr,
771 p->tcfa_index));
772 mutex_unlock(&idrinfo->lock);
773
774 tcf_action_cleanup(p);
775 module_put(owner);
776 return 0;
777 }
778 ret = 0;
779 } else {
780 ret = -EPERM;
781 }
782
783 mutex_unlock(&idrinfo->lock);
784 return ret;
785 }
786
tcf_idr_create(struct tc_action_net * tn,u32 index,struct nlattr * est,struct tc_action ** a,const struct tc_action_ops * ops,int bind,bool cpustats,u32 flags)787 int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
788 struct tc_action **a, const struct tc_action_ops *ops,
789 int bind, bool cpustats, u32 flags)
790 {
791 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
792 struct tcf_idrinfo *idrinfo = tn->idrinfo;
793 int err = -ENOMEM;
794
795 if (unlikely(!p))
796 return -ENOMEM;
797 refcount_set(&p->tcfa_refcnt, 1);
798 if (bind)
799 atomic_set(&p->tcfa_bindcnt, 1);
800
801 if (cpustats) {
802 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync);
803 if (!p->cpu_bstats)
804 goto err1;
805 p->cpu_bstats_hw = netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync);
806 if (!p->cpu_bstats_hw)
807 goto err2;
808 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
809 if (!p->cpu_qstats)
810 goto err3;
811 }
812 gnet_stats_basic_sync_init(&p->tcfa_bstats);
813 gnet_stats_basic_sync_init(&p->tcfa_bstats_hw);
814 spin_lock_init(&p->tcfa_lock);
815 p->tcfa_index = index;
816 p->tcfa_tm.install = jiffies;
817 p->tcfa_tm.lastuse = jiffies;
818 p->tcfa_tm.firstuse = 0;
819 p->tcfa_flags = flags;
820 if (est) {
821 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
822 &p->tcfa_rate_est,
823 &p->tcfa_lock, false, est);
824 if (err)
825 goto err4;
826 }
827
828 p->idrinfo = idrinfo;
829 __module_get(ops->owner);
830 p->ops = ops;
831 *a = p;
832 return 0;
833 err4:
834 free_percpu(p->cpu_qstats);
835 err3:
836 free_percpu(p->cpu_bstats_hw);
837 err2:
838 free_percpu(p->cpu_bstats);
839 err1:
840 kfree(p);
841 return err;
842 }
843 EXPORT_SYMBOL(tcf_idr_create);
844
tcf_idr_create_from_flags(struct tc_action_net * tn,u32 index,struct nlattr * est,struct tc_action ** a,const struct tc_action_ops * ops,int bind,u32 flags)845 int tcf_idr_create_from_flags(struct tc_action_net *tn, u32 index,
846 struct nlattr *est, struct tc_action **a,
847 const struct tc_action_ops *ops, int bind,
848 u32 flags)
849 {
850 /* Set cpustats according to actions flags. */
851 return tcf_idr_create(tn, index, est, a, ops, bind,
852 !(flags & TCA_ACT_FLAGS_NO_PERCPU_STATS), flags);
853 }
854 EXPORT_SYMBOL(tcf_idr_create_from_flags);
855
856 /* Cleanup idr index that was allocated but not initialized. */
857
tcf_idr_cleanup(struct tc_action_net * tn,u32 index)858 void tcf_idr_cleanup(struct tc_action_net *tn, u32 index)
859 {
860 struct tcf_idrinfo *idrinfo = tn->idrinfo;
861
862 mutex_lock(&idrinfo->lock);
863 /* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
864 WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index)));
865 mutex_unlock(&idrinfo->lock);
866 }
867 EXPORT_SYMBOL(tcf_idr_cleanup);
868
869 /* Check if action with specified index exists. If actions is found, increments
870 * its reference and bind counters, and return 1. Otherwise insert temporary
871 * error pointer (to prevent concurrent users from inserting actions with same
872 * index) and return 0.
873 *
874 * May return -EAGAIN for binding actions in case of a parallel add/delete on
875 * the requested index.
876 */
877
tcf_idr_check_alloc(struct tc_action_net * tn,u32 * index,struct tc_action ** a,int bind)878 int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
879 struct tc_action **a, int bind)
880 {
881 struct tcf_idrinfo *idrinfo = tn->idrinfo;
882 struct tc_action *p;
883 int ret;
884 u32 max;
885
886 if (*index) {
887 rcu_read_lock();
888 p = idr_find(&idrinfo->action_idr, *index);
889
890 if (IS_ERR(p)) {
891 /* This means that another process allocated
892 * index but did not assign the pointer yet.
893 */
894 rcu_read_unlock();
895 return -EAGAIN;
896 }
897
898 if (!p) {
899 /* Empty slot, try to allocate it */
900 max = *index;
901 rcu_read_unlock();
902 goto new;
903 }
904
905 if (!refcount_inc_not_zero(&p->tcfa_refcnt)) {
906 /* Action was deleted in parallel */
907 rcu_read_unlock();
908 return -EAGAIN;
909 }
910
911 if (bind)
912 atomic_inc(&p->tcfa_bindcnt);
913 *a = p;
914
915 rcu_read_unlock();
916
917 return 1;
918 } else {
919 /* Find a slot */
920 *index = 1;
921 max = UINT_MAX;
922 }
923
924 new:
925 *a = NULL;
926
927 mutex_lock(&idrinfo->lock);
928 ret = idr_alloc_u32(&idrinfo->action_idr, ERR_PTR(-EBUSY), index, max,
929 GFP_KERNEL);
930 mutex_unlock(&idrinfo->lock);
931
932 /* N binds raced for action allocation,
933 * retry for all the ones that failed.
934 */
935 if (ret == -ENOSPC && *index == max)
936 ret = -EAGAIN;
937
938 return ret;
939 }
940 EXPORT_SYMBOL(tcf_idr_check_alloc);
941
tcf_idrinfo_destroy(const struct tc_action_ops * ops,struct tcf_idrinfo * idrinfo)942 void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
943 struct tcf_idrinfo *idrinfo)
944 {
945 struct idr *idr = &idrinfo->action_idr;
946 bool mutex_taken = false;
947 struct tc_action *p;
948 unsigned long id = 1;
949 unsigned long tmp;
950 int ret;
951
952 idr_for_each_entry_ul(idr, p, tmp, id) {
953 if (IS_ERR(p))
954 continue;
955 if (tc_act_in_hw(p) && !mutex_taken) {
956 rtnl_lock();
957 mutex_taken = true;
958 }
959 ret = __tcf_idr_release(p, false, true);
960 if (ret == ACT_P_DELETED)
961 module_put(ops->owner);
962 else if (ret < 0)
963 return;
964 }
965 if (mutex_taken)
966 rtnl_unlock();
967 idr_destroy(&idrinfo->action_idr);
968 }
969 EXPORT_SYMBOL(tcf_idrinfo_destroy);
970
971 static LIST_HEAD(act_base);
972 static DEFINE_RWLOCK(act_mod_lock);
973 /* since act ops id is stored in pernet subsystem list,
974 * then there is no way to walk through only all the action
975 * subsystem, so we keep tc action pernet ops id for
976 * reoffload to walk through.
977 */
978 static LIST_HEAD(act_pernet_id_list);
979 static DEFINE_MUTEX(act_id_mutex);
980 struct tc_act_pernet_id {
981 struct list_head list;
982 unsigned int id;
983 };
984
tcf_pernet_add_id_list(unsigned int id)985 static int tcf_pernet_add_id_list(unsigned int id)
986 {
987 struct tc_act_pernet_id *id_ptr;
988 int ret = 0;
989
990 mutex_lock(&act_id_mutex);
991 list_for_each_entry(id_ptr, &act_pernet_id_list, list) {
992 if (id_ptr->id == id) {
993 ret = -EEXIST;
994 goto err_out;
995 }
996 }
997
998 id_ptr = kzalloc_obj(*id_ptr);
999 if (!id_ptr) {
1000 ret = -ENOMEM;
1001 goto err_out;
1002 }
1003 id_ptr->id = id;
1004
1005 list_add_tail(&id_ptr->list, &act_pernet_id_list);
1006
1007 err_out:
1008 mutex_unlock(&act_id_mutex);
1009 return ret;
1010 }
1011
tcf_pernet_del_id_list(unsigned int id)1012 static void tcf_pernet_del_id_list(unsigned int id)
1013 {
1014 struct tc_act_pernet_id *id_ptr;
1015
1016 mutex_lock(&act_id_mutex);
1017 list_for_each_entry(id_ptr, &act_pernet_id_list, list) {
1018 if (id_ptr->id == id) {
1019 list_del(&id_ptr->list);
1020 kfree(id_ptr);
1021 break;
1022 }
1023 }
1024 mutex_unlock(&act_id_mutex);
1025 }
1026
tcf_register_action(struct tc_action_ops * act,struct pernet_operations * ops)1027 int tcf_register_action(struct tc_action_ops *act,
1028 struct pernet_operations *ops)
1029 {
1030 struct tc_action_ops *a;
1031 int ret;
1032
1033 if (!act->act || !act->dump || !act->init)
1034 return -EINVAL;
1035
1036 /* We have to register pernet ops before making the action ops visible,
1037 * otherwise tcf_action_init_1() could get a partially initialized
1038 * netns.
1039 */
1040 ret = register_pernet_subsys(ops);
1041 if (ret)
1042 return ret;
1043
1044 if (ops->id) {
1045 ret = tcf_pernet_add_id_list(*ops->id);
1046 if (ret)
1047 goto err_id;
1048 }
1049
1050 write_lock(&act_mod_lock);
1051 list_for_each_entry(a, &act_base, head) {
1052 if (act->id == a->id || (strcmp(act->kind, a->kind) == 0)) {
1053 ret = -EEXIST;
1054 goto err_out;
1055 }
1056 }
1057 list_add_tail(&act->head, &act_base);
1058 write_unlock(&act_mod_lock);
1059
1060 return 0;
1061
1062 err_out:
1063 write_unlock(&act_mod_lock);
1064 if (ops->id)
1065 tcf_pernet_del_id_list(*ops->id);
1066 err_id:
1067 unregister_pernet_subsys(ops);
1068 return ret;
1069 }
1070 EXPORT_SYMBOL(tcf_register_action);
1071
tcf_unregister_action(struct tc_action_ops * act,struct pernet_operations * ops)1072 int tcf_unregister_action(struct tc_action_ops *act,
1073 struct pernet_operations *ops)
1074 {
1075 struct tc_action_ops *a;
1076 int err = -ENOENT;
1077
1078 write_lock(&act_mod_lock);
1079 list_for_each_entry(a, &act_base, head) {
1080 if (a == act) {
1081 list_del(&act->head);
1082 err = 0;
1083 break;
1084 }
1085 }
1086 write_unlock(&act_mod_lock);
1087 if (!err) {
1088 unregister_pernet_subsys(ops);
1089 if (ops->id)
1090 tcf_pernet_del_id_list(*ops->id);
1091 }
1092 return err;
1093 }
1094 EXPORT_SYMBOL(tcf_unregister_action);
1095
1096 /* lookup by name */
tc_lookup_action_n(char * kind)1097 static struct tc_action_ops *tc_lookup_action_n(char *kind)
1098 {
1099 struct tc_action_ops *a, *res = NULL;
1100
1101 if (kind) {
1102 read_lock(&act_mod_lock);
1103 list_for_each_entry(a, &act_base, head) {
1104 if (strcmp(kind, a->kind) == 0) {
1105 if (try_module_get(a->owner))
1106 res = a;
1107 break;
1108 }
1109 }
1110 read_unlock(&act_mod_lock);
1111 }
1112 return res;
1113 }
1114
1115 /* lookup by nlattr */
tc_lookup_action(struct nlattr * kind)1116 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
1117 {
1118 struct tc_action_ops *a, *res = NULL;
1119
1120 if (kind) {
1121 read_lock(&act_mod_lock);
1122 list_for_each_entry(a, &act_base, head) {
1123 if (nla_strcmp(kind, a->kind) == 0) {
1124 if (try_module_get(a->owner))
1125 res = a;
1126 break;
1127 }
1128 }
1129 read_unlock(&act_mod_lock);
1130 }
1131 return res;
1132 }
1133
1134 /*TCA_ACT_MAX_PRIO is 32, there count up to 32 */
1135 #define TCA_ACT_MAX_PRIO_MASK 0x1FF
tcf_action_exec(struct sk_buff * skb,struct tc_action ** actions,int nr_actions,struct tcf_result * res)1136 int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
1137 int nr_actions, struct tcf_result *res)
1138 {
1139 u32 jmp_prgcnt = 0;
1140 u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
1141 int i;
1142 int ret = TC_ACT_OK;
1143
1144 if (skb_skip_tc_classify(skb))
1145 return TC_ACT_OK;
1146
1147 restart_act_graph:
1148 for (i = 0; i < nr_actions; i++) {
1149 const struct tc_action *a = actions[i];
1150 int repeat_ttl;
1151
1152 if (jmp_prgcnt > 0) {
1153 jmp_prgcnt -= 1;
1154 continue;
1155 }
1156
1157 if (tc_act_skip_sw(a->tcfa_flags))
1158 continue;
1159
1160 repeat_ttl = 32;
1161 repeat:
1162 ret = tc_act(skb, a, res);
1163 if (unlikely(ret == TC_ACT_REPEAT)) {
1164 if (--repeat_ttl != 0)
1165 goto repeat;
1166 /* suspicious opcode, stop pipeline */
1167 net_warn_ratelimited("TC_ACT_REPEAT abuse ?\n");
1168 return TC_ACT_OK;
1169 }
1170 if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
1171 jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
1172 if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
1173 /* faulty opcode, stop pipeline */
1174 return TC_ACT_OK;
1175 } else {
1176 jmp_ttl -= 1;
1177 if (jmp_ttl > 0)
1178 goto restart_act_graph;
1179 else /* faulty graph, stop pipeline */
1180 return TC_ACT_OK;
1181 }
1182 } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
1183 struct tcf_chain *chain = rcu_dereference_bh(a->goto_chain);
1184
1185 if (unlikely(!chain)) {
1186 tcf_set_drop_reason(skb,
1187 SKB_DROP_REASON_TC_CHAIN_NOTFOUND);
1188 return TC_ACT_SHOT;
1189 }
1190 tcf_action_goto_chain_exec(chain, res);
1191 }
1192
1193 if (ret != TC_ACT_PIPE)
1194 break;
1195 }
1196
1197 return ret;
1198 }
1199 EXPORT_SYMBOL(tcf_action_exec);
1200
tcf_action_destroy(struct tc_action * actions[],int bind)1201 int tcf_action_destroy(struct tc_action *actions[], int bind)
1202 {
1203 struct tc_action *a;
1204 int ret = 0, i;
1205
1206 tcf_act_for_each_action(i, a, actions) {
1207 actions[i] = NULL;
1208 /* Drop our reference even if the action is still bound to a filter. */
1209 ret = tcf_idr_release(a, bind);
1210 }
1211 return ret;
1212 }
1213
tcf_action_put(struct tc_action * p)1214 static int tcf_action_put(struct tc_action *p)
1215 {
1216 return __tcf_action_put(p, false);
1217 }
1218
tcf_action_put_many(struct tc_action * actions[])1219 static void tcf_action_put_many(struct tc_action *actions[])
1220 {
1221 int i;
1222
1223 /* Deletion may have cleared entries before failing. */
1224 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
1225 struct tc_action *a = actions[i];
1226 const struct tc_action_ops *ops;
1227
1228 if (!a)
1229 continue;
1230 ops = a->ops;
1231 if (tcf_action_put(a))
1232 module_put(ops->owner);
1233 }
1234 }
1235
tca_put_bound_many(struct tc_action * actions[],int init_res[])1236 static void tca_put_bound_many(struct tc_action *actions[], int init_res[])
1237 {
1238 struct tc_action *a;
1239 int i;
1240
1241 tcf_act_for_each_action(i, a, actions) {
1242 const struct tc_action_ops *ops = a->ops;
1243
1244 if (init_res[i] == ACT_P_CREATED)
1245 continue;
1246
1247 if (tcf_action_put(a))
1248 module_put(ops->owner);
1249 }
1250 }
1251
1252 int
tcf_action_dump_old(struct sk_buff * skb,struct tc_action * a,int bind,int ref)1253 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
1254 {
1255 return a->ops->dump(skb, a, bind, ref);
1256 }
1257
tcf_action_dump(struct sk_buff * skb,struct tc_action * actions[],int bind,int ref,bool terse)1258 int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[],
1259 int bind, int ref, bool terse)
1260 {
1261 struct tc_action *a;
1262 int err = -EINVAL, i;
1263 struct nlattr *nest;
1264
1265 tcf_act_for_each_action(i, a, actions) {
1266 nest = nla_nest_start_noflag(skb, i + 1);
1267 if (nest == NULL)
1268 goto nla_put_failure;
1269 err = terse ? tcf_action_dump_terse(skb, a, false) :
1270 tcf_action_dump_1(skb, a, bind, ref);
1271 if (err < 0)
1272 goto errout;
1273 nla_nest_end(skb, nest);
1274 }
1275
1276 return 0;
1277
1278 nla_put_failure:
1279 err = -EINVAL;
1280 errout:
1281 nla_nest_cancel(skb, nest);
1282 return err;
1283 }
1284
nla_memdup_cookie(struct nlattr ** tb)1285 static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
1286 {
1287 struct tc_cookie *c = kzalloc_obj(*c);
1288 if (!c)
1289 return NULL;
1290
1291 c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
1292 if (!c->data) {
1293 kfree(c);
1294 return NULL;
1295 }
1296 c->len = nla_len(tb[TCA_ACT_COOKIE]);
1297
1298 return c;
1299 }
1300
tcf_action_hw_stats_get(struct nlattr * hw_stats_attr)1301 static u8 tcf_action_hw_stats_get(struct nlattr *hw_stats_attr)
1302 {
1303 struct nla_bitfield32 hw_stats_bf;
1304
1305 /* If the user did not pass the attr, that means he does
1306 * not care about the type. Return "any" in that case
1307 * which is setting on all supported types.
1308 */
1309 if (!hw_stats_attr)
1310 return TCA_ACT_HW_STATS_ANY;
1311 hw_stats_bf = nla_get_bitfield32(hw_stats_attr);
1312 return hw_stats_bf.value;
1313 }
1314
1315 static const struct nla_policy tcf_action_policy[TCA_ACT_MAX + 1] = {
1316 [TCA_ACT_KIND] = { .type = NLA_STRING },
1317 [TCA_ACT_INDEX] = { .type = NLA_U32 },
1318 [TCA_ACT_COOKIE] = { .type = NLA_BINARY,
1319 .len = TC_COOKIE_MAX_SIZE },
1320 [TCA_ACT_OPTIONS] = { .type = NLA_NESTED },
1321 [TCA_ACT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_ACT_FLAGS_NO_PERCPU_STATS |
1322 TCA_ACT_FLAGS_SKIP_HW |
1323 TCA_ACT_FLAGS_SKIP_SW),
1324 [TCA_ACT_HW_STATS] = NLA_POLICY_BITFIELD32(TCA_ACT_HW_STATS_ANY),
1325 };
1326
tcf_idr_insert_many(struct tc_action * actions[],int init_res[])1327 void tcf_idr_insert_many(struct tc_action *actions[], int init_res[])
1328 {
1329 struct tc_action *a;
1330 int i;
1331
1332 tcf_act_for_each_action(i, a, actions) {
1333 struct tcf_idrinfo *idrinfo;
1334
1335 if (init_res[i] == ACT_P_BOUND)
1336 continue;
1337
1338 idrinfo = a->idrinfo;
1339 mutex_lock(&idrinfo->lock);
1340 /* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
1341 idr_replace(&idrinfo->action_idr, a, a->tcfa_index);
1342 mutex_unlock(&idrinfo->lock);
1343 }
1344 }
1345
tc_action_load_ops(struct nlattr * nla,u32 flags,struct netlink_ext_ack * extack)1346 struct tc_action_ops *tc_action_load_ops(struct nlattr *nla, u32 flags,
1347 struct netlink_ext_ack *extack)
1348 {
1349 bool police = flags & TCA_ACT_FLAGS_POLICE;
1350 struct nlattr *tb[TCA_ACT_MAX + 1];
1351 struct tc_action_ops *a_o;
1352 char act_name[IFNAMSIZ];
1353 struct nlattr *kind;
1354 int err;
1355
1356 if (!police) {
1357 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1358 tcf_action_policy, extack);
1359 if (err < 0)
1360 return ERR_PTR(err);
1361 err = -EINVAL;
1362 kind = tb[TCA_ACT_KIND];
1363 if (!kind) {
1364 NL_SET_ERR_MSG(extack, "TC action kind must be specified");
1365 return ERR_PTR(err);
1366 }
1367 if (nla_strscpy(act_name, kind, IFNAMSIZ) < 0) {
1368 NL_SET_ERR_MSG(extack, "TC action name too long");
1369 return ERR_PTR(err);
1370 }
1371 } else {
1372 if (strscpy(act_name, "police", IFNAMSIZ) < 0) {
1373 NL_SET_ERR_MSG(extack, "TC action name too long");
1374 return ERR_PTR(-EINVAL);
1375 }
1376 }
1377
1378 a_o = tc_lookup_action_n(act_name);
1379 if (a_o == NULL) {
1380 #ifdef CONFIG_MODULES
1381 bool rtnl_held = !(flags & TCA_ACT_FLAGS_NO_RTNL);
1382
1383 if (rtnl_held)
1384 rtnl_unlock();
1385 request_module(NET_ACT_ALIAS_PREFIX "%s", act_name);
1386 if (rtnl_held)
1387 rtnl_lock();
1388
1389 a_o = tc_lookup_action_n(act_name);
1390
1391 /* We dropped the RTNL semaphore in order to
1392 * perform the module load. So, even if we
1393 * succeeded in loading the module we have to
1394 * tell the caller to replay the request. We
1395 * indicate this using -EAGAIN.
1396 */
1397 if (a_o != NULL) {
1398 module_put(a_o->owner);
1399 return ERR_PTR(-EAGAIN);
1400 }
1401 #endif
1402 NL_SET_ERR_MSG(extack, "Failed to load TC action module");
1403 return ERR_PTR(-ENOENT);
1404 }
1405
1406 return a_o;
1407 }
1408
tcf_action_init_1(struct net * net,struct tcf_proto * tp,struct nlattr * nla,struct nlattr * est,struct tc_action_ops * a_o,int * init_res,u32 flags,struct netlink_ext_ack * extack)1409 struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
1410 struct nlattr *nla, struct nlattr *est,
1411 struct tc_action_ops *a_o, int *init_res,
1412 u32 flags, struct netlink_ext_ack *extack)
1413 {
1414 bool police = flags & TCA_ACT_FLAGS_POLICE;
1415 struct nla_bitfield32 userflags = { 0, 0 };
1416 struct tc_cookie *user_cookie = NULL;
1417 u8 hw_stats = TCA_ACT_HW_STATS_ANY;
1418 struct nlattr *tb[TCA_ACT_MAX + 1];
1419 struct tc_action *a;
1420 int err;
1421
1422 /* backward compatibility for policer */
1423 if (!police) {
1424 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1425 tcf_action_policy, extack);
1426 if (err < 0)
1427 return ERR_PTR(err);
1428 if (tb[TCA_ACT_COOKIE]) {
1429 user_cookie = nla_memdup_cookie(tb);
1430 if (!user_cookie) {
1431 NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
1432 err = -ENOMEM;
1433 goto err_out;
1434 }
1435 }
1436 hw_stats = tcf_action_hw_stats_get(tb[TCA_ACT_HW_STATS]);
1437 if (tb[TCA_ACT_FLAGS]) {
1438 userflags = nla_get_bitfield32(tb[TCA_ACT_FLAGS]);
1439 if (!tc_act_flags_valid(userflags.value)) {
1440 err = -EINVAL;
1441 goto err_out;
1442 }
1443 }
1444
1445 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, tp,
1446 userflags.value | flags, extack);
1447 } else {
1448 err = a_o->init(net, nla, est, &a, tp, userflags.value | flags,
1449 extack);
1450 }
1451 if (err < 0)
1452 goto err_out;
1453 *init_res = err;
1454
1455 if (!police && tb[TCA_ACT_COOKIE])
1456 tcf_set_action_cookie(&a->user_cookie, user_cookie);
1457
1458 if (!police)
1459 a->hw_stats = hw_stats;
1460
1461 return a;
1462
1463 err_out:
1464 if (user_cookie) {
1465 kfree(user_cookie->data);
1466 kfree(user_cookie);
1467 }
1468 return ERR_PTR(err);
1469 }
1470
tc_act_bind(u32 flags)1471 static bool tc_act_bind(u32 flags)
1472 {
1473 return !!(flags & TCA_ACT_FLAGS_BIND);
1474 }
1475
1476 /* Returns numbers of initialized actions or negative error. */
1477
tcf_action_init(struct net * net,struct tcf_proto * tp,struct nlattr * nla,struct nlattr * est,struct tc_action * actions[],int init_res[],size_t * attr_size,u32 flags,u32 fl_flags,struct netlink_ext_ack * extack)1478 int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
1479 struct nlattr *est, struct tc_action *actions[],
1480 int init_res[], size_t *attr_size,
1481 u32 flags, u32 fl_flags,
1482 struct netlink_ext_ack *extack)
1483 {
1484 struct tc_action_ops *ops[TCA_ACT_MAX_PRIO] = {};
1485 struct nlattr *tb[TCA_ACT_MAX_PRIO + 2];
1486 struct tc_action *act;
1487 size_t sz = 0;
1488 int err;
1489 int i;
1490
1491 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO + 1, nla, NULL,
1492 extack);
1493 if (err < 0)
1494 return err;
1495
1496 /* The nested attributes are parsed as types, but they are really an
1497 * array of actions. So we parse one more than we can handle, and return
1498 * an error if the last one is set (as that indicates that the request
1499 * contained more than the maximum number of actions).
1500 */
1501 if (tb[TCA_ACT_MAX_PRIO + 1]) {
1502 NL_SET_ERR_MSG_FMT(extack,
1503 "Only %d actions supported per filter",
1504 TCA_ACT_MAX_PRIO);
1505 return -EINVAL;
1506 }
1507
1508 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
1509 struct tc_action_ops *a_o;
1510
1511 a_o = tc_action_load_ops(tb[i], flags, extack);
1512 if (IS_ERR(a_o)) {
1513 err = PTR_ERR(a_o);
1514 goto err_mod;
1515 }
1516 ops[i - 1] = a_o;
1517 }
1518
1519 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
1520 act = tcf_action_init_1(net, tp, tb[i], est, ops[i - 1],
1521 &init_res[i - 1], flags, extack);
1522 if (IS_ERR(act)) {
1523 err = PTR_ERR(act);
1524 goto err;
1525 }
1526 sz += tcf_action_fill_size(act);
1527 /* Start from index 0 */
1528 actions[i - 1] = act;
1529 if (tc_act_bind(flags)) {
1530 bool skip_sw = tc_skip_sw(fl_flags);
1531 bool skip_hw = tc_skip_hw(fl_flags);
1532
1533 if (tc_act_bind(act->tcfa_flags)) {
1534 /* Action is created by classifier and is not
1535 * standalone. Check that the user did not set
1536 * any action flags different than the
1537 * classifier flags, and inherit the flags from
1538 * the classifier for the compatibility case
1539 * where no flags were specified at all.
1540 */
1541 if ((tc_act_skip_sw(act->tcfa_flags) && !skip_sw) ||
1542 (tc_act_skip_hw(act->tcfa_flags) && !skip_hw)) {
1543 NL_SET_ERR_MSG(extack,
1544 "Mismatch between action and filter offload flags");
1545 err = -EINVAL;
1546 goto err;
1547 }
1548 if (skip_sw)
1549 act->tcfa_flags |= TCA_ACT_FLAGS_SKIP_SW;
1550 if (skip_hw)
1551 act->tcfa_flags |= TCA_ACT_FLAGS_SKIP_HW;
1552 continue;
1553 }
1554
1555 /* Action is standalone */
1556 if (skip_sw != tc_act_skip_sw(act->tcfa_flags) ||
1557 skip_hw != tc_act_skip_hw(act->tcfa_flags)) {
1558 NL_SET_ERR_MSG(extack,
1559 "Mismatch between action and filter offload flags");
1560 err = -EINVAL;
1561 goto err;
1562 }
1563 } else {
1564 err = tcf_action_offload_add(act, extack);
1565 if (tc_act_skip_sw(act->tcfa_flags) && err)
1566 goto err;
1567 }
1568 }
1569
1570 /* We have to commit them all together, because if any error happened in
1571 * between, we could not handle the failure gracefully.
1572 */
1573 tcf_idr_insert_many(actions, init_res);
1574
1575 *attr_size = tcf_action_full_attrs_size(sz);
1576 err = i - 1;
1577 goto err_mod;
1578
1579 err:
1580 tcf_action_destroy(actions, flags & TCA_ACT_FLAGS_BIND);
1581 err_mod:
1582 for (i = 0; i < TCA_ACT_MAX_PRIO && ops[i]; i++)
1583 module_put(ops[i]->owner);
1584 return err;
1585 }
1586
tcf_action_update_stats(struct tc_action * a,u64 bytes,u64 packets,u64 drops,bool hw)1587 void tcf_action_update_stats(struct tc_action *a, u64 bytes, u64 packets,
1588 u64 drops, bool hw)
1589 {
1590 if (a->cpu_bstats) {
1591 _bstats_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
1592
1593 this_cpu_add(a->cpu_qstats->drops, drops);
1594
1595 if (hw)
1596 _bstats_update(this_cpu_ptr(a->cpu_bstats_hw),
1597 bytes, packets);
1598 return;
1599 }
1600
1601 _bstats_update(&a->tcfa_bstats, bytes, packets);
1602 atomic_add(drops, &a->tcfa_drops);
1603 if (hw)
1604 _bstats_update(&a->tcfa_bstats_hw, bytes, packets);
1605 }
1606 EXPORT_SYMBOL(tcf_action_update_stats);
1607
tcf_action_copy_stats(struct sk_buff * skb,struct tc_action * p,int compat_mode)1608 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
1609 int compat_mode)
1610 {
1611 struct gnet_stats_queue qstats = {0};
1612 struct gnet_dump d;
1613 int err = 0;
1614
1615 if (p == NULL)
1616 goto errout;
1617
1618 /* compat_mode being true specifies a call that is supposed
1619 * to add additional backward compatibility statistic TLVs.
1620 */
1621 if (compat_mode) {
1622 if (p->type == TCA_OLD_COMPAT)
1623 err = gnet_stats_start_copy_compat(skb, 0,
1624 TCA_STATS,
1625 TCA_XSTATS,
1626 &p->tcfa_lock, &d,
1627 TCA_PAD);
1628 else
1629 return 0;
1630 } else
1631 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
1632 &p->tcfa_lock, &d, TCA_ACT_PAD);
1633
1634 if (err < 0)
1635 goto errout;
1636
1637 qstats.drops = atomic_read(&p->tcfa_drops);
1638 qstats.overlimits = atomic_read(&p->tcfa_overlimits);
1639
1640 if (gnet_stats_copy_basic(&d, p->cpu_bstats,
1641 &p->tcfa_bstats, false) < 0 ||
1642 gnet_stats_copy_basic_hw(&d, p->cpu_bstats_hw,
1643 &p->tcfa_bstats_hw, false) < 0 ||
1644 gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
1645 gnet_stats_copy_queue(&d, p->cpu_qstats,
1646 &qstats,
1647 qstats.qlen) < 0)
1648 goto errout;
1649
1650 if (gnet_stats_finish_copy(&d) < 0)
1651 goto errout;
1652
1653 return 0;
1654
1655 errout:
1656 return -1;
1657 }
1658
tca_get_fill(struct sk_buff * skb,struct tc_action * actions[],u32 portid,u32 seq,u16 flags,int event,int bind,int ref,struct netlink_ext_ack * extack)1659 static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[],
1660 u32 portid, u32 seq, u16 flags, int event, int bind,
1661 int ref, struct netlink_ext_ack *extack)
1662 {
1663 struct tcamsg *t;
1664 struct nlmsghdr *nlh;
1665 unsigned char *b = skb_tail_pointer(skb);
1666 struct nlattr *nest;
1667
1668 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
1669 if (!nlh)
1670 goto out_nlmsg_trim;
1671 t = nlmsg_data(nlh);
1672 t->tca_family = AF_UNSPEC;
1673 t->tca__pad1 = 0;
1674 t->tca__pad2 = 0;
1675
1676 if (extack && extack->_msg &&
1677 nla_put_string(skb, TCA_ROOT_EXT_WARN_MSG, extack->_msg))
1678 goto out_nlmsg_trim;
1679
1680 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
1681 if (!nest)
1682 goto out_nlmsg_trim;
1683
1684 if (tcf_action_dump(skb, actions, bind, ref, false) < 0)
1685 goto out_nlmsg_trim;
1686
1687 nla_nest_end(skb, nest);
1688
1689 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1690
1691 return skb->len;
1692
1693 out_nlmsg_trim:
1694 nlmsg_trim(skb, b);
1695 return -1;
1696 }
1697
1698 static int
tcf_get_notify(struct net * net,u32 portid,struct nlmsghdr * n,struct tc_action * actions[],size_t attr_size,int event,struct netlink_ext_ack * extack)1699 tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
1700 struct tc_action *actions[], size_t attr_size, int event,
1701 struct netlink_ext_ack *extack)
1702 {
1703 struct sk_buff *skb;
1704
1705 skb = alloc_skb(max(attr_size, NLMSG_GOODSIZE), GFP_KERNEL);
1706 if (!skb)
1707 return -ENOBUFS;
1708 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
1709 0, 1, NULL) <= 0) {
1710 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
1711 kfree_skb(skb);
1712 return -EINVAL;
1713 }
1714
1715 return rtnl_unicast(skb, net, portid);
1716 }
1717
tcf_action_get_1(struct net * net,struct nlattr * nla,struct nlmsghdr * n,u32 portid,struct netlink_ext_ack * extack)1718 static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
1719 struct nlmsghdr *n, u32 portid,
1720 struct netlink_ext_ack *extack)
1721 {
1722 struct nlattr *tb[TCA_ACT_MAX + 1];
1723 const struct tc_action_ops *ops;
1724 struct tc_action *a;
1725 int index;
1726 int err;
1727
1728 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1729 tcf_action_policy, extack);
1730 if (err < 0)
1731 goto err_out;
1732
1733 err = -EINVAL;
1734 if (tb[TCA_ACT_INDEX] == NULL ||
1735 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
1736 NL_SET_ERR_MSG(extack, "Invalid TC action index value");
1737 goto err_out;
1738 }
1739 index = nla_get_u32(tb[TCA_ACT_INDEX]);
1740
1741 err = -EINVAL;
1742 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
1743 if (!ops) { /* could happen in batch of actions */
1744 NL_SET_ERR_MSG(extack, "Specified TC action kind not found");
1745 goto err_out;
1746 }
1747 err = -ENOENT;
1748 if (__tcf_idr_search(net, ops, &a, index) == 0) {
1749 NL_SET_ERR_MSG(extack, "TC action with specified index not found");
1750 goto err_mod;
1751 }
1752
1753 module_put(ops->owner);
1754 return a;
1755
1756 err_mod:
1757 module_put(ops->owner);
1758 err_out:
1759 return ERR_PTR(err);
1760 }
1761
tca_action_flush(struct net * net,struct nlattr * nla,struct nlmsghdr * n,u32 portid,struct netlink_ext_ack * extack)1762 static int tca_action_flush(struct net *net, struct nlattr *nla,
1763 struct nlmsghdr *n, u32 portid,
1764 struct netlink_ext_ack *extack)
1765 {
1766 struct sk_buff *skb;
1767 unsigned char *b;
1768 struct nlmsghdr *nlh;
1769 struct tcamsg *t;
1770 struct netlink_callback dcb;
1771 struct nlattr *nest;
1772 struct nlattr *tb[TCA_ACT_MAX + 1];
1773 const struct tc_action_ops *ops;
1774 struct nlattr *kind;
1775 int err = -ENOMEM;
1776
1777 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1778 if (!skb)
1779 return err;
1780
1781 b = skb_tail_pointer(skb);
1782
1783 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1784 tcf_action_policy, extack);
1785 if (err < 0)
1786 goto err_out;
1787
1788 err = -EINVAL;
1789 kind = tb[TCA_ACT_KIND];
1790 ops = tc_lookup_action(kind);
1791 if (!ops) { /*some idjot trying to flush unknown action */
1792 NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
1793 goto err_out;
1794 }
1795
1796 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
1797 sizeof(*t), 0);
1798 if (!nlh) {
1799 NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
1800 goto out_module_put;
1801 }
1802 t = nlmsg_data(nlh);
1803 t->tca_family = AF_UNSPEC;
1804 t->tca__pad1 = 0;
1805 t->tca__pad2 = 0;
1806
1807 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
1808 if (!nest) {
1809 NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
1810 goto out_module_put;
1811 }
1812
1813 err = __tcf_generic_walker(net, skb, &dcb, RTM_DELACTION, ops, extack);
1814 if (err <= 0) {
1815 nla_nest_cancel(skb, nest);
1816 goto out_module_put;
1817 }
1818
1819 nla_nest_end(skb, nest);
1820
1821 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1822 nlh->nlmsg_flags |= NLM_F_ROOT;
1823 module_put(ops->owner);
1824 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1825 n->nlmsg_flags & NLM_F_ECHO);
1826 if (err < 0)
1827 NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
1828
1829 return err;
1830
1831 out_module_put:
1832 module_put(ops->owner);
1833 err_out:
1834 kfree_skb(skb);
1835 return err;
1836 }
1837
tcf_action_delete(struct net * net,struct tc_action * actions[])1838 static int tcf_action_delete(struct net *net, struct tc_action *actions[])
1839 {
1840 struct tc_action *a;
1841 int i;
1842
1843 tcf_act_for_each_action(i, a, actions) {
1844 const struct tc_action_ops *ops = a->ops;
1845 /* Actions can be deleted concurrently so we must save their
1846 * type and id to search again after reference is released.
1847 */
1848 struct tcf_idrinfo *idrinfo = a->idrinfo;
1849 u32 act_index = a->tcfa_index;
1850
1851 actions[i] = NULL;
1852 if (tcf_action_put(a)) {
1853 /* last reference, action was deleted concurrently */
1854 module_put(ops->owner);
1855 } else {
1856 int ret;
1857
1858 /* now do the delete */
1859 ret = tcf_idr_delete_index(idrinfo, act_index);
1860 if (ret < 0)
1861 return ret;
1862 }
1863 }
1864 return 0;
1865 }
1866
tcf_reoffload_del_notify_msg(struct net * net,struct tc_action * action)1867 static struct sk_buff *tcf_reoffload_del_notify_msg(struct net *net,
1868 struct tc_action *action)
1869 {
1870 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {
1871 [0] = action,
1872 };
1873 struct sk_buff *skb;
1874 size_t attr_size;
1875
1876 attr_size = tcf_action_full_attrs_size(tcf_action_fill_size(action));
1877
1878 skb = alloc_skb(max(attr_size, NLMSG_GOODSIZE), GFP_KERNEL);
1879 if (!skb)
1880 return ERR_PTR(-ENOBUFS);
1881
1882 if (tca_get_fill(skb, actions, 0, 0, 0, RTM_DELACTION, 0, 1, NULL) <= 0) {
1883 kfree_skb(skb);
1884 return ERR_PTR(-EINVAL);
1885 }
1886
1887 return skb;
1888 }
1889
tcf_reoffload_del_notify(struct net * net,struct tc_action * action)1890 static int tcf_reoffload_del_notify(struct net *net, struct tc_action *action)
1891 {
1892 const struct tc_action_ops *ops = action->ops;
1893 struct sk_buff *skb = NULL;
1894 int ret;
1895
1896 if (rtnl_notify_needed(net, 0, RTNLGRP_TC)) {
1897 skb = tcf_reoffload_del_notify_msg(net, action);
1898 /* The action has already lost its hardware instance and is
1899 * skip_sw, so it must be released whether or not the
1900 * notification can be built. Drop the notification rather
1901 * than leave an action behind that processes no packets.
1902 */
1903 if (IS_ERR(skb))
1904 skb = NULL;
1905 }
1906
1907 ret = tcf_idr_release_unsafe(action);
1908 if (ret == ACT_P_DELETED) {
1909 module_put(ops->owner);
1910 ret = rtnetlink_maybe_send(skb, net, 0, RTNLGRP_TC, 0);
1911 } else {
1912 kfree_skb(skb);
1913 }
1914
1915 return ret;
1916 }
1917
tcf_action_reoffload_cb(flow_indr_block_bind_cb_t * cb,void * cb_priv,bool add)1918 int tcf_action_reoffload_cb(flow_indr_block_bind_cb_t *cb,
1919 void *cb_priv, bool add)
1920 {
1921 struct tc_act_pernet_id *id_ptr;
1922 struct tcf_idrinfo *idrinfo;
1923 struct tc_action_net *tn;
1924 struct tc_action *p;
1925 unsigned int act_id;
1926 unsigned long tmp;
1927 unsigned long id;
1928 struct idr *idr;
1929 struct net *net;
1930 int ret;
1931
1932 if (!cb)
1933 return -EINVAL;
1934
1935 down_read(&net_rwsem);
1936 mutex_lock(&act_id_mutex);
1937
1938 for_each_net(net) {
1939 list_for_each_entry(id_ptr, &act_pernet_id_list, list) {
1940 act_id = id_ptr->id;
1941 tn = net_generic(net, act_id);
1942 if (!tn)
1943 continue;
1944 idrinfo = tn->idrinfo;
1945 if (!idrinfo)
1946 continue;
1947
1948 mutex_lock(&idrinfo->lock);
1949 idr = &idrinfo->action_idr;
1950 idr_for_each_entry_ul(idr, p, tmp, id) {
1951 if (IS_ERR(p) || tc_act_bind(p->tcfa_flags))
1952 continue;
1953 if (add) {
1954 tcf_action_offload_add_ex(p, NULL, cb,
1955 cb_priv);
1956 continue;
1957 }
1958
1959 /* cb unregister to update hw count */
1960 ret = tcf_action_offload_del_ex(p, cb, cb_priv);
1961 if (ret < 0)
1962 continue;
1963 if (tc_act_skip_sw(p->tcfa_flags) &&
1964 !tc_act_in_hw(p))
1965 tcf_reoffload_del_notify(net, p);
1966 }
1967 mutex_unlock(&idrinfo->lock);
1968 }
1969 }
1970 mutex_unlock(&act_id_mutex);
1971 up_read(&net_rwsem);
1972
1973 return 0;
1974 }
1975
tcf_del_notify_msg(struct net * net,struct nlmsghdr * n,struct tc_action * actions[],u32 portid,size_t attr_size,struct netlink_ext_ack * extack)1976 static struct sk_buff *tcf_del_notify_msg(struct net *net, struct nlmsghdr *n,
1977 struct tc_action *actions[],
1978 u32 portid, size_t attr_size,
1979 struct netlink_ext_ack *extack)
1980 {
1981 struct sk_buff *skb;
1982
1983 skb = alloc_skb(max(attr_size, NLMSG_GOODSIZE), GFP_KERNEL);
1984 if (!skb)
1985 return ERR_PTR(-ENOBUFS);
1986
1987 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
1988 0, 2, extack) <= 0) {
1989 NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
1990 kfree_skb(skb);
1991 return ERR_PTR(-EINVAL);
1992 }
1993
1994 return skb;
1995 }
1996
tcf_del_notify(struct net * net,struct nlmsghdr * n,struct tc_action * actions[],u32 portid,size_t attr_size,struct netlink_ext_ack * extack)1997 static int tcf_del_notify(struct net *net, struct nlmsghdr *n,
1998 struct tc_action *actions[], u32 portid,
1999 size_t attr_size, struct netlink_ext_ack *extack)
2000 {
2001 struct sk_buff *skb;
2002 int ret;
2003
2004 if (!rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC)) {
2005 skb = NULL;
2006 } else {
2007 skb = tcf_del_notify_msg(net, n, actions, portid, attr_size,
2008 extack);
2009 if (IS_ERR(skb))
2010 return PTR_ERR(skb);
2011 }
2012
2013 /* now do the delete */
2014 ret = tcf_action_delete(net, actions);
2015 if (ret < 0) {
2016 NL_SET_ERR_MSG(extack, "Failed to delete TC action");
2017 kfree_skb(skb);
2018 return ret;
2019 }
2020
2021 return rtnetlink_maybe_send(skb, net, portid, RTNLGRP_TC,
2022 n->nlmsg_flags & NLM_F_ECHO);
2023 }
2024
2025 static int
tca_action_gd(struct net * net,struct nlattr * nla,struct nlmsghdr * n,u32 portid,int event,struct netlink_ext_ack * extack)2026 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
2027 u32 portid, int event, struct netlink_ext_ack *extack)
2028 {
2029 int i, ret;
2030 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
2031 struct tc_action *act;
2032 size_t attr_size = 0;
2033 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
2034
2035 ret = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
2036 extack);
2037 if (ret < 0)
2038 return ret;
2039
2040 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
2041 if (tb[1])
2042 return tca_action_flush(net, tb[1], n, portid, extack);
2043
2044 NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
2045 return -EINVAL;
2046 }
2047
2048 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
2049 act = tcf_action_get_1(net, tb[i], n, portid, extack);
2050 if (IS_ERR(act)) {
2051 ret = PTR_ERR(act);
2052 goto err;
2053 }
2054 attr_size += tcf_action_fill_size(act);
2055 actions[i - 1] = act;
2056 }
2057
2058 attr_size = tcf_action_full_attrs_size(attr_size);
2059
2060 if (event == RTM_GETACTION)
2061 ret = tcf_get_notify(net, portid, n, actions, attr_size, event,
2062 extack);
2063 else { /* delete */
2064 ret = tcf_del_notify(net, n, actions, portid, attr_size, extack);
2065 if (ret)
2066 goto err;
2067 return 0;
2068 }
2069 err:
2070 tcf_action_put_many(actions);
2071 return ret;
2072 }
2073
tcf_add_notify_msg(struct net * net,struct nlmsghdr * n,struct tc_action * actions[],u32 portid,size_t attr_size,struct netlink_ext_ack * extack)2074 static struct sk_buff *tcf_add_notify_msg(struct net *net, struct nlmsghdr *n,
2075 struct tc_action *actions[],
2076 u32 portid, size_t attr_size,
2077 struct netlink_ext_ack *extack)
2078 {
2079 struct sk_buff *skb;
2080
2081 skb = alloc_skb(max(attr_size, NLMSG_GOODSIZE), GFP_KERNEL);
2082 if (!skb)
2083 return ERR_PTR(-ENOBUFS);
2084
2085 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
2086 RTM_NEWACTION, 0, 0, extack) <= 0) {
2087 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
2088 kfree_skb(skb);
2089 return ERR_PTR(-EINVAL);
2090 }
2091
2092 return skb;
2093 }
2094
tcf_add_notify(struct net * net,struct nlmsghdr * n,struct tc_action * actions[],u32 portid,size_t attr_size,struct netlink_ext_ack * extack)2095 static int tcf_add_notify(struct net *net, struct nlmsghdr *n,
2096 struct tc_action *actions[], u32 portid,
2097 size_t attr_size, struct netlink_ext_ack *extack)
2098 {
2099 struct sk_buff *skb;
2100
2101 if (!rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC)) {
2102 skb = NULL;
2103 } else {
2104 skb = tcf_add_notify_msg(net, n, actions, portid, attr_size,
2105 extack);
2106 if (IS_ERR(skb))
2107 return PTR_ERR(skb);
2108 }
2109
2110 return rtnetlink_maybe_send(skb, net, portid, RTNLGRP_TC,
2111 n->nlmsg_flags & NLM_F_ECHO);
2112 }
2113
tcf_action_add(struct net * net,struct nlattr * nla,struct nlmsghdr * n,u32 portid,u32 flags,struct netlink_ext_ack * extack)2114 static int tcf_action_add(struct net *net, struct nlattr *nla,
2115 struct nlmsghdr *n, u32 portid, u32 flags,
2116 struct netlink_ext_ack *extack)
2117 {
2118 size_t attr_size = 0;
2119 int loop, ret;
2120 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
2121 int init_res[TCA_ACT_MAX_PRIO] = {};
2122
2123 for (loop = 0; loop < 10; loop++) {
2124 ret = tcf_action_init(net, NULL, nla, NULL, actions, init_res,
2125 &attr_size, flags, 0, extack);
2126 if (ret != -EAGAIN)
2127 break;
2128 }
2129
2130 if (ret < 0)
2131 return ret;
2132
2133 ret = tcf_add_notify(net, n, actions, portid, attr_size, extack);
2134
2135 /* only put bound actions */
2136 tca_put_bound_many(actions, init_res);
2137
2138 return ret;
2139 }
2140
2141 static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
2142 [TCA_ROOT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_ACT_FLAG_LARGE_DUMP_ON |
2143 TCA_ACT_FLAG_TERSE_DUMP),
2144 [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
2145 };
2146
tc_ctl_action(struct sk_buff * skb,struct nlmsghdr * n,struct netlink_ext_ack * extack)2147 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
2148 struct netlink_ext_ack *extack)
2149 {
2150 struct net *net = sock_net(skb->sk);
2151 struct nlattr *tca[TCA_ROOT_MAX + 1];
2152 u32 portid = NETLINK_CB(skb).portid;
2153 u32 flags = 0;
2154 int ret = 0;
2155
2156 if ((n->nlmsg_type != RTM_GETACTION) &&
2157 !netlink_capable(skb, CAP_NET_ADMIN))
2158 return -EPERM;
2159
2160 ret = nlmsg_parse_deprecated(n, sizeof(struct tcamsg), tca,
2161 TCA_ROOT_MAX, NULL, extack);
2162 if (ret < 0)
2163 return ret;
2164
2165 if (tca[TCA_ACT_TAB] == NULL) {
2166 NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
2167 return -EINVAL;
2168 }
2169
2170 /* n->nlmsg_flags & NLM_F_CREATE */
2171 switch (n->nlmsg_type) {
2172 case RTM_NEWACTION:
2173 /* we are going to assume all other flags
2174 * imply create only if it doesn't exist
2175 * Note that CREATE | EXCL implies that
2176 * but since we want avoid ambiguity (eg when flags
2177 * is zero) then just set this
2178 */
2179 if (n->nlmsg_flags & NLM_F_REPLACE)
2180 flags = TCA_ACT_FLAGS_REPLACE;
2181 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, flags,
2182 extack);
2183 break;
2184 case RTM_DELACTION:
2185 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
2186 portid, RTM_DELACTION, extack);
2187 break;
2188 case RTM_GETACTION:
2189 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
2190 portid, RTM_GETACTION, extack);
2191 break;
2192 default:
2193 BUG();
2194 }
2195
2196 return ret;
2197 }
2198
find_dump_kind(struct nlattr ** nla)2199 static struct nlattr *find_dump_kind(struct nlattr **nla)
2200 {
2201 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
2202 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
2203 struct nlattr *kind;
2204
2205 tb1 = nla[TCA_ACT_TAB];
2206 if (tb1 == NULL)
2207 return NULL;
2208
2209 if (nla_parse_deprecated(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
2210 return NULL;
2211
2212 if (tb[1] == NULL)
2213 return NULL;
2214 if (nla_parse_nested_deprecated(tb2, TCA_ACT_MAX, tb[1], tcf_action_policy, NULL) < 0)
2215 return NULL;
2216 kind = tb2[TCA_ACT_KIND];
2217
2218 return kind;
2219 }
2220
tc_dump_action(struct sk_buff * skb,struct netlink_callback * cb)2221 static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
2222 {
2223 struct net *net = sock_net(skb->sk);
2224 struct nlmsghdr *nlh;
2225 unsigned char *b = skb_tail_pointer(skb);
2226 struct nlattr *nest;
2227 struct tc_action_ops *a_o;
2228 int ret = 0;
2229 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
2230 struct nlattr *tb[TCA_ROOT_MAX + 1];
2231 struct nlattr *count_attr = NULL;
2232 unsigned long jiffy_since = 0;
2233 struct nlattr *kind = NULL;
2234 struct nla_bitfield32 bf;
2235 u32 msecs_since = 0;
2236 u32 act_count = 0;
2237
2238 ret = nlmsg_parse_deprecated(cb->nlh, sizeof(struct tcamsg), tb,
2239 TCA_ROOT_MAX, tcaa_policy, cb->extack);
2240 if (ret < 0)
2241 return ret;
2242
2243 kind = find_dump_kind(tb);
2244 if (kind == NULL) {
2245 pr_info("tc_dump_action: action bad kind\n");
2246 return 0;
2247 }
2248
2249 a_o = tc_lookup_action(kind);
2250 if (a_o == NULL)
2251 return 0;
2252
2253 cb->args[2] = 0;
2254 if (tb[TCA_ROOT_FLAGS]) {
2255 bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
2256 cb->args[2] = bf.value;
2257 }
2258
2259 if (tb[TCA_ROOT_TIME_DELTA]) {
2260 msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
2261 }
2262
2263 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2264 cb->nlh->nlmsg_type, sizeof(*t), 0);
2265 if (!nlh)
2266 goto out_module_put;
2267
2268 if (msecs_since)
2269 jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
2270
2271 t = nlmsg_data(nlh);
2272 t->tca_family = AF_UNSPEC;
2273 t->tca__pad1 = 0;
2274 t->tca__pad2 = 0;
2275 cb->args[3] = jiffy_since;
2276 count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
2277 if (!count_attr)
2278 goto out_module_put;
2279
2280 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
2281 if (nest == NULL)
2282 goto out_module_put;
2283
2284 ret = __tcf_generic_walker(net, skb, cb, RTM_GETACTION, a_o, NULL);
2285 if (ret < 0)
2286 goto out_module_put;
2287
2288 if (ret > 0) {
2289 nla_nest_end(skb, nest);
2290 ret = skb->len;
2291 act_count = cb->args[1];
2292 memcpy(nla_data(count_attr), &act_count, sizeof(u32));
2293 cb->args[1] = 0;
2294 } else
2295 nlmsg_trim(skb, b);
2296
2297 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2298 if (NETLINK_CB(cb->skb).portid && ret)
2299 nlh->nlmsg_flags |= NLM_F_MULTI;
2300 module_put(a_o->owner);
2301 return skb->len;
2302
2303 out_module_put:
2304 module_put(a_o->owner);
2305 nlmsg_trim(skb, b);
2306 return skb->len;
2307 }
2308
2309 static const struct rtnl_msg_handler tc_action_rtnl_msg_handlers[] __initconst = {
2310 {.msgtype = RTM_NEWACTION, .doit = tc_ctl_action},
2311 {.msgtype = RTM_DELACTION, .doit = tc_ctl_action},
2312 {.msgtype = RTM_GETACTION, .doit = tc_ctl_action,
2313 .dumpit = tc_dump_action},
2314 };
2315
tc_action_init(void)2316 static int __init tc_action_init(void)
2317 {
2318 rtnl_register_many(tc_action_rtnl_msg_handlers);
2319 return 0;
2320 }
2321
2322 subsys_initcall(tc_action_init);
2323