udp_tunnel.c 5.4 KB

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