162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * net/sched/act_gact.c		Generic actions
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * copyright 	Jamal Hadi Salim (2002-4)
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/types.h>
962306a36Sopenharmony_ci#include <linux/kernel.h>
1062306a36Sopenharmony_ci#include <linux/string.h>
1162306a36Sopenharmony_ci#include <linux/errno.h>
1262306a36Sopenharmony_ci#include <linux/skbuff.h>
1362306a36Sopenharmony_ci#include <linux/rtnetlink.h>
1462306a36Sopenharmony_ci#include <linux/module.h>
1562306a36Sopenharmony_ci#include <linux/init.h>
1662306a36Sopenharmony_ci#include <net/netlink.h>
1762306a36Sopenharmony_ci#include <net/pkt_sched.h>
1862306a36Sopenharmony_ci#include <net/pkt_cls.h>
1962306a36Sopenharmony_ci#include <linux/tc_act/tc_gact.h>
2062306a36Sopenharmony_ci#include <net/tc_act/tc_gact.h>
2162306a36Sopenharmony_ci#include <net/tc_wrapper.h>
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_cistatic struct tc_action_ops act_gact_ops;
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#ifdef CONFIG_GACT_PROB
2662306a36Sopenharmony_cistatic int gact_net_rand(struct tcf_gact *gact)
2762306a36Sopenharmony_ci{
2862306a36Sopenharmony_ci	smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
2962306a36Sopenharmony_ci	if (get_random_u32_below(gact->tcfg_pval))
3062306a36Sopenharmony_ci		return gact->tcf_action;
3162306a36Sopenharmony_ci	return gact->tcfg_paction;
3262306a36Sopenharmony_ci}
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_cistatic int gact_determ(struct tcf_gact *gact)
3562306a36Sopenharmony_ci{
3662306a36Sopenharmony_ci	u32 pack = atomic_inc_return(&gact->packets);
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci	smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
3962306a36Sopenharmony_ci	if (pack % gact->tcfg_pval)
4062306a36Sopenharmony_ci		return gact->tcf_action;
4162306a36Sopenharmony_ci	return gact->tcfg_paction;
4262306a36Sopenharmony_ci}
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_citypedef int (*g_rand)(struct tcf_gact *gact);
4562306a36Sopenharmony_cistatic g_rand gact_rand[MAX_RAND] = { NULL, gact_net_rand, gact_determ };
4662306a36Sopenharmony_ci#endif /* CONFIG_GACT_PROB */
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cistatic const struct nla_policy gact_policy[TCA_GACT_MAX + 1] = {
4962306a36Sopenharmony_ci	[TCA_GACT_PARMS]	= { .len = sizeof(struct tc_gact) },
5062306a36Sopenharmony_ci	[TCA_GACT_PROB]		= { .len = sizeof(struct tc_gact_p) },
5162306a36Sopenharmony_ci};
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_cistatic int tcf_gact_init(struct net *net, struct nlattr *nla,
5462306a36Sopenharmony_ci			 struct nlattr *est, struct tc_action **a,
5562306a36Sopenharmony_ci			 struct tcf_proto *tp, u32 flags,
5662306a36Sopenharmony_ci			 struct netlink_ext_ack *extack)
5762306a36Sopenharmony_ci{
5862306a36Sopenharmony_ci	struct tc_action_net *tn = net_generic(net, act_gact_ops.net_id);
5962306a36Sopenharmony_ci	bool bind = flags & TCA_ACT_FLAGS_BIND;
6062306a36Sopenharmony_ci	struct nlattr *tb[TCA_GACT_MAX + 1];
6162306a36Sopenharmony_ci	struct tcf_chain *goto_ch = NULL;
6262306a36Sopenharmony_ci	struct tc_gact *parm;
6362306a36Sopenharmony_ci	struct tcf_gact *gact;
6462306a36Sopenharmony_ci	int ret = 0;
6562306a36Sopenharmony_ci	u32 index;
6662306a36Sopenharmony_ci	int err;
6762306a36Sopenharmony_ci#ifdef CONFIG_GACT_PROB
6862306a36Sopenharmony_ci	struct tc_gact_p *p_parm = NULL;
6962306a36Sopenharmony_ci#endif
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci	if (nla == NULL)
7262306a36Sopenharmony_ci		return -EINVAL;
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	err = nla_parse_nested_deprecated(tb, TCA_GACT_MAX, nla, gact_policy,
7562306a36Sopenharmony_ci					  NULL);
7662306a36Sopenharmony_ci	if (err < 0)
7762306a36Sopenharmony_ci		return err;
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	if (tb[TCA_GACT_PARMS] == NULL)
8062306a36Sopenharmony_ci		return -EINVAL;
8162306a36Sopenharmony_ci	parm = nla_data(tb[TCA_GACT_PARMS]);
8262306a36Sopenharmony_ci	index = parm->index;
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci#ifndef CONFIG_GACT_PROB
8562306a36Sopenharmony_ci	if (tb[TCA_GACT_PROB] != NULL)
8662306a36Sopenharmony_ci		return -EOPNOTSUPP;
8762306a36Sopenharmony_ci#else
8862306a36Sopenharmony_ci	if (tb[TCA_GACT_PROB]) {
8962306a36Sopenharmony_ci		p_parm = nla_data(tb[TCA_GACT_PROB]);
9062306a36Sopenharmony_ci		if (p_parm->ptype >= MAX_RAND)
9162306a36Sopenharmony_ci			return -EINVAL;
9262306a36Sopenharmony_ci		if (TC_ACT_EXT_CMP(p_parm->paction, TC_ACT_GOTO_CHAIN)) {
9362306a36Sopenharmony_ci			NL_SET_ERR_MSG(extack,
9462306a36Sopenharmony_ci				       "goto chain not allowed on fallback");
9562306a36Sopenharmony_ci			return -EINVAL;
9662306a36Sopenharmony_ci		}
9762306a36Sopenharmony_ci	}
9862306a36Sopenharmony_ci#endif
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci	err = tcf_idr_check_alloc(tn, &index, a, bind);
10162306a36Sopenharmony_ci	if (!err) {
10262306a36Sopenharmony_ci		ret = tcf_idr_create_from_flags(tn, index, est, a,
10362306a36Sopenharmony_ci						&act_gact_ops, bind, flags);
10462306a36Sopenharmony_ci		if (ret) {
10562306a36Sopenharmony_ci			tcf_idr_cleanup(tn, index);
10662306a36Sopenharmony_ci			return ret;
10762306a36Sopenharmony_ci		}
10862306a36Sopenharmony_ci		ret = ACT_P_CREATED;
10962306a36Sopenharmony_ci	} else if (err > 0) {
11062306a36Sopenharmony_ci		if (bind)/* dont override defaults */
11162306a36Sopenharmony_ci			return 0;
11262306a36Sopenharmony_ci		if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
11362306a36Sopenharmony_ci			tcf_idr_release(*a, bind);
11462306a36Sopenharmony_ci			return -EEXIST;
11562306a36Sopenharmony_ci		}
11662306a36Sopenharmony_ci	} else {
11762306a36Sopenharmony_ci		return err;
11862306a36Sopenharmony_ci	}
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
12162306a36Sopenharmony_ci	if (err < 0)
12262306a36Sopenharmony_ci		goto release_idr;
12362306a36Sopenharmony_ci	gact = to_gact(*a);
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	spin_lock_bh(&gact->tcf_lock);
12662306a36Sopenharmony_ci	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
12762306a36Sopenharmony_ci#ifdef CONFIG_GACT_PROB
12862306a36Sopenharmony_ci	if (p_parm) {
12962306a36Sopenharmony_ci		gact->tcfg_paction = p_parm->paction;
13062306a36Sopenharmony_ci		gact->tcfg_pval    = max_t(u16, 1, p_parm->pval);
13162306a36Sopenharmony_ci		/* Make sure tcfg_pval is written before tcfg_ptype
13262306a36Sopenharmony_ci		 * coupled with smp_rmb() in gact_net_rand() & gact_determ()
13362306a36Sopenharmony_ci		 */
13462306a36Sopenharmony_ci		smp_wmb();
13562306a36Sopenharmony_ci		gact->tcfg_ptype   = p_parm->ptype;
13662306a36Sopenharmony_ci	}
13762306a36Sopenharmony_ci#endif
13862306a36Sopenharmony_ci	spin_unlock_bh(&gact->tcf_lock);
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	if (goto_ch)
14162306a36Sopenharmony_ci		tcf_chain_put_by_act(goto_ch);
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	return ret;
14462306a36Sopenharmony_cirelease_idr:
14562306a36Sopenharmony_ci	tcf_idr_release(*a, bind);
14662306a36Sopenharmony_ci	return err;
14762306a36Sopenharmony_ci}
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ciTC_INDIRECT_SCOPE int tcf_gact_act(struct sk_buff *skb,
15062306a36Sopenharmony_ci				   const struct tc_action *a,
15162306a36Sopenharmony_ci				   struct tcf_result *res)
15262306a36Sopenharmony_ci{
15362306a36Sopenharmony_ci	struct tcf_gact *gact = to_gact(a);
15462306a36Sopenharmony_ci	int action = READ_ONCE(gact->tcf_action);
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci#ifdef CONFIG_GACT_PROB
15762306a36Sopenharmony_ci	{
15862306a36Sopenharmony_ci	u32 ptype = READ_ONCE(gact->tcfg_ptype);
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci	if (ptype)
16162306a36Sopenharmony_ci		action = gact_rand[ptype](gact);
16262306a36Sopenharmony_ci	}
16362306a36Sopenharmony_ci#endif
16462306a36Sopenharmony_ci	tcf_action_update_bstats(&gact->common, skb);
16562306a36Sopenharmony_ci	if (action == TC_ACT_SHOT)
16662306a36Sopenharmony_ci		tcf_action_inc_drop_qstats(&gact->common);
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci	tcf_lastuse_update(&gact->tcf_tm);
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci	return action;
17162306a36Sopenharmony_ci}
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_cistatic void tcf_gact_stats_update(struct tc_action *a, u64 bytes, u64 packets,
17462306a36Sopenharmony_ci				  u64 drops, u64 lastuse, bool hw)
17562306a36Sopenharmony_ci{
17662306a36Sopenharmony_ci	struct tcf_gact *gact = to_gact(a);
17762306a36Sopenharmony_ci	int action = READ_ONCE(gact->tcf_action);
17862306a36Sopenharmony_ci	struct tcf_t *tm = &gact->tcf_tm;
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci	tcf_action_update_stats(a, bytes, packets,
18162306a36Sopenharmony_ci				action == TC_ACT_SHOT ? packets : drops, hw);
18262306a36Sopenharmony_ci	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
18362306a36Sopenharmony_ci}
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_cistatic int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a,
18662306a36Sopenharmony_ci			 int bind, int ref)
18762306a36Sopenharmony_ci{
18862306a36Sopenharmony_ci	unsigned char *b = skb_tail_pointer(skb);
18962306a36Sopenharmony_ci	struct tcf_gact *gact = to_gact(a);
19062306a36Sopenharmony_ci	struct tc_gact opt = {
19162306a36Sopenharmony_ci		.index   = gact->tcf_index,
19262306a36Sopenharmony_ci		.refcnt  = refcount_read(&gact->tcf_refcnt) - ref,
19362306a36Sopenharmony_ci		.bindcnt = atomic_read(&gact->tcf_bindcnt) - bind,
19462306a36Sopenharmony_ci	};
19562306a36Sopenharmony_ci	struct tcf_t t;
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci	spin_lock_bh(&gact->tcf_lock);
19862306a36Sopenharmony_ci	opt.action = gact->tcf_action;
19962306a36Sopenharmony_ci	if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt))
20062306a36Sopenharmony_ci		goto nla_put_failure;
20162306a36Sopenharmony_ci#ifdef CONFIG_GACT_PROB
20262306a36Sopenharmony_ci	if (gact->tcfg_ptype) {
20362306a36Sopenharmony_ci		struct tc_gact_p p_opt = {
20462306a36Sopenharmony_ci			.paction = gact->tcfg_paction,
20562306a36Sopenharmony_ci			.pval    = gact->tcfg_pval,
20662306a36Sopenharmony_ci			.ptype   = gact->tcfg_ptype,
20762306a36Sopenharmony_ci		};
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci		if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt))
21062306a36Sopenharmony_ci			goto nla_put_failure;
21162306a36Sopenharmony_ci	}
21262306a36Sopenharmony_ci#endif
21362306a36Sopenharmony_ci	tcf_tm_dump(&t, &gact->tcf_tm);
21462306a36Sopenharmony_ci	if (nla_put_64bit(skb, TCA_GACT_TM, sizeof(t), &t, TCA_GACT_PAD))
21562306a36Sopenharmony_ci		goto nla_put_failure;
21662306a36Sopenharmony_ci	spin_unlock_bh(&gact->tcf_lock);
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci	return skb->len;
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_cinla_put_failure:
22162306a36Sopenharmony_ci	spin_unlock_bh(&gact->tcf_lock);
22262306a36Sopenharmony_ci	nlmsg_trim(skb, b);
22362306a36Sopenharmony_ci	return -1;
22462306a36Sopenharmony_ci}
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_cistatic size_t tcf_gact_get_fill_size(const struct tc_action *act)
22762306a36Sopenharmony_ci{
22862306a36Sopenharmony_ci	size_t sz = nla_total_size(sizeof(struct tc_gact)); /* TCA_GACT_PARMS */
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci#ifdef CONFIG_GACT_PROB
23162306a36Sopenharmony_ci	if (to_gact(act)->tcfg_ptype)
23262306a36Sopenharmony_ci		/* TCA_GACT_PROB */
23362306a36Sopenharmony_ci		sz += nla_total_size(sizeof(struct tc_gact_p));
23462306a36Sopenharmony_ci#endif
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci	return sz;
23762306a36Sopenharmony_ci}
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_cistatic int tcf_gact_offload_act_setup(struct tc_action *act, void *entry_data,
24062306a36Sopenharmony_ci				      u32 *index_inc, bool bind,
24162306a36Sopenharmony_ci				      struct netlink_ext_ack *extack)
24262306a36Sopenharmony_ci{
24362306a36Sopenharmony_ci	if (bind) {
24462306a36Sopenharmony_ci		struct flow_action_entry *entry = entry_data;
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci		if (is_tcf_gact_ok(act)) {
24762306a36Sopenharmony_ci			entry->id = FLOW_ACTION_ACCEPT;
24862306a36Sopenharmony_ci		} else if (is_tcf_gact_shot(act)) {
24962306a36Sopenharmony_ci			entry->id = FLOW_ACTION_DROP;
25062306a36Sopenharmony_ci		} else if (is_tcf_gact_trap(act)) {
25162306a36Sopenharmony_ci			entry->id = FLOW_ACTION_TRAP;
25262306a36Sopenharmony_ci		} else if (is_tcf_gact_goto_chain(act)) {
25362306a36Sopenharmony_ci			entry->id = FLOW_ACTION_GOTO;
25462306a36Sopenharmony_ci			entry->chain_index = tcf_gact_goto_chain_index(act);
25562306a36Sopenharmony_ci		} else if (is_tcf_gact_continue(act)) {
25662306a36Sopenharmony_ci			NL_SET_ERR_MSG_MOD(extack, "Offload of \"continue\" action is not supported");
25762306a36Sopenharmony_ci			return -EOPNOTSUPP;
25862306a36Sopenharmony_ci		} else if (is_tcf_gact_reclassify(act)) {
25962306a36Sopenharmony_ci			NL_SET_ERR_MSG_MOD(extack, "Offload of \"reclassify\" action is not supported");
26062306a36Sopenharmony_ci			return -EOPNOTSUPP;
26162306a36Sopenharmony_ci		} else if (is_tcf_gact_pipe(act)) {
26262306a36Sopenharmony_ci			NL_SET_ERR_MSG_MOD(extack, "Offload of \"pipe\" action is not supported");
26362306a36Sopenharmony_ci			return -EOPNOTSUPP;
26462306a36Sopenharmony_ci		} else {
26562306a36Sopenharmony_ci			NL_SET_ERR_MSG_MOD(extack, "Unsupported generic action offload");
26662306a36Sopenharmony_ci			return -EOPNOTSUPP;
26762306a36Sopenharmony_ci		}
26862306a36Sopenharmony_ci		*index_inc = 1;
26962306a36Sopenharmony_ci	} else {
27062306a36Sopenharmony_ci		struct flow_offload_action *fl_action = entry_data;
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci		if (is_tcf_gact_ok(act))
27362306a36Sopenharmony_ci			fl_action->id = FLOW_ACTION_ACCEPT;
27462306a36Sopenharmony_ci		else if (is_tcf_gact_shot(act))
27562306a36Sopenharmony_ci			fl_action->id = FLOW_ACTION_DROP;
27662306a36Sopenharmony_ci		else if (is_tcf_gact_trap(act))
27762306a36Sopenharmony_ci			fl_action->id = FLOW_ACTION_TRAP;
27862306a36Sopenharmony_ci		else if (is_tcf_gact_goto_chain(act))
27962306a36Sopenharmony_ci			fl_action->id = FLOW_ACTION_GOTO;
28062306a36Sopenharmony_ci		else
28162306a36Sopenharmony_ci			return -EOPNOTSUPP;
28262306a36Sopenharmony_ci	}
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci	return 0;
28562306a36Sopenharmony_ci}
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_cistatic struct tc_action_ops act_gact_ops = {
28862306a36Sopenharmony_ci	.kind		=	"gact",
28962306a36Sopenharmony_ci	.id		=	TCA_ID_GACT,
29062306a36Sopenharmony_ci	.owner		=	THIS_MODULE,
29162306a36Sopenharmony_ci	.act		=	tcf_gact_act,
29262306a36Sopenharmony_ci	.stats_update	=	tcf_gact_stats_update,
29362306a36Sopenharmony_ci	.dump		=	tcf_gact_dump,
29462306a36Sopenharmony_ci	.init		=	tcf_gact_init,
29562306a36Sopenharmony_ci	.get_fill_size	=	tcf_gact_get_fill_size,
29662306a36Sopenharmony_ci	.offload_act_setup =	tcf_gact_offload_act_setup,
29762306a36Sopenharmony_ci	.size		=	sizeof(struct tcf_gact),
29862306a36Sopenharmony_ci};
29962306a36Sopenharmony_ci
30062306a36Sopenharmony_cistatic __net_init int gact_init_net(struct net *net)
30162306a36Sopenharmony_ci{
30262306a36Sopenharmony_ci	struct tc_action_net *tn = net_generic(net, act_gact_ops.net_id);
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci	return tc_action_net_init(net, tn, &act_gact_ops);
30562306a36Sopenharmony_ci}
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_cistatic void __net_exit gact_exit_net(struct list_head *net_list)
30862306a36Sopenharmony_ci{
30962306a36Sopenharmony_ci	tc_action_net_exit(net_list, act_gact_ops.net_id);
31062306a36Sopenharmony_ci}
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_cistatic struct pernet_operations gact_net_ops = {
31362306a36Sopenharmony_ci	.init = gact_init_net,
31462306a36Sopenharmony_ci	.exit_batch = gact_exit_net,
31562306a36Sopenharmony_ci	.id   = &act_gact_ops.net_id,
31662306a36Sopenharmony_ci	.size = sizeof(struct tc_action_net),
31762306a36Sopenharmony_ci};
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ciMODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
32062306a36Sopenharmony_ciMODULE_DESCRIPTION("Generic Classifier actions");
32162306a36Sopenharmony_ciMODULE_LICENSE("GPL");
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_cistatic int __init gact_init_module(void)
32462306a36Sopenharmony_ci{
32562306a36Sopenharmony_ci#ifdef CONFIG_GACT_PROB
32662306a36Sopenharmony_ci	pr_info("GACT probability on\n");
32762306a36Sopenharmony_ci#else
32862306a36Sopenharmony_ci	pr_info("GACT probability NOT on\n");
32962306a36Sopenharmony_ci#endif
33062306a36Sopenharmony_ci
33162306a36Sopenharmony_ci	return tcf_register_action(&act_gact_ops, &gact_net_ops);
33262306a36Sopenharmony_ci}
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_cistatic void __exit gact_cleanup_module(void)
33562306a36Sopenharmony_ci{
33662306a36Sopenharmony_ci	tcf_unregister_action(&act_gact_ops, &gact_net_ops);
33762306a36Sopenharmony_ci}
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_cimodule_init(gact_init_module);
34062306a36Sopenharmony_cimodule_exit(gact_cleanup_module);
341