2 #define __NET_VXLAN_H 1
5 #include <linux/ipv6.h>
6 #include <linux/if_vlan.h>
7 #include <linux/skbuff.h>
8 #include <linux/netdevice.h>
10 #include <net/dst_metadata.h>
12 #define VNI_HASH_BITS 10
13 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
16 * VXLAN Group Based Policy Extension:
17 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
18 * |1|-|-|-|1|-|-|-|R|D|R|R|A|R|R|R| Group Policy ID |
19 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
20 * | VXLAN Network Identifier (VNI) | Reserved |
21 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
23 * D = Don't Learn bit. When set, this bit indicates that the egress
24 * VTEP MUST NOT learn the source address of the encapsulated frame.
26 * A = Indicates that the group policy has already been applied to
27 * this packet. Policies MUST NOT be applied by devices when the
30 * [0] https://tools.ietf.org/html/draft-smith-vxlan-group-policy
34 #ifdef __LITTLE_ENDIAN_BITFIELD
35 __u8 reserved_flags1
:3,
40 #elif defined(__BIG_ENDIAN_BITFIELD)
41 __u8 reserved_flags1
:1,
47 #error "Please fix <asm/byteorder.h>"
53 #define VXLAN_GBP_USED_BITS (VXLAN_HF_GBP | 0xFFFFFF)
57 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 * |R|R|R|R|R|R|R|R|R|D|R|R|A|R|R|R| Group Policy ID |
59 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61 #define VXLAN_GBP_DONT_LEARN (BIT(6) << 16)
62 #define VXLAN_GBP_POLICY_APPLIED (BIT(3) << 16)
63 #define VXLAN_GBP_ID_MASK (0xFFFF)
65 /* VXLAN protocol header:
66 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67 * |G|R|R|R|I|R|R|C| Reserved |
68 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69 * | VXLAN Network Identifier (VNI) | Reserved |
70 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
72 * G = 1 Group Policy (VXLAN-GBP)
73 * I = 1 VXLAN Network Identifier (VNI) present
74 * C = 1 Remote checksum offload (RCO)
81 /* VXLAN header flags. */
82 #define VXLAN_HF_RCO BIT(24)
83 #define VXLAN_HF_VNI BIT(27)
84 #define VXLAN_HF_GBP BIT(31)
86 /* Remote checksum offload header option */
87 #define VXLAN_RCO_MASK 0x7f /* Last byte of vni field */
88 #define VXLAN_RCO_UDP 0x80 /* Indicate UDP RCO (TCP when not set *) */
89 #define VXLAN_RCO_SHIFT 1 /* Left shift of start */
90 #define VXLAN_RCO_SHIFT_MASK ((1 << VXLAN_RCO_SHIFT) - 1)
91 #define VXLAN_MAX_REMCSUM_START (VXLAN_RCO_MASK << VXLAN_RCO_SHIFT)
93 #define VXLAN_N_VID (1u << 24)
94 #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
95 #define VXLAN_VNI_MASK (VXLAN_VID_MASK << 8)
96 #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
98 #define VNI_HASH_BITS 10
99 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
100 #define FDB_HASH_BITS 8
101 #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
103 struct vxlan_metadata
{
107 /* per UDP socket information */
109 struct hlist_node hlist
;
110 struct work_struct del_work
;
113 struct hlist_head vni_list
[VNI_HASH_SIZE
];
115 struct udp_offload udp_offloads
;
120 struct sockaddr_in sin
;
121 struct sockaddr_in6 sin6
;
126 union vxlan_addr remote_ip
;
130 struct list_head list
;
134 struct vxlan_config
{
135 union vxlan_addr remote_ip
;
136 union vxlan_addr saddr
;
146 unsigned long age_interval
;
147 unsigned int addrmax
;
151 /* Pseudo network device */
153 struct hlist_node hlist
; /* vni hash table */
154 struct list_head next
; /* vxlan's per namespace list */
155 struct vxlan_sock
*vn_sock
; /* listening socket */
156 struct net_device
*dev
;
157 struct net
*net
; /* netns for packet i/o */
158 struct vxlan_rdst default_dst
; /* default destination */
159 u32 flags
; /* VXLAN_F_* in vxlan.h */
161 struct timer_list age_timer
;
162 spinlock_t hash_lock
;
163 unsigned int addrcnt
;
164 struct gro_cells gro_cells
;
166 struct vxlan_config cfg
;
168 struct hlist_head fdb_head
[FDB_HASH_SIZE
];
171 #define VXLAN_F_LEARN 0x01
172 #define VXLAN_F_PROXY 0x02
173 #define VXLAN_F_RSC 0x04
174 #define VXLAN_F_L2MISS 0x08
175 #define VXLAN_F_L3MISS 0x10
176 #define VXLAN_F_IPV6 0x20
177 #define VXLAN_F_UDP_CSUM 0x40
178 #define VXLAN_F_UDP_ZERO_CSUM6_TX 0x80
179 #define VXLAN_F_UDP_ZERO_CSUM6_RX 0x100
180 #define VXLAN_F_REMCSUM_TX 0x200
181 #define VXLAN_F_REMCSUM_RX 0x400
182 #define VXLAN_F_GBP 0x800
183 #define VXLAN_F_REMCSUM_NOPARTIAL 0x1000
184 #define VXLAN_F_COLLECT_METADATA 0x2000
186 /* Flags that are used in the receive path. These flags must match in
187 * order for a socket to be shareable
189 #define VXLAN_F_RCV_FLAGS (VXLAN_F_GBP | \
190 VXLAN_F_UDP_ZERO_CSUM6_RX | \
191 VXLAN_F_REMCSUM_RX | \
192 VXLAN_F_REMCSUM_NOPARTIAL | \
193 VXLAN_F_COLLECT_METADATA)
195 struct net_device
*vxlan_dev_create(struct net
*net
, const char *name
,
196 u8 name_assign_type
, struct vxlan_config
*conf
);
198 static inline __be16
vxlan_dev_dst_port(struct vxlan_dev
*vxlan
)
200 return inet_sk(vxlan
->vn_sock
->sock
->sk
)->inet_sport
;
203 static inline netdev_features_t
vxlan_features_check(struct sk_buff
*skb
,
204 netdev_features_t features
)
208 if (!skb
->encapsulation
)
211 switch (vlan_get_protocol(skb
)) {
212 case htons(ETH_P_IP
):
213 l4_hdr
= ip_hdr(skb
)->protocol
;
215 case htons(ETH_P_IPV6
):
216 l4_hdr
= ipv6_hdr(skb
)->nexthdr
;
222 if ((l4_hdr
== IPPROTO_UDP
) &&
223 (skb
->inner_protocol_type
!= ENCAP_TYPE_ETHER
||
224 skb
->inner_protocol
!= htons(ETH_P_TEB
) ||
225 (skb_inner_mac_header(skb
) - skb_transport_header(skb
) !=
226 sizeof(struct udphdr
) + sizeof(struct vxlanhdr
))))
227 return features
& ~(NETIF_F_ALL_CSUM
| NETIF_F_GSO_MASK
);
232 /* IP header + UDP + VXLAN + Ethernet header */
233 #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
234 /* IPv6 header + UDP + VXLAN + Ethernet header */
235 #define VXLAN6_HEADROOM (40 + 8 + 8 + 14)
237 #if IS_ENABLED(CONFIG_VXLAN)
238 void vxlan_get_rx_port(struct net_device
*netdev
);
240 static inline void vxlan_get_rx_port(struct net_device
*netdev
)
245 static inline unsigned short vxlan_get_sk_family(struct vxlan_sock
*vs
)
247 return vs
->sock
->sk
->sk_family
;