blob: 25430c991cea34e952a47f01326f400556b08d1e [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{
Cong Wangfb242742018-12-18 21:17:44 -080018 struct sockaddr_in6 udp6_addr = {};
Andy Zhoufd384412014-09-16 17:31:16 -070019 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 }
Alexis Bauvinda5095d052018-12-03 10:54:38 +010034 if (cfg->bind_ifindex) {
David Herrmann49b49942019-01-15 14:42:16 +010035 err = kernel_setsockopt(sock, SOL_SOCKET, SO_BINDTOIFINDEX,
36 (void *)&cfg->bind_ifindex,
37 sizeof(cfg->bind_ifindex));
Alexis Bauvinda5095d052018-12-03 10:54:38 +010038 if (err < 0)
39 goto error;
40 }
Jiri Benca43a9ef2015-08-28 20:48:22 +020041
Andy Zhoufd384412014-09-16 17:31:16 -070042 udp6_addr.sin6_family = AF_INET6;
43 memcpy(&udp6_addr.sin6_addr, &cfg->local_ip6,
44 sizeof(udp6_addr.sin6_addr));
45 udp6_addr.sin6_port = cfg->local_udp_port;
46 err = kernel_bind(sock, (struct sockaddr *)&udp6_addr,
47 sizeof(udp6_addr));
48 if (err < 0)
49 goto error;
50
51 if (cfg->peer_udp_port) {
Cong Wangfb242742018-12-18 21:17:44 -080052 memset(&udp6_addr, 0, sizeof(udp6_addr));
Andy Zhoufd384412014-09-16 17:31:16 -070053 udp6_addr.sin6_family = AF_INET6;
54 memcpy(&udp6_addr.sin6_addr, &cfg->peer_ip6,
55 sizeof(udp6_addr.sin6_addr));
56 udp6_addr.sin6_port = cfg->peer_udp_port;
57 err = kernel_connect(sock,
58 (struct sockaddr *)&udp6_addr,
59 sizeof(udp6_addr), 0);
60 }
61 if (err < 0)
62 goto error;
63
64 udp_set_no_check6_tx(sock->sk, !cfg->use_udp6_tx_checksums);
65 udp_set_no_check6_rx(sock->sk, !cfg->use_udp6_rx_checksums);
66
67 *sockp = sock;
68 return 0;
69
70error:
71 if (sock) {
72 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -050073 sock_release(sock);
Andy Zhoufd384412014-09-16 17:31:16 -070074 }
75 *sockp = NULL;
76 return err;
77}
78EXPORT_SYMBOL_GPL(udp_sock_create6);
Andy Zhou6a93cc92014-09-16 17:31:17 -070079
David Miller79b16aa2015-04-05 22:19:09 -040080int udp_tunnel6_xmit_skb(struct dst_entry *dst, struct sock *sk,
81 struct sk_buff *skb,
Tom Herbertd998f8e2015-01-20 11:23:04 -080082 struct net_device *dev, struct in6_addr *saddr,
83 struct in6_addr *daddr,
Daniel Borkmann13461142016-03-09 03:00:02 +010084 __u8 prio, __u8 ttl, __be32 label,
85 __be16 src_port, __be16 dst_port, bool nocheck)
Andy Zhou6a93cc92014-09-16 17:31:17 -070086{
87 struct udphdr *uh;
88 struct ipv6hdr *ip6h;
Andy Zhou6a93cc92014-09-16 17:31:17 -070089
90 __skb_push(skb, sizeof(*uh));
91 skb_reset_transport_header(skb);
92 uh = udp_hdr(skb);
93
94 uh->dest = dst_port;
95 uh->source = src_port;
96
97 uh->len = htons(skb->len);
Andy Zhou6a93cc92014-09-16 17:31:17 -070098
Andy Zhou6a93cc92014-09-16 17:31:17 -070099 skb_dst_set(skb, dst);
100
Tom Herbertd998f8e2015-01-20 11:23:04 -0800101 udp6_set_csum(nocheck, skb, saddr, daddr, skb->len);
Andy Zhou6a93cc92014-09-16 17:31:17 -0700102
103 __skb_push(skb, sizeof(*ip6h));
104 skb_reset_network_header(skb);
105 ip6h = ipv6_hdr(skb);
Daniel Borkmann13461142016-03-09 03:00:02 +0100106 ip6_flow_hdr(ip6h, prio, label);
Andy Zhou6a93cc92014-09-16 17:31:17 -0700107 ip6h->payload_len = htons(skb->len);
108 ip6h->nexthdr = IPPROTO_UDP;
109 ip6h->hop_limit = ttl;
110 ip6h->daddr = *daddr;
111 ip6h->saddr = *saddr;
112
David Miller79b16aa2015-04-05 22:19:09 -0400113 ip6tunnel_xmit(sk, skb, dev);
Andy Zhou6a93cc92014-09-16 17:31:17 -0700114 return 0;
115}
116EXPORT_SYMBOL_GPL(udp_tunnel6_xmit_skb);
Tom Herbert3fcb95a2014-09-22 11:39:44 -0700117
118MODULE_LICENSE("GPL");