Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * net/sched/act_api.c Packet action API. |
| 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Author: Jamal Hadi Salim |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/types.h> |
| 9 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/errno.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/init.h> |
| 15 | #include <linux/kmod.h> |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 16 | #include <linux/err.h> |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 17 | #include <linux/module.h> |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 18 | #include <net/net_namespace.h> |
| 19 | #include <net/sock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <net/sch_generic.h> |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 21 | #include <net/pkt_cls.h> |
Baowen Zheng | 8cbfe93 | 2021-12-17 19:16:22 +0100 | [diff] [blame] | 22 | #include <net/tc_act/tc_pedit.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <net/act_api.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 24 | #include <net/netlink.h> |
Baowen Zheng | 8cbfe93 | 2021-12-17 19:16:22 +0100 | [diff] [blame] | 25 | #include <net/flow_offload.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
wenxu | c129412 | 2020-11-25 12:01:23 +0800 | [diff] [blame] | 27 | #ifdef CONFIG_INET |
| 28 | DEFINE_STATIC_KEY_FALSE(tcf_frag_xmit_count); |
| 29 | EXPORT_SYMBOL_GPL(tcf_frag_xmit_count); |
| 30 | #endif |
| 31 | |
| 32 | int tcf_dev_queue_xmit(struct sk_buff *skb, int (*xmit)(struct sk_buff *skb)) |
| 33 | { |
| 34 | #ifdef CONFIG_INET |
| 35 | if (static_branch_unlikely(&tcf_frag_xmit_count)) |
| 36 | return sch_frag_xmit_hook(skb, xmit); |
| 37 | #endif |
| 38 | |
| 39 | return xmit(skb); |
| 40 | } |
| 41 | EXPORT_SYMBOL_GPL(tcf_dev_queue_xmit); |
| 42 | |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 43 | static void tcf_action_goto_chain_exec(const struct tc_action *a, |
| 44 | struct tcf_result *res) |
| 45 | { |
Davide Caratti | ee3bbfe | 2019-03-20 15:00:16 +0100 | [diff] [blame] | 46 | const struct tcf_chain *chain = rcu_dereference_bh(a->goto_chain); |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 47 | |
| 48 | res->goto_tp = rcu_dereference_bh(chain->filter_chain); |
| 49 | } |
| 50 | |
Vlad Buslov | eec94fd | 2018-07-05 17:24:23 +0300 | [diff] [blame] | 51 | static void tcf_free_cookie_rcu(struct rcu_head *p) |
| 52 | { |
| 53 | struct tc_cookie *cookie = container_of(p, struct tc_cookie, rcu); |
| 54 | |
| 55 | kfree(cookie->data); |
| 56 | kfree(cookie); |
| 57 | } |
| 58 | |
| 59 | static void tcf_set_action_cookie(struct tc_cookie __rcu **old_cookie, |
| 60 | struct tc_cookie *new_cookie) |
| 61 | { |
| 62 | struct tc_cookie *old; |
| 63 | |
David S. Miller | 0dbc81e | 2018-07-08 17:02:59 +0900 | [diff] [blame] | 64 | old = xchg((__force struct tc_cookie **)old_cookie, new_cookie); |
Vlad Buslov | eec94fd | 2018-07-05 17:24:23 +0300 | [diff] [blame] | 65 | if (old) |
| 66 | call_rcu(&old->rcu, tcf_free_cookie_rcu); |
| 67 | } |
| 68 | |
Davide Caratti | 85d0966 | 2019-03-20 14:59:59 +0100 | [diff] [blame] | 69 | int tcf_action_check_ctrlact(int action, struct tcf_proto *tp, |
| 70 | struct tcf_chain **newchain, |
| 71 | struct netlink_ext_ack *extack) |
| 72 | { |
| 73 | int opcode = TC_ACT_EXT_OPCODE(action), ret = -EINVAL; |
| 74 | u32 chain_index; |
| 75 | |
| 76 | if (!opcode) |
| 77 | ret = action > TC_ACT_VALUE_MAX ? -EINVAL : 0; |
| 78 | else if (opcode <= TC_ACT_EXT_OPCODE_MAX || action == TC_ACT_UNSPEC) |
| 79 | ret = 0; |
| 80 | if (ret) { |
| 81 | NL_SET_ERR_MSG(extack, "invalid control action"); |
| 82 | goto end; |
| 83 | } |
| 84 | |
| 85 | if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN)) { |
| 86 | chain_index = action & TC_ACT_EXT_VAL_MASK; |
| 87 | if (!tp || !newchain) { |
| 88 | ret = -EINVAL; |
| 89 | NL_SET_ERR_MSG(extack, |
| 90 | "can't goto NULL proto/chain"); |
| 91 | goto end; |
| 92 | } |
| 93 | *newchain = tcf_chain_get_by_act(tp->chain->block, chain_index); |
| 94 | if (!*newchain) { |
| 95 | ret = -ENOMEM; |
| 96 | NL_SET_ERR_MSG(extack, |
| 97 | "can't allocate goto_chain"); |
| 98 | } |
| 99 | } |
| 100 | end: |
| 101 | return ret; |
| 102 | } |
| 103 | EXPORT_SYMBOL(tcf_action_check_ctrlact); |
| 104 | |
| 105 | struct tcf_chain *tcf_action_set_ctrlact(struct tc_action *a, int action, |
Davide Caratti | ee3bbfe | 2019-03-20 15:00:16 +0100 | [diff] [blame] | 106 | struct tcf_chain *goto_chain) |
Davide Caratti | 85d0966 | 2019-03-20 14:59:59 +0100 | [diff] [blame] | 107 | { |
Davide Caratti | 85d0966 | 2019-03-20 14:59:59 +0100 | [diff] [blame] | 108 | a->tcfa_action = action; |
Paul E. McKenney | 445d374 | 2019-09-23 16:09:18 -0700 | [diff] [blame] | 109 | goto_chain = rcu_replace_pointer(a->goto_chain, goto_chain, 1); |
Davide Caratti | ee3bbfe | 2019-03-20 15:00:16 +0100 | [diff] [blame] | 110 | return goto_chain; |
Davide Caratti | 85d0966 | 2019-03-20 14:59:59 +0100 | [diff] [blame] | 111 | } |
| 112 | EXPORT_SYMBOL(tcf_action_set_ctrlact); |
| 113 | |
Cong Wang | d7fb60b | 2017-09-11 16:33:30 -0700 | [diff] [blame] | 114 | /* XXX: For standalone actions, we don't need a RCU grace period either, because |
| 115 | * actions are always connected to filters and filters are already destroyed in |
| 116 | * RCU callbacks, so after a RCU grace period actions are already disconnected |
| 117 | * from filters. Readers later can not find us. |
| 118 | */ |
| 119 | static void free_tcf(struct tc_action *p) |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 120 | { |
Davide Caratti | ee3bbfe | 2019-03-20 15:00:16 +0100 | [diff] [blame] | 121 | struct tcf_chain *chain = rcu_dereference_protected(p->goto_chain, 1); |
Davide Caratti | 85d0966 | 2019-03-20 14:59:59 +0100 | [diff] [blame] | 122 | |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 123 | free_percpu(p->cpu_bstats); |
Eelco Chaudron | 28169ab | 2018-09-21 07:14:02 -0400 | [diff] [blame] | 124 | free_percpu(p->cpu_bstats_hw); |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 125 | free_percpu(p->cpu_qstats); |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 126 | |
Vlad Buslov | eec94fd | 2018-07-05 17:24:23 +0300 | [diff] [blame] | 127 | tcf_set_action_cookie(&p->act_cookie, NULL); |
Davide Caratti | 85d0966 | 2019-03-20 14:59:59 +0100 | [diff] [blame] | 128 | if (chain) |
| 129 | tcf_chain_put_by_act(chain); |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 130 | |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 131 | kfree(p); |
| 132 | } |
| 133 | |
Baowen Zheng | 7adc576 | 2021-12-17 19:16:23 +0100 | [diff] [blame] | 134 | static void offload_action_hw_count_set(struct tc_action *act, |
| 135 | u32 hw_count) |
| 136 | { |
| 137 | act->in_hw_count = hw_count; |
| 138 | } |
| 139 | |
Baowen Zheng | 8cbfe93 | 2021-12-17 19:16:22 +0100 | [diff] [blame] | 140 | static unsigned int tcf_offload_act_num_actions_single(struct tc_action *act) |
| 141 | { |
| 142 | if (is_tcf_pedit(act)) |
| 143 | return tcf_pedit_nkeys(act); |
| 144 | else |
| 145 | return 1; |
| 146 | } |
| 147 | |
Baowen Zheng | 7adc576 | 2021-12-17 19:16:23 +0100 | [diff] [blame] | 148 | static bool tc_act_skip_hw(u32 flags) |
| 149 | { |
| 150 | return (flags & TCA_ACT_FLAGS_SKIP_HW) ? true : false; |
| 151 | } |
| 152 | |
| 153 | static bool tc_act_skip_sw(u32 flags) |
| 154 | { |
| 155 | return (flags & TCA_ACT_FLAGS_SKIP_SW) ? true : false; |
| 156 | } |
| 157 | |
| 158 | static bool tc_act_in_hw(struct tc_action *act) |
| 159 | { |
| 160 | return !!act->in_hw_count; |
| 161 | } |
| 162 | |
| 163 | /* SKIP_HW and SKIP_SW are mutually exclusive flags. */ |
| 164 | static bool tc_act_flags_valid(u32 flags) |
| 165 | { |
| 166 | flags &= TCA_ACT_FLAGS_SKIP_HW | TCA_ACT_FLAGS_SKIP_SW; |
| 167 | |
| 168 | return flags ^ (TCA_ACT_FLAGS_SKIP_HW | TCA_ACT_FLAGS_SKIP_SW); |
| 169 | } |
| 170 | |
Baowen Zheng | 8cbfe93 | 2021-12-17 19:16:22 +0100 | [diff] [blame] | 171 | static int offload_action_init(struct flow_offload_action *fl_action, |
| 172 | struct tc_action *act, |
| 173 | enum offload_act_command cmd, |
| 174 | struct netlink_ext_ack *extack) |
| 175 | { |
| 176 | fl_action->extack = extack; |
| 177 | fl_action->command = cmd; |
| 178 | fl_action->index = act->tcfa_index; |
| 179 | |
| 180 | if (act->ops->offload_act_setup) |
| 181 | return act->ops->offload_act_setup(act, fl_action, NULL, false); |
| 182 | |
| 183 | return -EOPNOTSUPP; |
| 184 | } |
| 185 | |
| 186 | static int tcf_action_offload_cmd(struct flow_offload_action *fl_act, |
Baowen Zheng | 7adc576 | 2021-12-17 19:16:23 +0100 | [diff] [blame] | 187 | u32 *hw_count, |
Baowen Zheng | 8cbfe93 | 2021-12-17 19:16:22 +0100 | [diff] [blame] | 188 | struct netlink_ext_ack *extack) |
| 189 | { |
| 190 | int err; |
| 191 | |
| 192 | err = flow_indr_dev_setup_offload(NULL, NULL, TC_SETUP_ACT, |
| 193 | fl_act, NULL, NULL); |
| 194 | if (err < 0) |
| 195 | return err; |
| 196 | |
Baowen Zheng | 7adc576 | 2021-12-17 19:16:23 +0100 | [diff] [blame] | 197 | if (hw_count) |
| 198 | *hw_count = err; |
| 199 | |
Baowen Zheng | 8cbfe93 | 2021-12-17 19:16:22 +0100 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | /* offload the tc action after it is inserted */ |
| 204 | static int tcf_action_offload_add(struct tc_action *action, |
| 205 | struct netlink_ext_ack *extack) |
| 206 | { |
Baowen Zheng | 7adc576 | 2021-12-17 19:16:23 +0100 | [diff] [blame] | 207 | bool skip_sw = tc_act_skip_sw(action->tcfa_flags); |
Baowen Zheng | 8cbfe93 | 2021-12-17 19:16:22 +0100 | [diff] [blame] | 208 | struct tc_action *actions[TCA_ACT_MAX_PRIO] = { |
| 209 | [0] = action, |
| 210 | }; |
| 211 | struct flow_offload_action *fl_action; |
Baowen Zheng | 7adc576 | 2021-12-17 19:16:23 +0100 | [diff] [blame] | 212 | u32 in_hw_count = 0; |
Baowen Zheng | 8cbfe93 | 2021-12-17 19:16:22 +0100 | [diff] [blame] | 213 | int num, err = 0; |
| 214 | |
Baowen Zheng | 7adc576 | 2021-12-17 19:16:23 +0100 | [diff] [blame] | 215 | if (tc_act_skip_hw(action->tcfa_flags)) |
| 216 | return 0; |
| 217 | |
Baowen Zheng | 8cbfe93 | 2021-12-17 19:16:22 +0100 | [diff] [blame] | 218 | num = tcf_offload_act_num_actions_single(action); |
| 219 | fl_action = offload_action_alloc(num); |
| 220 | if (!fl_action) |
| 221 | return -ENOMEM; |
| 222 | |
| 223 | err = offload_action_init(fl_action, action, FLOW_ACT_REPLACE, extack); |
| 224 | if (err) |
| 225 | goto fl_err; |
| 226 | |
| 227 | err = tc_setup_action(&fl_action->action, actions); |
| 228 | if (err) { |
| 229 | NL_SET_ERR_MSG_MOD(extack, |
| 230 | "Failed to setup tc actions for offload\n"); |
| 231 | goto fl_err; |
| 232 | } |
| 233 | |
Baowen Zheng | 7adc576 | 2021-12-17 19:16:23 +0100 | [diff] [blame] | 234 | err = tcf_action_offload_cmd(fl_action, &in_hw_count, extack); |
| 235 | if (!err) |
| 236 | offload_action_hw_count_set(action, in_hw_count); |
| 237 | |
| 238 | if (skip_sw && !tc_act_in_hw(action)) |
| 239 | err = -EINVAL; |
| 240 | |
Baowen Zheng | 8cbfe93 | 2021-12-17 19:16:22 +0100 | [diff] [blame] | 241 | tc_cleanup_offload_action(&fl_action->action); |
| 242 | |
| 243 | fl_err: |
| 244 | kfree(fl_action); |
| 245 | |
| 246 | return err; |
| 247 | } |
| 248 | |
Baowen Zheng | c7a66f8 | 2021-12-17 19:16:25 +0100 | [diff] [blame] | 249 | int tcf_action_update_hw_stats(struct tc_action *action) |
| 250 | { |
| 251 | struct flow_offload_action fl_act = {}; |
| 252 | int err; |
| 253 | |
| 254 | if (!tc_act_in_hw(action)) |
| 255 | return -EOPNOTSUPP; |
| 256 | |
| 257 | err = offload_action_init(&fl_act, action, FLOW_ACT_STATS, NULL); |
| 258 | if (err) |
| 259 | return err; |
| 260 | |
| 261 | err = tcf_action_offload_cmd(&fl_act, NULL, NULL); |
| 262 | if (!err) { |
| 263 | preempt_disable(); |
| 264 | tcf_action_stats_update(action, fl_act.stats.bytes, |
| 265 | fl_act.stats.pkts, |
| 266 | fl_act.stats.drops, |
| 267 | fl_act.stats.lastused, |
| 268 | true); |
| 269 | preempt_enable(); |
| 270 | action->used_hw_stats = fl_act.stats.used_hw_stats; |
| 271 | action->used_hw_stats_valid = true; |
| 272 | } else { |
| 273 | return -EOPNOTSUPP; |
| 274 | } |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | EXPORT_SYMBOL(tcf_action_update_hw_stats); |
| 279 | |
Baowen Zheng | 8cbfe93 | 2021-12-17 19:16:22 +0100 | [diff] [blame] | 280 | static int tcf_action_offload_del(struct tc_action *action) |
| 281 | { |
| 282 | struct flow_offload_action fl_act = {}; |
Baowen Zheng | 7adc576 | 2021-12-17 19:16:23 +0100 | [diff] [blame] | 283 | u32 in_hw_count = 0; |
Baowen Zheng | 8cbfe93 | 2021-12-17 19:16:22 +0100 | [diff] [blame] | 284 | int err = 0; |
| 285 | |
Baowen Zheng | 7adc576 | 2021-12-17 19:16:23 +0100 | [diff] [blame] | 286 | if (!tc_act_in_hw(action)) |
| 287 | return 0; |
| 288 | |
Baowen Zheng | 8cbfe93 | 2021-12-17 19:16:22 +0100 | [diff] [blame] | 289 | err = offload_action_init(&fl_act, action, FLOW_ACT_DESTROY, NULL); |
| 290 | if (err) |
| 291 | return err; |
| 292 | |
Baowen Zheng | 7adc576 | 2021-12-17 19:16:23 +0100 | [diff] [blame] | 293 | err = tcf_action_offload_cmd(&fl_act, &in_hw_count, NULL); |
| 294 | if (err) |
| 295 | return err; |
| 296 | |
| 297 | if (action->in_hw_count != in_hw_count) |
| 298 | return -EINVAL; |
| 299 | |
| 300 | return 0; |
Baowen Zheng | 8cbfe93 | 2021-12-17 19:16:22 +0100 | [diff] [blame] | 301 | } |
| 302 | |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 303 | static void tcf_action_cleanup(struct tc_action *p) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 304 | { |
Baowen Zheng | 8cbfe93 | 2021-12-17 19:16:22 +0100 | [diff] [blame] | 305 | tcf_action_offload_del(p); |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 306 | if (p->ops->cleanup) |
| 307 | p->ops->cleanup(p); |
| 308 | |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 309 | gen_kill_estimator(&p->tcfa_rate_est); |
Cong Wang | d7fb60b | 2017-09-11 16:33:30 -0700 | [diff] [blame] | 310 | free_tcf(p); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 311 | } |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 312 | |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 313 | static int __tcf_action_put(struct tc_action *p, bool bind) |
| 314 | { |
| 315 | struct tcf_idrinfo *idrinfo = p->idrinfo; |
| 316 | |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 317 | if (refcount_dec_and_mutex_lock(&p->tcfa_refcnt, &idrinfo->lock)) { |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 318 | if (bind) |
| 319 | atomic_dec(&p->tcfa_bindcnt); |
| 320 | idr_remove(&idrinfo->action_idr, p->tcfa_index); |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 321 | mutex_unlock(&idrinfo->lock); |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 322 | |
| 323 | tcf_action_cleanup(p); |
| 324 | return 1; |
| 325 | } |
| 326 | |
| 327 | if (bind) |
| 328 | atomic_dec(&p->tcfa_bindcnt); |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
Vlad Buslov | b3650bf7 | 2021-04-07 18:36:04 +0300 | [diff] [blame] | 333 | static int __tcf_idr_release(struct tc_action *p, bool bind, bool strict) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 334 | { |
| 335 | int ret = 0; |
| 336 | |
Vlad Buslov | 036bb44 | 2018-07-05 17:24:24 +0300 | [diff] [blame] | 337 | /* Release with strict==1 and bind==0 is only called through act API |
| 338 | * interface (classifiers always bind). Only case when action with |
| 339 | * positive reference count and zero bind count can exist is when it was |
| 340 | * also created with act API (unbinding last classifier will destroy the |
| 341 | * action if it was created by classifier). So only case when bind count |
| 342 | * can be changed after initial check is when unbound action is |
| 343 | * destroyed by act API while classifier binds to action with same id |
| 344 | * concurrently. This result either creation of new action(same behavior |
| 345 | * as before), or reusing existing action if concurrent process |
| 346 | * increments reference count before action is deleted. Both scenarios |
| 347 | * are acceptable. |
| 348 | */ |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 349 | if (p) { |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 350 | if (!bind && strict && atomic_read(&p->tcfa_bindcnt) > 0) |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 351 | return -EPERM; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 352 | |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 353 | if (__tcf_action_put(p, bind)) |
WANG Cong | 1d4150c | 2016-02-22 15:57:52 -0800 | [diff] [blame] | 354 | ret = ACT_P_DELETED; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 355 | } |
Daniel Borkmann | 28e6b67 | 2015-07-29 23:35:25 +0200 | [diff] [blame] | 356 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 357 | return ret; |
| 358 | } |
Vlad Buslov | b3650bf7 | 2021-04-07 18:36:04 +0300 | [diff] [blame] | 359 | |
| 360 | int tcf_idr_release(struct tc_action *a, bool bind) |
| 361 | { |
| 362 | const struct tc_action_ops *ops = a->ops; |
| 363 | int ret; |
| 364 | |
| 365 | ret = __tcf_idr_release(a, bind, false); |
| 366 | if (ret == ACT_P_DELETED) |
| 367 | module_put(ops->owner); |
| 368 | return ret; |
| 369 | } |
| 370 | EXPORT_SYMBOL(tcf_idr_release); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 371 | |
Roman Mashak | 4e76e75 | 2018-03-08 16:59:19 -0500 | [diff] [blame] | 372 | static size_t tcf_action_shared_attrs_size(const struct tc_action *act) |
| 373 | { |
Vlad Buslov | e0479b6 | 2018-07-09 20:26:47 +0300 | [diff] [blame] | 374 | struct tc_cookie *act_cookie; |
Roman Mashak | 4e76e75 | 2018-03-08 16:59:19 -0500 | [diff] [blame] | 375 | u32 cookie_len = 0; |
| 376 | |
Vlad Buslov | e0479b6 | 2018-07-09 20:26:47 +0300 | [diff] [blame] | 377 | rcu_read_lock(); |
| 378 | act_cookie = rcu_dereference(act->act_cookie); |
| 379 | |
| 380 | if (act_cookie) |
| 381 | cookie_len = nla_total_size(act_cookie->len); |
| 382 | rcu_read_unlock(); |
Roman Mashak | 4e76e75 | 2018-03-08 16:59:19 -0500 | [diff] [blame] | 383 | |
| 384 | return nla_total_size(0) /* action number nested */ |
| 385 | + nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */ |
| 386 | + cookie_len /* TCA_ACT_COOKIE */ |
Jakub Kicinski | 0dfb2d8 | 2020-03-19 16:26:23 -0700 | [diff] [blame] | 387 | + nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_HW_STATS */ |
Roman Mashak | 4e76e75 | 2018-03-08 16:59:19 -0500 | [diff] [blame] | 388 | + nla_total_size(0) /* TCA_ACT_STATS nested */ |
Jiri Pirko | 1521a67 | 2020-02-25 13:54:12 +0100 | [diff] [blame] | 389 | + nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_FLAGS */ |
Roman Mashak | 4e76e75 | 2018-03-08 16:59:19 -0500 | [diff] [blame] | 390 | /* TCA_STATS_BASIC */ |
| 391 | + nla_total_size_64bit(sizeof(struct gnet_stats_basic)) |
Eric Dumazet | b33e699 | 2019-11-04 19:13:15 -0800 | [diff] [blame] | 392 | /* TCA_STATS_PKT64 */ |
| 393 | + nla_total_size_64bit(sizeof(u64)) |
Roman Mashak | 4e76e75 | 2018-03-08 16:59:19 -0500 | [diff] [blame] | 394 | /* TCA_STATS_QUEUE */ |
| 395 | + nla_total_size_64bit(sizeof(struct gnet_stats_queue)) |
| 396 | + nla_total_size(0) /* TCA_OPTIONS nested */ |
| 397 | + nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */ |
| 398 | } |
| 399 | |
| 400 | static size_t tcf_action_full_attrs_size(size_t sz) |
| 401 | { |
| 402 | return NLMSG_HDRLEN /* struct nlmsghdr */ |
| 403 | + sizeof(struct tcamsg) |
| 404 | + nla_total_size(0) /* TCA_ACT_TAB nested */ |
| 405 | + sz; |
| 406 | } |
| 407 | |
| 408 | static size_t tcf_action_fill_size(const struct tc_action *act) |
| 409 | { |
| 410 | size_t sz = tcf_action_shared_attrs_size(act); |
| 411 | |
| 412 | if (act->ops->get_fill_size) |
| 413 | return act->ops->get_fill_size(act) + sz; |
| 414 | return sz; |
| 415 | } |
| 416 | |
Vlad Buslov | 94f44f2 | 2020-11-02 22:12:43 +0200 | [diff] [blame] | 417 | static int |
| 418 | tcf_action_dump_terse(struct sk_buff *skb, struct tc_action *a, bool from_act) |
| 419 | { |
| 420 | unsigned char *b = skb_tail_pointer(skb); |
| 421 | struct tc_cookie *cookie; |
| 422 | |
| 423 | if (nla_put_string(skb, TCA_KIND, a->ops->kind)) |
| 424 | goto nla_put_failure; |
| 425 | if (tcf_action_copy_stats(skb, a, 0)) |
| 426 | goto nla_put_failure; |
| 427 | if (from_act && nla_put_u32(skb, TCA_ACT_INDEX, a->tcfa_index)) |
| 428 | goto nla_put_failure; |
| 429 | |
| 430 | rcu_read_lock(); |
| 431 | cookie = rcu_dereference(a->act_cookie); |
| 432 | if (cookie) { |
| 433 | if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) { |
| 434 | rcu_read_unlock(); |
| 435 | goto nla_put_failure; |
| 436 | } |
| 437 | } |
| 438 | rcu_read_unlock(); |
| 439 | |
| 440 | return 0; |
| 441 | |
| 442 | nla_put_failure: |
| 443 | nlmsg_trim(skb, b); |
| 444 | return -1; |
| 445 | } |
| 446 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 447 | static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 448 | struct netlink_callback *cb) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 449 | { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 450 | int err = 0, index = -1, s_i = 0, n_i = 0; |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 451 | u32 act_flags = cb->args[2]; |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 452 | unsigned long jiffy_since = cb->args[3]; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 453 | struct nlattr *nest; |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 454 | struct idr *idr = &idrinfo->action_idr; |
| 455 | struct tc_action *p; |
| 456 | unsigned long id = 1; |
Cong Wang | e33d2b7 | 2019-06-28 11:03:41 -0700 | [diff] [blame] | 457 | unsigned long tmp; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 458 | |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 459 | mutex_lock(&idrinfo->lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 460 | |
| 461 | s_i = cb->args[0]; |
| 462 | |
Cong Wang | e33d2b7 | 2019-06-28 11:03:41 -0700 | [diff] [blame] | 463 | idr_for_each_entry_ul(idr, p, tmp, id) { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 464 | index++; |
| 465 | if (index < s_i) |
| 466 | continue; |
Cong Wang | 580e427 | 2020-10-02 12:13:34 -0700 | [diff] [blame] | 467 | if (IS_ERR(p)) |
| 468 | continue; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 469 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 470 | if (jiffy_since && |
| 471 | time_after(jiffy_since, |
| 472 | (unsigned long)p->tcfa_tm.lastuse)) |
| 473 | continue; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 474 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 475 | nest = nla_nest_start_noflag(skb, n_i); |
Craig Dillabaugh | 734549e | 2018-03-26 14:58:32 -0400 | [diff] [blame] | 476 | if (!nest) { |
| 477 | index--; |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 478 | goto nla_put_failure; |
Craig Dillabaugh | 734549e | 2018-03-26 14:58:32 -0400 | [diff] [blame] | 479 | } |
Vlad Buslov | f460019 | 2020-11-24 18:40:54 +0200 | [diff] [blame] | 480 | err = (act_flags & TCA_ACT_FLAG_TERSE_DUMP) ? |
Vlad Buslov | 94f44f2 | 2020-11-02 22:12:43 +0200 | [diff] [blame] | 481 | tcf_action_dump_terse(skb, p, true) : |
| 482 | tcf_action_dump_1(skb, p, 0, 0); |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 483 | if (err < 0) { |
| 484 | index--; |
| 485 | nlmsg_trim(skb, nest); |
| 486 | goto done; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 487 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 488 | nla_nest_end(skb, nest); |
| 489 | n_i++; |
Vlad Buslov | f460019 | 2020-11-24 18:40:54 +0200 | [diff] [blame] | 490 | if (!(act_flags & TCA_ACT_FLAG_LARGE_DUMP_ON) && |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 491 | n_i >= TCA_ACT_MAX_PRIO) |
| 492 | goto done; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 493 | } |
| 494 | done: |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 495 | if (index >= 0) |
| 496 | cb->args[0] = index + 1; |
| 497 | |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 498 | mutex_unlock(&idrinfo->lock); |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 499 | if (n_i) { |
Vlad Buslov | f460019 | 2020-11-24 18:40:54 +0200 | [diff] [blame] | 500 | if (act_flags & TCA_ACT_FLAG_LARGE_DUMP_ON) |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 501 | cb->args[1] = n_i; |
| 502 | } |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 503 | return n_i; |
| 504 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 505 | nla_put_failure: |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 506 | nla_nest_cancel(skb, nest); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 507 | goto done; |
| 508 | } |
| 509 | |
Vlad Buslov | ec3ed29 | 2018-09-19 16:37:29 -0700 | [diff] [blame] | 510 | static int tcf_idr_release_unsafe(struct tc_action *p) |
| 511 | { |
| 512 | if (atomic_read(&p->tcfa_bindcnt) > 0) |
| 513 | return -EPERM; |
| 514 | |
| 515 | if (refcount_dec_and_test(&p->tcfa_refcnt)) { |
| 516 | idr_remove(&p->idrinfo->action_idr, p->tcfa_index); |
| 517 | tcf_action_cleanup(p); |
| 518 | return ACT_P_DELETED; |
| 519 | } |
| 520 | |
| 521 | return 0; |
| 522 | } |
| 523 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 524 | static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 525 | const struct tc_action_ops *ops) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 526 | { |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 527 | struct nlattr *nest; |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 528 | int n_i = 0; |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 529 | int ret = -EINVAL; |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 530 | struct idr *idr = &idrinfo->action_idr; |
| 531 | struct tc_action *p; |
| 532 | unsigned long id = 1; |
Cong Wang | e33d2b7 | 2019-06-28 11:03:41 -0700 | [diff] [blame] | 533 | unsigned long tmp; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 534 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 535 | nest = nla_nest_start_noflag(skb, 0); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 536 | if (nest == NULL) |
| 537 | goto nla_put_failure; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 538 | if (nla_put_string(skb, TCA_KIND, ops->kind)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 539 | goto nla_put_failure; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 540 | |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 541 | mutex_lock(&idrinfo->lock); |
Cong Wang | e33d2b7 | 2019-06-28 11:03:41 -0700 | [diff] [blame] | 542 | idr_for_each_entry_ul(idr, p, tmp, id) { |
Cong Wang | 0fedc63 | 2020-09-22 20:56:24 -0700 | [diff] [blame] | 543 | if (IS_ERR(p)) |
| 544 | continue; |
Vlad Buslov | ec3ed29 | 2018-09-19 16:37:29 -0700 | [diff] [blame] | 545 | ret = tcf_idr_release_unsafe(p); |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 546 | if (ret == ACT_P_DELETED) { |
Jiri Pirko | 255cd50 | 2017-09-13 17:32:37 +0200 | [diff] [blame] | 547 | module_put(ops->owner); |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 548 | n_i++; |
| 549 | } else if (ret < 0) { |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 550 | mutex_unlock(&idrinfo->lock); |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 551 | goto nla_put_failure; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 552 | } |
| 553 | } |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 554 | mutex_unlock(&idrinfo->lock); |
Vlad Buslov | ec3ed29 | 2018-09-19 16:37:29 -0700 | [diff] [blame] | 555 | |
Yang Yingliang | 55d96f7 | 2021-06-17 16:02:07 +0800 | [diff] [blame] | 556 | ret = nla_put_u32(skb, TCA_FCNT, n_i); |
| 557 | if (ret) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 558 | goto nla_put_failure; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 559 | nla_nest_end(skb, nest); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 560 | |
| 561 | return n_i; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 562 | nla_put_failure: |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 563 | nla_nest_cancel(skb, nest); |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 564 | return ret; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 565 | } |
| 566 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 567 | int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb, |
| 568 | struct netlink_callback *cb, int type, |
Alexander Aring | b362014 | 2018-02-15 10:54:59 -0500 | [diff] [blame] | 569 | const struct tc_action_ops *ops, |
| 570 | struct netlink_ext_ack *extack) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 571 | { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 572 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 573 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 574 | if (type == RTM_DELACTION) { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 575 | return tcf_del_walker(idrinfo, skb, ops); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 576 | } else if (type == RTM_GETACTION) { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 577 | return tcf_dump_walker(idrinfo, skb, cb); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 578 | } else { |
Alexander Aring | b362014 | 2018-02-15 10:54:59 -0500 | [diff] [blame] | 579 | WARN(1, "tcf_generic_walker: unknown command %d\n", type); |
| 580 | NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command"); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 581 | return -EINVAL; |
| 582 | } |
| 583 | } |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 584 | EXPORT_SYMBOL(tcf_generic_walker); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 585 | |
Cong Wang | 7d485c4 | 2018-08-19 12:22:08 -0700 | [diff] [blame] | 586 | int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 587 | { |
Vlad Buslov | 3f7c72b | 2018-07-05 17:24:26 +0300 | [diff] [blame] | 588 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
| 589 | struct tc_action *p; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 590 | |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 591 | mutex_lock(&idrinfo->lock); |
Matthew Wilcox | 322d884 | 2017-11-28 10:01:24 -0500 | [diff] [blame] | 592 | p = idr_find(&idrinfo->action_idr, index); |
Cong Wang | 7d485c4 | 2018-08-19 12:22:08 -0700 | [diff] [blame] | 593 | if (IS_ERR(p)) |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 594 | p = NULL; |
Cong Wang | 7d485c4 | 2018-08-19 12:22:08 -0700 | [diff] [blame] | 595 | else if (p) |
Vlad Buslov | 3f7c72b | 2018-07-05 17:24:26 +0300 | [diff] [blame] | 596 | refcount_inc(&p->tcfa_refcnt); |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 597 | mutex_unlock(&idrinfo->lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 598 | |
Vlad Buslov | 3f7c72b | 2018-07-05 17:24:26 +0300 | [diff] [blame] | 599 | if (p) { |
| 600 | *a = p; |
| 601 | return true; |
| 602 | } |
| 603 | return false; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 604 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 605 | EXPORT_SYMBOL(tcf_idr_search); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 606 | |
Cong Wang | 97a3f84f | 2018-08-19 12:22:06 -0700 | [diff] [blame] | 607 | static int tcf_idr_delete_index(struct tcf_idrinfo *idrinfo, u32 index) |
Vlad Buslov | 2a2ea34 | 2018-07-05 17:24:27 +0300 | [diff] [blame] | 608 | { |
Vlad Buslov | 2a2ea34 | 2018-07-05 17:24:27 +0300 | [diff] [blame] | 609 | struct tc_action *p; |
| 610 | int ret = 0; |
| 611 | |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 612 | mutex_lock(&idrinfo->lock); |
Vlad Buslov | 2a2ea34 | 2018-07-05 17:24:27 +0300 | [diff] [blame] | 613 | p = idr_find(&idrinfo->action_idr, index); |
| 614 | if (!p) { |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 615 | mutex_unlock(&idrinfo->lock); |
Vlad Buslov | 2a2ea34 | 2018-07-05 17:24:27 +0300 | [diff] [blame] | 616 | return -ENOENT; |
| 617 | } |
| 618 | |
| 619 | if (!atomic_read(&p->tcfa_bindcnt)) { |
| 620 | if (refcount_dec_and_test(&p->tcfa_refcnt)) { |
| 621 | struct module *owner = p->ops->owner; |
| 622 | |
| 623 | WARN_ON(p != idr_remove(&idrinfo->action_idr, |
| 624 | p->tcfa_index)); |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 625 | mutex_unlock(&idrinfo->lock); |
Vlad Buslov | 2a2ea34 | 2018-07-05 17:24:27 +0300 | [diff] [blame] | 626 | |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 627 | tcf_action_cleanup(p); |
Vlad Buslov | 2a2ea34 | 2018-07-05 17:24:27 +0300 | [diff] [blame] | 628 | module_put(owner); |
| 629 | return 0; |
| 630 | } |
| 631 | ret = 0; |
| 632 | } else { |
| 633 | ret = -EPERM; |
| 634 | } |
| 635 | |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 636 | mutex_unlock(&idrinfo->lock); |
Vlad Buslov | 2a2ea34 | 2018-07-05 17:24:27 +0300 | [diff] [blame] | 637 | return ret; |
| 638 | } |
Vlad Buslov | 2a2ea34 | 2018-07-05 17:24:27 +0300 | [diff] [blame] | 639 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 640 | int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est, |
| 641 | struct tc_action **a, const struct tc_action_ops *ops, |
Vlad Buslov | e382267 | 2019-10-30 16:09:06 +0200 | [diff] [blame] | 642 | int bind, bool cpustats, u32 flags) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 643 | { |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 644 | struct tc_action *p = kzalloc(ops->size, GFP_KERNEL); |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 645 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 646 | int err = -ENOMEM; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 647 | |
| 648 | if (unlikely(!p)) |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 649 | return -ENOMEM; |
Vlad Buslov | 036bb44 | 2018-07-05 17:24:24 +0300 | [diff] [blame] | 650 | refcount_set(&p->tcfa_refcnt, 1); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 651 | if (bind) |
Vlad Buslov | 036bb44 | 2018-07-05 17:24:24 +0300 | [diff] [blame] | 652 | atomic_set(&p->tcfa_bindcnt, 1); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 653 | |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 654 | if (cpustats) { |
Ahmed S. Darwish | 50dc9a8 | 2021-10-16 10:49:09 +0200 | [diff] [blame] | 655 | p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync); |
Matthew Wilcox | 339913a | 2017-11-28 10:28:15 -0500 | [diff] [blame] | 656 | if (!p->cpu_bstats) |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 657 | goto err1; |
Ahmed S. Darwish | 50dc9a8 | 2021-10-16 10:49:09 +0200 | [diff] [blame] | 658 | p->cpu_bstats_hw = netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync); |
Eelco Chaudron | 28169ab | 2018-09-21 07:14:02 -0400 | [diff] [blame] | 659 | if (!p->cpu_bstats_hw) |
| 660 | goto err2; |
Matthew Wilcox | 339913a | 2017-11-28 10:28:15 -0500 | [diff] [blame] | 661 | p->cpu_qstats = alloc_percpu(struct gnet_stats_queue); |
| 662 | if (!p->cpu_qstats) |
Eelco Chaudron | 28169ab | 2018-09-21 07:14:02 -0400 | [diff] [blame] | 663 | goto err3; |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 664 | } |
Ahmed S. Darwish | 50dc9a8 | 2021-10-16 10:49:09 +0200 | [diff] [blame] | 665 | gnet_stats_basic_sync_init(&p->tcfa_bstats); |
| 666 | gnet_stats_basic_sync_init(&p->tcfa_bstats_hw); |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 667 | spin_lock_init(&p->tcfa_lock); |
Matthew Wilcox | 339913a | 2017-11-28 10:28:15 -0500 | [diff] [blame] | 668 | p->tcfa_index = index; |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 669 | p->tcfa_tm.install = jiffies; |
| 670 | p->tcfa_tm.lastuse = jiffies; |
| 671 | p->tcfa_tm.firstuse = 0; |
Baowen Zheng | e8cb5bc | 2021-12-17 19:16:26 +0100 | [diff] [blame^] | 672 | p->tcfa_flags = flags; |
Stephen Hemminger | 0e991ec | 2008-11-25 21:12:32 -0800 | [diff] [blame] | 673 | if (est) { |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 674 | err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats, |
| 675 | &p->tcfa_rate_est, |
Ahmed S. Darwish | 29cbcd8 | 2021-10-16 10:49:10 +0200 | [diff] [blame] | 676 | &p->tcfa_lock, false, est); |
Matthew Wilcox | 339913a | 2017-11-28 10:28:15 -0500 | [diff] [blame] | 677 | if (err) |
Eelco Chaudron | 28169ab | 2018-09-21 07:14:02 -0400 | [diff] [blame] | 678 | goto err4; |
Stephen Hemminger | 0e991ec | 2008-11-25 21:12:32 -0800 | [diff] [blame] | 679 | } |
| 680 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 681 | p->idrinfo = idrinfo; |
Vlad Buslov | b3650bf7 | 2021-04-07 18:36:04 +0300 | [diff] [blame] | 682 | __module_get(ops->owner); |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 683 | p->ops = ops; |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 684 | *a = p; |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 685 | return 0; |
Eelco Chaudron | 28169ab | 2018-09-21 07:14:02 -0400 | [diff] [blame] | 686 | err4: |
Matthew Wilcox | 339913a | 2017-11-28 10:28:15 -0500 | [diff] [blame] | 687 | free_percpu(p->cpu_qstats); |
Eelco Chaudron | 28169ab | 2018-09-21 07:14:02 -0400 | [diff] [blame] | 688 | err3: |
| 689 | free_percpu(p->cpu_bstats_hw); |
Matthew Wilcox | 339913a | 2017-11-28 10:28:15 -0500 | [diff] [blame] | 690 | err2: |
| 691 | free_percpu(p->cpu_bstats); |
| 692 | err1: |
| 693 | kfree(p); |
| 694 | return err; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 695 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 696 | EXPORT_SYMBOL(tcf_idr_create); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 697 | |
Vlad Buslov | e382267 | 2019-10-30 16:09:06 +0200 | [diff] [blame] | 698 | int tcf_idr_create_from_flags(struct tc_action_net *tn, u32 index, |
| 699 | struct nlattr *est, struct tc_action **a, |
| 700 | const struct tc_action_ops *ops, int bind, |
| 701 | u32 flags) |
| 702 | { |
| 703 | /* Set cpustats according to actions flags. */ |
| 704 | return tcf_idr_create(tn, index, est, a, ops, bind, |
| 705 | !(flags & TCA_ACT_FLAGS_NO_PERCPU_STATS), flags); |
| 706 | } |
| 707 | EXPORT_SYMBOL(tcf_idr_create_from_flags); |
| 708 | |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 709 | /* Cleanup idr index that was allocated but not initialized. */ |
| 710 | |
| 711 | void tcf_idr_cleanup(struct tc_action_net *tn, u32 index) |
| 712 | { |
| 713 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
| 714 | |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 715 | mutex_lock(&idrinfo->lock); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 716 | /* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */ |
| 717 | WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index))); |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 718 | mutex_unlock(&idrinfo->lock); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 719 | } |
| 720 | EXPORT_SYMBOL(tcf_idr_cleanup); |
| 721 | |
| 722 | /* Check if action with specified index exists. If actions is found, increments |
| 723 | * its reference and bind counters, and return 1. Otherwise insert temporary |
| 724 | * error pointer (to prevent concurrent users from inserting actions with same |
| 725 | * index) and return 0. |
| 726 | */ |
| 727 | |
| 728 | int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index, |
| 729 | struct tc_action **a, int bind) |
| 730 | { |
| 731 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
| 732 | struct tc_action *p; |
| 733 | int ret; |
| 734 | |
| 735 | again: |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 736 | mutex_lock(&idrinfo->lock); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 737 | if (*index) { |
| 738 | p = idr_find(&idrinfo->action_idr, *index); |
| 739 | if (IS_ERR(p)) { |
| 740 | /* This means that another process allocated |
| 741 | * index but did not assign the pointer yet. |
| 742 | */ |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 743 | mutex_unlock(&idrinfo->lock); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 744 | goto again; |
| 745 | } |
| 746 | |
| 747 | if (p) { |
| 748 | refcount_inc(&p->tcfa_refcnt); |
| 749 | if (bind) |
| 750 | atomic_inc(&p->tcfa_bindcnt); |
| 751 | *a = p; |
| 752 | ret = 1; |
| 753 | } else { |
| 754 | *a = NULL; |
| 755 | ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index, |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 756 | *index, GFP_KERNEL); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 757 | if (!ret) |
| 758 | idr_replace(&idrinfo->action_idr, |
| 759 | ERR_PTR(-EBUSY), *index); |
| 760 | } |
| 761 | } else { |
| 762 | *index = 1; |
| 763 | *a = NULL; |
| 764 | ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index, |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 765 | UINT_MAX, GFP_KERNEL); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 766 | if (!ret) |
| 767 | idr_replace(&idrinfo->action_idr, ERR_PTR(-EBUSY), |
| 768 | *index); |
| 769 | } |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 770 | mutex_unlock(&idrinfo->lock); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 771 | return ret; |
| 772 | } |
| 773 | EXPORT_SYMBOL(tcf_idr_check_alloc); |
| 774 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 775 | void tcf_idrinfo_destroy(const struct tc_action_ops *ops, |
| 776 | struct tcf_idrinfo *idrinfo) |
WANG Cong | 1d4150c | 2016-02-22 15:57:52 -0800 | [diff] [blame] | 777 | { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 778 | struct idr *idr = &idrinfo->action_idr; |
| 779 | struct tc_action *p; |
| 780 | int ret; |
| 781 | unsigned long id = 1; |
Cong Wang | e33d2b7 | 2019-06-28 11:03:41 -0700 | [diff] [blame] | 782 | unsigned long tmp; |
WANG Cong | 1d4150c | 2016-02-22 15:57:52 -0800 | [diff] [blame] | 783 | |
Cong Wang | e33d2b7 | 2019-06-28 11:03:41 -0700 | [diff] [blame] | 784 | idr_for_each_entry_ul(idr, p, tmp, id) { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 785 | ret = __tcf_idr_release(p, false, true); |
| 786 | if (ret == ACT_P_DELETED) |
| 787 | module_put(ops->owner); |
| 788 | else if (ret < 0) |
| 789 | return; |
WANG Cong | 1d4150c | 2016-02-22 15:57:52 -0800 | [diff] [blame] | 790 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 791 | idr_destroy(&idrinfo->action_idr); |
WANG Cong | 1d4150c | 2016-02-22 15:57:52 -0800 | [diff] [blame] | 792 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 793 | EXPORT_SYMBOL(tcf_idrinfo_destroy); |
WANG Cong | 1d4150c | 2016-02-22 15:57:52 -0800 | [diff] [blame] | 794 | |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 795 | static LIST_HEAD(act_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | static DEFINE_RWLOCK(act_mod_lock); |
| 797 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 798 | int tcf_register_action(struct tc_action_ops *act, |
| 799 | struct pernet_operations *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | { |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 801 | struct tc_action_ops *a; |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 802 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 804 | if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup) |
Jamal Hadi Salim | 76c82d7 | 2013-12-04 09:26:52 -0500 | [diff] [blame] | 805 | return -EINVAL; |
| 806 | |
WANG Cong | ab102b8 | 2016-10-11 10:56:45 -0700 | [diff] [blame] | 807 | /* We have to register pernet ops before making the action ops visible, |
| 808 | * otherwise tcf_action_init_1() could get a partially initialized |
| 809 | * netns. |
| 810 | */ |
| 811 | ret = register_pernet_subsys(ops); |
| 812 | if (ret) |
| 813 | return ret; |
| 814 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | write_lock(&act_mod_lock); |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 816 | list_for_each_entry(a, &act_base, head) { |
Eli Cohen | eddd2cf | 2019-02-10 14:25:00 +0200 | [diff] [blame] | 817 | if (act->id == a->id || (strcmp(act->kind, a->kind) == 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | write_unlock(&act_mod_lock); |
WANG Cong | ab102b8 | 2016-10-11 10:56:45 -0700 | [diff] [blame] | 819 | unregister_pernet_subsys(ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | return -EEXIST; |
| 821 | } |
| 822 | } |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 823 | list_add_tail(&act->head, &act_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | write_unlock(&act_mod_lock); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 825 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | return 0; |
| 827 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 828 | EXPORT_SYMBOL(tcf_register_action); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 830 | int tcf_unregister_action(struct tc_action_ops *act, |
| 831 | struct pernet_operations *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | { |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 833 | struct tc_action_ops *a; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | int err = -ENOENT; |
| 835 | |
| 836 | write_lock(&act_mod_lock); |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 837 | list_for_each_entry(a, &act_base, head) { |
| 838 | if (a == act) { |
| 839 | list_del(&act->head); |
| 840 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | break; |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 842 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | } |
| 844 | write_unlock(&act_mod_lock); |
WANG Cong | ab102b8 | 2016-10-11 10:56:45 -0700 | [diff] [blame] | 845 | if (!err) |
| 846 | unregister_pernet_subsys(ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | return err; |
| 848 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 849 | EXPORT_SYMBOL(tcf_unregister_action); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | |
| 851 | /* lookup by name */ |
| 852 | static struct tc_action_ops *tc_lookup_action_n(char *kind) |
| 853 | { |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 854 | struct tc_action_ops *a, *res = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | |
| 856 | if (kind) { |
| 857 | read_lock(&act_mod_lock); |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 858 | list_for_each_entry(a, &act_base, head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | if (strcmp(kind, a->kind) == 0) { |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 860 | if (try_module_get(a->owner)) |
| 861 | res = a; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | break; |
| 863 | } |
| 864 | } |
| 865 | read_unlock(&act_mod_lock); |
| 866 | } |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 867 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | } |
| 869 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 870 | /* lookup by nlattr */ |
| 871 | static struct tc_action_ops *tc_lookup_action(struct nlattr *kind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | { |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 873 | struct tc_action_ops *a, *res = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | |
| 875 | if (kind) { |
| 876 | read_lock(&act_mod_lock); |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 877 | list_for_each_entry(a, &act_base, head) { |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 878 | if (nla_strcmp(kind, a->kind) == 0) { |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 879 | if (try_module_get(a->owner)) |
| 880 | res = a; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | break; |
| 882 | } |
| 883 | } |
| 884 | read_unlock(&act_mod_lock); |
| 885 | } |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 886 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | } |
| 888 | |
Menglong Dong | e5a4b17 | 2020-11-09 02:02:17 -0500 | [diff] [blame] | 889 | /*TCA_ACT_MAX_PRIO is 32, there count up to 32 */ |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 890 | #define TCA_ACT_MAX_PRIO_MASK 0x1FF |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 891 | int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions, |
| 892 | int nr_actions, struct tcf_result *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | { |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 894 | u32 jmp_prgcnt = 0; |
| 895 | u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */ |
Jiri Pirko | ec1a9cc | 2017-08-04 14:29:02 +0200 | [diff] [blame] | 896 | int i; |
| 897 | int ret = TC_ACT_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | |
Willem de Bruijn | e7246e1 | 2017-01-07 17:06:35 -0500 | [diff] [blame] | 899 | if (skb_skip_tc_classify(skb)) |
| 900 | return TC_ACT_OK; |
| 901 | |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 902 | restart_act_graph: |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 903 | for (i = 0; i < nr_actions; i++) { |
| 904 | const struct tc_action *a = actions[i]; |
| 905 | |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 906 | if (jmp_prgcnt > 0) { |
| 907 | jmp_prgcnt -= 1; |
| 908 | continue; |
| 909 | } |
Baowen Zheng | 7adc576 | 2021-12-17 19:16:23 +0100 | [diff] [blame] | 910 | |
| 911 | if (tc_act_skip_sw(a->tcfa_flags)) |
| 912 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | repeat: |
Jamal Hadi Salim | 63acd68 | 2013-12-23 08:02:12 -0500 | [diff] [blame] | 914 | ret = a->ops->act(skb, a, res); |
Jamal Hadi Salim | 63acd68 | 2013-12-23 08:02:12 -0500 | [diff] [blame] | 915 | if (ret == TC_ACT_REPEAT) |
| 916 | goto repeat; /* we need a ttl - JHS */ |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 917 | |
Jiri Pirko | 9da3242 | 2017-05-02 10:12:00 +0200 | [diff] [blame] | 918 | if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) { |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 919 | jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK; |
| 920 | if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) { |
| 921 | /* faulty opcode, stop pipeline */ |
| 922 | return TC_ACT_OK; |
| 923 | } else { |
| 924 | jmp_ttl -= 1; |
| 925 | if (jmp_ttl > 0) |
| 926 | goto restart_act_graph; |
| 927 | else /* faulty graph, stop pipeline */ |
| 928 | return TC_ACT_OK; |
| 929 | } |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 930 | } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) { |
Davide Caratti | ee3bbfe | 2019-03-20 15:00:16 +0100 | [diff] [blame] | 931 | if (unlikely(!rcu_access_pointer(a->goto_chain))) { |
| 932 | net_warn_ratelimited("can't go to NULL chain!\n"); |
| 933 | return TC_ACT_SHOT; |
| 934 | } |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 935 | tcf_action_goto_chain_exec(a, res); |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 936 | } |
| 937 | |
Jamal Hadi Salim | 63acd68 | 2013-12-23 08:02:12 -0500 | [diff] [blame] | 938 | if (ret != TC_ACT_PIPE) |
Willem de Bruijn | e7246e1 | 2017-01-07 17:06:35 -0500 | [diff] [blame] | 939 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | } |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | return ret; |
| 943 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 944 | EXPORT_SYMBOL(tcf_action_exec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 946 | int tcf_action_destroy(struct tc_action *actions[], int bind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | { |
Jiri Pirko | 255cd50 | 2017-09-13 17:32:37 +0200 | [diff] [blame] | 948 | const struct tc_action_ops *ops; |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 949 | struct tc_action *a; |
| 950 | int ret = 0, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 952 | for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) { |
| 953 | a = actions[i]; |
| 954 | actions[i] = NULL; |
Jiri Pirko | 255cd50 | 2017-09-13 17:32:37 +0200 | [diff] [blame] | 955 | ops = a->ops; |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 956 | ret = __tcf_idr_release(a, bind, true); |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 957 | if (ret == ACT_P_DELETED) |
Jiri Pirko | 255cd50 | 2017-09-13 17:32:37 +0200 | [diff] [blame] | 958 | module_put(ops->owner); |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 959 | else if (ret < 0) |
| 960 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | } |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 962 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | } |
| 964 | |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 965 | static int tcf_action_put(struct tc_action *p) |
| 966 | { |
| 967 | return __tcf_action_put(p, false); |
| 968 | } |
| 969 | |
Cong Wang | edfaf94 | 2018-08-19 12:22:05 -0700 | [diff] [blame] | 970 | /* Put all actions in this array, skip those NULL's. */ |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 971 | static void tcf_action_put_many(struct tc_action *actions[]) |
Vlad Buslov | cae422f | 2018-07-05 17:24:31 +0300 | [diff] [blame] | 972 | { |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 973 | int i; |
Vlad Buslov | cae422f | 2018-07-05 17:24:31 +0300 | [diff] [blame] | 974 | |
Cong Wang | edfaf94 | 2018-08-19 12:22:05 -0700 | [diff] [blame] | 975 | for (i = 0; i < TCA_ACT_MAX_PRIO; i++) { |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 976 | struct tc_action *a = actions[i]; |
Cong Wang | edfaf94 | 2018-08-19 12:22:05 -0700 | [diff] [blame] | 977 | const struct tc_action_ops *ops; |
Vlad Buslov | cae422f | 2018-07-05 17:24:31 +0300 | [diff] [blame] | 978 | |
Cong Wang | edfaf94 | 2018-08-19 12:22:05 -0700 | [diff] [blame] | 979 | if (!a) |
| 980 | continue; |
| 981 | ops = a->ops; |
Vlad Buslov | cae422f | 2018-07-05 17:24:31 +0300 | [diff] [blame] | 982 | if (tcf_action_put(a)) |
| 983 | module_put(ops->owner); |
| 984 | } |
| 985 | } |
| 986 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | int |
| 988 | tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref) |
| 989 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | return a->ops->dump(skb, a, bind, ref); |
| 991 | } |
| 992 | |
Vlad Buslov | ca44b73 | 2020-05-15 14:40:12 +0300 | [diff] [blame] | 993 | int |
| 994 | tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref) |
| 995 | { |
| 996 | int err = -EINVAL; |
| 997 | unsigned char *b = skb_tail_pointer(skb); |
| 998 | struct nlattr *nest; |
Baowen Zheng | e8cb5bc | 2021-12-17 19:16:26 +0100 | [diff] [blame^] | 999 | u32 flags; |
Vlad Buslov | ca44b73 | 2020-05-15 14:40:12 +0300 | [diff] [blame] | 1000 | |
Vlad Buslov | 94f44f2 | 2020-11-02 22:12:43 +0200 | [diff] [blame] | 1001 | if (tcf_action_dump_terse(skb, a, false)) |
Vlad Buslov | ca44b73 | 2020-05-15 14:40:12 +0300 | [diff] [blame] | 1002 | goto nla_put_failure; |
| 1003 | |
Jiri Pirko | 8953b07 | 2020-03-28 16:37:42 +0100 | [diff] [blame] | 1004 | if (a->hw_stats != TCA_ACT_HW_STATS_ANY && |
| 1005 | nla_put_bitfield32(skb, TCA_ACT_HW_STATS, |
| 1006 | a->hw_stats, TCA_ACT_HW_STATS_ANY)) |
| 1007 | goto nla_put_failure; |
Jiri Pirko | 44f8658 | 2020-03-07 12:40:20 +0100 | [diff] [blame] | 1008 | |
Jiri Pirko | 93a129e | 2020-03-28 16:37:43 +0100 | [diff] [blame] | 1009 | if (a->used_hw_stats_valid && |
| 1010 | nla_put_bitfield32(skb, TCA_ACT_USED_HW_STATS, |
| 1011 | a->used_hw_stats, TCA_ACT_HW_STATS_ANY)) |
| 1012 | goto nla_put_failure; |
| 1013 | |
Baowen Zheng | e8cb5bc | 2021-12-17 19:16:26 +0100 | [diff] [blame^] | 1014 | flags = a->tcfa_flags & TCA_ACT_FLAGS_USER_MASK; |
| 1015 | if (flags && |
Jiri Pirko | 8953b07 | 2020-03-28 16:37:42 +0100 | [diff] [blame] | 1016 | nla_put_bitfield32(skb, TCA_ACT_FLAGS, |
Baowen Zheng | e8cb5bc | 2021-12-17 19:16:26 +0100 | [diff] [blame^] | 1017 | flags, flags)) |
Jiri Pirko | 8953b07 | 2020-03-28 16:37:42 +0100 | [diff] [blame] | 1018 | goto nla_put_failure; |
Vlad Buslov | e382267 | 2019-10-30 16:09:06 +0200 | [diff] [blame] | 1019 | |
Baowen Zheng | 7adc576 | 2021-12-17 19:16:23 +0100 | [diff] [blame] | 1020 | if (nla_put_u32(skb, TCA_ACT_IN_HW_COUNT, a->in_hw_count)) |
| 1021 | goto nla_put_failure; |
| 1022 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1023 | nest = nla_nest_start_noflag(skb, TCA_OPTIONS); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1024 | if (nest == NULL) |
| 1025 | goto nla_put_failure; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1026 | err = tcf_action_dump_old(skb, a, bind, ref); |
| 1027 | if (err > 0) { |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1028 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | return err; |
| 1030 | } |
| 1031 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1032 | nla_put_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 1033 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | return -1; |
| 1035 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 1036 | EXPORT_SYMBOL(tcf_action_dump_1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1038 | int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[], |
Vlad Buslov | ca44b73 | 2020-05-15 14:40:12 +0300 | [diff] [blame] | 1039 | int bind, int ref, bool terse) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | { |
| 1041 | struct tc_action *a; |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1042 | int err = -EINVAL, i; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1043 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1045 | for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) { |
| 1046 | a = actions[i]; |
Vlad Buslov | 4097e9d2 | 2019-05-23 09:32:31 +0300 | [diff] [blame] | 1047 | nest = nla_nest_start_noflag(skb, i + 1); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1048 | if (nest == NULL) |
| 1049 | goto nla_put_failure; |
Vlad Buslov | 94f44f2 | 2020-11-02 22:12:43 +0200 | [diff] [blame] | 1050 | err = terse ? tcf_action_dump_terse(skb, a, false) : |
Vlad Buslov | ca44b73 | 2020-05-15 14:40:12 +0300 | [diff] [blame] | 1051 | tcf_action_dump_1(skb, a, bind, ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | if (err < 0) |
Thomas Graf | 4fe683f | 2006-07-05 20:47:28 -0700 | [diff] [blame] | 1053 | goto errout; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1054 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | return 0; |
| 1058 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1059 | nla_put_failure: |
Thomas Graf | 4fe683f | 2006-07-05 20:47:28 -0700 | [diff] [blame] | 1060 | err = -EINVAL; |
| 1061 | errout: |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1062 | nla_nest_cancel(skb, nest); |
Thomas Graf | 4fe683f | 2006-07-05 20:47:28 -0700 | [diff] [blame] | 1063 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 1066 | static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb) |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 1067 | { |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 1068 | struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL); |
| 1069 | if (!c) |
| 1070 | return NULL; |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 1071 | |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 1072 | c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL); |
| 1073 | if (!c->data) { |
| 1074 | kfree(c); |
| 1075 | return NULL; |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 1076 | } |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 1077 | c->len = nla_len(tb[TCA_ACT_COOKIE]); |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 1078 | |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 1079 | return c; |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 1080 | } |
| 1081 | |
Jakub Kicinski | 0dfb2d8 | 2020-03-19 16:26:23 -0700 | [diff] [blame] | 1082 | static u8 tcf_action_hw_stats_get(struct nlattr *hw_stats_attr) |
Jiri Pirko | 44f8658 | 2020-03-07 12:40:20 +0100 | [diff] [blame] | 1083 | { |
Jakub Kicinski | 0dfb2d8 | 2020-03-19 16:26:23 -0700 | [diff] [blame] | 1084 | struct nla_bitfield32 hw_stats_bf; |
Jiri Pirko | 44f8658 | 2020-03-07 12:40:20 +0100 | [diff] [blame] | 1085 | |
| 1086 | /* If the user did not pass the attr, that means he does |
| 1087 | * not care about the type. Return "any" in that case |
| 1088 | * which is setting on all supported types. |
| 1089 | */ |
Jakub Kicinski | 0dfb2d8 | 2020-03-19 16:26:23 -0700 | [diff] [blame] | 1090 | if (!hw_stats_attr) |
| 1091 | return TCA_ACT_HW_STATS_ANY; |
| 1092 | hw_stats_bf = nla_get_bitfield32(hw_stats_attr); |
| 1093 | return hw_stats_bf.value; |
Jiri Pirko | 44f8658 | 2020-03-07 12:40:20 +0100 | [diff] [blame] | 1094 | } |
| 1095 | |
Cong Wang | 199ce85 | 2019-09-18 18:44:43 -0700 | [diff] [blame] | 1096 | static const struct nla_policy tcf_action_policy[TCA_ACT_MAX + 1] = { |
Cong Wang | 4b793fe | 2019-10-07 13:26:29 -0700 | [diff] [blame] | 1097 | [TCA_ACT_KIND] = { .type = NLA_STRING }, |
Cong Wang | 199ce85 | 2019-09-18 18:44:43 -0700 | [diff] [blame] | 1098 | [TCA_ACT_INDEX] = { .type = NLA_U32 }, |
| 1099 | [TCA_ACT_COOKIE] = { .type = NLA_BINARY, |
| 1100 | .len = TC_COOKIE_MAX_SIZE }, |
| 1101 | [TCA_ACT_OPTIONS] = { .type = NLA_NESTED }, |
Baowen Zheng | 7adc576 | 2021-12-17 19:16:23 +0100 | [diff] [blame] | 1102 | [TCA_ACT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_ACT_FLAGS_NO_PERCPU_STATS | |
| 1103 | TCA_ACT_FLAGS_SKIP_HW | |
| 1104 | TCA_ACT_FLAGS_SKIP_SW), |
Johannes Berg | 47a1494 | 2020-04-30 22:13:05 +0200 | [diff] [blame] | 1105 | [TCA_ACT_HW_STATS] = NLA_POLICY_BITFIELD32(TCA_ACT_HW_STATS_ANY), |
Cong Wang | 199ce85 | 2019-09-18 18:44:43 -0700 | [diff] [blame] | 1106 | }; |
| 1107 | |
Vlad Buslov | 396d7f2 | 2021-02-16 18:22:00 +0200 | [diff] [blame] | 1108 | void tcf_idr_insert_many(struct tc_action *actions[]) |
Cong Wang | e49d8c2 | 2020-09-22 20:56:23 -0700 | [diff] [blame] | 1109 | { |
Cong Wang | 0fedc63 | 2020-09-22 20:56:24 -0700 | [diff] [blame] | 1110 | int i; |
Cong Wang | e49d8c2 | 2020-09-22 20:56:23 -0700 | [diff] [blame] | 1111 | |
Cong Wang | 0fedc63 | 2020-09-22 20:56:24 -0700 | [diff] [blame] | 1112 | for (i = 0; i < TCA_ACT_MAX_PRIO; i++) { |
| 1113 | struct tc_action *a = actions[i]; |
| 1114 | struct tcf_idrinfo *idrinfo; |
| 1115 | |
| 1116 | if (!a) |
| 1117 | continue; |
| 1118 | idrinfo = a->idrinfo; |
| 1119 | mutex_lock(&idrinfo->lock); |
| 1120 | /* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc if |
| 1121 | * it is just created, otherwise this is just a nop. |
| 1122 | */ |
| 1123 | idr_replace(&idrinfo->action_idr, a, a->tcfa_index); |
| 1124 | mutex_unlock(&idrinfo->lock); |
| 1125 | } |
Cong Wang | e49d8c2 | 2020-09-22 20:56:23 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1128 | struct tc_action_ops *tc_action_load_ops(struct nlattr *nla, bool police, |
Cong Wang | d349f99 | 2021-01-16 16:56:57 -0800 | [diff] [blame] | 1129 | bool rtnl_held, |
| 1130 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1132 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
Cong Wang | d349f99 | 2021-01-16 16:56:57 -0800 | [diff] [blame] | 1133 | struct tc_action_ops *a_o; |
| 1134 | char act_name[IFNAMSIZ]; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1135 | struct nlattr *kind; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1136 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1138 | if (!police) { |
Cong Wang | 199ce85 | 2019-09-18 18:44:43 -0700 | [diff] [blame] | 1139 | err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla, |
| 1140 | tcf_action_policy, extack); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 1141 | if (err < 0) |
Cong Wang | d349f99 | 2021-01-16 16:56:57 -0800 | [diff] [blame] | 1142 | return ERR_PTR(err); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 1143 | err = -EINVAL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1144 | kind = tb[TCA_ACT_KIND]; |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1145 | if (!kind) { |
| 1146 | NL_SET_ERR_MSG(extack, "TC action kind must be specified"); |
Cong Wang | d349f99 | 2021-01-16 16:56:57 -0800 | [diff] [blame] | 1147 | return ERR_PTR(err); |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1148 | } |
Francis Laniel | 872f690 | 2020-11-15 18:08:06 +0100 | [diff] [blame] | 1149 | if (nla_strscpy(act_name, kind, IFNAMSIZ) < 0) { |
Cong Wang | 4b793fe | 2019-10-07 13:26:29 -0700 | [diff] [blame] | 1150 | NL_SET_ERR_MSG(extack, "TC action name too long"); |
Cong Wang | d349f99 | 2021-01-16 16:56:57 -0800 | [diff] [blame] | 1151 | return ERR_PTR(err); |
Cong Wang | 4b793fe | 2019-10-07 13:26:29 -0700 | [diff] [blame] | 1152 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | } else { |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1154 | if (strlcpy(act_name, "police", IFNAMSIZ) >= IFNAMSIZ) { |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1155 | NL_SET_ERR_MSG(extack, "TC action name too long"); |
Cong Wang | d349f99 | 2021-01-16 16:56:57 -0800 | [diff] [blame] | 1156 | return ERR_PTR(-EINVAL); |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1157 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | a_o = tc_lookup_action_n(act_name); |
| 1161 | if (a_o == NULL) { |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 1162 | #ifdef CONFIG_MODULES |
Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 1163 | if (rtnl_held) |
| 1164 | rtnl_unlock(); |
Patrick McHardy | 4bba392 | 2006-01-08 22:22:14 -0800 | [diff] [blame] | 1165 | request_module("act_%s", act_name); |
Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 1166 | if (rtnl_held) |
| 1167 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | |
| 1169 | a_o = tc_lookup_action_n(act_name); |
| 1170 | |
| 1171 | /* We dropped the RTNL semaphore in order to |
| 1172 | * perform the module load. So, even if we |
| 1173 | * succeeded in loading the module we have to |
| 1174 | * tell the caller to replay the request. We |
| 1175 | * indicate this using -EAGAIN. |
| 1176 | */ |
| 1177 | if (a_o != NULL) { |
Cong Wang | d349f99 | 2021-01-16 16:56:57 -0800 | [diff] [blame] | 1178 | module_put(a_o->owner); |
| 1179 | return ERR_PTR(-EAGAIN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | } |
| 1181 | #endif |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1182 | NL_SET_ERR_MSG(extack, "Failed to load TC action module"); |
Cong Wang | d349f99 | 2021-01-16 16:56:57 -0800 | [diff] [blame] | 1183 | return ERR_PTR(-ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
Cong Wang | d349f99 | 2021-01-16 16:56:57 -0800 | [diff] [blame] | 1186 | return a_o; |
| 1187 | } |
| 1188 | |
| 1189 | struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp, |
| 1190 | struct nlattr *nla, struct nlattr *est, |
Vlad Buslov | 87c750e | 2021-04-07 18:36:03 +0300 | [diff] [blame] | 1191 | struct tc_action_ops *a_o, int *init_res, |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1192 | u32 flags, struct netlink_ext_ack *extack) |
Cong Wang | d349f99 | 2021-01-16 16:56:57 -0800 | [diff] [blame] | 1193 | { |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1194 | bool police = flags & TCA_ACT_FLAGS_POLICE; |
| 1195 | struct nla_bitfield32 userflags = { 0, 0 }; |
Cong Wang | d349f99 | 2021-01-16 16:56:57 -0800 | [diff] [blame] | 1196 | u8 hw_stats = TCA_ACT_HW_STATS_ANY; |
| 1197 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
| 1198 | struct tc_cookie *cookie = NULL; |
| 1199 | struct tc_action *a; |
| 1200 | int err; |
| 1201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | /* backward compatibility for policer */ |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1203 | if (!police) { |
Cong Wang | d349f99 | 2021-01-16 16:56:57 -0800 | [diff] [blame] | 1204 | err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla, |
| 1205 | tcf_action_policy, extack); |
| 1206 | if (err < 0) |
| 1207 | return ERR_PTR(err); |
| 1208 | if (tb[TCA_ACT_COOKIE]) { |
| 1209 | cookie = nla_memdup_cookie(tb); |
| 1210 | if (!cookie) { |
| 1211 | NL_SET_ERR_MSG(extack, "No memory to generate TC cookie"); |
| 1212 | err = -ENOMEM; |
| 1213 | goto err_out; |
| 1214 | } |
| 1215 | } |
| 1216 | hw_stats = tcf_action_hw_stats_get(tb[TCA_ACT_HW_STATS]); |
Baowen Zheng | 7adc576 | 2021-12-17 19:16:23 +0100 | [diff] [blame] | 1217 | if (tb[TCA_ACT_FLAGS]) { |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1218 | userflags = nla_get_bitfield32(tb[TCA_ACT_FLAGS]); |
Baowen Zheng | 7adc576 | 2021-12-17 19:16:23 +0100 | [diff] [blame] | 1219 | if (!tc_act_flags_valid(userflags.value)) { |
| 1220 | err = -EINVAL; |
| 1221 | goto err_out; |
| 1222 | } |
| 1223 | } |
Cong Wang | d349f99 | 2021-01-16 16:56:57 -0800 | [diff] [blame] | 1224 | |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1225 | err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, tp, |
| 1226 | userflags.value | flags, extack); |
Cong Wang | d349f99 | 2021-01-16 16:56:57 -0800 | [diff] [blame] | 1227 | } else { |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1228 | err = a_o->init(net, nla, est, &a, tp, userflags.value | flags, |
| 1229 | extack); |
Cong Wang | d349f99 | 2021-01-16 16:56:57 -0800 | [diff] [blame] | 1230 | } |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1231 | if (err < 0) |
Cong Wang | d349f99 | 2021-01-16 16:56:57 -0800 | [diff] [blame] | 1232 | goto err_out; |
Vlad Buslov | 87c750e | 2021-04-07 18:36:03 +0300 | [diff] [blame] | 1233 | *init_res = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1235 | if (!police && tb[TCA_ACT_COOKIE]) |
Vlad Buslov | eec94fd | 2018-07-05 17:24:23 +0300 | [diff] [blame] | 1236 | tcf_set_action_cookie(&a->act_cookie, cookie); |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 1237 | |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1238 | if (!police) |
Jakub Kicinski | 0dfb2d8 | 2020-03-19 16:26:23 -0700 | [diff] [blame] | 1239 | a->hw_stats = hw_stats; |
Jiri Pirko | 44f8658 | 2020-03-07 12:40:20 +0100 | [diff] [blame] | 1240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | return a; |
| 1242 | |
Cong Wang | d349f99 | 2021-01-16 16:56:57 -0800 | [diff] [blame] | 1243 | err_out: |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 1244 | if (cookie) { |
| 1245 | kfree(cookie->data); |
| 1246 | kfree(cookie); |
| 1247 | } |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1248 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | } |
| 1250 | |
Baowen Zheng | 8cbfe93 | 2021-12-17 19:16:22 +0100 | [diff] [blame] | 1251 | static bool tc_act_bind(u32 flags) |
| 1252 | { |
| 1253 | return !!(flags & TCA_ACT_FLAGS_BIND); |
| 1254 | } |
| 1255 | |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1256 | /* Returns numbers of initialized actions or negative error. */ |
| 1257 | |
Jiri Pirko | 9fb9f25 | 2017-05-17 11:08:02 +0200 | [diff] [blame] | 1258 | int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla, |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1259 | struct nlattr *est, struct tc_action *actions[], |
| 1260 | int init_res[], size_t *attr_size, u32 flags, |
| 1261 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | { |
Cong Wang | d349f99 | 2021-01-16 16:56:57 -0800 | [diff] [blame] | 1263 | struct tc_action_ops *ops[TCA_ACT_MAX_PRIO] = {}; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1264 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1265 | struct tc_action *act; |
Roman Mashak | 4e76e75 | 2018-03-08 16:59:19 -0500 | [diff] [blame] | 1266 | size_t sz = 0; |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 1267 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | int i; |
| 1269 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 1270 | err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL, |
| 1271 | extack); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 1272 | if (err < 0) |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1273 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1275 | for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { |
Cong Wang | d349f99 | 2021-01-16 16:56:57 -0800 | [diff] [blame] | 1276 | struct tc_action_ops *a_o; |
| 1277 | |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1278 | a_o = tc_action_load_ops(tb[i], flags & TCA_ACT_FLAGS_POLICE, |
| 1279 | !(flags & TCA_ACT_FLAGS_NO_RTNL), |
| 1280 | extack); |
Cong Wang | d349f99 | 2021-01-16 16:56:57 -0800 | [diff] [blame] | 1281 | if (IS_ERR(a_o)) { |
| 1282 | err = PTR_ERR(a_o); |
| 1283 | goto err_mod; |
| 1284 | } |
| 1285 | ops[i - 1] = a_o; |
| 1286 | } |
| 1287 | |
| 1288 | for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1289 | act = tcf_action_init_1(net, tp, tb[i], est, ops[i - 1], |
| 1290 | &init_res[i - 1], flags, extack); |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1291 | if (IS_ERR(act)) { |
| 1292 | err = PTR_ERR(act); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | goto err; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1294 | } |
Roman Mashak | 4e76e75 | 2018-03-08 16:59:19 -0500 | [diff] [blame] | 1295 | sz += tcf_action_fill_size(act); |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1296 | /* Start from index 0 */ |
| 1297 | actions[i - 1] = act; |
Baowen Zheng | 7adc576 | 2021-12-17 19:16:23 +0100 | [diff] [blame] | 1298 | if (!tc_act_bind(flags)) { |
| 1299 | err = tcf_action_offload_add(act, extack); |
| 1300 | if (tc_act_skip_sw(act->tcfa_flags) && err) |
| 1301 | goto err; |
| 1302 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | } |
Jamal Hadi Salim | aecc5ce | 2016-09-19 19:02:51 -0400 | [diff] [blame] | 1304 | |
Cong Wang | 0fedc63 | 2020-09-22 20:56:24 -0700 | [diff] [blame] | 1305 | /* We have to commit them all together, because if any error happened in |
| 1306 | * between, we could not handle the failure gracefully. |
| 1307 | */ |
| 1308 | tcf_idr_insert_many(actions); |
| 1309 | |
Roman Mashak | 4e76e75 | 2018-03-08 16:59:19 -0500 | [diff] [blame] | 1310 | *attr_size = tcf_action_full_attrs_size(sz); |
Vlad Buslov | b3650bf7 | 2021-04-07 18:36:04 +0300 | [diff] [blame] | 1311 | err = i - 1; |
| 1312 | goto err_mod; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | |
| 1314 | err: |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1315 | tcf_action_destroy(actions, flags & TCA_ACT_FLAGS_BIND); |
Cong Wang | d349f99 | 2021-01-16 16:56:57 -0800 | [diff] [blame] | 1316 | err_mod: |
| 1317 | for (i = 0; i < TCA_ACT_MAX_PRIO; i++) { |
| 1318 | if (ops[i]) |
| 1319 | module_put(ops[i]->owner); |
| 1320 | } |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1321 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | } |
| 1323 | |
Po Liu | 4b61d3e | 2020-06-19 14:01:07 +0800 | [diff] [blame] | 1324 | void tcf_action_update_stats(struct tc_action *a, u64 bytes, u64 packets, |
| 1325 | u64 drops, bool hw) |
Vlad Buslov | c8ecebd | 2019-10-30 16:09:00 +0200 | [diff] [blame] | 1326 | { |
Vlad Buslov | 5e174d5 | 2019-10-30 16:09:04 +0200 | [diff] [blame] | 1327 | if (a->cpu_bstats) { |
Ahmed S. Darwish | 50dc9a8 | 2021-10-16 10:49:09 +0200 | [diff] [blame] | 1328 | _bstats_update(this_cpu_ptr(a->cpu_bstats), bytes, packets); |
Vlad Buslov | c8ecebd | 2019-10-30 16:09:00 +0200 | [diff] [blame] | 1329 | |
Po Liu | 4b61d3e | 2020-06-19 14:01:07 +0800 | [diff] [blame] | 1330 | this_cpu_ptr(a->cpu_qstats)->drops += drops; |
Vlad Buslov | 5e174d5 | 2019-10-30 16:09:04 +0200 | [diff] [blame] | 1331 | |
| 1332 | if (hw) |
Ahmed S. Darwish | 50dc9a8 | 2021-10-16 10:49:09 +0200 | [diff] [blame] | 1333 | _bstats_update(this_cpu_ptr(a->cpu_bstats_hw), |
| 1334 | bytes, packets); |
Vlad Buslov | 5e174d5 | 2019-10-30 16:09:04 +0200 | [diff] [blame] | 1335 | return; |
| 1336 | } |
| 1337 | |
| 1338 | _bstats_update(&a->tcfa_bstats, bytes, packets); |
Po Liu | 4b61d3e | 2020-06-19 14:01:07 +0800 | [diff] [blame] | 1339 | a->tcfa_qstats.drops += drops; |
Vlad Buslov | c8ecebd | 2019-10-30 16:09:00 +0200 | [diff] [blame] | 1340 | if (hw) |
Vlad Buslov | 5e174d5 | 2019-10-30 16:09:04 +0200 | [diff] [blame] | 1341 | _bstats_update(&a->tcfa_bstats_hw, bytes, packets); |
Vlad Buslov | c8ecebd | 2019-10-30 16:09:00 +0200 | [diff] [blame] | 1342 | } |
| 1343 | EXPORT_SYMBOL(tcf_action_update_stats); |
| 1344 | |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 1345 | int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | int compat_mode) |
| 1347 | { |
| 1348 | int err = 0; |
| 1349 | struct gnet_dump d; |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 1350 | |
WANG Cong | 7eb8896 | 2014-01-09 16:14:05 -0800 | [diff] [blame] | 1351 | if (p == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | goto errout; |
| 1353 | |
Baowen Zheng | c7a66f8 | 2021-12-17 19:16:25 +0100 | [diff] [blame] | 1354 | /* update hw stats for this action */ |
| 1355 | tcf_action_update_hw_stats(p); |
| 1356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | /* compat_mode being true specifies a call that is supposed |
Dirk Hohndel | 06fe9fb | 2009-09-28 21:43:57 -0400 | [diff] [blame] | 1358 | * to add additional backward compatibility statistic TLVs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | */ |
| 1360 | if (compat_mode) { |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 1361 | if (p->type == TCA_OLD_COMPAT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | err = gnet_stats_start_copy_compat(skb, 0, |
Nicolas Dichtel | 9854518e | 2016-04-26 10:06:18 +0200 | [diff] [blame] | 1363 | TCA_STATS, |
| 1364 | TCA_XSTATS, |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 1365 | &p->tcfa_lock, &d, |
Nicolas Dichtel | 9854518e | 2016-04-26 10:06:18 +0200 | [diff] [blame] | 1366 | TCA_PAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | else |
| 1368 | return 0; |
| 1369 | } else |
| 1370 | err = gnet_stats_start_copy(skb, TCA_ACT_STATS, |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 1371 | &p->tcfa_lock, &d, TCA_ACT_PAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | |
| 1373 | if (err < 0) |
| 1374 | goto errout; |
| 1375 | |
Ahmed S. Darwish | 29cbcd8 | 2021-10-16 10:49:10 +0200 | [diff] [blame] | 1376 | if (gnet_stats_copy_basic(&d, p->cpu_bstats, |
| 1377 | &p->tcfa_bstats, false) < 0 || |
| 1378 | gnet_stats_copy_basic_hw(&d, p->cpu_bstats_hw, |
| 1379 | &p->tcfa_bstats_hw, false) < 0 || |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 1380 | gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 || |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 1381 | gnet_stats_copy_queue(&d, p->cpu_qstats, |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 1382 | &p->tcfa_qstats, |
| 1383 | p->tcfa_qstats.qlen) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | goto errout; |
| 1385 | |
| 1386 | if (gnet_stats_finish_copy(&d) < 0) |
| 1387 | goto errout; |
| 1388 | |
| 1389 | return 0; |
| 1390 | |
| 1391 | errout: |
| 1392 | return -1; |
| 1393 | } |
| 1394 | |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1395 | static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[], |
Jamal Hadi Salim | 0b0f43f | 2016-06-05 10:41:32 -0400 | [diff] [blame] | 1396 | u32 portid, u32 seq, u16 flags, int event, int bind, |
| 1397 | int ref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | { |
| 1399 | struct tcamsg *t; |
| 1400 | struct nlmsghdr *nlh; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1401 | unsigned char *b = skb_tail_pointer(skb); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1402 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1404 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags); |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1405 | if (!nlh) |
| 1406 | goto out_nlmsg_trim; |
| 1407 | t = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | t->tca_family = AF_UNSPEC; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 1409 | t->tca__pad1 = 0; |
| 1410 | t->tca__pad2 = 0; |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 1411 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1412 | nest = nla_nest_start_noflag(skb, TCA_ACT_TAB); |
Alexander Aring | 1af851558 | 2018-02-15 10:54:53 -0500 | [diff] [blame] | 1413 | if (!nest) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1414 | goto out_nlmsg_trim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | |
Vlad Buslov | ca44b73 | 2020-05-15 14:40:12 +0300 | [diff] [blame] | 1416 | if (tcf_action_dump(skb, actions, bind, ref, false) < 0) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1417 | goto out_nlmsg_trim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1419 | nla_nest_end(skb, nest); |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 1420 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1421 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | return skb->len; |
| 1423 | |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1424 | out_nlmsg_trim: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 1425 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | return -1; |
| 1427 | } |
| 1428 | |
| 1429 | static int |
Roman Mashak | c4c4290 | 2017-07-13 13:12:18 -0400 | [diff] [blame] | 1430 | tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n, |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1431 | struct tc_action *actions[], int event, |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1432 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | { |
| 1434 | struct sk_buff *skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | |
| 1436 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1437 | if (!skb) |
| 1438 | return -ENOBUFS; |
Jamal Hadi Salim | 0b0f43f | 2016-06-05 10:41:32 -0400 | [diff] [blame] | 1439 | if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, |
Vlad Buslov | 3f7c72b | 2018-07-05 17:24:26 +0300 | [diff] [blame] | 1440 | 0, 1) <= 0) { |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1441 | NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | kfree_skb(skb); |
| 1443 | return -EINVAL; |
| 1444 | } |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 1445 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1446 | return rtnl_unicast(skb, net, portid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | } |
| 1448 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 1449 | static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla, |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1450 | struct nlmsghdr *n, u32 portid, |
| 1451 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1453 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 1454 | const struct tc_action_ops *ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | struct tc_action *a; |
| 1456 | int index; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1457 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | |
Cong Wang | 199ce85 | 2019-09-18 18:44:43 -0700 | [diff] [blame] | 1459 | err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla, |
| 1460 | tcf_action_policy, extack); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 1461 | if (err < 0) |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1462 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 1464 | err = -EINVAL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1465 | if (tb[TCA_ACT_INDEX] == NULL || |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1466 | nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) { |
| 1467 | NL_SET_ERR_MSG(extack, "Invalid TC action index value"); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1468 | goto err_out; |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1469 | } |
Patrick McHardy | 1587bac | 2008-01-23 20:35:03 -0800 | [diff] [blame] | 1470 | index = nla_get_u32(tb[TCA_ACT_INDEX]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1472 | err = -EINVAL; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 1473 | ops = tc_lookup_action(tb[TCA_ACT_KIND]); |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1474 | if (!ops) { /* could happen in batch of actions */ |
Cong Wang | f061b48 | 2018-08-29 10:15:35 -0700 | [diff] [blame] | 1475 | NL_SET_ERR_MSG(extack, "Specified TC action kind not found"); |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 1476 | goto err_out; |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1477 | } |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1478 | err = -ENOENT; |
Cong Wang | f061b48 | 2018-08-29 10:15:35 -0700 | [diff] [blame] | 1479 | if (ops->lookup(net, &a, index) == 0) { |
| 1480 | NL_SET_ERR_MSG(extack, "TC action with specified index not found"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | goto err_mod; |
Cong Wang | f061b48 | 2018-08-29 10:15:35 -0700 | [diff] [blame] | 1482 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 1484 | module_put(ops->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | return a; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | err_mod: |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 1488 | module_put(ops->owner); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1489 | err_out: |
| 1490 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | } |
| 1492 | |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 1493 | static int tca_action_flush(struct net *net, struct nlattr *nla, |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1494 | struct nlmsghdr *n, u32 portid, |
| 1495 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | { |
| 1497 | struct sk_buff *skb; |
| 1498 | unsigned char *b; |
| 1499 | struct nlmsghdr *nlh; |
| 1500 | struct tcamsg *t; |
| 1501 | struct netlink_callback dcb; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1502 | struct nlattr *nest; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1503 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 1504 | const struct tc_action_ops *ops; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1505 | struct nlattr *kind; |
Jamal Hadi Salim | 3672387 | 2008-08-13 02:41:45 -0700 | [diff] [blame] | 1506 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1509 | if (!skb) |
Jamal Hadi Salim | 3672387 | 2008-08-13 02:41:45 -0700 | [diff] [blame] | 1510 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1512 | b = skb_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | |
Cong Wang | 199ce85 | 2019-09-18 18:44:43 -0700 | [diff] [blame] | 1514 | err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla, |
| 1515 | tcf_action_policy, extack); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 1516 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | goto err_out; |
| 1518 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 1519 | err = -EINVAL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1520 | kind = tb[TCA_ACT_KIND]; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 1521 | ops = tc_lookup_action(kind); |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1522 | if (!ops) { /*some idjot trying to flush unknown action */ |
| 1523 | NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | goto err_out; |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1525 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | |
Jamal Hadi Salim | 0b0f43f | 2016-06-05 10:41:32 -0400 | [diff] [blame] | 1527 | nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, |
| 1528 | sizeof(*t), 0); |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1529 | if (!nlh) { |
| 1530 | NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification"); |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1531 | goto out_module_put; |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1532 | } |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1533 | t = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | t->tca_family = AF_UNSPEC; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 1535 | t->tca__pad1 = 0; |
| 1536 | t->tca__pad2 = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1538 | nest = nla_nest_start_noflag(skb, TCA_ACT_TAB); |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1539 | if (!nest) { |
| 1540 | NL_SET_ERR_MSG(extack, "Failed to add new netlink message"); |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1541 | goto out_module_put; |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1542 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | |
Alexander Aring | 4178010 | 2018-02-15 10:54:58 -0500 | [diff] [blame] | 1544 | err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack); |
Davide Caratti | 66dede2 | 2018-02-15 15:50:57 +0100 | [diff] [blame] | 1545 | if (err <= 0) { |
| 1546 | nla_nest_cancel(skb, nest); |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1547 | goto out_module_put; |
Davide Caratti | 66dede2 | 2018-02-15 15:50:57 +0100 | [diff] [blame] | 1548 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1550 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1552 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | nlh->nlmsg_flags |= NLM_F_ROOT; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 1554 | module_put(ops->owner); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1555 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1556 | n->nlmsg_flags & NLM_F_ECHO); |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1557 | if (err < 0) |
| 1558 | NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | |
| 1560 | return err; |
| 1561 | |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1562 | out_module_put: |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 1563 | module_put(ops->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | err_out: |
| 1565 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | return err; |
| 1567 | } |
| 1568 | |
Cong Wang | b144e7e | 2018-08-19 12:22:07 -0700 | [diff] [blame] | 1569 | static int tcf_action_delete(struct net *net, struct tc_action *actions[]) |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 1570 | { |
Cong Wang | 97a3f84f | 2018-08-19 12:22:06 -0700 | [diff] [blame] | 1571 | int i; |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 1572 | |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1573 | for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) { |
| 1574 | struct tc_action *a = actions[i]; |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 1575 | const struct tc_action_ops *ops = a->ops; |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 1576 | /* Actions can be deleted concurrently so we must save their |
| 1577 | * type and id to search again after reference is released. |
| 1578 | */ |
Cong Wang | 97a3f84f | 2018-08-19 12:22:06 -0700 | [diff] [blame] | 1579 | struct tcf_idrinfo *idrinfo = a->idrinfo; |
| 1580 | u32 act_index = a->tcfa_index; |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 1581 | |
Vlad Buslov | c10bbfa | 2018-09-03 10:04:55 +0300 | [diff] [blame] | 1582 | actions[i] = NULL; |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 1583 | if (tcf_action_put(a)) { |
| 1584 | /* last reference, action was deleted concurrently */ |
| 1585 | module_put(ops->owner); |
| 1586 | } else { |
Cong Wang | 97a3f84f | 2018-08-19 12:22:06 -0700 | [diff] [blame] | 1587 | int ret; |
| 1588 | |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 1589 | /* now do the delete */ |
Cong Wang | 97a3f84f | 2018-08-19 12:22:06 -0700 | [diff] [blame] | 1590 | ret = tcf_idr_delete_index(idrinfo, act_index); |
Cong Wang | edfaf94 | 2018-08-19 12:22:05 -0700 | [diff] [blame] | 1591 | if (ret < 0) |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 1592 | return ret; |
| 1593 | } |
| 1594 | } |
| 1595 | return 0; |
| 1596 | } |
| 1597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | static int |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1599 | tcf_del_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[], |
Cong Wang | edfaf94 | 2018-08-19 12:22:05 -0700 | [diff] [blame] | 1600 | u32 portid, size_t attr_size, struct netlink_ext_ack *extack) |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 1601 | { |
| 1602 | int ret; |
| 1603 | struct sk_buff *skb; |
| 1604 | |
Roman Mashak | d04e699 | 2018-03-08 16:59:17 -0500 | [diff] [blame] | 1605 | skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size, |
| 1606 | GFP_KERNEL); |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 1607 | if (!skb) |
| 1608 | return -ENOBUFS; |
| 1609 | |
| 1610 | if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION, |
Vlad Buslov | 3f7c72b | 2018-07-05 17:24:26 +0300 | [diff] [blame] | 1611 | 0, 2) <= 0) { |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1612 | NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes"); |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 1613 | kfree_skb(skb); |
| 1614 | return -EINVAL; |
| 1615 | } |
| 1616 | |
| 1617 | /* now do the delete */ |
Cong Wang | b144e7e | 2018-08-19 12:22:07 -0700 | [diff] [blame] | 1618 | ret = tcf_action_delete(net, actions); |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 1619 | if (ret < 0) { |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1620 | NL_SET_ERR_MSG(extack, "Failed to delete TC action"); |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 1621 | kfree_skb(skb); |
| 1622 | return ret; |
| 1623 | } |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 1624 | |
| 1625 | ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 1626 | n->nlmsg_flags & NLM_F_ECHO); |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 1627 | return ret; |
| 1628 | } |
| 1629 | |
| 1630 | static int |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 1631 | tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n, |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1632 | u32 portid, int event, struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | { |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 1634 | int i, ret; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1635 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1636 | struct tc_action *act; |
Roman Mashak | d04e699 | 2018-03-08 16:59:17 -0500 | [diff] [blame] | 1637 | size_t attr_size = 0; |
Cong Wang | edfaf94 | 2018-08-19 12:22:05 -0700 | [diff] [blame] | 1638 | struct tc_action *actions[TCA_ACT_MAX_PRIO] = {}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 1640 | ret = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL, |
| 1641 | extack); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 1642 | if (ret < 0) |
| 1643 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1645 | if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) { |
Alexander Aring | 1af851558 | 2018-02-15 10:54:53 -0500 | [diff] [blame] | 1646 | if (tb[1]) |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1647 | return tca_action_flush(net, tb[1], n, portid, extack); |
Alexander Aring | 1af851558 | 2018-02-15 10:54:53 -0500 | [diff] [blame] | 1648 | |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1649 | NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action"); |
Alexander Aring | 1af851558 | 2018-02-15 10:54:53 -0500 | [diff] [blame] | 1650 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | } |
| 1652 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1653 | for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1654 | act = tcf_action_get_1(net, tb[i], n, portid, extack); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1655 | if (IS_ERR(act)) { |
| 1656 | ret = PTR_ERR(act); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | goto err; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1658 | } |
Roman Mashak | 4e76e75 | 2018-03-08 16:59:19 -0500 | [diff] [blame] | 1659 | attr_size += tcf_action_fill_size(act); |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1660 | actions[i - 1] = act; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | } |
| 1662 | |
Roman Mashak | 4e76e75 | 2018-03-08 16:59:19 -0500 | [diff] [blame] | 1663 | attr_size = tcf_action_full_attrs_size(attr_size); |
| 1664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | if (event == RTM_GETACTION) |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1666 | ret = tcf_get_notify(net, portid, n, actions, event, extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | else { /* delete */ |
Cong Wang | edfaf94 | 2018-08-19 12:22:05 -0700 | [diff] [blame] | 1668 | ret = tcf_del_notify(net, n, actions, portid, attr_size, extack); |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 1669 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | goto err; |
Cong Wang | edfaf94 | 2018-08-19 12:22:05 -0700 | [diff] [blame] | 1671 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | } |
| 1673 | err: |
Cong Wang | edfaf94 | 2018-08-19 12:22:05 -0700 | [diff] [blame] | 1674 | tcf_action_put_many(actions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | return ret; |
| 1676 | } |
| 1677 | |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 1678 | static int |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1679 | tcf_add_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[], |
Roman Mashak | d04e699 | 2018-03-08 16:59:17 -0500 | [diff] [blame] | 1680 | u32 portid, size_t attr_size, struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | struct sk_buff *skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | |
Roman Mashak | d04e699 | 2018-03-08 16:59:17 -0500 | [diff] [blame] | 1684 | skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size, |
| 1685 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | if (!skb) |
| 1687 | return -ENOBUFS; |
| 1688 | |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 1689 | if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags, |
| 1690 | RTM_NEWACTION, 0, 0) <= 0) { |
Roman Mashak | d143b9e | 2018-03-02 20:52:01 -0500 | [diff] [blame] | 1691 | NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action"); |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 1692 | kfree_skb(skb); |
| 1693 | return -EINVAL; |
| 1694 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | |
Yajun Deng | f79a3bc | 2021-07-15 20:24:24 +0800 | [diff] [blame] | 1696 | return rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 1697 | n->nlmsg_flags & NLM_F_ECHO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | } |
| 1699 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 1700 | static int tcf_action_add(struct net *net, struct nlattr *nla, |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1701 | struct nlmsghdr *n, u32 portid, u32 flags, |
Alexander Aring | aea0d72 | 2018-02-15 10:54:54 -0500 | [diff] [blame] | 1702 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | { |
Roman Mashak | d04e699 | 2018-03-08 16:59:17 -0500 | [diff] [blame] | 1704 | size_t attr_size = 0; |
Vlad Buslov | 87c750e | 2021-04-07 18:36:03 +0300 | [diff] [blame] | 1705 | int loop, ret, i; |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1706 | struct tc_action *actions[TCA_ACT_MAX_PRIO] = {}; |
Vlad Buslov | 87c750e | 2021-04-07 18:36:03 +0300 | [diff] [blame] | 1707 | int init_res[TCA_ACT_MAX_PRIO] = {}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | |
Eric Dumazet | 39f13ea | 2019-10-14 11:22:30 -0700 | [diff] [blame] | 1709 | for (loop = 0; loop < 10; loop++) { |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1710 | ret = tcf_action_init(net, NULL, nla, NULL, actions, init_res, |
| 1711 | &attr_size, flags, extack); |
Eric Dumazet | 39f13ea | 2019-10-14 11:22:30 -0700 | [diff] [blame] | 1712 | if (ret != -EAGAIN) |
| 1713 | break; |
| 1714 | } |
| 1715 | |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1716 | if (ret < 0) |
WANG Cong | f07fed8 | 2016-08-13 22:34:56 -0700 | [diff] [blame] | 1717 | return ret; |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1718 | ret = tcf_add_notify(net, n, actions, portid, attr_size, extack); |
Vlad Buslov | 87c750e | 2021-04-07 18:36:03 +0300 | [diff] [blame] | 1719 | |
| 1720 | /* only put existing actions */ |
| 1721 | for (i = 0; i < TCA_ACT_MAX_PRIO; i++) |
| 1722 | if (init_res[i] == ACT_P_CREATED) |
| 1723 | actions[i] = NULL; |
| 1724 | tcf_action_put_many(actions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | |
Vlad Buslov | cae422f | 2018-07-05 17:24:31 +0300 | [diff] [blame] | 1726 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | } |
| 1728 | |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1729 | static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = { |
Vlad Buslov | f460019 | 2020-11-24 18:40:54 +0200 | [diff] [blame] | 1730 | [TCA_ROOT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_ACT_FLAG_LARGE_DUMP_ON | |
| 1731 | TCA_ACT_FLAG_TERSE_DUMP), |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 1732 | [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 }, |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1733 | }; |
| 1734 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 1735 | static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, |
| 1736 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1738 | struct net *net = sock_net(skb->sk); |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1739 | struct nlattr *tca[TCA_ROOT_MAX + 1]; |
Gaurav Singh | 8bf1539 | 2020-06-19 15:24:13 -0400 | [diff] [blame] | 1740 | u32 portid = NETLINK_CB(skb).portid; |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1741 | u32 flags = 0; |
| 1742 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | |
Jamal Hadi Salim | 0b0f43f | 2016-06-05 10:41:32 -0400 | [diff] [blame] | 1744 | if ((n->nlmsg_type != RTM_GETACTION) && |
| 1745 | !netlink_capable(skb, CAP_NET_ADMIN)) |
Eric W. Biederman | dfc47ef | 2012-11-16 03:03:00 +0000 | [diff] [blame] | 1746 | return -EPERM; |
| 1747 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 1748 | ret = nlmsg_parse_deprecated(n, sizeof(struct tcamsg), tca, |
| 1749 | TCA_ROOT_MAX, NULL, extack); |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1750 | if (ret < 0) |
| 1751 | return ret; |
| 1752 | |
| 1753 | if (tca[TCA_ACT_TAB] == NULL) { |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1754 | NL_SET_ERR_MSG(extack, "Netlink action attributes missing"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | return -EINVAL; |
| 1756 | } |
| 1757 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1758 | /* n->nlmsg_flags & NLM_F_CREATE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | switch (n->nlmsg_type) { |
| 1760 | case RTM_NEWACTION: |
| 1761 | /* we are going to assume all other flags |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1762 | * imply create only if it doesn't exist |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | * Note that CREATE | EXCL implies that |
| 1764 | * but since we want avoid ambiguity (eg when flags |
| 1765 | * is zero) then just set this |
| 1766 | */ |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1767 | if (n->nlmsg_flags & NLM_F_REPLACE) |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1768 | flags = TCA_ACT_FLAGS_REPLACE; |
| 1769 | ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, flags, |
Alexander Aring | aea0d72 | 2018-02-15 10:54:54 -0500 | [diff] [blame] | 1770 | extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | break; |
| 1772 | case RTM_DELACTION: |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 1773 | ret = tca_action_gd(net, tca[TCA_ACT_TAB], n, |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1774 | portid, RTM_DELACTION, extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | break; |
| 1776 | case RTM_GETACTION: |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 1777 | ret = tca_action_gd(net, tca[TCA_ACT_TAB], n, |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1778 | portid, RTM_GETACTION, extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | break; |
| 1780 | default: |
| 1781 | BUG(); |
| 1782 | } |
| 1783 | |
| 1784 | return ret; |
| 1785 | } |
| 1786 | |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1787 | static struct nlattr *find_dump_kind(struct nlattr **nla) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1789 | struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1]; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1790 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1791 | struct nlattr *kind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1793 | tb1 = nla[TCA_ACT_TAB]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | if (tb1 == NULL) |
| 1795 | return NULL; |
| 1796 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 1797 | if (nla_parse_deprecated(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | |
Patrick McHardy | 6d834e0 | 2008-01-23 20:32:42 -0800 | [diff] [blame] | 1800 | if (tb[1] == NULL) |
| 1801 | return NULL; |
Cong Wang | 199ce85 | 2019-09-18 18:44:43 -0700 | [diff] [blame] | 1802 | if (nla_parse_nested_deprecated(tb2, TCA_ACT_MAX, tb[1], tcf_action_policy, NULL) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | return NULL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1804 | kind = tb2[TCA_ACT_KIND]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | |
Thomas Graf | 26dab89 | 2006-07-05 20:45:06 -0700 | [diff] [blame] | 1806 | return kind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | } |
| 1808 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 1809 | static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 1811 | struct net *net = sock_net(skb->sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | struct nlmsghdr *nlh; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1813 | unsigned char *b = skb_tail_pointer(skb); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1814 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | struct tc_action_ops *a_o; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | int ret = 0; |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1817 | struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh); |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1818 | struct nlattr *tb[TCA_ROOT_MAX + 1]; |
| 1819 | struct nlattr *count_attr = NULL; |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 1820 | unsigned long jiffy_since = 0; |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1821 | struct nlattr *kind = NULL; |
| 1822 | struct nla_bitfield32 bf; |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 1823 | u32 msecs_since = 0; |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1824 | u32 act_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 1826 | ret = nlmsg_parse_deprecated(cb->nlh, sizeof(struct tcamsg), tb, |
| 1827 | TCA_ROOT_MAX, tcaa_policy, cb->extack); |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1828 | if (ret < 0) |
| 1829 | return ret; |
| 1830 | |
| 1831 | kind = find_dump_kind(tb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | if (kind == NULL) { |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 1833 | pr_info("tc_dump_action: action bad kind\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | return 0; |
| 1835 | } |
| 1836 | |
Thomas Graf | 26dab89 | 2006-07-05 20:45:06 -0700 | [diff] [blame] | 1837 | a_o = tc_lookup_action(kind); |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1838 | if (a_o == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1841 | cb->args[2] = 0; |
| 1842 | if (tb[TCA_ROOT_FLAGS]) { |
| 1843 | bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]); |
| 1844 | cb->args[2] = bf.value; |
| 1845 | } |
| 1846 | |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 1847 | if (tb[TCA_ROOT_TIME_DELTA]) { |
| 1848 | msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]); |
| 1849 | } |
| 1850 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1851 | nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1852 | cb->nlh->nlmsg_type, sizeof(*t), 0); |
| 1853 | if (!nlh) |
| 1854 | goto out_module_put; |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1855 | |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 1856 | if (msecs_since) |
| 1857 | jiffy_since = jiffies - msecs_to_jiffies(msecs_since); |
| 1858 | |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1859 | t = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | t->tca_family = AF_UNSPEC; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 1861 | t->tca__pad1 = 0; |
| 1862 | t->tca__pad2 = 0; |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 1863 | cb->args[3] = jiffy_since; |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1864 | count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32)); |
| 1865 | if (!count_attr) |
| 1866 | goto out_module_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1868 | nest = nla_nest_start_noflag(skb, TCA_ACT_TAB); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1869 | if (nest == NULL) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1870 | goto out_module_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | |
Alexander Aring | 4178010 | 2018-02-15 10:54:58 -0500 | [diff] [blame] | 1872 | ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | if (ret < 0) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1874 | goto out_module_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | |
| 1876 | if (ret > 0) { |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1877 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | ret = skb->len; |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1879 | act_count = cb->args[1]; |
| 1880 | memcpy(nla_data(count_attr), &act_count, sizeof(u32)); |
| 1881 | cb->args[1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | } else |
Jamal Hadi Salim | ebecaa6 | 2016-06-13 18:08:42 -0400 | [diff] [blame] | 1883 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1885 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1886 | if (NETLINK_CB(cb->skb).portid && ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | nlh->nlmsg_flags |= NLM_F_MULTI; |
| 1888 | module_put(a_o->owner); |
| 1889 | return skb->len; |
| 1890 | |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1891 | out_module_put: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | module_put(a_o->owner); |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 1893 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | return skb->len; |
| 1895 | } |
| 1896 | |
| 1897 | static int __init tc_action_init(void) |
| 1898 | { |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 1899 | rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0); |
| 1900 | rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0); |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 1901 | rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action, |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 1902 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | return 0; |
| 1905 | } |
| 1906 | |
| 1907 | subsys_initcall(tc_action_init); |