18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * net/sched/act_gact.c		Generic actions
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * copyright 	Jamal Hadi Salim (2002-4)
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/types.h>
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/string.h>
118c2ecf20Sopenharmony_ci#include <linux/errno.h>
128c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
138c2ecf20Sopenharmony_ci#include <linux/rtnetlink.h>
148c2ecf20Sopenharmony_ci#include <linux/module.h>
158c2ecf20Sopenharmony_ci#include <linux/init.h>
168c2ecf20Sopenharmony_ci#include <net/netlink.h>
178c2ecf20Sopenharmony_ci#include <net/pkt_sched.h>
188c2ecf20Sopenharmony_ci#include <net/pkt_cls.h>
198c2ecf20Sopenharmony_ci#include <linux/tc_act/tc_gact.h>
208c2ecf20Sopenharmony_ci#include <net/tc_act/tc_gact.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic unsigned int gact_net_id;
238c2ecf20Sopenharmony_cistatic struct tc_action_ops act_gact_ops;
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#ifdef CONFIG_GACT_PROB
268c2ecf20Sopenharmony_cistatic int gact_net_rand(struct tcf_gact *gact)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
298c2ecf20Sopenharmony_ci	if (prandom_u32() % gact->tcfg_pval)
308c2ecf20Sopenharmony_ci		return gact->tcf_action;
318c2ecf20Sopenharmony_ci	return gact->tcfg_paction;
328c2ecf20Sopenharmony_ci}
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic int gact_determ(struct tcf_gact *gact)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	u32 pack = atomic_inc_return(&gact->packets);
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
398c2ecf20Sopenharmony_ci	if (pack % gact->tcfg_pval)
408c2ecf20Sopenharmony_ci		return gact->tcf_action;
418c2ecf20Sopenharmony_ci	return gact->tcfg_paction;
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_citypedef int (*g_rand)(struct tcf_gact *gact);
458c2ecf20Sopenharmony_cistatic g_rand gact_rand[MAX_RAND] = { NULL, gact_net_rand, gact_determ };
468c2ecf20Sopenharmony_ci#endif /* CONFIG_GACT_PROB */
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic const struct nla_policy gact_policy[TCA_GACT_MAX + 1] = {
498c2ecf20Sopenharmony_ci	[TCA_GACT_PARMS]	= { .len = sizeof(struct tc_gact) },
508c2ecf20Sopenharmony_ci	[TCA_GACT_PROB]		= { .len = sizeof(struct tc_gact_p) },
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic int tcf_gact_init(struct net *net, struct nlattr *nla,
548c2ecf20Sopenharmony_ci			 struct nlattr *est, struct tc_action **a,
558c2ecf20Sopenharmony_ci			 int ovr, int bind, bool rtnl_held,
568c2ecf20Sopenharmony_ci			 struct tcf_proto *tp, u32 flags,
578c2ecf20Sopenharmony_ci			 struct netlink_ext_ack *extack)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	struct tc_action_net *tn = net_generic(net, gact_net_id);
608c2ecf20Sopenharmony_ci	struct nlattr *tb[TCA_GACT_MAX + 1];
618c2ecf20Sopenharmony_ci	struct tcf_chain *goto_ch = NULL;
628c2ecf20Sopenharmony_ci	struct tc_gact *parm;
638c2ecf20Sopenharmony_ci	struct tcf_gact *gact;
648c2ecf20Sopenharmony_ci	int ret = 0;
658c2ecf20Sopenharmony_ci	u32 index;
668c2ecf20Sopenharmony_ci	int err;
678c2ecf20Sopenharmony_ci#ifdef CONFIG_GACT_PROB
688c2ecf20Sopenharmony_ci	struct tc_gact_p *p_parm = NULL;
698c2ecf20Sopenharmony_ci#endif
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	if (nla == NULL)
728c2ecf20Sopenharmony_ci		return -EINVAL;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	err = nla_parse_nested_deprecated(tb, TCA_GACT_MAX, nla, gact_policy,
758c2ecf20Sopenharmony_ci					  NULL);
768c2ecf20Sopenharmony_ci	if (err < 0)
778c2ecf20Sopenharmony_ci		return err;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	if (tb[TCA_GACT_PARMS] == NULL)
808c2ecf20Sopenharmony_ci		return -EINVAL;
818c2ecf20Sopenharmony_ci	parm = nla_data(tb[TCA_GACT_PARMS]);
828c2ecf20Sopenharmony_ci	index = parm->index;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#ifndef CONFIG_GACT_PROB
858c2ecf20Sopenharmony_ci	if (tb[TCA_GACT_PROB] != NULL)
868c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
878c2ecf20Sopenharmony_ci#else
888c2ecf20Sopenharmony_ci	if (tb[TCA_GACT_PROB]) {
898c2ecf20Sopenharmony_ci		p_parm = nla_data(tb[TCA_GACT_PROB]);
908c2ecf20Sopenharmony_ci		if (p_parm->ptype >= MAX_RAND)
918c2ecf20Sopenharmony_ci			return -EINVAL;
928c2ecf20Sopenharmony_ci		if (TC_ACT_EXT_CMP(p_parm->paction, TC_ACT_GOTO_CHAIN)) {
938c2ecf20Sopenharmony_ci			NL_SET_ERR_MSG(extack,
948c2ecf20Sopenharmony_ci				       "goto chain not allowed on fallback");
958c2ecf20Sopenharmony_ci			return -EINVAL;
968c2ecf20Sopenharmony_ci		}
978c2ecf20Sopenharmony_ci	}
988c2ecf20Sopenharmony_ci#endif
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	err = tcf_idr_check_alloc(tn, &index, a, bind);
1018c2ecf20Sopenharmony_ci	if (!err) {
1028c2ecf20Sopenharmony_ci		ret = tcf_idr_create_from_flags(tn, index, est, a,
1038c2ecf20Sopenharmony_ci						&act_gact_ops, bind, flags);
1048c2ecf20Sopenharmony_ci		if (ret) {
1058c2ecf20Sopenharmony_ci			tcf_idr_cleanup(tn, index);
1068c2ecf20Sopenharmony_ci			return ret;
1078c2ecf20Sopenharmony_ci		}
1088c2ecf20Sopenharmony_ci		ret = ACT_P_CREATED;
1098c2ecf20Sopenharmony_ci	} else if (err > 0) {
1108c2ecf20Sopenharmony_ci		if (bind)/* dont override defaults */
1118c2ecf20Sopenharmony_ci			return 0;
1128c2ecf20Sopenharmony_ci		if (!ovr) {
1138c2ecf20Sopenharmony_ci			tcf_idr_release(*a, bind);
1148c2ecf20Sopenharmony_ci			return -EEXIST;
1158c2ecf20Sopenharmony_ci		}
1168c2ecf20Sopenharmony_ci	} else {
1178c2ecf20Sopenharmony_ci		return err;
1188c2ecf20Sopenharmony_ci	}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
1218c2ecf20Sopenharmony_ci	if (err < 0)
1228c2ecf20Sopenharmony_ci		goto release_idr;
1238c2ecf20Sopenharmony_ci	gact = to_gact(*a);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	spin_lock_bh(&gact->tcf_lock);
1268c2ecf20Sopenharmony_ci	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
1278c2ecf20Sopenharmony_ci#ifdef CONFIG_GACT_PROB
1288c2ecf20Sopenharmony_ci	if (p_parm) {
1298c2ecf20Sopenharmony_ci		gact->tcfg_paction = p_parm->paction;
1308c2ecf20Sopenharmony_ci		gact->tcfg_pval    = max_t(u16, 1, p_parm->pval);
1318c2ecf20Sopenharmony_ci		/* Make sure tcfg_pval is written before tcfg_ptype
1328c2ecf20Sopenharmony_ci		 * coupled with smp_rmb() in gact_net_rand() & gact_determ()
1338c2ecf20Sopenharmony_ci		 */
1348c2ecf20Sopenharmony_ci		smp_wmb();
1358c2ecf20Sopenharmony_ci		gact->tcfg_ptype   = p_parm->ptype;
1368c2ecf20Sopenharmony_ci	}
1378c2ecf20Sopenharmony_ci#endif
1388c2ecf20Sopenharmony_ci	spin_unlock_bh(&gact->tcf_lock);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	if (goto_ch)
1418c2ecf20Sopenharmony_ci		tcf_chain_put_by_act(goto_ch);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	return ret;
1448c2ecf20Sopenharmony_cirelease_idr:
1458c2ecf20Sopenharmony_ci	tcf_idr_release(*a, bind);
1468c2ecf20Sopenharmony_ci	return err;
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic int tcf_gact_act(struct sk_buff *skb, const struct tc_action *a,
1508c2ecf20Sopenharmony_ci			struct tcf_result *res)
1518c2ecf20Sopenharmony_ci{
1528c2ecf20Sopenharmony_ci	struct tcf_gact *gact = to_gact(a);
1538c2ecf20Sopenharmony_ci	int action = READ_ONCE(gact->tcf_action);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci#ifdef CONFIG_GACT_PROB
1568c2ecf20Sopenharmony_ci	{
1578c2ecf20Sopenharmony_ci	u32 ptype = READ_ONCE(gact->tcfg_ptype);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	if (ptype)
1608c2ecf20Sopenharmony_ci		action = gact_rand[ptype](gact);
1618c2ecf20Sopenharmony_ci	}
1628c2ecf20Sopenharmony_ci#endif
1638c2ecf20Sopenharmony_ci	tcf_action_update_bstats(&gact->common, skb);
1648c2ecf20Sopenharmony_ci	if (action == TC_ACT_SHOT)
1658c2ecf20Sopenharmony_ci		tcf_action_inc_drop_qstats(&gact->common);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	tcf_lastuse_update(&gact->tcf_tm);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	return action;
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic void tcf_gact_stats_update(struct tc_action *a, u64 bytes, u64 packets,
1738c2ecf20Sopenharmony_ci				  u64 drops, u64 lastuse, bool hw)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci	struct tcf_gact *gact = to_gact(a);
1768c2ecf20Sopenharmony_ci	int action = READ_ONCE(gact->tcf_action);
1778c2ecf20Sopenharmony_ci	struct tcf_t *tm = &gact->tcf_tm;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	tcf_action_update_stats(a, bytes, packets,
1808c2ecf20Sopenharmony_ci				action == TC_ACT_SHOT ? packets : drops, hw);
1818c2ecf20Sopenharmony_ci	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a,
1858c2ecf20Sopenharmony_ci			 int bind, int ref)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	unsigned char *b = skb_tail_pointer(skb);
1888c2ecf20Sopenharmony_ci	struct tcf_gact *gact = to_gact(a);
1898c2ecf20Sopenharmony_ci	struct tc_gact opt = {
1908c2ecf20Sopenharmony_ci		.index   = gact->tcf_index,
1918c2ecf20Sopenharmony_ci		.refcnt  = refcount_read(&gact->tcf_refcnt) - ref,
1928c2ecf20Sopenharmony_ci		.bindcnt = atomic_read(&gact->tcf_bindcnt) - bind,
1938c2ecf20Sopenharmony_ci	};
1948c2ecf20Sopenharmony_ci	struct tcf_t t;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	spin_lock_bh(&gact->tcf_lock);
1978c2ecf20Sopenharmony_ci	opt.action = gact->tcf_action;
1988c2ecf20Sopenharmony_ci	if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt))
1998c2ecf20Sopenharmony_ci		goto nla_put_failure;
2008c2ecf20Sopenharmony_ci#ifdef CONFIG_GACT_PROB
2018c2ecf20Sopenharmony_ci	if (gact->tcfg_ptype) {
2028c2ecf20Sopenharmony_ci		struct tc_gact_p p_opt = {
2038c2ecf20Sopenharmony_ci			.paction = gact->tcfg_paction,
2048c2ecf20Sopenharmony_ci			.pval    = gact->tcfg_pval,
2058c2ecf20Sopenharmony_ci			.ptype   = gact->tcfg_ptype,
2068c2ecf20Sopenharmony_ci		};
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci		if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt))
2098c2ecf20Sopenharmony_ci			goto nla_put_failure;
2108c2ecf20Sopenharmony_ci	}
2118c2ecf20Sopenharmony_ci#endif
2128c2ecf20Sopenharmony_ci	tcf_tm_dump(&t, &gact->tcf_tm);
2138c2ecf20Sopenharmony_ci	if (nla_put_64bit(skb, TCA_GACT_TM, sizeof(t), &t, TCA_GACT_PAD))
2148c2ecf20Sopenharmony_ci		goto nla_put_failure;
2158c2ecf20Sopenharmony_ci	spin_unlock_bh(&gact->tcf_lock);
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	return skb->len;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cinla_put_failure:
2208c2ecf20Sopenharmony_ci	spin_unlock_bh(&gact->tcf_lock);
2218c2ecf20Sopenharmony_ci	nlmsg_trim(skb, b);
2228c2ecf20Sopenharmony_ci	return -1;
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cistatic int tcf_gact_walker(struct net *net, struct sk_buff *skb,
2268c2ecf20Sopenharmony_ci			   struct netlink_callback *cb, int type,
2278c2ecf20Sopenharmony_ci			   const struct tc_action_ops *ops,
2288c2ecf20Sopenharmony_ci			   struct netlink_ext_ack *extack)
2298c2ecf20Sopenharmony_ci{
2308c2ecf20Sopenharmony_ci	struct tc_action_net *tn = net_generic(net, gact_net_id);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cistatic int tcf_gact_search(struct net *net, struct tc_action **a, u32 index)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	struct tc_action_net *tn = net_generic(net, gact_net_id);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	return tcf_idr_search(tn, a, index);
2408c2ecf20Sopenharmony_ci}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_cistatic size_t tcf_gact_get_fill_size(const struct tc_action *act)
2438c2ecf20Sopenharmony_ci{
2448c2ecf20Sopenharmony_ci	size_t sz = nla_total_size(sizeof(struct tc_gact)); /* TCA_GACT_PARMS */
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci#ifdef CONFIG_GACT_PROB
2478c2ecf20Sopenharmony_ci	if (to_gact(act)->tcfg_ptype)
2488c2ecf20Sopenharmony_ci		/* TCA_GACT_PROB */
2498c2ecf20Sopenharmony_ci		sz += nla_total_size(sizeof(struct tc_gact_p));
2508c2ecf20Sopenharmony_ci#endif
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	return sz;
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_cistatic struct tc_action_ops act_gact_ops = {
2568c2ecf20Sopenharmony_ci	.kind		=	"gact",
2578c2ecf20Sopenharmony_ci	.id		=	TCA_ID_GACT,
2588c2ecf20Sopenharmony_ci	.owner		=	THIS_MODULE,
2598c2ecf20Sopenharmony_ci	.act		=	tcf_gact_act,
2608c2ecf20Sopenharmony_ci	.stats_update	=	tcf_gact_stats_update,
2618c2ecf20Sopenharmony_ci	.dump		=	tcf_gact_dump,
2628c2ecf20Sopenharmony_ci	.init		=	tcf_gact_init,
2638c2ecf20Sopenharmony_ci	.walk		=	tcf_gact_walker,
2648c2ecf20Sopenharmony_ci	.lookup		=	tcf_gact_search,
2658c2ecf20Sopenharmony_ci	.get_fill_size	=	tcf_gact_get_fill_size,
2668c2ecf20Sopenharmony_ci	.size		=	sizeof(struct tcf_gact),
2678c2ecf20Sopenharmony_ci};
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_cistatic __net_init int gact_init_net(struct net *net)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	struct tc_action_net *tn = net_generic(net, gact_net_id);
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	return tc_action_net_init(net, tn, &act_gact_ops);
2748c2ecf20Sopenharmony_ci}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_cistatic void __net_exit gact_exit_net(struct list_head *net_list)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	tc_action_net_exit(net_list, gact_net_id);
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_cistatic struct pernet_operations gact_net_ops = {
2828c2ecf20Sopenharmony_ci	.init = gact_init_net,
2838c2ecf20Sopenharmony_ci	.exit_batch = gact_exit_net,
2848c2ecf20Sopenharmony_ci	.id   = &gact_net_id,
2858c2ecf20Sopenharmony_ci	.size = sizeof(struct tc_action_net),
2868c2ecf20Sopenharmony_ci};
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
2898c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Generic Classifier actions");
2908c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_cistatic int __init gact_init_module(void)
2938c2ecf20Sopenharmony_ci{
2948c2ecf20Sopenharmony_ci#ifdef CONFIG_GACT_PROB
2958c2ecf20Sopenharmony_ci	pr_info("GACT probability on\n");
2968c2ecf20Sopenharmony_ci#else
2978c2ecf20Sopenharmony_ci	pr_info("GACT probability NOT on\n");
2988c2ecf20Sopenharmony_ci#endif
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	return tcf_register_action(&act_gact_ops, &gact_net_ops);
3018c2ecf20Sopenharmony_ci}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_cistatic void __exit gact_cleanup_module(void)
3048c2ecf20Sopenharmony_ci{
3058c2ecf20Sopenharmony_ci	tcf_unregister_action(&act_gact_ops, &gact_net_ops);
3068c2ecf20Sopenharmony_ci}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_cimodule_init(gact_init_module);
3098c2ecf20Sopenharmony_cimodule_exit(gact_cleanup_module);
310