GNU Linux-libre 4.9.333-gnu1
[releases.git] / include / net / dst_metadata.h
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
8 struct metadata_dst {
9         struct dst_entry                dst;
10         union {
11                 struct ip_tunnel_info   tun_info;
12         } u;
13 };
14
15 static 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
25 static inline struct ip_tunnel_info *skb_tunnel_info(struct sk_buff *skb)
26 {
27         struct metadata_dst *md_dst = skb_metadata_dst(skb);
28         struct dst_entry *dst;
29
30         if (md_dst)
31                 return &md_dst->u.tun_info;
32
33         dst = skb_dst(skb);
34         if (dst && dst->lwtstate &&
35             (dst->lwtstate->type == LWTUNNEL_ENCAP_IP ||
36              dst->lwtstate->type == LWTUNNEL_ENCAP_IP6))
37                 return lwt_tun_info(dst->lwtstate);
38
39         return NULL;
40 }
41
42 static inline bool skb_valid_dst(const struct sk_buff *skb)
43 {
44         struct dst_entry *dst = skb_dst(skb);
45
46         return dst && !(dst->flags & DST_METADATA);
47 }
48
49 static inline int skb_metadata_dst_cmp(const struct sk_buff *skb_a,
50                                        const struct sk_buff *skb_b)
51 {
52         const struct metadata_dst *a, *b;
53
54         if (!(skb_a->_skb_refdst | skb_b->_skb_refdst))
55                 return 0;
56
57         a = (const struct metadata_dst *) skb_dst(skb_a);
58         b = (const struct metadata_dst *) skb_dst(skb_b);
59
60         if (!a != !b || a->u.tun_info.options_len != b->u.tun_info.options_len)
61                 return 1;
62
63         return memcmp(&a->u.tun_info, &b->u.tun_info,
64                       sizeof(a->u.tun_info) + a->u.tun_info.options_len);
65 }
66
67 void metadata_dst_free(struct metadata_dst *);
68 struct metadata_dst *metadata_dst_alloc(u8 optslen, gfp_t flags);
69 struct metadata_dst __percpu *metadata_dst_alloc_percpu(u8 optslen, gfp_t flags);
70
71 static inline struct metadata_dst *tun_rx_dst(int md_size)
72 {
73         struct metadata_dst *tun_dst;
74
75         tun_dst = metadata_dst_alloc(md_size, GFP_ATOMIC);
76         if (!tun_dst)
77                 return NULL;
78
79         tun_dst->u.tun_info.options_len = 0;
80         tun_dst->u.tun_info.mode = 0;
81         return tun_dst;
82 }
83
84 static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
85 {
86         struct metadata_dst *md_dst = skb_metadata_dst(skb);
87         int md_size;
88         struct metadata_dst *new_md;
89
90         if (!md_dst)
91                 return ERR_PTR(-EINVAL);
92
93         md_size = md_dst->u.tun_info.options_len;
94         new_md = metadata_dst_alloc(md_size, GFP_ATOMIC);
95         if (!new_md)
96                 return ERR_PTR(-ENOMEM);
97
98         memcpy(&new_md->u.tun_info, &md_dst->u.tun_info,
99                sizeof(struct ip_tunnel_info) + md_size);
100 #ifdef CONFIG_DST_CACHE
101         /* Unclone the dst cache if there is one */
102         if (new_md->u.tun_info.dst_cache.cache) {
103                 int ret;
104
105                 ret = dst_cache_init(&new_md->u.tun_info.dst_cache, GFP_ATOMIC);
106                 if (ret) {
107                         metadata_dst_free(new_md);
108                         return ERR_PTR(ret);
109                 }
110         }
111 #endif
112
113         skb_dst_drop(skb);
114         skb_dst_set(skb, &new_md->dst);
115         return new_md;
116 }
117
118 static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb)
119 {
120         struct metadata_dst *dst;
121
122         dst = tun_dst_unclone(skb);
123         if (IS_ERR(dst))
124                 return NULL;
125
126         return &dst->u.tun_info;
127 }
128
129 static inline struct metadata_dst *__ip_tun_set_dst(__be32 saddr,
130                                                     __be32 daddr,
131                                                     __u8 tos, __u8 ttl,
132                                                     __be16 flags,
133                                                     __be64 tunnel_id,
134                                                     int md_size)
135 {
136         struct metadata_dst *tun_dst;
137
138         tun_dst = tun_rx_dst(md_size);
139         if (!tun_dst)
140                 return NULL;
141
142         ip_tunnel_key_init(&tun_dst->u.tun_info.key,
143                            saddr, daddr, tos, ttl,
144                            0, 0, 0, tunnel_id, flags);
145         return tun_dst;
146 }
147
148 static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
149                                                  __be16 flags,
150                                                  __be64 tunnel_id,
151                                                  int md_size)
152 {
153         const struct iphdr *iph = ip_hdr(skb);
154
155         return __ip_tun_set_dst(iph->saddr, iph->daddr, iph->tos, iph->ttl,
156                                 flags, tunnel_id, md_size);
157 }
158
159 static inline struct metadata_dst *__ipv6_tun_set_dst(const struct in6_addr *saddr,
160                                                       const struct in6_addr *daddr,
161                                                       __u8 tos, __u8 ttl,
162                                                       __be32 label,
163                                                       __be16 flags,
164                                                       __be64 tunnel_id,
165                                                       int md_size)
166 {
167         struct metadata_dst *tun_dst;
168         struct ip_tunnel_info *info;
169
170         tun_dst = tun_rx_dst(md_size);
171         if (!tun_dst)
172                 return NULL;
173
174         info = &tun_dst->u.tun_info;
175         info->mode = IP_TUNNEL_INFO_IPV6;
176         info->key.tun_flags = flags;
177         info->key.tun_id = tunnel_id;
178         info->key.tp_src = 0;
179         info->key.tp_dst = 0;
180
181         info->key.u.ipv6.src = *saddr;
182         info->key.u.ipv6.dst = *daddr;
183
184         info->key.tos = tos;
185         info->key.ttl = ttl;
186         info->key.label = label;
187
188         return tun_dst;
189 }
190
191 static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb,
192                                                    __be16 flags,
193                                                    __be64 tunnel_id,
194                                                    int md_size)
195 {
196         const struct ipv6hdr *ip6h = ipv6_hdr(skb);
197
198         return __ipv6_tun_set_dst(&ip6h->saddr, &ip6h->daddr,
199                                   ipv6_get_dsfield(ip6h), ip6h->hop_limit,
200                                   ip6_flowlabel(ip6h), flags, tunnel_id,
201                                   md_size);
202 }
203 #endif /* __NET_DST_METADATA_H */