dst_metadata.h 4.4 KB
Newer Older
T
Thomas Graf 已提交
1 2 3 4 5 6 7 8 9
#ifndef __NET_DST_METADATA_H
#define __NET_DST_METADATA_H 1

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

struct metadata_dst {
	struct dst_entry		dst;
T
Thomas Graf 已提交
10 11 12
	union {
		struct ip_tunnel_info	tun_info;
	} u;
T
Thomas Graf 已提交
13 14 15 16 17 18 19 20 21 22 23 24
};

static inline struct metadata_dst *skb_metadata_dst(struct sk_buff *skb)
{
	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;
}

25
static inline struct ip_tunnel_info *skb_tunnel_info(struct sk_buff *skb)
T
Thomas Graf 已提交
26 27
{
	struct metadata_dst *md_dst = skb_metadata_dst(skb);
28
	struct dst_entry *dst;
T
Thomas Graf 已提交
29 30 31 32

	if (md_dst)
		return &md_dst->u.tun_info;

33 34 35
	dst = skb_dst(skb);
	if (dst && dst->lwtstate)
		return lwt_tun_info(dst->lwtstate);
36

T
Thomas Graf 已提交
37 38 39
	return NULL;
}

T
Thomas Graf 已提交
40 41 42 43 44 45 46
static inline bool skb_valid_dst(const struct sk_buff *skb)
{
	struct dst_entry *dst = skb_dst(skb);

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

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
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);

	if (!a != !b || a->u.tun_info.options_len != b->u.tun_info.options_len)
		return 1;

	return memcmp(&a->u.tun_info, &b->u.tun_info,
		      sizeof(a->u.tun_info) + a->u.tun_info.options_len);
}

65
void metadata_dst_free(struct metadata_dst *);
T
Thomas Graf 已提交
66
struct metadata_dst *metadata_dst_alloc(u8 optslen, gfp_t flags);
67
struct metadata_dst __percpu *metadata_dst_alloc_percpu(u8 optslen, gfp_t flags);
T
Thomas Graf 已提交
68

69
static inline struct metadata_dst *tun_rx_dst(int md_size)
70 71 72 73 74 75 76
{
	struct metadata_dst *tun_dst;

	tun_dst = metadata_dst_alloc(md_size, GFP_ATOMIC);
	if (!tun_dst)
		return NULL;

77 78
	tun_dst->u.tun_info.options_len = 0;
	tun_dst->u.tun_info.mode = 0;
79 80 81
	return tun_dst;
}

82 83 84
static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
{
	struct metadata_dst *md_dst = skb_metadata_dst(skb);
85
	int md_size;
86 87 88 89 90
	struct metadata_dst *new_md;

	if (!md_dst)
		return ERR_PTR(-EINVAL);

91
	md_size = md_dst->u.tun_info.options_len;
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	new_md = metadata_dst_alloc(md_size, GFP_ATOMIC);
	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);
	skb_dst_drop(skb);
	dst_hold(&new_md->dst);
	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;
}

115 116 117
static inline struct metadata_dst *__ip_tun_set_dst(__be32 saddr,
						    __be32 daddr,
						    __u8 tos, __u8 ttl,
118
						    __be16 tp_dst,
119 120 121
						    __be16 flags,
						    __be64 tunnel_id,
						    int md_size)
122 123 124
{
	struct metadata_dst *tun_dst;

125
	tun_dst = tun_rx_dst(md_size);
126 127 128
	if (!tun_dst)
		return NULL;

129
	ip_tunnel_key_init(&tun_dst->u.tun_info.key,
130
			   saddr, daddr, tos, ttl,
131
			   0, 0, tp_dst, tunnel_id, flags);
132 133 134
	return tun_dst;
}

135
static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
136 137 138 139
						 __be16 flags,
						 __be64 tunnel_id,
						 int md_size)
{
140 141 142
	const struct iphdr *iph = ip_hdr(skb);

	return __ip_tun_set_dst(iph->saddr, iph->daddr, iph->tos, iph->ttl,
143
				0, flags, tunnel_id, md_size);
144 145 146 147 148
}

static inline struct metadata_dst *__ipv6_tun_set_dst(const struct in6_addr *saddr,
						      const struct in6_addr *daddr,
						      __u8 tos, __u8 ttl,
149
						      __be16 tp_dst,
150 151 152 153 154
						      __be32 label,
						      __be16 flags,
						      __be64 tunnel_id,
						      int md_size)
{
155 156 157
	struct metadata_dst *tun_dst;
	struct ip_tunnel_info *info;

158
	tun_dst = tun_rx_dst(md_size);
159 160 161 162
	if (!tun_dst)
		return NULL;

	info = &tun_dst->u.tun_info;
163 164 165 166
	info->mode = IP_TUNNEL_INFO_IPV6;
	info->key.tun_flags = flags;
	info->key.tun_id = tunnel_id;
	info->key.tp_src = 0;
167
	info->key.tp_dst = tp_dst;
168

169 170
	info->key.u.ipv6.src = *saddr;
	info->key.u.ipv6.dst = *daddr;
171

172 173 174
	info->key.tos = tos;
	info->key.ttl = ttl;
	info->key.label = label;
175

176 177 178
	return tun_dst;
}

179 180 181 182 183 184 185 186 187
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,
188
				  0, ip6_flowlabel(ip6h), flags, tunnel_id,
189 190
				  md_size);
}
T
Thomas Graf 已提交
191
#endif /* __NET_DST_METADATA_H */