udp_tunnel.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <linux/module.h>
  2. #include <linux/errno.h>
  3. #include <linux/socket.h>
  4. #include <linux/udp.h>
  5. #include <linux/types.h>
  6. #include <linux/kernel.h>
  7. #include <net/dst_metadata.h>
  8. #include <net/net_namespace.h>
  9. #include <net/udp.h>
  10. #include <net/udp_tunnel.h>
  11. int udp_sock_create4(struct net *net, struct udp_port_cfg *cfg,
  12. struct socket **sockp)
  13. {
  14. int err;
  15. struct socket *sock = NULL;
  16. struct sockaddr_in udp_addr;
  17. err = sock_create_kern(net, AF_INET, SOCK_DGRAM, 0, &sock);
  18. if (err < 0)
  19. goto error;
  20. udp_addr.sin_family = AF_INET;
  21. udp_addr.sin_addr = cfg->local_ip;
  22. udp_addr.sin_port = cfg->local_udp_port;
  23. err = kernel_bind(sock, (struct sockaddr *)&udp_addr,
  24. sizeof(udp_addr));
  25. if (err < 0)
  26. goto error;
  27. if (cfg->peer_udp_port) {
  28. udp_addr.sin_family = AF_INET;
  29. udp_addr.sin_addr = cfg->peer_ip;
  30. udp_addr.sin_port = cfg->peer_udp_port;
  31. err = kernel_connect(sock, (struct sockaddr *)&udp_addr,
  32. sizeof(udp_addr), 0);
  33. if (err < 0)
  34. goto error;
  35. }
  36. sock->sk->sk_no_check_tx = !cfg->use_udp_checksums;
  37. *sockp = sock;
  38. return 0;
  39. error:
  40. if (sock) {
  41. kernel_sock_shutdown(sock, SHUT_RDWR);
  42. sock_release(sock);
  43. }
  44. *sockp = NULL;
  45. return err;
  46. }
  47. EXPORT_SYMBOL(udp_sock_create4);
  48. void setup_udp_tunnel_sock(struct net *net, struct socket *sock,
  49. struct udp_tunnel_sock_cfg *cfg)
  50. {
  51. struct sock *sk = sock->sk;
  52. /* Disable multicast loopback */
  53. inet_sk(sk)->mc_loop = 0;
  54. /* Enable CHECKSUM_UNNECESSARY to CHECKSUM_COMPLETE conversion */
  55. inet_inc_convert_csum(sk);
  56. rcu_assign_sk_user_data(sk, cfg->sk_user_data);
  57. udp_sk(sk)->encap_type = cfg->encap_type;
  58. udp_sk(sk)->encap_rcv = cfg->encap_rcv;
  59. udp_sk(sk)->encap_destroy = cfg->encap_destroy;
  60. udp_tunnel_encap_enable(sock);
  61. }
  62. EXPORT_SYMBOL_GPL(setup_udp_tunnel_sock);
  63. int udp_tunnel_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
  64. __be32 src, __be32 dst, __u8 tos, __u8 ttl,
  65. __be16 df, __be16 src_port, __be16 dst_port,
  66. bool xnet, bool nocheck)
  67. {
  68. struct udphdr *uh;
  69. __skb_push(skb, sizeof(*uh));
  70. skb_reset_transport_header(skb);
  71. uh = udp_hdr(skb);
  72. uh->dest = dst_port;
  73. uh->source = src_port;
  74. uh->len = htons(skb->len);
  75. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  76. udp_set_csum(nocheck, skb, src, dst, skb->len);
  77. return iptunnel_xmit(sk, rt, skb, src, dst, IPPROTO_UDP,
  78. tos, ttl, df, xnet);
  79. }
  80. EXPORT_SYMBOL_GPL(udp_tunnel_xmit_skb);
  81. void udp_tunnel_sock_release(struct socket *sock)
  82. {
  83. rcu_assign_sk_user_data(sock->sk, NULL);
  84. kernel_sock_shutdown(sock, SHUT_RDWR);
  85. sock_release(sock);
  86. }
  87. EXPORT_SYMBOL_GPL(udp_tunnel_sock_release);
  88. struct metadata_dst *udp_tun_rx_dst(struct sk_buff *skb, unsigned short family,
  89. __be16 flags, __be64 tunnel_id, int md_size)
  90. {
  91. struct metadata_dst *tun_dst;
  92. struct ip_tunnel_info *info;
  93. if (family == AF_INET)
  94. tun_dst = ip_tun_rx_dst(skb, flags, tunnel_id, md_size);
  95. else
  96. tun_dst = ipv6_tun_rx_dst(skb, flags, tunnel_id, md_size);
  97. if (!tun_dst)
  98. return NULL;
  99. info = &tun_dst->u.tun_info;
  100. info->key.tp_src = udp_hdr(skb)->source;
  101. info->key.tp_dst = udp_hdr(skb)->dest;
  102. if (udp_hdr(skb)->check)
  103. info->key.tun_flags |= TUNNEL_CSUM;
  104. return tun_dst;
  105. }
  106. EXPORT_SYMBOL_GPL(udp_tun_rx_dst);
  107. MODULE_LICENSE("GPL");