udp_tunnel.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /* Transmit the skb using UDP encapsulation. */
  73. void udp_tunnel_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
  74. __be32 src, __be32 dst, __u8 tos, __u8 ttl,
  75. __be16 df, __be16 src_port, __be16 dst_port,
  76. bool xnet, bool nocheck);
  77. #if IS_ENABLED(CONFIG_IPV6)
  78. int udp_tunnel6_xmit_skb(struct dst_entry *dst, struct sock *sk,
  79. struct sk_buff *skb,
  80. struct net_device *dev, struct in6_addr *saddr,
  81. struct in6_addr *daddr,
  82. __u8 prio, __u8 ttl, __be32 label,
  83. __be16 src_port, __be16 dst_port, bool nocheck);
  84. #endif
  85. void udp_tunnel_sock_release(struct socket *sock);
  86. struct metadata_dst *udp_tun_rx_dst(struct sk_buff *skb, unsigned short family,
  87. __be16 flags, __be64 tunnel_id,
  88. int md_size);
  89. static inline struct sk_buff *udp_tunnel_handle_offloads(struct sk_buff *skb,
  90. bool udp_csum)
  91. {
  92. int type = udp_csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
  93. return iptunnel_handle_offloads(skb, type);
  94. }
  95. static inline void udp_tunnel_gro_complete(struct sk_buff *skb, int nhoff)
  96. {
  97. struct udphdr *uh;
  98. uh = (struct udphdr *)(skb->data + nhoff - sizeof(struct udphdr));
  99. skb_shinfo(skb)->gso_type |= uh->check ?
  100. SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
  101. }
  102. static inline void udp_tunnel_encap_enable(struct socket *sock)
  103. {
  104. #if IS_ENABLED(CONFIG_IPV6)
  105. if (sock->sk->sk_family == PF_INET6)
  106. ipv6_stub->udpv6_encap_enable();
  107. else
  108. #endif
  109. udp_encap_enable();
  110. }
  111. #endif