vxlan.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. struct vxlan_metadata {
  110. u32 gbp;
  111. };
  112. /* per UDP socket information */
  113. struct vxlan_sock {
  114. struct hlist_node hlist;
  115. struct work_struct del_work;
  116. struct socket *sock;
  117. struct rcu_head rcu;
  118. struct hlist_head vni_list[VNI_HASH_SIZE];
  119. atomic_t refcnt;
  120. struct udp_offload udp_offloads;
  121. u32 flags;
  122. };
  123. union vxlan_addr {
  124. struct sockaddr_in sin;
  125. struct sockaddr_in6 sin6;
  126. struct sockaddr sa;
  127. };
  128. struct vxlan_rdst {
  129. union vxlan_addr remote_ip;
  130. __be16 remote_port;
  131. __be32 remote_vni;
  132. u32 remote_ifindex;
  133. struct list_head list;
  134. struct rcu_head rcu;
  135. struct dst_cache dst_cache;
  136. };
  137. struct vxlan_config {
  138. union vxlan_addr remote_ip;
  139. union vxlan_addr saddr;
  140. __be32 vni;
  141. int remote_ifindex;
  142. int mtu;
  143. __be16 dst_port;
  144. u16 port_min;
  145. u16 port_max;
  146. u8 tos;
  147. u8 ttl;
  148. __be32 label;
  149. u32 flags;
  150. unsigned long age_interval;
  151. unsigned int addrmax;
  152. bool no_share;
  153. };
  154. /* Pseudo network device */
  155. struct vxlan_dev {
  156. struct hlist_node hlist; /* vni hash table */
  157. struct list_head next; /* vxlan's per namespace list */
  158. struct vxlan_sock *vn4_sock; /* listening socket for IPv4 */
  159. #if IS_ENABLED(CONFIG_IPV6)
  160. struct vxlan_sock *vn6_sock; /* listening socket for IPv6 */
  161. #endif
  162. struct net_device *dev;
  163. struct net *net; /* netns for packet i/o */
  164. struct vxlan_rdst default_dst; /* default destination */
  165. u32 flags; /* VXLAN_F_* in vxlan.h */
  166. struct timer_list age_timer;
  167. spinlock_t hash_lock;
  168. unsigned int addrcnt;
  169. struct gro_cells gro_cells;
  170. struct vxlan_config cfg;
  171. struct hlist_head fdb_head[FDB_HASH_SIZE];
  172. };
  173. #define VXLAN_F_LEARN 0x01
  174. #define VXLAN_F_PROXY 0x02
  175. #define VXLAN_F_RSC 0x04
  176. #define VXLAN_F_L2MISS 0x08
  177. #define VXLAN_F_L3MISS 0x10
  178. #define VXLAN_F_IPV6 0x20
  179. #define VXLAN_F_UDP_ZERO_CSUM_TX 0x40
  180. #define VXLAN_F_UDP_ZERO_CSUM6_TX 0x80
  181. #define VXLAN_F_UDP_ZERO_CSUM6_RX 0x100
  182. #define VXLAN_F_REMCSUM_TX 0x200
  183. #define VXLAN_F_REMCSUM_RX 0x400
  184. #define VXLAN_F_GBP 0x800
  185. #define VXLAN_F_REMCSUM_NOPARTIAL 0x1000
  186. #define VXLAN_F_COLLECT_METADATA 0x2000
  187. /* Flags that are used in the receive path. These flags must match in
  188. * order for a socket to be shareable
  189. */
  190. #define VXLAN_F_RCV_FLAGS (VXLAN_F_GBP | \
  191. VXLAN_F_UDP_ZERO_CSUM6_RX | \
  192. VXLAN_F_REMCSUM_RX | \
  193. VXLAN_F_REMCSUM_NOPARTIAL | \
  194. VXLAN_F_COLLECT_METADATA)
  195. struct net_device *vxlan_dev_create(struct net *net, const char *name,
  196. u8 name_assign_type, struct vxlan_config *conf);
  197. static inline __be16 vxlan_dev_dst_port(struct vxlan_dev *vxlan,
  198. unsigned short family)
  199. {
  200. #if IS_ENABLED(CONFIG_IPV6)
  201. if (family == AF_INET6)
  202. return inet_sk(vxlan->vn6_sock->sock->sk)->inet_sport;
  203. #endif
  204. return inet_sk(vxlan->vn4_sock->sock->sk)->inet_sport;
  205. }
  206. static inline netdev_features_t vxlan_features_check(struct sk_buff *skb,
  207. netdev_features_t features)
  208. {
  209. u8 l4_hdr = 0;
  210. if (!skb->encapsulation)
  211. return features;
  212. switch (vlan_get_protocol(skb)) {
  213. case htons(ETH_P_IP):
  214. l4_hdr = ip_hdr(skb)->protocol;
  215. break;
  216. case htons(ETH_P_IPV6):
  217. l4_hdr = ipv6_hdr(skb)->nexthdr;
  218. break;
  219. default:
  220. return features;;
  221. }
  222. if ((l4_hdr == IPPROTO_UDP) &&
  223. (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
  224. skb->inner_protocol != htons(ETH_P_TEB) ||
  225. (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
  226. sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
  227. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  228. return features;
  229. }
  230. /* IP header + UDP + VXLAN + Ethernet header */
  231. #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
  232. /* IPv6 header + UDP + VXLAN + Ethernet header */
  233. #define VXLAN6_HEADROOM (40 + 8 + 8 + 14)
  234. static inline struct vxlanhdr *vxlan_hdr(struct sk_buff *skb)
  235. {
  236. return (struct vxlanhdr *)(udp_hdr(skb) + 1);
  237. }
  238. static inline __be32 vxlan_vni(__be32 vni_field)
  239. {
  240. #if defined(__BIG_ENDIAN)
  241. return (__force __be32)((__force u32)vni_field >> 8);
  242. #else
  243. return (__force __be32)((__force u32)(vni_field & VXLAN_VNI_MASK) << 8);
  244. #endif
  245. }
  246. static inline __be32 vxlan_vni_field(__be32 vni)
  247. {
  248. #if defined(__BIG_ENDIAN)
  249. return (__force __be32)((__force u32)vni << 8);
  250. #else
  251. return (__force __be32)((__force u32)vni >> 8);
  252. #endif
  253. }
  254. static inline __be32 vxlan_tun_id_to_vni(__be64 tun_id)
  255. {
  256. #if defined(__BIG_ENDIAN)
  257. return (__force __be32)tun_id;
  258. #else
  259. return (__force __be32)((__force u64)tun_id >> 32);
  260. #endif
  261. }
  262. static inline __be64 vxlan_vni_to_tun_id(__be32 vni)
  263. {
  264. #if defined(__BIG_ENDIAN)
  265. return (__force __be64)vni;
  266. #else
  267. return (__force __be64)((u64)(__force u32)vni << 32);
  268. #endif
  269. }
  270. static inline size_t vxlan_rco_start(__be32 vni_field)
  271. {
  272. return be32_to_cpu(vni_field & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
  273. }
  274. static inline size_t vxlan_rco_offset(__be32 vni_field)
  275. {
  276. return (vni_field & VXLAN_RCO_UDP) ?
  277. offsetof(struct udphdr, check) :
  278. offsetof(struct tcphdr, check);
  279. }
  280. static inline __be32 vxlan_compute_rco(unsigned int start, unsigned int offset)
  281. {
  282. __be32 vni_field = cpu_to_be32(start >> VXLAN_RCO_SHIFT);
  283. if (offset == offsetof(struct udphdr, check))
  284. vni_field |= VXLAN_RCO_UDP;
  285. return vni_field;
  286. }
  287. #if IS_ENABLED(CONFIG_VXLAN)
  288. void vxlan_get_rx_port(struct net_device *netdev);
  289. #else
  290. static inline void vxlan_get_rx_port(struct net_device *netdev)
  291. {
  292. }
  293. #endif
  294. static inline unsigned short vxlan_get_sk_family(struct vxlan_sock *vs)
  295. {
  296. return vs->sock->sk->sk_family;
  297. }
  298. #endif