diff options
author | Stefan Hajnoczi <stefanha@redhat.com> | 2017-10-05 16:46:53 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2017-10-05 21:44:17 -0400 |
commit | 413a4317aca7d6367d57a5971b0c461f03851207 (patch) | |
tree | cf1b8a4ac209b5ce6ef8aaf861f84bc8279cf4d8 /net/vmw_vsock | |
parent | 3b4477d2dcf2709d0be89e2a8dced3d0f4a017f2 (diff) |
VSOCK: add sock_diag interface
This patch adds the sock_diag interface for querying sockets from
userspace. Tools like ss(8) and netstat(8) can use this interface to
list open sockets.
The userspace ABI is defined in <linux/vm_sockets_diag.h> and includes
netlink request and response structs. The request can query sockets
based on their sk_state (e.g. listening sockets only) and the response
contains socket information fields including the local/remote addresses,
inode number, etc.
This patch does not dump VMCI pending sockets because I have only tested
the virtio transport, which does not use pending sockets. Support can
be added later by extending vsock_diag_dump() if needed by VMCI users.
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/vmw_vsock')
-rw-r--r-- | net/vmw_vsock/Kconfig | 10 | ||||
-rw-r--r-- | net/vmw_vsock/Makefile | 3 | ||||
-rw-r--r-- | net/vmw_vsock/diag.c | 186 |
3 files changed, 199 insertions, 0 deletions
diff --git a/net/vmw_vsock/Kconfig b/net/vmw_vsock/Kconfig index a24369d175fd..970f96489fe7 100644 --- a/net/vmw_vsock/Kconfig +++ b/net/vmw_vsock/Kconfig | |||
@@ -15,6 +15,16 @@ config VSOCKETS | |||
15 | To compile this driver as a module, choose M here: the module | 15 | To compile this driver as a module, choose M here: the module |
16 | will be called vsock. If unsure, say N. | 16 | will be called vsock. If unsure, say N. |
17 | 17 | ||
18 | config VSOCKETS_DIAG | ||
19 | tristate "Virtual Sockets monitoring interface" | ||
20 | depends on VSOCKETS | ||
21 | default y | ||
22 | help | ||
23 | Support for PF_VSOCK sockets monitoring interface used by the ss tool. | ||
24 | If unsure, say Y. | ||
25 | |||
26 | Enable this module so userspace applications can query open sockets. | ||
27 | |||
18 | config VMWARE_VMCI_VSOCKETS | 28 | config VMWARE_VMCI_VSOCKETS |
19 | tristate "VMware VMCI transport for Virtual Sockets" | 29 | tristate "VMware VMCI transport for Virtual Sockets" |
20 | depends on VSOCKETS && VMWARE_VMCI | 30 | depends on VSOCKETS && VMWARE_VMCI |
diff --git a/net/vmw_vsock/Makefile b/net/vmw_vsock/Makefile index e63d574234a9..64afc06805da 100644 --- a/net/vmw_vsock/Makefile +++ b/net/vmw_vsock/Makefile | |||
@@ -1,4 +1,5 @@ | |||
1 | obj-$(CONFIG_VSOCKETS) += vsock.o | 1 | obj-$(CONFIG_VSOCKETS) += vsock.o |
2 | obj-$(CONFIG_VSOCKETS_DIAG) += vsock_diag.o | ||
2 | obj-$(CONFIG_VMWARE_VMCI_VSOCKETS) += vmw_vsock_vmci_transport.o | 3 | obj-$(CONFIG_VMWARE_VMCI_VSOCKETS) += vmw_vsock_vmci_transport.o |
3 | obj-$(CONFIG_VIRTIO_VSOCKETS) += vmw_vsock_virtio_transport.o | 4 | obj-$(CONFIG_VIRTIO_VSOCKETS) += vmw_vsock_virtio_transport.o |
4 | obj-$(CONFIG_VIRTIO_VSOCKETS_COMMON) += vmw_vsock_virtio_transport_common.o | 5 | obj-$(CONFIG_VIRTIO_VSOCKETS_COMMON) += vmw_vsock_virtio_transport_common.o |
@@ -6,6 +7,8 @@ obj-$(CONFIG_HYPERV_VSOCKETS) += hv_sock.o | |||
6 | 7 | ||
7 | vsock-y += af_vsock.o af_vsock_tap.o vsock_addr.o | 8 | vsock-y += af_vsock.o af_vsock_tap.o vsock_addr.o |
8 | 9 | ||
10 | vsock_diag-y += diag.o | ||
11 | |||
9 | vmw_vsock_vmci_transport-y += vmci_transport.o vmci_transport_notify.o \ | 12 | vmw_vsock_vmci_transport-y += vmci_transport.o vmci_transport_notify.o \ |
10 | vmci_transport_notify_qstate.o | 13 | vmci_transport_notify_qstate.o |
11 | 14 | ||
diff --git a/net/vmw_vsock/diag.c b/net/vmw_vsock/diag.c new file mode 100644 index 000000000000..31b567652250 --- /dev/null +++ b/net/vmw_vsock/diag.c | |||
@@ -0,0 +1,186 @@ | |||
1 | /* | ||
2 | * vsock sock_diag(7) module | ||
3 | * | ||
4 | * Copyright (C) 2017 Red Hat, Inc. | ||
5 | * Author: Stefan Hajnoczi <stefanha@redhat.com> | ||
6 | * | ||
7 | * This program is free software; you can redistribute it and/or modify it | ||
8 | * under the terms of the GNU General Public License as published by the Free | ||
9 | * Software Foundation version 2 and no later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
14 | * more details. | ||
15 | */ | ||
16 | |||
17 | #include <linux/module.h> | ||
18 | #include <linux/sock_diag.h> | ||
19 | #include <linux/vm_sockets_diag.h> | ||
20 | #include <net/af_vsock.h> | ||
21 | |||
22 | static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, | ||
23 | u32 portid, u32 seq, u32 flags) | ||
24 | { | ||
25 | struct vsock_sock *vsk = vsock_sk(sk); | ||
26 | struct vsock_diag_msg *rep; | ||
27 | struct nlmsghdr *nlh; | ||
28 | |||
29 | nlh = nlmsg_put(skb, portid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep), | ||
30 | flags); | ||
31 | if (!nlh) | ||
32 | return -EMSGSIZE; | ||
33 | |||
34 | rep = nlmsg_data(nlh); | ||
35 | rep->vdiag_family = AF_VSOCK; | ||
36 | |||
37 | /* Lock order dictates that sk_lock is acquired before | ||
38 | * vsock_table_lock, so we cannot lock here. Simply don't take | ||
39 | * sk_lock; sk is guaranteed to stay alive since vsock_table_lock is | ||
40 | * held. | ||
41 | */ | ||
42 | rep->vdiag_type = sk->sk_type; | ||
43 | rep->vdiag_state = sk->sk_state; | ||
44 | rep->vdiag_shutdown = sk->sk_shutdown; | ||
45 | rep->vdiag_src_cid = vsk->local_addr.svm_cid; | ||
46 | rep->vdiag_src_port = vsk->local_addr.svm_port; | ||
47 | rep->vdiag_dst_cid = vsk->remote_addr.svm_cid; | ||
48 | rep->vdiag_dst_port = vsk->remote_addr.svm_port; | ||
49 | rep->vdiag_ino = sock_i_ino(sk); | ||
50 | |||
51 | sock_diag_save_cookie(sk, rep->vdiag_cookie); | ||
52 | |||
53 | return 0; | ||
54 | } | ||
55 | |||
56 | static int vsock_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) | ||
57 | { | ||
58 | struct vsock_diag_req *req; | ||
59 | struct vsock_sock *vsk; | ||
60 | unsigned int bucket; | ||
61 | unsigned int last_i; | ||
62 | unsigned int table; | ||
63 | struct net *net; | ||
64 | unsigned int i; | ||
65 | |||
66 | req = nlmsg_data(cb->nlh); | ||
67 | net = sock_net(skb->sk); | ||
68 | |||
69 | /* State saved between calls: */ | ||
70 | table = cb->args[0]; | ||
71 | bucket = cb->args[1]; | ||
72 | i = last_i = cb->args[2]; | ||
73 | |||
74 | /* TODO VMCI pending sockets? */ | ||
75 | |||
76 | spin_lock_bh(&vsock_table_lock); | ||
77 | |||
78 | /* Bind table (locally created sockets) */ | ||
79 | if (table == 0) { | ||
80 | while (bucket < ARRAY_SIZE(vsock_bind_table)) { | ||
81 | struct list_head *head = &vsock_bind_table[bucket]; | ||
82 | |||
83 | i = 0; | ||
84 | list_for_each_entry(vsk, head, bound_table) { | ||
85 | struct sock *sk = sk_vsock(vsk); | ||
86 | |||
87 | if (!net_eq(sock_net(sk), net)) | ||
88 | continue; | ||
89 | if (i < last_i) | ||
90 | goto next_bind; | ||
91 | if (!(req->vdiag_states & (1 << sk->sk_state))) | ||
92 | goto next_bind; | ||
93 | if (sk_diag_fill(sk, skb, | ||
94 | NETLINK_CB(cb->skb).portid, | ||
95 | cb->nlh->nlmsg_seq, | ||
96 | NLM_F_MULTI) < 0) | ||
97 | goto done; | ||
98 | next_bind: | ||
99 | i++; | ||
100 | } | ||
101 | last_i = 0; | ||
102 | bucket++; | ||
103 | } | ||
104 | |||
105 | table++; | ||
106 | bucket = 0; | ||
107 | } | ||
108 | |||
109 | /* Connected table (accepted connections) */ | ||
110 | while (bucket < ARRAY_SIZE(vsock_connected_table)) { | ||
111 | struct list_head *head = &vsock_connected_table[bucket]; | ||
112 | |||
113 | i = 0; | ||
114 | list_for_each_entry(vsk, head, connected_table) { | ||
115 | struct sock *sk = sk_vsock(vsk); | ||
116 | |||
117 | /* Skip sockets we've already seen above */ | ||
118 | if (__vsock_in_bound_table(vsk)) | ||
119 | continue; | ||
120 | |||
121 | if (!net_eq(sock_net(sk), net)) | ||
122 | continue; | ||
123 | if (i < last_i) | ||
124 | goto next_connected; | ||
125 | if (!(req->vdiag_states & (1 << sk->sk_state))) | ||
126 | goto next_connected; | ||
127 | if (sk_diag_fill(sk, skb, | ||
128 | NETLINK_CB(cb->skb).portid, | ||
129 | cb->nlh->nlmsg_seq, | ||
130 | NLM_F_MULTI) < 0) | ||
131 | goto done; | ||
132 | next_connected: | ||
133 | i++; | ||
134 | } | ||
135 | last_i = 0; | ||
136 | bucket++; | ||
137 | } | ||
138 | |||
139 | done: | ||
140 | spin_unlock_bh(&vsock_table_lock); | ||
141 | |||
142 | cb->args[0] = table; | ||
143 | cb->args[1] = bucket; | ||
144 | cb->args[2] = i; | ||
145 | |||
146 | return skb->len; | ||
147 | } | ||
148 | |||
149 | static int vsock_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h) | ||
150 | { | ||
151 | int hdrlen = sizeof(struct vsock_diag_req); | ||
152 | struct net *net = sock_net(skb->sk); | ||
153 | |||
154 | if (nlmsg_len(h) < hdrlen) | ||
155 | return -EINVAL; | ||
156 | |||
157 | if (h->nlmsg_flags & NLM_F_DUMP) { | ||
158 | struct netlink_dump_control c = { | ||
159 | .dump = vsock_diag_dump, | ||
160 | }; | ||
161 | return netlink_dump_start(net->diag_nlsk, skb, h, &c); | ||
162 | } | ||
163 | |||
164 | return -EOPNOTSUPP; | ||
165 | } | ||
166 | |||
167 | static const struct sock_diag_handler vsock_diag_handler = { | ||
168 | .family = AF_VSOCK, | ||
169 | .dump = vsock_diag_handler_dump, | ||
170 | }; | ||
171 | |||
172 | static int __init vsock_diag_init(void) | ||
173 | { | ||
174 | return sock_diag_register(&vsock_diag_handler); | ||
175 | } | ||
176 | |||
177 | static void __exit vsock_diag_exit(void) | ||
178 | { | ||
179 | sock_diag_unregister(&vsock_diag_handler); | ||
180 | } | ||
181 | |||
182 | module_init(vsock_diag_init); | ||
183 | module_exit(vsock_diag_exit); | ||
184 | MODULE_LICENSE("GPL"); | ||
185 | MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, | ||
186 | 40 /* AF_VSOCK */); | ||