udp_tunnel.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. struct udp_tunnel_sock_cfg {
  56. void *sk_user_data; /* user data used by encap_rcv call back */
  57. /* Used for setting up udp_sock fields, see udp.h for details */
  58. __u8 encap_type;
  59. udp_tunnel_encap_rcv_t encap_rcv;
  60. udp_tunnel_encap_destroy_t encap_destroy;
  61. };
  62. /* Setup the given (UDP) sock to receive UDP encapsulated packets */
  63. void setup_udp_tunnel_sock(struct net *net, struct socket *sock,
  64. struct udp_tunnel_sock_cfg *sock_cfg);
  65. /* Transmit the skb using UDP encapsulation. */
  66. int udp_tunnel_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
  67. __be32 src, __be32 dst, __u8 tos, __u8 ttl,
  68. __be16 df, __be16 src_port, __be16 dst_port,
  69. bool xnet, bool nocheck);
  70. #if IS_ENABLED(CONFIG_IPV6)
  71. int udp_tunnel6_xmit_skb(struct dst_entry *dst, struct sock *sk,
  72. struct sk_buff *skb,
  73. struct net_device *dev, struct in6_addr *saddr,
  74. struct in6_addr *daddr,
  75. __u8 prio, __u8 ttl, __be16 src_port,
  76. __be16 dst_port, bool nocheck);
  77. #endif
  78. void udp_tunnel_sock_release(struct socket *sock);
  79. struct metadata_dst *udp_tun_rx_dst(struct sk_buff *skb, unsigned short family,
  80. __be16 flags, __be64 tunnel_id,
  81. int md_size);
  82. static inline struct sk_buff *udp_tunnel_handle_offloads(struct sk_buff *skb,
  83. bool udp_csum)
  84. {
  85. int type = udp_csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
  86. return iptunnel_handle_offloads(skb, udp_csum, type);
  87. }
  88. static inline void udp_tunnel_gro_complete(struct sk_buff *skb, int nhoff)
  89. {
  90. struct udphdr *uh;
  91. uh = (struct udphdr *)(skb->data + nhoff - sizeof(struct udphdr));
  92. skb_shinfo(skb)->gso_type |= uh->check ?
  93. SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
  94. }
  95. static inline void udp_tunnel_encap_enable(struct socket *sock)
  96. {
  97. #if IS_ENABLED(CONFIG_IPV6)
  98. if (sock->sk->sk_family == PF_INET6)
  99. ipv6_stub->udpv6_encap_enable();
  100. else
  101. #endif
  102. udp_encap_enable();
  103. }
  104. #endif