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