]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - include/net/dst_metadata.h
Merge branch 'drm-vmwgfx-fixes-4_10' of git://people.freedesktop.org/~syeh/repos_linu...
[mirror_ubuntu-zesty-kernel.git] / include / net / dst_metadata.h
CommitLineData
f38a9eb1
TG
1#ifndef __NET_DST_METADATA_H
2#define __NET_DST_METADATA_H 1
3
4#include <linux/skbuff.h>
5#include <net/ip_tunnels.h>
6#include <net/dst.h>
7
8struct metadata_dst {
9 struct dst_entry dst;
ee122c79
TG
10 union {
11 struct ip_tunnel_info tun_info;
12 } u;
f38a9eb1
TG
13};
14
15static inline struct metadata_dst *skb_metadata_dst(struct sk_buff *skb)
16{
17 struct metadata_dst *md_dst = (struct metadata_dst *) skb_dst(skb);
18
19 if (md_dst && md_dst->dst.flags & DST_METADATA)
20 return md_dst;
21
22 return NULL;
23}
24
61adedf3 25static inline struct ip_tunnel_info *skb_tunnel_info(struct sk_buff *skb)
ee122c79
TG
26{
27 struct metadata_dst *md_dst = skb_metadata_dst(skb);
61adedf3 28 struct dst_entry *dst;
ee122c79
TG
29
30 if (md_dst)
31 return &md_dst->u.tun_info;
32
61adedf3
JB
33 dst = skb_dst(skb);
34 if (dst && dst->lwtstate)
35 return lwt_tun_info(dst->lwtstate);
3093fbe7 36
ee122c79
TG
37 return NULL;
38}
39
f38a9eb1
TG
40static inline bool skb_valid_dst(const struct sk_buff *skb)
41{
42 struct dst_entry *dst = skb_dst(skb);
43
44 return dst && !(dst->flags & DST_METADATA);
45}
46
ce87fc6c
JG
47static inline int skb_metadata_dst_cmp(const struct sk_buff *skb_a,
48 const struct sk_buff *skb_b)
49{
50 const struct metadata_dst *a, *b;
51
52 if (!(skb_a->_skb_refdst | skb_b->_skb_refdst))
53 return 0;
54
55 a = (const struct metadata_dst *) skb_dst(skb_a);
56 b = (const struct metadata_dst *) skb_dst(skb_b);
57
58 if (!a != !b || a->u.tun_info.options_len != b->u.tun_info.options_len)
59 return 1;
60
61 return memcmp(&a->u.tun_info, &b->u.tun_info,
62 sizeof(a->u.tun_info) + a->u.tun_info.options_len);
63}
64
d71785ff 65void metadata_dst_free(struct metadata_dst *);
f38a9eb1 66struct metadata_dst *metadata_dst_alloc(u8 optslen, gfp_t flags);
d3aa45ce 67struct metadata_dst __percpu *metadata_dst_alloc_percpu(u8 optslen, gfp_t flags);
f38a9eb1 68
4c222798 69static inline struct metadata_dst *tun_rx_dst(int md_size)
c29a70d2
PS
70{
71 struct metadata_dst *tun_dst;
c29a70d2
PS
72
73 tun_dst = metadata_dst_alloc(md_size, GFP_ATOMIC);
74 if (!tun_dst)
75 return NULL;
76
4c222798
PS
77 tun_dst->u.tun_info.options_len = 0;
78 tun_dst->u.tun_info.mode = 0;
c29a70d2
PS
79 return tun_dst;
80}
81
fc4099f1
PS
82static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
83{
84 struct metadata_dst *md_dst = skb_metadata_dst(skb);
f63ce5b6 85 int md_size;
fc4099f1
PS
86 struct metadata_dst *new_md;
87
88 if (!md_dst)
89 return ERR_PTR(-EINVAL);
90
f63ce5b6 91 md_size = md_dst->u.tun_info.options_len;
fc4099f1
PS
92 new_md = metadata_dst_alloc(md_size, GFP_ATOMIC);
93 if (!new_md)
94 return ERR_PTR(-ENOMEM);
95
96 memcpy(&new_md->u.tun_info, &md_dst->u.tun_info,
97 sizeof(struct ip_tunnel_info) + md_size);
98 skb_dst_drop(skb);
99 dst_hold(&new_md->dst);
100 skb_dst_set(skb, &new_md->dst);
101 return new_md;
102}
103
104static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb)
105{
106 struct metadata_dst *dst;
107
108 dst = tun_dst_unclone(skb);
109 if (IS_ERR(dst))
110 return NULL;
111
112 return &dst->u.tun_info;
113}
114
2ff378b7
AV
115static inline struct metadata_dst *__ip_tun_set_dst(__be32 saddr,
116 __be32 daddr,
117 __u8 tos, __u8 ttl,
24ba898d 118 __be16 tp_dst,
2ff378b7
AV
119 __be16 flags,
120 __be64 tunnel_id,
121 int md_size)
c29a70d2 122{
c29a70d2 123 struct metadata_dst *tun_dst;
c29a70d2 124
4c222798 125 tun_dst = tun_rx_dst(md_size);
c29a70d2
PS
126 if (!tun_dst)
127 return NULL;
128
4c222798 129 ip_tunnel_key_init(&tun_dst->u.tun_info.key,
2ff378b7 130 saddr, daddr, tos, ttl,
24ba898d 131 0, 0, tp_dst, tunnel_id, flags);
c29a70d2
PS
132 return tun_dst;
133}
134
2ff378b7 135static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
c29a70d2
PS
136 __be16 flags,
137 __be64 tunnel_id,
138 int md_size)
139{
2ff378b7
AV
140 const struct iphdr *iph = ip_hdr(skb);
141
142 return __ip_tun_set_dst(iph->saddr, iph->daddr, iph->tos, iph->ttl,
24ba898d 143 0, flags, tunnel_id, md_size);
2ff378b7
AV
144}
145
146static inline struct metadata_dst *__ipv6_tun_set_dst(const struct in6_addr *saddr,
147 const struct in6_addr *daddr,
148 __u8 tos, __u8 ttl,
24ba898d 149 __be16 tp_dst,
2ff378b7
AV
150 __be32 label,
151 __be16 flags,
152 __be64 tunnel_id,
153 int md_size)
154{
c29a70d2
PS
155 struct metadata_dst *tun_dst;
156 struct ip_tunnel_info *info;
157
4c222798 158 tun_dst = tun_rx_dst(md_size);
c29a70d2
PS
159 if (!tun_dst)
160 return NULL;
161
162 info = &tun_dst->u.tun_info;
4c222798
PS
163 info->mode = IP_TUNNEL_INFO_IPV6;
164 info->key.tun_flags = flags;
165 info->key.tun_id = tunnel_id;
166 info->key.tp_src = 0;
24ba898d 167 info->key.tp_dst = tp_dst;
4c222798 168
2ff378b7
AV
169 info->key.u.ipv6.src = *saddr;
170 info->key.u.ipv6.dst = *daddr;
13461144 171
2ff378b7
AV
172 info->key.tos = tos;
173 info->key.ttl = ttl;
174 info->key.label = label;
13461144 175
c29a70d2
PS
176 return tun_dst;
177}
178
2ff378b7
AV
179static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb,
180 __be16 flags,
181 __be64 tunnel_id,
182 int md_size)
183{
184 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
185
186 return __ipv6_tun_set_dst(&ip6h->saddr, &ip6h->daddr,
187 ipv6_get_dsfield(ip6h), ip6h->hop_limit,
24ba898d 188 0, ip6_flowlabel(ip6h), flags, tunnel_id,
2ff378b7
AV
189 md_size);
190}
f38a9eb1 191#endif /* __NET_DST_METADATA_H */