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>
17 #include <linux/ipv6.h>
18 #include <linux/slab.h>
20 #include <net/netlink.h>
21 #include <net/pkt_sched.h>
22 #include <linux/tc_act/tc_pedit.h>
23 #include <net/tc_act/tc_pedit.h>
24 #include <uapi/linux/tc_act/tc_pedit.h>
25 #include <net/pkt_cls.h>
27 static struct tc_action_ops act_pedit_ops;
29 static const struct nla_policy pedit_policy[TCA_PEDIT_MAX + 1] = {
30 [TCA_PEDIT_PARMS] = { .len = sizeof(struct tc_pedit) },
31 [TCA_PEDIT_KEYS_EX] = { .type = NLA_NESTED },
34 static const struct nla_policy pedit_key_ex_policy[TCA_PEDIT_KEY_EX_MAX + 1] = {
35 [TCA_PEDIT_KEY_EX_HTYPE] = { .type = NLA_U16 },
36 [TCA_PEDIT_KEY_EX_CMD] = { .type = NLA_U16 },
39 static struct tcf_pedit_key_ex *tcf_pedit_keys_ex_parse(struct nlattr *nla,
42 struct tcf_pedit_key_ex *keys_ex;
43 struct tcf_pedit_key_ex *k;
44 const struct nlattr *ka;
51 keys_ex = kcalloc(n, sizeof(*k), GFP_KERNEL);
53 return ERR_PTR(-ENOMEM);
57 nla_for_each_nested(ka, nla, rem) {
58 struct nlattr *tb[TCA_PEDIT_KEY_EX_MAX + 1];
66 if (nla_type(ka) != TCA_PEDIT_KEY_EX) {
71 err = nla_parse_nested_deprecated(tb, TCA_PEDIT_KEY_EX_MAX,
72 ka, pedit_key_ex_policy,
77 if (!tb[TCA_PEDIT_KEY_EX_HTYPE] ||
78 !tb[TCA_PEDIT_KEY_EX_CMD]) {
83 k->htype = nla_get_u16(tb[TCA_PEDIT_KEY_EX_HTYPE]);
84 k->cmd = nla_get_u16(tb[TCA_PEDIT_KEY_EX_CMD]);
86 if (k->htype > TCA_PEDIT_HDR_TYPE_MAX ||
87 k->cmd > TCA_PEDIT_CMD_MAX) {
107 static int tcf_pedit_key_ex_dump(struct sk_buff *skb,
108 struct tcf_pedit_key_ex *keys_ex, int n)
110 struct nlattr *keys_start = nla_nest_start_noflag(skb,
116 struct nlattr *key_start;
118 key_start = nla_nest_start_noflag(skb, TCA_PEDIT_KEY_EX);
122 if (nla_put_u16(skb, TCA_PEDIT_KEY_EX_HTYPE, keys_ex->htype) ||
123 nla_put_u16(skb, TCA_PEDIT_KEY_EX_CMD, keys_ex->cmd))
126 nla_nest_end(skb, key_start);
131 nla_nest_end(skb, keys_start);
135 nla_nest_cancel(skb, keys_start);
139 static void tcf_pedit_cleanup_rcu(struct rcu_head *head)
141 struct tcf_pedit_parms *parms =
142 container_of(head, struct tcf_pedit_parms, rcu);
144 kfree(parms->tcfp_keys_ex);
145 kfree(parms->tcfp_keys);
150 static int tcf_pedit_init(struct net *net, struct nlattr *nla,
151 struct nlattr *est, struct tc_action **a,
152 struct tcf_proto *tp, u32 flags,
153 struct netlink_ext_ack *extack)
155 struct tc_action_net *tn = net_generic(net, act_pedit_ops.net_id);
156 bool bind = flags & TCA_ACT_FLAGS_BIND;
157 struct tcf_chain *goto_ch = NULL;
158 struct tcf_pedit_parms *oparms, *nparms;
159 struct nlattr *tb[TCA_PEDIT_MAX + 1];
160 struct tc_pedit *parm;
161 struct nlattr *pattr;
168 NL_SET_ERR_MSG_MOD(extack, "Pedit requires attributes to be passed");
172 err = nla_parse_nested_deprecated(tb, TCA_PEDIT_MAX, nla,
177 pattr = tb[TCA_PEDIT_PARMS];
179 pattr = tb[TCA_PEDIT_PARMS_EX];
181 NL_SET_ERR_MSG_MOD(extack, "Missing required TCA_PEDIT_PARMS or TCA_PEDIT_PARMS_EX pedit attribute");
185 parm = nla_data(pattr);
188 err = tcf_idr_check_alloc(tn, &index, a, bind);
190 ret = tcf_idr_create_from_flags(tn, index, est, a,
191 &act_pedit_ops, bind, flags);
193 tcf_idr_cleanup(tn, index);
197 } else if (err > 0) {
200 if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
209 NL_SET_ERR_MSG_MOD(extack, "Pedit requires keys to be passed");
213 ksize = parm->nkeys * sizeof(struct tc_pedit_key);
214 if (nla_len(pattr) < sizeof(*parm) + ksize) {
215 NL_SET_ERR_MSG_ATTR(extack, pattr, "Length of TCA_PEDIT_PARMS or TCA_PEDIT_PARMS_EX pedit attribute is invalid");
220 nparms = kzalloc(sizeof(*nparms), GFP_KERNEL);
226 nparms->tcfp_keys_ex =
227 tcf_pedit_keys_ex_parse(tb[TCA_PEDIT_KEYS_EX], parm->nkeys);
228 if (IS_ERR(nparms->tcfp_keys_ex)) {
229 ret = PTR_ERR(nparms->tcfp_keys_ex);
233 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
239 nparms->tcfp_off_max_hint = 0;
240 nparms->tcfp_flags = parm->flags;
241 nparms->tcfp_nkeys = parm->nkeys;
243 nparms->tcfp_keys = kmalloc(ksize, GFP_KERNEL);
244 if (!nparms->tcfp_keys) {
249 memcpy(nparms->tcfp_keys, parm->keys, ksize);
251 for (i = 0; i < nparms->tcfp_nkeys; ++i) {
252 u32 cur = nparms->tcfp_keys[i].off;
254 /* sanitize the shift value for any later use */
255 nparms->tcfp_keys[i].shift = min_t(size_t,
256 BITS_PER_TYPE(int) - 1,
257 nparms->tcfp_keys[i].shift);
259 /* The AT option can read a single byte, we can bound the actual
260 * value with uchar max.
262 cur += (0xff & nparms->tcfp_keys[i].offmask) >> nparms->tcfp_keys[i].shift;
264 /* Each key touches 4 bytes starting from the computed offset */
265 nparms->tcfp_off_max_hint =
266 max(nparms->tcfp_off_max_hint, cur + 4);
271 spin_lock_bh(&p->tcf_lock);
272 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
273 oparms = rcu_replace_pointer(p->parms, nparms, 1);
274 spin_unlock_bh(&p->tcf_lock);
277 call_rcu(&oparms->rcu, tcf_pedit_cleanup_rcu);
280 tcf_chain_put_by_act(goto_ch);
286 tcf_chain_put_by_act(goto_ch);
288 kfree(nparms->tcfp_keys_ex);
292 tcf_idr_release(*a, bind);
296 static void tcf_pedit_cleanup(struct tc_action *a)
298 struct tcf_pedit *p = to_pedit(a);
299 struct tcf_pedit_parms *parms;
301 parms = rcu_dereference_protected(p->parms, 1);
304 call_rcu(&parms->rcu, tcf_pedit_cleanup_rcu);
307 static bool offset_valid(struct sk_buff *skb, int offset)
309 if (offset > 0 && offset > skb->len)
312 if (offset < 0 && -offset > skb_headroom(skb))
318 static int pedit_l4_skb_offset(struct sk_buff *skb, int *hoffset, const int header_type)
320 const int noff = skb_network_offset(skb);
324 switch (skb->protocol) {
325 case htons(ETH_P_IP): {
326 const struct iphdr *iph = skb_header_pointer(skb, noff, sizeof(_iph), &_iph);
330 *hoffset = noff + iph->ihl * 4;
334 case htons(ETH_P_IPV6):
335 ret = ipv6_find_hdr(skb, hoffset, header_type, NULL, NULL) == header_type ? 0 : -EINVAL;
342 static int pedit_skb_hdr_offset(struct sk_buff *skb,
343 enum pedit_header_type htype, int *hoffset)
346 /* 'htype' is validated in the netlink parsing */
348 case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
349 if (skb_mac_header_was_set(skb)) {
350 *hoffset = skb_mac_offset(skb);
354 case TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK:
355 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
356 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
357 *hoffset = skb_network_offset(skb);
360 case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
361 ret = pedit_l4_skb_offset(skb, hoffset, IPPROTO_TCP);
363 case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
364 ret = pedit_l4_skb_offset(skb, hoffset, IPPROTO_UDP);
372 static int tcf_pedit_act(struct sk_buff *skb, const struct tc_action *a,
373 struct tcf_result *res)
375 enum pedit_header_type htype = TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK;
376 enum pedit_cmd cmd = TCA_PEDIT_KEY_EX_CMD_SET;
377 struct tcf_pedit *p = to_pedit(a);
378 struct tcf_pedit_key_ex *tkey_ex;
379 struct tcf_pedit_parms *parms;
380 struct tc_pedit_key *tkey;
384 parms = rcu_dereference_bh(p->parms);
386 max_offset = (skb_transport_header_was_set(skb) ?
387 skb_transport_offset(skb) :
388 skb_network_offset(skb)) +
389 parms->tcfp_off_max_hint;
390 if (skb_ensure_writable(skb, min(skb->len, max_offset)))
393 tcf_lastuse_update(&p->tcf_tm);
394 tcf_action_update_bstats(&p->common, skb);
396 tkey = parms->tcfp_keys;
397 tkey_ex = parms->tcfp_keys_ex;
399 for (i = parms->tcfp_nkeys; i > 0; i--, tkey++) {
400 int offset = tkey->off;
407 htype = tkey_ex->htype;
413 rc = pedit_skb_hdr_offset(skb, htype, &hoffset);
415 pr_info_ratelimited("tc action pedit unable to extract header offset for header type (0x%x)\n", htype);
422 if (!offset_valid(skb, hoffset + tkey->at)) {
423 pr_info("tc action pedit 'at' offset %d out of bounds\n",
427 d = skb_header_pointer(skb, hoffset + tkey->at,
431 offset += (*d & tkey->offmask) >> tkey->shift;
435 pr_info("tc action pedit offset must be on 32 bit boundaries\n");
439 if (!offset_valid(skb, hoffset + offset)) {
440 pr_info("tc action pedit offset %d out of bounds\n",
445 ptr = skb_header_pointer(skb, hoffset + offset,
446 sizeof(hdata), &hdata);
449 /* just do it, baby */
451 case TCA_PEDIT_KEY_EX_CMD_SET:
454 case TCA_PEDIT_KEY_EX_CMD_ADD:
455 val = (*ptr + tkey->val) & ~tkey->mask;
458 pr_info("tc action pedit bad command (%d)\n",
463 *ptr = ((*ptr & tkey->mask) ^ val);
465 skb_store_bits(skb, hoffset + offset, ptr, 4);
471 spin_lock(&p->tcf_lock);
472 p->tcf_qstats.overlimits++;
473 spin_unlock(&p->tcf_lock);
475 return p->tcf_action;
478 static void tcf_pedit_stats_update(struct tc_action *a, u64 bytes, u64 packets,
479 u64 drops, u64 lastuse, bool hw)
481 struct tcf_pedit *d = to_pedit(a);
482 struct tcf_t *tm = &d->tcf_tm;
484 tcf_action_update_stats(a, bytes, packets, drops, hw);
485 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
488 static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,
491 unsigned char *b = skb_tail_pointer(skb);
492 struct tcf_pedit *p = to_pedit(a);
493 struct tcf_pedit_parms *parms;
494 struct tc_pedit *opt;
498 spin_lock_bh(&p->tcf_lock);
499 parms = rcu_dereference_protected(p->parms, 1);
500 s = struct_size(opt, keys, parms->tcfp_nkeys);
502 opt = kzalloc(s, GFP_ATOMIC);
503 if (unlikely(!opt)) {
504 spin_unlock_bh(&p->tcf_lock);
508 memcpy(opt->keys, parms->tcfp_keys,
509 flex_array_size(opt, keys, parms->tcfp_nkeys));
510 opt->index = p->tcf_index;
511 opt->nkeys = parms->tcfp_nkeys;
512 opt->flags = parms->tcfp_flags;
513 opt->action = p->tcf_action;
514 opt->refcnt = refcount_read(&p->tcf_refcnt) - ref;
515 opt->bindcnt = atomic_read(&p->tcf_bindcnt) - bind;
517 if (parms->tcfp_keys_ex) {
518 if (tcf_pedit_key_ex_dump(skb, parms->tcfp_keys_ex,
520 goto nla_put_failure;
522 if (nla_put(skb, TCA_PEDIT_PARMS_EX, s, opt))
523 goto nla_put_failure;
525 if (nla_put(skb, TCA_PEDIT_PARMS, s, opt))
526 goto nla_put_failure;
529 tcf_tm_dump(&t, &p->tcf_tm);
530 if (nla_put_64bit(skb, TCA_PEDIT_TM, sizeof(t), &t, TCA_PEDIT_PAD))
531 goto nla_put_failure;
532 spin_unlock_bh(&p->tcf_lock);
538 spin_unlock_bh(&p->tcf_lock);
544 static int tcf_pedit_offload_act_setup(struct tc_action *act, void *entry_data,
545 u32 *index_inc, bool bind,
546 struct netlink_ext_ack *extack)
549 struct flow_action_entry *entry = entry_data;
552 for (k = 0; k < tcf_pedit_nkeys(act); k++) {
553 switch (tcf_pedit_cmd(act, k)) {
554 case TCA_PEDIT_KEY_EX_CMD_SET:
555 entry->id = FLOW_ACTION_MANGLE;
557 case TCA_PEDIT_KEY_EX_CMD_ADD:
558 entry->id = FLOW_ACTION_ADD;
561 NL_SET_ERR_MSG_MOD(extack, "Unsupported pedit command offload");
564 entry->mangle.htype = tcf_pedit_htype(act, k);
565 entry->mangle.mask = tcf_pedit_mask(act, k);
566 entry->mangle.val = tcf_pedit_val(act, k);
567 entry->mangle.offset = tcf_pedit_offset(act, k);
568 entry->hw_stats = tc_act_hw_stats(act->hw_stats);
579 static struct tc_action_ops act_pedit_ops = {
582 .owner = THIS_MODULE,
583 .act = tcf_pedit_act,
584 .stats_update = tcf_pedit_stats_update,
585 .dump = tcf_pedit_dump,
586 .cleanup = tcf_pedit_cleanup,
587 .init = tcf_pedit_init,
588 .offload_act_setup = tcf_pedit_offload_act_setup,
589 .size = sizeof(struct tcf_pedit),
592 static __net_init int pedit_init_net(struct net *net)
594 struct tc_action_net *tn = net_generic(net, act_pedit_ops.net_id);
596 return tc_action_net_init(net, tn, &act_pedit_ops);
599 static void __net_exit pedit_exit_net(struct list_head *net_list)
601 tc_action_net_exit(net_list, act_pedit_ops.net_id);
604 static struct pernet_operations pedit_net_ops = {
605 .init = pedit_init_net,
606 .exit_batch = pedit_exit_net,
607 .id = &act_pedit_ops.net_id,
608 .size = sizeof(struct tc_action_net),
611 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
612 MODULE_DESCRIPTION("Generic Packet Editor actions");
613 MODULE_LICENSE("GPL");
615 static int __init pedit_init_module(void)
617 return tcf_register_action(&act_pedit_ops, &pedit_net_ops);
620 static void __exit pedit_cleanup_module(void)
622 tcf_unregister_action(&act_pedit_ops, &pedit_net_ops);
625 module_init(pedit_init_module);
626 module_exit(pedit_cleanup_module);