blob: 4fd8728ce79a3bf150e097cbbbeb9a23eb466a72 [file] [log] [blame]
Eric Andersen31a0ece2001-10-31 11:00:46 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini netstat implementation(s) for busybox
4 * based in part on the netstat implementation from net-tools.
5 *
Eric Andersen51b8bd62002-07-03 11:46:38 +00006 * Copyright (C) 2002 by Bart Visscher <magick@linux-fan.com>
Eric Andersen31a0ece2001-10-31 11:00:46 +00007 *
Eric Andersen51b8bd62002-07-03 11:46:38 +00008 * 2002-04-20
9 * IPV6 support added by Bart Visscher <magick@linux-fan.com>
"Robert P. J. Day"2819f752006-07-11 11:32:31 +000010 *
Denis Vlasenko6e69e422008-07-27 12:10:07 +000011 * 2008-07-10
12 * optional '-p' flag support ported from net-tools by G. Somlo <somlo@cmu.edu>
13 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020014 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen31a0ece2001-10-31 11:00:46 +000015 */
16
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000017#include "libbb.h"
Rob Landleyc9c1a412006-07-12 19:17:55 +000018#include "inet_common.h"
Eric Andersen31a0ece2001-10-31 11:00:46 +000019
Bob Dunlop55a046b2010-10-27 02:12:29 +020020//usage:#define netstat_trivial_usage
21//usage: "[-"IF_ROUTE("r")"al] [-tuwx] [-en"IF_FEATURE_NETSTAT_WIDE("W")IF_FEATURE_NETSTAT_PRG("p")"]"
22//usage:#define netstat_full_usage "\n\n"
23//usage: "Display networking information\n"
24//usage: "\nOptions:"
25//usage: IF_ROUTE(
26//usage: "\n -r Routing table"
27//usage: )
28//usage: "\n -a All sockets"
29//usage: "\n -l Listening sockets"
30//usage: "\n Else: connected sockets"
31//usage: "\n -t TCP sockets"
32//usage: "\n -u UDP sockets"
33//usage: "\n -w Raw sockets"
34//usage: "\n -x Unix sockets"
35//usage: "\n Else: all socket types"
36//usage: "\n -e Other/more information"
37//usage: "\n -n Don't resolve names"
38//usage: IF_FEATURE_NETSTAT_WIDE(
39//usage: "\n -W Wide display"
40//usage: )
41//usage: IF_FEATURE_NETSTAT_PRG(
42//usage: "\n -p Show PID/program name for sockets"
43//usage: )
44
Denis Vlasenko6e69e422008-07-27 12:10:07 +000045#define NETSTAT_OPTS "laentuwx" \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000046 IF_ROUTE( "r") \
47 IF_FEATURE_NETSTAT_WIDE("W") \
48 IF_FEATURE_NETSTAT_PRG( "p")
Denis Vlasenko6e69e422008-07-27 12:10:07 +000049
Denis Vlasenko418a7fb2007-05-15 23:57:46 +000050enum {
Denis Vlasenko6e69e422008-07-27 12:10:07 +000051 OPT_sock_listen = 1 << 0, // l
52 OPT_sock_all = 1 << 1, // a
53 OPT_extended = 1 << 2, // e
54 OPT_noresolve = 1 << 3, // n
55 OPT_sock_tcp = 1 << 4, // t
56 OPT_sock_udp = 1 << 5, // u
57 OPT_sock_raw = 1 << 6, // w
58 OPT_sock_unix = 1 << 7, // x
Bob Dunlop55a046b2010-10-27 02:12:29 +020059 OPTBIT_x = 7,
60 IF_ROUTE( OPTBIT_ROUTE,)
61 IF_FEATURE_NETSTAT_WIDE(OPTBIT_WIDE ,)
62 IF_FEATURE_NETSTAT_PRG( OPTBIT_PRG ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000063 OPT_route = IF_ROUTE( (1 << OPTBIT_ROUTE)) + 0, // r
64 OPT_wide = IF_FEATURE_NETSTAT_WIDE((1 << OPTBIT_WIDE )) + 0, // W
65 OPT_prg = IF_FEATURE_NETSTAT_PRG( (1 << OPTBIT_PRG )) + 0, // p
Denis Vlasenko418a7fb2007-05-15 23:57:46 +000066};
Denis Vlasenko418a7fb2007-05-15 23:57:46 +000067
Denis Vlasenko703e2022007-01-22 14:12:08 +000068#define NETSTAT_CONNECTED 0x01
69#define NETSTAT_LISTENING 0x02
70#define NETSTAT_NUMERIC 0x04
Denis Vlasenko67b23e62006-10-03 21:00:06 +000071/* Must match getopt32 option string */
Denis Vlasenko703e2022007-01-22 14:12:08 +000072#define NETSTAT_TCP 0x10
73#define NETSTAT_UDP 0x20
74#define NETSTAT_RAW 0x40
75#define NETSTAT_UNIX 0x80
76#define NETSTAT_ALLPROTO (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW|NETSTAT_UNIX)
Eric Andersen31a0ece2001-10-31 11:00:46 +000077
Eric Andersen31a0ece2001-10-31 11:00:46 +000078
Eric Andersen31a0ece2001-10-31 11:00:46 +000079enum {
Denis Vlasenko703e2022007-01-22 14:12:08 +000080 TCP_ESTABLISHED = 1,
81 TCP_SYN_SENT,
82 TCP_SYN_RECV,
83 TCP_FIN_WAIT1,
84 TCP_FIN_WAIT2,
85 TCP_TIME_WAIT,
86 TCP_CLOSE,
87 TCP_CLOSE_WAIT,
88 TCP_LAST_ACK,
89 TCP_LISTEN,
Denis Vlasenko6e69e422008-07-27 12:10:07 +000090 TCP_CLOSING, /* now a valid state */
Eric Andersen31a0ece2001-10-31 11:00:46 +000091};
92
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000093static const char *const tcp_state[] = {
Denis Vlasenko703e2022007-01-22 14:12:08 +000094 "",
95 "ESTABLISHED",
96 "SYN_SENT",
97 "SYN_RECV",
98 "FIN_WAIT1",
99 "FIN_WAIT2",
100 "TIME_WAIT",
101 "CLOSE",
102 "CLOSE_WAIT",
103 "LAST_ACK",
104 "LISTEN",
105 "CLOSING"
Eric Andersen31a0ece2001-10-31 11:00:46 +0000106};
107
108typedef enum {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000109 SS_FREE = 0, /* not allocated */
110 SS_UNCONNECTED, /* unconnected to any socket */
111 SS_CONNECTING, /* in process of connecting */
112 SS_CONNECTED, /* connected to socket */
113 SS_DISCONNECTING /* in process of disconnecting */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000114} socket_state;
115
Denis Vlasenko703e2022007-01-22 14:12:08 +0000116#define SO_ACCEPTCON (1<<16) /* performed a listen */
117#define SO_WAITDATA (1<<17) /* wait data to read */
118#define SO_NOSPACE (1<<18) /* no space to write */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000119
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000120/* Standard printout size */
121#define PRINT_IP_MAX_SIZE 23
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000122#define PRINT_NET_CONN "%s %6ld %6ld %-23s %-23s %-12s"
123#define PRINT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-23s %-23s State "
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000124
125/* When there are IPv6 connections the IPv6 addresses will be
126 * truncated to none-recognition. The '-W' option makes the
127 * address columns wide enough to accomodate for longest possible
128 * IPv6 addresses, i.e. addresses of the form
129 * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd
130 */
131#define PRINT_IP_MAX_SIZE_WIDE 51 /* INET6_ADDRSTRLEN + 5 for the port number */
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000132#define PRINT_NET_CONN_WIDE "%s %6ld %6ld %-51s %-51s %-12s"
133#define PRINT_NET_CONN_HEADER_WIDE "\nProto Recv-Q Send-Q %-51s %-51s State "
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000134
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000135#define PROGNAME_WIDTH 20
136#define PROGNAME_WIDTH_STR "20"
137/* PROGNAME_WIDTH chars: 12345678901234567890 */
138#define PROGNAME_BANNER "PID/Program name "
139
140struct prg_node {
141 struct prg_node *next;
142 long inode;
143 char name[PROGNAME_WIDTH];
144};
145
146#define PRG_HASH_SIZE 211
147
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000148struct globals {
149 const char *net_conn_line;
150 smallint flags;
151#if ENABLE_FEATURE_NETSTAT_PRG
152 smallint prg_cache_loaded;
153 struct prg_node *prg_hash[PRG_HASH_SIZE];
154#endif
155};
156#define G (*ptr_to_globals)
157#define flags (G.flags )
158#define net_conn_line (G.net_conn_line )
159#define prg_hash (G.prg_hash )
160#define prg_cache_loaded (G.prg_cache_loaded)
161#define INIT_G() do { \
162 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
163 flags = NETSTAT_CONNECTED | NETSTAT_ALLPROTO; \
164 net_conn_line = PRINT_NET_CONN; \
165} while (0)
166
167
168#if ENABLE_FEATURE_NETSTAT_PRG
169
170/* Deliberately truncating long to unsigned *int* */
171#define PRG_HASHIT(x) ((unsigned)(x) % PRG_HASH_SIZE)
172
173#define print_progname_banner() do { \
174 if (option_mask32 & OPT_prg) printf(PROGNAME_BANNER); \
175} while (0)
176
177static void prg_cache_add(long inode, char *name)
178{
179 unsigned hi = PRG_HASHIT(inode);
180 struct prg_node **pnp, *pn;
181
182 prg_cache_loaded = 2;
183 for (pnp = prg_hash + hi; (pn = *pnp) != NULL; pnp = &pn->next) {
184 if (pn->inode == inode) {
185 /* Some warning should be appropriate here
186 as we got multiple processes for one i-node */
187 return;
188 }
189 }
190 *pnp = xzalloc(sizeof(struct prg_node));
191 pn = *pnp;
192 pn->inode = inode;
193 safe_strncpy(pn->name, name, PROGNAME_WIDTH);
194}
195
196static const char *prg_cache_get(long inode)
197{
198 unsigned hi = PRG_HASHIT(inode);
199 struct prg_node *pn;
200
201 for (pn = prg_hash[hi]; pn; pn = pn->next)
202 if (pn->inode == inode)
203 return pn->name;
204 return "-";
205}
206
207#if ENABLE_FEATURE_CLEAN_UP
208static void prg_cache_clear(void)
209{
210 struct prg_node **pnp, *pn;
211
212 for (pnp = prg_hash; pnp < prg_hash + PRG_HASH_SIZE; pnp++) {
213 while ((pn = *pnp) != NULL) {
214 *pnp = pn->next;
215 free(pn);
216 }
217 }
218}
219#else
220#define prg_cache_clear() ((void)0)
221#endif
222
Denis Vlasenkocf8b1ef2008-07-27 14:15:42 +0000223static long extract_socket_inode(const char *lname)
224{
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000225 long inode = -1;
226
227 if (strncmp(lname, "socket:[", sizeof("socket:[")-1) == 0) {
228 /* "socket:[12345]", extract the "12345" as inode */
229 inode = bb_strtol(lname + sizeof("socket:[")-1, (char**)&lname, 0);
230 if (*lname != ']')
231 inode = -1;
232 } else if (strncmp(lname, "[0000]:", sizeof("[0000]:")-1) == 0) {
233 /* "[0000]:12345", extract the "12345" as inode */
234 inode = bb_strtol(lname + sizeof("[0000]:")-1, NULL, 0);
235 if (errno) /* not NUL terminated? */
236 inode = -1;
237 }
238
239#if 0 /* bb_strtol returns all-ones bit pattern on ERANGE anyway */
240 if (errno == ERANGE)
241 inode = -1;
242#endif
243 return inode;
244}
245
246static int FAST_FUNC file_act(const char *fileName,
247 struct stat *statbuf UNUSED_PARAM,
Bob Dunlop55a046b2010-10-27 02:12:29 +0200248 void *pid_slash_progname,
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000249 int depth UNUSED_PARAM)
250{
251 char *linkname;
252 long inode;
253
254 linkname = xmalloc_readlink(fileName);
255 if (linkname != NULL) {
256 inode = extract_socket_inode(linkname);
257 free(linkname);
258 if (inode >= 0)
Bob Dunlop55a046b2010-10-27 02:12:29 +0200259 prg_cache_add(inode, (char *)pid_slash_progname);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000260 }
261 return TRUE;
262}
263
264static int FAST_FUNC dir_act(const char *fileName,
265 struct stat *statbuf UNUSED_PARAM,
266 void *userData UNUSED_PARAM,
267 int depth)
268{
Bob Dunlop55a046b2010-10-27 02:12:29 +0200269 const char *pid;
270 char *p, *pid_slash_progname;
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000271 char cmdline_buf[512];
272 int i;
273
274 if (depth == 0) /* "/proc" itself */
275 return TRUE; /* continue looking one level below /proc */
276
Bob Dunlop55a046b2010-10-27 02:12:29 +0200277 pid = fileName + sizeof("/proc/")-1; /* point after "/proc/" */
278 if (!isdigit(pid[0])) /* skip /proc entries which aren't processes */
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000279 return SKIP;
280
281 p = concat_path_file(fileName, "cmdline"); /* "/proc/PID/cmdline" */
282 i = open_read_close(p, cmdline_buf, sizeof(cmdline_buf) - 1);
283 free(p);
284 if (i < 0)
285 return FALSE;
286 cmdline_buf[i] = '\0';
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000287
288 /* go through all files in /proc/PID/fd */
Bob Dunlop55a046b2010-10-27 02:12:29 +0200289 pid_slash_progname = concat_path_file(pid, bb_basename(cmdline_buf)); /* "PID/argv0" */
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000290 p = concat_path_file(fileName, "fd");
291 i = recursive_action(p, ACTION_RECURSE | ACTION_QUIET,
Bob Dunlop55a046b2010-10-27 02:12:29 +0200292 file_act, NULL, (void *)pid_slash_progname, 0);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000293 free(p);
Bob Dunlop55a046b2010-10-27 02:12:29 +0200294 free(pid_slash_progname);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000295
296 if (!i)
Bob Dunlop55a046b2010-10-27 02:12:29 +0200297 return FALSE; /* signal permissions error to caller */
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000298
Bob Dunlop55a046b2010-10-27 02:12:29 +0200299 return SKIP; /* caller should not recurse further into this dir */
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000300}
301
302static void prg_cache_load(void)
303{
304 int load_ok;
305
306 prg_cache_loaded = 1;
307 load_ok = recursive_action("/proc", ACTION_RECURSE | ACTION_QUIET,
308 NULL, dir_act, NULL, 0);
309 if (load_ok)
310 return;
311
312 if (prg_cache_loaded == 1)
313 bb_error_msg("can't scan /proc - are you root?");
314 else
315 bb_error_msg("showing only processes with your user ID");
316}
317
318#else
319
320#define prg_cache_clear() ((void)0)
321#define print_progname_banner() ((void)0)
322
323#endif //ENABLE_FEATURE_NETSTAT_PRG
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000324
325
326#if ENABLE_FEATURE_IPV6
327static void build_ipv6_addr(char* local_addr, struct sockaddr_in6* localaddr)
328{
329 char addr6[INET6_ADDRSTRLEN];
330 struct in6_addr in6;
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000331
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000332 sscanf(local_addr, "%08X%08X%08X%08X",
333 &in6.s6_addr32[0], &in6.s6_addr32[1],
334 &in6.s6_addr32[2], &in6.s6_addr32[3]);
335 inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6));
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100336 inet_pton(AF_INET6, addr6, &localaddr->sin6_addr);
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000337
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000338 localaddr->sin6_family = AF_INET6;
339}
340#endif
341
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000342static void build_ipv4_addr(char* local_addr, struct sockaddr_in* localaddr)
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000343{
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100344 sscanf(local_addr, "%X", &localaddr->sin_addr.s_addr);
345 localaddr->sin_family = AF_INET;
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000346}
347
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000348static const char *get_sname(int port, const char *proto, int numeric)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000349{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000350 if (!port)
351 return "*";
352 if (!numeric) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000353 struct servent *se = getservbyport(port, proto);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000354 if (se)
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000355 return se->s_name;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000356 }
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000357 /* hummm, we may return static buffer here!! */
358 return itoa(ntohs(port));
Eric Andersen31a0ece2001-10-31 11:00:46 +0000359}
360
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000361static char *ip_port_str(struct sockaddr *addr, int port, const char *proto, int numeric)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000362{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000363 char *host, *host_port;
Eric Andersencd8c4362001-11-10 11:22:46 +0000364
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000365 /* Code which used "*" for INADDR_ANY is removed: it's ambiguous
366 * in IPv6, while "0.0.0.0" is not. */
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000367
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000368 host = numeric ? xmalloc_sockaddr2dotted_noport(addr)
369 : xmalloc_sockaddr2host_noport(addr);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000370
371 host_port = xasprintf("%s:%s", host, get_sname(htons(port), proto, numeric));
372 free(host);
373 return host_port;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000374}
375
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000376struct inet_params {
377 int local_port, rem_port, state, uid;
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100378 union {
379 struct sockaddr sa;
380 struct sockaddr_in sin;
Denis Vlasenko703e2022007-01-22 14:12:08 +0000381#if ENABLE_FEATURE_IPV6
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100382 struct sockaddr_in6 sin6;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000383#endif
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100384 } localaddr, remaddr;
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000385 unsigned long rxq, txq, inode;
386};
Eric Andersen31a0ece2001-10-31 11:00:46 +0000387
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000388static int scan_inet_proc_line(struct inet_params *param, char *line)
389{
390 int num;
391 char local_addr[64], rem_addr[64];
Eric Andersen31a0ece2001-10-31 11:00:46 +0000392
Eric Andersen31a0ece2001-10-31 11:00:46 +0000393 num = sscanf(line,
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000394 "%*d: %64[0-9A-Fa-f]:%X "
395 "%64[0-9A-Fa-f]:%X %X "
396 "%lX:%lX %*X:%*X "
397 "%*X %d %*d %ld ",
398 local_addr, &param->local_port,
399 rem_addr, &param->rem_port, &param->state,
400 &param->txq, &param->rxq,
401 &param->uid, &param->inode);
402 if (num < 9) {
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000403 return 1; /* error */
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000404 }
405
Eric Andersen31a0ece2001-10-31 11:00:46 +0000406 if (strlen(local_addr) > 8) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000407#if ENABLE_FEATURE_IPV6
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100408 build_ipv6_addr(local_addr, &param->localaddr.sin6);
409 build_ipv6_addr(rem_addr, &param->remaddr.sin6);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000410#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000411 } else {
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100412 build_ipv4_addr(local_addr, &param->localaddr.sin);
413 build_ipv4_addr(rem_addr, &param->remaddr.sin);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000414 }
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000415 return 0;
416}
Eric Andersen31a0ece2001-10-31 11:00:46 +0000417
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000418static void print_inet_line(struct inet_params *param,
419 const char *state_str, const char *proto, int is_connected)
420{
421 if ((is_connected && (flags & NETSTAT_CONNECTED))
422 || (!is_connected && (flags & NETSTAT_LISTENING))
Denis Vlasenko703e2022007-01-22 14:12:08 +0000423 ) {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000424 char *l = ip_port_str(
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100425 &param->localaddr.sa, param->local_port,
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000426 proto, flags & NETSTAT_NUMERIC);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000427 char *r = ip_port_str(
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100428 &param->remaddr.sa, param->rem_port,
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000429 proto, flags & NETSTAT_NUMERIC);
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000430 printf(net_conn_line,
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000431 proto, param->rxq, param->txq, l, r, state_str);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000432#if ENABLE_FEATURE_NETSTAT_PRG
433 if (option_mask32 & OPT_prg)
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000434 printf("%."PROGNAME_WIDTH_STR"s", prg_cache_get(param->inode));
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000435#endif
436 bb_putchar('\n');
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000437 free(l);
438 free(r);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000439 }
440}
441
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000442static int FAST_FUNC tcp_do_one(char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000443{
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000444 struct inet_params param;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000445
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000446 if (scan_inet_proc_line(&param, line))
447 return 1;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000448
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000449 print_inet_line(&param, tcp_state[param.state], "tcp", param.rem_port);
450 return 0;
451}
Eric Andersen31a0ece2001-10-31 11:00:46 +0000452
Denis Vlasenko703e2022007-01-22 14:12:08 +0000453#if ENABLE_FEATURE_IPV6
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100454# define NOT_NULL_ADDR(A) ( \
455 ( (A.sa.sa_family == AF_INET6) \
456 && (A.sin6.sin6_addr.s6_addr32[0] | A.sin6.sin6_addr.s6_addr32[1] | \
457 A.sin6.sin6_addr.s6_addr32[2] | A.sin6.sin6_addr.s6_addr32[3]) \
458 ) || ( \
459 (A.sa.sa_family == AF_INET) \
460 && A.sin.sin_addr.s_addr != 0 \
461 ) \
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000462)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000463#else
Denys Vlasenko45e97922010-02-19 08:29:32 +0100464# define NOT_NULL_ADDR(A) (A.sin.sin_addr.s_addr)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000465#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000466
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000467static int FAST_FUNC udp_do_one(char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000468{
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000469 int have_remaddr;
470 const char *state_str;
471 struct inet_params param;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000472
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000473 if (scan_inet_proc_line(&param, line))
474 return 1;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000475
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000476 state_str = "UNKNOWN";
477 switch (param.state) {
478 case TCP_ESTABLISHED:
479 state_str = "ESTABLISHED";
480 break;
481 case TCP_CLOSE:
482 state_str = "";
483 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000484 }
485
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100486 have_remaddr = NOT_NULL_ADDR(param.remaddr);
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000487 print_inet_line(&param, state_str, "udp", have_remaddr);
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000488 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000489}
490
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000491static int FAST_FUNC raw_do_one(char *line)
492{
493 int have_remaddr;
494 struct inet_params param;
495
496 if (scan_inet_proc_line(&param, line))
497 return 1;
498
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100499 have_remaddr = NOT_NULL_ADDR(param.remaddr);
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000500 print_inet_line(&param, itoa(param.state), "raw", have_remaddr);
501 return 0;
502}
503
504static int FAST_FUNC unix_do_one(char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000505{
Eric Andersen31a0ece2001-10-31 11:00:46 +0000506 unsigned long refcnt, proto, unix_flags;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000507 unsigned long inode;
508 int type, state;
509 int num, path_ofs;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000510 const char *ss_proto, *ss_state, *ss_type;
511 char ss_flags[32];
Eric Andersen31a0ece2001-10-31 11:00:46 +0000512
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000513 /* 2.6.15 may report lines like "... @/tmp/fam-user-^@^@^@^@^@^@^@..."
Denis Vlasenko474d1c52008-01-07 19:06:47 +0000514 * Other users report long lines filled by NUL bytes.
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000515 * (those ^@ are NUL bytes too). We see them as empty lines. */
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000516 if (!line[0])
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000517 return 0;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000518
519 path_ofs = 0; /* paranoia */
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000520 num = sscanf(line, "%*p: %lX %lX %lX %X %X %lu %n",
521 &refcnt, &proto, &unix_flags, &type, &state, &inode, &path_ofs);
522 if (num < 6) {
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000523 return 1; /* error */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000524 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000525 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000526 if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000527 if (!(flags & NETSTAT_LISTENING))
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000528 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000529 } else {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000530 if (!(flags & NETSTAT_CONNECTED))
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000531 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000532 }
533 }
534
535 switch (proto) {
536 case 0:
537 ss_proto = "unix";
538 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000539 default:
540 ss_proto = "??";
541 }
542
543 switch (type) {
544 case SOCK_STREAM:
545 ss_type = "STREAM";
546 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000547 case SOCK_DGRAM:
548 ss_type = "DGRAM";
549 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000550 case SOCK_RAW:
551 ss_type = "RAW";
552 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000553 case SOCK_RDM:
554 ss_type = "RDM";
555 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000556 case SOCK_SEQPACKET:
557 ss_type = "SEQPACKET";
558 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000559 default:
560 ss_type = "UNKNOWN";
561 }
562
563 switch (state) {
564 case SS_FREE:
565 ss_state = "FREE";
566 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000567 case SS_UNCONNECTED:
568 /*
569 * Unconnected sockets may be listening
570 * for something.
571 */
572 if (unix_flags & SO_ACCEPTCON) {
573 ss_state = "LISTENING";
574 } else {
575 ss_state = "";
576 }
577 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000578 case SS_CONNECTING:
579 ss_state = "CONNECTING";
580 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000581 case SS_CONNECTED:
582 ss_state = "CONNECTED";
583 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000584 case SS_DISCONNECTING:
585 ss_state = "DISCONNECTING";
586 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000587 default:
588 ss_state = "UNKNOWN";
589 }
590
591 strcpy(ss_flags, "[ ");
592 if (unix_flags & SO_ACCEPTCON)
593 strcat(ss_flags, "ACC ");
594 if (unix_flags & SO_WAITDATA)
595 strcat(ss_flags, "W ");
596 if (unix_flags & SO_NOSPACE)
597 strcat(ss_flags, "N ");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000598 strcat(ss_flags, "]");
599
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000600 printf("%-5s %-6ld %-11s %-10s %-13s %6lu ",
601 ss_proto, refcnt, ss_flags, ss_type, ss_state, inode
602 );
603
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000604#if ENABLE_FEATURE_NETSTAT_PRG
605 if (option_mask32 & OPT_prg)
606 printf("%-"PROGNAME_WIDTH_STR"s", prg_cache_get(inode));
607#endif
608
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000609 /* TODO: currently we stop at first NUL byte. Is it a problem? */
610 line += path_ofs;
611 *strchrnul(line, '\n') = '\0';
612 while (*line)
613 fputc_printable(*line++, stdout);
614 bb_putchar('\n');
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000615 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000616}
617
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000618static void do_info(const char *file, int FAST_FUNC (*proc)(char *))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000619{
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000620 int lnr;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000621 FILE *procinfo;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000622 char *buffer;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000623
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000624 /* _stdin is just to save "r" param */
625 procinfo = fopen_or_warn_stdin(file);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000626 if (procinfo == NULL) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000627 return;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000628 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000629 lnr = 0;
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000630 /* Why xmalloc_fgets_str? because it doesn't stop on NULs */
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000631 while ((buffer = xmalloc_fgets_str(procinfo, "\n")) != NULL) {
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000632 /* line 0 is skipped */
633 if (lnr && proc(buffer))
634 bb_error_msg("%s: bogus data on line %d", file, lnr + 1);
635 lnr++;
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000636 free(buffer);
637 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000638 fclose(procinfo);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000639}
640
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000641int netstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000642int netstat_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000643{
Denis Vlasenkoa35958d2007-05-16 22:25:35 +0000644 const char *net_conn_line_header = PRINT_NET_CONN_HEADER;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000645 unsigned opt;
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000646
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000647 INIT_G();
648
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000649 /* Option string must match NETSTAT_xxx constants */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000650 opt = getopt32(argv, NETSTAT_OPTS);
Bob Dunlop55a046b2010-10-27 02:12:29 +0200651 if (opt & OPT_sock_listen) { // -l
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000652 flags &= ~NETSTAT_CONNECTED;
653 flags |= NETSTAT_LISTENING;
654 }
Bob Dunlop55a046b2010-10-27 02:12:29 +0200655 if (opt & OPT_sock_all) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a
656 //if (opt & OPT_extended) // -e
657 if (opt & OPT_noresolve) flags |= NETSTAT_NUMERIC; // -n
658 //if (opt & OPT_sock_tcp) // -t: NETSTAT_TCP
659 //if (opt & OPT_sock_udp) // -u: NETSTAT_UDP
660 //if (opt & OPT_sock_raw) // -w: NETSTAT_RAW
661 //if (opt & OPT_sock_unix) // -x: NETSTAT_UNIX
Denis Vlasenko703e2022007-01-22 14:12:08 +0000662#if ENABLE_ROUTE
Bob Dunlop55a046b2010-10-27 02:12:29 +0200663 if (opt & OPT_route) { // -r
Denis Vlasenko703e2022007-01-22 14:12:08 +0000664 bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000665 return 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000666 }
Bob Dunlop55a046b2010-10-27 02:12:29 +0200667#endif
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000668 if (opt & OPT_wide) { // -W
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000669 net_conn_line = PRINT_NET_CONN_WIDE;
Denis Vlasenkoa35958d2007-05-16 22:25:35 +0000670 net_conn_line_header = PRINT_NET_CONN_HEADER_WIDE;
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000671 }
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000672#if ENABLE_FEATURE_NETSTAT_PRG
673 if (opt & OPT_prg) { // -p
674 prg_cache_load();
675 }
676#endif
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000677
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000678 opt &= NETSTAT_ALLPROTO;
679 if (opt) {
680 flags &= ~NETSTAT_ALLPROTO;
681 flags |= opt;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000682 }
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000683 if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000684 printf("Active Internet connections "); /* xxx */
685
Denis Vlasenko703e2022007-01-22 14:12:08 +0000686 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000687 printf("(servers and established)");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000688 else if (flags & NETSTAT_LISTENING)
689 printf("(only servers)");
690 else
691 printf("(w/o servers)");
Denis Vlasenkoa35958d2007-05-16 22:25:35 +0000692 printf(net_conn_line_header, "Local Address", "Foreign Address");
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000693 print_progname_banner();
694 bb_putchar('\n');
Eric Andersen31a0ece2001-10-31 11:00:46 +0000695 }
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000696 if (flags & NETSTAT_TCP) {
697 do_info("/proc/net/tcp", tcp_do_one);
Denis Vlasenko703e2022007-01-22 14:12:08 +0000698#if ENABLE_FEATURE_IPV6
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000699 do_info("/proc/net/tcp6", tcp_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000700#endif
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000701 }
702 if (flags & NETSTAT_UDP) {
703 do_info("/proc/net/udp", udp_do_one);
Denis Vlasenko703e2022007-01-22 14:12:08 +0000704#if ENABLE_FEATURE_IPV6
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000705 do_info("/proc/net/udp6", udp_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000706#endif
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000707 }
708 if (flags & NETSTAT_RAW) {
709 do_info("/proc/net/raw", raw_do_one);
Denis Vlasenko703e2022007-01-22 14:12:08 +0000710#if ENABLE_FEATURE_IPV6
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000711 do_info("/proc/net/raw6", raw_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000712#endif
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000713 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000714 if (flags & NETSTAT_UNIX) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000715 printf("Active UNIX domain sockets ");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000716 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000717 printf("(servers and established)");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000718 else if (flags & NETSTAT_LISTENING)
719 printf("(only servers)");
720 else
721 printf("(w/o servers)");
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000722 printf("\nProto RefCnt Flags Type State I-Node ");
723 print_progname_banner();
724 printf("Path\n");
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000725 do_info("/proc/net/unix", unix_do_one);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000726 }
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000727 prg_cache_clear();
Eric Andersen31a0ece2001-10-31 11:00:46 +0000728 return 0;
729}