ip6_udp_tunnel.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. if (cfg->ipv6_v6only) {
  24. int val = 1;
  25. err = kernel_setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
  26. (char *) &val, sizeof(val));
  27. if (err < 0)
  28. goto error;
  29. }
  30. udp6_addr.sin6_family = AF_INET6;
  31. memcpy(&udp6_addr.sin6_addr, &cfg->local_ip6,
  32. sizeof(udp6_addr.sin6_addr));
  33. udp6_addr.sin6_port = cfg->local_udp_port;
  34. err = kernel_bind(sock, (struct sockaddr *)&udp6_addr,
  35. sizeof(udp6_addr));
  36. if (err < 0)
  37. goto error;
  38. if (cfg->peer_udp_port) {
  39. udp6_addr.sin6_family = AF_INET6;
  40. memcpy(&udp6_addr.sin6_addr, &cfg->peer_ip6,
  41. sizeof(udp6_addr.sin6_addr));
  42. udp6_addr.sin6_port = cfg->peer_udp_port;
  43. err = kernel_connect(sock,
  44. (struct sockaddr *)&udp6_addr,
  45. sizeof(udp6_addr), 0);
  46. }
  47. if (err < 0)
  48. goto error;
  49. udp_set_no_check6_tx(sock->sk, !cfg->use_udp6_tx_checksums);
  50. udp_set_no_check6_rx(sock->sk, !cfg->use_udp6_rx_checksums);
  51. *sockp = sock;
  52. return 0;
  53. error:
  54. if (sock) {
  55. kernel_sock_shutdown(sock, SHUT_RDWR);
  56. sock_release(sock);
  57. }
  58. *sockp = NULL;
  59. return err;
  60. }
  61. EXPORT_SYMBOL_GPL(udp_sock_create6);
  62. int udp_tunnel6_xmit_skb(struct dst_entry *dst, struct sock *sk,
  63. struct sk_buff *skb,
  64. struct net_device *dev, struct in6_addr *saddr,
  65. struct in6_addr *daddr,
  66. __u8 prio, __u8 ttl, __be32 label,
  67. __be16 src_port, __be16 dst_port, bool nocheck)
  68. {
  69. struct udphdr *uh;
  70. struct ipv6hdr *ip6h;
  71. __skb_push(skb, sizeof(*uh));
  72. skb_reset_transport_header(skb);
  73. uh = udp_hdr(skb);
  74. uh->dest = dst_port;
  75. uh->source = src_port;
  76. uh->len = htons(skb->len);
  77. skb_dst_set(skb, dst);
  78. udp6_set_csum(nocheck, skb, saddr, daddr, skb->len);
  79. __skb_push(skb, sizeof(*ip6h));
  80. skb_reset_network_header(skb);
  81. ip6h = ipv6_hdr(skb);
  82. ip6_flow_hdr(ip6h, prio, label);
  83. ip6h->payload_len = htons(skb->len);
  84. ip6h->nexthdr = IPPROTO_UDP;
  85. ip6h->hop_limit = ttl;
  86. ip6h->daddr = *daddr;
  87. ip6h->saddr = *saddr;
  88. ip6tunnel_xmit(sk, skb, dev);
  89. return 0;
  90. }
  91. EXPORT_SYMBOL_GPL(udp_tunnel6_xmit_skb);
  92. MODULE_LICENSE("GPL");