From: Bobby Eshleman <[email protected]>

virtio-vsock did not have namespace support (the device was always
accessible to any global namespace).

Make the virtio-vsock device assignable to a namespace and initialize it
to init_net. Because virtio-vsock and init_net are both hardcoded to
global mode, nothing changes until the assign command is issued.

When the device's local-mode namespace is being destroyed, received
packets are reset until a new valid namespace has been assigned and/or
automatically returned to, and the next RX batch begins (in
virtio_transport_rx_work). They are reset rather than dropped because
vsock does not retransmit, so a silent drop would leave the host waiting
for a timeout, and a connection request arriving in that window has no
socket whose teardown would tell it otherwise. This requires making
virtio_transport_reset_no_sock() available outside of the common code.

When a device is assigned to a namespace, every already established
vsock socket that is no longer able to reach the device is forcibly
reset. For that reason, adding new sockets to the connected table must
be performed atomically with regards to namespace assignment. This
ensures that when the socket is added to the connected table that it
actually passes the new reachability conditions set by ns assignment. If
it wins the race to the table and does NOT pass the reachability tests,
then it will be reset. This is the purpose of the new helper
'vsock_maybe_set_connected()'.

Signed-off-by: Bobby Eshleman <[email protected]>
---
Changes in v2:
- Export virtio_transport_reset(), wire it to the new .reset op (Stefano)
- netns_assign_allow is now a bool (Stefano)
- Pass NULL, not &init_net, in virtio_transport_rx_work(), and
  comment why (Stefano)
- Drop the if (net) guard around put_net() (Stefano)
- Drop the comments at the vsock_maybe_set_connected() call sites,
  the commit msg seems sufficient
---
 include/linux/virtio_vsock.h            |  3 +++
 net/vmw_vsock/virtio_transport.c        | 24 ++++++++++++++++++------
 net/vmw_vsock/virtio_transport_common.c | 27 ++++++++++++++++++---------
 3 files changed, 39 insertions(+), 15 deletions(-)

diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
index f91704731057..5d15b6d6bdf7 100644
--- a/include/linux/virtio_vsock.h
+++ b/include/linux/virtio_vsock.h
@@ -286,6 +286,9 @@ void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock 
*vvs, struct sk_buff *
 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 wanted);
 void virtio_transport_put_credit(struct virtio_vsock_sock *vvs, u32 credit);
 void virtio_transport_deliver_tap_pkt(struct sk_buff *skb);
+int virtio_transport_reset(struct vsock_sock *vsk, struct sk_buff *skb);
+int virtio_transport_reset_no_sock(const struct virtio_transport *t,
+                                  struct sk_buff *skb, struct net *net);
 int virtio_transport_purge_skbs(void *vsk, struct sk_buff_head *list);
 int virtio_transport_read_skb(struct vsock_sock *vsk, skb_read_actor_t 
read_actor);
 int virtio_transport_notify_set_rcvlowat(struct vsock_sock *vsk, int val);
diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c
index 4f9aa9c4c3aa..5ad93af4bd2b 100644
--- a/net/vmw_vsock/virtio_transport.c
+++ b/net/vmw_vsock/virtio_transport.c
@@ -542,7 +542,7 @@ static bool virtio_transport_msgzerocopy_allow(void)
 
 bool virtio_transport_stream_allow(struct vsock_sock *vsk, u32 cid, u32 port)
 {
-       return vsock_net_mode_global(vsk);
+       return vsock_g2h_net_reachable(sock_net(sk_vsock(vsk)));
 }
 
 static bool virtio_transport_seqpacket_allow(struct vsock_sock *vsk,
@@ -587,6 +587,8 @@ static struct virtio_transport virtio_transport = {
                .seqpacket_has_data       = virtio_transport_seqpacket_has_data,
 
                .msgzerocopy_allow        = virtio_transport_msgzerocopy_allow,
+               .netns_assign_allow       = true,
+               .reset                    = virtio_transport_reset,
 
                .notify_poll_in           = virtio_transport_notify_poll_in,
                .notify_poll_out          = virtio_transport_notify_poll_out,
@@ -616,7 +618,7 @@ virtio_transport_seqpacket_allow(struct vsock_sock *vsk, 
u32 remote_cid)
        struct virtio_vsock *vsock;
        bool seqpacket_allow;
 
-       if (!vsock_net_mode_global(vsk))
+       if (!vsock_g2h_net_reachable(sock_net(sk_vsock(vsk))))
                return false;
 
        seqpacket_allow = false;
@@ -633,7 +635,11 @@ static void virtio_transport_rx_work(struct work_struct 
*work)
 {
        struct virtio_vsock *vsock =
                container_of(work, struct virtio_vsock, rx_work);
+       struct virtio_transport *t = &virtio_transport;
        struct virtqueue *vq;
+       struct net *net;
+
+       net = vsock_g2h_net_get();
 
        mutex_lock(&vsock->rx_lock);
 
@@ -682,10 +688,14 @@ static void virtio_transport_rx_work(struct work_struct 
*work)
 
                        virtio_transport_deliver_tap_pkt(skb);
 
-                       /* Force virtio-transport into global mode since it
-                        * does not yet support local-mode namespacing.
-                        */
-                       virtio_transport_recv_pkt(&virtio_transport, skb, NULL);
+                       /* The virtio send path does not use @net. */
+                       if (unlikely(!net)) {
+                               virtio_transport_reset_no_sock(t, skb, NULL);
+                               kfree_skb(skb);
+                               continue;
+                       }
+
+                       virtio_transport_recv_pkt(t, skb, net);
                }
        } while (!virtqueue_enable_cb(vq));
 
@@ -694,6 +704,8 @@ static void virtio_transport_rx_work(struct work_struct 
*work)
                virtio_vsock_rx_fill(vsock);
 out_nofill:
        mutex_unlock(&vsock->rx_lock);
+
+       put_net(net);
 }
 
 static int virtio_vsock_vqs_init(struct virtio_vsock *vsock)
diff --git a/net/vmw_vsock/virtio_transport_common.c 
b/net/vmw_vsock/virtio_transport_common.c
index f225f53ed4ba..c24049b2a386 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -1291,8 +1291,7 @@ ssize_t virtio_transport_unsent_bytes(struct vsock_sock 
*vsk)
 }
 EXPORT_SYMBOL_GPL(virtio_transport_unsent_bytes);
 
-static int virtio_transport_reset(struct vsock_sock *vsk,
-                                 struct sk_buff *skb)
+int virtio_transport_reset(struct vsock_sock *vsk, struct sk_buff *skb)
 {
        struct virtio_vsock_pkt_info info = {
                .op = VIRTIO_VSOCK_OP_RST,
@@ -1307,6 +1306,7 @@ static int virtio_transport_reset(struct vsock_sock *vsk,
 
        return virtio_transport_send_pkt_info(vsk, &info);
 }
+EXPORT_SYMBOL_GPL(virtio_transport_reset);
 
 /* Normally packets are associated with a socket.  There may be no socket if an
  * attempt was made to connect to a socket that does not exist.
@@ -1315,8 +1315,8 @@ static int virtio_transport_reset(struct vsock_sock *vsk,
  * loopback, this is the namespace of the socket. For vhost, this is the
  * namespace of the VM (i.e., vhost_vsock).
  */
-static int virtio_transport_reset_no_sock(const struct virtio_transport *t,
-                                         struct sk_buff *skb, struct net *net)
+int virtio_transport_reset_no_sock(const struct virtio_transport *t,
+                                  struct sk_buff *skb, struct net *net)
 {
        struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
        struct virtio_vsock_pkt_info info = {
@@ -1355,6 +1355,7 @@ static int virtio_transport_reset_no_sock(const struct 
virtio_transport *t,
 
        return t->send_pkt(reply, net);
 }
+EXPORT_SYMBOL_GPL(virtio_transport_reset_no_sock);
 
 /* This function should be called with sk_lock held and SOCK_DONE set */
 static void virtio_transport_remove_sock(struct vsock_sock *vsk)
@@ -1478,9 +1479,13 @@ virtio_transport_recv_connecting(struct sock *sk,
 
        switch (le16_to_cpu(hdr->op)) {
        case VIRTIO_VSOCK_OP_RESPONSE:
-               sk->sk_state = TCP_ESTABLISHED;
+               if (!vsock_maybe_set_connected(vsk)) {
+                       skerr = ECONNRESET;
+                       err = -ENETUNREACH;
+                       goto destroy;
+               }
+
                sk->sk_socket->state = SS_CONNECTED;
-               vsock_insert_connected(vsk);
                sk->sk_state_change(sk);
                break;
        case VIRTIO_VSOCK_OP_INVALID:
@@ -1736,8 +1741,6 @@ virtio_transport_recv_listen(struct sock *sk, struct 
sk_buff *skb,
 
        lock_sock_nested(child, SINGLE_DEPTH_NESTING);
 
-       child->sk_state = TCP_ESTABLISHED;
-
        vchild = vsock_sk(child);
        vsock_addr_init(&vchild->local_addr, le64_to_cpu(hdr->dst_cid),
                        le32_to_cpu(hdr->dst_port));
@@ -1758,7 +1761,13 @@ virtio_transport_recv_listen(struct sock *sk, struct 
sk_buff *skb,
        if (virtio_transport_space_update(child, skb))
                child->sk_write_space(child);
 
-       vsock_insert_connected(vchild);
+       if (!vsock_maybe_set_connected(vchild)) {
+               release_sock(child);
+               virtio_transport_reset_no_sock(t, skb, sock_net(sk));
+               sock_put(child);
+               return -ENETUNREACH;
+       }
+
        vsock_enqueue_accept(sk, child);
        virtio_transport_send_response(vchild, skb);
 

-- 
2.53.0-Meta


Reply via email to