malloc.3: Clarify that realloc() may move the memory block
[man-pages.git] / man7 / sock_diag.7
blobcccd732647e2de7b69e29a7761c5deba8f742712
1 .\" Copyright (c) 2016 Pavel Emelyanov <xemul@virtuozzo.com>
2 .\" Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
3 .\"
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.
9 .\"
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.
14 .\"
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.
19 .\"
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/>.
23 .\" %%%LICENSE_END
24 .TH SOCK_DIAG 7 2021-03-22 "Linux" "Linux Programmer's Manual"
25 .SH NAME
26 sock_diag \- obtaining information about sockets
27 .SH SYNOPSIS
28 .nf
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 */"
33 .PP
34 .BI "diag_socket = socket(AF_NETLINK, " socket_type ", NETLINK_SOCK_DIAG);"
35 .fi
36 .SH DESCRIPTION
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.
41 .PP
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.
45 .PP
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.)
50 .PP
51 Note that sock_diag reports only those sockets that have a name;
52 that is, either sockets bound explicitly with
53 .BR bind (2)
54 or sockets that were automatically bound to an address (e.g., by
55 .BR connect (2)).
56 This is the same set of sockets that is available via
57 .IR /proc/net/unix ,
58 .IR /proc/net/tcp ,
59 .IR /proc/net/udp ,
60 and so on.
61 .\"
62 .SS Request
63 The request starts with a
64 .I "struct nlmsghdr"
65 header described in
66 .BR netlink (7)
67 with
68 .I nlmsg_type
69 field set to
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:
73 .PP
74 .in +4n
75 .EX
76 struct sock_diag_req {
77     __u8 sdiag_family;
78     __u8 sdiag_protocol;
80 .EE
81 .in
82 .PP
83 The fields of this structure are as follows:
84 .TP
85 .I sdiag_family
86 An address family.
87 It should be set to the appropriate
88 .B AF_*
89 constant.
90 .TP
91 .I sdiag_protocol
92 Depends on
93 .IR sdiag_family .
94 It should be set to the appropriate
95 .B IPPROTO_*
96 constant for
97 .B AF_INET
98 and
99 .BR AF_INET6 ,
100 and to 0 otherwise.
102 If the
103 .I nlmsg_flags
104 field of the
105 .I "struct nlmsghdr"
106 header has the
107 .BR NLM_F_DUMP
108 flag set, it means that a list of sockets is being requested;
109 otherwise it is a query about an individual socket.
111 .SS Response
112 The response starts with a
113 .I "struct nlmsghdr"
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
116 .B NLMSG_*
117 macros from the
118 .BR netlink (3)
119 API.
121 Each object is the NLA (netlink attributes) list that is to be accessed
122 with the
123 .B RTA_*
124 macros from
125 .BR rtnetlink (3)
126 API.
128 .SS UNIX domain sockets
129 For UNIX domain sockets the request is represented in the following structure:
131 .in +4n
133 struct unix_diag_req {
134     __u8    sdiag_family;
135     __u8    sdiag_protocol;
136     __u16   pad;
137     __u32   udiag_states;
138     __u32   udiag_ino;
139     __u32   udiag_show;
140     __u32   udiag_cookie[2];
145 The fields of this structure are as follows:
147 .I sdiag_family
148 The address family; it should be set to
149 .BR AF_UNIX .
151 .I sdiag_protocol
152 .PD 0
155 .I pad
156 These fields should be set to 0.
158 .I udiag_states
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:
164 .RS 12
165 1 <<
166 .B TCP_ESTABLISHED
168 1 <<
169 .B TCP_LISTEN
172 .I udiag_ino
173 This is an inode number when querying for an individual socket.
174 Ignored when querying for a list of sockets.
176 .I udiag_show
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:
182 .B UDIAG_SHOW_NAME
183 The attribute reported in answer to this request is
184 .BR UNIX_DIAG_NAME .
185 The payload associated with this attribute is the pathname to which
186 the socket was bound (a sequence of bytes up to
187 .B UNIX_PATH_MAX
188 length).
190 .B UDIAG_SHOW_VFS
191 The attribute reported in answer to this request is
192 .BR UNIX_DIAG_VFS .
193 The payload associated with this attribute is represented in the following
194 structure:
196 .in +4n
198 struct unix_diag_vfs {
199     __u32 udiag_vfs_dev;
200     __u32 udiag_vfs_ino;
205 The fields of this structure are as follows:
208 .I udiag_vfs_dev
209 The device number of the corresponding on-disk socket inode.
211 .I udiag_vfs_ino
212 The inode number of the corresponding on-disk socket inode.
215 .B UDIAG_SHOW_PEER
216 The attribute reported in answer to this request is
217 .BR UNIX_DIAG_PEER .
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.
222 .B UDIAG_SHOW_ICONS
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
227 .BR connect (2)
228 call, but hasn't been processed with
229 .BR accept (2)
230 yet.
231 This attribute is reported for listening sockets only.
233 .B UDIAG_SHOW_RQLEN
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
237 structure:
239 .in +4n
241 struct unix_diag_rqlen {
242     __u32 udiag_rqueue;
243     __u32 udiag_wqueue;
248 The fields of this structure are as follows:
251 .I udiag_rqueue
252 For listening sockets:
253 the number of pending connections.
254 The length of the array associated with the
255 .B UNIX_DIAG_ICONS
256 response attribute is equal to this value.
258 For established sockets:
259 the amount of data in incoming queue.
261 .I udiag_wqueue
262 For listening sockets:
263 the backlog length which equals to the value passed as the second argument to
264 .BR listen (2).
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
280 bits of
281 .BR shutdown (2)
282 state.
285 .I udiag_cookie
286 This is an array of opaque identifiers that could be used along with
287 .I udiag_ino
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
294 .in +4n
296 struct unix_diag_msg {
297     __u8    udiag_family;
298     __u8    udiag_type;
299     __u8    udiag_state;
300     __u8    pad;
301     __u32   udiag_ino;
302     __u32   udiag_cookie[2];
307 followed by netlink attributes.
309 The fields of this structure are as follows:
311 .I udiag_family
312 This field has the same meaning as in
313 .IR "struct unix_diag_req" .
315 .I udiag_type
316 This is set to one of
317 .BR SOCK_PACKET ,
318 .BR SOCK_STREAM ,
320 .BR SOCK_SEQPACKET .
322 .I udiag_state
323 This is set to one of
324 .BR TCP_LISTEN
326 .BR TCP_ESTABLISHED .
328 .I pad
329 This field is set to 0.
331 .I udiag_ino
332 This is the socket inode number.
334 .I udiag_cookie
335 This is an array of opaque identifiers that could be used in subsequent
336 queries.
338 .SS IPv4 and IPv6 sockets
339 For IPv4 and IPv6 sockets,
340 the request is represented in the following structure:
342 .in +4n
344 struct inet_diag_req_v2 {
345     __u8    sdiag_family;
346     __u8    sdiag_protocol;
347     __u8    idiag_ext;
348     __u8    pad;
349     __u32   idiag_states;
350     struct inet_diag_sockid id;
355 where
356 .I "struct inet_diag_sockid"
357 is defined as follows:
359 .in +4n
361 struct inet_diag_sockid {
362     __be16  idiag_sport;
363     __be16  idiag_dport;
364     __be32  idiag_src[4];
365     __be32  idiag_dst[4];
366     __u32   idiag_if;
367     __u32   idiag_cookie[2];
372 The fields of
373 .I "struct inet_diag_req_v2"
374 are as follows:
376 .I sdiag_family
377 This should be set to either
378 .B AF_INET
380 .B AF_INET6
381 for IPv4 or IPv6 sockets respectively.
383 .I sdiag_protocol
384 This should be set to one of
385 .BR IPPROTO_TCP ,
386 .BR IPPROTO_UDP ,
388 .BR IPPROTO_UDPLITE .
390 .I idiag_ext
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
393 as described below:
396 .B INET_DIAG_TOS
397 The payload associated with this attribute is a __u8 value
398 which is the TOS of the socket.
400 .B INET_DIAG_TCLASS
401 The payload associated with this attribute is a __u8 value
402 which is the TClass of the socket.
403 IPv6 sockets only.
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
407 is IPv6-only or not.
409 .B INET_DIAG_MEMINFO
410 The payload associated with this attribute is represented in the following
411 structure:
413 .in +4n
415 struct inet_diag_meminfo {
416     __u32 idiag_rmem;
417     __u32 idiag_wmem;
418     __u32 idiag_fmem;
419     __u32 idiag_tmem;
424 The fields of this structure are as follows:
426 .TP 12
427 .I idiag_rmem
428 The amount of data in the receive queue.
430 .I idiag_wmem
431 The amount of data that is queued by TCP but not yet sent.
433 .I idiag_fmem
434 The amount of memory scheduled for future use (TCP only).
436 .I idiag_tmem
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".
444 .B INET_DIAG_INFO
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" .
449 .B INET_DIAG_CONG
450 The payload associated with this attribute is a string that describes the
451 congestion control algorithm used.
452 For TCP sockets only.
455 .I pad
456 This should be set to 0.
458 .I idiag_states
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.
463 .I id
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.
470 The fields of
471 .I "struct inet_diag_sockid"
472 are as follows:
474 .I idiag_sport
475 The source port.
477 .I idiag_dport
478 The destination port.
480 .I idiag_src
481 The source address.
483 .I idiag_dst
484 The destination address.
486 .I idiag_if
487 The interface number the socket is bound to.
489 .I idiag_cookie
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
497 .in +4n
499 struct inet_diag_msg {
500     __u8    idiag_family;
501     __u8    idiag_state;
502     __u8    idiag_timer;
503     __u8    idiag_retrans;
505     struct inet_diag_sockid id;
507     __u32   idiag_expires;
508     __u32   idiag_rqueue;
509     __u32   idiag_wqueue;
510     __u32   idiag_uid;
511     __u32   idiag_inode;
516 followed by netlink attributes.
518 The fields of this structure are as follows:
520 .I idiag_family
521 This is the same field as in
522 .IR "struct inet_diag_req_v2" .
524 .I idiag_state
525 This denotes socket state as in
526 .IR "struct inet_diag_req_v2" .
528 .I idiag_timer
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:
533 .PD 0
534 .RS 12
536 .B 0
537 no timer is active
539 .B 1
540 a retransmit timer
542 .B 2
543 a keep-alive timer
545 .B 3
546 a TIME_WAIT timer
548 .B 4
549 a zero window probe timer
553 For non-TCP sockets, this field is set to 0.
555 .I idiag_retrans
557 .I idiag_timer
558 values 1, 2, and 4, this field contains the number of retransmits.
559 For other
560 .I idiag_timer
561 values, this field is set to 0.
563 .I idiag_expires
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.
568 .I idiag_rqueue
569 For listening sockets:
570 the number of pending connections.
572 For other sockets:
573 the amount of data in the incoming queue.
575 .I idiag_wqueue
576 For listening sockets:
577 the backlog length.
579 For other sockets:
580 the amount of memory available for sending.
582 .I idiag_uid
583 This is the socket owner UID.
585 .I idiag_inode
586 This is the socket inode number.
588 .SS Socket memory information
589 The payload associated with
590 .B UNIX_DIAG_MEMINFO
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.
598 .B SK_MEMINFO_RCVBUF
599 The receive socket buffer as set by
600 .BR SO_RCVBUF .
602 .B SK_MEMINFO_WMEM_ALLOC
603 The amount of data in send queue.
605 .B SK_MEMINFO_SNDBUF
606 The send socket buffer as set by
607 .BR SO_SNDBUF .
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.
615 .B SK_MEMINFO_OPTMEM
616 The amount of memory allocated for the socket's service needs (e.g., socket
617 filter).
619 .B SK_MEMINFO_BACKLOG
620 The amount of packets in the backlog (not yet processed).
621 .SH VERSIONS
622 .B NETLINK_INET_DIAG
623 was introduced in Linux 2.6.14 and supported
624 .B AF_INET
626 .B AF_INET6
627 sockets only.
628 In Linux 3.3, it was renamed to
629 .B NETLINK_SOCK_DIAG
630 and extended to support
631 .B AF_UNIX
632 sockets.
634 .B UNIX_DIAG_MEMINFO
636 .BR INET_DIAG_SKMEMINFO
637 were introduced in Linux 3.6.
638 .SH CONFORMING TO
639 The NETLINK_SOCK_DIAG API is Linux-specific.
640 .SH EXAMPLES
641 The following example program prints inode number, peer's inode number,
642 and name of all UNIX domain sockets in the current namespace.
645 #include <errno.h>
646 #include <stdio.h>
647 #include <string.h>
648 #include <unistd.h>
649 #include <sys/socket.h>
650 #include <sys/un.h>
651 #include <linux/netlink.h>
652 #include <linux/rtnetlink.h>
653 #include <linux/sock_diag.h>
654 #include <linux/unix_diag.h>
656 static int
657 send_query(int fd)
659     struct sockaddr_nl nladdr = {
660         .nl_family = AF_NETLINK
661     };
662     struct
663     {
664         struct nlmsghdr nlh;
665         struct unix_diag_req udr;
666     } req = {
667         .nlh = {
668             .nlmsg_len = sizeof(req),
669             .nlmsg_type = SOCK_DIAG_BY_FAMILY,
670             .nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP
671         },
672         .udr = {
673             .sdiag_family = AF_UNIX,
674             .udiag_states = \-1,
675             .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER
676         }
677     };
678     struct iovec iov = {
679         .iov_base = &req,
680         .iov_len = sizeof(req)
681     };
682     struct msghdr msg = {
683         .msg_name = &nladdr,
684         .msg_namelen = sizeof(nladdr),
685         .msg_iov = &iov,
686         .msg_iovlen = 1
687     };
689     for (;;) {
690         if (sendmsg(fd, &msg, 0) < 0) {
691             if (errno == EINTR)
692                 continue;
694             perror("sendmsg");
695             return \-1;
696         }
698         return 0;
699     }
702 static int
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);
707         return \-1;
708     }
709     if (diag\->udiag_family != AF_UNIX) {
710         fprintf(stderr, "unexpected family %u\en", diag\->udiag_family);
711         return \-1;
712     }
714     unsigned int rta_len = len \- NLMSG_LENGTH(sizeof(*diag));
715     unsigned int peer = 0;
716     size_t path_len = 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) {
722         case UNIX_DIAG_NAME:
723             if (!path_len) {
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;
729             }
730             break;
732         case UNIX_DIAG_PEER:
733             if (RTA_PAYLOAD(attr) >= sizeof(peer))
734                 peer = *(unsigned int *) RTA_DATA(attr);
735             break;
736         }
737     }
739     printf("inode=%u", diag\->udiag_ino);
741     if (peer)
742         printf(", peer=%u", peer);
744     if (path_len)
745         printf(", name=%s%s", *path ? "" : "@",
746                 *path ? path : path + 1);
748     putchar(\(aq\en\(aq);
749     return 0;
752 static int
753 receive_responses(int fd)
755     long buf[8192 / sizeof(long)];
756     struct sockaddr_nl nladdr;
757     struct iovec iov = {
758         .iov_base = buf,
759         .iov_len = sizeof(buf)
760     };
761     int flags = 0;
763     for (;;) {
764         struct msghdr msg = {
765             .msg_name = &nladdr,
766             .msg_namelen = sizeof(nladdr),
767             .msg_iov = &iov,
768             .msg_iovlen = 1
769         };
771         ssize_t ret = recvmsg(fd, &msg, flags);
773         if (ret < 0) {
774             if (errno == EINTR)
775                 continue;
777             perror("recvmsg");
778             return \-1;
779         }
780         if (ret == 0)
781             return 0;
783         if (nladdr.nl_family != AF_NETLINK) {
784             fputs("!AF_NETLINK\en", stderr);
785             return \-1;
786         }
788         const struct nlmsghdr *h = (struct nlmsghdr *) buf;
790         if (!NLMSG_OK(h, ret)) {
791             fputs("!NLMSG_OK\en", stderr);
792             return \-1;
793         }
795         for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) {
796             if (h\->nlmsg_type == NLMSG_DONE)
797                 return 0;
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);
804                 } else {
805                     errno = \-err\->error;
806                     perror("NLMSG_ERROR");
807                 }
809                 return \-1;
810             }
812             if (h\->nlmsg_type != SOCK_DIAG_BY_FAMILY) {
813                 fprintf(stderr, "unexpected nlmsg_type %u\en",
814                         (unsigned) h\->nlmsg_type);
815                 return \-1;
816             }
818             if (print_diag(NLMSG_DATA(h), h\->nlmsg_len))
819                 return \-1;
820         }
821     }
825 main(void)
827     int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
829     if (fd < 0) {
830         perror("socket");
831         return 1;
832     }
834     int ret = send_query(fd) || receive_responses(fd);
836     close(fd);
837     return ret;
840 .SH SEE ALSO
841 .BR netlink (3),
842 .BR rtnetlink (3),
843 .BR netlink (7),
844 .BR tcp (7)