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)
54 #define CACHE_SIZE 1024U
55 static cache_entry cache[CACHE_SIZE];
56 #define CACHE_MASK (CACHE_SIZE - 1)
59 cache_inode_details(const unsigned long inode, char *const details)
61 cache_entry *e = &cache[inode & CACHE_MASK];
70 get_sockaddr_by_inode_cached(const unsigned long inode)
72 const cache_entry *const e = &cache[inode & CACHE_MASK];
73 return (e && inode == e->inode) ? e->details : NULL;
77 print_sockaddr_by_inode_cached(const unsigned long inode)
79 const char *const details = get_sockaddr_by_inode_cached(inode);
88 send_query(const int fd, void *req, size_t req_size)
90 struct sockaddr_nl nladdr = {
91 .nl_family = AF_NETLINK
97 const struct msghdr msg = {
99 .msg_namelen = sizeof(nladdr),
105 if (sendmsg(fd, &msg, 0) < 0) {
115 inet_send_query(const int fd, const int family, const int proto)
118 const struct nlmsghdr nlh;
119 const struct inet_diag_req_v2 idr;
122 .nlmsg_len = sizeof(req),
123 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
124 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
127 .sdiag_family = family,
128 .sdiag_protocol = proto,
132 return send_query(fd, &req, sizeof(req));
136 inet_parse_response(const void *const data, const int data_len,
137 const unsigned long inode, void *opaque_data)
139 const char *const proto_name = opaque_data;
140 const struct inet_diag_msg *const diag_msg = data;
141 static const char zero_addr[sizeof(struct in6_addr)];
142 socklen_t addr_size, text_size;
144 if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
146 if (diag_msg->idiag_inode != inode)
149 switch (diag_msg->idiag_family) {
151 addr_size = sizeof(struct in_addr);
152 text_size = INET_ADDRSTRLEN;
155 addr_size = sizeof(struct in6_addr);
156 text_size = INET6_ADDRSTRLEN;
162 char src_buf[text_size];
165 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_src,
169 if (diag_msg->id.idiag_dport ||
170 memcmp(zero_addr, diag_msg->id.idiag_dst, addr_size)) {
171 char dst_buf[text_size];
173 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_dst,
177 if (asprintf(&details, "%s:[%s:%u->%s:%u]", proto_name,
178 src_buf, ntohs(diag_msg->id.idiag_sport),
179 dst_buf, ntohs(diag_msg->id.idiag_dport)) < 0)
182 if (asprintf(&details, "%s:[%s:%u]", proto_name, src_buf,
183 ntohs(diag_msg->id.idiag_sport)) < 0)
187 return cache_inode_details(inode, details);
191 receive_responses(const int fd, const unsigned long inode,
192 const unsigned long expected_msg_type,
193 int (*parser)(const void *, int,
194 unsigned long, void *),
199 long buf[8192 / sizeof(long)];
202 struct sockaddr_nl nladdr = {
203 .nl_family = AF_NETLINK
206 .iov_base = hdr_buf.buf,
207 .iov_len = sizeof(hdr_buf.buf)
212 struct msghdr msg = {
214 .msg_namelen = sizeof(nladdr),
219 ssize_t ret = recvmsg(fd, &msg, flags);
226 const struct nlmsghdr *h = &hdr_buf.hdr;
227 if (!NLMSG_OK(h, ret))
229 for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) {
230 if (h->nlmsg_type != expected_msg_type)
232 const int rc = parser(NLMSG_DATA(h),
233 h->nlmsg_len, inode, opaque_data);
239 flags = MSG_DONTWAIT;
244 unix_send_query(const int fd, const unsigned long inode)
247 const struct nlmsghdr nlh;
248 const struct unix_diag_req udr;
251 .nlmsg_len = sizeof(req),
252 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
253 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
256 .sdiag_family = AF_UNIX,
259 .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER
262 return send_query(fd, &req, sizeof(req));
266 unix_parse_response(const void *data, const int data_len,
267 const unsigned long inode, void *opaque_data)
269 const char *proto_name = opaque_data;
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_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 void *data, const int data_len,
366 const unsigned long inode, void *opaque_data)
368 const char *proto_name = opaque_data;
369 const struct netlink_diag_msg *const diag_msg = data;
370 const char *netlink_proto;
373 if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
375 if (diag_msg->ndiag_ino != inode)
378 if (diag_msg->ndiag_family != AF_NETLINK)
381 netlink_proto = xlookup(netlink_protocols,
382 diag_msg->ndiag_protocol);
385 netlink_proto = STR_STRIP_PREFIX(netlink_proto, "NETLINK_");
386 if (asprintf(&details, "%s:[%s:%u]", proto_name,
387 netlink_proto, diag_msg->ndiag_portid) < 0)
390 if (asprintf(&details, "%s:[%u]", proto_name,
391 (unsigned) diag_msg->ndiag_protocol) < 0)
395 return cache_inode_details(inode, details);
399 unix_get(const int fd, const unsigned long inode)
401 return unix_send_query(fd, inode)
402 && receive_responses(fd, inode, SOCK_DIAG_BY_FAMILY,
403 unix_parse_response, (void *) "UNIX")
404 ? get_sockaddr_by_inode_cached(inode) : NULL;
408 inet_get(const int fd, const int family, const int protocol,
409 const unsigned long inode, const char *proto_name)
411 return inet_send_query(fd, family, protocol)
412 && receive_responses(fd, inode, SOCK_DIAG_BY_FAMILY,
413 inet_parse_response, (void *) proto_name)
414 ? get_sockaddr_by_inode_cached(inode) : NULL;
418 tcp_v4_get(const int fd, const unsigned long inode)
420 return inet_get(fd, AF_INET, IPPROTO_TCP, inode, "TCP");
424 udp_v4_get(const int fd, const unsigned long inode)
426 return inet_get(fd, AF_INET, IPPROTO_UDP, inode, "UDP");
430 tcp_v6_get(const int fd, const unsigned long inode)
432 return inet_get(fd, AF_INET6, IPPROTO_TCP, inode, "TCPv6");
436 udp_v6_get(const int fd, const unsigned long inode)
438 return inet_get(fd, AF_INET6, IPPROTO_UDP, inode, "UDPv6");
442 netlink_get(const int fd, const unsigned long inode)
444 return netlink_send_query(fd, inode)
445 && receive_responses(fd, inode, SOCK_DIAG_BY_FAMILY,
446 netlink_parse_response, (void *) "NETLINK")
447 ? get_sockaddr_by_inode_cached(inode) : NULL;
450 static const struct {
451 const char *const name;
452 const char * (*const get)(int, unsigned long);
454 [SOCK_PROTO_UNIX] = { "UNIX", unix_get },
455 [SOCK_PROTO_TCP] = { "TCP", tcp_v4_get },
456 [SOCK_PROTO_UDP] = { "UDP", udp_v4_get },
457 [SOCK_PROTO_TCPv6] = { "TCPv6", tcp_v6_get },
458 [SOCK_PROTO_UDPv6] = { "UDPv6", udp_v6_get },
459 [SOCK_PROTO_NETLINK] = { "NETLINK", netlink_get }
463 get_proto_by_name(const char *const name)
466 for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
467 i < ARRAY_SIZE(protocols); ++i) {
468 if (protocols[i].name && !strcmp(name, protocols[i].name))
469 return (enum sock_proto) i;
471 return SOCK_PROTO_UNKNOWN;
475 get_sockaddr_by_inode_uncached(const unsigned long inode,
476 const enum sock_proto proto)
478 if ((unsigned int) proto >= ARRAY_SIZE(protocols) ||
479 (proto != SOCK_PROTO_UNKNOWN && !protocols[proto].get))
482 const int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
485 const char *details = NULL;
487 if (proto != SOCK_PROTO_UNKNOWN) {
488 details = protocols[proto].get(fd, inode);
491 for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
492 i < ARRAY_SIZE(protocols); ++i) {
493 if (!protocols[i].get)
495 details = protocols[i].get(fd, inode);
506 print_sockaddr_by_inode_uncached(const unsigned long inode,
507 const enum sock_proto proto)
509 const char *details = get_sockaddr_by_inode_uncached(inode, proto);
516 if ((unsigned int) proto < ARRAY_SIZE(protocols) &&
517 protocols[proto].name) {
518 tprintf("%s:[%lu]", protocols[proto].name, inode);
525 /* Given an inode number of a socket, return its protocol details. */
527 get_sockaddr_by_inode(struct tcb *const tcp, const int fd,
528 const unsigned long inode)
530 const char *details = get_sockaddr_by_inode_cached(inode);
531 return details ? details :
532 get_sockaddr_by_inode_uncached(inode, getfdproto(tcp, fd));
535 /* Given an inode number of a socket, print out its protocol details. */
537 print_sockaddr_by_inode(struct tcb *const tcp, const int fd,
538 const unsigned long inode)
540 return print_sockaddr_by_inode_cached(inode) ? true :
541 print_sockaddr_by_inode_uncached(inode, getfdproto(tcp, fd));
544 #ifdef HAVE_LINUX_GENETLINK_H
546 * Managing the cache for decoding communications of Netlink GENERIC protocol
548 * As name shown Netlink GENERIC protocol is generic protocol. The
549 * numbers of msg types used in the protocol are not defined
550 * statically. Kernel defines them on demand. So the xlat converted
551 * from header files doesn't help for decoding the protocol. Following
552 * codes are building xlat(dyxlat) at runtime.
555 genl_send_dump_families(const int fd)
558 const struct nlmsghdr nlh;
559 struct genlmsghdr gnlh;
562 .nlmsg_len = sizeof(req),
563 .nlmsg_type = GENL_ID_CTRL,
564 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
567 .cmd = CTRL_CMD_GETFAMILY,
570 return send_query(fd, &req, sizeof(req));
574 genl_parse_families_response(const void *const data,
575 const int data_len, const unsigned long inode,
578 struct dyxlat *const dyxlat = opaque_data;
579 const struct genlmsghdr *const gnlh = data;
581 int rta_len = data_len - NLMSG_LENGTH(sizeof(*gnlh));
584 unsigned int name_len = 0;
589 if (gnlh->cmd != CTRL_CMD_NEWFAMILY)
591 if (gnlh->version != 2)
594 for (attr = (struct rtattr *) (gnlh + 1);
595 RTA_OK(attr, rta_len);
596 attr = RTA_NEXT(attr, rta_len)) {
597 switch (attr->rta_type) {
598 case CTRL_ATTR_FAMILY_NAME:
600 name = RTA_DATA(attr);
601 name_len = RTA_PAYLOAD(attr);
604 case CTRL_ATTR_FAMILY_ID:
605 if (!id && RTA_PAYLOAD(attr) == sizeof(*id))
611 dyxlat_add_pair(dyxlat, *id, name, name_len);
621 genl_families_xlat(void)
623 static struct dyxlat *dyxlat;
626 dyxlat = dyxlat_alloc(32);
628 int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
632 if (genl_send_dump_families(fd))
633 receive_responses(fd, 0, GENL_ID_CTRL,
634 genl_parse_families_response, dyxlat);
639 return dyxlat_get(dyxlat);
642 #else /* !HAVE_LINUX_GENETLINK_H */
645 genl_families_xlat(void)