2 #define __NET_VXLAN_H 1
4 #include <linux/if_vlan.h>
5 #include <net/udp_tunnel.h>
6 #include <net/dst_metadata.h>
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
15 * I = VXLAN Network Identifier (VNI) present.
22 /* VXLAN header flags. */
23 #define VXLAN_HF_VNI cpu_to_be32(BIT(27))
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))
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)
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 * C = Remote checksum offload bit. When set indicates that the
43 * remote checksum offload data is present.
45 * O = Offset bit. Indicates the checksum offset relative to
48 * Csum start = Checksum start divided by two.
50 * http://tools.ietf.org/html/draft-herbert-vxlan-rco
53 /* VXLAN-RCO header flags. */
54 #define VXLAN_HF_RCO cpu_to_be32(BIT(21))
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)
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71 * G = Group Policy ID present.
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.
76 * A = Indicates that the group policy has already been applied to
77 * this packet. Policies MUST NOT be applied by devices when the
80 * https://tools.ietf.org/html/draft-smith-vxlan-group-policy
84 #ifdef __LITTLE_ENDIAN_BITFIELD
90 #elif defined(__BIG_ENDIAN_BITFIELD)
97 #error "Please fix <asm/byteorder.h>"
103 /* VXLAN-GBP header flags. */
104 #define VXLAN_HF_GBP cpu_to_be32(BIT(31))
106 #define VXLAN_GBP_USED_BITS (VXLAN_HF_GBP | cpu_to_be32(0xFFFFFF))
110 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111 * |R|R|R|R|R|R|R|R|R|D|R|R|A|R|R|R| Group Policy ID |
112 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
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)
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
126 * Ver = Version. Indicates VXLAN GPE protocol version.
128 * P = Next Protocol Bit. The P bit is set to indicate that the
129 * Next Protocol field is present.
131 * O = OAM Flag Bit. The O bit is set to indicate that the packet
134 * Next Protocol = This 8 bit field indicates the protocol header
135 * immediately following the VXLAN GPE header.
137 * https://tools.ietf.org/html/draft-ietf-nvo3-vxlan-gpe-01
140 struct vxlanhdr_gpe {
141 #if defined(__LITTLE_ENDIAN_BITFIELD)
148 #elif defined(__BIG_ENDIAN_BITFIELD)
149 u8 reserved_flags2:2,
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))
167 #define VXLAN_GPE_USED_BITS (VXLAN_HF_VER | VXLAN_HF_NP | VXLAN_HF_OAM | \
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
176 struct vxlan_metadata {
180 /* per UDP socket information */
182 struct hlist_node hlist;
184 struct hlist_head vni_list[VNI_HASH_SIZE];
190 struct sockaddr_in sin;
191 struct sockaddr_in6 sin6;
196 union vxlan_addr remote_ip;
200 struct list_head list;
202 struct dst_cache dst_cache;
205 struct vxlan_config {
206 union vxlan_addr remote_ip;
207 union vxlan_addr saddr;
218 unsigned long age_interval;
219 unsigned int addrmax;
223 /* Pseudo network device */
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 */
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 */
236 struct timer_list age_timer;
237 spinlock_t hash_lock;
238 unsigned int addrcnt;
239 struct gro_cells gro_cells;
241 struct vxlan_config cfg;
243 struct hlist_head fdb_head[FDB_HASH_SIZE];
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
262 /* Flags that are used in the receive path. These flags must match in
263 * order for a socket to be shareable
265 #define VXLAN_F_RCV_FLAGS (VXLAN_F_GBP | \
267 VXLAN_F_UDP_ZERO_CSUM6_RX | \
268 VXLAN_F_REMCSUM_RX | \
269 VXLAN_F_REMCSUM_NOPARTIAL | \
270 VXLAN_F_COLLECT_METADATA)
272 /* Flags that can be set together with VXLAN_F_GPE. */
273 #define VXLAN_F_ALLOWED_GPE (VXLAN_F_GPE | \
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)
280 struct net_device *vxlan_dev_create(struct net *net, const char *name,
281 u8 name_assign_type, struct vxlan_config *conf);
283 static inline __be16 vxlan_dev_dst_port(struct vxlan_dev *vxlan,
284 unsigned short family)
286 #if IS_ENABLED(CONFIG_IPV6)
287 if (family == AF_INET6)
288 return inet_sk(vxlan->vn6_sock->sock->sk)->inet_sport;
290 return inet_sk(vxlan->vn4_sock->sock->sk)->inet_sport;
293 static inline netdev_features_t vxlan_features_check(struct sk_buff *skb,
294 netdev_features_t features)
298 if (!skb->encapsulation)
301 switch (vlan_get_protocol(skb)) {
302 case htons(ETH_P_IP):
303 l4_hdr = ip_hdr(skb)->protocol;
305 case htons(ETH_P_IPV6):
306 l4_hdr = ipv6_hdr(skb)->nexthdr;
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);
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)
329 static inline struct vxlanhdr *vxlan_hdr(struct sk_buff *skb)
331 return (struct vxlanhdr *)(udp_hdr(skb) + 1);
334 static inline __be32 vxlan_vni(__be32 vni_field)
336 #if defined(__BIG_ENDIAN)
337 return (__force __be32)((__force u32)vni_field >> 8);
339 return (__force __be32)((__force u32)(vni_field & VXLAN_VNI_MASK) << 8);
343 static inline __be32 vxlan_vni_field(__be32 vni)
345 #if defined(__BIG_ENDIAN)
346 return (__force __be32)((__force u32)vni << 8);
348 return (__force __be32)((__force u32)vni >> 8);
352 static inline __be32 vxlan_tun_id_to_vni(__be64 tun_id)
354 #if defined(__BIG_ENDIAN)
355 return (__force __be32)tun_id;
357 return (__force __be32)((__force u64)tun_id >> 32);
361 static inline __be64 vxlan_vni_to_tun_id(__be32 vni)
363 #if defined(__BIG_ENDIAN)
364 return (__force __be64)vni;
366 return (__force __be64)((u64)(__force u32)vni << 32);
370 static inline size_t vxlan_rco_start(__be32 vni_field)
372 return be32_to_cpu(vni_field & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
375 static inline size_t vxlan_rco_offset(__be32 vni_field)
377 return (vni_field & VXLAN_RCO_UDP) ?
378 offsetof(struct udphdr, check) :
379 offsetof(struct tcphdr, check);
382 static inline __be32 vxlan_compute_rco(unsigned int start, unsigned int offset)
384 __be32 vni_field = cpu_to_be32(start >> VXLAN_RCO_SHIFT);
386 if (offset == offsetof(struct udphdr, check))
387 vni_field |= VXLAN_RCO_UDP;
391 static inline void vxlan_get_rx_port(struct net_device *netdev)
394 call_netdevice_notifiers(NETDEV_OFFLOAD_PUSH_VXLAN, netdev);
397 static inline unsigned short vxlan_get_sk_family(struct vxlan_sock *vs)
399 return vs->sock->sk->sk_family;