.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * net/sched/act_pedit.c Generic packet editor |
---|
3 | | - * |
---|
4 | | - * This program is free software; you can redistribute it and/or |
---|
5 | | - * modify it under the terms of the GNU General Public License |
---|
6 | | - * as published by the Free Software Foundation; either version |
---|
7 | | - * 2 of the License, or (at your option) any later version. |
---|
8 | 4 | * |
---|
9 | 5 | * Authors: Jamal Hadi Salim (2002-4) |
---|
10 | 6 | */ |
---|
.. | .. |
---|
23 | 19 | #include <linux/tc_act/tc_pedit.h> |
---|
24 | 20 | #include <net/tc_act/tc_pedit.h> |
---|
25 | 21 | #include <uapi/linux/tc_act/tc_pedit.h> |
---|
| 22 | +#include <net/pkt_cls.h> |
---|
26 | 23 | |
---|
27 | 24 | static unsigned int pedit_net_id; |
---|
28 | 25 | static struct tc_action_ops act_pedit_ops; |
---|
.. | .. |
---|
69 | 66 | goto err_out; |
---|
70 | 67 | } |
---|
71 | 68 | |
---|
72 | | - err = nla_parse_nested(tb, TCA_PEDIT_KEY_EX_MAX, ka, |
---|
73 | | - pedit_key_ex_policy, NULL); |
---|
| 69 | + err = nla_parse_nested_deprecated(tb, TCA_PEDIT_KEY_EX_MAX, |
---|
| 70 | + ka, pedit_key_ex_policy, |
---|
| 71 | + NULL); |
---|
74 | 72 | if (err) |
---|
75 | 73 | goto err_out; |
---|
76 | 74 | |
---|
.. | .. |
---|
107 | 105 | static int tcf_pedit_key_ex_dump(struct sk_buff *skb, |
---|
108 | 106 | struct tcf_pedit_key_ex *keys_ex, int n) |
---|
109 | 107 | { |
---|
110 | | - struct nlattr *keys_start = nla_nest_start(skb, TCA_PEDIT_KEYS_EX); |
---|
| 108 | + struct nlattr *keys_start = nla_nest_start_noflag(skb, |
---|
| 109 | + TCA_PEDIT_KEYS_EX); |
---|
111 | 110 | |
---|
112 | 111 | if (!keys_start) |
---|
113 | 112 | goto nla_failure; |
---|
114 | 113 | for (; n > 0; n--) { |
---|
115 | 114 | struct nlattr *key_start; |
---|
116 | 115 | |
---|
117 | | - key_start = nla_nest_start(skb, TCA_PEDIT_KEY_EX); |
---|
| 116 | + key_start = nla_nest_start_noflag(skb, TCA_PEDIT_KEY_EX); |
---|
118 | 117 | if (!key_start) |
---|
119 | 118 | goto nla_failure; |
---|
120 | 119 | |
---|
.. | .. |
---|
138 | 137 | static int tcf_pedit_init(struct net *net, struct nlattr *nla, |
---|
139 | 138 | struct nlattr *est, struct tc_action **a, |
---|
140 | 139 | int ovr, int bind, bool rtnl_held, |
---|
| 140 | + struct tcf_proto *tp, u32 flags, |
---|
141 | 141 | struct netlink_ext_ack *extack) |
---|
142 | 142 | { |
---|
143 | 143 | struct tc_action_net *tn = net_generic(net, pedit_net_id); |
---|
144 | 144 | struct nlattr *tb[TCA_PEDIT_MAX + 1]; |
---|
| 145 | + struct tcf_chain *goto_ch = NULL; |
---|
145 | 146 | struct tc_pedit_key *keys = NULL; |
---|
146 | 147 | struct tcf_pedit_key_ex *keys_ex; |
---|
147 | 148 | struct tc_pedit *parm; |
---|
148 | 149 | struct nlattr *pattr; |
---|
149 | 150 | struct tcf_pedit *p; |
---|
150 | 151 | int ret = 0, err; |
---|
151 | | - int ksize; |
---|
| 152 | + int i, ksize; |
---|
152 | 153 | u32 index; |
---|
153 | 154 | |
---|
154 | 155 | if (!nla) { |
---|
.. | .. |
---|
156 | 157 | return -EINVAL; |
---|
157 | 158 | } |
---|
158 | 159 | |
---|
159 | | - err = nla_parse_nested(tb, TCA_PEDIT_MAX, nla, pedit_policy, NULL); |
---|
| 160 | + err = nla_parse_nested_deprecated(tb, TCA_PEDIT_MAX, nla, |
---|
| 161 | + pedit_policy, NULL); |
---|
160 | 162 | if (err < 0) |
---|
161 | 163 | return err; |
---|
162 | 164 | |
---|
.. | .. |
---|
187 | 189 | err = tcf_idr_check_alloc(tn, &index, a, bind); |
---|
188 | 190 | if (!err) { |
---|
189 | 191 | ret = tcf_idr_create(tn, index, est, a, |
---|
190 | | - &act_pedit_ops, bind, false); |
---|
| 192 | + &act_pedit_ops, bind, false, 0); |
---|
191 | 193 | if (ret) { |
---|
192 | 194 | tcf_idr_cleanup(tn, index); |
---|
193 | 195 | goto out_free; |
---|
.. | .. |
---|
205 | 207 | goto out_free; |
---|
206 | 208 | } |
---|
207 | 209 | |
---|
| 210 | + err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); |
---|
| 211 | + if (err < 0) { |
---|
| 212 | + ret = err; |
---|
| 213 | + goto out_release; |
---|
| 214 | + } |
---|
208 | 215 | p = to_pedit(*a); |
---|
209 | 216 | spin_lock_bh(&p->tcf_lock); |
---|
210 | 217 | |
---|
.. | .. |
---|
214 | 221 | if (!keys) { |
---|
215 | 222 | spin_unlock_bh(&p->tcf_lock); |
---|
216 | 223 | ret = -ENOMEM; |
---|
217 | | - goto out_release; |
---|
| 224 | + goto put_chain; |
---|
218 | 225 | } |
---|
219 | 226 | kfree(p->tcfp_keys); |
---|
220 | 227 | p->tcfp_keys = keys; |
---|
221 | 228 | p->tcfp_nkeys = parm->nkeys; |
---|
222 | 229 | } |
---|
223 | 230 | memcpy(p->tcfp_keys, parm->keys, ksize); |
---|
| 231 | + p->tcfp_off_max_hint = 0; |
---|
| 232 | + for (i = 0; i < p->tcfp_nkeys; ++i) { |
---|
| 233 | + u32 cur = p->tcfp_keys[i].off; |
---|
| 234 | + |
---|
| 235 | + /* sanitize the shift value for any later use */ |
---|
| 236 | + p->tcfp_keys[i].shift = min_t(size_t, BITS_PER_TYPE(int) - 1, |
---|
| 237 | + p->tcfp_keys[i].shift); |
---|
| 238 | + |
---|
| 239 | + /* The AT option can read a single byte, we can bound the actual |
---|
| 240 | + * value with uchar max. |
---|
| 241 | + */ |
---|
| 242 | + cur += (0xff & p->tcfp_keys[i].offmask) >> p->tcfp_keys[i].shift; |
---|
| 243 | + |
---|
| 244 | + /* Each key touches 4 bytes starting from the computed offset */ |
---|
| 245 | + p->tcfp_off_max_hint = max(p->tcfp_off_max_hint, cur + 4); |
---|
| 246 | + } |
---|
224 | 247 | |
---|
225 | 248 | p->tcfp_flags = parm->flags; |
---|
226 | | - p->tcf_action = parm->action; |
---|
| 249 | + goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); |
---|
227 | 250 | |
---|
228 | 251 | kfree(p->tcfp_keys_ex); |
---|
229 | 252 | p->tcfp_keys_ex = keys_ex; |
---|
230 | 253 | |
---|
231 | 254 | spin_unlock_bh(&p->tcf_lock); |
---|
232 | | - if (ret == ACT_P_CREATED) |
---|
233 | | - tcf_idr_insert(tn, *a); |
---|
| 255 | + if (goto_ch) |
---|
| 256 | + tcf_chain_put_by_act(goto_ch); |
---|
234 | 257 | return ret; |
---|
235 | 258 | |
---|
| 259 | +put_chain: |
---|
| 260 | + if (goto_ch) |
---|
| 261 | + tcf_chain_put_by_act(goto_ch); |
---|
236 | 262 | out_release: |
---|
237 | 263 | tcf_idr_release(*a, bind); |
---|
238 | 264 | out_free: |
---|
.. | .. |
---|
298 | 324 | struct tcf_result *res) |
---|
299 | 325 | { |
---|
300 | 326 | struct tcf_pedit *p = to_pedit(a); |
---|
| 327 | + u32 max_offset; |
---|
301 | 328 | int i; |
---|
302 | 329 | |
---|
303 | | - if (skb_unclone(skb, GFP_ATOMIC)) |
---|
304 | | - return p->tcf_action; |
---|
305 | | - |
---|
306 | 330 | spin_lock(&p->tcf_lock); |
---|
| 331 | + |
---|
| 332 | + max_offset = (skb_transport_header_was_set(skb) ? |
---|
| 333 | + skb_transport_offset(skb) : |
---|
| 334 | + skb_network_offset(skb)) + |
---|
| 335 | + p->tcfp_off_max_hint; |
---|
| 336 | + if (skb_ensure_writable(skb, min(skb->len, max_offset))) |
---|
| 337 | + goto unlock; |
---|
307 | 338 | |
---|
308 | 339 | tcf_lastuse_update(&p->tcf_tm); |
---|
309 | 340 | |
---|
.. | .. |
---|
393 | 424 | p->tcf_qstats.overlimits++; |
---|
394 | 425 | done: |
---|
395 | 426 | bstats_update(&p->tcf_bstats, skb); |
---|
| 427 | +unlock: |
---|
396 | 428 | spin_unlock(&p->tcf_lock); |
---|
397 | 429 | return p->tcf_action; |
---|
| 430 | +} |
---|
| 431 | + |
---|
| 432 | +static void tcf_pedit_stats_update(struct tc_action *a, u64 bytes, u64 packets, |
---|
| 433 | + u64 drops, u64 lastuse, bool hw) |
---|
| 434 | +{ |
---|
| 435 | + struct tcf_pedit *d = to_pedit(a); |
---|
| 436 | + struct tcf_t *tm = &d->tcf_tm; |
---|
| 437 | + |
---|
| 438 | + tcf_action_update_stats(a, bytes, packets, drops, hw); |
---|
| 439 | + tm->lastuse = max_t(u64, tm->lastuse, lastuse); |
---|
398 | 440 | } |
---|
399 | 441 | |
---|
400 | 442 | static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a, |
---|
.. | .. |
---|
406 | 448 | struct tcf_t t; |
---|
407 | 449 | int s; |
---|
408 | 450 | |
---|
409 | | - s = sizeof(*opt) + p->tcfp_nkeys * sizeof(struct tc_pedit_key); |
---|
| 451 | + s = struct_size(opt, keys, p->tcfp_nkeys); |
---|
410 | 452 | |
---|
411 | 453 | /* netlink spinlocks held above us - must use ATOMIC */ |
---|
412 | 454 | opt = kzalloc(s, GFP_ATOMIC); |
---|
.. | .. |
---|
414 | 456 | return -ENOBUFS; |
---|
415 | 457 | |
---|
416 | 458 | spin_lock_bh(&p->tcf_lock); |
---|
417 | | - memcpy(opt->keys, p->tcfp_keys, |
---|
418 | | - p->tcfp_nkeys * sizeof(struct tc_pedit_key)); |
---|
| 459 | + memcpy(opt->keys, p->tcfp_keys, flex_array_size(opt, keys, p->tcfp_nkeys)); |
---|
419 | 460 | opt->index = p->tcf_index; |
---|
420 | 461 | opt->nkeys = p->tcfp_nkeys; |
---|
421 | 462 | opt->flags = p->tcfp_flags; |
---|
.. | .. |
---|
461 | 502 | return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
---|
462 | 503 | } |
---|
463 | 504 | |
---|
464 | | -static int tcf_pedit_search(struct net *net, struct tc_action **a, u32 index, |
---|
465 | | - struct netlink_ext_ack *extack) |
---|
| 505 | +static int tcf_pedit_search(struct net *net, struct tc_action **a, u32 index) |
---|
466 | 506 | { |
---|
467 | 507 | struct tc_action_net *tn = net_generic(net, pedit_net_id); |
---|
468 | 508 | |
---|
.. | .. |
---|
471 | 511 | |
---|
472 | 512 | static struct tc_action_ops act_pedit_ops = { |
---|
473 | 513 | .kind = "pedit", |
---|
474 | | - .type = TCA_ACT_PEDIT, |
---|
| 514 | + .id = TCA_ID_PEDIT, |
---|
475 | 515 | .owner = THIS_MODULE, |
---|
476 | 516 | .act = tcf_pedit_act, |
---|
| 517 | + .stats_update = tcf_pedit_stats_update, |
---|
477 | 518 | .dump = tcf_pedit_dump, |
---|
478 | 519 | .cleanup = tcf_pedit_cleanup, |
---|
479 | 520 | .init = tcf_pedit_init, |
---|