man/: ffix
[man-pages.git] / man / man7 / sock_diag.7
blob33fb2702f02193d944df3fc6146ddf1a9459319b
1 .\" Copyright (c) 2016 Pavel Emelyanov <xemul@virtuozzo.com>
2 .\" Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
3 .\"
4 .\" SPDX-License-Identifier: GPL-2.0-or-later
5 .TH sock_diag 7 (date) "Linux man-pages (unreleased)"
6 .SH NAME
7 sock_diag \- obtaining information about sockets
8 .SH SYNOPSIS
9 .nf
10 .B #include <sys/socket.h>
11 .B #include <linux/sock_diag.h>
12 .BR "#include <linux/unix_diag.h>" " /* for UNIX domain sockets */"
13 .BR "#include <linux/inet_diag.h>" " /* for IPv4 and IPv6 sockets */"
15 .BI "diag_socket = socket(AF_NETLINK, " socket_type ", NETLINK_SOCK_DIAG);"
16 .fi
17 .SH DESCRIPTION
18 The sock_diag netlink subsystem provides a mechanism for obtaining
19 information about sockets of various address families from the kernel.
20 This subsystem can be used to obtain information about individual
21 sockets or request a list of sockets.
23 In the request, the caller can specify additional information it would
24 like to obtain about the socket, for example, memory information or
25 information specific to the address family.
27 When requesting a list of sockets, the caller can specify filters that
28 would be applied by the kernel to select a subset of sockets to report.
29 For now, there is only the ability to filter sockets by state (connected,
30 listening, and so on.)
32 Note that sock_diag reports only those sockets that have a name;
33 that is, either sockets bound explicitly with
34 .BR bind (2)
35 or sockets that were automatically bound to an address (e.g., by
36 .BR connect (2)).
37 This is the same set of sockets that is available via
38 .IR /proc/net/unix ,
39 .IR /proc/net/tcp ,
40 .IR /proc/net/udp ,
41 and so on.
42 .\"
43 .SS Request
44 The request starts with a
45 .I "struct nlmsghdr"
46 header described in
47 .BR netlink (7)
48 with
49 .I nlmsg_type
50 field set to
51 .BR SOCK_DIAG_BY_FAMILY .
52 It is followed by a header specific to the address family that starts with
53 a common part shared by all address families:
55 .in +4n
56 .EX
57 struct sock_diag_req {
58     __u8 sdiag_family;
59     __u8 sdiag_protocol;
61 .EE
62 .in
64 The fields of this structure are as follows:
65 .TP
66 .I sdiag_family
67 An address family.
68 It should be set to the appropriate
69 .B AF_*
70 constant.
71 .TP
72 .I sdiag_protocol
73 Depends on
74 .IR sdiag_family .
75 It should be set to the appropriate
76 .B IPPROTO_*
77 constant for
78 .B AF_INET
79 and
80 .BR AF_INET6 ,
81 and to 0 otherwise.
83 If the
84 .I nlmsg_flags
85 field of the
86 .I "struct nlmsghdr"
87 header has the
88 .B NLM_F_DUMP
89 flag set, it means that a list of sockets is being requested;
90 otherwise it is a query about an individual socket.
91 .\"
92 .SS Response
93 The response starts with a
94 .I "struct nlmsghdr"
95 header and is followed by an array of objects specific to the address family.
96 The array is to be accessed with the standard
97 .B NLMSG_*
98 macros from the
99 .BR netlink (3)
100 API.
102 Each object is the NLA (netlink attributes) list that is to be accessed
103 with the
104 .B RTA_*
105 macros from
106 .BR rtnetlink (3)
107 API.
109 .SS UNIX domain sockets
110 For UNIX domain sockets the request is represented in the following structure:
112 .in +4n
114 struct unix_diag_req {
115     __u8    sdiag_family;
116     __u8    sdiag_protocol;
117     __u16   pad;
118     __u32   udiag_states;
119     __u32   udiag_ino;
120     __u32   udiag_show;
121     __u32   udiag_cookie[2];
126 The fields of this structure are as follows:
128 .I sdiag_family
129 The address family; it should be set to
130 .BR AF_UNIX .
132 .I sdiag_protocol
133 .PD 0
136 .I pad
137 These fields should be set to 0.
139 .I udiag_states
140 This is a bit mask that defines a filter of sockets states.
141 Only those sockets whose states are in this mask will be reported.
142 Ignored when querying for an individual socket.
143 Supported values are:
145 .RS 12
146 1 <<
147 .B TCP_ESTABLISHED
149 1 <<
150 .B TCP_LISTEN
153 .I udiag_ino
154 This is an inode number when querying for an individual socket.
155 Ignored when querying for a list of sockets.
157 .I udiag_show
158 This is a set of flags defining what kind of information to report.
159 Each requested kind of information is reported back as a netlink
160 attribute as described below:
163 .B UDIAG_SHOW_NAME
164 The attribute reported in answer to this request is
165 .BR UNIX_DIAG_NAME .
166 The payload associated with this attribute is the pathname to which
167 the socket was bound (a sequence of bytes up to
168 .B UNIX_PATH_MAX
169 length).
171 .B UDIAG_SHOW_VFS
172 The attribute reported in answer to this request is
173 .BR UNIX_DIAG_VFS .
174 The payload associated with this attribute is represented in the following
175 structure:
177 .in +4n
179 struct unix_diag_vfs {
180     __u32 udiag_vfs_dev;
181     __u32 udiag_vfs_ino;
186 The fields of this structure are as follows:
189 .I udiag_vfs_dev
190 The device number of the corresponding on-disk socket inode.
192 .I udiag_vfs_ino
193 The inode number of the corresponding on-disk socket inode.
196 .B UDIAG_SHOW_PEER
197 The attribute reported in answer to this request is
198 .BR UNIX_DIAG_PEER .
199 The payload associated with this attribute is a __u32 value
200 which is the peer's inode number.
201 This attribute is reported for connected sockets only.
203 .B UDIAG_SHOW_ICONS
204 The attribute reported in answer to this request is
205 .BR UNIX_DIAG_ICONS .
206 The payload associated with this attribute is an array of __u32 values
207 which are inode numbers of sockets that has passed the
208 .BR connect (2)
209 call, but hasn't been processed with
210 .BR accept (2)
211 yet.
212 This attribute is reported for listening sockets only.
214 .B UDIAG_SHOW_RQLEN
215 The attribute reported in answer to this request is
216 .BR UNIX_DIAG_RQLEN .
217 The payload associated with this attribute is represented in the following
218 structure:
220 .in +4n
222 struct unix_diag_rqlen {
223     __u32 udiag_rqueue;
224     __u32 udiag_wqueue;
229 The fields of this structure are as follows:
232 .I udiag_rqueue
233 For listening sockets:
234 the number of pending connections.
235 The length of the array associated with the
236 .B UNIX_DIAG_ICONS
237 response attribute is equal to this value.
239 For established sockets:
240 the amount of data in incoming queue.
242 .I udiag_wqueue
243 For listening sockets:
244 the backlog length which equals to the value passed as the second argument to
245 .BR listen (2).
247 For established sockets:
248 the amount of memory available for sending.
251 .B UDIAG_SHOW_MEMINFO
252 The attribute reported in answer to this request is
253 .BR UNIX_DIAG_MEMINFO .
254 The payload associated with this attribute is an array of __u32 values
255 described below in the subsection "Socket memory information".
257 The following attributes are reported back without any specific request:
259 .B UNIX_DIAG_SHUTDOWN
260 The payload associated with this attribute is __u8 value which represents
261 bits of
262 .BR shutdown (2)
263 state.
266 .I udiag_cookie
267 This is an array of opaque identifiers that could be used along with
268 .I udiag_ino
269 to specify an individual socket.
270 It is ignored when querying for a list
271 of sockets, as well as when all its elements are set to \-1.
273 The response to a query for UNIX domain sockets is represented as an array of
275 .in +4n
277 struct unix_diag_msg {
278     __u8    udiag_family;
279     __u8    udiag_type;
280     __u8    udiag_state;
281     __u8    pad;
282     __u32   udiag_ino;
283     __u32   udiag_cookie[2];
288 followed by netlink attributes.
290 The fields of this structure are as follows:
292 .I udiag_family
293 This field has the same meaning as in
294 .IR "struct unix_diag_req" .
296 .I udiag_type
297 This is set to one of
298 .BR SOCK_PACKET ,
299 .BR SOCK_STREAM ,
301 .BR SOCK_SEQPACKET .
303 .I udiag_state
304 This is set to one of
305 .B TCP_LISTEN
307 .BR TCP_ESTABLISHED .
309 .I pad
310 This field is set to 0.
312 .I udiag_ino
313 This is the socket inode number.
315 .I udiag_cookie
316 This is an array of opaque identifiers that could be used in subsequent
317 queries.
319 .SS IPv4 and IPv6 sockets
320 For IPv4 and IPv6 sockets,
321 the request is represented in the following structure:
323 .in +4n
325 struct inet_diag_req_v2 {
326     __u8    sdiag_family;
327     __u8    sdiag_protocol;
328     __u8    idiag_ext;
329     __u8    pad;
330     __u32   idiag_states;
331     struct inet_diag_sockid id;
336 where
337 .I "struct inet_diag_sockid"
338 is defined as follows:
340 .in +4n
342 struct inet_diag_sockid {
343     __be16  idiag_sport;
344     __be16  idiag_dport;
345     __be32  idiag_src[4];
346     __be32  idiag_dst[4];
347     __u32   idiag_if;
348     __u32   idiag_cookie[2];
353 The fields of
354 .I "struct inet_diag_req_v2"
355 are as follows:
357 .I sdiag_family
358 This should be set to either
359 .B AF_INET
361 .B AF_INET6
362 for IPv4 or IPv6 sockets respectively.
364 .I sdiag_protocol
365 This should be set to one of
366 .BR IPPROTO_TCP ,
367 .BR IPPROTO_UDP ,
369 .BR IPPROTO_UDPLITE .
371 .I idiag_ext
372 This is a set of flags defining what kind of extended information to report.
373 Each requested kind of information is reported back as a netlink attribute
374 as described below:
377 .B INET_DIAG_TOS
378 The payload associated with this attribute is a __u8 value
379 which is the TOS of the socket.
381 .B INET_DIAG_TCLASS
382 The payload associated with this attribute is a __u8 value
383 which is the TClass of the socket.
384 IPv6 sockets only.
385 For LISTEN and CLOSE sockets, this is followed by
386 .B INET_DIAG_SKV6ONLY
387 attribute with associated __u8 payload value meaning whether the socket
388 is IPv6-only or not.
390 .B INET_DIAG_MEMINFO
391 The payload associated with this attribute is represented in the following
392 structure:
394 .in +4n
396 struct inet_diag_meminfo {
397     __u32 idiag_rmem;
398     __u32 idiag_wmem;
399     __u32 idiag_fmem;
400     __u32 idiag_tmem;
405 The fields of this structure are as follows:
407 .TP 12
408 .I idiag_rmem
409 The amount of data in the receive queue.
411 .I idiag_wmem
412 The amount of data that is queued by TCP but not yet sent.
414 .I idiag_fmem
415 The amount of memory scheduled for future use (TCP only).
417 .I idiag_tmem
418 The amount of data in send queue.
421 .B INET_DIAG_SKMEMINFO
422 The payload associated with this attribute is an array of __u32 values
423 described below in the subsection "Socket memory information".
425 .B INET_DIAG_INFO
426 The payload associated with this attribute is specific to the address family.
427 For TCP sockets, it is an object of type
428 .IR "struct tcp_info" .
430 .B INET_DIAG_CONG
431 The payload associated with this attribute is a string that describes the
432 congestion control algorithm used.
433 For TCP sockets only.
436 .I pad
437 This should be set to 0.
439 .I idiag_states
440 This is a bit mask that defines a filter of socket states.
441 Only those sockets whose states are in this mask will be reported.
442 Ignored when querying for an individual socket.
444 .I id
445 This is a socket ID object that is used in dump requests, in queries
446 about individual sockets, and is reported back in each response.
447 Unlike UNIX domain sockets, IPv4 and IPv6 sockets are identified
448 using addresses and ports.
449 All values are in network byte order.
451 The fields of
452 .I "struct inet_diag_sockid"
453 are as follows:
455 .I idiag_sport
456 The source port.
458 .I idiag_dport
459 The destination port.
461 .I idiag_src
462 The source address.
464 .I idiag_dst
465 The destination address.
467 .I idiag_if
468 The interface number the socket is bound to.
470 .I idiag_cookie
471 This is an array of opaque identifiers that could be used along with
472 other fields of this structure to specify an individual socket.
473 It is ignored when querying for a list of sockets, as well as
474 when all its elements are set to \-1.
476 The response to a query for IPv4 or IPv6 sockets is represented as an array of
478 .in +4n
480 struct inet_diag_msg {
481     __u8    idiag_family;
482     __u8    idiag_state;
483     __u8    idiag_timer;
484     __u8    idiag_retrans;
486     struct inet_diag_sockid id;
488     __u32   idiag_expires;
489     __u32   idiag_rqueue;
490     __u32   idiag_wqueue;
491     __u32   idiag_uid;
492     __u32   idiag_inode;
497 followed by netlink attributes.
499 The fields of this structure are as follows:
501 .I idiag_family
502 This is the same field as in
503 .IR "struct inet_diag_req_v2" .
505 .I idiag_state
506 This denotes socket state as in
507 .IR "struct inet_diag_req_v2" .
509 .I idiag_timer
510 For TCP sockets, this field describes the type of timer that is currently
511 active for the socket.
512 It is set to one of the following constants:
514 .PD 0
515 .RS 12
517 .B 0
518 no timer is active
520 .B 1
521 a retransmit timer
523 .B 2
524 a keep-alive timer
526 .B 3
527 a TIME_WAIT timer
529 .B 4
530 a zero window probe timer
534 For non-TCP sockets, this field is set to 0.
536 .I idiag_retrans
538 .I idiag_timer
539 values 1, 2, and 4, this field contains the number of retransmits.
540 For other
541 .I idiag_timer
542 values, this field is set to 0.
544 .I idiag_expires
545 For TCP sockets that have an active timer, this field describes its expiration
546 time in milliseconds.
547 For other sockets, this field is set to 0.
549 .I idiag_rqueue
550 For listening sockets:
551 the number of pending connections.
553 For other sockets:
554 the amount of data in the incoming queue.
556 .I idiag_wqueue
557 For listening sockets:
558 the backlog length.
560 For other sockets:
561 the amount of memory available for sending.
563 .I idiag_uid
564 This is the socket owner UID.
566 .I idiag_inode
567 This is the socket inode number.
569 .SS Socket memory information
570 The payload associated with
571 .B UNIX_DIAG_MEMINFO
573 .B INET_DIAG_SKMEMINFO
574 netlink attributes is an array of the following __u32 values:
576 .B SK_MEMINFO_RMEM_ALLOC
577 The amount of data in receive queue.
579 .B SK_MEMINFO_RCVBUF
580 The receive socket buffer as set by
581 .BR SO_RCVBUF .
583 .B SK_MEMINFO_WMEM_ALLOC
584 The amount of data in send queue.
586 .B SK_MEMINFO_SNDBUF
587 The send socket buffer as set by
588 .BR SO_SNDBUF .
590 .B SK_MEMINFO_FWD_ALLOC
591 The amount of memory scheduled for future use (TCP only).
593 .B SK_MEMINFO_WMEM_QUEUED
594 The amount of data queued by TCP, but not yet sent.
596 .B SK_MEMINFO_OPTMEM
597 The amount of memory allocated for the socket's service needs (e.g., socket
598 filter).
600 .B SK_MEMINFO_BACKLOG
601 The amount of packets in the backlog (not yet processed).
602 .SH VERSIONS
603 .B NETLINK_INET_DIAG
604 was introduced in Linux 2.6.14 and supported
605 .B AF_INET
607 .B AF_INET6
608 sockets only.
609 In Linux 3.3, it was renamed to
610 .B NETLINK_SOCK_DIAG
611 and extended to support
612 .B AF_UNIX
613 sockets.
615 .B UNIX_DIAG_MEMINFO
617 .B INET_DIAG_SKMEMINFO
618 were introduced in Linux 3.6.
619 .SH STANDARDS
620 Linux.
621 .SH EXAMPLES
622 The following example program prints inode number, peer's inode number,
623 and name of all UNIX domain sockets in the current namespace.
626 #include <errno.h>
627 #include <stdio.h>
628 #include <string.h>
629 #include <unistd.h>
630 #include <sys/socket.h>
631 #include <sys/un.h>
632 #include <linux/netlink.h>
633 #include <linux/rtnetlink.h>
634 #include <linux/sock_diag.h>
635 #include <linux/unix_diag.h>
637 static int
638 send_query(int fd)
640     struct sockaddr_nl nladdr = {
641         .nl_family = AF_NETLINK
642     };
643     struct
644     {
645         struct nlmsghdr nlh;
646         struct unix_diag_req udr;
647     } req = {
648         .nlh = {
649             .nlmsg_len = sizeof(req),
650             .nlmsg_type = SOCK_DIAG_BY_FAMILY,
651             .nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP
652         },
653         .udr = {
654             .sdiag_family = AF_UNIX,
655             .udiag_states = \-1,
656             .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER
657         }
658     };
659     struct iovec iov = {
660         .iov_base = &req,
661         .iov_len = sizeof(req)
662     };
663     struct msghdr msg = {
664         .msg_name = &nladdr,
665         .msg_namelen = sizeof(nladdr),
666         .msg_iov = &iov,
667         .msg_iovlen = 1
668     };
670     for (;;) {
671         if (sendmsg(fd, &msg, 0) < 0) {
672             if (errno == EINTR)
673                 continue;
675             perror("sendmsg");
676             return \-1;
677         }
679         return 0;
680     }
683 static int
684 print_diag(const struct unix_diag_msg *diag, unsigned int len)
686     if (len < NLMSG_LENGTH(sizeof(*diag))) {
687         fputs("short response\[rs]n", stderr);
688         return \-1;
689     }
690     if (diag\->udiag_family != AF_UNIX) {
691         fprintf(stderr, "unexpected family %u\[rs]n", diag\->udiag_family);
692         return \-1;
693     }
695     unsigned int rta_len = len \- NLMSG_LENGTH(sizeof(*diag));
696     unsigned int peer = 0;
697     size_t path_len = 0;
698     char path[sizeof(((struct sockaddr_un *) 0)\->sun_path) + 1];
700     for (struct rtattr *attr = (struct rtattr *) (diag + 1);
701              RTA_OK(attr, rta_len); attr = RTA_NEXT(attr, rta_len)) {
702         switch (attr\->rta_type) {
703         case UNIX_DIAG_NAME:
704             if (!path_len) {
705                 path_len = RTA_PAYLOAD(attr);
706                 if (path_len > sizeof(path) \- 1)
707                     path_len = sizeof(path) \- 1;
708                 memcpy(path, RTA_DATA(attr), path_len);
709                 path[path_len] = \[aq]\[rs]0\[aq];
710             }
711             break;
713         case UNIX_DIAG_PEER:
714             if (RTA_PAYLOAD(attr) >= sizeof(peer))
715                 peer = *(unsigned int *) RTA_DATA(attr);
716             break;
717         }
718     }
720     printf("inode=%u", diag\->udiag_ino);
722     if (peer)
723         printf(", peer=%u", peer);
725     if (path_len)
726         printf(", name=%s%s", *path ? "" : "@",
727                 *path ? path : path + 1);
729     putchar(\[aq]\[rs]n\[aq]);
730     return 0;
733 static int
734 receive_responses(int fd)
736     long buf[8192 / sizeof(long)];
737     struct sockaddr_nl nladdr;
738     struct iovec iov = {
739         .iov_base = buf,
740         .iov_len = sizeof(buf)
741     };
742     int flags = 0;
744     for (;;) {
745         struct msghdr msg = {
746             .msg_name = &nladdr,
747             .msg_namelen = sizeof(nladdr),
748             .msg_iov = &iov,
749             .msg_iovlen = 1
750         };
752         ssize_t ret = recvmsg(fd, &msg, flags);
754         if (ret < 0) {
755             if (errno == EINTR)
756                 continue;
758             perror("recvmsg");
759             return \-1;
760         }
761         if (ret == 0)
762             return 0;
764         if (nladdr.nl_family != AF_NETLINK) {
765             fputs("!AF_NETLINK\[rs]n", stderr);
766             return \-1;
767         }
769         const struct nlmsghdr *h = (struct nlmsghdr *) buf;
771         if (!NLMSG_OK(h, ret)) {
772             fputs("!NLMSG_OK\[rs]n", stderr);
773             return \-1;
774         }
776         for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) {
777             if (h\->nlmsg_type == NLMSG_DONE)
778                 return 0;
780             if (h\->nlmsg_type == NLMSG_ERROR) {
781                 const struct nlmsgerr *err = NLMSG_DATA(h);
783                 if (h\->nlmsg_len < NLMSG_LENGTH(sizeof(*err))) {
784                     fputs("NLMSG_ERROR\[rs]n", stderr);
785                 } else {
786                     errno = \-err\->error;
787                     perror("NLMSG_ERROR");
788                 }
790                 return \-1;
791             }
793             if (h\->nlmsg_type != SOCK_DIAG_BY_FAMILY) {
794                 fprintf(stderr, "unexpected nlmsg_type %u\[rs]n",
795                         (unsigned) h\->nlmsg_type);
796                 return \-1;
797             }
799             if (print_diag(NLMSG_DATA(h), h\->nlmsg_len))
800                 return \-1;
801         }
802     }
806 main(void)
808     int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
810     if (fd < 0) {
811         perror("socket");
812         return 1;
813     }
815     int ret = send_query(fd) || receive_responses(fd);
817     close(fd);
818     return ret;
821 .SH SEE ALSO
822 .BR netlink (3),
823 .BR rtnetlink (3),
824 .BR netlink (7),
825 .BR tcp (7)