vxlan/geneve: Include udp_tunnel.h in vxlan/geneve.h and fixup includes
[cascardo/linux.git] / include / net / vxlan.h
1 #ifndef __NET_VXLAN_H
2 #define __NET_VXLAN_H 1
3
4 #include <linux/if_vlan.h>
5 #include <net/udp_tunnel.h>
6 #include <net/dst_metadata.h>
7
8 /* VXLAN protocol (RFC 7348) header:
9  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
10  * |R|R|R|R|I|R|R|R|               Reserved                        |
11  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
12  * |                VXLAN Network Identifier (VNI) |   Reserved    |
13  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
14  *
15  * I = VXLAN Network Identifier (VNI) present.
16  */
17 struct vxlanhdr {
18         __be32 vx_flags;
19         __be32 vx_vni;
20 };
21
22 /* VXLAN header flags. */
23 #define VXLAN_HF_VNI    cpu_to_be32(BIT(27))
24
25 #define VXLAN_N_VID     (1u << 24)
26 #define VXLAN_VID_MASK  (VXLAN_N_VID - 1)
27 #define VXLAN_VNI_MASK  cpu_to_be32(VXLAN_VID_MASK << 8)
28 #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
29
30 #define VNI_HASH_BITS   10
31 #define VNI_HASH_SIZE   (1<<VNI_HASH_BITS)
32 #define FDB_HASH_BITS   8
33 #define FDB_HASH_SIZE   (1<<FDB_HASH_BITS)
34
35 /* Remote checksum offload for VXLAN (VXLAN_F_REMCSUM_[RT]X):
36  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37  * |R|R|R|R|I|R|R|R|R|R|C|              Reserved                   |
38  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39  * |           VXLAN Network Identifier (VNI)      |O| Csum start  |
40  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41  *
42  * C = Remote checksum offload bit. When set indicates that the
43  *     remote checksum offload data is present.
44  *
45  * O = Offset bit. Indicates the checksum offset relative to
46  *     checksum start.
47  *
48  * Csum start = Checksum start divided by two.
49  *
50  * http://tools.ietf.org/html/draft-herbert-vxlan-rco
51  */
52
53 /* VXLAN-RCO header flags. */
54 #define VXLAN_HF_RCO    cpu_to_be32(BIT(21))
55
56 /* Remote checksum offload header option */
57 #define VXLAN_RCO_MASK  cpu_to_be32(0x7f)  /* Last byte of vni field */
58 #define VXLAN_RCO_UDP   cpu_to_be32(0x80)  /* Indicate UDP RCO (TCP when not set *) */
59 #define VXLAN_RCO_SHIFT 1                  /* Left shift of start */
60 #define VXLAN_RCO_SHIFT_MASK ((1 << VXLAN_RCO_SHIFT) - 1)
61 #define VXLAN_MAX_REMCSUM_START (0x7f << VXLAN_RCO_SHIFT)
62
63 /*
64  * VXLAN Group Based Policy Extension (VXLAN_F_GBP):
65  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66  * |G|R|R|R|I|R|R|R|R|D|R|R|A|R|R|R|        Group Policy ID        |
67  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68  * |                VXLAN Network Identifier (VNI) |   Reserved    |
69  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
70  *
71  * G = Group Policy ID present.
72  *
73  * D = Don't Learn bit. When set, this bit indicates that the egress
74  *     VTEP MUST NOT learn the source address of the encapsulated frame.
75  *
76  * A = Indicates that the group policy has already been applied to
77  *     this packet. Policies MUST NOT be applied by devices when the
78  *     A bit is set.
79  *
80  * https://tools.ietf.org/html/draft-smith-vxlan-group-policy
81  */
82 struct vxlanhdr_gbp {
83         u8      vx_flags;
84 #ifdef __LITTLE_ENDIAN_BITFIELD
85         u8      reserved_flags1:3,
86                 policy_applied:1,
87                 reserved_flags2:2,
88                 dont_learn:1,
89                 reserved_flags3:1;
90 #elif defined(__BIG_ENDIAN_BITFIELD)
91         u8      reserved_flags1:1,
92                 dont_learn:1,
93                 reserved_flags2:2,
94                 policy_applied:1,
95                 reserved_flags3:3;
96 #else
97 #error  "Please fix <asm/byteorder.h>"
98 #endif
99         __be16  policy_id;
100         __be32  vx_vni;
101 };
102
103 /* VXLAN-GBP header flags. */
104 #define VXLAN_HF_GBP    cpu_to_be32(BIT(31))
105
106 #define VXLAN_GBP_USED_BITS (VXLAN_HF_GBP | cpu_to_be32(0xFFFFFF))
107
108 /* skb->mark mapping
109  *
110  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111  * |R|R|R|R|R|R|R|R|R|D|R|R|A|R|R|R|        Group Policy ID        |
112  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
113  */
114 #define VXLAN_GBP_DONT_LEARN            (BIT(6) << 16)
115 #define VXLAN_GBP_POLICY_APPLIED        (BIT(3) << 16)
116 #define VXLAN_GBP_ID_MASK               (0xFFFF)
117
118 /*
119  * VXLAN Generic Protocol Extension (VXLAN_F_GPE):
120  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
121  * |R|R|Ver|I|P|R|O|       Reserved                |Next Protocol  |
122  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
123  * |                VXLAN Network Identifier (VNI) |   Reserved    |
124  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
125  *
126  * Ver = Version. Indicates VXLAN GPE protocol version.
127  *
128  * P = Next Protocol Bit. The P bit is set to indicate that the
129  *     Next Protocol field is present.
130  *
131  * O = OAM Flag Bit. The O bit is set to indicate that the packet
132  *     is an OAM packet.
133  *
134  * Next Protocol = This 8 bit field indicates the protocol header
135  * immediately following the VXLAN GPE header.
136  *
137  * https://tools.ietf.org/html/draft-ietf-nvo3-vxlan-gpe-01
138  */
139
140 struct vxlanhdr_gpe {
141 #if defined(__LITTLE_ENDIAN_BITFIELD)
142         u8      oam_flag:1,
143                 reserved_flags1:1,
144                 np_applied:1,
145                 instance_applied:1,
146                 version:2,
147 reserved_flags2:2;
148 #elif defined(__BIG_ENDIAN_BITFIELD)
149         u8      reserved_flags2:2,
150                 version:2,
151                 instance_applied:1,
152                 np_applied:1,
153                 reserved_flags1:1,
154                 oam_flag:1;
155 #endif
156         u8      reserved_flags3;
157         u8      reserved_flags4;
158         u8      next_protocol;
159         __be32  vx_vni;
160 };
161
162 /* VXLAN-GPE header flags. */
163 #define VXLAN_HF_VER    cpu_to_be32(BIT(29) | BIT(28))
164 #define VXLAN_HF_NP     cpu_to_be32(BIT(26))
165 #define VXLAN_HF_OAM    cpu_to_be32(BIT(24))
166
167 #define VXLAN_GPE_USED_BITS (VXLAN_HF_VER | VXLAN_HF_NP | VXLAN_HF_OAM | \
168                              cpu_to_be32(0xff))
169
170 /* VXLAN-GPE header Next Protocol. */
171 #define VXLAN_GPE_NP_IPV4      0x01
172 #define VXLAN_GPE_NP_IPV6      0x02
173 #define VXLAN_GPE_NP_ETHERNET  0x03
174 #define VXLAN_GPE_NP_NSH       0x04
175
176 struct vxlan_metadata {
177         u32             gbp;
178 };
179
180 /* per UDP socket information */
181 struct vxlan_sock {
182         struct hlist_node hlist;
183         struct socket    *sock;
184         struct hlist_head vni_list[VNI_HASH_SIZE];
185         atomic_t          refcnt;
186         u32               flags;
187 };
188
189 union vxlan_addr {
190         struct sockaddr_in sin;
191         struct sockaddr_in6 sin6;
192         struct sockaddr sa;
193 };
194
195 struct vxlan_rdst {
196         union vxlan_addr         remote_ip;
197         __be16                   remote_port;
198         __be32                   remote_vni;
199         u32                      remote_ifindex;
200         struct list_head         list;
201         struct rcu_head          rcu;
202         struct dst_cache         dst_cache;
203 };
204
205 struct vxlan_config {
206         union vxlan_addr        remote_ip;
207         union vxlan_addr        saddr;
208         __be32                  vni;
209         int                     remote_ifindex;
210         int                     mtu;
211         __be16                  dst_port;
212         u16                     port_min;
213         u16                     port_max;
214         u8                      tos;
215         u8                      ttl;
216         __be32                  label;
217         u32                     flags;
218         unsigned long           age_interval;
219         unsigned int            addrmax;
220         bool                    no_share;
221 };
222
223 /* Pseudo network device */
224 struct vxlan_dev {
225         struct hlist_node hlist;        /* vni hash table */
226         struct list_head  next;         /* vxlan's per namespace list */
227         struct vxlan_sock *vn4_sock;    /* listening socket for IPv4 */
228 #if IS_ENABLED(CONFIG_IPV6)
229         struct vxlan_sock *vn6_sock;    /* listening socket for IPv6 */
230 #endif
231         struct net_device *dev;
232         struct net        *net;         /* netns for packet i/o */
233         struct vxlan_rdst default_dst;  /* default destination */
234         u32               flags;        /* VXLAN_F_* in vxlan.h */
235
236         struct timer_list age_timer;
237         spinlock_t        hash_lock;
238         unsigned int      addrcnt;
239         struct gro_cells  gro_cells;
240
241         struct vxlan_config     cfg;
242
243         struct hlist_head fdb_head[FDB_HASH_SIZE];
244 };
245
246 #define VXLAN_F_LEARN                   0x01
247 #define VXLAN_F_PROXY                   0x02
248 #define VXLAN_F_RSC                     0x04
249 #define VXLAN_F_L2MISS                  0x08
250 #define VXLAN_F_L3MISS                  0x10
251 #define VXLAN_F_IPV6                    0x20
252 #define VXLAN_F_UDP_ZERO_CSUM_TX        0x40
253 #define VXLAN_F_UDP_ZERO_CSUM6_TX       0x80
254 #define VXLAN_F_UDP_ZERO_CSUM6_RX       0x100
255 #define VXLAN_F_REMCSUM_TX              0x200
256 #define VXLAN_F_REMCSUM_RX              0x400
257 #define VXLAN_F_GBP                     0x800
258 #define VXLAN_F_REMCSUM_NOPARTIAL       0x1000
259 #define VXLAN_F_COLLECT_METADATA        0x2000
260 #define VXLAN_F_GPE                     0x4000
261
262 /* Flags that are used in the receive path. These flags must match in
263  * order for a socket to be shareable
264  */
265 #define VXLAN_F_RCV_FLAGS               (VXLAN_F_GBP |                  \
266                                          VXLAN_F_GPE |                  \
267                                          VXLAN_F_UDP_ZERO_CSUM6_RX |    \
268                                          VXLAN_F_REMCSUM_RX |           \
269                                          VXLAN_F_REMCSUM_NOPARTIAL |    \
270                                          VXLAN_F_COLLECT_METADATA)
271
272 /* Flags that can be set together with VXLAN_F_GPE. */
273 #define VXLAN_F_ALLOWED_GPE             (VXLAN_F_GPE |                  \
274                                          VXLAN_F_IPV6 |                 \
275                                          VXLAN_F_UDP_ZERO_CSUM_TX |     \
276                                          VXLAN_F_UDP_ZERO_CSUM6_TX |    \
277                                          VXLAN_F_UDP_ZERO_CSUM6_RX |    \
278                                          VXLAN_F_COLLECT_METADATA)
279
280 struct net_device *vxlan_dev_create(struct net *net, const char *name,
281                                     u8 name_assign_type, struct vxlan_config *conf);
282
283 static inline __be16 vxlan_dev_dst_port(struct vxlan_dev *vxlan,
284                                         unsigned short family)
285 {
286 #if IS_ENABLED(CONFIG_IPV6)
287         if (family == AF_INET6)
288                 return inet_sk(vxlan->vn6_sock->sock->sk)->inet_sport;
289 #endif
290         return inet_sk(vxlan->vn4_sock->sock->sk)->inet_sport;
291 }
292
293 static inline netdev_features_t vxlan_features_check(struct sk_buff *skb,
294                                                      netdev_features_t features)
295 {
296         u8 l4_hdr = 0;
297
298         if (!skb->encapsulation)
299                 return features;
300
301         switch (vlan_get_protocol(skb)) {
302         case htons(ETH_P_IP):
303                 l4_hdr = ip_hdr(skb)->protocol;
304                 break;
305         case htons(ETH_P_IPV6):
306                 l4_hdr = ipv6_hdr(skb)->nexthdr;
307                 break;
308         default:
309                 return features;;
310         }
311
312         if ((l4_hdr == IPPROTO_UDP) &&
313             (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
314              skb->inner_protocol != htons(ETH_P_TEB) ||
315              (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
316               sizeof(struct udphdr) + sizeof(struct vxlanhdr)) ||
317              (skb->ip_summed != CHECKSUM_NONE &&
318               !can_checksum_protocol(features, inner_eth_hdr(skb)->h_proto))))
319                 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
320
321         return features;
322 }
323
324 /* IP header + UDP + VXLAN + Ethernet header */
325 #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
326 /* IPv6 header + UDP + VXLAN + Ethernet header */
327 #define VXLAN6_HEADROOM (40 + 8 + 8 + 14)
328
329 static inline struct vxlanhdr *vxlan_hdr(struct sk_buff *skb)
330 {
331         return (struct vxlanhdr *)(udp_hdr(skb) + 1);
332 }
333
334 static inline __be32 vxlan_vni(__be32 vni_field)
335 {
336 #if defined(__BIG_ENDIAN)
337         return (__force __be32)((__force u32)vni_field >> 8);
338 #else
339         return (__force __be32)((__force u32)(vni_field & VXLAN_VNI_MASK) << 8);
340 #endif
341 }
342
343 static inline __be32 vxlan_vni_field(__be32 vni)
344 {
345 #if defined(__BIG_ENDIAN)
346         return (__force __be32)((__force u32)vni << 8);
347 #else
348         return (__force __be32)((__force u32)vni >> 8);
349 #endif
350 }
351
352 static inline __be32 vxlan_tun_id_to_vni(__be64 tun_id)
353 {
354 #if defined(__BIG_ENDIAN)
355         return (__force __be32)tun_id;
356 #else
357         return (__force __be32)((__force u64)tun_id >> 32);
358 #endif
359 }
360
361 static inline __be64 vxlan_vni_to_tun_id(__be32 vni)
362 {
363 #if defined(__BIG_ENDIAN)
364         return (__force __be64)vni;
365 #else
366         return (__force __be64)((u64)(__force u32)vni << 32);
367 #endif
368 }
369
370 static inline size_t vxlan_rco_start(__be32 vni_field)
371 {
372         return be32_to_cpu(vni_field & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
373 }
374
375 static inline size_t vxlan_rco_offset(__be32 vni_field)
376 {
377         return (vni_field & VXLAN_RCO_UDP) ?
378                 offsetof(struct udphdr, check) :
379                 offsetof(struct tcphdr, check);
380 }
381
382 static inline __be32 vxlan_compute_rco(unsigned int start, unsigned int offset)
383 {
384         __be32 vni_field = cpu_to_be32(start >> VXLAN_RCO_SHIFT);
385
386         if (offset == offsetof(struct udphdr, check))
387                 vni_field |= VXLAN_RCO_UDP;
388         return vni_field;
389 }
390
391 static inline void vxlan_get_rx_port(struct net_device *netdev)
392 {
393         ASSERT_RTNL();
394         call_netdevice_notifiers(NETDEV_OFFLOAD_PUSH_VXLAN, netdev);
395 }
396
397 static inline unsigned short vxlan_get_sk_family(struct vxlan_sock *vs)
398 {
399         return vs->sock->sk->sk_family;
400 }
401
402 #endif