Commit | Line | Data |
---|---|---|
09c434b8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
8024e028 TH |
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> | |
c29a70d2 PS |
8 | #include <net/dst_metadata.h> |
9 | #include <net/net_namespace.h> | |
8024e028 TH |
10 | #include <net/udp.h> |
11 | #include <net/udp_tunnel.h> | |
8024e028 | 12 | |
fd384412 AZ |
13 | int udp_sock_create4(struct net *net, struct udp_port_cfg *cfg, |
14 | struct socket **sockp) | |
8024e028 | 15 | { |
6a93cc90 | 16 | int err; |
8024e028 | 17 | struct socket *sock = NULL; |
fd384412 | 18 | struct sockaddr_in udp_addr; |
8024e028 | 19 | |
26abe143 | 20 | err = sock_create_kern(net, AF_INET, SOCK_DGRAM, 0, &sock); |
fd384412 AZ |
21 | if (err < 0) |
22 | goto error; | |
8024e028 | 23 | |
da5095d0 | 24 | if (cfg->bind_ifindex) { |
8ea204c2 | 25 | err = sock_bindtoindex(sock->sk, cfg->bind_ifindex, true); |
da5095d0 AB |
26 | if (err < 0) |
27 | goto error; | |
28 | } | |
29 | ||
fd384412 AZ |
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; | |
8024e028 | 37 | |
fd384412 | 38 | if (cfg->peer_udp_port) { |
8024e028 | 39 | udp_addr.sin_family = AF_INET; |
fd384412 AZ |
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); | |
8024e028 TH |
44 | if (err < 0) |
45 | goto error; | |
8024e028 TH |
46 | } |
47 | ||
fd384412 | 48 | sock->sk->sk_no_check_tx = !cfg->use_udp_checksums; |
8024e028 TH |
49 | |
50 | *sockp = sock; | |
8024e028 TH |
51 | return 0; |
52 | ||
53 | error: | |
54 | if (sock) { | |
55 | kernel_sock_shutdown(sock, SHUT_RDWR); | |
26abe143 | 56 | sock_release(sock); |
8024e028 TH |
57 | } |
58 | *sockp = NULL; | |
59 | return err; | |
60 | } | |
fd384412 | 61 | EXPORT_SYMBOL(udp_sock_create4); |
8024e028 | 62 | |
6a93cc90 AZ |
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 */ | |
224d019c | 72 | inet_inc_convert_csum(sk); |
6a93cc90 AZ |
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; | |
a36e185e | 78 | udp_sk(sk)->encap_err_lookup = cfg->encap_err_lookup; |
6a93cc90 | 79 | udp_sk(sk)->encap_destroy = cfg->encap_destroy; |
38fd2af2 TH |
80 | udp_sk(sk)->gro_receive = cfg->gro_receive; |
81 | udp_sk(sk)->gro_complete = cfg->gro_complete; | |
6a93cc90 AZ |
82 | |
83 | udp_tunnel_encap_enable(sock); | |
84 | } | |
85 | EXPORT_SYMBOL_GPL(setup_udp_tunnel_sock); | |
86 | ||
e7b3db5e AD |
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 | ti.type = type; | |
94 | ti.sa_family = sk->sk_family; | |
95 | ti.port = inet_sk(sk)->inet_sport; | |
96 | ||
876c4384 | 97 | udp_tunnel_nic_add_port(dev, &ti); |
e7b3db5e AD |
98 | } |
99 | EXPORT_SYMBOL_GPL(udp_tunnel_push_rx_port); | |
100 | ||
296d8ee3 SD |
101 | void udp_tunnel_drop_rx_port(struct net_device *dev, struct socket *sock, |
102 | unsigned short type) | |
103 | { | |
104 | struct sock *sk = sock->sk; | |
105 | struct udp_tunnel_info ti; | |
106 | ||
296d8ee3 SD |
107 | ti.type = type; |
108 | ti.sa_family = sk->sk_family; | |
109 | ti.port = inet_sk(sk)->inet_sport; | |
110 | ||
876c4384 | 111 | udp_tunnel_nic_del_port(dev, &ti); |
296d8ee3 SD |
112 | } |
113 | EXPORT_SYMBOL_GPL(udp_tunnel_drop_rx_port); | |
114 | ||
e7b3db5e AD |
115 | /* Notify netdevs that UDP port started listening */ |
116 | void udp_tunnel_notify_add_rx_port(struct socket *sock, unsigned short type) | |
117 | { | |
118 | struct sock *sk = sock->sk; | |
119 | struct net *net = sock_net(sk); | |
120 | struct udp_tunnel_info ti; | |
121 | struct net_device *dev; | |
122 | ||
123 | ti.type = type; | |
124 | ti.sa_family = sk->sk_family; | |
125 | ti.port = inet_sk(sk)->inet_sport; | |
126 | ||
127 | rcu_read_lock(); | |
1938ee1f | 128 | for_each_netdev_rcu(net, dev) { |
876c4384 | 129 | udp_tunnel_nic_add_port(dev, &ti); |
1938ee1f | 130 | } |
e7b3db5e AD |
131 | rcu_read_unlock(); |
132 | } | |
133 | EXPORT_SYMBOL_GPL(udp_tunnel_notify_add_rx_port); | |
134 | ||
e7b3db5e AD |
135 | /* Notify netdevs that UDP port is no more listening */ |
136 | void udp_tunnel_notify_del_rx_port(struct socket *sock, unsigned short type) | |
137 | { | |
138 | struct sock *sk = sock->sk; | |
139 | struct net *net = sock_net(sk); | |
140 | struct udp_tunnel_info ti; | |
141 | struct net_device *dev; | |
142 | ||
143 | ti.type = type; | |
144 | ti.sa_family = sk->sk_family; | |
145 | ti.port = inet_sk(sk)->inet_sport; | |
146 | ||
147 | rcu_read_lock(); | |
1938ee1f | 148 | for_each_netdev_rcu(net, dev) { |
876c4384 | 149 | udp_tunnel_nic_del_port(dev, &ti); |
1938ee1f | 150 | } |
e7b3db5e AD |
151 | rcu_read_unlock(); |
152 | } | |
153 | EXPORT_SYMBOL_GPL(udp_tunnel_notify_del_rx_port); | |
154 | ||
039f5062 PS |
155 | void udp_tunnel_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb, |
156 | __be32 src, __be32 dst, __u8 tos, __u8 ttl, | |
157 | __be16 df, __be16 src_port, __be16 dst_port, | |
158 | bool xnet, bool nocheck) | |
6a93cc90 AZ |
159 | { |
160 | struct udphdr *uh; | |
161 | ||
162 | __skb_push(skb, sizeof(*uh)); | |
163 | skb_reset_transport_header(skb); | |
164 | uh = udp_hdr(skb); | |
165 | ||
166 | uh->dest = dst_port; | |
167 | uh->source = src_port; | |
168 | uh->len = htons(skb->len); | |
169 | ||
5146d1f1 BH |
170 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
171 | ||
d998f8ef | 172 | udp_set_csum(nocheck, skb, src, dst, skb->len); |
6a93cc90 | 173 | |
039f5062 | 174 | iptunnel_xmit(sk, rt, skb, src, dst, IPPROTO_UDP, tos, ttl, df, xnet); |
6a93cc90 AZ |
175 | } |
176 | EXPORT_SYMBOL_GPL(udp_tunnel_xmit_skb); | |
177 | ||
178 | void udp_tunnel_sock_release(struct socket *sock) | |
179 | { | |
180 | rcu_assign_sk_user_data(sock->sk, NULL); | |
181 | kernel_sock_shutdown(sock, SHUT_RDWR); | |
26abe143 | 182 | sock_release(sock); |
6a93cc90 AZ |
183 | } |
184 | EXPORT_SYMBOL_GPL(udp_tunnel_sock_release); | |
185 | ||
c29a70d2 PS |
186 | struct metadata_dst *udp_tun_rx_dst(struct sk_buff *skb, unsigned short family, |
187 | __be16 flags, __be64 tunnel_id, int md_size) | |
188 | { | |
189 | struct metadata_dst *tun_dst; | |
190 | struct ip_tunnel_info *info; | |
191 | ||
192 | if (family == AF_INET) | |
193 | tun_dst = ip_tun_rx_dst(skb, flags, tunnel_id, md_size); | |
194 | else | |
195 | tun_dst = ipv6_tun_rx_dst(skb, flags, tunnel_id, md_size); | |
196 | if (!tun_dst) | |
197 | return NULL; | |
198 | ||
199 | info = &tun_dst->u.tun_info; | |
200 | info->key.tp_src = udp_hdr(skb)->source; | |
201 | info->key.tp_dst = udp_hdr(skb)->dest; | |
202 | if (udp_hdr(skb)->check) | |
203 | info->key.tun_flags |= TUNNEL_CSUM; | |
204 | return tun_dst; | |
205 | } | |
206 | EXPORT_SYMBOL_GPL(udp_tun_rx_dst); | |
207 | ||
8024e028 | 208 | MODULE_LICENSE("GPL"); |