1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2012a5729SPravin B Shelar #ifndef __NET_VXLAN_H
3012a5729SPravin B Shelar #define __NET_VXLAN_H 1
4012a5729SPravin B Shelar
55f35227eSJesse Gross #include <linux/if_vlan.h>
6a3a48de5SIdo Schimmel #include <linux/rhashtable-types.h>
786a98057SAlexander Duyck #include <net/udp_tunnel.h>
8ee122c79SThomas Graf #include <net/dst_metadata.h>
95ff4ff4fSIdo Schimmel #include <net/rtnetlink.h>
109a997353SPetr Machata #include <net/switchdev.h>
111274e1ccSRoopa Prabhu #include <net/nexthop.h>
12012a5729SPravin B Shelar
13bea96410SMoshe Shemesh #define IANA_VXLAN_UDP_PORT 4789
14ed618bd8SHao Chen #define IANA_VXLAN_GPE_UDP_PORT 4790
15bea96410SMoshe Shemesh
16828788acSJiri Benc /* VXLAN protocol (RFC 7348) header:
173511494cSThomas Graf * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
18828788acSJiri Benc * |R|R|R|R|I|R|R|R| Reserved |
193511494cSThomas Graf * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
203511494cSThomas Graf * | VXLAN Network Identifier (VNI) | Reserved |
213511494cSThomas Graf * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
223511494cSThomas Graf *
23828788acSJiri Benc * I = VXLAN Network Identifier (VNI) present.
24828788acSJiri Benc */
25828788acSJiri Benc struct vxlanhdr {
26828788acSJiri Benc __be32 vx_flags;
27828788acSJiri Benc __be32 vx_vni;
28828788acSJiri Benc };
29828788acSJiri Benc
30828788acSJiri Benc /* VXLAN header flags. */
3154bfd872SJiri Benc #define VXLAN_HF_VNI cpu_to_be32(BIT(27))
32828788acSJiri Benc
33828788acSJiri Benc #define VXLAN_N_VID (1u << 24)
34828788acSJiri Benc #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
3554bfd872SJiri Benc #define VXLAN_VNI_MASK cpu_to_be32(VXLAN_VID_MASK << 8)
36828788acSJiri Benc #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
37828788acSJiri Benc
38828788acSJiri Benc #define VNI_HASH_BITS 10
39828788acSJiri Benc #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
40828788acSJiri Benc #define FDB_HASH_BITS 8
41828788acSJiri Benc #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
42828788acSJiri Benc
43828788acSJiri Benc /* Remote checksum offload for VXLAN (VXLAN_F_REMCSUM_[RT]X):
44828788acSJiri Benc * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45828788acSJiri Benc * |R|R|R|R|I|R|R|R|R|R|C| Reserved |
46828788acSJiri Benc * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47828788acSJiri Benc * | VXLAN Network Identifier (VNI) |O| Csum start |
48828788acSJiri Benc * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49828788acSJiri Benc *
50828788acSJiri Benc * C = Remote checksum offload bit. When set indicates that the
51828788acSJiri Benc * remote checksum offload data is present.
52828788acSJiri Benc *
53828788acSJiri Benc * O = Offset bit. Indicates the checksum offset relative to
54828788acSJiri Benc * checksum start.
55828788acSJiri Benc *
56828788acSJiri Benc * Csum start = Checksum start divided by two.
57828788acSJiri Benc *
58828788acSJiri Benc * http://tools.ietf.org/html/draft-herbert-vxlan-rco
59828788acSJiri Benc */
60828788acSJiri Benc
61828788acSJiri Benc /* VXLAN-RCO header flags. */
6254bfd872SJiri Benc #define VXLAN_HF_RCO cpu_to_be32(BIT(21))
63828788acSJiri Benc
64828788acSJiri Benc /* Remote checksum offload header option */
6554bfd872SJiri Benc #define VXLAN_RCO_MASK cpu_to_be32(0x7f) /* Last byte of vni field */
6654bfd872SJiri Benc #define VXLAN_RCO_UDP cpu_to_be32(0x80) /* Indicate UDP RCO (TCP when not set *) */
67828788acSJiri Benc #define VXLAN_RCO_SHIFT 1 /* Left shift of start */
68828788acSJiri Benc #define VXLAN_RCO_SHIFT_MASK ((1 << VXLAN_RCO_SHIFT) - 1)
6954bfd872SJiri Benc #define VXLAN_MAX_REMCSUM_START (0x7f << VXLAN_RCO_SHIFT)
70828788acSJiri Benc
71828788acSJiri Benc /*
72828788acSJiri Benc * VXLAN Group Based Policy Extension (VXLAN_F_GBP):
73828788acSJiri Benc * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
74828788acSJiri Benc * |G|R|R|R|I|R|R|R|R|D|R|R|A|R|R|R| Group Policy ID |
75828788acSJiri Benc * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
76828788acSJiri Benc * | VXLAN Network Identifier (VNI) | Reserved |
77828788acSJiri Benc * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
78828788acSJiri Benc *
79828788acSJiri Benc * G = Group Policy ID present.
80828788acSJiri Benc *
813511494cSThomas Graf * D = Don't Learn bit. When set, this bit indicates that the egress
823511494cSThomas Graf * VTEP MUST NOT learn the source address of the encapsulated frame.
833511494cSThomas Graf *
843511494cSThomas Graf * A = Indicates that the group policy has already been applied to
853511494cSThomas Graf * this packet. Policies MUST NOT be applied by devices when the
863511494cSThomas Graf * A bit is set.
873511494cSThomas Graf *
88828788acSJiri Benc * https://tools.ietf.org/html/draft-smith-vxlan-group-policy
893511494cSThomas Graf */
903511494cSThomas Graf struct vxlanhdr_gbp {
910e715d6fSJiri Benc u8 vx_flags;
923511494cSThomas Graf #ifdef __LITTLE_ENDIAN_BITFIELD
930e715d6fSJiri Benc u8 reserved_flags1:3,
943511494cSThomas Graf policy_applied:1,
953511494cSThomas Graf reserved_flags2:2,
963511494cSThomas Graf dont_learn:1,
973511494cSThomas Graf reserved_flags3:1;
983511494cSThomas Graf #elif defined(__BIG_ENDIAN_BITFIELD)
990e715d6fSJiri Benc u8 reserved_flags1:1,
1003511494cSThomas Graf dont_learn:1,
1013511494cSThomas Graf reserved_flags2:2,
1023511494cSThomas Graf policy_applied:1,
1033511494cSThomas Graf reserved_flags3:3;
1043511494cSThomas Graf #else
1053511494cSThomas Graf #error "Please fix <asm/byteorder.h>"
1063511494cSThomas Graf #endif
1073511494cSThomas Graf __be16 policy_id;
1083511494cSThomas Graf __be32 vx_vni;
1093511494cSThomas Graf };
1103511494cSThomas Graf
111828788acSJiri Benc /* VXLAN-GBP header flags. */
11254bfd872SJiri Benc #define VXLAN_HF_GBP cpu_to_be32(BIT(31))
113828788acSJiri Benc
11454bfd872SJiri Benc #define VXLAN_GBP_USED_BITS (VXLAN_HF_GBP | cpu_to_be32(0xFFFFFF))
1153511494cSThomas Graf
1163511494cSThomas Graf /* skb->mark mapping
1173511494cSThomas Graf *
1183511494cSThomas Graf * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1193511494cSThomas Graf * |R|R|R|R|R|R|R|R|R|D|R|R|A|R|R|R| Group Policy ID |
1203511494cSThomas Graf * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1213511494cSThomas Graf */
1223511494cSThomas Graf #define VXLAN_GBP_DONT_LEARN (BIT(6) << 16)
1233511494cSThomas Graf #define VXLAN_GBP_POLICY_APPLIED (BIT(3) << 16)
1243511494cSThomas Graf #define VXLAN_GBP_ID_MASK (0xFFFF)
1253511494cSThomas Graf
12613e6ce98SXin Long #define VXLAN_GBP_MASK (VXLAN_GBP_DONT_LEARN | VXLAN_GBP_POLICY_APPLIED | \
12713e6ce98SXin Long VXLAN_GBP_ID_MASK)
12813e6ce98SXin Long
129e1e5314dSJiri Benc /*
130e1e5314dSJiri Benc * VXLAN Generic Protocol Extension (VXLAN_F_GPE):
131e1e5314dSJiri Benc * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
132e1e5314dSJiri Benc * |R|R|Ver|I|P|R|O| Reserved |Next Protocol |
133e1e5314dSJiri Benc * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
134e1e5314dSJiri Benc * | VXLAN Network Identifier (VNI) | Reserved |
135e1e5314dSJiri Benc * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
136e1e5314dSJiri Benc *
137e1e5314dSJiri Benc * Ver = Version. Indicates VXLAN GPE protocol version.
138e1e5314dSJiri Benc *
139e1e5314dSJiri Benc * P = Next Protocol Bit. The P bit is set to indicate that the
140e1e5314dSJiri Benc * Next Protocol field is present.
141e1e5314dSJiri Benc *
142e1e5314dSJiri Benc * O = OAM Flag Bit. The O bit is set to indicate that the packet
143e1e5314dSJiri Benc * is an OAM packet.
144e1e5314dSJiri Benc *
145e1e5314dSJiri Benc * Next Protocol = This 8 bit field indicates the protocol header
146e1e5314dSJiri Benc * immediately following the VXLAN GPE header.
147e1e5314dSJiri Benc *
148e1e5314dSJiri Benc * https://tools.ietf.org/html/draft-ietf-nvo3-vxlan-gpe-01
149e1e5314dSJiri Benc */
150e1e5314dSJiri Benc
151e1e5314dSJiri Benc struct vxlanhdr_gpe {
152e1e5314dSJiri Benc #if defined(__LITTLE_ENDIAN_BITFIELD)
153e1e5314dSJiri Benc u8 oam_flag:1,
154e1e5314dSJiri Benc reserved_flags1:1,
155e1e5314dSJiri Benc np_applied:1,
156e1e5314dSJiri Benc instance_applied:1,
157e1e5314dSJiri Benc version:2,
158e1e5314dSJiri Benc reserved_flags2:2;
159e1e5314dSJiri Benc #elif defined(__BIG_ENDIAN_BITFIELD)
160e1e5314dSJiri Benc u8 reserved_flags2:2,
161e1e5314dSJiri Benc version:2,
162e1e5314dSJiri Benc instance_applied:1,
163e1e5314dSJiri Benc np_applied:1,
164e1e5314dSJiri Benc reserved_flags1:1,
165e1e5314dSJiri Benc oam_flag:1;
166e1e5314dSJiri Benc #endif
167e1e5314dSJiri Benc u8 reserved_flags3;
168e1e5314dSJiri Benc u8 reserved_flags4;
169e1e5314dSJiri Benc u8 next_protocol;
170e1e5314dSJiri Benc __be32 vx_vni;
171e1e5314dSJiri Benc };
172e1e5314dSJiri Benc
173e1e5314dSJiri Benc /* VXLAN-GPE header flags. */
174e1e5314dSJiri Benc #define VXLAN_HF_VER cpu_to_be32(BIT(29) | BIT(28))
175e1e5314dSJiri Benc #define VXLAN_HF_NP cpu_to_be32(BIT(26))
176e1e5314dSJiri Benc #define VXLAN_HF_OAM cpu_to_be32(BIT(24))
177e1e5314dSJiri Benc
178e1e5314dSJiri Benc #define VXLAN_GPE_USED_BITS (VXLAN_HF_VER | VXLAN_HF_NP | VXLAN_HF_OAM | \
179e1e5314dSJiri Benc cpu_to_be32(0xff))
180e1e5314dSJiri Benc
1813511494cSThomas Graf struct vxlan_metadata {
1823511494cSThomas Graf u32 gbp;
1833511494cSThomas Graf };
1843511494cSThomas Graf
185012a5729SPravin B Shelar /* per UDP socket information */
186012a5729SPravin B Shelar struct vxlan_sock {
187012a5729SPravin B Shelar struct hlist_node hlist;
188012a5729SPravin B Shelar struct socket *sock;
189012a5729SPravin B Shelar struct hlist_head vni_list[VNI_HASH_SIZE];
19066af846fSReshetova, Elena refcount_t refcnt;
191dfd8645eSTom Herbert u32 flags;
192012a5729SPravin B Shelar };
193012a5729SPravin B Shelar
1940dfbdf41SThomas Graf union vxlan_addr {
1950dfbdf41SThomas Graf struct sockaddr_in sin;
1960dfbdf41SThomas Graf struct sockaddr_in6 sin6;
1970dfbdf41SThomas Graf struct sockaddr sa;
1980dfbdf41SThomas Graf };
1990dfbdf41SThomas Graf
2000dfbdf41SThomas Graf struct vxlan_rdst {
2010dfbdf41SThomas Graf union vxlan_addr remote_ip;
2020dfbdf41SThomas Graf __be16 remote_port;
2030efe1173SPetr Machata u8 offloaded:1;
20454bfd872SJiri Benc __be32 remote_vni;
2050dfbdf41SThomas Graf u32 remote_ifindex;
2060ce1822cSTaehee Yoo struct net_device *remote_dev;
2070dfbdf41SThomas Graf struct list_head list;
2080dfbdf41SThomas Graf struct rcu_head rcu;
2090c1d70afSPaolo Abeni struct dst_cache dst_cache;
2100dfbdf41SThomas Graf };
2110dfbdf41SThomas Graf
2120dfbdf41SThomas Graf struct vxlan_config {
2130dfbdf41SThomas Graf union vxlan_addr remote_ip;
2140dfbdf41SThomas Graf union vxlan_addr saddr;
21554bfd872SJiri Benc __be32 vni;
2160dfbdf41SThomas Graf int remote_ifindex;
2170dfbdf41SThomas Graf int mtu;
2180dfbdf41SThomas Graf __be16 dst_port;
2190e715d6fSJiri Benc u16 port_min;
2200e715d6fSJiri Benc u16 port_max;
2210e715d6fSJiri Benc u8 tos;
2220e715d6fSJiri Benc u8 ttl;
223e7f70af1SDaniel Borkmann __be32 label;
224*c6e9dba3SAlce Lafranque enum ifla_vxlan_label_policy label_policy;
2250dfbdf41SThomas Graf u32 flags;
2260dfbdf41SThomas Graf unsigned long age_interval;
2270dfbdf41SThomas Graf unsigned int addrmax;
2280dfbdf41SThomas Graf bool no_share;
229b4d30697SStefano Brivio enum ifla_vxlan_df df;
2300dfbdf41SThomas Graf };
2310dfbdf41SThomas Graf
2324095e0e1SNikolay Aleksandrov enum {
2334095e0e1SNikolay Aleksandrov VXLAN_VNI_STATS_RX,
2344095e0e1SNikolay Aleksandrov VXLAN_VNI_STATS_RX_DROPS,
2354095e0e1SNikolay Aleksandrov VXLAN_VNI_STATS_RX_ERRORS,
2364095e0e1SNikolay Aleksandrov VXLAN_VNI_STATS_TX,
2374095e0e1SNikolay Aleksandrov VXLAN_VNI_STATS_TX_DROPS,
2384095e0e1SNikolay Aleksandrov VXLAN_VNI_STATS_TX_ERRORS,
2394095e0e1SNikolay Aleksandrov };
2404095e0e1SNikolay Aleksandrov
2414095e0e1SNikolay Aleksandrov struct vxlan_vni_stats {
2424095e0e1SNikolay Aleksandrov u64 rx_packets;
2434095e0e1SNikolay Aleksandrov u64 rx_bytes;
2444095e0e1SNikolay Aleksandrov u64 rx_drops;
2454095e0e1SNikolay Aleksandrov u64 rx_errors;
2464095e0e1SNikolay Aleksandrov u64 tx_packets;
2474095e0e1SNikolay Aleksandrov u64 tx_bytes;
2484095e0e1SNikolay Aleksandrov u64 tx_drops;
2494095e0e1SNikolay Aleksandrov u64 tx_errors;
2504095e0e1SNikolay Aleksandrov };
2514095e0e1SNikolay Aleksandrov
2524095e0e1SNikolay Aleksandrov struct vxlan_vni_stats_pcpu {
2534095e0e1SNikolay Aleksandrov struct vxlan_vni_stats stats;
2544095e0e1SNikolay Aleksandrov struct u64_stats_sync syncp;
2554095e0e1SNikolay Aleksandrov };
2564095e0e1SNikolay Aleksandrov
25769e76661SJiri Benc struct vxlan_dev_node {
25869e76661SJiri Benc struct hlist_node hlist;
25969e76661SJiri Benc struct vxlan_dev *vxlan;
26069e76661SJiri Benc };
26169e76661SJiri Benc
262f9c4bb0bSRoopa Prabhu struct vxlan_vni_node {
263f9c4bb0bSRoopa Prabhu struct rhash_head vnode;
264f9c4bb0bSRoopa Prabhu struct vxlan_dev_node hlist4; /* vni hash table for IPv4 socket */
265f9c4bb0bSRoopa Prabhu #if IS_ENABLED(CONFIG_IPV6)
266f9c4bb0bSRoopa Prabhu struct vxlan_dev_node hlist6; /* vni hash table for IPv6 socket */
267f9c4bb0bSRoopa Prabhu #endif
268f9c4bb0bSRoopa Prabhu struct list_head vlist;
269f9c4bb0bSRoopa Prabhu __be32 vni;
270f9c4bb0bSRoopa Prabhu union vxlan_addr remote_ip; /* default remote ip for this vni */
2714095e0e1SNikolay Aleksandrov struct vxlan_vni_stats_pcpu __percpu *stats;
272f9c4bb0bSRoopa Prabhu
273f9c4bb0bSRoopa Prabhu struct rcu_head rcu;
274f9c4bb0bSRoopa Prabhu };
275f9c4bb0bSRoopa Prabhu
276f9c4bb0bSRoopa Prabhu struct vxlan_vni_group {
277f9c4bb0bSRoopa Prabhu struct rhashtable vni_hash;
278f9c4bb0bSRoopa Prabhu struct list_head vni_list;
279f9c4bb0bSRoopa Prabhu u32 num_vnis;
280f9c4bb0bSRoopa Prabhu };
281f9c4bb0bSRoopa Prabhu
2820dfbdf41SThomas Graf /* Pseudo network device */
2830dfbdf41SThomas Graf struct vxlan_dev {
28469e76661SJiri Benc struct vxlan_dev_node hlist4; /* vni hash table for IPv4 socket */
28569e76661SJiri Benc #if IS_ENABLED(CONFIG_IPV6)
28669e76661SJiri Benc struct vxlan_dev_node hlist6; /* vni hash table for IPv6 socket */
28769e76661SJiri Benc #endif
2880dfbdf41SThomas Graf struct list_head next; /* vxlan's per namespace list */
289c6fcc4fcSpravin shelar struct vxlan_sock __rcu *vn4_sock; /* listening socket for IPv4 */
290b1be00a6SJiri Benc #if IS_ENABLED(CONFIG_IPV6)
291c6fcc4fcSpravin shelar struct vxlan_sock __rcu *vn6_sock; /* listening socket for IPv6 */
292b1be00a6SJiri Benc #endif
2930dfbdf41SThomas Graf struct net_device *dev;
2940dfbdf41SThomas Graf struct net *net; /* netns for packet i/o */
2950dfbdf41SThomas Graf struct vxlan_rdst default_dst; /* default destination */
2960dfbdf41SThomas Graf
2970dfbdf41SThomas Graf struct timer_list age_timer;
298fe1e0713SLitao jiao spinlock_t hash_lock[FDB_HASH_SIZE];
2990dfbdf41SThomas Graf unsigned int addrcnt;
30058ce31ccSTom Herbert struct gro_cells gro_cells;
3010dfbdf41SThomas Graf
3020dfbdf41SThomas Graf struct vxlan_config cfg;
3030dfbdf41SThomas Graf
304f9c4bb0bSRoopa Prabhu struct vxlan_vni_group __rcu *vnigrp;
305f9c4bb0bSRoopa Prabhu
3060dfbdf41SThomas Graf struct hlist_head fdb_head[FDB_HASH_SIZE];
307a3a48de5SIdo Schimmel
308a3a48de5SIdo Schimmel struct rhashtable mdb_tbl;
309a3a48de5SIdo Schimmel struct hlist_head mdb_list;
310a3a48de5SIdo Schimmel unsigned int mdb_seq;
3110dfbdf41SThomas Graf };
3120dfbdf41SThomas Graf
313359a0ea9STom Herbert #define VXLAN_F_LEARN 0x01
314359a0ea9STom Herbert #define VXLAN_F_PROXY 0x02
315359a0ea9STom Herbert #define VXLAN_F_RSC 0x04
316359a0ea9STom Herbert #define VXLAN_F_L2MISS 0x08
317359a0ea9STom Herbert #define VXLAN_F_L3MISS 0x10
318359a0ea9STom Herbert #define VXLAN_F_IPV6 0x20
3196ceb31caSAlexander Duyck #define VXLAN_F_UDP_ZERO_CSUM_TX 0x40
320359a0ea9STom Herbert #define VXLAN_F_UDP_ZERO_CSUM6_TX 0x80
321359a0ea9STom Herbert #define VXLAN_F_UDP_ZERO_CSUM6_RX 0x100
322dfd8645eSTom Herbert #define VXLAN_F_REMCSUM_TX 0x200
323dfd8645eSTom Herbert #define VXLAN_F_REMCSUM_RX 0x400
3243511494cSThomas Graf #define VXLAN_F_GBP 0x800
3250ace2ca8STom Herbert #define VXLAN_F_REMCSUM_NOPARTIAL 0x1000
326ee122c79SThomas Graf #define VXLAN_F_COLLECT_METADATA 0x2000
327e1e5314dSJiri Benc #define VXLAN_F_GPE 0x4000
3280f22a3c6SMatthias Schiffer #define VXLAN_F_IPV6_LINKLOCAL 0x8000
32972f6d71eSHangbin Liu #define VXLAN_F_TTL_INHERIT 0x10000
330f9c4bb0bSRoopa Prabhu #define VXLAN_F_VNIFILTER 0x20000
331bc6c6b01SIdo Schimmel #define VXLAN_F_MDB 0x40000
33269474a8aSVladimir Nikishkin #define VXLAN_F_LOCALBYPASS 0x80000
333359a0ea9STom Herbert
334d299ce14SSimon Horman /* Flags that are used in the receive path. These flags must match in
335af33c1adSTom Herbert * order for a socket to be shareable
336af33c1adSTom Herbert */
337af33c1adSTom Herbert #define VXLAN_F_RCV_FLAGS (VXLAN_F_GBP | \
338e1e5314dSJiri Benc VXLAN_F_GPE | \
339af33c1adSTom Herbert VXLAN_F_UDP_ZERO_CSUM6_RX | \
3400ace2ca8STom Herbert VXLAN_F_REMCSUM_RX | \
341ee122c79SThomas Graf VXLAN_F_REMCSUM_NOPARTIAL | \
342f9c4bb0bSRoopa Prabhu VXLAN_F_COLLECT_METADATA | \
343f9c4bb0bSRoopa Prabhu VXLAN_F_VNIFILTER)
344ac5132d1SThomas Graf
345e1e5314dSJiri Benc /* Flags that can be set together with VXLAN_F_GPE. */
346e1e5314dSJiri Benc #define VXLAN_F_ALLOWED_GPE (VXLAN_F_GPE | \
347e1e5314dSJiri Benc VXLAN_F_IPV6 | \
3480f22a3c6SMatthias Schiffer VXLAN_F_IPV6_LINKLOCAL | \
349e1e5314dSJiri Benc VXLAN_F_UDP_ZERO_CSUM_TX | \
350e1e5314dSJiri Benc VXLAN_F_UDP_ZERO_CSUM6_TX | \
351e1e5314dSJiri Benc VXLAN_F_UDP_ZERO_CSUM6_RX | \
352f9c4bb0bSRoopa Prabhu VXLAN_F_COLLECT_METADATA | \
35369474a8aSVladimir Nikishkin VXLAN_F_VNIFILTER | \
35469474a8aSVladimir Nikishkin VXLAN_F_LOCALBYPASS)
355e1e5314dSJiri Benc
3560dfbdf41SThomas Graf struct net_device *vxlan_dev_create(struct net *net, const char *name,
3570dfbdf41SThomas Graf u8 name_assign_type, struct vxlan_config *conf);
3580dfbdf41SThomas Graf
vxlan_features_check(struct sk_buff * skb,netdev_features_t features)3595f35227eSJesse Gross static inline netdev_features_t vxlan_features_check(struct sk_buff *skb,
3605f35227eSJesse Gross netdev_features_t features)
36111bf7828SJoe Stringer {
3625f35227eSJesse Gross u8 l4_hdr = 0;
3635f35227eSJesse Gross
3645f35227eSJesse Gross if (!skb->encapsulation)
3655f35227eSJesse Gross return features;
3665f35227eSJesse Gross
3675f35227eSJesse Gross switch (vlan_get_protocol(skb)) {
3685f35227eSJesse Gross case htons(ETH_P_IP):
3695f35227eSJesse Gross l4_hdr = ip_hdr(skb)->protocol;
3705f35227eSJesse Gross break;
3715f35227eSJesse Gross case htons(ETH_P_IPV6):
3725f35227eSJesse Gross l4_hdr = ipv6_hdr(skb)->nexthdr;
3735f35227eSJesse Gross break;
3745f35227eSJesse Gross default:
3755ef7e0baSLuis de Bethencourt return features;
3765f35227eSJesse Gross }
3775f35227eSJesse Gross
3785f35227eSJesse Gross if ((l4_hdr == IPPROTO_UDP) &&
37911bf7828SJoe Stringer (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
38011bf7828SJoe Stringer skb->inner_protocol != htons(ETH_P_TEB) ||
38111bf7828SJoe Stringer (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
382af67eb9eSAlexander Duyck sizeof(struct udphdr) + sizeof(struct vxlanhdr)) ||
383af67eb9eSAlexander Duyck (skb->ip_summed != CHECKSUM_NONE &&
384af67eb9eSAlexander Duyck !can_checksum_protocol(features, inner_eth_hdr(skb)->h_proto))))
385a188222bSTom Herbert return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
38611bf7828SJoe Stringer
3875f35227eSJesse Gross return features;
38811bf7828SJoe Stringer }
38923e62de3SJoe Stringer
vxlan_headroom(u32 flags)39094d166c5SJiri Benc static inline int vxlan_headroom(u32 flags)
39194d166c5SJiri Benc {
39294d166c5SJiri Benc /* VXLAN: IP4/6 header + UDP + VXLAN + Ethernet header */
39394d166c5SJiri Benc /* VXLAN-GPE: IP4/6 header + UDP + VXLAN */
39494d166c5SJiri Benc return (flags & VXLAN_F_IPV6 ? sizeof(struct ipv6hdr) :
39594d166c5SJiri Benc sizeof(struct iphdr)) +
39694d166c5SJiri Benc sizeof(struct udphdr) + sizeof(struct vxlanhdr) +
39794d166c5SJiri Benc (flags & VXLAN_F_GPE ? 0 : ETH_HLEN);
39894d166c5SJiri Benc }
399e6cd988cSJoseph Gasparakis
vxlan_hdr(struct sk_buff * skb)400d4ac05ffSJiri Benc static inline struct vxlanhdr *vxlan_hdr(struct sk_buff *skb)
401d4ac05ffSJiri Benc {
402d4ac05ffSJiri Benc return (struct vxlanhdr *)(udp_hdr(skb) + 1);
403d4ac05ffSJiri Benc }
404d4ac05ffSJiri Benc
vxlan_vni(__be32 vni_field)40554bfd872SJiri Benc static inline __be32 vxlan_vni(__be32 vni_field)
40654bfd872SJiri Benc {
40754bfd872SJiri Benc #if defined(__BIG_ENDIAN)
4085692d7eaSJiri Benc return (__force __be32)((__force u32)vni_field >> 8);
40954bfd872SJiri Benc #else
4105692d7eaSJiri Benc return (__force __be32)((__force u32)(vni_field & VXLAN_VNI_MASK) << 8);
41154bfd872SJiri Benc #endif
41254bfd872SJiri Benc }
41354bfd872SJiri Benc
vxlan_vni_field(__be32 vni)41454bfd872SJiri Benc static inline __be32 vxlan_vni_field(__be32 vni)
41554bfd872SJiri Benc {
41654bfd872SJiri Benc #if defined(__BIG_ENDIAN)
4175692d7eaSJiri Benc return (__force __be32)((__force u32)vni << 8);
41854bfd872SJiri Benc #else
4195692d7eaSJiri Benc return (__force __be32)((__force u32)vni >> 8);
42054bfd872SJiri Benc #endif
42154bfd872SJiri Benc }
42254bfd872SJiri Benc
vxlan_rco_start(__be32 vni_field)42354bfd872SJiri Benc static inline size_t vxlan_rco_start(__be32 vni_field)
42454bfd872SJiri Benc {
42554bfd872SJiri Benc return be32_to_cpu(vni_field & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
42654bfd872SJiri Benc }
42754bfd872SJiri Benc
vxlan_rco_offset(__be32 vni_field)42854bfd872SJiri Benc static inline size_t vxlan_rco_offset(__be32 vni_field)
42954bfd872SJiri Benc {
43054bfd872SJiri Benc return (vni_field & VXLAN_RCO_UDP) ?
43154bfd872SJiri Benc offsetof(struct udphdr, check) :
43254bfd872SJiri Benc offsetof(struct tcphdr, check);
43354bfd872SJiri Benc }
43454bfd872SJiri Benc
vxlan_compute_rco(unsigned int start,unsigned int offset)43554bfd872SJiri Benc static inline __be32 vxlan_compute_rco(unsigned int start, unsigned int offset)
43654bfd872SJiri Benc {
43754bfd872SJiri Benc __be32 vni_field = cpu_to_be32(start >> VXLAN_RCO_SHIFT);
43854bfd872SJiri Benc
43954bfd872SJiri Benc if (offset == offsetof(struct udphdr, check))
44054bfd872SJiri Benc vni_field |= VXLAN_RCO_UDP;
44154bfd872SJiri Benc return vni_field;
44254bfd872SJiri Benc }
44354bfd872SJiri Benc
vxlan_get_sk_family(struct vxlan_sock * vs)444705cc62fSJiri Benc static inline unsigned short vxlan_get_sk_family(struct vxlan_sock *vs)
445705cc62fSJiri Benc {
446705cc62fSJiri Benc return vs->sock->sk->sk_family;
447705cc62fSJiri Benc }
44848e92c44SJiri Benc
449cca45e05SIdo Schimmel #if IS_ENABLED(CONFIG_IPV6)
450cca45e05SIdo Schimmel
vxlan_addr_any(const union vxlan_addr * ipa)451cca45e05SIdo Schimmel static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
452cca45e05SIdo Schimmel {
453cca45e05SIdo Schimmel if (ipa->sa.sa_family == AF_INET6)
454cca45e05SIdo Schimmel return ipv6_addr_any(&ipa->sin6.sin6_addr);
455cca45e05SIdo Schimmel else
456cca45e05SIdo Schimmel return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
457cca45e05SIdo Schimmel }
458cca45e05SIdo Schimmel
vxlan_addr_multicast(const union vxlan_addr * ipa)459cca45e05SIdo Schimmel static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
460cca45e05SIdo Schimmel {
461cca45e05SIdo Schimmel if (ipa->sa.sa_family == AF_INET6)
462cca45e05SIdo Schimmel return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
463cca45e05SIdo Schimmel else
464842841ecSDave Taht return ipv4_is_multicast(ipa->sin.sin_addr.s_addr);
465cca45e05SIdo Schimmel }
466cca45e05SIdo Schimmel
467cca45e05SIdo Schimmel #else /* !IS_ENABLED(CONFIG_IPV6) */
468cca45e05SIdo Schimmel
vxlan_addr_any(const union vxlan_addr * ipa)469cca45e05SIdo Schimmel static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
470cca45e05SIdo Schimmel {
471cca45e05SIdo Schimmel return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
472cca45e05SIdo Schimmel }
473cca45e05SIdo Schimmel
vxlan_addr_multicast(const union vxlan_addr * ipa)474cca45e05SIdo Schimmel static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
475cca45e05SIdo Schimmel {
476842841ecSDave Taht return ipv4_is_multicast(ipa->sin.sin_addr.s_addr);
477cca45e05SIdo Schimmel }
478cca45e05SIdo Schimmel
479cca45e05SIdo Schimmel #endif /* IS_ENABLED(CONFIG_IPV6) */
480cca45e05SIdo Schimmel
netif_is_vxlan(const struct net_device * dev)4815ff4ff4fSIdo Schimmel static inline bool netif_is_vxlan(const struct net_device *dev)
4825ff4ff4fSIdo Schimmel {
4835ff4ff4fSIdo Schimmel return dev->rtnl_link_ops &&
4845ff4ff4fSIdo Schimmel !strcmp(dev->rtnl_link_ops->kind, "vxlan");
4855ff4ff4fSIdo Schimmel }
4865ff4ff4fSIdo Schimmel
4879a997353SPetr Machata struct switchdev_notifier_vxlan_fdb_info {
4889a997353SPetr Machata struct switchdev_notifier_info info; /* must be first */
4899a997353SPetr Machata union vxlan_addr remote_ip;
4909a997353SPetr Machata __be16 remote_port;
4919a997353SPetr Machata __be32 remote_vni;
4929a997353SPetr Machata u32 remote_ifindex;
4939a997353SPetr Machata u8 eth_addr[ETH_ALEN];
4949a997353SPetr Machata __be32 vni;
4950efe1173SPetr Machata bool offloaded;
49645598c1cSPetr Machata bool added_by_user;
4979a997353SPetr Machata };
4989a997353SPetr Machata
4991941f1d6SPetr Machata #if IS_ENABLED(CONFIG_VXLAN)
5001941f1d6SPetr Machata int vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
5011941f1d6SPetr Machata struct switchdev_notifier_vxlan_fdb_info *fdb_info);
5024f89f5b5SPetr Machata int vxlan_fdb_replay(const struct net_device *dev, __be32 vni,
5034c59b7d1SPetr Machata struct notifier_block *nb,
5044c59b7d1SPetr Machata struct netlink_ext_ack *extack);
505e5ff4b19SPetr Machata void vxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni);
5064f89f5b5SPetr Machata
5071941f1d6SPetr Machata #else
5081941f1d6SPetr Machata static inline int
vxlan_fdb_find_uc(struct net_device * dev,const u8 * mac,__be32 vni,struct switchdev_notifier_vxlan_fdb_info * fdb_info)5091941f1d6SPetr Machata vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
5101941f1d6SPetr Machata struct switchdev_notifier_vxlan_fdb_info *fdb_info)
5111941f1d6SPetr Machata {
5121941f1d6SPetr Machata return -ENOENT;
5131941f1d6SPetr Machata }
5144f89f5b5SPetr Machata
vxlan_fdb_replay(const struct net_device * dev,__be32 vni,struct notifier_block * nb,struct netlink_ext_ack * extack)5154f89f5b5SPetr Machata static inline int vxlan_fdb_replay(const struct net_device *dev, __be32 vni,
5164c59b7d1SPetr Machata struct notifier_block *nb,
5174c59b7d1SPetr Machata struct netlink_ext_ack *extack)
5184f89f5b5SPetr Machata {
5194f89f5b5SPetr Machata return -EOPNOTSUPP;
5204f89f5b5SPetr Machata }
521e5ff4b19SPetr Machata
522e5ff4b19SPetr Machata static inline void
vxlan_fdb_clear_offload(const struct net_device * dev,__be32 vni)523e5ff4b19SPetr Machata vxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni)
524e5ff4b19SPetr Machata {
525e5ff4b19SPetr Machata }
5261941f1d6SPetr Machata #endif
5271941f1d6SPetr Machata
vxlan_flag_attr_error(int attrtype,struct netlink_ext_ack * extack)52870fb0828SRoopa Prabhu static inline void vxlan_flag_attr_error(int attrtype,
52970fb0828SRoopa Prabhu struct netlink_ext_ack *extack)
53070fb0828SRoopa Prabhu {
53170fb0828SRoopa Prabhu #define VXLAN_FLAG(flg) \
53270fb0828SRoopa Prabhu case IFLA_VXLAN_##flg: \
53370fb0828SRoopa Prabhu NL_SET_ERR_MSG_MOD(extack, \
53470fb0828SRoopa Prabhu "cannot change " #flg " flag"); \
53570fb0828SRoopa Prabhu break
53670fb0828SRoopa Prabhu switch (attrtype) {
53770fb0828SRoopa Prabhu VXLAN_FLAG(TTL_INHERIT);
53870fb0828SRoopa Prabhu VXLAN_FLAG(LEARNING);
53970fb0828SRoopa Prabhu VXLAN_FLAG(PROXY);
54070fb0828SRoopa Prabhu VXLAN_FLAG(RSC);
54170fb0828SRoopa Prabhu VXLAN_FLAG(L2MISS);
54270fb0828SRoopa Prabhu VXLAN_FLAG(L3MISS);
54370fb0828SRoopa Prabhu VXLAN_FLAG(COLLECT_METADATA);
54470fb0828SRoopa Prabhu VXLAN_FLAG(UDP_ZERO_CSUM6_TX);
54570fb0828SRoopa Prabhu VXLAN_FLAG(UDP_ZERO_CSUM6_RX);
54670fb0828SRoopa Prabhu VXLAN_FLAG(REMCSUM_TX);
54770fb0828SRoopa Prabhu VXLAN_FLAG(REMCSUM_RX);
54870fb0828SRoopa Prabhu VXLAN_FLAG(GBP);
54970fb0828SRoopa Prabhu VXLAN_FLAG(GPE);
55070fb0828SRoopa Prabhu VXLAN_FLAG(REMCSUM_NOPARTIAL);
55170fb0828SRoopa Prabhu default:
55270fb0828SRoopa Prabhu NL_SET_ERR_MSG_MOD(extack, \
55370fb0828SRoopa Prabhu "cannot change flag");
55470fb0828SRoopa Prabhu break;
55570fb0828SRoopa Prabhu }
55670fb0828SRoopa Prabhu #undef VXLAN_FLAG
55770fb0828SRoopa Prabhu }
55870fb0828SRoopa Prabhu
vxlan_fdb_nh_path_select(struct nexthop * nh,u32 hash,struct vxlan_rdst * rdst)5591274e1ccSRoopa Prabhu static inline bool vxlan_fdb_nh_path_select(struct nexthop *nh,
5600756384fSBenjamin Poirier u32 hash,
5611274e1ccSRoopa Prabhu struct vxlan_rdst *rdst)
5621274e1ccSRoopa Prabhu {
5631274e1ccSRoopa Prabhu struct fib_nh_common *nhc;
5641274e1ccSRoopa Prabhu
5650756384fSBenjamin Poirier nhc = nexthop_path_fdb_result(nh, hash >> 1);
5661274e1ccSRoopa Prabhu if (unlikely(!nhc))
5671274e1ccSRoopa Prabhu return false;
5681274e1ccSRoopa Prabhu
5691274e1ccSRoopa Prabhu switch (nhc->nhc_gw_family) {
5701274e1ccSRoopa Prabhu case AF_INET:
5711274e1ccSRoopa Prabhu rdst->remote_ip.sin.sin_addr.s_addr = nhc->nhc_gw.ipv4;
5721274e1ccSRoopa Prabhu rdst->remote_ip.sa.sa_family = AF_INET;
5731274e1ccSRoopa Prabhu break;
5741274e1ccSRoopa Prabhu case AF_INET6:
5751274e1ccSRoopa Prabhu rdst->remote_ip.sin6.sin6_addr = nhc->nhc_gw.ipv6;
5761274e1ccSRoopa Prabhu rdst->remote_ip.sa.sa_family = AF_INET6;
5771274e1ccSRoopa Prabhu break;
5781274e1ccSRoopa Prabhu }
5791274e1ccSRoopa Prabhu
5801274e1ccSRoopa Prabhu return true;
5811274e1ccSRoopa Prabhu }
5821274e1ccSRoopa Prabhu
vxlan_build_gbp_hdr(struct vxlanhdr * vxh,const struct vxlan_metadata * md)583c641e927SGavin Li static inline void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, const struct vxlan_metadata *md)
584c641e927SGavin Li {
585c641e927SGavin Li struct vxlanhdr_gbp *gbp;
586c641e927SGavin Li
587c641e927SGavin Li if (!md->gbp)
588c641e927SGavin Li return;
589c641e927SGavin Li
590c641e927SGavin Li gbp = (struct vxlanhdr_gbp *)vxh;
591c641e927SGavin Li vxh->vx_flags |= VXLAN_HF_GBP;
592c641e927SGavin Li
593c641e927SGavin Li if (md->gbp & VXLAN_GBP_DONT_LEARN)
594c641e927SGavin Li gbp->dont_learn = 1;
595c641e927SGavin Li
596c641e927SGavin Li if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
597c641e927SGavin Li gbp->policy_applied = 1;
598c641e927SGavin Li
599c641e927SGavin Li gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
600c641e927SGavin Li }
601c641e927SGavin Li
60248e92c44SJiri Benc #endif
603