| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#include "main.h" |
| 10 |
#include "bind.h" |
| 11 |
#include "ovpn.h" |
| 12 |
#include "ovpnstruct.h" |
| 13 |
#include "peer.h" |
| 14 |
#include "proto.h" |
| 15 |
#include "skb.h" |
| 16 |
#include "udp.h" |
| 17 |
|
| 18 |
#include <linux/inetdevice.h> |
| 19 |
#include <linux/skbuff.h> |
| 20 |
#include <linux/socket.h> |
| 21 |
#include <net/addrconf.h> |
| 22 |
#include <net/dst_cache.h> |
| 23 |
#include <net/route.h> |
| 24 |
#include <net/ipv6.h> |
| 25 |
#include <net/udp_tunnel.h> |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
static int ovpn_udp_encap_recv(struct sock *sk, struct sk_buff *skb) |
| 41 |
{ |
| 42 |
struct ovpn_peer *peer = NULL; |
| 43 |
struct ovpn_struct *ovpn; |
| 44 |
u32 peer_id; |
| 45 |
u8 opcode; |
| 46 |
int ret; |
| 47 |
|
| 48 |
ovpn = ovpn_from_udp_sock(sk); |
| 49 |
if (unlikely(!ovpn)) { |
| 50 |
net_err_ratelimited("%s: cannot obtain ovpn object from UDP socket\n", __func__); |
| 51 |
goto drop; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
if (unlikely(!pskb_may_pull(skb, sizeof(struct udphdr) + 4))) { |
| 58 |
net_dbg_ratelimited("%s: packet too small\n", __func__); |
| 59 |
goto drop; |
| 60 |
} |
| 61 |
|
| 62 |
opcode = ovpn_opcode_from_skb(skb, sizeof(struct udphdr)); |
| 63 |
if (likely(opcode == OVPN_DATA_V2)) { |
| 64 |
peer_id = ovpn_peer_id_from_skb(skb, sizeof(struct udphdr)); |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
if (peer_id != OVPN_PEER_ID_UNDEF) { |
| 70 |
peer = ovpn_peer_lookup_id(ovpn, peer_id); |
| 71 |
if (!peer) { |
| 72 |
net_err_ratelimited("%s: received data from unknown peer (id: %d)\n", |
| 73 |
__func__, peer_id); |
| 74 |
goto drop; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
ovpn_peer_float(peer, skb); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
if (!peer) { |
| 83 |
|
| 84 |
peer = ovpn_peer_lookup_transp_addr(ovpn, skb); |
| 85 |
if (unlikely(!peer)) { |
| 86 |
if (opcode != OVPN_DATA_V2) { |
| 87 |
netdev_dbg(ovpn->dev, |
| 88 |
"%s: control packet from unknown peer, sending to userspace", |
| 89 |
__func__); |
| 90 |
return 1; |
| 91 |
} |
| 92 |
|
| 93 |
netdev_dbg(ovpn->dev, |
| 94 |
"%s: received data with undef peer-id from unknown source\n", |
| 95 |
__func__); |
| 96 |
goto drop; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
if (unlikely(opcode != OVPN_DATA_V2)) { |
| 106 |
ovpn_peer_put(peer); |
| 107 |
return 1; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
__skb_pull(skb, sizeof(struct udphdr)); |
| 112 |
|
| 113 |
ret = ovpn_recv(ovpn, peer, skb); |
| 114 |
if (unlikely(ret < 0)) { |
| 115 |
net_err_ratelimited("%s: cannot handle incoming packet from peer %d: %d\n", |
| 116 |
__func__, peer->id, ret); |
| 117 |
goto drop; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
return ret; |
| 124 |
|
| 125 |
drop: |
| 126 |
if (peer) |
| 127 |
ovpn_peer_put(peer); |
| 128 |
kfree_skb(skb); |
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
static int ovpn_udp4_output(struct ovpn_struct *ovpn, struct ovpn_bind *bind, |
| 133 |
struct dst_cache *cache, struct sock *sk, |
| 134 |
struct sk_buff *skb) |
| 135 |
{ |
| 136 |
struct rtable *rt; |
| 137 |
struct flowi4 fl = { |
| 138 |
.saddr = bind->local.ipv4.s_addr, |
| 139 |
.daddr = bind->sa.in4.sin_addr.s_addr, |
| 140 |
.fl4_sport = inet_sk(sk)->inet_sport, |
| 141 |
.fl4_dport = bind->sa.in4.sin_port, |
| 142 |
.flowi4_proto = sk->sk_protocol, |
| 143 |
.flowi4_mark = sk->sk_mark, |
| 144 |
}; |
| 145 |
int ret; |
| 146 |
|
| 147 |
local_bh_disable(); |
| 148 |
rt = dst_cache_get_ip4(cache, &fl.saddr); |
| 149 |
if (rt) |
| 150 |
goto transmit; |
| 151 |
|
| 152 |
if (unlikely(!inet_confirm_addr(sock_net(sk), NULL, 0, fl.saddr, RT_SCOPE_HOST))) { |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
fl.saddr = 0; |
| 157 |
bind->local.ipv4.s_addr = 0; |
| 158 |
dst_cache_reset(cache); |
| 159 |
} |
| 160 |
|
| 161 |
rt = ip_route_output_flow(sock_net(sk), &fl, sk); |
| 162 |
if (IS_ERR(rt) && PTR_ERR(rt) == -EINVAL) { |
| 163 |
fl.saddr = 0; |
| 164 |
bind->local.ipv4.s_addr = 0; |
| 165 |
dst_cache_reset(cache); |
| 166 |
|
| 167 |
rt = ip_route_output_flow(sock_net(sk), &fl, sk); |
| 168 |
} |
| 169 |
|
| 170 |
if (IS_ERR(rt)) { |
| 171 |
ret = PTR_ERR(rt); |
| 172 |
net_dbg_ratelimited("%s: no route to host %pISpc: %d\n", ovpn->dev->name, |
| 173 |
&bind->sa.in4, ret); |
| 174 |
goto err; |
| 175 |
} |
| 176 |
dst_cache_set_ip4(cache, &rt->dst, fl.saddr); |
| 177 |
|
| 178 |
transmit: |
| 179 |
udp_tunnel_xmit_skb(rt, sk, skb, fl.saddr, fl.daddr, 0, |
| 180 |
ip4_dst_hoplimit(&rt->dst), 0, fl.fl4_sport, |
| 181 |
fl.fl4_dport, false, sk->sk_no_check_tx, 0); |
| 182 |
ret = 0; |
| 183 |
err: |
| 184 |
local_bh_enable(); |
| 185 |
return ret; |
| 186 |
} |
| 187 |
|
| 188 |
#if IS_ENABLED(CONFIG_IPV6) |
| 189 |
static int ovpn_udp6_output(struct ovpn_struct *ovpn, struct ovpn_bind *bind, |
| 190 |
struct dst_cache *cache, struct sock *sk, |
| 191 |
struct sk_buff *skb) |
| 192 |
{ |
| 193 |
struct dst_entry *dst; |
| 194 |
int ret; |
| 195 |
|
| 196 |
struct flowi6 fl = { |
| 197 |
.saddr = bind->local.ipv6, |
| 198 |
.daddr = bind->sa.in6.sin6_addr, |
| 199 |
.fl6_sport = inet_sk(sk)->inet_sport, |
| 200 |
.fl6_dport = bind->sa.in6.sin6_port, |
| 201 |
.flowi6_proto = sk->sk_protocol, |
| 202 |
.flowi6_mark = sk->sk_mark, |
| 203 |
.flowi6_oif = bind->sa.in6.sin6_scope_id, |
| 204 |
}; |
| 205 |
|
| 206 |
local_bh_disable(); |
| 207 |
dst = dst_cache_get_ip6(cache, &fl.saddr); |
| 208 |
if (dst) |
| 209 |
goto transmit; |
| 210 |
|
| 211 |
if (unlikely(!ipv6_chk_addr(sock_net(sk), &fl.saddr, NULL, 0))) { |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
fl.saddr = in6addr_any; |
| 216 |
bind->local.ipv6 = in6addr_any; |
| 217 |
dst_cache_reset(cache); |
| 218 |
} |
| 219 |
|
| 220 |
dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl, NULL); |
| 221 |
if (IS_ERR(dst)) { |
| 222 |
ret = PTR_ERR(dst); |
| 223 |
net_dbg_ratelimited("%s: no route to host %pISpc: %d\n", ovpn->dev->name, |
| 224 |
&bind->sa.in6, ret); |
| 225 |
goto err; |
| 226 |
} |
| 227 |
dst_cache_set_ip6(cache, dst, &fl.saddr); |
| 228 |
|
| 229 |
transmit: |
| 230 |
udp_tunnel6_xmit_skb(dst, sk, skb, skb->dev, &fl.saddr, &fl.daddr, 0, |
| 231 |
ip6_dst_hoplimit(dst), 0, fl.fl6_sport, |
| 232 |
fl.fl6_dport, udp_get_no_check6_tx(sk), 0); |
| 233 |
ret = 0; |
| 234 |
err: |
| 235 |
local_bh_enable(); |
| 236 |
return ret; |
| 237 |
} |
| 238 |
#endif |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
static int ovpn_udp_output(struct ovpn_struct *ovpn, struct ovpn_bind *bind, |
| 246 |
struct dst_cache *cache, struct sock *sk, |
| 247 |
struct sk_buff *skb) |
| 248 |
{ |
| 249 |
int ret; |
| 250 |
|
| 251 |
ovpn_rcu_lockdep_assert_held(); |
| 252 |
|
| 253 |
|
| 254 |
if (!skb->destructor) |
| 255 |
skb->sk = NULL; |
| 256 |
|
| 257 |
|
| 258 |
skb->ignore_df = 1; |
| 259 |
|
| 260 |
switch (bind->sa.in4.sin_family) { |
| 261 |
case AF_INET: |
| 262 |
ret = ovpn_udp4_output(ovpn, bind, cache, sk, skb); |
| 263 |
break; |
| 264 |
#if IS_ENABLED(CONFIG_IPV6) |
| 265 |
case AF_INET6: |
| 266 |
ret = ovpn_udp6_output(ovpn, bind, cache, sk, skb); |
| 267 |
break; |
| 268 |
#endif |
| 269 |
default: |
| 270 |
ret = -EAFNOSUPPORT; |
| 271 |
break; |
| 272 |
} |
| 273 |
|
| 274 |
return ret; |
| 275 |
} |
| 276 |
|
| 277 |
void ovpn_udp_send_skb(struct ovpn_struct *ovpn, struct ovpn_peer *peer, |
| 278 |
struct sk_buff *skb) |
| 279 |
{ |
| 280 |
struct ovpn_bind *bind; |
| 281 |
struct socket *sock; |
| 282 |
int ret = -1; |
| 283 |
|
| 284 |
skb->dev = ovpn->dev; |
| 285 |
|
| 286 |
skb->ip_summed = CHECKSUM_NONE; |
| 287 |
|
| 288 |
|
| 289 |
sock = peer->sock->sock; |
| 290 |
if (unlikely(!sock)) { |
| 291 |
net_warn_ratelimited("%s: no sock for remote peer\n", __func__); |
| 292 |
goto out; |
| 293 |
} |
| 294 |
|
| 295 |
rcu_read_lock(); |
| 296 |
|
| 297 |
bind = rcu_dereference(peer->bind); |
| 298 |
if (unlikely(!bind)) { |
| 299 |
net_warn_ratelimited("%s: no bind for remote peer\n", __func__); |
| 300 |
goto out_unlock; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
ret = ovpn_udp_output(ovpn, bind, &peer->dst_cache, sock->sk, skb); |
| 305 |
|
| 306 |
out_unlock: |
| 307 |
rcu_read_unlock(); |
| 308 |
out: |
| 309 |
if (ret < 0) |
| 310 |
kfree_skb(skb); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
int ovpn_udp_socket_attach(struct socket *sock, struct ovpn_struct *ovpn) |
| 315 |
{ |
| 316 |
struct udp_tunnel_sock_cfg cfg = { |
| 317 |
.sk_user_data = ovpn, |
| 318 |
.encap_type = UDP_ENCAP_OVPNINUDP, |
| 319 |
.encap_rcv = ovpn_udp_encap_recv, |
| 320 |
}; |
| 321 |
struct ovpn_socket *old_data; |
| 322 |
|
| 323 |
|
| 324 |
if (sock->sk->sk_protocol != IPPROTO_UDP) { |
| 325 |
netdev_err(ovpn->dev, "%s: expected UDP socket\n", __func__); |
| 326 |
return -EINVAL; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
rcu_read_lock(); |
| 331 |
old_data = rcu_dereference_sk_user_data(sock->sk); |
| 332 |
rcu_read_unlock(); |
| 333 |
if (old_data) { |
| 334 |
if (old_data->ovpn == ovpn) { |
| 335 |
netdev_dbg(ovpn->dev, |
| 336 |
"%s: provided socket already owned by this interface\n", |
| 337 |
__func__); |
| 338 |
return -EALREADY; |
| 339 |
} |
| 340 |
|
| 341 |
netdev_err(ovpn->dev, "%s: provided socket already taken by other user\n", |
| 342 |
__func__); |
| 343 |
return -EBUSY; |
| 344 |
} |
| 345 |
|
| 346 |
setup_udp_tunnel_sock(sock_net(sock->sk), sock->sk, &cfg); |
| 347 |
|
| 348 |
return 0; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
void ovpn_udp_socket_detach(struct socket *sock) |
| 353 |
{ |
| 354 |
struct udp_tunnel_sock_cfg cfg = { }; |
| 355 |
|
| 356 |
setup_udp_tunnel_sock(sock_net(sock->sk), sock->sk, &cfg); |
| 357 |
} |