udp_tunnel.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 socket *sock, struct rtable *rt,
  66. struct sk_buff *skb, __be32 src, __be32 dst,
  67. __u8 tos, __u8 ttl, __be16 df, __be16 src_port,
  68. __be16 dst_port, bool xnet);
  69. #if IS_ENABLED(CONFIG_IPV6)
  70. int udp_tunnel6_xmit_skb(struct socket *sock, struct dst_entry *dst,
  71. struct sk_buff *skb, struct net_device *dev,
  72. struct in6_addr *saddr, struct in6_addr *daddr,
  73. __u8 prio, __u8 ttl, __be16 src_port,
  74. __be16 dst_port);
  75. #endif
  76. void udp_tunnel_sock_release(struct socket *sock);
  77. static inline struct sk_buff *udp_tunnel_handle_offloads(struct sk_buff *skb,
  78. bool udp_csum)
  79. {
  80. int type = udp_csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
  81. return iptunnel_handle_offloads(skb, udp_csum, type);
  82. }
  83. static inline void udp_tunnel_gro_complete(struct sk_buff *skb, int nhoff)
  84. {
  85. struct udphdr *uh;
  86. uh = (struct udphdr *)(skb->data + nhoff - sizeof(struct udphdr));
  87. skb_shinfo(skb)->gso_type |= uh->check ?
  88. SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
  89. }
  90. static inline void udp_tunnel_encap_enable(struct socket *sock)
  91. {
  92. #if IS_ENABLED(CONFIG_IPV6)
  93. if (sock->sk->sk_family == PF_INET6)
  94. ipv6_stub->udpv6_encap_enable();
  95. else
  96. #endif
  97. udp_encap_enable();
  98. }
  99. #endif