2 * Copyright (c) 2014 Zubin Mithra <zubin.mithra@gmail.com>
3 * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
4 * Copyright (c) 2014-2017 The strace developers.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <netinet/in.h>
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
34 #include <linux/netlink.h>
35 #include <linux/sock_diag.h>
36 #include <linux/inet_diag.h>
37 #include <linux/unix_diag.h>
38 #include <linux/netlink_diag.h>
39 #include <linux/rtnetlink.h>
43 # define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) 0)->sun_path)
46 #ifndef NETLINK_SOCK_DIAG
47 # define NETLINK_SOCK_DIAG 4
55 #define CACHE_SIZE 1024U
56 static cache_entry cache[CACHE_SIZE];
57 #define CACHE_MASK (CACHE_SIZE - 1)
60 cache_and_print_inode_details(const unsigned long inode, char *const details)
62 cache_entry *e = &cache[inode & CACHE_MASK];
72 print_sockaddr_by_inode_cached(const unsigned long inode)
74 const cache_entry *const e = &cache[inode & CACHE_MASK];
75 if (e && inode == e->inode) {
83 send_query(const int fd, void *req, size_t req_size)
85 struct sockaddr_nl nladdr = {
86 .nl_family = AF_NETLINK
92 const struct msghdr msg = {
94 .msg_namelen = sizeof(nladdr),
100 if (sendmsg(fd, &msg, 0) < 0) {
110 inet_send_query(const int fd, const int family, const int proto)
113 const struct nlmsghdr nlh;
114 const struct inet_diag_req_v2 idr;
117 .nlmsg_len = sizeof(req),
118 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
119 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
122 .sdiag_family = family,
123 .sdiag_protocol = proto,
127 return send_query(fd, &req, sizeof(req));
131 inet_parse_response(const char *const proto_name, const void *const data,
132 const int data_len, const unsigned long inode)
134 const struct inet_diag_msg *const diag_msg = data;
135 static const char zero_addr[sizeof(struct in6_addr)];
136 socklen_t addr_size, text_size;
138 if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
140 if (diag_msg->idiag_inode != inode)
143 switch(diag_msg->idiag_family) {
145 addr_size = sizeof(struct in_addr);
146 text_size = INET_ADDRSTRLEN;
149 addr_size = sizeof(struct in6_addr);
150 text_size = INET6_ADDRSTRLEN;
156 char src_buf[text_size];
159 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_src,
163 if (diag_msg->id.idiag_dport ||
164 memcmp(zero_addr, diag_msg->id.idiag_dst, addr_size)) {
165 char dst_buf[text_size];
167 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_dst,
171 if (asprintf(&details, "%s:[%s:%u->%s:%u]", proto_name,
172 src_buf, ntohs(diag_msg->id.idiag_sport),
173 dst_buf, ntohs(diag_msg->id.idiag_dport)) < 0)
176 if (asprintf(&details, "%s:[%s:%u]", proto_name, src_buf,
177 ntohs(diag_msg->id.idiag_sport)) < 0)
181 return cache_and_print_inode_details(inode, details);
185 receive_responses(const int fd, const unsigned long inode,
186 const char *proto_name,
187 int (* parser) (const char *, const void *,
192 long buf[8192 / sizeof(long)];
195 struct sockaddr_nl nladdr = {
196 .nl_family = AF_NETLINK
199 .iov_base = hdr_buf.buf,
200 .iov_len = sizeof(hdr_buf.buf)
205 struct msghdr msg = {
207 .msg_namelen = sizeof(nladdr),
212 ssize_t ret = recvmsg(fd, &msg, flags);
219 const struct nlmsghdr *h = &hdr_buf.hdr;
220 if (!NLMSG_OK(h, ret))
222 for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) {
223 if (h->nlmsg_type != SOCK_DIAG_BY_FAMILY)
225 const int rc = parser(proto_name, NLMSG_DATA(h),
226 h->nlmsg_len, inode);
232 flags = MSG_DONTWAIT;
237 inet_print(const int fd, const int family, const int protocol,
238 const unsigned long inode, const char *proto_name)
240 return inet_send_query(fd, family, protocol)
241 && receive_responses(fd, inode, proto_name, inet_parse_response);
245 unix_send_query(const int fd, const unsigned long inode)
248 const struct nlmsghdr nlh;
249 const struct unix_diag_req udr;
252 .nlmsg_len = sizeof(req),
253 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
254 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
257 .sdiag_family = AF_UNIX,
260 .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER
263 return send_query(fd, &req, sizeof(req));
267 unix_parse_response(const char *proto_name, const void *data,
268 const int data_len, const unsigned long inode)
270 const struct unix_diag_msg *diag_msg = data;
272 int rta_len = data_len - NLMSG_LENGTH(sizeof(*diag_msg));
275 char path[UNIX_PATH_MAX + 1];
279 if (diag_msg->udiag_ino != inode)
281 if (diag_msg->udiag_family != AF_UNIX)
284 for (attr = (struct rtattr *) (diag_msg + 1);
285 RTA_OK(attr, rta_len);
286 attr = RTA_NEXT(attr, rta_len)) {
287 switch (attr->rta_type) {
290 path_len = RTA_PAYLOAD(attr);
291 if (path_len > UNIX_PATH_MAX)
292 path_len = UNIX_PATH_MAX;
293 memcpy(path, RTA_DATA(attr), path_len);
294 path[path_len] = '\0';
298 if (RTA_PAYLOAD(attr) >= 4)
299 peer = *(uint32_t *) RTA_DATA(attr);
305 * print obtained information in the following format:
306 * "UNIX:[" SELF_INODE [ "->" PEER_INODE ][ "," SOCKET_FILE ] "]"
308 if (!peer && !path_len)
311 char peer_str[3 + sizeof(peer) * 3];
313 snprintf(peer_str, sizeof(peer_str), "->%u", peer);
317 const char *path_str;
319 char *outstr = alloca(4 * path_len + 4);
322 if (path[0] == '\0') {
324 string_quote(path + 1, outstr + 2,
325 path_len - 1, QUOTE_0_TERMINATED);
327 string_quote(path, outstr + 1,
328 path_len, QUOTE_0_TERMINATED);
336 if (asprintf(&details, "%s:[%lu%s%s]", proto_name, inode,
337 peer_str, path_str) < 0)
340 return cache_and_print_inode_details(inode, details);
344 netlink_send_query(const int fd, const unsigned long inode)
347 const struct nlmsghdr nlh;
348 const struct netlink_diag_req ndr;
351 .nlmsg_len = sizeof(req),
352 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
353 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
356 .sdiag_family = AF_NETLINK,
357 .sdiag_protocol = NDIAG_PROTO_ALL,
358 .ndiag_show = NDIAG_SHOW_MEMINFO
361 return send_query(fd, &req, sizeof(req));
365 netlink_parse_response(const char *proto_name, const void *data,
366 const int data_len, const unsigned long inode)
368 const struct netlink_diag_msg *const diag_msg = data;
369 const char *netlink_proto;
372 if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
374 if (diag_msg->ndiag_ino != inode)
377 if (diag_msg->ndiag_family != AF_NETLINK)
380 netlink_proto = xlookup(netlink_protocols,
381 diag_msg->ndiag_protocol);
384 netlink_proto = STR_STRIP_PREFIX(netlink_proto, "NETLINK_");
385 if (asprintf(&details, "%s:[%s:%u]", proto_name,
386 netlink_proto, diag_msg->ndiag_portid) < 0)
389 if (asprintf(&details, "%s:[%u]", proto_name,
390 (unsigned) diag_msg->ndiag_protocol) < 0)
394 return cache_and_print_inode_details(inode, details);
398 unix_print(const int fd, const unsigned long inode)
400 return unix_send_query(fd, inode)
401 && receive_responses(fd, inode, "UNIX", unix_parse_response);
405 tcp_v4_print(const int fd, const unsigned long inode)
407 return inet_print(fd, AF_INET, IPPROTO_TCP, inode, "TCP");
411 udp_v4_print(const int fd, const unsigned long inode)
413 return inet_print(fd, AF_INET, IPPROTO_UDP, inode, "UDP");
417 tcp_v6_print(const int fd, const unsigned long inode)
419 return inet_print(fd, AF_INET6, IPPROTO_TCP, inode, "TCPv6");
423 udp_v6_print(const int fd, const unsigned long inode)
425 return inet_print(fd, AF_INET6, IPPROTO_UDP, inode, "UDPv6");
429 netlink_print(const int fd, const unsigned long inode)
431 return netlink_send_query(fd, inode)
432 && receive_responses(fd, inode, "NETLINK",
433 netlink_parse_response);
436 static const struct {
437 const char *const name;
438 bool (*const print)(int, unsigned long);
440 [SOCK_PROTO_UNIX] = { "UNIX", unix_print },
441 [SOCK_PROTO_TCP] = { "TCP", tcp_v4_print },
442 [SOCK_PROTO_UDP] = { "UDP", udp_v4_print },
443 [SOCK_PROTO_TCPv6] = { "TCPv6", tcp_v6_print },
444 [SOCK_PROTO_UDPv6] = { "UDPv6", udp_v6_print },
445 [SOCK_PROTO_NETLINK] = { "NETLINK", netlink_print }
449 get_proto_by_name(const char *const name)
452 for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
453 i < ARRAY_SIZE(protocols); ++i) {
454 if (protocols[i].name && !strcmp(name, protocols[i].name))
455 return (enum sock_proto) i;
457 return SOCK_PROTO_UNKNOWN;
461 print_sockaddr_by_inode_uncached(const unsigned long inode,
462 const enum sock_proto proto)
464 if ((unsigned int) proto >= ARRAY_SIZE(protocols) ||
465 (proto != SOCK_PROTO_UNKNOWN && !protocols[proto].print))
468 const int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
473 if (proto != SOCK_PROTO_UNKNOWN) {
474 r = protocols[proto].print(fd, inode);
476 tprintf("%s:[%lu]", protocols[proto].name, inode);
481 for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
482 i < ARRAY_SIZE(protocols); ++i) {
483 if (!protocols[i].print)
485 r = protocols[i].print(fd, inode);
495 /* Given an inode number of a socket, print out its protocol details. */
497 print_sockaddr_by_inode(struct tcb *const tcp, const int fd,
498 const unsigned long inode)
500 return print_sockaddr_by_inode_cached(inode) ? true :
501 print_sockaddr_by_inode_uncached(inode, getfdproto(tcp, fd));