blob: 58956a6b66a21568cb4709191b458f82490615cb [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Andy Zhoufd384412014-09-16 17:31:16 -07002#include <linux/module.h>
3#include <linux/errno.h>
4#include <linux/socket.h>
5#include <linux/udp.h>
6#include <linux/types.h>
7#include <linux/kernel.h>
8#include <linux/in6.h>
9#include <net/udp.h>
10#include <net/udp_tunnel.h>
11#include <net/net_namespace.h>
12#include <net/netns/generic.h>
13#include <net/ip6_tunnel.h>
14#include <net/ip6_checksum.h>
15
16int udp_sock_create6(struct net *net, struct udp_port_cfg *cfg,
17 struct socket **sockp)
18{
Cong Wangfb242742018-12-18 21:17:44 -080019 struct sockaddr_in6 udp6_addr = {};
Andy Zhoufd384412014-09-16 17:31:16 -070020 int err;
21 struct socket *sock = NULL;
22
Eric W. Biederman26abe142015-05-08 21:10:31 -050023 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM, 0, &sock);
Andy Zhoufd384412014-09-16 17:31:16 -070024 if (err < 0)
25 goto error;
26
Jiri Benca43a9ef2015-08-28 20:48:22 +020027 if (cfg->ipv6_v6only) {
28 int val = 1;
29
30 err = kernel_setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
31 (char *) &val, sizeof(val));
32 if (err < 0)
33 goto error;
34 }
Alexis Bauvinda5095d052018-12-03 10:54:38 +010035 if (cfg->bind_ifindex) {
David Herrmann49b49942019-01-15 14:42:16 +010036 err = kernel_setsockopt(sock, SOL_SOCKET, SO_BINDTOIFINDEX,
37 (void *)&cfg->bind_ifindex,
38 sizeof(cfg->bind_ifindex));
Alexis Bauvinda5095d052018-12-03 10:54:38 +010039 if (err < 0)
40 goto error;
41 }
Jiri Benca43a9ef2015-08-28 20:48:22 +020042
Andy Zhoufd384412014-09-16 17:31:16 -070043 udp6_addr.sin6_family = AF_INET6;
44 memcpy(&udp6_addr.sin6_addr, &cfg->local_ip6,
45 sizeof(udp6_addr.sin6_addr));
46 udp6_addr.sin6_port = cfg->local_udp_port;
47 err = kernel_bind(sock, (struct sockaddr *)&udp6_addr,
48 sizeof(udp6_addr));
49 if (err < 0)
50 goto error;
51
52 if (cfg->peer_udp_port) {
Cong Wangfb242742018-12-18 21:17:44 -080053 memset(&udp6_addr, 0, sizeof(udp6_addr));
Andy Zhoufd384412014-09-16 17:31:16 -070054 udp6_addr.sin6_family = AF_INET6;
55 memcpy(&udp6_addr.sin6_addr, &cfg->peer_ip6,
56 sizeof(udp6_addr.sin6_addr));
57 udp6_addr.sin6_port = cfg->peer_udp_port;
58 err = kernel_connect(sock,
59 (struct sockaddr *)&udp6_addr,
60 sizeof(udp6_addr), 0);
61 }
62 if (err < 0)
63 goto error;
64
65 udp_set_no_check6_tx(sock->sk, !cfg->use_udp6_tx_checksums);
66 udp_set_no_check6_rx(sock->sk, !cfg->use_udp6_rx_checksums);
67
68 *sockp = sock;
69 return 0;
70
71error:
72 if (sock) {
73 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -050074 sock_release(sock);
Andy Zhoufd384412014-09-16 17:31:16 -070075 }
76 *sockp = NULL;
77 return err;
78}
79EXPORT_SYMBOL_GPL(udp_sock_create6);
Andy Zhou6a93cc92014-09-16 17:31:17 -070080
David Miller79b16aa2015-04-05 22:19:09 -040081int udp_tunnel6_xmit_skb(struct dst_entry *dst, struct sock *sk,
82 struct sk_buff *skb,
Tom Herbertd998f8e2015-01-20 11:23:04 -080083 struct net_device *dev, struct in6_addr *saddr,
84 struct in6_addr *daddr,
Daniel Borkmann13461142016-03-09 03:00:02 +010085 __u8 prio, __u8 ttl, __be32 label,
86 __be16 src_port, __be16 dst_port, bool nocheck)
Andy Zhou6a93cc92014-09-16 17:31:17 -070087{
88 struct udphdr *uh;
89 struct ipv6hdr *ip6h;
Andy Zhou6a93cc92014-09-16 17:31:17 -070090
91 __skb_push(skb, sizeof(*uh));
92 skb_reset_transport_header(skb);
93 uh = udp_hdr(skb);
94
95 uh->dest = dst_port;
96 uh->source = src_port;
97
98 uh->len = htons(skb->len);
Andy Zhou6a93cc92014-09-16 17:31:17 -070099
Andy Zhou6a93cc92014-09-16 17:31:17 -0700100 skb_dst_set(skb, dst);
101
Tom Herbertd998f8e2015-01-20 11:23:04 -0800102 udp6_set_csum(nocheck, skb, saddr, daddr, skb->len);
Andy Zhou6a93cc92014-09-16 17:31:17 -0700103
104 __skb_push(skb, sizeof(*ip6h));
105 skb_reset_network_header(skb);
106 ip6h = ipv6_hdr(skb);
Daniel Borkmann13461142016-03-09 03:00:02 +0100107 ip6_flow_hdr(ip6h, prio, label);
Andy Zhou6a93cc92014-09-16 17:31:17 -0700108 ip6h->payload_len = htons(skb->len);
109 ip6h->nexthdr = IPPROTO_UDP;
110 ip6h->hop_limit = ttl;
111 ip6h->daddr = *daddr;
112 ip6h->saddr = *saddr;
113
David Miller79b16aa2015-04-05 22:19:09 -0400114 ip6tunnel_xmit(sk, skb, dev);
Andy Zhou6a93cc92014-09-16 17:31:17 -0700115 return 0;
116}
117EXPORT_SYMBOL_GPL(udp_tunnel6_xmit_skb);
Tom Herbert3fcb95a2014-09-22 11:39:44 -0700118
119MODULE_LICENSE("GPL");