vxlan.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NET_VXLAN_H
  3. #define __NET_VXLAN_H 1
  4. #include <linux/if_vlan.h>
  5. #include <net/udp_tunnel.h>
  6. #include <net/dst_metadata.h>
  7. #include <net/udp_tunnel.h>
  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. */
  17. struct vxlanhdr {
  18. __be32 vx_flags;
  19. __be32 vx_vni;
  20. };
  21. /* VXLAN header flags. */
  22. #define VXLAN_HF_VNI cpu_to_be32(BIT(27))
  23. #define VXLAN_N_VID (1u << 24)
  24. #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
  25. #define VXLAN_VNI_MASK cpu_to_be32(VXLAN_VID_MASK << 8)
  26. #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
  27. #define VNI_HASH_BITS 10
  28. #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
  29. #define FDB_HASH_BITS 8
  30. #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
  31. /* Remote checksum offload for VXLAN (VXLAN_F_REMCSUM_[RT]X):
  32. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  33. * |R|R|R|R|I|R|R|R|R|R|C| Reserved |
  34. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  35. * | VXLAN Network Identifier (VNI) |O| Csum start |
  36. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  37. *
  38. * C = Remote checksum offload bit. When set indicates that the
  39. * remote checksum offload data is present.
  40. *
  41. * O = Offset bit. Indicates the checksum offset relative to
  42. * checksum start.
  43. *
  44. * Csum start = Checksum start divided by two.
  45. *
  46. * http://tools.ietf.org/html/draft-herbert-vxlan-rco
  47. */
  48. /* VXLAN-RCO header flags. */
  49. #define VXLAN_HF_RCO cpu_to_be32(BIT(21))
  50. /* Remote checksum offload header option */
  51. #define VXLAN_RCO_MASK cpu_to_be32(0x7f) /* Last byte of vni field */
  52. #define VXLAN_RCO_UDP cpu_to_be32(0x80) /* Indicate UDP RCO (TCP when not set *) */
  53. #define VXLAN_RCO_SHIFT 1 /* Left shift of start */
  54. #define VXLAN_RCO_SHIFT_MASK ((1 << VXLAN_RCO_SHIFT) - 1)
  55. #define VXLAN_MAX_REMCSUM_START (0x7f << VXLAN_RCO_SHIFT)
  56. /*
  57. * VXLAN Group Based Policy Extension (VXLAN_F_GBP):
  58. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  59. * |G|R|R|R|I|R|R|R|R|D|R|R|A|R|R|R| Group Policy ID |
  60. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  61. * | VXLAN Network Identifier (VNI) | Reserved |
  62. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  63. *
  64. * G = Group Policy ID present.
  65. *
  66. * D = Don't Learn bit. When set, this bit indicates that the egress
  67. * VTEP MUST NOT learn the source address of the encapsulated frame.
  68. *
  69. * A = Indicates that the group policy has already been applied to
  70. * this packet. Policies MUST NOT be applied by devices when the
  71. * A bit is set.
  72. *
  73. * https://tools.ietf.org/html/draft-smith-vxlan-group-policy
  74. */
  75. struct vxlanhdr_gbp {
  76. u8 vx_flags;
  77. #ifdef __LITTLE_ENDIAN_BITFIELD
  78. u8 reserved_flags1:3,
  79. policy_applied:1,
  80. reserved_flags2:2,
  81. dont_learn:1,
  82. reserved_flags3:1;
  83. #elif defined(__BIG_ENDIAN_BITFIELD)
  84. u8 reserved_flags1:1,
  85. dont_learn:1,
  86. reserved_flags2:2,
  87. policy_applied:1,
  88. reserved_flags3:3;
  89. #else
  90. #error "Please fix <asm/byteorder.h>"
  91. #endif
  92. __be16 policy_id;
  93. __be32 vx_vni;
  94. };
  95. /* VXLAN-GBP header flags. */
  96. #define VXLAN_HF_GBP cpu_to_be32(BIT(31))
  97. #define VXLAN_GBP_USED_BITS (VXLAN_HF_GBP | cpu_to_be32(0xFFFFFF))
  98. /* skb->mark mapping
  99. *
  100. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  101. * |R|R|R|R|R|R|R|R|R|D|R|R|A|R|R|R| Group Policy ID |
  102. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  103. */
  104. #define VXLAN_GBP_DONT_LEARN (BIT(6) << 16)
  105. #define VXLAN_GBP_POLICY_APPLIED (BIT(3) << 16)
  106. #define VXLAN_GBP_ID_MASK (0xFFFF)
  107. /*
  108. * VXLAN Generic Protocol Extension (VXLAN_F_GPE):
  109. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  110. * |R|R|Ver|I|P|R|O| Reserved |Next Protocol |
  111. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  112. * | VXLAN Network Identifier (VNI) | Reserved |
  113. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  114. *
  115. * Ver = Version. Indicates VXLAN GPE protocol version.
  116. *
  117. * P = Next Protocol Bit. The P bit is set to indicate that the
  118. * Next Protocol field is present.
  119. *
  120. * O = OAM Flag Bit. The O bit is set to indicate that the packet
  121. * is an OAM packet.
  122. *
  123. * Next Protocol = This 8 bit field indicates the protocol header
  124. * immediately following the VXLAN GPE header.
  125. *
  126. * https://tools.ietf.org/html/draft-ietf-nvo3-vxlan-gpe-01
  127. */
  128. struct vxlanhdr_gpe {
  129. #if defined(__LITTLE_ENDIAN_BITFIELD)
  130. u8 oam_flag:1,
  131. reserved_flags1:1,
  132. np_applied:1,
  133. instance_applied:1,
  134. version:2,
  135. reserved_flags2:2;
  136. #elif defined(__BIG_ENDIAN_BITFIELD)
  137. u8 reserved_flags2:2,
  138. version:2,
  139. instance_applied:1,
  140. np_applied:1,
  141. reserved_flags1:1,
  142. oam_flag:1;
  143. #endif
  144. u8 reserved_flags3;
  145. u8 reserved_flags4;
  146. u8 next_protocol;
  147. __be32 vx_vni;
  148. };
  149. /* VXLAN-GPE header flags. */
  150. #define VXLAN_HF_VER cpu_to_be32(BIT(29) | BIT(28))
  151. #define VXLAN_HF_NP cpu_to_be32(BIT(26))
  152. #define VXLAN_HF_OAM cpu_to_be32(BIT(24))
  153. #define VXLAN_GPE_USED_BITS (VXLAN_HF_VER | VXLAN_HF_NP | VXLAN_HF_OAM | \
  154. cpu_to_be32(0xff))
  155. struct vxlan_metadata {
  156. u32 gbp;
  157. };
  158. /* per UDP socket information */
  159. struct vxlan_sock {
  160. struct hlist_node hlist;
  161. struct socket *sock;
  162. struct hlist_head vni_list[VNI_HASH_SIZE];
  163. refcount_t refcnt;
  164. u32 flags;
  165. };
  166. union vxlan_addr {
  167. struct sockaddr_in sin;
  168. struct sockaddr_in6 sin6;
  169. struct sockaddr sa;
  170. };
  171. struct vxlan_rdst {
  172. union vxlan_addr remote_ip;
  173. __be16 remote_port;
  174. __be32 remote_vni;
  175. u32 remote_ifindex;
  176. struct list_head list;
  177. struct rcu_head rcu;
  178. struct dst_cache dst_cache;
  179. };
  180. struct vxlan_config {
  181. union vxlan_addr remote_ip;
  182. union vxlan_addr saddr;
  183. __be32 vni;
  184. int remote_ifindex;
  185. int mtu;
  186. __be16 dst_port;
  187. u16 port_min;
  188. u16 port_max;
  189. u8 tos;
  190. u8 ttl;
  191. __be32 label;
  192. u32 flags;
  193. unsigned long age_interval;
  194. unsigned int addrmax;
  195. bool no_share;
  196. };
  197. struct vxlan_dev_node {
  198. struct hlist_node hlist;
  199. struct vxlan_dev *vxlan;
  200. };
  201. /* Pseudo network device */
  202. struct vxlan_dev {
  203. struct vxlan_dev_node hlist4; /* vni hash table for IPv4 socket */
  204. #if IS_ENABLED(CONFIG_IPV6)
  205. struct vxlan_dev_node hlist6; /* vni hash table for IPv6 socket */
  206. #endif
  207. struct list_head next; /* vxlan's per namespace list */
  208. struct vxlan_sock __rcu *vn4_sock; /* listening socket for IPv4 */
  209. #if IS_ENABLED(CONFIG_IPV6)
  210. struct vxlan_sock __rcu *vn6_sock; /* listening socket for IPv6 */
  211. #endif
  212. struct net_device *dev;
  213. struct net *net; /* netns for packet i/o */
  214. struct vxlan_rdst default_dst; /* default destination */
  215. struct timer_list age_timer;
  216. spinlock_t hash_lock;
  217. unsigned int addrcnt;
  218. struct gro_cells gro_cells;
  219. struct vxlan_config cfg;
  220. struct hlist_head fdb_head[FDB_HASH_SIZE];
  221. };
  222. #define VXLAN_F_LEARN 0x01
  223. #define VXLAN_F_PROXY 0x02
  224. #define VXLAN_F_RSC 0x04
  225. #define VXLAN_F_L2MISS 0x08
  226. #define VXLAN_F_L3MISS 0x10
  227. #define VXLAN_F_IPV6 0x20
  228. #define VXLAN_F_UDP_ZERO_CSUM_TX 0x40
  229. #define VXLAN_F_UDP_ZERO_CSUM6_TX 0x80
  230. #define VXLAN_F_UDP_ZERO_CSUM6_RX 0x100
  231. #define VXLAN_F_REMCSUM_TX 0x200
  232. #define VXLAN_F_REMCSUM_RX 0x400
  233. #define VXLAN_F_GBP 0x800
  234. #define VXLAN_F_REMCSUM_NOPARTIAL 0x1000
  235. #define VXLAN_F_COLLECT_METADATA 0x2000
  236. #define VXLAN_F_GPE 0x4000
  237. #define VXLAN_F_IPV6_LINKLOCAL 0x8000
  238. #define VXLAN_F_TTL_INHERIT 0x10000
  239. /* Flags that are used in the receive path. These flags must match in
  240. * order for a socket to be shareable
  241. */
  242. #define VXLAN_F_RCV_FLAGS (VXLAN_F_GBP | \
  243. VXLAN_F_GPE | \
  244. VXLAN_F_UDP_ZERO_CSUM6_RX | \
  245. VXLAN_F_REMCSUM_RX | \
  246. VXLAN_F_REMCSUM_NOPARTIAL | \
  247. VXLAN_F_COLLECT_METADATA)
  248. /* Flags that can be set together with VXLAN_F_GPE. */
  249. #define VXLAN_F_ALLOWED_GPE (VXLAN_F_GPE | \
  250. VXLAN_F_IPV6 | \
  251. VXLAN_F_IPV6_LINKLOCAL | \
  252. VXLAN_F_UDP_ZERO_CSUM_TX | \
  253. VXLAN_F_UDP_ZERO_CSUM6_TX | \
  254. VXLAN_F_UDP_ZERO_CSUM6_RX | \
  255. VXLAN_F_COLLECT_METADATA)
  256. struct net_device *vxlan_dev_create(struct net *net, const char *name,
  257. u8 name_assign_type, struct vxlan_config *conf);
  258. static inline netdev_features_t vxlan_features_check(struct sk_buff *skb,
  259. netdev_features_t features)
  260. {
  261. u8 l4_hdr = 0;
  262. if (!skb->encapsulation)
  263. return features;
  264. switch (vlan_get_protocol(skb)) {
  265. case htons(ETH_P_IP):
  266. l4_hdr = ip_hdr(skb)->protocol;
  267. break;
  268. case htons(ETH_P_IPV6):
  269. l4_hdr = ipv6_hdr(skb)->nexthdr;
  270. break;
  271. default:
  272. return features;
  273. }
  274. if ((l4_hdr == IPPROTO_UDP) &&
  275. (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
  276. skb->inner_protocol != htons(ETH_P_TEB) ||
  277. (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
  278. sizeof(struct udphdr) + sizeof(struct vxlanhdr)) ||
  279. (skb->ip_summed != CHECKSUM_NONE &&
  280. !can_checksum_protocol(features, inner_eth_hdr(skb)->h_proto))))
  281. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  282. return features;
  283. }
  284. /* IP header + UDP + VXLAN + Ethernet header */
  285. #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
  286. /* IPv6 header + UDP + VXLAN + Ethernet header */
  287. #define VXLAN6_HEADROOM (40 + 8 + 8 + 14)
  288. static inline struct vxlanhdr *vxlan_hdr(struct sk_buff *skb)
  289. {
  290. return (struct vxlanhdr *)(udp_hdr(skb) + 1);
  291. }
  292. static inline __be32 vxlan_vni(__be32 vni_field)
  293. {
  294. #if defined(__BIG_ENDIAN)
  295. return (__force __be32)((__force u32)vni_field >> 8);
  296. #else
  297. return (__force __be32)((__force u32)(vni_field & VXLAN_VNI_MASK) << 8);
  298. #endif
  299. }
  300. static inline __be32 vxlan_vni_field(__be32 vni)
  301. {
  302. #if defined(__BIG_ENDIAN)
  303. return (__force __be32)((__force u32)vni << 8);
  304. #else
  305. return (__force __be32)((__force u32)vni >> 8);
  306. #endif
  307. }
  308. static inline size_t vxlan_rco_start(__be32 vni_field)
  309. {
  310. return be32_to_cpu(vni_field & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
  311. }
  312. static inline size_t vxlan_rco_offset(__be32 vni_field)
  313. {
  314. return (vni_field & VXLAN_RCO_UDP) ?
  315. offsetof(struct udphdr, check) :
  316. offsetof(struct tcphdr, check);
  317. }
  318. static inline __be32 vxlan_compute_rco(unsigned int start, unsigned int offset)
  319. {
  320. __be32 vni_field = cpu_to_be32(start >> VXLAN_RCO_SHIFT);
  321. if (offset == offsetof(struct udphdr, check))
  322. vni_field |= VXLAN_RCO_UDP;
  323. return vni_field;
  324. }
  325. static inline unsigned short vxlan_get_sk_family(struct vxlan_sock *vs)
  326. {
  327. return vs->sock->sk->sk_family;
  328. }
  329. #endif