blob: 38b864cdf16919ca65b18f662550f24cdeeebda9 [file] [log] [blame]
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00001/*
2 * Copyright (c) 2014 Zubin Mithra <zubin.mithra@gmail.com>
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +00003 * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
Elliott Hughesb7556142018-02-20 17:03:16 -08004 * Copyright (c) 2014-2018 The strace developers.
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00005 * 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. Levin2f6510c2014-08-21 03:17:48 +000030#include "defs.h"
31#include <netinet/in.h>
32#include <sys/socket.h>
33#include <arpa/inet.h>
Elliott Hughesdc75b012017-07-05 13:54:44 -070034#include "netlink.h"
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +000035#include <linux/sock_diag.h>
36#include <linux/inet_diag.h>
Masatake YAMATO120e5db2014-12-24 20:59:31 +090037#include <linux/unix_diag.h>
Fabien Siron814c0d52016-05-17 10:08:47 +000038#include <linux/netlink_diag.h>
Masatake YAMATO120e5db2014-12-24 20:59:31 +090039#include <linux/rtnetlink.h>
Elliott Hughesdc75b012017-07-05 13:54:44 -070040#if HAVE_LINUX_GENETLINK_H
41#include <linux/genetlink.h>
42#endif
Masatake YAMATO120e5db2014-12-24 20:59:31 +090043
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. Levin2f6510c2014-08-21 03:17:48 +000048
Elliott Hughesb7556142018-02-20 17:03:16 -080049#include "xstring.h"
50
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +000051typedef struct {
52 unsigned long inode;
53 char *details;
54} cache_entry;
55
56#define CACHE_SIZE 1024U
57static cache_entry cache[CACHE_SIZE];
58#define CACHE_MASK (CACHE_SIZE - 1)
59
60static int
Elliott Hughesdc75b012017-07-05 13:54:44 -070061cache_inode_details(const unsigned long inode, char *const details)
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +000062{
63 cache_entry *e = &cache[inode & CACHE_MASK];
64 free(e->details);
65 e->inode = inode;
66 e->details = details;
67
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +000068 return 1;
69}
70
Elliott Hughesdc75b012017-07-05 13:54:44 -070071static const char *
72get_sockaddr_by_inode_cached(const unsigned long inode)
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +000073{
Dmitry V. Levinea218232016-02-10 17:43:09 +000074 const cache_entry *const e = &cache[inode & CACHE_MASK];
Elliott Hughesdc75b012017-07-05 13:54:44 -070075 return (e && inode == e->inode) ? e->details : NULL;
76}
77
78static bool
79print_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. Levin3c17d1b2016-02-01 23:14:59 +000084 return true;
85 }
86 return false;
87}
88
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +000089static bool
Elliott Hughesb7556142018-02-20 17:03:16 -080090send_query(struct tcb *tcp, const int fd, void *req, size_t req_size)
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +000091{
Dmitry V. Levinaf534b82014-11-21 19:59:16 +000092 struct sockaddr_nl nladdr = {
93 .nl_family = AF_NETLINK
94 };
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +000095 struct iovec iov = {
Fabien Siron071b9272016-05-08 10:45:52 +000096 .iov_base = req,
97 .iov_len = req_size
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +000098 };
Dmitry V. Levinea218232016-02-10 17:43:09 +000099 const struct msghdr msg = {
100 .msg_name = &nladdr,
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000101 .msg_namelen = sizeof(nladdr),
102 .msg_iov = &iov,
103 .msg_iovlen = 1
104 };
105
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000106 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 Siron071b9272016-05-08 10:45:52 +0000116static bool
Elliott Hughesb7556142018-02-20 17:03:16 -0800117inet_send_query(struct tcb *tcp, const int fd, const int family,
118 const int proto)
Fabien Siron071b9272016-05-08 10:45:52 +0000119{
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 Hughesb7556142018-02-20 17:03:16 -0800135 return send_query(tcp, fd, &req, sizeof(req));
Fabien Siron071b9272016-05-08 10:45:52 +0000136}
137
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000138static int
Elliott Hughesdc75b012017-07-05 13:54:44 -0700139inet_parse_response(const void *const data, const int data_len,
140 const unsigned long inode, void *opaque_data)
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000141{
Elliott Hughesdc75b012017-07-05 13:54:44 -0700142 const char *const proto_name = opaque_data;
Dmitry V. Levinea218232016-02-10 17:43:09 +0000143 const struct inet_diag_msg *const diag_msg = data;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000144 static const char zero_addr[sizeof(struct in6_addr)];
145 socklen_t addr_size, text_size;
146
Dmitry V. Levincc09ba12016-01-28 23:46:56 +0000147 if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000148 return -1;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000149 if (diag_msg->idiag_inode != inode)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000150 return 0;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000151
Elliott Hughesdc75b012017-07-05 13:54:44 -0700152 switch (diag_msg->idiag_family) {
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000153 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. Levin3c86e0e2016-01-30 23:50:54 +0000162 return -1;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000163 }
164
165 char src_buf[text_size];
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000166 char *details;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000167
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700168 /* 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. Levin2f6510c2014-08-21 03:17:48 +0000172 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_src,
173 src_buf, text_size))
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000174 return -1;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000175
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. Levin3c86e0e2016-01-30 23:50:54 +0000182 return -1;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000183
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700184 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. Levin3c17d1b2016-02-01 23:14:59 +0000188 return false;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000189 } else {
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700190 if (asprintf(&details, "%s:[%s%s%s:%u]",
191 proto_name, ob, src_buf, cb,
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000192 ntohs(diag_msg->id.idiag_sport)) < 0)
193 return false;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000194 }
195
Elliott Hughesdc75b012017-07-05 13:54:44 -0700196 return cache_inode_details(inode, details);
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000197}
198
199static bool
Elliott Hughesb7556142018-02-20 17:03:16 -0800200receive_responses(struct tcb *tcp, const int fd, const unsigned long inode,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700201 const unsigned long expected_msg_type,
202 int (*parser)(const void *, int,
203 unsigned long, void *),
204 void *opaque_data)
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000205{
Dmitry V. Levin7b697972016-05-19 01:23:40 +0000206 static union {
207 struct nlmsghdr hdr;
208 long buf[8192 / sizeof(long)];
209 } hdr_buf;
210
Dmitry V. Levinaf534b82014-11-21 19:59:16 +0000211 struct sockaddr_nl nladdr = {
212 .nl_family = AF_NETLINK
213 };
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000214 struct iovec iov = {
Dmitry V. Levin7b697972016-05-19 01:23:40 +0000215 .iov_base = hdr_buf.buf,
216 .iov_len = sizeof(hdr_buf.buf)
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000217 };
Dmitry V. Levin2215c3e2016-01-27 21:35:50 +0000218 int flags = 0;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000219
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000220 for (;;) {
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000221 struct msghdr msg = {
Dmitry V. Levinea218232016-02-10 17:43:09 +0000222 .msg_name = &nladdr,
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000223 .msg_namelen = sizeof(nladdr),
224 .msg_iov = &iov,
Dmitry V. Levinaf534b82014-11-21 19:59:16 +0000225 .msg_iovlen = 1
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000226 };
227
Dmitry V. Levinea218232016-02-10 17:43:09 +0000228 ssize_t ret = recvmsg(fd, &msg, flags);
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000229 if (ret < 0) {
230 if (errno == EINTR)
231 continue;
232 return false;
233 }
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000234
Dmitry V. Levin7b697972016-05-19 01:23:40 +0000235 const struct nlmsghdr *h = &hdr_buf.hdr;
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000236 if (!NLMSG_OK(h, ret))
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000237 return false;
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000238 for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) {
Elliott Hughesdc75b012017-07-05 13:54:44 -0700239 if (h->nlmsg_type != expected_msg_type)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000240 return false;
Elliott Hughesdc75b012017-07-05 13:54:44 -0700241 const int rc = parser(NLMSG_DATA(h),
242 h->nlmsg_len, inode, opaque_data);
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000243 if (rc > 0)
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000244 return true;
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000245 if (rc < 0)
246 return false;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000247 }
Dmitry V. Levin2215c3e2016-01-27 21:35:50 +0000248 flags = MSG_DONTWAIT;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000249 }
250}
251
Masatake YAMATOf605e922014-12-10 12:55:06 +0900252static bool
Elliott Hughesb7556142018-02-20 17:03:16 -0800253unix_send_query(struct tcb *tcp, const int fd, const unsigned long inode)
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900254{
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900255 struct {
Dmitry V. Levinea218232016-02-10 17:43:09 +0000256 const struct nlmsghdr nlh;
257 const struct unix_diag_req udr;
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900258 } req = {
259 .nlh = {
260 .nlmsg_len = sizeof(req),
261 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
Dmitry V. Levin10c61e32016-02-19 01:30:34 +0000262 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900263 },
264 .udr = {
265 .sdiag_family = AF_UNIX,
266 .udiag_ino = inode,
267 .udiag_states = -1,
Dmitry V. Levin10c61e32016-02-19 01:30:34 +0000268 .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900269 }
270 };
Elliott Hughesb7556142018-02-20 17:03:16 -0800271 return send_query(tcp, fd, &req, sizeof(req));
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900272}
273
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000274static int
Elliott Hughesdc75b012017-07-05 13:54:44 -0700275unix_parse_response(const void *data, const int data_len,
276 const unsigned long inode, void *opaque_data)
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900277{
Elliott Hughesdc75b012017-07-05 13:54:44 -0700278 const char *proto_name = opaque_data;
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900279 const struct unix_diag_msg *diag_msg = data;
280 struct rtattr *attr;
281 int rta_len = data_len - NLMSG_LENGTH(sizeof(*diag_msg));
282 uint32_t peer = 0;
283 size_t path_len = 0;
284 char path[UNIX_PATH_MAX + 1];
285
Dmitry V. Levin3d0f55e2016-01-24 01:46:40 +0300286 if (rta_len < 0)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000287 return -1;
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900288 if (diag_msg->udiag_ino != inode)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000289 return 0;
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900290 if (diag_msg->udiag_family != AF_UNIX)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000291 return -1;
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900292
293 for (attr = (struct rtattr *) (diag_msg + 1);
294 RTA_OK(attr, rta_len);
295 attr = RTA_NEXT(attr, rta_len)) {
296 switch (attr->rta_type) {
297 case UNIX_DIAG_NAME:
298 if (!path_len) {
299 path_len = RTA_PAYLOAD(attr);
300 if (path_len > UNIX_PATH_MAX)
301 path_len = UNIX_PATH_MAX;
302 memcpy(path, RTA_DATA(attr), path_len);
303 path[path_len] = '\0';
304 }
305 break;
306 case UNIX_DIAG_PEER:
307 if (RTA_PAYLOAD(attr) >= 4)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000308 peer = *(uint32_t *) RTA_DATA(attr);
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900309 break;
310 }
311 }
312
313 /*
314 * print obtained information in the following format:
315 * "UNIX:[" SELF_INODE [ "->" PEER_INODE ][ "," SOCKET_FILE ] "]"
316 */
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000317 if (!peer && !path_len)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000318 return -1;
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000319
320 char peer_str[3 + sizeof(peer) * 3];
321 if (peer)
Elliott Hughesb7556142018-02-20 17:03:16 -0800322 xsprintf(peer_str, "->%u", peer);
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000323 else
324 peer_str[0] = '\0';
325
326 const char *path_str;
327 if (path_len) {
328 char *outstr = alloca(4 * path_len + 4);
329
330 outstr[0] = ',';
331 if (path[0] == '\0') {
332 outstr[1] = '@';
333 string_quote(path + 1, outstr + 2,
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700334 path_len - 1, QUOTE_0_TERMINATED, NULL);
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000335 } else {
336 string_quote(path, outstr + 1,
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700337 path_len, QUOTE_0_TERMINATED, NULL);
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000338 }
339 path_str = outstr;
340 } else {
341 path_str = "";
342 }
343
344 char *details;
345 if (asprintf(&details, "%s:[%lu%s%s]", proto_name, inode,
346 peer_str, path_str) < 0)
347 return -1;
348
Elliott Hughesdc75b012017-07-05 13:54:44 -0700349 return cache_inode_details(inode, details);
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900350}
351
352static bool
Elliott Hughesb7556142018-02-20 17:03:16 -0800353netlink_send_query(struct tcb *tcp, const int fd, const unsigned long inode)
Fabien Siron814c0d52016-05-17 10:08:47 +0000354{
355 struct {
356 const struct nlmsghdr nlh;
357 const struct netlink_diag_req ndr;
358 } req = {
359 .nlh = {
360 .nlmsg_len = sizeof(req),
361 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
362 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
363 },
364 .ndr = {
365 .sdiag_family = AF_NETLINK,
366 .sdiag_protocol = NDIAG_PROTO_ALL,
367 .ndiag_show = NDIAG_SHOW_MEMINFO
368 }
369 };
Elliott Hughesb7556142018-02-20 17:03:16 -0800370 return send_query(tcp, fd, &req, sizeof(req));
Fabien Siron814c0d52016-05-17 10:08:47 +0000371}
372
373static int
Elliott Hughesdc75b012017-07-05 13:54:44 -0700374netlink_parse_response(const void *data, const int data_len,
375 const unsigned long inode, void *opaque_data)
Fabien Siron814c0d52016-05-17 10:08:47 +0000376{
Elliott Hughesdc75b012017-07-05 13:54:44 -0700377 const char *proto_name = opaque_data;
Fabien Siron814c0d52016-05-17 10:08:47 +0000378 const struct netlink_diag_msg *const diag_msg = data;
379 const char *netlink_proto;
380 char *details;
381
382 if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
383 return -1;
384 if (diag_msg->ndiag_ino != inode)
385 return 0;
386
387 if (diag_msg->ndiag_family != AF_NETLINK)
388 return -1;
389
390 netlink_proto = xlookup(netlink_protocols,
391 diag_msg->ndiag_protocol);
392
393 if (netlink_proto) {
Elliott Hughesdc75b012017-07-05 13:54:44 -0700394 netlink_proto = STR_STRIP_PREFIX(netlink_proto, "NETLINK_");
Fabien Siron814c0d52016-05-17 10:08:47 +0000395 if (asprintf(&details, "%s:[%s:%u]", proto_name,
396 netlink_proto, diag_msg->ndiag_portid) < 0)
397 return -1;
398 } else {
399 if (asprintf(&details, "%s:[%u]", proto_name,
400 (unsigned) diag_msg->ndiag_protocol) < 0)
401 return -1;
402 }
403
Elliott Hughesdc75b012017-07-05 13:54:44 -0700404 return cache_inode_details(inode, details);
Fabien Siron814c0d52016-05-17 10:08:47 +0000405}
406
Elliott Hughesdc75b012017-07-05 13:54:44 -0700407static const char *
Elliott Hughesb7556142018-02-20 17:03:16 -0800408unix_get(struct tcb *tcp, const int fd, const unsigned long inode)
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900409{
Elliott Hughesb7556142018-02-20 17:03:16 -0800410 return unix_send_query(tcp, fd, inode)
411 && receive_responses(tcp, fd, inode, SOCK_DIAG_BY_FAMILY,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700412 unix_parse_response, (void *) "UNIX")
413 ? get_sockaddr_by_inode_cached(inode) : NULL;
Masatake YAMATOf605e922014-12-10 12:55:06 +0900414}
415
Elliott Hughesdc75b012017-07-05 13:54:44 -0700416static const char *
Elliott Hughesb7556142018-02-20 17:03:16 -0800417inet_get(struct tcb *tcp, const int fd, const int family, const int protocol,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700418 const unsigned long inode, const char *proto_name)
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000419{
Elliott Hughesb7556142018-02-20 17:03:16 -0800420 return inet_send_query(tcp, fd, family, protocol)
421 && receive_responses(tcp, fd, inode, SOCK_DIAG_BY_FAMILY,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700422 inet_parse_response, (void *) proto_name)
423 ? get_sockaddr_by_inode_cached(inode) : NULL;
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000424}
425
Elliott Hughesdc75b012017-07-05 13:54:44 -0700426static const char *
Elliott Hughesb7556142018-02-20 17:03:16 -0800427tcp_v4_get(struct tcb *tcp, const int fd, const unsigned long inode)
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000428{
Elliott Hughesb7556142018-02-20 17:03:16 -0800429 return inet_get(tcp, fd, AF_INET, IPPROTO_TCP, inode, "TCP");
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000430}
431
Elliott Hughesdc75b012017-07-05 13:54:44 -0700432static const char *
Elliott Hughesb7556142018-02-20 17:03:16 -0800433udp_v4_get(struct tcb *tcp, const int fd, const unsigned long inode)
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000434{
Elliott Hughesb7556142018-02-20 17:03:16 -0800435 return inet_get(tcp, fd, AF_INET, IPPROTO_UDP, inode, "UDP");
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000436}
437
Elliott Hughesdc75b012017-07-05 13:54:44 -0700438static const char *
Elliott Hughesb7556142018-02-20 17:03:16 -0800439tcp_v6_get(struct tcb *tcp, const int fd, const unsigned long inode)
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000440{
Elliott Hughesb7556142018-02-20 17:03:16 -0800441 return inet_get(tcp, fd, AF_INET6, IPPROTO_TCP, inode, "TCPv6");
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000442}
443
Elliott Hughesdc75b012017-07-05 13:54:44 -0700444static const char *
Elliott Hughesb7556142018-02-20 17:03:16 -0800445udp_v6_get(struct tcb *tcp, const int fd, const unsigned long inode)
Elliott Hughesdc75b012017-07-05 13:54:44 -0700446{
Elliott Hughesb7556142018-02-20 17:03:16 -0800447 return inet_get(tcp, fd, AF_INET6, IPPROTO_UDP, inode, "UDPv6");
Elliott Hughesdc75b012017-07-05 13:54:44 -0700448}
449
450static const char *
Elliott Hughesb7556142018-02-20 17:03:16 -0800451netlink_get(struct tcb *tcp, const int fd, const unsigned long inode)
Fabien Siron814c0d52016-05-17 10:08:47 +0000452{
Elliott Hughesb7556142018-02-20 17:03:16 -0800453 return netlink_send_query(tcp, fd, inode)
454 && receive_responses(tcp, fd, inode, SOCK_DIAG_BY_FAMILY,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700455 netlink_parse_response, (void *) "NETLINK")
456 ? get_sockaddr_by_inode_cached(inode) : NULL;
Fabien Siron814c0d52016-05-17 10:08:47 +0000457}
458
Fabien Siron802cc282016-06-17 16:29:53 +0000459static const struct {
460 const char *const name;
Elliott Hughesb7556142018-02-20 17:03:16 -0800461 const char * (*const get)(struct tcb *, int, unsigned long);
Fabien Siron802cc282016-06-17 16:29:53 +0000462} protocols[] = {
Elliott Hughesdc75b012017-07-05 13:54:44 -0700463 [SOCK_PROTO_UNIX] = { "UNIX", unix_get },
464 [SOCK_PROTO_TCP] = { "TCP", tcp_v4_get },
465 [SOCK_PROTO_UDP] = { "UDP", udp_v4_get },
466 [SOCK_PROTO_TCPv6] = { "TCPv6", tcp_v6_get },
467 [SOCK_PROTO_UDPv6] = { "UDPv6", udp_v6_get },
468 [SOCK_PROTO_NETLINK] = { "NETLINK", netlink_get }
Fabien Siron802cc282016-06-17 16:29:53 +0000469};
470
471enum sock_proto
472get_proto_by_name(const char *const name)
473{
474 unsigned int i;
475 for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
476 i < ARRAY_SIZE(protocols); ++i) {
477 if (protocols[i].name && !strcmp(name, protocols[i].name))
478 return (enum sock_proto) i;
479 }
480 return SOCK_PROTO_UNKNOWN;
481}
482
Elliott Hughesdc75b012017-07-05 13:54:44 -0700483static const char *
Elliott Hughesb7556142018-02-20 17:03:16 -0800484get_sockaddr_by_inode_uncached(struct tcb *tcp, const unsigned long inode,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700485 const enum sock_proto proto)
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000486{
Fabien Siron802cc282016-06-17 16:29:53 +0000487 if ((unsigned int) proto >= ARRAY_SIZE(protocols) ||
Elliott Hughesdc75b012017-07-05 13:54:44 -0700488 (proto != SOCK_PROTO_UNKNOWN && !protocols[proto].get))
489 return NULL;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000490
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000491 const int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000492 if (fd < 0)
Elliott Hughesdc75b012017-07-05 13:54:44 -0700493 return NULL;
494 const char *details = NULL;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000495
Fabien Siron802cc282016-06-17 16:29:53 +0000496 if (proto != SOCK_PROTO_UNKNOWN) {
Elliott Hughesb7556142018-02-20 17:03:16 -0800497 details = protocols[proto].get(tcp, fd, inode);
Masatake YAMATOf605e922014-12-10 12:55:06 +0900498 } else {
Fabien Siron802cc282016-06-17 16:29:53 +0000499 unsigned int i;
500 for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
501 i < ARRAY_SIZE(protocols); ++i) {
Elliott Hughesdc75b012017-07-05 13:54:44 -0700502 if (!protocols[i].get)
Fabien Siron802cc282016-06-17 16:29:53 +0000503 continue;
Elliott Hughesb7556142018-02-20 17:03:16 -0800504 details = protocols[i].get(tcp, fd, inode);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700505 if (details)
Dmitry V. Levin959205c2014-12-26 23:29:26 +0000506 break;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000507 }
508 }
Dmitry V. Levin959205c2014-12-26 23:29:26 +0000509
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000510 close(fd);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700511 return details;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000512}
Elliott Hughesdc75b012017-07-05 13:54:44 -0700513
514static bool
Elliott Hughesb7556142018-02-20 17:03:16 -0800515print_sockaddr_by_inode_uncached(struct tcb *tcp, const unsigned long inode,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700516 const enum sock_proto proto)
517{
Elliott Hughesb7556142018-02-20 17:03:16 -0800518 const char *details = get_sockaddr_by_inode_uncached(tcp, inode, proto);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700519
520 if (details) {
521 tprints(details);
522 return true;
523 }
524
525 if ((unsigned int) proto < ARRAY_SIZE(protocols) &&
526 protocols[proto].name) {
527 tprintf("%s:[%lu]", protocols[proto].name, inode);
528 return true;
529 }
530
531 return false;
532}
533
534/* Given an inode number of a socket, return its protocol details. */
535const char *
536get_sockaddr_by_inode(struct tcb *const tcp, const int fd,
537 const unsigned long inode)
538{
539 const char *details = get_sockaddr_by_inode_cached(inode);
540 return details ? details :
Elliott Hughesb7556142018-02-20 17:03:16 -0800541 get_sockaddr_by_inode_uncached(tcp, inode, getfdproto(tcp, fd));
Elliott Hughesdc75b012017-07-05 13:54:44 -0700542}
543
544/* Given an inode number of a socket, print out its protocol details. */
545bool
546print_sockaddr_by_inode(struct tcb *const tcp, const int fd,
547 const unsigned long inode)
548{
549 return print_sockaddr_by_inode_cached(inode) ? true :
Elliott Hughesb7556142018-02-20 17:03:16 -0800550 print_sockaddr_by_inode_uncached(tcp, inode,
551 getfdproto(tcp, fd));
Elliott Hughesdc75b012017-07-05 13:54:44 -0700552}
553
554#ifdef HAVE_LINUX_GENETLINK_H
555/*
556 * Managing the cache for decoding communications of Netlink GENERIC protocol
557 *
558 * As name shown Netlink GENERIC protocol is generic protocol. The
559 * numbers of msg types used in the protocol are not defined
560 * statically. Kernel defines them on demand. So the xlat converted
561 * from header files doesn't help for decoding the protocol. Following
562 * codes are building xlat(dyxlat) at runtime.
563 */
564static bool
Elliott Hughesb7556142018-02-20 17:03:16 -0800565genl_send_dump_families(struct tcb *tcp, const int fd)
Elliott Hughesdc75b012017-07-05 13:54:44 -0700566{
567 struct {
568 const struct nlmsghdr nlh;
569 struct genlmsghdr gnlh;
570 } req = {
571 .nlh = {
572 .nlmsg_len = sizeof(req),
573 .nlmsg_type = GENL_ID_CTRL,
574 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
575 },
576 .gnlh = {
577 .cmd = CTRL_CMD_GETFAMILY,
578 }
579 };
Elliott Hughesb7556142018-02-20 17:03:16 -0800580 return send_query(tcp, fd, &req, sizeof(req));
Elliott Hughesdc75b012017-07-05 13:54:44 -0700581}
582
583static int
584genl_parse_families_response(const void *const data,
585 const int data_len, const unsigned long inode,
586 void *opaque_data)
587{
588 struct dyxlat *const dyxlat = opaque_data;
589 const struct genlmsghdr *const gnlh = data;
590 struct rtattr *attr;
591 int rta_len = data_len - NLMSG_LENGTH(sizeof(*gnlh));
592
593 char *name = NULL;
594 unsigned int name_len = 0;
595 uint16_t *id = NULL;
596
597 if (rta_len < 0)
598 return -1;
599 if (gnlh->cmd != CTRL_CMD_NEWFAMILY)
600 return -1;
601 if (gnlh->version != 2)
602 return -1;
603
604 for (attr = (struct rtattr *) (gnlh + 1);
605 RTA_OK(attr, rta_len);
606 attr = RTA_NEXT(attr, rta_len)) {
607 switch (attr->rta_type) {
608 case CTRL_ATTR_FAMILY_NAME:
609 if (!name) {
610 name = RTA_DATA(attr);
611 name_len = RTA_PAYLOAD(attr);
612 }
613 break;
614 case CTRL_ATTR_FAMILY_ID:
615 if (!id && RTA_PAYLOAD(attr) == sizeof(*id))
616 id = RTA_DATA(attr);
617 break;
618 }
619
620 if (name && id) {
621 dyxlat_add_pair(dyxlat, *id, name, name_len);
622 name = NULL;
623 id = NULL;
624 }
625 }
626
627 return 0;
628}
629
630const struct xlat *
Elliott Hughesb7556142018-02-20 17:03:16 -0800631genl_families_xlat(struct tcb *tcp)
Elliott Hughesdc75b012017-07-05 13:54:44 -0700632{
633 static struct dyxlat *dyxlat;
634
635 if (!dyxlat) {
636 dyxlat = dyxlat_alloc(32);
637
638 int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
639 if (fd < 0)
640 goto out;
641
Elliott Hughesb7556142018-02-20 17:03:16 -0800642 if (genl_send_dump_families(tcp, fd))
643 receive_responses(tcp, fd, 0, GENL_ID_CTRL,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700644 genl_parse_families_response, dyxlat);
645 close(fd);
646 }
647
648out:
649 return dyxlat_get(dyxlat);
650}
651
652#else /* !HAVE_LINUX_GENETLINK_H */
653
654const struct xlat *
Elliott Hughesb7556142018-02-20 17:03:16 -0800655genl_families_xlat(struct tcb *tcp)
Elliott Hughesdc75b012017-07-05 13:54:44 -0700656{
657 return NULL;
658}
659#endif