udp_tunnel.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. };
  30. int udp_sock_create4(struct net *net, struct udp_port_cfg *cfg,
  31. struct socket **sockp);
  32. #if IS_ENABLED(CONFIG_IPV6)
  33. int udp_sock_create6(struct net *net, struct udp_port_cfg *cfg,
  34. struct socket **sockp);
  35. #else
  36. static inline int udp_sock_create6(struct net *net, struct udp_port_cfg *cfg,
  37. struct socket **sockp)
  38. {
  39. return 0;
  40. }
  41. #endif
  42. static inline int udp_sock_create(struct net *net,
  43. struct udp_port_cfg *cfg,
  44. struct socket **sockp)
  45. {
  46. if (cfg->family == AF_INET)
  47. return udp_sock_create4(net, cfg, sockp);
  48. if (cfg->family == AF_INET6)
  49. return udp_sock_create6(net, cfg, sockp);
  50. return -EPFNOSUPPORT;
  51. }
  52. typedef int (*udp_tunnel_encap_rcv_t)(struct sock *sk, struct sk_buff *skb);
  53. typedef void (*udp_tunnel_encap_destroy_t)(struct sock *sk);
  54. struct udp_tunnel_sock_cfg {
  55. void *sk_user_data; /* user data used by encap_rcv call back */
  56. /* Used for setting up udp_sock fields, see udp.h for details */
  57. __u8 encap_type;
  58. udp_tunnel_encap_rcv_t encap_rcv;
  59. udp_tunnel_encap_destroy_t encap_destroy;
  60. };
  61. /* Setup the given (UDP) sock to receive UDP encapsulated packets */
  62. void setup_udp_tunnel_sock(struct net *net, struct socket *sock,
  63. struct udp_tunnel_sock_cfg *sock_cfg);
  64. /* Transmit the skb using UDP encapsulation. */
  65. int udp_tunnel_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
  66. __be32 src, __be32 dst, __u8 tos, __u8 ttl,
  67. __be16 df, __be16 src_port, __be16 dst_port,
  68. bool xnet, bool nocheck);
  69. #if IS_ENABLED(CONFIG_IPV6)
  70. int udp_tunnel6_xmit_skb(struct dst_entry *dst, struct sock *sk,
  71. struct sk_buff *skb,
  72. struct net_device *dev, struct in6_addr *saddr,
  73. struct in6_addr *daddr,
  74. __u8 prio, __u8 ttl, __be16 src_port,
  75. __be16 dst_port, bool nocheck);
  76. #endif
  77. void udp_tunnel_sock_release(struct socket *sock);
  78. static inline struct sk_buff *udp_tunnel_handle_offloads(struct sk_buff *skb,
  79. bool udp_csum)
  80. {
  81. int type = udp_csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
  82. return iptunnel_handle_offloads(skb, udp_csum, type);
  83. }
  84. static inline void udp_tunnel_gro_complete(struct sk_buff *skb, int nhoff)
  85. {
  86. struct udphdr *uh;
  87. uh = (struct udphdr *)(skb->data + nhoff - sizeof(struct udphdr));
  88. skb_shinfo(skb)->gso_type |= uh->check ?
  89. SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
  90. }
  91. static inline void udp_tunnel_encap_enable(struct socket *sock)
  92. {
  93. #if IS_ENABLED(CONFIG_IPV6)
  94. if (sock->sk->sk_family == PF_INET6)
  95. ipv6_stub->udpv6_encap_enable();
  96. else
  97. #endif
  98. udp_encap_enable();
  99. }
  100. #endif