dst_metadata.h 5.4 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
T
Thomas Graf 已提交
2 3 4 5 6 7 8
#ifndef __NET_DST_METADATA_H
#define __NET_DST_METADATA_H 1

#include <linux/skbuff.h>
#include <net/ip_tunnels.h>
#include <net/dst.h>

9 10 11 12 13 14 15 16 17 18
enum metadata_type {
	METADATA_IP_TUNNEL,
	METADATA_HW_PORT_MUX,
};

struct hw_port_info {
	struct net_device *lower_dev;
	u32 port_id;
};

T
Thomas Graf 已提交
19 20
struct metadata_dst {
	struct dst_entry		dst;
21
	enum metadata_type		type;
T
Thomas Graf 已提交
22 23
	union {
		struct ip_tunnel_info	tun_info;
24
		struct hw_port_info	port_info;
T
Thomas Graf 已提交
25
	} u;
T
Thomas Graf 已提交
26 27
};

28
static inline struct metadata_dst *skb_metadata_dst(const struct sk_buff *skb)
T
Thomas Graf 已提交
29 30 31 32 33 34 35 36 37
{
	struct metadata_dst *md_dst = (struct metadata_dst *) skb_dst(skb);

	if (md_dst && md_dst->dst.flags & DST_METADATA)
		return md_dst;

	return NULL;
}

38 39
static inline struct ip_tunnel_info *
skb_tunnel_info(const struct sk_buff *skb)
T
Thomas Graf 已提交
40 41
{
	struct metadata_dst *md_dst = skb_metadata_dst(skb);
42
	struct dst_entry *dst;
T
Thomas Graf 已提交
43

44
	if (md_dst && md_dst->type == METADATA_IP_TUNNEL)
T
Thomas Graf 已提交
45 46
		return &md_dst->u.tun_info;

47
	dst = skb_dst(skb);
48 49 50
	if (dst && dst->lwtstate &&
	    (dst->lwtstate->type == LWTUNNEL_ENCAP_IP ||
	     dst->lwtstate->type == LWTUNNEL_ENCAP_IP6))
51
		return lwt_tun_info(dst->lwtstate);
52

T
Thomas Graf 已提交
53 54 55
	return NULL;
}

T
Thomas Graf 已提交
56 57 58 59 60 61 62
static inline bool skb_valid_dst(const struct sk_buff *skb)
{
	struct dst_entry *dst = skb_dst(skb);

	return dst && !(dst->flags & DST_METADATA);
}

63 64 65 66 67 68 69 70 71 72 73
static inline int skb_metadata_dst_cmp(const struct sk_buff *skb_a,
				       const struct sk_buff *skb_b)
{
	const struct metadata_dst *a, *b;

	if (!(skb_a->_skb_refdst | skb_b->_skb_refdst))
		return 0;

	a = (const struct metadata_dst *) skb_dst(skb_a);
	b = (const struct metadata_dst *) skb_dst(skb_b);

74
	if (!a != !b || a->type != b->type)
75 76
		return 1;

77 78 79 80 81 82 83 84 85 86 87
	switch (a->type) {
	case METADATA_HW_PORT_MUX:
		return memcmp(&a->u.port_info, &b->u.port_info,
			      sizeof(a->u.port_info));
	case METADATA_IP_TUNNEL:
		return memcmp(&a->u.tun_info, &b->u.tun_info,
			      sizeof(a->u.tun_info) +
					 a->u.tun_info.options_len);
	default:
		return 1;
	}
88 89
}

90
void metadata_dst_free(struct metadata_dst *);
91 92
struct metadata_dst *metadata_dst_alloc(u8 optslen, enum metadata_type type,
					gfp_t flags);
93
void metadata_dst_free_percpu(struct metadata_dst __percpu *md_dst);
94 95
struct metadata_dst __percpu *
metadata_dst_alloc_percpu(u8 optslen, enum metadata_type type, gfp_t flags);
T
Thomas Graf 已提交
96

97
static inline struct metadata_dst *tun_rx_dst(int md_size)
98 99 100
{
	struct metadata_dst *tun_dst;

101
	tun_dst = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC);
102 103 104
	if (!tun_dst)
		return NULL;

105 106
	tun_dst->u.tun_info.options_len = 0;
	tun_dst->u.tun_info.mode = 0;
107 108 109
	return tun_dst;
}

110 111 112
static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
{
	struct metadata_dst *md_dst = skb_metadata_dst(skb);
113
	int md_size;
114 115
	struct metadata_dst *new_md;

116
	if (!md_dst || md_dst->type != METADATA_IP_TUNNEL)
117 118
		return ERR_PTR(-EINVAL);

119
	md_size = md_dst->u.tun_info.options_len;
120
	new_md = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC);
121 122 123 124 125
	if (!new_md)
		return ERR_PTR(-ENOMEM);

	memcpy(&new_md->u.tun_info, &md_dst->u.tun_info,
	       sizeof(struct ip_tunnel_info) + md_size);
126 127 128 129 130 131 132 133 134 135 136 137 138
#ifdef CONFIG_DST_CACHE
	/* Unclone the dst cache if there is one */
	if (new_md->u.tun_info.dst_cache.cache) {
		int ret;

		ret = dst_cache_init(&new_md->u.tun_info.dst_cache, GFP_ATOMIC);
		if (ret) {
			metadata_dst_free(new_md);
			return ERR_PTR(ret);
		}
	}
#endif

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	skb_dst_drop(skb);
	skb_dst_set(skb, &new_md->dst);
	return new_md;
}

static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb)
{
	struct metadata_dst *dst;

	dst = tun_dst_unclone(skb);
	if (IS_ERR(dst))
		return NULL;

	return &dst->u.tun_info;
}

155 156 157
static inline struct metadata_dst *__ip_tun_set_dst(__be32 saddr,
						    __be32 daddr,
						    __u8 tos, __u8 ttl,
158
						    __be16 tp_dst,
159 160 161
						    __be16 flags,
						    __be64 tunnel_id,
						    int md_size)
162 163 164
{
	struct metadata_dst *tun_dst;

165
	tun_dst = tun_rx_dst(md_size);
166 167 168
	if (!tun_dst)
		return NULL;

169
	ip_tunnel_key_init(&tun_dst->u.tun_info.key,
170
			   saddr, daddr, tos, ttl,
171
			   0, 0, tp_dst, tunnel_id, flags);
172 173 174
	return tun_dst;
}

175
static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
176 177 178 179
						 __be16 flags,
						 __be64 tunnel_id,
						 int md_size)
{
180 181 182
	const struct iphdr *iph = ip_hdr(skb);

	return __ip_tun_set_dst(iph->saddr, iph->daddr, iph->tos, iph->ttl,
183
				0, flags, tunnel_id, md_size);
184 185 186 187 188
}

static inline struct metadata_dst *__ipv6_tun_set_dst(const struct in6_addr *saddr,
						      const struct in6_addr *daddr,
						      __u8 tos, __u8 ttl,
189
						      __be16 tp_dst,
190 191 192 193 194
						      __be32 label,
						      __be16 flags,
						      __be64 tunnel_id,
						      int md_size)
{
195 196 197
	struct metadata_dst *tun_dst;
	struct ip_tunnel_info *info;

198
	tun_dst = tun_rx_dst(md_size);
199 200 201 202
	if (!tun_dst)
		return NULL;

	info = &tun_dst->u.tun_info;
203 204 205 206
	info->mode = IP_TUNNEL_INFO_IPV6;
	info->key.tun_flags = flags;
	info->key.tun_id = tunnel_id;
	info->key.tp_src = 0;
207
	info->key.tp_dst = tp_dst;
208

209 210
	info->key.u.ipv6.src = *saddr;
	info->key.u.ipv6.dst = *daddr;
211

212 213 214
	info->key.tos = tos;
	info->key.ttl = ttl;
	info->key.label = label;
215

216 217 218
	return tun_dst;
}

219 220 221 222 223 224 225 226 227
static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb,
						   __be16 flags,
						   __be64 tunnel_id,
						   int md_size)
{
	const struct ipv6hdr *ip6h = ipv6_hdr(skb);

	return __ipv6_tun_set_dst(&ip6h->saddr, &ip6h->daddr,
				  ipv6_get_dsfield(ip6h), ip6h->hop_limit,
228
				  0, ip6_flowlabel(ip6h), flags, tunnel_id,
229 230
				  md_size);
}
T
Thomas Graf 已提交
231
#endif /* __NET_DST_METADATA_H */