18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef __NET_VXLAN_H
38c2ecf20Sopenharmony_ci#define __NET_VXLAN_H 1
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/if_vlan.h>
68c2ecf20Sopenharmony_ci#include <net/udp_tunnel.h>
78c2ecf20Sopenharmony_ci#include <net/dst_metadata.h>
88c2ecf20Sopenharmony_ci#include <net/rtnetlink.h>
98c2ecf20Sopenharmony_ci#include <net/switchdev.h>
108c2ecf20Sopenharmony_ci#include <net/nexthop.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#define IANA_VXLAN_UDP_PORT     4789
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/* VXLAN protocol (RFC 7348) header:
158c2ecf20Sopenharmony_ci * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
168c2ecf20Sopenharmony_ci * |R|R|R|R|I|R|R|R|               Reserved                        |
178c2ecf20Sopenharmony_ci * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
188c2ecf20Sopenharmony_ci * |                VXLAN Network Identifier (VNI) |   Reserved    |
198c2ecf20Sopenharmony_ci * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * I = VXLAN Network Identifier (VNI) present.
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_cistruct vxlanhdr {
248c2ecf20Sopenharmony_ci	__be32 vx_flags;
258c2ecf20Sopenharmony_ci	__be32 vx_vni;
268c2ecf20Sopenharmony_ci};
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* VXLAN header flags. */
298c2ecf20Sopenharmony_ci#define VXLAN_HF_VNI	cpu_to_be32(BIT(27))
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define VXLAN_N_VID     (1u << 24)
328c2ecf20Sopenharmony_ci#define VXLAN_VID_MASK  (VXLAN_N_VID - 1)
338c2ecf20Sopenharmony_ci#define VXLAN_VNI_MASK	cpu_to_be32(VXLAN_VID_MASK << 8)
348c2ecf20Sopenharmony_ci#define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define VNI_HASH_BITS	10
378c2ecf20Sopenharmony_ci#define VNI_HASH_SIZE	(1<<VNI_HASH_BITS)
388c2ecf20Sopenharmony_ci#define FDB_HASH_BITS	8
398c2ecf20Sopenharmony_ci#define FDB_HASH_SIZE	(1<<FDB_HASH_BITS)
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* Remote checksum offload for VXLAN (VXLAN_F_REMCSUM_[RT]X):
428c2ecf20Sopenharmony_ci * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
438c2ecf20Sopenharmony_ci * |R|R|R|R|I|R|R|R|R|R|C|              Reserved                   |
448c2ecf20Sopenharmony_ci * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
458c2ecf20Sopenharmony_ci * |           VXLAN Network Identifier (VNI)      |O| Csum start  |
468c2ecf20Sopenharmony_ci * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
478c2ecf20Sopenharmony_ci *
488c2ecf20Sopenharmony_ci * C = Remote checksum offload bit. When set indicates that the
498c2ecf20Sopenharmony_ci *     remote checksum offload data is present.
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci * O = Offset bit. Indicates the checksum offset relative to
528c2ecf20Sopenharmony_ci *     checksum start.
538c2ecf20Sopenharmony_ci *
548c2ecf20Sopenharmony_ci * Csum start = Checksum start divided by two.
558c2ecf20Sopenharmony_ci *
568c2ecf20Sopenharmony_ci * http://tools.ietf.org/html/draft-herbert-vxlan-rco
578c2ecf20Sopenharmony_ci */
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/* VXLAN-RCO header flags. */
608c2ecf20Sopenharmony_ci#define VXLAN_HF_RCO	cpu_to_be32(BIT(21))
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/* Remote checksum offload header option */
638c2ecf20Sopenharmony_ci#define VXLAN_RCO_MASK	cpu_to_be32(0x7f)  /* Last byte of vni field */
648c2ecf20Sopenharmony_ci#define VXLAN_RCO_UDP	cpu_to_be32(0x80)  /* Indicate UDP RCO (TCP when not set *) */
658c2ecf20Sopenharmony_ci#define VXLAN_RCO_SHIFT	1		   /* Left shift of start */
668c2ecf20Sopenharmony_ci#define VXLAN_RCO_SHIFT_MASK ((1 << VXLAN_RCO_SHIFT) - 1)
678c2ecf20Sopenharmony_ci#define VXLAN_MAX_REMCSUM_START (0x7f << VXLAN_RCO_SHIFT)
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/*
708c2ecf20Sopenharmony_ci * VXLAN Group Based Policy Extension (VXLAN_F_GBP):
718c2ecf20Sopenharmony_ci * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
728c2ecf20Sopenharmony_ci * |G|R|R|R|I|R|R|R|R|D|R|R|A|R|R|R|        Group Policy ID        |
738c2ecf20Sopenharmony_ci * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
748c2ecf20Sopenharmony_ci * |                VXLAN Network Identifier (VNI) |   Reserved    |
758c2ecf20Sopenharmony_ci * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
768c2ecf20Sopenharmony_ci *
778c2ecf20Sopenharmony_ci * G = Group Policy ID present.
788c2ecf20Sopenharmony_ci *
798c2ecf20Sopenharmony_ci * D = Don't Learn bit. When set, this bit indicates that the egress
808c2ecf20Sopenharmony_ci *     VTEP MUST NOT learn the source address of the encapsulated frame.
818c2ecf20Sopenharmony_ci *
828c2ecf20Sopenharmony_ci * A = Indicates that the group policy has already been applied to
838c2ecf20Sopenharmony_ci *     this packet. Policies MUST NOT be applied by devices when the
848c2ecf20Sopenharmony_ci *     A bit is set.
858c2ecf20Sopenharmony_ci *
868c2ecf20Sopenharmony_ci * https://tools.ietf.org/html/draft-smith-vxlan-group-policy
878c2ecf20Sopenharmony_ci */
888c2ecf20Sopenharmony_cistruct vxlanhdr_gbp {
898c2ecf20Sopenharmony_ci	u8	vx_flags;
908c2ecf20Sopenharmony_ci#ifdef __LITTLE_ENDIAN_BITFIELD
918c2ecf20Sopenharmony_ci	u8	reserved_flags1:3,
928c2ecf20Sopenharmony_ci		policy_applied:1,
938c2ecf20Sopenharmony_ci		reserved_flags2:2,
948c2ecf20Sopenharmony_ci		dont_learn:1,
958c2ecf20Sopenharmony_ci		reserved_flags3:1;
968c2ecf20Sopenharmony_ci#elif defined(__BIG_ENDIAN_BITFIELD)
978c2ecf20Sopenharmony_ci	u8	reserved_flags1:1,
988c2ecf20Sopenharmony_ci		dont_learn:1,
998c2ecf20Sopenharmony_ci		reserved_flags2:2,
1008c2ecf20Sopenharmony_ci		policy_applied:1,
1018c2ecf20Sopenharmony_ci		reserved_flags3:3;
1028c2ecf20Sopenharmony_ci#else
1038c2ecf20Sopenharmony_ci#error	"Please fix <asm/byteorder.h>"
1048c2ecf20Sopenharmony_ci#endif
1058c2ecf20Sopenharmony_ci	__be16	policy_id;
1068c2ecf20Sopenharmony_ci	__be32	vx_vni;
1078c2ecf20Sopenharmony_ci};
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci/* VXLAN-GBP header flags. */
1108c2ecf20Sopenharmony_ci#define VXLAN_HF_GBP	cpu_to_be32(BIT(31))
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci#define VXLAN_GBP_USED_BITS (VXLAN_HF_GBP | cpu_to_be32(0xFFFFFF))
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci/* skb->mark mapping
1158c2ecf20Sopenharmony_ci *
1168c2ecf20Sopenharmony_ci * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1178c2ecf20Sopenharmony_ci * |R|R|R|R|R|R|R|R|R|D|R|R|A|R|R|R|        Group Policy ID        |
1188c2ecf20Sopenharmony_ci * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1198c2ecf20Sopenharmony_ci */
1208c2ecf20Sopenharmony_ci#define VXLAN_GBP_DONT_LEARN		(BIT(6) << 16)
1218c2ecf20Sopenharmony_ci#define VXLAN_GBP_POLICY_APPLIED	(BIT(3) << 16)
1228c2ecf20Sopenharmony_ci#define VXLAN_GBP_ID_MASK		(0xFFFF)
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci#define VXLAN_GBP_MASK (VXLAN_GBP_DONT_LEARN | VXLAN_GBP_POLICY_APPLIED | \
1258c2ecf20Sopenharmony_ci			VXLAN_GBP_ID_MASK)
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci/*
1288c2ecf20Sopenharmony_ci * VXLAN Generic Protocol Extension (VXLAN_F_GPE):
1298c2ecf20Sopenharmony_ci * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1308c2ecf20Sopenharmony_ci * |R|R|Ver|I|P|R|O|       Reserved                |Next Protocol  |
1318c2ecf20Sopenharmony_ci * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1328c2ecf20Sopenharmony_ci * |                VXLAN Network Identifier (VNI) |   Reserved    |
1338c2ecf20Sopenharmony_ci * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1348c2ecf20Sopenharmony_ci *
1358c2ecf20Sopenharmony_ci * Ver = Version. Indicates VXLAN GPE protocol version.
1368c2ecf20Sopenharmony_ci *
1378c2ecf20Sopenharmony_ci * P = Next Protocol Bit. The P bit is set to indicate that the
1388c2ecf20Sopenharmony_ci *     Next Protocol field is present.
1398c2ecf20Sopenharmony_ci *
1408c2ecf20Sopenharmony_ci * O = OAM Flag Bit. The O bit is set to indicate that the packet
1418c2ecf20Sopenharmony_ci *     is an OAM packet.
1428c2ecf20Sopenharmony_ci *
1438c2ecf20Sopenharmony_ci * Next Protocol = This 8 bit field indicates the protocol header
1448c2ecf20Sopenharmony_ci * immediately following the VXLAN GPE header.
1458c2ecf20Sopenharmony_ci *
1468c2ecf20Sopenharmony_ci * https://tools.ietf.org/html/draft-ietf-nvo3-vxlan-gpe-01
1478c2ecf20Sopenharmony_ci */
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistruct vxlanhdr_gpe {
1508c2ecf20Sopenharmony_ci#if defined(__LITTLE_ENDIAN_BITFIELD)
1518c2ecf20Sopenharmony_ci	u8	oam_flag:1,
1528c2ecf20Sopenharmony_ci		reserved_flags1:1,
1538c2ecf20Sopenharmony_ci		np_applied:1,
1548c2ecf20Sopenharmony_ci		instance_applied:1,
1558c2ecf20Sopenharmony_ci		version:2,
1568c2ecf20Sopenharmony_ci		reserved_flags2:2;
1578c2ecf20Sopenharmony_ci#elif defined(__BIG_ENDIAN_BITFIELD)
1588c2ecf20Sopenharmony_ci	u8	reserved_flags2:2,
1598c2ecf20Sopenharmony_ci		version:2,
1608c2ecf20Sopenharmony_ci		instance_applied:1,
1618c2ecf20Sopenharmony_ci		np_applied:1,
1628c2ecf20Sopenharmony_ci		reserved_flags1:1,
1638c2ecf20Sopenharmony_ci		oam_flag:1;
1648c2ecf20Sopenharmony_ci#endif
1658c2ecf20Sopenharmony_ci	u8	reserved_flags3;
1668c2ecf20Sopenharmony_ci	u8	reserved_flags4;
1678c2ecf20Sopenharmony_ci	u8	next_protocol;
1688c2ecf20Sopenharmony_ci	__be32	vx_vni;
1698c2ecf20Sopenharmony_ci};
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci/* VXLAN-GPE header flags. */
1728c2ecf20Sopenharmony_ci#define VXLAN_HF_VER	cpu_to_be32(BIT(29) | BIT(28))
1738c2ecf20Sopenharmony_ci#define VXLAN_HF_NP	cpu_to_be32(BIT(26))
1748c2ecf20Sopenharmony_ci#define VXLAN_HF_OAM	cpu_to_be32(BIT(24))
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci#define VXLAN_GPE_USED_BITS (VXLAN_HF_VER | VXLAN_HF_NP | VXLAN_HF_OAM | \
1778c2ecf20Sopenharmony_ci			     cpu_to_be32(0xff))
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistruct vxlan_metadata {
1808c2ecf20Sopenharmony_ci	u32		gbp;
1818c2ecf20Sopenharmony_ci};
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci/* per UDP socket information */
1848c2ecf20Sopenharmony_cistruct vxlan_sock {
1858c2ecf20Sopenharmony_ci	struct hlist_node hlist;
1868c2ecf20Sopenharmony_ci	struct socket	 *sock;
1878c2ecf20Sopenharmony_ci	struct hlist_head vni_list[VNI_HASH_SIZE];
1888c2ecf20Sopenharmony_ci	refcount_t	  refcnt;
1898c2ecf20Sopenharmony_ci	u32		  flags;
1908c2ecf20Sopenharmony_ci};
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ciunion vxlan_addr {
1938c2ecf20Sopenharmony_ci	struct sockaddr_in sin;
1948c2ecf20Sopenharmony_ci	struct sockaddr_in6 sin6;
1958c2ecf20Sopenharmony_ci	struct sockaddr sa;
1968c2ecf20Sopenharmony_ci};
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_cistruct vxlan_rdst {
1998c2ecf20Sopenharmony_ci	union vxlan_addr	 remote_ip;
2008c2ecf20Sopenharmony_ci	__be16			 remote_port;
2018c2ecf20Sopenharmony_ci	u8			 offloaded:1;
2028c2ecf20Sopenharmony_ci	__be32			 remote_vni;
2038c2ecf20Sopenharmony_ci	u32			 remote_ifindex;
2048c2ecf20Sopenharmony_ci	struct net_device	 *remote_dev;
2058c2ecf20Sopenharmony_ci	struct list_head	 list;
2068c2ecf20Sopenharmony_ci	struct rcu_head		 rcu;
2078c2ecf20Sopenharmony_ci	struct dst_cache	 dst_cache;
2088c2ecf20Sopenharmony_ci};
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_cistruct vxlan_config {
2118c2ecf20Sopenharmony_ci	union vxlan_addr	remote_ip;
2128c2ecf20Sopenharmony_ci	union vxlan_addr	saddr;
2138c2ecf20Sopenharmony_ci	__be32			vni;
2148c2ecf20Sopenharmony_ci	int			remote_ifindex;
2158c2ecf20Sopenharmony_ci	int			mtu;
2168c2ecf20Sopenharmony_ci	__be16			dst_port;
2178c2ecf20Sopenharmony_ci	u16			port_min;
2188c2ecf20Sopenharmony_ci	u16			port_max;
2198c2ecf20Sopenharmony_ci	u8			tos;
2208c2ecf20Sopenharmony_ci	u8			ttl;
2218c2ecf20Sopenharmony_ci	__be32			label;
2228c2ecf20Sopenharmony_ci	u32			flags;
2238c2ecf20Sopenharmony_ci	unsigned long		age_interval;
2248c2ecf20Sopenharmony_ci	unsigned int		addrmax;
2258c2ecf20Sopenharmony_ci	bool			no_share;
2268c2ecf20Sopenharmony_ci	enum ifla_vxlan_df	df;
2278c2ecf20Sopenharmony_ci};
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_cistruct vxlan_dev_node {
2308c2ecf20Sopenharmony_ci	struct hlist_node hlist;
2318c2ecf20Sopenharmony_ci	struct vxlan_dev *vxlan;
2328c2ecf20Sopenharmony_ci};
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci/* Pseudo network device */
2358c2ecf20Sopenharmony_cistruct vxlan_dev {
2368c2ecf20Sopenharmony_ci	struct vxlan_dev_node hlist4;	/* vni hash table for IPv4 socket */
2378c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
2388c2ecf20Sopenharmony_ci	struct vxlan_dev_node hlist6;	/* vni hash table for IPv6 socket */
2398c2ecf20Sopenharmony_ci#endif
2408c2ecf20Sopenharmony_ci	struct list_head  next;		/* vxlan's per namespace list */
2418c2ecf20Sopenharmony_ci	struct vxlan_sock __rcu *vn4_sock;	/* listening socket for IPv4 */
2428c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
2438c2ecf20Sopenharmony_ci	struct vxlan_sock __rcu *vn6_sock;	/* listening socket for IPv6 */
2448c2ecf20Sopenharmony_ci#endif
2458c2ecf20Sopenharmony_ci	struct net_device *dev;
2468c2ecf20Sopenharmony_ci	struct net	  *net;		/* netns for packet i/o */
2478c2ecf20Sopenharmony_ci	struct vxlan_rdst default_dst;	/* default destination */
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	struct timer_list age_timer;
2508c2ecf20Sopenharmony_ci	spinlock_t	  hash_lock[FDB_HASH_SIZE];
2518c2ecf20Sopenharmony_ci	unsigned int	  addrcnt;
2528c2ecf20Sopenharmony_ci	struct gro_cells  gro_cells;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	struct vxlan_config	cfg;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	struct hlist_head fdb_head[FDB_HASH_SIZE];
2578c2ecf20Sopenharmony_ci};
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci#define VXLAN_F_LEARN			0x01
2608c2ecf20Sopenharmony_ci#define VXLAN_F_PROXY			0x02
2618c2ecf20Sopenharmony_ci#define VXLAN_F_RSC			0x04
2628c2ecf20Sopenharmony_ci#define VXLAN_F_L2MISS			0x08
2638c2ecf20Sopenharmony_ci#define VXLAN_F_L3MISS			0x10
2648c2ecf20Sopenharmony_ci#define VXLAN_F_IPV6			0x20
2658c2ecf20Sopenharmony_ci#define VXLAN_F_UDP_ZERO_CSUM_TX	0x40
2668c2ecf20Sopenharmony_ci#define VXLAN_F_UDP_ZERO_CSUM6_TX	0x80
2678c2ecf20Sopenharmony_ci#define VXLAN_F_UDP_ZERO_CSUM6_RX	0x100
2688c2ecf20Sopenharmony_ci#define VXLAN_F_REMCSUM_TX		0x200
2698c2ecf20Sopenharmony_ci#define VXLAN_F_REMCSUM_RX		0x400
2708c2ecf20Sopenharmony_ci#define VXLAN_F_GBP			0x800
2718c2ecf20Sopenharmony_ci#define VXLAN_F_REMCSUM_NOPARTIAL	0x1000
2728c2ecf20Sopenharmony_ci#define VXLAN_F_COLLECT_METADATA	0x2000
2738c2ecf20Sopenharmony_ci#define VXLAN_F_GPE			0x4000
2748c2ecf20Sopenharmony_ci#define VXLAN_F_IPV6_LINKLOCAL		0x8000
2758c2ecf20Sopenharmony_ci#define VXLAN_F_TTL_INHERIT		0x10000
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci/* Flags that are used in the receive path. These flags must match in
2788c2ecf20Sopenharmony_ci * order for a socket to be shareable
2798c2ecf20Sopenharmony_ci */
2808c2ecf20Sopenharmony_ci#define VXLAN_F_RCV_FLAGS		(VXLAN_F_GBP |			\
2818c2ecf20Sopenharmony_ci					 VXLAN_F_GPE |			\
2828c2ecf20Sopenharmony_ci					 VXLAN_F_UDP_ZERO_CSUM6_RX |	\
2838c2ecf20Sopenharmony_ci					 VXLAN_F_REMCSUM_RX |		\
2848c2ecf20Sopenharmony_ci					 VXLAN_F_REMCSUM_NOPARTIAL |	\
2858c2ecf20Sopenharmony_ci					 VXLAN_F_COLLECT_METADATA)
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci/* Flags that can be set together with VXLAN_F_GPE. */
2888c2ecf20Sopenharmony_ci#define VXLAN_F_ALLOWED_GPE		(VXLAN_F_GPE |			\
2898c2ecf20Sopenharmony_ci					 VXLAN_F_IPV6 |			\
2908c2ecf20Sopenharmony_ci					 VXLAN_F_IPV6_LINKLOCAL |	\
2918c2ecf20Sopenharmony_ci					 VXLAN_F_UDP_ZERO_CSUM_TX |	\
2928c2ecf20Sopenharmony_ci					 VXLAN_F_UDP_ZERO_CSUM6_TX |	\
2938c2ecf20Sopenharmony_ci					 VXLAN_F_UDP_ZERO_CSUM6_RX |	\
2948c2ecf20Sopenharmony_ci					 VXLAN_F_COLLECT_METADATA)
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_cistruct net_device *vxlan_dev_create(struct net *net, const char *name,
2978c2ecf20Sopenharmony_ci				    u8 name_assign_type, struct vxlan_config *conf);
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_cistatic inline netdev_features_t vxlan_features_check(struct sk_buff *skb,
3008c2ecf20Sopenharmony_ci						     netdev_features_t features)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	u8 l4_hdr = 0;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	if (!skb->encapsulation)
3058c2ecf20Sopenharmony_ci		return features;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	switch (vlan_get_protocol(skb)) {
3088c2ecf20Sopenharmony_ci	case htons(ETH_P_IP):
3098c2ecf20Sopenharmony_ci		l4_hdr = ip_hdr(skb)->protocol;
3108c2ecf20Sopenharmony_ci		break;
3118c2ecf20Sopenharmony_ci	case htons(ETH_P_IPV6):
3128c2ecf20Sopenharmony_ci		l4_hdr = ipv6_hdr(skb)->nexthdr;
3138c2ecf20Sopenharmony_ci		break;
3148c2ecf20Sopenharmony_ci	default:
3158c2ecf20Sopenharmony_ci		return features;
3168c2ecf20Sopenharmony_ci	}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	if ((l4_hdr == IPPROTO_UDP) &&
3198c2ecf20Sopenharmony_ci	    (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
3208c2ecf20Sopenharmony_ci	     skb->inner_protocol != htons(ETH_P_TEB) ||
3218c2ecf20Sopenharmony_ci	     (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
3228c2ecf20Sopenharmony_ci	      sizeof(struct udphdr) + sizeof(struct vxlanhdr)) ||
3238c2ecf20Sopenharmony_ci	     (skb->ip_summed != CHECKSUM_NONE &&
3248c2ecf20Sopenharmony_ci	      !can_checksum_protocol(features, inner_eth_hdr(skb)->h_proto))))
3258c2ecf20Sopenharmony_ci		return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	return features;
3288c2ecf20Sopenharmony_ci}
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_cistatic inline int vxlan_headroom(u32 flags)
3318c2ecf20Sopenharmony_ci{
3328c2ecf20Sopenharmony_ci	/* VXLAN:     IP4/6 header + UDP + VXLAN + Ethernet header */
3338c2ecf20Sopenharmony_ci	/* VXLAN-GPE: IP4/6 header + UDP + VXLAN */
3348c2ecf20Sopenharmony_ci	return (flags & VXLAN_F_IPV6 ? sizeof(struct ipv6hdr) :
3358c2ecf20Sopenharmony_ci				       sizeof(struct iphdr)) +
3368c2ecf20Sopenharmony_ci	       sizeof(struct udphdr) + sizeof(struct vxlanhdr) +
3378c2ecf20Sopenharmony_ci	       (flags & VXLAN_F_GPE ? 0 : ETH_HLEN);
3388c2ecf20Sopenharmony_ci}
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_cistatic inline struct vxlanhdr *vxlan_hdr(struct sk_buff *skb)
3418c2ecf20Sopenharmony_ci{
3428c2ecf20Sopenharmony_ci	return (struct vxlanhdr *)(udp_hdr(skb) + 1);
3438c2ecf20Sopenharmony_ci}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_cistatic inline __be32 vxlan_vni(__be32 vni_field)
3468c2ecf20Sopenharmony_ci{
3478c2ecf20Sopenharmony_ci#if defined(__BIG_ENDIAN)
3488c2ecf20Sopenharmony_ci	return (__force __be32)((__force u32)vni_field >> 8);
3498c2ecf20Sopenharmony_ci#else
3508c2ecf20Sopenharmony_ci	return (__force __be32)((__force u32)(vni_field & VXLAN_VNI_MASK) << 8);
3518c2ecf20Sopenharmony_ci#endif
3528c2ecf20Sopenharmony_ci}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_cistatic inline __be32 vxlan_vni_field(__be32 vni)
3558c2ecf20Sopenharmony_ci{
3568c2ecf20Sopenharmony_ci#if defined(__BIG_ENDIAN)
3578c2ecf20Sopenharmony_ci	return (__force __be32)((__force u32)vni << 8);
3588c2ecf20Sopenharmony_ci#else
3598c2ecf20Sopenharmony_ci	return (__force __be32)((__force u32)vni >> 8);
3608c2ecf20Sopenharmony_ci#endif
3618c2ecf20Sopenharmony_ci}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistatic inline size_t vxlan_rco_start(__be32 vni_field)
3648c2ecf20Sopenharmony_ci{
3658c2ecf20Sopenharmony_ci	return be32_to_cpu(vni_field & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
3668c2ecf20Sopenharmony_ci}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_cistatic inline size_t vxlan_rco_offset(__be32 vni_field)
3698c2ecf20Sopenharmony_ci{
3708c2ecf20Sopenharmony_ci	return (vni_field & VXLAN_RCO_UDP) ?
3718c2ecf20Sopenharmony_ci		offsetof(struct udphdr, check) :
3728c2ecf20Sopenharmony_ci		offsetof(struct tcphdr, check);
3738c2ecf20Sopenharmony_ci}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_cistatic inline __be32 vxlan_compute_rco(unsigned int start, unsigned int offset)
3768c2ecf20Sopenharmony_ci{
3778c2ecf20Sopenharmony_ci	__be32 vni_field = cpu_to_be32(start >> VXLAN_RCO_SHIFT);
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	if (offset == offsetof(struct udphdr, check))
3808c2ecf20Sopenharmony_ci		vni_field |= VXLAN_RCO_UDP;
3818c2ecf20Sopenharmony_ci	return vni_field;
3828c2ecf20Sopenharmony_ci}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_cistatic inline unsigned short vxlan_get_sk_family(struct vxlan_sock *vs)
3858c2ecf20Sopenharmony_ci{
3868c2ecf20Sopenharmony_ci	return vs->sock->sk->sk_family;
3878c2ecf20Sopenharmony_ci}
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistatic inline bool vxlan_addr_any(const union vxlan_addr *ipa)
3928c2ecf20Sopenharmony_ci{
3938c2ecf20Sopenharmony_ci	if (ipa->sa.sa_family == AF_INET6)
3948c2ecf20Sopenharmony_ci		return ipv6_addr_any(&ipa->sin6.sin6_addr);
3958c2ecf20Sopenharmony_ci	else
3968c2ecf20Sopenharmony_ci		return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
3978c2ecf20Sopenharmony_ci}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_cistatic inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
4008c2ecf20Sopenharmony_ci{
4018c2ecf20Sopenharmony_ci	if (ipa->sa.sa_family == AF_INET6)
4028c2ecf20Sopenharmony_ci		return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
4038c2ecf20Sopenharmony_ci	else
4048c2ecf20Sopenharmony_ci		return ipv4_is_multicast(ipa->sin.sin_addr.s_addr);
4058c2ecf20Sopenharmony_ci}
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci#else /* !IS_ENABLED(CONFIG_IPV6) */
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_cistatic inline bool vxlan_addr_any(const union vxlan_addr *ipa)
4108c2ecf20Sopenharmony_ci{
4118c2ecf20Sopenharmony_ci	return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
4128c2ecf20Sopenharmony_ci}
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_cistatic inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
4158c2ecf20Sopenharmony_ci{
4168c2ecf20Sopenharmony_ci	return ipv4_is_multicast(ipa->sin.sin_addr.s_addr);
4178c2ecf20Sopenharmony_ci}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci#endif /* IS_ENABLED(CONFIG_IPV6) */
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_cistatic inline bool netif_is_vxlan(const struct net_device *dev)
4228c2ecf20Sopenharmony_ci{
4238c2ecf20Sopenharmony_ci	return dev->rtnl_link_ops &&
4248c2ecf20Sopenharmony_ci	       !strcmp(dev->rtnl_link_ops->kind, "vxlan");
4258c2ecf20Sopenharmony_ci}
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_cistruct switchdev_notifier_vxlan_fdb_info {
4288c2ecf20Sopenharmony_ci	struct switchdev_notifier_info info; /* must be first */
4298c2ecf20Sopenharmony_ci	union vxlan_addr remote_ip;
4308c2ecf20Sopenharmony_ci	__be16 remote_port;
4318c2ecf20Sopenharmony_ci	__be32 remote_vni;
4328c2ecf20Sopenharmony_ci	u32 remote_ifindex;
4338c2ecf20Sopenharmony_ci	u8 eth_addr[ETH_ALEN];
4348c2ecf20Sopenharmony_ci	__be32 vni;
4358c2ecf20Sopenharmony_ci	bool offloaded;
4368c2ecf20Sopenharmony_ci	bool added_by_user;
4378c2ecf20Sopenharmony_ci};
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_VXLAN)
4408c2ecf20Sopenharmony_ciint vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
4418c2ecf20Sopenharmony_ci		      struct switchdev_notifier_vxlan_fdb_info *fdb_info);
4428c2ecf20Sopenharmony_ciint vxlan_fdb_replay(const struct net_device *dev, __be32 vni,
4438c2ecf20Sopenharmony_ci		     struct notifier_block *nb,
4448c2ecf20Sopenharmony_ci		     struct netlink_ext_ack *extack);
4458c2ecf20Sopenharmony_civoid vxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni);
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci#else
4488c2ecf20Sopenharmony_cistatic inline int
4498c2ecf20Sopenharmony_civxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
4508c2ecf20Sopenharmony_ci		  struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4518c2ecf20Sopenharmony_ci{
4528c2ecf20Sopenharmony_ci	return -ENOENT;
4538c2ecf20Sopenharmony_ci}
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_cistatic inline int vxlan_fdb_replay(const struct net_device *dev, __be32 vni,
4568c2ecf20Sopenharmony_ci				   struct notifier_block *nb,
4578c2ecf20Sopenharmony_ci				   struct netlink_ext_ack *extack)
4588c2ecf20Sopenharmony_ci{
4598c2ecf20Sopenharmony_ci	return -EOPNOTSUPP;
4608c2ecf20Sopenharmony_ci}
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_cistatic inline void
4638c2ecf20Sopenharmony_civxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni)
4648c2ecf20Sopenharmony_ci{
4658c2ecf20Sopenharmony_ci}
4668c2ecf20Sopenharmony_ci#endif
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_cistatic inline void vxlan_flag_attr_error(int attrtype,
4698c2ecf20Sopenharmony_ci					 struct netlink_ext_ack *extack)
4708c2ecf20Sopenharmony_ci{
4718c2ecf20Sopenharmony_ci#define VXLAN_FLAG(flg) \
4728c2ecf20Sopenharmony_ci	case IFLA_VXLAN_##flg: \
4738c2ecf20Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, \
4748c2ecf20Sopenharmony_ci				   "cannot change " #flg " flag"); \
4758c2ecf20Sopenharmony_ci		break
4768c2ecf20Sopenharmony_ci	switch (attrtype) {
4778c2ecf20Sopenharmony_ci	VXLAN_FLAG(TTL_INHERIT);
4788c2ecf20Sopenharmony_ci	VXLAN_FLAG(LEARNING);
4798c2ecf20Sopenharmony_ci	VXLAN_FLAG(PROXY);
4808c2ecf20Sopenharmony_ci	VXLAN_FLAG(RSC);
4818c2ecf20Sopenharmony_ci	VXLAN_FLAG(L2MISS);
4828c2ecf20Sopenharmony_ci	VXLAN_FLAG(L3MISS);
4838c2ecf20Sopenharmony_ci	VXLAN_FLAG(COLLECT_METADATA);
4848c2ecf20Sopenharmony_ci	VXLAN_FLAG(UDP_ZERO_CSUM6_TX);
4858c2ecf20Sopenharmony_ci	VXLAN_FLAG(UDP_ZERO_CSUM6_RX);
4868c2ecf20Sopenharmony_ci	VXLAN_FLAG(REMCSUM_TX);
4878c2ecf20Sopenharmony_ci	VXLAN_FLAG(REMCSUM_RX);
4888c2ecf20Sopenharmony_ci	VXLAN_FLAG(GBP);
4898c2ecf20Sopenharmony_ci	VXLAN_FLAG(GPE);
4908c2ecf20Sopenharmony_ci	VXLAN_FLAG(REMCSUM_NOPARTIAL);
4918c2ecf20Sopenharmony_ci	default:
4928c2ecf20Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, \
4938c2ecf20Sopenharmony_ci				   "cannot change flag");
4948c2ecf20Sopenharmony_ci		break;
4958c2ecf20Sopenharmony_ci	}
4968c2ecf20Sopenharmony_ci#undef VXLAN_FLAG
4978c2ecf20Sopenharmony_ci}
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_cistatic inline bool vxlan_fdb_nh_path_select(struct nexthop *nh,
5008c2ecf20Sopenharmony_ci					    u32 hash,
5018c2ecf20Sopenharmony_ci					    struct vxlan_rdst *rdst)
5028c2ecf20Sopenharmony_ci{
5038c2ecf20Sopenharmony_ci	struct fib_nh_common *nhc;
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	nhc = nexthop_path_fdb_result(nh, hash >> 1);
5068c2ecf20Sopenharmony_ci	if (unlikely(!nhc))
5078c2ecf20Sopenharmony_ci		return false;
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	switch (nhc->nhc_gw_family) {
5108c2ecf20Sopenharmony_ci	case AF_INET:
5118c2ecf20Sopenharmony_ci		rdst->remote_ip.sin.sin_addr.s_addr = nhc->nhc_gw.ipv4;
5128c2ecf20Sopenharmony_ci		rdst->remote_ip.sa.sa_family = AF_INET;
5138c2ecf20Sopenharmony_ci		break;
5148c2ecf20Sopenharmony_ci	case AF_INET6:
5158c2ecf20Sopenharmony_ci		rdst->remote_ip.sin6.sin6_addr = nhc->nhc_gw.ipv6;
5168c2ecf20Sopenharmony_ci		rdst->remote_ip.sa.sa_family = AF_INET6;
5178c2ecf20Sopenharmony_ci		break;
5188c2ecf20Sopenharmony_ci	}
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	return true;
5218c2ecf20Sopenharmony_ci}
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci#endif
524