vxlan.h 11 KB

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