blob: ce4e524913d50ea940289d668c19d5e6d8b1e304 [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 Hughes39bac052017-05-25 16:56:11 -07004 * Copyright (c) 2014-2017 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
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +000049typedef struct {
50 unsigned long inode;
51 char *details;
52} cache_entry;
53
54#define CACHE_SIZE 1024U
55static cache_entry cache[CACHE_SIZE];
56#define CACHE_MASK (CACHE_SIZE - 1)
57
58static int
Elliott Hughesdc75b012017-07-05 13:54:44 -070059cache_inode_details(const unsigned long inode, char *const details)
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +000060{
61 cache_entry *e = &cache[inode & CACHE_MASK];
62 free(e->details);
63 e->inode = inode;
64 e->details = details;
65
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +000066 return 1;
67}
68
Elliott Hughesdc75b012017-07-05 13:54:44 -070069static const char *
70get_sockaddr_by_inode_cached(const unsigned long inode)
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +000071{
Dmitry V. Levinea218232016-02-10 17:43:09 +000072 const cache_entry *const e = &cache[inode & CACHE_MASK];
Elliott Hughesdc75b012017-07-05 13:54:44 -070073 return (e && inode == e->inode) ? e->details : NULL;
74}
75
76static bool
77print_sockaddr_by_inode_cached(const unsigned long inode)
78{
79 const char *const details = get_sockaddr_by_inode_cached(inode);
80 if (details) {
81 tprints(details);
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +000082 return true;
83 }
84 return false;
85}
86
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +000087static bool
Fabien Siron071b9272016-05-08 10:45:52 +000088send_query(const int fd, void *req, size_t req_size)
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +000089{
Dmitry V. Levinaf534b82014-11-21 19:59:16 +000090 struct sockaddr_nl nladdr = {
91 .nl_family = AF_NETLINK
92 };
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +000093 struct iovec iov = {
Fabien Siron071b9272016-05-08 10:45:52 +000094 .iov_base = req,
95 .iov_len = req_size
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +000096 };
Dmitry V. Levinea218232016-02-10 17:43:09 +000097 const struct msghdr msg = {
98 .msg_name = &nladdr,
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +000099 .msg_namelen = sizeof(nladdr),
100 .msg_iov = &iov,
101 .msg_iovlen = 1
102 };
103
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000104 for (;;) {
105 if (sendmsg(fd, &msg, 0) < 0) {
106 if (errno == EINTR)
107 continue;
108 return false;
109 }
110 return true;
111 }
112}
113
Fabien Siron071b9272016-05-08 10:45:52 +0000114static bool
115inet_send_query(const int fd, const int family, const int proto)
116{
117 struct {
118 const struct nlmsghdr nlh;
119 const struct inet_diag_req_v2 idr;
120 } req = {
121 .nlh = {
122 .nlmsg_len = sizeof(req),
123 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
124 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
125 },
126 .idr = {
127 .sdiag_family = family,
128 .sdiag_protocol = proto,
129 .idiag_states = -1
130 }
131 };
132 return send_query(fd, &req, sizeof(req));
133}
134
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000135static int
Elliott Hughesdc75b012017-07-05 13:54:44 -0700136inet_parse_response(const void *const data, const int data_len,
137 const unsigned long inode, void *opaque_data)
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000138{
Elliott Hughesdc75b012017-07-05 13:54:44 -0700139 const char *const proto_name = opaque_data;
Dmitry V. Levinea218232016-02-10 17:43:09 +0000140 const struct inet_diag_msg *const diag_msg = data;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000141 static const char zero_addr[sizeof(struct in6_addr)];
142 socklen_t addr_size, text_size;
143
Dmitry V. Levincc09ba12016-01-28 23:46:56 +0000144 if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000145 return -1;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000146 if (diag_msg->idiag_inode != inode)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000147 return 0;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000148
Elliott Hughesdc75b012017-07-05 13:54:44 -0700149 switch (diag_msg->idiag_family) {
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000150 case AF_INET:
151 addr_size = sizeof(struct in_addr);
152 text_size = INET_ADDRSTRLEN;
153 break;
154 case AF_INET6:
155 addr_size = sizeof(struct in6_addr);
156 text_size = INET6_ADDRSTRLEN;
157 break;
158 default:
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000159 return -1;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000160 }
161
162 char src_buf[text_size];
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000163 char *details;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000164
165 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_src,
166 src_buf, text_size))
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000167 return -1;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000168
169 if (diag_msg->id.idiag_dport ||
170 memcmp(zero_addr, diag_msg->id.idiag_dst, addr_size)) {
171 char dst_buf[text_size];
172
173 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_dst,
174 dst_buf, text_size))
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000175 return -1;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000176
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000177 if (asprintf(&details, "%s:[%s:%u->%s:%u]", proto_name,
178 src_buf, ntohs(diag_msg->id.idiag_sport),
179 dst_buf, ntohs(diag_msg->id.idiag_dport)) < 0)
180 return false;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000181 } else {
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000182 if (asprintf(&details, "%s:[%s:%u]", proto_name, src_buf,
183 ntohs(diag_msg->id.idiag_sport)) < 0)
184 return false;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000185 }
186
Elliott Hughesdc75b012017-07-05 13:54:44 -0700187 return cache_inode_details(inode, details);
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000188}
189
190static bool
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900191receive_responses(const int fd, const unsigned long inode,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700192 const unsigned long expected_msg_type,
193 int (*parser)(const void *, int,
194 unsigned long, void *),
195 void *opaque_data)
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000196{
Dmitry V. Levin7b697972016-05-19 01:23:40 +0000197 static union {
198 struct nlmsghdr hdr;
199 long buf[8192 / sizeof(long)];
200 } hdr_buf;
201
Dmitry V. Levinaf534b82014-11-21 19:59:16 +0000202 struct sockaddr_nl nladdr = {
203 .nl_family = AF_NETLINK
204 };
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000205 struct iovec iov = {
Dmitry V. Levin7b697972016-05-19 01:23:40 +0000206 .iov_base = hdr_buf.buf,
207 .iov_len = sizeof(hdr_buf.buf)
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000208 };
Dmitry V. Levin2215c3e2016-01-27 21:35:50 +0000209 int flags = 0;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000210
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000211 for (;;) {
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000212 struct msghdr msg = {
Dmitry V. Levinea218232016-02-10 17:43:09 +0000213 .msg_name = &nladdr,
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000214 .msg_namelen = sizeof(nladdr),
215 .msg_iov = &iov,
Dmitry V. Levinaf534b82014-11-21 19:59:16 +0000216 .msg_iovlen = 1
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000217 };
218
Dmitry V. Levinea218232016-02-10 17:43:09 +0000219 ssize_t ret = recvmsg(fd, &msg, flags);
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000220 if (ret < 0) {
221 if (errno == EINTR)
222 continue;
223 return false;
224 }
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000225
Dmitry V. Levin7b697972016-05-19 01:23:40 +0000226 const struct nlmsghdr *h = &hdr_buf.hdr;
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000227 if (!NLMSG_OK(h, ret))
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000228 return false;
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000229 for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) {
Elliott Hughesdc75b012017-07-05 13:54:44 -0700230 if (h->nlmsg_type != expected_msg_type)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000231 return false;
Elliott Hughesdc75b012017-07-05 13:54:44 -0700232 const int rc = parser(NLMSG_DATA(h),
233 h->nlmsg_len, inode, opaque_data);
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000234 if (rc > 0)
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000235 return true;
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000236 if (rc < 0)
237 return false;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000238 }
Dmitry V. Levin2215c3e2016-01-27 21:35:50 +0000239 flags = MSG_DONTWAIT;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000240 }
241}
242
Masatake YAMATOf605e922014-12-10 12:55:06 +0900243static bool
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900244unix_send_query(const int fd, const unsigned long inode)
245{
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900246 struct {
Dmitry V. Levinea218232016-02-10 17:43:09 +0000247 const struct nlmsghdr nlh;
248 const struct unix_diag_req udr;
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900249 } req = {
250 .nlh = {
251 .nlmsg_len = sizeof(req),
252 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
Dmitry V. Levin10c61e32016-02-19 01:30:34 +0000253 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900254 },
255 .udr = {
256 .sdiag_family = AF_UNIX,
257 .udiag_ino = inode,
258 .udiag_states = -1,
Dmitry V. Levin10c61e32016-02-19 01:30:34 +0000259 .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900260 }
261 };
Fabien Siron071b9272016-05-08 10:45:52 +0000262 return send_query(fd, &req, sizeof(req));
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900263}
264
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000265static int
Elliott Hughesdc75b012017-07-05 13:54:44 -0700266unix_parse_response(const void *data, const int data_len,
267 const unsigned long inode, void *opaque_data)
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900268{
Elliott Hughesdc75b012017-07-05 13:54:44 -0700269 const char *proto_name = opaque_data;
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900270 const struct unix_diag_msg *diag_msg = data;
271 struct rtattr *attr;
272 int rta_len = data_len - NLMSG_LENGTH(sizeof(*diag_msg));
273 uint32_t peer = 0;
274 size_t path_len = 0;
275 char path[UNIX_PATH_MAX + 1];
276
Dmitry V. Levin3d0f55e2016-01-24 01:46:40 +0300277 if (rta_len < 0)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000278 return -1;
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900279 if (diag_msg->udiag_ino != inode)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000280 return 0;
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900281 if (diag_msg->udiag_family != AF_UNIX)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000282 return -1;
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900283
284 for (attr = (struct rtattr *) (diag_msg + 1);
285 RTA_OK(attr, rta_len);
286 attr = RTA_NEXT(attr, rta_len)) {
287 switch (attr->rta_type) {
288 case UNIX_DIAG_NAME:
289 if (!path_len) {
290 path_len = RTA_PAYLOAD(attr);
291 if (path_len > UNIX_PATH_MAX)
292 path_len = UNIX_PATH_MAX;
293 memcpy(path, RTA_DATA(attr), path_len);
294 path[path_len] = '\0';
295 }
296 break;
297 case UNIX_DIAG_PEER:
298 if (RTA_PAYLOAD(attr) >= 4)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000299 peer = *(uint32_t *) RTA_DATA(attr);
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900300 break;
301 }
302 }
303
304 /*
305 * print obtained information in the following format:
306 * "UNIX:[" SELF_INODE [ "->" PEER_INODE ][ "," SOCKET_FILE ] "]"
307 */
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000308 if (!peer && !path_len)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000309 return -1;
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000310
311 char peer_str[3 + sizeof(peer) * 3];
312 if (peer)
313 snprintf(peer_str, sizeof(peer_str), "->%u", peer);
314 else
315 peer_str[0] = '\0';
316
317 const char *path_str;
318 if (path_len) {
319 char *outstr = alloca(4 * path_len + 4);
320
321 outstr[0] = ',';
322 if (path[0] == '\0') {
323 outstr[1] = '@';
324 string_quote(path + 1, outstr + 2,
325 path_len - 1, QUOTE_0_TERMINATED);
326 } else {
327 string_quote(path, outstr + 1,
328 path_len, QUOTE_0_TERMINATED);
329 }
330 path_str = outstr;
331 } else {
332 path_str = "";
333 }
334
335 char *details;
336 if (asprintf(&details, "%s:[%lu%s%s]", proto_name, inode,
337 peer_str, path_str) < 0)
338 return -1;
339
Elliott Hughesdc75b012017-07-05 13:54:44 -0700340 return cache_inode_details(inode, details);
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900341}
342
343static bool
Fabien Siron814c0d52016-05-17 10:08:47 +0000344netlink_send_query(const int fd, const unsigned long inode)
345{
346 struct {
347 const struct nlmsghdr nlh;
348 const struct netlink_diag_req ndr;
349 } req = {
350 .nlh = {
351 .nlmsg_len = sizeof(req),
352 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
353 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
354 },
355 .ndr = {
356 .sdiag_family = AF_NETLINK,
357 .sdiag_protocol = NDIAG_PROTO_ALL,
358 .ndiag_show = NDIAG_SHOW_MEMINFO
359 }
360 };
361 return send_query(fd, &req, sizeof(req));
362}
363
364static int
Elliott Hughesdc75b012017-07-05 13:54:44 -0700365netlink_parse_response(const void *data, const int data_len,
366 const unsigned long inode, void *opaque_data)
Fabien Siron814c0d52016-05-17 10:08:47 +0000367{
Elliott Hughesdc75b012017-07-05 13:54:44 -0700368 const char *proto_name = opaque_data;
Fabien Siron814c0d52016-05-17 10:08:47 +0000369 const struct netlink_diag_msg *const diag_msg = data;
370 const char *netlink_proto;
371 char *details;
372
373 if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
374 return -1;
375 if (diag_msg->ndiag_ino != inode)
376 return 0;
377
378 if (diag_msg->ndiag_family != AF_NETLINK)
379 return -1;
380
381 netlink_proto = xlookup(netlink_protocols,
382 diag_msg->ndiag_protocol);
383
384 if (netlink_proto) {
Elliott Hughesdc75b012017-07-05 13:54:44 -0700385 netlink_proto = STR_STRIP_PREFIX(netlink_proto, "NETLINK_");
Fabien Siron814c0d52016-05-17 10:08:47 +0000386 if (asprintf(&details, "%s:[%s:%u]", proto_name,
387 netlink_proto, diag_msg->ndiag_portid) < 0)
388 return -1;
389 } else {
390 if (asprintf(&details, "%s:[%u]", proto_name,
391 (unsigned) diag_msg->ndiag_protocol) < 0)
392 return -1;
393 }
394
Elliott Hughesdc75b012017-07-05 13:54:44 -0700395 return cache_inode_details(inode, details);
Fabien Siron814c0d52016-05-17 10:08:47 +0000396}
397
Elliott Hughesdc75b012017-07-05 13:54:44 -0700398static const char *
399unix_get(const int fd, const unsigned long inode)
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900400{
401 return unix_send_query(fd, inode)
Elliott Hughesdc75b012017-07-05 13:54:44 -0700402 && receive_responses(fd, inode, SOCK_DIAG_BY_FAMILY,
403 unix_parse_response, (void *) "UNIX")
404 ? get_sockaddr_by_inode_cached(inode) : NULL;
Masatake YAMATOf605e922014-12-10 12:55:06 +0900405}
406
Elliott Hughesdc75b012017-07-05 13:54:44 -0700407static const char *
408inet_get(const int fd, const int family, const int protocol,
409 const unsigned long inode, const char *proto_name)
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000410{
Elliott Hughesdc75b012017-07-05 13:54:44 -0700411 return inet_send_query(fd, family, protocol)
412 && receive_responses(fd, inode, SOCK_DIAG_BY_FAMILY,
413 inet_parse_response, (void *) proto_name)
414 ? get_sockaddr_by_inode_cached(inode) : NULL;
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000415}
416
Elliott Hughesdc75b012017-07-05 13:54:44 -0700417static const char *
418tcp_v4_get(const int fd, const unsigned long inode)
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000419{
Elliott Hughesdc75b012017-07-05 13:54:44 -0700420 return inet_get(fd, AF_INET, IPPROTO_TCP, inode, "TCP");
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000421}
422
Elliott Hughesdc75b012017-07-05 13:54:44 -0700423static const char *
424udp_v4_get(const int fd, const unsigned long inode)
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000425{
Elliott Hughesdc75b012017-07-05 13:54:44 -0700426 return inet_get(fd, AF_INET, IPPROTO_UDP, inode, "UDP");
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000427}
428
Elliott Hughesdc75b012017-07-05 13:54:44 -0700429static const char *
430tcp_v6_get(const int fd, const unsigned long inode)
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000431{
Elliott Hughesdc75b012017-07-05 13:54:44 -0700432 return inet_get(fd, AF_INET6, IPPROTO_TCP, inode, "TCPv6");
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000433}
434
Elliott Hughesdc75b012017-07-05 13:54:44 -0700435static const char *
436udp_v6_get(const int fd, const unsigned long inode)
437{
438 return inet_get(fd, AF_INET6, IPPROTO_UDP, inode, "UDPv6");
439}
440
441static const char *
442netlink_get(const int fd, const unsigned long inode)
Fabien Siron814c0d52016-05-17 10:08:47 +0000443{
444 return netlink_send_query(fd, inode)
Elliott Hughesdc75b012017-07-05 13:54:44 -0700445 && receive_responses(fd, inode, SOCK_DIAG_BY_FAMILY,
446 netlink_parse_response, (void *) "NETLINK")
447 ? get_sockaddr_by_inode_cached(inode) : NULL;
Fabien Siron814c0d52016-05-17 10:08:47 +0000448}
449
Fabien Siron802cc282016-06-17 16:29:53 +0000450static const struct {
451 const char *const name;
Elliott Hughesdc75b012017-07-05 13:54:44 -0700452 const char * (*const get)(int, unsigned long);
Fabien Siron802cc282016-06-17 16:29:53 +0000453} protocols[] = {
Elliott Hughesdc75b012017-07-05 13:54:44 -0700454 [SOCK_PROTO_UNIX] = { "UNIX", unix_get },
455 [SOCK_PROTO_TCP] = { "TCP", tcp_v4_get },
456 [SOCK_PROTO_UDP] = { "UDP", udp_v4_get },
457 [SOCK_PROTO_TCPv6] = { "TCPv6", tcp_v6_get },
458 [SOCK_PROTO_UDPv6] = { "UDPv6", udp_v6_get },
459 [SOCK_PROTO_NETLINK] = { "NETLINK", netlink_get }
Fabien Siron802cc282016-06-17 16:29:53 +0000460};
461
462enum sock_proto
463get_proto_by_name(const char *const name)
464{
465 unsigned int i;
466 for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
467 i < ARRAY_SIZE(protocols); ++i) {
468 if (protocols[i].name && !strcmp(name, protocols[i].name))
469 return (enum sock_proto) i;
470 }
471 return SOCK_PROTO_UNKNOWN;
472}
473
Elliott Hughesdc75b012017-07-05 13:54:44 -0700474static const char *
475get_sockaddr_by_inode_uncached(const unsigned long inode,
476 const enum sock_proto proto)
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000477{
Fabien Siron802cc282016-06-17 16:29:53 +0000478 if ((unsigned int) proto >= ARRAY_SIZE(protocols) ||
Elliott Hughesdc75b012017-07-05 13:54:44 -0700479 (proto != SOCK_PROTO_UNKNOWN && !protocols[proto].get))
480 return NULL;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000481
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000482 const int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000483 if (fd < 0)
Elliott Hughesdc75b012017-07-05 13:54:44 -0700484 return NULL;
485 const char *details = NULL;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000486
Fabien Siron802cc282016-06-17 16:29:53 +0000487 if (proto != SOCK_PROTO_UNKNOWN) {
Elliott Hughesdc75b012017-07-05 13:54:44 -0700488 details = protocols[proto].get(fd, inode);
Masatake YAMATOf605e922014-12-10 12:55:06 +0900489 } else {
Fabien Siron802cc282016-06-17 16:29:53 +0000490 unsigned int i;
491 for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
492 i < ARRAY_SIZE(protocols); ++i) {
Elliott Hughesdc75b012017-07-05 13:54:44 -0700493 if (!protocols[i].get)
Fabien Siron802cc282016-06-17 16:29:53 +0000494 continue;
Elliott Hughesdc75b012017-07-05 13:54:44 -0700495 details = protocols[i].get(fd, inode);
496 if (details)
Dmitry V. Levin959205c2014-12-26 23:29:26 +0000497 break;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000498 }
499 }
Dmitry V. Levin959205c2014-12-26 23:29:26 +0000500
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000501 close(fd);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700502 return details;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000503}
Elliott Hughesdc75b012017-07-05 13:54:44 -0700504
505static bool
506print_sockaddr_by_inode_uncached(const unsigned long inode,
507 const enum sock_proto proto)
508{
509 const char *details = get_sockaddr_by_inode_uncached(inode, proto);
510
511 if (details) {
512 tprints(details);
513 return true;
514 }
515
516 if ((unsigned int) proto < ARRAY_SIZE(protocols) &&
517 protocols[proto].name) {
518 tprintf("%s:[%lu]", protocols[proto].name, inode);
519 return true;
520 }
521
522 return false;
523}
524
525/* Given an inode number of a socket, return its protocol details. */
526const char *
527get_sockaddr_by_inode(struct tcb *const tcp, const int fd,
528 const unsigned long inode)
529{
530 const char *details = get_sockaddr_by_inode_cached(inode);
531 return details ? details :
532 get_sockaddr_by_inode_uncached(inode, getfdproto(tcp, fd));
533}
534
535/* Given an inode number of a socket, print out its protocol details. */
536bool
537print_sockaddr_by_inode(struct tcb *const tcp, const int fd,
538 const unsigned long inode)
539{
540 return print_sockaddr_by_inode_cached(inode) ? true :
541 print_sockaddr_by_inode_uncached(inode, getfdproto(tcp, fd));
542}
543
544#ifdef HAVE_LINUX_GENETLINK_H
545/*
546 * Managing the cache for decoding communications of Netlink GENERIC protocol
547 *
548 * As name shown Netlink GENERIC protocol is generic protocol. The
549 * numbers of msg types used in the protocol are not defined
550 * statically. Kernel defines them on demand. So the xlat converted
551 * from header files doesn't help for decoding the protocol. Following
552 * codes are building xlat(dyxlat) at runtime.
553 */
554static bool
555genl_send_dump_families(const int fd)
556{
557 struct {
558 const struct nlmsghdr nlh;
559 struct genlmsghdr gnlh;
560 } req = {
561 .nlh = {
562 .nlmsg_len = sizeof(req),
563 .nlmsg_type = GENL_ID_CTRL,
564 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
565 },
566 .gnlh = {
567 .cmd = CTRL_CMD_GETFAMILY,
568 }
569 };
570 return send_query(fd, &req, sizeof(req));
571}
572
573static int
574genl_parse_families_response(const void *const data,
575 const int data_len, const unsigned long inode,
576 void *opaque_data)
577{
578 struct dyxlat *const dyxlat = opaque_data;
579 const struct genlmsghdr *const gnlh = data;
580 struct rtattr *attr;
581 int rta_len = data_len - NLMSG_LENGTH(sizeof(*gnlh));
582
583 char *name = NULL;
584 unsigned int name_len = 0;
585 uint16_t *id = NULL;
586
587 if (rta_len < 0)
588 return -1;
589 if (gnlh->cmd != CTRL_CMD_NEWFAMILY)
590 return -1;
591 if (gnlh->version != 2)
592 return -1;
593
594 for (attr = (struct rtattr *) (gnlh + 1);
595 RTA_OK(attr, rta_len);
596 attr = RTA_NEXT(attr, rta_len)) {
597 switch (attr->rta_type) {
598 case CTRL_ATTR_FAMILY_NAME:
599 if (!name) {
600 name = RTA_DATA(attr);
601 name_len = RTA_PAYLOAD(attr);
602 }
603 break;
604 case CTRL_ATTR_FAMILY_ID:
605 if (!id && RTA_PAYLOAD(attr) == sizeof(*id))
606 id = RTA_DATA(attr);
607 break;
608 }
609
610 if (name && id) {
611 dyxlat_add_pair(dyxlat, *id, name, name_len);
612 name = NULL;
613 id = NULL;
614 }
615 }
616
617 return 0;
618}
619
620const struct xlat *
621genl_families_xlat(void)
622{
623 static struct dyxlat *dyxlat;
624
625 if (!dyxlat) {
626 dyxlat = dyxlat_alloc(32);
627
628 int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
629 if (fd < 0)
630 goto out;
631
632 if (genl_send_dump_families(fd))
633 receive_responses(fd, 0, GENL_ID_CTRL,
634 genl_parse_families_response, dyxlat);
635 close(fd);
636 }
637
638out:
639 return dyxlat_get(dyxlat);
640}
641
642#else /* !HAVE_LINUX_GENETLINK_H */
643
644const struct xlat *
645genl_families_xlat(void)
646{
647 return NULL;
648}
649#endif