udp_tunnel.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/udp.h>
  8. #include <net/udp_tunnel.h>
  9. #include <net/net_namespace.h>
  10. int udp_sock_create4(struct net *net, struct udp_port_cfg *cfg,
  11. struct socket **sockp)
  12. {
  13. int err;
  14. struct socket *sock = NULL;
  15. struct sockaddr_in udp_addr;
  16. err = sock_create_kern(net, AF_INET, SOCK_DGRAM, 0, &sock);
  17. if (err < 0)
  18. goto error;
  19. udp_addr.sin_family = AF_INET;
  20. udp_addr.sin_addr = cfg->local_ip;
  21. udp_addr.sin_port = cfg->local_udp_port;
  22. err = kernel_bind(sock, (struct sockaddr *)&udp_addr,
  23. sizeof(udp_addr));
  24. if (err < 0)
  25. goto error;
  26. if (cfg->peer_udp_port) {
  27. udp_addr.sin_family = AF_INET;
  28. udp_addr.sin_addr = cfg->peer_ip;
  29. udp_addr.sin_port = cfg->peer_udp_port;
  30. err = kernel_connect(sock, (struct sockaddr *)&udp_addr,
  31. sizeof(udp_addr), 0);
  32. if (err < 0)
  33. goto error;
  34. }
  35. sock->sk->sk_no_check_tx = !cfg->use_udp_checksums;
  36. *sockp = sock;
  37. return 0;
  38. error:
  39. if (sock) {
  40. kernel_sock_shutdown(sock, SHUT_RDWR);
  41. sock_release(sock);
  42. }
  43. *sockp = NULL;
  44. return err;
  45. }
  46. EXPORT_SYMBOL(udp_sock_create4);
  47. void setup_udp_tunnel_sock(struct net *net, struct socket *sock,
  48. struct udp_tunnel_sock_cfg *cfg)
  49. {
  50. struct sock *sk = sock->sk;
  51. /* Disable multicast loopback */
  52. inet_sk(sk)->mc_loop = 0;
  53. /* Enable CHECKSUM_UNNECESSARY to CHECKSUM_COMPLETE conversion */
  54. inet_inc_convert_csum(sk);
  55. rcu_assign_sk_user_data(sk, cfg->sk_user_data);
  56. udp_sk(sk)->encap_type = cfg->encap_type;
  57. udp_sk(sk)->encap_rcv = cfg->encap_rcv;
  58. udp_sk(sk)->encap_destroy = cfg->encap_destroy;
  59. udp_tunnel_encap_enable(sock);
  60. }
  61. EXPORT_SYMBOL_GPL(setup_udp_tunnel_sock);
  62. int udp_tunnel_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
  63. __be32 src, __be32 dst, __u8 tos, __u8 ttl,
  64. __be16 df, __be16 src_port, __be16 dst_port,
  65. bool xnet, bool nocheck)
  66. {
  67. struct udphdr *uh;
  68. __skb_push(skb, sizeof(*uh));
  69. skb_reset_transport_header(skb);
  70. uh = udp_hdr(skb);
  71. uh->dest = dst_port;
  72. uh->source = src_port;
  73. uh->len = htons(skb->len);
  74. udp_set_csum(nocheck, skb, src, dst, skb->len);
  75. return iptunnel_xmit(sk, rt, skb, src, dst, IPPROTO_UDP,
  76. tos, ttl, df, xnet);
  77. }
  78. EXPORT_SYMBOL_GPL(udp_tunnel_xmit_skb);
  79. void udp_tunnel_sock_release(struct socket *sock)
  80. {
  81. rcu_assign_sk_user_data(sock->sk, NULL);
  82. kernel_sock_shutdown(sock, SHUT_RDWR);
  83. sock_release(sock);
  84. }
  85. EXPORT_SYMBOL_GPL(udp_tunnel_sock_release);
  86. MODULE_LICENSE("GPL");