162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2008, Intel Corporation.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Author: Alexander Duyck <alexander.h.duyck@intel.com>
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/module.h>
962306a36Sopenharmony_ci#include <linux/init.h>
1062306a36Sopenharmony_ci#include <linux/kernel.h>
1162306a36Sopenharmony_ci#include <linux/skbuff.h>
1262306a36Sopenharmony_ci#include <linux/rtnetlink.h>
1362306a36Sopenharmony_ci#include <net/netlink.h>
1462306a36Sopenharmony_ci#include <net/pkt_sched.h>
1562306a36Sopenharmony_ci#include <net/ip.h>
1662306a36Sopenharmony_ci#include <net/ipv6.h>
1762306a36Sopenharmony_ci#include <net/dsfield.h>
1862306a36Sopenharmony_ci#include <net/pkt_cls.h>
1962306a36Sopenharmony_ci#include <net/tc_wrapper.h>
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci#include <linux/tc_act/tc_skbedit.h>
2262306a36Sopenharmony_ci#include <net/tc_act/tc_skbedit.h>
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_cistatic struct tc_action_ops act_skbedit_ops;
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_cistatic u16 tcf_skbedit_hash(struct tcf_skbedit_params *params,
2762306a36Sopenharmony_ci			    struct sk_buff *skb)
2862306a36Sopenharmony_ci{
2962306a36Sopenharmony_ci	u16 queue_mapping = params->queue_mapping;
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci	if (params->flags & SKBEDIT_F_TXQ_SKBHASH) {
3262306a36Sopenharmony_ci		u32 hash = skb_get_hash(skb);
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci		queue_mapping += hash % params->mapping_mod;
3562306a36Sopenharmony_ci	}
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci	return netdev_cap_txqueue(skb->dev, queue_mapping);
3862306a36Sopenharmony_ci}
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ciTC_INDIRECT_SCOPE int tcf_skbedit_act(struct sk_buff *skb,
4162306a36Sopenharmony_ci				      const struct tc_action *a,
4262306a36Sopenharmony_ci				      struct tcf_result *res)
4362306a36Sopenharmony_ci{
4462306a36Sopenharmony_ci	struct tcf_skbedit *d = to_skbedit(a);
4562306a36Sopenharmony_ci	struct tcf_skbedit_params *params;
4662306a36Sopenharmony_ci	int action;
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci	tcf_lastuse_update(&d->tcf_tm);
4962306a36Sopenharmony_ci	bstats_update(this_cpu_ptr(d->common.cpu_bstats), skb);
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci	params = rcu_dereference_bh(d->params);
5262306a36Sopenharmony_ci	action = READ_ONCE(d->tcf_action);
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci	if (params->flags & SKBEDIT_F_PRIORITY)
5562306a36Sopenharmony_ci		skb->priority = params->priority;
5662306a36Sopenharmony_ci	if (params->flags & SKBEDIT_F_INHERITDSFIELD) {
5762306a36Sopenharmony_ci		int wlen = skb_network_offset(skb);
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci		switch (skb_protocol(skb, true)) {
6062306a36Sopenharmony_ci		case htons(ETH_P_IP):
6162306a36Sopenharmony_ci			wlen += sizeof(struct iphdr);
6262306a36Sopenharmony_ci			if (!pskb_may_pull(skb, wlen))
6362306a36Sopenharmony_ci				goto err;
6462306a36Sopenharmony_ci			skb->priority = ipv4_get_dsfield(ip_hdr(skb)) >> 2;
6562306a36Sopenharmony_ci			break;
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci		case htons(ETH_P_IPV6):
6862306a36Sopenharmony_ci			wlen += sizeof(struct ipv6hdr);
6962306a36Sopenharmony_ci			if (!pskb_may_pull(skb, wlen))
7062306a36Sopenharmony_ci				goto err;
7162306a36Sopenharmony_ci			skb->priority = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2;
7262306a36Sopenharmony_ci			break;
7362306a36Sopenharmony_ci		}
7462306a36Sopenharmony_ci	}
7562306a36Sopenharmony_ci	if (params->flags & SKBEDIT_F_QUEUE_MAPPING &&
7662306a36Sopenharmony_ci	    skb->dev->real_num_tx_queues > params->queue_mapping) {
7762306a36Sopenharmony_ci#ifdef CONFIG_NET_EGRESS
7862306a36Sopenharmony_ci		netdev_xmit_skip_txqueue(true);
7962306a36Sopenharmony_ci#endif
8062306a36Sopenharmony_ci		skb_set_queue_mapping(skb, tcf_skbedit_hash(params, skb));
8162306a36Sopenharmony_ci	}
8262306a36Sopenharmony_ci	if (params->flags & SKBEDIT_F_MARK) {
8362306a36Sopenharmony_ci		skb->mark &= ~params->mask;
8462306a36Sopenharmony_ci		skb->mark |= params->mark & params->mask;
8562306a36Sopenharmony_ci	}
8662306a36Sopenharmony_ci	if (params->flags & SKBEDIT_F_PTYPE)
8762306a36Sopenharmony_ci		skb->pkt_type = params->ptype;
8862306a36Sopenharmony_ci	return action;
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_cierr:
9162306a36Sopenharmony_ci	qstats_drop_inc(this_cpu_ptr(d->common.cpu_qstats));
9262306a36Sopenharmony_ci	return TC_ACT_SHOT;
9362306a36Sopenharmony_ci}
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_cistatic void tcf_skbedit_stats_update(struct tc_action *a, u64 bytes,
9662306a36Sopenharmony_ci				     u64 packets, u64 drops,
9762306a36Sopenharmony_ci				     u64 lastuse, bool hw)
9862306a36Sopenharmony_ci{
9962306a36Sopenharmony_ci	struct tcf_skbedit *d = to_skbedit(a);
10062306a36Sopenharmony_ci	struct tcf_t *tm = &d->tcf_tm;
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci	tcf_action_update_stats(a, bytes, packets, drops, hw);
10362306a36Sopenharmony_ci	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
10462306a36Sopenharmony_ci}
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_cistatic const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = {
10762306a36Sopenharmony_ci	[TCA_SKBEDIT_PARMS]		= { .len = sizeof(struct tc_skbedit) },
10862306a36Sopenharmony_ci	[TCA_SKBEDIT_PRIORITY]		= { .len = sizeof(u32) },
10962306a36Sopenharmony_ci	[TCA_SKBEDIT_QUEUE_MAPPING]	= { .len = sizeof(u16) },
11062306a36Sopenharmony_ci	[TCA_SKBEDIT_MARK]		= { .len = sizeof(u32) },
11162306a36Sopenharmony_ci	[TCA_SKBEDIT_PTYPE]		= { .len = sizeof(u16) },
11262306a36Sopenharmony_ci	[TCA_SKBEDIT_MASK]		= { .len = sizeof(u32) },
11362306a36Sopenharmony_ci	[TCA_SKBEDIT_FLAGS]		= { .len = sizeof(u64) },
11462306a36Sopenharmony_ci	[TCA_SKBEDIT_QUEUE_MAPPING_MAX]	= { .len = sizeof(u16) },
11562306a36Sopenharmony_ci};
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_cistatic int tcf_skbedit_init(struct net *net, struct nlattr *nla,
11862306a36Sopenharmony_ci			    struct nlattr *est, struct tc_action **a,
11962306a36Sopenharmony_ci			    struct tcf_proto *tp, u32 act_flags,
12062306a36Sopenharmony_ci			    struct netlink_ext_ack *extack)
12162306a36Sopenharmony_ci{
12262306a36Sopenharmony_ci	struct tc_action_net *tn = net_generic(net, act_skbedit_ops.net_id);
12362306a36Sopenharmony_ci	bool bind = act_flags & TCA_ACT_FLAGS_BIND;
12462306a36Sopenharmony_ci	struct tcf_skbedit_params *params_new;
12562306a36Sopenharmony_ci	struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
12662306a36Sopenharmony_ci	struct tcf_chain *goto_ch = NULL;
12762306a36Sopenharmony_ci	struct tc_skbedit *parm;
12862306a36Sopenharmony_ci	struct tcf_skbedit *d;
12962306a36Sopenharmony_ci	u32 flags = 0, *priority = NULL, *mark = NULL, *mask = NULL;
13062306a36Sopenharmony_ci	u16 *queue_mapping = NULL, *ptype = NULL;
13162306a36Sopenharmony_ci	u16 mapping_mod = 1;
13262306a36Sopenharmony_ci	bool exists = false;
13362306a36Sopenharmony_ci	int ret = 0, err;
13462306a36Sopenharmony_ci	u32 index;
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci	if (nla == NULL)
13762306a36Sopenharmony_ci		return -EINVAL;
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	err = nla_parse_nested_deprecated(tb, TCA_SKBEDIT_MAX, nla,
14062306a36Sopenharmony_ci					  skbedit_policy, NULL);
14162306a36Sopenharmony_ci	if (err < 0)
14262306a36Sopenharmony_ci		return err;
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci	if (tb[TCA_SKBEDIT_PARMS] == NULL)
14562306a36Sopenharmony_ci		return -EINVAL;
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci	if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
14862306a36Sopenharmony_ci		flags |= SKBEDIT_F_PRIORITY;
14962306a36Sopenharmony_ci		priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
15062306a36Sopenharmony_ci	}
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci	if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
15362306a36Sopenharmony_ci		if (is_tcf_skbedit_ingress(act_flags) &&
15462306a36Sopenharmony_ci		    !(act_flags & TCA_ACT_FLAGS_SKIP_SW)) {
15562306a36Sopenharmony_ci			NL_SET_ERR_MSG_MOD(extack, "\"queue_mapping\" option on receive side is hardware only, use skip_sw");
15662306a36Sopenharmony_ci			return -EOPNOTSUPP;
15762306a36Sopenharmony_ci		}
15862306a36Sopenharmony_ci		flags |= SKBEDIT_F_QUEUE_MAPPING;
15962306a36Sopenharmony_ci		queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
16062306a36Sopenharmony_ci	}
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	if (tb[TCA_SKBEDIT_PTYPE] != NULL) {
16362306a36Sopenharmony_ci		ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]);
16462306a36Sopenharmony_ci		if (!skb_pkt_type_ok(*ptype))
16562306a36Sopenharmony_ci			return -EINVAL;
16662306a36Sopenharmony_ci		flags |= SKBEDIT_F_PTYPE;
16762306a36Sopenharmony_ci	}
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	if (tb[TCA_SKBEDIT_MARK] != NULL) {
17062306a36Sopenharmony_ci		flags |= SKBEDIT_F_MARK;
17162306a36Sopenharmony_ci		mark = nla_data(tb[TCA_SKBEDIT_MARK]);
17262306a36Sopenharmony_ci	}
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	if (tb[TCA_SKBEDIT_MASK] != NULL) {
17562306a36Sopenharmony_ci		flags |= SKBEDIT_F_MASK;
17662306a36Sopenharmony_ci		mask = nla_data(tb[TCA_SKBEDIT_MASK]);
17762306a36Sopenharmony_ci	}
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci	if (tb[TCA_SKBEDIT_FLAGS] != NULL) {
18062306a36Sopenharmony_ci		u64 *pure_flags = nla_data(tb[TCA_SKBEDIT_FLAGS]);
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci		if (*pure_flags & SKBEDIT_F_TXQ_SKBHASH) {
18362306a36Sopenharmony_ci			u16 *queue_mapping_max;
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci			if (!tb[TCA_SKBEDIT_QUEUE_MAPPING] ||
18662306a36Sopenharmony_ci			    !tb[TCA_SKBEDIT_QUEUE_MAPPING_MAX]) {
18762306a36Sopenharmony_ci				NL_SET_ERR_MSG_MOD(extack, "Missing required range of queue_mapping.");
18862306a36Sopenharmony_ci				return -EINVAL;
18962306a36Sopenharmony_ci			}
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci			queue_mapping_max =
19262306a36Sopenharmony_ci				nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING_MAX]);
19362306a36Sopenharmony_ci			if (*queue_mapping_max < *queue_mapping) {
19462306a36Sopenharmony_ci				NL_SET_ERR_MSG_MOD(extack, "The range of queue_mapping is invalid, max < min.");
19562306a36Sopenharmony_ci				return -EINVAL;
19662306a36Sopenharmony_ci			}
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci			mapping_mod = *queue_mapping_max - *queue_mapping + 1;
19962306a36Sopenharmony_ci			flags |= SKBEDIT_F_TXQ_SKBHASH;
20062306a36Sopenharmony_ci		}
20162306a36Sopenharmony_ci		if (*pure_flags & SKBEDIT_F_INHERITDSFIELD)
20262306a36Sopenharmony_ci			flags |= SKBEDIT_F_INHERITDSFIELD;
20362306a36Sopenharmony_ci	}
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci	parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
20662306a36Sopenharmony_ci	index = parm->index;
20762306a36Sopenharmony_ci	err = tcf_idr_check_alloc(tn, &index, a, bind);
20862306a36Sopenharmony_ci	if (err < 0)
20962306a36Sopenharmony_ci		return err;
21062306a36Sopenharmony_ci	exists = err;
21162306a36Sopenharmony_ci	if (exists && bind)
21262306a36Sopenharmony_ci		return 0;
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci	if (!flags) {
21562306a36Sopenharmony_ci		if (exists)
21662306a36Sopenharmony_ci			tcf_idr_release(*a, bind);
21762306a36Sopenharmony_ci		else
21862306a36Sopenharmony_ci			tcf_idr_cleanup(tn, index);
21962306a36Sopenharmony_ci		return -EINVAL;
22062306a36Sopenharmony_ci	}
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci	if (!exists) {
22362306a36Sopenharmony_ci		ret = tcf_idr_create(tn, index, est, a,
22462306a36Sopenharmony_ci				     &act_skbedit_ops, bind, true, act_flags);
22562306a36Sopenharmony_ci		if (ret) {
22662306a36Sopenharmony_ci			tcf_idr_cleanup(tn, index);
22762306a36Sopenharmony_ci			return ret;
22862306a36Sopenharmony_ci		}
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci		d = to_skbedit(*a);
23162306a36Sopenharmony_ci		ret = ACT_P_CREATED;
23262306a36Sopenharmony_ci	} else {
23362306a36Sopenharmony_ci		d = to_skbedit(*a);
23462306a36Sopenharmony_ci		if (!(act_flags & TCA_ACT_FLAGS_REPLACE)) {
23562306a36Sopenharmony_ci			tcf_idr_release(*a, bind);
23662306a36Sopenharmony_ci			return -EEXIST;
23762306a36Sopenharmony_ci		}
23862306a36Sopenharmony_ci	}
23962306a36Sopenharmony_ci	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
24062306a36Sopenharmony_ci	if (err < 0)
24162306a36Sopenharmony_ci		goto release_idr;
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ci	params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
24462306a36Sopenharmony_ci	if (unlikely(!params_new)) {
24562306a36Sopenharmony_ci		err = -ENOMEM;
24662306a36Sopenharmony_ci		goto put_chain;
24762306a36Sopenharmony_ci	}
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci	params_new->flags = flags;
25062306a36Sopenharmony_ci	if (flags & SKBEDIT_F_PRIORITY)
25162306a36Sopenharmony_ci		params_new->priority = *priority;
25262306a36Sopenharmony_ci	if (flags & SKBEDIT_F_QUEUE_MAPPING) {
25362306a36Sopenharmony_ci		params_new->queue_mapping = *queue_mapping;
25462306a36Sopenharmony_ci		params_new->mapping_mod = mapping_mod;
25562306a36Sopenharmony_ci	}
25662306a36Sopenharmony_ci	if (flags & SKBEDIT_F_MARK)
25762306a36Sopenharmony_ci		params_new->mark = *mark;
25862306a36Sopenharmony_ci	if (flags & SKBEDIT_F_PTYPE)
25962306a36Sopenharmony_ci		params_new->ptype = *ptype;
26062306a36Sopenharmony_ci	/* default behaviour is to use all the bits */
26162306a36Sopenharmony_ci	params_new->mask = 0xffffffff;
26262306a36Sopenharmony_ci	if (flags & SKBEDIT_F_MASK)
26362306a36Sopenharmony_ci		params_new->mask = *mask;
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci	spin_lock_bh(&d->tcf_lock);
26662306a36Sopenharmony_ci	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
26762306a36Sopenharmony_ci	params_new = rcu_replace_pointer(d->params, params_new,
26862306a36Sopenharmony_ci					 lockdep_is_held(&d->tcf_lock));
26962306a36Sopenharmony_ci	spin_unlock_bh(&d->tcf_lock);
27062306a36Sopenharmony_ci	if (params_new)
27162306a36Sopenharmony_ci		kfree_rcu(params_new, rcu);
27262306a36Sopenharmony_ci	if (goto_ch)
27362306a36Sopenharmony_ci		tcf_chain_put_by_act(goto_ch);
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ci	return ret;
27662306a36Sopenharmony_ciput_chain:
27762306a36Sopenharmony_ci	if (goto_ch)
27862306a36Sopenharmony_ci		tcf_chain_put_by_act(goto_ch);
27962306a36Sopenharmony_cirelease_idr:
28062306a36Sopenharmony_ci	tcf_idr_release(*a, bind);
28162306a36Sopenharmony_ci	return err;
28262306a36Sopenharmony_ci}
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_cistatic int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
28562306a36Sopenharmony_ci			    int bind, int ref)
28662306a36Sopenharmony_ci{
28762306a36Sopenharmony_ci	unsigned char *b = skb_tail_pointer(skb);
28862306a36Sopenharmony_ci	struct tcf_skbedit *d = to_skbedit(a);
28962306a36Sopenharmony_ci	struct tcf_skbedit_params *params;
29062306a36Sopenharmony_ci	struct tc_skbedit opt = {
29162306a36Sopenharmony_ci		.index   = d->tcf_index,
29262306a36Sopenharmony_ci		.refcnt  = refcount_read(&d->tcf_refcnt) - ref,
29362306a36Sopenharmony_ci		.bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
29462306a36Sopenharmony_ci	};
29562306a36Sopenharmony_ci	u64 pure_flags = 0;
29662306a36Sopenharmony_ci	struct tcf_t t;
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ci	spin_lock_bh(&d->tcf_lock);
29962306a36Sopenharmony_ci	params = rcu_dereference_protected(d->params,
30062306a36Sopenharmony_ci					   lockdep_is_held(&d->tcf_lock));
30162306a36Sopenharmony_ci	opt.action = d->tcf_action;
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci	if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
30462306a36Sopenharmony_ci		goto nla_put_failure;
30562306a36Sopenharmony_ci	if ((params->flags & SKBEDIT_F_PRIORITY) &&
30662306a36Sopenharmony_ci	    nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, params->priority))
30762306a36Sopenharmony_ci		goto nla_put_failure;
30862306a36Sopenharmony_ci	if ((params->flags & SKBEDIT_F_QUEUE_MAPPING) &&
30962306a36Sopenharmony_ci	    nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, params->queue_mapping))
31062306a36Sopenharmony_ci		goto nla_put_failure;
31162306a36Sopenharmony_ci	if ((params->flags & SKBEDIT_F_MARK) &&
31262306a36Sopenharmony_ci	    nla_put_u32(skb, TCA_SKBEDIT_MARK, params->mark))
31362306a36Sopenharmony_ci		goto nla_put_failure;
31462306a36Sopenharmony_ci	if ((params->flags & SKBEDIT_F_PTYPE) &&
31562306a36Sopenharmony_ci	    nla_put_u16(skb, TCA_SKBEDIT_PTYPE, params->ptype))
31662306a36Sopenharmony_ci		goto nla_put_failure;
31762306a36Sopenharmony_ci	if ((params->flags & SKBEDIT_F_MASK) &&
31862306a36Sopenharmony_ci	    nla_put_u32(skb, TCA_SKBEDIT_MASK, params->mask))
31962306a36Sopenharmony_ci		goto nla_put_failure;
32062306a36Sopenharmony_ci	if (params->flags & SKBEDIT_F_INHERITDSFIELD)
32162306a36Sopenharmony_ci		pure_flags |= SKBEDIT_F_INHERITDSFIELD;
32262306a36Sopenharmony_ci	if (params->flags & SKBEDIT_F_TXQ_SKBHASH) {
32362306a36Sopenharmony_ci		if (nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING_MAX,
32462306a36Sopenharmony_ci				params->queue_mapping + params->mapping_mod - 1))
32562306a36Sopenharmony_ci			goto nla_put_failure;
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci		pure_flags |= SKBEDIT_F_TXQ_SKBHASH;
32862306a36Sopenharmony_ci	}
32962306a36Sopenharmony_ci	if (pure_flags != 0 &&
33062306a36Sopenharmony_ci	    nla_put(skb, TCA_SKBEDIT_FLAGS, sizeof(pure_flags), &pure_flags))
33162306a36Sopenharmony_ci		goto nla_put_failure;
33262306a36Sopenharmony_ci
33362306a36Sopenharmony_ci	tcf_tm_dump(&t, &d->tcf_tm);
33462306a36Sopenharmony_ci	if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD))
33562306a36Sopenharmony_ci		goto nla_put_failure;
33662306a36Sopenharmony_ci	spin_unlock_bh(&d->tcf_lock);
33762306a36Sopenharmony_ci
33862306a36Sopenharmony_ci	return skb->len;
33962306a36Sopenharmony_ci
34062306a36Sopenharmony_cinla_put_failure:
34162306a36Sopenharmony_ci	spin_unlock_bh(&d->tcf_lock);
34262306a36Sopenharmony_ci	nlmsg_trim(skb, b);
34362306a36Sopenharmony_ci	return -1;
34462306a36Sopenharmony_ci}
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_cistatic void tcf_skbedit_cleanup(struct tc_action *a)
34762306a36Sopenharmony_ci{
34862306a36Sopenharmony_ci	struct tcf_skbedit *d = to_skbedit(a);
34962306a36Sopenharmony_ci	struct tcf_skbedit_params *params;
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_ci	params = rcu_dereference_protected(d->params, 1);
35262306a36Sopenharmony_ci	if (params)
35362306a36Sopenharmony_ci		kfree_rcu(params, rcu);
35462306a36Sopenharmony_ci}
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_cistatic size_t tcf_skbedit_get_fill_size(const struct tc_action *act)
35762306a36Sopenharmony_ci{
35862306a36Sopenharmony_ci	return nla_total_size(sizeof(struct tc_skbedit))
35962306a36Sopenharmony_ci		+ nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_PRIORITY */
36062306a36Sopenharmony_ci		+ nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_QUEUE_MAPPING */
36162306a36Sopenharmony_ci		+ nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_QUEUE_MAPPING_MAX */
36262306a36Sopenharmony_ci		+ nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_MARK */
36362306a36Sopenharmony_ci		+ nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_PTYPE */
36462306a36Sopenharmony_ci		+ nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_MASK */
36562306a36Sopenharmony_ci		+ nla_total_size_64bit(sizeof(u64)); /* TCA_SKBEDIT_FLAGS */
36662306a36Sopenharmony_ci}
36762306a36Sopenharmony_ci
36862306a36Sopenharmony_cistatic int tcf_skbedit_offload_act_setup(struct tc_action *act, void *entry_data,
36962306a36Sopenharmony_ci					 u32 *index_inc, bool bind,
37062306a36Sopenharmony_ci					 struct netlink_ext_ack *extack)
37162306a36Sopenharmony_ci{
37262306a36Sopenharmony_ci	if (bind) {
37362306a36Sopenharmony_ci		struct flow_action_entry *entry = entry_data;
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_ci		if (is_tcf_skbedit_mark(act)) {
37662306a36Sopenharmony_ci			entry->id = FLOW_ACTION_MARK;
37762306a36Sopenharmony_ci			entry->mark = tcf_skbedit_mark(act);
37862306a36Sopenharmony_ci		} else if (is_tcf_skbedit_ptype(act)) {
37962306a36Sopenharmony_ci			entry->id = FLOW_ACTION_PTYPE;
38062306a36Sopenharmony_ci			entry->ptype = tcf_skbedit_ptype(act);
38162306a36Sopenharmony_ci		} else if (is_tcf_skbedit_priority(act)) {
38262306a36Sopenharmony_ci			entry->id = FLOW_ACTION_PRIORITY;
38362306a36Sopenharmony_ci			entry->priority = tcf_skbedit_priority(act);
38462306a36Sopenharmony_ci		} else if (is_tcf_skbedit_tx_queue_mapping(act)) {
38562306a36Sopenharmony_ci			NL_SET_ERR_MSG_MOD(extack, "Offload not supported when \"queue_mapping\" option is used on transmit side");
38662306a36Sopenharmony_ci			return -EOPNOTSUPP;
38762306a36Sopenharmony_ci		} else if (is_tcf_skbedit_rx_queue_mapping(act)) {
38862306a36Sopenharmony_ci			entry->id = FLOW_ACTION_RX_QUEUE_MAPPING;
38962306a36Sopenharmony_ci			entry->rx_queue = tcf_skbedit_rx_queue_mapping(act);
39062306a36Sopenharmony_ci		} else if (is_tcf_skbedit_inheritdsfield(act)) {
39162306a36Sopenharmony_ci			NL_SET_ERR_MSG_MOD(extack, "Offload not supported when \"inheritdsfield\" option is used");
39262306a36Sopenharmony_ci			return -EOPNOTSUPP;
39362306a36Sopenharmony_ci		} else {
39462306a36Sopenharmony_ci			NL_SET_ERR_MSG_MOD(extack, "Unsupported skbedit option offload");
39562306a36Sopenharmony_ci			return -EOPNOTSUPP;
39662306a36Sopenharmony_ci		}
39762306a36Sopenharmony_ci		*index_inc = 1;
39862306a36Sopenharmony_ci	} else {
39962306a36Sopenharmony_ci		struct flow_offload_action *fl_action = entry_data;
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ci		if (is_tcf_skbedit_mark(act))
40262306a36Sopenharmony_ci			fl_action->id = FLOW_ACTION_MARK;
40362306a36Sopenharmony_ci		else if (is_tcf_skbedit_ptype(act))
40462306a36Sopenharmony_ci			fl_action->id = FLOW_ACTION_PTYPE;
40562306a36Sopenharmony_ci		else if (is_tcf_skbedit_priority(act))
40662306a36Sopenharmony_ci			fl_action->id = FLOW_ACTION_PRIORITY;
40762306a36Sopenharmony_ci		else if (is_tcf_skbedit_rx_queue_mapping(act))
40862306a36Sopenharmony_ci			fl_action->id = FLOW_ACTION_RX_QUEUE_MAPPING;
40962306a36Sopenharmony_ci		else
41062306a36Sopenharmony_ci			return -EOPNOTSUPP;
41162306a36Sopenharmony_ci	}
41262306a36Sopenharmony_ci
41362306a36Sopenharmony_ci	return 0;
41462306a36Sopenharmony_ci}
41562306a36Sopenharmony_ci
41662306a36Sopenharmony_cistatic struct tc_action_ops act_skbedit_ops = {
41762306a36Sopenharmony_ci	.kind		=	"skbedit",
41862306a36Sopenharmony_ci	.id		=	TCA_ID_SKBEDIT,
41962306a36Sopenharmony_ci	.owner		=	THIS_MODULE,
42062306a36Sopenharmony_ci	.act		=	tcf_skbedit_act,
42162306a36Sopenharmony_ci	.stats_update	=	tcf_skbedit_stats_update,
42262306a36Sopenharmony_ci	.dump		=	tcf_skbedit_dump,
42362306a36Sopenharmony_ci	.init		=	tcf_skbedit_init,
42462306a36Sopenharmony_ci	.cleanup	=	tcf_skbedit_cleanup,
42562306a36Sopenharmony_ci	.get_fill_size	=	tcf_skbedit_get_fill_size,
42662306a36Sopenharmony_ci	.offload_act_setup =	tcf_skbedit_offload_act_setup,
42762306a36Sopenharmony_ci	.size		=	sizeof(struct tcf_skbedit),
42862306a36Sopenharmony_ci};
42962306a36Sopenharmony_ci
43062306a36Sopenharmony_cistatic __net_init int skbedit_init_net(struct net *net)
43162306a36Sopenharmony_ci{
43262306a36Sopenharmony_ci	struct tc_action_net *tn = net_generic(net, act_skbedit_ops.net_id);
43362306a36Sopenharmony_ci
43462306a36Sopenharmony_ci	return tc_action_net_init(net, tn, &act_skbedit_ops);
43562306a36Sopenharmony_ci}
43662306a36Sopenharmony_ci
43762306a36Sopenharmony_cistatic void __net_exit skbedit_exit_net(struct list_head *net_list)
43862306a36Sopenharmony_ci{
43962306a36Sopenharmony_ci	tc_action_net_exit(net_list, act_skbedit_ops.net_id);
44062306a36Sopenharmony_ci}
44162306a36Sopenharmony_ci
44262306a36Sopenharmony_cistatic struct pernet_operations skbedit_net_ops = {
44362306a36Sopenharmony_ci	.init = skbedit_init_net,
44462306a36Sopenharmony_ci	.exit_batch = skbedit_exit_net,
44562306a36Sopenharmony_ci	.id   = &act_skbedit_ops.net_id,
44662306a36Sopenharmony_ci	.size = sizeof(struct tc_action_net),
44762306a36Sopenharmony_ci};
44862306a36Sopenharmony_ci
44962306a36Sopenharmony_ciMODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
45062306a36Sopenharmony_ciMODULE_DESCRIPTION("SKB Editing");
45162306a36Sopenharmony_ciMODULE_LICENSE("GPL");
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_cistatic int __init skbedit_init_module(void)
45462306a36Sopenharmony_ci{
45562306a36Sopenharmony_ci	return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops);
45662306a36Sopenharmony_ci}
45762306a36Sopenharmony_ci
45862306a36Sopenharmony_cistatic void __exit skbedit_cleanup_module(void)
45962306a36Sopenharmony_ci{
46062306a36Sopenharmony_ci	tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops);
46162306a36Sopenharmony_ci}
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_cimodule_init(skbedit_init_module);
46462306a36Sopenharmony_cimodule_exit(skbedit_cleanup_module);
465