blob: c3f28a1bf71b01406636fc86b62abebc9fad4f00 [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
Elliott Hughesc1873762018-12-19 15:13:36 -080051#define XLAT_MACROS_ONLY
52# include "xlat/inet_protocols.h"
53#undef XLAT_MACROS_ONLY
54
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +000055typedef struct {
56 unsigned long inode;
57 char *details;
58} cache_entry;
59
60#define CACHE_SIZE 1024U
61static cache_entry cache[CACHE_SIZE];
62#define CACHE_MASK (CACHE_SIZE - 1)
63
64static int
Elliott Hughesdc75b012017-07-05 13:54:44 -070065cache_inode_details(const unsigned long inode, char *const details)
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +000066{
67 cache_entry *e = &cache[inode & CACHE_MASK];
68 free(e->details);
69 e->inode = inode;
70 e->details = details;
71
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +000072 return 1;
73}
74
Elliott Hughesdc75b012017-07-05 13:54:44 -070075static const char *
76get_sockaddr_by_inode_cached(const unsigned long inode)
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +000077{
Dmitry V. Levinea218232016-02-10 17:43:09 +000078 const cache_entry *const e = &cache[inode & CACHE_MASK];
Elliott Hughesdc75b012017-07-05 13:54:44 -070079 return (e && inode == e->inode) ? e->details : NULL;
80}
81
82static bool
83print_sockaddr_by_inode_cached(const unsigned long inode)
84{
85 const char *const details = get_sockaddr_by_inode_cached(inode);
86 if (details) {
87 tprints(details);
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +000088 return true;
89 }
90 return false;
91}
92
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +000093static bool
Elliott Hughesb7556142018-02-20 17:03:16 -080094send_query(struct tcb *tcp, const int fd, void *req, size_t req_size)
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +000095{
Dmitry V. Levinaf534b82014-11-21 19:59:16 +000096 struct sockaddr_nl nladdr = {
97 .nl_family = AF_NETLINK
98 };
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +000099 struct iovec iov = {
Fabien Siron071b9272016-05-08 10:45:52 +0000100 .iov_base = req,
101 .iov_len = req_size
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000102 };
Dmitry V. Levinea218232016-02-10 17:43:09 +0000103 const struct msghdr msg = {
104 .msg_name = &nladdr,
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000105 .msg_namelen = sizeof(nladdr),
106 .msg_iov = &iov,
107 .msg_iovlen = 1
108 };
109
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000110 for (;;) {
111 if (sendmsg(fd, &msg, 0) < 0) {
112 if (errno == EINTR)
113 continue;
114 return false;
115 }
116 return true;
117 }
118}
119
Fabien Siron071b9272016-05-08 10:45:52 +0000120static bool
Elliott Hughesb7556142018-02-20 17:03:16 -0800121inet_send_query(struct tcb *tcp, const int fd, const int family,
122 const int proto)
Fabien Siron071b9272016-05-08 10:45:52 +0000123{
124 struct {
125 const struct nlmsghdr nlh;
126 const struct inet_diag_req_v2 idr;
127 } req = {
128 .nlh = {
129 .nlmsg_len = sizeof(req),
130 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
131 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
132 },
133 .idr = {
134 .sdiag_family = family,
135 .sdiag_protocol = proto,
136 .idiag_states = -1
137 }
138 };
Elliott Hughesb7556142018-02-20 17:03:16 -0800139 return send_query(tcp, fd, &req, sizeof(req));
Fabien Siron071b9272016-05-08 10:45:52 +0000140}
141
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000142static int
Elliott Hughesdc75b012017-07-05 13:54:44 -0700143inet_parse_response(const void *const data, const int data_len,
144 const unsigned long inode, void *opaque_data)
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000145{
Elliott Hughesdc75b012017-07-05 13:54:44 -0700146 const char *const proto_name = opaque_data;
Dmitry V. Levinea218232016-02-10 17:43:09 +0000147 const struct inet_diag_msg *const diag_msg = data;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000148 static const char zero_addr[sizeof(struct in6_addr)];
149 socklen_t addr_size, text_size;
150
Dmitry V. Levincc09ba12016-01-28 23:46:56 +0000151 if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000152 return -1;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000153 if (diag_msg->idiag_inode != inode)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000154 return 0;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000155
Elliott Hughesdc75b012017-07-05 13:54:44 -0700156 switch (diag_msg->idiag_family) {
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000157 case AF_INET:
158 addr_size = sizeof(struct in_addr);
159 text_size = INET_ADDRSTRLEN;
160 break;
161 case AF_INET6:
162 addr_size = sizeof(struct in6_addr);
163 text_size = INET6_ADDRSTRLEN;
164 break;
165 default:
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000166 return -1;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000167 }
168
169 char src_buf[text_size];
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000170 char *details;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000171
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700172 /* open/closing brackets for IPv6 addresses */
173 const char *ob = diag_msg->idiag_family == AF_INET6 ? "[" : "";
174 const char *cb = diag_msg->idiag_family == AF_INET6 ? "]" : "";
175
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000176 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_src,
177 src_buf, text_size))
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000178 return -1;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000179
180 if (diag_msg->id.idiag_dport ||
181 memcmp(zero_addr, diag_msg->id.idiag_dst, addr_size)) {
182 char dst_buf[text_size];
183
184 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_dst,
185 dst_buf, text_size))
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000186 return -1;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000187
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700188 if (asprintf(&details, "%s:[%s%s%s:%u->%s%s%s:%u]", proto_name,
189 ob, src_buf, cb, ntohs(diag_msg->id.idiag_sport),
190 ob, dst_buf, cb, ntohs(diag_msg->id.idiag_dport))
191 < 0)
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000192 return false;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000193 } else {
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700194 if (asprintf(&details, "%s:[%s%s%s:%u]",
195 proto_name, ob, src_buf, cb,
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000196 ntohs(diag_msg->id.idiag_sport)) < 0)
197 return false;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000198 }
199
Elliott Hughesdc75b012017-07-05 13:54:44 -0700200 return cache_inode_details(inode, details);
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000201}
202
203static bool
Elliott Hughesb7556142018-02-20 17:03:16 -0800204receive_responses(struct tcb *tcp, const int fd, const unsigned long inode,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700205 const unsigned long expected_msg_type,
206 int (*parser)(const void *, int,
207 unsigned long, void *),
208 void *opaque_data)
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000209{
Dmitry V. Levin7b697972016-05-19 01:23:40 +0000210 static union {
211 struct nlmsghdr hdr;
212 long buf[8192 / sizeof(long)];
213 } hdr_buf;
214
Dmitry V. Levinaf534b82014-11-21 19:59:16 +0000215 struct sockaddr_nl nladdr = {
216 .nl_family = AF_NETLINK
217 };
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000218 struct iovec iov = {
Dmitry V. Levin7b697972016-05-19 01:23:40 +0000219 .iov_base = hdr_buf.buf,
220 .iov_len = sizeof(hdr_buf.buf)
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000221 };
Dmitry V. Levin2215c3e2016-01-27 21:35:50 +0000222 int flags = 0;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000223
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000224 for (;;) {
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000225 struct msghdr msg = {
Dmitry V. Levinea218232016-02-10 17:43:09 +0000226 .msg_name = &nladdr,
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000227 .msg_namelen = sizeof(nladdr),
228 .msg_iov = &iov,
Dmitry V. Levinaf534b82014-11-21 19:59:16 +0000229 .msg_iovlen = 1
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000230 };
231
Dmitry V. Levinea218232016-02-10 17:43:09 +0000232 ssize_t ret = recvmsg(fd, &msg, flags);
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000233 if (ret < 0) {
234 if (errno == EINTR)
235 continue;
236 return false;
237 }
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000238
Dmitry V. Levin7b697972016-05-19 01:23:40 +0000239 const struct nlmsghdr *h = &hdr_buf.hdr;
Elliott Hughesc1873762018-12-19 15:13:36 -0800240 if (!is_nlmsg_ok(h, ret))
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000241 return false;
Elliott Hughesc1873762018-12-19 15:13:36 -0800242 for (; is_nlmsg_ok(h, ret); h = NLMSG_NEXT(h, ret)) {
Elliott Hughesdc75b012017-07-05 13:54:44 -0700243 if (h->nlmsg_type != expected_msg_type)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000244 return false;
Elliott Hughesdc75b012017-07-05 13:54:44 -0700245 const int rc = parser(NLMSG_DATA(h),
246 h->nlmsg_len, inode, opaque_data);
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000247 if (rc > 0)
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000248 return true;
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000249 if (rc < 0)
250 return false;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000251 }
Dmitry V. Levin2215c3e2016-01-27 21:35:50 +0000252 flags = MSG_DONTWAIT;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000253 }
254}
255
Masatake YAMATOf605e922014-12-10 12:55:06 +0900256static bool
Elliott Hughesb7556142018-02-20 17:03:16 -0800257unix_send_query(struct tcb *tcp, const int fd, const unsigned long inode)
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900258{
Elliott Hughes03a418e2018-06-15 13:11:40 -0700259 /*
260 * The kernel bug was fixed in mainline by commit v4.5-rc6~35^2~11
261 * and backported to stable/linux-4.4.y by commit v4.4.4~297.
262 */
263 const uint16_t dump_flag =
264 os_release < KERNEL_VERSION(4, 4, 4) ? NLM_F_DUMP : 0;
265
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900266 struct {
Dmitry V. Levinea218232016-02-10 17:43:09 +0000267 const struct nlmsghdr nlh;
268 const struct unix_diag_req udr;
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900269 } req = {
270 .nlh = {
271 .nlmsg_len = sizeof(req),
272 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
Elliott Hughes03a418e2018-06-15 13:11:40 -0700273 .nlmsg_flags = NLM_F_REQUEST | dump_flag
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900274 },
275 .udr = {
276 .sdiag_family = AF_UNIX,
277 .udiag_ino = inode,
278 .udiag_states = -1,
Elliott Hughes03a418e2018-06-15 13:11:40 -0700279 .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER,
280 .udiag_cookie = { ~0U, ~0U }
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900281 }
282 };
Elliott Hughesb7556142018-02-20 17:03:16 -0800283 return send_query(tcp, fd, &req, sizeof(req));
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900284}
285
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000286static int
Elliott Hughesdc75b012017-07-05 13:54:44 -0700287unix_parse_response(const void *data, const int data_len,
288 const unsigned long inode, void *opaque_data)
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900289{
Elliott Hughesdc75b012017-07-05 13:54:44 -0700290 const char *proto_name = opaque_data;
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900291 const struct unix_diag_msg *diag_msg = data;
292 struct rtattr *attr;
293 int rta_len = data_len - NLMSG_LENGTH(sizeof(*diag_msg));
294 uint32_t peer = 0;
295 size_t path_len = 0;
296 char path[UNIX_PATH_MAX + 1];
297
Dmitry V. Levin3d0f55e2016-01-24 01:46:40 +0300298 if (rta_len < 0)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000299 return -1;
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900300 if (diag_msg->udiag_ino != inode)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000301 return 0;
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900302 if (diag_msg->udiag_family != AF_UNIX)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000303 return -1;
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900304
305 for (attr = (struct rtattr *) (diag_msg + 1);
306 RTA_OK(attr, rta_len);
307 attr = RTA_NEXT(attr, rta_len)) {
308 switch (attr->rta_type) {
309 case UNIX_DIAG_NAME:
310 if (!path_len) {
311 path_len = RTA_PAYLOAD(attr);
312 if (path_len > UNIX_PATH_MAX)
313 path_len = UNIX_PATH_MAX;
314 memcpy(path, RTA_DATA(attr), path_len);
315 path[path_len] = '\0';
316 }
317 break;
318 case UNIX_DIAG_PEER:
319 if (RTA_PAYLOAD(attr) >= 4)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000320 peer = *(uint32_t *) RTA_DATA(attr);
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900321 break;
322 }
323 }
324
325 /*
326 * print obtained information in the following format:
327 * "UNIX:[" SELF_INODE [ "->" PEER_INODE ][ "," SOCKET_FILE ] "]"
328 */
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000329 if (!peer && !path_len)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000330 return -1;
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000331
332 char peer_str[3 + sizeof(peer) * 3];
333 if (peer)
Elliott Hughesb7556142018-02-20 17:03:16 -0800334 xsprintf(peer_str, "->%u", peer);
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000335 else
336 peer_str[0] = '\0';
337
338 const char *path_str;
339 if (path_len) {
340 char *outstr = alloca(4 * path_len + 4);
341
342 outstr[0] = ',';
343 if (path[0] == '\0') {
344 outstr[1] = '@';
345 string_quote(path + 1, outstr + 2,
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700346 path_len - 1, QUOTE_0_TERMINATED, NULL);
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000347 } else {
348 string_quote(path, outstr + 1,
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700349 path_len, QUOTE_0_TERMINATED, NULL);
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000350 }
351 path_str = outstr;
352 } else {
353 path_str = "";
354 }
355
356 char *details;
357 if (asprintf(&details, "%s:[%lu%s%s]", proto_name, inode,
358 peer_str, path_str) < 0)
359 return -1;
360
Elliott Hughesdc75b012017-07-05 13:54:44 -0700361 return cache_inode_details(inode, details);
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900362}
363
364static bool
Elliott Hughesb7556142018-02-20 17:03:16 -0800365netlink_send_query(struct tcb *tcp, const int fd, const unsigned long inode)
Fabien Siron814c0d52016-05-17 10:08:47 +0000366{
367 struct {
368 const struct nlmsghdr nlh;
369 const struct netlink_diag_req ndr;
370 } req = {
371 .nlh = {
372 .nlmsg_len = sizeof(req),
373 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
374 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
375 },
376 .ndr = {
377 .sdiag_family = AF_NETLINK,
Elliott Hughes03a418e2018-06-15 13:11:40 -0700378 .sdiag_protocol = NDIAG_PROTO_ALL
Fabien Siron814c0d52016-05-17 10:08:47 +0000379 }
380 };
Elliott Hughesb7556142018-02-20 17:03:16 -0800381 return send_query(tcp, fd, &req, sizeof(req));
Fabien Siron814c0d52016-05-17 10:08:47 +0000382}
383
384static int
Elliott Hughesdc75b012017-07-05 13:54:44 -0700385netlink_parse_response(const void *data, const int data_len,
386 const unsigned long inode, void *opaque_data)
Fabien Siron814c0d52016-05-17 10:08:47 +0000387{
Elliott Hughesdc75b012017-07-05 13:54:44 -0700388 const char *proto_name = opaque_data;
Fabien Siron814c0d52016-05-17 10:08:47 +0000389 const struct netlink_diag_msg *const diag_msg = data;
390 const char *netlink_proto;
391 char *details;
392
393 if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
394 return -1;
395 if (diag_msg->ndiag_ino != inode)
396 return 0;
397
398 if (diag_msg->ndiag_family != AF_NETLINK)
399 return -1;
400
401 netlink_proto = xlookup(netlink_protocols,
402 diag_msg->ndiag_protocol);
403
404 if (netlink_proto) {
Elliott Hughesdc75b012017-07-05 13:54:44 -0700405 netlink_proto = STR_STRIP_PREFIX(netlink_proto, "NETLINK_");
Fabien Siron814c0d52016-05-17 10:08:47 +0000406 if (asprintf(&details, "%s:[%s:%u]", proto_name,
407 netlink_proto, diag_msg->ndiag_portid) < 0)
408 return -1;
409 } else {
410 if (asprintf(&details, "%s:[%u]", proto_name,
411 (unsigned) diag_msg->ndiag_protocol) < 0)
412 return -1;
413 }
414
Elliott Hughesdc75b012017-07-05 13:54:44 -0700415 return cache_inode_details(inode, details);
Fabien Siron814c0d52016-05-17 10:08:47 +0000416}
417
Elliott Hughesdc75b012017-07-05 13:54:44 -0700418static const char *
Elliott Hughesc1873762018-12-19 15:13:36 -0800419unix_get(struct tcb *tcp, const int fd, const int family, const int proto,
420 const unsigned long inode, const char *name)
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900421{
Elliott Hughesb7556142018-02-20 17:03:16 -0800422 return unix_send_query(tcp, fd, inode)
423 && receive_responses(tcp, fd, inode, SOCK_DIAG_BY_FAMILY,
Elliott Hughesc1873762018-12-19 15:13:36 -0800424 unix_parse_response, (void *) name)
Elliott Hughesdc75b012017-07-05 13:54:44 -0700425 ? get_sockaddr_by_inode_cached(inode) : NULL;
Masatake YAMATOf605e922014-12-10 12:55:06 +0900426}
427
Elliott Hughesdc75b012017-07-05 13:54:44 -0700428static const char *
Elliott Hughesb7556142018-02-20 17:03:16 -0800429inet_get(struct tcb *tcp, const int fd, const int family, const int protocol,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700430 const unsigned long inode, const char *proto_name)
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000431{
Elliott Hughesb7556142018-02-20 17:03:16 -0800432 return inet_send_query(tcp, fd, family, protocol)
433 && receive_responses(tcp, fd, inode, SOCK_DIAG_BY_FAMILY,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700434 inet_parse_response, (void *) proto_name)
435 ? get_sockaddr_by_inode_cached(inode) : NULL;
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000436}
437
Elliott Hughesdc75b012017-07-05 13:54:44 -0700438static const char *
Elliott Hughesc1873762018-12-19 15:13:36 -0800439netlink_get(struct tcb *tcp, const int fd, const int family, const int protocol,
440 const unsigned long inode, const char *proto_name)
Fabien Siron814c0d52016-05-17 10:08:47 +0000441{
Elliott Hughesb7556142018-02-20 17:03:16 -0800442 return netlink_send_query(tcp, fd, inode)
443 && receive_responses(tcp, fd, inode, SOCK_DIAG_BY_FAMILY,
Elliott Hughesc1873762018-12-19 15:13:36 -0800444 netlink_parse_response,
445 (void *) proto_name)
Elliott Hughesdc75b012017-07-05 13:54:44 -0700446 ? get_sockaddr_by_inode_cached(inode) : NULL;
Fabien Siron814c0d52016-05-17 10:08:47 +0000447}
448
Fabien Siron802cc282016-06-17 16:29:53 +0000449static const struct {
450 const char *const name;
Elliott Hughesc1873762018-12-19 15:13:36 -0800451 const char * (*const get)(struct tcb *, int fd, int family,
452 int protocol, unsigned long inode,
453 const char *proto_name);
454 int family;
455 int proto;
Fabien Siron802cc282016-06-17 16:29:53 +0000456} protocols[] = {
Elliott Hughesc1873762018-12-19 15:13:36 -0800457 [SOCK_PROTO_UNIX] = { "UNIX", unix_get, AF_UNIX},
458 /*
459 * inet_diag handlers are currently implemented only for TCP,
460 * UDP(lite), SCTP, RAW, and DCCP, but we try to resolve it for all
461 * protocols anyway, just in case.
462 */
463 [SOCK_PROTO_TCP] =
464 { "TCP", inet_get, AF_INET, IPPROTO_TCP },
465 [SOCK_PROTO_UDP] =
466 { "UDP", inet_get, AF_INET, IPPROTO_UDP },
467 [SOCK_PROTO_UDPLITE] =
468 { "UDPLITE", inet_get, AF_INET, IPPROTO_UDPLITE },
469 [SOCK_PROTO_DCCP] =
470 { "DCCP", inet_get, AF_INET, IPPROTO_DCCP },
471 [SOCK_PROTO_SCTP] =
472 { "SCTP", inet_get, AF_INET, IPPROTO_SCTP },
473 [SOCK_PROTO_L2TP_IP] =
474 { "L2TP/IP", inet_get, AF_INET, IPPROTO_L2TP },
475 [SOCK_PROTO_PING] =
476 { "PING", inet_get, AF_INET, IPPROTO_ICMP },
477 [SOCK_PROTO_RAW] =
478 { "RAW", inet_get, AF_INET, IPPROTO_RAW },
479 [SOCK_PROTO_TCPv6] =
480 { "TCPv6", inet_get, AF_INET6, IPPROTO_TCP },
481 [SOCK_PROTO_UDPv6] =
482 { "UDPv6", inet_get, AF_INET6, IPPROTO_UDP },
483 [SOCK_PROTO_UDPLITEv6] =
484 { "UDPLITEv6", inet_get, AF_INET6, IPPROTO_UDPLITE },
485 [SOCK_PROTO_DCCPv6] =
486 { "DCCPv6", inet_get, AF_INET6, IPPROTO_DCCP },
487 [SOCK_PROTO_SCTPv6] =
488 { "SCTPv6", inet_get, AF_INET6, IPPROTO_SCTP },
489 [SOCK_PROTO_L2TP_IPv6] =
490 { "L2TP/IPv6", inet_get, AF_INET6, IPPROTO_L2TP },
491 [SOCK_PROTO_PINGv6] =
492 { "PINGv6", inet_get, AF_INET6, IPPROTO_ICMP },
493 [SOCK_PROTO_RAWv6] =
494 { "RAWv6", inet_get, AF_INET6, IPPROTO_RAW },
495 [SOCK_PROTO_NETLINK] = { "NETLINK", netlink_get, AF_NETLINK },
Fabien Siron802cc282016-06-17 16:29:53 +0000496};
497
498enum sock_proto
499get_proto_by_name(const char *const name)
500{
501 unsigned int i;
502 for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
503 i < ARRAY_SIZE(protocols); ++i) {
504 if (protocols[i].name && !strcmp(name, protocols[i].name))
505 return (enum sock_proto) i;
506 }
507 return SOCK_PROTO_UNKNOWN;
508}
509
Elliott Hughesc1873762018-12-19 15:13:36 -0800510int
511get_family_by_proto(enum sock_proto proto)
512{
513 if ((size_t) proto < ARRAY_SIZE(protocols))
514 return protocols[proto].family;
515
516 return AF_UNSPEC;
517}
518
Elliott Hughesdc75b012017-07-05 13:54:44 -0700519static const char *
Elliott Hughesb7556142018-02-20 17:03:16 -0800520get_sockaddr_by_inode_uncached(struct tcb *tcp, const unsigned long inode,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700521 const enum sock_proto proto)
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000522{
Fabien Siron802cc282016-06-17 16:29:53 +0000523 if ((unsigned int) proto >= ARRAY_SIZE(protocols) ||
Elliott Hughesdc75b012017-07-05 13:54:44 -0700524 (proto != SOCK_PROTO_UNKNOWN && !protocols[proto].get))
525 return NULL;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000526
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000527 const int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000528 if (fd < 0)
Elliott Hughesdc75b012017-07-05 13:54:44 -0700529 return NULL;
530 const char *details = NULL;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000531
Fabien Siron802cc282016-06-17 16:29:53 +0000532 if (proto != SOCK_PROTO_UNKNOWN) {
Elliott Hughesc1873762018-12-19 15:13:36 -0800533 details = protocols[proto].get(tcp, fd, protocols[proto].family,
534 protocols[proto].proto, inode,
535 protocols[proto].name);
Masatake YAMATOf605e922014-12-10 12:55:06 +0900536 } else {
Fabien Siron802cc282016-06-17 16:29:53 +0000537 unsigned int i;
538 for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
539 i < ARRAY_SIZE(protocols); ++i) {
Elliott Hughesdc75b012017-07-05 13:54:44 -0700540 if (!protocols[i].get)
Fabien Siron802cc282016-06-17 16:29:53 +0000541 continue;
Elliott Hughesc1873762018-12-19 15:13:36 -0800542 details = protocols[i].get(tcp, fd,
543 protocols[proto].family,
544 protocols[proto].proto,
545 inode,
546 protocols[proto].name);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700547 if (details)
Dmitry V. Levin959205c2014-12-26 23:29:26 +0000548 break;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000549 }
550 }
Dmitry V. Levin959205c2014-12-26 23:29:26 +0000551
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000552 close(fd);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700553 return details;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000554}
Elliott Hughesdc75b012017-07-05 13:54:44 -0700555
556static bool
Elliott Hughesb7556142018-02-20 17:03:16 -0800557print_sockaddr_by_inode_uncached(struct tcb *tcp, const unsigned long inode,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700558 const enum sock_proto proto)
559{
Elliott Hughesb7556142018-02-20 17:03:16 -0800560 const char *details = get_sockaddr_by_inode_uncached(tcp, inode, proto);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700561
562 if (details) {
563 tprints(details);
564 return true;
565 }
566
567 if ((unsigned int) proto < ARRAY_SIZE(protocols) &&
568 protocols[proto].name) {
569 tprintf("%s:[%lu]", protocols[proto].name, inode);
570 return true;
571 }
572
573 return false;
574}
575
576/* Given an inode number of a socket, return its protocol details. */
577const char *
578get_sockaddr_by_inode(struct tcb *const tcp, const int fd,
579 const unsigned long inode)
580{
581 const char *details = get_sockaddr_by_inode_cached(inode);
582 return details ? details :
Elliott Hughesb7556142018-02-20 17:03:16 -0800583 get_sockaddr_by_inode_uncached(tcp, inode, getfdproto(tcp, fd));
Elliott Hughesdc75b012017-07-05 13:54:44 -0700584}
585
586/* Given an inode number of a socket, print out its protocol details. */
587bool
588print_sockaddr_by_inode(struct tcb *const tcp, const int fd,
589 const unsigned long inode)
590{
591 return print_sockaddr_by_inode_cached(inode) ? true :
Elliott Hughesb7556142018-02-20 17:03:16 -0800592 print_sockaddr_by_inode_uncached(tcp, inode,
593 getfdproto(tcp, fd));
Elliott Hughesdc75b012017-07-05 13:54:44 -0700594}
595
596#ifdef HAVE_LINUX_GENETLINK_H
597/*
598 * Managing the cache for decoding communications of Netlink GENERIC protocol
599 *
600 * As name shown Netlink GENERIC protocol is generic protocol. The
601 * numbers of msg types used in the protocol are not defined
602 * statically. Kernel defines them on demand. So the xlat converted
603 * from header files doesn't help for decoding the protocol. Following
604 * codes are building xlat(dyxlat) at runtime.
605 */
606static bool
Elliott Hughesb7556142018-02-20 17:03:16 -0800607genl_send_dump_families(struct tcb *tcp, const int fd)
Elliott Hughesdc75b012017-07-05 13:54:44 -0700608{
609 struct {
610 const struct nlmsghdr nlh;
611 struct genlmsghdr gnlh;
612 } req = {
613 .nlh = {
614 .nlmsg_len = sizeof(req),
615 .nlmsg_type = GENL_ID_CTRL,
616 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
617 },
618 .gnlh = {
619 .cmd = CTRL_CMD_GETFAMILY,
620 }
621 };
Elliott Hughesb7556142018-02-20 17:03:16 -0800622 return send_query(tcp, fd, &req, sizeof(req));
Elliott Hughesdc75b012017-07-05 13:54:44 -0700623}
624
625static int
626genl_parse_families_response(const void *const data,
627 const int data_len, const unsigned long inode,
628 void *opaque_data)
629{
630 struct dyxlat *const dyxlat = opaque_data;
631 const struct genlmsghdr *const gnlh = data;
632 struct rtattr *attr;
633 int rta_len = data_len - NLMSG_LENGTH(sizeof(*gnlh));
634
635 char *name = NULL;
636 unsigned int name_len = 0;
637 uint16_t *id = NULL;
638
639 if (rta_len < 0)
640 return -1;
641 if (gnlh->cmd != CTRL_CMD_NEWFAMILY)
642 return -1;
643 if (gnlh->version != 2)
644 return -1;
645
646 for (attr = (struct rtattr *) (gnlh + 1);
647 RTA_OK(attr, rta_len);
648 attr = RTA_NEXT(attr, rta_len)) {
649 switch (attr->rta_type) {
650 case CTRL_ATTR_FAMILY_NAME:
651 if (!name) {
652 name = RTA_DATA(attr);
653 name_len = RTA_PAYLOAD(attr);
654 }
655 break;
656 case CTRL_ATTR_FAMILY_ID:
657 if (!id && RTA_PAYLOAD(attr) == sizeof(*id))
658 id = RTA_DATA(attr);
659 break;
660 }
661
662 if (name && id) {
663 dyxlat_add_pair(dyxlat, *id, name, name_len);
664 name = NULL;
665 id = NULL;
666 }
667 }
668
669 return 0;
670}
671
672const struct xlat *
Elliott Hughesb7556142018-02-20 17:03:16 -0800673genl_families_xlat(struct tcb *tcp)
Elliott Hughesdc75b012017-07-05 13:54:44 -0700674{
675 static struct dyxlat *dyxlat;
676
677 if (!dyxlat) {
678 dyxlat = dyxlat_alloc(32);
679
680 int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
681 if (fd < 0)
682 goto out;
683
Elliott Hughesb7556142018-02-20 17:03:16 -0800684 if (genl_send_dump_families(tcp, fd))
685 receive_responses(tcp, fd, 0, GENL_ID_CTRL,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700686 genl_parse_families_response, dyxlat);
687 close(fd);
688 }
689
690out:
691 return dyxlat_get(dyxlat);
692}
693
694#else /* !HAVE_LINUX_GENETLINK_H */
695
696const struct xlat *
Elliott Hughesb7556142018-02-20 17:03:16 -0800697genl_families_xlat(struct tcb *tcp)
Elliott Hughesdc75b012017-07-05 13:54:44 -0700698{
699 return NULL;
700}
701#endif