blob: c1c899c3a51be42f680fff5ce0f5c5e997652daa [file] [log] [blame]
Pravin B Shelar012a5722013-08-19 11:23:07 -07001#ifndef __NET_VXLAN_H
2#define __NET_VXLAN_H 1
3
Jesse Gross5f352272014-12-23 22:37:26 -08004#include <linux/ip.h>
5#include <linux/ipv6.h>
6#include <linux/if_vlan.h>
Pravin B Shelar012a5722013-08-19 11:23:07 -07007#include <linux/skbuff.h>
8#include <linux/netdevice.h>
9#include <linux/udp.h>
Thomas Grafee122c72015-07-21 10:43:58 +020010#include <net/dst_metadata.h>
Pravin B Shelar012a5722013-08-19 11:23:07 -070011
12#define VNI_HASH_BITS 10
13#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
14
Thomas Graf35114942015-01-15 03:53:55 +010015/*
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
22 *
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.
25 *
26 * A = Indicates that the group policy has already been applied to
27 * this packet. Policies MUST NOT be applied by devices when the
28 * A bit is set.
29 *
30 * [0] https://tools.ietf.org/html/draft-smith-vxlan-group-policy
31 */
32struct vxlanhdr_gbp {
33 __u8 vx_flags;
34#ifdef __LITTLE_ENDIAN_BITFIELD
35 __u8 reserved_flags1:3,
36 policy_applied:1,
37 reserved_flags2:2,
38 dont_learn:1,
39 reserved_flags3:1;
40#elif defined(__BIG_ENDIAN_BITFIELD)
41 __u8 reserved_flags1:1,
42 dont_learn:1,
43 reserved_flags2:2,
44 policy_applied:1,
45 reserved_flags3:3;
46#else
47#error "Please fix <asm/byteorder.h>"
48#endif
49 __be16 policy_id;
50 __be32 vx_vni;
51};
52
53#define VXLAN_GBP_USED_BITS (VXLAN_HF_GBP | 0xFFFFFF)
54
55/* skb->mark mapping
56 *
57 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 * |R|R|R|R|R|R|R|R|R|D|R|R|A|R|R|R| Group Policy ID |
59 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60 */
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)
64
65/* VXLAN protocol header:
66 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67 * |G|R|R|R|I|R|R|C| Reserved |
68 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69 * | VXLAN Network Identifier (VNI) | Reserved |
70 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71 *
72 * G = 1 Group Policy (VXLAN-GBP)
73 * I = 1 VXLAN Network Identifier (VNI) present
74 * C = 1 Remote checksum offload (RCO)
75 */
Joe Stringer11bf7822014-11-17 16:24:54 -080076struct vxlanhdr {
77 __be32 vx_flags;
78 __be32 vx_vni;
79};
80
Tom Herbert3bf39472015-01-08 12:31:18 -080081/* VXLAN header flags. */
Thomas Graf35114942015-01-15 03:53:55 +010082#define VXLAN_HF_RCO BIT(24)
83#define VXLAN_HF_VNI BIT(27)
84#define VXLAN_HF_GBP BIT(31)
Tom Herbertdfd86452015-01-12 17:00:38 -080085
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)
Tom Herbert3bf39472015-01-08 12:31:18 -080092
93#define VXLAN_N_VID (1u << 24)
94#define VXLAN_VID_MASK (VXLAN_N_VID - 1)
Alexey Kodanev40fb70f2015-03-13 19:13:53 +030095#define VXLAN_VNI_MASK (VXLAN_VID_MASK << 8)
Tom Herbert3bf39472015-01-08 12:31:18 -080096#define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
97
Thomas Graf0dfbdf42015-07-21 10:44:02 +020098#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)
102
Thomas Graf35114942015-01-15 03:53:55 +0100103struct vxlan_metadata {
Thomas Graf35114942015-01-15 03:53:55 +0100104 u32 gbp;
105};
106
Pravin B Shelar012a5722013-08-19 11:23:07 -0700107/* per UDP socket information */
108struct vxlan_sock {
109 struct hlist_node hlist;
Pravin B Shelar012a5722013-08-19 11:23:07 -0700110 struct work_struct del_work;
111 struct socket *sock;
112 struct rcu_head rcu;
113 struct hlist_head vni_list[VNI_HASH_SIZE];
114 atomic_t refcnt;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200115 struct udp_offload udp_offloads;
Tom Herbertdfd86452015-01-12 17:00:38 -0800116 u32 flags;
Pravin B Shelar012a5722013-08-19 11:23:07 -0700117};
118
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200119union vxlan_addr {
120 struct sockaddr_in sin;
121 struct sockaddr_in6 sin6;
122 struct sockaddr sa;
123};
124
125struct vxlan_rdst {
126 union vxlan_addr remote_ip;
127 __be16 remote_port;
128 u32 remote_vni;
129 u32 remote_ifindex;
130 struct list_head list;
131 struct rcu_head rcu;
132};
133
134struct vxlan_config {
135 union vxlan_addr remote_ip;
136 union vxlan_addr saddr;
137 u32 vni;
138 int remote_ifindex;
139 int mtu;
140 __be16 dst_port;
141 __u16 port_min;
142 __u16 port_max;
143 __u8 tos;
144 __u8 ttl;
145 u32 flags;
146 unsigned long age_interval;
147 unsigned int addrmax;
148 bool no_share;
149};
150
151/* Pseudo network device */
152struct vxlan_dev {
153 struct hlist_node hlist; /* vni hash table */
154 struct list_head next; /* vxlan's per namespace list */
Jiri Bencb1be00a2015-09-24 13:50:02 +0200155 struct vxlan_sock *vn4_sock; /* listening socket for IPv4 */
156#if IS_ENABLED(CONFIG_IPV6)
157 struct vxlan_sock *vn6_sock; /* listening socket for IPv6 */
158#endif
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200159 struct net_device *dev;
160 struct net *net; /* netns for packet i/o */
161 struct vxlan_rdst default_dst; /* default destination */
162 u32 flags; /* VXLAN_F_* in vxlan.h */
163
164 struct timer_list age_timer;
165 spinlock_t hash_lock;
166 unsigned int addrcnt;
Tom Herbert58ce31c2015-08-19 17:07:33 -0700167 struct gro_cells gro_cells;
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200168
169 struct vxlan_config cfg;
170
171 struct hlist_head fdb_head[FDB_HASH_SIZE];
172};
173
Tom Herbert359a0ea2014-06-04 17:20:29 -0700174#define VXLAN_F_LEARN 0x01
175#define VXLAN_F_PROXY 0x02
176#define VXLAN_F_RSC 0x04
177#define VXLAN_F_L2MISS 0x08
178#define VXLAN_F_L3MISS 0x10
179#define VXLAN_F_IPV6 0x20
180#define VXLAN_F_UDP_CSUM 0x40
181#define VXLAN_F_UDP_ZERO_CSUM6_TX 0x80
182#define VXLAN_F_UDP_ZERO_CSUM6_RX 0x100
Tom Herbertdfd86452015-01-12 17:00:38 -0800183#define VXLAN_F_REMCSUM_TX 0x200
184#define VXLAN_F_REMCSUM_RX 0x400
Thomas Graf35114942015-01-15 03:53:55 +0100185#define VXLAN_F_GBP 0x800
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800186#define VXLAN_F_REMCSUM_NOPARTIAL 0x1000
Thomas Grafee122c72015-07-21 10:43:58 +0200187#define VXLAN_F_COLLECT_METADATA 0x2000
Tom Herbert359a0ea2014-06-04 17:20:29 -0700188
Simon Hormand299ce12015-03-12 11:00:10 +0900189/* Flags that are used in the receive path. These flags must match in
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800190 * order for a socket to be shareable
191 */
192#define VXLAN_F_RCV_FLAGS (VXLAN_F_GBP | \
193 VXLAN_F_UDP_ZERO_CSUM6_RX | \
Tom Herbert0ace2ca2015-02-10 16:30:32 -0800194 VXLAN_F_REMCSUM_RX | \
Thomas Grafee122c72015-07-21 10:43:58 +0200195 VXLAN_F_REMCSUM_NOPARTIAL | \
Alexei Starovoitovda8b43c2015-08-04 22:51:07 -0700196 VXLAN_F_COLLECT_METADATA)
Thomas Grafac5132d2015-01-15 03:53:56 +0100197
Thomas Graf0dfbdf42015-07-21 10:44:02 +0200198struct net_device *vxlan_dev_create(struct net *net, const char *name,
199 u8 name_assign_type, struct vxlan_config *conf);
200
Jiri Bencb1be00a2015-09-24 13:50:02 +0200201static inline __be16 vxlan_dev_dst_port(struct vxlan_dev *vxlan,
202 unsigned short family)
Thomas Graf614732e2015-07-21 10:44:06 +0200203{
Jiri Bencb1be00a2015-09-24 13:50:02 +0200204#if IS_ENABLED(CONFIG_IPV6)
205 if (family == AF_INET6)
206 return inet_sk(vxlan->vn6_sock->sock->sk)->inet_sport;
207#endif
208 return inet_sk(vxlan->vn4_sock->sock->sk)->inet_sport;
Thomas Graf614732e2015-07-21 10:44:06 +0200209}
Pravin B Shelar49560532013-08-19 11:23:17 -0700210
Jesse Gross5f352272014-12-23 22:37:26 -0800211static inline netdev_features_t vxlan_features_check(struct sk_buff *skb,
212 netdev_features_t features)
Joe Stringer11bf7822014-11-17 16:24:54 -0800213{
Jesse Gross5f352272014-12-23 22:37:26 -0800214 u8 l4_hdr = 0;
215
216 if (!skb->encapsulation)
217 return features;
218
219 switch (vlan_get_protocol(skb)) {
220 case htons(ETH_P_IP):
221 l4_hdr = ip_hdr(skb)->protocol;
222 break;
223 case htons(ETH_P_IPV6):
224 l4_hdr = ipv6_hdr(skb)->nexthdr;
225 break;
226 default:
227 return features;;
228 }
229
230 if ((l4_hdr == IPPROTO_UDP) &&
Joe Stringer11bf7822014-11-17 16:24:54 -0800231 (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
232 skb->inner_protocol != htons(ETH_P_TEB) ||
233 (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
234 sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
Jesse Gross5f352272014-12-23 22:37:26 -0800235 return features & ~(NETIF_F_ALL_CSUM | NETIF_F_GSO_MASK);
Joe Stringer11bf7822014-11-17 16:24:54 -0800236
Jesse Gross5f352272014-12-23 22:37:26 -0800237 return features;
Joe Stringer11bf7822014-11-17 16:24:54 -0800238}
Joe Stringer23e62de2014-11-13 16:38:12 -0800239
Joseph Gasparakise6cd9882013-10-24 06:27:10 +0000240/* IP header + UDP + VXLAN + Ethernet header */
241#define VXLAN_HEADROOM (20 + 8 + 8 + 14)
242/* IPv6 header + UDP + VXLAN + Ethernet header */
243#define VXLAN6_HEADROOM (40 + 8 + 8 + 14)
244
245#if IS_ENABLED(CONFIG_VXLAN)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700246void vxlan_get_rx_port(struct net_device *netdev);
Joseph Gasparakise6cd9882013-10-24 06:27:10 +0000247#else
248static inline void vxlan_get_rx_port(struct net_device *netdev)
249{
250}
251#endif
Jiri Benc705cc622015-08-20 13:56:28 +0200252
253static inline unsigned short vxlan_get_sk_family(struct vxlan_sock *vs)
254{
255 return vs->sock->sk->sk_family;
256}
Jiri Benc48e92c42015-08-25 18:36:50 +0200257
258#endif