udp_tunnel.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #ifndef __NET_UDP_TUNNEL_H
  2. #define __NET_UDP_TUNNEL_H
  3. #include <net/ip_tunnels.h>
  4. #include <net/udp.h>
  5. #if IS_ENABLED(CONFIG_IPV6)
  6. #include <net/ipv6.h>
  7. #include <net/addrconf.h>
  8. #endif
  9. struct udp_port_cfg {
  10. u8 family;
  11. /* Used only for kernel-created sockets */
  12. union {
  13. struct in_addr local_ip;
  14. #if IS_ENABLED(CONFIG_IPV6)
  15. struct in6_addr local_ip6;
  16. #endif
  17. };
  18. union {
  19. struct in_addr peer_ip;
  20. #if IS_ENABLED(CONFIG_IPV6)
  21. struct in6_addr peer_ip6;
  22. #endif
  23. };
  24. __be16 local_udp_port;
  25. __be16 peer_udp_port;
  26. unsigned int use_udp_checksums:1,
  27. use_udp6_tx_checksums:1,
  28. use_udp6_rx_checksums:1,
  29. ipv6_v6only:1;
  30. };
  31. int udp_sock_create4(struct net *net, struct udp_port_cfg *cfg,
  32. struct socket **sockp);
  33. #if IS_ENABLED(CONFIG_IPV6)
  34. int udp_sock_create6(struct net *net, struct udp_port_cfg *cfg,
  35. struct socket **sockp);
  36. #else
  37. static inline int udp_sock_create6(struct net *net, struct udp_port_cfg *cfg,
  38. struct socket **sockp)
  39. {
  40. return 0;
  41. }
  42. #endif
  43. static inline int udp_sock_create(struct net *net,
  44. struct udp_port_cfg *cfg,
  45. struct socket **sockp)
  46. {
  47. if (cfg->family == AF_INET)
  48. return udp_sock_create4(net, cfg, sockp);
  49. if (cfg->family == AF_INET6)
  50. return udp_sock_create6(net, cfg, sockp);
  51. return -EPFNOSUPPORT;
  52. }
  53. typedef int (*udp_tunnel_encap_rcv_t)(struct sock *sk, struct sk_buff *skb);
  54. typedef void (*udp_tunnel_encap_destroy_t)(struct sock *sk);
  55. typedef struct sk_buff **(*udp_tunnel_gro_receive_t)(struct sock *sk,
  56. struct sk_buff **head,
  57. struct sk_buff *skb);
  58. typedef int (*udp_tunnel_gro_complete_t)(struct sock *sk, struct sk_buff *skb,
  59. int nhoff);
  60. struct udp_tunnel_sock_cfg {
  61. void *sk_user_data; /* user data used by encap_rcv call back */
  62. /* Used for setting up udp_sock fields, see udp.h for details */
  63. __u8 encap_type;
  64. udp_tunnel_encap_rcv_t encap_rcv;
  65. udp_tunnel_encap_destroy_t encap_destroy;
  66. udp_tunnel_gro_receive_t gro_receive;
  67. udp_tunnel_gro_complete_t gro_complete;
  68. };
  69. /* Setup the given (UDP) sock to receive UDP encapsulated packets */
  70. void setup_udp_tunnel_sock(struct net *net, struct socket *sock,
  71. struct udp_tunnel_sock_cfg *sock_cfg);
  72. /* -- List of parsable UDP tunnel types --
  73. *
  74. * Adding to this list will result in serious debate. The main issue is
  75. * that this list is essentially a list of workarounds for either poorly
  76. * designed tunnels, or poorly designed device offloads.
  77. *
  78. * The parsing supported via these types should really be used for Rx
  79. * traffic only as the network stack will have already inserted offsets for
  80. * the location of the headers in the skb. In addition any ports that are
  81. * pushed should be kept within the namespace without leaking to other
  82. * devices such as VFs or other ports on the same device.
  83. *
  84. * It is strongly encouraged to use CHECKSUM_COMPLETE for Rx to avoid the
  85. * need to use this for Rx checksum offload. It should not be necessary to
  86. * call this function to perform Tx offloads on outgoing traffic.
  87. */
  88. enum udp_parsable_tunnel_type {
  89. UDP_TUNNEL_TYPE_VXLAN, /* RFC 7348 */
  90. UDP_TUNNEL_TYPE_GENEVE, /* draft-ietf-nvo3-geneve */
  91. UDP_TUNNEL_TYPE_VXLAN_GPE, /* draft-ietf-nvo3-vxlan-gpe */
  92. };
  93. struct udp_tunnel_info {
  94. unsigned short type;
  95. sa_family_t sa_family;
  96. __be16 port;
  97. };
  98. /* Notify network devices of offloadable types */
  99. void udp_tunnel_push_rx_port(struct net_device *dev, struct socket *sock,
  100. unsigned short type);
  101. void udp_tunnel_notify_add_rx_port(struct socket *sock, unsigned short type);
  102. void udp_tunnel_notify_del_rx_port(struct socket *sock, unsigned short type);
  103. static inline void udp_tunnel_get_rx_info(struct net_device *dev)
  104. {
  105. ASSERT_RTNL();
  106. call_netdevice_notifiers(NETDEV_UDP_TUNNEL_PUSH_INFO, dev);
  107. }
  108. /* Transmit the skb using UDP encapsulation. */
  109. void udp_tunnel_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
  110. __be32 src, __be32 dst, __u8 tos, __u8 ttl,
  111. __be16 df, __be16 src_port, __be16 dst_port,
  112. bool xnet, bool nocheck);
  113. #if IS_ENABLED(CONFIG_IPV6)
  114. int udp_tunnel6_xmit_skb(struct dst_entry *dst, struct sock *sk,
  115. struct sk_buff *skb,
  116. struct net_device *dev, struct in6_addr *saddr,
  117. struct in6_addr *daddr,
  118. __u8 prio, __u8 ttl, __be32 label,
  119. __be16 src_port, __be16 dst_port, bool nocheck);
  120. #endif
  121. void udp_tunnel_sock_release(struct socket *sock);
  122. struct metadata_dst *udp_tun_rx_dst(struct sk_buff *skb, unsigned short family,
  123. __be16 flags, __be64 tunnel_id,
  124. int md_size);
  125. #ifdef CONFIG_INET
  126. static inline int udp_tunnel_handle_offloads(struct sk_buff *skb, bool udp_csum)
  127. {
  128. int type = udp_csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
  129. return iptunnel_handle_offloads(skb, type);
  130. }
  131. #endif
  132. static inline void udp_tunnel_encap_enable(struct socket *sock)
  133. {
  134. #if IS_ENABLED(CONFIG_IPV6)
  135. if (sock->sk->sk_family == PF_INET6)
  136. ipv6_stub->udpv6_encap_enable();
  137. else
  138. #endif
  139. udp_encap_enable();
  140. }
  141. #endif