udp_tunnel.h 5.1 KB

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