1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/sched/act_pedit.c Generic packet editor
5 * Authors: Jamal Hadi Salim (2002-4)
8 #include <linux/types.h>
9 #include <linux/kernel.h>
10 #include <linux/string.h>
11 #include <linux/errno.h>
12 #include <linux/skbuff.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <net/netlink.h>
18 #include <net/pkt_sched.h>
19 #include <linux/tc_act/tc_pedit.h>
20 #include <net/tc_act/tc_pedit.h>
21 #include <uapi/linux/tc_act/tc_pedit.h>
22 #include <net/pkt_cls.h>
23 #include <net/tc_wrapper.h>
25 static struct tc_action_ops act_pedit_ops;
27 static const struct nla_policy pedit_policy[TCA_PEDIT_MAX + 1] = {
28 [TCA_PEDIT_PARMS] = { .len = sizeof(struct tc_pedit) },
29 [TCA_PEDIT_KEYS_EX] = { .type = NLA_NESTED },
32 static const struct nla_policy pedit_key_ex_policy[TCA_PEDIT_KEY_EX_MAX + 1] = {
33 [TCA_PEDIT_KEY_EX_HTYPE] = { .type = NLA_U16 },
34 [TCA_PEDIT_KEY_EX_CMD] = { .type = NLA_U16 },
37 static struct tcf_pedit_key_ex *tcf_pedit_keys_ex_parse(struct nlattr *nla,
40 struct tcf_pedit_key_ex *keys_ex;
41 struct tcf_pedit_key_ex *k;
42 const struct nlattr *ka;
49 keys_ex = kcalloc(n, sizeof(*k), GFP_KERNEL);
51 return ERR_PTR(-ENOMEM);
55 nla_for_each_nested(ka, nla, rem) {
56 struct nlattr *tb[TCA_PEDIT_KEY_EX_MAX + 1];
64 if (nla_type(ka) != TCA_PEDIT_KEY_EX) {
69 err = nla_parse_nested_deprecated(tb, TCA_PEDIT_KEY_EX_MAX,
70 ka, pedit_key_ex_policy,
75 if (!tb[TCA_PEDIT_KEY_EX_HTYPE] ||
76 !tb[TCA_PEDIT_KEY_EX_CMD]) {
81 k->htype = nla_get_u16(tb[TCA_PEDIT_KEY_EX_HTYPE]);
82 k->cmd = nla_get_u16(tb[TCA_PEDIT_KEY_EX_CMD]);
84 if (k->htype > TCA_PEDIT_HDR_TYPE_MAX ||
85 k->cmd > TCA_PEDIT_CMD_MAX) {
105 static int tcf_pedit_key_ex_dump(struct sk_buff *skb,
106 struct tcf_pedit_key_ex *keys_ex, int n)
108 struct nlattr *keys_start = nla_nest_start_noflag(skb,
114 struct nlattr *key_start;
116 key_start = nla_nest_start_noflag(skb, TCA_PEDIT_KEY_EX);
120 if (nla_put_u16(skb, TCA_PEDIT_KEY_EX_HTYPE, keys_ex->htype) ||
121 nla_put_u16(skb, TCA_PEDIT_KEY_EX_CMD, keys_ex->cmd))
124 nla_nest_end(skb, key_start);
129 nla_nest_end(skb, keys_start);
133 nla_nest_cancel(skb, keys_start);
137 static void tcf_pedit_cleanup_rcu(struct rcu_head *head)
139 struct tcf_pedit_parms *parms =
140 container_of(head, struct tcf_pedit_parms, rcu);
142 kfree(parms->tcfp_keys_ex);
143 kfree(parms->tcfp_keys);
148 static int tcf_pedit_init(struct net *net, struct nlattr *nla,
149 struct nlattr *est, struct tc_action **a,
150 struct tcf_proto *tp, u32 flags,
151 struct netlink_ext_ack *extack)
153 struct tc_action_net *tn = net_generic(net, act_pedit_ops.net_id);
154 bool bind = flags & TCA_ACT_FLAGS_BIND;
155 struct tcf_chain *goto_ch = NULL;
156 struct tcf_pedit_parms *oparms, *nparms;
157 struct nlattr *tb[TCA_PEDIT_MAX + 1];
158 struct tc_pedit *parm;
159 struct nlattr *pattr;
166 NL_SET_ERR_MSG_MOD(extack, "Pedit requires attributes to be passed");
170 err = nla_parse_nested_deprecated(tb, TCA_PEDIT_MAX, nla,
175 pattr = tb[TCA_PEDIT_PARMS];
177 pattr = tb[TCA_PEDIT_PARMS_EX];
179 NL_SET_ERR_MSG_MOD(extack, "Missing required TCA_PEDIT_PARMS or TCA_PEDIT_PARMS_EX pedit attribute");
183 parm = nla_data(pattr);
185 NL_SET_ERR_MSG_MOD(extack, "Pedit requires keys to be passed");
188 ksize = parm->nkeys * sizeof(struct tc_pedit_key);
189 if (nla_len(pattr) < sizeof(*parm) + ksize) {
190 NL_SET_ERR_MSG_ATTR(extack, pattr, "Length of TCA_PEDIT_PARMS or TCA_PEDIT_PARMS_EX pedit attribute is invalid");
194 nparms = kzalloc(sizeof(*nparms), GFP_KERNEL);
198 nparms->tcfp_keys_ex =
199 tcf_pedit_keys_ex_parse(tb[TCA_PEDIT_KEYS_EX], parm->nkeys);
200 if (IS_ERR(nparms->tcfp_keys_ex)) {
201 ret = PTR_ERR(nparms->tcfp_keys_ex);
206 err = tcf_idr_check_alloc(tn, &index, a, bind);
208 ret = tcf_idr_create_from_flags(tn, index, est, a,
209 &act_pedit_ops, bind, flags);
211 tcf_idr_cleanup(tn, index);
215 } else if (err > 0) {
218 if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
227 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
233 nparms->tcfp_off_max_hint = 0;
234 nparms->tcfp_flags = parm->flags;
235 nparms->tcfp_nkeys = parm->nkeys;
237 nparms->tcfp_keys = kmalloc(ksize, GFP_KERNEL);
238 if (!nparms->tcfp_keys) {
243 memcpy(nparms->tcfp_keys, parm->keys, ksize);
245 for (i = 0; i < nparms->tcfp_nkeys; ++i) {
246 u32 cur = nparms->tcfp_keys[i].off;
248 /* sanitize the shift value for any later use */
249 nparms->tcfp_keys[i].shift = min_t(size_t,
250 BITS_PER_TYPE(int) - 1,
251 nparms->tcfp_keys[i].shift);
253 /* The AT option can read a single byte, we can bound the actual
254 * value with uchar max.
256 cur += (0xff & nparms->tcfp_keys[i].offmask) >> nparms->tcfp_keys[i].shift;
258 /* Each key touches 4 bytes starting from the computed offset */
259 nparms->tcfp_off_max_hint =
260 max(nparms->tcfp_off_max_hint, cur + 4);
265 spin_lock_bh(&p->tcf_lock);
266 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
267 oparms = rcu_replace_pointer(p->parms, nparms, 1);
268 spin_unlock_bh(&p->tcf_lock);
271 call_rcu(&oparms->rcu, tcf_pedit_cleanup_rcu);
274 tcf_chain_put_by_act(goto_ch);
280 tcf_chain_put_by_act(goto_ch);
282 tcf_idr_release(*a, bind);
284 kfree(nparms->tcfp_keys_ex);
290 static void tcf_pedit_cleanup(struct tc_action *a)
292 struct tcf_pedit *p = to_pedit(a);
293 struct tcf_pedit_parms *parms;
295 parms = rcu_dereference_protected(p->parms, 1);
298 call_rcu(&parms->rcu, tcf_pedit_cleanup_rcu);
301 static bool offset_valid(struct sk_buff *skb, int offset)
303 if (offset > 0 && offset > skb->len)
306 if (offset < 0 && -offset > skb_headroom(skb))
312 static int pedit_skb_hdr_offset(struct sk_buff *skb,
313 enum pedit_header_type htype, int *hoffset)
318 case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
319 if (skb_mac_header_was_set(skb)) {
320 *hoffset = skb_mac_offset(skb);
324 case TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK:
325 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
326 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
327 *hoffset = skb_network_offset(skb);
330 case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
331 case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
332 if (skb_transport_header_was_set(skb)) {
333 *hoffset = skb_transport_offset(skb);
345 TC_INDIRECT_SCOPE int tcf_pedit_act(struct sk_buff *skb,
346 const struct tc_action *a,
347 struct tcf_result *res)
349 enum pedit_header_type htype = TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK;
350 enum pedit_cmd cmd = TCA_PEDIT_KEY_EX_CMD_SET;
351 struct tcf_pedit *p = to_pedit(a);
352 struct tcf_pedit_key_ex *tkey_ex;
353 struct tcf_pedit_parms *parms;
354 struct tc_pedit_key *tkey;
358 parms = rcu_dereference_bh(p->parms);
360 max_offset = (skb_transport_header_was_set(skb) ?
361 skb_transport_offset(skb) :
362 skb_network_offset(skb)) +
363 parms->tcfp_off_max_hint;
364 if (skb_ensure_writable(skb, min(skb->len, max_offset)))
367 tcf_lastuse_update(&p->tcf_tm);
368 tcf_action_update_bstats(&p->common, skb);
370 tkey = parms->tcfp_keys;
371 tkey_ex = parms->tcfp_keys_ex;
373 for (i = parms->tcfp_nkeys; i > 0; i--, tkey++) {
374 int offset = tkey->off;
381 htype = tkey_ex->htype;
387 rc = pedit_skb_hdr_offset(skb, htype, &hoffset);
389 pr_info("tc action pedit bad header type specified (0x%x)\n",
397 if (!offset_valid(skb, hoffset + tkey->at)) {
398 pr_info("tc action pedit 'at' offset %d out of bounds\n",
402 d = skb_header_pointer(skb, hoffset + tkey->at,
406 offset += (*d & tkey->offmask) >> tkey->shift;
410 pr_info("tc action pedit offset must be on 32 bit boundaries\n");
414 if (!offset_valid(skb, hoffset + offset)) {
415 pr_info("tc action pedit offset %d out of bounds\n",
420 ptr = skb_header_pointer(skb, hoffset + offset,
421 sizeof(hdata), &hdata);
424 /* just do it, baby */
426 case TCA_PEDIT_KEY_EX_CMD_SET:
429 case TCA_PEDIT_KEY_EX_CMD_ADD:
430 val = (*ptr + tkey->val) & ~tkey->mask;
433 pr_info("tc action pedit bad command (%d)\n",
438 *ptr = ((*ptr & tkey->mask) ^ val);
440 skb_store_bits(skb, hoffset + offset, ptr, 4);
446 spin_lock(&p->tcf_lock);
447 p->tcf_qstats.overlimits++;
448 spin_unlock(&p->tcf_lock);
450 return p->tcf_action;
453 static void tcf_pedit_stats_update(struct tc_action *a, u64 bytes, u64 packets,
454 u64 drops, u64 lastuse, bool hw)
456 struct tcf_pedit *d = to_pedit(a);
457 struct tcf_t *tm = &d->tcf_tm;
459 tcf_action_update_stats(a, bytes, packets, drops, hw);
460 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
463 static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,
466 unsigned char *b = skb_tail_pointer(skb);
467 struct tcf_pedit *p = to_pedit(a);
468 struct tcf_pedit_parms *parms;
469 struct tc_pedit *opt;
473 spin_lock_bh(&p->tcf_lock);
474 parms = rcu_dereference_protected(p->parms, 1);
475 s = struct_size(opt, keys, parms->tcfp_nkeys);
477 opt = kzalloc(s, GFP_ATOMIC);
478 if (unlikely(!opt)) {
479 spin_unlock_bh(&p->tcf_lock);
483 memcpy(opt->keys, parms->tcfp_keys,
484 flex_array_size(opt, keys, parms->tcfp_nkeys));
485 opt->index = p->tcf_index;
486 opt->nkeys = parms->tcfp_nkeys;
487 opt->flags = parms->tcfp_flags;
488 opt->action = p->tcf_action;
489 opt->refcnt = refcount_read(&p->tcf_refcnt) - ref;
490 opt->bindcnt = atomic_read(&p->tcf_bindcnt) - bind;
492 if (parms->tcfp_keys_ex) {
493 if (tcf_pedit_key_ex_dump(skb, parms->tcfp_keys_ex,
495 goto nla_put_failure;
497 if (nla_put(skb, TCA_PEDIT_PARMS_EX, s, opt))
498 goto nla_put_failure;
500 if (nla_put(skb, TCA_PEDIT_PARMS, s, opt))
501 goto nla_put_failure;
504 tcf_tm_dump(&t, &p->tcf_tm);
505 if (nla_put_64bit(skb, TCA_PEDIT_TM, sizeof(t), &t, TCA_PEDIT_PAD))
506 goto nla_put_failure;
507 spin_unlock_bh(&p->tcf_lock);
513 spin_unlock_bh(&p->tcf_lock);
519 static int tcf_pedit_offload_act_setup(struct tc_action *act, void *entry_data,
520 u32 *index_inc, bool bind,
521 struct netlink_ext_ack *extack)
524 struct flow_action_entry *entry = entry_data;
527 for (k = 0; k < tcf_pedit_nkeys(act); k++) {
528 switch (tcf_pedit_cmd(act, k)) {
529 case TCA_PEDIT_KEY_EX_CMD_SET:
530 entry->id = FLOW_ACTION_MANGLE;
532 case TCA_PEDIT_KEY_EX_CMD_ADD:
533 entry->id = FLOW_ACTION_ADD;
536 NL_SET_ERR_MSG_MOD(extack, "Unsupported pedit command offload");
539 entry->mangle.htype = tcf_pedit_htype(act, k);
540 entry->mangle.mask = tcf_pedit_mask(act, k);
541 entry->mangle.val = tcf_pedit_val(act, k);
542 entry->mangle.offset = tcf_pedit_offset(act, k);
543 entry->hw_stats = tc_act_hw_stats(act->hw_stats);
548 struct flow_offload_action *fl_action = entry_data;
549 u32 cmd = tcf_pedit_cmd(act, 0);
553 case TCA_PEDIT_KEY_EX_CMD_SET:
554 fl_action->id = FLOW_ACTION_MANGLE;
556 case TCA_PEDIT_KEY_EX_CMD_ADD:
557 fl_action->id = FLOW_ACTION_ADD;
560 NL_SET_ERR_MSG_MOD(extack, "Unsupported pedit command offload");
564 for (k = 1; k < tcf_pedit_nkeys(act); k++) {
565 if (cmd != tcf_pedit_cmd(act, k)) {
566 NL_SET_ERR_MSG_MOD(extack, "Unsupported pedit command offload");
575 static struct tc_action_ops act_pedit_ops = {
578 .owner = THIS_MODULE,
579 .act = tcf_pedit_act,
580 .stats_update = tcf_pedit_stats_update,
581 .dump = tcf_pedit_dump,
582 .cleanup = tcf_pedit_cleanup,
583 .init = tcf_pedit_init,
584 .offload_act_setup = tcf_pedit_offload_act_setup,
585 .size = sizeof(struct tcf_pedit),
588 static __net_init int pedit_init_net(struct net *net)
590 struct tc_action_net *tn = net_generic(net, act_pedit_ops.net_id);
592 return tc_action_net_init(net, tn, &act_pedit_ops);
595 static void __net_exit pedit_exit_net(struct list_head *net_list)
597 tc_action_net_exit(net_list, act_pedit_ops.net_id);
600 static struct pernet_operations pedit_net_ops = {
601 .init = pedit_init_net,
602 .exit_batch = pedit_exit_net,
603 .id = &act_pedit_ops.net_id,
604 .size = sizeof(struct tc_action_net),
607 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
608 MODULE_DESCRIPTION("Generic Packet Editor actions");
609 MODULE_LICENSE("GPL");
611 static int __init pedit_init_module(void)
613 return tcf_register_action(&act_pedit_ops, &pedit_net_ops);
616 static void __exit pedit_cleanup_module(void)
618 tcf_unregister_action(&act_pedit_ops, &pedit_net_ops);
621 module_init(pedit_init_module);
622 module_exit(pedit_cleanup_module);