1 // SPDX-License-Identifier: GPL-2.0-only
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 <net/dst_metadata.h>
9 #include <net/net_namespace.h>
10 #include <net/udp.h>
11 #include <net/udp_tunnel.h>
12
udp_sock_create4(struct net * net,struct udp_port_cfg * cfg,struct socket ** sockp)13 int udp_sock_create4(struct net *net, struct udp_port_cfg *cfg,
14 struct socket **sockp)
15 {
16 int err;
17 struct socket *sock = NULL;
18 struct sockaddr_in udp_addr;
19
20 err = sock_create_kern(net, AF_INET, SOCK_DGRAM, 0, &sock);
21 if (err < 0)
22 goto error;
23
24 if (cfg->bind_ifindex) {
25 err = sock_bindtoindex(sock->sk, cfg->bind_ifindex, true);
26 if (err < 0)
27 goto error;
28 }
29
30 udp_addr.sin_family = AF_INET;
31 udp_addr.sin_addr = cfg->local_ip;
32 udp_addr.sin_port = cfg->local_udp_port;
33 err = kernel_bind(sock, (struct sockaddr *)&udp_addr,
34 sizeof(udp_addr));
35 if (err < 0)
36 goto error;
37
38 if (cfg->peer_udp_port) {
39 udp_addr.sin_family = AF_INET;
40 udp_addr.sin_addr = cfg->peer_ip;
41 udp_addr.sin_port = cfg->peer_udp_port;
42 err = kernel_connect(sock, (struct sockaddr *)&udp_addr,
43 sizeof(udp_addr), 0);
44 if (err < 0)
45 goto error;
46 }
47
48 sock->sk->sk_no_check_tx = !cfg->use_udp_checksums;
49
50 *sockp = sock;
51 return 0;
52
53 error:
54 if (sock) {
55 kernel_sock_shutdown(sock, SHUT_RDWR);
56 sock_release(sock);
57 }
58 *sockp = NULL;
59 return err;
60 }
61 EXPORT_SYMBOL(udp_sock_create4);
62
setup_udp_tunnel_sock(struct net * net,struct socket * sock,struct udp_tunnel_sock_cfg * cfg)63 void setup_udp_tunnel_sock(struct net *net, struct socket *sock,
64 struct udp_tunnel_sock_cfg *cfg)
65 {
66 struct sock *sk = sock->sk;
67
68 /* Disable multicast loopback */
69 inet_sk(sk)->mc_loop = 0;
70
71 /* Enable CHECKSUM_UNNECESSARY to CHECKSUM_COMPLETE conversion */
72 inet_inc_convert_csum(sk);
73
74 rcu_assign_sk_user_data(sk, cfg->sk_user_data);
75
76 udp_sk(sk)->encap_type = cfg->encap_type;
77 udp_sk(sk)->encap_rcv = cfg->encap_rcv;
78 udp_sk(sk)->encap_err_lookup = cfg->encap_err_lookup;
79 udp_sk(sk)->encap_destroy = cfg->encap_destroy;
80 udp_sk(sk)->gro_receive = cfg->gro_receive;
81 udp_sk(sk)->gro_complete = cfg->gro_complete;
82
83 udp_tunnel_encap_enable(sock);
84 }
85 EXPORT_SYMBOL_GPL(setup_udp_tunnel_sock);
86
udp_tunnel_push_rx_port(struct net_device * dev,struct socket * sock,unsigned short type)87 void udp_tunnel_push_rx_port(struct net_device *dev, struct socket *sock,
88 unsigned short type)
89 {
90 struct sock *sk = sock->sk;
91 struct udp_tunnel_info ti;
92
93 if (!dev->netdev_ops->ndo_udp_tunnel_add ||
94 !(dev->features & NETIF_F_RX_UDP_TUNNEL_PORT))
95 return;
96
97 ti.type = type;
98 ti.sa_family = sk->sk_family;
99 ti.port = inet_sk(sk)->inet_sport;
100
101 dev->netdev_ops->ndo_udp_tunnel_add(dev, &ti);
102 }
103 EXPORT_SYMBOL_GPL(udp_tunnel_push_rx_port);
104
udp_tunnel_drop_rx_port(struct net_device * dev,struct socket * sock,unsigned short type)105 void udp_tunnel_drop_rx_port(struct net_device *dev, struct socket *sock,
106 unsigned short type)
107 {
108 struct sock *sk = sock->sk;
109 struct udp_tunnel_info ti;
110
111 if (!dev->netdev_ops->ndo_udp_tunnel_del ||
112 !(dev->features & NETIF_F_RX_UDP_TUNNEL_PORT))
113 return;
114
115 ti.type = type;
116 ti.sa_family = sk->sk_family;
117 ti.port = inet_sk(sk)->inet_sport;
118
119 dev->netdev_ops->ndo_udp_tunnel_del(dev, &ti);
120 }
121 EXPORT_SYMBOL_GPL(udp_tunnel_drop_rx_port);
122
123 /* Notify netdevs that UDP port started listening */
udp_tunnel_notify_add_rx_port(struct socket * sock,unsigned short type)124 void udp_tunnel_notify_add_rx_port(struct socket *sock, unsigned short type)
125 {
126 struct sock *sk = sock->sk;
127 struct net *net = sock_net(sk);
128 struct udp_tunnel_info ti;
129 struct net_device *dev;
130
131 ti.type = type;
132 ti.sa_family = sk->sk_family;
133 ti.port = inet_sk(sk)->inet_sport;
134
135 rcu_read_lock();
136 for_each_netdev_rcu(net, dev) {
137 if (!dev->netdev_ops->ndo_udp_tunnel_add)
138 continue;
139 if (!(dev->features & NETIF_F_RX_UDP_TUNNEL_PORT))
140 continue;
141 dev->netdev_ops->ndo_udp_tunnel_add(dev, &ti);
142 }
143 rcu_read_unlock();
144 }
145 EXPORT_SYMBOL_GPL(udp_tunnel_notify_add_rx_port);
146
147 /* Notify netdevs that UDP port is no more listening */
udp_tunnel_notify_del_rx_port(struct socket * sock,unsigned short type)148 void udp_tunnel_notify_del_rx_port(struct socket *sock, unsigned short type)
149 {
150 struct sock *sk = sock->sk;
151 struct net *net = sock_net(sk);
152 struct udp_tunnel_info ti;
153 struct net_device *dev;
154
155 ti.type = type;
156 ti.sa_family = sk->sk_family;
157 ti.port = inet_sk(sk)->inet_sport;
158
159 rcu_read_lock();
160 for_each_netdev_rcu(net, dev) {
161 if (!dev->netdev_ops->ndo_udp_tunnel_del)
162 continue;
163 if (!(dev->features & NETIF_F_RX_UDP_TUNNEL_PORT))
164 continue;
165 dev->netdev_ops->ndo_udp_tunnel_del(dev, &ti);
166 }
167 rcu_read_unlock();
168 }
169 EXPORT_SYMBOL_GPL(udp_tunnel_notify_del_rx_port);
170
udp_tunnel_xmit_skb(struct rtable * rt,struct sock * sk,struct sk_buff * skb,__be32 src,__be32 dst,__u8 tos,__u8 ttl,__be16 df,__be16 src_port,__be16 dst_port,bool xnet,bool nocheck)171 void udp_tunnel_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
172 __be32 src, __be32 dst, __u8 tos, __u8 ttl,
173 __be16 df, __be16 src_port, __be16 dst_port,
174 bool xnet, bool nocheck)
175 {
176 struct udphdr *uh;
177
178 __skb_push(skb, sizeof(*uh));
179 skb_reset_transport_header(skb);
180 uh = udp_hdr(skb);
181
182 uh->dest = dst_port;
183 uh->source = src_port;
184 uh->len = htons(skb->len);
185
186 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
187
188 udp_set_csum(nocheck, skb, src, dst, skb->len);
189
190 iptunnel_xmit(sk, rt, skb, src, dst, IPPROTO_UDP, tos, ttl, df, xnet);
191 }
192 EXPORT_SYMBOL_GPL(udp_tunnel_xmit_skb);
193
udp_tunnel_sock_release(struct socket * sock)194 void udp_tunnel_sock_release(struct socket *sock)
195 {
196 rcu_assign_sk_user_data(sock->sk, NULL);
197 kernel_sock_shutdown(sock, SHUT_RDWR);
198 sock_release(sock);
199 }
200 EXPORT_SYMBOL_GPL(udp_tunnel_sock_release);
201
udp_tun_rx_dst(struct sk_buff * skb,unsigned short family,__be16 flags,__be64 tunnel_id,int md_size)202 struct metadata_dst *udp_tun_rx_dst(struct sk_buff *skb, unsigned short family,
203 __be16 flags, __be64 tunnel_id, int md_size)
204 {
205 struct metadata_dst *tun_dst;
206 struct ip_tunnel_info *info;
207
208 if (family == AF_INET)
209 tun_dst = ip_tun_rx_dst(skb, flags, tunnel_id, md_size);
210 else
211 tun_dst = ipv6_tun_rx_dst(skb, flags, tunnel_id, md_size);
212 if (!tun_dst)
213 return NULL;
214
215 info = &tun_dst->u.tun_info;
216 info->key.tp_src = udp_hdr(skb)->source;
217 info->key.tp_dst = udp_hdr(skb)->dest;
218 if (udp_hdr(skb)->check)
219 info->key.tun_flags |= TUNNEL_CSUM;
220 return tun_dst;
221 }
222 EXPORT_SYMBOL_GPL(udp_tun_rx_dst);
223
224 MODULE_LICENSE("GPL");
225