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 | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame] | 4 | * Copyright (c) 2014-2017 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 | |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 49 | typedef struct { |
| 50 | unsigned long inode; |
| 51 | char *details; |
| 52 | } cache_entry; |
| 53 | |
| 54 | #define CACHE_SIZE 1024U |
| 55 | static cache_entry cache[CACHE_SIZE]; |
| 56 | #define CACHE_MASK (CACHE_SIZE - 1) |
| 57 | |
| 58 | static int |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 59 | cache_inode_details(const unsigned long inode, char *const details) |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 60 | { |
| 61 | cache_entry *e = &cache[inode & CACHE_MASK]; |
| 62 | free(e->details); |
| 63 | e->inode = inode; |
| 64 | e->details = details; |
| 65 | |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 66 | return 1; |
| 67 | } |
| 68 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 69 | static const char * |
| 70 | get_sockaddr_by_inode_cached(const unsigned long inode) |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 71 | { |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 72 | const cache_entry *const e = &cache[inode & CACHE_MASK]; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 73 | return (e && inode == e->inode) ? e->details : NULL; |
| 74 | } |
| 75 | |
| 76 | static bool |
| 77 | print_sockaddr_by_inode_cached(const unsigned long inode) |
| 78 | { |
| 79 | const char *const details = get_sockaddr_by_inode_cached(inode); |
| 80 | if (details) { |
| 81 | tprints(details); |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 82 | return true; |
| 83 | } |
| 84 | return false; |
| 85 | } |
| 86 | |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 87 | static bool |
Fabien Siron | 071b927 | 2016-05-08 10:45:52 +0000 | [diff] [blame] | 88 | send_query(const int fd, void *req, size_t req_size) |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 89 | { |
Dmitry V. Levin | af534b8 | 2014-11-21 19:59:16 +0000 | [diff] [blame] | 90 | struct sockaddr_nl nladdr = { |
| 91 | .nl_family = AF_NETLINK |
| 92 | }; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 93 | struct iovec iov = { |
Fabien Siron | 071b927 | 2016-05-08 10:45:52 +0000 | [diff] [blame] | 94 | .iov_base = req, |
| 95 | .iov_len = req_size |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 96 | }; |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 97 | const struct msghdr msg = { |
| 98 | .msg_name = &nladdr, |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 99 | .msg_namelen = sizeof(nladdr), |
| 100 | .msg_iov = &iov, |
| 101 | .msg_iovlen = 1 |
| 102 | }; |
| 103 | |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 104 | for (;;) { |
| 105 | if (sendmsg(fd, &msg, 0) < 0) { |
| 106 | if (errno == EINTR) |
| 107 | continue; |
| 108 | return false; |
| 109 | } |
| 110 | return true; |
| 111 | } |
| 112 | } |
| 113 | |
Fabien Siron | 071b927 | 2016-05-08 10:45:52 +0000 | [diff] [blame] | 114 | static bool |
| 115 | inet_send_query(const int fd, const int family, const int proto) |
| 116 | { |
| 117 | struct { |
| 118 | const struct nlmsghdr nlh; |
| 119 | const struct inet_diag_req_v2 idr; |
| 120 | } req = { |
| 121 | .nlh = { |
| 122 | .nlmsg_len = sizeof(req), |
| 123 | .nlmsg_type = SOCK_DIAG_BY_FAMILY, |
| 124 | .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST |
| 125 | }, |
| 126 | .idr = { |
| 127 | .sdiag_family = family, |
| 128 | .sdiag_protocol = proto, |
| 129 | .idiag_states = -1 |
| 130 | } |
| 131 | }; |
| 132 | return send_query(fd, &req, sizeof(req)); |
| 133 | } |
| 134 | |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 135 | static int |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 136 | inet_parse_response(const void *const data, const int data_len, |
| 137 | const unsigned long inode, void *opaque_data) |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 138 | { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 139 | const char *const proto_name = opaque_data; |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 140 | const struct inet_diag_msg *const diag_msg = data; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 141 | static const char zero_addr[sizeof(struct in6_addr)]; |
| 142 | socklen_t addr_size, text_size; |
| 143 | |
Dmitry V. Levin | cc09ba1 | 2016-01-28 23:46:56 +0000 | [diff] [blame] | 144 | if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg))) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 145 | return -1; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 146 | if (diag_msg->idiag_inode != inode) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 147 | return 0; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 148 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 149 | switch (diag_msg->idiag_family) { |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 150 | case AF_INET: |
| 151 | addr_size = sizeof(struct in_addr); |
| 152 | text_size = INET_ADDRSTRLEN; |
| 153 | break; |
| 154 | case AF_INET6: |
| 155 | addr_size = sizeof(struct in6_addr); |
| 156 | text_size = INET6_ADDRSTRLEN; |
| 157 | break; |
| 158 | default: |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 159 | return -1; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | char src_buf[text_size]; |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 163 | char *details; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 164 | |
| 165 | if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_src, |
| 166 | src_buf, text_size)) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 167 | return -1; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 168 | |
| 169 | if (diag_msg->id.idiag_dport || |
| 170 | memcmp(zero_addr, diag_msg->id.idiag_dst, addr_size)) { |
| 171 | char dst_buf[text_size]; |
| 172 | |
| 173 | if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_dst, |
| 174 | dst_buf, text_size)) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 175 | return -1; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 176 | |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 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) |
| 180 | return false; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 181 | } else { |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 182 | if (asprintf(&details, "%s:[%s:%u]", proto_name, src_buf, |
| 183 | ntohs(diag_msg->id.idiag_sport)) < 0) |
| 184 | return false; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 187 | return cache_inode_details(inode, details); |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static bool |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 191 | receive_responses(const int fd, const unsigned long inode, |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 192 | const unsigned long expected_msg_type, |
| 193 | int (*parser)(const void *, int, |
| 194 | unsigned long, void *), |
| 195 | void *opaque_data) |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 196 | { |
Dmitry V. Levin | 7b69797 | 2016-05-19 01:23:40 +0000 | [diff] [blame] | 197 | static union { |
| 198 | struct nlmsghdr hdr; |
| 199 | long buf[8192 / sizeof(long)]; |
| 200 | } hdr_buf; |
| 201 | |
Dmitry V. Levin | af534b8 | 2014-11-21 19:59:16 +0000 | [diff] [blame] | 202 | struct sockaddr_nl nladdr = { |
| 203 | .nl_family = AF_NETLINK |
| 204 | }; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 205 | struct iovec iov = { |
Dmitry V. Levin | 7b69797 | 2016-05-19 01:23:40 +0000 | [diff] [blame] | 206 | .iov_base = hdr_buf.buf, |
| 207 | .iov_len = sizeof(hdr_buf.buf) |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 208 | }; |
Dmitry V. Levin | 2215c3e | 2016-01-27 21:35:50 +0000 | [diff] [blame] | 209 | int flags = 0; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 210 | |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 211 | for (;;) { |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 212 | struct msghdr msg = { |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 213 | .msg_name = &nladdr, |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 214 | .msg_namelen = sizeof(nladdr), |
| 215 | .msg_iov = &iov, |
Dmitry V. Levin | af534b8 | 2014-11-21 19:59:16 +0000 | [diff] [blame] | 216 | .msg_iovlen = 1 |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 219 | ssize_t ret = recvmsg(fd, &msg, flags); |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 220 | if (ret < 0) { |
| 221 | if (errno == EINTR) |
| 222 | continue; |
| 223 | return false; |
| 224 | } |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 225 | |
Dmitry V. Levin | 7b69797 | 2016-05-19 01:23:40 +0000 | [diff] [blame] | 226 | const struct nlmsghdr *h = &hdr_buf.hdr; |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 227 | if (!NLMSG_OK(h, ret)) |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 228 | return false; |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 229 | for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 230 | if (h->nlmsg_type != expected_msg_type) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 231 | return false; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 232 | const int rc = parser(NLMSG_DATA(h), |
| 233 | h->nlmsg_len, inode, opaque_data); |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 234 | if (rc > 0) |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 235 | return true; |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 236 | if (rc < 0) |
| 237 | return false; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 238 | } |
Dmitry V. Levin | 2215c3e | 2016-01-27 21:35:50 +0000 | [diff] [blame] | 239 | flags = MSG_DONTWAIT; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
Masatake YAMATO | f605e92 | 2014-12-10 12:55:06 +0900 | [diff] [blame] | 243 | static bool |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 244 | unix_send_query(const int fd, const unsigned long inode) |
| 245 | { |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 246 | struct { |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 247 | const struct nlmsghdr nlh; |
| 248 | const struct unix_diag_req udr; |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 249 | } req = { |
| 250 | .nlh = { |
| 251 | .nlmsg_len = sizeof(req), |
| 252 | .nlmsg_type = SOCK_DIAG_BY_FAMILY, |
Dmitry V. Levin | 10c61e3 | 2016-02-19 01:30:34 +0000 | [diff] [blame] | 253 | .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 254 | }, |
| 255 | .udr = { |
| 256 | .sdiag_family = AF_UNIX, |
| 257 | .udiag_ino = inode, |
| 258 | .udiag_states = -1, |
Dmitry V. Levin | 10c61e3 | 2016-02-19 01:30:34 +0000 | [diff] [blame] | 259 | .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 260 | } |
| 261 | }; |
Fabien Siron | 071b927 | 2016-05-08 10:45:52 +0000 | [diff] [blame] | 262 | return send_query(fd, &req, sizeof(req)); |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 263 | } |
| 264 | |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 265 | static int |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 266 | unix_parse_response(const void *data, const int data_len, |
| 267 | const unsigned long inode, void *opaque_data) |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 268 | { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 269 | const char *proto_name = opaque_data; |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 270 | const struct unix_diag_msg *diag_msg = data; |
| 271 | struct rtattr *attr; |
| 272 | int rta_len = data_len - NLMSG_LENGTH(sizeof(*diag_msg)); |
| 273 | uint32_t peer = 0; |
| 274 | size_t path_len = 0; |
| 275 | char path[UNIX_PATH_MAX + 1]; |
| 276 | |
Dmitry V. Levin | 3d0f55e | 2016-01-24 01:46:40 +0300 | [diff] [blame] | 277 | if (rta_len < 0) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 278 | return -1; |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 279 | if (diag_msg->udiag_ino != inode) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 280 | return 0; |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 281 | if (diag_msg->udiag_family != AF_UNIX) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 282 | return -1; |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 283 | |
| 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) { |
| 288 | case UNIX_DIAG_NAME: |
| 289 | if (!path_len) { |
| 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'; |
| 295 | } |
| 296 | break; |
| 297 | case UNIX_DIAG_PEER: |
| 298 | if (RTA_PAYLOAD(attr) >= 4) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 299 | peer = *(uint32_t *) RTA_DATA(attr); |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 300 | break; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | * print obtained information in the following format: |
| 306 | * "UNIX:[" SELF_INODE [ "->" PEER_INODE ][ "," SOCKET_FILE ] "]" |
| 307 | */ |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 308 | if (!peer && !path_len) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 309 | return -1; |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 310 | |
| 311 | char peer_str[3 + sizeof(peer) * 3]; |
| 312 | if (peer) |
| 313 | snprintf(peer_str, sizeof(peer_str), "->%u", peer); |
| 314 | else |
| 315 | peer_str[0] = '\0'; |
| 316 | |
| 317 | const char *path_str; |
| 318 | if (path_len) { |
| 319 | char *outstr = alloca(4 * path_len + 4); |
| 320 | |
| 321 | outstr[0] = ','; |
| 322 | if (path[0] == '\0') { |
| 323 | outstr[1] = '@'; |
| 324 | string_quote(path + 1, outstr + 2, |
| 325 | path_len - 1, QUOTE_0_TERMINATED); |
| 326 | } else { |
| 327 | string_quote(path, outstr + 1, |
| 328 | path_len, QUOTE_0_TERMINATED); |
| 329 | } |
| 330 | path_str = outstr; |
| 331 | } else { |
| 332 | path_str = ""; |
| 333 | } |
| 334 | |
| 335 | char *details; |
| 336 | if (asprintf(&details, "%s:[%lu%s%s]", proto_name, inode, |
| 337 | peer_str, path_str) < 0) |
| 338 | return -1; |
| 339 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 340 | return cache_inode_details(inode, details); |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static bool |
Fabien Siron | 814c0d5 | 2016-05-17 10:08:47 +0000 | [diff] [blame] | 344 | netlink_send_query(const int fd, const unsigned long inode) |
| 345 | { |
| 346 | struct { |
| 347 | const struct nlmsghdr nlh; |
| 348 | const struct netlink_diag_req ndr; |
| 349 | } req = { |
| 350 | .nlh = { |
| 351 | .nlmsg_len = sizeof(req), |
| 352 | .nlmsg_type = SOCK_DIAG_BY_FAMILY, |
| 353 | .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST |
| 354 | }, |
| 355 | .ndr = { |
| 356 | .sdiag_family = AF_NETLINK, |
| 357 | .sdiag_protocol = NDIAG_PROTO_ALL, |
| 358 | .ndiag_show = NDIAG_SHOW_MEMINFO |
| 359 | } |
| 360 | }; |
| 361 | return send_query(fd, &req, sizeof(req)); |
| 362 | } |
| 363 | |
| 364 | static int |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 365 | netlink_parse_response(const void *data, const int data_len, |
| 366 | const unsigned long inode, void *opaque_data) |
Fabien Siron | 814c0d5 | 2016-05-17 10:08:47 +0000 | [diff] [blame] | 367 | { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 368 | const char *proto_name = opaque_data; |
Fabien Siron | 814c0d5 | 2016-05-17 10:08:47 +0000 | [diff] [blame] | 369 | const struct netlink_diag_msg *const diag_msg = data; |
| 370 | const char *netlink_proto; |
| 371 | char *details; |
| 372 | |
| 373 | if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg))) |
| 374 | return -1; |
| 375 | if (diag_msg->ndiag_ino != inode) |
| 376 | return 0; |
| 377 | |
| 378 | if (diag_msg->ndiag_family != AF_NETLINK) |
| 379 | return -1; |
| 380 | |
| 381 | netlink_proto = xlookup(netlink_protocols, |
| 382 | diag_msg->ndiag_protocol); |
| 383 | |
| 384 | if (netlink_proto) { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 385 | netlink_proto = STR_STRIP_PREFIX(netlink_proto, "NETLINK_"); |
Fabien Siron | 814c0d5 | 2016-05-17 10:08:47 +0000 | [diff] [blame] | 386 | if (asprintf(&details, "%s:[%s:%u]", proto_name, |
| 387 | netlink_proto, diag_msg->ndiag_portid) < 0) |
| 388 | return -1; |
| 389 | } else { |
| 390 | if (asprintf(&details, "%s:[%u]", proto_name, |
| 391 | (unsigned) diag_msg->ndiag_protocol) < 0) |
| 392 | return -1; |
| 393 | } |
| 394 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 395 | return cache_inode_details(inode, details); |
Fabien Siron | 814c0d5 | 2016-05-17 10:08:47 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 398 | static const char * |
| 399 | unix_get(const int fd, const unsigned long inode) |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 400 | { |
| 401 | return unix_send_query(fd, inode) |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 402 | && receive_responses(fd, inode, SOCK_DIAG_BY_FAMILY, |
| 403 | unix_parse_response, (void *) "UNIX") |
| 404 | ? get_sockaddr_by_inode_cached(inode) : NULL; |
Masatake YAMATO | f605e92 | 2014-12-10 12:55:06 +0900 | [diff] [blame] | 405 | } |
| 406 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 407 | static const char * |
| 408 | inet_get(const int fd, const int family, const int protocol, |
| 409 | const unsigned long inode, const char *proto_name) |
Dmitry V. Levin | bcf5975 | 2016-02-10 17:39:57 +0000 | [diff] [blame] | 410 | { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 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; |
Dmitry V. Levin | bcf5975 | 2016-02-10 17:39:57 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 417 | static const char * |
| 418 | tcp_v4_get(const int fd, const unsigned long inode) |
Dmitry V. Levin | bcf5975 | 2016-02-10 17:39:57 +0000 | [diff] [blame] | 419 | { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 420 | return inet_get(fd, AF_INET, IPPROTO_TCP, inode, "TCP"); |
Dmitry V. Levin | bcf5975 | 2016-02-10 17:39:57 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 423 | static const char * |
| 424 | udp_v4_get(const int fd, const unsigned long inode) |
Dmitry V. Levin | bcf5975 | 2016-02-10 17:39:57 +0000 | [diff] [blame] | 425 | { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 426 | return inet_get(fd, AF_INET, IPPROTO_UDP, inode, "UDP"); |
Dmitry V. Levin | bcf5975 | 2016-02-10 17:39:57 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 429 | static const char * |
| 430 | tcp_v6_get(const int fd, const unsigned long inode) |
Dmitry V. Levin | bcf5975 | 2016-02-10 17:39:57 +0000 | [diff] [blame] | 431 | { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 432 | return inet_get(fd, AF_INET6, IPPROTO_TCP, inode, "TCPv6"); |
Dmitry V. Levin | bcf5975 | 2016-02-10 17:39:57 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 435 | static const char * |
| 436 | udp_v6_get(const int fd, const unsigned long inode) |
| 437 | { |
| 438 | return inet_get(fd, AF_INET6, IPPROTO_UDP, inode, "UDPv6"); |
| 439 | } |
| 440 | |
| 441 | static const char * |
| 442 | netlink_get(const int fd, const unsigned long inode) |
Fabien Siron | 814c0d5 | 2016-05-17 10:08:47 +0000 | [diff] [blame] | 443 | { |
| 444 | return netlink_send_query(fd, inode) |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 445 | && receive_responses(fd, inode, SOCK_DIAG_BY_FAMILY, |
| 446 | netlink_parse_response, (void *) "NETLINK") |
| 447 | ? get_sockaddr_by_inode_cached(inode) : NULL; |
Fabien Siron | 814c0d5 | 2016-05-17 10:08:47 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Fabien Siron | 802cc28 | 2016-06-17 16:29:53 +0000 | [diff] [blame] | 450 | static const struct { |
| 451 | const char *const name; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 452 | const char * (*const get)(int, unsigned long); |
Fabien Siron | 802cc28 | 2016-06-17 16:29:53 +0000 | [diff] [blame] | 453 | } protocols[] = { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 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 } |
Fabien Siron | 802cc28 | 2016-06-17 16:29:53 +0000 | [diff] [blame] | 460 | }; |
| 461 | |
| 462 | enum sock_proto |
| 463 | get_proto_by_name(const char *const name) |
| 464 | { |
| 465 | unsigned int i; |
| 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; |
| 470 | } |
| 471 | return SOCK_PROTO_UNKNOWN; |
| 472 | } |
| 473 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 474 | static const char * |
| 475 | get_sockaddr_by_inode_uncached(const unsigned long inode, |
| 476 | const enum sock_proto proto) |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 477 | { |
Fabien Siron | 802cc28 | 2016-06-17 16:29:53 +0000 | [diff] [blame] | 478 | if ((unsigned int) proto >= ARRAY_SIZE(protocols) || |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 479 | (proto != SOCK_PROTO_UNKNOWN && !protocols[proto].get)) |
| 480 | return NULL; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 481 | |
Dmitry V. Levin | bcf5975 | 2016-02-10 17:39:57 +0000 | [diff] [blame] | 482 | const int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG); |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 483 | if (fd < 0) |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 484 | return NULL; |
| 485 | const char *details = NULL; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 486 | |
Fabien Siron | 802cc28 | 2016-06-17 16:29:53 +0000 | [diff] [blame] | 487 | if (proto != SOCK_PROTO_UNKNOWN) { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 488 | details = protocols[proto].get(fd, inode); |
Masatake YAMATO | f605e92 | 2014-12-10 12:55:06 +0900 | [diff] [blame] | 489 | } else { |
Fabien Siron | 802cc28 | 2016-06-17 16:29:53 +0000 | [diff] [blame] | 490 | unsigned int i; |
| 491 | for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1; |
| 492 | i < ARRAY_SIZE(protocols); ++i) { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 493 | if (!protocols[i].get) |
Fabien Siron | 802cc28 | 2016-06-17 16:29:53 +0000 | [diff] [blame] | 494 | continue; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 495 | details = protocols[i].get(fd, inode); |
| 496 | if (details) |
Dmitry V. Levin | 959205c | 2014-12-26 23:29:26 +0000 | [diff] [blame] | 497 | break; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 498 | } |
| 499 | } |
Dmitry V. Levin | 959205c | 2014-12-26 23:29:26 +0000 | [diff] [blame] | 500 | |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 501 | close(fd); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 502 | return details; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 503 | } |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame^] | 504 | |
| 505 | static bool |
| 506 | print_sockaddr_by_inode_uncached(const unsigned long inode, |
| 507 | const enum sock_proto proto) |
| 508 | { |
| 509 | const char *details = get_sockaddr_by_inode_uncached(inode, proto); |
| 510 | |
| 511 | if (details) { |
| 512 | tprints(details); |
| 513 | return true; |
| 514 | } |
| 515 | |
| 516 | if ((unsigned int) proto < ARRAY_SIZE(protocols) && |
| 517 | protocols[proto].name) { |
| 518 | tprintf("%s:[%lu]", protocols[proto].name, inode); |
| 519 | return true; |
| 520 | } |
| 521 | |
| 522 | return false; |
| 523 | } |
| 524 | |
| 525 | /* Given an inode number of a socket, return its protocol details. */ |
| 526 | const char * |
| 527 | get_sockaddr_by_inode(struct tcb *const tcp, const int fd, |
| 528 | const unsigned long inode) |
| 529 | { |
| 530 | const char *details = get_sockaddr_by_inode_cached(inode); |
| 531 | return details ? details : |
| 532 | get_sockaddr_by_inode_uncached(inode, getfdproto(tcp, fd)); |
| 533 | } |
| 534 | |
| 535 | /* Given an inode number of a socket, print out its protocol details. */ |
| 536 | bool |
| 537 | print_sockaddr_by_inode(struct tcb *const tcp, const int fd, |
| 538 | const unsigned long inode) |
| 539 | { |
| 540 | return print_sockaddr_by_inode_cached(inode) ? true : |
| 541 | print_sockaddr_by_inode_uncached(inode, getfdproto(tcp, fd)); |
| 542 | } |
| 543 | |
| 544 | #ifdef HAVE_LINUX_GENETLINK_H |
| 545 | /* |
| 546 | * Managing the cache for decoding communications of Netlink GENERIC protocol |
| 547 | * |
| 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. |
| 553 | */ |
| 554 | static bool |
| 555 | genl_send_dump_families(const int fd) |
| 556 | { |
| 557 | struct { |
| 558 | const struct nlmsghdr nlh; |
| 559 | struct genlmsghdr gnlh; |
| 560 | } req = { |
| 561 | .nlh = { |
| 562 | .nlmsg_len = sizeof(req), |
| 563 | .nlmsg_type = GENL_ID_CTRL, |
| 564 | .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, |
| 565 | }, |
| 566 | .gnlh = { |
| 567 | .cmd = CTRL_CMD_GETFAMILY, |
| 568 | } |
| 569 | }; |
| 570 | return send_query(fd, &req, sizeof(req)); |
| 571 | } |
| 572 | |
| 573 | static int |
| 574 | genl_parse_families_response(const void *const data, |
| 575 | const int data_len, const unsigned long inode, |
| 576 | void *opaque_data) |
| 577 | { |
| 578 | struct dyxlat *const dyxlat = opaque_data; |
| 579 | const struct genlmsghdr *const gnlh = data; |
| 580 | struct rtattr *attr; |
| 581 | int rta_len = data_len - NLMSG_LENGTH(sizeof(*gnlh)); |
| 582 | |
| 583 | char *name = NULL; |
| 584 | unsigned int name_len = 0; |
| 585 | uint16_t *id = NULL; |
| 586 | |
| 587 | if (rta_len < 0) |
| 588 | return -1; |
| 589 | if (gnlh->cmd != CTRL_CMD_NEWFAMILY) |
| 590 | return -1; |
| 591 | if (gnlh->version != 2) |
| 592 | return -1; |
| 593 | |
| 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: |
| 599 | if (!name) { |
| 600 | name = RTA_DATA(attr); |
| 601 | name_len = RTA_PAYLOAD(attr); |
| 602 | } |
| 603 | break; |
| 604 | case CTRL_ATTR_FAMILY_ID: |
| 605 | if (!id && RTA_PAYLOAD(attr) == sizeof(*id)) |
| 606 | id = RTA_DATA(attr); |
| 607 | break; |
| 608 | } |
| 609 | |
| 610 | if (name && id) { |
| 611 | dyxlat_add_pair(dyxlat, *id, name, name_len); |
| 612 | name = NULL; |
| 613 | id = NULL; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | const struct xlat * |
| 621 | genl_families_xlat(void) |
| 622 | { |
| 623 | static struct dyxlat *dyxlat; |
| 624 | |
| 625 | if (!dyxlat) { |
| 626 | dyxlat = dyxlat_alloc(32); |
| 627 | |
| 628 | int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC); |
| 629 | if (fd < 0) |
| 630 | goto out; |
| 631 | |
| 632 | if (genl_send_dump_families(fd)) |
| 633 | receive_responses(fd, 0, GENL_ID_CTRL, |
| 634 | genl_parse_families_response, dyxlat); |
| 635 | close(fd); |
| 636 | } |
| 637 | |
| 638 | out: |
| 639 | return dyxlat_get(dyxlat); |
| 640 | } |
| 641 | |
| 642 | #else /* !HAVE_LINUX_GENETLINK_H */ |
| 643 | |
| 644 | const struct xlat * |
| 645 | genl_families_xlat(void) |
| 646 | { |
| 647 | return NULL; |
| 648 | } |
| 649 | #endif |