109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2fd384412SAndy Zhou #include <linux/module.h>
3fd384412SAndy Zhou #include <linux/errno.h>
4fd384412SAndy Zhou #include <linux/socket.h>
5fd384412SAndy Zhou #include <linux/udp.h>
6fd384412SAndy Zhou #include <linux/types.h>
7fd384412SAndy Zhou #include <linux/kernel.h>
8fd384412SAndy Zhou #include <linux/in6.h>
9fd384412SAndy Zhou #include <net/udp.h>
10fd384412SAndy Zhou #include <net/udp_tunnel.h>
11fd384412SAndy Zhou #include <net/net_namespace.h>
12fd384412SAndy Zhou #include <net/netns/generic.h>
13fd384412SAndy Zhou #include <net/ip6_tunnel.h>
14fd384412SAndy Zhou #include <net/ip6_checksum.h>
15fd384412SAndy Zhou
udp_sock_create6(struct net * net,struct udp_port_cfg * cfg,struct socket ** sockp)16fd384412SAndy Zhou int udp_sock_create6(struct net *net, struct udp_port_cfg *cfg,
17fd384412SAndy Zhou struct socket **sockp)
18fd384412SAndy Zhou {
19fb242745SCong Wang struct sockaddr_in6 udp6_addr = {};
20fd384412SAndy Zhou int err;
21fd384412SAndy Zhou struct socket *sock = NULL;
22fd384412SAndy Zhou
2326abe143SEric W. Biederman err = sock_create_kern(net, AF_INET6, SOCK_DGRAM, 0, &sock);
24fd384412SAndy Zhou if (err < 0)
25fd384412SAndy Zhou goto error;
26fd384412SAndy Zhou
27a43a9ef6SJiri Benc if (cfg->ipv6_v6only) {
289b115749SChristoph Hellwig err = ip6_sock_set_v6only(sock->sk);
29a43a9ef6SJiri Benc if (err < 0)
30a43a9ef6SJiri Benc goto error;
31a43a9ef6SJiri Benc }
32da5095d0SAlexis Bauvin if (cfg->bind_ifindex) {
33*8ea204c2SFerenc Fejes err = sock_bindtoindex(sock->sk, cfg->bind_ifindex, true);
34da5095d0SAlexis Bauvin if (err < 0)
35da5095d0SAlexis Bauvin goto error;
36da5095d0SAlexis Bauvin }
37a43a9ef6SJiri Benc
38fd384412SAndy Zhou udp6_addr.sin6_family = AF_INET6;
39fd384412SAndy Zhou memcpy(&udp6_addr.sin6_addr, &cfg->local_ip6,
40fd384412SAndy Zhou sizeof(udp6_addr.sin6_addr));
41fd384412SAndy Zhou udp6_addr.sin6_port = cfg->local_udp_port;
42fd384412SAndy Zhou err = kernel_bind(sock, (struct sockaddr *)&udp6_addr,
43fd384412SAndy Zhou sizeof(udp6_addr));
44fd384412SAndy Zhou if (err < 0)
45fd384412SAndy Zhou goto error;
46fd384412SAndy Zhou
47fd384412SAndy Zhou if (cfg->peer_udp_port) {
48fb242745SCong Wang memset(&udp6_addr, 0, sizeof(udp6_addr));
49fd384412SAndy Zhou udp6_addr.sin6_family = AF_INET6;
50fd384412SAndy Zhou memcpy(&udp6_addr.sin6_addr, &cfg->peer_ip6,
51fd384412SAndy Zhou sizeof(udp6_addr.sin6_addr));
52fd384412SAndy Zhou udp6_addr.sin6_port = cfg->peer_udp_port;
53fd384412SAndy Zhou err = kernel_connect(sock,
54fd384412SAndy Zhou (struct sockaddr *)&udp6_addr,
55fd384412SAndy Zhou sizeof(udp6_addr), 0);
56fd384412SAndy Zhou }
57fd384412SAndy Zhou if (err < 0)
58fd384412SAndy Zhou goto error;
59fd384412SAndy Zhou
60fd384412SAndy Zhou udp_set_no_check6_tx(sock->sk, !cfg->use_udp6_tx_checksums);
61fd384412SAndy Zhou udp_set_no_check6_rx(sock->sk, !cfg->use_udp6_rx_checksums);
62fd384412SAndy Zhou
63fd384412SAndy Zhou *sockp = sock;
64fd384412SAndy Zhou return 0;
65fd384412SAndy Zhou
66fd384412SAndy Zhou error:
67fd384412SAndy Zhou if (sock) {
68fd384412SAndy Zhou kernel_sock_shutdown(sock, SHUT_RDWR);
6926abe143SEric W. Biederman sock_release(sock);
70fd384412SAndy Zhou }
71fd384412SAndy Zhou *sockp = NULL;
72fd384412SAndy Zhou return err;
73fd384412SAndy Zhou }
74fd384412SAndy Zhou EXPORT_SYMBOL_GPL(udp_sock_create6);
756a93cc90SAndy Zhou
udp_tunnel6_xmit_skb(struct dst_entry * dst,struct sock * sk,struct sk_buff * skb,struct net_device * dev,struct in6_addr * saddr,struct in6_addr * daddr,__u8 prio,__u8 ttl,__be32 label,__be16 src_port,__be16 dst_port,bool nocheck)7679b16aadSDavid Miller int udp_tunnel6_xmit_skb(struct dst_entry *dst, struct sock *sk,
7779b16aadSDavid Miller struct sk_buff *skb,
78d998f8efSTom Herbert struct net_device *dev, struct in6_addr *saddr,
79d998f8efSTom Herbert struct in6_addr *daddr,
8013461144SDaniel Borkmann __u8 prio, __u8 ttl, __be32 label,
8113461144SDaniel Borkmann __be16 src_port, __be16 dst_port, bool nocheck)
826a93cc90SAndy Zhou {
836a93cc90SAndy Zhou struct udphdr *uh;
846a93cc90SAndy Zhou struct ipv6hdr *ip6h;
856a93cc90SAndy Zhou
866a93cc90SAndy Zhou __skb_push(skb, sizeof(*uh));
876a93cc90SAndy Zhou skb_reset_transport_header(skb);
886a93cc90SAndy Zhou uh = udp_hdr(skb);
896a93cc90SAndy Zhou
906a93cc90SAndy Zhou uh->dest = dst_port;
916a93cc90SAndy Zhou uh->source = src_port;
926a93cc90SAndy Zhou
936a93cc90SAndy Zhou uh->len = htons(skb->len);
946a93cc90SAndy Zhou
956a93cc90SAndy Zhou skb_dst_set(skb, dst);
966a93cc90SAndy Zhou
97d998f8efSTom Herbert udp6_set_csum(nocheck, skb, saddr, daddr, skb->len);
986a93cc90SAndy Zhou
996a93cc90SAndy Zhou __skb_push(skb, sizeof(*ip6h));
1006a93cc90SAndy Zhou skb_reset_network_header(skb);
1016a93cc90SAndy Zhou ip6h = ipv6_hdr(skb);
10213461144SDaniel Borkmann ip6_flow_hdr(ip6h, prio, label);
1036a93cc90SAndy Zhou ip6h->payload_len = htons(skb->len);
1046a93cc90SAndy Zhou ip6h->nexthdr = IPPROTO_UDP;
1056a93cc90SAndy Zhou ip6h->hop_limit = ttl;
1066a93cc90SAndy Zhou ip6h->daddr = *daddr;
1076a93cc90SAndy Zhou ip6h->saddr = *saddr;
1086a93cc90SAndy Zhou
10979b16aadSDavid Miller ip6tunnel_xmit(sk, skb, dev);
1106a93cc90SAndy Zhou return 0;
1116a93cc90SAndy Zhou }
1126a93cc90SAndy Zhou EXPORT_SYMBOL_GPL(udp_tunnel6_xmit_skb);
1133fcb95a8STom Herbert
1143fcb95a8STom Herbert MODULE_LICENSE("GPL");
115