blob: 32d9b268e7d85faa6b33a3fa1a3f3fff9a36cad8 [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
22 err = sock_create_kern(AF_INET6, SOCK_DGRAM, 0, &sock);
23 if (err < 0)
24 goto error;
25
26 sk_change_net(sock->sk, net);
27
28 udp6_addr.sin6_family = AF_INET6;
29 memcpy(&udp6_addr.sin6_addr, &cfg->local_ip6,
30 sizeof(udp6_addr.sin6_addr));
31 udp6_addr.sin6_port = cfg->local_udp_port;
32 err = kernel_bind(sock, (struct sockaddr *)&udp6_addr,
33 sizeof(udp6_addr));
34 if (err < 0)
35 goto error;
36
37 if (cfg->peer_udp_port) {
38 udp6_addr.sin6_family = AF_INET6;
39 memcpy(&udp6_addr.sin6_addr, &cfg->peer_ip6,
40 sizeof(udp6_addr.sin6_addr));
41 udp6_addr.sin6_port = cfg->peer_udp_port;
42 err = kernel_connect(sock,
43 (struct sockaddr *)&udp6_addr,
44 sizeof(udp6_addr), 0);
45 }
46 if (err < 0)
47 goto error;
48
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
52 *sockp = sock;
53 return 0;
54
55error:
56 if (sock) {
57 kernel_sock_shutdown(sock, SHUT_RDWR);
58 sk_release_kernel(sock->sk);
59 }
60 *sockp = NULL;
61 return err;
62}
63EXPORT_SYMBOL_GPL(udp_sock_create6);
Andy Zhou6a93cc92014-09-16 17:31:17 -070064
Tom Herbertd998f8e2015-01-20 11:23:04 -080065int udp_tunnel6_xmit_skb(struct dst_entry *dst, struct sk_buff *skb,
66 struct net_device *dev, struct in6_addr *saddr,
67 struct in6_addr *daddr,
68 __u8 prio, __u8 ttl, __be16 src_port,
69 __be16 dst_port, bool nocheck)
Andy Zhou6a93cc92014-09-16 17:31:17 -070070{
71 struct udphdr *uh;
72 struct ipv6hdr *ip6h;
Andy Zhou6a93cc92014-09-16 17:31:17 -070073
74 __skb_push(skb, sizeof(*uh));
75 skb_reset_transport_header(skb);
76 uh = udp_hdr(skb);
77
78 uh->dest = dst_port;
79 uh->source = src_port;
80
81 uh->len = htons(skb->len);
Andy Zhou6a93cc92014-09-16 17:31:17 -070082
83 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
84 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED
85 | IPSKB_REROUTED);
86 skb_dst_set(skb, dst);
87
Tom Herbertd998f8e2015-01-20 11:23:04 -080088 udp6_set_csum(nocheck, skb, saddr, daddr, skb->len);
Andy Zhou6a93cc92014-09-16 17:31:17 -070089
90 __skb_push(skb, sizeof(*ip6h));
91 skb_reset_network_header(skb);
92 ip6h = ipv6_hdr(skb);
93 ip6_flow_hdr(ip6h, prio, htonl(0));
94 ip6h->payload_len = htons(skb->len);
95 ip6h->nexthdr = IPPROTO_UDP;
96 ip6h->hop_limit = ttl;
97 ip6h->daddr = *daddr;
98 ip6h->saddr = *saddr;
99
100 ip6tunnel_xmit(skb, dev);
101 return 0;
102}
103EXPORT_SYMBOL_GPL(udp_tunnel6_xmit_skb);
Tom Herbert3fcb95a2014-09-22 11:39:44 -0700104
105MODULE_LICENSE("GPL");