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