blob: 885119848e25d42ea033ec5ae7b99bb2781f7b26 [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>
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +000029#include "defs.h"
30#include <netinet/in.h>
31#include <sys/socket.h>
32#include <arpa/inet.h>
33#include <linux/netlink.h>
34#include <linux/sock_diag.h>
35#include <linux/inet_diag.h>
Masatake YAMATO120e5db2014-12-24 20:59:31 +090036#include <linux/unix_diag.h>
37#include <linux/rtnetlink.h>
38
Dmitry V. Levind9f7e7a2015-01-09 03:03:39 +000039#if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG
40# define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
41#endif
42
Masatake YAMATO120e5db2014-12-24 20:59:31 +090043#include <sys/un.h>
44#ifndef UNIX_PATH_MAX
45# define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) 0)->sun_path)
46#endif
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +000047
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +000048typedef struct {
49 unsigned long inode;
50 char *details;
51} cache_entry;
52
53#define CACHE_SIZE 1024U
54static cache_entry cache[CACHE_SIZE];
55#define CACHE_MASK (CACHE_SIZE - 1)
56
57static int
Dmitry V. Levinea218232016-02-10 17:43:09 +000058cache_and_print_inode_details(const unsigned long inode, char *const details)
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +000059{
60 cache_entry *e = &cache[inode & CACHE_MASK];
61 free(e->details);
62 e->inode = inode;
63 e->details = details;
64
65 tprints(details);
66 return 1;
67}
68
69bool
70print_sockaddr_by_inode_cached(const unsigned long inode)
71{
Dmitry V. Levinea218232016-02-10 17:43:09 +000072 const cache_entry *const e = &cache[inode & CACHE_MASK];
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +000073 if (e && inode == e->inode) {
74 tprints(e->details);
75 return true;
76 }
77 return false;
78}
79
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +000080static bool
Fabien Siron071b9272016-05-08 10:45:52 +000081send_query(const int fd, void *req, size_t req_size)
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +000082{
Dmitry V. Levinaf534b82014-11-21 19:59:16 +000083 struct sockaddr_nl nladdr = {
84 .nl_family = AF_NETLINK
85 };
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +000086 struct iovec iov = {
Fabien Siron071b9272016-05-08 10:45:52 +000087 .iov_base = req,
88 .iov_len = req_size
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +000089 };
Dmitry V. Levinea218232016-02-10 17:43:09 +000090 const struct msghdr msg = {
91 .msg_name = &nladdr,
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +000092 .msg_namelen = sizeof(nladdr),
93 .msg_iov = &iov,
94 .msg_iovlen = 1
95 };
96
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +000097 for (;;) {
98 if (sendmsg(fd, &msg, 0) < 0) {
99 if (errno == EINTR)
100 continue;
101 return false;
102 }
103 return true;
104 }
105}
106
Fabien Siron071b9272016-05-08 10:45:52 +0000107static bool
108inet_send_query(const int fd, const int family, const int proto)
109{
110 struct {
111 const struct nlmsghdr nlh;
112 const struct inet_diag_req_v2 idr;
113 } req = {
114 .nlh = {
115 .nlmsg_len = sizeof(req),
116 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
117 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
118 },
119 .idr = {
120 .sdiag_family = family,
121 .sdiag_protocol = proto,
122 .idiag_states = -1
123 }
124 };
125 return send_query(fd, &req, sizeof(req));
126}
127
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000128static int
Dmitry V. Levinea218232016-02-10 17:43:09 +0000129inet_parse_response(const char *const proto_name, const void *const data,
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000130 const int data_len, const unsigned long inode)
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000131{
Dmitry V. Levinea218232016-02-10 17:43:09 +0000132 const struct inet_diag_msg *const diag_msg = data;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000133 static const char zero_addr[sizeof(struct in6_addr)];
134 socklen_t addr_size, text_size;
135
Dmitry V. Levincc09ba12016-01-28 23:46:56 +0000136 if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000137 return -1;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000138 if (diag_msg->idiag_inode != inode)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000139 return 0;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000140
141 switch(diag_msg->idiag_family) {
142 case AF_INET:
143 addr_size = sizeof(struct in_addr);
144 text_size = INET_ADDRSTRLEN;
145 break;
146 case AF_INET6:
147 addr_size = sizeof(struct in6_addr);
148 text_size = INET6_ADDRSTRLEN;
149 break;
150 default:
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000151 return -1;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000152 }
153
154 char src_buf[text_size];
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000155 char *details;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000156
157 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_src,
158 src_buf, text_size))
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000159 return -1;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000160
161 if (diag_msg->id.idiag_dport ||
162 memcmp(zero_addr, diag_msg->id.idiag_dst, addr_size)) {
163 char dst_buf[text_size];
164
165 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_dst,
166 dst_buf, text_size))
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000167 return -1;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000168
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000169 if (asprintf(&details, "%s:[%s:%u->%s:%u]", proto_name,
170 src_buf, ntohs(diag_msg->id.idiag_sport),
171 dst_buf, ntohs(diag_msg->id.idiag_dport)) < 0)
172 return false;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000173 } else {
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000174 if (asprintf(&details, "%s:[%s:%u]", proto_name, src_buf,
175 ntohs(diag_msg->id.idiag_sport)) < 0)
176 return false;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000177 }
178
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000179 return cache_and_print_inode_details(inode, details);
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000180}
181
182static bool
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900183receive_responses(const int fd, const unsigned long inode,
Dmitry V. Levin959205c2014-12-26 23:29:26 +0000184 const char *proto_name,
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000185 int (* parser) (const char *, const void *,
186 int, unsigned long))
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000187{
Dmitry V. Levin301c65c2015-03-02 23:39:41 +0000188 static long buf[8192 / sizeof(long)];
Dmitry V. Levinaf534b82014-11-21 19:59:16 +0000189 struct sockaddr_nl nladdr = {
190 .nl_family = AF_NETLINK
191 };
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000192 struct iovec iov = {
193 .iov_base = buf,
194 .iov_len = sizeof(buf)
195 };
Dmitry V. Levin2215c3e2016-01-27 21:35:50 +0000196 int flags = 0;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000197
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000198 for (;;) {
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000199 struct msghdr msg = {
Dmitry V. Levinea218232016-02-10 17:43:09 +0000200 .msg_name = &nladdr,
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000201 .msg_namelen = sizeof(nladdr),
202 .msg_iov = &iov,
Dmitry V. Levinaf534b82014-11-21 19:59:16 +0000203 .msg_iovlen = 1
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000204 };
205
Dmitry V. Levinea218232016-02-10 17:43:09 +0000206 ssize_t ret = recvmsg(fd, &msg, flags);
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000207 if (ret < 0) {
208 if (errno == EINTR)
209 continue;
210 return false;
211 }
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000212
Dmitry V. Levinea218232016-02-10 17:43:09 +0000213 const struct nlmsghdr *h = (struct nlmsghdr *) buf;
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000214 if (!NLMSG_OK(h, ret))
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000215 return false;
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000216 for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) {
217 if (h->nlmsg_type != SOCK_DIAG_BY_FAMILY)
218 return false;
Dmitry V. Levinea218232016-02-10 17:43:09 +0000219 const int rc = parser(proto_name, NLMSG_DATA(h),
220 h->nlmsg_len, inode);
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000221 if (rc > 0)
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000222 return true;
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000223 if (rc < 0)
224 return false;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000225 }
Dmitry V. Levin2215c3e2016-01-27 21:35:50 +0000226 flags = MSG_DONTWAIT;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000227 }
228}
229
Masatake YAMATOf605e922014-12-10 12:55:06 +0900230static bool
Dmitry V. Levin959205c2014-12-26 23:29:26 +0000231inet_print(const int fd, const int family, const int protocol,
232 const unsigned long inode, const char *proto_name)
Masatake YAMATOf605e922014-12-10 12:55:06 +0900233{
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900234 return inet_send_query(fd, family, protocol)
Dmitry V. Levin959205c2014-12-26 23:29:26 +0000235 && receive_responses(fd, inode, proto_name, inet_parse_response);
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900236}
237
238static bool
239unix_send_query(const int fd, const unsigned long inode)
240{
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900241 struct {
Dmitry V. Levinea218232016-02-10 17:43:09 +0000242 const struct nlmsghdr nlh;
243 const struct unix_diag_req udr;
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900244 } req = {
245 .nlh = {
246 .nlmsg_len = sizeof(req),
247 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
Dmitry V. Levin10c61e32016-02-19 01:30:34 +0000248 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900249 },
250 .udr = {
251 .sdiag_family = AF_UNIX,
252 .udiag_ino = inode,
253 .udiag_states = -1,
Dmitry V. Levin10c61e32016-02-19 01:30:34 +0000254 .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900255 }
256 };
Fabien Siron071b9272016-05-08 10:45:52 +0000257 return send_query(fd, &req, sizeof(req));
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900258}
259
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000260static int
261unix_parse_response(const char *proto_name, const void *data,
262 const int data_len, const unsigned long inode)
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900263{
264 const struct unix_diag_msg *diag_msg = data;
265 struct rtattr *attr;
266 int rta_len = data_len - NLMSG_LENGTH(sizeof(*diag_msg));
267 uint32_t peer = 0;
268 size_t path_len = 0;
269 char path[UNIX_PATH_MAX + 1];
270
Dmitry V. Levin3d0f55e2016-01-24 01:46:40 +0300271 if (rta_len < 0)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000272 return -1;
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900273 if (diag_msg->udiag_ino != inode)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000274 return 0;
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900275 if (diag_msg->udiag_family != AF_UNIX)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000276 return -1;
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900277
278 for (attr = (struct rtattr *) (diag_msg + 1);
279 RTA_OK(attr, rta_len);
280 attr = RTA_NEXT(attr, rta_len)) {
281 switch (attr->rta_type) {
282 case UNIX_DIAG_NAME:
283 if (!path_len) {
284 path_len = RTA_PAYLOAD(attr);
285 if (path_len > UNIX_PATH_MAX)
286 path_len = UNIX_PATH_MAX;
287 memcpy(path, RTA_DATA(attr), path_len);
288 path[path_len] = '\0';
289 }
290 break;
291 case UNIX_DIAG_PEER:
292 if (RTA_PAYLOAD(attr) >= 4)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000293 peer = *(uint32_t *) RTA_DATA(attr);
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900294 break;
295 }
296 }
297
298 /*
299 * print obtained information in the following format:
300 * "UNIX:[" SELF_INODE [ "->" PEER_INODE ][ "," SOCKET_FILE ] "]"
301 */
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000302 if (!peer && !path_len)
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000303 return -1;
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000304
305 char peer_str[3 + sizeof(peer) * 3];
306 if (peer)
307 snprintf(peer_str, sizeof(peer_str), "->%u", peer);
308 else
309 peer_str[0] = '\0';
310
311 const char *path_str;
312 if (path_len) {
313 char *outstr = alloca(4 * path_len + 4);
314
315 outstr[0] = ',';
316 if (path[0] == '\0') {
317 outstr[1] = '@';
318 string_quote(path + 1, outstr + 2,
319 path_len - 1, QUOTE_0_TERMINATED);
320 } else {
321 string_quote(path, outstr + 1,
322 path_len, QUOTE_0_TERMINATED);
323 }
324 path_str = outstr;
325 } else {
326 path_str = "";
327 }
328
329 char *details;
330 if (asprintf(&details, "%s:[%lu%s%s]", proto_name, inode,
331 peer_str, path_str) < 0)
332 return -1;
333
334 return cache_and_print_inode_details(inode, details);
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900335}
336
337static bool
Dmitry V. Levinea218232016-02-10 17:43:09 +0000338unix_print(const int fd, const unsigned long inode)
Masatake YAMATO120e5db2014-12-24 20:59:31 +0900339{
340 return unix_send_query(fd, inode)
Dmitry V. Levin959205c2014-12-26 23:29:26 +0000341 && receive_responses(fd, inode, "UNIX", unix_parse_response);
Masatake YAMATOf605e922014-12-10 12:55:06 +0900342}
343
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000344static bool
345tcp_v4_print(const int fd, const unsigned long inode)
346{
347 return inet_print(fd, AF_INET, IPPROTO_TCP, inode, "TCP");
348}
349
350static bool
351udp_v4_print(const int fd, const unsigned long inode)
352{
353 return inet_print(fd, AF_INET, IPPROTO_UDP, inode, "UDP");
354}
355
356static bool
357tcp_v6_print(const int fd, const unsigned long inode)
358{
359 return inet_print(fd, AF_INET6, IPPROTO_TCP, inode, "TCPv6");
360}
361
362static bool
363udp_v6_print(const int fd, const unsigned long inode)
364{
365 return inet_print(fd, AF_INET6, IPPROTO_UDP, inode, "UDPv6");
366}
367
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000368/* Given an inode number of a socket, print out the details
369 * of the ip address and port. */
370bool
Dmitry V. Levinea218232016-02-10 17:43:09 +0000371print_sockaddr_by_inode(const unsigned long inode, const char *const proto_name)
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000372{
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000373 static const struct {
374 const char *const name;
375 bool (*const print)(int, unsigned long);
376 } protocols[] = {
377 { "TCP", tcp_v4_print },
378 { "UDP", udp_v4_print },
379 { "TCPv6", tcp_v6_print },
380 { "UDPv6", udp_v6_print },
381 { "UNIX", unix_print }
382 };
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000383
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000384 const int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000385 if (fd < 0)
386 return false;
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000387 bool r = false;
388 unsigned int i;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000389
Masatake YAMATOf605e922014-12-10 12:55:06 +0900390 if (proto_name) {
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000391 for (i = 0; i < ARRAY_SIZE(protocols); ++i) {
392 if (strcmp(proto_name, protocols[i].name) == 0) {
393 r = protocols[i].print(fd, inode);
394 break;
395 }
396 }
Dmitry V. Levinea8b8e32016-01-23 16:35:02 +0000397
398 if (!r) {
399 tprintf("%s:[%lu]", proto_name, inode);
400 r = true;
401 }
Masatake YAMATOf605e922014-12-10 12:55:06 +0900402 } else {
Dmitry V. Levin959205c2014-12-26 23:29:26 +0000403 for (i = 0; i < ARRAY_SIZE(protocols); ++i) {
Dmitry V. Levinbcf59752016-02-10 17:39:57 +0000404 if ((r = protocols[i].print(fd, inode)))
Dmitry V. Levin959205c2014-12-26 23:29:26 +0000405 break;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000406 }
407 }
Dmitry V. Levin959205c2014-12-26 23:29:26 +0000408
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000409 close(fd);
Masatake YAMATOf605e922014-12-10 12:55:06 +0900410 return r;
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000411}