Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 2 | #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 | |
| 16 | int udp_sock_create6(struct net *net, struct udp_port_cfg *cfg, |
| 17 | struct socket **sockp) |
| 18 | { |
Cong Wang | fb24274 | 2018-12-18 21:17:44 -0800 | [diff] [blame] | 19 | struct sockaddr_in6 udp6_addr = {}; |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 20 | int err; |
| 21 | struct socket *sock = NULL; |
| 22 | |
Eric W. Biederman | 26abe14 | 2015-05-08 21:10:31 -0500 | [diff] [blame] | 23 | err = sock_create_kern(net, AF_INET6, SOCK_DGRAM, 0, &sock); |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 24 | if (err < 0) |
| 25 | goto error; |
| 26 | |
Jiri Benc | a43a9ef | 2015-08-28 20:48:22 +0200 | [diff] [blame] | 27 | if (cfg->ipv6_v6only) { |
Christoph Hellwig | 9b11574 | 2020-05-28 07:12:31 +0200 | [diff] [blame] | 28 | err = ip6_sock_set_v6only(sock->sk); |
Jiri Benc | a43a9ef | 2015-08-28 20:48:22 +0200 | [diff] [blame] | 29 | if (err < 0) |
| 30 | goto error; |
| 31 | } |
Alexis Bauvin | da5095d05 | 2018-12-03 10:54:38 +0100 | [diff] [blame] | 32 | if (cfg->bind_ifindex) { |
Ferenc Fejes | 8ea204c | 2020-05-30 23:09:00 +0200 | [diff] [blame] | 33 | err = sock_bindtoindex(sock->sk, cfg->bind_ifindex, true); |
Alexis Bauvin | da5095d05 | 2018-12-03 10:54:38 +0100 | [diff] [blame] | 34 | if (err < 0) |
| 35 | goto error; |
| 36 | } |
Jiri Benc | a43a9ef | 2015-08-28 20:48:22 +0200 | [diff] [blame] | 37 | |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 38 | udp6_addr.sin6_family = AF_INET6; |
| 39 | memcpy(&udp6_addr.sin6_addr, &cfg->local_ip6, |
| 40 | sizeof(udp6_addr.sin6_addr)); |
| 41 | udp6_addr.sin6_port = cfg->local_udp_port; |
| 42 | err = kernel_bind(sock, (struct sockaddr *)&udp6_addr, |
| 43 | sizeof(udp6_addr)); |
| 44 | if (err < 0) |
| 45 | goto error; |
| 46 | |
| 47 | if (cfg->peer_udp_port) { |
Cong Wang | fb24274 | 2018-12-18 21:17:44 -0800 | [diff] [blame] | 48 | memset(&udp6_addr, 0, sizeof(udp6_addr)); |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 49 | udp6_addr.sin6_family = AF_INET6; |
| 50 | memcpy(&udp6_addr.sin6_addr, &cfg->peer_ip6, |
| 51 | sizeof(udp6_addr.sin6_addr)); |
| 52 | udp6_addr.sin6_port = cfg->peer_udp_port; |
| 53 | err = kernel_connect(sock, |
| 54 | (struct sockaddr *)&udp6_addr, |
| 55 | sizeof(udp6_addr), 0); |
| 56 | } |
| 57 | if (err < 0) |
| 58 | goto error; |
| 59 | |
| 60 | udp_set_no_check6_tx(sock->sk, !cfg->use_udp6_tx_checksums); |
| 61 | udp_set_no_check6_rx(sock->sk, !cfg->use_udp6_rx_checksums); |
| 62 | |
| 63 | *sockp = sock; |
| 64 | return 0; |
| 65 | |
| 66 | error: |
| 67 | if (sock) { |
| 68 | kernel_sock_shutdown(sock, SHUT_RDWR); |
Eric W. Biederman | 26abe14 | 2015-05-08 21:10:31 -0500 | [diff] [blame] | 69 | sock_release(sock); |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 70 | } |
| 71 | *sockp = NULL; |
| 72 | return err; |
| 73 | } |
| 74 | EXPORT_SYMBOL_GPL(udp_sock_create6); |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 75 | |
David Miller | 79b16aa | 2015-04-05 22:19:09 -0400 | [diff] [blame] | 76 | int udp_tunnel6_xmit_skb(struct dst_entry *dst, struct sock *sk, |
| 77 | struct sk_buff *skb, |
Tom Herbert | d998f8e | 2015-01-20 11:23:04 -0800 | [diff] [blame] | 78 | struct net_device *dev, struct in6_addr *saddr, |
| 79 | struct in6_addr *daddr, |
Daniel Borkmann | 1346114 | 2016-03-09 03:00:02 +0100 | [diff] [blame] | 80 | __u8 prio, __u8 ttl, __be32 label, |
| 81 | __be16 src_port, __be16 dst_port, bool nocheck) |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 82 | { |
| 83 | struct udphdr *uh; |
| 84 | struct ipv6hdr *ip6h; |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 85 | |
| 86 | __skb_push(skb, sizeof(*uh)); |
| 87 | skb_reset_transport_header(skb); |
| 88 | uh = udp_hdr(skb); |
| 89 | |
| 90 | uh->dest = dst_port; |
| 91 | uh->source = src_port; |
| 92 | |
| 93 | uh->len = htons(skb->len); |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 94 | |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 95 | skb_dst_set(skb, dst); |
| 96 | |
Tom Herbert | d998f8e | 2015-01-20 11:23:04 -0800 | [diff] [blame] | 97 | udp6_set_csum(nocheck, skb, saddr, daddr, skb->len); |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 98 | |
| 99 | __skb_push(skb, sizeof(*ip6h)); |
| 100 | skb_reset_network_header(skb); |
| 101 | ip6h = ipv6_hdr(skb); |
Daniel Borkmann | 1346114 | 2016-03-09 03:00:02 +0100 | [diff] [blame] | 102 | ip6_flow_hdr(ip6h, prio, label); |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 103 | ip6h->payload_len = htons(skb->len); |
| 104 | ip6h->nexthdr = IPPROTO_UDP; |
| 105 | ip6h->hop_limit = ttl; |
| 106 | ip6h->daddr = *daddr; |
| 107 | ip6h->saddr = *saddr; |
| 108 | |
David Miller | 79b16aa | 2015-04-05 22:19:09 -0400 | [diff] [blame] | 109 | ip6tunnel_xmit(sk, skb, dev); |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | EXPORT_SYMBOL_GPL(udp_tunnel6_xmit_skb); |
Tom Herbert | 3fcb95a | 2014-09-22 11:39:44 -0700 | [diff] [blame] | 113 | |
| 114 | MODULE_LICENSE("GPL"); |