1 .\" Copyright (c) 2016 Pavel Emelyanov <xemul@virtuozzo.com>
2 .\" Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
4 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
5 .\" This is free documentation; you can redistribute it and/or
6 .\" modify it under the terms of the GNU General Public License as
7 .\" published by the Free Software Foundation; either version 2 of
8 .\" the License, or (at your option) any later version.
10 .\" The GNU General Public License's references to "object code"
11 .\" and "executables" are to be interpreted as the output of any
12 .\" document formatting or typesetting system, including
13 .\" intermediate and printed output.
15 .\" This manual is distributed in the hope that it will be useful,
16 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
17 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 .\" GNU General Public License for more details.
20 .\" You should have received a copy of the GNU General Public
21 .\" License along with this manual; if not, see
22 .\" <http://www.gnu.org/licenses/>.
24 .TH SOCK_DIAG 7 2021-03-22 "Linux" "Linux Programmer's Manual"
26 sock_diag \- obtaining information about sockets
29 .B #include <sys/socket.h>
30 .B #include <linux/sock_diag.h>
31 .BR "#include <linux/unix_diag.h>" " /* for UNIX domain sockets */"
32 .BR "#include <linux/inet_diag.h>" " /* for IPv4 and IPv6 sockets */"
34 .BI "diag_socket = socket(AF_NETLINK, " socket_type ", NETLINK_SOCK_DIAG);"
37 The sock_diag netlink subsystem provides a mechanism for obtaining
38 information about sockets of various address families from the kernel.
39 This subsystem can be used to obtain information about individual
40 sockets or request a list of sockets.
42 In the request, the caller can specify additional information it would
43 like to obtain about the socket, for example, memory information or
44 information specific to the address family.
46 When requesting a list of sockets, the caller can specify filters that
47 would be applied by the kernel to select a subset of sockets to report.
48 For now, there is only the ability to filter sockets by state (connected,
49 listening, and so on.)
51 Note that sock_diag reports only those sockets that have a name;
52 that is, either sockets bound explicitly with
54 or sockets that were automatically bound to an address (e.g., by
56 This is the same set of sockets that is available via
63 The request starts with a
70 .BR SOCK_DIAG_BY_FAMILY .
71 It is followed by a header specific to the address family that starts with
72 a common part shared by all address families:
76 struct sock_diag_req {
83 The fields of this structure are as follows:
87 It should be set to the appropriate
94 It should be set to the appropriate
108 flag set, it means that a list of sockets is being requested;
109 otherwise it is a query about an individual socket.
112 The response starts with a
114 header and is followed by an array of objects specific to the address family.
115 The array is to be accessed with the standard
121 Each object is the NLA (netlink attributes) list that is to be accessed
128 .SS UNIX domain sockets
129 For UNIX domain sockets the request is represented in the following structure:
133 struct unix_diag_req {
140 __u32 udiag_cookie[2];
145 The fields of this structure are as follows:
148 The address family; it should be set to
156 These fields should be set to 0.
159 This is a bit mask that defines a filter of sockets states.
160 Only those sockets whose states are in this mask will be reported.
161 Ignored when querying for an individual socket.
162 Supported values are:
173 This is an inode number when querying for an individual socket.
174 Ignored when querying for a list of sockets.
177 This is a set of flags defining what kind of information to report.
178 Each requested kind of information is reported back as a netlink
179 attribute as described below:
183 The attribute reported in answer to this request is
185 The payload associated with this attribute is the pathname to which
186 the socket was bound (a sequence of bytes up to
191 The attribute reported in answer to this request is
193 The payload associated with this attribute is represented in the following
198 struct unix_diag_vfs {
205 The fields of this structure are as follows:
209 The device number of the corresponding on-disk socket inode.
212 The inode number of the corresponding on-disk socket inode.
216 The attribute reported in answer to this request is
218 The payload associated with this attribute is a __u32 value
219 which is the peer's inode number.
220 This attribute is reported for connected sockets only.
223 The attribute reported in answer to this request is
224 .BR UNIX_DIAG_ICONS .
225 The payload associated with this attribute is an array of __u32 values
226 which are inode numbers of sockets that has passed the
228 call, but hasn't been processed with
231 This attribute is reported for listening sockets only.
234 The attribute reported in answer to this request is
235 .BR UNIX_DIAG_RQLEN .
236 The payload associated with this attribute is represented in the following
241 struct unix_diag_rqlen {
248 The fields of this structure are as follows:
252 For listening sockets:
253 the number of pending connections.
254 The length of the array associated with the
256 response attribute is equal to this value.
258 For established sockets:
259 the amount of data in incoming queue.
262 For listening sockets:
263 the backlog length which equals to the value passed as the second argument to
266 For established sockets:
267 the amount of memory available for sending.
270 .B UDIAG_SHOW_MEMINFO
271 The attribute reported in answer to this request is
272 .BR UNIX_DIAG_MEMINFO .
273 The payload associated with this attribute is an array of __u32 values
274 described below in the subsection "Socket memory information".
276 The following attributes are reported back without any specific request:
278 .BR UNIX_DIAG_SHUTDOWN
279 The payload associated with this attribute is __u8 value which represents
286 This is an array of opaque identifiers that could be used along with
288 to specify an individual socket.
289 It is ignored when querying for a list
290 of sockets, as well as when all its elements are set to \-1.
292 The response to a query for UNIX domain sockets is represented as an array of
296 struct unix_diag_msg {
302 __u32 udiag_cookie[2];
307 followed by netlink attributes.
309 The fields of this structure are as follows:
312 This field has the same meaning as in
313 .IR "struct unix_diag_req" .
316 This is set to one of
323 This is set to one of
326 .BR TCP_ESTABLISHED .
329 This field is set to 0.
332 This is the socket inode number.
335 This is an array of opaque identifiers that could be used in subsequent
338 .SS IPv4 and IPv6 sockets
339 For IPv4 and IPv6 sockets,
340 the request is represented in the following structure:
344 struct inet_diag_req_v2 {
350 struct inet_diag_sockid id;
356 .I "struct inet_diag_sockid"
357 is defined as follows:
361 struct inet_diag_sockid {
367 __u32 idiag_cookie[2];
373 .I "struct inet_diag_req_v2"
377 This should be set to either
381 for IPv4 or IPv6 sockets respectively.
384 This should be set to one of
388 .BR IPPROTO_UDPLITE .
391 This is a set of flags defining what kind of extended information to report.
392 Each requested kind of information is reported back as a netlink attribute
397 The payload associated with this attribute is a __u8 value
398 which is the TOS of the socket.
401 The payload associated with this attribute is a __u8 value
402 which is the TClass of the socket.
404 For LISTEN and CLOSE sockets, this is followed by
405 .B INET_DIAG_SKV6ONLY
406 attribute with associated __u8 payload value meaning whether the socket
410 The payload associated with this attribute is represented in the following
415 struct inet_diag_meminfo {
424 The fields of this structure are as follows:
428 The amount of data in the receive queue.
431 The amount of data that is queued by TCP but not yet sent.
434 The amount of memory scheduled for future use (TCP only).
437 The amount of data in send queue.
440 .B INET_DIAG_SKMEMINFO
441 The payload associated with this attribute is an array of __u32 values
442 described below in the subsection "Socket memory information".
445 The payload associated with this attribute is specific to the address family.
446 For TCP sockets, it is an object of type
447 .IR "struct tcp_info" .
450 The payload associated with this attribute is a string that describes the
451 congestion control algorithm used.
452 For TCP sockets only.
456 This should be set to 0.
459 This is a bit mask that defines a filter of socket states.
460 Only those sockets whose states are in this mask will be reported.
461 Ignored when querying for an individual socket.
464 This is a socket ID object that is used in dump requests, in queries
465 about individual sockets, and is reported back in each response.
466 Unlike UNIX domain sockets, IPv4 and IPv6 sockets are identified
467 using addresses and ports.
468 All values are in network byte order.
471 .I "struct inet_diag_sockid"
478 The destination port.
484 The destination address.
487 The interface number the socket is bound to.
490 This is an array of opaque identifiers that could be used along with
491 other fields of this structure to specify an individual socket.
492 It is ignored when querying for a list of sockets, as well as
493 when all its elements are set to \-1.
495 The response to a query for IPv4 or IPv6 sockets is represented as an array of
499 struct inet_diag_msg {
505 struct inet_diag_sockid id;
516 followed by netlink attributes.
518 The fields of this structure are as follows:
521 This is the same field as in
522 .IR "struct inet_diag_req_v2" .
525 This denotes socket state as in
526 .IR "struct inet_diag_req_v2" .
529 For TCP sockets, this field describes the type of timer that is currently
530 active for the socket.
531 It is set to one of the following constants:
549 a zero window probe timer
553 For non-TCP sockets, this field is set to 0.
558 values 1, 2, and 4, this field contains the number of retransmits.
561 values, this field is set to 0.
564 For TCP sockets that have an active timer, this field describes its expiration
565 time in milliseconds.
566 For other sockets, this field is set to 0.
569 For listening sockets:
570 the number of pending connections.
573 the amount of data in the incoming queue.
576 For listening sockets:
580 the amount of memory available for sending.
583 This is the socket owner UID.
586 This is the socket inode number.
588 .SS Socket memory information
589 The payload associated with
592 .BR INET_DIAG_SKMEMINFO
593 netlink attributes is an array of the following __u32 values:
595 .B SK_MEMINFO_RMEM_ALLOC
596 The amount of data in receive queue.
599 The receive socket buffer as set by
602 .B SK_MEMINFO_WMEM_ALLOC
603 The amount of data in send queue.
606 The send socket buffer as set by
609 .B SK_MEMINFO_FWD_ALLOC
610 The amount of memory scheduled for future use (TCP only).
612 .B SK_MEMINFO_WMEM_QUEUED
613 The amount of data queued by TCP, but not yet sent.
616 The amount of memory allocated for the socket's service needs (e.g., socket
619 .B SK_MEMINFO_BACKLOG
620 The amount of packets in the backlog (not yet processed).
623 was introduced in Linux 2.6.14 and supported
628 In Linux 3.3, it was renamed to
630 and extended to support
636 .BR INET_DIAG_SKMEMINFO
637 were introduced in Linux 3.6.
639 The NETLINK_SOCK_DIAG API is Linux-specific.
641 The following example program prints inode number, peer's inode number,
642 and name of all UNIX domain sockets in the current namespace.
649 #include <sys/socket.h>
651 #include <linux/netlink.h>
652 #include <linux/rtnetlink.h>
653 #include <linux/sock_diag.h>
654 #include <linux/unix_diag.h>
659 struct sockaddr_nl nladdr = {
660 .nl_family = AF_NETLINK
665 struct unix_diag_req udr;
668 .nlmsg_len = sizeof(req),
669 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
670 .nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP
673 .sdiag_family = AF_UNIX,
675 .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER
680 .iov_len = sizeof(req)
682 struct msghdr msg = {
684 .msg_namelen = sizeof(nladdr),
690 if (sendmsg(fd, &msg, 0) < 0) {
703 print_diag(const struct unix_diag_msg *diag, unsigned int len)
705 if (len < NLMSG_LENGTH(sizeof(*diag))) {
706 fputs("short response\en", stderr);
709 if (diag\->udiag_family != AF_UNIX) {
710 fprintf(stderr, "unexpected family %u\en", diag\->udiag_family);
714 unsigned int rta_len = len \- NLMSG_LENGTH(sizeof(*diag));
715 unsigned int peer = 0;
717 char path[sizeof(((struct sockaddr_un *) 0)\->sun_path) + 1];
719 for (struct rtattr *attr = (struct rtattr *) (diag + 1);
720 RTA_OK(attr, rta_len); attr = RTA_NEXT(attr, rta_len)) {
721 switch (attr\->rta_type) {
724 path_len = RTA_PAYLOAD(attr);
725 if (path_len > sizeof(path) \- 1)
726 path_len = sizeof(path) \- 1;
727 memcpy(path, RTA_DATA(attr), path_len);
728 path[path_len] = \(aq\e0\(aq;
733 if (RTA_PAYLOAD(attr) >= sizeof(peer))
734 peer = *(unsigned int *) RTA_DATA(attr);
739 printf("inode=%u", diag\->udiag_ino);
742 printf(", peer=%u", peer);
745 printf(", name=%s%s", *path ? "" : "@",
746 *path ? path : path + 1);
748 putchar(\(aq\en\(aq);
753 receive_responses(int fd)
755 long buf[8192 / sizeof(long)];
756 struct sockaddr_nl nladdr;
759 .iov_len = sizeof(buf)
764 struct msghdr msg = {
766 .msg_namelen = sizeof(nladdr),
771 ssize_t ret = recvmsg(fd, &msg, flags);
783 if (nladdr.nl_family != AF_NETLINK) {
784 fputs("!AF_NETLINK\en", stderr);
788 const struct nlmsghdr *h = (struct nlmsghdr *) buf;
790 if (!NLMSG_OK(h, ret)) {
791 fputs("!NLMSG_OK\en", stderr);
795 for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) {
796 if (h\->nlmsg_type == NLMSG_DONE)
799 if (h\->nlmsg_type == NLMSG_ERROR) {
800 const struct nlmsgerr *err = NLMSG_DATA(h);
802 if (h\->nlmsg_len < NLMSG_LENGTH(sizeof(*err))) {
803 fputs("NLMSG_ERROR\en", stderr);
805 errno = \-err\->error;
806 perror("NLMSG_ERROR");
812 if (h\->nlmsg_type != SOCK_DIAG_BY_FAMILY) {
813 fprintf(stderr, "unexpected nlmsg_type %u\en",
814 (unsigned) h\->nlmsg_type);
818 if (print_diag(NLMSG_DATA(h), h\->nlmsg_len))
827 int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
834 int ret = send_query(fd) || receive_responses(fd);