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