xref: /openbmc/linux/include/net/vxlan.h (revision 31e67366)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NET_VXLAN_H
3 #define __NET_VXLAN_H 1
4 
5 #include <linux/if_vlan.h>
6 #include <net/udp_tunnel.h>
7 #include <net/dst_metadata.h>
8 #include <net/rtnetlink.h>
9 #include <net/switchdev.h>
10 #include <net/nexthop.h>
11 
12 #define IANA_VXLAN_UDP_PORT     4789
13 
14 /* VXLAN protocol (RFC 7348) header:
15  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16  * |R|R|R|R|I|R|R|R|               Reserved                        |
17  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
18  * |                VXLAN Network Identifier (VNI) |   Reserved    |
19  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
20  *
21  * I = VXLAN Network Identifier (VNI) present.
22  */
23 struct vxlanhdr {
24 	__be32 vx_flags;
25 	__be32 vx_vni;
26 };
27 
28 /* VXLAN header flags. */
29 #define VXLAN_HF_VNI	cpu_to_be32(BIT(27))
30 
31 #define VXLAN_N_VID     (1u << 24)
32 #define VXLAN_VID_MASK  (VXLAN_N_VID - 1)
33 #define VXLAN_VNI_MASK	cpu_to_be32(VXLAN_VID_MASK << 8)
34 #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
35 
36 #define VNI_HASH_BITS	10
37 #define VNI_HASH_SIZE	(1<<VNI_HASH_BITS)
38 #define FDB_HASH_BITS	8
39 #define FDB_HASH_SIZE	(1<<FDB_HASH_BITS)
40 
41 /* Remote checksum offload for VXLAN (VXLAN_F_REMCSUM_[RT]X):
42  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43  * |R|R|R|R|I|R|R|R|R|R|C|              Reserved                   |
44  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45  * |           VXLAN Network Identifier (VNI)      |O| Csum start  |
46  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47  *
48  * C = Remote checksum offload bit. When set indicates that the
49  *     remote checksum offload data is present.
50  *
51  * O = Offset bit. Indicates the checksum offset relative to
52  *     checksum start.
53  *
54  * Csum start = Checksum start divided by two.
55  *
56  * http://tools.ietf.org/html/draft-herbert-vxlan-rco
57  */
58 
59 /* VXLAN-RCO header flags. */
60 #define VXLAN_HF_RCO	cpu_to_be32(BIT(21))
61 
62 /* Remote checksum offload header option */
63 #define VXLAN_RCO_MASK	cpu_to_be32(0x7f)  /* Last byte of vni field */
64 #define VXLAN_RCO_UDP	cpu_to_be32(0x80)  /* Indicate UDP RCO (TCP when not set *) */
65 #define VXLAN_RCO_SHIFT	1		   /* Left shift of start */
66 #define VXLAN_RCO_SHIFT_MASK ((1 << VXLAN_RCO_SHIFT) - 1)
67 #define VXLAN_MAX_REMCSUM_START (0x7f << VXLAN_RCO_SHIFT)
68 
69 /*
70  * VXLAN Group Based Policy Extension (VXLAN_F_GBP):
71  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
72  * |G|R|R|R|I|R|R|R|R|D|R|R|A|R|R|R|        Group Policy ID        |
73  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
74  * |                VXLAN Network Identifier (VNI) |   Reserved    |
75  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
76  *
77  * G = Group Policy ID present.
78  *
79  * D = Don't Learn bit. When set, this bit indicates that the egress
80  *     VTEP MUST NOT learn the source address of the encapsulated frame.
81  *
82  * A = Indicates that the group policy has already been applied to
83  *     this packet. Policies MUST NOT be applied by devices when the
84  *     A bit is set.
85  *
86  * https://tools.ietf.org/html/draft-smith-vxlan-group-policy
87  */
88 struct vxlanhdr_gbp {
89 	u8	vx_flags;
90 #ifdef __LITTLE_ENDIAN_BITFIELD
91 	u8	reserved_flags1:3,
92 		policy_applied:1,
93 		reserved_flags2:2,
94 		dont_learn:1,
95 		reserved_flags3:1;
96 #elif defined(__BIG_ENDIAN_BITFIELD)
97 	u8	reserved_flags1:1,
98 		dont_learn:1,
99 		reserved_flags2:2,
100 		policy_applied:1,
101 		reserved_flags3:3;
102 #else
103 #error	"Please fix <asm/byteorder.h>"
104 #endif
105 	__be16	policy_id;
106 	__be32	vx_vni;
107 };
108 
109 /* VXLAN-GBP header flags. */
110 #define VXLAN_HF_GBP	cpu_to_be32(BIT(31))
111 
112 #define VXLAN_GBP_USED_BITS (VXLAN_HF_GBP | cpu_to_be32(0xFFFFFF))
113 
114 /* skb->mark mapping
115  *
116  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
117  * |R|R|R|R|R|R|R|R|R|D|R|R|A|R|R|R|        Group Policy ID        |
118  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
119  */
120 #define VXLAN_GBP_DONT_LEARN		(BIT(6) << 16)
121 #define VXLAN_GBP_POLICY_APPLIED	(BIT(3) << 16)
122 #define VXLAN_GBP_ID_MASK		(0xFFFF)
123 
124 #define VXLAN_GBP_MASK (VXLAN_GBP_DONT_LEARN | VXLAN_GBP_POLICY_APPLIED | \
125 			VXLAN_GBP_ID_MASK)
126 
127 /*
128  * VXLAN Generic Protocol Extension (VXLAN_F_GPE):
129  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
130  * |R|R|Ver|I|P|R|O|       Reserved                |Next Protocol  |
131  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
132  * |                VXLAN Network Identifier (VNI) |   Reserved    |
133  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
134  *
135  * Ver = Version. Indicates VXLAN GPE protocol version.
136  *
137  * P = Next Protocol Bit. The P bit is set to indicate that the
138  *     Next Protocol field is present.
139  *
140  * O = OAM Flag Bit. The O bit is set to indicate that the packet
141  *     is an OAM packet.
142  *
143  * Next Protocol = This 8 bit field indicates the protocol header
144  * immediately following the VXLAN GPE header.
145  *
146  * https://tools.ietf.org/html/draft-ietf-nvo3-vxlan-gpe-01
147  */
148 
149 struct vxlanhdr_gpe {
150 #if defined(__LITTLE_ENDIAN_BITFIELD)
151 	u8	oam_flag:1,
152 		reserved_flags1:1,
153 		np_applied:1,
154 		instance_applied:1,
155 		version:2,
156 		reserved_flags2:2;
157 #elif defined(__BIG_ENDIAN_BITFIELD)
158 	u8	reserved_flags2:2,
159 		version:2,
160 		instance_applied:1,
161 		np_applied:1,
162 		reserved_flags1:1,
163 		oam_flag:1;
164 #endif
165 	u8	reserved_flags3;
166 	u8	reserved_flags4;
167 	u8	next_protocol;
168 	__be32	vx_vni;
169 };
170 
171 /* VXLAN-GPE header flags. */
172 #define VXLAN_HF_VER	cpu_to_be32(BIT(29) | BIT(28))
173 #define VXLAN_HF_NP	cpu_to_be32(BIT(26))
174 #define VXLAN_HF_OAM	cpu_to_be32(BIT(24))
175 
176 #define VXLAN_GPE_USED_BITS (VXLAN_HF_VER | VXLAN_HF_NP | VXLAN_HF_OAM | \
177 			     cpu_to_be32(0xff))
178 
179 struct vxlan_metadata {
180 	u32		gbp;
181 };
182 
183 /* per UDP socket information */
184 struct vxlan_sock {
185 	struct hlist_node hlist;
186 	struct socket	 *sock;
187 	struct hlist_head vni_list[VNI_HASH_SIZE];
188 	refcount_t	  refcnt;
189 	u32		  flags;
190 };
191 
192 union vxlan_addr {
193 	struct sockaddr_in sin;
194 	struct sockaddr_in6 sin6;
195 	struct sockaddr sa;
196 };
197 
198 struct vxlan_rdst {
199 	union vxlan_addr	 remote_ip;
200 	__be16			 remote_port;
201 	u8			 offloaded:1;
202 	__be32			 remote_vni;
203 	u32			 remote_ifindex;
204 	struct net_device	 *remote_dev;
205 	struct list_head	 list;
206 	struct rcu_head		 rcu;
207 	struct dst_cache	 dst_cache;
208 };
209 
210 struct vxlan_config {
211 	union vxlan_addr	remote_ip;
212 	union vxlan_addr	saddr;
213 	__be32			vni;
214 	int			remote_ifindex;
215 	int			mtu;
216 	__be16			dst_port;
217 	u16			port_min;
218 	u16			port_max;
219 	u8			tos;
220 	u8			ttl;
221 	__be32			label;
222 	u32			flags;
223 	unsigned long		age_interval;
224 	unsigned int		addrmax;
225 	bool			no_share;
226 	enum ifla_vxlan_df	df;
227 };
228 
229 struct vxlan_dev_node {
230 	struct hlist_node hlist;
231 	struct vxlan_dev *vxlan;
232 };
233 
234 /* Pseudo network device */
235 struct vxlan_dev {
236 	struct vxlan_dev_node hlist4;	/* vni hash table for IPv4 socket */
237 #if IS_ENABLED(CONFIG_IPV6)
238 	struct vxlan_dev_node hlist6;	/* vni hash table for IPv6 socket */
239 #endif
240 	struct list_head  next;		/* vxlan's per namespace list */
241 	struct vxlan_sock __rcu *vn4_sock;	/* listening socket for IPv4 */
242 #if IS_ENABLED(CONFIG_IPV6)
243 	struct vxlan_sock __rcu *vn6_sock;	/* listening socket for IPv6 */
244 #endif
245 	struct net_device *dev;
246 	struct net	  *net;		/* netns for packet i/o */
247 	struct vxlan_rdst default_dst;	/* default destination */
248 
249 	struct timer_list age_timer;
250 	spinlock_t	  hash_lock[FDB_HASH_SIZE];
251 	unsigned int	  addrcnt;
252 	struct gro_cells  gro_cells;
253 
254 	struct vxlan_config	cfg;
255 
256 	struct hlist_head fdb_head[FDB_HASH_SIZE];
257 };
258 
259 #define VXLAN_F_LEARN			0x01
260 #define VXLAN_F_PROXY			0x02
261 #define VXLAN_F_RSC			0x04
262 #define VXLAN_F_L2MISS			0x08
263 #define VXLAN_F_L3MISS			0x10
264 #define VXLAN_F_IPV6			0x20
265 #define VXLAN_F_UDP_ZERO_CSUM_TX	0x40
266 #define VXLAN_F_UDP_ZERO_CSUM6_TX	0x80
267 #define VXLAN_F_UDP_ZERO_CSUM6_RX	0x100
268 #define VXLAN_F_REMCSUM_TX		0x200
269 #define VXLAN_F_REMCSUM_RX		0x400
270 #define VXLAN_F_GBP			0x800
271 #define VXLAN_F_REMCSUM_NOPARTIAL	0x1000
272 #define VXLAN_F_COLLECT_METADATA	0x2000
273 #define VXLAN_F_GPE			0x4000
274 #define VXLAN_F_IPV6_LINKLOCAL		0x8000
275 #define VXLAN_F_TTL_INHERIT		0x10000
276 
277 /* Flags that are used in the receive path. These flags must match in
278  * order for a socket to be shareable
279  */
280 #define VXLAN_F_RCV_FLAGS		(VXLAN_F_GBP |			\
281 					 VXLAN_F_GPE |			\
282 					 VXLAN_F_UDP_ZERO_CSUM6_RX |	\
283 					 VXLAN_F_REMCSUM_RX |		\
284 					 VXLAN_F_REMCSUM_NOPARTIAL |	\
285 					 VXLAN_F_COLLECT_METADATA)
286 
287 /* Flags that can be set together with VXLAN_F_GPE. */
288 #define VXLAN_F_ALLOWED_GPE		(VXLAN_F_GPE |			\
289 					 VXLAN_F_IPV6 |			\
290 					 VXLAN_F_IPV6_LINKLOCAL |	\
291 					 VXLAN_F_UDP_ZERO_CSUM_TX |	\
292 					 VXLAN_F_UDP_ZERO_CSUM6_TX |	\
293 					 VXLAN_F_UDP_ZERO_CSUM6_RX |	\
294 					 VXLAN_F_COLLECT_METADATA)
295 
296 struct net_device *vxlan_dev_create(struct net *net, const char *name,
297 				    u8 name_assign_type, struct vxlan_config *conf);
298 
299 static inline netdev_features_t vxlan_features_check(struct sk_buff *skb,
300 						     netdev_features_t features)
301 {
302 	u8 l4_hdr = 0;
303 
304 	if (!skb->encapsulation)
305 		return features;
306 
307 	switch (vlan_get_protocol(skb)) {
308 	case htons(ETH_P_IP):
309 		l4_hdr = ip_hdr(skb)->protocol;
310 		break;
311 	case htons(ETH_P_IPV6):
312 		l4_hdr = ipv6_hdr(skb)->nexthdr;
313 		break;
314 	default:
315 		return features;
316 	}
317 
318 	if ((l4_hdr == IPPROTO_UDP) &&
319 	    (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
320 	     skb->inner_protocol != htons(ETH_P_TEB) ||
321 	     (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
322 	      sizeof(struct udphdr) + sizeof(struct vxlanhdr)) ||
323 	     (skb->ip_summed != CHECKSUM_NONE &&
324 	      !can_checksum_protocol(features, inner_eth_hdr(skb)->h_proto))))
325 		return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
326 
327 	return features;
328 }
329 
330 /* IP header + UDP + VXLAN + Ethernet header */
331 #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
332 /* IPv6 header + UDP + VXLAN + Ethernet header */
333 #define VXLAN6_HEADROOM (40 + 8 + 8 + 14)
334 
335 static inline struct vxlanhdr *vxlan_hdr(struct sk_buff *skb)
336 {
337 	return (struct vxlanhdr *)(udp_hdr(skb) + 1);
338 }
339 
340 static inline __be32 vxlan_vni(__be32 vni_field)
341 {
342 #if defined(__BIG_ENDIAN)
343 	return (__force __be32)((__force u32)vni_field >> 8);
344 #else
345 	return (__force __be32)((__force u32)(vni_field & VXLAN_VNI_MASK) << 8);
346 #endif
347 }
348 
349 static inline __be32 vxlan_vni_field(__be32 vni)
350 {
351 #if defined(__BIG_ENDIAN)
352 	return (__force __be32)((__force u32)vni << 8);
353 #else
354 	return (__force __be32)((__force u32)vni >> 8);
355 #endif
356 }
357 
358 static inline size_t vxlan_rco_start(__be32 vni_field)
359 {
360 	return be32_to_cpu(vni_field & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
361 }
362 
363 static inline size_t vxlan_rco_offset(__be32 vni_field)
364 {
365 	return (vni_field & VXLAN_RCO_UDP) ?
366 		offsetof(struct udphdr, check) :
367 		offsetof(struct tcphdr, check);
368 }
369 
370 static inline __be32 vxlan_compute_rco(unsigned int start, unsigned int offset)
371 {
372 	__be32 vni_field = cpu_to_be32(start >> VXLAN_RCO_SHIFT);
373 
374 	if (offset == offsetof(struct udphdr, check))
375 		vni_field |= VXLAN_RCO_UDP;
376 	return vni_field;
377 }
378 
379 static inline unsigned short vxlan_get_sk_family(struct vxlan_sock *vs)
380 {
381 	return vs->sock->sk->sk_family;
382 }
383 
384 #if IS_ENABLED(CONFIG_IPV6)
385 
386 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
387 {
388 	if (ipa->sa.sa_family == AF_INET6)
389 		return ipv6_addr_any(&ipa->sin6.sin6_addr);
390 	else
391 		return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
392 }
393 
394 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
395 {
396 	if (ipa->sa.sa_family == AF_INET6)
397 		return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
398 	else
399 		return ipv4_is_multicast(ipa->sin.sin_addr.s_addr);
400 }
401 
402 #else /* !IS_ENABLED(CONFIG_IPV6) */
403 
404 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
405 {
406 	return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
407 }
408 
409 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
410 {
411 	return ipv4_is_multicast(ipa->sin.sin_addr.s_addr);
412 }
413 
414 #endif /* IS_ENABLED(CONFIG_IPV6) */
415 
416 static inline bool netif_is_vxlan(const struct net_device *dev)
417 {
418 	return dev->rtnl_link_ops &&
419 	       !strcmp(dev->rtnl_link_ops->kind, "vxlan");
420 }
421 
422 struct switchdev_notifier_vxlan_fdb_info {
423 	struct switchdev_notifier_info info; /* must be first */
424 	union vxlan_addr remote_ip;
425 	__be16 remote_port;
426 	__be32 remote_vni;
427 	u32 remote_ifindex;
428 	u8 eth_addr[ETH_ALEN];
429 	__be32 vni;
430 	bool offloaded;
431 	bool added_by_user;
432 };
433 
434 #if IS_ENABLED(CONFIG_VXLAN)
435 int vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
436 		      struct switchdev_notifier_vxlan_fdb_info *fdb_info);
437 int vxlan_fdb_replay(const struct net_device *dev, __be32 vni,
438 		     struct notifier_block *nb,
439 		     struct netlink_ext_ack *extack);
440 void vxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni);
441 
442 #else
443 static inline int
444 vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
445 		  struct switchdev_notifier_vxlan_fdb_info *fdb_info)
446 {
447 	return -ENOENT;
448 }
449 
450 static inline int vxlan_fdb_replay(const struct net_device *dev, __be32 vni,
451 				   struct notifier_block *nb,
452 				   struct netlink_ext_ack *extack)
453 {
454 	return -EOPNOTSUPP;
455 }
456 
457 static inline void
458 vxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni)
459 {
460 }
461 #endif
462 
463 static inline void vxlan_flag_attr_error(int attrtype,
464 					 struct netlink_ext_ack *extack)
465 {
466 #define VXLAN_FLAG(flg) \
467 	case IFLA_VXLAN_##flg: \
468 		NL_SET_ERR_MSG_MOD(extack, \
469 				   "cannot change " #flg " flag"); \
470 		break
471 	switch (attrtype) {
472 	VXLAN_FLAG(TTL_INHERIT);
473 	VXLAN_FLAG(LEARNING);
474 	VXLAN_FLAG(PROXY);
475 	VXLAN_FLAG(RSC);
476 	VXLAN_FLAG(L2MISS);
477 	VXLAN_FLAG(L3MISS);
478 	VXLAN_FLAG(COLLECT_METADATA);
479 	VXLAN_FLAG(UDP_ZERO_CSUM6_TX);
480 	VXLAN_FLAG(UDP_ZERO_CSUM6_RX);
481 	VXLAN_FLAG(REMCSUM_TX);
482 	VXLAN_FLAG(REMCSUM_RX);
483 	VXLAN_FLAG(GBP);
484 	VXLAN_FLAG(GPE);
485 	VXLAN_FLAG(REMCSUM_NOPARTIAL);
486 	default:
487 		NL_SET_ERR_MSG_MOD(extack, \
488 				   "cannot change flag");
489 		break;
490 	}
491 #undef VXLAN_FLAG
492 }
493 
494 static inline bool vxlan_fdb_nh_path_select(struct nexthop *nh,
495 					    int hash,
496 					    struct vxlan_rdst *rdst)
497 {
498 	struct fib_nh_common *nhc;
499 
500 	nhc = nexthop_path_fdb_result(nh, hash);
501 	if (unlikely(!nhc))
502 		return false;
503 
504 	switch (nhc->nhc_gw_family) {
505 	case AF_INET:
506 		rdst->remote_ip.sin.sin_addr.s_addr = nhc->nhc_gw.ipv4;
507 		rdst->remote_ip.sa.sa_family = AF_INET;
508 		break;
509 	case AF_INET6:
510 		rdst->remote_ip.sin6.sin6_addr = nhc->nhc_gw.ipv6;
511 		rdst->remote_ip.sa.sa_family = AF_INET6;
512 		break;
513 	}
514 
515 	return true;
516 }
517 
518 #endif
519