1 #ifndef __NET_VXLAN_WRAPPER_H
2 #define __NET_VXLAN_WRAPPER_H 1
5 #include <net/udp_tunnel.h>
8 #ifdef USE_UPSTREAM_TUNNEL
9 #include_next <net/vxlan.h>
11 static inline int rpl_vxlan_init_module(void)
15 static inline void rpl_vxlan_cleanup_module(void)
18 #define vxlan_xmit dev_queue_xmit
20 #else /* USE_UPSTREAM_TUNNEL */
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>
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 * I = VXLAN Network Identifier (VNI) present.
47 /* VXLAN header flags. */
48 #define VXLAN_HF_VNI cpu_to_be32(BIT(27))
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))
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)
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67 * C = Remote checksum offload bit. When set indicates that the
68 * remote checksum offload data is present.
70 * O = Offset bit. Indicates the checksum offset relative to
73 * Csum start = Checksum start divided by two.
75 * http://tools.ietf.org/html/draft-herbert-vxlan-rco
78 /* VXLAN-RCO header flags. */
79 #define VXLAN_HF_RCO cpu_to_be32(BIT(21))
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)
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96 * G = Group Policy ID present.
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.
101 * A = Indicates that the group policy has already been applied to
102 * this packet. Policies MUST NOT be applied by devices when the
105 * https://tools.ietf.org/html/draft-smith-vxlan-group-policy
107 struct vxlanhdr_gbp {
109 #ifdef __LITTLE_ENDIAN_BITFIELD
110 u8 reserved_flags1:3,
115 #elif defined(__BIG_ENDIAN_BITFIELD)
116 u8 reserved_flags1:1,
122 #error "Please fix <asm/byteorder.h>"
128 /* VXLAN-GBP header flags. */
129 #define VXLAN_HF_GBP cpu_to_be32(BIT(31))
131 #define VXLAN_GBP_USED_BITS (VXLAN_HF_GBP | cpu_to_be32(0xFFFFFF))
135 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
136 * |R|R|R|R|R|R|R|R|R|D|R|R|A|R|R|R| Group Policy ID |
137 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
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)
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
151 * Ver = Version. Indicates VXLAN GPE protocol version.
153 * P = Next Protocol Bit. The P bit is set to indicate that the
154 * Next Protocol field is present.
156 * O = OAM Flag Bit. The O bit is set to indicate that the packet
159 * Next Protocol = This 8 bit field indicates the protocol header
160 * immediately following the VXLAN GPE header.
162 * https://tools.ietf.org/html/draft-ietf-nvo3-vxlan-gpe-01
165 struct vxlanhdr_gpe {
166 #if defined(__LITTLE_ENDIAN_BITFIELD)
173 #elif defined(__BIG_ENDIAN_BITFIELD)
174 u8 reserved_flags2:2,
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))
192 #define VXLAN_GPE_USED_BITS (VXLAN_HF_VER | VXLAN_HF_NP | VXLAN_HF_OAM | \
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
201 struct vxlan_metadata {
205 /* per UDP socket information */
207 struct hlist_node hlist;
209 struct hlist_head vni_list[VNI_HASH_SIZE];
212 #ifdef HAVE_UDP_OFFLOAD
213 struct udp_offload udp_offloads;
218 struct sockaddr_in sin;
219 struct sockaddr_in6 sin6;
224 union vxlan_addr remote_ip;
228 struct list_head list;
232 struct vxlan_config {
233 union vxlan_addr remote_ip;
234 union vxlan_addr saddr;
245 unsigned long age_interval;
246 unsigned int addrmax;
250 /* Pseudo network device */
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 */
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 */
263 struct timer_list age_timer;
264 spinlock_t hash_lock;
265 unsigned int addrcnt;
267 struct vxlan_config cfg;
269 struct hlist_head fdb_head[FDB_HASH_SIZE];
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
288 /* Flags that are used in the receive path. These flags must match in
289 * order for a socket to be shareable
291 #define VXLAN_F_RCV_FLAGS (VXLAN_F_GBP | \
293 VXLAN_F_UDP_ZERO_CSUM6_RX | \
294 VXLAN_F_REMCSUM_RX | \
295 VXLAN_F_REMCSUM_NOPARTIAL | \
296 VXLAN_F_COLLECT_METADATA)
298 /* Flags that can be set together with VXLAN_F_GPE. */
299 #define VXLAN_F_ALLOWED_GPE (VXLAN_F_GPE | \
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)
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);
310 static inline __be16 vxlan_dev_dst_port(struct vxlan_dev *vxlan,
311 unsigned short family)
313 #if IS_ENABLED(CONFIG_IPV6)
314 if (family == AF_INET6)
315 return inet_sk(vxlan->vn6_sock->sock->sk)->inet_sport;
317 return inet_sk(vxlan->vn4_sock->sock->sk)->inet_sport;
320 static inline netdev_features_t vxlan_features_check(struct sk_buff *skb,
321 netdev_features_t features)
325 if (!skb->encapsulation)
328 switch (vlan_get_protocol(skb)) {
329 case htons(ETH_P_IP):
330 l4_hdr = ip_hdr(skb)->protocol;
332 case htons(ETH_P_IPV6):
333 l4_hdr = ipv6_hdr(skb)->nexthdr;
339 if ((l4_hdr == IPPROTO_UDP) && (
340 #ifdef HAVE_INNER_PROTOCOL_TYPE
341 skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
343 #ifdef HAVE_INNER_PROTOCOL
344 skb->inner_protocol != htons(ETH_P_TEB) ||
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);
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)
360 static inline struct vxlanhdr *vxlan_hdr(struct sk_buff *skb)
362 return (struct vxlanhdr *)(udp_hdr(skb) + 1);
365 static inline __be32 vxlan_vni(__be32 vni_field)
367 #if defined(__BIG_ENDIAN)
368 return (__force __be32)((__force u32)vni_field >> 8);
370 return (__force __be32)((__force u32)(vni_field & VXLAN_VNI_MASK) << 8);
374 static inline __be32 vxlan_vni_field(__be32 vni)
376 #if defined(__BIG_ENDIAN)
377 return (__force __be32)((__force u32)vni << 8);
379 return (__force __be32)((__force u32)vni >> 8);
383 static inline __be32 vxlan_tun_id_to_vni(__be64 tun_id)
385 #if defined(__BIG_ENDIAN)
386 return (__force __be32)tun_id;
388 return (__force __be32)((__force u64)tun_id >> 32);
392 static inline __be64 vxlan_vni_to_tun_id(__be32 vni)
394 #if defined(__BIG_ENDIAN)
395 return (__force __be64)vni;
397 return (__force __be64)((u64)(__force u32)vni << 32);
401 static inline size_t vxlan_rco_start(__be32 vni_field)
403 return be32_to_cpu(vni_field & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
406 static inline size_t vxlan_rco_offset(__be32 vni_field)
408 return (vni_field & VXLAN_RCO_UDP) ?
409 offsetof(struct udphdr, check) :
410 offsetof(struct tcphdr, check);
413 static inline __be32 vxlan_compute_rco(unsigned int start, unsigned int offset)
415 __be32 vni_field = cpu_to_be32(start >> VXLAN_RCO_SHIFT);
417 if (offset == offsetof(struct udphdr, check))
418 vni_field |= VXLAN_RCO_UDP;
422 static inline void vxlan_get_rx_port(struct net_device *netdev)
425 call_netdevice_notifiers(NETDEV_OFFLOAD_PUSH_VXLAN, netdev);
428 static inline unsigned short vxlan_get_sk_family(struct vxlan_sock *vs)
430 return vs->sock->sk->sk_family;
433 int rpl_vxlan_init_module(void);
434 void rpl_vxlan_cleanup_module(void);
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);
439 #define vxlan_xmit rpl_vxlan_xmit
440 netdev_tx_t rpl_vxlan_xmit(struct sk_buff *skb);
442 #endif /* USE_UPSTREAM_TUNNEL */
444 #define vxlan_init_module rpl_vxlan_init_module
445 #define vxlan_cleanup_module rpl_vxlan_cleanup_module