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