1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43#include "libbb.h"
44#include "inet_common.h"
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70#define NETSTAT_OPTS "laentuwx" \
71 IF_ROUTE( "r") \
72 IF_FEATURE_NETSTAT_WIDE("W") \
73 IF_FEATURE_NETSTAT_PRG( "p")
74
75enum {
76 OPT_sock_listen = 1 << 0,
77 OPT_sock_all = 1 << 1,
78 OPT_extended = 1 << 2,
79 OPT_noresolve = 1 << 3,
80 OPT_sock_tcp = 1 << 4,
81 OPT_sock_udp = 1 << 5,
82 OPT_sock_raw = 1 << 6,
83 OPT_sock_unix = 1 << 7,
84 OPTBIT_x = 7,
85 IF_ROUTE( OPTBIT_ROUTE,)
86 IF_FEATURE_NETSTAT_WIDE(OPTBIT_WIDE ,)
87 IF_FEATURE_NETSTAT_PRG( OPTBIT_PRG ,)
88 OPT_route = IF_ROUTE( (1 << OPTBIT_ROUTE)) + 0,
89 OPT_wide = IF_FEATURE_NETSTAT_WIDE((1 << OPTBIT_WIDE )) + 0,
90 OPT_prg = IF_FEATURE_NETSTAT_PRG( (1 << OPTBIT_PRG )) + 0,
91};
92
93#define NETSTAT_CONNECTED 0x01
94#define NETSTAT_LISTENING 0x02
95#define NETSTAT_NUMERIC 0x04
96
97#define NETSTAT_TCP 0x10
98#define NETSTAT_UDP 0x20
99#define NETSTAT_RAW 0x40
100#define NETSTAT_UNIX 0x80
101#define NETSTAT_ALLPROTO (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW|NETSTAT_UNIX)
102
103
104enum {
105 TCP_ESTABLISHED = 1,
106 TCP_SYN_SENT,
107 TCP_SYN_RECV,
108 TCP_FIN_WAIT1,
109 TCP_FIN_WAIT2,
110 TCP_TIME_WAIT,
111 TCP_CLOSE,
112 TCP_CLOSE_WAIT,
113 TCP_LAST_ACK,
114 TCP_LISTEN,
115 TCP_CLOSING,
116};
117
118static const char *const tcp_state[] = {
119 "",
120 "ESTABLISHED",
121 "SYN_SENT",
122 "SYN_RECV",
123 "FIN_WAIT1",
124 "FIN_WAIT2",
125 "TIME_WAIT",
126 "CLOSE",
127 "CLOSE_WAIT",
128 "LAST_ACK",
129 "LISTEN",
130 "CLOSING"
131};
132
133typedef enum {
134 SS_FREE = 0,
135 SS_UNCONNECTED,
136 SS_CONNECTING,
137 SS_CONNECTED,
138 SS_DISCONNECTING
139} socket_state;
140
141#define SO_ACCEPTCON (1<<16)
142#define SO_WAITDATA (1<<17)
143#define SO_NOSPACE (1<<18)
144
145#define ADDR_NORMAL_WIDTH 23
146
147
148
149
150
151
152#define ADDR_WIDE 51
153#if ENABLE_FEATURE_NETSTAT_WIDE
154# define FMT_NET_CONN_DATA "%s %6lu %6lu %-*s %-*s %-12s"
155# define FMT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-*s %-*s State %s\n"
156#else
157# define FMT_NET_CONN_DATA "%s %6lu %6lu %-23s %-23s %-12s"
158# define FMT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-23s %-23s State %s\n"
159#endif
160
161#define PROGNAME_WIDTH 20
162#define PROGNAME_WIDTH_STR "20"
163
164#define PROGNAME_BANNER "PID/Program name "
165
166struct prg_node {
167 struct prg_node *next;
168 long inode;
169 char name[PROGNAME_WIDTH];
170};
171
172#define PRG_HASH_SIZE 211
173
174struct globals {
175 smallint flags;
176#if ENABLE_FEATURE_NETSTAT_PRG
177 smallint prg_cache_loaded;
178 struct prg_node *prg_hash[PRG_HASH_SIZE];
179#endif
180#if ENABLE_FEATURE_NETSTAT_PRG
181 const char *progname_banner;
182#endif
183#if ENABLE_FEATURE_NETSTAT_WIDE
184 unsigned addr_width;
185#endif
186};
187#define G (*ptr_to_globals)
188#define flags (G.flags )
189#define prg_cache_loaded (G.prg_cache_loaded)
190#define prg_hash (G.prg_hash )
191#if ENABLE_FEATURE_NETSTAT_PRG
192# define progname_banner (G.progname_banner )
193#else
194# define progname_banner ""
195#endif
196#define INIT_G() do { \
197 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
198 flags = NETSTAT_CONNECTED | NETSTAT_ALLPROTO; \
199} while (0)
200
201
202#if ENABLE_FEATURE_NETSTAT_PRG
203
204
205#define PRG_HASHIT(x) ((unsigned)(x) % PRG_HASH_SIZE)
206
207static void prg_cache_add(long inode, char *name)
208{
209 unsigned hi = PRG_HASHIT(inode);
210 struct prg_node **pnp, *pn;
211
212 prg_cache_loaded = 2;
213 for (pnp = prg_hash + hi; (pn = *pnp) != NULL; pnp = &pn->next) {
214 if (pn->inode == inode) {
215
216
217 return;
218 }
219 }
220 *pnp = xzalloc(sizeof(struct prg_node));
221 pn = *pnp;
222 pn->inode = inode;
223 safe_strncpy(pn->name, name, PROGNAME_WIDTH);
224}
225
226static const char *prg_cache_get(long inode)
227{
228 unsigned hi = PRG_HASHIT(inode);
229 struct prg_node *pn;
230
231 for (pn = prg_hash[hi]; pn; pn = pn->next)
232 if (pn->inode == inode)
233 return pn->name;
234 return "-";
235}
236
237#if ENABLE_FEATURE_CLEAN_UP
238static void prg_cache_clear(void)
239{
240 struct prg_node **pnp, *pn;
241
242 for (pnp = prg_hash; pnp < prg_hash + PRG_HASH_SIZE; pnp++) {
243 while ((pn = *pnp) != NULL) {
244 *pnp = pn->next;
245 free(pn);
246 }
247 }
248}
249#else
250#define prg_cache_clear() ((void)0)
251#endif
252
253static long extract_socket_inode(const char *lname)
254{
255 long inode = -1;
256
257 if (is_prefixed_with(lname, "socket:[")) {
258
259 inode = bb_strtoul(lname + sizeof("socket:[")-1, (char**)&lname, 0);
260 if (*lname != ']')
261 inode = -1;
262 } else if (is_prefixed_with(lname, "[0000]:")) {
263
264 inode = bb_strtoul(lname + sizeof("[0000]:")-1, NULL, 0);
265 if (errno)
266 inode = -1;
267 }
268
269#if 0
270 if (errno == ERANGE)
271 inode = -1;
272#endif
273 return inode;
274}
275
276static int FAST_FUNC add_to_prg_cache_if_socket(const char *fileName,
277 struct stat *statbuf UNUSED_PARAM,
278 void *pid_slash_progname,
279 int depth UNUSED_PARAM)
280{
281 char *linkname;
282 long inode;
283
284 linkname = xmalloc_readlink(fileName);
285 if (linkname != NULL) {
286 inode = extract_socket_inode(linkname);
287 free(linkname);
288 if (inode >= 0)
289 prg_cache_add(inode, (char *)pid_slash_progname);
290 }
291 return TRUE;
292}
293
294static int FAST_FUNC dir_act(const char *fileName,
295 struct stat *statbuf UNUSED_PARAM,
296 void *userData UNUSED_PARAM,
297 int depth)
298{
299 const char *pid;
300 char *pid_slash_progname;
301 char proc_pid_fname[sizeof("/proc/%u/cmdline") + sizeof(long)*3];
302 char cmdline_buf[512];
303 int n, len;
304
305 if (depth == 0)
306 return TRUE;
307
308 pid = fileName + sizeof("/proc/")-1;
309 if (!isdigit(pid[0]))
310 return SKIP;
311
312 len = snprintf(proc_pid_fname, sizeof(proc_pid_fname), "%s/cmdline", fileName);
313 n = open_read_close(proc_pid_fname, cmdline_buf, sizeof(cmdline_buf) - 1);
314 if (n < 0)
315 return FALSE;
316 cmdline_buf[n] = '\0';
317
318
319 strcpy(proc_pid_fname + len - (sizeof("cmdline")-1), "fd");
320 pid_slash_progname = concat_path_file(pid, bb_basename(cmdline_buf));
321 n = recursive_action(proc_pid_fname,
322 ACTION_RECURSE | ACTION_QUIET,
323 add_to_prg_cache_if_socket,
324 NULL,
325 (void *)pid_slash_progname,
326 0);
327 free(pid_slash_progname);
328
329 if (!n)
330 return FALSE;
331
332 return SKIP;
333}
334
335static void prg_cache_load(void)
336{
337 int load_ok;
338
339 prg_cache_loaded = 1;
340 load_ok = recursive_action("/proc", ACTION_RECURSE | ACTION_QUIET,
341 NULL, dir_act, NULL, 0);
342 if (load_ok)
343 return;
344
345 if (prg_cache_loaded == 1)
346 bb_error_msg("can't scan /proc - are you root?");
347 else
348 bb_error_msg("showing only processes with your user ID");
349}
350
351#else
352
353#define prg_cache_clear() ((void)0)
354
355#endif
356
357
358#if ENABLE_FEATURE_IPV6
359static void build_ipv6_addr(char* local_addr, struct sockaddr_in6* localaddr)
360{
361 char addr6[INET6_ADDRSTRLEN];
362 struct in6_addr in6;
363
364 sscanf(local_addr, "%08X%08X%08X%08X",
365 &in6.s6_addr32[0], &in6.s6_addr32[1],
366 &in6.s6_addr32[2], &in6.s6_addr32[3]);
367 inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6));
368 inet_pton(AF_INET6, addr6, &localaddr->sin6_addr);
369
370 localaddr->sin6_family = AF_INET6;
371}
372#endif
373
374static void build_ipv4_addr(char* local_addr, struct sockaddr_in* localaddr)
375{
376 sscanf(local_addr, "%X", &localaddr->sin_addr.s_addr);
377 localaddr->sin_family = AF_INET;
378}
379
380static const char *get_sname(int port, const char *proto, int numeric)
381{
382 if (!port)
383 return "*";
384 if (!numeric) {
385 struct servent *se = getservbyport(port, proto);
386 if (se)
387 return se->s_name;
388 }
389
390 return itoa(ntohs(port));
391}
392
393static char *ip_port_str(struct sockaddr *addr, int port, const char *proto, int numeric)
394{
395 char *host, *host_port;
396
397
398
399
400 host = NULL;
401 if (!numeric)
402 host = xmalloc_sockaddr2host_noport(addr);
403 if (!host)
404 host = xmalloc_sockaddr2dotted_noport(addr);
405
406 host_port = xasprintf("%s:%s", host, get_sname(htons(port), proto, numeric));
407 free(host);
408 return host_port;
409}
410
411struct inet_params {
412 int local_port, rem_port, state, uid;
413 union {
414 struct sockaddr sa;
415 struct sockaddr_in sin;
416#if ENABLE_FEATURE_IPV6
417 struct sockaddr_in6 sin6;
418#endif
419 } localaddr, remaddr;
420 unsigned long rxq, txq, inode;
421};
422
423static int scan_inet_proc_line(struct inet_params *param, char *line)
424{
425 int num;
426
427
428
429 char local_addr[33], rem_addr[33];
430
431 num = sscanf(line,
432 "%*d: %32[0-9A-Fa-f]:%X "
433 "%32[0-9A-Fa-f]:%X %X "
434 "%lX:%lX %*X:%*X "
435 "%*X %d %*d %lu ",
436 local_addr, ¶m->local_port,
437 rem_addr, ¶m->rem_port, ¶m->state,
438 ¶m->txq, ¶m->rxq,
439 ¶m->uid, ¶m->inode);
440 if (num < 9) {
441 return 1;
442 }
443
444 if (strlen(local_addr) > 8) {
445#if ENABLE_FEATURE_IPV6
446 build_ipv6_addr(local_addr, ¶m->localaddr.sin6);
447 build_ipv6_addr(rem_addr, ¶m->remaddr.sin6);
448#endif
449 } else {
450 build_ipv4_addr(local_addr, ¶m->localaddr.sin);
451 build_ipv4_addr(rem_addr, ¶m->remaddr.sin);
452 }
453 return 0;
454}
455
456static void print_inet_line(struct inet_params *param,
457 const char *state_str, const char *proto, int is_connected)
458{
459 if ((is_connected && (flags & NETSTAT_CONNECTED))
460 || (!is_connected && (flags & NETSTAT_LISTENING))
461 ) {
462 char *l = ip_port_str(
463 ¶m->localaddr.sa, param->local_port,
464 proto, flags & NETSTAT_NUMERIC);
465 char *r = ip_port_str(
466 ¶m->remaddr.sa, param->rem_port,
467 proto, flags & NETSTAT_NUMERIC);
468 printf(FMT_NET_CONN_DATA,
469 proto, param->rxq, param->txq,
470 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) l,
471 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) r,
472 state_str);
473#if ENABLE_FEATURE_NETSTAT_PRG
474 if (option_mask32 & OPT_prg)
475 printf("%."PROGNAME_WIDTH_STR"s", prg_cache_get(param->inode));
476#endif
477 bb_putchar('\n');
478 free(l);
479 free(r);
480 }
481}
482
483static int FAST_FUNC tcp_do_one(char *line)
484{
485 struct inet_params param;
486
487 memset(¶m, 0, sizeof(param));
488 if (scan_inet_proc_line(¶m, line))
489 return 1;
490
491 print_inet_line(¶m, tcp_state[param.state], "tcp", param.rem_port);
492 return 0;
493}
494
495#if ENABLE_FEATURE_IPV6
496# define NOT_NULL_ADDR(A) ( \
497 ( (A.sa.sa_family == AF_INET6) \
498 && (A.sin6.sin6_addr.s6_addr32[0] | A.sin6.sin6_addr.s6_addr32[1] | \
499 A.sin6.sin6_addr.s6_addr32[2] | A.sin6.sin6_addr.s6_addr32[3]) \
500 ) || ( \
501 (A.sa.sa_family == AF_INET) \
502 && A.sin.sin_addr.s_addr != 0 \
503 ) \
504)
505#else
506# define NOT_NULL_ADDR(A) (A.sin.sin_addr.s_addr)
507#endif
508
509static int FAST_FUNC udp_do_one(char *line)
510{
511 int have_remaddr;
512 const char *state_str;
513 struct inet_params param;
514
515 memset(¶m, 0, sizeof(param));
516 if (scan_inet_proc_line(¶m, line))
517 return 1;
518
519 state_str = "UNKNOWN";
520 switch (param.state) {
521 case TCP_ESTABLISHED:
522 state_str = "ESTABLISHED";
523 break;
524 case TCP_CLOSE:
525 state_str = "";
526 break;
527 }
528
529 have_remaddr = NOT_NULL_ADDR(param.remaddr);
530 print_inet_line(¶m, state_str, "udp", have_remaddr);
531 return 0;
532}
533
534static int FAST_FUNC raw_do_one(char *line)
535{
536 int have_remaddr;
537 struct inet_params param;
538
539 if (scan_inet_proc_line(¶m, line))
540 return 1;
541
542 have_remaddr = NOT_NULL_ADDR(param.remaddr);
543 print_inet_line(¶m, itoa(param.state), "raw", have_remaddr);
544 return 0;
545}
546
547static int FAST_FUNC unix_do_one(char *line)
548{
549 unsigned long refcnt, proto, unix_flags;
550 unsigned long inode;
551 int type, state;
552 int num, path_ofs;
553 const char *ss_proto, *ss_state, *ss_type;
554 char ss_flags[32];
555
556
557
558
559 if (!line[0])
560 return 0;
561
562 path_ofs = 0;
563 num = sscanf(line, "%*p: %lX %lX %lX %X %X %lu %n",
564 &refcnt, &proto, &unix_flags, &type, &state, &inode, &path_ofs);
565 if (num < 6) {
566 return 1;
567 }
568 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) {
569 if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) {
570 if (!(flags & NETSTAT_LISTENING))
571 return 0;
572 } else {
573 if (!(flags & NETSTAT_CONNECTED))
574 return 0;
575 }
576 }
577
578 switch (proto) {
579 case 0:
580 ss_proto = "unix";
581 break;
582 default:
583 ss_proto = "??";
584 }
585
586 switch (type) {
587 case SOCK_STREAM:
588 ss_type = "STREAM";
589 break;
590 case SOCK_DGRAM:
591 ss_type = "DGRAM";
592 break;
593 case SOCK_RAW:
594 ss_type = "RAW";
595 break;
596 case SOCK_RDM:
597 ss_type = "RDM";
598 break;
599 case SOCK_SEQPACKET:
600 ss_type = "SEQPACKET";
601 break;
602 default:
603 ss_type = "UNKNOWN";
604 }
605
606 switch (state) {
607 case SS_FREE:
608 ss_state = "FREE";
609 break;
610 case SS_UNCONNECTED:
611
612
613
614
615 if (unix_flags & SO_ACCEPTCON) {
616 ss_state = "LISTENING";
617 } else {
618 ss_state = "";
619 }
620 break;
621 case SS_CONNECTING:
622 ss_state = "CONNECTING";
623 break;
624 case SS_CONNECTED:
625 ss_state = "CONNECTED";
626 break;
627 case SS_DISCONNECTING:
628 ss_state = "DISCONNECTING";
629 break;
630 default:
631 ss_state = "UNKNOWN";
632 }
633
634 strcpy(ss_flags, "[ ");
635 if (unix_flags & SO_ACCEPTCON)
636 strcat(ss_flags, "ACC ");
637 if (unix_flags & SO_WAITDATA)
638 strcat(ss_flags, "W ");
639 if (unix_flags & SO_NOSPACE)
640 strcat(ss_flags, "N ");
641 strcat(ss_flags, "]");
642
643 printf("%-5s %-6lu %-11s %-10s %-13s %6lu ",
644 ss_proto, refcnt, ss_flags, ss_type, ss_state, inode
645 );
646
647#if ENABLE_FEATURE_NETSTAT_PRG
648 if (option_mask32 & OPT_prg)
649 printf("%-"PROGNAME_WIDTH_STR"s", prg_cache_get(inode));
650#endif
651
652
653 line += path_ofs;
654 chomp(line);
655 while (*line)
656 fputc_printable(*line++, stdout);
657 bb_putchar('\n');
658 return 0;
659}
660
661static void do_info(const char *file, int FAST_FUNC (*proc)(char *))
662{
663 int lnr;
664 FILE *procinfo;
665 char *buffer;
666
667
668 procinfo = fopen_or_warn_stdin(file);
669 if (procinfo == NULL) {
670 return;
671 }
672 lnr = 0;
673
674 while ((buffer = xmalloc_fgets_str(procinfo, "\n")) != NULL) {
675
676 if (lnr && proc(buffer))
677 bb_error_msg("%s: bogus data on line %d", file, lnr + 1);
678 lnr++;
679 free(buffer);
680 }
681 fclose(procinfo);
682}
683
684int netstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
685int netstat_main(int argc UNUSED_PARAM, char **argv)
686{
687 unsigned opt;
688
689 INIT_G();
690
691
692 opt = getopt32(argv, NETSTAT_OPTS);
693 if (opt & OPT_sock_listen) {
694 flags &= ~NETSTAT_CONNECTED;
695 flags |= NETSTAT_LISTENING;
696 }
697 if (opt & OPT_sock_all) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED;
698
699 if (opt & OPT_noresolve) flags |= NETSTAT_NUMERIC;
700
701
702
703
704#if ENABLE_ROUTE
705 if (opt & OPT_route) {
706 bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended));
707 return 0;
708 }
709#endif
710#if ENABLE_FEATURE_NETSTAT_WIDE
711 G.addr_width = ADDR_NORMAL_WIDTH;
712 if (opt & OPT_wide) {
713 G.addr_width = ADDR_WIDE;
714 }
715#endif
716#if ENABLE_FEATURE_NETSTAT_PRG
717 progname_banner = "";
718 if (opt & OPT_prg) {
719 progname_banner = PROGNAME_BANNER;
720 prg_cache_load();
721 }
722#endif
723
724 opt &= NETSTAT_ALLPROTO;
725 if (opt) {
726 flags &= ~NETSTAT_ALLPROTO;
727 flags |= opt;
728 }
729 if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) {
730 printf("Active Internet connections ");
731
732 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
733 printf("(servers and established)");
734 else if (flags & NETSTAT_LISTENING)
735 printf("(only servers)");
736 else
737 printf("(w/o servers)");
738 printf(FMT_NET_CONN_HEADER,
739 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) "Local Address",
740 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) "Foreign Address",
741 progname_banner
742 );
743 }
744 if (flags & NETSTAT_TCP) {
745 do_info("/proc/net/tcp", tcp_do_one);
746#if ENABLE_FEATURE_IPV6
747 do_info("/proc/net/tcp6", tcp_do_one);
748#endif
749 }
750 if (flags & NETSTAT_UDP) {
751 do_info("/proc/net/udp", udp_do_one);
752#if ENABLE_FEATURE_IPV6
753 do_info("/proc/net/udp6", udp_do_one);
754#endif
755 }
756 if (flags & NETSTAT_RAW) {
757 do_info("/proc/net/raw", raw_do_one);
758#if ENABLE_FEATURE_IPV6
759 do_info("/proc/net/raw6", raw_do_one);
760#endif
761 }
762 if (flags & NETSTAT_UNIX) {
763 printf("Active UNIX domain sockets ");
764 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
765 printf("(servers and established)");
766 else if (flags & NETSTAT_LISTENING)
767 printf("(only servers)");
768 else
769 printf("(w/o servers)");
770 printf("\nProto RefCnt Flags Type State I-Node %sPath\n", progname_banner);
771 do_info("/proc/net/unix", unix_do_one);
772 }
773 prg_cache_clear();
774 return 0;
775}
776