vxlan.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #define VNI_HASH_BITS 10
  11. #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
  12. /*
  13. * VXLAN Group Based Policy Extension:
  14. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  15. * |1|-|-|-|1|-|-|-|R|D|R|R|A|R|R|R| Group Policy ID |
  16. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  17. * | VXLAN Network Identifier (VNI) | Reserved |
  18. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  19. *
  20. * D = Don't Learn bit. When set, this bit indicates that the egress
  21. * VTEP MUST NOT learn the source address of the encapsulated frame.
  22. *
  23. * A = Indicates that the group policy has already been applied to
  24. * this packet. Policies MUST NOT be applied by devices when the
  25. * A bit is set.
  26. *
  27. * [0] https://tools.ietf.org/html/draft-smith-vxlan-group-policy
  28. */
  29. struct vxlanhdr_gbp {
  30. __u8 vx_flags;
  31. #ifdef __LITTLE_ENDIAN_BITFIELD
  32. __u8 reserved_flags1:3,
  33. policy_applied:1,
  34. reserved_flags2:2,
  35. dont_learn:1,
  36. reserved_flags3:1;
  37. #elif defined(__BIG_ENDIAN_BITFIELD)
  38. __u8 reserved_flags1:1,
  39. dont_learn:1,
  40. reserved_flags2:2,
  41. policy_applied:1,
  42. reserved_flags3:3;
  43. #else
  44. #error "Please fix <asm/byteorder.h>"
  45. #endif
  46. __be16 policy_id;
  47. __be32 vx_vni;
  48. };
  49. #define VXLAN_GBP_USED_BITS (VXLAN_HF_GBP | 0xFFFFFF)
  50. /* skb->mark mapping
  51. *
  52. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  53. * |R|R|R|R|R|R|R|R|R|D|R|R|A|R|R|R| Group Policy ID |
  54. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  55. */
  56. #define VXLAN_GBP_DONT_LEARN (BIT(6) << 16)
  57. #define VXLAN_GBP_POLICY_APPLIED (BIT(3) << 16)
  58. #define VXLAN_GBP_ID_MASK (0xFFFF)
  59. /* VXLAN protocol header:
  60. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  61. * |G|R|R|R|I|R|R|C| Reserved |
  62. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  63. * | VXLAN Network Identifier (VNI) | Reserved |
  64. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  65. *
  66. * G = 1 Group Policy (VXLAN-GBP)
  67. * I = 1 VXLAN Network Identifier (VNI) present
  68. * C = 1 Remote checksum offload (RCO)
  69. */
  70. struct vxlanhdr {
  71. __be32 vx_flags;
  72. __be32 vx_vni;
  73. };
  74. /* VXLAN header flags. */
  75. #define VXLAN_HF_RCO BIT(21)
  76. #define VXLAN_HF_VNI BIT(27)
  77. #define VXLAN_HF_GBP BIT(31)
  78. /* Remote checksum offload header option */
  79. #define VXLAN_RCO_MASK 0x7f /* Last byte of vni field */
  80. #define VXLAN_RCO_UDP 0x80 /* Indicate UDP RCO (TCP when not set *) */
  81. #define VXLAN_RCO_SHIFT 1 /* Left shift of start */
  82. #define VXLAN_RCO_SHIFT_MASK ((1 << VXLAN_RCO_SHIFT) - 1)
  83. #define VXLAN_MAX_REMCSUM_START (VXLAN_RCO_MASK << VXLAN_RCO_SHIFT)
  84. #define VXLAN_N_VID (1u << 24)
  85. #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
  86. #define VXLAN_VNI_MASK (VXLAN_VID_MASK << 8)
  87. #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
  88. #define VNI_HASH_BITS 10
  89. #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
  90. #define FDB_HASH_BITS 8
  91. #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
  92. struct vxlan_metadata {
  93. u32 gbp;
  94. };
  95. /* per UDP socket information */
  96. struct vxlan_sock {
  97. struct hlist_node hlist;
  98. struct work_struct del_work;
  99. struct socket *sock;
  100. struct rcu_head rcu;
  101. struct hlist_head vni_list[VNI_HASH_SIZE];
  102. atomic_t refcnt;
  103. struct udp_offload udp_offloads;
  104. u32 flags;
  105. };
  106. union vxlan_addr {
  107. struct sockaddr_in sin;
  108. struct sockaddr_in6 sin6;
  109. struct sockaddr sa;
  110. };
  111. struct vxlan_rdst {
  112. union vxlan_addr remote_ip;
  113. __be16 remote_port;
  114. u32 remote_vni;
  115. u32 remote_ifindex;
  116. struct list_head list;
  117. struct rcu_head rcu;
  118. };
  119. struct vxlan_config {
  120. union vxlan_addr remote_ip;
  121. union vxlan_addr saddr;
  122. u32 vni;
  123. int remote_ifindex;
  124. int mtu;
  125. __be16 dst_port;
  126. __u16 port_min;
  127. __u16 port_max;
  128. __u8 tos;
  129. __u8 ttl;
  130. u32 flags;
  131. unsigned long age_interval;
  132. unsigned int addrmax;
  133. bool no_share;
  134. };
  135. /* Pseudo network device */
  136. struct vxlan_dev {
  137. struct hlist_node hlist; /* vni hash table */
  138. struct list_head next; /* vxlan's per namespace list */
  139. struct vxlan_sock *vn4_sock; /* listening socket for IPv4 */
  140. #if IS_ENABLED(CONFIG_IPV6)
  141. struct vxlan_sock *vn6_sock; /* listening socket for IPv6 */
  142. #endif
  143. struct net_device *dev;
  144. struct net *net; /* netns for packet i/o */
  145. struct vxlan_rdst default_dst; /* default destination */
  146. u32 flags; /* VXLAN_F_* in vxlan.h */
  147. struct timer_list age_timer;
  148. spinlock_t hash_lock;
  149. unsigned int addrcnt;
  150. struct gro_cells gro_cells;
  151. struct vxlan_config cfg;
  152. struct hlist_head fdb_head[FDB_HASH_SIZE];
  153. };
  154. #define VXLAN_F_LEARN 0x01
  155. #define VXLAN_F_PROXY 0x02
  156. #define VXLAN_F_RSC 0x04
  157. #define VXLAN_F_L2MISS 0x08
  158. #define VXLAN_F_L3MISS 0x10
  159. #define VXLAN_F_IPV6 0x20
  160. #define VXLAN_F_UDP_CSUM 0x40
  161. #define VXLAN_F_UDP_ZERO_CSUM6_TX 0x80
  162. #define VXLAN_F_UDP_ZERO_CSUM6_RX 0x100
  163. #define VXLAN_F_REMCSUM_TX 0x200
  164. #define VXLAN_F_REMCSUM_RX 0x400
  165. #define VXLAN_F_GBP 0x800
  166. #define VXLAN_F_REMCSUM_NOPARTIAL 0x1000
  167. #define VXLAN_F_COLLECT_METADATA 0x2000
  168. /* Flags that are used in the receive path. These flags must match in
  169. * order for a socket to be shareable
  170. */
  171. #define VXLAN_F_RCV_FLAGS (VXLAN_F_GBP | \
  172. VXLAN_F_UDP_ZERO_CSUM6_RX | \
  173. VXLAN_F_REMCSUM_RX | \
  174. VXLAN_F_REMCSUM_NOPARTIAL | \
  175. VXLAN_F_COLLECT_METADATA)
  176. struct net_device *vxlan_dev_create(struct net *net, const char *name,
  177. u8 name_assign_type, struct vxlan_config *conf);
  178. static inline __be16 vxlan_dev_dst_port(struct vxlan_dev *vxlan,
  179. unsigned short family)
  180. {
  181. #if IS_ENABLED(CONFIG_IPV6)
  182. if (family == AF_INET6)
  183. return inet_sk(vxlan->vn6_sock->sock->sk)->inet_sport;
  184. #endif
  185. return inet_sk(vxlan->vn4_sock->sock->sk)->inet_sport;
  186. }
  187. static inline netdev_features_t vxlan_features_check(struct sk_buff *skb,
  188. netdev_features_t features)
  189. {
  190. u8 l4_hdr = 0;
  191. if (!skb->encapsulation)
  192. return features;
  193. switch (vlan_get_protocol(skb)) {
  194. case htons(ETH_P_IP):
  195. l4_hdr = ip_hdr(skb)->protocol;
  196. break;
  197. case htons(ETH_P_IPV6):
  198. l4_hdr = ipv6_hdr(skb)->nexthdr;
  199. break;
  200. default:
  201. return features;;
  202. }
  203. if ((l4_hdr == IPPROTO_UDP) &&
  204. (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
  205. skb->inner_protocol != htons(ETH_P_TEB) ||
  206. (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
  207. sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
  208. return features & ~(NETIF_F_ALL_CSUM | NETIF_F_GSO_MASK);
  209. return features;
  210. }
  211. /* IP header + UDP + VXLAN + Ethernet header */
  212. #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
  213. /* IPv6 header + UDP + VXLAN + Ethernet header */
  214. #define VXLAN6_HEADROOM (40 + 8 + 8 + 14)
  215. #if IS_ENABLED(CONFIG_VXLAN)
  216. void vxlan_get_rx_port(struct net_device *netdev);
  217. #else
  218. static inline void vxlan_get_rx_port(struct net_device *netdev)
  219. {
  220. }
  221. #endif
  222. static inline unsigned short vxlan_get_sk_family(struct vxlan_sock *vs)
  223. {
  224. return vs->sock->sk->sk_family;
  225. }
  226. #endif