.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * Copyright (c) 2008, Intel Corporation. |
---|
3 | | - * |
---|
4 | | - * This program is free software; you can redistribute it and/or modify it |
---|
5 | | - * under the terms and conditions of the GNU General Public License, |
---|
6 | | - * version 2, as published by the Free Software Foundation. |
---|
7 | | - * |
---|
8 | | - * This program is distributed in the hope it will be useful, but WITHOUT |
---|
9 | | - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
10 | | - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
---|
11 | | - * more details. |
---|
12 | | - * |
---|
13 | | - * You should have received a copy of the GNU General Public License along with |
---|
14 | | - * this program; if not, see <http://www.gnu.org/licenses/>. |
---|
15 | 4 | * |
---|
16 | 5 | * Author: Alexander Duyck <alexander.h.duyck@intel.com> |
---|
17 | 6 | */ |
---|
.. | .. |
---|
26 | 15 | #include <net/ip.h> |
---|
27 | 16 | #include <net/ipv6.h> |
---|
28 | 17 | #include <net/dsfield.h> |
---|
| 18 | +#include <net/pkt_cls.h> |
---|
29 | 19 | |
---|
30 | 20 | #include <linux/tc_act/tc_skbedit.h> |
---|
31 | 21 | #include <net/tc_act/tc_skbedit.h> |
---|
.. | .. |
---|
83 | 73 | return TC_ACT_SHOT; |
---|
84 | 74 | } |
---|
85 | 75 | |
---|
| 76 | +static void tcf_skbedit_stats_update(struct tc_action *a, u64 bytes, |
---|
| 77 | + u64 packets, u64 drops, |
---|
| 78 | + u64 lastuse, bool hw) |
---|
| 79 | +{ |
---|
| 80 | + struct tcf_skbedit *d = to_skbedit(a); |
---|
| 81 | + struct tcf_t *tm = &d->tcf_tm; |
---|
| 82 | + |
---|
| 83 | + tcf_action_update_stats(a, bytes, packets, drops, hw); |
---|
| 84 | + tm->lastuse = max_t(u64, tm->lastuse, lastuse); |
---|
| 85 | +} |
---|
| 86 | + |
---|
86 | 87 | static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = { |
---|
87 | 88 | [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) }, |
---|
88 | 89 | [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) }, |
---|
.. | .. |
---|
96 | 97 | static int tcf_skbedit_init(struct net *net, struct nlattr *nla, |
---|
97 | 98 | struct nlattr *est, struct tc_action **a, |
---|
98 | 99 | int ovr, int bind, bool rtnl_held, |
---|
| 100 | + struct tcf_proto *tp, u32 act_flags, |
---|
99 | 101 | struct netlink_ext_ack *extack) |
---|
100 | 102 | { |
---|
101 | 103 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
---|
102 | | - struct tcf_skbedit_params *params_old, *params_new; |
---|
| 104 | + struct tcf_skbedit_params *params_new; |
---|
103 | 105 | struct nlattr *tb[TCA_SKBEDIT_MAX + 1]; |
---|
| 106 | + struct tcf_chain *goto_ch = NULL; |
---|
104 | 107 | struct tc_skbedit *parm; |
---|
105 | 108 | struct tcf_skbedit *d; |
---|
106 | 109 | u32 flags = 0, *priority = NULL, *mark = NULL, *mask = NULL; |
---|
.. | .. |
---|
112 | 115 | if (nla == NULL) |
---|
113 | 116 | return -EINVAL; |
---|
114 | 117 | |
---|
115 | | - err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy, NULL); |
---|
| 118 | + err = nla_parse_nested_deprecated(tb, TCA_SKBEDIT_MAX, nla, |
---|
| 119 | + skbedit_policy, NULL); |
---|
116 | 120 | if (err < 0) |
---|
117 | 121 | return err; |
---|
118 | 122 | |
---|
.. | .. |
---|
172 | 176 | |
---|
173 | 177 | if (!exists) { |
---|
174 | 178 | ret = tcf_idr_create(tn, index, est, a, |
---|
175 | | - &act_skbedit_ops, bind, true); |
---|
| 179 | + &act_skbedit_ops, bind, true, act_flags); |
---|
176 | 180 | if (ret) { |
---|
177 | 181 | tcf_idr_cleanup(tn, index); |
---|
178 | 182 | return ret; |
---|
.. | .. |
---|
187 | 191 | return -EEXIST; |
---|
188 | 192 | } |
---|
189 | 193 | } |
---|
190 | | - |
---|
191 | | - ASSERT_RTNL(); |
---|
| 194 | + err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); |
---|
| 195 | + if (err < 0) |
---|
| 196 | + goto release_idr; |
---|
192 | 197 | |
---|
193 | 198 | params_new = kzalloc(sizeof(*params_new), GFP_KERNEL); |
---|
194 | 199 | if (unlikely(!params_new)) { |
---|
195 | | - tcf_idr_release(*a, bind); |
---|
196 | | - return -ENOMEM; |
---|
| 200 | + err = -ENOMEM; |
---|
| 201 | + goto put_chain; |
---|
197 | 202 | } |
---|
198 | 203 | |
---|
199 | 204 | params_new->flags = flags; |
---|
.. | .. |
---|
210 | 215 | if (flags & SKBEDIT_F_MASK) |
---|
211 | 216 | params_new->mask = *mask; |
---|
212 | 217 | |
---|
213 | | - d->tcf_action = parm->action; |
---|
214 | | - params_old = rtnl_dereference(d->params); |
---|
215 | | - rcu_assign_pointer(d->params, params_new); |
---|
216 | | - if (params_old) |
---|
217 | | - kfree_rcu(params_old, rcu); |
---|
| 218 | + spin_lock_bh(&d->tcf_lock); |
---|
| 219 | + goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); |
---|
| 220 | + params_new = rcu_replace_pointer(d->params, params_new, |
---|
| 221 | + lockdep_is_held(&d->tcf_lock)); |
---|
| 222 | + spin_unlock_bh(&d->tcf_lock); |
---|
| 223 | + if (params_new) |
---|
| 224 | + kfree_rcu(params_new, rcu); |
---|
| 225 | + if (goto_ch) |
---|
| 226 | + tcf_chain_put_by_act(goto_ch); |
---|
218 | 227 | |
---|
219 | | - if (ret == ACT_P_CREATED) |
---|
220 | | - tcf_idr_insert(tn, *a); |
---|
221 | 228 | return ret; |
---|
| 229 | +put_chain: |
---|
| 230 | + if (goto_ch) |
---|
| 231 | + tcf_chain_put_by_act(goto_ch); |
---|
| 232 | +release_idr: |
---|
| 233 | + tcf_idr_release(*a, bind); |
---|
| 234 | + return err; |
---|
222 | 235 | } |
---|
223 | 236 | |
---|
224 | 237 | static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a, |
---|
.. | .. |
---|
231 | 244 | .index = d->tcf_index, |
---|
232 | 245 | .refcnt = refcount_read(&d->tcf_refcnt) - ref, |
---|
233 | 246 | .bindcnt = atomic_read(&d->tcf_bindcnt) - bind, |
---|
234 | | - .action = d->tcf_action, |
---|
235 | 247 | }; |
---|
236 | 248 | u64 pure_flags = 0; |
---|
237 | 249 | struct tcf_t t; |
---|
238 | 250 | |
---|
239 | | - params = rtnl_dereference(d->params); |
---|
| 251 | + spin_lock_bh(&d->tcf_lock); |
---|
| 252 | + params = rcu_dereference_protected(d->params, |
---|
| 253 | + lockdep_is_held(&d->tcf_lock)); |
---|
| 254 | + opt.action = d->tcf_action; |
---|
240 | 255 | |
---|
241 | 256 | if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt)) |
---|
242 | 257 | goto nla_put_failure; |
---|
.. | .. |
---|
264 | 279 | tcf_tm_dump(&t, &d->tcf_tm); |
---|
265 | 280 | if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD)) |
---|
266 | 281 | goto nla_put_failure; |
---|
| 282 | + spin_unlock_bh(&d->tcf_lock); |
---|
| 283 | + |
---|
267 | 284 | return skb->len; |
---|
268 | 285 | |
---|
269 | 286 | nla_put_failure: |
---|
| 287 | + spin_unlock_bh(&d->tcf_lock); |
---|
270 | 288 | nlmsg_trim(skb, b); |
---|
271 | 289 | return -1; |
---|
272 | 290 | } |
---|
.. | .. |
---|
291 | 309 | return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
---|
292 | 310 | } |
---|
293 | 311 | |
---|
294 | | -static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index, |
---|
295 | | - struct netlink_ext_ack *extack) |
---|
| 312 | +static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index) |
---|
296 | 313 | { |
---|
297 | 314 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
---|
298 | 315 | |
---|
299 | 316 | return tcf_idr_search(tn, a, index); |
---|
300 | 317 | } |
---|
301 | 318 | |
---|
| 319 | +static size_t tcf_skbedit_get_fill_size(const struct tc_action *act) |
---|
| 320 | +{ |
---|
| 321 | + return nla_total_size(sizeof(struct tc_skbedit)) |
---|
| 322 | + + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_PRIORITY */ |
---|
| 323 | + + nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_QUEUE_MAPPING */ |
---|
| 324 | + + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_MARK */ |
---|
| 325 | + + nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_PTYPE */ |
---|
| 326 | + + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_MASK */ |
---|
| 327 | + + nla_total_size_64bit(sizeof(u64)); /* TCA_SKBEDIT_FLAGS */ |
---|
| 328 | +} |
---|
| 329 | + |
---|
302 | 330 | static struct tc_action_ops act_skbedit_ops = { |
---|
303 | 331 | .kind = "skbedit", |
---|
304 | | - .type = TCA_ACT_SKBEDIT, |
---|
| 332 | + .id = TCA_ID_SKBEDIT, |
---|
305 | 333 | .owner = THIS_MODULE, |
---|
306 | 334 | .act = tcf_skbedit_act, |
---|
| 335 | + .stats_update = tcf_skbedit_stats_update, |
---|
307 | 336 | .dump = tcf_skbedit_dump, |
---|
308 | 337 | .init = tcf_skbedit_init, |
---|
309 | 338 | .cleanup = tcf_skbedit_cleanup, |
---|
310 | 339 | .walk = tcf_skbedit_walker, |
---|
| 340 | + .get_fill_size = tcf_skbedit_get_fill_size, |
---|
311 | 341 | .lookup = tcf_skbedit_search, |
---|
312 | 342 | .size = sizeof(struct tcf_skbedit), |
---|
313 | 343 | }; |
---|