19952f691SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2ca9b0e27SAlexander Duyck /* 3ca9b0e27SAlexander Duyck * Copyright (c) 2008, Intel Corporation. 4ca9b0e27SAlexander Duyck * 5ca9b0e27SAlexander Duyck * Author: Alexander Duyck <alexander.h.duyck@intel.com> 6ca9b0e27SAlexander Duyck */ 7ca9b0e27SAlexander Duyck 8ca9b0e27SAlexander Duyck #include <linux/module.h> 9ca9b0e27SAlexander Duyck #include <linux/init.h> 10ca9b0e27SAlexander Duyck #include <linux/kernel.h> 11ca9b0e27SAlexander Duyck #include <linux/skbuff.h> 12ca9b0e27SAlexander Duyck #include <linux/rtnetlink.h> 13ca9b0e27SAlexander Duyck #include <net/netlink.h> 14ca9b0e27SAlexander Duyck #include <net/pkt_sched.h> 15e7e3728bSQiaobin Fu #include <net/ip.h> 16e7e3728bSQiaobin Fu #include <net/ipv6.h> 17e7e3728bSQiaobin Fu #include <net/dsfield.h> 18ec7727bbSDavide Caratti #include <net/pkt_cls.h> 19ca9b0e27SAlexander Duyck 20ca9b0e27SAlexander Duyck #include <linux/tc_act/tc_skbedit.h> 21ca9b0e27SAlexander Duyck #include <net/tc_act/tc_skbedit.h> 22ca9b0e27SAlexander Duyck 23c7d03a00SAlexey Dobriyan static unsigned int skbedit_net_id; 24a85a970aSWANG Cong static struct tc_action_ops act_skbedit_ops; 25ddf97ccdSWANG Cong 26*38a6f086STonghao Zhang static u16 tcf_skbedit_hash(struct tcf_skbedit_params *params, 27*38a6f086STonghao Zhang struct sk_buff *skb) 28*38a6f086STonghao Zhang { 29*38a6f086STonghao Zhang u16 queue_mapping = params->queue_mapping; 30*38a6f086STonghao Zhang 31*38a6f086STonghao Zhang if (params->flags & SKBEDIT_F_TXQ_SKBHASH) { 32*38a6f086STonghao Zhang u32 hash = skb_get_hash(skb); 33*38a6f086STonghao Zhang 34*38a6f086STonghao Zhang queue_mapping += hash % params->mapping_mod; 35*38a6f086STonghao Zhang } 36*38a6f086STonghao Zhang 37*38a6f086STonghao Zhang return netdev_cap_txqueue(skb->dev, queue_mapping); 38*38a6f086STonghao Zhang } 39*38a6f086STonghao Zhang 4045da1dacSJamal Hadi Salim static int tcf_skbedit_act(struct sk_buff *skb, const struct tc_action *a, 41ca9b0e27SAlexander Duyck struct tcf_result *res) 42ca9b0e27SAlexander Duyck { 43a85a970aSWANG Cong struct tcf_skbedit *d = to_skbedit(a); 44c749cddaSDavide Caratti struct tcf_skbedit_params *params; 45c749cddaSDavide Caratti int action; 46ca9b0e27SAlexander Duyck 479c4a4e48SJamal Hadi Salim tcf_lastuse_update(&d->tcf_tm); 4850dc9a85SAhmed S. Darwish bstats_update(this_cpu_ptr(d->common.cpu_bstats), skb); 49ca9b0e27SAlexander Duyck 507fd4b288SPaolo Abeni params = rcu_dereference_bh(d->params); 51c749cddaSDavide Caratti action = READ_ONCE(d->tcf_action); 52c749cddaSDavide Caratti 53c749cddaSDavide Caratti if (params->flags & SKBEDIT_F_PRIORITY) 54c749cddaSDavide Caratti skb->priority = params->priority; 55c749cddaSDavide Caratti if (params->flags & SKBEDIT_F_INHERITDSFIELD) { 56e7e3728bSQiaobin Fu int wlen = skb_network_offset(skb); 57e7e3728bSQiaobin Fu 58d7bf2ebeSToke Høiland-Jørgensen switch (skb_protocol(skb, true)) { 59e7e3728bSQiaobin Fu case htons(ETH_P_IP): 60e7e3728bSQiaobin Fu wlen += sizeof(struct iphdr); 61e7e3728bSQiaobin Fu if (!pskb_may_pull(skb, wlen)) 62e7e3728bSQiaobin Fu goto err; 63e7e3728bSQiaobin Fu skb->priority = ipv4_get_dsfield(ip_hdr(skb)) >> 2; 64e7e3728bSQiaobin Fu break; 65e7e3728bSQiaobin Fu 66e7e3728bSQiaobin Fu case htons(ETH_P_IPV6): 67e7e3728bSQiaobin Fu wlen += sizeof(struct ipv6hdr); 68e7e3728bSQiaobin Fu if (!pskb_may_pull(skb, wlen)) 69e7e3728bSQiaobin Fu goto err; 70e7e3728bSQiaobin Fu skb->priority = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2; 71e7e3728bSQiaobin Fu break; 72e7e3728bSQiaobin Fu } 73e7e3728bSQiaobin Fu } 74c749cddaSDavide Caratti if (params->flags & SKBEDIT_F_QUEUE_MAPPING && 752f1e85b1STonghao Zhang skb->dev->real_num_tx_queues > params->queue_mapping) { 762f1e85b1STonghao Zhang #ifdef CONFIG_NET_EGRESS 772f1e85b1STonghao Zhang netdev_xmit_skip_txqueue(true); 782f1e85b1STonghao Zhang #endif 79*38a6f086STonghao Zhang skb_set_queue_mapping(skb, tcf_skbedit_hash(params, skb)); 802f1e85b1STonghao Zhang } 81c749cddaSDavide Caratti if (params->flags & SKBEDIT_F_MARK) { 82c749cddaSDavide Caratti skb->mark &= ~params->mask; 83c749cddaSDavide Caratti skb->mark |= params->mark & params->mask; 844fe77d82SAntonio Quartulli } 85c749cddaSDavide Caratti if (params->flags & SKBEDIT_F_PTYPE) 86c749cddaSDavide Caratti skb->pkt_type = params->ptype; 87c749cddaSDavide Caratti return action; 887fd4b288SPaolo Abeni 89e7e3728bSQiaobin Fu err: 906f3dfb0dSDavide Caratti qstats_drop_inc(this_cpu_ptr(d->common.cpu_qstats)); 917fd4b288SPaolo Abeni return TC_ACT_SHOT; 92ca9b0e27SAlexander Duyck } 93ca9b0e27SAlexander Duyck 94837cb17dSPetr Machata static void tcf_skbedit_stats_update(struct tc_action *a, u64 bytes, 954b61d3e8SPo Liu u64 packets, u64 drops, 964b61d3e8SPo Liu u64 lastuse, bool hw) 97837cb17dSPetr Machata { 98837cb17dSPetr Machata struct tcf_skbedit *d = to_skbedit(a); 99837cb17dSPetr Machata struct tcf_t *tm = &d->tcf_tm; 100837cb17dSPetr Machata 1014b61d3e8SPo Liu tcf_action_update_stats(a, bytes, packets, drops, hw); 102837cb17dSPetr Machata tm->lastuse = max_t(u64, tm->lastuse, lastuse); 103837cb17dSPetr Machata } 104837cb17dSPetr Machata 105ca9b0e27SAlexander Duyck static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = { 106ca9b0e27SAlexander Duyck [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) }, 107ca9b0e27SAlexander Duyck [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) }, 108ca9b0e27SAlexander Duyck [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) }, 1091c55d62eSjamal [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) }, 110ff202ee1SJamal Hadi Salim [TCA_SKBEDIT_PTYPE] = { .len = sizeof(u16) }, 1114fe77d82SAntonio Quartulli [TCA_SKBEDIT_MASK] = { .len = sizeof(u32) }, 112e7e3728bSQiaobin Fu [TCA_SKBEDIT_FLAGS] = { .len = sizeof(u64) }, 113*38a6f086STonghao Zhang [TCA_SKBEDIT_QUEUE_MAPPING_MAX] = { .len = sizeof(u16) }, 114ca9b0e27SAlexander Duyck }; 115ca9b0e27SAlexander Duyck 116c1b52739SBenjamin LaHaise static int tcf_skbedit_init(struct net *net, struct nlattr *nla, 117a85a970aSWANG Cong struct nlattr *est, struct tc_action **a, 118abbb0d33SVlad Buslov struct tcf_proto *tp, u32 act_flags, 119789871bbSVlad Buslov struct netlink_ext_ack *extack) 120ca9b0e27SAlexander Duyck { 121ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, skbedit_net_id); 122695176bfSCong Wang bool bind = act_flags & TCA_ACT_FLAGS_BIND; 1236d7a8df6SVlad Buslov struct tcf_skbedit_params *params_new; 124ca9b0e27SAlexander Duyck struct nlattr *tb[TCA_SKBEDIT_MAX + 1]; 125ec7727bbSDavide Caratti struct tcf_chain *goto_ch = NULL; 126ca9b0e27SAlexander Duyck struct tc_skbedit *parm; 127ca9b0e27SAlexander Duyck struct tcf_skbedit *d; 1284fe77d82SAntonio Quartulli u32 flags = 0, *priority = NULL, *mark = NULL, *mask = NULL; 129ff202ee1SJamal Hadi Salim u16 *queue_mapping = NULL, *ptype = NULL; 130*38a6f086STonghao Zhang u16 mapping_mod = 1; 131b2313077SWANG Cong bool exists = false; 132b2313077SWANG Cong int ret = 0, err; 1337be8ef2cSDmytro Linkin u32 index; 134ca9b0e27SAlexander Duyck 135ca9b0e27SAlexander Duyck if (nla == NULL) 136ca9b0e27SAlexander Duyck return -EINVAL; 137ca9b0e27SAlexander Duyck 1388cb08174SJohannes Berg err = nla_parse_nested_deprecated(tb, TCA_SKBEDIT_MAX, nla, 1398cb08174SJohannes Berg skbedit_policy, NULL); 140ca9b0e27SAlexander Duyck if (err < 0) 141ca9b0e27SAlexander Duyck return err; 142ca9b0e27SAlexander Duyck 143ca9b0e27SAlexander Duyck if (tb[TCA_SKBEDIT_PARMS] == NULL) 144ca9b0e27SAlexander Duyck return -EINVAL; 145ca9b0e27SAlexander Duyck 146ca9b0e27SAlexander Duyck if (tb[TCA_SKBEDIT_PRIORITY] != NULL) { 147ca9b0e27SAlexander Duyck flags |= SKBEDIT_F_PRIORITY; 148ca9b0e27SAlexander Duyck priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]); 149ca9b0e27SAlexander Duyck } 150ca9b0e27SAlexander Duyck 151ca9b0e27SAlexander Duyck if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) { 152ca9b0e27SAlexander Duyck flags |= SKBEDIT_F_QUEUE_MAPPING; 153ca9b0e27SAlexander Duyck queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]); 154ca9b0e27SAlexander Duyck } 1551c55d62eSjamal 156ff202ee1SJamal Hadi Salim if (tb[TCA_SKBEDIT_PTYPE] != NULL) { 157ff202ee1SJamal Hadi Salim ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]); 158ff202ee1SJamal Hadi Salim if (!skb_pkt_type_ok(*ptype)) 159ff202ee1SJamal Hadi Salim return -EINVAL; 160ff202ee1SJamal Hadi Salim flags |= SKBEDIT_F_PTYPE; 161ff202ee1SJamal Hadi Salim } 162ff202ee1SJamal Hadi Salim 1631c55d62eSjamal if (tb[TCA_SKBEDIT_MARK] != NULL) { 1641c55d62eSjamal flags |= SKBEDIT_F_MARK; 1651c55d62eSjamal mark = nla_data(tb[TCA_SKBEDIT_MARK]); 1661c55d62eSjamal } 1671c55d62eSjamal 1684fe77d82SAntonio Quartulli if (tb[TCA_SKBEDIT_MASK] != NULL) { 1694fe77d82SAntonio Quartulli flags |= SKBEDIT_F_MASK; 1704fe77d82SAntonio Quartulli mask = nla_data(tb[TCA_SKBEDIT_MASK]); 1714fe77d82SAntonio Quartulli } 1724fe77d82SAntonio Quartulli 173e7e3728bSQiaobin Fu if (tb[TCA_SKBEDIT_FLAGS] != NULL) { 174e7e3728bSQiaobin Fu u64 *pure_flags = nla_data(tb[TCA_SKBEDIT_FLAGS]); 175e7e3728bSQiaobin Fu 176*38a6f086STonghao Zhang if (*pure_flags & SKBEDIT_F_TXQ_SKBHASH) { 177*38a6f086STonghao Zhang u16 *queue_mapping_max; 178*38a6f086STonghao Zhang 179*38a6f086STonghao Zhang if (!tb[TCA_SKBEDIT_QUEUE_MAPPING] || 180*38a6f086STonghao Zhang !tb[TCA_SKBEDIT_QUEUE_MAPPING_MAX]) { 181*38a6f086STonghao Zhang NL_SET_ERR_MSG_MOD(extack, "Missing required range of queue_mapping."); 182*38a6f086STonghao Zhang return -EINVAL; 183*38a6f086STonghao Zhang } 184*38a6f086STonghao Zhang 185*38a6f086STonghao Zhang queue_mapping_max = 186*38a6f086STonghao Zhang nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING_MAX]); 187*38a6f086STonghao Zhang if (*queue_mapping_max < *queue_mapping) { 188*38a6f086STonghao Zhang NL_SET_ERR_MSG_MOD(extack, "The range of queue_mapping is invalid, max < min."); 189*38a6f086STonghao Zhang return -EINVAL; 190*38a6f086STonghao Zhang } 191*38a6f086STonghao Zhang 192*38a6f086STonghao Zhang mapping_mod = *queue_mapping_max - *queue_mapping + 1; 193*38a6f086STonghao Zhang flags |= SKBEDIT_F_TXQ_SKBHASH; 194*38a6f086STonghao Zhang } 195e7e3728bSQiaobin Fu if (*pure_flags & SKBEDIT_F_INHERITDSFIELD) 196e7e3728bSQiaobin Fu flags |= SKBEDIT_F_INHERITDSFIELD; 197e7e3728bSQiaobin Fu } 198e7e3728bSQiaobin Fu 199ca9b0e27SAlexander Duyck parm = nla_data(tb[TCA_SKBEDIT_PARMS]); 2007be8ef2cSDmytro Linkin index = parm->index; 2017be8ef2cSDmytro Linkin err = tcf_idr_check_alloc(tn, &index, a, bind); 2020190c1d4SVlad Buslov if (err < 0) 2030190c1d4SVlad Buslov return err; 2040190c1d4SVlad Buslov exists = err; 2055e1567aeSJamal Hadi Salim if (exists && bind) 2065e1567aeSJamal Hadi Salim return 0; 2075e1567aeSJamal Hadi Salim 2085e1567aeSJamal Hadi Salim if (!flags) { 209af5d0184SRoman Mashak if (exists) 21065a206c0SChris Mi tcf_idr_release(*a, bind); 2110190c1d4SVlad Buslov else 2127be8ef2cSDmytro Linkin tcf_idr_cleanup(tn, index); 2135e1567aeSJamal Hadi Salim return -EINVAL; 2145e1567aeSJamal Hadi Salim } 2155e1567aeSJamal Hadi Salim 2165e1567aeSJamal Hadi Salim if (!exists) { 2177be8ef2cSDmytro Linkin ret = tcf_idr_create(tn, index, est, a, 21840bd094dSBaowen Zheng &act_skbedit_ops, bind, true, act_flags); 2190190c1d4SVlad Buslov if (ret) { 2207be8ef2cSDmytro Linkin tcf_idr_cleanup(tn, index); 22186062033SWANG Cong return ret; 2220190c1d4SVlad Buslov } 223ca9b0e27SAlexander Duyck 224a85a970aSWANG Cong d = to_skbedit(*a); 225ca9b0e27SAlexander Duyck ret = ACT_P_CREATED; 226ca9b0e27SAlexander Duyck } else { 227a85a970aSWANG Cong d = to_skbedit(*a); 228695176bfSCong Wang if (!(act_flags & TCA_ACT_FLAGS_REPLACE)) { 22965a206c0SChris Mi tcf_idr_release(*a, bind); 230ca9b0e27SAlexander Duyck return -EEXIST; 231ca9b0e27SAlexander Duyck } 2324e8ddd7fSVlad Buslov } 233ec7727bbSDavide Caratti err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); 234ec7727bbSDavide Caratti if (err < 0) 235ec7727bbSDavide Caratti goto release_idr; 236ca9b0e27SAlexander Duyck 237c749cddaSDavide Caratti params_new = kzalloc(sizeof(*params_new), GFP_KERNEL); 238c749cddaSDavide Caratti if (unlikely(!params_new)) { 239ec7727bbSDavide Caratti err = -ENOMEM; 240ec7727bbSDavide Caratti goto put_chain; 241c749cddaSDavide Caratti } 242c749cddaSDavide Caratti 243c749cddaSDavide Caratti params_new->flags = flags; 244ca9b0e27SAlexander Duyck if (flags & SKBEDIT_F_PRIORITY) 245c749cddaSDavide Caratti params_new->priority = *priority; 246*38a6f086STonghao Zhang if (flags & SKBEDIT_F_QUEUE_MAPPING) { 247c749cddaSDavide Caratti params_new->queue_mapping = *queue_mapping; 248*38a6f086STonghao Zhang params_new->mapping_mod = mapping_mod; 249*38a6f086STonghao Zhang } 2501c55d62eSjamal if (flags & SKBEDIT_F_MARK) 251c749cddaSDavide Caratti params_new->mark = *mark; 252ff202ee1SJamal Hadi Salim if (flags & SKBEDIT_F_PTYPE) 253c749cddaSDavide Caratti params_new->ptype = *ptype; 2544fe77d82SAntonio Quartulli /* default behaviour is to use all the bits */ 255c749cddaSDavide Caratti params_new->mask = 0xffffffff; 2564fe77d82SAntonio Quartulli if (flags & SKBEDIT_F_MASK) 257c749cddaSDavide Caratti params_new->mask = *mask; 2581c55d62eSjamal 2596d7a8df6SVlad Buslov spin_lock_bh(&d->tcf_lock); 260ec7727bbSDavide Caratti goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); 261445d3749SPaul E. McKenney params_new = rcu_replace_pointer(d->params, params_new, 2626d7a8df6SVlad Buslov lockdep_is_held(&d->tcf_lock)); 2636d7a8df6SVlad Buslov spin_unlock_bh(&d->tcf_lock); 2646d7a8df6SVlad Buslov if (params_new) 2656d7a8df6SVlad Buslov kfree_rcu(params_new, rcu); 266ec7727bbSDavide Caratti if (goto_ch) 267ec7727bbSDavide Caratti tcf_chain_put_by_act(goto_ch); 268ca9b0e27SAlexander Duyck 269ca9b0e27SAlexander Duyck return ret; 270ec7727bbSDavide Caratti put_chain: 271ec7727bbSDavide Caratti if (goto_ch) 272ec7727bbSDavide Caratti tcf_chain_put_by_act(goto_ch); 273ec7727bbSDavide Caratti release_idr: 274ec7727bbSDavide Caratti tcf_idr_release(*a, bind); 275ec7727bbSDavide Caratti return err; 276ca9b0e27SAlexander Duyck } 277ca9b0e27SAlexander Duyck 278cc7ec456SEric Dumazet static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a, 279ca9b0e27SAlexander Duyck int bind, int ref) 280ca9b0e27SAlexander Duyck { 281ca9b0e27SAlexander Duyck unsigned char *b = skb_tail_pointer(skb); 282a85a970aSWANG Cong struct tcf_skbedit *d = to_skbedit(a); 283c749cddaSDavide Caratti struct tcf_skbedit_params *params; 2841c40be12SEric Dumazet struct tc_skbedit opt = { 2851c40be12SEric Dumazet .index = d->tcf_index, 286036bb443SVlad Buslov .refcnt = refcount_read(&d->tcf_refcnt) - ref, 287036bb443SVlad Buslov .bindcnt = atomic_read(&d->tcf_bindcnt) - bind, 2881c40be12SEric Dumazet }; 289e7e3728bSQiaobin Fu u64 pure_flags = 0; 290c749cddaSDavide Caratti struct tcf_t t; 291c749cddaSDavide Caratti 2926d7a8df6SVlad Buslov spin_lock_bh(&d->tcf_lock); 2936d7a8df6SVlad Buslov params = rcu_dereference_protected(d->params, 2946d7a8df6SVlad Buslov lockdep_is_held(&d->tcf_lock)); 2956d7a8df6SVlad Buslov opt.action = d->tcf_action; 296ca9b0e27SAlexander Duyck 2971b34ec43SDavid S. Miller if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt)) 2981b34ec43SDavid S. Miller goto nla_put_failure; 299c749cddaSDavide Caratti if ((params->flags & SKBEDIT_F_PRIORITY) && 300c749cddaSDavide Caratti nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, params->priority)) 3011b34ec43SDavid S. Miller goto nla_put_failure; 302c749cddaSDavide Caratti if ((params->flags & SKBEDIT_F_QUEUE_MAPPING) && 303c749cddaSDavide Caratti nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, params->queue_mapping)) 3041b34ec43SDavid S. Miller goto nla_put_failure; 305c749cddaSDavide Caratti if ((params->flags & SKBEDIT_F_MARK) && 306c749cddaSDavide Caratti nla_put_u32(skb, TCA_SKBEDIT_MARK, params->mark)) 3071b34ec43SDavid S. Miller goto nla_put_failure; 308c749cddaSDavide Caratti if ((params->flags & SKBEDIT_F_PTYPE) && 309c749cddaSDavide Caratti nla_put_u16(skb, TCA_SKBEDIT_PTYPE, params->ptype)) 310ff202ee1SJamal Hadi Salim goto nla_put_failure; 311c749cddaSDavide Caratti if ((params->flags & SKBEDIT_F_MASK) && 312c749cddaSDavide Caratti nla_put_u32(skb, TCA_SKBEDIT_MASK, params->mask)) 3134fe77d82SAntonio Quartulli goto nla_put_failure; 314c749cddaSDavide Caratti if (params->flags & SKBEDIT_F_INHERITDSFIELD) 315e7e3728bSQiaobin Fu pure_flags |= SKBEDIT_F_INHERITDSFIELD; 316*38a6f086STonghao Zhang if (params->flags & SKBEDIT_F_TXQ_SKBHASH) { 317*38a6f086STonghao Zhang if (nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING_MAX, 318*38a6f086STonghao Zhang params->queue_mapping + params->mapping_mod - 1)) 319*38a6f086STonghao Zhang goto nla_put_failure; 320*38a6f086STonghao Zhang 321*38a6f086STonghao Zhang pure_flags |= SKBEDIT_F_TXQ_SKBHASH; 322*38a6f086STonghao Zhang } 323e7e3728bSQiaobin Fu if (pure_flags != 0 && 324e7e3728bSQiaobin Fu nla_put(skb, TCA_SKBEDIT_FLAGS, sizeof(pure_flags), &pure_flags)) 325e7e3728bSQiaobin Fu goto nla_put_failure; 32648d8ee16SJamal Hadi Salim 32748d8ee16SJamal Hadi Salim tcf_tm_dump(&t, &d->tcf_tm); 3289854518eSNicolas Dichtel if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD)) 3291b34ec43SDavid S. Miller goto nla_put_failure; 3306d7a8df6SVlad Buslov spin_unlock_bh(&d->tcf_lock); 3316d7a8df6SVlad Buslov 332ca9b0e27SAlexander Duyck return skb->len; 333ca9b0e27SAlexander Duyck 334ca9b0e27SAlexander Duyck nla_put_failure: 3356d7a8df6SVlad Buslov spin_unlock_bh(&d->tcf_lock); 336ca9b0e27SAlexander Duyck nlmsg_trim(skb, b); 337ca9b0e27SAlexander Duyck return -1; 338ca9b0e27SAlexander Duyck } 339ca9b0e27SAlexander Duyck 340c749cddaSDavide Caratti static void tcf_skbedit_cleanup(struct tc_action *a) 341c749cddaSDavide Caratti { 342c749cddaSDavide Caratti struct tcf_skbedit *d = to_skbedit(a); 343c749cddaSDavide Caratti struct tcf_skbedit_params *params; 344c749cddaSDavide Caratti 345c749cddaSDavide Caratti params = rcu_dereference_protected(d->params, 1); 346c749cddaSDavide Caratti if (params) 347c749cddaSDavide Caratti kfree_rcu(params, rcu); 348c749cddaSDavide Caratti } 349c749cddaSDavide Caratti 350ddf97ccdSWANG Cong static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb, 351ddf97ccdSWANG Cong struct netlink_callback *cb, int type, 35241780105SAlexander Aring const struct tc_action_ops *ops, 35341780105SAlexander Aring struct netlink_ext_ack *extack) 354ddf97ccdSWANG Cong { 355ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, skbedit_net_id); 356ddf97ccdSWANG Cong 357b3620145SAlexander Aring return tcf_generic_walker(tn, skb, cb, type, ops, extack); 358ddf97ccdSWANG Cong } 359ddf97ccdSWANG Cong 360f061b48cSCong Wang static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index) 361ddf97ccdSWANG Cong { 362ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, skbedit_net_id); 363ddf97ccdSWANG Cong 36465a206c0SChris Mi return tcf_idr_search(tn, a, index); 365ddf97ccdSWANG Cong } 366ddf97ccdSWANG Cong 367e1fea322SRoman Mashak static size_t tcf_skbedit_get_fill_size(const struct tc_action *act) 368e1fea322SRoman Mashak { 369e1fea322SRoman Mashak return nla_total_size(sizeof(struct tc_skbedit)) 370e1fea322SRoman Mashak + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_PRIORITY */ 371e1fea322SRoman Mashak + nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_QUEUE_MAPPING */ 372*38a6f086STonghao Zhang + nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_QUEUE_MAPPING_MAX */ 373e1fea322SRoman Mashak + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_MARK */ 374e1fea322SRoman Mashak + nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_PTYPE */ 375e1fea322SRoman Mashak + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_MASK */ 376e1fea322SRoman Mashak + nla_total_size_64bit(sizeof(u64)); /* TCA_SKBEDIT_FLAGS */ 377e1fea322SRoman Mashak } 378e1fea322SRoman Mashak 379c54e1d92SBaowen Zheng static int tcf_skbedit_offload_act_setup(struct tc_action *act, void *entry_data, 380c2ccf84eSIdo Schimmel u32 *index_inc, bool bind, 381c2ccf84eSIdo Schimmel struct netlink_ext_ack *extack) 382c54e1d92SBaowen Zheng { 383c54e1d92SBaowen Zheng if (bind) { 384c54e1d92SBaowen Zheng struct flow_action_entry *entry = entry_data; 385c54e1d92SBaowen Zheng 386c54e1d92SBaowen Zheng if (is_tcf_skbedit_mark(act)) { 387c54e1d92SBaowen Zheng entry->id = FLOW_ACTION_MARK; 388c54e1d92SBaowen Zheng entry->mark = tcf_skbedit_mark(act); 389c54e1d92SBaowen Zheng } else if (is_tcf_skbedit_ptype(act)) { 390c54e1d92SBaowen Zheng entry->id = FLOW_ACTION_PTYPE; 391c54e1d92SBaowen Zheng entry->ptype = tcf_skbedit_ptype(act); 392c54e1d92SBaowen Zheng } else if (is_tcf_skbedit_priority(act)) { 393c54e1d92SBaowen Zheng entry->id = FLOW_ACTION_PRIORITY; 394c54e1d92SBaowen Zheng entry->priority = tcf_skbedit_priority(act); 395a9c64939SIdo Schimmel } else if (is_tcf_skbedit_queue_mapping(act)) { 396a9c64939SIdo Schimmel NL_SET_ERR_MSG_MOD(extack, "Offload not supported when \"queue_mapping\" option is used"); 397a9c64939SIdo Schimmel return -EOPNOTSUPP; 398a9c64939SIdo Schimmel } else if (is_tcf_skbedit_inheritdsfield(act)) { 399a9c64939SIdo Schimmel NL_SET_ERR_MSG_MOD(extack, "Offload not supported when \"inheritdsfield\" option is used"); 400a9c64939SIdo Schimmel return -EOPNOTSUPP; 401c54e1d92SBaowen Zheng } else { 402a9c64939SIdo Schimmel NL_SET_ERR_MSG_MOD(extack, "Unsupported skbedit option offload"); 403c54e1d92SBaowen Zheng return -EOPNOTSUPP; 404c54e1d92SBaowen Zheng } 405c54e1d92SBaowen Zheng *index_inc = 1; 406c54e1d92SBaowen Zheng } else { 4078cbfe939SBaowen Zheng struct flow_offload_action *fl_action = entry_data; 4088cbfe939SBaowen Zheng 4098cbfe939SBaowen Zheng if (is_tcf_skbedit_mark(act)) 4108cbfe939SBaowen Zheng fl_action->id = FLOW_ACTION_MARK; 4118cbfe939SBaowen Zheng else if (is_tcf_skbedit_ptype(act)) 4128cbfe939SBaowen Zheng fl_action->id = FLOW_ACTION_PTYPE; 4138cbfe939SBaowen Zheng else if (is_tcf_skbedit_priority(act)) 4148cbfe939SBaowen Zheng fl_action->id = FLOW_ACTION_PRIORITY; 4158cbfe939SBaowen Zheng else 416c54e1d92SBaowen Zheng return -EOPNOTSUPP; 417c54e1d92SBaowen Zheng } 418c54e1d92SBaowen Zheng 419c54e1d92SBaowen Zheng return 0; 420c54e1d92SBaowen Zheng } 421c54e1d92SBaowen Zheng 422ca9b0e27SAlexander Duyck static struct tc_action_ops act_skbedit_ops = { 423ca9b0e27SAlexander Duyck .kind = "skbedit", 424eddd2cf1SEli Cohen .id = TCA_ID_SKBEDIT, 425ca9b0e27SAlexander Duyck .owner = THIS_MODULE, 42645da1dacSJamal Hadi Salim .act = tcf_skbedit_act, 427837cb17dSPetr Machata .stats_update = tcf_skbedit_stats_update, 428ca9b0e27SAlexander Duyck .dump = tcf_skbedit_dump, 429ca9b0e27SAlexander Duyck .init = tcf_skbedit_init, 430c749cddaSDavide Caratti .cleanup = tcf_skbedit_cleanup, 431ddf97ccdSWANG Cong .walk = tcf_skbedit_walker, 432e1fea322SRoman Mashak .get_fill_size = tcf_skbedit_get_fill_size, 433ddf97ccdSWANG Cong .lookup = tcf_skbedit_search, 434c54e1d92SBaowen Zheng .offload_act_setup = tcf_skbedit_offload_act_setup, 435a85a970aSWANG Cong .size = sizeof(struct tcf_skbedit), 436ddf97ccdSWANG Cong }; 437ddf97ccdSWANG Cong 438ddf97ccdSWANG Cong static __net_init int skbedit_init_net(struct net *net) 439ddf97ccdSWANG Cong { 440ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, skbedit_net_id); 441ddf97ccdSWANG Cong 442981471bdSCong Wang return tc_action_net_init(net, tn, &act_skbedit_ops); 443ddf97ccdSWANG Cong } 444ddf97ccdSWANG Cong 445039af9c6SCong Wang static void __net_exit skbedit_exit_net(struct list_head *net_list) 446ddf97ccdSWANG Cong { 447039af9c6SCong Wang tc_action_net_exit(net_list, skbedit_net_id); 448ddf97ccdSWANG Cong } 449ddf97ccdSWANG Cong 450ddf97ccdSWANG Cong static struct pernet_operations skbedit_net_ops = { 451ddf97ccdSWANG Cong .init = skbedit_init_net, 452039af9c6SCong Wang .exit_batch = skbedit_exit_net, 453ddf97ccdSWANG Cong .id = &skbedit_net_id, 454ddf97ccdSWANG Cong .size = sizeof(struct tc_action_net), 455ca9b0e27SAlexander Duyck }; 456ca9b0e27SAlexander Duyck 457ca9b0e27SAlexander Duyck MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>"); 458ca9b0e27SAlexander Duyck MODULE_DESCRIPTION("SKB Editing"); 459ca9b0e27SAlexander Duyck MODULE_LICENSE("GPL"); 460ca9b0e27SAlexander Duyck 461ca9b0e27SAlexander Duyck static int __init skbedit_init_module(void) 462ca9b0e27SAlexander Duyck { 463ddf97ccdSWANG Cong return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops); 464ca9b0e27SAlexander Duyck } 465ca9b0e27SAlexander Duyck 466ca9b0e27SAlexander Duyck static void __exit skbedit_cleanup_module(void) 467ca9b0e27SAlexander Duyck { 468ddf97ccdSWANG Cong tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops); 469ca9b0e27SAlexander Duyck } 470ca9b0e27SAlexander Duyck 471ca9b0e27SAlexander Duyck module_init(skbedit_init_module); 472ca9b0e27SAlexander Duyck module_exit(skbedit_cleanup_module); 473