1 // SPDX-License-Identifier: GPL-2.0-only
3 * vsock sock_diag(7) module
5 * Copyright (C) 2017 Red Hat, Inc.
6 * Author: Stefan Hajnoczi <stefanha@redhat.com>
9 #include <linux/module.h>
10 #include <linux/sock_diag.h>
11 #include <linux/vm_sockets_diag.h>
12 #include <net/af_vsock.h>
14 static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
15 u32 portid, u32 seq, u32 flags)
17 struct vsock_sock *vsk = vsock_sk(sk);
18 struct vsock_diag_msg *rep;
21 nlh = nlmsg_put(skb, portid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep),
26 rep = nlmsg_data(nlh);
27 rep->vdiag_family = AF_VSOCK;
29 /* Lock order dictates that sk_lock is acquired before
30 * vsock_table_lock, so we cannot lock here. Simply don't take
31 * sk_lock; sk is guaranteed to stay alive since vsock_table_lock is
34 rep->vdiag_type = sk->sk_type;
35 rep->vdiag_state = sk->sk_state;
36 rep->vdiag_shutdown = sk->sk_shutdown;
37 rep->vdiag_src_cid = vsk->local_addr.svm_cid;
38 rep->vdiag_src_port = vsk->local_addr.svm_port;
39 rep->vdiag_dst_cid = vsk->remote_addr.svm_cid;
40 rep->vdiag_dst_port = vsk->remote_addr.svm_port;
41 rep->vdiag_ino = sock_i_ino(sk);
43 sock_diag_save_cookie(sk, rep->vdiag_cookie);
48 static int vsock_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
50 struct vsock_diag_req *req;
51 struct vsock_sock *vsk;
58 req = nlmsg_data(cb->nlh);
59 net = sock_net(skb->sk);
61 /* State saved between calls: */
64 i = last_i = cb->args[2];
66 /* TODO VMCI pending sockets? */
68 spin_lock_bh(&vsock_table_lock);
70 /* Bind table (locally created sockets) */
72 while (bucket < ARRAY_SIZE(vsock_bind_table)) {
73 struct list_head *head = &vsock_bind_table[bucket];
76 list_for_each_entry(vsk, head, bound_table) {
77 struct sock *sk = sk_vsock(vsk);
79 if (!net_eq(sock_net(sk), net))
83 if (!(req->vdiag_states & (1 << sk->sk_state)))
85 if (sk_diag_fill(sk, skb,
86 NETLINK_CB(cb->skb).portid,
101 /* Connected table (accepted connections) */
102 while (bucket < ARRAY_SIZE(vsock_connected_table)) {
103 struct list_head *head = &vsock_connected_table[bucket];
106 list_for_each_entry(vsk, head, connected_table) {
107 struct sock *sk = sk_vsock(vsk);
109 /* Skip sockets we've already seen above */
110 if (__vsock_in_bound_table(vsk))
113 if (!net_eq(sock_net(sk), net))
117 if (!(req->vdiag_states & (1 << sk->sk_state)))
119 if (sk_diag_fill(sk, skb,
120 NETLINK_CB(cb->skb).portid,
132 spin_unlock_bh(&vsock_table_lock);
135 cb->args[1] = bucket;
141 static int vsock_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
143 int hdrlen = sizeof(struct vsock_diag_req);
144 struct net *net = sock_net(skb->sk);
146 if (nlmsg_len(h) < hdrlen)
149 if (h->nlmsg_flags & NLM_F_DUMP) {
150 struct netlink_dump_control c = {
151 .dump = vsock_diag_dump,
153 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
159 static const struct sock_diag_handler vsock_diag_handler = {
161 .dump = vsock_diag_handler_dump,
164 static int __init vsock_diag_init(void)
166 return sock_diag_register(&vsock_diag_handler);
169 static void __exit vsock_diag_exit(void)
171 sock_diag_unregister(&vsock_diag_handler);
174 module_init(vsock_diag_init);
175 module_exit(vsock_diag_exit);
176 MODULE_LICENSE("GPL");
177 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG,