blob: ce009710120ca8b541615b237a329ee089ec357b [file] [log] [blame]
Thomas Graff38a9eb2015-07-21 10:43:56 +02001#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;
Thomas Grafee122c72015-07-21 10:43:58 +020010 union {
11 struct ip_tunnel_info tun_info;
12 } u;
Thomas Graff38a9eb2015-07-21 10:43:56 +020013};
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
Jiri Benc61adedf2015-08-20 13:56:25 +020025static inline struct ip_tunnel_info *skb_tunnel_info(struct sk_buff *skb)
Thomas Grafee122c72015-07-21 10:43:58 +020026{
27 struct metadata_dst *md_dst = skb_metadata_dst(skb);
Jiri Benc61adedf2015-08-20 13:56:25 +020028 struct dst_entry *dst;
Thomas Grafee122c72015-07-21 10:43:58 +020029
30 if (md_dst)
31 return &md_dst->u.tun_info;
32
Jiri Benc61adedf2015-08-20 13:56:25 +020033 dst = skb_dst(skb);
34 if (dst && dst->lwtstate)
35 return lwt_tun_info(dst->lwtstate);
Thomas Graf3093fbe2015-07-21 10:44:00 +020036
Thomas Grafee122c72015-07-21 10:43:58 +020037 return NULL;
38}
39
Thomas Graff38a9eb2015-07-21 10:43:56 +020040static 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);
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -070048struct metadata_dst __percpu *metadata_dst_alloc_percpu(u8 optslen, gfp_t flags);
Thomas Graff38a9eb2015-07-21 10:43:56 +020049
Pravin B Shelar4c222792015-08-30 18:09:38 -070050static inline struct metadata_dst *tun_rx_dst(int md_size)
Pravin B Shelarc29a70d2015-08-26 23:46:50 -070051{
52 struct metadata_dst *tun_dst;
Pravin B Shelarc29a70d2015-08-26 23:46:50 -070053
54 tun_dst = metadata_dst_alloc(md_size, GFP_ATOMIC);
55 if (!tun_dst)
56 return NULL;
57
Pravin B Shelar4c222792015-08-30 18:09:38 -070058 tun_dst->u.tun_info.options_len = 0;
59 tun_dst->u.tun_info.mode = 0;
Pravin B Shelarc29a70d2015-08-26 23:46:50 -070060 return tun_dst;
61}
62
Pravin B Shelarfc4099f2015-10-22 18:17:16 -070063static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
64{
65 struct metadata_dst *md_dst = skb_metadata_dst(skb);
66 int md_size = md_dst->u.tun_info.options_len;
67 struct metadata_dst *new_md;
68
69 if (!md_dst)
70 return ERR_PTR(-EINVAL);
71
72 new_md = metadata_dst_alloc(md_size, GFP_ATOMIC);
73 if (!new_md)
74 return ERR_PTR(-ENOMEM);
75
76 memcpy(&new_md->u.tun_info, &md_dst->u.tun_info,
77 sizeof(struct ip_tunnel_info) + md_size);
78 skb_dst_drop(skb);
79 dst_hold(&new_md->dst);
80 skb_dst_set(skb, &new_md->dst);
81 return new_md;
82}
83
84static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb)
85{
86 struct metadata_dst *dst;
87
88 dst = tun_dst_unclone(skb);
89 if (IS_ERR(dst))
90 return NULL;
91
92 return &dst->u.tun_info;
93}
94
Pravin B Shelarc29a70d2015-08-26 23:46:50 -070095static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
96 __be16 flags,
97 __be64 tunnel_id,
98 int md_size)
99{
100 const struct iphdr *iph = ip_hdr(skb);
101 struct metadata_dst *tun_dst;
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700102
Pravin B Shelar4c222792015-08-30 18:09:38 -0700103 tun_dst = tun_rx_dst(md_size);
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700104 if (!tun_dst)
105 return NULL;
106
Pravin B Shelar4c222792015-08-30 18:09:38 -0700107 ip_tunnel_key_init(&tun_dst->u.tun_info.key,
108 iph->saddr, iph->daddr, iph->tos, iph->ttl,
109 0, 0, tunnel_id, flags);
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700110 return tun_dst;
111}
112
113static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb,
114 __be16 flags,
115 __be64 tunnel_id,
116 int md_size)
117{
118 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
119 struct metadata_dst *tun_dst;
120 struct ip_tunnel_info *info;
121
Pravin B Shelar4c222792015-08-30 18:09:38 -0700122 tun_dst = tun_rx_dst(md_size);
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700123 if (!tun_dst)
124 return NULL;
125
126 info = &tun_dst->u.tun_info;
Pravin B Shelar4c222792015-08-30 18:09:38 -0700127 info->mode = IP_TUNNEL_INFO_IPV6;
128 info->key.tun_flags = flags;
129 info->key.tun_id = tunnel_id;
130 info->key.tp_src = 0;
131 info->key.tp_dst = 0;
132
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700133 info->key.u.ipv6.src = ip6h->saddr;
134 info->key.u.ipv6.dst = ip6h->daddr;
135 info->key.tos = ipv6_get_dsfield(ip6h);
136 info->key.ttl = ip6h->hop_limit;
137 return tun_dst;
138}
139
Thomas Graff38a9eb2015-07-21 10:43:56 +0200140#endif /* __NET_DST_METADATA_H */