osdl.net!shemminger | b9de3ec | 2005-01-19 00:23:53 +0000 | [diff] [blame] | 1 | /* |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2 | * ss.c "sockstat", socket statistics |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <unistd.h> |
| 15 | #include <syslog.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <sys/ioctl.h> |
| 18 | #include <sys/socket.h> |
| 19 | #include <sys/uio.h> |
| 20 | #include <netinet/in.h> |
| 21 | #include <string.h> |
| 22 | #include <errno.h> |
| 23 | #include <netdb.h> |
| 24 | #include <arpa/inet.h> |
| 25 | #include <resolv.h> |
| 26 | #include <dirent.h> |
| 27 | #include <fnmatch.h> |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 28 | #include <getopt.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 29 | |
| 30 | #include "utils.h" |
| 31 | #include "rt_names.h" |
| 32 | #include "ll_map.h" |
| 33 | #include "libnetlink.h" |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 34 | #include "SNAPSHOT.h" |
| 35 | |
Stephen Hemminger | 77aa4d0 | 2007-12-31 10:41:18 -0800 | [diff] [blame] | 36 | #include <netinet/tcp.h> |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 37 | #include <linux/inet_diag.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 38 | |
| 39 | int resolve_hosts = 0; |
| 40 | int resolve_services = 1; |
| 41 | int preferred_family = AF_UNSPEC; |
| 42 | int show_options = 0; |
| 43 | int show_details = 0; |
| 44 | int show_users = 0; |
| 45 | int show_mem = 0; |
| 46 | int show_tcpinfo = 0; |
| 47 | |
| 48 | int netid_width; |
| 49 | int state_width; |
| 50 | int addrp_width; |
| 51 | int addr_width; |
| 52 | int serv_width; |
| 53 | int screen_width; |
| 54 | |
| 55 | static const char *TCP_PROTO = "tcp"; |
| 56 | static const char *UDP_PROTO = "udp"; |
| 57 | static const char *RAW_PROTO = "raw"; |
| 58 | static const char *dg_proto = NULL; |
| 59 | |
| 60 | enum |
| 61 | { |
| 62 | TCP_DB, |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 63 | DCCP_DB, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 64 | UDP_DB, |
| 65 | RAW_DB, |
| 66 | UNIX_DG_DB, |
| 67 | UNIX_ST_DB, |
| 68 | PACKET_DG_DB, |
| 69 | PACKET_R_DB, |
| 70 | NETLINK_DB, |
| 71 | MAX_DB |
| 72 | }; |
| 73 | |
| 74 | #define PACKET_DBM ((1<<PACKET_DG_DB)|(1<<PACKET_R_DB)) |
| 75 | #define UNIX_DBM ((1<<UNIX_DG_DB)|(1<<UNIX_ST_DB)) |
| 76 | #define ALL_DB ((1<<MAX_DB)-1) |
| 77 | |
| 78 | enum { |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 79 | SS_UNKNOWN, |
| 80 | SS_ESTABLISHED, |
| 81 | SS_SYN_SENT, |
| 82 | SS_SYN_RECV, |
| 83 | SS_FIN_WAIT1, |
| 84 | SS_FIN_WAIT2, |
| 85 | SS_TIME_WAIT, |
| 86 | SS_CLOSE, |
| 87 | SS_CLOSE_WAIT, |
| 88 | SS_LAST_ACK, |
| 89 | SS_LISTEN, |
| 90 | SS_CLOSING, |
| 91 | SS_MAX |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | #define SS_ALL ((1<<SS_MAX)-1) |
| 95 | |
| 96 | #include "ssfilter.h" |
| 97 | |
| 98 | struct filter |
| 99 | { |
| 100 | int dbs; |
| 101 | int states; |
| 102 | int families; |
| 103 | struct ssfilter *f; |
| 104 | }; |
| 105 | |
| 106 | struct filter default_filter = { |
Stephen Hemminger | e7113c6 | 2007-07-10 18:26:54 -0700 | [diff] [blame] | 107 | .dbs = (1<<TCP_DB), |
| 108 | .states = SS_ALL & ~((1<<SS_LISTEN)|(1<<SS_CLOSE)|(1<<SS_TIME_WAIT)|(1<<SS_SYN_RECV)), |
| 109 | .families= (1<<AF_INET)|(1<<AF_INET6), |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | struct filter current_filter; |
| 113 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 114 | static FILE *generic_proc_open(const char *env, const char *name) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 115 | { |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 116 | const char *p = getenv(env); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 117 | char store[128]; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 118 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 119 | if (!p) { |
| 120 | p = getenv("PROC_ROOT") ? : "/proc"; |
| 121 | snprintf(store, sizeof(store)-1, "%s/%s", p, name); |
| 122 | p = store; |
| 123 | } |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 124 | |
| 125 | return fopen(p, "r"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 128 | static FILE *net_tcp_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 129 | { |
| 130 | return generic_proc_open("PROC_NET_TCP", "net/tcp"); |
| 131 | } |
| 132 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 133 | static FILE *net_tcp6_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 134 | { |
| 135 | return generic_proc_open("PROC_NET_TCP6", "net/tcp6"); |
| 136 | } |
| 137 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 138 | static FILE *net_udp_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 139 | { |
| 140 | return generic_proc_open("PROC_NET_UDP", "net/udp"); |
| 141 | } |
| 142 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 143 | static FILE *net_udp6_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 144 | { |
| 145 | return generic_proc_open("PROC_NET_UDP6", "net/udp6"); |
| 146 | } |
| 147 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 148 | static FILE *net_raw_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 149 | { |
| 150 | return generic_proc_open("PROC_NET_RAW", "net/raw"); |
| 151 | } |
| 152 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 153 | static FILE *net_raw6_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 154 | { |
| 155 | return generic_proc_open("PROC_NET_RAW6", "net/raw6"); |
| 156 | } |
| 157 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 158 | static FILE *net_unix_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 159 | { |
| 160 | return generic_proc_open("PROC_NET_UNIX", "net/unix"); |
| 161 | } |
| 162 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 163 | static FILE *net_packet_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 164 | { |
| 165 | return generic_proc_open("PROC_NET_PACKET", "net/packet"); |
| 166 | } |
| 167 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 168 | static FILE *net_netlink_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 169 | { |
| 170 | return generic_proc_open("PROC_NET_NETLINK", "net/netlink"); |
| 171 | } |
| 172 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 173 | static FILE *slabinfo_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 174 | { |
| 175 | return generic_proc_open("PROC_SLABINFO", "slabinfo"); |
| 176 | } |
| 177 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 178 | static FILE *net_sockstat_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 179 | { |
| 180 | return generic_proc_open("PROC_NET_SOCKSTAT", "net/sockstat"); |
| 181 | } |
| 182 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 183 | static FILE *net_sockstat6_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 184 | { |
| 185 | return generic_proc_open("PROC_NET_SOCKSTAT6", "net/sockstat6"); |
| 186 | } |
| 187 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 188 | static FILE *net_snmp_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 189 | { |
| 190 | return generic_proc_open("PROC_NET_SNMP", "net/snmp"); |
| 191 | } |
| 192 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 193 | static FILE *ephemeral_ports_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 194 | { |
| 195 | return generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", "sys/net/ipv4/ip_local_port_range"); |
| 196 | } |
| 197 | |
Stephen Hemminger | e7113c6 | 2007-07-10 18:26:54 -0700 | [diff] [blame] | 198 | int find_users(unsigned ino, char *buf, int buflen) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 199 | { |
| 200 | char pattern[64]; |
| 201 | int pattern_len; |
| 202 | char *ptr = buf; |
| 203 | char name[1024]; |
| 204 | DIR *dir; |
| 205 | struct dirent *d; |
| 206 | int cnt = 0; |
| 207 | int nameoff; |
| 208 | |
| 209 | if (!ino) |
| 210 | return 0; |
| 211 | |
Stephen Hemminger | e7113c6 | 2007-07-10 18:26:54 -0700 | [diff] [blame] | 212 | sprintf(pattern, "socket:[%u]", ino); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 213 | pattern_len = strlen(pattern); |
| 214 | |
| 215 | strncpy(name, getenv("PROC_ROOT") ? : "/proc/", sizeof(name)/2); |
| 216 | name[sizeof(name)/2] = 0; |
| 217 | if (strlen(name) == 0 || |
| 218 | name[strlen(name)-1] != '/') |
| 219 | strcat(name, "/"); |
| 220 | nameoff = strlen(name); |
| 221 | if ((dir = opendir(name)) == NULL) |
| 222 | return 0; |
| 223 | |
| 224 | while ((d = readdir(dir)) != NULL) { |
| 225 | DIR *dir1; |
| 226 | struct dirent *d1; |
| 227 | int pid; |
| 228 | int pos; |
| 229 | char crap; |
| 230 | char process[16]; |
| 231 | |
| 232 | if (sscanf(d->d_name, "%d%c", &pid, &crap) != 1) |
| 233 | continue; |
| 234 | |
| 235 | sprintf(name+nameoff, "%d/fd/", pid); |
| 236 | pos = strlen(name); |
| 237 | if ((dir1 = opendir(name)) == NULL) |
| 238 | continue; |
| 239 | |
| 240 | process[0] = 0; |
| 241 | |
| 242 | while ((d1 = readdir(dir1)) != NULL) { |
| 243 | int fd, n; |
| 244 | char lnk[64]; |
| 245 | |
| 246 | if (sscanf(d1->d_name, "%d%c", &fd, &crap) != 1) |
| 247 | continue; |
| 248 | |
| 249 | sprintf(name+pos, "%d", fd); |
| 250 | n = readlink(name, lnk, sizeof(lnk)-1); |
| 251 | if (n != pattern_len || |
| 252 | memcmp(lnk, pattern, n)) |
| 253 | continue; |
| 254 | |
| 255 | if (ptr-buf >= buflen-1) |
| 256 | break; |
| 257 | |
| 258 | if (process[0] == 0) { |
| 259 | char tmp[1024]; |
| 260 | FILE *fp; |
| 261 | snprintf(tmp, sizeof(tmp), "%s/%d/stat", |
| 262 | getenv("PROC_ROOT") ? : "/proc", pid); |
| 263 | if ((fp = fopen(tmp, "r")) != NULL) { |
| 264 | fscanf(fp, "%*d (%[^)])", process); |
| 265 | fclose(fp); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | snprintf(ptr, buflen-(ptr-buf), "(\"%s\",%d,%d),", process, pid, fd); |
| 270 | ptr += strlen(ptr); |
| 271 | cnt++; |
| 272 | } |
| 273 | closedir(dir1); |
| 274 | } |
| 275 | closedir(dir); |
| 276 | if (ptr != buf) |
| 277 | ptr[-1] = 0; |
| 278 | return cnt; |
| 279 | } |
| 280 | |
| 281 | |
| 282 | /* Get stats from slab */ |
| 283 | |
| 284 | struct slabstat |
| 285 | { |
| 286 | int socks; |
| 287 | int tcp_ports; |
| 288 | int tcp_tws; |
| 289 | int tcp_syns; |
| 290 | int skbs; |
| 291 | }; |
| 292 | |
| 293 | struct slabstat slabstat; |
| 294 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 295 | static const char *slabstat_ids[] = |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 296 | { |
| 297 | "sock", |
| 298 | "tcp_bind_bucket", |
| 299 | "tcp_tw_bucket", |
| 300 | "tcp_open_request", |
| 301 | "skbuff_head_cache", |
| 302 | }; |
| 303 | |
| 304 | int get_slabstat(struct slabstat *s) |
| 305 | { |
| 306 | char buf[256]; |
| 307 | FILE *fp; |
| 308 | int cnt; |
| 309 | |
| 310 | memset(s, 0, sizeof(*s)); |
| 311 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 312 | fp = slabinfo_open(); |
| 313 | if (!fp) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 314 | return -1; |
| 315 | |
| 316 | cnt = sizeof(*s)/sizeof(int); |
| 317 | |
| 318 | fgets(buf, sizeof(buf), fp); |
| 319 | while(fgets(buf, sizeof(buf), fp) != NULL) { |
| 320 | int i; |
| 321 | for (i=0; i<sizeof(slabstat_ids)/sizeof(slabstat_ids[0]); i++) { |
| 322 | if (memcmp(buf, slabstat_ids[i], strlen(slabstat_ids[i])) == 0) { |
| 323 | sscanf(buf, "%*s%d", ((int *)s) + i); |
| 324 | cnt--; |
| 325 | break; |
| 326 | } |
| 327 | } |
| 328 | if (cnt <= 0) |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | fclose(fp); |
| 333 | return 0; |
| 334 | } |
| 335 | |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 336 | static const char *sstate_name[] = { |
| 337 | "UNKNOWN", |
| 338 | [TCP_ESTABLISHED] = "ESTAB", |
| 339 | [TCP_SYN_SENT] = "SYN-SENT", |
| 340 | [TCP_SYN_RECV] = "SYN-RECV", |
| 341 | [TCP_FIN_WAIT1] = "FIN-WAIT-1", |
| 342 | [TCP_FIN_WAIT2] = "FIN-WAIT-2", |
| 343 | [TCP_TIME_WAIT] = "TIME-WAIT", |
| 344 | [TCP_CLOSE] = "UNCONN", |
| 345 | [TCP_CLOSE_WAIT] = "CLOSE-WAIT", |
| 346 | [TCP_LAST_ACK] = "LAST-ACK", |
| 347 | [TCP_LISTEN] = "LISTEN", |
| 348 | [TCP_CLOSING] = "CLOSING", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 349 | }; |
| 350 | |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 351 | static const char *sstate_namel[] = { |
| 352 | "UNKNOWN", |
| 353 | [TCP_ESTABLISHED] = "established", |
| 354 | [TCP_SYN_SENT] = "syn-sent", |
| 355 | [TCP_SYN_RECV] = "syn-recv", |
| 356 | [TCP_FIN_WAIT1] = "fin-wait-1", |
| 357 | [TCP_FIN_WAIT2] = "fin-wait-2", |
| 358 | [TCP_TIME_WAIT] = "time-wait", |
| 359 | [TCP_CLOSE] = "unconnected", |
| 360 | [TCP_CLOSE_WAIT] = "close-wait", |
| 361 | [TCP_LAST_ACK] = "last-ack", |
| 362 | [TCP_LISTEN] = "listening", |
| 363 | [TCP_CLOSING] = "closing", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 364 | }; |
| 365 | |
| 366 | struct tcpstat |
| 367 | { |
| 368 | inet_prefix local; |
| 369 | inet_prefix remote; |
| 370 | int lport; |
| 371 | int rport; |
| 372 | int state; |
| 373 | int rq, wq; |
| 374 | int timer; |
| 375 | int timeout; |
| 376 | int retrs; |
Stephen Hemminger | e7113c6 | 2007-07-10 18:26:54 -0700 | [diff] [blame] | 377 | unsigned ino; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 378 | int probes; |
Stephen Hemminger | e7113c6 | 2007-07-10 18:26:54 -0700 | [diff] [blame] | 379 | unsigned uid; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 380 | int refcnt; |
| 381 | unsigned long long sk; |
| 382 | int rto, ato, qack, cwnd, ssthresh; |
| 383 | }; |
| 384 | |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 385 | static const char *tmr_name[] = { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 386 | "off", |
| 387 | "on", |
| 388 | "keepalive", |
| 389 | "timewait", |
| 390 | "persist", |
| 391 | "unknown" |
| 392 | }; |
| 393 | |
osdl.org!shemminger | 05e1811 | 2004-06-08 20:33:44 +0000 | [diff] [blame] | 394 | const char *print_ms_timer(int timeout) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 395 | { |
| 396 | static char buf[64]; |
| 397 | int secs, msecs, minutes; |
| 398 | if (timeout < 0) |
| 399 | timeout = 0; |
| 400 | secs = timeout/1000; |
| 401 | minutes = secs/60; |
| 402 | secs = secs%60; |
| 403 | msecs = timeout%1000; |
| 404 | buf[0] = 0; |
| 405 | if (minutes) { |
| 406 | msecs = 0; |
| 407 | snprintf(buf, sizeof(buf)-16, "%dmin", minutes); |
| 408 | if (minutes > 9) |
| 409 | secs = 0; |
| 410 | } |
| 411 | if (secs) { |
| 412 | if (secs > 9) |
| 413 | msecs = 0; |
| 414 | sprintf(buf+strlen(buf), "%d%s", secs, msecs ? "." : "sec"); |
| 415 | } |
| 416 | if (msecs) |
| 417 | sprintf(buf+strlen(buf), "%03dms", msecs); |
| 418 | return buf; |
Stephen Hemminger | e7113c6 | 2007-07-10 18:26:54 -0700 | [diff] [blame] | 419 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 420 | |
osdl.org!shemminger | 05e1811 | 2004-06-08 20:33:44 +0000 | [diff] [blame] | 421 | const char *print_hz_timer(int timeout) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 422 | { |
Stephen Hemminger | 2d44be1 | 2008-06-20 12:40:03 -0700 | [diff] [blame] | 423 | int hz = get_user_hz(); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 424 | return print_ms_timer(((timeout*1000) + hz-1)/hz); |
Stephen Hemminger | e7113c6 | 2007-07-10 18:26:54 -0700 | [diff] [blame] | 425 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 426 | |
| 427 | struct scache |
| 428 | { |
| 429 | struct scache *next; |
| 430 | int port; |
| 431 | char *name; |
| 432 | const char *proto; |
| 433 | }; |
| 434 | |
| 435 | struct scache *rlist; |
| 436 | |
| 437 | void init_service_resolver(void) |
| 438 | { |
| 439 | char buf[128]; |
| 440 | FILE *fp = popen("/usr/sbin/rpcinfo -p 2>/dev/null", "r"); |
| 441 | if (fp) { |
| 442 | fgets(buf, sizeof(buf), fp); |
| 443 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 444 | unsigned int progn, port; |
| 445 | char proto[128], prog[128]; |
| 446 | if (sscanf(buf, "%u %*d %s %u %s", &progn, proto, |
| 447 | &port, prog+4) == 4) { |
| 448 | struct scache *c = malloc(sizeof(*c)); |
| 449 | if (c) { |
| 450 | c->port = port; |
| 451 | memcpy(prog, "rpc.", 4); |
| 452 | c->name = strdup(prog); |
| 453 | if (strcmp(proto, TCP_PROTO) == 0) |
| 454 | c->proto = TCP_PROTO; |
| 455 | else if (strcmp(proto, UDP_PROTO) == 0) |
| 456 | c->proto = UDP_PROTO; |
| 457 | else |
| 458 | c->proto = NULL; |
| 459 | c->next = rlist; |
| 460 | rlist = c; |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 467 | static int ip_local_port_min, ip_local_port_max; |
| 468 | |
| 469 | /* Even do not try default linux ephemeral port ranges: |
| 470 | * default /etc/services contains so much of useless crap |
| 471 | * wouldbe "allocated" to this area that resolution |
| 472 | * is really harmful. I shrug each time when seeing |
| 473 | * "socks" or "cfinger" in dumps. |
| 474 | */ |
| 475 | static int is_ephemeral(int port) |
| 476 | { |
| 477 | if (!ip_local_port_min) { |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 478 | FILE *f = ephemeral_ports_open(); |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 479 | if (f) { |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 480 | fscanf(f, "%d %d", |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 481 | &ip_local_port_min, &ip_local_port_max); |
| 482 | fclose(f); |
| 483 | } else { |
| 484 | ip_local_port_min = 1024; |
| 485 | ip_local_port_max = 4999; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | return (port >= ip_local_port_min && port<= ip_local_port_max); |
| 490 | } |
| 491 | |
| 492 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 493 | const char *__resolve_service(int port) |
| 494 | { |
| 495 | struct scache *c; |
| 496 | |
| 497 | for (c = rlist; c; c = c->next) { |
| 498 | if (c->port == port && c->proto == dg_proto) |
| 499 | return c->name; |
| 500 | } |
| 501 | |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 502 | if (!is_ephemeral(port)) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 503 | static int notfirst; |
| 504 | struct servent *se; |
| 505 | if (!notfirst) { |
| 506 | setservent(1); |
| 507 | notfirst = 1; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 508 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 509 | se = getservbyport(htons(port), dg_proto); |
| 510 | if (se) |
| 511 | return se->s_name; |
| 512 | } |
| 513 | |
| 514 | return NULL; |
| 515 | } |
| 516 | |
| 517 | |
| 518 | const char *resolve_service(int port) |
| 519 | { |
| 520 | static char buf[128]; |
| 521 | static struct scache cache[256]; |
| 522 | |
| 523 | if (port == 0) { |
| 524 | buf[0] = '*'; |
| 525 | buf[1] = 0; |
| 526 | return buf; |
| 527 | } |
| 528 | |
| 529 | if (resolve_services) { |
| 530 | if (dg_proto == RAW_PROTO) { |
| 531 | return inet_proto_n2a(port, buf, sizeof(buf)); |
| 532 | } else { |
| 533 | struct scache *c; |
| 534 | const char *res; |
| 535 | int hash = (port^(((unsigned long)dg_proto)>>2))&255; |
| 536 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 537 | for (c = &cache[hash]; c; c = c->next) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 538 | if (c->port == port && |
| 539 | c->proto == dg_proto) { |
| 540 | if (c->name) |
| 541 | return c->name; |
| 542 | goto do_numeric; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | if ((res = __resolve_service(port)) != NULL) { |
| 547 | if ((c = malloc(sizeof(*c))) == NULL) |
| 548 | goto do_numeric; |
| 549 | } else { |
| 550 | c = &cache[hash]; |
| 551 | if (c->name) |
| 552 | free(c->name); |
| 553 | } |
| 554 | c->port = port; |
| 555 | c->name = NULL; |
| 556 | c->proto = dg_proto; |
| 557 | if (res) { |
| 558 | c->name = strdup(res); |
| 559 | c->next = cache[hash].next; |
| 560 | cache[hash].next = c; |
| 561 | } |
| 562 | if (c->name) |
| 563 | return c->name; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | do_numeric: |
| 568 | sprintf(buf, "%u", port); |
| 569 | return buf; |
| 570 | } |
| 571 | |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 572 | void formatted_print(const inet_prefix *a, int port) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 573 | { |
| 574 | char buf[1024]; |
| 575 | const char *ap = buf; |
| 576 | int est_len; |
| 577 | |
| 578 | est_len = addr_width; |
| 579 | |
| 580 | if (a->family == AF_INET) { |
| 581 | if (a->data[0] == 0) { |
| 582 | buf[0] = '*'; |
| 583 | buf[1] = 0; |
| 584 | } else { |
| 585 | ap = format_host(AF_INET, 4, a->data, buf, sizeof(buf)); |
| 586 | } |
| 587 | } else { |
| 588 | ap = format_host(a->family, 16, a->data, buf, sizeof(buf)); |
| 589 | est_len = strlen(ap); |
| 590 | if (est_len <= addr_width) |
| 591 | est_len = addr_width; |
| 592 | else |
| 593 | est_len = addr_width + ((est_len-addr_width+3)/4)*4; |
| 594 | } |
| 595 | printf("%*s:%-*s ", est_len, ap, serv_width, resolve_service(port)); |
| 596 | } |
| 597 | |
| 598 | struct aafilter |
| 599 | { |
| 600 | inet_prefix addr; |
| 601 | int port; |
| 602 | struct aafilter *next; |
| 603 | }; |
| 604 | |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 605 | int inet2_addr_match(const inet_prefix *a, const inet_prefix *p, int plen) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 606 | { |
| 607 | if (!inet_addr_match(a, p, plen)) |
| 608 | return 0; |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 609 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 610 | /* Cursed "v4 mapped" addresses: v4 mapped socket matches |
| 611 | * pure IPv4 rule, but v4-mapped rule selects only v4-mapped |
| 612 | * sockets. Fair? */ |
| 613 | if (p->family == AF_INET && a->family == AF_INET6) { |
| 614 | if (a->data[0] == 0 && a->data[1] == 0 && |
| 615 | a->data[2] == htonl(0xffff)) { |
| 616 | inet_prefix tmp = *a; |
| 617 | tmp.data[0] = a->data[3]; |
| 618 | return inet_addr_match(&tmp, p, plen); |
| 619 | } |
| 620 | } |
| 621 | return 1; |
| 622 | } |
| 623 | |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 624 | int unix_match(const inet_prefix *a, const inet_prefix *p) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 625 | { |
| 626 | char *addr, *pattern; |
| 627 | memcpy(&addr, a->data, sizeof(addr)); |
| 628 | memcpy(&pattern, p->data, sizeof(pattern)); |
| 629 | if (pattern == NULL) |
| 630 | return 1; |
| 631 | if (addr == NULL) |
| 632 | addr = ""; |
| 633 | return !fnmatch(pattern, addr, 0); |
| 634 | } |
| 635 | |
| 636 | int run_ssfilter(struct ssfilter *f, struct tcpstat *s) |
| 637 | { |
| 638 | switch (f->type) { |
| 639 | case SSF_S_AUTO: |
| 640 | { |
| 641 | static int low, high=65535; |
| 642 | |
| 643 | if (s->local.family == AF_UNIX) { |
| 644 | char *p; |
| 645 | memcpy(&p, s->local.data, sizeof(p)); |
| 646 | return p == NULL || (p[0] == '@' && strlen(p) == 6 && |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 647 | strspn(p+1, "0123456789abcdef") == 5); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 648 | } |
| 649 | if (s->local.family == AF_PACKET) |
| 650 | return s->lport == 0 && s->local.data == 0; |
| 651 | if (s->local.family == AF_NETLINK) |
| 652 | return s->lport < 0; |
| 653 | |
| 654 | if (!low) { |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 655 | FILE *fp = ephemeral_ports_open(); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 656 | if (fp) { |
| 657 | fscanf(fp, "%d%d", &low, &high); |
| 658 | fclose(fp); |
| 659 | } |
| 660 | } |
| 661 | return s->lport >= low && s->lport <= high; |
| 662 | } |
| 663 | case SSF_DCOND: |
| 664 | { |
| 665 | struct aafilter *a = (void*)f->pred; |
| 666 | if (a->addr.family == AF_UNIX) |
| 667 | return unix_match(&s->remote, &a->addr); |
| 668 | if (a->port != -1 && a->port != s->rport) |
| 669 | return 0; |
| 670 | if (a->addr.bitlen) { |
| 671 | do { |
| 672 | if (!inet2_addr_match(&s->remote, &a->addr, a->addr.bitlen)) |
| 673 | return 1; |
| 674 | } while ((a = a->next) != NULL); |
| 675 | return 0; |
| 676 | } |
| 677 | return 1; |
| 678 | } |
| 679 | case SSF_SCOND: |
| 680 | { |
| 681 | struct aafilter *a = (void*)f->pred; |
| 682 | if (a->addr.family == AF_UNIX) |
| 683 | return unix_match(&s->local, &a->addr); |
| 684 | if (a->port != -1 && a->port != s->lport) |
| 685 | return 0; |
| 686 | if (a->addr.bitlen) { |
| 687 | do { |
| 688 | if (!inet2_addr_match(&s->local, &a->addr, a->addr.bitlen)) |
| 689 | return 1; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 690 | } while ((a = a->next) != NULL); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 691 | return 0; |
| 692 | } |
| 693 | return 1; |
| 694 | } |
| 695 | case SSF_D_GE: |
| 696 | { |
| 697 | struct aafilter *a = (void*)f->pred; |
| 698 | return s->rport >= a->port; |
| 699 | } |
| 700 | case SSF_D_LE: |
| 701 | { |
| 702 | struct aafilter *a = (void*)f->pred; |
| 703 | return s->rport <= a->port; |
| 704 | } |
| 705 | case SSF_S_GE: |
| 706 | { |
| 707 | struct aafilter *a = (void*)f->pred; |
| 708 | return s->lport >= a->port; |
| 709 | } |
| 710 | case SSF_S_LE: |
| 711 | { |
| 712 | struct aafilter *a = (void*)f->pred; |
| 713 | return s->lport <= a->port; |
| 714 | } |
| 715 | |
| 716 | /* Yup. It is recursion. Sorry. */ |
| 717 | case SSF_AND: |
| 718 | return run_ssfilter(f->pred, s) && run_ssfilter(f->post, s); |
| 719 | case SSF_OR: |
| 720 | return run_ssfilter(f->pred, s) || run_ssfilter(f->post, s); |
| 721 | case SSF_NOT: |
| 722 | return !run_ssfilter(f->pred, s); |
| 723 | default: |
| 724 | abort(); |
| 725 | } |
| 726 | } |
| 727 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 728 | /* Relocate external jumps by reloc. */ |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 729 | static void ssfilter_patch(char *a, int len, int reloc) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 730 | { |
| 731 | while (len > 0) { |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 732 | struct inet_diag_bc_op *op = (struct inet_diag_bc_op*)a; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 733 | if (op->no == len+4) |
| 734 | op->no += reloc; |
| 735 | len -= op->yes; |
| 736 | a += op->yes; |
| 737 | } |
| 738 | if (len < 0) |
| 739 | abort(); |
| 740 | } |
| 741 | |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 742 | static int ssfilter_bytecompile(struct ssfilter *f, char **bytecode) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 743 | { |
| 744 | switch (f->type) { |
| 745 | case SSF_S_AUTO: |
| 746 | { |
| 747 | if (!(*bytecode=malloc(4))) abort(); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 748 | ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_AUTO, 4, 8 }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 749 | return 8; |
| 750 | } |
| 751 | case SSF_DCOND: |
| 752 | case SSF_SCOND: |
| 753 | { |
| 754 | struct aafilter *a = (void*)f->pred; |
| 755 | struct aafilter *b; |
| 756 | char *ptr; |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 757 | int code = (f->type == SSF_DCOND ? INET_DIAG_BC_D_COND : INET_DIAG_BC_S_COND); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 758 | int len = 0; |
| 759 | |
| 760 | for (b=a; b; b=b->next) { |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 761 | len += 4 + sizeof(struct inet_diag_hostcond); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 762 | if (a->addr.family == AF_INET6) |
| 763 | len += 16; |
| 764 | else |
| 765 | len += 4; |
| 766 | if (b->next) |
| 767 | len += 4; |
| 768 | } |
| 769 | if (!(ptr = malloc(len))) abort(); |
| 770 | *bytecode = ptr; |
| 771 | for (b=a; b; b=b->next) { |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 772 | struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)ptr; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 773 | int alen = (a->addr.family == AF_INET6 ? 16 : 4); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 774 | int oplen = alen + 4 + sizeof(struct inet_diag_hostcond); |
| 775 | struct inet_diag_hostcond *cond = (struct inet_diag_hostcond*)(ptr+4); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 776 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 777 | *op = (struct inet_diag_bc_op){ code, oplen, oplen+4 }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 778 | cond->family = a->addr.family; |
| 779 | cond->port = a->port; |
| 780 | cond->prefix_len = a->addr.bitlen; |
| 781 | memcpy(cond->addr, a->addr.data, alen); |
| 782 | ptr += oplen; |
| 783 | if (b->next) { |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 784 | op = (struct inet_diag_bc_op *)ptr; |
| 785 | *op = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, len - (ptr-*bytecode)}; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 786 | ptr += 4; |
| 787 | } |
| 788 | } |
| 789 | return ptr - *bytecode; |
| 790 | } |
| 791 | case SSF_D_GE: |
| 792 | { |
| 793 | struct aafilter *x = (void*)f->pred; |
| 794 | if (!(*bytecode=malloc(8))) abort(); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 795 | ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_GE, 8, 12 }; |
| 796 | ((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 797 | return 8; |
| 798 | } |
| 799 | case SSF_D_LE: |
| 800 | { |
| 801 | struct aafilter *x = (void*)f->pred; |
| 802 | if (!(*bytecode=malloc(8))) abort(); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 803 | ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_LE, 8, 12 }; |
| 804 | ((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 805 | return 8; |
| 806 | } |
| 807 | case SSF_S_GE: |
| 808 | { |
| 809 | struct aafilter *x = (void*)f->pred; |
| 810 | if (!(*bytecode=malloc(8))) abort(); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 811 | ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_GE, 8, 12 }; |
| 812 | ((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 813 | return 8; |
| 814 | } |
| 815 | case SSF_S_LE: |
| 816 | { |
| 817 | struct aafilter *x = (void*)f->pred; |
| 818 | if (!(*bytecode=malloc(8))) abort(); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 819 | ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_LE, 8, 12 }; |
| 820 | ((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 821 | return 8; |
| 822 | } |
| 823 | |
| 824 | case SSF_AND: |
| 825 | { |
| 826 | char *a1, *a2, *a, l1, l2; |
| 827 | l1 = ssfilter_bytecompile(f->pred, &a1); |
| 828 | l2 = ssfilter_bytecompile(f->post, &a2); |
| 829 | if (!(a = malloc(l1+l2))) abort(); |
| 830 | memcpy(a, a1, l1); |
| 831 | memcpy(a+l1, a2, l2); |
| 832 | free(a1); free(a2); |
| 833 | ssfilter_patch(a, l1, l2); |
| 834 | *bytecode = a; |
| 835 | return l1+l2; |
| 836 | } |
| 837 | case SSF_OR: |
| 838 | { |
| 839 | char *a1, *a2, *a, l1, l2; |
| 840 | l1 = ssfilter_bytecompile(f->pred, &a1); |
| 841 | l2 = ssfilter_bytecompile(f->post, &a2); |
| 842 | if (!(a = malloc(l1+l2+4))) abort(); |
| 843 | memcpy(a, a1, l1); |
| 844 | memcpy(a+l1+4, a2, l2); |
| 845 | free(a1); free(a2); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 846 | *(struct inet_diag_bc_op*)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, l2+4 }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 847 | *bytecode = a; |
| 848 | return l1+l2+4; |
| 849 | } |
| 850 | case SSF_NOT: |
| 851 | { |
| 852 | char *a1, *a, l1; |
| 853 | l1 = ssfilter_bytecompile(f->pred, &a1); |
| 854 | if (!(a = malloc(l1+4))) abort(); |
| 855 | memcpy(a, a1, l1); |
| 856 | free(a1); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 857 | *(struct inet_diag_bc_op*)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, 8 }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 858 | *bytecode = a; |
| 859 | return l1+4; |
| 860 | } |
| 861 | default: |
| 862 | abort(); |
| 863 | } |
| 864 | } |
| 865 | |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 866 | static int remember_he(struct aafilter *a, struct hostent *he) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 867 | { |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 868 | char **ptr = he->h_addr_list; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 869 | int cnt = 0; |
| 870 | int len; |
| 871 | |
| 872 | if (he->h_addrtype == AF_INET) |
| 873 | len = 4; |
| 874 | else if (he->h_addrtype == AF_INET6) |
| 875 | len = 16; |
| 876 | else |
| 877 | return 0; |
| 878 | |
| 879 | while (*ptr) { |
| 880 | struct aafilter *b = a; |
| 881 | if (a->addr.bitlen) { |
| 882 | if ((b = malloc(sizeof(*b))) == NULL) |
| 883 | return cnt; |
| 884 | *b = *a; |
| 885 | b->next = a->next; |
| 886 | a->next = b; |
| 887 | } |
| 888 | memcpy(b->addr.data, *ptr, len); |
| 889 | b->addr.bytelen = len; |
| 890 | b->addr.bitlen = len*8; |
| 891 | b->addr.family = he->h_addrtype; |
| 892 | ptr++; |
| 893 | cnt++; |
| 894 | } |
| 895 | return cnt; |
| 896 | } |
| 897 | |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 898 | static int get_dns_host(struct aafilter *a, const char *addr, int fam) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 899 | { |
| 900 | static int notfirst; |
| 901 | int cnt = 0; |
| 902 | struct hostent *he; |
| 903 | |
| 904 | a->addr.bitlen = 0; |
| 905 | if (!notfirst) { |
| 906 | sethostent(1); |
| 907 | notfirst = 1; |
| 908 | } |
| 909 | he = gethostbyname2(addr, fam == AF_UNSPEC ? AF_INET : fam); |
| 910 | if (he) |
| 911 | cnt = remember_he(a, he); |
| 912 | if (fam == AF_UNSPEC) { |
| 913 | he = gethostbyname2(addr, AF_INET6); |
| 914 | if (he) |
| 915 | cnt += remember_he(a, he); |
| 916 | } |
| 917 | return !cnt; |
| 918 | } |
| 919 | |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 920 | static int xll_initted = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 921 | |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 922 | static void xll_init(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 923 | { |
| 924 | struct rtnl_handle rth; |
| 925 | rtnl_open(&rth, 0); |
| 926 | ll_init_map(&rth); |
| 927 | rtnl_close(&rth); |
| 928 | xll_initted = 1; |
| 929 | } |
| 930 | |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 931 | static const char *xll_index_to_name(int index) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 932 | { |
| 933 | if (!xll_initted) |
| 934 | xll_init(); |
| 935 | return ll_index_to_name(index); |
| 936 | } |
| 937 | |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 938 | static int xll_name_to_index(const char *dev) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 939 | { |
| 940 | if (!xll_initted) |
| 941 | xll_init(); |
| 942 | return ll_name_to_index(dev); |
| 943 | } |
| 944 | |
| 945 | void *parse_hostcond(char *addr) |
| 946 | { |
| 947 | char *port = NULL; |
| 948 | struct aafilter a; |
| 949 | struct aafilter *res; |
| 950 | int fam = preferred_family; |
| 951 | |
| 952 | memset(&a, 0, sizeof(a)); |
| 953 | a.port = -1; |
| 954 | |
| 955 | if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) { |
| 956 | char *p; |
| 957 | a.addr.family = AF_UNIX; |
| 958 | if (strncmp(addr, "unix:", 5) == 0) |
| 959 | addr+=5; |
| 960 | p = strdup(addr); |
| 961 | a.addr.bitlen = 8*strlen(p); |
| 962 | memcpy(a.addr.data, &p, sizeof(p)); |
| 963 | goto out; |
| 964 | } |
| 965 | |
| 966 | if (fam == AF_PACKET || strncmp(addr, "link:", 5) == 0) { |
| 967 | a.addr.family = AF_PACKET; |
| 968 | a.addr.bitlen = 0; |
| 969 | if (strncmp(addr, "link:", 5) == 0) |
| 970 | addr+=5; |
| 971 | port = strchr(addr, ':'); |
| 972 | if (port) { |
| 973 | *port = 0; |
| 974 | if (port[1] && strcmp(port+1, "*")) { |
| 975 | if (get_integer(&a.port, port+1, 0)) { |
| 976 | if ((a.port = xll_name_to_index(port+1)) <= 0) |
| 977 | return NULL; |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | if (addr[0] && strcmp(addr, "*")) { |
| 982 | unsigned short tmp; |
| 983 | a.addr.bitlen = 32; |
| 984 | if (ll_proto_a2n(&tmp, addr)) |
| 985 | return NULL; |
| 986 | a.addr.data[0] = ntohs(tmp); |
| 987 | } |
| 988 | goto out; |
| 989 | } |
| 990 | |
| 991 | if (fam == AF_NETLINK || strncmp(addr, "netlink:", 8) == 0) { |
| 992 | a.addr.family = AF_NETLINK; |
| 993 | a.addr.bitlen = 0; |
| 994 | if (strncmp(addr, "netlink:", 8) == 0) |
| 995 | addr+=8; |
| 996 | port = strchr(addr, ':'); |
| 997 | if (port) { |
| 998 | *port = 0; |
| 999 | if (port[1] && strcmp(port+1, "*")) { |
| 1000 | if (get_integer(&a.port, port+1, 0)) { |
| 1001 | if (strcmp(port+1, "kernel") == 0) |
| 1002 | a.port = 0; |
| 1003 | else |
| 1004 | return NULL; |
| 1005 | } |
| 1006 | } |
| 1007 | } |
| 1008 | if (addr[0] && strcmp(addr, "*")) { |
| 1009 | a.addr.bitlen = 32; |
| 1010 | if (get_u32(a.addr.data, addr, 0)) { |
| 1011 | if (strcmp(addr, "rtnl") == 0) |
| 1012 | a.addr.data[0] = 0; |
| 1013 | else if (strcmp(addr, "fw") == 0) |
| 1014 | a.addr.data[0] = 3; |
| 1015 | else if (strcmp(addr, "tcpdiag") == 0) |
| 1016 | a.addr.data[0] = 4; |
| 1017 | else |
| 1018 | return NULL; |
| 1019 | } |
| 1020 | } |
| 1021 | goto out; |
| 1022 | } |
| 1023 | |
| 1024 | if (strncmp(addr, "inet:", 5) == 0) { |
| 1025 | addr += 5; |
| 1026 | fam = AF_INET; |
| 1027 | } else if (strncmp(addr, "inet6:", 6) == 0) { |
| 1028 | addr += 6; |
| 1029 | fam = AF_INET6; |
| 1030 | } |
| 1031 | |
| 1032 | /* URL-like literal [] */ |
| 1033 | if (addr[0] == '[') { |
| 1034 | addr++; |
| 1035 | if ((port = strchr(addr, ']')) == NULL) |
| 1036 | return NULL; |
| 1037 | *port++ = 0; |
| 1038 | } else if (addr[0] == '*') { |
| 1039 | port = addr+1; |
| 1040 | } else { |
| 1041 | port = strrchr(strchr(addr, '/') ? : addr, ':'); |
| 1042 | } |
| 1043 | if (port && *port) { |
| 1044 | if (*port != ':') |
| 1045 | return NULL; |
| 1046 | *port++ = 0; |
| 1047 | if (*port && *port != '*') { |
| 1048 | if (get_integer(&a.port, port, 0)) { |
| 1049 | struct servent *se1 = NULL; |
| 1050 | struct servent *se2 = NULL; |
| 1051 | if (current_filter.dbs&(1<<UDP_DB)) |
| 1052 | se1 = getservbyname(port, UDP_PROTO); |
| 1053 | if (current_filter.dbs&(1<<TCP_DB)) |
| 1054 | se2 = getservbyname(port, TCP_PROTO); |
| 1055 | if (se1 && se2 && se1->s_port != se2->s_port) { |
| 1056 | fprintf(stderr, "Error: ambiguous port \"%s\".\n", port); |
| 1057 | return NULL; |
| 1058 | } |
| 1059 | if (!se1) |
| 1060 | se1 = se2; |
| 1061 | if (se1) { |
| 1062 | a.port = ntohs(se1->s_port); |
| 1063 | } else { |
| 1064 | struct scache *s; |
| 1065 | for (s = rlist; s; s = s->next) { |
| 1066 | if ((s->proto == UDP_PROTO && |
| 1067 | (current_filter.dbs&(1<<UDP_DB))) || |
| 1068 | (s->proto == TCP_PROTO && |
| 1069 | (current_filter.dbs&(1<<TCP_DB)))) { |
| 1070 | if (s->name && strcmp(s->name, port) == 0) { |
| 1071 | if (a.port > 0 && a.port != s->port) { |
| 1072 | fprintf(stderr, "Error: ambiguous port \"%s\".\n", port); |
| 1073 | return NULL; |
| 1074 | } |
| 1075 | a.port = s->port; |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | if (a.port <= 0) { |
| 1080 | fprintf(stderr, "Error: \"%s\" does not look like a port.\n", port); |
| 1081 | return NULL; |
| 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | if (addr && *addr && *addr != '*') { |
| 1088 | if (get_prefix_1(&a.addr, addr, fam)) { |
| 1089 | if (get_dns_host(&a, addr, fam)) { |
| 1090 | fprintf(stderr, "Error: an inet prefix is expected rather than \"%s\".\n", addr); |
| 1091 | return NULL; |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | out: |
| 1097 | res = malloc(sizeof(*res)); |
| 1098 | if (res) |
| 1099 | memcpy(res, &a, sizeof(a)); |
| 1100 | return res; |
| 1101 | } |
| 1102 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1103 | static int tcp_show_line(char *line, const struct filter *f, int family) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1104 | { |
| 1105 | struct tcpstat s; |
| 1106 | char *loc, *rem, *data; |
| 1107 | char opt[256]; |
| 1108 | int n; |
| 1109 | char *p; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1110 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1111 | if ((p = strchr(line, ':')) == NULL) |
| 1112 | return -1; |
| 1113 | loc = p+2; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1114 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1115 | if ((p = strchr(loc, ':')) == NULL) |
| 1116 | return -1; |
| 1117 | p[5] = 0; |
| 1118 | rem = p+6; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1119 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1120 | if ((p = strchr(rem, ':')) == NULL) |
| 1121 | return -1; |
| 1122 | p[5] = 0; |
| 1123 | data = p+6; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1124 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1125 | do { |
| 1126 | int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0'); |
| 1127 | |
| 1128 | if (!(f->states & (1<<state))) |
| 1129 | return 0; |
| 1130 | } while (0); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1131 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1132 | s.local.family = s.remote.family = family; |
| 1133 | if (family == AF_INET) { |
| 1134 | sscanf(loc, "%x:%x", s.local.data, (unsigned*)&s.lport); |
| 1135 | sscanf(rem, "%x:%x", s.remote.data, (unsigned*)&s.rport); |
| 1136 | s.local.bytelen = s.remote.bytelen = 4; |
| 1137 | } else { |
| 1138 | sscanf(loc, "%08x%08x%08x%08x:%x", |
| 1139 | s.local.data, |
| 1140 | s.local.data+1, |
| 1141 | s.local.data+2, |
| 1142 | s.local.data+3, |
| 1143 | &s.lport); |
| 1144 | sscanf(rem, "%08x%08x%08x%08x:%x", |
| 1145 | s.remote.data, |
| 1146 | s.remote.data+1, |
| 1147 | s.remote.data+2, |
| 1148 | s.remote.data+3, |
| 1149 | &s.rport); |
| 1150 | s.local.bytelen = s.remote.bytelen = 16; |
| 1151 | } |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1152 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1153 | if (f->f && run_ssfilter(f->f, &s) == 0) |
| 1154 | return 0; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1155 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1156 | opt[0] = 0; |
Stephen Hemminger | e7113c6 | 2007-07-10 18:26:54 -0700 | [diff] [blame] | 1157 | n = sscanf(data, "%x %x:%x %x:%x %x %d %d %u %d %llx %d %d %d %d %d %[^\n]\n", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1158 | &s.state, &s.wq, &s.rq, |
| 1159 | &s.timer, &s.timeout, &s.retrs, &s.uid, &s.probes, &s.ino, |
| 1160 | &s.refcnt, &s.sk, &s.rto, &s.ato, &s.qack, |
| 1161 | &s.cwnd, &s.ssthresh, opt); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1162 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1163 | if (n < 17) |
| 1164 | opt[0] = 0; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1165 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1166 | if (n < 12) { |
| 1167 | s.rto = 0; |
| 1168 | s.cwnd = 2; |
| 1169 | s.ssthresh = -1; |
| 1170 | s.ato = s.qack = 0; |
| 1171 | } |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1172 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1173 | if (netid_width) |
| 1174 | printf("%-*s ", netid_width, "tcp"); |
| 1175 | if (state_width) |
| 1176 | printf("%-*s ", state_width, sstate_name[s.state]); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1177 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1178 | printf("%-6d %-6d ", s.rq, s.wq); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1179 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1180 | formatted_print(&s.local, s.lport); |
| 1181 | formatted_print(&s.remote, s.rport); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1182 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1183 | if (show_options) { |
| 1184 | if (s.timer) { |
| 1185 | if (s.timer > 4) |
| 1186 | s.timer = 5; |
| 1187 | printf(" timer:(%s,%s,%d)", |
| 1188 | tmr_name[s.timer], |
| 1189 | print_hz_timer(s.timeout), |
| 1190 | s.timer != 1 ? s.probes : s.retrs); |
| 1191 | } |
| 1192 | } |
| 1193 | if (show_tcpinfo) { |
Stephen Hemminger | 4c1db13 | 2008-06-20 12:34:15 -0700 | [diff] [blame] | 1194 | int hz = get_user_hz(); |
| 1195 | if (s.rto && s.rto != 3*hz) |
| 1196 | printf(" rto:%g", (double)s.rto/hz); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1197 | if (s.ato) |
Stephen Hemminger | 4c1db13 | 2008-06-20 12:34:15 -0700 | [diff] [blame] | 1198 | printf(" ato:%g", (double)s.ato/hz); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1199 | if (s.cwnd != 2) |
| 1200 | printf(" cwnd:%d", s.cwnd); |
| 1201 | if (s.ssthresh != -1) |
| 1202 | printf(" ssthresh:%d", s.ssthresh); |
| 1203 | if (s.qack/2) |
| 1204 | printf(" qack:%d", s.qack/2); |
| 1205 | if (s.qack&1) |
| 1206 | printf(" bidir"); |
| 1207 | } |
| 1208 | if (show_users) { |
| 1209 | char ubuf[4096]; |
| 1210 | if (find_users(s.ino, ubuf, sizeof(ubuf)) > 0) |
| 1211 | printf(" users:(%s)", ubuf); |
| 1212 | } |
| 1213 | if (show_details) { |
| 1214 | if (s.uid) |
| 1215 | printf(" uid:%u", (unsigned)s.uid); |
Stephen Hemminger | e7113c6 | 2007-07-10 18:26:54 -0700 | [diff] [blame] | 1216 | printf(" ino:%u", s.ino); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1217 | printf(" sk:%llx", s.sk); |
| 1218 | if (opt[0]) |
| 1219 | printf(" opt:\"%s\"", opt); |
| 1220 | } |
| 1221 | printf("\n"); |
| 1222 | |
| 1223 | return 0; |
| 1224 | } |
| 1225 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1226 | static int generic_record_read(FILE *fp, |
| 1227 | int (*worker)(char*, const struct filter *, int), |
| 1228 | const struct filter *f, int fam) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1229 | { |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1230 | char line[256]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1231 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1232 | /* skip header */ |
| 1233 | if (fgets(line, sizeof(line), fp) == NULL) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1234 | goto outerr; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1235 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1236 | while (fgets(line, sizeof(line), fp) != NULL) { |
| 1237 | int n = strlen(line); |
| 1238 | if (n == 0 || line[n-1] != '\n') { |
| 1239 | errno = -EINVAL; |
| 1240 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1241 | } |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1242 | line[n-1] = 0; |
| 1243 | |
| 1244 | if (worker(line, f, fam) < 0) |
| 1245 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1246 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1247 | outerr: |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1248 | |
| 1249 | return ferror(fp) ? -1 : 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1250 | } |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1251 | |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1252 | static char *sprint_bw(char *buf, double bw) |
| 1253 | { |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1254 | if (bw > 1000000.) |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1255 | sprintf(buf,"%.1fM", bw / 1000000.); |
| 1256 | else if (bw > 1000.) |
| 1257 | sprintf(buf,"%.1fK", bw / 1000.); |
| 1258 | else |
| 1259 | sprintf(buf, "%g", bw); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1260 | |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1261 | return buf; |
| 1262 | } |
| 1263 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1264 | static void tcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r) |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 1265 | { |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1266 | struct rtattr * tb[INET_DIAG_MAX+1]; |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1267 | char b1[64]; |
| 1268 | double rtt = 0; |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 1269 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1270 | parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr*)(r+1), |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 1271 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); |
| 1272 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1273 | if (tb[INET_DIAG_MEMINFO]) { |
| 1274 | const struct inet_diag_meminfo *minfo |
| 1275 | = RTA_DATA(tb[INET_DIAG_MEMINFO]); |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 1276 | printf(" mem:(r%u,w%u,f%u,t%u)", |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1277 | minfo->idiag_rmem, |
| 1278 | minfo->idiag_wmem, |
| 1279 | minfo->idiag_fmem, |
| 1280 | minfo->idiag_tmem); |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1283 | if (tb[INET_DIAG_INFO]) { |
osdl.org!shemminger | 05e1811 | 2004-06-08 20:33:44 +0000 | [diff] [blame] | 1284 | struct tcp_info *info; |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1285 | int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]); |
osdl.org!shemminger | 05e1811 | 2004-06-08 20:33:44 +0000 | [diff] [blame] | 1286 | |
| 1287 | /* workaround for older kernels with less fields */ |
| 1288 | if (len < sizeof(*info)) { |
| 1289 | info = alloca(sizeof(*info)); |
| 1290 | memset(info, 0, sizeof(*info)); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1291 | memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len); |
osdl.org!shemminger | 05e1811 | 2004-06-08 20:33:44 +0000 | [diff] [blame] | 1292 | } else |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1293 | info = RTA_DATA(tb[INET_DIAG_INFO]); |
osdl.org!shemminger | 05e1811 | 2004-06-08 20:33:44 +0000 | [diff] [blame] | 1294 | |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1295 | if (show_options) { |
| 1296 | if (info->tcpi_options & TCPI_OPT_TIMESTAMPS) |
| 1297 | printf(" ts"); |
| 1298 | if (info->tcpi_options & TCPI_OPT_SACK) |
| 1299 | printf(" sack"); |
| 1300 | if (info->tcpi_options & TCPI_OPT_ECN) |
| 1301 | printf(" ecn"); |
| 1302 | } |
shemminger | 52d5ac3 | 2005-07-05 22:11:37 +0000 | [diff] [blame] | 1303 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1304 | if (tb[INET_DIAG_CONG]) |
| 1305 | printf("%s", (char *) RTA_DATA(tb[INET_DIAG_CONG])); |
shemminger | ea8fc10 | 2005-06-22 18:27:49 +0000 | [diff] [blame] | 1306 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1307 | if (info->tcpi_options & TCPI_OPT_WSCALE) |
osdl.org!shemminger | 05e1811 | 2004-06-08 20:33:44 +0000 | [diff] [blame] | 1308 | printf(" wscale:%d,%d", info->tcpi_snd_wscale, |
| 1309 | info->tcpi_rcv_wscale); |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 1310 | if (info->tcpi_rto && info->tcpi_rto != 3000000) |
| 1311 | printf(" rto:%g", (double)info->tcpi_rto/1000); |
| 1312 | if (info->tcpi_rtt) |
| 1313 | printf(" rtt:%g/%g", (double)info->tcpi_rtt/1000, |
| 1314 | (double)info->tcpi_rttvar/1000); |
| 1315 | if (info->tcpi_ato) |
| 1316 | printf(" ato:%g", (double)info->tcpi_ato/1000); |
| 1317 | if (info->tcpi_snd_cwnd != 2) |
| 1318 | printf(" cwnd:%d", info->tcpi_snd_cwnd); |
| 1319 | if (info->tcpi_snd_ssthresh < 0xFFFF) |
| 1320 | printf(" ssthresh:%d", info->tcpi_snd_ssthresh); |
shemminger | 52d5ac3 | 2005-07-05 22:11:37 +0000 | [diff] [blame] | 1321 | |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1322 | rtt = (double) info->tcpi_rtt; |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1323 | if (tb[INET_DIAG_VEGASINFO]) { |
osdl.org!shemminger | 05e1811 | 2004-06-08 20:33:44 +0000 | [diff] [blame] | 1324 | const struct tcpvegas_info *vinfo |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1325 | = RTA_DATA(tb[INET_DIAG_VEGASINFO]); |
osdl.org!shemminger | 05e1811 | 2004-06-08 20:33:44 +0000 | [diff] [blame] | 1326 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1327 | if (vinfo->tcpv_enabled && |
shemminger | ea8fc10 | 2005-06-22 18:27:49 +0000 | [diff] [blame] | 1328 | vinfo->tcpv_rtt && vinfo->tcpv_rtt != 0x7fffffff) |
| 1329 | rtt = vinfo->tcpv_rtt; |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 1330 | } |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1331 | |
| 1332 | if (rtt > 0 && info->tcpi_snd_mss && info->tcpi_snd_cwnd) { |
| 1333 | printf(" send %sbps", |
| 1334 | sprint_bw(b1, (double) info->tcpi_snd_cwnd * |
| 1335 | (double) info->tcpi_snd_mss * 8000000. |
| 1336 | / rtt)); |
| 1337 | } |
| 1338 | |
| 1339 | if (info->tcpi_rcv_rtt) |
| 1340 | printf(" rcv_rtt:%g", (double) info->tcpi_rcv_rtt/1000); |
| 1341 | if (info->tcpi_rcv_space) |
| 1342 | printf(" rcv_space:%d", info->tcpi_rcv_space); |
| 1343 | |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 1344 | } |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 1345 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1346 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1347 | static int tcp_show_sock(struct nlmsghdr *nlh, struct filter *f) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1348 | { |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1349 | struct inet_diag_msg *r = NLMSG_DATA(nlh); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1350 | struct tcpstat s; |
| 1351 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1352 | s.state = r->idiag_state; |
| 1353 | s.local.family = s.remote.family = r->idiag_family; |
| 1354 | s.lport = ntohs(r->id.idiag_sport); |
| 1355 | s.rport = ntohs(r->id.idiag_dport); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1356 | if (s.local.family == AF_INET) { |
| 1357 | s.local.bytelen = s.remote.bytelen = 4; |
| 1358 | } else { |
| 1359 | s.local.bytelen = s.remote.bytelen = 16; |
| 1360 | } |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1361 | memcpy(s.local.data, r->id.idiag_src, s.local.bytelen); |
| 1362 | memcpy(s.remote.data, r->id.idiag_dst, s.local.bytelen); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1363 | |
| 1364 | if (f && f->f && run_ssfilter(f->f, &s) == 0) |
| 1365 | return 0; |
| 1366 | |
| 1367 | if (netid_width) |
| 1368 | printf("%-*s ", netid_width, "tcp"); |
| 1369 | if (state_width) |
| 1370 | printf("%-*s ", state_width, sstate_name[s.state]); |
| 1371 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1372 | printf("%-6d %-6d ", r->idiag_rqueue, r->idiag_wqueue); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1373 | |
| 1374 | formatted_print(&s.local, s.lport); |
| 1375 | formatted_print(&s.remote, s.rport); |
| 1376 | |
| 1377 | if (show_options) { |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1378 | if (r->idiag_timer) { |
| 1379 | if (r->idiag_timer > 4) |
| 1380 | r->idiag_timer = 5; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1381 | printf(" timer:(%s,%s,%d)", |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1382 | tmr_name[r->idiag_timer], |
| 1383 | print_ms_timer(r->idiag_expires), |
| 1384 | r->idiag_retrans); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1385 | } |
| 1386 | } |
| 1387 | if (show_users) { |
| 1388 | char ubuf[4096]; |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1389 | if (find_users(r->idiag_inode, ubuf, sizeof(ubuf)) > 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1390 | printf(" users:(%s)", ubuf); |
| 1391 | } |
| 1392 | if (show_details) { |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1393 | if (r->idiag_uid) |
| 1394 | printf(" uid:%u", (unsigned)r->idiag_uid); |
Stephen Hemminger | e7113c6 | 2007-07-10 18:26:54 -0700 | [diff] [blame] | 1395 | printf(" ino:%u", r->idiag_inode); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1396 | printf(" sk:%08x", r->id.idiag_cookie[0]); |
| 1397 | if (r->id.idiag_cookie[1] != 0) |
| 1398 | printf("%08x", r->id.idiag_cookie[1]); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1399 | } |
| 1400 | if (show_mem || show_tcpinfo) { |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 1401 | printf("\n\t"); |
| 1402 | tcp_show_info(nlh, r); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1403 | } |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 1404 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1405 | printf("\n"); |
| 1406 | |
| 1407 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1410 | static int tcp_show_netlink(struct filter *f, FILE *dump_fp, int socktype) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1411 | { |
| 1412 | int fd; |
| 1413 | struct sockaddr_nl nladdr; |
| 1414 | struct { |
| 1415 | struct nlmsghdr nlh; |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1416 | struct inet_diag_req r; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1417 | } req; |
| 1418 | char *bc = NULL; |
| 1419 | int bclen; |
| 1420 | struct msghdr msg; |
| 1421 | struct rtattr rta; |
| 1422 | char buf[8192]; |
| 1423 | struct iovec iov[3]; |
| 1424 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1425 | if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG)) < 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1426 | return -1; |
| 1427 | |
| 1428 | memset(&nladdr, 0, sizeof(nladdr)); |
| 1429 | nladdr.nl_family = AF_NETLINK; |
| 1430 | |
| 1431 | req.nlh.nlmsg_len = sizeof(req); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1432 | req.nlh.nlmsg_type = socktype; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1433 | req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST; |
| 1434 | req.nlh.nlmsg_pid = 0; |
| 1435 | req.nlh.nlmsg_seq = 123456; |
| 1436 | memset(&req.r, 0, sizeof(req.r)); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1437 | req.r.idiag_family = AF_INET; |
| 1438 | req.r.idiag_states = f->states; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1439 | if (show_mem) |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1440 | req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1)); |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1441 | |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 1442 | if (show_tcpinfo) { |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1443 | req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1)); |
| 1444 | req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1)); |
| 1445 | req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1)); |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 1446 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1447 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1448 | iov[0] = (struct iovec){ |
| 1449 | .iov_base = &req, |
| 1450 | .iov_len = sizeof(req) |
shemminger | ea8fc10 | 2005-06-22 18:27:49 +0000 | [diff] [blame] | 1451 | }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1452 | if (f->f) { |
| 1453 | bclen = ssfilter_bytecompile(f->f, &bc); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1454 | rta.rta_type = INET_DIAG_REQ_BYTECODE; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1455 | rta.rta_len = RTA_LENGTH(bclen); |
| 1456 | iov[1] = (struct iovec){ &rta, sizeof(rta) }; |
| 1457 | iov[2] = (struct iovec){ bc, bclen }; |
| 1458 | req.nlh.nlmsg_len += RTA_LENGTH(bclen); |
| 1459 | } |
| 1460 | |
| 1461 | msg = (struct msghdr) { |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1462 | .msg_name = (void*)&nladdr, |
shemminger | ea8fc10 | 2005-06-22 18:27:49 +0000 | [diff] [blame] | 1463 | .msg_namelen = sizeof(nladdr), |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1464 | .msg_iov = iov, |
shemminger | ea8fc10 | 2005-06-22 18:27:49 +0000 | [diff] [blame] | 1465 | .msg_iovlen = f->f ? 3 : 1, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1466 | }; |
| 1467 | |
| 1468 | if (sendmsg(fd, &msg, 0) < 0) |
| 1469 | return -1; |
| 1470 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1471 | iov[0] = (struct iovec){ |
| 1472 | .iov_base = buf, |
| 1473 | .iov_len = sizeof(buf) |
shemminger | ea8fc10 | 2005-06-22 18:27:49 +0000 | [diff] [blame] | 1474 | }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1475 | |
| 1476 | while (1) { |
| 1477 | int status; |
| 1478 | struct nlmsghdr *h; |
| 1479 | |
| 1480 | msg = (struct msghdr) { |
| 1481 | (void*)&nladdr, sizeof(nladdr), |
| 1482 | iov, 1, |
| 1483 | NULL, 0, |
| 1484 | 0 |
| 1485 | }; |
| 1486 | |
| 1487 | status = recvmsg(fd, &msg, 0); |
| 1488 | |
| 1489 | if (status < 0) { |
| 1490 | if (errno == EINTR) |
| 1491 | continue; |
| 1492 | perror("OVERRUN"); |
| 1493 | continue; |
| 1494 | } |
| 1495 | if (status == 0) { |
| 1496 | fprintf(stderr, "EOF on netlink\n"); |
| 1497 | return 0; |
| 1498 | } |
| 1499 | |
| 1500 | if (dump_fp) |
| 1501 | fwrite(buf, 1, NLMSG_ALIGN(status), dump_fp); |
| 1502 | |
| 1503 | h = (struct nlmsghdr*)buf; |
| 1504 | while (NLMSG_OK(h, status)) { |
| 1505 | int err; |
Li Yewang | a37b01c | 2008-09-09 09:13:42 +0800 | [diff] [blame] | 1506 | struct inet_diag_msg *r = NLMSG_DATA(h); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1507 | |
| 1508 | if (/*h->nlmsg_pid != rth->local.nl_pid ||*/ |
| 1509 | h->nlmsg_seq != 123456) |
| 1510 | goto skip_it; |
| 1511 | |
| 1512 | if (h->nlmsg_type == NLMSG_DONE) |
| 1513 | return 0; |
| 1514 | if (h->nlmsg_type == NLMSG_ERROR) { |
| 1515 | struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); |
| 1516 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { |
| 1517 | fprintf(stderr, "ERROR truncated\n"); |
| 1518 | } else { |
| 1519 | errno = -err->error; |
| 1520 | perror("TCPDIAG answers"); |
| 1521 | } |
| 1522 | return 0; |
| 1523 | } |
| 1524 | if (!dump_fp) { |
Li Yewang | a37b01c | 2008-09-09 09:13:42 +0800 | [diff] [blame] | 1525 | if (!(f->families & (1<<r->idiag_family))) { |
| 1526 | h = NLMSG_NEXT(h, status); |
| 1527 | continue; |
| 1528 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1529 | err = tcp_show_sock(h, NULL); |
| 1530 | if (err < 0) |
| 1531 | return err; |
| 1532 | } |
| 1533 | |
| 1534 | skip_it: |
| 1535 | h = NLMSG_NEXT(h, status); |
| 1536 | } |
| 1537 | if (msg.msg_flags & MSG_TRUNC) { |
| 1538 | fprintf(stderr, "Message truncated\n"); |
| 1539 | continue; |
| 1540 | } |
| 1541 | if (status) { |
| 1542 | fprintf(stderr, "!!!Remnant of size %d\n", status); |
| 1543 | exit(1); |
| 1544 | } |
| 1545 | } |
| 1546 | return 0; |
| 1547 | } |
| 1548 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1549 | static int tcp_show_netlink_file(struct filter *f) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1550 | { |
| 1551 | FILE *fp; |
| 1552 | char buf[8192]; |
| 1553 | |
| 1554 | if ((fp = fopen(getenv("TCPDIAG_FILE"), "r")) == NULL) { |
| 1555 | perror("fopen($TCPDIAG_FILE)"); |
| 1556 | return -1; |
| 1557 | } |
| 1558 | |
| 1559 | while (1) { |
| 1560 | int status, err; |
| 1561 | struct nlmsghdr *h = (struct nlmsghdr*)buf; |
| 1562 | |
| 1563 | status = fread(buf, 1, sizeof(*h), fp); |
| 1564 | if (status < 0) { |
| 1565 | perror("Reading header from $TCPDIAG_FILE"); |
| 1566 | return -1; |
| 1567 | } |
| 1568 | if (status != sizeof(*h)) { |
| 1569 | perror("Unexpected EOF reading $TCPDIAG_FILE"); |
| 1570 | return -1; |
| 1571 | } |
| 1572 | |
| 1573 | status = fread(h+1, 1, NLMSG_ALIGN(h->nlmsg_len-sizeof(*h)), fp); |
| 1574 | |
| 1575 | if (status < 0) { |
| 1576 | perror("Reading $TCPDIAG_FILE"); |
| 1577 | return -1; |
| 1578 | } |
| 1579 | if (status + sizeof(*h) < h->nlmsg_len) { |
| 1580 | perror("Unexpected EOF reading $TCPDIAG_FILE"); |
| 1581 | return -1; |
| 1582 | } |
| 1583 | |
| 1584 | /* The only legal exit point */ |
| 1585 | if (h->nlmsg_type == NLMSG_DONE) |
| 1586 | return 0; |
| 1587 | |
| 1588 | if (h->nlmsg_type == NLMSG_ERROR) { |
| 1589 | struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); |
| 1590 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { |
| 1591 | fprintf(stderr, "ERROR truncated\n"); |
| 1592 | } else { |
| 1593 | errno = -err->error; |
| 1594 | perror("TCPDIAG answered"); |
| 1595 | } |
| 1596 | return -1; |
| 1597 | } |
| 1598 | |
| 1599 | err = tcp_show_sock(h, f); |
| 1600 | if (err < 0) |
| 1601 | return err; |
| 1602 | } |
| 1603 | } |
| 1604 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1605 | static int tcp_show(struct filter *f, int socktype) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1606 | { |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1607 | FILE *fp = NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1608 | char *buf = NULL; |
| 1609 | int bufsize = 64*1024; |
| 1610 | |
| 1611 | dg_proto = TCP_PROTO; |
| 1612 | |
| 1613 | if (getenv("TCPDIAG_FILE")) |
| 1614 | return tcp_show_netlink_file(f); |
| 1615 | |
| 1616 | if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT") |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1617 | && tcp_show_netlink(f, NULL, socktype) == 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1618 | return 0; |
| 1619 | |
| 1620 | /* Sigh... We have to parse /proc/net/tcp... */ |
| 1621 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1622 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1623 | /* Estimate amount of sockets and try to allocate |
| 1624 | * huge buffer to read all the table at one read. |
| 1625 | * Limit it by 16MB though. The assumption is: as soon as |
| 1626 | * kernel was able to hold information about N connections, |
| 1627 | * it is able to give us some memory for snapshot. |
| 1628 | */ |
| 1629 | if (1) { |
| 1630 | int guess = slabstat.socks+slabstat.tcp_syns; |
| 1631 | if (f->states&(1<<SS_TIME_WAIT)) |
| 1632 | guess += slabstat.tcp_tws; |
| 1633 | if (guess > (16*1024*1024)/128) |
| 1634 | guess = (16*1024*1024)/128; |
| 1635 | guess *= 128; |
| 1636 | if (guess > bufsize) |
| 1637 | bufsize = guess; |
| 1638 | } |
| 1639 | while (bufsize >= 64*1024) { |
| 1640 | if ((buf = malloc(bufsize)) != NULL) |
| 1641 | break; |
| 1642 | bufsize /= 2; |
| 1643 | } |
| 1644 | if (buf == NULL) { |
| 1645 | errno = ENOMEM; |
| 1646 | return -1; |
| 1647 | } |
| 1648 | |
| 1649 | if (f->families & (1<<AF_INET)) { |
Björn Steinbrink | 69cae64 | 2008-04-03 11:42:41 +0200 | [diff] [blame] | 1650 | if ((fp = net_tcp_open()) == NULL) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1651 | goto outerr; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1652 | |
| 1653 | setbuffer(fp, buf, bufsize); |
| 1654 | if (generic_record_read(fp, tcp_show_line, f, AF_INET)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1655 | goto outerr; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1656 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | if ((f->families & (1<<AF_INET6)) && |
Björn Steinbrink | 69cae64 | 2008-04-03 11:42:41 +0200 | [diff] [blame] | 1660 | (fp = net_tcp6_open()) != NULL) { |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1661 | setbuffer(fp, buf, bufsize); |
| 1662 | if (generic_record_read(fp, tcp_show_line, f, AF_INET6)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1663 | goto outerr; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1664 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | free(buf); |
| 1668 | return 0; |
| 1669 | |
| 1670 | outerr: |
| 1671 | do { |
| 1672 | int saved_errno = errno; |
| 1673 | if (buf) |
| 1674 | free(buf); |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1675 | if (fp) |
| 1676 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1677 | errno = saved_errno; |
| 1678 | return -1; |
| 1679 | } while (0); |
| 1680 | } |
| 1681 | |
| 1682 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1683 | int dgram_show_line(char *line, const struct filter *f, int family) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1684 | { |
| 1685 | struct tcpstat s; |
| 1686 | char *loc, *rem, *data; |
| 1687 | char opt[256]; |
| 1688 | int n; |
| 1689 | char *p; |
| 1690 | |
| 1691 | if ((p = strchr(line, ':')) == NULL) |
| 1692 | return -1; |
| 1693 | loc = p+2; |
| 1694 | |
| 1695 | if ((p = strchr(loc, ':')) == NULL) |
| 1696 | return -1; |
| 1697 | p[5] = 0; |
| 1698 | rem = p+6; |
| 1699 | |
| 1700 | if ((p = strchr(rem, ':')) == NULL) |
| 1701 | return -1; |
| 1702 | p[5] = 0; |
| 1703 | data = p+6; |
| 1704 | |
| 1705 | do { |
| 1706 | int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0'); |
| 1707 | |
| 1708 | if (!(f->states & (1<<state))) |
| 1709 | return 0; |
| 1710 | } while (0); |
| 1711 | |
| 1712 | s.local.family = s.remote.family = family; |
| 1713 | if (family == AF_INET) { |
| 1714 | sscanf(loc, "%x:%x", s.local.data, (unsigned*)&s.lport); |
| 1715 | sscanf(rem, "%x:%x", s.remote.data, (unsigned*)&s.rport); |
| 1716 | s.local.bytelen = s.remote.bytelen = 4; |
| 1717 | } else { |
| 1718 | sscanf(loc, "%08x%08x%08x%08x:%x", |
| 1719 | s.local.data, |
| 1720 | s.local.data+1, |
| 1721 | s.local.data+2, |
| 1722 | s.local.data+3, |
| 1723 | &s.lport); |
| 1724 | sscanf(rem, "%08x%08x%08x%08x:%x", |
| 1725 | s.remote.data, |
| 1726 | s.remote.data+1, |
| 1727 | s.remote.data+2, |
| 1728 | s.remote.data+3, |
| 1729 | &s.rport); |
| 1730 | s.local.bytelen = s.remote.bytelen = 16; |
| 1731 | } |
| 1732 | |
| 1733 | if (f->f && run_ssfilter(f->f, &s) == 0) |
| 1734 | return 0; |
| 1735 | |
| 1736 | opt[0] = 0; |
Stephen Hemminger | e7113c6 | 2007-07-10 18:26:54 -0700 | [diff] [blame] | 1737 | n = sscanf(data, "%x %x:%x %*x:%*x %*x %d %*d %u %d %llx %[^\n]\n", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1738 | &s.state, &s.wq, &s.rq, |
| 1739 | &s.uid, &s.ino, |
| 1740 | &s.refcnt, &s.sk, opt); |
| 1741 | |
| 1742 | if (n < 9) |
| 1743 | opt[0] = 0; |
| 1744 | |
| 1745 | if (netid_width) |
| 1746 | printf("%-*s ", netid_width, dg_proto); |
| 1747 | if (state_width) |
| 1748 | printf("%-*s ", state_width, sstate_name[s.state]); |
| 1749 | |
| 1750 | printf("%-6d %-6d ", s.rq, s.wq); |
| 1751 | |
| 1752 | formatted_print(&s.local, s.lport); |
| 1753 | formatted_print(&s.remote, s.rport); |
| 1754 | |
| 1755 | if (show_users) { |
| 1756 | char ubuf[4096]; |
| 1757 | if (find_users(s.ino, ubuf, sizeof(ubuf)) > 0) |
| 1758 | printf(" users:(%s)", ubuf); |
| 1759 | } |
| 1760 | |
| 1761 | if (show_details) { |
| 1762 | if (s.uid) |
| 1763 | printf(" uid=%u", (unsigned)s.uid); |
Stephen Hemminger | e7113c6 | 2007-07-10 18:26:54 -0700 | [diff] [blame] | 1764 | printf(" ino=%u", s.ino); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1765 | printf(" sk=%llx", s.sk); |
| 1766 | if (opt[0]) |
| 1767 | printf(" opt:\"%s\"", opt); |
| 1768 | } |
| 1769 | printf("\n"); |
| 1770 | |
| 1771 | return 0; |
| 1772 | } |
| 1773 | |
| 1774 | |
| 1775 | int udp_show(struct filter *f) |
| 1776 | { |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1777 | FILE *fp = NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1778 | |
| 1779 | dg_proto = UDP_PROTO; |
| 1780 | |
| 1781 | if (f->families&(1<<AF_INET)) { |
Björn Steinbrink | 69cae64 | 2008-04-03 11:42:41 +0200 | [diff] [blame] | 1782 | if ((fp = net_udp_open()) == NULL) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1783 | goto outerr; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1784 | if (generic_record_read(fp, dgram_show_line, f, AF_INET)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1785 | goto outerr; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1786 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | if ((f->families&(1<<AF_INET6)) && |
Björn Steinbrink | 69cae64 | 2008-04-03 11:42:41 +0200 | [diff] [blame] | 1790 | (fp = net_udp6_open()) != NULL) { |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1791 | if (generic_record_read(fp, dgram_show_line, f, AF_INET6)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1792 | goto outerr; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1793 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1794 | } |
| 1795 | return 0; |
| 1796 | |
| 1797 | outerr: |
| 1798 | do { |
| 1799 | int saved_errno = errno; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1800 | if (fp) |
| 1801 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1802 | errno = saved_errno; |
| 1803 | return -1; |
| 1804 | } while (0); |
| 1805 | } |
| 1806 | |
| 1807 | int raw_show(struct filter *f) |
| 1808 | { |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1809 | FILE *fp = NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1810 | |
| 1811 | dg_proto = RAW_PROTO; |
| 1812 | |
| 1813 | if (f->families&(1<<AF_INET)) { |
Björn Steinbrink | 69cae64 | 2008-04-03 11:42:41 +0200 | [diff] [blame] | 1814 | if ((fp = net_raw_open()) == NULL) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1815 | goto outerr; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1816 | if (generic_record_read(fp, dgram_show_line, f, AF_INET)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1817 | goto outerr; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1818 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | if ((f->families&(1<<AF_INET6)) && |
Björn Steinbrink | 69cae64 | 2008-04-03 11:42:41 +0200 | [diff] [blame] | 1822 | (fp = net_raw6_open()) != NULL) { |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1823 | if (generic_record_read(fp, dgram_show_line, f, AF_INET6)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1824 | goto outerr; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1825 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1826 | } |
| 1827 | return 0; |
| 1828 | |
| 1829 | outerr: |
| 1830 | do { |
| 1831 | int saved_errno = errno; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1832 | if (fp) |
| 1833 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1834 | errno = saved_errno; |
| 1835 | return -1; |
| 1836 | } while (0); |
| 1837 | } |
| 1838 | |
| 1839 | |
| 1840 | struct unixstat |
| 1841 | { |
| 1842 | struct unixstat *next; |
| 1843 | int ino; |
| 1844 | int peer; |
| 1845 | int rq; |
| 1846 | int wq; |
| 1847 | int state; |
| 1848 | int type; |
| 1849 | char *name; |
| 1850 | }; |
| 1851 | |
| 1852 | |
| 1853 | |
| 1854 | int unix_state_map[] = { SS_CLOSE, SS_SYN_SENT, |
| 1855 | SS_ESTABLISHED, SS_CLOSING }; |
| 1856 | |
| 1857 | |
| 1858 | #define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct unixstat)) |
| 1859 | |
| 1860 | void unix_list_free(struct unixstat *list) |
| 1861 | { |
| 1862 | while (list) { |
| 1863 | struct unixstat *s = list; |
| 1864 | list = list->next; |
| 1865 | if (s->name) |
| 1866 | free(s->name); |
| 1867 | free(s); |
| 1868 | } |
| 1869 | } |
| 1870 | |
| 1871 | void unix_list_print(struct unixstat *list, struct filter *f) |
| 1872 | { |
| 1873 | struct unixstat *s; |
| 1874 | char *peer; |
| 1875 | |
| 1876 | for (s = list; s; s = s->next) { |
| 1877 | if (!(f->states & (1<<s->state))) |
| 1878 | continue; |
| 1879 | if (s->type == SOCK_STREAM && !(f->dbs&(1<<UNIX_ST_DB))) |
| 1880 | continue; |
| 1881 | if (s->type == SOCK_DGRAM && !(f->dbs&(1<<UNIX_DG_DB))) |
| 1882 | continue; |
| 1883 | |
| 1884 | peer = "*"; |
| 1885 | if (s->peer) { |
| 1886 | struct unixstat *p; |
| 1887 | for (p = list; p; p = p->next) { |
| 1888 | if (s->peer == p->ino) |
| 1889 | break; |
| 1890 | } |
| 1891 | if (!p) { |
| 1892 | peer = "?"; |
| 1893 | } else { |
| 1894 | peer = p->name ? : "*"; |
| 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | if (f->f) { |
| 1899 | struct tcpstat tst; |
| 1900 | tst.local.family = AF_UNIX; |
| 1901 | tst.remote.family = AF_UNIX; |
| 1902 | memcpy(tst.local.data, &s->name, sizeof(s->name)); |
| 1903 | if (strcmp(peer, "*") == 0) |
| 1904 | memset(tst.remote.data, 0, sizeof(peer)); |
| 1905 | else |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1906 | memcpy(tst.remote.data, &peer, sizeof(peer)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1907 | if (run_ssfilter(f->f, &tst) == 0) |
| 1908 | continue; |
| 1909 | } |
| 1910 | |
| 1911 | if (netid_width) |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1912 | printf("%-*s ", netid_width, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1913 | s->type == SOCK_STREAM ? "u_str" : "u_dgr"); |
| 1914 | if (state_width) |
| 1915 | printf("%-*s ", state_width, sstate_name[s->state]); |
| 1916 | printf("%-6d %-6d ", s->rq, s->wq); |
| 1917 | printf("%*s %-*d %*s %-*d", |
| 1918 | addr_width, s->name ? : "*", serv_width, s->ino, |
| 1919 | addr_width, peer, serv_width, s->peer); |
| 1920 | if (show_users) { |
| 1921 | char ubuf[4096]; |
| 1922 | if (find_users(s->ino, ubuf, sizeof(ubuf)) > 0) |
| 1923 | printf(" users:(%s)", ubuf); |
| 1924 | } |
| 1925 | printf("\n"); |
| 1926 | } |
| 1927 | } |
| 1928 | |
| 1929 | int unix_show(struct filter *f) |
| 1930 | { |
| 1931 | FILE *fp; |
| 1932 | char buf[256]; |
| 1933 | char name[128]; |
| 1934 | int newformat = 0; |
| 1935 | int cnt; |
| 1936 | struct unixstat *list = NULL; |
| 1937 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1938 | if ((fp = net_unix_open()) == NULL) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1939 | return -1; |
| 1940 | fgets(buf, sizeof(buf)-1, fp); |
| 1941 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1942 | if (memcmp(buf, "Peer", 4) == 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1943 | newformat = 1; |
| 1944 | cnt = 0; |
| 1945 | |
| 1946 | while (fgets(buf, sizeof(buf)-1, fp)) { |
| 1947 | struct unixstat *u, **insp; |
| 1948 | int flags; |
| 1949 | |
| 1950 | if (!(u = malloc(sizeof(*u)))) |
| 1951 | break; |
| 1952 | u->name = NULL; |
| 1953 | |
| 1954 | if (sscanf(buf, "%x: %x %x %x %x %x %d %s", |
| 1955 | &u->peer, &u->rq, &u->wq, &flags, &u->type, |
| 1956 | &u->state, &u->ino, name) < 8) |
| 1957 | name[0] = 0; |
| 1958 | |
| 1959 | if (flags&(1<<16)) { |
| 1960 | u->state = SS_LISTEN; |
| 1961 | } else { |
| 1962 | u->state = unix_state_map[u->state-1]; |
| 1963 | if (u->type == SOCK_DGRAM && |
| 1964 | u->state == SS_CLOSE && |
| 1965 | u->peer) |
| 1966 | u->state = SS_ESTABLISHED; |
| 1967 | } |
| 1968 | |
| 1969 | if (!newformat) { |
| 1970 | u->peer = 0; |
| 1971 | u->rq = 0; |
| 1972 | u->wq = 0; |
| 1973 | } |
| 1974 | |
| 1975 | insp = &list; |
| 1976 | while (*insp) { |
| 1977 | if (u->type < (*insp)->type || |
| 1978 | (u->type == (*insp)->type && |
| 1979 | u->ino < (*insp)->ino)) |
| 1980 | break; |
| 1981 | insp = &(*insp)->next; |
| 1982 | } |
| 1983 | u->next = *insp; |
| 1984 | *insp = u; |
| 1985 | |
| 1986 | if (name[0]) { |
| 1987 | if ((u->name = malloc(strlen(name)+1)) == NULL) |
| 1988 | break; |
| 1989 | strcpy(u->name, name); |
| 1990 | } |
| 1991 | if (++cnt > MAX_UNIX_REMEMBER) { |
| 1992 | unix_list_print(list, f); |
| 1993 | unix_list_free(list); |
| 1994 | list = NULL; |
| 1995 | cnt = 0; |
| 1996 | } |
| 1997 | } |
| 1998 | |
| 1999 | if (list) { |
| 2000 | unix_list_print(list, f); |
| 2001 | unix_list_free(list); |
| 2002 | list = NULL; |
| 2003 | cnt = 0; |
| 2004 | } |
| 2005 | |
| 2006 | return 0; |
| 2007 | } |
| 2008 | |
| 2009 | |
| 2010 | int packet_show(struct filter *f) |
| 2011 | { |
| 2012 | FILE *fp; |
| 2013 | char buf[256]; |
| 2014 | int type; |
| 2015 | int prot; |
| 2016 | int iface; |
| 2017 | int state; |
| 2018 | int rq; |
| 2019 | int uid; |
| 2020 | int ino; |
| 2021 | unsigned long long sk; |
| 2022 | |
| 2023 | if (!(f->states & (1<<SS_CLOSE))) |
| 2024 | return 0; |
| 2025 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2026 | if ((fp = net_packet_open()) == NULL) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2027 | return -1; |
| 2028 | fgets(buf, sizeof(buf)-1, fp); |
| 2029 | |
| 2030 | while (fgets(buf, sizeof(buf)-1, fp)) { |
| 2031 | sscanf(buf, "%llx %*d %d %x %d %d %u %u %u", |
| 2032 | &sk, |
| 2033 | &type, &prot, &iface, &state, |
| 2034 | &rq, &uid, &ino); |
| 2035 | |
| 2036 | if (type == SOCK_RAW && !(f->dbs&(1<<PACKET_R_DB))) |
| 2037 | continue; |
| 2038 | if (type == SOCK_DGRAM && !(f->dbs&(1<<PACKET_DG_DB))) |
| 2039 | continue; |
| 2040 | if (f->f) { |
| 2041 | struct tcpstat tst; |
| 2042 | tst.local.family = AF_PACKET; |
| 2043 | tst.remote.family = AF_PACKET; |
| 2044 | tst.rport = 0; |
| 2045 | tst.lport = iface; |
| 2046 | tst.local.data[0] = prot; |
| 2047 | tst.remote.data[0] = 0; |
| 2048 | if (run_ssfilter(f->f, &tst) == 0) |
| 2049 | continue; |
| 2050 | } |
| 2051 | |
| 2052 | if (netid_width) |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 2053 | printf("%-*s ", netid_width, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2054 | type == SOCK_RAW ? "p_raw" : "p_dgr"); |
| 2055 | if (state_width) |
| 2056 | printf("%-*s ", state_width, "UNCONN"); |
| 2057 | printf("%-6d %-6d ", rq, 0); |
| 2058 | if (prot == 3) { |
| 2059 | printf("%*s:", addr_width, "*"); |
| 2060 | } else { |
| 2061 | char tb[16]; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 2062 | printf("%*s:", addr_width, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2063 | ll_proto_n2a(htons(prot), tb, sizeof(tb))); |
| 2064 | } |
| 2065 | if (iface == 0) { |
| 2066 | printf("%-*s ", serv_width, "*"); |
| 2067 | } else { |
| 2068 | printf("%-*s ", serv_width, xll_index_to_name(iface)); |
| 2069 | } |
| 2070 | printf("%*s*%-*s", |
| 2071 | addr_width, "", serv_width, ""); |
| 2072 | |
| 2073 | if (show_users) { |
| 2074 | char ubuf[4096]; |
| 2075 | if (find_users(ino, ubuf, sizeof(ubuf)) > 0) |
| 2076 | printf(" users:(%s)", ubuf); |
| 2077 | } |
| 2078 | if (show_details) { |
| 2079 | printf(" ino=%u uid=%u sk=%llx", ino, uid, sk); |
| 2080 | } |
| 2081 | printf("\n"); |
| 2082 | } |
| 2083 | |
| 2084 | return 0; |
| 2085 | } |
| 2086 | |
| 2087 | int netlink_show(struct filter *f) |
| 2088 | { |
| 2089 | FILE *fp; |
| 2090 | char buf[256]; |
| 2091 | int prot, pid; |
| 2092 | unsigned groups; |
| 2093 | int rq, wq, rc; |
| 2094 | unsigned long long sk, cb; |
| 2095 | |
| 2096 | if (!(f->states & (1<<SS_CLOSE))) |
| 2097 | return 0; |
| 2098 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2099 | if ((fp = net_netlink_open()) == NULL) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2100 | return -1; |
| 2101 | fgets(buf, sizeof(buf)-1, fp); |
| 2102 | |
| 2103 | while (fgets(buf, sizeof(buf)-1, fp)) { |
| 2104 | sscanf(buf, "%llx %d %d %x %d %d %llx %d", |
| 2105 | &sk, |
| 2106 | &prot, &pid, &groups, &rq, &wq, &cb, &rc); |
| 2107 | |
| 2108 | if (f->f) { |
| 2109 | struct tcpstat tst; |
| 2110 | tst.local.family = AF_NETLINK; |
| 2111 | tst.remote.family = AF_NETLINK; |
| 2112 | tst.rport = -1; |
| 2113 | tst.lport = pid; |
| 2114 | tst.local.data[0] = prot; |
| 2115 | tst.remote.data[0] = 0; |
| 2116 | if (run_ssfilter(f->f, &tst) == 0) |
| 2117 | continue; |
| 2118 | } |
| 2119 | |
| 2120 | if (netid_width) |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 2121 | printf("%-*s ", netid_width, "nl"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2122 | if (state_width) |
| 2123 | printf("%-*s ", state_width, "UNCONN"); |
| 2124 | printf("%-6d %-6d ", rq, wq); |
| 2125 | if (resolve_services && prot == 0) |
| 2126 | printf("%*s:", addr_width, "rtnl"); |
| 2127 | else if (resolve_services && prot == 3) |
| 2128 | printf("%*s:", addr_width, "fw"); |
| 2129 | else if (resolve_services && prot == 4) |
| 2130 | printf("%*s:", addr_width, "tcpdiag"); |
| 2131 | else |
| 2132 | printf("%*d:", addr_width, prot); |
| 2133 | if (pid == -1) { |
| 2134 | printf("%-*s ", serv_width, "*"); |
| 2135 | } else if (resolve_services) { |
| 2136 | int done = 0; |
| 2137 | if (!pid) { |
| 2138 | done = 1; |
| 2139 | printf("%-*s ", serv_width, "kernel"); |
| 2140 | } else if (pid > 0) { |
| 2141 | char procname[64]; |
| 2142 | FILE *fp; |
| 2143 | sprintf(procname, "%s/%d/stat", |
| 2144 | getenv("PROC_ROOT") ? : "/proc", pid); |
| 2145 | if ((fp = fopen(procname, "r")) != NULL) { |
| 2146 | if (fscanf(fp, "%*d (%[^)])", procname) == 1) { |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 2147 | sprintf(procname+strlen(procname), "/%d", pid); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2148 | printf("%-*s ", serv_width, procname); |
| 2149 | done = 1; |
| 2150 | } |
| 2151 | fclose(fp); |
| 2152 | } |
| 2153 | } |
| 2154 | if (!done) |
| 2155 | printf("%-*d ", serv_width, pid); |
| 2156 | } else { |
| 2157 | printf("%-*d ", serv_width, pid); |
| 2158 | } |
| 2159 | printf("%*s*%-*s", |
| 2160 | addr_width, "", serv_width, ""); |
| 2161 | |
| 2162 | if (show_details) { |
| 2163 | printf(" sk=%llx cb=%llx groups=0x%08x", sk, cb, groups); |
| 2164 | } |
| 2165 | printf("\n"); |
| 2166 | } |
| 2167 | |
| 2168 | return 0; |
| 2169 | } |
| 2170 | |
| 2171 | struct snmpstat |
| 2172 | { |
| 2173 | int tcp_estab; |
| 2174 | }; |
| 2175 | |
| 2176 | int get_snmp_int(char *proto, char *key, int *result) |
| 2177 | { |
| 2178 | char buf[1024]; |
| 2179 | FILE *fp; |
| 2180 | int protolen = strlen(proto); |
| 2181 | int keylen = strlen(key); |
| 2182 | |
| 2183 | *result = 0; |
| 2184 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2185 | if ((fp = net_snmp_open()) == NULL) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2186 | return -1; |
| 2187 | |
| 2188 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 2189 | char *p = buf; |
| 2190 | int pos = 0; |
| 2191 | if (memcmp(buf, proto, protolen)) |
| 2192 | continue; |
| 2193 | while ((p = strchr(p, ' ')) != NULL) { |
| 2194 | pos++; |
| 2195 | p++; |
| 2196 | if (memcmp(p, key, keylen) == 0 && |
| 2197 | (p[keylen] == ' ' || p[keylen] == '\n')) |
| 2198 | break; |
| 2199 | } |
| 2200 | if (fgets(buf, sizeof(buf), fp) == NULL) |
| 2201 | break; |
| 2202 | if (memcmp(buf, proto, protolen)) |
| 2203 | break; |
| 2204 | p = buf; |
| 2205 | while ((p = strchr(p, ' ')) != NULL) { |
| 2206 | p++; |
| 2207 | if (--pos == 0) { |
| 2208 | sscanf(p, "%d", result); |
| 2209 | fclose(fp); |
| 2210 | return 0; |
| 2211 | } |
| 2212 | } |
| 2213 | } |
| 2214 | |
| 2215 | fclose(fp); |
| 2216 | errno = ESRCH; |
| 2217 | return -1; |
| 2218 | } |
| 2219 | |
| 2220 | |
| 2221 | /* Get stats from sockstat */ |
| 2222 | |
| 2223 | struct sockstat |
| 2224 | { |
| 2225 | int socks; |
| 2226 | int tcp_mem; |
| 2227 | int tcp_total; |
| 2228 | int tcp_orphans; |
| 2229 | int tcp_tws; |
| 2230 | int tcp4_hashed; |
| 2231 | int udp4; |
| 2232 | int raw4; |
| 2233 | int frag4; |
| 2234 | int frag4_mem; |
| 2235 | int tcp6_hashed; |
| 2236 | int udp6; |
| 2237 | int raw6; |
| 2238 | int frag6; |
| 2239 | int frag6_mem; |
| 2240 | }; |
| 2241 | |
| 2242 | static void get_sockstat_line(char *line, struct sockstat *s) |
| 2243 | { |
| 2244 | char id[256], rem[256]; |
| 2245 | |
| 2246 | if (sscanf(line, "%[^ ] %[^\n]\n", id, rem) != 2) |
| 2247 | return; |
| 2248 | |
| 2249 | if (strcmp(id, "sockets:") == 0) |
| 2250 | sscanf(rem, "%*s%d", &s->socks); |
| 2251 | else if (strcmp(id, "UDP:") == 0) |
| 2252 | sscanf(rem, "%*s%d", &s->udp4); |
| 2253 | else if (strcmp(id, "UDP6:") == 0) |
| 2254 | sscanf(rem, "%*s%d", &s->udp6); |
| 2255 | else if (strcmp(id, "RAW:") == 0) |
| 2256 | sscanf(rem, "%*s%d", &s->raw4); |
| 2257 | else if (strcmp(id, "RAW6:") == 0) |
| 2258 | sscanf(rem, "%*s%d", &s->raw6); |
| 2259 | else if (strcmp(id, "TCP6:") == 0) |
| 2260 | sscanf(rem, "%*s%d", &s->tcp6_hashed); |
| 2261 | else if (strcmp(id, "FRAG:") == 0) |
| 2262 | sscanf(rem, "%*s%d%*s%d", &s->frag4, &s->frag4_mem); |
| 2263 | else if (strcmp(id, "FRAG6:") == 0) |
| 2264 | sscanf(rem, "%*s%d%*s%d", &s->frag6, &s->frag6_mem); |
| 2265 | else if (strcmp(id, "TCP:") == 0) |
| 2266 | sscanf(rem, "%*s%d%*s%d%*s%d%*s%d%*s%d", |
| 2267 | &s->tcp4_hashed, |
| 2268 | &s->tcp_orphans, &s->tcp_tws, &s->tcp_total, &s->tcp_mem); |
| 2269 | } |
| 2270 | |
| 2271 | int get_sockstat(struct sockstat *s) |
| 2272 | { |
| 2273 | char buf[256]; |
| 2274 | FILE *fp; |
| 2275 | |
| 2276 | memset(s, 0, sizeof(*s)); |
| 2277 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2278 | if ((fp = net_sockstat_open()) == NULL) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2279 | return -1; |
| 2280 | while(fgets(buf, sizeof(buf), fp) != NULL) |
| 2281 | get_sockstat_line(buf, s); |
| 2282 | fclose(fp); |
| 2283 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2284 | if ((fp = net_sockstat6_open()) == NULL) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2285 | return 0; |
| 2286 | while(fgets(buf, sizeof(buf), fp) != NULL) |
| 2287 | get_sockstat_line(buf, s); |
| 2288 | fclose(fp); |
| 2289 | |
| 2290 | return 0; |
| 2291 | } |
| 2292 | |
| 2293 | int print_summary(void) |
| 2294 | { |
| 2295 | struct sockstat s; |
| 2296 | struct snmpstat sn; |
| 2297 | |
| 2298 | if (get_sockstat(&s) < 0) |
| 2299 | perror("ss: get_sockstat"); |
| 2300 | if (get_snmp_int("Tcp:", "CurrEstab", &sn.tcp_estab) < 0) |
| 2301 | perror("ss: get_snmpstat"); |
| 2302 | |
| 2303 | printf("Total: %d (kernel %d)\n", s.socks, slabstat.socks); |
| 2304 | |
| 2305 | printf("TCP: %d (estab %d, closed %d, orphaned %d, synrecv %d, timewait %d/%d), ports %d\n", |
| 2306 | s.tcp_total + slabstat.tcp_syns + s.tcp_tws, |
| 2307 | sn.tcp_estab, |
| 2308 | s.tcp_total - (s.tcp4_hashed+s.tcp6_hashed-s.tcp_tws), |
| 2309 | s.tcp_orphans, |
| 2310 | slabstat.tcp_syns, |
| 2311 | s.tcp_tws, slabstat.tcp_tws, |
| 2312 | slabstat.tcp_ports |
| 2313 | ); |
| 2314 | |
| 2315 | printf("\n"); |
| 2316 | printf("Transport Total IP IPv6\n"); |
| 2317 | printf("* %-9d %-9s %-9s\n", slabstat.socks, "-", "-"); |
| 2318 | printf("RAW %-9d %-9d %-9d\n", s.raw4+s.raw6, s.raw4, s.raw6); |
| 2319 | printf("UDP %-9d %-9d %-9d\n", s.udp4+s.udp6, s.udp4, s.udp6); |
| 2320 | printf("TCP %-9d %-9d %-9d\n", s.tcp4_hashed+s.tcp6_hashed, s.tcp4_hashed, s.tcp6_hashed); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 2321 | printf("INET %-9d %-9d %-9d\n", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2322 | s.raw4+s.udp4+s.tcp4_hashed+ |
| 2323 | s.raw6+s.udp6+s.tcp6_hashed, |
| 2324 | s.raw4+s.udp4+s.tcp4_hashed, |
| 2325 | s.raw6+s.udp6+s.tcp6_hashed); |
| 2326 | printf("FRAG %-9d %-9d %-9d\n", s.frag4+s.frag6, s.frag4, s.frag6); |
| 2327 | |
| 2328 | printf("\n"); |
| 2329 | |
| 2330 | return 0; |
| 2331 | } |
| 2332 | |
| 2333 | |
| 2334 | static void usage(void) __attribute__((noreturn)); |
| 2335 | |
| 2336 | static void usage(void) |
| 2337 | { |
| 2338 | fprintf(stderr, |
| 2339 | "Usage: ss [ OPTIONS ]\n" |
| 2340 | " ss [ OPTIONS ] [ FILTER ]\n" |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 2341 | " -h, --help this message\n" |
| 2342 | " -V, --version output version information\n" |
| 2343 | " -n, --numeric don't resolve service names\n" |
| 2344 | " -r, --resolve resolve host names\n" |
| 2345 | " -a, --all display all sockets\n" |
| 2346 | " -l, --listening display listening sockets\n" |
| 2347 | " -o, --options show timer information\n" |
| 2348 | " -e, --extended show detailed socket information\n" |
| 2349 | " -m, --memory show socket memory usage\n" |
| 2350 | " -p, --processes show process using socket\n" |
| 2351 | " -i, --info show internal TCP information\n" |
| 2352 | " -s, --summary show socket usage summary\n" |
| 2353 | "\n" |
| 2354 | " -4, --ipv4 display only IP version 4 sockets\n" |
| 2355 | " -6, --ipv6 display only IP version 6 sockets\n" |
| 2356 | " -0, --packet display PACKET sockets\n" |
| 2357 | " -t, --tcp display only TCP sockets\n" |
| 2358 | " -u, --udp display only UDP sockets\n" |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 2359 | " -d, --dccp display only DCCP sockets\n" |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 2360 | " -w, --raw display only RAW sockets\n" |
| 2361 | " -x, --unix display only Unix domain sockets\n" |
| 2362 | " -f, --family=FAMILY display sockets of type FAMILY\n" |
| 2363 | "\n" |
| 2364 | " -A, --query=QUERY\n" |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2365 | " QUERY := {all|inet|tcp|udp|raw|unix|packet|netlink}[,QUERY]\n" |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 2366 | "\n" |
| 2367 | " -F, --filter=FILE read filter information from FILE\n" |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2368 | " FILTER := [ state TCP-STATE ] [ EXPRESSION ]\n" |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 2369 | ); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2370 | exit(-1); |
| 2371 | } |
| 2372 | |
| 2373 | |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 2374 | int scan_state(const char *state) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2375 | { |
| 2376 | int i; |
| 2377 | if (strcasecmp(state, "close") == 0 || |
| 2378 | strcasecmp(state, "closed") == 0) |
| 2379 | return (1<<SS_CLOSE); |
| 2380 | if (strcasecmp(state, "syn-rcv") == 0) |
| 2381 | return (1<<SS_SYN_RECV); |
osdl.net!shemminger | 1a5bad5 | 2004-07-07 17:06:35 +0000 | [diff] [blame] | 2382 | if (strcasecmp(state, "established") == 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2383 | return (1<<SS_ESTABLISHED); |
| 2384 | if (strcasecmp(state, "all") == 0) |
| 2385 | return SS_ALL; |
| 2386 | if (strcasecmp(state, "connected") == 0) |
| 2387 | return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)); |
osdl.net!shemminger | 1a5bad5 | 2004-07-07 17:06:35 +0000 | [diff] [blame] | 2388 | if (strcasecmp(state, "synchronized") == 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2389 | return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)|(1<<SS_SYN_SENT)); |
| 2390 | if (strcasecmp(state, "bucket") == 0) |
| 2391 | return (1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT); |
| 2392 | if (strcasecmp(state, "big") == 0) |
| 2393 | return SS_ALL & ~((1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT)); |
| 2394 | for (i=0; i<SS_MAX; i++) { |
osdl.net!shemminger | 1a5bad5 | 2004-07-07 17:06:35 +0000 | [diff] [blame] | 2395 | if (strcasecmp(state, sstate_namel[i]) == 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2396 | return (1<<i); |
| 2397 | } |
| 2398 | return 0; |
| 2399 | } |
| 2400 | |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 2401 | static const struct option long_opts[] = { |
| 2402 | { "numeric", 0, 0, 'n' }, |
| 2403 | { "resolve", 0, 0, 'r' }, |
| 2404 | { "options", 0, 0, 'o' }, |
| 2405 | { "extended", 0, 0, 'e' }, |
| 2406 | { "memory", 0, 0, 'm' }, |
| 2407 | { "info", 0, 0, 'i' }, |
| 2408 | { "processes", 0, 0, 'p' }, |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 2409 | { "dccp", 0, 0, 'd' }, |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 2410 | { "tcp", 0, 0, 't' }, |
| 2411 | { "udp", 0, 0, 'u' }, |
| 2412 | { "raw", 0, 0, 'w' }, |
| 2413 | { "unix", 0, 0, 'x' }, |
| 2414 | { "all", 0, 0, 'a' }, |
| 2415 | { "listening", 0, 0, 'l' }, |
| 2416 | { "ipv4", 0, 0, '4' }, |
| 2417 | { "ipv6", 0, 0, '6' }, |
| 2418 | { "packet", 0, 0, '0' }, |
| 2419 | { "family", 1, 0, 'f' }, |
| 2420 | { "socket", 1, 0, 'A' }, |
osdl.net!shemminger | c3f346b | 2005-01-18 22:31:42 +0000 | [diff] [blame] | 2421 | { "summary", 0, 0, 's' }, |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 2422 | { "diag", 0, 0, 'D' }, |
| 2423 | { "filter", 1, 0, 'F' }, |
| 2424 | { "version", 0, 0, 'V' }, |
| 2425 | { "help", 0, 0, 'h' }, |
| 2426 | { 0 } |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 2427 | |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 2428 | }; |
| 2429 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2430 | int main(int argc, char *argv[]) |
| 2431 | { |
| 2432 | int do_default = 1; |
| 2433 | int saw_states = 0; |
| 2434 | int saw_query = 0; |
| 2435 | int do_summary = 0; |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 2436 | const char *dump_tcpdiag = NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2437 | FILE *filter_fp = NULL; |
| 2438 | int ch; |
| 2439 | |
| 2440 | memset(¤t_filter, 0, sizeof(current_filter)); |
| 2441 | |
| 2442 | current_filter.states = default_filter.states; |
| 2443 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 2444 | while ((ch = getopt_long(argc, argv, "dhaletuwxnro460spf:miA:D:F:vV", |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 2445 | long_opts, NULL)) != EOF) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2446 | switch(ch) { |
| 2447 | case 'n': |
| 2448 | resolve_services = 0; |
| 2449 | break; |
| 2450 | case 'r': |
| 2451 | resolve_hosts = 1; |
| 2452 | break; |
| 2453 | case 'o': |
| 2454 | show_options = 1; |
| 2455 | break; |
| 2456 | case 'e': |
| 2457 | show_options = 1; |
| 2458 | show_details++; |
| 2459 | break; |
| 2460 | case 'm': |
| 2461 | show_mem = 1; |
| 2462 | break; |
| 2463 | case 'i': |
| 2464 | show_tcpinfo = 1; |
| 2465 | break; |
| 2466 | case 'p': |
| 2467 | show_users++; |
| 2468 | break; |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 2469 | case 'd': |
| 2470 | current_filter.dbs |= (1<<DCCP_DB); |
| 2471 | do_default = 0; |
| 2472 | break; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2473 | case 't': |
| 2474 | current_filter.dbs |= (1<<TCP_DB); |
| 2475 | do_default = 0; |
| 2476 | break; |
| 2477 | case 'u': |
| 2478 | current_filter.dbs |= (1<<UDP_DB); |
| 2479 | do_default = 0; |
| 2480 | break; |
| 2481 | case 'w': |
| 2482 | current_filter.dbs |= (1<<RAW_DB); |
| 2483 | do_default = 0; |
| 2484 | break; |
| 2485 | case 'x': |
| 2486 | current_filter.dbs |= UNIX_DBM; |
| 2487 | do_default = 0; |
| 2488 | break; |
| 2489 | case 'a': |
| 2490 | current_filter.states = SS_ALL; |
| 2491 | break; |
| 2492 | case 'l': |
| 2493 | current_filter.states = (1<<SS_LISTEN); |
| 2494 | break; |
| 2495 | case '4': |
| 2496 | preferred_family = AF_INET; |
| 2497 | break; |
| 2498 | case '6': |
| 2499 | preferred_family = AF_INET6; |
| 2500 | break; |
| 2501 | case '0': |
| 2502 | preferred_family = AF_PACKET; |
| 2503 | break; |
| 2504 | case 'f': |
| 2505 | if (strcmp(optarg, "inet") == 0) |
| 2506 | preferred_family = AF_INET; |
| 2507 | else if (strcmp(optarg, "inet6") == 0) |
| 2508 | preferred_family = AF_INET6; |
| 2509 | else if (strcmp(optarg, "link") == 0) |
| 2510 | preferred_family = AF_PACKET; |
| 2511 | else if (strcmp(optarg, "unix") == 0) |
| 2512 | preferred_family = AF_UNIX; |
| 2513 | else if (strcmp(optarg, "netlink") == 0) |
| 2514 | preferred_family = AF_NETLINK; |
| 2515 | else if (strcmp(optarg, "help") == 0) |
| 2516 | usage(); |
| 2517 | else { |
| 2518 | fprintf(stderr, "ss: \"%s\" is invalid family\n", optarg); |
| 2519 | usage(); |
| 2520 | } |
| 2521 | break; |
| 2522 | case 'A': |
| 2523 | { |
| 2524 | char *p, *p1; |
| 2525 | if (!saw_query) { |
| 2526 | current_filter.dbs = 0; |
| 2527 | saw_query = 1; |
| 2528 | do_default = 0; |
| 2529 | } |
| 2530 | p = p1 = optarg; |
| 2531 | do { |
| 2532 | if ((p1 = strchr(p, ',')) != NULL) |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 2533 | *p1 = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2534 | if (strcmp(p, "all") == 0) { |
| 2535 | current_filter.dbs = ALL_DB; |
| 2536 | } else if (strcmp(p, "inet") == 0) { |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 2537 | current_filter.dbs |= (1<<TCP_DB)|(1<<DCCP_DB)|(1<<UDP_DB)|(1<<RAW_DB); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2538 | } else if (strcmp(p, "udp") == 0) { |
| 2539 | current_filter.dbs |= (1<<UDP_DB); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 2540 | } else if (strcmp(p, "dccp") == 0) { |
| 2541 | current_filter.dbs |= (1<<DCCP_DB); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2542 | } else if (strcmp(p, "tcp") == 0) { |
| 2543 | current_filter.dbs |= (1<<TCP_DB); |
| 2544 | } else if (strcmp(p, "raw") == 0) { |
| 2545 | current_filter.dbs |= (1<<RAW_DB); |
| 2546 | } else if (strcmp(p, "unix") == 0) { |
| 2547 | current_filter.dbs |= UNIX_DBM; |
osdl.net!shemminger | 1a5bad5 | 2004-07-07 17:06:35 +0000 | [diff] [blame] | 2548 | } else if (strcasecmp(p, "unix_stream") == 0 || |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2549 | strcmp(p, "u_str") == 0) { |
| 2550 | current_filter.dbs |= (1<<UNIX_ST_DB); |
osdl.net!shemminger | 1a5bad5 | 2004-07-07 17:06:35 +0000 | [diff] [blame] | 2551 | } else if (strcasecmp(p, "unix_dgram") == 0 || |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2552 | strcmp(p, "u_dgr") == 0) { |
| 2553 | current_filter.dbs |= (1<<UNIX_DG_DB); |
| 2554 | } else if (strcmp(p, "packet") == 0) { |
| 2555 | current_filter.dbs |= PACKET_DBM; |
| 2556 | } else if (strcmp(p, "packet_raw") == 0 || |
| 2557 | strcmp(p, "p_raw") == 0) { |
| 2558 | current_filter.dbs |= (1<<PACKET_R_DB); |
| 2559 | } else if (strcmp(p, "packet_dgram") == 0 || |
| 2560 | strcmp(p, "p_dgr") == 0) { |
| 2561 | current_filter.dbs |= (1<<PACKET_DG_DB); |
| 2562 | } else if (strcmp(p, "netlink") == 0) { |
| 2563 | current_filter.dbs |= (1<<NETLINK_DB); |
| 2564 | } else { |
| 2565 | fprintf(stderr, "ss: \"%s\" is illegal socket table id\n", p); |
| 2566 | usage(); |
| 2567 | } |
| 2568 | p = p1 + 1; |
| 2569 | } while (p1); |
| 2570 | break; |
| 2571 | } |
| 2572 | case 's': |
| 2573 | do_summary = 1; |
| 2574 | break; |
| 2575 | case 'D': |
| 2576 | dump_tcpdiag = optarg; |
| 2577 | break; |
| 2578 | case 'F': |
| 2579 | if (filter_fp) { |
| 2580 | fprintf(stderr, "More than one filter file\n"); |
| 2581 | exit(-1); |
| 2582 | } |
| 2583 | if (optarg[0] == '-') |
| 2584 | filter_fp = stdin; |
| 2585 | else |
| 2586 | filter_fp = fopen(optarg, "r"); |
| 2587 | if (!filter_fp) { |
| 2588 | perror("fopen filter file"); |
| 2589 | exit(-1); |
| 2590 | } |
| 2591 | break; |
| 2592 | case 'v': |
| 2593 | case 'V': |
| 2594 | printf("ss utility, iproute2-ss%s\n", SNAPSHOT); |
| 2595 | exit(0); |
| 2596 | case 'h': |
| 2597 | case '?': |
| 2598 | default: |
| 2599 | usage(); |
| 2600 | } |
| 2601 | } |
| 2602 | |
| 2603 | argc -= optind; |
| 2604 | argv += optind; |
| 2605 | |
| 2606 | get_slabstat(&slabstat); |
| 2607 | |
| 2608 | if (do_summary) { |
| 2609 | print_summary(); |
| 2610 | if (do_default && argc == 0) |
| 2611 | exit(0); |
| 2612 | } |
| 2613 | |
| 2614 | if (do_default) |
| 2615 | current_filter.dbs = default_filter.dbs; |
| 2616 | |
| 2617 | if (preferred_family == AF_UNSPEC) { |
| 2618 | if (!(current_filter.dbs&~UNIX_DBM)) |
| 2619 | preferred_family = AF_UNIX; |
| 2620 | else if (!(current_filter.dbs&~PACKET_DBM)) |
| 2621 | preferred_family = AF_PACKET; |
| 2622 | else if (!(current_filter.dbs&~(1<<NETLINK_DB))) |
| 2623 | preferred_family = AF_NETLINK; |
| 2624 | } |
| 2625 | |
| 2626 | if (preferred_family != AF_UNSPEC) { |
| 2627 | int mask2; |
| 2628 | if (preferred_family == AF_INET || |
| 2629 | preferred_family == AF_INET6) { |
Li Yewang | f70d96a | 2008-09-09 09:06:47 +0800 | [diff] [blame] | 2630 | mask2= current_filter.dbs; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2631 | } else if (preferred_family == AF_PACKET) { |
| 2632 | mask2 = PACKET_DBM; |
| 2633 | } else if (preferred_family == AF_UNIX) { |
| 2634 | mask2 = UNIX_DBM; |
| 2635 | } else if (preferred_family == AF_NETLINK) { |
| 2636 | mask2 = (1<<NETLINK_DB); |
| 2637 | } else { |
| 2638 | mask2 = 0; |
| 2639 | } |
| 2640 | |
| 2641 | if (do_default) |
| 2642 | current_filter.dbs = mask2; |
| 2643 | else |
| 2644 | current_filter.dbs &= mask2; |
| 2645 | current_filter.families = (1<<preferred_family); |
| 2646 | } else { |
| 2647 | if (!do_default) |
| 2648 | current_filter.families = ~0; |
| 2649 | else |
| 2650 | current_filter.families = default_filter.families; |
| 2651 | } |
| 2652 | if (current_filter.dbs == 0) { |
| 2653 | fprintf(stderr, "ss: no socket tables to show with such filter.\n"); |
| 2654 | exit(0); |
| 2655 | } |
| 2656 | if (current_filter.families == 0) { |
| 2657 | fprintf(stderr, "ss: no families to show with such filter.\n"); |
| 2658 | exit(0); |
| 2659 | } |
| 2660 | |
| 2661 | if (resolve_services && resolve_hosts && |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 2662 | (current_filter.dbs&(UNIX_DBM|(1<<TCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB)))) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2663 | init_service_resolver(); |
| 2664 | |
| 2665 | /* Now parse filter... */ |
| 2666 | if (argc == 0 && filter_fp) { |
| 2667 | if (ssfilter_parse(¤t_filter.f, 0, NULL, filter_fp)) |
| 2668 | usage(); |
| 2669 | } |
| 2670 | |
| 2671 | while (argc > 0) { |
| 2672 | if (strcmp(*argv, "state") == 0) { |
| 2673 | NEXT_ARG(); |
| 2674 | if (!saw_states) |
| 2675 | current_filter.states = 0; |
| 2676 | current_filter.states |= scan_state(*argv); |
| 2677 | saw_states = 1; |
| 2678 | } else if (strcmp(*argv, "exclude") == 0 || |
| 2679 | strcmp(*argv, "excl") == 0) { |
| 2680 | NEXT_ARG(); |
| 2681 | if (!saw_states) |
| 2682 | current_filter.states = SS_ALL; |
| 2683 | current_filter.states &= ~scan_state(*argv); |
| 2684 | saw_states = 1; |
| 2685 | } else { |
| 2686 | if (ssfilter_parse(¤t_filter.f, argc, argv, filter_fp)) |
| 2687 | usage(); |
| 2688 | break; |
| 2689 | } |
| 2690 | argc--; argv++; |
| 2691 | } |
| 2692 | |
| 2693 | if (current_filter.states == 0) { |
| 2694 | fprintf(stderr, "ss: no socket states to show with such filter.\n"); |
| 2695 | exit(0); |
| 2696 | } |
| 2697 | |
| 2698 | if (dump_tcpdiag) { |
| 2699 | FILE *dump_fp = stdout; |
| 2700 | if (!(current_filter.dbs & (1<<TCP_DB))) { |
| 2701 | fprintf(stderr, "ss: tcpdiag dump requested and no tcp in filter.\n"); |
| 2702 | exit(0); |
| 2703 | } |
| 2704 | if (dump_tcpdiag[0] != '-') { |
| 2705 | dump_fp = fopen(dump_tcpdiag, "w"); |
| 2706 | if (!dump_tcpdiag) { |
| 2707 | perror("fopen dump file"); |
| 2708 | exit(-1); |
| 2709 | } |
| 2710 | } |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 2711 | tcp_show_netlink(¤t_filter, dump_fp, TCPDIAG_GETSOCK); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2712 | fflush(dump_fp); |
| 2713 | exit(0); |
| 2714 | } |
| 2715 | |
| 2716 | netid_width = 0; |
| 2717 | if (current_filter.dbs&(current_filter.dbs-1)) |
| 2718 | netid_width = 5; |
| 2719 | |
| 2720 | state_width = 0; |
| 2721 | if (current_filter.states&(current_filter.states-1)) |
| 2722 | state_width = 10; |
| 2723 | |
| 2724 | screen_width = 80; |
| 2725 | if (isatty(STDOUT_FILENO)) { |
| 2726 | struct winsize w; |
| 2727 | |
| 2728 | if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) { |
| 2729 | if (w.ws_col > 0) |
| 2730 | screen_width = w.ws_col; |
| 2731 | } |
| 2732 | } |
| 2733 | |
| 2734 | addrp_width = screen_width; |
| 2735 | addrp_width -= netid_width+1; |
| 2736 | addrp_width -= state_width+1; |
| 2737 | addrp_width -= 14; |
| 2738 | |
| 2739 | if (addrp_width&1) { |
| 2740 | if (netid_width) |
| 2741 | netid_width++; |
| 2742 | else if (state_width) |
| 2743 | state_width++; |
| 2744 | } |
| 2745 | |
| 2746 | addrp_width /= 2; |
| 2747 | addrp_width--; |
| 2748 | |
| 2749 | serv_width = resolve_services ? 7 : 5; |
| 2750 | |
| 2751 | if (addrp_width < 15+serv_width+1) |
| 2752 | addrp_width = 15+serv_width+1; |
| 2753 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 2754 | addr_width = addrp_width - serv_width - 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2755 | |
| 2756 | if (netid_width) |
| 2757 | printf("%-*s ", netid_width, "Netid"); |
| 2758 | if (state_width) |
| 2759 | printf("%-*s ", state_width, "State"); |
| 2760 | printf("%-6s %-6s ", "Recv-Q", "Send-Q"); |
| 2761 | |
| 2762 | printf("%*s:%-*s %*s:%-*s\n", |
| 2763 | addr_width, "Local Address", serv_width, "Port", |
| 2764 | addr_width, "Peer Address", serv_width, "Port"); |
| 2765 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2766 | fflush(stdout); |
| 2767 | |
| 2768 | if (current_filter.dbs & (1<<NETLINK_DB)) |
| 2769 | netlink_show(¤t_filter); |
| 2770 | if (current_filter.dbs & PACKET_DBM) |
| 2771 | packet_show(¤t_filter); |
| 2772 | if (current_filter.dbs & UNIX_DBM) |
| 2773 | unix_show(¤t_filter); |
| 2774 | if (current_filter.dbs & (1<<RAW_DB)) |
| 2775 | raw_show(¤t_filter); |
| 2776 | if (current_filter.dbs & (1<<UDP_DB)) |
| 2777 | udp_show(¤t_filter); |
| 2778 | if (current_filter.dbs & (1<<TCP_DB)) |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 2779 | tcp_show(¤t_filter, TCPDIAG_GETSOCK); |
| 2780 | if (current_filter.dbs & (1<<DCCP_DB)) |
| 2781 | tcp_show(¤t_filter, DCCPDIAG_GETSOCK); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2782 | return 0; |
| 2783 | } |