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>
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(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(const int fd, const int family, const int proto)
120 const struct nlmsghdr nlh;
121 const struct inet_diag_req_v2 idr;
124 .nlmsg_len = sizeof(req),
125 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
126 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
129 .sdiag_family = family,
130 .sdiag_protocol = proto,
134 return send_query(fd, &req, sizeof(req));
138 inet_parse_response(const void *const data, const int data_len,
139 const unsigned long inode, void *opaque_data)
141 const char *const proto_name = opaque_data;
142 const struct inet_diag_msg *const diag_msg = data;
143 static const char zero_addr[sizeof(struct in6_addr)];
144 socklen_t addr_size, text_size;
146 if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
148 if (diag_msg->idiag_inode != inode)
151 switch (diag_msg->idiag_family) {
153 addr_size = sizeof(struct in_addr);
154 text_size = INET_ADDRSTRLEN;
157 addr_size = sizeof(struct in6_addr);
158 text_size = INET6_ADDRSTRLEN;
164 char src_buf[text_size];
167 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_src,
171 if (diag_msg->id.idiag_dport ||
172 memcmp(zero_addr, diag_msg->id.idiag_dst, addr_size)) {
173 char dst_buf[text_size];
175 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_dst,
179 if (asprintf(&details, "%s:[%s:%u->%s:%u]", proto_name,
180 src_buf, ntohs(diag_msg->id.idiag_sport),
181 dst_buf, ntohs(diag_msg->id.idiag_dport)) < 0)
184 if (asprintf(&details, "%s:[%s:%u]", proto_name, src_buf,
185 ntohs(diag_msg->id.idiag_sport)) < 0)
189 return cache_inode_details(inode, details);
193 receive_responses(const int fd, const unsigned long inode,
194 const unsigned long expected_msg_type,
195 int (*parser)(const void *, int,
196 unsigned long, void *),
201 long buf[8192 / sizeof(long)];
204 struct sockaddr_nl nladdr = {
205 .nl_family = AF_NETLINK
208 .iov_base = hdr_buf.buf,
209 .iov_len = sizeof(hdr_buf.buf)
214 struct msghdr msg = {
216 .msg_namelen = sizeof(nladdr),
221 ssize_t ret = recvmsg(fd, &msg, flags);
228 const struct nlmsghdr *h = &hdr_buf.hdr;
229 if (!NLMSG_OK(h, ret))
231 for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) {
232 if (h->nlmsg_type != expected_msg_type)
234 const int rc = parser(NLMSG_DATA(h),
235 h->nlmsg_len, inode, opaque_data);
241 flags = MSG_DONTWAIT;
246 unix_send_query(const int fd, const unsigned long inode)
249 const struct nlmsghdr nlh;
250 const struct unix_diag_req udr;
253 .nlmsg_len = sizeof(req),
254 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
255 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
258 .sdiag_family = AF_UNIX,
261 .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER
264 return send_query(fd, &req, sizeof(req));
268 unix_parse_response(const void *data, const int data_len,
269 const unsigned long inode, void *opaque_data)
271 const char *proto_name = opaque_data;
272 const struct unix_diag_msg *diag_msg = data;
274 int rta_len = data_len - NLMSG_LENGTH(sizeof(*diag_msg));
277 char path[UNIX_PATH_MAX + 1];
281 if (diag_msg->udiag_ino != inode)
283 if (diag_msg->udiag_family != AF_UNIX)
286 for (attr = (struct rtattr *) (diag_msg + 1);
287 RTA_OK(attr, rta_len);
288 attr = RTA_NEXT(attr, rta_len)) {
289 switch (attr->rta_type) {
292 path_len = RTA_PAYLOAD(attr);
293 if (path_len > UNIX_PATH_MAX)
294 path_len = UNIX_PATH_MAX;
295 memcpy(path, RTA_DATA(attr), path_len);
296 path[path_len] = '\0';
300 if (RTA_PAYLOAD(attr) >= 4)
301 peer = *(uint32_t *) RTA_DATA(attr);
307 * print obtained information in the following format:
308 * "UNIX:[" SELF_INODE [ "->" PEER_INODE ][ "," SOCKET_FILE ] "]"
310 if (!peer && !path_len)
313 char peer_str[3 + sizeof(peer) * 3];
315 xsprintf(peer_str, "->%u", peer);
319 const char *path_str;
321 char *outstr = alloca(4 * path_len + 4);
324 if (path[0] == '\0') {
326 string_quote(path + 1, outstr + 2,
327 path_len - 1, QUOTE_0_TERMINATED);
329 string_quote(path, outstr + 1,
330 path_len, QUOTE_0_TERMINATED);
338 if (asprintf(&details, "%s:[%lu%s%s]", proto_name, inode,
339 peer_str, path_str) < 0)
342 return cache_inode_details(inode, details);
346 netlink_send_query(const int fd, const unsigned long inode)
349 const struct nlmsghdr nlh;
350 const struct netlink_diag_req ndr;
353 .nlmsg_len = sizeof(req),
354 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
355 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
358 .sdiag_family = AF_NETLINK,
359 .sdiag_protocol = NDIAG_PROTO_ALL,
360 .ndiag_show = NDIAG_SHOW_MEMINFO
363 return send_query(fd, &req, sizeof(req));
367 netlink_parse_response(const void *data, const int data_len,
368 const unsigned long inode, void *opaque_data)
370 const char *proto_name = opaque_data;
371 const struct netlink_diag_msg *const diag_msg = data;
372 const char *netlink_proto;
375 if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
377 if (diag_msg->ndiag_ino != inode)
380 if (diag_msg->ndiag_family != AF_NETLINK)
383 netlink_proto = xlookup(netlink_protocols,
384 diag_msg->ndiag_protocol);
387 netlink_proto = STR_STRIP_PREFIX(netlink_proto, "NETLINK_");
388 if (asprintf(&details, "%s:[%s:%u]", proto_name,
389 netlink_proto, diag_msg->ndiag_portid) < 0)
392 if (asprintf(&details, "%s:[%u]", proto_name,
393 (unsigned) diag_msg->ndiag_protocol) < 0)
397 return cache_inode_details(inode, details);
401 unix_get(const int fd, const unsigned long inode)
403 return unix_send_query(fd, inode)
404 && receive_responses(fd, inode, SOCK_DIAG_BY_FAMILY,
405 unix_parse_response, (void *) "UNIX")
406 ? get_sockaddr_by_inode_cached(inode) : NULL;
410 inet_get(const int fd, const int family, const int protocol,
411 const unsigned long inode, const char *proto_name)
413 return inet_send_query(fd, family, protocol)
414 && receive_responses(fd, inode, SOCK_DIAG_BY_FAMILY,
415 inet_parse_response, (void *) proto_name)
416 ? get_sockaddr_by_inode_cached(inode) : NULL;
420 tcp_v4_get(const int fd, const unsigned long inode)
422 return inet_get(fd, AF_INET, IPPROTO_TCP, inode, "TCP");
426 udp_v4_get(const int fd, const unsigned long inode)
428 return inet_get(fd, AF_INET, IPPROTO_UDP, inode, "UDP");
432 tcp_v6_get(const int fd, const unsigned long inode)
434 return inet_get(fd, AF_INET6, IPPROTO_TCP, inode, "TCPv6");
438 udp_v6_get(const int fd, const unsigned long inode)
440 return inet_get(fd, AF_INET6, IPPROTO_UDP, inode, "UDPv6");
444 netlink_get(const int fd, const unsigned long inode)
446 return netlink_send_query(fd, inode)
447 && receive_responses(fd, inode, SOCK_DIAG_BY_FAMILY,
448 netlink_parse_response, (void *) "NETLINK")
449 ? get_sockaddr_by_inode_cached(inode) : NULL;
452 static const struct {
453 const char *const name;
454 const char * (*const get)(int, unsigned long);
456 [SOCK_PROTO_UNIX] = { "UNIX", unix_get },
457 [SOCK_PROTO_TCP] = { "TCP", tcp_v4_get },
458 [SOCK_PROTO_UDP] = { "UDP", udp_v4_get },
459 [SOCK_PROTO_TCPv6] = { "TCPv6", tcp_v6_get },
460 [SOCK_PROTO_UDPv6] = { "UDPv6", udp_v6_get },
461 [SOCK_PROTO_NETLINK] = { "NETLINK", netlink_get }
465 get_proto_by_name(const char *const name)
468 for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
469 i < ARRAY_SIZE(protocols); ++i) {
470 if (protocols[i].name && !strcmp(name, protocols[i].name))
471 return (enum sock_proto) i;
473 return SOCK_PROTO_UNKNOWN;
477 get_sockaddr_by_inode_uncached(const unsigned long inode,
478 const enum sock_proto proto)
480 if ((unsigned int) proto >= ARRAY_SIZE(protocols) ||
481 (proto != SOCK_PROTO_UNKNOWN && !protocols[proto].get))
484 const int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
487 const char *details = NULL;
489 if (proto != SOCK_PROTO_UNKNOWN) {
490 details = protocols[proto].get(fd, inode);
493 for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
494 i < ARRAY_SIZE(protocols); ++i) {
495 if (!protocols[i].get)
497 details = protocols[i].get(fd, inode);
508 print_sockaddr_by_inode_uncached(const unsigned long inode,
509 const enum sock_proto proto)
511 const char *details = get_sockaddr_by_inode_uncached(inode, proto);
518 if ((unsigned int) proto < ARRAY_SIZE(protocols) &&
519 protocols[proto].name) {
520 tprintf("%s:[%lu]", protocols[proto].name, inode);
527 /* Given an inode number of a socket, return its protocol details. */
529 get_sockaddr_by_inode(struct tcb *const tcp, const int fd,
530 const unsigned long inode)
532 const char *details = get_sockaddr_by_inode_cached(inode);
533 return details ? details :
534 get_sockaddr_by_inode_uncached(inode, getfdproto(tcp, fd));
537 /* Given an inode number of a socket, print out its protocol details. */
539 print_sockaddr_by_inode(struct tcb *const tcp, const int fd,
540 const unsigned long inode)
542 return print_sockaddr_by_inode_cached(inode) ? true :
543 print_sockaddr_by_inode_uncached(inode, getfdproto(tcp, fd));
546 #ifdef HAVE_LINUX_GENETLINK_H
548 * Managing the cache for decoding communications of Netlink GENERIC protocol
550 * As name shown Netlink GENERIC protocol is generic protocol. The
551 * numbers of msg types used in the protocol are not defined
552 * statically. Kernel defines them on demand. So the xlat converted
553 * from header files doesn't help for decoding the protocol. Following
554 * codes are building xlat(dyxlat) at runtime.
557 genl_send_dump_families(const int fd)
560 const struct nlmsghdr nlh;
561 struct genlmsghdr gnlh;
564 .nlmsg_len = sizeof(req),
565 .nlmsg_type = GENL_ID_CTRL,
566 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
569 .cmd = CTRL_CMD_GETFAMILY,
572 return send_query(fd, &req, sizeof(req));
576 genl_parse_families_response(const void *const data,
577 const int data_len, const unsigned long inode,
580 struct dyxlat *const dyxlat = opaque_data;
581 const struct genlmsghdr *const gnlh = data;
583 int rta_len = data_len - NLMSG_LENGTH(sizeof(*gnlh));
586 unsigned int name_len = 0;
591 if (gnlh->cmd != CTRL_CMD_NEWFAMILY)
593 if (gnlh->version != 2)
596 for (attr = (struct rtattr *) (gnlh + 1);
597 RTA_OK(attr, rta_len);
598 attr = RTA_NEXT(attr, rta_len)) {
599 switch (attr->rta_type) {
600 case CTRL_ATTR_FAMILY_NAME:
602 name = RTA_DATA(attr);
603 name_len = RTA_PAYLOAD(attr);
606 case CTRL_ATTR_FAMILY_ID:
607 if (!id && RTA_PAYLOAD(attr) == sizeof(*id))
613 dyxlat_add_pair(dyxlat, *id, name, name_len);
623 genl_families_xlat(void)
625 static struct dyxlat *dyxlat;
628 dyxlat = dyxlat_alloc(32);
630 int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
634 if (genl_send_dump_families(fd))
635 receive_responses(fd, 0, GENL_ID_CTRL,
636 genl_parse_families_response, dyxlat);
641 return dyxlat_get(dyxlat);
644 #else /* !HAVE_LINUX_GENETLINK_H */
647 genl_families_xlat(void)