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-2018 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>
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>
40 #if HAVE_LINUX_GENETLINK_H
41 #include <linux/genetlink.h>
46 # define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) 0)->sun_path)
56 #define CACHE_SIZE 1024U
57 static cache_entry cache[CACHE_SIZE];
58 #define CACHE_MASK (CACHE_SIZE - 1)
61 cache_inode_details(const unsigned long inode, char *const details)
63 cache_entry *e = &cache[inode & CACHE_MASK];
72 get_sockaddr_by_inode_cached(const unsigned long inode)
74 const cache_entry *const e = &cache[inode & CACHE_MASK];
75 return (e && inode == e->inode) ? e->details : NULL;
79 print_sockaddr_by_inode_cached(const unsigned long inode)
81 const char *const details = get_sockaddr_by_inode_cached(inode);
90 send_query(struct tcb *tcp, const int fd, void *req, size_t req_size)
92 struct sockaddr_nl nladdr = {
93 .nl_family = AF_NETLINK
99 const struct msghdr msg = {
101 .msg_namelen = sizeof(nladdr),
107 if (sendmsg(fd, &msg, 0) < 0) {
117 inet_send_query(struct tcb *tcp, const int fd, const int family,
121 const struct nlmsghdr nlh;
122 const struct inet_diag_req_v2 idr;
125 .nlmsg_len = sizeof(req),
126 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
127 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
130 .sdiag_family = family,
131 .sdiag_protocol = proto,
135 return send_query(tcp, fd, &req, sizeof(req));
139 inet_parse_response(const void *const data, const int data_len,
140 const unsigned long inode, void *opaque_data)
142 const char *const proto_name = opaque_data;
143 const struct inet_diag_msg *const diag_msg = data;
144 static const char zero_addr[sizeof(struct in6_addr)];
145 socklen_t addr_size, text_size;
147 if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
149 if (diag_msg->idiag_inode != inode)
152 switch (diag_msg->idiag_family) {
154 addr_size = sizeof(struct in_addr);
155 text_size = INET_ADDRSTRLEN;
158 addr_size = sizeof(struct in6_addr);
159 text_size = INET6_ADDRSTRLEN;
165 char src_buf[text_size];
168 /* open/closing brackets for IPv6 addresses */
169 const char *ob = diag_msg->idiag_family == AF_INET6 ? "[" : "";
170 const char *cb = diag_msg->idiag_family == AF_INET6 ? "]" : "";
172 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_src,
176 if (diag_msg->id.idiag_dport ||
177 memcmp(zero_addr, diag_msg->id.idiag_dst, addr_size)) {
178 char dst_buf[text_size];
180 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_dst,
184 if (asprintf(&details, "%s:[%s%s%s:%u->%s%s%s:%u]", proto_name,
185 ob, src_buf, cb, ntohs(diag_msg->id.idiag_sport),
186 ob, dst_buf, cb, ntohs(diag_msg->id.idiag_dport))
190 if (asprintf(&details, "%s:[%s%s%s:%u]",
191 proto_name, ob, src_buf, cb,
192 ntohs(diag_msg->id.idiag_sport)) < 0)
196 return cache_inode_details(inode, details);
200 receive_responses(struct tcb *tcp, const int fd, const unsigned long inode,
201 const unsigned long expected_msg_type,
202 int (*parser)(const void *, int,
203 unsigned long, void *),
208 long buf[8192 / sizeof(long)];
211 struct sockaddr_nl nladdr = {
212 .nl_family = AF_NETLINK
215 .iov_base = hdr_buf.buf,
216 .iov_len = sizeof(hdr_buf.buf)
221 struct msghdr msg = {
223 .msg_namelen = sizeof(nladdr),
228 ssize_t ret = recvmsg(fd, &msg, flags);
235 const struct nlmsghdr *h = &hdr_buf.hdr;
236 if (!NLMSG_OK(h, ret))
238 for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) {
239 if (h->nlmsg_type != expected_msg_type)
241 const int rc = parser(NLMSG_DATA(h),
242 h->nlmsg_len, inode, opaque_data);
248 flags = MSG_DONTWAIT;
253 unix_send_query(struct tcb *tcp, const int fd, const unsigned long inode)
256 * The kernel bug was fixed in mainline by commit v4.5-rc6~35^2~11
257 * and backported to stable/linux-4.4.y by commit v4.4.4~297.
259 const uint16_t dump_flag =
260 os_release < KERNEL_VERSION(4, 4, 4) ? NLM_F_DUMP : 0;
263 const struct nlmsghdr nlh;
264 const struct unix_diag_req udr;
267 .nlmsg_len = sizeof(req),
268 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
269 .nlmsg_flags = NLM_F_REQUEST | dump_flag
272 .sdiag_family = AF_UNIX,
275 .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER,
276 .udiag_cookie = { ~0U, ~0U }
279 return send_query(tcp, fd, &req, sizeof(req));
283 unix_parse_response(const void *data, const int data_len,
284 const unsigned long inode, void *opaque_data)
286 const char *proto_name = opaque_data;
287 const struct unix_diag_msg *diag_msg = data;
289 int rta_len = data_len - NLMSG_LENGTH(sizeof(*diag_msg));
292 char path[UNIX_PATH_MAX + 1];
296 if (diag_msg->udiag_ino != inode)
298 if (diag_msg->udiag_family != AF_UNIX)
301 for (attr = (struct rtattr *) (diag_msg + 1);
302 RTA_OK(attr, rta_len);
303 attr = RTA_NEXT(attr, rta_len)) {
304 switch (attr->rta_type) {
307 path_len = RTA_PAYLOAD(attr);
308 if (path_len > UNIX_PATH_MAX)
309 path_len = UNIX_PATH_MAX;
310 memcpy(path, RTA_DATA(attr), path_len);
311 path[path_len] = '\0';
315 if (RTA_PAYLOAD(attr) >= 4)
316 peer = *(uint32_t *) RTA_DATA(attr);
322 * print obtained information in the following format:
323 * "UNIX:[" SELF_INODE [ "->" PEER_INODE ][ "," SOCKET_FILE ] "]"
325 if (!peer && !path_len)
328 char peer_str[3 + sizeof(peer) * 3];
330 xsprintf(peer_str, "->%u", peer);
334 const char *path_str;
336 char *outstr = alloca(4 * path_len + 4);
339 if (path[0] == '\0') {
341 string_quote(path + 1, outstr + 2,
342 path_len - 1, QUOTE_0_TERMINATED, NULL);
344 string_quote(path, outstr + 1,
345 path_len, QUOTE_0_TERMINATED, NULL);
353 if (asprintf(&details, "%s:[%lu%s%s]", proto_name, inode,
354 peer_str, path_str) < 0)
357 return cache_inode_details(inode, details);
361 netlink_send_query(struct tcb *tcp, const int fd, const unsigned long inode)
364 const struct nlmsghdr nlh;
365 const struct netlink_diag_req ndr;
368 .nlmsg_len = sizeof(req),
369 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
370 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
373 .sdiag_family = AF_NETLINK,
374 .sdiag_protocol = NDIAG_PROTO_ALL
377 return send_query(tcp, fd, &req, sizeof(req));
381 netlink_parse_response(const void *data, const int data_len,
382 const unsigned long inode, void *opaque_data)
384 const char *proto_name = opaque_data;
385 const struct netlink_diag_msg *const diag_msg = data;
386 const char *netlink_proto;
389 if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
391 if (diag_msg->ndiag_ino != inode)
394 if (diag_msg->ndiag_family != AF_NETLINK)
397 netlink_proto = xlookup(netlink_protocols,
398 diag_msg->ndiag_protocol);
401 netlink_proto = STR_STRIP_PREFIX(netlink_proto, "NETLINK_");
402 if (asprintf(&details, "%s:[%s:%u]", proto_name,
403 netlink_proto, diag_msg->ndiag_portid) < 0)
406 if (asprintf(&details, "%s:[%u]", proto_name,
407 (unsigned) diag_msg->ndiag_protocol) < 0)
411 return cache_inode_details(inode, details);
415 unix_get(struct tcb *tcp, const int fd, const unsigned long inode)
417 return unix_send_query(tcp, fd, inode)
418 && receive_responses(tcp, fd, inode, SOCK_DIAG_BY_FAMILY,
419 unix_parse_response, (void *) "UNIX")
420 ? get_sockaddr_by_inode_cached(inode) : NULL;
424 inet_get(struct tcb *tcp, const int fd, const int family, const int protocol,
425 const unsigned long inode, const char *proto_name)
427 return inet_send_query(tcp, fd, family, protocol)
428 && receive_responses(tcp, fd, inode, SOCK_DIAG_BY_FAMILY,
429 inet_parse_response, (void *) proto_name)
430 ? get_sockaddr_by_inode_cached(inode) : NULL;
434 tcp_v4_get(struct tcb *tcp, const int fd, const unsigned long inode)
436 return inet_get(tcp, fd, AF_INET, IPPROTO_TCP, inode, "TCP");
440 udp_v4_get(struct tcb *tcp, const int fd, const unsigned long inode)
442 return inet_get(tcp, fd, AF_INET, IPPROTO_UDP, inode, "UDP");
446 tcp_v6_get(struct tcb *tcp, const int fd, const unsigned long inode)
448 return inet_get(tcp, fd, AF_INET6, IPPROTO_TCP, inode, "TCPv6");
452 udp_v6_get(struct tcb *tcp, const int fd, const unsigned long inode)
454 return inet_get(tcp, fd, AF_INET6, IPPROTO_UDP, inode, "UDPv6");
458 netlink_get(struct tcb *tcp, const int fd, const unsigned long inode)
460 return netlink_send_query(tcp, fd, inode)
461 && receive_responses(tcp, fd, inode, SOCK_DIAG_BY_FAMILY,
462 netlink_parse_response, (void *) "NETLINK")
463 ? get_sockaddr_by_inode_cached(inode) : NULL;
466 static const struct {
467 const char *const name;
468 const char * (*const get)(struct tcb *, int, unsigned long);
470 [SOCK_PROTO_UNIX] = { "UNIX", unix_get },
471 [SOCK_PROTO_TCP] = { "TCP", tcp_v4_get },
472 [SOCK_PROTO_UDP] = { "UDP", udp_v4_get },
473 [SOCK_PROTO_TCPv6] = { "TCPv6", tcp_v6_get },
474 [SOCK_PROTO_UDPv6] = { "UDPv6", udp_v6_get },
475 [SOCK_PROTO_NETLINK] = { "NETLINK", netlink_get }
479 get_proto_by_name(const char *const name)
482 for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
483 i < ARRAY_SIZE(protocols); ++i) {
484 if (protocols[i].name && !strcmp(name, protocols[i].name))
485 return (enum sock_proto) i;
487 return SOCK_PROTO_UNKNOWN;
491 get_sockaddr_by_inode_uncached(struct tcb *tcp, const unsigned long inode,
492 const enum sock_proto proto)
494 if ((unsigned int) proto >= ARRAY_SIZE(protocols) ||
495 (proto != SOCK_PROTO_UNKNOWN && !protocols[proto].get))
498 const int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
501 const char *details = NULL;
503 if (proto != SOCK_PROTO_UNKNOWN) {
504 details = protocols[proto].get(tcp, fd, inode);
507 for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
508 i < ARRAY_SIZE(protocols); ++i) {
509 if (!protocols[i].get)
511 details = protocols[i].get(tcp, fd, inode);
522 print_sockaddr_by_inode_uncached(struct tcb *tcp, const unsigned long inode,
523 const enum sock_proto proto)
525 const char *details = get_sockaddr_by_inode_uncached(tcp, inode, proto);
532 if ((unsigned int) proto < ARRAY_SIZE(protocols) &&
533 protocols[proto].name) {
534 tprintf("%s:[%lu]", protocols[proto].name, inode);
541 /* Given an inode number of a socket, return its protocol details. */
543 get_sockaddr_by_inode(struct tcb *const tcp, const int fd,
544 const unsigned long inode)
546 const char *details = get_sockaddr_by_inode_cached(inode);
547 return details ? details :
548 get_sockaddr_by_inode_uncached(tcp, inode, getfdproto(tcp, fd));
551 /* Given an inode number of a socket, print out its protocol details. */
553 print_sockaddr_by_inode(struct tcb *const tcp, const int fd,
554 const unsigned long inode)
556 return print_sockaddr_by_inode_cached(inode) ? true :
557 print_sockaddr_by_inode_uncached(tcp, inode,
558 getfdproto(tcp, fd));
561 #ifdef HAVE_LINUX_GENETLINK_H
563 * Managing the cache for decoding communications of Netlink GENERIC protocol
565 * As name shown Netlink GENERIC protocol is generic protocol. The
566 * numbers of msg types used in the protocol are not defined
567 * statically. Kernel defines them on demand. So the xlat converted
568 * from header files doesn't help for decoding the protocol. Following
569 * codes are building xlat(dyxlat) at runtime.
572 genl_send_dump_families(struct tcb *tcp, const int fd)
575 const struct nlmsghdr nlh;
576 struct genlmsghdr gnlh;
579 .nlmsg_len = sizeof(req),
580 .nlmsg_type = GENL_ID_CTRL,
581 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
584 .cmd = CTRL_CMD_GETFAMILY,
587 return send_query(tcp, fd, &req, sizeof(req));
591 genl_parse_families_response(const void *const data,
592 const int data_len, const unsigned long inode,
595 struct dyxlat *const dyxlat = opaque_data;
596 const struct genlmsghdr *const gnlh = data;
598 int rta_len = data_len - NLMSG_LENGTH(sizeof(*gnlh));
601 unsigned int name_len = 0;
606 if (gnlh->cmd != CTRL_CMD_NEWFAMILY)
608 if (gnlh->version != 2)
611 for (attr = (struct rtattr *) (gnlh + 1);
612 RTA_OK(attr, rta_len);
613 attr = RTA_NEXT(attr, rta_len)) {
614 switch (attr->rta_type) {
615 case CTRL_ATTR_FAMILY_NAME:
617 name = RTA_DATA(attr);
618 name_len = RTA_PAYLOAD(attr);
621 case CTRL_ATTR_FAMILY_ID:
622 if (!id && RTA_PAYLOAD(attr) == sizeof(*id))
628 dyxlat_add_pair(dyxlat, *id, name, name_len);
638 genl_families_xlat(struct tcb *tcp)
640 static struct dyxlat *dyxlat;
643 dyxlat = dyxlat_alloc(32);
645 int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
649 if (genl_send_dump_families(tcp, fd))
650 receive_responses(tcp, fd, 0, GENL_ID_CTRL,
651 genl_parse_families_response, dyxlat);
656 return dyxlat_get(dyxlat);
659 #else /* !HAVE_LINUX_GENETLINK_H */
662 genl_families_xlat(struct tcb *tcp)