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-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (c) 2008, Intel Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Author: Alexander Duyck <alexander.h.duyck@intel.com>
^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/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <net/pkt_sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <net/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <net/dsfield.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <net/pkt_cls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/tc_act/tc_skbedit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <net/tc_act/tc_skbedit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static unsigned int skbedit_net_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static struct tc_action_ops act_skbedit_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static int tcf_skbedit_act(struct sk_buff *skb, const struct tc_action *a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 			   struct tcf_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct tcf_skbedit *d = to_skbedit(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct tcf_skbedit_params *params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	int action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	tcf_lastuse_update(&d->tcf_tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	bstats_cpu_update(this_cpu_ptr(d->common.cpu_bstats), skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	params = rcu_dereference_bh(d->params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	action = READ_ONCE(d->tcf_action);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	if (params->flags & SKBEDIT_F_PRIORITY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		skb->priority = params->priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	if (params->flags & SKBEDIT_F_INHERITDSFIELD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		int wlen = skb_network_offset(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		switch (skb_protocol(skb, true)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		case htons(ETH_P_IP):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 			wlen += sizeof(struct iphdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 			if (!pskb_may_pull(skb, wlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 				goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 			skb->priority = ipv4_get_dsfield(ip_hdr(skb)) >> 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		case htons(ETH_P_IPV6):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 			wlen += sizeof(struct ipv6hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 			if (!pskb_may_pull(skb, wlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 				goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 			skb->priority = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	if (params->flags & SKBEDIT_F_QUEUE_MAPPING &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	    skb->dev->real_num_tx_queues > params->queue_mapping)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		skb_set_queue_mapping(skb, params->queue_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	if (params->flags & SKBEDIT_F_MARK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		skb->mark &= ~params->mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		skb->mark |= params->mark & params->mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	if (params->flags & SKBEDIT_F_PTYPE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		skb->pkt_type = params->ptype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	return action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	qstats_drop_inc(this_cpu_ptr(d->common.cpu_qstats));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	return TC_ACT_SHOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) static void tcf_skbedit_stats_update(struct tc_action *a, u64 bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 				     u64 packets, u64 drops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 				     u64 lastuse, bool hw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	struct tcf_skbedit *d = to_skbedit(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	struct tcf_t *tm = &d->tcf_tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	tcf_action_update_stats(a, bytes, packets, drops, hw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	[TCA_SKBEDIT_PARMS]		= { .len = sizeof(struct tc_skbedit) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	[TCA_SKBEDIT_PRIORITY]		= { .len = sizeof(u32) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	[TCA_SKBEDIT_QUEUE_MAPPING]	= { .len = sizeof(u16) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	[TCA_SKBEDIT_MARK]		= { .len = sizeof(u32) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	[TCA_SKBEDIT_PTYPE]		= { .len = sizeof(u16) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	[TCA_SKBEDIT_MASK]		= { .len = sizeof(u32) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	[TCA_SKBEDIT_FLAGS]		= { .len = sizeof(u64) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			    struct nlattr *est, struct tc_action **a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			    int ovr, int bind, bool rtnl_held,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			    struct tcf_proto *tp, u32 act_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			    struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	struct tc_action_net *tn = net_generic(net, skbedit_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	struct tcf_skbedit_params *params_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	struct tcf_chain *goto_ch = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	struct tc_skbedit *parm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	struct tcf_skbedit *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	u32 flags = 0, *priority = NULL, *mark = NULL, *mask = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	u16 *queue_mapping = NULL, *ptype = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	bool exists = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	int ret = 0, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	u32 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (nla == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	err = nla_parse_nested_deprecated(tb, TCA_SKBEDIT_MAX, nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 					  skbedit_policy, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	if (tb[TCA_SKBEDIT_PARMS] == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		flags |= SKBEDIT_F_PRIORITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		flags |= SKBEDIT_F_QUEUE_MAPPING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (tb[TCA_SKBEDIT_PTYPE] != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		if (!skb_pkt_type_ok(*ptype))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		flags |= SKBEDIT_F_PTYPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (tb[TCA_SKBEDIT_MARK] != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		flags |= SKBEDIT_F_MARK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		mark = nla_data(tb[TCA_SKBEDIT_MARK]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (tb[TCA_SKBEDIT_MASK] != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		flags |= SKBEDIT_F_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		mask = nla_data(tb[TCA_SKBEDIT_MASK]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (tb[TCA_SKBEDIT_FLAGS] != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		u64 *pure_flags = nla_data(tb[TCA_SKBEDIT_FLAGS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		if (*pure_flags & SKBEDIT_F_INHERITDSFIELD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			flags |= SKBEDIT_F_INHERITDSFIELD;
^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) 	parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	index = parm->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	err = tcf_idr_check_alloc(tn, &index, a, bind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	exists = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	if (exists && bind)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	if (!flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		if (exists)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			tcf_idr_release(*a, bind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			tcf_idr_cleanup(tn, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if (!exists) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		ret = tcf_idr_create(tn, index, est, a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 				     &act_skbedit_ops, bind, true, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			tcf_idr_cleanup(tn, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		d = to_skbedit(*a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		ret = ACT_P_CREATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		d = to_skbedit(*a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		if (!ovr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			tcf_idr_release(*a, bind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		goto release_idr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	if (unlikely(!params_new)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		goto put_chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	params_new->flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	if (flags & SKBEDIT_F_PRIORITY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		params_new->priority = *priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (flags & SKBEDIT_F_QUEUE_MAPPING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		params_new->queue_mapping = *queue_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	if (flags & SKBEDIT_F_MARK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		params_new->mark = *mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (flags & SKBEDIT_F_PTYPE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		params_new->ptype = *ptype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	/* default behaviour is to use all the bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	params_new->mask = 0xffffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	if (flags & SKBEDIT_F_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		params_new->mask = *mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	spin_lock_bh(&d->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	params_new = rcu_replace_pointer(d->params, params_new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 					 lockdep_is_held(&d->tcf_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	spin_unlock_bh(&d->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	if (params_new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		kfree_rcu(params_new, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (goto_ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		tcf_chain_put_by_act(goto_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) put_chain:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	if (goto_ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		tcf_chain_put_by_act(goto_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) release_idr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	tcf_idr_release(*a, bind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			    int bind, int ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	unsigned char *b = skb_tail_pointer(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	struct tcf_skbedit *d = to_skbedit(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	struct tcf_skbedit_params *params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	struct tc_skbedit opt = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		.index   = d->tcf_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		.refcnt  = refcount_read(&d->tcf_refcnt) - ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		.bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	u64 pure_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	struct tcf_t t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	spin_lock_bh(&d->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	params = rcu_dereference_protected(d->params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 					   lockdep_is_held(&d->tcf_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	opt.action = d->tcf_action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	if ((params->flags & SKBEDIT_F_PRIORITY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	    nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, params->priority))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	if ((params->flags & SKBEDIT_F_QUEUE_MAPPING) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	    nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, params->queue_mapping))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if ((params->flags & SKBEDIT_F_MARK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	    nla_put_u32(skb, TCA_SKBEDIT_MARK, params->mark))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	if ((params->flags & SKBEDIT_F_PTYPE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	    nla_put_u16(skb, TCA_SKBEDIT_PTYPE, params->ptype))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	if ((params->flags & SKBEDIT_F_MASK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	    nla_put_u32(skb, TCA_SKBEDIT_MASK, params->mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	if (params->flags & SKBEDIT_F_INHERITDSFIELD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		pure_flags |= SKBEDIT_F_INHERITDSFIELD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	if (pure_flags != 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	    nla_put(skb, TCA_SKBEDIT_FLAGS, sizeof(pure_flags), &pure_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	tcf_tm_dump(&t, &d->tcf_tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	spin_unlock_bh(&d->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	spin_unlock_bh(&d->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	nlmsg_trim(skb, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static void tcf_skbedit_cleanup(struct tc_action *a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	struct tcf_skbedit *d = to_skbedit(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	struct tcf_skbedit_params *params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	params = rcu_dereference_protected(d->params, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	if (params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		kfree_rcu(params, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			      struct netlink_callback *cb, int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			      const struct tc_action_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 			      struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	struct tc_action_net *tn = net_generic(net, skbedit_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	struct tc_action_net *tn = net_generic(net, skbedit_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	return tcf_idr_search(tn, a, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static size_t tcf_skbedit_get_fill_size(const struct tc_action *act)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	return nla_total_size(sizeof(struct tc_skbedit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		+ nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_PRIORITY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		+ nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_QUEUE_MAPPING */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		+ nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_MARK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		+ nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_PTYPE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		+ nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_MASK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		+ nla_total_size_64bit(sizeof(u64)); /* TCA_SKBEDIT_FLAGS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static struct tc_action_ops act_skbedit_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	.kind		=	"skbedit",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	.id		=	TCA_ID_SKBEDIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	.owner		=	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	.act		=	tcf_skbedit_act,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	.stats_update	=	tcf_skbedit_stats_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	.dump		=	tcf_skbedit_dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	.init		=	tcf_skbedit_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	.cleanup	=	tcf_skbedit_cleanup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	.walk		=	tcf_skbedit_walker,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	.get_fill_size	=	tcf_skbedit_get_fill_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	.lookup		=	tcf_skbedit_search,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	.size		=	sizeof(struct tcf_skbedit),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) static __net_init int skbedit_init_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	struct tc_action_net *tn = net_generic(net, skbedit_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	return tc_action_net_init(net, tn, &act_skbedit_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static void __net_exit skbedit_exit_net(struct list_head *net_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	tc_action_net_exit(net_list, skbedit_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) static struct pernet_operations skbedit_net_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	.init = skbedit_init_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	.exit_batch = skbedit_exit_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	.id   = &skbedit_net_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	.size = sizeof(struct tc_action_net),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) MODULE_DESCRIPTION("SKB Editing");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) static int __init skbedit_init_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) static void __exit skbedit_cleanup_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) module_init(skbedit_init_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) module_exit(skbedit_cleanup_module);