Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
0004  */
0005 
0006 #include "device.h"
0007 #include "peer.h"
0008 #include "socket.h"
0009 #include "queueing.h"
0010 #include "messages.h"
0011 
0012 #include <linux/ctype.h>
0013 #include <linux/net.h>
0014 #include <linux/if_vlan.h>
0015 #include <linux/if_ether.h>
0016 #include <linux/inetdevice.h>
0017 #include <net/udp_tunnel.h>
0018 #include <net/ipv6.h>
0019 
0020 static int send4(struct wg_device *wg, struct sk_buff *skb,
0021          struct endpoint *endpoint, u8 ds, struct dst_cache *cache)
0022 {
0023     struct flowi4 fl = {
0024         .saddr = endpoint->src4.s_addr,
0025         .daddr = endpoint->addr4.sin_addr.s_addr,
0026         .fl4_dport = endpoint->addr4.sin_port,
0027         .flowi4_mark = wg->fwmark,
0028         .flowi4_proto = IPPROTO_UDP
0029     };
0030     struct rtable *rt = NULL;
0031     struct sock *sock;
0032     int ret = 0;
0033 
0034     skb_mark_not_on_list(skb);
0035     skb->dev = wg->dev;
0036     skb->mark = wg->fwmark;
0037 
0038     rcu_read_lock_bh();
0039     sock = rcu_dereference_bh(wg->sock4);
0040 
0041     if (unlikely(!sock)) {
0042         ret = -ENONET;
0043         goto err;
0044     }
0045 
0046     fl.fl4_sport = inet_sk(sock)->inet_sport;
0047 
0048     if (cache)
0049         rt = dst_cache_get_ip4(cache, &fl.saddr);
0050 
0051     if (!rt) {
0052         security_sk_classify_flow(sock, flowi4_to_flowi_common(&fl));
0053         if (unlikely(!inet_confirm_addr(sock_net(sock), NULL, 0,
0054                         fl.saddr, RT_SCOPE_HOST))) {
0055             endpoint->src4.s_addr = 0;
0056             endpoint->src_if4 = 0;
0057             fl.saddr = 0;
0058             if (cache)
0059                 dst_cache_reset(cache);
0060         }
0061         rt = ip_route_output_flow(sock_net(sock), &fl, sock);
0062         if (unlikely(endpoint->src_if4 && ((IS_ERR(rt) &&
0063                  PTR_ERR(rt) == -EINVAL) || (!IS_ERR(rt) &&
0064                  rt->dst.dev->ifindex != endpoint->src_if4)))) {
0065             endpoint->src4.s_addr = 0;
0066             endpoint->src_if4 = 0;
0067             fl.saddr = 0;
0068             if (cache)
0069                 dst_cache_reset(cache);
0070             if (!IS_ERR(rt))
0071                 ip_rt_put(rt);
0072             rt = ip_route_output_flow(sock_net(sock), &fl, sock);
0073         }
0074         if (IS_ERR(rt)) {
0075             ret = PTR_ERR(rt);
0076             net_dbg_ratelimited("%s: No route to %pISpfsc, error %d\n",
0077                         wg->dev->name, &endpoint->addr, ret);
0078             goto err;
0079         }
0080         if (cache)
0081             dst_cache_set_ip4(cache, &rt->dst, fl.saddr);
0082     }
0083 
0084     skb->ignore_df = 1;
0085     udp_tunnel_xmit_skb(rt, sock, skb, fl.saddr, fl.daddr, ds,
0086                 ip4_dst_hoplimit(&rt->dst), 0, fl.fl4_sport,
0087                 fl.fl4_dport, false, false);
0088     goto out;
0089 
0090 err:
0091     kfree_skb(skb);
0092 out:
0093     rcu_read_unlock_bh();
0094     return ret;
0095 }
0096 
0097 static int send6(struct wg_device *wg, struct sk_buff *skb,
0098          struct endpoint *endpoint, u8 ds, struct dst_cache *cache)
0099 {
0100 #if IS_ENABLED(CONFIG_IPV6)
0101     struct flowi6 fl = {
0102         .saddr = endpoint->src6,
0103         .daddr = endpoint->addr6.sin6_addr,
0104         .fl6_dport = endpoint->addr6.sin6_port,
0105         .flowi6_mark = wg->fwmark,
0106         .flowi6_oif = endpoint->addr6.sin6_scope_id,
0107         .flowi6_proto = IPPROTO_UDP
0108         /* TODO: addr->sin6_flowinfo */
0109     };
0110     struct dst_entry *dst = NULL;
0111     struct sock *sock;
0112     int ret = 0;
0113 
0114     skb_mark_not_on_list(skb);
0115     skb->dev = wg->dev;
0116     skb->mark = wg->fwmark;
0117 
0118     rcu_read_lock_bh();
0119     sock = rcu_dereference_bh(wg->sock6);
0120 
0121     if (unlikely(!sock)) {
0122         ret = -ENONET;
0123         goto err;
0124     }
0125 
0126     fl.fl6_sport = inet_sk(sock)->inet_sport;
0127 
0128     if (cache)
0129         dst = dst_cache_get_ip6(cache, &fl.saddr);
0130 
0131     if (!dst) {
0132         security_sk_classify_flow(sock, flowi6_to_flowi_common(&fl));
0133         if (unlikely(!ipv6_addr_any(&fl.saddr) &&
0134                  !ipv6_chk_addr(sock_net(sock), &fl.saddr, NULL, 0))) {
0135             endpoint->src6 = fl.saddr = in6addr_any;
0136             if (cache)
0137                 dst_cache_reset(cache);
0138         }
0139         dst = ipv6_stub->ipv6_dst_lookup_flow(sock_net(sock), sock, &fl,
0140                               NULL);
0141         if (IS_ERR(dst)) {
0142             ret = PTR_ERR(dst);
0143             net_dbg_ratelimited("%s: No route to %pISpfsc, error %d\n",
0144                         wg->dev->name, &endpoint->addr, ret);
0145             goto err;
0146         }
0147         if (cache)
0148             dst_cache_set_ip6(cache, dst, &fl.saddr);
0149     }
0150 
0151     skb->ignore_df = 1;
0152     udp_tunnel6_xmit_skb(dst, sock, skb, skb->dev, &fl.saddr, &fl.daddr, ds,
0153                  ip6_dst_hoplimit(dst), 0, fl.fl6_sport,
0154                  fl.fl6_dport, false);
0155     goto out;
0156 
0157 err:
0158     kfree_skb(skb);
0159 out:
0160     rcu_read_unlock_bh();
0161     return ret;
0162 #else
0163     kfree_skb(skb);
0164     return -EAFNOSUPPORT;
0165 #endif
0166 }
0167 
0168 int wg_socket_send_skb_to_peer(struct wg_peer *peer, struct sk_buff *skb, u8 ds)
0169 {
0170     size_t skb_len = skb->len;
0171     int ret = -EAFNOSUPPORT;
0172 
0173     read_lock_bh(&peer->endpoint_lock);
0174     if (peer->endpoint.addr.sa_family == AF_INET)
0175         ret = send4(peer->device, skb, &peer->endpoint, ds,
0176                 &peer->endpoint_cache);
0177     else if (peer->endpoint.addr.sa_family == AF_INET6)
0178         ret = send6(peer->device, skb, &peer->endpoint, ds,
0179                 &peer->endpoint_cache);
0180     else
0181         dev_kfree_skb(skb);
0182     if (likely(!ret))
0183         peer->tx_bytes += skb_len;
0184     read_unlock_bh(&peer->endpoint_lock);
0185 
0186     return ret;
0187 }
0188 
0189 int wg_socket_send_buffer_to_peer(struct wg_peer *peer, void *buffer,
0190                   size_t len, u8 ds)
0191 {
0192     struct sk_buff *skb = alloc_skb(len + SKB_HEADER_LEN, GFP_ATOMIC);
0193 
0194     if (unlikely(!skb))
0195         return -ENOMEM;
0196 
0197     skb_reserve(skb, SKB_HEADER_LEN);
0198     skb_set_inner_network_header(skb, 0);
0199     skb_put_data(skb, buffer, len);
0200     return wg_socket_send_skb_to_peer(peer, skb, ds);
0201 }
0202 
0203 int wg_socket_send_buffer_as_reply_to_skb(struct wg_device *wg,
0204                       struct sk_buff *in_skb, void *buffer,
0205                       size_t len)
0206 {
0207     int ret = 0;
0208     struct sk_buff *skb;
0209     struct endpoint endpoint;
0210 
0211     if (unlikely(!in_skb))
0212         return -EINVAL;
0213     ret = wg_socket_endpoint_from_skb(&endpoint, in_skb);
0214     if (unlikely(ret < 0))
0215         return ret;
0216 
0217     skb = alloc_skb(len + SKB_HEADER_LEN, GFP_ATOMIC);
0218     if (unlikely(!skb))
0219         return -ENOMEM;
0220     skb_reserve(skb, SKB_HEADER_LEN);
0221     skb_set_inner_network_header(skb, 0);
0222     skb_put_data(skb, buffer, len);
0223 
0224     if (endpoint.addr.sa_family == AF_INET)
0225         ret = send4(wg, skb, &endpoint, 0, NULL);
0226     else if (endpoint.addr.sa_family == AF_INET6)
0227         ret = send6(wg, skb, &endpoint, 0, NULL);
0228     /* No other possibilities if the endpoint is valid, which it is,
0229      * as we checked above.
0230      */
0231 
0232     return ret;
0233 }
0234 
0235 int wg_socket_endpoint_from_skb(struct endpoint *endpoint,
0236                 const struct sk_buff *skb)
0237 {
0238     memset(endpoint, 0, sizeof(*endpoint));
0239     if (skb->protocol == htons(ETH_P_IP)) {
0240         endpoint->addr4.sin_family = AF_INET;
0241         endpoint->addr4.sin_port = udp_hdr(skb)->source;
0242         endpoint->addr4.sin_addr.s_addr = ip_hdr(skb)->saddr;
0243         endpoint->src4.s_addr = ip_hdr(skb)->daddr;
0244         endpoint->src_if4 = skb->skb_iif;
0245     } else if (IS_ENABLED(CONFIG_IPV6) && skb->protocol == htons(ETH_P_IPV6)) {
0246         endpoint->addr6.sin6_family = AF_INET6;
0247         endpoint->addr6.sin6_port = udp_hdr(skb)->source;
0248         endpoint->addr6.sin6_addr = ipv6_hdr(skb)->saddr;
0249         endpoint->addr6.sin6_scope_id = ipv6_iface_scope_id(
0250             &ipv6_hdr(skb)->saddr, skb->skb_iif);
0251         endpoint->src6 = ipv6_hdr(skb)->daddr;
0252     } else {
0253         return -EINVAL;
0254     }
0255     return 0;
0256 }
0257 
0258 static bool endpoint_eq(const struct endpoint *a, const struct endpoint *b)
0259 {
0260     return (a->addr.sa_family == AF_INET && b->addr.sa_family == AF_INET &&
0261         a->addr4.sin_port == b->addr4.sin_port &&
0262         a->addr4.sin_addr.s_addr == b->addr4.sin_addr.s_addr &&
0263         a->src4.s_addr == b->src4.s_addr && a->src_if4 == b->src_if4) ||
0264            (a->addr.sa_family == AF_INET6 &&
0265         b->addr.sa_family == AF_INET6 &&
0266         a->addr6.sin6_port == b->addr6.sin6_port &&
0267         ipv6_addr_equal(&a->addr6.sin6_addr, &b->addr6.sin6_addr) &&
0268         a->addr6.sin6_scope_id == b->addr6.sin6_scope_id &&
0269         ipv6_addr_equal(&a->src6, &b->src6)) ||
0270            unlikely(!a->addr.sa_family && !b->addr.sa_family);
0271 }
0272 
0273 void wg_socket_set_peer_endpoint(struct wg_peer *peer,
0274                  const struct endpoint *endpoint)
0275 {
0276     /* First we check unlocked, in order to optimize, since it's pretty rare
0277      * that an endpoint will change. If we happen to be mid-write, and two
0278      * CPUs wind up writing the same thing or something slightly different,
0279      * it doesn't really matter much either.
0280      */
0281     if (endpoint_eq(endpoint, &peer->endpoint))
0282         return;
0283     write_lock_bh(&peer->endpoint_lock);
0284     if (endpoint->addr.sa_family == AF_INET) {
0285         peer->endpoint.addr4 = endpoint->addr4;
0286         peer->endpoint.src4 = endpoint->src4;
0287         peer->endpoint.src_if4 = endpoint->src_if4;
0288     } else if (IS_ENABLED(CONFIG_IPV6) && endpoint->addr.sa_family == AF_INET6) {
0289         peer->endpoint.addr6 = endpoint->addr6;
0290         peer->endpoint.src6 = endpoint->src6;
0291     } else {
0292         goto out;
0293     }
0294     dst_cache_reset(&peer->endpoint_cache);
0295 out:
0296     write_unlock_bh(&peer->endpoint_lock);
0297 }
0298 
0299 void wg_socket_set_peer_endpoint_from_skb(struct wg_peer *peer,
0300                       const struct sk_buff *skb)
0301 {
0302     struct endpoint endpoint;
0303 
0304     if (!wg_socket_endpoint_from_skb(&endpoint, skb))
0305         wg_socket_set_peer_endpoint(peer, &endpoint);
0306 }
0307 
0308 void wg_socket_clear_peer_endpoint_src(struct wg_peer *peer)
0309 {
0310     write_lock_bh(&peer->endpoint_lock);
0311     memset(&peer->endpoint.src6, 0, sizeof(peer->endpoint.src6));
0312     dst_cache_reset_now(&peer->endpoint_cache);
0313     write_unlock_bh(&peer->endpoint_lock);
0314 }
0315 
0316 static int wg_receive(struct sock *sk, struct sk_buff *skb)
0317 {
0318     struct wg_device *wg;
0319 
0320     if (unlikely(!sk))
0321         goto err;
0322     wg = sk->sk_user_data;
0323     if (unlikely(!wg))
0324         goto err;
0325     skb_mark_not_on_list(skb);
0326     wg_packet_receive(wg, skb);
0327     return 0;
0328 
0329 err:
0330     kfree_skb(skb);
0331     return 0;
0332 }
0333 
0334 static void sock_free(struct sock *sock)
0335 {
0336     if (unlikely(!sock))
0337         return;
0338     sk_clear_memalloc(sock);
0339     udp_tunnel_sock_release(sock->sk_socket);
0340 }
0341 
0342 static void set_sock_opts(struct socket *sock)
0343 {
0344     sock->sk->sk_allocation = GFP_ATOMIC;
0345     sock->sk->sk_sndbuf = INT_MAX;
0346     sk_set_memalloc(sock->sk);
0347 }
0348 
0349 int wg_socket_init(struct wg_device *wg, u16 port)
0350 {
0351     struct net *net;
0352     int ret;
0353     struct udp_tunnel_sock_cfg cfg = {
0354         .sk_user_data = wg,
0355         .encap_type = 1,
0356         .encap_rcv = wg_receive
0357     };
0358     struct socket *new4 = NULL, *new6 = NULL;
0359     struct udp_port_cfg port4 = {
0360         .family = AF_INET,
0361         .local_ip.s_addr = htonl(INADDR_ANY),
0362         .local_udp_port = htons(port),
0363         .use_udp_checksums = true
0364     };
0365 #if IS_ENABLED(CONFIG_IPV6)
0366     int retries = 0;
0367     struct udp_port_cfg port6 = {
0368         .family = AF_INET6,
0369         .local_ip6 = IN6ADDR_ANY_INIT,
0370         .use_udp6_tx_checksums = true,
0371         .use_udp6_rx_checksums = true,
0372         .ipv6_v6only = true
0373     };
0374 #endif
0375 
0376     rcu_read_lock();
0377     net = rcu_dereference(wg->creating_net);
0378     net = net ? maybe_get_net(net) : NULL;
0379     rcu_read_unlock();
0380     if (unlikely(!net))
0381         return -ENONET;
0382 
0383 #if IS_ENABLED(CONFIG_IPV6)
0384 retry:
0385 #endif
0386 
0387     ret = udp_sock_create(net, &port4, &new4);
0388     if (ret < 0) {
0389         pr_err("%s: Could not create IPv4 socket\n", wg->dev->name);
0390         goto out;
0391     }
0392     set_sock_opts(new4);
0393     setup_udp_tunnel_sock(net, new4, &cfg);
0394 
0395 #if IS_ENABLED(CONFIG_IPV6)
0396     if (ipv6_mod_enabled()) {
0397         port6.local_udp_port = inet_sk(new4->sk)->inet_sport;
0398         ret = udp_sock_create(net, &port6, &new6);
0399         if (ret < 0) {
0400             udp_tunnel_sock_release(new4);
0401             if (ret == -EADDRINUSE && !port && retries++ < 100)
0402                 goto retry;
0403             pr_err("%s: Could not create IPv6 socket\n",
0404                    wg->dev->name);
0405             goto out;
0406         }
0407         set_sock_opts(new6);
0408         setup_udp_tunnel_sock(net, new6, &cfg);
0409     }
0410 #endif
0411 
0412     wg_socket_reinit(wg, new4->sk, new6 ? new6->sk : NULL);
0413     ret = 0;
0414 out:
0415     put_net(net);
0416     return ret;
0417 }
0418 
0419 void wg_socket_reinit(struct wg_device *wg, struct sock *new4,
0420               struct sock *new6)
0421 {
0422     struct sock *old4, *old6;
0423 
0424     mutex_lock(&wg->socket_update_lock);
0425     old4 = rcu_dereference_protected(wg->sock4,
0426                 lockdep_is_held(&wg->socket_update_lock));
0427     old6 = rcu_dereference_protected(wg->sock6,
0428                 lockdep_is_held(&wg->socket_update_lock));
0429     rcu_assign_pointer(wg->sock4, new4);
0430     rcu_assign_pointer(wg->sock6, new6);
0431     if (new4)
0432         wg->incoming_port = ntohs(inet_sk(new4)->inet_sport);
0433     mutex_unlock(&wg->socket_update_lock);
0434     synchronize_net();
0435     sock_free(old4);
0436     sock_free(old6);
0437 }