^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * vsock sock_diag(7) module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2017 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Stefan Hajnoczi <stefanha@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/sock_diag.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/vm_sockets_diag.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <net/af_vsock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) u32 portid, u32 seq, u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct vsock_sock *vsk = vsock_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct vsock_diag_msg *rep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) nlh = nlmsg_put(skb, portid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) if (!nlh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) rep = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) rep->vdiag_family = AF_VSOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /* Lock order dictates that sk_lock is acquired before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * vsock_table_lock, so we cannot lock here. Simply don't take
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * sk_lock; sk is guaranteed to stay alive since vsock_table_lock is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) rep->vdiag_type = sk->sk_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) rep->vdiag_state = sk->sk_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) rep->vdiag_shutdown = sk->sk_shutdown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) rep->vdiag_src_cid = vsk->local_addr.svm_cid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) rep->vdiag_src_port = vsk->local_addr.svm_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) rep->vdiag_dst_cid = vsk->remote_addr.svm_cid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) rep->vdiag_dst_port = vsk->remote_addr.svm_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) rep->vdiag_ino = sock_i_ino(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) sock_diag_save_cookie(sk, rep->vdiag_cookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static int vsock_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct vsock_diag_req *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct vsock_sock *vsk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned int bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) unsigned int last_i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) unsigned int table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct net *net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) req = nlmsg_data(cb->nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /* State saved between calls: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) table = cb->args[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) bucket = cb->args[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) i = last_i = cb->args[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /* TODO VMCI pending sockets? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) spin_lock_bh(&vsock_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /* Bind table (locally created sockets) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (table == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) while (bucket < ARRAY_SIZE(vsock_bind_table)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct list_head *head = &vsock_bind_table[bucket];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) list_for_each_entry(vsk, head, bound_table) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct sock *sk = sk_vsock(vsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (!net_eq(sock_net(sk), net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (i < last_i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) goto next_bind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!(req->vdiag_states & (1 << sk->sk_state)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) goto next_bind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (sk_diag_fill(sk, skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) NETLINK_CB(cb->skb).portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) cb->nlh->nlmsg_seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) NLM_F_MULTI) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) next_bind:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) last_i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) bucket++;
^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) table++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) bucket = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /* Connected table (accepted connections) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) while (bucket < ARRAY_SIZE(vsock_connected_table)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct list_head *head = &vsock_connected_table[bucket];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) list_for_each_entry(vsk, head, connected_table) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct sock *sk = sk_vsock(vsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /* Skip sockets we've already seen above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (__vsock_in_bound_table(vsk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (!net_eq(sock_net(sk), net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (i < last_i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) goto next_connected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (!(req->vdiag_states & (1 << sk->sk_state)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) goto next_connected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (sk_diag_fill(sk, skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) NETLINK_CB(cb->skb).portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) cb->nlh->nlmsg_seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) NLM_F_MULTI) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) next_connected:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) last_i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) bucket++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) spin_unlock_bh(&vsock_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) cb->args[0] = table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) cb->args[1] = bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) cb->args[2] = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static int vsock_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) int hdrlen = sizeof(struct vsock_diag_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (nlmsg_len(h) < hdrlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (h->nlmsg_flags & NLM_F_DUMP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct netlink_dump_control c = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) .dump = vsock_diag_dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return netlink_dump_start(net->diag_nlsk, skb, h, &c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static const struct sock_diag_handler vsock_diag_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) .family = AF_VSOCK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) .dump = vsock_diag_handler_dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static int __init vsock_diag_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return sock_diag_register(&vsock_diag_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static void __exit vsock_diag_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) sock_diag_unregister(&vsock_diag_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) module_init(vsock_diag_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) module_exit(vsock_diag_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 40 /* AF_VSOCK */);