Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Zubin Mithra <zubin.mithra@gmail.com> |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org> |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 4 | * Copyright (c) 2014-2018 The strace developers. |
Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 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. |
| 17 | * |
| 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. |
| 28 | */ |
| 29 | |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 30 | #include "defs.h" |
| 31 | #include <netinet/in.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <arpa/inet.h> |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 34 | #include "netlink.h" |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 35 | #include <linux/sock_diag.h> |
| 36 | #include <linux/inet_diag.h> |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 37 | #include <linux/unix_diag.h> |
Fabien Siron | 814c0d5 | 2016-05-17 10:08:47 +0000 | [diff] [blame] | 38 | #include <linux/netlink_diag.h> |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 39 | #include <linux/rtnetlink.h> |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 40 | #if HAVE_LINUX_GENETLINK_H |
| 41 | #include <linux/genetlink.h> |
| 42 | #endif |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 43 | |
| 44 | #include <sys/un.h> |
| 45 | #ifndef UNIX_PATH_MAX |
| 46 | # define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) 0)->sun_path) |
| 47 | #endif |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 48 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 49 | #include "xstring.h" |
| 50 | |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 51 | #define XLAT_MACROS_ONLY |
| 52 | # include "xlat/inet_protocols.h" |
| 53 | #undef XLAT_MACROS_ONLY |
| 54 | |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 55 | typedef struct { |
| 56 | unsigned long inode; |
| 57 | char *details; |
| 58 | } cache_entry; |
| 59 | |
| 60 | #define CACHE_SIZE 1024U |
| 61 | static cache_entry cache[CACHE_SIZE]; |
| 62 | #define CACHE_MASK (CACHE_SIZE - 1) |
| 63 | |
| 64 | static int |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 65 | cache_inode_details(const unsigned long inode, char *const details) |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 66 | { |
| 67 | cache_entry *e = &cache[inode & CACHE_MASK]; |
| 68 | free(e->details); |
| 69 | e->inode = inode; |
| 70 | e->details = details; |
| 71 | |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 72 | return 1; |
| 73 | } |
| 74 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 75 | static const char * |
| 76 | get_sockaddr_by_inode_cached(const unsigned long inode) |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 77 | { |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 78 | const cache_entry *const e = &cache[inode & CACHE_MASK]; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 79 | return (e && inode == e->inode) ? e->details : NULL; |
| 80 | } |
| 81 | |
| 82 | static bool |
| 83 | print_sockaddr_by_inode_cached(const unsigned long inode) |
| 84 | { |
| 85 | const char *const details = get_sockaddr_by_inode_cached(inode); |
| 86 | if (details) { |
| 87 | tprints(details); |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 88 | return true; |
| 89 | } |
| 90 | return false; |
| 91 | } |
| 92 | |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 93 | static bool |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 94 | send_query(struct tcb *tcp, const int fd, void *req, size_t req_size) |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 95 | { |
Dmitry V. Levin | af534b8 | 2014-11-21 19:59:16 +0000 | [diff] [blame] | 96 | struct sockaddr_nl nladdr = { |
| 97 | .nl_family = AF_NETLINK |
| 98 | }; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 99 | struct iovec iov = { |
Fabien Siron | 071b927 | 2016-05-08 10:45:52 +0000 | [diff] [blame] | 100 | .iov_base = req, |
| 101 | .iov_len = req_size |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 102 | }; |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 103 | const struct msghdr msg = { |
| 104 | .msg_name = &nladdr, |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 105 | .msg_namelen = sizeof(nladdr), |
| 106 | .msg_iov = &iov, |
| 107 | .msg_iovlen = 1 |
| 108 | }; |
| 109 | |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 110 | for (;;) { |
| 111 | if (sendmsg(fd, &msg, 0) < 0) { |
| 112 | if (errno == EINTR) |
| 113 | continue; |
| 114 | return false; |
| 115 | } |
| 116 | return true; |
| 117 | } |
| 118 | } |
| 119 | |
Fabien Siron | 071b927 | 2016-05-08 10:45:52 +0000 | [diff] [blame] | 120 | static bool |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 121 | inet_send_query(struct tcb *tcp, const int fd, const int family, |
| 122 | const int proto) |
Fabien Siron | 071b927 | 2016-05-08 10:45:52 +0000 | [diff] [blame] | 123 | { |
| 124 | struct { |
| 125 | const struct nlmsghdr nlh; |
| 126 | const struct inet_diag_req_v2 idr; |
| 127 | } req = { |
| 128 | .nlh = { |
| 129 | .nlmsg_len = sizeof(req), |
| 130 | .nlmsg_type = SOCK_DIAG_BY_FAMILY, |
| 131 | .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST |
| 132 | }, |
| 133 | .idr = { |
| 134 | .sdiag_family = family, |
| 135 | .sdiag_protocol = proto, |
| 136 | .idiag_states = -1 |
| 137 | } |
| 138 | }; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 139 | return send_query(tcp, fd, &req, sizeof(req)); |
Fabien Siron | 071b927 | 2016-05-08 10:45:52 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 142 | static int |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 143 | inet_parse_response(const void *const data, const int data_len, |
| 144 | const unsigned long inode, void *opaque_data) |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 145 | { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 146 | const char *const proto_name = opaque_data; |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 147 | const struct inet_diag_msg *const diag_msg = data; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 148 | static const char zero_addr[sizeof(struct in6_addr)]; |
| 149 | socklen_t addr_size, text_size; |
| 150 | |
Dmitry V. Levin | cc09ba1 | 2016-01-28 23:46:56 +0000 | [diff] [blame] | 151 | if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg))) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 152 | return -1; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 153 | if (diag_msg->idiag_inode != inode) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 154 | return 0; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 155 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 156 | switch (diag_msg->idiag_family) { |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 157 | case AF_INET: |
| 158 | addr_size = sizeof(struct in_addr); |
| 159 | text_size = INET_ADDRSTRLEN; |
| 160 | break; |
| 161 | case AF_INET6: |
| 162 | addr_size = sizeof(struct in6_addr); |
| 163 | text_size = INET6_ADDRSTRLEN; |
| 164 | break; |
| 165 | default: |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 166 | return -1; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | char src_buf[text_size]; |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 170 | char *details; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 171 | |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 172 | /* open/closing brackets for IPv6 addresses */ |
| 173 | const char *ob = diag_msg->idiag_family == AF_INET6 ? "[" : ""; |
| 174 | const char *cb = diag_msg->idiag_family == AF_INET6 ? "]" : ""; |
| 175 | |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 176 | if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_src, |
| 177 | src_buf, text_size)) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 178 | return -1; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 179 | |
| 180 | if (diag_msg->id.idiag_dport || |
| 181 | memcmp(zero_addr, diag_msg->id.idiag_dst, addr_size)) { |
| 182 | char dst_buf[text_size]; |
| 183 | |
| 184 | if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_dst, |
| 185 | dst_buf, text_size)) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 186 | return -1; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 187 | |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 188 | if (asprintf(&details, "%s:[%s%s%s:%u->%s%s%s:%u]", proto_name, |
| 189 | ob, src_buf, cb, ntohs(diag_msg->id.idiag_sport), |
| 190 | ob, dst_buf, cb, ntohs(diag_msg->id.idiag_dport)) |
| 191 | < 0) |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 192 | return false; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 193 | } else { |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 194 | if (asprintf(&details, "%s:[%s%s%s:%u]", |
| 195 | proto_name, ob, src_buf, cb, |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 196 | ntohs(diag_msg->id.idiag_sport)) < 0) |
| 197 | return false; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 200 | return cache_inode_details(inode, details); |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | static bool |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 204 | receive_responses(struct tcb *tcp, const int fd, const unsigned long inode, |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 205 | const unsigned long expected_msg_type, |
| 206 | int (*parser)(const void *, int, |
| 207 | unsigned long, void *), |
| 208 | void *opaque_data) |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 209 | { |
Dmitry V. Levin | 7b69797 | 2016-05-19 01:23:40 +0000 | [diff] [blame] | 210 | static union { |
| 211 | struct nlmsghdr hdr; |
| 212 | long buf[8192 / sizeof(long)]; |
| 213 | } hdr_buf; |
| 214 | |
Dmitry V. Levin | af534b8 | 2014-11-21 19:59:16 +0000 | [diff] [blame] | 215 | struct sockaddr_nl nladdr = { |
| 216 | .nl_family = AF_NETLINK |
| 217 | }; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 218 | struct iovec iov = { |
Dmitry V. Levin | 7b69797 | 2016-05-19 01:23:40 +0000 | [diff] [blame] | 219 | .iov_base = hdr_buf.buf, |
| 220 | .iov_len = sizeof(hdr_buf.buf) |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 221 | }; |
Dmitry V. Levin | 2215c3e | 2016-01-27 21:35:50 +0000 | [diff] [blame] | 222 | int flags = 0; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 223 | |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 224 | for (;;) { |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 225 | struct msghdr msg = { |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 226 | .msg_name = &nladdr, |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 227 | .msg_namelen = sizeof(nladdr), |
| 228 | .msg_iov = &iov, |
Dmitry V. Levin | af534b8 | 2014-11-21 19:59:16 +0000 | [diff] [blame] | 229 | .msg_iovlen = 1 |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 230 | }; |
| 231 | |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 232 | ssize_t ret = recvmsg(fd, &msg, flags); |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 233 | if (ret < 0) { |
| 234 | if (errno == EINTR) |
| 235 | continue; |
| 236 | return false; |
| 237 | } |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 238 | |
Dmitry V. Levin | 7b69797 | 2016-05-19 01:23:40 +0000 | [diff] [blame] | 239 | const struct nlmsghdr *h = &hdr_buf.hdr; |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 240 | if (!is_nlmsg_ok(h, ret)) |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 241 | return false; |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 242 | for (; is_nlmsg_ok(h, ret); h = NLMSG_NEXT(h, ret)) { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 243 | if (h->nlmsg_type != expected_msg_type) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 244 | return false; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 245 | const int rc = parser(NLMSG_DATA(h), |
| 246 | h->nlmsg_len, inode, opaque_data); |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 247 | if (rc > 0) |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 248 | return true; |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 249 | if (rc < 0) |
| 250 | return false; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 251 | } |
Dmitry V. Levin | 2215c3e | 2016-01-27 21:35:50 +0000 | [diff] [blame] | 252 | flags = MSG_DONTWAIT; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
Masatake YAMATO | f605e92 | 2014-12-10 12:55:06 +0900 | [diff] [blame] | 256 | static bool |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 257 | unix_send_query(struct tcb *tcp, const int fd, const unsigned long inode) |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 258 | { |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 259 | /* |
| 260 | * The kernel bug was fixed in mainline by commit v4.5-rc6~35^2~11 |
| 261 | * and backported to stable/linux-4.4.y by commit v4.4.4~297. |
| 262 | */ |
| 263 | const uint16_t dump_flag = |
| 264 | os_release < KERNEL_VERSION(4, 4, 4) ? NLM_F_DUMP : 0; |
| 265 | |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 266 | struct { |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 267 | const struct nlmsghdr nlh; |
| 268 | const struct unix_diag_req udr; |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 269 | } req = { |
| 270 | .nlh = { |
| 271 | .nlmsg_len = sizeof(req), |
| 272 | .nlmsg_type = SOCK_DIAG_BY_FAMILY, |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 273 | .nlmsg_flags = NLM_F_REQUEST | dump_flag |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 274 | }, |
| 275 | .udr = { |
| 276 | .sdiag_family = AF_UNIX, |
| 277 | .udiag_ino = inode, |
| 278 | .udiag_states = -1, |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 279 | .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER, |
| 280 | .udiag_cookie = { ~0U, ~0U } |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 281 | } |
| 282 | }; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 283 | return send_query(tcp, fd, &req, sizeof(req)); |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 284 | } |
| 285 | |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 286 | static int |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 287 | unix_parse_response(const void *data, const int data_len, |
| 288 | const unsigned long inode, void *opaque_data) |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 289 | { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 290 | const char *proto_name = opaque_data; |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 291 | const struct unix_diag_msg *diag_msg = data; |
| 292 | struct rtattr *attr; |
| 293 | int rta_len = data_len - NLMSG_LENGTH(sizeof(*diag_msg)); |
| 294 | uint32_t peer = 0; |
| 295 | size_t path_len = 0; |
| 296 | char path[UNIX_PATH_MAX + 1]; |
| 297 | |
Dmitry V. Levin | 3d0f55e | 2016-01-24 01:46:40 +0300 | [diff] [blame] | 298 | if (rta_len < 0) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 299 | return -1; |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 300 | if (diag_msg->udiag_ino != inode) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 301 | return 0; |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 302 | if (diag_msg->udiag_family != AF_UNIX) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 303 | return -1; |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 304 | |
| 305 | for (attr = (struct rtattr *) (diag_msg + 1); |
| 306 | RTA_OK(attr, rta_len); |
| 307 | attr = RTA_NEXT(attr, rta_len)) { |
| 308 | switch (attr->rta_type) { |
| 309 | case UNIX_DIAG_NAME: |
| 310 | if (!path_len) { |
| 311 | path_len = RTA_PAYLOAD(attr); |
| 312 | if (path_len > UNIX_PATH_MAX) |
| 313 | path_len = UNIX_PATH_MAX; |
| 314 | memcpy(path, RTA_DATA(attr), path_len); |
| 315 | path[path_len] = '\0'; |
| 316 | } |
| 317 | break; |
| 318 | case UNIX_DIAG_PEER: |
| 319 | if (RTA_PAYLOAD(attr) >= 4) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 320 | peer = *(uint32_t *) RTA_DATA(attr); |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 321 | break; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * print obtained information in the following format: |
| 327 | * "UNIX:[" SELF_INODE [ "->" PEER_INODE ][ "," SOCKET_FILE ] "]" |
| 328 | */ |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 329 | if (!peer && !path_len) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 330 | return -1; |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 331 | |
| 332 | char peer_str[3 + sizeof(peer) * 3]; |
| 333 | if (peer) |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 334 | xsprintf(peer_str, "->%u", peer); |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 335 | else |
| 336 | peer_str[0] = '\0'; |
| 337 | |
| 338 | const char *path_str; |
| 339 | if (path_len) { |
| 340 | char *outstr = alloca(4 * path_len + 4); |
| 341 | |
| 342 | outstr[0] = ','; |
| 343 | if (path[0] == '\0') { |
| 344 | outstr[1] = '@'; |
| 345 | string_quote(path + 1, outstr + 2, |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 346 | path_len - 1, QUOTE_0_TERMINATED, NULL); |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 347 | } else { |
| 348 | string_quote(path, outstr + 1, |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 349 | path_len, QUOTE_0_TERMINATED, NULL); |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 350 | } |
| 351 | path_str = outstr; |
| 352 | } else { |
| 353 | path_str = ""; |
| 354 | } |
| 355 | |
| 356 | char *details; |
| 357 | if (asprintf(&details, "%s:[%lu%s%s]", proto_name, inode, |
| 358 | peer_str, path_str) < 0) |
| 359 | return -1; |
| 360 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 361 | return cache_inode_details(inode, details); |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | static bool |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 365 | netlink_send_query(struct tcb *tcp, const int fd, const unsigned long inode) |
Fabien Siron | 814c0d5 | 2016-05-17 10:08:47 +0000 | [diff] [blame] | 366 | { |
| 367 | struct { |
| 368 | const struct nlmsghdr nlh; |
| 369 | const struct netlink_diag_req ndr; |
| 370 | } req = { |
| 371 | .nlh = { |
| 372 | .nlmsg_len = sizeof(req), |
| 373 | .nlmsg_type = SOCK_DIAG_BY_FAMILY, |
| 374 | .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST |
| 375 | }, |
| 376 | .ndr = { |
| 377 | .sdiag_family = AF_NETLINK, |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 378 | .sdiag_protocol = NDIAG_PROTO_ALL |
Fabien Siron | 814c0d5 | 2016-05-17 10:08:47 +0000 | [diff] [blame] | 379 | } |
| 380 | }; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 381 | return send_query(tcp, fd, &req, sizeof(req)); |
Fabien Siron | 814c0d5 | 2016-05-17 10:08:47 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | static int |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 385 | netlink_parse_response(const void *data, const int data_len, |
| 386 | const unsigned long inode, void *opaque_data) |
Fabien Siron | 814c0d5 | 2016-05-17 10:08:47 +0000 | [diff] [blame] | 387 | { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 388 | const char *proto_name = opaque_data; |
Fabien Siron | 814c0d5 | 2016-05-17 10:08:47 +0000 | [diff] [blame] | 389 | const struct netlink_diag_msg *const diag_msg = data; |
| 390 | const char *netlink_proto; |
| 391 | char *details; |
| 392 | |
| 393 | if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg))) |
| 394 | return -1; |
| 395 | if (diag_msg->ndiag_ino != inode) |
| 396 | return 0; |
| 397 | |
| 398 | if (diag_msg->ndiag_family != AF_NETLINK) |
| 399 | return -1; |
| 400 | |
| 401 | netlink_proto = xlookup(netlink_protocols, |
| 402 | diag_msg->ndiag_protocol); |
| 403 | |
| 404 | if (netlink_proto) { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 405 | netlink_proto = STR_STRIP_PREFIX(netlink_proto, "NETLINK_"); |
Fabien Siron | 814c0d5 | 2016-05-17 10:08:47 +0000 | [diff] [blame] | 406 | if (asprintf(&details, "%s:[%s:%u]", proto_name, |
| 407 | netlink_proto, diag_msg->ndiag_portid) < 0) |
| 408 | return -1; |
| 409 | } else { |
| 410 | if (asprintf(&details, "%s:[%u]", proto_name, |
| 411 | (unsigned) diag_msg->ndiag_protocol) < 0) |
| 412 | return -1; |
| 413 | } |
| 414 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 415 | return cache_inode_details(inode, details); |
Fabien Siron | 814c0d5 | 2016-05-17 10:08:47 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 418 | static const char * |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 419 | unix_get(struct tcb *tcp, const int fd, const int family, const int proto, |
| 420 | const unsigned long inode, const char *name) |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 421 | { |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 422 | return unix_send_query(tcp, fd, inode) |
| 423 | && receive_responses(tcp, fd, inode, SOCK_DIAG_BY_FAMILY, |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 424 | unix_parse_response, (void *) name) |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 425 | ? get_sockaddr_by_inode_cached(inode) : NULL; |
Masatake YAMATO | f605e92 | 2014-12-10 12:55:06 +0900 | [diff] [blame] | 426 | } |
| 427 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 428 | static const char * |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 429 | inet_get(struct tcb *tcp, const int fd, const int family, const int protocol, |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 430 | const unsigned long inode, const char *proto_name) |
Dmitry V. Levin | bcf5975 | 2016-02-10 17:39:57 +0000 | [diff] [blame] | 431 | { |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 432 | return inet_send_query(tcp, fd, family, protocol) |
| 433 | && receive_responses(tcp, fd, inode, SOCK_DIAG_BY_FAMILY, |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 434 | inet_parse_response, (void *) proto_name) |
| 435 | ? get_sockaddr_by_inode_cached(inode) : NULL; |
Dmitry V. Levin | bcf5975 | 2016-02-10 17:39:57 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 438 | static const char * |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 439 | netlink_get(struct tcb *tcp, const int fd, const int family, const int protocol, |
| 440 | const unsigned long inode, const char *proto_name) |
Fabien Siron | 814c0d5 | 2016-05-17 10:08:47 +0000 | [diff] [blame] | 441 | { |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 442 | return netlink_send_query(tcp, fd, inode) |
| 443 | && receive_responses(tcp, fd, inode, SOCK_DIAG_BY_FAMILY, |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 444 | netlink_parse_response, |
| 445 | (void *) proto_name) |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 446 | ? get_sockaddr_by_inode_cached(inode) : NULL; |
Fabien Siron | 814c0d5 | 2016-05-17 10:08:47 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Fabien Siron | 802cc28 | 2016-06-17 16:29:53 +0000 | [diff] [blame] | 449 | static const struct { |
| 450 | const char *const name; |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 451 | const char * (*const get)(struct tcb *, int fd, int family, |
| 452 | int protocol, unsigned long inode, |
| 453 | const char *proto_name); |
| 454 | int family; |
| 455 | int proto; |
Fabien Siron | 802cc28 | 2016-06-17 16:29:53 +0000 | [diff] [blame] | 456 | } protocols[] = { |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 457 | [SOCK_PROTO_UNIX] = { "UNIX", unix_get, AF_UNIX}, |
| 458 | /* |
| 459 | * inet_diag handlers are currently implemented only for TCP, |
| 460 | * UDP(lite), SCTP, RAW, and DCCP, but we try to resolve it for all |
| 461 | * protocols anyway, just in case. |
| 462 | */ |
| 463 | [SOCK_PROTO_TCP] = |
| 464 | { "TCP", inet_get, AF_INET, IPPROTO_TCP }, |
| 465 | [SOCK_PROTO_UDP] = |
| 466 | { "UDP", inet_get, AF_INET, IPPROTO_UDP }, |
| 467 | [SOCK_PROTO_UDPLITE] = |
| 468 | { "UDPLITE", inet_get, AF_INET, IPPROTO_UDPLITE }, |
| 469 | [SOCK_PROTO_DCCP] = |
| 470 | { "DCCP", inet_get, AF_INET, IPPROTO_DCCP }, |
| 471 | [SOCK_PROTO_SCTP] = |
| 472 | { "SCTP", inet_get, AF_INET, IPPROTO_SCTP }, |
| 473 | [SOCK_PROTO_L2TP_IP] = |
| 474 | { "L2TP/IP", inet_get, AF_INET, IPPROTO_L2TP }, |
| 475 | [SOCK_PROTO_PING] = |
| 476 | { "PING", inet_get, AF_INET, IPPROTO_ICMP }, |
| 477 | [SOCK_PROTO_RAW] = |
| 478 | { "RAW", inet_get, AF_INET, IPPROTO_RAW }, |
| 479 | [SOCK_PROTO_TCPv6] = |
| 480 | { "TCPv6", inet_get, AF_INET6, IPPROTO_TCP }, |
| 481 | [SOCK_PROTO_UDPv6] = |
| 482 | { "UDPv6", inet_get, AF_INET6, IPPROTO_UDP }, |
| 483 | [SOCK_PROTO_UDPLITEv6] = |
| 484 | { "UDPLITEv6", inet_get, AF_INET6, IPPROTO_UDPLITE }, |
| 485 | [SOCK_PROTO_DCCPv6] = |
| 486 | { "DCCPv6", inet_get, AF_INET6, IPPROTO_DCCP }, |
| 487 | [SOCK_PROTO_SCTPv6] = |
| 488 | { "SCTPv6", inet_get, AF_INET6, IPPROTO_SCTP }, |
| 489 | [SOCK_PROTO_L2TP_IPv6] = |
| 490 | { "L2TP/IPv6", inet_get, AF_INET6, IPPROTO_L2TP }, |
| 491 | [SOCK_PROTO_PINGv6] = |
| 492 | { "PINGv6", inet_get, AF_INET6, IPPROTO_ICMP }, |
| 493 | [SOCK_PROTO_RAWv6] = |
| 494 | { "RAWv6", inet_get, AF_INET6, IPPROTO_RAW }, |
| 495 | [SOCK_PROTO_NETLINK] = { "NETLINK", netlink_get, AF_NETLINK }, |
Fabien Siron | 802cc28 | 2016-06-17 16:29:53 +0000 | [diff] [blame] | 496 | }; |
| 497 | |
| 498 | enum sock_proto |
| 499 | get_proto_by_name(const char *const name) |
| 500 | { |
| 501 | unsigned int i; |
| 502 | for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1; |
| 503 | i < ARRAY_SIZE(protocols); ++i) { |
| 504 | if (protocols[i].name && !strcmp(name, protocols[i].name)) |
| 505 | return (enum sock_proto) i; |
| 506 | } |
| 507 | return SOCK_PROTO_UNKNOWN; |
| 508 | } |
| 509 | |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 510 | int |
| 511 | get_family_by_proto(enum sock_proto proto) |
| 512 | { |
| 513 | if ((size_t) proto < ARRAY_SIZE(protocols)) |
| 514 | return protocols[proto].family; |
| 515 | |
| 516 | return AF_UNSPEC; |
| 517 | } |
| 518 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 519 | static const char * |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 520 | get_sockaddr_by_inode_uncached(struct tcb *tcp, const unsigned long inode, |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 521 | const enum sock_proto proto) |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 522 | { |
Fabien Siron | 802cc28 | 2016-06-17 16:29:53 +0000 | [diff] [blame] | 523 | if ((unsigned int) proto >= ARRAY_SIZE(protocols) || |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 524 | (proto != SOCK_PROTO_UNKNOWN && !protocols[proto].get)) |
| 525 | return NULL; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 526 | |
Dmitry V. Levin | bcf5975 | 2016-02-10 17:39:57 +0000 | [diff] [blame] | 527 | const int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG); |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 528 | if (fd < 0) |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 529 | return NULL; |
| 530 | const char *details = NULL; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 531 | |
Fabien Siron | 802cc28 | 2016-06-17 16:29:53 +0000 | [diff] [blame] | 532 | if (proto != SOCK_PROTO_UNKNOWN) { |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 533 | details = protocols[proto].get(tcp, fd, protocols[proto].family, |
| 534 | protocols[proto].proto, inode, |
| 535 | protocols[proto].name); |
Masatake YAMATO | f605e92 | 2014-12-10 12:55:06 +0900 | [diff] [blame] | 536 | } else { |
Fabien Siron | 802cc28 | 2016-06-17 16:29:53 +0000 | [diff] [blame] | 537 | unsigned int i; |
| 538 | for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1; |
| 539 | i < ARRAY_SIZE(protocols); ++i) { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 540 | if (!protocols[i].get) |
Fabien Siron | 802cc28 | 2016-06-17 16:29:53 +0000 | [diff] [blame] | 541 | continue; |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 542 | details = protocols[i].get(tcp, fd, |
| 543 | protocols[proto].family, |
| 544 | protocols[proto].proto, |
| 545 | inode, |
| 546 | protocols[proto].name); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 547 | if (details) |
Dmitry V. Levin | 959205c | 2014-12-26 23:29:26 +0000 | [diff] [blame] | 548 | break; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 549 | } |
| 550 | } |
Dmitry V. Levin | 959205c | 2014-12-26 23:29:26 +0000 | [diff] [blame] | 551 | |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 552 | close(fd); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 553 | return details; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 554 | } |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 555 | |
| 556 | static bool |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 557 | print_sockaddr_by_inode_uncached(struct tcb *tcp, const unsigned long inode, |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 558 | const enum sock_proto proto) |
| 559 | { |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 560 | const char *details = get_sockaddr_by_inode_uncached(tcp, inode, proto); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 561 | |
| 562 | if (details) { |
| 563 | tprints(details); |
| 564 | return true; |
| 565 | } |
| 566 | |
| 567 | if ((unsigned int) proto < ARRAY_SIZE(protocols) && |
| 568 | protocols[proto].name) { |
| 569 | tprintf("%s:[%lu]", protocols[proto].name, inode); |
| 570 | return true; |
| 571 | } |
| 572 | |
| 573 | return false; |
| 574 | } |
| 575 | |
| 576 | /* Given an inode number of a socket, return its protocol details. */ |
| 577 | const char * |
| 578 | get_sockaddr_by_inode(struct tcb *const tcp, const int fd, |
| 579 | const unsigned long inode) |
| 580 | { |
| 581 | const char *details = get_sockaddr_by_inode_cached(inode); |
| 582 | return details ? details : |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 583 | get_sockaddr_by_inode_uncached(tcp, inode, getfdproto(tcp, fd)); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /* Given an inode number of a socket, print out its protocol details. */ |
| 587 | bool |
| 588 | print_sockaddr_by_inode(struct tcb *const tcp, const int fd, |
| 589 | const unsigned long inode) |
| 590 | { |
| 591 | return print_sockaddr_by_inode_cached(inode) ? true : |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 592 | print_sockaddr_by_inode_uncached(tcp, inode, |
| 593 | getfdproto(tcp, fd)); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | #ifdef HAVE_LINUX_GENETLINK_H |
| 597 | /* |
| 598 | * Managing the cache for decoding communications of Netlink GENERIC protocol |
| 599 | * |
| 600 | * As name shown Netlink GENERIC protocol is generic protocol. The |
| 601 | * numbers of msg types used in the protocol are not defined |
| 602 | * statically. Kernel defines them on demand. So the xlat converted |
| 603 | * from header files doesn't help for decoding the protocol. Following |
| 604 | * codes are building xlat(dyxlat) at runtime. |
| 605 | */ |
| 606 | static bool |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 607 | genl_send_dump_families(struct tcb *tcp, const int fd) |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 608 | { |
| 609 | struct { |
| 610 | const struct nlmsghdr nlh; |
| 611 | struct genlmsghdr gnlh; |
| 612 | } req = { |
| 613 | .nlh = { |
| 614 | .nlmsg_len = sizeof(req), |
| 615 | .nlmsg_type = GENL_ID_CTRL, |
| 616 | .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, |
| 617 | }, |
| 618 | .gnlh = { |
| 619 | .cmd = CTRL_CMD_GETFAMILY, |
| 620 | } |
| 621 | }; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 622 | return send_query(tcp, fd, &req, sizeof(req)); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | static int |
| 626 | genl_parse_families_response(const void *const data, |
| 627 | const int data_len, const unsigned long inode, |
| 628 | void *opaque_data) |
| 629 | { |
| 630 | struct dyxlat *const dyxlat = opaque_data; |
| 631 | const struct genlmsghdr *const gnlh = data; |
| 632 | struct rtattr *attr; |
| 633 | int rta_len = data_len - NLMSG_LENGTH(sizeof(*gnlh)); |
| 634 | |
| 635 | char *name = NULL; |
| 636 | unsigned int name_len = 0; |
| 637 | uint16_t *id = NULL; |
| 638 | |
| 639 | if (rta_len < 0) |
| 640 | return -1; |
| 641 | if (gnlh->cmd != CTRL_CMD_NEWFAMILY) |
| 642 | return -1; |
| 643 | if (gnlh->version != 2) |
| 644 | return -1; |
| 645 | |
| 646 | for (attr = (struct rtattr *) (gnlh + 1); |
| 647 | RTA_OK(attr, rta_len); |
| 648 | attr = RTA_NEXT(attr, rta_len)) { |
| 649 | switch (attr->rta_type) { |
| 650 | case CTRL_ATTR_FAMILY_NAME: |
| 651 | if (!name) { |
| 652 | name = RTA_DATA(attr); |
| 653 | name_len = RTA_PAYLOAD(attr); |
| 654 | } |
| 655 | break; |
| 656 | case CTRL_ATTR_FAMILY_ID: |
| 657 | if (!id && RTA_PAYLOAD(attr) == sizeof(*id)) |
| 658 | id = RTA_DATA(attr); |
| 659 | break; |
| 660 | } |
| 661 | |
| 662 | if (name && id) { |
| 663 | dyxlat_add_pair(dyxlat, *id, name, name_len); |
| 664 | name = NULL; |
| 665 | id = NULL; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | const struct xlat * |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 673 | genl_families_xlat(struct tcb *tcp) |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 674 | { |
| 675 | static struct dyxlat *dyxlat; |
| 676 | |
| 677 | if (!dyxlat) { |
| 678 | dyxlat = dyxlat_alloc(32); |
| 679 | |
| 680 | int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC); |
| 681 | if (fd < 0) |
| 682 | goto out; |
| 683 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 684 | if (genl_send_dump_families(tcp, fd)) |
| 685 | receive_responses(tcp, fd, 0, GENL_ID_CTRL, |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 686 | genl_parse_families_response, dyxlat); |
| 687 | close(fd); |
| 688 | } |
| 689 | |
| 690 | out: |
| 691 | return dyxlat_get(dyxlat); |
| 692 | } |
| 693 | |
| 694 | #else /* !HAVE_LINUX_GENETLINK_H */ |
| 695 | |
| 696 | const struct xlat * |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 697 | genl_families_xlat(struct tcb *tcp) |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 698 | { |
| 699 | return NULL; |
| 700 | } |
| 701 | #endif |