History

wireguard: device: reset peer src endpoint when netns exits

Each peer's endpoint contains a dst_cache entry that takes a reference to another netdev. When the containing namespace exits, we take down the socket and prevent future sockets from being created (by setting creating_net to NULL), which removes that potential reference on the netns. However, it doesn't release references to the netns that a netdev cached in dst_cache might be taking, so the netns still might fail to exit. Since the socket is gimped anyway, we can simply clear all the dst_caches (by way of clearing the endpoint src), which will release all references. However, the current dst_cache_reset function only releases those references lazily. But it turns out that all of our usages of wg_socket_clear_peer_endpoint_src are called from contexts that are not exactly high-speed or bottle-necked. For example, when there's connection difficulty, or when userspace is reconfiguring the interface. And in particular for this patch, when the netns is exiting. So for those cases, it makes more sense to call dst_release immediately. For that, we add a small helper function to dst_cache. This patch also adds a test to netns.sh from Hangbin Liu to ensure this doesn't regress.

Jason A. Donenfeld committed 4 years ago

Commit 20ae1d61 parent 7e938be5 files · +57 −2Browse files

Unified · Split

5 files
  1. drivers/net/wireguard/device.c +3 −0
  2. drivers/net/wireguard/socket.c +1 −1
  3. include/net/dst_cache.h +11 −0
  4. net/core/dst_cache.c +19 −0
  5. tools/testing/selftests/wireguard/netns.sh +23 −1
drivers/net/wireguard/device.c+3−0
@@ -398,6 +398,7 @@ static struct rtnl_link_ops link_ops __read_mostly = { · 397 unmodified lines
398static void wg_netns_pre_exit(struct net *net)398static void wg_netns_pre_exit(struct net *net)
399{399{
400 struct wg_device *wg;400 struct wg_device *wg;
401 struct wg_peer *peer;
401 402
402 rtnl_lock();403 rtnl_lock();
403 list_for_each_entry(wg, &device_list, device_list) {404 list_for_each_entry(wg, &device_list, device_list) {
@@ -407,6 +408,8 @@ static void wg_netns_pre_exit(struct net *net) · 3 unmodified lines
407 mutex_lock(&wg->device_update_lock);408 mutex_lock(&wg->device_update_lock);
408 rcu_assign_pointer(wg->creating_net, NULL);409 rcu_assign_pointer(wg->creating_net, NULL);
409 wg_socket_reinit(wg, NULL, NULL);410 wg_socket_reinit(wg, NULL, NULL);
411 list_for_each_entry(peer, &wg->peer_list, peer_list)
412 wg_socket_clear_peer_endpoint_src(peer);
410 mutex_unlock(&wg->device_update_lock);413 mutex_unlock(&wg->device_update_lock);
411 }414 }
412 }415 }
drivers/net/wireguard/socket.c+1−1
@@ -308,7 +308,7 @@ void wg_socket_clear_peer_endpoint_src(struct wg_peer *peer) · 307 unmodified lines
308{308{
309 write_lock_bh(&peer->endpoint_lock);309 write_lock_bh(&peer->endpoint_lock);
310 memset(&peer->endpoint.src6, 0, sizeof(peer->endpoint.src6));310 memset(&peer->endpoint.src6, 0, sizeof(peer->endpoint.src6));
311 dst_cache_reset(&peer->endpoint_cache);311 dst_cache_reset_now(&peer->endpoint_cache);
312 write_unlock_bh(&peer->endpoint_lock);312 write_unlock_bh(&peer->endpoint_lock);
313}313}
314 314
include/net/dst_cache.h+11−0
@@ -79,6 +79,17 @@ static inline void dst_cache_reset(struct dst_cache *dst_cache) · 78 unmodified lines
79 dst_cache->reset_ts = jiffies;79 dst_cache->reset_ts = jiffies;
80}80}
81 81
82/**
83 * dst_cache_reset_now - invalidate the cache contents immediately
84 * @dst_cache: the cache
85 *
86 * The caller must be sure there are no concurrent users, as this frees
87 * all dst_cache users immediately, rather than waiting for the next
88 * per-cpu usage like dst_cache_reset does. Most callers should use the
89 * higher speed lazily-freed dst_cache_reset function instead.
90 */
91void dst_cache_reset_now(struct dst_cache *dst_cache);
92
82/**93/**
83 * dst_cache_init - initialize the cache, allocating the required storage94 * dst_cache_init - initialize the cache, allocating the required storage
84 * @dst_cache: the cache95 * @dst_cache: the cache
net/core/dst_cache.c+19−0
@@ -162,3 +162,22 @@ void dst_cache_destroy(struct dst_cache *dst_cache) · 161 unmodified lines
162 free_percpu(dst_cache->cache);162 free_percpu(dst_cache->cache);
163}163}
164EXPORT_SYMBOL_GPL(dst_cache_destroy);164EXPORT_SYMBOL_GPL(dst_cache_destroy);
165
166void dst_cache_reset_now(struct dst_cache *dst_cache)
167{
168 int i;
169
170 if (!dst_cache->cache)
171 return;
172
173 dst_cache->reset_ts = jiffies;
174 for_each_possible_cpu(i) {
175 struct dst_cache_pcpu *idst = per_cpu_ptr(dst_cache->cache, i);
176 struct dst_entry *dst = idst->dst;
177
178 idst->cookie = 0;
179 idst->dst = NULL;
180 dst_release(dst);
181 }
182}
183EXPORT_SYMBOL_GPL(dst_cache_reset_now);
tools/testing/selftests/wireguard/netns.sh+23−1
@@ -613,6 +613,28 @@ ip0 link set wg0 up · 612 unmodified lines
613kill $ncat_pid613kill $ncat_pid
614ip0 link del wg0614ip0 link del wg0
615 615
616# Ensure that dst_cache references don't outlive netns lifetime
617ip1 link add dev wg0 type wireguard
618ip2 link add dev wg0 type wireguard
619configure_peers
620ip1 link add veth1 type veth peer name veth2
621ip1 link set veth2 netns $netns2
622ip1 addr add fd00:aa::1/64 dev veth1
623ip2 addr add fd00:aa::2/64 dev veth2
624ip1 link set veth1 up
625ip2 link set veth2 up
626waitiface $netns1 veth1
627waitiface $netns2 veth2
628ip1 -6 route add default dev veth1 via fd00:aa::2
629ip2 -6 route add default dev veth2 via fd00:aa::1
630n1 wg set wg0 peer "$pub2" endpoint [fd00:aa::2]:2
631n2 wg set wg0 peer "$pub1" endpoint [fd00:aa::1]:1
632n1 ping6 -c 1 fd00::2
633pp ip netns delete $netns1
634pp ip netns delete $netns2
635pp ip netns add $netns1
636pp ip netns add $netns2
637
616# Ensure there aren't circular reference loops638# Ensure there aren't circular reference loops
617ip1 link add wg1 type wireguard639ip1 link add wg1 type wireguard
618ip2 link add wg2 type wireguard640ip2 link add wg2 type wireguard
@@ -631,7 +653,7 @@ while read -t 0.1 -r line 2>/dev/null || [[ $? -ne 142 ]]; do · 12 unmodified lines
631done < /dev/kmsg653done < /dev/kmsg
632alldeleted=1654alldeleted=1
633for object in "${!objects[@]}"; do655for object in "${!objects[@]}"; do
634 if [[ ${objects["$object"]} != *createddestroyed ]]; then656 if [[ ${objects["$object"]} != *createddestroyed && ${objects["$object"]} != *createdcreateddestroyeddestroyed ]]; then
635 echo "Error: $object: merely ${objects["$object"]}" >&3657 echo "Error: $object: merely ${objects["$object"]}" >&3
636 alldeleted=0658 alldeleted=0
637 fi659 fi

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t