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