udp_tunnel.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_sk(sk)->gro_receive = cfg->gro_receive;
  61. udp_sk(sk)->gro_complete = cfg->gro_complete;
  62. udp_tunnel_encap_enable(sock);
  63. }
  64. EXPORT_SYMBOL_GPL(setup_udp_tunnel_sock);
  65. void udp_tunnel_push_rx_port(struct net_device *dev, struct socket *sock,
  66. unsigned short type)
  67. {
  68. struct sock *sk = sock->sk;
  69. struct udp_tunnel_info ti;
  70. if (!dev->netdev_ops->ndo_udp_tunnel_add)
  71. return;
  72. ti.type = type;
  73. ti.sa_family = sk->sk_family;
  74. ti.port = inet_sk(sk)->inet_sport;
  75. dev->netdev_ops->ndo_udp_tunnel_add(dev, &ti);
  76. }
  77. EXPORT_SYMBOL_GPL(udp_tunnel_push_rx_port);
  78. /* Notify netdevs that UDP port started listening */
  79. void udp_tunnel_notify_add_rx_port(struct socket *sock, unsigned short type)
  80. {
  81. struct sock *sk = sock->sk;
  82. struct net *net = sock_net(sk);
  83. struct udp_tunnel_info ti;
  84. struct net_device *dev;
  85. ti.type = type;
  86. ti.sa_family = sk->sk_family;
  87. ti.port = inet_sk(sk)->inet_sport;
  88. rcu_read_lock();
  89. for_each_netdev_rcu(net, dev) {
  90. if (!dev->netdev_ops->ndo_udp_tunnel_add)
  91. continue;
  92. dev->netdev_ops->ndo_udp_tunnel_add(dev, &ti);
  93. }
  94. rcu_read_unlock();
  95. }
  96. EXPORT_SYMBOL_GPL(udp_tunnel_notify_add_rx_port);
  97. /* Notify netdevs that UDP port is no more listening */
  98. void udp_tunnel_notify_del_rx_port(struct socket *sock, unsigned short type)
  99. {
  100. struct sock *sk = sock->sk;
  101. struct net *net = sock_net(sk);
  102. struct udp_tunnel_info ti;
  103. struct net_device *dev;
  104. ti.type = type;
  105. ti.sa_family = sk->sk_family;
  106. ti.port = inet_sk(sk)->inet_sport;
  107. rcu_read_lock();
  108. for_each_netdev_rcu(net, dev) {
  109. if (!dev->netdev_ops->ndo_udp_tunnel_del)
  110. continue;
  111. dev->netdev_ops->ndo_udp_tunnel_del(dev, &ti);
  112. }
  113. rcu_read_unlock();
  114. }
  115. EXPORT_SYMBOL_GPL(udp_tunnel_notify_del_rx_port);
  116. void udp_tunnel_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
  117. __be32 src, __be32 dst, __u8 tos, __u8 ttl,
  118. __be16 df, __be16 src_port, __be16 dst_port,
  119. bool xnet, bool nocheck)
  120. {
  121. struct udphdr *uh;
  122. __skb_push(skb, sizeof(*uh));
  123. skb_reset_transport_header(skb);
  124. uh = udp_hdr(skb);
  125. uh->dest = dst_port;
  126. uh->source = src_port;
  127. uh->len = htons(skb->len);
  128. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  129. udp_set_csum(nocheck, skb, src, dst, skb->len);
  130. iptunnel_xmit(sk, rt, skb, src, dst, IPPROTO_UDP, tos, ttl, df, xnet);
  131. }
  132. EXPORT_SYMBOL_GPL(udp_tunnel_xmit_skb);
  133. void udp_tunnel_sock_release(struct socket *sock)
  134. {
  135. rcu_assign_sk_user_data(sock->sk, NULL);
  136. kernel_sock_shutdown(sock, SHUT_RDWR);
  137. sock_release(sock);
  138. }
  139. EXPORT_SYMBOL_GPL(udp_tunnel_sock_release);
  140. struct metadata_dst *udp_tun_rx_dst(struct sk_buff *skb, unsigned short family,
  141. __be16 flags, __be64 tunnel_id, int md_size)
  142. {
  143. struct metadata_dst *tun_dst;
  144. struct ip_tunnel_info *info;
  145. if (family == AF_INET)
  146. tun_dst = ip_tun_rx_dst(skb, flags, tunnel_id, md_size);
  147. else
  148. tun_dst = ipv6_tun_rx_dst(skb, flags, tunnel_id, md_size);
  149. if (!tun_dst)
  150. return NULL;
  151. info = &tun_dst->u.tun_info;
  152. info->key.tp_src = udp_hdr(skb)->source;
  153. info->key.tp_dst = udp_hdr(skb)->dest;
  154. if (udp_hdr(skb)->check)
  155. info->key.tun_flags |= TUNNEL_CSUM;
  156. return tun_dst;
  157. }
  158. EXPORT_SYMBOL_GPL(udp_tun_rx_dst);
  159. MODULE_LICENSE("GPL");