vxlan.h 11 KB

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