Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 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 | |
| 15 | int udp_sock_create6(struct net *net, struct udp_port_cfg *cfg, |
| 16 | struct socket **sockp) |
| 17 | { |
| 18 | struct sockaddr_in6 udp6_addr; |
| 19 | int err; |
| 20 | struct socket *sock = NULL; |
| 21 | |
Eric W. Biederman | 26abe14 | 2015-05-08 21:10:31 -0500 | [diff] [blame] | 22 | err = sock_create_kern(net, AF_INET6, SOCK_DGRAM, 0, &sock); |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 23 | if (err < 0) |
| 24 | goto error; |
| 25 | |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 26 | udp6_addr.sin6_family = AF_INET6; |
| 27 | memcpy(&udp6_addr.sin6_addr, &cfg->local_ip6, |
| 28 | sizeof(udp6_addr.sin6_addr)); |
| 29 | udp6_addr.sin6_port = cfg->local_udp_port; |
| 30 | err = kernel_bind(sock, (struct sockaddr *)&udp6_addr, |
| 31 | sizeof(udp6_addr)); |
| 32 | if (err < 0) |
| 33 | goto error; |
| 34 | |
| 35 | if (cfg->peer_udp_port) { |
| 36 | udp6_addr.sin6_family = AF_INET6; |
| 37 | memcpy(&udp6_addr.sin6_addr, &cfg->peer_ip6, |
| 38 | sizeof(udp6_addr.sin6_addr)); |
| 39 | udp6_addr.sin6_port = cfg->peer_udp_port; |
| 40 | err = kernel_connect(sock, |
| 41 | (struct sockaddr *)&udp6_addr, |
| 42 | sizeof(udp6_addr), 0); |
| 43 | } |
| 44 | if (err < 0) |
| 45 | goto error; |
| 46 | |
| 47 | udp_set_no_check6_tx(sock->sk, !cfg->use_udp6_tx_checksums); |
| 48 | udp_set_no_check6_rx(sock->sk, !cfg->use_udp6_rx_checksums); |
| 49 | |
| 50 | *sockp = sock; |
| 51 | return 0; |
| 52 | |
| 53 | error: |
| 54 | if (sock) { |
| 55 | kernel_sock_shutdown(sock, SHUT_RDWR); |
Eric W. Biederman | 26abe14 | 2015-05-08 21:10:31 -0500 | [diff] [blame] | 56 | sock_release(sock); |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 57 | } |
| 58 | *sockp = NULL; |
| 59 | return err; |
| 60 | } |
| 61 | EXPORT_SYMBOL_GPL(udp_sock_create6); |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 62 | |
David Miller | 79b16aa | 2015-04-05 22:19:09 -0400 | [diff] [blame] | 63 | int udp_tunnel6_xmit_skb(struct dst_entry *dst, struct sock *sk, |
| 64 | struct sk_buff *skb, |
Tom Herbert | d998f8e | 2015-01-20 11:23:04 -0800 | [diff] [blame] | 65 | struct net_device *dev, struct in6_addr *saddr, |
| 66 | struct in6_addr *daddr, |
| 67 | __u8 prio, __u8 ttl, __be16 src_port, |
| 68 | __be16 dst_port, bool nocheck) |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 69 | { |
| 70 | struct udphdr *uh; |
| 71 | struct ipv6hdr *ip6h; |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 72 | |
| 73 | __skb_push(skb, sizeof(*uh)); |
| 74 | skb_reset_transport_header(skb); |
| 75 | uh = udp_hdr(skb); |
| 76 | |
| 77 | uh->dest = dst_port; |
| 78 | uh->source = src_port; |
| 79 | |
| 80 | uh->len = htons(skb->len); |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 81 | |
| 82 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
| 83 | IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
| 84 | | IPSKB_REROUTED); |
| 85 | skb_dst_set(skb, dst); |
| 86 | |
Tom Herbert | d998f8e | 2015-01-20 11:23:04 -0800 | [diff] [blame] | 87 | udp6_set_csum(nocheck, skb, saddr, daddr, skb->len); |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 88 | |
| 89 | __skb_push(skb, sizeof(*ip6h)); |
| 90 | skb_reset_network_header(skb); |
| 91 | ip6h = ipv6_hdr(skb); |
| 92 | ip6_flow_hdr(ip6h, prio, htonl(0)); |
| 93 | ip6h->payload_len = htons(skb->len); |
| 94 | ip6h->nexthdr = IPPROTO_UDP; |
| 95 | ip6h->hop_limit = ttl; |
| 96 | ip6h->daddr = *daddr; |
| 97 | ip6h->saddr = *saddr; |
| 98 | |
David Miller | 79b16aa | 2015-04-05 22:19:09 -0400 | [diff] [blame] | 99 | ip6tunnel_xmit(sk, skb, dev); |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 100 | return 0; |
| 101 | } |
| 102 | EXPORT_SYMBOL_GPL(udp_tunnel6_xmit_skb); |
Tom Herbert | 3fcb95a | 2014-09-22 11:39:44 -0700 | [diff] [blame] | 103 | |
| 104 | MODULE_LICENSE("GPL"); |