net: Combine GENEVE and VXLAN port notifiers into single functions
[linux-block.git] / include / net / vxlan.h
CommitLineData
012a5729
PS
1#ifndef __NET_VXLAN_H
2#define __NET_VXLAN_H 1
3
5f35227e 4#include <linux/if_vlan.h>
86a98057 5#include <net/udp_tunnel.h>
ee122c79 6#include <net/dst_metadata.h>
012a5729 7
828788ac
JB
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 */
17struct vxlanhdr {
18 __be32 vx_flags;
19 __be32 vx_vni;
20};
21
22/* VXLAN header flags. */
54bfd872 23#define VXLAN_HF_VNI cpu_to_be32(BIT(27))
828788ac
JB
24
25#define VXLAN_N_VID (1u << 24)
26#define VXLAN_VID_MASK (VXLAN_N_VID - 1)
54bfd872 27#define VXLAN_VNI_MASK cpu_to_be32(VXLAN_VID_MASK << 8)
828788ac
JB
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. */
54bfd872 54#define VXLAN_HF_RCO cpu_to_be32(BIT(21))
828788ac
JB
55
56/* Remote checksum offload header option */
54bfd872
JB
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 */
828788ac 60#define VXLAN_RCO_SHIFT_MASK ((1 << VXLAN_RCO_SHIFT) - 1)
54bfd872 61#define VXLAN_MAX_REMCSUM_START (0x7f << VXLAN_RCO_SHIFT)
828788ac 62
3511494c 63/*
828788ac 64 * VXLAN Group Based Policy Extension (VXLAN_F_GBP):
3511494c 65 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
828788ac 66 * |G|R|R|R|I|R|R|R|R|D|R|R|A|R|R|R| Group Policy ID |
3511494c
TG
67 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68 * | VXLAN Network Identifier (VNI) | Reserved |
69 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
70 *
828788ac
JB
71 * G = Group Policy ID present.
72 *
3511494c
TG
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 *
828788ac 80 * https://tools.ietf.org/html/draft-smith-vxlan-group-policy
3511494c
TG
81 */
82struct vxlanhdr_gbp {
0e715d6f 83 u8 vx_flags;
3511494c 84#ifdef __LITTLE_ENDIAN_BITFIELD
0e715d6f 85 u8 reserved_flags1:3,
3511494c
TG
86 policy_applied:1,
87 reserved_flags2:2,
88 dont_learn:1,
89 reserved_flags3:1;
90#elif defined(__BIG_ENDIAN_BITFIELD)
0e715d6f 91 u8 reserved_flags1:1,
3511494c
TG
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
828788ac 103/* VXLAN-GBP header flags. */
54bfd872 104#define VXLAN_HF_GBP cpu_to_be32(BIT(31))
828788ac 105
54bfd872 106#define VXLAN_GBP_USED_BITS (VXLAN_HF_GBP | cpu_to_be32(0xFFFFFF))
3511494c
TG
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
e1e5314d
JB
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
140struct 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,
147reserved_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
3511494c 176struct vxlan_metadata {
3511494c
TG
177 u32 gbp;
178};
179
012a5729
PS
180/* per UDP socket information */
181struct vxlan_sock {
182 struct hlist_node hlist;
012a5729 183 struct socket *sock;
012a5729
PS
184 struct hlist_head vni_list[VNI_HASH_SIZE];
185 atomic_t refcnt;
dfd8645e 186 u32 flags;
012a5729
PS
187};
188
0dfbdf41
TG
189union vxlan_addr {
190 struct sockaddr_in sin;
191 struct sockaddr_in6 sin6;
192 struct sockaddr sa;
193};
194
195struct vxlan_rdst {
196 union vxlan_addr remote_ip;
197 __be16 remote_port;
54bfd872 198 __be32 remote_vni;
0dfbdf41
TG
199 u32 remote_ifindex;
200 struct list_head list;
201 struct rcu_head rcu;
0c1d70af 202 struct dst_cache dst_cache;
0dfbdf41
TG
203};
204
205struct vxlan_config {
206 union vxlan_addr remote_ip;
207 union vxlan_addr saddr;
54bfd872 208 __be32 vni;
0dfbdf41
TG
209 int remote_ifindex;
210 int mtu;
211 __be16 dst_port;
0e715d6f
JB
212 u16 port_min;
213 u16 port_max;
214 u8 tos;
215 u8 ttl;
e7f70af1 216 __be32 label;
0dfbdf41
TG
217 u32 flags;
218 unsigned long age_interval;
219 unsigned int addrmax;
220 bool no_share;
221};
222
223/* Pseudo network device */
224struct vxlan_dev {
225 struct hlist_node hlist; /* vni hash table */
226 struct list_head next; /* vxlan's per namespace list */
b1be00a6
JB
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
0dfbdf41
TG
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;
58ce31cc 239 struct gro_cells gro_cells;
0dfbdf41
TG
240
241 struct vxlan_config cfg;
242
243 struct hlist_head fdb_head[FDB_HASH_SIZE];
244};
245
359a0ea9
TH
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
6ceb31ca 252#define VXLAN_F_UDP_ZERO_CSUM_TX 0x40
359a0ea9
TH
253#define VXLAN_F_UDP_ZERO_CSUM6_TX 0x80
254#define VXLAN_F_UDP_ZERO_CSUM6_RX 0x100
dfd8645e
TH
255#define VXLAN_F_REMCSUM_TX 0x200
256#define VXLAN_F_REMCSUM_RX 0x400
3511494c 257#define VXLAN_F_GBP 0x800
0ace2ca8 258#define VXLAN_F_REMCSUM_NOPARTIAL 0x1000
ee122c79 259#define VXLAN_F_COLLECT_METADATA 0x2000
e1e5314d 260#define VXLAN_F_GPE 0x4000
359a0ea9 261
d299ce14 262/* Flags that are used in the receive path. These flags must match in
af33c1ad
TH
263 * order for a socket to be shareable
264 */
265#define VXLAN_F_RCV_FLAGS (VXLAN_F_GBP | \
e1e5314d 266 VXLAN_F_GPE | \
af33c1ad 267 VXLAN_F_UDP_ZERO_CSUM6_RX | \
0ace2ca8 268 VXLAN_F_REMCSUM_RX | \
ee122c79 269 VXLAN_F_REMCSUM_NOPARTIAL | \
da8b43c0 270 VXLAN_F_COLLECT_METADATA)
ac5132d1 271
e1e5314d
JB
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
0dfbdf41
TG
280struct net_device *vxlan_dev_create(struct net *net, const char *name,
281 u8 name_assign_type, struct vxlan_config *conf);
282
b1be00a6
JB
283static inline __be16 vxlan_dev_dst_port(struct vxlan_dev *vxlan,
284 unsigned short family)
614732ea 285{
b1be00a6
JB
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;
614732ea 291}
49560532 292
5f35227e
JG
293static inline netdev_features_t vxlan_features_check(struct sk_buff *skb,
294 netdev_features_t features)
11bf7828 295{
5f35227e
JG
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) &&
11bf7828
JS
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) !=
af67eb9e
AD
316 sizeof(struct udphdr) + sizeof(struct vxlanhdr)) ||
317 (skb->ip_summed != CHECKSUM_NONE &&
318 !can_checksum_protocol(features, inner_eth_hdr(skb)->h_proto))))
a188222b 319 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
11bf7828 320
5f35227e 321 return features;
11bf7828 322}
23e62de3 323
e6cd988c
JG
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
d4ac05ff
JB
329static inline struct vxlanhdr *vxlan_hdr(struct sk_buff *skb)
330{
331 return (struct vxlanhdr *)(udp_hdr(skb) + 1);
332}
333
54bfd872
JB
334static inline __be32 vxlan_vni(__be32 vni_field)
335{
336#if defined(__BIG_ENDIAN)
5692d7ea 337 return (__force __be32)((__force u32)vni_field >> 8);
54bfd872 338#else
5692d7ea 339 return (__force __be32)((__force u32)(vni_field & VXLAN_VNI_MASK) << 8);
54bfd872
JB
340#endif
341}
342
343static inline __be32 vxlan_vni_field(__be32 vni)
344{
345#if defined(__BIG_ENDIAN)
5692d7ea 346 return (__force __be32)((__force u32)vni << 8);
54bfd872 347#else
5692d7ea 348 return (__force __be32)((__force u32)vni >> 8);
54bfd872
JB
349#endif
350}
351
352static inline __be32 vxlan_tun_id_to_vni(__be64 tun_id)
353{
354#if defined(__BIG_ENDIAN)
5692d7ea 355 return (__force __be32)tun_id;
54bfd872 356#else
5692d7ea 357 return (__force __be32)((__force u64)tun_id >> 32);
54bfd872
JB
358#endif
359}
360
07dabf20
JB
361static inline __be64 vxlan_vni_to_tun_id(__be32 vni)
362{
363#if defined(__BIG_ENDIAN)
5692d7ea 364 return (__force __be64)vni;
07dabf20 365#else
5692d7ea 366 return (__force __be64)((u64)(__force u32)vni << 32);
07dabf20
JB
367#endif
368}
369
54bfd872
JB
370static 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
375static 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
382static 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
e6cd988c
JG
391static inline void vxlan_get_rx_port(struct net_device *netdev)
392{
b7aade15
HFS
393 ASSERT_RTNL();
394 call_netdevice_notifiers(NETDEV_OFFLOAD_PUSH_VXLAN, netdev);
e6cd988c 395}
705cc62f
JB
396
397static inline unsigned short vxlan_get_sk_family(struct vxlan_sock *vs)
398{
399 return vs->sock->sk->sk_family;
400}
48e92c44
JB
401
402#endif