Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec
[deliverable/linux.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
47struct metadata_dst *metadata_dst_alloc(u8 optslen, gfp_t flags);
d3aa45ce 48struct metadata_dst __percpu *metadata_dst_alloc_percpu(u8 optslen, gfp_t flags);
f38a9eb1 49
4c222798 50static inline struct metadata_dst *tun_rx_dst(int md_size)
c29a70d2
PS
51{
52 struct metadata_dst *tun_dst;
c29a70d2
PS
53
54 tun_dst = metadata_dst_alloc(md_size, GFP_ATOMIC);
55 if (!tun_dst)
56 return NULL;
57
4c222798
PS
58 tun_dst->u.tun_info.options_len = 0;
59 tun_dst->u.tun_info.mode = 0;
c29a70d2
PS
60 return tun_dst;
61}
62
fc4099f1
PS
63static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
64{
65 struct metadata_dst *md_dst = skb_metadata_dst(skb);
f63ce5b6 66 int md_size;
fc4099f1
PS
67 struct metadata_dst *new_md;
68
69 if (!md_dst)
70 return ERR_PTR(-EINVAL);
71
f63ce5b6 72 md_size = md_dst->u.tun_info.options_len;
fc4099f1
PS
73 new_md = metadata_dst_alloc(md_size, GFP_ATOMIC);
74 if (!new_md)
75 return ERR_PTR(-ENOMEM);
76
77 memcpy(&new_md->u.tun_info, &md_dst->u.tun_info,
78 sizeof(struct ip_tunnel_info) + md_size);
79 skb_dst_drop(skb);
80 dst_hold(&new_md->dst);
81 skb_dst_set(skb, &new_md->dst);
82 return new_md;
83}
84
85static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb)
86{
87 struct metadata_dst *dst;
88
89 dst = tun_dst_unclone(skb);
90 if (IS_ERR(dst))
91 return NULL;
92
93 return &dst->u.tun_info;
94}
95
c29a70d2
PS
96static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
97 __be16 flags,
98 __be64 tunnel_id,
99 int md_size)
100{
101 const struct iphdr *iph = ip_hdr(skb);
102 struct metadata_dst *tun_dst;
c29a70d2 103
4c222798 104 tun_dst = tun_rx_dst(md_size);
c29a70d2
PS
105 if (!tun_dst)
106 return NULL;
107
4c222798
PS
108 ip_tunnel_key_init(&tun_dst->u.tun_info.key,
109 iph->saddr, iph->daddr, iph->tos, iph->ttl,
110 0, 0, tunnel_id, flags);
c29a70d2
PS
111 return tun_dst;
112}
113
114static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb,
115 __be16 flags,
116 __be64 tunnel_id,
117 int md_size)
118{
119 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
120 struct metadata_dst *tun_dst;
121 struct ip_tunnel_info *info;
122
4c222798 123 tun_dst = tun_rx_dst(md_size);
c29a70d2
PS
124 if (!tun_dst)
125 return NULL;
126
127 info = &tun_dst->u.tun_info;
4c222798
PS
128 info->mode = IP_TUNNEL_INFO_IPV6;
129 info->key.tun_flags = flags;
130 info->key.tun_id = tunnel_id;
131 info->key.tp_src = 0;
132 info->key.tp_dst = 0;
133
c29a70d2
PS
134 info->key.u.ipv6.src = ip6h->saddr;
135 info->key.u.ipv6.dst = ip6h->daddr;
136 info->key.tos = ipv6_get_dsfield(ip6h);
137 info->key.ttl = ip6h->hop_limit;
138 return tun_dst;
139}
140
f38a9eb1 141#endif /* __NET_DST_METADATA_H */
This page took 0.050101 seconds and 5 git commands to generate.