blob: 356fb53cb24e676c4d8da95610dc06491dea54c8 [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
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200116#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
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200120#define ADDR_NORMAL_WIDTH 23
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000121/* When there are IPv6 connections the IPv6 addresses will be
122 * truncated to none-recognition. The '-W' option makes the
123 * address columns wide enough to accomodate for longest possible
124 * IPv6 addresses, i.e. addresses of the form
125 * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd
126 */
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200127#define ADDR_WIDE 51 /* INET6_ADDRSTRLEN + 5 for the port number */
128#if ENABLE_FEATURE_NETSTAT_WIDE
129# define FMT_NET_CONN_DATA "%s %6ld %6ld %-*s %-*s %-12s"
130# define FMT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-*s %-*s State %s\n"
131#else
132# define FMT_NET_CONN_DATA "%s %6ld %6ld %-23s %-23s %-12s"
133# define FMT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-23s %-23s State %s\n"
134#endif
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000135
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000136#define PROGNAME_WIDTH 20
137#define PROGNAME_WIDTH_STR "20"
138/* PROGNAME_WIDTH chars: 12345678901234567890 */
139#define PROGNAME_BANNER "PID/Program name "
140
141struct prg_node {
142 struct prg_node *next;
143 long inode;
144 char name[PROGNAME_WIDTH];
145};
146
147#define PRG_HASH_SIZE 211
148
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000149struct globals {
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000150 smallint flags;
151#if ENABLE_FEATURE_NETSTAT_PRG
152 smallint prg_cache_loaded;
153 struct prg_node *prg_hash[PRG_HASH_SIZE];
154#endif
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200155#if ENABLE_FEATURE_NETSTAT_PRG
156 const char *progname_banner;
157#endif
158#if ENABLE_FEATURE_NETSTAT_WIDE
159 unsigned addr_width;
160#endif
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000161};
162#define G (*ptr_to_globals)
163#define flags (G.flags )
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000164#define prg_cache_loaded (G.prg_cache_loaded)
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200165#define prg_hash (G.prg_hash )
166#if ENABLE_FEATURE_NETSTAT_PRG
167# define progname_banner (G.progname_banner )
168#else
169# define progname_banner ""
170#endif
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000171#define INIT_G() do { \
172 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
173 flags = NETSTAT_CONNECTED | NETSTAT_ALLPROTO; \
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000174} while (0)
175
176
177#if ENABLE_FEATURE_NETSTAT_PRG
178
179/* Deliberately truncating long to unsigned *int* */
180#define PRG_HASHIT(x) ((unsigned)(x) % PRG_HASH_SIZE)
181
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000182static void prg_cache_add(long inode, char *name)
183{
184 unsigned hi = PRG_HASHIT(inode);
185 struct prg_node **pnp, *pn;
186
187 prg_cache_loaded = 2;
188 for (pnp = prg_hash + hi; (pn = *pnp) != NULL; pnp = &pn->next) {
189 if (pn->inode == inode) {
190 /* Some warning should be appropriate here
191 as we got multiple processes for one i-node */
192 return;
193 }
194 }
195 *pnp = xzalloc(sizeof(struct prg_node));
196 pn = *pnp;
197 pn->inode = inode;
198 safe_strncpy(pn->name, name, PROGNAME_WIDTH);
199}
200
201static const char *prg_cache_get(long inode)
202{
203 unsigned hi = PRG_HASHIT(inode);
204 struct prg_node *pn;
205
206 for (pn = prg_hash[hi]; pn; pn = pn->next)
207 if (pn->inode == inode)
208 return pn->name;
209 return "-";
210}
211
212#if ENABLE_FEATURE_CLEAN_UP
213static void prg_cache_clear(void)
214{
215 struct prg_node **pnp, *pn;
216
217 for (pnp = prg_hash; pnp < prg_hash + PRG_HASH_SIZE; pnp++) {
218 while ((pn = *pnp) != NULL) {
219 *pnp = pn->next;
220 free(pn);
221 }
222 }
223}
224#else
225#define prg_cache_clear() ((void)0)
226#endif
227
Denis Vlasenkocf8b1ef2008-07-27 14:15:42 +0000228static long extract_socket_inode(const char *lname)
229{
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000230 long inode = -1;
231
232 if (strncmp(lname, "socket:[", sizeof("socket:[")-1) == 0) {
233 /* "socket:[12345]", extract the "12345" as inode */
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200234 inode = bb_strtoul(lname + sizeof("socket:[")-1, (char**)&lname, 0);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000235 if (*lname != ']')
236 inode = -1;
237 } else if (strncmp(lname, "[0000]:", sizeof("[0000]:")-1) == 0) {
238 /* "[0000]:12345", extract the "12345" as inode */
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200239 inode = bb_strtoul(lname + sizeof("[0000]:")-1, NULL, 0);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000240 if (errno) /* not NUL terminated? */
241 inode = -1;
242 }
243
244#if 0 /* bb_strtol returns all-ones bit pattern on ERANGE anyway */
245 if (errno == ERANGE)
246 inode = -1;
247#endif
248 return inode;
249}
250
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200251static int FAST_FUNC add_to_prg_cache_if_socket(const char *fileName,
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000252 struct stat *statbuf UNUSED_PARAM,
Bob Dunlop55a046b2010-10-27 02:12:29 +0200253 void *pid_slash_progname,
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000254 int depth UNUSED_PARAM)
255{
256 char *linkname;
257 long inode;
258
259 linkname = xmalloc_readlink(fileName);
260 if (linkname != NULL) {
261 inode = extract_socket_inode(linkname);
262 free(linkname);
263 if (inode >= 0)
Bob Dunlop55a046b2010-10-27 02:12:29 +0200264 prg_cache_add(inode, (char *)pid_slash_progname);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000265 }
266 return TRUE;
267}
268
269static int FAST_FUNC dir_act(const char *fileName,
270 struct stat *statbuf UNUSED_PARAM,
271 void *userData UNUSED_PARAM,
272 int depth)
273{
Bob Dunlop55a046b2010-10-27 02:12:29 +0200274 const char *pid;
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200275 char *pid_slash_progname;
276 char proc_pid_fname[sizeof("/proc/%u/cmdline") + sizeof(long)*3];
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000277 char cmdline_buf[512];
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200278 int n, len;
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000279
280 if (depth == 0) /* "/proc" itself */
281 return TRUE; /* continue looking one level below /proc */
282
Bob Dunlop55a046b2010-10-27 02:12:29 +0200283 pid = fileName + sizeof("/proc/")-1; /* point after "/proc/" */
284 if (!isdigit(pid[0])) /* skip /proc entries which aren't processes */
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000285 return SKIP;
286
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200287 len = snprintf(proc_pid_fname, sizeof(proc_pid_fname), "%s/cmdline", fileName);
288 n = open_read_close(proc_pid_fname, cmdline_buf, sizeof(cmdline_buf) - 1);
289 if (n < 0)
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000290 return FALSE;
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200291 cmdline_buf[n] = '\0';
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000292
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200293 /* go through all files in /proc/PID/fd and check whether they are sockets */
294 strcpy(proc_pid_fname + len - (sizeof("cmdline")-1), "fd");
Bob Dunlop55a046b2010-10-27 02:12:29 +0200295 pid_slash_progname = concat_path_file(pid, bb_basename(cmdline_buf)); /* "PID/argv0" */
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200296 n = recursive_action(proc_pid_fname,
297 ACTION_RECURSE | ACTION_QUIET,
298 add_to_prg_cache_if_socket,
299 NULL,
300 (void *)pid_slash_progname,
301 0);
Bob Dunlop55a046b2010-10-27 02:12:29 +0200302 free(pid_slash_progname);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000303
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200304 if (!n)
Bob Dunlop55a046b2010-10-27 02:12:29 +0200305 return FALSE; /* signal permissions error to caller */
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000306
Bob Dunlop55a046b2010-10-27 02:12:29 +0200307 return SKIP; /* caller should not recurse further into this dir */
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000308}
309
310static void prg_cache_load(void)
311{
312 int load_ok;
313
314 prg_cache_loaded = 1;
315 load_ok = recursive_action("/proc", ACTION_RECURSE | ACTION_QUIET,
316 NULL, dir_act, NULL, 0);
317 if (load_ok)
318 return;
319
320 if (prg_cache_loaded == 1)
321 bb_error_msg("can't scan /proc - are you root?");
322 else
323 bb_error_msg("showing only processes with your user ID");
324}
325
326#else
327
328#define prg_cache_clear() ((void)0)
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000329
330#endif //ENABLE_FEATURE_NETSTAT_PRG
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000331
332
333#if ENABLE_FEATURE_IPV6
334static void build_ipv6_addr(char* local_addr, struct sockaddr_in6* localaddr)
335{
336 char addr6[INET6_ADDRSTRLEN];
337 struct in6_addr in6;
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000338
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000339 sscanf(local_addr, "%08X%08X%08X%08X",
340 &in6.s6_addr32[0], &in6.s6_addr32[1],
341 &in6.s6_addr32[2], &in6.s6_addr32[3]);
342 inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6));
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100343 inet_pton(AF_INET6, addr6, &localaddr->sin6_addr);
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000344
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000345 localaddr->sin6_family = AF_INET6;
346}
347#endif
348
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000349static void build_ipv4_addr(char* local_addr, struct sockaddr_in* localaddr)
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000350{
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100351 sscanf(local_addr, "%X", &localaddr->sin_addr.s_addr);
352 localaddr->sin_family = AF_INET;
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000353}
354
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000355static const char *get_sname(int port, const char *proto, int numeric)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000356{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000357 if (!port)
358 return "*";
359 if (!numeric) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000360 struct servent *se = getservbyport(port, proto);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000361 if (se)
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000362 return se->s_name;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000363 }
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000364 /* hummm, we may return static buffer here!! */
365 return itoa(ntohs(port));
Eric Andersen31a0ece2001-10-31 11:00:46 +0000366}
367
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000368static char *ip_port_str(struct sockaddr *addr, int port, const char *proto, int numeric)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000369{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000370 char *host, *host_port;
Eric Andersencd8c4362001-11-10 11:22:46 +0000371
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000372 /* Code which used "*" for INADDR_ANY is removed: it's ambiguous
373 * in IPv6, while "0.0.0.0" is not. */
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000374
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000375 host = numeric ? xmalloc_sockaddr2dotted_noport(addr)
376 : xmalloc_sockaddr2host_noport(addr);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000377
378 host_port = xasprintf("%s:%s", host, get_sname(htons(port), proto, numeric));
379 free(host);
380 return host_port;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000381}
382
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000383struct inet_params {
384 int local_port, rem_port, state, uid;
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100385 union {
386 struct sockaddr sa;
387 struct sockaddr_in sin;
Denis Vlasenko703e2022007-01-22 14:12:08 +0000388#if ENABLE_FEATURE_IPV6
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100389 struct sockaddr_in6 sin6;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000390#endif
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100391 } localaddr, remaddr;
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000392 unsigned long rxq, txq, inode;
393};
Eric Andersen31a0ece2001-10-31 11:00:46 +0000394
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000395static int scan_inet_proc_line(struct inet_params *param, char *line)
396{
397 int num;
Denys Vlasenkodb9ccc52010-10-28 02:36:17 +0200398 /* IPv6 /proc files use 32-char hex representation
399 * of IPv6 address, followed by :PORT_IN_HEX
400 */
401 char local_addr[33], rem_addr[33]; /* 32 + 1 for NUL */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000402
Eric Andersen31a0ece2001-10-31 11:00:46 +0000403 num = sscanf(line,
Denys Vlasenkodb9ccc52010-10-28 02:36:17 +0200404 "%*d: %32[0-9A-Fa-f]:%X "
405 "%32[0-9A-Fa-f]:%X %X "
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000406 "%lX:%lX %*X:%*X "
407 "%*X %d %*d %ld ",
408 local_addr, &param->local_port,
409 rem_addr, &param->rem_port, &param->state,
410 &param->txq, &param->rxq,
411 &param->uid, &param->inode);
412 if (num < 9) {
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000413 return 1; /* error */
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000414 }
415
Eric Andersen31a0ece2001-10-31 11:00:46 +0000416 if (strlen(local_addr) > 8) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000417#if ENABLE_FEATURE_IPV6
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100418 build_ipv6_addr(local_addr, &param->localaddr.sin6);
419 build_ipv6_addr(rem_addr, &param->remaddr.sin6);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000420#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000421 } else {
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100422 build_ipv4_addr(local_addr, &param->localaddr.sin);
423 build_ipv4_addr(rem_addr, &param->remaddr.sin);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000424 }
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000425 return 0;
426}
Eric Andersen31a0ece2001-10-31 11:00:46 +0000427
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000428static void print_inet_line(struct inet_params *param,
429 const char *state_str, const char *proto, int is_connected)
430{
431 if ((is_connected && (flags & NETSTAT_CONNECTED))
432 || (!is_connected && (flags & NETSTAT_LISTENING))
Denis Vlasenko703e2022007-01-22 14:12:08 +0000433 ) {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000434 char *l = ip_port_str(
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100435 &param->localaddr.sa, param->local_port,
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000436 proto, flags & NETSTAT_NUMERIC);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000437 char *r = ip_port_str(
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100438 &param->remaddr.sa, param->rem_port,
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000439 proto, flags & NETSTAT_NUMERIC);
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200440 printf(FMT_NET_CONN_DATA,
441 proto, param->rxq, param->txq,
442 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) l,
443 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) r,
444 state_str);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000445#if ENABLE_FEATURE_NETSTAT_PRG
446 if (option_mask32 & OPT_prg)
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000447 printf("%."PROGNAME_WIDTH_STR"s", prg_cache_get(param->inode));
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000448#endif
449 bb_putchar('\n');
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000450 free(l);
451 free(r);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000452 }
453}
454
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000455static int FAST_FUNC tcp_do_one(char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000456{
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000457 struct inet_params param;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000458
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200459 memset(&param, 0, sizeof(param));
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000460 if (scan_inet_proc_line(&param, line))
461 return 1;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000462
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000463 print_inet_line(&param, tcp_state[param.state], "tcp", param.rem_port);
464 return 0;
465}
Eric Andersen31a0ece2001-10-31 11:00:46 +0000466
Denis Vlasenko703e2022007-01-22 14:12:08 +0000467#if ENABLE_FEATURE_IPV6
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100468# define NOT_NULL_ADDR(A) ( \
469 ( (A.sa.sa_family == AF_INET6) \
470 && (A.sin6.sin6_addr.s6_addr32[0] | A.sin6.sin6_addr.s6_addr32[1] | \
471 A.sin6.sin6_addr.s6_addr32[2] | A.sin6.sin6_addr.s6_addr32[3]) \
472 ) || ( \
473 (A.sa.sa_family == AF_INET) \
474 && A.sin.sin_addr.s_addr != 0 \
475 ) \
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000476)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000477#else
Denys Vlasenko45e97922010-02-19 08:29:32 +0100478# define NOT_NULL_ADDR(A) (A.sin.sin_addr.s_addr)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000479#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000480
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000481static int FAST_FUNC udp_do_one(char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000482{
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000483 int have_remaddr;
484 const char *state_str;
485 struct inet_params param;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000486
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200487 memset(&param, 0, sizeof(param)); /* otherwise we display garbage IPv6 scope_ids */
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000488 if (scan_inet_proc_line(&param, line))
489 return 1;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000490
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000491 state_str = "UNKNOWN";
492 switch (param.state) {
493 case TCP_ESTABLISHED:
494 state_str = "ESTABLISHED";
495 break;
496 case TCP_CLOSE:
497 state_str = "";
498 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000499 }
500
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100501 have_remaddr = NOT_NULL_ADDR(param.remaddr);
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000502 print_inet_line(&param, state_str, "udp", have_remaddr);
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000503 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000504}
505
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000506static int FAST_FUNC raw_do_one(char *line)
507{
508 int have_remaddr;
509 struct inet_params param;
510
511 if (scan_inet_proc_line(&param, line))
512 return 1;
513
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100514 have_remaddr = NOT_NULL_ADDR(param.remaddr);
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000515 print_inet_line(&param, itoa(param.state), "raw", have_remaddr);
516 return 0;
517}
518
519static int FAST_FUNC unix_do_one(char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000520{
Eric Andersen31a0ece2001-10-31 11:00:46 +0000521 unsigned long refcnt, proto, unix_flags;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000522 unsigned long inode;
523 int type, state;
524 int num, path_ofs;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000525 const char *ss_proto, *ss_state, *ss_type;
526 char ss_flags[32];
Eric Andersen31a0ece2001-10-31 11:00:46 +0000527
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000528 /* 2.6.15 may report lines like "... @/tmp/fam-user-^@^@^@^@^@^@^@..."
Denis Vlasenko474d1c52008-01-07 19:06:47 +0000529 * Other users report long lines filled by NUL bytes.
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000530 * (those ^@ are NUL bytes too). We see them as empty lines. */
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000531 if (!line[0])
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000532 return 0;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000533
534 path_ofs = 0; /* paranoia */
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000535 num = sscanf(line, "%*p: %lX %lX %lX %X %X %lu %n",
536 &refcnt, &proto, &unix_flags, &type, &state, &inode, &path_ofs);
537 if (num < 6) {
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000538 return 1; /* error */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000539 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000540 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000541 if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000542 if (!(flags & NETSTAT_LISTENING))
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000543 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000544 } else {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000545 if (!(flags & NETSTAT_CONNECTED))
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000546 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000547 }
548 }
549
550 switch (proto) {
551 case 0:
552 ss_proto = "unix";
553 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000554 default:
555 ss_proto = "??";
556 }
557
558 switch (type) {
559 case SOCK_STREAM:
560 ss_type = "STREAM";
561 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000562 case SOCK_DGRAM:
563 ss_type = "DGRAM";
564 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000565 case SOCK_RAW:
566 ss_type = "RAW";
567 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000568 case SOCK_RDM:
569 ss_type = "RDM";
570 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000571 case SOCK_SEQPACKET:
572 ss_type = "SEQPACKET";
573 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000574 default:
575 ss_type = "UNKNOWN";
576 }
577
578 switch (state) {
579 case SS_FREE:
580 ss_state = "FREE";
581 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000582 case SS_UNCONNECTED:
583 /*
584 * Unconnected sockets may be listening
585 * for something.
586 */
587 if (unix_flags & SO_ACCEPTCON) {
588 ss_state = "LISTENING";
589 } else {
590 ss_state = "";
591 }
592 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000593 case SS_CONNECTING:
594 ss_state = "CONNECTING";
595 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000596 case SS_CONNECTED:
597 ss_state = "CONNECTED";
598 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000599 case SS_DISCONNECTING:
600 ss_state = "DISCONNECTING";
601 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000602 default:
603 ss_state = "UNKNOWN";
604 }
605
606 strcpy(ss_flags, "[ ");
607 if (unix_flags & SO_ACCEPTCON)
608 strcat(ss_flags, "ACC ");
609 if (unix_flags & SO_WAITDATA)
610 strcat(ss_flags, "W ");
611 if (unix_flags & SO_NOSPACE)
612 strcat(ss_flags, "N ");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000613 strcat(ss_flags, "]");
614
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000615 printf("%-5s %-6ld %-11s %-10s %-13s %6lu ",
616 ss_proto, refcnt, ss_flags, ss_type, ss_state, inode
617 );
618
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000619#if ENABLE_FEATURE_NETSTAT_PRG
620 if (option_mask32 & OPT_prg)
621 printf("%-"PROGNAME_WIDTH_STR"s", prg_cache_get(inode));
622#endif
623
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000624 /* TODO: currently we stop at first NUL byte. Is it a problem? */
625 line += path_ofs;
626 *strchrnul(line, '\n') = '\0';
627 while (*line)
628 fputc_printable(*line++, stdout);
629 bb_putchar('\n');
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000630 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000631}
632
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000633static void do_info(const char *file, int FAST_FUNC (*proc)(char *))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000634{
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000635 int lnr;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000636 FILE *procinfo;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000637 char *buffer;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000638
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000639 /* _stdin is just to save "r" param */
640 procinfo = fopen_or_warn_stdin(file);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000641 if (procinfo == NULL) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000642 return;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000643 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000644 lnr = 0;
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000645 /* Why xmalloc_fgets_str? because it doesn't stop on NULs */
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000646 while ((buffer = xmalloc_fgets_str(procinfo, "\n")) != NULL) {
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000647 /* line 0 is skipped */
648 if (lnr && proc(buffer))
649 bb_error_msg("%s: bogus data on line %d", file, lnr + 1);
650 lnr++;
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000651 free(buffer);
652 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000653 fclose(procinfo);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000654}
655
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000656int netstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000657int netstat_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000658{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000659 unsigned opt;
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000660
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000661 INIT_G();
662
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000663 /* Option string must match NETSTAT_xxx constants */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000664 opt = getopt32(argv, NETSTAT_OPTS);
Bob Dunlop55a046b2010-10-27 02:12:29 +0200665 if (opt & OPT_sock_listen) { // -l
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000666 flags &= ~NETSTAT_CONNECTED;
667 flags |= NETSTAT_LISTENING;
668 }
Bob Dunlop55a046b2010-10-27 02:12:29 +0200669 if (opt & OPT_sock_all) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a
670 //if (opt & OPT_extended) // -e
671 if (opt & OPT_noresolve) flags |= NETSTAT_NUMERIC; // -n
672 //if (opt & OPT_sock_tcp) // -t: NETSTAT_TCP
673 //if (opt & OPT_sock_udp) // -u: NETSTAT_UDP
674 //if (opt & OPT_sock_raw) // -w: NETSTAT_RAW
675 //if (opt & OPT_sock_unix) // -x: NETSTAT_UNIX
Denis Vlasenko703e2022007-01-22 14:12:08 +0000676#if ENABLE_ROUTE
Bob Dunlop55a046b2010-10-27 02:12:29 +0200677 if (opt & OPT_route) { // -r
Denis Vlasenko703e2022007-01-22 14:12:08 +0000678 bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000679 return 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000680 }
Bob Dunlop55a046b2010-10-27 02:12:29 +0200681#endif
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200682#if ENABLE_FEATURE_NETSTAT_WIDE
683 G.addr_width = ADDR_NORMAL_WIDTH;
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000684 if (opt & OPT_wide) { // -W
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200685 G.addr_width = ADDR_WIDE;
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000686 }
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200687#endif
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000688#if ENABLE_FEATURE_NETSTAT_PRG
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200689 progname_banner = "";
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000690 if (opt & OPT_prg) { // -p
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200691 progname_banner = PROGNAME_BANNER;
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000692 prg_cache_load();
693 }
694#endif
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000695
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000696 opt &= NETSTAT_ALLPROTO;
697 if (opt) {
698 flags &= ~NETSTAT_ALLPROTO;
699 flags |= opt;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000700 }
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000701 if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) {
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200702 printf("Active Internet connections "); /* xxx */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000703
Denis Vlasenko703e2022007-01-22 14:12:08 +0000704 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000705 printf("(servers and established)");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000706 else if (flags & NETSTAT_LISTENING)
707 printf("(only servers)");
708 else
709 printf("(w/o servers)");
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200710 printf(FMT_NET_CONN_HEADER,
711 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) "Local Address",
712 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) "Foreign Address",
713 progname_banner
714 );
Eric Andersen31a0ece2001-10-31 11:00:46 +0000715 }
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000716 if (flags & NETSTAT_TCP) {
717 do_info("/proc/net/tcp", tcp_do_one);
Denis Vlasenko703e2022007-01-22 14:12:08 +0000718#if ENABLE_FEATURE_IPV6
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000719 do_info("/proc/net/tcp6", tcp_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000720#endif
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000721 }
722 if (flags & NETSTAT_UDP) {
723 do_info("/proc/net/udp", udp_do_one);
Denis Vlasenko703e2022007-01-22 14:12:08 +0000724#if ENABLE_FEATURE_IPV6
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000725 do_info("/proc/net/udp6", udp_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000726#endif
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000727 }
728 if (flags & NETSTAT_RAW) {
729 do_info("/proc/net/raw", raw_do_one);
Denis Vlasenko703e2022007-01-22 14:12:08 +0000730#if ENABLE_FEATURE_IPV6
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000731 do_info("/proc/net/raw6", raw_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000732#endif
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000733 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000734 if (flags & NETSTAT_UNIX) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000735 printf("Active UNIX domain sockets ");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000736 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000737 printf("(servers and established)");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000738 else if (flags & NETSTAT_LISTENING)
739 printf("(only servers)");
740 else
741 printf("(w/o servers)");
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200742 printf("\nProto RefCnt Flags Type State I-Node %sPath\n", progname_banner);
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000743 do_info("/proc/net/unix", unix_do_one);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000744 }
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000745 prg_cache_clear();
Eric Andersen31a0ece2001-10-31 11:00:46 +0000746 return 0;
747}