blob: 14dacf1df529d9602cc497976db0868f68d9fbb7 [file] [log] [blame]
Andy Zhoufd384412014-09-16 17:31:16 -07001#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
15int 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. Biederman26abe142015-05-08 21:10:31 -050022 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM, 0, &sock);
Andy Zhoufd384412014-09-16 17:31:16 -070023 if (err < 0)
24 goto error;
25
Jiri Benca43a9ef2015-08-28 20:48:22 +020026 if (cfg->ipv6_v6only) {
27 int val = 1;
28
29 err = kernel_setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
30 (char *) &val, sizeof(val));
31 if (err < 0)
32 goto error;
33 }
34
Andy Zhoufd384412014-09-16 17:31:16 -070035 udp6_addr.sin6_family = AF_INET6;
36 memcpy(&udp6_addr.sin6_addr, &cfg->local_ip6,
37 sizeof(udp6_addr.sin6_addr));
38 udp6_addr.sin6_port = cfg->local_udp_port;
39 err = kernel_bind(sock, (struct sockaddr *)&udp6_addr,
40 sizeof(udp6_addr));
41 if (err < 0)
42 goto error;
43
44 if (cfg->peer_udp_port) {
45 udp6_addr.sin6_family = AF_INET6;
46 memcpy(&udp6_addr.sin6_addr, &cfg->peer_ip6,
47 sizeof(udp6_addr.sin6_addr));
48 udp6_addr.sin6_port = cfg->peer_udp_port;
49 err = kernel_connect(sock,
50 (struct sockaddr *)&udp6_addr,
51 sizeof(udp6_addr), 0);
52 }
53 if (err < 0)
54 goto error;
55
56 udp_set_no_check6_tx(sock->sk, !cfg->use_udp6_tx_checksums);
57 udp_set_no_check6_rx(sock->sk, !cfg->use_udp6_rx_checksums);
58
59 *sockp = sock;
60 return 0;
61
62error:
63 if (sock) {
64 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -050065 sock_release(sock);
Andy Zhoufd384412014-09-16 17:31:16 -070066 }
67 *sockp = NULL;
68 return err;
69}
70EXPORT_SYMBOL_GPL(udp_sock_create6);
Andy Zhou6a93cc92014-09-16 17:31:17 -070071
David Miller79b16aa2015-04-05 22:19:09 -040072int udp_tunnel6_xmit_skb(struct dst_entry *dst, struct sock *sk,
73 struct sk_buff *skb,
Tom Herbertd998f8e2015-01-20 11:23:04 -080074 struct net_device *dev, struct in6_addr *saddr,
75 struct in6_addr *daddr,
76 __u8 prio, __u8 ttl, __be16 src_port,
77 __be16 dst_port, bool nocheck)
Andy Zhou6a93cc92014-09-16 17:31:17 -070078{
79 struct udphdr *uh;
80 struct ipv6hdr *ip6h;
Andy Zhou6a93cc92014-09-16 17:31:17 -070081
82 __skb_push(skb, sizeof(*uh));
83 skb_reset_transport_header(skb);
84 uh = udp_hdr(skb);
85
86 uh->dest = dst_port;
87 uh->source = src_port;
88
89 uh->len = htons(skb->len);
Andy Zhou6a93cc92014-09-16 17:31:17 -070090
91 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
92 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED
93 | IPSKB_REROUTED);
94 skb_dst_set(skb, dst);
95
Tom Herbertd998f8e2015-01-20 11:23:04 -080096 udp6_set_csum(nocheck, skb, saddr, daddr, skb->len);
Andy Zhou6a93cc92014-09-16 17:31:17 -070097
98 __skb_push(skb, sizeof(*ip6h));
99 skb_reset_network_header(skb);
100 ip6h = ipv6_hdr(skb);
101 ip6_flow_hdr(ip6h, prio, htonl(0));
102 ip6h->payload_len = htons(skb->len);
103 ip6h->nexthdr = IPPROTO_UDP;
104 ip6h->hop_limit = ttl;
105 ip6h->daddr = *daddr;
106 ip6h->saddr = *saddr;
107
David Miller79b16aa2015-04-05 22:19:09 -0400108 ip6tunnel_xmit(sk, skb, dev);
Andy Zhou6a93cc92014-09-16 17:31:17 -0700109 return 0;
110}
111EXPORT_SYMBOL_GPL(udp_tunnel6_xmit_skb);
Tom Herbert3fcb95a2014-09-22 11:39:44 -0700112
113MODULE_LICENSE("GPL");