ip6_udp_tunnel.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 <linux/in6.h>
  8. #include <net/udp.h>
  9. #include <net/udp_tunnel.h>
  10. #include <net/net_namespace.h>
  11. #include <net/netns/generic.h>
  12. #include <net/ip6_tunnel.h>
  13. #include <net/ip6_checksum.h>
  14. int udp_sock_create6(struct net *net, struct udp_port_cfg *cfg,
  15. struct socket **sockp)
  16. {
  17. struct sockaddr_in6 udp6_addr;
  18. int err;
  19. struct socket *sock = NULL;
  20. err = sock_create_kern(net, AF_INET6, SOCK_DGRAM, 0, &sock);
  21. if (err < 0)
  22. goto error;
  23. udp6_addr.sin6_family = AF_INET6;
  24. memcpy(&udp6_addr.sin6_addr, &cfg->local_ip6,
  25. sizeof(udp6_addr.sin6_addr));
  26. udp6_addr.sin6_port = cfg->local_udp_port;
  27. err = kernel_bind(sock, (struct sockaddr *)&udp6_addr,
  28. sizeof(udp6_addr));
  29. if (err < 0)
  30. goto error;
  31. if (cfg->peer_udp_port) {
  32. udp6_addr.sin6_family = AF_INET6;
  33. memcpy(&udp6_addr.sin6_addr, &cfg->peer_ip6,
  34. sizeof(udp6_addr.sin6_addr));
  35. udp6_addr.sin6_port = cfg->peer_udp_port;
  36. err = kernel_connect(sock,
  37. (struct sockaddr *)&udp6_addr,
  38. sizeof(udp6_addr), 0);
  39. }
  40. if (err < 0)
  41. goto error;
  42. udp_set_no_check6_tx(sock->sk, !cfg->use_udp6_tx_checksums);
  43. udp_set_no_check6_rx(sock->sk, !cfg->use_udp6_rx_checksums);
  44. *sockp = sock;
  45. return 0;
  46. error:
  47. if (sock) {
  48. kernel_sock_shutdown(sock, SHUT_RDWR);
  49. sock_release(sock);
  50. }
  51. *sockp = NULL;
  52. return err;
  53. }
  54. EXPORT_SYMBOL_GPL(udp_sock_create6);
  55. int udp_tunnel6_xmit_skb(struct dst_entry *dst, struct sock *sk,
  56. struct sk_buff *skb,
  57. struct net_device *dev, struct in6_addr *saddr,
  58. struct in6_addr *daddr,
  59. __u8 prio, __u8 ttl, __be16 src_port,
  60. __be16 dst_port, bool nocheck)
  61. {
  62. struct udphdr *uh;
  63. struct ipv6hdr *ip6h;
  64. __skb_push(skb, sizeof(*uh));
  65. skb_reset_transport_header(skb);
  66. uh = udp_hdr(skb);
  67. uh->dest = dst_port;
  68. uh->source = src_port;
  69. uh->len = htons(skb->len);
  70. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  71. IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED
  72. | IPSKB_REROUTED);
  73. skb_dst_set(skb, dst);
  74. udp6_set_csum(nocheck, skb, saddr, daddr, skb->len);
  75. __skb_push(skb, sizeof(*ip6h));
  76. skb_reset_network_header(skb);
  77. ip6h = ipv6_hdr(skb);
  78. ip6_flow_hdr(ip6h, prio, htonl(0));
  79. ip6h->payload_len = htons(skb->len);
  80. ip6h->nexthdr = IPPROTO_UDP;
  81. ip6h->hop_limit = ttl;
  82. ip6h->daddr = *daddr;
  83. ip6h->saddr = *saddr;
  84. ip6tunnel_xmit(sk, skb, dev);
  85. return 0;
  86. }
  87. EXPORT_SYMBOL_GPL(udp_tunnel6_xmit_skb);
  88. MODULE_LICENSE("GPL");