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