ip6_udp_tunnel.c 2.6 KB

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