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_inode_details(const unsigned long inode, char *const details)
62 cache_entry *e = &cache[inode & CACHE_MASK];
71 get_sockaddr_by_inode_cached(const unsigned long inode)
73 const cache_entry *const e = &cache[inode & CACHE_MASK];
74 return (e && inode == e->inode) ? e->details : NULL;
78 print_sockaddr_by_inode_cached(const unsigned long inode)
80 const char *const details = get_sockaddr_by_inode_cached(inode);
89 send_query(const int fd, void *req, size_t req_size)
91 struct sockaddr_nl nladdr = {
92 .nl_family = AF_NETLINK
98 const struct msghdr msg = {
100 .msg_namelen = sizeof(nladdr),
106 if (sendmsg(fd, &msg, 0) < 0) {
116 inet_send_query(const int fd, const int family, const int proto)
119 const struct nlmsghdr nlh;
120 const struct inet_diag_req_v2 idr;
123 .nlmsg_len = sizeof(req),
124 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
125 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
128 .sdiag_family = family,
129 .sdiag_protocol = proto,
133 return send_query(fd, &req, sizeof(req));
137 inet_parse_response(const void *const data, const int data_len,
138 const unsigned long inode, void *opaque_data)
140 const char *const proto_name = opaque_data;
141 const struct inet_diag_msg *const diag_msg = data;
142 static const char zero_addr[sizeof(struct in6_addr)];
143 socklen_t addr_size, text_size;
145 if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
147 if (diag_msg->idiag_inode != inode)
150 switch(diag_msg->idiag_family) {
152 addr_size = sizeof(struct in_addr);
153 text_size = INET_ADDRSTRLEN;
156 addr_size = sizeof(struct in6_addr);
157 text_size = INET6_ADDRSTRLEN;
163 char src_buf[text_size];
166 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_src,
170 if (diag_msg->id.idiag_dport ||
171 memcmp(zero_addr, diag_msg->id.idiag_dst, addr_size)) {
172 char dst_buf[text_size];
174 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_dst,
178 if (asprintf(&details, "%s:[%s:%u->%s:%u]", proto_name,
179 src_buf, ntohs(diag_msg->id.idiag_sport),
180 dst_buf, ntohs(diag_msg->id.idiag_dport)) < 0)
183 if (asprintf(&details, "%s:[%s:%u]", proto_name, src_buf,
184 ntohs(diag_msg->id.idiag_sport)) < 0)
188 return cache_inode_details(inode, details);
192 receive_responses(const int fd, const unsigned long inode,
193 const unsigned long expected_msg_type,
194 int (*parser)(const void *, int,
195 unsigned long, void *),
200 long buf[8192 / sizeof(long)];
203 struct sockaddr_nl nladdr = {
204 .nl_family = AF_NETLINK
207 .iov_base = hdr_buf.buf,
208 .iov_len = sizeof(hdr_buf.buf)
213 struct msghdr msg = {
215 .msg_namelen = sizeof(nladdr),
220 ssize_t ret = recvmsg(fd, &msg, flags);
227 const struct nlmsghdr *h = &hdr_buf.hdr;
228 if (!NLMSG_OK(h, ret))
230 for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) {
231 if (h->nlmsg_type != expected_msg_type)
233 const int rc = parser(NLMSG_DATA(h),
234 h->nlmsg_len, inode, opaque_data);
240 flags = MSG_DONTWAIT;
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 void *data, const int data_len,
268 const unsigned long inode, void *opaque_data)
270 const char *proto_name = opaque_data;
271 const struct unix_diag_msg *diag_msg = data;
273 int rta_len = data_len - NLMSG_LENGTH(sizeof(*diag_msg));
276 char path[UNIX_PATH_MAX + 1];
280 if (diag_msg->udiag_ino != inode)
282 if (diag_msg->udiag_family != AF_UNIX)
285 for (attr = (struct rtattr *) (diag_msg + 1);
286 RTA_OK(attr, rta_len);
287 attr = RTA_NEXT(attr, rta_len)) {
288 switch (attr->rta_type) {
291 path_len = RTA_PAYLOAD(attr);
292 if (path_len > UNIX_PATH_MAX)
293 path_len = UNIX_PATH_MAX;
294 memcpy(path, RTA_DATA(attr), path_len);
295 path[path_len] = '\0';
299 if (RTA_PAYLOAD(attr) >= 4)
300 peer = *(uint32_t *) RTA_DATA(attr);
306 * print obtained information in the following format:
307 * "UNIX:[" SELF_INODE [ "->" PEER_INODE ][ "," SOCKET_FILE ] "]"
309 if (!peer && !path_len)
312 char peer_str[3 + sizeof(peer) * 3];
314 snprintf(peer_str, sizeof(peer_str), "->%u", peer);
318 const char *path_str;
320 char *outstr = alloca(4 * path_len + 4);
323 if (path[0] == '\0') {
325 string_quote(path + 1, outstr + 2,
326 path_len - 1, QUOTE_0_TERMINATED);
328 string_quote(path, outstr + 1,
329 path_len, QUOTE_0_TERMINATED);
337 if (asprintf(&details, "%s:[%lu%s%s]", proto_name, inode,
338 peer_str, path_str) < 0)
341 return cache_inode_details(inode, details);
345 netlink_send_query(const int fd, const unsigned long inode)
348 const struct nlmsghdr nlh;
349 const struct netlink_diag_req ndr;
352 .nlmsg_len = sizeof(req),
353 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
354 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
357 .sdiag_family = AF_NETLINK,
358 .sdiag_protocol = NDIAG_PROTO_ALL,
359 .ndiag_show = NDIAG_SHOW_MEMINFO
362 return send_query(fd, &req, sizeof(req));
366 netlink_parse_response(const void *data, const int data_len,
367 const unsigned long inode, void *opaque_data)
369 const char *proto_name = opaque_data;
370 const struct netlink_diag_msg *const diag_msg = data;
371 const char *netlink_proto;
374 if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
376 if (diag_msg->ndiag_ino != inode)
379 if (diag_msg->ndiag_family != AF_NETLINK)
382 netlink_proto = xlookup(netlink_protocols,
383 diag_msg->ndiag_protocol);
386 netlink_proto = STR_STRIP_PREFIX(netlink_proto, "NETLINK_");
387 if (asprintf(&details, "%s:[%s:%u]", proto_name,
388 netlink_proto, diag_msg->ndiag_portid) < 0)
391 if (asprintf(&details, "%s:[%u]", proto_name,
392 (unsigned) diag_msg->ndiag_protocol) < 0)
396 return cache_inode_details(inode, details);
400 unix_get(const int fd, const unsigned long inode)
402 return unix_send_query(fd, inode)
403 && receive_responses(fd, inode, SOCK_DIAG_BY_FAMILY,
404 unix_parse_response, (void *) "UNIX")
405 ? get_sockaddr_by_inode_cached(inode) : NULL;
409 inet_get(const int fd, const int family, const int protocol,
410 const unsigned long inode, const char *proto_name)
412 return inet_send_query(fd, family, protocol)
413 && receive_responses(fd, inode, SOCK_DIAG_BY_FAMILY,
414 inet_parse_response, (void *) proto_name)
415 ? get_sockaddr_by_inode_cached(inode) : NULL;
419 tcp_v4_get(const int fd, const unsigned long inode)
421 return inet_get(fd, AF_INET, IPPROTO_TCP, inode, "TCP");
425 udp_v4_get(const int fd, const unsigned long inode)
427 return inet_get(fd, AF_INET, IPPROTO_UDP, inode, "UDP");
431 tcp_v6_get(const int fd, const unsigned long inode)
433 return inet_get(fd, AF_INET6, IPPROTO_TCP, inode, "TCPv6");
437 udp_v6_get(const int fd, const unsigned long inode)
439 return inet_get(fd, AF_INET6, IPPROTO_UDP, inode, "UDPv6");
443 netlink_get(const int fd, const unsigned long inode)
445 return netlink_send_query(fd, inode)
446 && receive_responses(fd, inode, SOCK_DIAG_BY_FAMILY,
447 netlink_parse_response, (void *) "NETLINK")
448 ? get_sockaddr_by_inode_cached(inode) : NULL;
451 static const struct {
452 const char *const name;
453 const char * (*const get)(int, unsigned long);
455 [SOCK_PROTO_UNIX] = { "UNIX", unix_get },
456 [SOCK_PROTO_TCP] = { "TCP", tcp_v4_get },
457 [SOCK_PROTO_UDP] = { "UDP", udp_v4_get },
458 [SOCK_PROTO_TCPv6] = { "TCPv6", tcp_v6_get },
459 [SOCK_PROTO_UDPv6] = { "UDPv6", udp_v6_get },
460 [SOCK_PROTO_NETLINK] = { "NETLINK", netlink_get }
464 get_proto_by_name(const char *const name)
467 for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
468 i < ARRAY_SIZE(protocols); ++i) {
469 if (protocols[i].name && !strcmp(name, protocols[i].name))
470 return (enum sock_proto) i;
472 return SOCK_PROTO_UNKNOWN;
476 get_sockaddr_by_inode_uncached(const unsigned long inode,
477 const enum sock_proto proto)
479 if ((unsigned int) proto >= ARRAY_SIZE(protocols) ||
480 (proto != SOCK_PROTO_UNKNOWN && !protocols[proto].get))
483 const int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
486 const char *details = NULL;
488 if (proto != SOCK_PROTO_UNKNOWN) {
489 details = protocols[proto].get(fd, inode);
492 for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
493 i < ARRAY_SIZE(protocols); ++i) {
494 if (!protocols[i].get)
496 details = protocols[i].get(fd, inode);
507 print_sockaddr_by_inode_uncached(const unsigned long inode,
508 const enum sock_proto proto)
510 const char *details = get_sockaddr_by_inode_uncached(inode, proto);
517 if ((unsigned int) proto < ARRAY_SIZE(protocols) &&
518 protocols[proto].name) {
519 tprintf("%s:[%lu]", protocols[proto].name, inode);
526 /* Given an inode number of a socket, return its protocol details. */
528 get_sockaddr_by_inode(struct tcb *const tcp, const int fd,
529 const unsigned long inode)
531 const char *details = get_sockaddr_by_inode_cached(inode);
532 return details ? details :
533 get_sockaddr_by_inode_uncached(inode, getfdproto(tcp, fd));
536 /* Given an inode number of a socket, print out its protocol details. */
538 print_sockaddr_by_inode(struct tcb *const tcp, const int fd,
539 const unsigned long inode)
541 return print_sockaddr_by_inode_cached(inode) ? true :
542 print_sockaddr_by_inode_uncached(inode, getfdproto(tcp, fd));