Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include "device.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include "peer.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include "socket.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include "queueing.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include "messages.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/if_vlan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/if_ether.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/inetdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <net/udp_tunnel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <net/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) static int send4(struct wg_device *wg, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 		 struct endpoint *endpoint, u8 ds, struct dst_cache *cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	struct flowi4 fl = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 		.saddr = endpoint->src4.s_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 		.daddr = endpoint->addr4.sin_addr.s_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 		.fl4_dport = endpoint->addr4.sin_port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 		.flowi4_mark = wg->fwmark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 		.flowi4_proto = IPPROTO_UDP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct rtable *rt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	struct sock *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	skb_mark_not_on_list(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	skb->dev = wg->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	skb->mark = wg->fwmark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	rcu_read_lock_bh();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	sock = rcu_dereference_bh(wg->sock4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	if (unlikely(!sock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		ret = -ENONET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	fl.fl4_sport = inet_sk(sock)->inet_sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	if (cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		rt = dst_cache_get_ip4(cache, &fl.saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	if (!rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		security_sk_classify_flow(sock, flowi4_to_flowi(&fl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		if (unlikely(!inet_confirm_addr(sock_net(sock), NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 						fl.saddr, RT_SCOPE_HOST))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 			endpoint->src4.s_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 			*(__force __be32 *)&endpoint->src_if4 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 			fl.saddr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 			if (cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 				dst_cache_reset(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		rt = ip_route_output_flow(sock_net(sock), &fl, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		if (unlikely(endpoint->src_if4 && ((IS_ERR(rt) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 			     PTR_ERR(rt) == -EINVAL) || (!IS_ERR(rt) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 			     rt->dst.dev->ifindex != endpoint->src_if4)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 			endpoint->src4.s_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			*(__force __be32 *)&endpoint->src_if4 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			fl.saddr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			if (cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 				dst_cache_reset(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 			if (!IS_ERR(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 				ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			rt = ip_route_output_flow(sock_net(sock), &fl, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		if (unlikely(IS_ERR(rt))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 			ret = PTR_ERR(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 			net_dbg_ratelimited("%s: No route to %pISpfsc, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 					    wg->dev->name, &endpoint->addr, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		if (cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			dst_cache_set_ip4(cache, &rt->dst, fl.saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	skb->ignore_df = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	udp_tunnel_xmit_skb(rt, sock, skb, fl.saddr, fl.daddr, ds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			    ip4_dst_hoplimit(&rt->dst), 0, fl.fl4_sport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			    fl.fl4_dport, false, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	rcu_read_unlock_bh();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) static int send6(struct wg_device *wg, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		 struct endpoint *endpoint, u8 ds, struct dst_cache *cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	struct flowi6 fl = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		.saddr = endpoint->src6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		.daddr = endpoint->addr6.sin6_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		.fl6_dport = endpoint->addr6.sin6_port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		.flowi6_mark = wg->fwmark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		.flowi6_oif = endpoint->addr6.sin6_scope_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		.flowi6_proto = IPPROTO_UDP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		/* TODO: addr->sin6_flowinfo */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct dst_entry *dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	struct sock *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	skb_mark_not_on_list(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	skb->dev = wg->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	skb->mark = wg->fwmark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	rcu_read_lock_bh();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	sock = rcu_dereference_bh(wg->sock6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (unlikely(!sock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		ret = -ENONET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	fl.fl6_sport = inet_sk(sock)->inet_sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	if (cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		dst = dst_cache_get_ip6(cache, &fl.saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (!dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		security_sk_classify_flow(sock, flowi6_to_flowi(&fl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		if (unlikely(!ipv6_addr_any(&fl.saddr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			     !ipv6_chk_addr(sock_net(sock), &fl.saddr, NULL, 0))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			endpoint->src6 = fl.saddr = in6addr_any;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			if (cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 				dst_cache_reset(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		dst = ipv6_stub->ipv6_dst_lookup_flow(sock_net(sock), sock, &fl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 						      NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		if (unlikely(IS_ERR(dst))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			ret = PTR_ERR(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			net_dbg_ratelimited("%s: No route to %pISpfsc, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 					    wg->dev->name, &endpoint->addr, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		if (cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			dst_cache_set_ip6(cache, dst, &fl.saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	skb->ignore_df = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	udp_tunnel6_xmit_skb(dst, sock, skb, skb->dev, &fl.saddr, &fl.daddr, ds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			     ip6_dst_hoplimit(dst), 0, fl.fl6_sport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			     fl.fl6_dport, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	rcu_read_unlock_bh();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	return -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int wg_socket_send_skb_to_peer(struct wg_peer *peer, struct sk_buff *skb, u8 ds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	size_t skb_len = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	int ret = -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	read_lock_bh(&peer->endpoint_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	if (peer->endpoint.addr.sa_family == AF_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		ret = send4(peer->device, skb, &peer->endpoint, ds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			    &peer->endpoint_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	else if (peer->endpoint.addr.sa_family == AF_INET6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		ret = send6(peer->device, skb, &peer->endpoint, ds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			    &peer->endpoint_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		dev_kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if (likely(!ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		peer->tx_bytes += skb_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	read_unlock_bh(&peer->endpoint_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) int wg_socket_send_buffer_to_peer(struct wg_peer *peer, void *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 				  size_t len, u8 ds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	struct sk_buff *skb = alloc_skb(len + SKB_HEADER_LEN, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if (unlikely(!skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	skb_reserve(skb, SKB_HEADER_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	skb_set_inner_network_header(skb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	skb_put_data(skb, buffer, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	return wg_socket_send_skb_to_peer(peer, skb, ds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) int wg_socket_send_buffer_as_reply_to_skb(struct wg_device *wg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 					  struct sk_buff *in_skb, void *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 					  size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	struct endpoint endpoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (unlikely(!in_skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	ret = wg_socket_endpoint_from_skb(&endpoint, in_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	if (unlikely(ret < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	skb = alloc_skb(len + SKB_HEADER_LEN, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	if (unlikely(!skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	skb_reserve(skb, SKB_HEADER_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	skb_set_inner_network_header(skb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	skb_put_data(skb, buffer, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	if (endpoint.addr.sa_family == AF_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		ret = send4(wg, skb, &endpoint, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	else if (endpoint.addr.sa_family == AF_INET6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		ret = send6(wg, skb, &endpoint, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	/* No other possibilities if the endpoint is valid, which it is,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	 * as we checked above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) int wg_socket_endpoint_from_skb(struct endpoint *endpoint,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 				const struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	memset(endpoint, 0, sizeof(*endpoint));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	if (skb->protocol == htons(ETH_P_IP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		endpoint->addr4.sin_family = AF_INET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		endpoint->addr4.sin_port = udp_hdr(skb)->source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		endpoint->addr4.sin_addr.s_addr = ip_hdr(skb)->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		endpoint->src4.s_addr = ip_hdr(skb)->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		endpoint->src_if4 = skb->skb_iif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	} else if (IS_ENABLED(CONFIG_IPV6) && skb->protocol == htons(ETH_P_IPV6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		endpoint->addr6.sin6_family = AF_INET6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		endpoint->addr6.sin6_port = udp_hdr(skb)->source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		endpoint->addr6.sin6_addr = ipv6_hdr(skb)->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		endpoint->addr6.sin6_scope_id = ipv6_iface_scope_id(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			&ipv6_hdr(skb)->saddr, skb->skb_iif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		endpoint->src6 = ipv6_hdr(skb)->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static bool endpoint_eq(const struct endpoint *a, const struct endpoint *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	return (a->addr.sa_family == AF_INET && b->addr.sa_family == AF_INET &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		a->addr4.sin_port == b->addr4.sin_port &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		a->addr4.sin_addr.s_addr == b->addr4.sin_addr.s_addr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		a->src4.s_addr == b->src4.s_addr && a->src_if4 == b->src_if4) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	       (a->addr.sa_family == AF_INET6 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		b->addr.sa_family == AF_INET6 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		a->addr6.sin6_port == b->addr6.sin6_port &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		ipv6_addr_equal(&a->addr6.sin6_addr, &b->addr6.sin6_addr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		a->addr6.sin6_scope_id == b->addr6.sin6_scope_id &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		ipv6_addr_equal(&a->src6, &b->src6)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	       unlikely(!a->addr.sa_family && !b->addr.sa_family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) void wg_socket_set_peer_endpoint(struct wg_peer *peer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 				 const struct endpoint *endpoint)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	/* First we check unlocked, in order to optimize, since it's pretty rare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	 * that an endpoint will change. If we happen to be mid-write, and two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	 * CPUs wind up writing the same thing or something slightly different,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	 * it doesn't really matter much either.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	if (endpoint_eq(endpoint, &peer->endpoint))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	write_lock_bh(&peer->endpoint_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	if (endpoint->addr.sa_family == AF_INET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		peer->endpoint.addr4 = endpoint->addr4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		peer->endpoint.src4 = endpoint->src4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		peer->endpoint.src_if4 = endpoint->src_if4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	} else if (IS_ENABLED(CONFIG_IPV6) && endpoint->addr.sa_family == AF_INET6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		peer->endpoint.addr6 = endpoint->addr6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		peer->endpoint.src6 = endpoint->src6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	dst_cache_reset(&peer->endpoint_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	write_unlock_bh(&peer->endpoint_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) void wg_socket_set_peer_endpoint_from_skb(struct wg_peer *peer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 					  const struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	struct endpoint endpoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	if (!wg_socket_endpoint_from_skb(&endpoint, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		wg_socket_set_peer_endpoint(peer, &endpoint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) void wg_socket_clear_peer_endpoint_src(struct wg_peer *peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	write_lock_bh(&peer->endpoint_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	memset(&peer->endpoint.src6, 0, sizeof(peer->endpoint.src6));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	dst_cache_reset_now(&peer->endpoint_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	write_unlock_bh(&peer->endpoint_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) static int wg_receive(struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	struct wg_device *wg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	if (unlikely(!sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	wg = sk->sk_user_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	if (unlikely(!wg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	skb_mark_not_on_list(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	wg_packet_receive(wg, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) static void sock_free(struct sock *sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	if (unlikely(!sock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	sk_clear_memalloc(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	udp_tunnel_sock_release(sock->sk_socket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) static void set_sock_opts(struct socket *sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	sock->sk->sk_allocation = GFP_ATOMIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	sock->sk->sk_sndbuf = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	sk_set_memalloc(sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) int wg_socket_init(struct wg_device *wg, u16 port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	struct net *net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	struct udp_tunnel_sock_cfg cfg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		.sk_user_data = wg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		.encap_type = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		.encap_rcv = wg_receive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	struct socket *new4 = NULL, *new6 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	struct udp_port_cfg port4 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		.family = AF_INET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		.local_ip.s_addr = htonl(INADDR_ANY),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		.local_udp_port = htons(port),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		.use_udp_checksums = true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	int retries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	struct udp_port_cfg port6 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		.family = AF_INET6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		.local_ip6 = IN6ADDR_ANY_INIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		.use_udp6_tx_checksums = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		.use_udp6_rx_checksums = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		.ipv6_v6only = true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	net = rcu_dereference(wg->creating_net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	net = net ? maybe_get_net(net) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	if (unlikely(!net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		return -ENONET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	ret = udp_sock_create(net, &port4, &new4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		pr_err("%s: Could not create IPv4 socket\n", wg->dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	set_sock_opts(new4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	setup_udp_tunnel_sock(net, new4, &cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	if (ipv6_mod_enabled()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		port6.local_udp_port = inet_sk(new4->sk)->inet_sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		ret = udp_sock_create(net, &port6, &new6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 			udp_tunnel_sock_release(new4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 			if (ret == -EADDRINUSE && !port && retries++ < 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 				goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 			pr_err("%s: Could not create IPv6 socket\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 			       wg->dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		set_sock_opts(new6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		setup_udp_tunnel_sock(net, new6, &cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	wg_socket_reinit(wg, new4->sk, new6 ? new6->sk : NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	put_net(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) void wg_socket_reinit(struct wg_device *wg, struct sock *new4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		      struct sock *new6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	struct sock *old4, *old6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	mutex_lock(&wg->socket_update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	old4 = rcu_dereference_protected(wg->sock4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 				lockdep_is_held(&wg->socket_update_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	old6 = rcu_dereference_protected(wg->sock6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 				lockdep_is_held(&wg->socket_update_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	rcu_assign_pointer(wg->sock4, new4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	rcu_assign_pointer(wg->sock6, new6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	if (new4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		wg->incoming_port = ntohs(inet_sk(new4)->inet_sport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	mutex_unlock(&wg->socket_update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	synchronize_net();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	sock_free(old4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	sock_free(old6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) }