blob: 485a7d1919f1dda862d069d7e40079919ff019e0 [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;
398 char local_addr[64], rem_addr[64];
Eric Andersen31a0ece2001-10-31 11:00:46 +0000399
Eric Andersen31a0ece2001-10-31 11:00:46 +0000400 num = sscanf(line,
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000401 "%*d: %64[0-9A-Fa-f]:%X "
402 "%64[0-9A-Fa-f]:%X %X "
403 "%lX:%lX %*X:%*X "
404 "%*X %d %*d %ld ",
405 local_addr, &param->local_port,
406 rem_addr, &param->rem_port, &param->state,
407 &param->txq, &param->rxq,
408 &param->uid, &param->inode);
409 if (num < 9) {
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000410 return 1; /* error */
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000411 }
412
Eric Andersen31a0ece2001-10-31 11:00:46 +0000413 if (strlen(local_addr) > 8) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000414#if ENABLE_FEATURE_IPV6
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100415 build_ipv6_addr(local_addr, &param->localaddr.sin6);
416 build_ipv6_addr(rem_addr, &param->remaddr.sin6);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000417#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000418 } else {
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100419 build_ipv4_addr(local_addr, &param->localaddr.sin);
420 build_ipv4_addr(rem_addr, &param->remaddr.sin);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000421 }
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000422 return 0;
423}
Eric Andersen31a0ece2001-10-31 11:00:46 +0000424
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000425static void print_inet_line(struct inet_params *param,
426 const char *state_str, const char *proto, int is_connected)
427{
428 if ((is_connected && (flags & NETSTAT_CONNECTED))
429 || (!is_connected && (flags & NETSTAT_LISTENING))
Denis Vlasenko703e2022007-01-22 14:12:08 +0000430 ) {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000431 char *l = ip_port_str(
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100432 &param->localaddr.sa, param->local_port,
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000433 proto, flags & NETSTAT_NUMERIC);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000434 char *r = ip_port_str(
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100435 &param->remaddr.sa, param->rem_port,
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000436 proto, flags & NETSTAT_NUMERIC);
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200437 printf(FMT_NET_CONN_DATA,
438 proto, param->rxq, param->txq,
439 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) l,
440 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) r,
441 state_str);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000442#if ENABLE_FEATURE_NETSTAT_PRG
443 if (option_mask32 & OPT_prg)
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000444 printf("%."PROGNAME_WIDTH_STR"s", prg_cache_get(param->inode));
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000445#endif
446 bb_putchar('\n');
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000447 free(l);
448 free(r);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000449 }
450}
451
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000452static int FAST_FUNC tcp_do_one(char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000453{
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000454 struct inet_params param;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000455
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200456 memset(&param, 0, sizeof(param));
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000457 if (scan_inet_proc_line(&param, line))
458 return 1;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000459
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000460 print_inet_line(&param, tcp_state[param.state], "tcp", param.rem_port);
461 return 0;
462}
Eric Andersen31a0ece2001-10-31 11:00:46 +0000463
Denis Vlasenko703e2022007-01-22 14:12:08 +0000464#if ENABLE_FEATURE_IPV6
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100465# define NOT_NULL_ADDR(A) ( \
466 ( (A.sa.sa_family == AF_INET6) \
467 && (A.sin6.sin6_addr.s6_addr32[0] | A.sin6.sin6_addr.s6_addr32[1] | \
468 A.sin6.sin6_addr.s6_addr32[2] | A.sin6.sin6_addr.s6_addr32[3]) \
469 ) || ( \
470 (A.sa.sa_family == AF_INET) \
471 && A.sin.sin_addr.s_addr != 0 \
472 ) \
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000473)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000474#else
Denys Vlasenko45e97922010-02-19 08:29:32 +0100475# define NOT_NULL_ADDR(A) (A.sin.sin_addr.s_addr)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000476#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000477
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000478static int FAST_FUNC udp_do_one(char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000479{
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000480 int have_remaddr;
481 const char *state_str;
482 struct inet_params param;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000483
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200484 memset(&param, 0, sizeof(param)); /* otherwise we display garbage IPv6 scope_ids */
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000485 if (scan_inet_proc_line(&param, line))
486 return 1;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000487
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000488 state_str = "UNKNOWN";
489 switch (param.state) {
490 case TCP_ESTABLISHED:
491 state_str = "ESTABLISHED";
492 break;
493 case TCP_CLOSE:
494 state_str = "";
495 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000496 }
497
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100498 have_remaddr = NOT_NULL_ADDR(param.remaddr);
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000499 print_inet_line(&param, state_str, "udp", have_remaddr);
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000500 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000501}
502
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000503static int FAST_FUNC raw_do_one(char *line)
504{
505 int have_remaddr;
506 struct inet_params param;
507
508 if (scan_inet_proc_line(&param, line))
509 return 1;
510
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100511 have_remaddr = NOT_NULL_ADDR(param.remaddr);
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000512 print_inet_line(&param, itoa(param.state), "raw", have_remaddr);
513 return 0;
514}
515
516static int FAST_FUNC unix_do_one(char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000517{
Eric Andersen31a0ece2001-10-31 11:00:46 +0000518 unsigned long refcnt, proto, unix_flags;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000519 unsigned long inode;
520 int type, state;
521 int num, path_ofs;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000522 const char *ss_proto, *ss_state, *ss_type;
523 char ss_flags[32];
Eric Andersen31a0ece2001-10-31 11:00:46 +0000524
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000525 /* 2.6.15 may report lines like "... @/tmp/fam-user-^@^@^@^@^@^@^@..."
Denis Vlasenko474d1c52008-01-07 19:06:47 +0000526 * Other users report long lines filled by NUL bytes.
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000527 * (those ^@ are NUL bytes too). We see them as empty lines. */
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000528 if (!line[0])
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000529 return 0;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000530
531 path_ofs = 0; /* paranoia */
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000532 num = sscanf(line, "%*p: %lX %lX %lX %X %X %lu %n",
533 &refcnt, &proto, &unix_flags, &type, &state, &inode, &path_ofs);
534 if (num < 6) {
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000535 return 1; /* error */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000536 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000537 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000538 if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000539 if (!(flags & NETSTAT_LISTENING))
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000540 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000541 } else {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000542 if (!(flags & NETSTAT_CONNECTED))
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000543 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000544 }
545 }
546
547 switch (proto) {
548 case 0:
549 ss_proto = "unix";
550 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000551 default:
552 ss_proto = "??";
553 }
554
555 switch (type) {
556 case SOCK_STREAM:
557 ss_type = "STREAM";
558 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000559 case SOCK_DGRAM:
560 ss_type = "DGRAM";
561 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000562 case SOCK_RAW:
563 ss_type = "RAW";
564 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000565 case SOCK_RDM:
566 ss_type = "RDM";
567 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000568 case SOCK_SEQPACKET:
569 ss_type = "SEQPACKET";
570 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000571 default:
572 ss_type = "UNKNOWN";
573 }
574
575 switch (state) {
576 case SS_FREE:
577 ss_state = "FREE";
578 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000579 case SS_UNCONNECTED:
580 /*
581 * Unconnected sockets may be listening
582 * for something.
583 */
584 if (unix_flags & SO_ACCEPTCON) {
585 ss_state = "LISTENING";
586 } else {
587 ss_state = "";
588 }
589 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000590 case SS_CONNECTING:
591 ss_state = "CONNECTING";
592 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000593 case SS_CONNECTED:
594 ss_state = "CONNECTED";
595 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000596 case SS_DISCONNECTING:
597 ss_state = "DISCONNECTING";
598 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000599 default:
600 ss_state = "UNKNOWN";
601 }
602
603 strcpy(ss_flags, "[ ");
604 if (unix_flags & SO_ACCEPTCON)
605 strcat(ss_flags, "ACC ");
606 if (unix_flags & SO_WAITDATA)
607 strcat(ss_flags, "W ");
608 if (unix_flags & SO_NOSPACE)
609 strcat(ss_flags, "N ");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000610 strcat(ss_flags, "]");
611
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000612 printf("%-5s %-6ld %-11s %-10s %-13s %6lu ",
613 ss_proto, refcnt, ss_flags, ss_type, ss_state, inode
614 );
615
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000616#if ENABLE_FEATURE_NETSTAT_PRG
617 if (option_mask32 & OPT_prg)
618 printf("%-"PROGNAME_WIDTH_STR"s", prg_cache_get(inode));
619#endif
620
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000621 /* TODO: currently we stop at first NUL byte. Is it a problem? */
622 line += path_ofs;
623 *strchrnul(line, '\n') = '\0';
624 while (*line)
625 fputc_printable(*line++, stdout);
626 bb_putchar('\n');
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000627 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000628}
629
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000630static void do_info(const char *file, int FAST_FUNC (*proc)(char *))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000631{
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000632 int lnr;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000633 FILE *procinfo;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000634 char *buffer;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000635
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000636 /* _stdin is just to save "r" param */
637 procinfo = fopen_or_warn_stdin(file);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000638 if (procinfo == NULL) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000639 return;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000640 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000641 lnr = 0;
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000642 /* Why xmalloc_fgets_str? because it doesn't stop on NULs */
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000643 while ((buffer = xmalloc_fgets_str(procinfo, "\n")) != NULL) {
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000644 /* line 0 is skipped */
645 if (lnr && proc(buffer))
646 bb_error_msg("%s: bogus data on line %d", file, lnr + 1);
647 lnr++;
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000648 free(buffer);
649 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000650 fclose(procinfo);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000651}
652
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000653int netstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000654int netstat_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000655{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000656 unsigned opt;
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000657
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000658 INIT_G();
659
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000660 /* Option string must match NETSTAT_xxx constants */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000661 opt = getopt32(argv, NETSTAT_OPTS);
Bob Dunlop55a046b2010-10-27 02:12:29 +0200662 if (opt & OPT_sock_listen) { // -l
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000663 flags &= ~NETSTAT_CONNECTED;
664 flags |= NETSTAT_LISTENING;
665 }
Bob Dunlop55a046b2010-10-27 02:12:29 +0200666 if (opt & OPT_sock_all) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a
667 //if (opt & OPT_extended) // -e
668 if (opt & OPT_noresolve) flags |= NETSTAT_NUMERIC; // -n
669 //if (opt & OPT_sock_tcp) // -t: NETSTAT_TCP
670 //if (opt & OPT_sock_udp) // -u: NETSTAT_UDP
671 //if (opt & OPT_sock_raw) // -w: NETSTAT_RAW
672 //if (opt & OPT_sock_unix) // -x: NETSTAT_UNIX
Denis Vlasenko703e2022007-01-22 14:12:08 +0000673#if ENABLE_ROUTE
Bob Dunlop55a046b2010-10-27 02:12:29 +0200674 if (opt & OPT_route) { // -r
Denis Vlasenko703e2022007-01-22 14:12:08 +0000675 bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000676 return 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000677 }
Bob Dunlop55a046b2010-10-27 02:12:29 +0200678#endif
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200679#if ENABLE_FEATURE_NETSTAT_WIDE
680 G.addr_width = ADDR_NORMAL_WIDTH;
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000681 if (opt & OPT_wide) { // -W
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200682 G.addr_width = ADDR_WIDE;
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000683 }
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200684#endif
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000685#if ENABLE_FEATURE_NETSTAT_PRG
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200686 progname_banner = "";
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000687 if (opt & OPT_prg) { // -p
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200688 progname_banner = PROGNAME_BANNER;
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000689 prg_cache_load();
690 }
691#endif
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000692
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000693 opt &= NETSTAT_ALLPROTO;
694 if (opt) {
695 flags &= ~NETSTAT_ALLPROTO;
696 flags |= opt;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000697 }
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000698 if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000699 printf("Active Internet connections "); /* xxx */
700
Denis Vlasenko703e2022007-01-22 14:12:08 +0000701 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000702 printf("(servers and established)");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000703 else if (flags & NETSTAT_LISTENING)
704 printf("(only servers)");
705 else
706 printf("(w/o servers)");
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200707 printf(FMT_NET_CONN_HEADER,
708 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) "Local Address",
709 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) "Foreign Address",
710 progname_banner
711 );
Eric Andersen31a0ece2001-10-31 11:00:46 +0000712 }
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000713 if (flags & NETSTAT_TCP) {
714 do_info("/proc/net/tcp", tcp_do_one);
Denis Vlasenko703e2022007-01-22 14:12:08 +0000715#if ENABLE_FEATURE_IPV6
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000716 do_info("/proc/net/tcp6", tcp_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000717#endif
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000718 }
719 if (flags & NETSTAT_UDP) {
720 do_info("/proc/net/udp", udp_do_one);
Denis Vlasenko703e2022007-01-22 14:12:08 +0000721#if ENABLE_FEATURE_IPV6
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000722 do_info("/proc/net/udp6", udp_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000723#endif
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000724 }
725 if (flags & NETSTAT_RAW) {
726 do_info("/proc/net/raw", raw_do_one);
Denis Vlasenko703e2022007-01-22 14:12:08 +0000727#if ENABLE_FEATURE_IPV6
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000728 do_info("/proc/net/raw6", raw_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000729#endif
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000730 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000731 if (flags & NETSTAT_UNIX) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000732 printf("Active UNIX domain sockets ");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000733 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000734 printf("(servers and established)");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000735 else if (flags & NETSTAT_LISTENING)
736 printf("(only servers)");
737 else
738 printf("(w/o servers)");
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200739 printf("\nProto RefCnt Flags Type State I-Node %sPath\n", progname_banner);
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000740 do_info("/proc/net/unix", unix_do_one);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000741 }
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000742 prg_cache_clear();
Eric Andersen31a0ece2001-10-31 11:00:46 +0000743 return 0;
744}