Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * net/sched/act_pedit.c	Generic packet editor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Authors:	Jamal Hadi Salim (2002-4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <net/pkt_sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/tc_act/tc_pedit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <net/tc_act/tc_pedit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <uapi/linux/tc_act/tc_pedit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <net/pkt_cls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static unsigned int pedit_net_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static struct tc_action_ops act_pedit_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) static const struct nla_policy pedit_policy[TCA_PEDIT_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	[TCA_PEDIT_PARMS]	= { .len = sizeof(struct tc_pedit) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	[TCA_PEDIT_KEYS_EX]   = { .type = NLA_NESTED },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) static const struct nla_policy pedit_key_ex_policy[TCA_PEDIT_KEY_EX_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	[TCA_PEDIT_KEY_EX_HTYPE]  = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	[TCA_PEDIT_KEY_EX_CMD]	  = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static struct tcf_pedit_key_ex *tcf_pedit_keys_ex_parse(struct nlattr *nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 							u8 n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct tcf_pedit_key_ex *keys_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct tcf_pedit_key_ex *k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	const struct nlattr *ka;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	int rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	if (!nla)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	keys_ex = kcalloc(n, sizeof(*k), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	if (!keys_ex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	k = keys_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	nla_for_each_nested(ka, nla, rem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		struct nlattr *tb[TCA_PEDIT_KEY_EX_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		if (!n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 			goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		n--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		if (nla_type(ka) != TCA_PEDIT_KEY_EX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		err = nla_parse_nested_deprecated(tb, TCA_PEDIT_KEY_EX_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 						  ka, pedit_key_ex_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 						  NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		if (!tb[TCA_PEDIT_KEY_EX_HTYPE] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		    !tb[TCA_PEDIT_KEY_EX_CMD]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		k->htype = nla_get_u16(tb[TCA_PEDIT_KEY_EX_HTYPE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		k->cmd = nla_get_u16(tb[TCA_PEDIT_KEY_EX_CMD]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		if (k->htype > TCA_PEDIT_HDR_TYPE_MAX ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		    k->cmd > TCA_PEDIT_CMD_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		k++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	if (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	return keys_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	kfree(keys_ex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static int tcf_pedit_key_ex_dump(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 				 struct tcf_pedit_key_ex *keys_ex, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	struct nlattr *keys_start = nla_nest_start_noflag(skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 							  TCA_PEDIT_KEYS_EX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	if (!keys_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		goto nla_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	for (; n > 0; n--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		struct nlattr *key_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		key_start = nla_nest_start_noflag(skb, TCA_PEDIT_KEY_EX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		if (!key_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 			goto nla_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		if (nla_put_u16(skb, TCA_PEDIT_KEY_EX_HTYPE, keys_ex->htype) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		    nla_put_u16(skb, TCA_PEDIT_KEY_EX_CMD, keys_ex->cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			goto nla_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		nla_nest_end(skb, key_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		keys_ex++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	nla_nest_end(skb, keys_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) nla_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	nla_nest_cancel(skb, keys_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static int tcf_pedit_init(struct net *net, struct nlattr *nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			  struct nlattr *est, struct tc_action **a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			  int ovr, int bind, bool rtnl_held,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			  struct tcf_proto *tp, u32 flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			  struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	struct tc_action_net *tn = net_generic(net, pedit_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	struct nlattr *tb[TCA_PEDIT_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	struct tcf_chain *goto_ch = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	struct tc_pedit_key *keys = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	struct tcf_pedit_key_ex *keys_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	struct tc_pedit *parm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	struct nlattr *pattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	struct tcf_pedit *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	int ret = 0, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	int ksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	u32 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	if (!nla) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		NL_SET_ERR_MSG_MOD(extack, "Pedit requires attributes to be passed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	err = nla_parse_nested_deprecated(tb, TCA_PEDIT_MAX, nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 					  pedit_policy, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	pattr = tb[TCA_PEDIT_PARMS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	if (!pattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		pattr = tb[TCA_PEDIT_PARMS_EX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	if (!pattr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		NL_SET_ERR_MSG_MOD(extack, "Missing required TCA_PEDIT_PARMS or TCA_PEDIT_PARMS_EX pedit attribute");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	parm = nla_data(pattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	if (!parm->nkeys) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		NL_SET_ERR_MSG_MOD(extack, "Pedit requires keys to be passed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	ksize = parm->nkeys * sizeof(struct tc_pedit_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	if (nla_len(pattr) < sizeof(*parm) + ksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		NL_SET_ERR_MSG_ATTR(extack, pattr, "Length of TCA_PEDIT_PARMS or TCA_PEDIT_PARMS_EX pedit attribute is invalid");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	keys_ex = tcf_pedit_keys_ex_parse(tb[TCA_PEDIT_KEYS_EX], parm->nkeys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	if (IS_ERR(keys_ex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		return PTR_ERR(keys_ex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	index = parm->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	err = tcf_idr_check_alloc(tn, &index, a, bind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		ret = tcf_idr_create(tn, index, est, a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 				     &act_pedit_ops, bind, false, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			tcf_idr_cleanup(tn, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		ret = ACT_P_CREATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	} else if (err > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		if (bind)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		if (!ovr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			goto out_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		goto out_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	p = to_pedit(*a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	spin_lock_bh(&p->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	if (ret == ACT_P_CREATED ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	    (p->tcfp_nkeys && p->tcfp_nkeys != parm->nkeys)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		keys = kmalloc(ksize, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		if (!keys) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			spin_unlock_bh(&p->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 			ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 			goto put_chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		kfree(p->tcfp_keys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		p->tcfp_keys = keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		p->tcfp_nkeys = parm->nkeys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	memcpy(p->tcfp_keys, parm->keys, ksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	p->tcfp_flags = parm->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	kfree(p->tcfp_keys_ex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	p->tcfp_keys_ex = keys_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	spin_unlock_bh(&p->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	if (goto_ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		tcf_chain_put_by_act(goto_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) put_chain:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (goto_ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		tcf_chain_put_by_act(goto_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) out_release:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	tcf_idr_release(*a, bind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	kfree(keys_ex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static void tcf_pedit_cleanup(struct tc_action *a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	struct tcf_pedit *p = to_pedit(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	struct tc_pedit_key *keys = p->tcfp_keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	kfree(keys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	kfree(p->tcfp_keys_ex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static bool offset_valid(struct sk_buff *skb, int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	if (offset > 0 && offset > skb->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	if  (offset < 0 && -offset > skb_headroom(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static int pedit_skb_hdr_offset(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 				enum pedit_header_type htype, int *hoffset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	switch (htype) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		if (skb_mac_header_was_set(skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			*hoffset = skb_mac_offset(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	case TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		*hoffset = skb_network_offset(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		if (skb_transport_header_was_set(skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			*hoffset = skb_transport_offset(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static int tcf_pedit_act(struct sk_buff *skb, const struct tc_action *a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			 struct tcf_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	struct tcf_pedit *p = to_pedit(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	if (skb_unclone(skb, GFP_ATOMIC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		return p->tcf_action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	spin_lock(&p->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	tcf_lastuse_update(&p->tcf_tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	if (p->tcfp_nkeys > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		struct tc_pedit_key *tkey = p->tcfp_keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		struct tcf_pedit_key_ex *tkey_ex = p->tcfp_keys_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		enum pedit_header_type htype =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		enum pedit_cmd cmd = TCA_PEDIT_KEY_EX_CMD_SET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		for (i = p->tcfp_nkeys; i > 0; i--, tkey++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			u32 *ptr, hdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 			int offset = tkey->off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			int hoffset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			u32 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			if (tkey_ex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 				htype = tkey_ex->htype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 				cmd = tkey_ex->cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 				tkey_ex++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			rc = pedit_skb_hdr_offset(skb, htype, &hoffset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 				pr_info("tc action pedit bad header type specified (0x%x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 					htype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 				goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 			if (tkey->offmask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 				u8 *d, _d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 				if (!offset_valid(skb, hoffset + tkey->at)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 					pr_info("tc action pedit 'at' offset %d out of bounds\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 						hoffset + tkey->at);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 					goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 				d = skb_header_pointer(skb, hoffset + tkey->at,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 						       sizeof(_d), &_d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 				if (!d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 					goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 				offset += (*d & tkey->offmask) >> tkey->shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			if (offset % 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 				pr_info("tc action pedit offset must be on 32 bit boundaries\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 				goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 			if (!offset_valid(skb, hoffset + offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 				pr_info("tc action pedit offset %d out of bounds\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 					hoffset + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 				goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 			ptr = skb_header_pointer(skb, hoffset + offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 						 sizeof(hdata), &hdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 			if (!ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 				goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 			/* just do it, baby */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 			switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 			case TCA_PEDIT_KEY_EX_CMD_SET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 				val = tkey->val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 			case TCA_PEDIT_KEY_EX_CMD_ADD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 				val = (*ptr + tkey->val) & ~tkey->mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 				pr_info("tc action pedit bad command (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 					cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 				goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 			*ptr = ((*ptr & tkey->mask) ^ val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 			if (ptr == &hdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 				skb_store_bits(skb, hoffset + offset, ptr, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		WARN(1, "pedit BUG: index %d\n", p->tcf_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	p->tcf_qstats.overlimits++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	bstats_update(&p->tcf_bstats, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	spin_unlock(&p->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	return p->tcf_action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) static void tcf_pedit_stats_update(struct tc_action *a, u64 bytes, u64 packets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 				   u64 drops, u64 lastuse, bool hw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	struct tcf_pedit *d = to_pedit(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	struct tcf_t *tm = &d->tcf_tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	tcf_action_update_stats(a, bytes, packets, drops, hw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 			  int bind, int ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	unsigned char *b = skb_tail_pointer(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	struct tcf_pedit *p = to_pedit(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	struct tc_pedit *opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	struct tcf_t t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	int s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	s = struct_size(opt, keys, p->tcfp_nkeys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	/* netlink spinlocks held above us - must use ATOMIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	opt = kzalloc(s, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	if (unlikely(!opt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	spin_lock_bh(&p->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	memcpy(opt->keys, p->tcfp_keys, flex_array_size(opt, keys, p->tcfp_nkeys));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	opt->index = p->tcf_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	opt->nkeys = p->tcfp_nkeys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	opt->flags = p->tcfp_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	opt->action = p->tcf_action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	opt->refcnt = refcount_read(&p->tcf_refcnt) - ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	opt->bindcnt = atomic_read(&p->tcf_bindcnt) - bind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	if (p->tcfp_keys_ex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		if (tcf_pedit_key_ex_dump(skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 					  p->tcfp_keys_ex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 					  p->tcfp_nkeys))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 			goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		if (nla_put(skb, TCA_PEDIT_PARMS_EX, s, opt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 			goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		if (nla_put(skb, TCA_PEDIT_PARMS, s, opt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 			goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	tcf_tm_dump(&t, &p->tcf_tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	if (nla_put_64bit(skb, TCA_PEDIT_TM, sizeof(t), &t, TCA_PEDIT_PAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	spin_unlock_bh(&p->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	kfree(opt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	spin_unlock_bh(&p->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	nlmsg_trim(skb, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	kfree(opt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) static int tcf_pedit_walker(struct net *net, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 			    struct netlink_callback *cb, int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 			    const struct tc_action_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 			    struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	struct tc_action_net *tn = net_generic(net, pedit_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) static int tcf_pedit_search(struct net *net, struct tc_action **a, u32 index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	struct tc_action_net *tn = net_generic(net, pedit_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	return tcf_idr_search(tn, a, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) static struct tc_action_ops act_pedit_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	.kind		=	"pedit",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	.id		=	TCA_ID_PEDIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	.owner		=	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	.act		=	tcf_pedit_act,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	.stats_update	=	tcf_pedit_stats_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	.dump		=	tcf_pedit_dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	.cleanup	=	tcf_pedit_cleanup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	.init		=	tcf_pedit_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	.walk		=	tcf_pedit_walker,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	.lookup		=	tcf_pedit_search,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	.size		=	sizeof(struct tcf_pedit),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) static __net_init int pedit_init_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	struct tc_action_net *tn = net_generic(net, pedit_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	return tc_action_net_init(net, tn, &act_pedit_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) static void __net_exit pedit_exit_net(struct list_head *net_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	tc_action_net_exit(net_list, pedit_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) static struct pernet_operations pedit_net_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	.init = pedit_init_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	.exit_batch = pedit_exit_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	.id   = &pedit_net_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	.size = sizeof(struct tc_action_net),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) MODULE_DESCRIPTION("Generic Packet Editor actions");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) static int __init pedit_init_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	return tcf_register_action(&act_pedit_ops, &pedit_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) static void __exit pedit_cleanup_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	tcf_unregister_action(&act_pedit_ops, &pedit_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) module_init(pedit_init_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) module_exit(pedit_cleanup_module);