blob: 9c239579fe8f0bdfad0187983cdcd5872a425dc6 [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"
Bob Dunlop55a046b2010-10-27 02:12:29 +020024//usage: IF_ROUTE(
25//usage: "\n -r Routing table"
26//usage: )
27//usage: "\n -a All sockets"
28//usage: "\n -l Listening sockets"
29//usage: "\n Else: connected sockets"
30//usage: "\n -t TCP sockets"
31//usage: "\n -u UDP sockets"
32//usage: "\n -w Raw sockets"
33//usage: "\n -x Unix sockets"
34//usage: "\n Else: all socket types"
35//usage: "\n -e Other/more information"
36//usage: "\n -n Don't resolve names"
37//usage: IF_FEATURE_NETSTAT_WIDE(
38//usage: "\n -W Wide display"
39//usage: )
40//usage: IF_FEATURE_NETSTAT_PRG(
41//usage: "\n -p Show PID/program name for sockets"
42//usage: )
43
Denis Vlasenko6e69e422008-07-27 12:10:07 +000044#define NETSTAT_OPTS "laentuwx" \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000045 IF_ROUTE( "r") \
46 IF_FEATURE_NETSTAT_WIDE("W") \
47 IF_FEATURE_NETSTAT_PRG( "p")
Denis Vlasenko6e69e422008-07-27 12:10:07 +000048
Denis Vlasenko418a7fb2007-05-15 23:57:46 +000049enum {
Denis Vlasenko6e69e422008-07-27 12:10:07 +000050 OPT_sock_listen = 1 << 0, // l
51 OPT_sock_all = 1 << 1, // a
52 OPT_extended = 1 << 2, // e
53 OPT_noresolve = 1 << 3, // n
54 OPT_sock_tcp = 1 << 4, // t
55 OPT_sock_udp = 1 << 5, // u
56 OPT_sock_raw = 1 << 6, // w
57 OPT_sock_unix = 1 << 7, // x
Bob Dunlop55a046b2010-10-27 02:12:29 +020058 OPTBIT_x = 7,
59 IF_ROUTE( OPTBIT_ROUTE,)
60 IF_FEATURE_NETSTAT_WIDE(OPTBIT_WIDE ,)
61 IF_FEATURE_NETSTAT_PRG( OPTBIT_PRG ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000062 OPT_route = IF_ROUTE( (1 << OPTBIT_ROUTE)) + 0, // r
63 OPT_wide = IF_FEATURE_NETSTAT_WIDE((1 << OPTBIT_WIDE )) + 0, // W
64 OPT_prg = IF_FEATURE_NETSTAT_PRG( (1 << OPTBIT_PRG )) + 0, // p
Denis Vlasenko418a7fb2007-05-15 23:57:46 +000065};
Denis Vlasenko418a7fb2007-05-15 23:57:46 +000066
Denis Vlasenko703e2022007-01-22 14:12:08 +000067#define NETSTAT_CONNECTED 0x01
68#define NETSTAT_LISTENING 0x02
69#define NETSTAT_NUMERIC 0x04
Denis Vlasenko67b23e62006-10-03 21:00:06 +000070/* Must match getopt32 option string */
Denis Vlasenko703e2022007-01-22 14:12:08 +000071#define NETSTAT_TCP 0x10
72#define NETSTAT_UDP 0x20
73#define NETSTAT_RAW 0x40
74#define NETSTAT_UNIX 0x80
75#define NETSTAT_ALLPROTO (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW|NETSTAT_UNIX)
Eric Andersen31a0ece2001-10-31 11:00:46 +000076
Eric Andersen31a0ece2001-10-31 11:00:46 +000077
Eric Andersen31a0ece2001-10-31 11:00:46 +000078enum {
Denis Vlasenko703e2022007-01-22 14:12:08 +000079 TCP_ESTABLISHED = 1,
80 TCP_SYN_SENT,
81 TCP_SYN_RECV,
82 TCP_FIN_WAIT1,
83 TCP_FIN_WAIT2,
84 TCP_TIME_WAIT,
85 TCP_CLOSE,
86 TCP_CLOSE_WAIT,
87 TCP_LAST_ACK,
88 TCP_LISTEN,
Denis Vlasenko6e69e422008-07-27 12:10:07 +000089 TCP_CLOSING, /* now a valid state */
Eric Andersen31a0ece2001-10-31 11:00:46 +000090};
91
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000092static const char *const tcp_state[] = {
Denis Vlasenko703e2022007-01-22 14:12:08 +000093 "",
94 "ESTABLISHED",
95 "SYN_SENT",
96 "SYN_RECV",
97 "FIN_WAIT1",
98 "FIN_WAIT2",
99 "TIME_WAIT",
100 "CLOSE",
101 "CLOSE_WAIT",
102 "LAST_ACK",
103 "LISTEN",
104 "CLOSING"
Eric Andersen31a0ece2001-10-31 11:00:46 +0000105};
106
107typedef enum {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000108 SS_FREE = 0, /* not allocated */
109 SS_UNCONNECTED, /* unconnected to any socket */
110 SS_CONNECTING, /* in process of connecting */
111 SS_CONNECTED, /* connected to socket */
112 SS_DISCONNECTING /* in process of disconnecting */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000113} socket_state;
114
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200115#define SO_ACCEPTCON (1<<16) /* performed a listen */
116#define SO_WAITDATA (1<<17) /* wait data to read */
117#define SO_NOSPACE (1<<18) /* no space to write */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000118
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200119#define ADDR_NORMAL_WIDTH 23
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000120/* When there are IPv6 connections the IPv6 addresses will be
121 * truncated to none-recognition. The '-W' option makes the
122 * address columns wide enough to accomodate for longest possible
123 * IPv6 addresses, i.e. addresses of the form
124 * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd
125 */
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200126#define ADDR_WIDE 51 /* INET6_ADDRSTRLEN + 5 for the port number */
127#if ENABLE_FEATURE_NETSTAT_WIDE
128# define FMT_NET_CONN_DATA "%s %6ld %6ld %-*s %-*s %-12s"
129# define FMT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-*s %-*s State %s\n"
130#else
131# define FMT_NET_CONN_DATA "%s %6ld %6ld %-23s %-23s %-12s"
132# define FMT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-23s %-23s State %s\n"
133#endif
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 {
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000149 smallint flags;
150#if ENABLE_FEATURE_NETSTAT_PRG
151 smallint prg_cache_loaded;
152 struct prg_node *prg_hash[PRG_HASH_SIZE];
153#endif
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200154#if ENABLE_FEATURE_NETSTAT_PRG
155 const char *progname_banner;
156#endif
157#if ENABLE_FEATURE_NETSTAT_WIDE
158 unsigned addr_width;
159#endif
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000160};
161#define G (*ptr_to_globals)
162#define flags (G.flags )
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000163#define prg_cache_loaded (G.prg_cache_loaded)
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200164#define prg_hash (G.prg_hash )
165#if ENABLE_FEATURE_NETSTAT_PRG
166# define progname_banner (G.progname_banner )
167#else
168# define progname_banner ""
169#endif
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000170#define INIT_G() do { \
171 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
172 flags = NETSTAT_CONNECTED | NETSTAT_ALLPROTO; \
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000173} while (0)
174
175
176#if ENABLE_FEATURE_NETSTAT_PRG
177
178/* Deliberately truncating long to unsigned *int* */
179#define PRG_HASHIT(x) ((unsigned)(x) % PRG_HASH_SIZE)
180
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000181static void prg_cache_add(long inode, char *name)
182{
183 unsigned hi = PRG_HASHIT(inode);
184 struct prg_node **pnp, *pn;
185
186 prg_cache_loaded = 2;
187 for (pnp = prg_hash + hi; (pn = *pnp) != NULL; pnp = &pn->next) {
188 if (pn->inode == inode) {
189 /* Some warning should be appropriate here
190 as we got multiple processes for one i-node */
191 return;
192 }
193 }
194 *pnp = xzalloc(sizeof(struct prg_node));
195 pn = *pnp;
196 pn->inode = inode;
197 safe_strncpy(pn->name, name, PROGNAME_WIDTH);
198}
199
200static const char *prg_cache_get(long inode)
201{
202 unsigned hi = PRG_HASHIT(inode);
203 struct prg_node *pn;
204
205 for (pn = prg_hash[hi]; pn; pn = pn->next)
206 if (pn->inode == inode)
207 return pn->name;
208 return "-";
209}
210
211#if ENABLE_FEATURE_CLEAN_UP
212static void prg_cache_clear(void)
213{
214 struct prg_node **pnp, *pn;
215
216 for (pnp = prg_hash; pnp < prg_hash + PRG_HASH_SIZE; pnp++) {
217 while ((pn = *pnp) != NULL) {
218 *pnp = pn->next;
219 free(pn);
220 }
221 }
222}
223#else
224#define prg_cache_clear() ((void)0)
225#endif
226
Denis Vlasenkocf8b1ef2008-07-27 14:15:42 +0000227static long extract_socket_inode(const char *lname)
228{
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000229 long inode = -1;
230
231 if (strncmp(lname, "socket:[", sizeof("socket:[")-1) == 0) {
232 /* "socket:[12345]", extract the "12345" as inode */
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200233 inode = bb_strtoul(lname + sizeof("socket:[")-1, (char**)&lname, 0);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000234 if (*lname != ']')
235 inode = -1;
236 } else if (strncmp(lname, "[0000]:", sizeof("[0000]:")-1) == 0) {
237 /* "[0000]:12345", extract the "12345" as inode */
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200238 inode = bb_strtoul(lname + sizeof("[0000]:")-1, NULL, 0);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000239 if (errno) /* not NUL terminated? */
240 inode = -1;
241 }
242
243#if 0 /* bb_strtol returns all-ones bit pattern on ERANGE anyway */
244 if (errno == ERANGE)
245 inode = -1;
246#endif
247 return inode;
248}
249
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200250static int FAST_FUNC add_to_prg_cache_if_socket(const char *fileName,
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000251 struct stat *statbuf UNUSED_PARAM,
Bob Dunlop55a046b2010-10-27 02:12:29 +0200252 void *pid_slash_progname,
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000253 int depth UNUSED_PARAM)
254{
255 char *linkname;
256 long inode;
257
258 linkname = xmalloc_readlink(fileName);
259 if (linkname != NULL) {
260 inode = extract_socket_inode(linkname);
261 free(linkname);
262 if (inode >= 0)
Bob Dunlop55a046b2010-10-27 02:12:29 +0200263 prg_cache_add(inode, (char *)pid_slash_progname);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000264 }
265 return TRUE;
266}
267
268static int FAST_FUNC dir_act(const char *fileName,
269 struct stat *statbuf UNUSED_PARAM,
270 void *userData UNUSED_PARAM,
271 int depth)
272{
Bob Dunlop55a046b2010-10-27 02:12:29 +0200273 const char *pid;
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200274 char *pid_slash_progname;
275 char proc_pid_fname[sizeof("/proc/%u/cmdline") + sizeof(long)*3];
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000276 char cmdline_buf[512];
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200277 int n, len;
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000278
279 if (depth == 0) /* "/proc" itself */
280 return TRUE; /* continue looking one level below /proc */
281
Bob Dunlop55a046b2010-10-27 02:12:29 +0200282 pid = fileName + sizeof("/proc/")-1; /* point after "/proc/" */
283 if (!isdigit(pid[0])) /* skip /proc entries which aren't processes */
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000284 return SKIP;
285
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200286 len = snprintf(proc_pid_fname, sizeof(proc_pid_fname), "%s/cmdline", fileName);
287 n = open_read_close(proc_pid_fname, cmdline_buf, sizeof(cmdline_buf) - 1);
288 if (n < 0)
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000289 return FALSE;
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200290 cmdline_buf[n] = '\0';
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000291
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200292 /* go through all files in /proc/PID/fd and check whether they are sockets */
293 strcpy(proc_pid_fname + len - (sizeof("cmdline")-1), "fd");
Bob Dunlop55a046b2010-10-27 02:12:29 +0200294 pid_slash_progname = concat_path_file(pid, bb_basename(cmdline_buf)); /* "PID/argv0" */
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200295 n = recursive_action(proc_pid_fname,
296 ACTION_RECURSE | ACTION_QUIET,
297 add_to_prg_cache_if_socket,
298 NULL,
299 (void *)pid_slash_progname,
300 0);
Bob Dunlop55a046b2010-10-27 02:12:29 +0200301 free(pid_slash_progname);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000302
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200303 if (!n)
Bob Dunlop55a046b2010-10-27 02:12:29 +0200304 return FALSE; /* signal permissions error to caller */
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000305
Bob Dunlop55a046b2010-10-27 02:12:29 +0200306 return SKIP; /* caller should not recurse further into this dir */
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000307}
308
309static void prg_cache_load(void)
310{
311 int load_ok;
312
313 prg_cache_loaded = 1;
314 load_ok = recursive_action("/proc", ACTION_RECURSE | ACTION_QUIET,
315 NULL, dir_act, NULL, 0);
316 if (load_ok)
317 return;
318
319 if (prg_cache_loaded == 1)
320 bb_error_msg("can't scan /proc - are you root?");
321 else
322 bb_error_msg("showing only processes with your user ID");
323}
324
325#else
326
327#define prg_cache_clear() ((void)0)
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000328
329#endif //ENABLE_FEATURE_NETSTAT_PRG
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000330
331
332#if ENABLE_FEATURE_IPV6
333static void build_ipv6_addr(char* local_addr, struct sockaddr_in6* localaddr)
334{
335 char addr6[INET6_ADDRSTRLEN];
336 struct in6_addr in6;
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000337
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000338 sscanf(local_addr, "%08X%08X%08X%08X",
339 &in6.s6_addr32[0], &in6.s6_addr32[1],
340 &in6.s6_addr32[2], &in6.s6_addr32[3]);
341 inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6));
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100342 inet_pton(AF_INET6, addr6, &localaddr->sin6_addr);
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000343
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000344 localaddr->sin6_family = AF_INET6;
345}
346#endif
347
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000348static void build_ipv4_addr(char* local_addr, struct sockaddr_in* localaddr)
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000349{
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100350 sscanf(local_addr, "%X", &localaddr->sin_addr.s_addr);
351 localaddr->sin_family = AF_INET;
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000352}
353
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000354static const char *get_sname(int port, const char *proto, int numeric)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000355{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000356 if (!port)
357 return "*";
358 if (!numeric) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000359 struct servent *se = getservbyport(port, proto);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000360 if (se)
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000361 return se->s_name;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000362 }
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000363 /* hummm, we may return static buffer here!! */
364 return itoa(ntohs(port));
Eric Andersen31a0ece2001-10-31 11:00:46 +0000365}
366
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000367static char *ip_port_str(struct sockaddr *addr, int port, const char *proto, int numeric)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000368{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000369 char *host, *host_port;
Eric Andersencd8c4362001-11-10 11:22:46 +0000370
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000371 /* Code which used "*" for INADDR_ANY is removed: it's ambiguous
372 * in IPv6, while "0.0.0.0" is not. */
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000373
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000374 host = numeric ? xmalloc_sockaddr2dotted_noport(addr)
375 : xmalloc_sockaddr2host_noport(addr);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000376
377 host_port = xasprintf("%s:%s", host, get_sname(htons(port), proto, numeric));
378 free(host);
379 return host_port;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000380}
381
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000382struct inet_params {
383 int local_port, rem_port, state, uid;
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100384 union {
385 struct sockaddr sa;
386 struct sockaddr_in sin;
Denis Vlasenko703e2022007-01-22 14:12:08 +0000387#if ENABLE_FEATURE_IPV6
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100388 struct sockaddr_in6 sin6;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000389#endif
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100390 } localaddr, remaddr;
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000391 unsigned long rxq, txq, inode;
392};
Eric Andersen31a0ece2001-10-31 11:00:46 +0000393
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000394static int scan_inet_proc_line(struct inet_params *param, char *line)
395{
396 int num;
Denys Vlasenkodb9ccc52010-10-28 02:36:17 +0200397 /* IPv6 /proc files use 32-char hex representation
398 * of IPv6 address, followed by :PORT_IN_HEX
399 */
400 char local_addr[33], rem_addr[33]; /* 32 + 1 for NUL */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000401
Eric Andersen31a0ece2001-10-31 11:00:46 +0000402 num = sscanf(line,
Denys Vlasenkodb9ccc52010-10-28 02:36:17 +0200403 "%*d: %32[0-9A-Fa-f]:%X "
404 "%32[0-9A-Fa-f]:%X %X "
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000405 "%lX:%lX %*X:%*X "
406 "%*X %d %*d %ld ",
407 local_addr, &param->local_port,
408 rem_addr, &param->rem_port, &param->state,
409 &param->txq, &param->rxq,
410 &param->uid, &param->inode);
411 if (num < 9) {
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000412 return 1; /* error */
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000413 }
414
Eric Andersen31a0ece2001-10-31 11:00:46 +0000415 if (strlen(local_addr) > 8) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000416#if ENABLE_FEATURE_IPV6
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100417 build_ipv6_addr(local_addr, &param->localaddr.sin6);
418 build_ipv6_addr(rem_addr, &param->remaddr.sin6);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000419#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000420 } else {
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100421 build_ipv4_addr(local_addr, &param->localaddr.sin);
422 build_ipv4_addr(rem_addr, &param->remaddr.sin);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000423 }
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000424 return 0;
425}
Eric Andersen31a0ece2001-10-31 11:00:46 +0000426
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000427static void print_inet_line(struct inet_params *param,
428 const char *state_str, const char *proto, int is_connected)
429{
430 if ((is_connected && (flags & NETSTAT_CONNECTED))
431 || (!is_connected && (flags & NETSTAT_LISTENING))
Denis Vlasenko703e2022007-01-22 14:12:08 +0000432 ) {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000433 char *l = ip_port_str(
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100434 &param->localaddr.sa, param->local_port,
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000435 proto, flags & NETSTAT_NUMERIC);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000436 char *r = ip_port_str(
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100437 &param->remaddr.sa, param->rem_port,
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000438 proto, flags & NETSTAT_NUMERIC);
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200439 printf(FMT_NET_CONN_DATA,
440 proto, param->rxq, param->txq,
441 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) l,
442 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) r,
443 state_str);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000444#if ENABLE_FEATURE_NETSTAT_PRG
445 if (option_mask32 & OPT_prg)
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000446 printf("%."PROGNAME_WIDTH_STR"s", prg_cache_get(param->inode));
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000447#endif
448 bb_putchar('\n');
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000449 free(l);
450 free(r);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000451 }
452}
453
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000454static int FAST_FUNC tcp_do_one(char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000455{
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000456 struct inet_params param;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000457
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200458 memset(&param, 0, sizeof(param));
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000459 if (scan_inet_proc_line(&param, line))
460 return 1;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000461
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000462 print_inet_line(&param, tcp_state[param.state], "tcp", param.rem_port);
463 return 0;
464}
Eric Andersen31a0ece2001-10-31 11:00:46 +0000465
Denis Vlasenko703e2022007-01-22 14:12:08 +0000466#if ENABLE_FEATURE_IPV6
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100467# define NOT_NULL_ADDR(A) ( \
468 ( (A.sa.sa_family == AF_INET6) \
469 && (A.sin6.sin6_addr.s6_addr32[0] | A.sin6.sin6_addr.s6_addr32[1] | \
470 A.sin6.sin6_addr.s6_addr32[2] | A.sin6.sin6_addr.s6_addr32[3]) \
471 ) || ( \
472 (A.sa.sa_family == AF_INET) \
473 && A.sin.sin_addr.s_addr != 0 \
474 ) \
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000475)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000476#else
Denys Vlasenko45e97922010-02-19 08:29:32 +0100477# define NOT_NULL_ADDR(A) (A.sin.sin_addr.s_addr)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000478#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000479
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000480static int FAST_FUNC udp_do_one(char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000481{
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000482 int have_remaddr;
483 const char *state_str;
484 struct inet_params param;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000485
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200486 memset(&param, 0, sizeof(param)); /* otherwise we display garbage IPv6 scope_ids */
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000487 if (scan_inet_proc_line(&param, line))
488 return 1;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000489
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000490 state_str = "UNKNOWN";
491 switch (param.state) {
492 case TCP_ESTABLISHED:
493 state_str = "ESTABLISHED";
494 break;
495 case TCP_CLOSE:
496 state_str = "";
497 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000498 }
499
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100500 have_remaddr = NOT_NULL_ADDR(param.remaddr);
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000501 print_inet_line(&param, state_str, "udp", have_remaddr);
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000502 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000503}
504
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000505static int FAST_FUNC raw_do_one(char *line)
506{
507 int have_remaddr;
508 struct inet_params param;
509
510 if (scan_inet_proc_line(&param, line))
511 return 1;
512
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100513 have_remaddr = NOT_NULL_ADDR(param.remaddr);
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000514 print_inet_line(&param, itoa(param.state), "raw", have_remaddr);
515 return 0;
516}
517
518static int FAST_FUNC unix_do_one(char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000519{
Eric Andersen31a0ece2001-10-31 11:00:46 +0000520 unsigned long refcnt, proto, unix_flags;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000521 unsigned long inode;
522 int type, state;
523 int num, path_ofs;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000524 const char *ss_proto, *ss_state, *ss_type;
525 char ss_flags[32];
Eric Andersen31a0ece2001-10-31 11:00:46 +0000526
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000527 /* 2.6.15 may report lines like "... @/tmp/fam-user-^@^@^@^@^@^@^@..."
Denis Vlasenko474d1c52008-01-07 19:06:47 +0000528 * Other users report long lines filled by NUL bytes.
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000529 * (those ^@ are NUL bytes too). We see them as empty lines. */
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000530 if (!line[0])
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000531 return 0;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000532
533 path_ofs = 0; /* paranoia */
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000534 num = sscanf(line, "%*p: %lX %lX %lX %X %X %lu %n",
535 &refcnt, &proto, &unix_flags, &type, &state, &inode, &path_ofs);
536 if (num < 6) {
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000537 return 1; /* error */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000538 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000539 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000540 if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000541 if (!(flags & NETSTAT_LISTENING))
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000542 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000543 } else {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000544 if (!(flags & NETSTAT_CONNECTED))
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000545 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000546 }
547 }
548
549 switch (proto) {
550 case 0:
551 ss_proto = "unix";
552 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000553 default:
554 ss_proto = "??";
555 }
556
557 switch (type) {
558 case SOCK_STREAM:
559 ss_type = "STREAM";
560 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000561 case SOCK_DGRAM:
562 ss_type = "DGRAM";
563 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000564 case SOCK_RAW:
565 ss_type = "RAW";
566 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000567 case SOCK_RDM:
568 ss_type = "RDM";
569 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000570 case SOCK_SEQPACKET:
571 ss_type = "SEQPACKET";
572 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000573 default:
574 ss_type = "UNKNOWN";
575 }
576
577 switch (state) {
578 case SS_FREE:
579 ss_state = "FREE";
580 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000581 case SS_UNCONNECTED:
582 /*
583 * Unconnected sockets may be listening
584 * for something.
585 */
586 if (unix_flags & SO_ACCEPTCON) {
587 ss_state = "LISTENING";
588 } else {
589 ss_state = "";
590 }
591 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000592 case SS_CONNECTING:
593 ss_state = "CONNECTING";
594 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000595 case SS_CONNECTED:
596 ss_state = "CONNECTED";
597 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000598 case SS_DISCONNECTING:
599 ss_state = "DISCONNECTING";
600 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000601 default:
602 ss_state = "UNKNOWN";
603 }
604
605 strcpy(ss_flags, "[ ");
606 if (unix_flags & SO_ACCEPTCON)
607 strcat(ss_flags, "ACC ");
608 if (unix_flags & SO_WAITDATA)
609 strcat(ss_flags, "W ");
610 if (unix_flags & SO_NOSPACE)
611 strcat(ss_flags, "N ");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000612 strcat(ss_flags, "]");
613
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000614 printf("%-5s %-6ld %-11s %-10s %-13s %6lu ",
615 ss_proto, refcnt, ss_flags, ss_type, ss_state, inode
616 );
617
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000618#if ENABLE_FEATURE_NETSTAT_PRG
619 if (option_mask32 & OPT_prg)
620 printf("%-"PROGNAME_WIDTH_STR"s", prg_cache_get(inode));
621#endif
622
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000623 /* TODO: currently we stop at first NUL byte. Is it a problem? */
624 line += path_ofs;
625 *strchrnul(line, '\n') = '\0';
626 while (*line)
627 fputc_printable(*line++, stdout);
628 bb_putchar('\n');
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000629 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000630}
631
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000632static void do_info(const char *file, int FAST_FUNC (*proc)(char *))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000633{
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000634 int lnr;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000635 FILE *procinfo;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000636 char *buffer;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000637
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000638 /* _stdin is just to save "r" param */
639 procinfo = fopen_or_warn_stdin(file);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000640 if (procinfo == NULL) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000641 return;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000642 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000643 lnr = 0;
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000644 /* Why xmalloc_fgets_str? because it doesn't stop on NULs */
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000645 while ((buffer = xmalloc_fgets_str(procinfo, "\n")) != NULL) {
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000646 /* line 0 is skipped */
647 if (lnr && proc(buffer))
648 bb_error_msg("%s: bogus data on line %d", file, lnr + 1);
649 lnr++;
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000650 free(buffer);
651 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000652 fclose(procinfo);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000653}
654
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000655int netstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000656int netstat_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000657{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000658 unsigned opt;
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000659
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000660 INIT_G();
661
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000662 /* Option string must match NETSTAT_xxx constants */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000663 opt = getopt32(argv, NETSTAT_OPTS);
Bob Dunlop55a046b2010-10-27 02:12:29 +0200664 if (opt & OPT_sock_listen) { // -l
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000665 flags &= ~NETSTAT_CONNECTED;
666 flags |= NETSTAT_LISTENING;
667 }
Bob Dunlop55a046b2010-10-27 02:12:29 +0200668 if (opt & OPT_sock_all) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a
669 //if (opt & OPT_extended) // -e
670 if (opt & OPT_noresolve) flags |= NETSTAT_NUMERIC; // -n
671 //if (opt & OPT_sock_tcp) // -t: NETSTAT_TCP
672 //if (opt & OPT_sock_udp) // -u: NETSTAT_UDP
673 //if (opt & OPT_sock_raw) // -w: NETSTAT_RAW
674 //if (opt & OPT_sock_unix) // -x: NETSTAT_UNIX
Denis Vlasenko703e2022007-01-22 14:12:08 +0000675#if ENABLE_ROUTE
Bob Dunlop55a046b2010-10-27 02:12:29 +0200676 if (opt & OPT_route) { // -r
Denis Vlasenko703e2022007-01-22 14:12:08 +0000677 bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000678 return 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000679 }
Bob Dunlop55a046b2010-10-27 02:12:29 +0200680#endif
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200681#if ENABLE_FEATURE_NETSTAT_WIDE
682 G.addr_width = ADDR_NORMAL_WIDTH;
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000683 if (opt & OPT_wide) { // -W
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200684 G.addr_width = ADDR_WIDE;
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000685 }
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200686#endif
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000687#if ENABLE_FEATURE_NETSTAT_PRG
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200688 progname_banner = "";
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000689 if (opt & OPT_prg) { // -p
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200690 progname_banner = PROGNAME_BANNER;
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000691 prg_cache_load();
692 }
693#endif
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000694
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000695 opt &= NETSTAT_ALLPROTO;
696 if (opt) {
697 flags &= ~NETSTAT_ALLPROTO;
698 flags |= opt;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000699 }
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000700 if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) {
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200701 printf("Active Internet connections "); /* xxx */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000702
Denis Vlasenko703e2022007-01-22 14:12:08 +0000703 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000704 printf("(servers and established)");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000705 else if (flags & NETSTAT_LISTENING)
706 printf("(only servers)");
707 else
708 printf("(w/o servers)");
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200709 printf(FMT_NET_CONN_HEADER,
710 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) "Local Address",
711 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) "Foreign Address",
712 progname_banner
713 );
Eric Andersen31a0ece2001-10-31 11:00:46 +0000714 }
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000715 if (flags & NETSTAT_TCP) {
716 do_info("/proc/net/tcp", tcp_do_one);
Denis Vlasenko703e2022007-01-22 14:12:08 +0000717#if ENABLE_FEATURE_IPV6
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000718 do_info("/proc/net/tcp6", tcp_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000719#endif
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000720 }
721 if (flags & NETSTAT_UDP) {
722 do_info("/proc/net/udp", udp_do_one);
Denis Vlasenko703e2022007-01-22 14:12:08 +0000723#if ENABLE_FEATURE_IPV6
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000724 do_info("/proc/net/udp6", udp_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000725#endif
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000726 }
727 if (flags & NETSTAT_RAW) {
728 do_info("/proc/net/raw", raw_do_one);
Denis Vlasenko703e2022007-01-22 14:12:08 +0000729#if ENABLE_FEATURE_IPV6
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000730 do_info("/proc/net/raw6", raw_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000731#endif
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000732 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000733 if (flags & NETSTAT_UNIX) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000734 printf("Active UNIX domain sockets ");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000735 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000736 printf("(servers and established)");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000737 else if (flags & NETSTAT_LISTENING)
738 printf("(only servers)");
739 else
740 printf("(w/o servers)");
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200741 printf("\nProto RefCnt Flags Type State I-Node %sPath\n", progname_banner);
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000742 do_info("/proc/net/unix", unix_do_one);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000743 }
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000744 prg_cache_clear();
Eric Andersen31a0ece2001-10-31 11:00:46 +0000745 return 0;
746}