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> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 25 | #include <dirent.h> |
| 26 | #include <fnmatch.h> |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 27 | #include <getopt.h> |
Vadim Kochan | bf4ceee | 2015-01-04 22:18:38 +0200 | [diff] [blame] | 28 | #include <stdbool.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" |
Vadim Kochan | 95ce04b | 2015-02-08 08:58:43 +0200 | [diff] [blame] | 34 | #include "namespace.h" |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 35 | #include "SNAPSHOT.h" |
| 36 | |
Eric Dumazet | 9cb1ecc | 2013-05-03 20:48:00 -0700 | [diff] [blame] | 37 | #include <linux/tcp.h> |
Stephen Hemminger | f606236 | 2012-01-20 12:48:00 -0800 | [diff] [blame] | 38 | #include <linux/sock_diag.h> |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 39 | #include <linux/inet_diag.h> |
Pavel Emelyanov | dfbaa90 | 2011-12-15 03:28:15 +0000 | [diff] [blame] | 40 | #include <linux/unix_diag.h> |
Nicolas Dichtel | 372c30d | 2013-05-17 08:42:34 -0700 | [diff] [blame] | 41 | #include <linux/netdevice.h> /* for MAX_ADDR_LEN */ |
| 42 | #include <linux/filter.h> |
| 43 | #include <linux/packet_diag.h> |
Andrey Vagin | ecb928c | 2013-06-05 12:42:01 +0400 | [diff] [blame] | 44 | #include <linux/netlink_diag.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 45 | |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 46 | #define MAGIC_SEQ 123456 |
| 47 | |
vadimk | 5fb421d | 2014-10-30 18:49:25 +0200 | [diff] [blame] | 48 | #define DIAG_REQUEST(_req, _r) \ |
| 49 | struct { \ |
| 50 | struct nlmsghdr nlh; \ |
| 51 | _r; \ |
| 52 | } _req = { \ |
| 53 | .nlh = { \ |
| 54 | .nlmsg_type = SOCK_DIAG_BY_FAMILY, \ |
| 55 | .nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST,\ |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 56 | .nlmsg_seq = MAGIC_SEQ, \ |
vadimk | 5fb421d | 2014-10-30 18:49:25 +0200 | [diff] [blame] | 57 | .nlmsg_len = sizeof(_req), \ |
| 58 | }, \ |
| 59 | } |
| 60 | |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 61 | #if HAVE_SELINUX |
| 62 | #include <selinux/selinux.h> |
| 63 | #else |
| 64 | /* Stubs for SELinux functions */ |
| 65 | static int is_selinux_enabled(void) |
| 66 | { |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | static int getpidcon(pid_t pid, char **context) |
| 71 | { |
| 72 | *context = NULL; |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | static int getfilecon(char *path, char **context) |
| 77 | { |
| 78 | *context = NULL; |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | static int security_get_initial_context(char *name, char **context) |
| 83 | { |
| 84 | *context = NULL; |
| 85 | return -1; |
| 86 | } |
| 87 | #endif |
| 88 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 89 | int resolve_hosts = 0; |
| 90 | int resolve_services = 1; |
| 91 | int preferred_family = AF_UNSPEC; |
| 92 | int show_options = 0; |
| 93 | int show_details = 0; |
| 94 | int show_users = 0; |
| 95 | int show_mem = 0; |
| 96 | int show_tcpinfo = 0; |
Nicolas Dichtel | 372c30d | 2013-05-17 08:42:34 -0700 | [diff] [blame] | 97 | int show_bpf = 0; |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 98 | int show_proc_ctx = 0; |
| 99 | int show_sock_ctx = 0; |
| 100 | /* If show_users & show_proc_ctx only do user_ent_hash_build() once */ |
| 101 | int user_ent_hash_build_init = 0; |
Craig Gallek | 6885e3b | 2015-06-17 11:14:48 -0400 | [diff] [blame] | 102 | int follow_events = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 103 | |
| 104 | int netid_width; |
| 105 | int state_width; |
| 106 | int addrp_width; |
| 107 | int addr_width; |
| 108 | int serv_width; |
| 109 | int screen_width; |
| 110 | |
| 111 | static const char *TCP_PROTO = "tcp"; |
| 112 | static const char *UDP_PROTO = "udp"; |
| 113 | static const char *RAW_PROTO = "raw"; |
| 114 | static const char *dg_proto = NULL; |
| 115 | |
| 116 | enum |
| 117 | { |
| 118 | TCP_DB, |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 119 | DCCP_DB, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 120 | UDP_DB, |
| 121 | RAW_DB, |
| 122 | UNIX_DG_DB, |
| 123 | UNIX_ST_DB, |
Masatake YAMATO | 30b669d | 2014-01-08 20:13:46 +0900 | [diff] [blame] | 124 | UNIX_SQ_DB, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 125 | PACKET_DG_DB, |
| 126 | PACKET_R_DB, |
| 127 | NETLINK_DB, |
| 128 | MAX_DB |
| 129 | }; |
| 130 | |
| 131 | #define PACKET_DBM ((1<<PACKET_DG_DB)|(1<<PACKET_R_DB)) |
Masatake YAMATO | 30b669d | 2014-01-08 20:13:46 +0900 | [diff] [blame] | 132 | #define UNIX_DBM ((1<<UNIX_DG_DB)|(1<<UNIX_ST_DB)|(1<<UNIX_SQ_DB)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 133 | #define ALL_DB ((1<<MAX_DB)-1) |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 134 | #define INET_DBM ((1<<TCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB)|(1<<RAW_DB)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 135 | |
| 136 | enum { |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 137 | SS_UNKNOWN, |
| 138 | SS_ESTABLISHED, |
| 139 | SS_SYN_SENT, |
| 140 | SS_SYN_RECV, |
| 141 | SS_FIN_WAIT1, |
| 142 | SS_FIN_WAIT2, |
| 143 | SS_TIME_WAIT, |
| 144 | SS_CLOSE, |
| 145 | SS_CLOSE_WAIT, |
| 146 | SS_LAST_ACK, |
| 147 | SS_LISTEN, |
| 148 | SS_CLOSING, |
| 149 | SS_MAX |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 152 | #define SS_ALL ((1 << SS_MAX) - 1) |
| 153 | #define SS_CONN (SS_ALL & ~((1<<SS_LISTEN)|(1<<SS_CLOSE)|(1<<SS_TIME_WAIT)|(1<<SS_SYN_RECV))) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 154 | |
| 155 | #include "ssfilter.h" |
| 156 | |
| 157 | struct filter |
| 158 | { |
| 159 | int dbs; |
| 160 | int states; |
| 161 | int families; |
| 162 | struct ssfilter *f; |
| 163 | }; |
| 164 | |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 165 | static const struct filter default_dbs[MAX_DB] = { |
| 166 | [TCP_DB] = { |
| 167 | .states = SS_CONN, |
| 168 | .families = (1 << AF_INET) | (1 << AF_INET6), |
| 169 | }, |
| 170 | [DCCP_DB] = { |
| 171 | .states = SS_CONN, |
| 172 | .families = (1 << AF_INET) | (1 << AF_INET6), |
| 173 | }, |
| 174 | [UDP_DB] = { |
Vadim Kochan | f42a457 | 2015-01-08 19:32:22 +0200 | [diff] [blame] | 175 | .states = (1 << SS_ESTABLISHED), |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 176 | .families = (1 << AF_INET) | (1 << AF_INET6), |
| 177 | }, |
| 178 | [RAW_DB] = { |
Vadim Kochan | f42a457 | 2015-01-08 19:32:22 +0200 | [diff] [blame] | 179 | .states = (1 << SS_ESTABLISHED), |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 180 | .families = (1 << AF_INET) | (1 << AF_INET6), |
| 181 | }, |
| 182 | [UNIX_DG_DB] = { |
| 183 | .states = (1 << SS_CLOSE), |
| 184 | .families = (1 << AF_UNIX), |
| 185 | }, |
| 186 | [UNIX_ST_DB] = { |
| 187 | .states = SS_CONN, |
| 188 | .families = (1 << AF_UNIX), |
| 189 | }, |
| 190 | [UNIX_SQ_DB] = { |
| 191 | .states = SS_CONN, |
| 192 | .families = (1 << AF_UNIX), |
| 193 | }, |
| 194 | [PACKET_DG_DB] = { |
| 195 | .states = (1 << SS_CLOSE), |
| 196 | .families = (1 << AF_PACKET), |
| 197 | }, |
| 198 | [PACKET_R_DB] = { |
| 199 | .states = (1 << SS_CLOSE), |
| 200 | .families = (1 << AF_PACKET), |
| 201 | }, |
| 202 | [NETLINK_DB] = { |
| 203 | .states = (1 << SS_CLOSE), |
| 204 | .families = (1 << AF_NETLINK), |
| 205 | }, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 208 | static const struct filter default_afs[AF_MAX] = { |
| 209 | [AF_INET] = { |
| 210 | .dbs = INET_DBM, |
| 211 | .states = SS_CONN, |
| 212 | }, |
| 213 | [AF_INET6] = { |
| 214 | .dbs = INET_DBM, |
| 215 | .states = SS_CONN, |
| 216 | }, |
| 217 | [AF_UNIX] = { |
| 218 | .dbs = UNIX_DBM, |
| 219 | .states = SS_CONN, |
| 220 | }, |
| 221 | [AF_PACKET] = { |
| 222 | .dbs = PACKET_DBM, |
| 223 | .states = (1 << SS_CLOSE), |
| 224 | }, |
| 225 | [AF_NETLINK] = { |
| 226 | .dbs = (1 << NETLINK_DB), |
| 227 | .states = (1 << SS_CLOSE), |
| 228 | }, |
| 229 | }; |
| 230 | |
| 231 | static int do_default = 1; |
| 232 | static struct filter current_filter; |
| 233 | |
| 234 | static void filter_db_set(struct filter *f, int db) |
| 235 | { |
| 236 | f->states |= default_dbs[db].states; |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 237 | f->dbs |= 1 << db; |
| 238 | do_default = 0; |
| 239 | } |
| 240 | |
| 241 | static void filter_af_set(struct filter *f, int af) |
| 242 | { |
Vadim Kochan | 1527a17 | 2015-02-13 13:01:08 +0200 | [diff] [blame] | 243 | f->states |= default_afs[af].states; |
| 244 | f->families |= 1 << af; |
| 245 | do_default = 0; |
| 246 | preferred_family = af; |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static int filter_af_get(struct filter *f, int af) |
| 250 | { |
| 251 | return f->families & (1 << af); |
| 252 | } |
| 253 | |
| 254 | static void filter_default_dbs(struct filter *f) |
| 255 | { |
| 256 | filter_db_set(f, UDP_DB); |
| 257 | filter_db_set(f, DCCP_DB); |
| 258 | filter_db_set(f, TCP_DB); |
| 259 | filter_db_set(f, RAW_DB); |
| 260 | filter_db_set(f, UNIX_ST_DB); |
| 261 | filter_db_set(f, UNIX_DG_DB); |
| 262 | filter_db_set(f, UNIX_SQ_DB); |
| 263 | filter_db_set(f, PACKET_R_DB); |
| 264 | filter_db_set(f, PACKET_DG_DB); |
| 265 | filter_db_set(f, NETLINK_DB); |
| 266 | } |
| 267 | |
Vadim Kochan | 57ff5a1 | 2015-04-30 07:30:24 +0300 | [diff] [blame] | 268 | static void filter_states_set(struct filter *f, int states) |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 269 | { |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 270 | if (states) |
Vadim Kochan | 57ff5a1 | 2015-04-30 07:30:24 +0300 | [diff] [blame] | 271 | f->states = (f->states | states) & states; |
| 272 | } |
| 273 | |
| 274 | static void filter_merge_defaults(struct filter *f) |
| 275 | { |
| 276 | int db; |
| 277 | int af; |
| 278 | |
| 279 | for (db = 0; db < MAX_DB; db++) { |
| 280 | if (!(f->dbs & (1 << db))) |
| 281 | continue; |
| 282 | |
| 283 | if (!(default_dbs[db].families & f->families)) |
| 284 | f->families |= default_dbs[db].families; |
| 285 | } |
| 286 | for (af = 0; af < AF_MAX; af++) { |
| 287 | if (!(f->families & (1 << af))) |
| 288 | continue; |
| 289 | |
| 290 | if (!(default_afs[af].dbs & f->dbs)) |
| 291 | f->dbs |= default_afs[af].dbs; |
| 292 | } |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 293 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 294 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 295 | static FILE *generic_proc_open(const char *env, const char *name) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 296 | { |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 297 | const char *p = getenv(env); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 298 | char store[128]; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 299 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 300 | if (!p) { |
| 301 | p = getenv("PROC_ROOT") ? : "/proc"; |
| 302 | snprintf(store, sizeof(store)-1, "%s/%s", p, name); |
| 303 | p = store; |
| 304 | } |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 305 | |
| 306 | return fopen(p, "r"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 309 | static FILE *net_tcp_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 310 | { |
| 311 | return generic_proc_open("PROC_NET_TCP", "net/tcp"); |
| 312 | } |
| 313 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 314 | static FILE *net_tcp6_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 315 | { |
| 316 | return generic_proc_open("PROC_NET_TCP6", "net/tcp6"); |
| 317 | } |
| 318 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 319 | static FILE *net_udp_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 320 | { |
| 321 | return generic_proc_open("PROC_NET_UDP", "net/udp"); |
| 322 | } |
| 323 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 324 | static FILE *net_udp6_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 325 | { |
| 326 | return generic_proc_open("PROC_NET_UDP6", "net/udp6"); |
| 327 | } |
| 328 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 329 | static FILE *net_raw_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 330 | { |
| 331 | return generic_proc_open("PROC_NET_RAW", "net/raw"); |
| 332 | } |
| 333 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 334 | static FILE *net_raw6_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 335 | { |
| 336 | return generic_proc_open("PROC_NET_RAW6", "net/raw6"); |
| 337 | } |
| 338 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 339 | static FILE *net_unix_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 340 | { |
| 341 | return generic_proc_open("PROC_NET_UNIX", "net/unix"); |
| 342 | } |
| 343 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 344 | static FILE *net_packet_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 345 | { |
| 346 | return generic_proc_open("PROC_NET_PACKET", "net/packet"); |
| 347 | } |
| 348 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 349 | static FILE *net_netlink_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 350 | { |
| 351 | return generic_proc_open("PROC_NET_NETLINK", "net/netlink"); |
| 352 | } |
| 353 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 354 | static FILE *slabinfo_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 355 | { |
| 356 | return generic_proc_open("PROC_SLABINFO", "slabinfo"); |
| 357 | } |
| 358 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 359 | static FILE *net_sockstat_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 360 | { |
| 361 | return generic_proc_open("PROC_NET_SOCKSTAT", "net/sockstat"); |
| 362 | } |
| 363 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 364 | static FILE *net_sockstat6_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 365 | { |
| 366 | return generic_proc_open("PROC_NET_SOCKSTAT6", "net/sockstat6"); |
| 367 | } |
| 368 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 369 | static FILE *net_snmp_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 370 | { |
| 371 | return generic_proc_open("PROC_NET_SNMP", "net/snmp"); |
| 372 | } |
| 373 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 374 | static FILE *ephemeral_ports_open(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 375 | { |
| 376 | return generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", "sys/net/ipv4/ip_local_port_range"); |
| 377 | } |
| 378 | |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 379 | struct user_ent { |
| 380 | struct user_ent *next; |
| 381 | unsigned int ino; |
| 382 | int pid; |
| 383 | int fd; |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 384 | char *process; |
| 385 | char *process_ctx; |
| 386 | char *socket_ctx; |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 387 | }; |
| 388 | |
| 389 | #define USER_ENT_HASH_SIZE 256 |
| 390 | struct user_ent *user_ent_hash[USER_ENT_HASH_SIZE]; |
| 391 | |
| 392 | static int user_ent_hashfn(unsigned int ino) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 393 | { |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 394 | int val = (ino >> 24) ^ (ino >> 16) ^ (ino >> 8) ^ ino; |
| 395 | |
| 396 | return val & (USER_ENT_HASH_SIZE - 1); |
| 397 | } |
| 398 | |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 399 | static void user_ent_add(unsigned int ino, char *process, |
| 400 | int pid, int fd, |
| 401 | char *proc_ctx, |
| 402 | char *sock_ctx) |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 403 | { |
| 404 | struct user_ent *p, **pp; |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 405 | |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 406 | p = malloc(sizeof(struct user_ent)); |
| 407 | if (!p) { |
| 408 | fprintf(stderr, "ss: failed to malloc buffer\n"); |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 409 | abort(); |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 410 | } |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 411 | p->next = NULL; |
| 412 | p->ino = ino; |
| 413 | p->pid = pid; |
| 414 | p->fd = fd; |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 415 | p->process = strdup(process); |
| 416 | p->process_ctx = strdup(proc_ctx); |
| 417 | p->socket_ctx = strdup(sock_ctx); |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 418 | |
| 419 | pp = &user_ent_hash[user_ent_hashfn(ino)]; |
| 420 | p->next = *pp; |
| 421 | *pp = p; |
| 422 | } |
| 423 | |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 424 | static void user_ent_destroy(void) |
| 425 | { |
| 426 | struct user_ent *p, *p_next; |
| 427 | int cnt = 0; |
| 428 | |
| 429 | while (cnt != USER_ENT_HASH_SIZE) { |
| 430 | p = user_ent_hash[cnt]; |
| 431 | while (p) { |
| 432 | free(p->process); |
| 433 | free(p->process_ctx); |
| 434 | free(p->socket_ctx); |
| 435 | p_next = p->next; |
| 436 | free(p); |
| 437 | p = p_next; |
| 438 | } |
| 439 | cnt++; |
| 440 | } |
| 441 | } |
| 442 | |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 443 | static void user_ent_hash_build(void) |
| 444 | { |
| 445 | const char *root = getenv("PROC_ROOT") ? : "/proc/"; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 446 | struct dirent *d; |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 447 | char name[1024]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 448 | int nameoff; |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 449 | DIR *dir; |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 450 | char *pid_context; |
| 451 | char *sock_context; |
| 452 | const char *no_ctx = "unavailable"; |
| 453 | |
| 454 | /* If show_users & show_proc_ctx set only do this once */ |
| 455 | if (user_ent_hash_build_init != 0) |
| 456 | return; |
| 457 | |
| 458 | user_ent_hash_build_init = 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 459 | |
willy tarreau | 0ee9052 | 2015-10-06 12:09:33 +0200 | [diff] [blame] | 460 | strncpy(name, root, sizeof(name)-1); |
| 461 | name[sizeof(name)-1] = 0; |
| 462 | |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 463 | if (strlen(name) == 0 || name[strlen(name)-1] != '/') |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 464 | strcat(name, "/"); |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 465 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 466 | nameoff = strlen(name); |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 467 | |
| 468 | dir = opendir(name); |
| 469 | if (!dir) |
| 470 | return; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 471 | |
| 472 | while ((d = readdir(dir)) != NULL) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 473 | struct dirent *d1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 474 | char process[16]; |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 475 | char *p; |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 476 | int pid, pos; |
| 477 | DIR *dir1; |
| 478 | char crap; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 479 | |
| 480 | if (sscanf(d->d_name, "%d%c", &pid, &crap) != 1) |
| 481 | continue; |
| 482 | |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 483 | if (getpidcon(pid, &pid_context) != 0) |
| 484 | pid_context = strdup(no_ctx); |
| 485 | |
willy tarreau | 0ee9052 | 2015-10-06 12:09:33 +0200 | [diff] [blame] | 486 | snprintf(name + nameoff, sizeof(name) - nameoff, "%d/fd/", pid); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 487 | pos = strlen(name); |
Phil Sutter | a02371f | 2015-08-06 14:24:36 +0200 | [diff] [blame] | 488 | if ((dir1 = opendir(name)) == NULL) { |
| 489 | free(pid_context); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 490 | continue; |
Phil Sutter | a02371f | 2015-08-06 14:24:36 +0200 | [diff] [blame] | 491 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 492 | |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 493 | process[0] = '\0'; |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 494 | p = process; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 495 | |
| 496 | while ((d1 = readdir(dir1)) != NULL) { |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 497 | const char *pattern = "socket:["; |
| 498 | unsigned int ino; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 499 | char lnk[64]; |
Stephen Hemminger | 18445b3 | 2011-06-29 15:58:37 -0700 | [diff] [blame] | 500 | int fd; |
Thomas Jarosch | 788731b | 2011-10-13 10:30:21 +0200 | [diff] [blame] | 501 | ssize_t link_len; |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 502 | char tmp[1024]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 503 | |
| 504 | if (sscanf(d1->d_name, "%d%c", &fd, &crap) != 1) |
| 505 | continue; |
| 506 | |
willy tarreau | 0ee9052 | 2015-10-06 12:09:33 +0200 | [diff] [blame] | 507 | snprintf(name+pos, sizeof(name) - pos, "%d", fd); |
Thomas Jarosch | 788731b | 2011-10-13 10:30:21 +0200 | [diff] [blame] | 508 | |
| 509 | link_len = readlink(name, lnk, sizeof(lnk)-1); |
| 510 | if (link_len == -1) |
| 511 | continue; |
| 512 | lnk[link_len] = '\0'; |
| 513 | |
| 514 | if (strncmp(lnk, pattern, strlen(pattern))) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 515 | continue; |
| 516 | |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 517 | sscanf(lnk, "socket:[%u]", &ino); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 518 | |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 519 | snprintf(tmp, sizeof(tmp), "%s/%d/fd/%s", |
| 520 | root, pid, d1->d_name); |
| 521 | |
| 522 | if (getfilecon(tmp, &sock_context) <= 0) |
| 523 | sock_context = strdup(no_ctx); |
| 524 | |
| 525 | if (*p == '\0') { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 526 | FILE *fp; |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 527 | |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 528 | snprintf(tmp, sizeof(tmp), "%s/%d/stat", |
| 529 | root, pid); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 530 | if ((fp = fopen(tmp, "r")) != NULL) { |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 531 | fscanf(fp, "%*d (%[^)])", p); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 532 | fclose(fp); |
| 533 | } |
| 534 | } |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 535 | user_ent_add(ino, p, pid, fd, |
| 536 | pid_context, sock_context); |
| 537 | free(sock_context); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 538 | } |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 539 | free(pid_context); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 540 | closedir(dir1); |
| 541 | } |
| 542 | closedir(dir); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 545 | enum entry_types { |
| 546 | USERS, |
| 547 | PROC_CTX, |
| 548 | PROC_SOCK_CTX |
| 549 | }; |
| 550 | |
| 551 | #define ENTRY_BUF_SIZE 512 |
| 552 | static int find_entry(unsigned ino, char **buf, int type) |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 553 | { |
| 554 | struct user_ent *p; |
| 555 | int cnt = 0; |
| 556 | char *ptr; |
Phil Sutter | 532ca40 | 2015-08-06 14:24:33 +0200 | [diff] [blame] | 557 | char *new_buf; |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 558 | int len, new_buf_len; |
| 559 | int buf_used = 0; |
| 560 | int buf_len = 0; |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 561 | |
| 562 | if (!ino) |
| 563 | return 0; |
| 564 | |
| 565 | p = user_ent_hash[user_ent_hashfn(ino)]; |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 566 | ptr = *buf = NULL; |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 567 | while (p) { |
| 568 | if (p->ino != ino) |
| 569 | goto next; |
| 570 | |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 571 | while (1) { |
| 572 | ptr = *buf + buf_used; |
| 573 | switch (type) { |
| 574 | case USERS: |
| 575 | len = snprintf(ptr, buf_len - buf_used, |
| 576 | "(\"%s\",pid=%d,fd=%d),", |
| 577 | p->process, p->pid, p->fd); |
| 578 | break; |
| 579 | case PROC_CTX: |
| 580 | len = snprintf(ptr, buf_len - buf_used, |
| 581 | "(\"%s\",pid=%d,proc_ctx=%s,fd=%d),", |
| 582 | p->process, p->pid, |
| 583 | p->process_ctx, p->fd); |
| 584 | break; |
| 585 | case PROC_SOCK_CTX: |
| 586 | len = snprintf(ptr, buf_len - buf_used, |
| 587 | "(\"%s\",pid=%d,proc_ctx=%s,fd=%d,sock_ctx=%s),", |
| 588 | p->process, p->pid, |
| 589 | p->process_ctx, p->fd, |
| 590 | p->socket_ctx); |
| 591 | break; |
| 592 | default: |
| 593 | fprintf(stderr, "ss: invalid type: %d\n", type); |
| 594 | abort(); |
| 595 | } |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 596 | |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 597 | if (len < 0 || len >= buf_len - buf_used) { |
| 598 | new_buf_len = buf_len + ENTRY_BUF_SIZE; |
Phil Sutter | 532ca40 | 2015-08-06 14:24:33 +0200 | [diff] [blame] | 599 | new_buf = realloc(*buf, new_buf_len); |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 600 | if (!new_buf) { |
| 601 | fprintf(stderr, "ss: failed to malloc buffer\n"); |
| 602 | abort(); |
| 603 | } |
Phil Sutter | 532ca40 | 2015-08-06 14:24:33 +0200 | [diff] [blame] | 604 | *buf = new_buf; |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 605 | buf_len = new_buf_len; |
| 606 | continue; |
| 607 | } else { |
| 608 | buf_used += len; |
| 609 | break; |
| 610 | } |
| 611 | } |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 612 | cnt++; |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 613 | next: |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 614 | p = p->next; |
| 615 | } |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 616 | if (buf_used) { |
| 617 | ptr = *buf + buf_used; |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 618 | ptr[-1] = '\0'; |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 619 | } |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 620 | return cnt; |
| 621 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 622 | |
| 623 | /* Get stats from slab */ |
| 624 | |
| 625 | struct slabstat |
| 626 | { |
| 627 | int socks; |
| 628 | int tcp_ports; |
| 629 | int tcp_tws; |
| 630 | int tcp_syns; |
| 631 | int skbs; |
| 632 | }; |
| 633 | |
Bryton Lee | a221d62 | 2015-02-12 14:16:04 +0800 | [diff] [blame] | 634 | static struct slabstat slabstat; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 635 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 636 | static const char *slabstat_ids[] = |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 637 | { |
| 638 | "sock", |
| 639 | "tcp_bind_bucket", |
| 640 | "tcp_tw_bucket", |
| 641 | "tcp_open_request", |
| 642 | "skbuff_head_cache", |
| 643 | }; |
| 644 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 645 | static int get_slabstat(struct slabstat *s) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 646 | { |
| 647 | char buf[256]; |
| 648 | FILE *fp; |
| 649 | int cnt; |
Bryton Lee | a221d62 | 2015-02-12 14:16:04 +0800 | [diff] [blame] | 650 | static int slabstat_valid; |
| 651 | |
| 652 | if (slabstat_valid) |
| 653 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 654 | |
| 655 | memset(s, 0, sizeof(*s)); |
| 656 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 657 | fp = slabinfo_open(); |
| 658 | if (!fp) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 659 | return -1; |
| 660 | |
| 661 | cnt = sizeof(*s)/sizeof(int); |
| 662 | |
| 663 | fgets(buf, sizeof(buf), fp); |
| 664 | while(fgets(buf, sizeof(buf), fp) != NULL) { |
| 665 | int i; |
| 666 | for (i=0; i<sizeof(slabstat_ids)/sizeof(slabstat_ids[0]); i++) { |
| 667 | if (memcmp(buf, slabstat_ids[i], strlen(slabstat_ids[i])) == 0) { |
| 668 | sscanf(buf, "%*s%d", ((int *)s) + i); |
| 669 | cnt--; |
| 670 | break; |
| 671 | } |
| 672 | } |
| 673 | if (cnt <= 0) |
| 674 | break; |
| 675 | } |
| 676 | |
Bryton Lee | a221d62 | 2015-02-12 14:16:04 +0800 | [diff] [blame] | 677 | slabstat_valid = 1; |
| 678 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 679 | fclose(fp); |
| 680 | return 0; |
| 681 | } |
| 682 | |
Eric Dumazet | 2e7e805 | 2015-03-10 09:56:47 -0700 | [diff] [blame] | 683 | static unsigned long long cookie_sk_get(const uint32_t *cookie) |
Vadim Kochan | f1b39e1 | 2015-02-13 22:14:02 +0200 | [diff] [blame] | 684 | { |
Eric Dumazet | 2e7e805 | 2015-03-10 09:56:47 -0700 | [diff] [blame] | 685 | return (((unsigned long long)cookie[1] << 31) << 1) | cookie[0]; |
Vadim Kochan | f1b39e1 | 2015-02-13 22:14:02 +0200 | [diff] [blame] | 686 | } |
| 687 | |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 688 | static const char *sstate_name[] = { |
| 689 | "UNKNOWN", |
Eric Dumazet | 9cb1ecc | 2013-05-03 20:48:00 -0700 | [diff] [blame] | 690 | [SS_ESTABLISHED] = "ESTAB", |
| 691 | [SS_SYN_SENT] = "SYN-SENT", |
| 692 | [SS_SYN_RECV] = "SYN-RECV", |
| 693 | [SS_FIN_WAIT1] = "FIN-WAIT-1", |
| 694 | [SS_FIN_WAIT2] = "FIN-WAIT-2", |
| 695 | [SS_TIME_WAIT] = "TIME-WAIT", |
| 696 | [SS_CLOSE] = "UNCONN", |
| 697 | [SS_CLOSE_WAIT] = "CLOSE-WAIT", |
| 698 | [SS_LAST_ACK] = "LAST-ACK", |
| 699 | [SS_LISTEN] = "LISTEN", |
| 700 | [SS_CLOSING] = "CLOSING", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 701 | }; |
| 702 | |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 703 | static const char *sstate_namel[] = { |
| 704 | "UNKNOWN", |
Eric Dumazet | 9cb1ecc | 2013-05-03 20:48:00 -0700 | [diff] [blame] | 705 | [SS_ESTABLISHED] = "established", |
| 706 | [SS_SYN_SENT] = "syn-sent", |
| 707 | [SS_SYN_RECV] = "syn-recv", |
| 708 | [SS_FIN_WAIT1] = "fin-wait-1", |
| 709 | [SS_FIN_WAIT2] = "fin-wait-2", |
| 710 | [SS_TIME_WAIT] = "time-wait", |
| 711 | [SS_CLOSE] = "unconnected", |
| 712 | [SS_CLOSE_WAIT] = "close-wait", |
| 713 | [SS_LAST_ACK] = "last-ack", |
| 714 | [SS_LISTEN] = "listening", |
| 715 | [SS_CLOSING] = "closing", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 716 | }; |
| 717 | |
Vadim Kochan | 055840f | 2015-02-13 22:13:58 +0200 | [diff] [blame] | 718 | struct sockstat |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 719 | { |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 720 | struct sockstat *next; |
| 721 | unsigned int type; |
Vadim Kochan | 89f634f | 2015-02-13 22:13:59 +0200 | [diff] [blame] | 722 | uint16_t prot; |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 723 | inet_prefix local; |
| 724 | inet_prefix remote; |
| 725 | int lport; |
| 726 | int rport; |
| 727 | int state; |
| 728 | int rq, wq; |
| 729 | unsigned ino; |
| 730 | unsigned uid; |
| 731 | int refcnt; |
| 732 | unsigned int iface; |
| 733 | unsigned long long sk; |
Vadim Kochan | 99bb68f | 2015-07-21 10:53:19 -0700 | [diff] [blame] | 734 | char *name; |
| 735 | char *peer_name; |
Vadim Kochan | 055840f | 2015-02-13 22:13:58 +0200 | [diff] [blame] | 736 | }; |
| 737 | |
| 738 | struct dctcpstat |
| 739 | { |
| 740 | unsigned int ce_state; |
| 741 | unsigned int alpha; |
| 742 | unsigned int ab_ecn; |
| 743 | unsigned int ab_tot; |
| 744 | bool enabled; |
| 745 | }; |
| 746 | |
| 747 | struct tcpstat |
| 748 | { |
| 749 | struct sockstat ss; |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 750 | int timer; |
| 751 | int timeout; |
| 752 | int probes; |
Eric Dumazet | d2055ea | 2015-05-29 04:04:05 -0700 | [diff] [blame] | 753 | char cong_alg[16]; |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 754 | double rto, ato, rtt, rttvar; |
| 755 | int qack, cwnd, ssthresh, backoff; |
| 756 | double send_bps; |
| 757 | int snd_wscale; |
| 758 | int rcv_wscale; |
| 759 | int mss; |
| 760 | unsigned int lastsnd; |
| 761 | unsigned int lastrcv; |
| 762 | unsigned int lastack; |
| 763 | double pacing_rate; |
| 764 | double pacing_rate_max; |
Eric Dumazet | 1a4dda7 | 2015-05-11 10:03:49 -0700 | [diff] [blame] | 765 | unsigned long long bytes_acked; |
| 766 | unsigned long long bytes_received; |
Craig Gallek | ecb435e | 2015-05-26 14:54:41 -0400 | [diff] [blame] | 767 | unsigned int segs_out; |
| 768 | unsigned int segs_in; |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 769 | unsigned int unacked; |
| 770 | unsigned int retrans; |
| 771 | unsigned int retrans_total; |
| 772 | unsigned int lost; |
| 773 | unsigned int sacked; |
| 774 | unsigned int fackets; |
| 775 | unsigned int reordering; |
| 776 | double rcv_rtt; |
| 777 | int rcv_space; |
| 778 | bool has_ts_opt; |
| 779 | bool has_sack_opt; |
| 780 | bool has_ecn_opt; |
| 781 | bool has_ecnseen_opt; |
| 782 | bool has_fastopen_opt; |
| 783 | bool has_wscale_opt; |
| 784 | struct dctcpstat *dctcp; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 785 | }; |
| 786 | |
Vadim Kochan | 2d791bc | 2015-02-13 22:14:01 +0200 | [diff] [blame] | 787 | static void sock_state_print(struct sockstat *s, const char *sock_name) |
| 788 | { |
| 789 | if (netid_width) |
| 790 | printf("%-*s ", netid_width, sock_name); |
| 791 | if (state_width) |
| 792 | printf("%-*s ", state_width, sstate_name[s->state]); |
| 793 | |
| 794 | printf("%-6d %-6d ", s->rq, s->wq); |
| 795 | } |
| 796 | |
Vadim Kochan | f1b39e1 | 2015-02-13 22:14:02 +0200 | [diff] [blame] | 797 | static void sock_details_print(struct sockstat *s) |
| 798 | { |
| 799 | if (s->uid) |
| 800 | printf(" uid:%u", s->uid); |
| 801 | |
| 802 | printf(" ino:%u", s->ino); |
| 803 | printf(" sk:%llx", s->sk); |
| 804 | } |
| 805 | |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 806 | static void sock_addr_print_width(int addr_len, const char *addr, char *delim, |
| 807 | int port_len, const char *port, const char *ifname) |
| 808 | { |
| 809 | if (ifname) { |
| 810 | printf("%*s%%%s%s%-*s ", addr_len, addr, ifname, delim, |
| 811 | port_len, port); |
| 812 | } |
| 813 | else { |
| 814 | printf("%*s%s%-*s ", addr_len, addr, delim, port_len, port); |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | static void sock_addr_print(const char *addr, char *delim, const char *port, |
| 819 | const char *ifname) |
| 820 | { |
| 821 | sock_addr_print_width(addr_width, addr, delim, serv_width, port, ifname); |
| 822 | } |
| 823 | |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 824 | static const char *tmr_name[] = { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 825 | "off", |
| 826 | "on", |
| 827 | "keepalive", |
| 828 | "timewait", |
| 829 | "persist", |
| 830 | "unknown" |
| 831 | }; |
| 832 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 833 | static const char *print_ms_timer(int timeout) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 834 | { |
| 835 | static char buf[64]; |
| 836 | int secs, msecs, minutes; |
| 837 | if (timeout < 0) |
| 838 | timeout = 0; |
| 839 | secs = timeout/1000; |
| 840 | minutes = secs/60; |
| 841 | secs = secs%60; |
| 842 | msecs = timeout%1000; |
| 843 | buf[0] = 0; |
| 844 | if (minutes) { |
| 845 | msecs = 0; |
| 846 | snprintf(buf, sizeof(buf)-16, "%dmin", minutes); |
| 847 | if (minutes > 9) |
| 848 | secs = 0; |
| 849 | } |
| 850 | if (secs) { |
| 851 | if (secs > 9) |
| 852 | msecs = 0; |
| 853 | sprintf(buf+strlen(buf), "%d%s", secs, msecs ? "." : "sec"); |
| 854 | } |
| 855 | if (msecs) |
| 856 | sprintf(buf+strlen(buf), "%03dms", msecs); |
| 857 | return buf; |
Stephen Hemminger | e7113c6 | 2007-07-10 18:26:54 -0700 | [diff] [blame] | 858 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 859 | |
Eric Dumazet | 22588a0 | 2015-05-29 04:45:48 -0700 | [diff] [blame] | 860 | struct scache { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 861 | struct scache *next; |
| 862 | int port; |
| 863 | char *name; |
| 864 | const char *proto; |
| 865 | }; |
| 866 | |
| 867 | struct scache *rlist; |
| 868 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 869 | static void init_service_resolver(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 870 | { |
| 871 | char buf[128]; |
| 872 | FILE *fp = popen("/usr/sbin/rpcinfo -p 2>/dev/null", "r"); |
| 873 | if (fp) { |
| 874 | fgets(buf, sizeof(buf), fp); |
| 875 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 876 | unsigned int progn, port; |
| 877 | char proto[128], prog[128]; |
| 878 | if (sscanf(buf, "%u %*d %s %u %s", &progn, proto, |
| 879 | &port, prog+4) == 4) { |
| 880 | struct scache *c = malloc(sizeof(*c)); |
| 881 | if (c) { |
| 882 | c->port = port; |
| 883 | memcpy(prog, "rpc.", 4); |
| 884 | c->name = strdup(prog); |
| 885 | if (strcmp(proto, TCP_PROTO) == 0) |
| 886 | c->proto = TCP_PROTO; |
| 887 | else if (strcmp(proto, UDP_PROTO) == 0) |
| 888 | c->proto = UDP_PROTO; |
| 889 | else |
| 890 | c->proto = NULL; |
| 891 | c->next = rlist; |
| 892 | rlist = c; |
| 893 | } |
| 894 | } |
| 895 | } |
Thomas Jarosch | 2bcc3c1 | 2011-10-03 05:22:27 +0000 | [diff] [blame] | 896 | pclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 900 | static int ip_local_port_min, ip_local_port_max; |
| 901 | |
| 902 | /* Even do not try default linux ephemeral port ranges: |
| 903 | * default /etc/services contains so much of useless crap |
| 904 | * wouldbe "allocated" to this area that resolution |
| 905 | * is really harmful. I shrug each time when seeing |
| 906 | * "socks" or "cfinger" in dumps. |
| 907 | */ |
| 908 | static int is_ephemeral(int port) |
| 909 | { |
| 910 | if (!ip_local_port_min) { |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 911 | FILE *f = ephemeral_ports_open(); |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 912 | if (f) { |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 913 | fscanf(f, "%d %d", |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 914 | &ip_local_port_min, &ip_local_port_max); |
| 915 | fclose(f); |
| 916 | } else { |
| 917 | ip_local_port_min = 1024; |
| 918 | ip_local_port_max = 4999; |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | return (port >= ip_local_port_min && port<= ip_local_port_max); |
| 923 | } |
| 924 | |
| 925 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 926 | static const char *__resolve_service(int port) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 927 | { |
| 928 | struct scache *c; |
| 929 | |
| 930 | for (c = rlist; c; c = c->next) { |
| 931 | if (c->port == port && c->proto == dg_proto) |
| 932 | return c->name; |
| 933 | } |
| 934 | |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 935 | if (!is_ephemeral(port)) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 936 | static int notfirst; |
| 937 | struct servent *se; |
| 938 | if (!notfirst) { |
| 939 | setservent(1); |
| 940 | notfirst = 1; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 941 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 942 | se = getservbyport(htons(port), dg_proto); |
| 943 | if (se) |
| 944 | return se->s_name; |
| 945 | } |
| 946 | |
| 947 | return NULL; |
| 948 | } |
| 949 | |
Eric Dumazet | 22588a0 | 2015-05-29 04:45:48 -0700 | [diff] [blame] | 950 | #define SCACHE_BUCKETS 1024 |
| 951 | static struct scache *cache_htab[SCACHE_BUCKETS]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 952 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 953 | static const char *resolve_service(int port) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 954 | { |
| 955 | static char buf[128]; |
Eric Dumazet | 22588a0 | 2015-05-29 04:45:48 -0700 | [diff] [blame] | 956 | struct scache *c; |
| 957 | const char *res; |
| 958 | int hash; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 959 | |
| 960 | if (port == 0) { |
| 961 | buf[0] = '*'; |
| 962 | buf[1] = 0; |
| 963 | return buf; |
| 964 | } |
| 965 | |
Eric Dumazet | 22588a0 | 2015-05-29 04:45:48 -0700 | [diff] [blame] | 966 | if (!resolve_services) |
| 967 | goto do_numeric; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 968 | |
Eric Dumazet | 22588a0 | 2015-05-29 04:45:48 -0700 | [diff] [blame] | 969 | if (dg_proto == RAW_PROTO) |
| 970 | return inet_proto_n2a(port, buf, sizeof(buf)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 971 | |
Eric Dumazet | 22588a0 | 2015-05-29 04:45:48 -0700 | [diff] [blame] | 972 | |
| 973 | hash = (port^(((unsigned long)dg_proto)>>2)) % SCACHE_BUCKETS; |
| 974 | |
| 975 | for (c = cache_htab[hash]; c; c = c->next) { |
| 976 | if (c->port == port && c->proto == dg_proto) |
| 977 | goto do_cache; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Eric Dumazet | 22588a0 | 2015-05-29 04:45:48 -0700 | [diff] [blame] | 980 | c = malloc(sizeof(*c)); |
| 981 | if (!c) |
| 982 | goto do_numeric; |
| 983 | res = __resolve_service(port); |
| 984 | c->port = port; |
| 985 | c->name = res ? strdup(res) : NULL; |
| 986 | c->proto = dg_proto; |
| 987 | c->next = cache_htab[hash]; |
| 988 | cache_htab[hash] = c; |
| 989 | |
| 990 | do_cache: |
| 991 | if (c->name) |
| 992 | return c->name; |
| 993 | |
| 994 | do_numeric: |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 995 | sprintf(buf, "%u", port); |
| 996 | return buf; |
| 997 | } |
| 998 | |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 999 | static void inet_addr_print(const inet_prefix *a, int port, unsigned int ifindex) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1000 | { |
| 1001 | char buf[1024]; |
| 1002 | const char *ap = buf; |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 1003 | int est_len = addr_width; |
| 1004 | const char *ifname = NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1005 | |
| 1006 | if (a->family == AF_INET) { |
| 1007 | if (a->data[0] == 0) { |
| 1008 | buf[0] = '*'; |
| 1009 | buf[1] = 0; |
| 1010 | } else { |
| 1011 | ap = format_host(AF_INET, 4, a->data, buf, sizeof(buf)); |
| 1012 | } |
| 1013 | } else { |
| 1014 | ap = format_host(a->family, 16, a->data, buf, sizeof(buf)); |
| 1015 | est_len = strlen(ap); |
| 1016 | if (est_len <= addr_width) |
| 1017 | est_len = addr_width; |
| 1018 | else |
| 1019 | est_len = addr_width + ((est_len-addr_width+3)/4)*4; |
| 1020 | } |
FX Le Bail | 7c8a3cf | 2014-02-11 10:06:23 +0100 | [diff] [blame] | 1021 | |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 1022 | if (ifindex) { |
| 1023 | ifname = ll_index_to_name(ifindex); |
| 1024 | est_len -= strlen(ifname) + 1; /* +1 for percent char */ |
Mike Saal | 4fcfb6b | 2015-08-26 11:59:17 -0400 | [diff] [blame] | 1025 | if (est_len < 0) |
| 1026 | est_len = 0; |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | sock_addr_print_width(est_len, ap, ":", serv_width, resolve_service(port), |
| 1030 | ifname); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | struct aafilter |
| 1034 | { |
| 1035 | inet_prefix addr; |
| 1036 | int port; |
| 1037 | struct aafilter *next; |
| 1038 | }; |
| 1039 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 1040 | static int inet2_addr_match(const inet_prefix *a, const inet_prefix *p, |
| 1041 | int plen) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1042 | { |
| 1043 | if (!inet_addr_match(a, p, plen)) |
| 1044 | return 0; |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 1045 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1046 | /* Cursed "v4 mapped" addresses: v4 mapped socket matches |
| 1047 | * pure IPv4 rule, but v4-mapped rule selects only v4-mapped |
| 1048 | * sockets. Fair? */ |
| 1049 | if (p->family == AF_INET && a->family == AF_INET6) { |
| 1050 | if (a->data[0] == 0 && a->data[1] == 0 && |
| 1051 | a->data[2] == htonl(0xffff)) { |
| 1052 | inet_prefix tmp = *a; |
| 1053 | tmp.data[0] = a->data[3]; |
| 1054 | return inet_addr_match(&tmp, p, plen); |
| 1055 | } |
| 1056 | } |
| 1057 | return 1; |
| 1058 | } |
| 1059 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 1060 | static int unix_match(const inet_prefix *a, const inet_prefix *p) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1061 | { |
Vadim Kochan | 99bb68f | 2015-07-21 10:53:19 -0700 | [diff] [blame] | 1062 | char *addr, *pattern; |
| 1063 | memcpy(&addr, a->data, sizeof(addr)); |
| 1064 | memcpy(&pattern, p->data, sizeof(pattern)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1065 | if (pattern == NULL) |
| 1066 | return 1; |
| 1067 | if (addr == NULL) |
| 1068 | addr = ""; |
| 1069 | return !fnmatch(pattern, addr, 0); |
| 1070 | } |
| 1071 | |
Vadim Kochan | 055840f | 2015-02-13 22:13:58 +0200 | [diff] [blame] | 1072 | static int run_ssfilter(struct ssfilter *f, struct sockstat *s) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1073 | { |
| 1074 | switch (f->type) { |
| 1075 | case SSF_S_AUTO: |
| 1076 | { |
| 1077 | static int low, high=65535; |
| 1078 | |
| 1079 | if (s->local.family == AF_UNIX) { |
Vadim Kochan | 99bb68f | 2015-07-21 10:53:19 -0700 | [diff] [blame] | 1080 | char *p; |
| 1081 | memcpy(&p, s->local.data, sizeof(p)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1082 | return p == NULL || (p[0] == '@' && strlen(p) == 6 && |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1083 | strspn(p+1, "0123456789abcdef") == 5); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1084 | } |
| 1085 | if (s->local.family == AF_PACKET) |
Maciej Żenczykowski | bbd303d | 2015-06-25 02:03:03 -0700 | [diff] [blame] | 1086 | return s->lport == 0 && s->local.data[0] == 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1087 | if (s->local.family == AF_NETLINK) |
| 1088 | return s->lport < 0; |
| 1089 | |
| 1090 | if (!low) { |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1091 | FILE *fp = ephemeral_ports_open(); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1092 | if (fp) { |
| 1093 | fscanf(fp, "%d%d", &low, &high); |
| 1094 | fclose(fp); |
| 1095 | } |
| 1096 | } |
| 1097 | return s->lport >= low && s->lport <= high; |
| 1098 | } |
| 1099 | case SSF_DCOND: |
| 1100 | { |
| 1101 | struct aafilter *a = (void*)f->pred; |
| 1102 | if (a->addr.family == AF_UNIX) |
| 1103 | return unix_match(&s->remote, &a->addr); |
| 1104 | if (a->port != -1 && a->port != s->rport) |
| 1105 | return 0; |
| 1106 | if (a->addr.bitlen) { |
| 1107 | do { |
| 1108 | if (!inet2_addr_match(&s->remote, &a->addr, a->addr.bitlen)) |
| 1109 | return 1; |
| 1110 | } while ((a = a->next) != NULL); |
| 1111 | return 0; |
| 1112 | } |
| 1113 | return 1; |
| 1114 | } |
| 1115 | case SSF_SCOND: |
| 1116 | { |
| 1117 | struct aafilter *a = (void*)f->pred; |
| 1118 | if (a->addr.family == AF_UNIX) |
| 1119 | return unix_match(&s->local, &a->addr); |
| 1120 | if (a->port != -1 && a->port != s->lport) |
| 1121 | return 0; |
| 1122 | if (a->addr.bitlen) { |
| 1123 | do { |
| 1124 | if (!inet2_addr_match(&s->local, &a->addr, a->addr.bitlen)) |
| 1125 | return 1; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1126 | } while ((a = a->next) != NULL); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1127 | return 0; |
| 1128 | } |
| 1129 | return 1; |
| 1130 | } |
| 1131 | case SSF_D_GE: |
| 1132 | { |
| 1133 | struct aafilter *a = (void*)f->pred; |
| 1134 | return s->rport >= a->port; |
| 1135 | } |
| 1136 | case SSF_D_LE: |
| 1137 | { |
| 1138 | struct aafilter *a = (void*)f->pred; |
| 1139 | return s->rport <= a->port; |
| 1140 | } |
| 1141 | case SSF_S_GE: |
| 1142 | { |
| 1143 | struct aafilter *a = (void*)f->pred; |
| 1144 | return s->lport >= a->port; |
| 1145 | } |
| 1146 | case SSF_S_LE: |
| 1147 | { |
| 1148 | struct aafilter *a = (void*)f->pred; |
| 1149 | return s->lport <= a->port; |
| 1150 | } |
| 1151 | |
| 1152 | /* Yup. It is recursion. Sorry. */ |
| 1153 | case SSF_AND: |
| 1154 | return run_ssfilter(f->pred, s) && run_ssfilter(f->post, s); |
| 1155 | case SSF_OR: |
| 1156 | return run_ssfilter(f->pred, s) || run_ssfilter(f->post, s); |
| 1157 | case SSF_NOT: |
| 1158 | return !run_ssfilter(f->pred, s); |
| 1159 | default: |
| 1160 | abort(); |
| 1161 | } |
| 1162 | } |
| 1163 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1164 | /* Relocate external jumps by reloc. */ |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1165 | static void ssfilter_patch(char *a, int len, int reloc) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1166 | { |
| 1167 | while (len > 0) { |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1168 | 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] | 1169 | if (op->no == len+4) |
| 1170 | op->no += reloc; |
| 1171 | len -= op->yes; |
| 1172 | a += op->yes; |
| 1173 | } |
| 1174 | if (len < 0) |
| 1175 | abort(); |
| 1176 | } |
| 1177 | |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1178 | static int ssfilter_bytecompile(struct ssfilter *f, char **bytecode) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1179 | { |
| 1180 | switch (f->type) { |
| 1181 | case SSF_S_AUTO: |
| 1182 | { |
| 1183 | if (!(*bytecode=malloc(4))) abort(); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1184 | ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_AUTO, 4, 8 }; |
Eric Dumazet | df39de8 | 2011-06-20 14:31:51 -0700 | [diff] [blame] | 1185 | return 4; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1186 | } |
| 1187 | case SSF_DCOND: |
| 1188 | case SSF_SCOND: |
| 1189 | { |
| 1190 | struct aafilter *a = (void*)f->pred; |
| 1191 | struct aafilter *b; |
| 1192 | char *ptr; |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1193 | 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] | 1194 | int len = 0; |
| 1195 | |
| 1196 | for (b=a; b; b=b->next) { |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1197 | len += 4 + sizeof(struct inet_diag_hostcond); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1198 | if (a->addr.family == AF_INET6) |
| 1199 | len += 16; |
| 1200 | else |
| 1201 | len += 4; |
| 1202 | if (b->next) |
| 1203 | len += 4; |
| 1204 | } |
| 1205 | if (!(ptr = malloc(len))) abort(); |
| 1206 | *bytecode = ptr; |
| 1207 | for (b=a; b; b=b->next) { |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1208 | 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] | 1209 | int alen = (a->addr.family == AF_INET6 ? 16 : 4); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1210 | int oplen = alen + 4 + sizeof(struct inet_diag_hostcond); |
| 1211 | struct inet_diag_hostcond *cond = (struct inet_diag_hostcond*)(ptr+4); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1212 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1213 | *op = (struct inet_diag_bc_op){ code, oplen, oplen+4 }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1214 | cond->family = a->addr.family; |
| 1215 | cond->port = a->port; |
| 1216 | cond->prefix_len = a->addr.bitlen; |
| 1217 | memcpy(cond->addr, a->addr.data, alen); |
| 1218 | ptr += oplen; |
| 1219 | if (b->next) { |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1220 | op = (struct inet_diag_bc_op *)ptr; |
| 1221 | *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] | 1222 | ptr += 4; |
| 1223 | } |
| 1224 | } |
| 1225 | return ptr - *bytecode; |
| 1226 | } |
| 1227 | case SSF_D_GE: |
| 1228 | { |
| 1229 | struct aafilter *x = (void*)f->pred; |
| 1230 | if (!(*bytecode=malloc(8))) abort(); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1231 | ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_GE, 8, 12 }; |
| 1232 | ((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] | 1233 | return 8; |
| 1234 | } |
| 1235 | case SSF_D_LE: |
| 1236 | { |
| 1237 | struct aafilter *x = (void*)f->pred; |
| 1238 | if (!(*bytecode=malloc(8))) abort(); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1239 | ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_LE, 8, 12 }; |
| 1240 | ((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] | 1241 | return 8; |
| 1242 | } |
| 1243 | case SSF_S_GE: |
| 1244 | { |
| 1245 | struct aafilter *x = (void*)f->pred; |
| 1246 | if (!(*bytecode=malloc(8))) abort(); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1247 | ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_GE, 8, 12 }; |
| 1248 | ((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] | 1249 | return 8; |
| 1250 | } |
| 1251 | case SSF_S_LE: |
| 1252 | { |
| 1253 | struct aafilter *x = (void*)f->pred; |
| 1254 | if (!(*bytecode=malloc(8))) abort(); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1255 | ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_LE, 8, 12 }; |
| 1256 | ((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] | 1257 | return 8; |
| 1258 | } |
| 1259 | |
| 1260 | case SSF_AND: |
| 1261 | { |
Andreas Henriksson | 2a4fa1c | 2013-11-13 09:46:42 +0100 | [diff] [blame] | 1262 | char *a1, *a2, *a; |
| 1263 | int l1, l2; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1264 | l1 = ssfilter_bytecompile(f->pred, &a1); |
| 1265 | l2 = ssfilter_bytecompile(f->post, &a2); |
| 1266 | if (!(a = malloc(l1+l2))) abort(); |
| 1267 | memcpy(a, a1, l1); |
| 1268 | memcpy(a+l1, a2, l2); |
| 1269 | free(a1); free(a2); |
| 1270 | ssfilter_patch(a, l1, l2); |
| 1271 | *bytecode = a; |
| 1272 | return l1+l2; |
| 1273 | } |
| 1274 | case SSF_OR: |
| 1275 | { |
Andreas Henriksson | 2a4fa1c | 2013-11-13 09:46:42 +0100 | [diff] [blame] | 1276 | char *a1, *a2, *a; |
| 1277 | int l1, l2; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1278 | l1 = ssfilter_bytecompile(f->pred, &a1); |
| 1279 | l2 = ssfilter_bytecompile(f->post, &a2); |
| 1280 | if (!(a = malloc(l1+l2+4))) abort(); |
| 1281 | memcpy(a, a1, l1); |
| 1282 | memcpy(a+l1+4, a2, l2); |
| 1283 | free(a1); free(a2); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1284 | *(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] | 1285 | *bytecode = a; |
| 1286 | return l1+l2+4; |
| 1287 | } |
| 1288 | case SSF_NOT: |
| 1289 | { |
Andreas Henriksson | 2a4fa1c | 2013-11-13 09:46:42 +0100 | [diff] [blame] | 1290 | char *a1, *a; |
| 1291 | int l1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1292 | l1 = ssfilter_bytecompile(f->pred, &a1); |
| 1293 | if (!(a = malloc(l1+4))) abort(); |
| 1294 | memcpy(a, a1, l1); |
| 1295 | free(a1); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1296 | *(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] | 1297 | *bytecode = a; |
| 1298 | return l1+4; |
| 1299 | } |
| 1300 | default: |
| 1301 | abort(); |
| 1302 | } |
| 1303 | } |
| 1304 | |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1305 | static int remember_he(struct aafilter *a, struct hostent *he) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1306 | { |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1307 | char **ptr = he->h_addr_list; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1308 | int cnt = 0; |
| 1309 | int len; |
| 1310 | |
| 1311 | if (he->h_addrtype == AF_INET) |
| 1312 | len = 4; |
| 1313 | else if (he->h_addrtype == AF_INET6) |
| 1314 | len = 16; |
| 1315 | else |
| 1316 | return 0; |
| 1317 | |
| 1318 | while (*ptr) { |
| 1319 | struct aafilter *b = a; |
| 1320 | if (a->addr.bitlen) { |
| 1321 | if ((b = malloc(sizeof(*b))) == NULL) |
| 1322 | return cnt; |
| 1323 | *b = *a; |
| 1324 | b->next = a->next; |
| 1325 | a->next = b; |
| 1326 | } |
| 1327 | memcpy(b->addr.data, *ptr, len); |
| 1328 | b->addr.bytelen = len; |
| 1329 | b->addr.bitlen = len*8; |
| 1330 | b->addr.family = he->h_addrtype; |
| 1331 | ptr++; |
| 1332 | cnt++; |
| 1333 | } |
| 1334 | return cnt; |
| 1335 | } |
| 1336 | |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1337 | 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] | 1338 | { |
| 1339 | static int notfirst; |
| 1340 | int cnt = 0; |
| 1341 | struct hostent *he; |
| 1342 | |
| 1343 | a->addr.bitlen = 0; |
| 1344 | if (!notfirst) { |
| 1345 | sethostent(1); |
| 1346 | notfirst = 1; |
| 1347 | } |
| 1348 | he = gethostbyname2(addr, fam == AF_UNSPEC ? AF_INET : fam); |
| 1349 | if (he) |
| 1350 | cnt = remember_he(a, he); |
| 1351 | if (fam == AF_UNSPEC) { |
| 1352 | he = gethostbyname2(addr, AF_INET6); |
| 1353 | if (he) |
| 1354 | cnt += remember_he(a, he); |
| 1355 | } |
| 1356 | return !cnt; |
| 1357 | } |
| 1358 | |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1359 | static int xll_initted = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1360 | |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1361 | static void xll_init(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1362 | { |
| 1363 | struct rtnl_handle rth; |
Stephen Hemminger | d2468da | 2013-12-20 08:15:02 -0800 | [diff] [blame] | 1364 | if (rtnl_open(&rth, 0) < 0) |
| 1365 | exit(1); |
| 1366 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1367 | ll_init_map(&rth); |
| 1368 | rtnl_close(&rth); |
| 1369 | xll_initted = 1; |
| 1370 | } |
| 1371 | |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1372 | static const char *xll_index_to_name(int index) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1373 | { |
| 1374 | if (!xll_initted) |
| 1375 | xll_init(); |
| 1376 | return ll_index_to_name(index); |
| 1377 | } |
| 1378 | |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1379 | static int xll_name_to_index(const char *dev) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1380 | { |
| 1381 | if (!xll_initted) |
| 1382 | xll_init(); |
| 1383 | return ll_name_to_index(dev); |
| 1384 | } |
| 1385 | |
Vadim Kochan | 7871f7d | 2015-02-27 23:54:36 +0200 | [diff] [blame] | 1386 | void *parse_hostcond(char *addr, bool is_port) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1387 | { |
| 1388 | char *port = NULL; |
Vadim Kochan | 1527a17 | 2015-02-13 13:01:08 +0200 | [diff] [blame] | 1389 | struct aafilter a = { .port = -1 }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1390 | struct aafilter *res; |
Vadim Kochan | 1527a17 | 2015-02-13 13:01:08 +0200 | [diff] [blame] | 1391 | int fam = preferred_family; |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 1392 | struct filter *f = ¤t_filter; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1393 | |
Vadim Kochan | 1527a17 | 2015-02-13 13:01:08 +0200 | [diff] [blame] | 1394 | if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1395 | char *p; |
| 1396 | a.addr.family = AF_UNIX; |
| 1397 | if (strncmp(addr, "unix:", 5) == 0) |
| 1398 | addr+=5; |
| 1399 | p = strdup(addr); |
| 1400 | a.addr.bitlen = 8*strlen(p); |
Vadim Kochan | 99bb68f | 2015-07-21 10:53:19 -0700 | [diff] [blame] | 1401 | memcpy(a.addr.data, &p, sizeof(p)); |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 1402 | fam = AF_UNIX; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1403 | goto out; |
| 1404 | } |
| 1405 | |
Vadim Kochan | 1527a17 | 2015-02-13 13:01:08 +0200 | [diff] [blame] | 1406 | if (fam == AF_PACKET || strncmp(addr, "link:", 5) == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1407 | a.addr.family = AF_PACKET; |
| 1408 | a.addr.bitlen = 0; |
| 1409 | if (strncmp(addr, "link:", 5) == 0) |
| 1410 | addr+=5; |
| 1411 | port = strchr(addr, ':'); |
| 1412 | if (port) { |
| 1413 | *port = 0; |
| 1414 | if (port[1] && strcmp(port+1, "*")) { |
| 1415 | if (get_integer(&a.port, port+1, 0)) { |
| 1416 | if ((a.port = xll_name_to_index(port+1)) <= 0) |
| 1417 | return NULL; |
| 1418 | } |
| 1419 | } |
| 1420 | } |
| 1421 | if (addr[0] && strcmp(addr, "*")) { |
| 1422 | unsigned short tmp; |
| 1423 | a.addr.bitlen = 32; |
| 1424 | if (ll_proto_a2n(&tmp, addr)) |
| 1425 | return NULL; |
| 1426 | a.addr.data[0] = ntohs(tmp); |
| 1427 | } |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 1428 | fam = AF_PACKET; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1429 | goto out; |
| 1430 | } |
| 1431 | |
Vadim Kochan | 1527a17 | 2015-02-13 13:01:08 +0200 | [diff] [blame] | 1432 | if (fam == AF_NETLINK || strncmp(addr, "netlink:", 8) == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1433 | a.addr.family = AF_NETLINK; |
| 1434 | a.addr.bitlen = 0; |
| 1435 | if (strncmp(addr, "netlink:", 8) == 0) |
| 1436 | addr+=8; |
| 1437 | port = strchr(addr, ':'); |
| 1438 | if (port) { |
| 1439 | *port = 0; |
| 1440 | if (port[1] && strcmp(port+1, "*")) { |
| 1441 | if (get_integer(&a.port, port+1, 0)) { |
| 1442 | if (strcmp(port+1, "kernel") == 0) |
| 1443 | a.port = 0; |
| 1444 | else |
| 1445 | return NULL; |
| 1446 | } |
| 1447 | } |
| 1448 | } |
| 1449 | if (addr[0] && strcmp(addr, "*")) { |
| 1450 | a.addr.bitlen = 32; |
vadimk | b00daf6 | 2014-12-06 02:52:19 +0200 | [diff] [blame] | 1451 | if (nl_proto_a2n(&a.addr.data[0], addr) == -1) |
| 1452 | return NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1453 | } |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 1454 | fam = AF_NETLINK; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1455 | goto out; |
| 1456 | } |
| 1457 | |
Vadim Kochan | 1527a17 | 2015-02-13 13:01:08 +0200 | [diff] [blame] | 1458 | if (fam == AF_INET || !strncmp(addr, "inet:", 5)) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1459 | fam = AF_INET; |
Vadim Kochan | 1527a17 | 2015-02-13 13:01:08 +0200 | [diff] [blame] | 1460 | if (!strncmp(addr, "inet:", 5)) |
| 1461 | addr += 5; |
| 1462 | } else if (fam == AF_INET6 || !strncmp(addr, "inet6:", 6)) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1463 | fam = AF_INET6; |
Vadim Kochan | 1527a17 | 2015-02-13 13:01:08 +0200 | [diff] [blame] | 1464 | if (!strncmp(addr, "inet6:", 6)) |
| 1465 | addr += 6; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | /* URL-like literal [] */ |
| 1469 | if (addr[0] == '[') { |
| 1470 | addr++; |
| 1471 | if ((port = strchr(addr, ']')) == NULL) |
| 1472 | return NULL; |
| 1473 | *port++ = 0; |
| 1474 | } else if (addr[0] == '*') { |
| 1475 | port = addr+1; |
| 1476 | } else { |
| 1477 | port = strrchr(strchr(addr, '/') ? : addr, ':'); |
| 1478 | } |
Vadim Kochan | 7871f7d | 2015-02-27 23:54:36 +0200 | [diff] [blame] | 1479 | |
| 1480 | if (is_port) |
| 1481 | port = addr; |
| 1482 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1483 | if (port && *port) { |
Vadim Kochan | 7871f7d | 2015-02-27 23:54:36 +0200 | [diff] [blame] | 1484 | if (*port == ':') |
| 1485 | *port++ = 0; |
| 1486 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1487 | if (*port && *port != '*') { |
| 1488 | if (get_integer(&a.port, port, 0)) { |
| 1489 | struct servent *se1 = NULL; |
| 1490 | struct servent *se2 = NULL; |
| 1491 | if (current_filter.dbs&(1<<UDP_DB)) |
| 1492 | se1 = getservbyname(port, UDP_PROTO); |
| 1493 | if (current_filter.dbs&(1<<TCP_DB)) |
| 1494 | se2 = getservbyname(port, TCP_PROTO); |
| 1495 | if (se1 && se2 && se1->s_port != se2->s_port) { |
| 1496 | fprintf(stderr, "Error: ambiguous port \"%s\".\n", port); |
| 1497 | return NULL; |
| 1498 | } |
| 1499 | if (!se1) |
| 1500 | se1 = se2; |
| 1501 | if (se1) { |
| 1502 | a.port = ntohs(se1->s_port); |
| 1503 | } else { |
| 1504 | struct scache *s; |
| 1505 | for (s = rlist; s; s = s->next) { |
| 1506 | if ((s->proto == UDP_PROTO && |
| 1507 | (current_filter.dbs&(1<<UDP_DB))) || |
| 1508 | (s->proto == TCP_PROTO && |
| 1509 | (current_filter.dbs&(1<<TCP_DB)))) { |
| 1510 | if (s->name && strcmp(s->name, port) == 0) { |
| 1511 | if (a.port > 0 && a.port != s->port) { |
| 1512 | fprintf(stderr, "Error: ambiguous port \"%s\".\n", port); |
| 1513 | return NULL; |
| 1514 | } |
| 1515 | a.port = s->port; |
| 1516 | } |
| 1517 | } |
| 1518 | } |
| 1519 | if (a.port <= 0) { |
| 1520 | fprintf(stderr, "Error: \"%s\" does not look like a port.\n", port); |
| 1521 | return NULL; |
| 1522 | } |
| 1523 | } |
| 1524 | } |
| 1525 | } |
| 1526 | } |
Vadim Kochan | 7871f7d | 2015-02-27 23:54:36 +0200 | [diff] [blame] | 1527 | if (!is_port && addr && *addr && *addr != '*') { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1528 | if (get_prefix_1(&a.addr, addr, fam)) { |
| 1529 | if (get_dns_host(&a, addr, fam)) { |
| 1530 | fprintf(stderr, "Error: an inet prefix is expected rather than \"%s\".\n", addr); |
| 1531 | return NULL; |
| 1532 | } |
| 1533 | } |
| 1534 | } |
| 1535 | |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 1536 | out: |
Vadim Kochan | 1527a17 | 2015-02-13 13:01:08 +0200 | [diff] [blame] | 1537 | if (fam != AF_UNSPEC) { |
| 1538 | f->families = 0; |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 1539 | filter_af_set(f, fam); |
Vadim Kochan | 57ff5a1 | 2015-04-30 07:30:24 +0300 | [diff] [blame] | 1540 | filter_states_set(f, 0); |
Vadim Kochan | 1527a17 | 2015-02-13 13:01:08 +0200 | [diff] [blame] | 1541 | } |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 1542 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1543 | res = malloc(sizeof(*res)); |
| 1544 | if (res) |
| 1545 | memcpy(res, &a, sizeof(a)); |
| 1546 | return res; |
| 1547 | } |
| 1548 | |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1549 | static char *proto_name(int protocol) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1550 | { |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1551 | switch (protocol) { |
Nikolay Aleksandrov | 235c445 | 2015-07-15 05:50:42 -0700 | [diff] [blame] | 1552 | case 0: |
| 1553 | return "raw"; |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1554 | case IPPROTO_UDP: |
| 1555 | return "udp"; |
| 1556 | case IPPROTO_TCP: |
| 1557 | return "tcp"; |
| 1558 | case IPPROTO_DCCP: |
| 1559 | return "dccp"; |
| 1560 | } |
| 1561 | |
| 1562 | return "???"; |
| 1563 | } |
| 1564 | |
Vadim Kochan | 055840f | 2015-02-13 22:13:58 +0200 | [diff] [blame] | 1565 | static void inet_stats_print(struct sockstat *s, int protocol) |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1566 | { |
| 1567 | char *buf = NULL; |
| 1568 | |
Vadim Kochan | 2d791bc | 2015-02-13 22:14:01 +0200 | [diff] [blame] | 1569 | sock_state_print(s, proto_name(protocol)); |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1570 | |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 1571 | inet_addr_print(&s->local, s->lport, s->iface); |
| 1572 | inet_addr_print(&s->remote, s->rport, 0); |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1573 | |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1574 | if (show_proc_ctx || show_sock_ctx) { |
| 1575 | if (find_entry(s->ino, &buf, |
| 1576 | (show_proc_ctx & show_sock_ctx) ? |
| 1577 | PROC_SOCK_CTX : PROC_CTX) > 0) { |
| 1578 | printf(" users:(%s)", buf); |
| 1579 | free(buf); |
| 1580 | } |
| 1581 | } else if (show_users) { |
| 1582 | if (find_entry(s->ino, &buf, USERS) > 0) { |
| 1583 | printf(" users:(%s)", buf); |
| 1584 | free(buf); |
| 1585 | } |
| 1586 | } |
| 1587 | } |
| 1588 | |
Vadim Kochan | 055840f | 2015-02-13 22:13:58 +0200 | [diff] [blame] | 1589 | static int proc_parse_inet_addr(char *loc, char *rem, int family, struct |
| 1590 | sockstat *s) |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1591 | { |
| 1592 | s->local.family = s->remote.family = family; |
| 1593 | if (family == AF_INET) { |
| 1594 | sscanf(loc, "%x:%x", s->local.data, (unsigned*)&s->lport); |
| 1595 | sscanf(rem, "%x:%x", s->remote.data, (unsigned*)&s->rport); |
| 1596 | s->local.bytelen = s->remote.bytelen = 4; |
| 1597 | return 0; |
| 1598 | } else { |
| 1599 | sscanf(loc, "%08x%08x%08x%08x:%x", |
| 1600 | s->local.data, |
| 1601 | s->local.data + 1, |
| 1602 | s->local.data + 2, |
| 1603 | s->local.data + 3, |
| 1604 | &s->lport); |
| 1605 | sscanf(rem, "%08x%08x%08x%08x:%x", |
| 1606 | s->remote.data, |
| 1607 | s->remote.data + 1, |
| 1608 | s->remote.data + 2, |
| 1609 | s->remote.data + 3, |
| 1610 | &s->rport); |
| 1611 | s->local.bytelen = s->remote.bytelen = 16; |
| 1612 | return 0; |
| 1613 | } |
| 1614 | return -1; |
| 1615 | } |
| 1616 | |
| 1617 | static int proc_inet_split_line(char *line, char **loc, char **rem, char **data) |
| 1618 | { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1619 | char *p; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1620 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1621 | if ((p = strchr(line, ':')) == NULL) |
| 1622 | return -1; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1623 | |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1624 | *loc = p+2; |
| 1625 | if ((p = strchr(*loc, ':')) == NULL) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1626 | return -1; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1627 | |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1628 | p[5] = 0; |
| 1629 | *rem = p+6; |
| 1630 | if ((p = strchr(*rem, ':')) == NULL) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1631 | return -1; |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1632 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1633 | p[5] = 0; |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1634 | *data = p+6; |
| 1635 | return 0; |
| 1636 | } |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1637 | |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1638 | static char *sprint_bw(char *buf, double bw) |
| 1639 | { |
| 1640 | if (bw > 1000000.) |
| 1641 | sprintf(buf,"%.1fM", bw / 1000000.); |
| 1642 | else if (bw > 1000.) |
| 1643 | sprintf(buf,"%.1fK", bw / 1000.); |
| 1644 | else |
| 1645 | sprintf(buf, "%g", bw); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1646 | |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1647 | return buf; |
| 1648 | } |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1649 | |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1650 | static void tcp_stats_print(struct tcpstat *s) |
| 1651 | { |
| 1652 | char b1[64]; |
| 1653 | |
| 1654 | if (s->has_ts_opt) |
| 1655 | printf(" ts"); |
| 1656 | if (s->has_sack_opt) |
| 1657 | printf(" sack"); |
| 1658 | if (s->has_ecn_opt) |
| 1659 | printf(" ecn"); |
| 1660 | if (s->has_ecnseen_opt) |
| 1661 | printf(" ecnseen"); |
| 1662 | if (s->has_fastopen_opt) |
| 1663 | printf(" fastopen"); |
Eric Dumazet | d2055ea | 2015-05-29 04:04:05 -0700 | [diff] [blame] | 1664 | if (s->cong_alg[0]) |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1665 | printf(" %s", s->cong_alg); |
| 1666 | if (s->has_wscale_opt) |
| 1667 | printf(" wscale:%d,%d", s->snd_wscale, s->rcv_wscale); |
| 1668 | if (s->rto) |
| 1669 | printf(" rto:%g", s->rto); |
| 1670 | if (s->backoff) |
| 1671 | printf(" backoff:%u", s->backoff); |
| 1672 | if (s->rtt) |
| 1673 | printf(" rtt:%g/%g", s->rtt, s->rttvar); |
| 1674 | if (s->ato) |
| 1675 | printf(" ato:%g", s->ato); |
| 1676 | |
| 1677 | if (s->qack) |
| 1678 | printf(" qack:%d", s->qack); |
| 1679 | if (s->qack & 1) |
| 1680 | printf(" bidir"); |
| 1681 | |
| 1682 | if (s->mss) |
| 1683 | printf(" mss:%d", s->mss); |
Eric Dumazet | 3bf5445 | 2015-05-08 13:28:40 -0700 | [diff] [blame] | 1684 | if (s->cwnd) |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1685 | printf(" cwnd:%d", s->cwnd); |
| 1686 | if (s->ssthresh) |
| 1687 | printf(" ssthresh:%d", s->ssthresh); |
| 1688 | |
Eric Dumazet | 1a4dda7 | 2015-05-11 10:03:49 -0700 | [diff] [blame] | 1689 | if (s->bytes_acked) |
| 1690 | printf(" bytes_acked:%llu", s->bytes_acked); |
| 1691 | if (s->bytes_received) |
| 1692 | printf(" bytes_received:%llu", s->bytes_received); |
Craig Gallek | ecb435e | 2015-05-26 14:54:41 -0400 | [diff] [blame] | 1693 | if (s->segs_out) |
| 1694 | printf(" segs_out:%u", s->segs_out); |
| 1695 | if (s->segs_in) |
| 1696 | printf(" segs_in:%u", s->segs_in); |
Eric Dumazet | 1a4dda7 | 2015-05-11 10:03:49 -0700 | [diff] [blame] | 1697 | |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1698 | if (s->dctcp && s->dctcp->enabled) { |
| 1699 | struct dctcpstat *dctcp = s->dctcp; |
| 1700 | |
Eric Dumazet | 3bf5445 | 2015-05-08 13:28:40 -0700 | [diff] [blame] | 1701 | printf(" dctcp:(ce_state:%u,alpha:%u,ab_ecn:%u,ab_tot:%u)", |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1702 | dctcp->ce_state, dctcp->alpha, dctcp->ab_ecn, |
| 1703 | dctcp->ab_tot); |
| 1704 | } else if (s->dctcp) { |
Eric Dumazet | 3bf5445 | 2015-05-08 13:28:40 -0700 | [diff] [blame] | 1705 | printf(" dctcp:fallback_mode"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1706 | } |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1707 | |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1708 | if (s->send_bps) |
| 1709 | printf(" send %sbps", sprint_bw(b1, s->send_bps)); |
| 1710 | if (s->lastsnd) |
| 1711 | printf(" lastsnd:%u", s->lastsnd); |
| 1712 | if (s->lastrcv) |
| 1713 | printf(" lastrcv:%u", s->lastrcv); |
| 1714 | if (s->lastack) |
| 1715 | printf(" lastack:%u", s->lastack); |
| 1716 | |
| 1717 | if (s->pacing_rate) { |
| 1718 | printf(" pacing_rate %sbps", sprint_bw(b1, s->pacing_rate)); |
| 1719 | if (s->pacing_rate_max) |
| 1720 | printf("/%sbps", sprint_bw(b1, |
| 1721 | s->pacing_rate_max)); |
| 1722 | } |
| 1723 | |
| 1724 | if (s->unacked) |
| 1725 | printf(" unacked:%u", s->unacked); |
| 1726 | if (s->retrans || s->retrans_total) |
| 1727 | printf(" retrans:%u/%u", s->retrans, s->retrans_total); |
| 1728 | if (s->lost) |
| 1729 | printf(" lost:%u", s->lost); |
Vadim Kochan | 055840f | 2015-02-13 22:13:58 +0200 | [diff] [blame] | 1730 | if (s->sacked && s->ss.state != SS_LISTEN) |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1731 | printf(" sacked:%u", s->sacked); |
| 1732 | if (s->fackets) |
| 1733 | printf(" fackets:%u", s->fackets); |
| 1734 | if (s->reordering != 3) |
| 1735 | printf(" reordering:%d", s->reordering); |
| 1736 | if (s->rcv_rtt) |
| 1737 | printf(" rcv_rtt:%g", s->rcv_rtt); |
| 1738 | if (s->rcv_space) |
| 1739 | printf(" rcv_space:%d", s->rcv_space); |
| 1740 | } |
| 1741 | |
Vadim Kochan | 055840f | 2015-02-13 22:13:58 +0200 | [diff] [blame] | 1742 | static void tcp_timer_print(struct tcpstat *s) |
| 1743 | { |
| 1744 | if (s->timer) { |
| 1745 | if (s->timer > 4) |
| 1746 | s->timer = 5; |
| 1747 | printf(" timer:(%s,%s,%d)", |
| 1748 | tmr_name[s->timer], |
| 1749 | print_ms_timer(s->timeout), |
| 1750 | s->retrans); |
| 1751 | } |
| 1752 | } |
| 1753 | |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1754 | static int tcp_show_line(char *line, const struct filter *f, int family) |
| 1755 | { |
| 1756 | int rto = 0, ato = 0; |
| 1757 | struct tcpstat s = {}; |
| 1758 | char *loc, *rem, *data; |
| 1759 | char opt[256]; |
| 1760 | int n; |
| 1761 | int hz = get_user_hz(); |
| 1762 | |
| 1763 | if (proc_inet_split_line(line, &loc, &rem, &data)) |
| 1764 | return -1; |
| 1765 | |
| 1766 | int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0'); |
| 1767 | if (!(f->states & (1 << state))) |
| 1768 | return 0; |
| 1769 | |
Vadim Kochan | 055840f | 2015-02-13 22:13:58 +0200 | [diff] [blame] | 1770 | proc_parse_inet_addr(loc, rem, family, &s.ss); |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1771 | |
Vadim Kochan | 055840f | 2015-02-13 22:13:58 +0200 | [diff] [blame] | 1772 | if (f->f && run_ssfilter(f->f, &s.ss) == 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1773 | return 0; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1774 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1775 | opt[0] = 0; |
Stephen Hemminger | e7113c6 | 2007-07-10 18:26:54 -0700 | [diff] [blame] | 1776 | n = sscanf(data, "%x %x:%x %x:%x %x %d %d %u %d %llx %d %d %d %d %d %[^\n]\n", |
Vadim Kochan | 055840f | 2015-02-13 22:13:58 +0200 | [diff] [blame] | 1777 | &s.ss.state, &s.ss.wq, &s.ss.rq, |
| 1778 | &s.timer, &s.timeout, &s.retrans, &s.ss.uid, &s.probes, |
| 1779 | &s.ss.ino, &s.ss.refcnt, &s.ss.sk, &rto, &ato, &s.qack, &s.cwnd, |
| 1780 | &s.ssthresh, opt); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1781 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1782 | if (n < 17) |
| 1783 | opt[0] = 0; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1784 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1785 | if (n < 12) { |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1786 | rto = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1787 | s.cwnd = 2; |
| 1788 | s.ssthresh = -1; |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1789 | ato = s.qack = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1790 | } |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1791 | |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1792 | s.retrans = s.timer != 1 ? s.probes : s.retrans; |
| 1793 | s.timeout = (s.timeout * 1000 + hz - 1) / hz; |
| 1794 | s.ato = (double)ato / hz; |
| 1795 | s.qack /= 2; |
| 1796 | s.rto = (double)rto; |
| 1797 | s.ssthresh = s.ssthresh == -1 ? 0 : s.ssthresh; |
| 1798 | s.rto = s.rto != 3 * hz ? s.rto / hz : 0; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1799 | |
Vadim Kochan | 055840f | 2015-02-13 22:13:58 +0200 | [diff] [blame] | 1800 | inet_stats_print(&s.ss, IPPROTO_TCP); |
| 1801 | |
| 1802 | if (show_options) |
| 1803 | tcp_timer_print(&s); |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 1804 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1805 | if (show_details) { |
Vadim Kochan | f1b39e1 | 2015-02-13 22:14:02 +0200 | [diff] [blame] | 1806 | sock_details_print(&s.ss); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1807 | if (opt[0]) |
| 1808 | printf(" opt:\"%s\"", opt); |
| 1809 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1810 | |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1811 | if (show_tcpinfo) |
| 1812 | tcp_stats_print(&s); |
| 1813 | |
| 1814 | printf("\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1815 | return 0; |
| 1816 | } |
| 1817 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1818 | static int generic_record_read(FILE *fp, |
| 1819 | int (*worker)(char*, const struct filter *, int), |
| 1820 | const struct filter *f, int fam) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1821 | { |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1822 | char line[256]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1823 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1824 | /* skip header */ |
| 1825 | if (fgets(line, sizeof(line), fp) == NULL) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1826 | goto outerr; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1827 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1828 | while (fgets(line, sizeof(line), fp) != NULL) { |
| 1829 | int n = strlen(line); |
| 1830 | if (n == 0 || line[n-1] != '\n') { |
| 1831 | errno = -EINVAL; |
| 1832 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1833 | } |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1834 | line[n-1] = 0; |
| 1835 | |
| 1836 | if (worker(line, f, fam) < 0) |
| 1837 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1838 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1839 | outerr: |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 1840 | |
| 1841 | return ferror(fp) ? -1 : 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1842 | } |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1843 | |
Hannes Frederic Sowa | 51ff9f2 | 2013-02-22 15:28:18 +0000 | [diff] [blame] | 1844 | static void print_skmeminfo(struct rtattr *tb[], int attrtype) |
| 1845 | { |
| 1846 | const __u32 *skmeminfo; |
Vadim Kochan | db08bdb | 2015-01-20 16:14:23 +0200 | [diff] [blame] | 1847 | |
| 1848 | if (!tb[attrtype]) { |
| 1849 | if (attrtype == INET_DIAG_SKMEMINFO) { |
| 1850 | if (!tb[INET_DIAG_MEMINFO]) |
| 1851 | return; |
| 1852 | |
| 1853 | const struct inet_diag_meminfo *minfo = |
| 1854 | RTA_DATA(tb[INET_DIAG_MEMINFO]); |
| 1855 | |
| 1856 | printf(" mem:(r%u,w%u,f%u,t%u)", |
| 1857 | minfo->idiag_rmem, |
| 1858 | minfo->idiag_wmem, |
| 1859 | minfo->idiag_fmem, |
| 1860 | minfo->idiag_tmem); |
| 1861 | } |
Hannes Frederic Sowa | 51ff9f2 | 2013-02-22 15:28:18 +0000 | [diff] [blame] | 1862 | return; |
Vadim Kochan | db08bdb | 2015-01-20 16:14:23 +0200 | [diff] [blame] | 1863 | } |
| 1864 | |
Hannes Frederic Sowa | 51ff9f2 | 2013-02-22 15:28:18 +0000 | [diff] [blame] | 1865 | skmeminfo = RTA_DATA(tb[attrtype]); |
| 1866 | |
| 1867 | printf(" skmem:(r%u,rb%u,t%u,tb%u,f%u,w%u,o%u", |
| 1868 | skmeminfo[SK_MEMINFO_RMEM_ALLOC], |
| 1869 | skmeminfo[SK_MEMINFO_RCVBUF], |
| 1870 | skmeminfo[SK_MEMINFO_WMEM_ALLOC], |
| 1871 | skmeminfo[SK_MEMINFO_SNDBUF], |
| 1872 | skmeminfo[SK_MEMINFO_FWD_ALLOC], |
| 1873 | skmeminfo[SK_MEMINFO_WMEM_QUEUED], |
| 1874 | skmeminfo[SK_MEMINFO_OPTMEM]); |
| 1875 | |
| 1876 | if (RTA_PAYLOAD(tb[attrtype]) >= |
| 1877 | (SK_MEMINFO_BACKLOG + 1) * sizeof(__u32)) |
| 1878 | printf(",bl%u", skmeminfo[SK_MEMINFO_BACKLOG]); |
| 1879 | |
| 1880 | printf(")"); |
| 1881 | } |
| 1882 | |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1883 | #define TCPI_HAS_OPT(info, opt) !!(info->tcpi_options & (opt)) |
| 1884 | |
Pavel Emelyanov | 5b81604 | 2013-05-17 08:02:14 -0700 | [diff] [blame] | 1885 | static void tcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r, |
| 1886 | struct rtattr *tb[]) |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 1887 | { |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1888 | double rtt = 0; |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1889 | struct tcpstat s = {}; |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 1890 | |
Vadim Kochan | 055840f | 2015-02-13 22:13:58 +0200 | [diff] [blame] | 1891 | s.ss.state = r->idiag_state; |
| 1892 | |
Vadim Kochan | db08bdb | 2015-01-20 16:14:23 +0200 | [diff] [blame] | 1893 | print_skmeminfo(tb, INET_DIAG_SKMEMINFO); |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 1894 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1895 | if (tb[INET_DIAG_INFO]) { |
osdl.org!shemminger | 05e1811 | 2004-06-08 20:33:44 +0000 | [diff] [blame] | 1896 | struct tcp_info *info; |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1897 | int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]); |
osdl.org!shemminger | 05e1811 | 2004-06-08 20:33:44 +0000 | [diff] [blame] | 1898 | |
| 1899 | /* workaround for older kernels with less fields */ |
| 1900 | if (len < sizeof(*info)) { |
| 1901 | info = alloca(sizeof(*info)); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1902 | memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len); |
Eric Dumazet | 656e8fd | 2015-05-06 11:33:23 -0700 | [diff] [blame] | 1903 | memset((char *)info + len, 0, sizeof(*info) - len); |
osdl.org!shemminger | 05e1811 | 2004-06-08 20:33:44 +0000 | [diff] [blame] | 1904 | } else |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1905 | info = RTA_DATA(tb[INET_DIAG_INFO]); |
osdl.org!shemminger | 05e1811 | 2004-06-08 20:33:44 +0000 | [diff] [blame] | 1906 | |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1907 | if (show_options) { |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1908 | s.has_ts_opt = TCPI_HAS_OPT(info, TCPI_OPT_TIMESTAMPS); |
| 1909 | s.has_sack_opt = TCPI_HAS_OPT(info, TCPI_OPT_SACK); |
| 1910 | s.has_ecn_opt = TCPI_HAS_OPT(info, TCPI_OPT_ECN); |
| 1911 | s.has_ecnseen_opt = TCPI_HAS_OPT(info, TCPI_OPT_ECN_SEEN); |
| 1912 | s.has_fastopen_opt = TCPI_HAS_OPT(info, TCPI_OPT_SYN_DATA); |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1913 | } |
shemminger | 52d5ac3 | 2005-07-05 22:11:37 +0000 | [diff] [blame] | 1914 | |
Eric Dumazet | d2055ea | 2015-05-29 04:04:05 -0700 | [diff] [blame] | 1915 | if (tb[INET_DIAG_CONG]) |
| 1916 | strncpy(s.cong_alg, |
| 1917 | rta_getattr_str(tb[INET_DIAG_CONG]), |
| 1918 | sizeof(s.cong_alg) - 1); |
shemminger | ea8fc10 | 2005-06-22 18:27:49 +0000 | [diff] [blame] | 1919 | |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1920 | if (TCPI_HAS_OPT(info, TCPI_OPT_WSCALE)) { |
| 1921 | s.has_wscale_opt = true; |
| 1922 | s.snd_wscale = info->tcpi_snd_wscale; |
| 1923 | s.rcv_wscale = info->tcpi_rcv_wscale; |
| 1924 | } |
| 1925 | |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 1926 | if (info->tcpi_rto && info->tcpi_rto != 3000000) |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1927 | s.rto = (double)info->tcpi_rto / 1000; |
| 1928 | |
| 1929 | s.backoff = info->tcpi_backoff; |
| 1930 | s.rtt = (double)info->tcpi_rtt / 1000; |
| 1931 | s.rttvar = (double)info->tcpi_rttvar / 1000; |
Vadim Kochan | 11ba90f | 2015-02-13 22:14:04 +0200 | [diff] [blame] | 1932 | s.ato = (double)info->tcpi_ato / 1000; |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1933 | s.mss = info->tcpi_snd_mss; |
| 1934 | s.rcv_space = info->tcpi_rcv_space; |
| 1935 | s.rcv_rtt = (double)info->tcpi_rcv_rtt / 1000; |
| 1936 | s.lastsnd = info->tcpi_last_data_sent; |
| 1937 | s.lastrcv = info->tcpi_last_data_recv; |
| 1938 | s.lastack = info->tcpi_last_ack_recv; |
| 1939 | s.unacked = info->tcpi_unacked; |
| 1940 | s.retrans = info->tcpi_retrans; |
| 1941 | s.retrans_total = info->tcpi_total_retrans; |
| 1942 | s.lost = info->tcpi_lost; |
| 1943 | s.sacked = info->tcpi_sacked; |
| 1944 | s.reordering = info->tcpi_reordering; |
| 1945 | s.rcv_space = info->tcpi_rcv_space; |
| 1946 | s.cwnd = info->tcpi_snd_cwnd; |
| 1947 | |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 1948 | if (info->tcpi_snd_ssthresh < 0xFFFF) |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1949 | s.ssthresh = info->tcpi_snd_ssthresh; |
shemminger | 52d5ac3 | 2005-07-05 22:11:37 +0000 | [diff] [blame] | 1950 | |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1951 | rtt = (double) info->tcpi_rtt; |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1952 | if (tb[INET_DIAG_VEGASINFO]) { |
osdl.org!shemminger | 05e1811 | 2004-06-08 20:33:44 +0000 | [diff] [blame] | 1953 | const struct tcpvegas_info *vinfo |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1954 | = RTA_DATA(tb[INET_DIAG_VEGASINFO]); |
osdl.org!shemminger | 05e1811 | 2004-06-08 20:33:44 +0000 | [diff] [blame] | 1955 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1956 | if (vinfo->tcpv_enabled && |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1957 | vinfo->tcpv_rtt && vinfo->tcpv_rtt != 0x7fffffff) |
shemminger | ea8fc10 | 2005-06-22 18:27:49 +0000 | [diff] [blame] | 1958 | rtt = vinfo->tcpv_rtt; |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 1959 | } |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1960 | |
Daniel Borkmann | 907e1ac | 2014-09-29 10:47:32 +0200 | [diff] [blame] | 1961 | if (tb[INET_DIAG_DCTCPINFO]) { |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1962 | struct dctcpstat *dctcp = malloc(sizeof(struct |
| 1963 | dctcpstat)); |
| 1964 | |
Daniel Borkmann | 907e1ac | 2014-09-29 10:47:32 +0200 | [diff] [blame] | 1965 | const struct tcp_dctcp_info *dinfo |
| 1966 | = RTA_DATA(tb[INET_DIAG_DCTCPINFO]); |
| 1967 | |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1968 | dctcp->enabled = !!dinfo->dctcp_enabled; |
| 1969 | dctcp->ce_state = dinfo->dctcp_ce_state; |
| 1970 | dctcp->alpha = dinfo->dctcp_alpha; |
| 1971 | dctcp->ab_ecn = dinfo->dctcp_ab_ecn; |
| 1972 | dctcp->ab_tot = dinfo->dctcp_ab_tot; |
| 1973 | s.dctcp = dctcp; |
Daniel Borkmann | 907e1ac | 2014-09-29 10:47:32 +0200 | [diff] [blame] | 1974 | } |
| 1975 | |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1976 | if (rtt > 0 && info->tcpi_snd_mss && info->tcpi_snd_cwnd) { |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1977 | s.send_bps = (double) info->tcpi_snd_cwnd * |
| 1978 | (double)info->tcpi_snd_mss * 8000000. / rtt; |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
Eric Dumazet | eb6028b | 2014-06-05 06:47:37 -0700 | [diff] [blame] | 1981 | if (info->tcpi_pacing_rate && |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1982 | info->tcpi_pacing_rate != ~0ULL) { |
| 1983 | s.pacing_rate = info->tcpi_pacing_rate * 8.; |
Eric Dumazet | eb6028b | 2014-06-05 06:47:37 -0700 | [diff] [blame] | 1984 | |
| 1985 | if (info->tcpi_max_pacing_rate && |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1986 | info->tcpi_max_pacing_rate != ~0ULL) |
| 1987 | s.pacing_rate_max = info->tcpi_max_pacing_rate * 8.; |
Eric Dumazet | eb6028b | 2014-06-05 06:47:37 -0700 | [diff] [blame] | 1988 | } |
Eric Dumazet | 1a4dda7 | 2015-05-11 10:03:49 -0700 | [diff] [blame] | 1989 | s.bytes_acked = info->tcpi_bytes_acked; |
| 1990 | s.bytes_received = info->tcpi_bytes_received; |
Craig Gallek | ecb435e | 2015-05-26 14:54:41 -0400 | [diff] [blame] | 1991 | s.segs_out = info->tcpi_segs_out; |
| 1992 | s.segs_in = info->tcpi_segs_in; |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 1993 | tcp_stats_print(&s); |
Stephen Hemminger | 92de1c2 | 2015-07-21 10:49:54 -0700 | [diff] [blame] | 1994 | free(s.dctcp); |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 1995 | } |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 1996 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1997 | |
Pavel Emelyanov | 77a8ca8 | 2014-01-28 10:24:15 +0400 | [diff] [blame] | 1998 | static int inet_show_sock(struct nlmsghdr *nlh, struct filter *f, int protocol) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1999 | { |
Pavel Emelyanov | 5b81604 | 2013-05-17 08:02:14 -0700 | [diff] [blame] | 2000 | struct rtattr * tb[INET_DIAG_MAX+1]; |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 2001 | struct inet_diag_msg *r = NLMSG_DATA(nlh); |
Vadim Kochan | 055840f | 2015-02-13 22:13:58 +0200 | [diff] [blame] | 2002 | struct sockstat s = {}; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2003 | |
Pavel Emelyanov | 5b81604 | 2013-05-17 08:02:14 -0700 | [diff] [blame] | 2004 | parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr*)(r+1), |
| 2005 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); |
| 2006 | |
Vadim Kochan | f1b39e1 | 2015-02-13 22:14:02 +0200 | [diff] [blame] | 2007 | s.state = r->idiag_state; |
| 2008 | s.local.family = s.remote.family = r->idiag_family; |
| 2009 | s.lport = ntohs(r->id.idiag_sport); |
| 2010 | s.rport = ntohs(r->id.idiag_dport); |
| 2011 | s.wq = r->idiag_wqueue; |
| 2012 | s.rq = r->idiag_rqueue; |
| 2013 | s.ino = r->idiag_inode; |
| 2014 | s.uid = r->idiag_uid; |
| 2015 | s.iface = r->id.idiag_if; |
| 2016 | s.sk = cookie_sk_get(&r->id.idiag_cookie[0]); |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 2017 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2018 | if (s.local.family == AF_INET) { |
| 2019 | s.local.bytelen = s.remote.bytelen = 4; |
| 2020 | } else { |
| 2021 | s.local.bytelen = s.remote.bytelen = 16; |
| 2022 | } |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 2023 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 2024 | memcpy(s.local.data, r->id.idiag_src, s.local.bytelen); |
| 2025 | memcpy(s.remote.data, r->id.idiag_dst, s.local.bytelen); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2026 | |
| 2027 | if (f && f->f && run_ssfilter(f->f, &s) == 0) |
| 2028 | return 0; |
| 2029 | |
Craig Gallek | 6885e3b | 2015-06-17 11:14:48 -0400 | [diff] [blame] | 2030 | if (tb[INET_DIAG_PROTOCOL]) |
| 2031 | protocol = *(__u8 *)RTA_DATA(tb[INET_DIAG_PROTOCOL]); |
| 2032 | |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 2033 | inet_stats_print(&s, protocol); |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 2034 | |
Vadim Kochan | 055840f | 2015-02-13 22:13:58 +0200 | [diff] [blame] | 2035 | if (show_options) { |
| 2036 | struct tcpstat t = {}; |
| 2037 | |
| 2038 | t.timer = r->idiag_timer; |
| 2039 | t.timeout = r->idiag_expires; |
| 2040 | t.retrans = r->idiag_retrans; |
| 2041 | tcp_timer_print(&t); |
| 2042 | } |
| 2043 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2044 | if (show_details) { |
Vadim Kochan | f1b39e1 | 2015-02-13 22:14:02 +0200 | [diff] [blame] | 2045 | sock_details_print(&s); |
Phil Sutter | f32dc74 | 2015-06-24 13:07:20 +0200 | [diff] [blame] | 2046 | if (s.local.family == AF_INET6 && tb[INET_DIAG_SKV6ONLY]) { |
| 2047 | unsigned char v6only; |
| 2048 | v6only = *(__u8 *)RTA_DATA(tb[INET_DIAG_SKV6ONLY]); |
| 2049 | printf(" v6only:%u", v6only); |
| 2050 | } |
Pavel Emelyanov | 5b81604 | 2013-05-17 08:02:14 -0700 | [diff] [blame] | 2051 | if (tb[INET_DIAG_SHUTDOWN]) { |
| 2052 | unsigned char mask; |
| 2053 | mask = *(__u8 *)RTA_DATA(tb[INET_DIAG_SHUTDOWN]); |
| 2054 | printf(" %c-%c", mask & 1 ? '-' : '<', mask & 2 ? '-' : '>'); |
| 2055 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2056 | } |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 2057 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2058 | if (show_mem || show_tcpinfo) { |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 2059 | printf("\n\t"); |
Pavel Emelyanov | 5b81604 | 2013-05-17 08:02:14 -0700 | [diff] [blame] | 2060 | tcp_show_info(nlh, r, tb); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2061 | } |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 2062 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2063 | printf("\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2064 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Pavel Emelyanov | 746a695 | 2012-10-25 03:21:39 +0000 | [diff] [blame] | 2067 | static int tcpdiag_send(int fd, int protocol, struct filter *f) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2068 | { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2069 | struct sockaddr_nl nladdr; |
| 2070 | struct { |
| 2071 | struct nlmsghdr nlh; |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 2072 | struct inet_diag_req r; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2073 | } req; |
| 2074 | char *bc = NULL; |
| 2075 | int bclen; |
| 2076 | struct msghdr msg; |
| 2077 | struct rtattr rta; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2078 | struct iovec iov[3]; |
| 2079 | |
Pavel Emelyanov | 346f8ca | 2012-10-25 03:24:58 +0000 | [diff] [blame] | 2080 | if (protocol == IPPROTO_UDP) |
| 2081 | return -1; |
| 2082 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2083 | memset(&nladdr, 0, sizeof(nladdr)); |
| 2084 | nladdr.nl_family = AF_NETLINK; |
| 2085 | |
| 2086 | req.nlh.nlmsg_len = sizeof(req); |
Pavel Emelyanov | 3fe5b53 | 2012-10-25 03:18:31 +0000 | [diff] [blame] | 2087 | if (protocol == IPPROTO_TCP) |
| 2088 | req.nlh.nlmsg_type = TCPDIAG_GETSOCK; |
| 2089 | else |
| 2090 | req.nlh.nlmsg_type = DCCPDIAG_GETSOCK; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2091 | req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST; |
| 2092 | req.nlh.nlmsg_pid = 0; |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 2093 | req.nlh.nlmsg_seq = MAGIC_SEQ; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2094 | memset(&req.r, 0, sizeof(req.r)); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 2095 | req.r.idiag_family = AF_INET; |
| 2096 | req.r.idiag_states = f->states; |
Shan Wei | 910b039 | 2012-05-03 16:39:52 +0800 | [diff] [blame] | 2097 | if (show_mem) { |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 2098 | req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1)); |
Shan Wei | 910b039 | 2012-05-03 16:39:52 +0800 | [diff] [blame] | 2099 | req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1)); |
| 2100 | } |
osdl.net!shemminger | b4b0b7d | 2004-09-28 18:14:03 +0000 | [diff] [blame] | 2101 | |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 2102 | if (show_tcpinfo) { |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 2103 | req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1)); |
| 2104 | req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1)); |
| 2105 | req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1)); |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 2106 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2107 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 2108 | iov[0] = (struct iovec){ |
| 2109 | .iov_base = &req, |
| 2110 | .iov_len = sizeof(req) |
shemminger | ea8fc10 | 2005-06-22 18:27:49 +0000 | [diff] [blame] | 2111 | }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2112 | if (f->f) { |
| 2113 | bclen = ssfilter_bytecompile(f->f, &bc); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 2114 | rta.rta_type = INET_DIAG_REQ_BYTECODE; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2115 | rta.rta_len = RTA_LENGTH(bclen); |
| 2116 | iov[1] = (struct iovec){ &rta, sizeof(rta) }; |
| 2117 | iov[2] = (struct iovec){ bc, bclen }; |
| 2118 | req.nlh.nlmsg_len += RTA_LENGTH(bclen); |
| 2119 | } |
| 2120 | |
| 2121 | msg = (struct msghdr) { |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 2122 | .msg_name = (void*)&nladdr, |
shemminger | ea8fc10 | 2005-06-22 18:27:49 +0000 | [diff] [blame] | 2123 | .msg_namelen = sizeof(nladdr), |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 2124 | .msg_iov = iov, |
shemminger | ea8fc10 | 2005-06-22 18:27:49 +0000 | [diff] [blame] | 2125 | .msg_iovlen = f->f ? 3 : 1, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2126 | }; |
| 2127 | |
Eric Dumazet | 930a75f | 2012-04-10 09:00:16 -0700 | [diff] [blame] | 2128 | if (sendmsg(fd, &msg, 0) < 0) { |
| 2129 | close(fd); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2130 | return -1; |
Eric Dumazet | 930a75f | 2012-04-10 09:00:16 -0700 | [diff] [blame] | 2131 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2132 | |
Pavel Emelyanov | 746a695 | 2012-10-25 03:21:39 +0000 | [diff] [blame] | 2133 | return 0; |
| 2134 | } |
| 2135 | |
Pavel Emelyanov | 886d19d | 2012-10-25 03:23:36 +0000 | [diff] [blame] | 2136 | static int sockdiag_send(int family, int fd, int protocol, struct filter *f) |
| 2137 | { |
| 2138 | struct sockaddr_nl nladdr; |
vadimk | 5fb421d | 2014-10-30 18:49:25 +0200 | [diff] [blame] | 2139 | DIAG_REQUEST(req, struct inet_diag_req_v2 r); |
Pavel Emelyanov | 886d19d | 2012-10-25 03:23:36 +0000 | [diff] [blame] | 2140 | char *bc = NULL; |
| 2141 | int bclen; |
| 2142 | struct msghdr msg; |
| 2143 | struct rtattr rta; |
| 2144 | struct iovec iov[3]; |
| 2145 | |
| 2146 | if (family == PF_UNSPEC) |
| 2147 | return tcpdiag_send(fd, protocol, f); |
| 2148 | |
| 2149 | memset(&nladdr, 0, sizeof(nladdr)); |
| 2150 | nladdr.nl_family = AF_NETLINK; |
| 2151 | |
Pavel Emelyanov | 886d19d | 2012-10-25 03:23:36 +0000 | [diff] [blame] | 2152 | memset(&req.r, 0, sizeof(req.r)); |
| 2153 | req.r.sdiag_family = family; |
| 2154 | req.r.sdiag_protocol = protocol; |
| 2155 | req.r.idiag_states = f->states; |
| 2156 | if (show_mem) { |
| 2157 | req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1)); |
| 2158 | req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1)); |
| 2159 | } |
| 2160 | |
| 2161 | if (show_tcpinfo) { |
| 2162 | req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1)); |
| 2163 | req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1)); |
| 2164 | req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1)); |
| 2165 | } |
| 2166 | |
| 2167 | iov[0] = (struct iovec){ |
| 2168 | .iov_base = &req, |
| 2169 | .iov_len = sizeof(req) |
| 2170 | }; |
| 2171 | if (f->f) { |
| 2172 | bclen = ssfilter_bytecompile(f->f, &bc); |
| 2173 | rta.rta_type = INET_DIAG_REQ_BYTECODE; |
| 2174 | rta.rta_len = RTA_LENGTH(bclen); |
| 2175 | iov[1] = (struct iovec){ &rta, sizeof(rta) }; |
| 2176 | iov[2] = (struct iovec){ bc, bclen }; |
| 2177 | req.nlh.nlmsg_len += RTA_LENGTH(bclen); |
| 2178 | } |
| 2179 | |
| 2180 | msg = (struct msghdr) { |
| 2181 | .msg_name = (void*)&nladdr, |
| 2182 | .msg_namelen = sizeof(nladdr), |
| 2183 | .msg_iov = iov, |
| 2184 | .msg_iovlen = f->f ? 3 : 1, |
| 2185 | }; |
| 2186 | |
| 2187 | if (sendmsg(fd, &msg, 0) < 0) { |
| 2188 | close(fd); |
| 2189 | return -1; |
| 2190 | } |
| 2191 | |
| 2192 | return 0; |
| 2193 | } |
| 2194 | |
Vadim Kochan | 486ccd9 | 2014-12-26 04:26:27 +0200 | [diff] [blame] | 2195 | struct inet_diag_arg { |
| 2196 | struct filter *f; |
| 2197 | int protocol; |
| 2198 | }; |
| 2199 | |
| 2200 | static int show_one_inet_sock(const struct sockaddr_nl *addr, |
| 2201 | struct nlmsghdr *h, void *arg) |
| 2202 | { |
| 2203 | int err; |
| 2204 | struct inet_diag_arg *diag_arg = arg; |
| 2205 | struct inet_diag_msg *r = NLMSG_DATA(h); |
| 2206 | |
| 2207 | if (!(diag_arg->f->families & (1 << r->idiag_family))) |
| 2208 | return 0; |
Craig Gallek | 6885e3b | 2015-06-17 11:14:48 -0400 | [diff] [blame] | 2209 | if ((err = inet_show_sock(h, diag_arg->f, diag_arg->protocol)) < 0) |
Vadim Kochan | 486ccd9 | 2014-12-26 04:26:27 +0200 | [diff] [blame] | 2210 | return err; |
| 2211 | |
| 2212 | return 0; |
| 2213 | } |
| 2214 | |
Pavel Emelyanov | 746a695 | 2012-10-25 03:21:39 +0000 | [diff] [blame] | 2215 | static int inet_show_netlink(struct filter *f, FILE *dump_fp, int protocol) |
| 2216 | { |
Vadim Kochan | 486ccd9 | 2014-12-26 04:26:27 +0200 | [diff] [blame] | 2217 | int err = 0; |
| 2218 | struct rtnl_handle rth; |
| 2219 | int family = PF_INET; |
| 2220 | struct inet_diag_arg arg = { .f = f, .protocol = protocol }; |
Pavel Emelyanov | 746a695 | 2012-10-25 03:21:39 +0000 | [diff] [blame] | 2221 | |
Vadim Kochan | 486ccd9 | 2014-12-26 04:26:27 +0200 | [diff] [blame] | 2222 | if (rtnl_open_byproto(&rth, 0, NETLINK_SOCK_DIAG)) |
Pavel Emelyanov | 746a695 | 2012-10-25 03:21:39 +0000 | [diff] [blame] | 2223 | return -1; |
Vadim Kochan | 486ccd9 | 2014-12-26 04:26:27 +0200 | [diff] [blame] | 2224 | rth.dump = MAGIC_SEQ; |
| 2225 | rth.dump_fp = dump_fp; |
Eric Dumazet | 518af1e | 2015-05-29 05:37:49 -0700 | [diff] [blame] | 2226 | if (preferred_family == PF_INET6) |
| 2227 | family = PF_INET6; |
Pavel Emelyanov | 746a695 | 2012-10-25 03:21:39 +0000 | [diff] [blame] | 2228 | |
Pavel Emelyanov | 886d19d | 2012-10-25 03:23:36 +0000 | [diff] [blame] | 2229 | again: |
Vadim Kochan | 486ccd9 | 2014-12-26 04:26:27 +0200 | [diff] [blame] | 2230 | if ((err = sockdiag_send(family, rth.fd, protocol, f))) |
| 2231 | goto Exit; |
Pavel Emelyanov | 746a695 | 2012-10-25 03:21:39 +0000 | [diff] [blame] | 2232 | |
Vadim Kochan | 486ccd9 | 2014-12-26 04:26:27 +0200 | [diff] [blame] | 2233 | if ((err = rtnl_dump_filter(&rth, show_one_inet_sock, &arg))) { |
| 2234 | if (family != PF_UNSPEC) { |
| 2235 | family = PF_UNSPEC; |
| 2236 | goto again; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2237 | } |
Vadim Kochan | 486ccd9 | 2014-12-26 04:26:27 +0200 | [diff] [blame] | 2238 | goto Exit; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2239 | } |
Eric Dumazet | 518af1e | 2015-05-29 05:37:49 -0700 | [diff] [blame] | 2240 | if (family == PF_INET && preferred_family != PF_INET) { |
Pavel Emelyanov | 886d19d | 2012-10-25 03:23:36 +0000 | [diff] [blame] | 2241 | family = PF_INET6; |
| 2242 | goto again; |
| 2243 | } |
| 2244 | |
Vadim Kochan | 486ccd9 | 2014-12-26 04:26:27 +0200 | [diff] [blame] | 2245 | Exit: |
| 2246 | rtnl_close(&rth); |
| 2247 | return err; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2250 | static int tcp_show_netlink_file(struct filter *f) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2251 | { |
| 2252 | FILE *fp; |
Eric Dumazet | e557212 | 2014-10-11 09:43:13 -0700 | [diff] [blame] | 2253 | char buf[16384]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2254 | |
| 2255 | if ((fp = fopen(getenv("TCPDIAG_FILE"), "r")) == NULL) { |
| 2256 | perror("fopen($TCPDIAG_FILE)"); |
| 2257 | return -1; |
| 2258 | } |
| 2259 | |
| 2260 | while (1) { |
| 2261 | int status, err; |
| 2262 | struct nlmsghdr *h = (struct nlmsghdr*)buf; |
| 2263 | |
| 2264 | status = fread(buf, 1, sizeof(*h), fp); |
| 2265 | if (status < 0) { |
| 2266 | perror("Reading header from $TCPDIAG_FILE"); |
| 2267 | return -1; |
| 2268 | } |
| 2269 | if (status != sizeof(*h)) { |
| 2270 | perror("Unexpected EOF reading $TCPDIAG_FILE"); |
| 2271 | return -1; |
| 2272 | } |
| 2273 | |
| 2274 | status = fread(h+1, 1, NLMSG_ALIGN(h->nlmsg_len-sizeof(*h)), fp); |
| 2275 | |
| 2276 | if (status < 0) { |
| 2277 | perror("Reading $TCPDIAG_FILE"); |
| 2278 | return -1; |
| 2279 | } |
| 2280 | if (status + sizeof(*h) < h->nlmsg_len) { |
| 2281 | perror("Unexpected EOF reading $TCPDIAG_FILE"); |
| 2282 | return -1; |
| 2283 | } |
| 2284 | |
| 2285 | /* The only legal exit point */ |
| 2286 | if (h->nlmsg_type == NLMSG_DONE) |
| 2287 | return 0; |
| 2288 | |
| 2289 | if (h->nlmsg_type == NLMSG_ERROR) { |
| 2290 | struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); |
| 2291 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { |
| 2292 | fprintf(stderr, "ERROR truncated\n"); |
| 2293 | } else { |
| 2294 | errno = -err->error; |
| 2295 | perror("TCPDIAG answered"); |
| 2296 | } |
| 2297 | return -1; |
| 2298 | } |
| 2299 | |
Pavel Emelyanov | 77a8ca8 | 2014-01-28 10:24:15 +0400 | [diff] [blame] | 2300 | err = inet_show_sock(h, f, IPPROTO_TCP); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2301 | if (err < 0) |
| 2302 | return err; |
| 2303 | } |
| 2304 | } |
| 2305 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2306 | static int tcp_show(struct filter *f, int socktype) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2307 | { |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2308 | FILE *fp = NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2309 | char *buf = NULL; |
| 2310 | int bufsize = 64*1024; |
| 2311 | |
Vadim Kochan | 1527a17 | 2015-02-13 13:01:08 +0200 | [diff] [blame] | 2312 | if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) |
| 2313 | return 0; |
| 2314 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2315 | dg_proto = TCP_PROTO; |
| 2316 | |
| 2317 | if (getenv("TCPDIAG_FILE")) |
| 2318 | return tcp_show_netlink_file(f); |
| 2319 | |
| 2320 | if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT") |
Pavel Emelyanov | 3fe5b53 | 2012-10-25 03:18:31 +0000 | [diff] [blame] | 2321 | && inet_show_netlink(f, NULL, socktype) == 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2322 | return 0; |
| 2323 | |
| 2324 | /* Sigh... We have to parse /proc/net/tcp... */ |
| 2325 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2326 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2327 | /* Estimate amount of sockets and try to allocate |
| 2328 | * huge buffer to read all the table at one read. |
| 2329 | * Limit it by 16MB though. The assumption is: as soon as |
| 2330 | * kernel was able to hold information about N connections, |
| 2331 | * it is able to give us some memory for snapshot. |
| 2332 | */ |
| 2333 | if (1) { |
Bryton Lee | a221d62 | 2015-02-12 14:16:04 +0800 | [diff] [blame] | 2334 | get_slabstat(&slabstat); |
| 2335 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2336 | int guess = slabstat.socks+slabstat.tcp_syns; |
| 2337 | if (f->states&(1<<SS_TIME_WAIT)) |
| 2338 | guess += slabstat.tcp_tws; |
| 2339 | if (guess > (16*1024*1024)/128) |
| 2340 | guess = (16*1024*1024)/128; |
| 2341 | guess *= 128; |
| 2342 | if (guess > bufsize) |
| 2343 | bufsize = guess; |
| 2344 | } |
| 2345 | while (bufsize >= 64*1024) { |
| 2346 | if ((buf = malloc(bufsize)) != NULL) |
| 2347 | break; |
| 2348 | bufsize /= 2; |
| 2349 | } |
| 2350 | if (buf == NULL) { |
| 2351 | errno = ENOMEM; |
| 2352 | return -1; |
| 2353 | } |
| 2354 | |
| 2355 | if (f->families & (1<<AF_INET)) { |
Björn Steinbrink | 69cae64 | 2008-04-03 11:42:41 +0200 | [diff] [blame] | 2356 | if ((fp = net_tcp_open()) == NULL) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2357 | goto outerr; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2358 | |
| 2359 | setbuffer(fp, buf, bufsize); |
| 2360 | if (generic_record_read(fp, tcp_show_line, f, AF_INET)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2361 | goto outerr; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2362 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
| 2365 | if ((f->families & (1<<AF_INET6)) && |
Björn Steinbrink | 69cae64 | 2008-04-03 11:42:41 +0200 | [diff] [blame] | 2366 | (fp = net_tcp6_open()) != NULL) { |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2367 | setbuffer(fp, buf, bufsize); |
| 2368 | if (generic_record_read(fp, tcp_show_line, f, AF_INET6)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2369 | goto outerr; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2370 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
| 2373 | free(buf); |
| 2374 | return 0; |
| 2375 | |
| 2376 | outerr: |
| 2377 | do { |
| 2378 | int saved_errno = errno; |
Stephen Hemminger | 92de1c2 | 2015-07-21 10:49:54 -0700 | [diff] [blame] | 2379 | free(buf); |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2380 | if (fp) |
| 2381 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2382 | errno = saved_errno; |
| 2383 | return -1; |
| 2384 | } while (0); |
| 2385 | } |
| 2386 | |
| 2387 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 2388 | static 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] | 2389 | { |
Vadim Kochan | 055840f | 2015-02-13 22:13:58 +0200 | [diff] [blame] | 2390 | struct sockstat s = {}; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2391 | char *loc, *rem, *data; |
| 2392 | char opt[256]; |
| 2393 | int n; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2394 | |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 2395 | if (proc_inet_split_line(line, &loc, &rem, &data)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2396 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2397 | |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 2398 | int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0'); |
| 2399 | if (!(f->states & (1 << state))) |
| 2400 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2401 | |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 2402 | proc_parse_inet_addr(loc, rem, family, &s); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2403 | |
| 2404 | if (f->f && run_ssfilter(f->f, &s) == 0) |
| 2405 | return 0; |
| 2406 | |
| 2407 | opt[0] = 0; |
Stephen Hemminger | e7113c6 | 2007-07-10 18:26:54 -0700 | [diff] [blame] | 2408 | 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] | 2409 | &s.state, &s.wq, &s.rq, |
| 2410 | &s.uid, &s.ino, |
| 2411 | &s.refcnt, &s.sk, opt); |
| 2412 | |
| 2413 | if (n < 9) |
| 2414 | opt[0] = 0; |
| 2415 | |
Nikolay Aleksandrov | 235c445 | 2015-07-15 05:50:42 -0700 | [diff] [blame] | 2416 | inet_stats_print(&s, dg_proto == UDP_PROTO ? IPPROTO_UDP : 0); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2417 | |
Vadim Kochan | f1b39e1 | 2015-02-13 22:14:02 +0200 | [diff] [blame] | 2418 | if (show_details && opt[0]) |
| 2419 | printf(" opt:\"%s\"", opt); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2420 | |
Vadim Kochan | 8250bc9 | 2015-01-20 16:14:24 +0200 | [diff] [blame] | 2421 | printf("\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2422 | return 0; |
| 2423 | } |
| 2424 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 2425 | static int udp_show(struct filter *f) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2426 | { |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2427 | FILE *fp = NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2428 | |
Vadim Kochan | 1527a17 | 2015-02-13 13:01:08 +0200 | [diff] [blame] | 2429 | if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) |
| 2430 | return 0; |
| 2431 | |
Vadim Kochan | ace5cb3 | 2015-01-08 02:42:54 +0200 | [diff] [blame] | 2432 | dg_proto = UDP_PROTO; |
| 2433 | |
Pavel Emelyanov | 346f8ca | 2012-10-25 03:24:58 +0000 | [diff] [blame] | 2434 | if (!getenv("PROC_NET_UDP") && !getenv("PROC_ROOT") |
| 2435 | && inet_show_netlink(f, NULL, IPPROTO_UDP) == 0) |
| 2436 | return 0; |
| 2437 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2438 | if (f->families&(1<<AF_INET)) { |
Björn Steinbrink | 69cae64 | 2008-04-03 11:42:41 +0200 | [diff] [blame] | 2439 | if ((fp = net_udp_open()) == NULL) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2440 | goto outerr; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2441 | if (generic_record_read(fp, dgram_show_line, f, AF_INET)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2442 | goto outerr; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2443 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
| 2446 | if ((f->families&(1<<AF_INET6)) && |
Björn Steinbrink | 69cae64 | 2008-04-03 11:42:41 +0200 | [diff] [blame] | 2447 | (fp = net_udp6_open()) != NULL) { |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2448 | if (generic_record_read(fp, dgram_show_line, f, AF_INET6)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2449 | goto outerr; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2450 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2451 | } |
| 2452 | return 0; |
| 2453 | |
| 2454 | outerr: |
| 2455 | do { |
| 2456 | int saved_errno = errno; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2457 | if (fp) |
| 2458 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2459 | errno = saved_errno; |
| 2460 | return -1; |
| 2461 | } while (0); |
| 2462 | } |
| 2463 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 2464 | static int raw_show(struct filter *f) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2465 | { |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2466 | FILE *fp = NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2467 | |
Vadim Kochan | 1527a17 | 2015-02-13 13:01:08 +0200 | [diff] [blame] | 2468 | if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6)) |
| 2469 | return 0; |
| 2470 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2471 | dg_proto = RAW_PROTO; |
| 2472 | |
| 2473 | if (f->families&(1<<AF_INET)) { |
Björn Steinbrink | 69cae64 | 2008-04-03 11:42:41 +0200 | [diff] [blame] | 2474 | if ((fp = net_raw_open()) == NULL) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2475 | goto outerr; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2476 | if (generic_record_read(fp, dgram_show_line, f, AF_INET)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2477 | goto outerr; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2478 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
| 2481 | if ((f->families&(1<<AF_INET6)) && |
Björn Steinbrink | 69cae64 | 2008-04-03 11:42:41 +0200 | [diff] [blame] | 2482 | (fp = net_raw6_open()) != NULL) { |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2483 | if (generic_record_read(fp, dgram_show_line, f, AF_INET6)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2484 | goto outerr; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2485 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2486 | } |
| 2487 | return 0; |
| 2488 | |
| 2489 | outerr: |
| 2490 | do { |
| 2491 | int saved_errno = errno; |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2492 | if (fp) |
| 2493 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2494 | errno = saved_errno; |
| 2495 | return -1; |
| 2496 | } while (0); |
| 2497 | } |
| 2498 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2499 | int unix_state_map[] = { SS_CLOSE, SS_SYN_SENT, |
| 2500 | SS_ESTABLISHED, SS_CLOSING }; |
| 2501 | |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 2502 | #define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct sockstat)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2503 | |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 2504 | static void unix_list_free(struct sockstat *list) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2505 | { |
| 2506 | while (list) { |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 2507 | struct sockstat *s = list; |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 2508 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2509 | list = list->next; |
Vadim Kochan | 99bb68f | 2015-07-21 10:53:19 -0700 | [diff] [blame] | 2510 | free(s->name); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2511 | free(s); |
| 2512 | } |
| 2513 | } |
| 2514 | |
Masatake YAMATO | 30b669d | 2014-01-08 20:13:46 +0900 | [diff] [blame] | 2515 | static const char *unix_netid_name(int type) |
| 2516 | { |
| 2517 | const char *netid; |
| 2518 | |
| 2519 | switch (type) { |
| 2520 | case SOCK_STREAM: |
| 2521 | netid = "u_str"; |
| 2522 | break; |
| 2523 | case SOCK_SEQPACKET: |
| 2524 | netid = "u_seq"; |
| 2525 | break; |
| 2526 | case SOCK_DGRAM: |
| 2527 | default: |
| 2528 | netid = "u_dgr"; |
| 2529 | break; |
| 2530 | } |
| 2531 | return netid; |
| 2532 | } |
| 2533 | |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 2534 | static bool unix_type_skip(struct sockstat *s, struct filter *f) |
Vadim Kochan | bf4ceee | 2015-01-04 22:18:38 +0200 | [diff] [blame] | 2535 | { |
| 2536 | if (s->type == SOCK_STREAM && !(f->dbs&(1<<UNIX_ST_DB))) |
| 2537 | return true; |
| 2538 | if (s->type == SOCK_DGRAM && !(f->dbs&(1<<UNIX_DG_DB))) |
| 2539 | return true; |
| 2540 | if (s->type == SOCK_SEQPACKET && !(f->dbs&(1<<UNIX_SQ_DB))) |
| 2541 | return true; |
| 2542 | return false; |
| 2543 | } |
| 2544 | |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 2545 | static bool unix_use_proc(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2546 | { |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 2547 | return getenv("PROC_NET_UNIX") || getenv("PROC_ROOT"); |
| 2548 | } |
| 2549 | |
| 2550 | static void unix_stats_print(struct sockstat *list, struct filter *f) |
| 2551 | { |
| 2552 | struct sockstat *s; |
Vadim Kochan | 99bb68f | 2015-07-21 10:53:19 -0700 | [diff] [blame] | 2553 | char *peer; |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 2554 | char *ctx_buf = NULL; |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 2555 | bool use_proc = unix_use_proc(); |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 2556 | char port_name[30] = {}; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2557 | |
| 2558 | for (s = list; s; s = s->next) { |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 2559 | if (!(f->states & (1 << s->state))) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2560 | continue; |
Vadim Kochan | bf4ceee | 2015-01-04 22:18:38 +0200 | [diff] [blame] | 2561 | if (unix_type_skip(s, f)) |
Masatake YAMATO | 30b669d | 2014-01-08 20:13:46 +0900 | [diff] [blame] | 2562 | continue; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2563 | |
Vadim Kochan | 99bb68f | 2015-07-21 10:53:19 -0700 | [diff] [blame] | 2564 | peer = "*"; |
| 2565 | if (s->peer_name) |
| 2566 | peer = s->peer_name; |
Vadim Kochan | bf4ceee | 2015-01-04 22:18:38 +0200 | [diff] [blame] | 2567 | |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 2568 | if (s->rport && use_proc) { |
| 2569 | struct sockstat *p; |
| 2570 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2571 | for (p = list; p; p = p->next) { |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 2572 | if (s->rport == p->lport) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2573 | break; |
| 2574 | } |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 2575 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2576 | if (!p) { |
| 2577 | peer = "?"; |
| 2578 | } else { |
Vadim Kochan | 99bb68f | 2015-07-21 10:53:19 -0700 | [diff] [blame] | 2579 | peer = p->name ? : "*"; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2580 | } |
| 2581 | } |
| 2582 | |
Vadim Kochan | 29999b0 | 2015-02-22 22:23:10 +0200 | [diff] [blame] | 2583 | if (use_proc && f->f) { |
Vadim Kochan | 99bb68f | 2015-07-21 10:53:19 -0700 | [diff] [blame] | 2584 | struct sockstat st; |
| 2585 | st.local.family = AF_UNIX; |
| 2586 | st.remote.family = AF_UNIX; |
| 2587 | memcpy(st.local.data, &s->name, sizeof(s->name)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2588 | if (strcmp(peer, "*") == 0) |
Vadim Kochan | 99bb68f | 2015-07-21 10:53:19 -0700 | [diff] [blame] | 2589 | memset(st.remote.data, 0, sizeof(peer)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2590 | else |
Vadim Kochan | 99bb68f | 2015-07-21 10:53:19 -0700 | [diff] [blame] | 2591 | memcpy(st.remote.data, &peer, sizeof(peer)); |
| 2592 | if (run_ssfilter(f->f, &st) == 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2593 | continue; |
| 2594 | } |
| 2595 | |
Vadim Kochan | 2d791bc | 2015-02-13 22:14:01 +0200 | [diff] [blame] | 2596 | sock_state_print(s, unix_netid_name(s->type)); |
| 2597 | |
Vadim Kochan | 99bb68f | 2015-07-21 10:53:19 -0700 | [diff] [blame] | 2598 | sock_addr_print(s->name ?: "*", " ", |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 2599 | int_to_str(s->lport, port_name), NULL); |
| 2600 | sock_addr_print(peer, " ", int_to_str(s->rport, port_name), |
| 2601 | NULL); |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 2602 | |
| 2603 | if (show_proc_ctx || show_sock_ctx) { |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 2604 | if (find_entry(s->ino, &ctx_buf, |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 2605 | (show_proc_ctx & show_sock_ctx) ? |
| 2606 | PROC_SOCK_CTX : PROC_CTX) > 0) { |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 2607 | printf(" users:(%s)", ctx_buf); |
| 2608 | free(ctx_buf); |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 2609 | } |
| 2610 | } else if (show_users) { |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 2611 | if (find_entry(s->ino, &ctx_buf, USERS) > 0) { |
| 2612 | printf(" users:(%s)", ctx_buf); |
| 2613 | free(ctx_buf); |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 2614 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2615 | } |
| 2616 | printf("\n"); |
| 2617 | } |
| 2618 | } |
| 2619 | |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 2620 | static int unix_show_sock(const struct sockaddr_nl *addr, struct nlmsghdr *nlh, |
| 2621 | void *arg) |
Pavel Emelyanov | dfbaa90 | 2011-12-15 03:28:15 +0000 | [diff] [blame] | 2622 | { |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 2623 | struct filter *f = (struct filter *)arg; |
Pavel Emelyanov | dfbaa90 | 2011-12-15 03:28:15 +0000 | [diff] [blame] | 2624 | struct unix_diag_msg *r = NLMSG_DATA(nlh); |
| 2625 | struct rtattr *tb[UNIX_DIAG_MAX+1]; |
Vadim Kochan | 99bb68f | 2015-07-21 10:53:19 -0700 | [diff] [blame] | 2626 | char name[128]; |
| 2627 | struct sockstat stat = { .name = "*", .peer_name = "*" }; |
Pavel Emelyanov | dfbaa90 | 2011-12-15 03:28:15 +0000 | [diff] [blame] | 2628 | |
| 2629 | parse_rtattr(tb, UNIX_DIAG_MAX, (struct rtattr*)(r+1), |
| 2630 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); |
| 2631 | |
Vadim Kochan | bf4ceee | 2015-01-04 22:18:38 +0200 | [diff] [blame] | 2632 | stat.type = r->udiag_type; |
| 2633 | stat.state = r->udiag_state; |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 2634 | stat.ino = stat.lport = r->udiag_ino; |
| 2635 | stat.local.family = stat.remote.family = AF_UNIX; |
Masatake YAMATO | 0d2e01c | 2014-01-08 20:13:47 +0900 | [diff] [blame] | 2636 | |
Vadim Kochan | bf4ceee | 2015-01-04 22:18:38 +0200 | [diff] [blame] | 2637 | if (unix_type_skip(&stat, f)) |
| 2638 | return 0; |
Pavel Emelyanov | dfbaa90 | 2011-12-15 03:28:15 +0000 | [diff] [blame] | 2639 | |
Hannes Frederic Sowa | defd61c | 2013-02-22 15:28:10 +0000 | [diff] [blame] | 2640 | if (tb[UNIX_DIAG_RQLEN]) { |
| 2641 | struct unix_diag_rqlen *rql = RTA_DATA(tb[UNIX_DIAG_RQLEN]); |
Vadim Kochan | bf4ceee | 2015-01-04 22:18:38 +0200 | [diff] [blame] | 2642 | stat.rq = rql->udiag_rqueue; |
| 2643 | stat.wq = rql->udiag_wqueue; |
Hannes Frederic Sowa | defd61c | 2013-02-22 15:28:10 +0000 | [diff] [blame] | 2644 | } |
Pavel Emelyanov | dfbaa90 | 2011-12-15 03:28:15 +0000 | [diff] [blame] | 2645 | if (tb[UNIX_DIAG_NAME]) { |
| 2646 | int len = RTA_PAYLOAD(tb[UNIX_DIAG_NAME]); |
| 2647 | |
| 2648 | memcpy(name, RTA_DATA(tb[UNIX_DIAG_NAME]), len); |
| 2649 | name[len] = '\0'; |
| 2650 | if (name[0] == '\0') |
| 2651 | name[0] = '@'; |
Vadim Kochan | 99bb68f | 2015-07-21 10:53:19 -0700 | [diff] [blame] | 2652 | stat.name = &name[0]; |
| 2653 | memcpy(stat.local.data, &stat.name, sizeof(stat.name)); |
Pavel Emelyanov | dfbaa90 | 2011-12-15 03:28:15 +0000 | [diff] [blame] | 2654 | } |
Vadim Kochan | bf4ceee | 2015-01-04 22:18:38 +0200 | [diff] [blame] | 2655 | if (tb[UNIX_DIAG_PEER]) |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 2656 | stat.rport = rta_getattr_u32(tb[UNIX_DIAG_PEER]); |
Vadim Kochan | bf4ceee | 2015-01-04 22:18:38 +0200 | [diff] [blame] | 2657 | |
Vadim Kochan | 29999b0 | 2015-02-22 22:23:10 +0200 | [diff] [blame] | 2658 | if (f->f && run_ssfilter(f->f, &stat) == 0) |
| 2659 | return 0; |
| 2660 | |
Vadim Kochan | bf4ceee | 2015-01-04 22:18:38 +0200 | [diff] [blame] | 2661 | unix_stats_print(&stat, f); |
Pavel Emelyanov | dfbaa90 | 2011-12-15 03:28:15 +0000 | [diff] [blame] | 2662 | |
Hannes Frederic Sowa | 51ff9f2 | 2013-02-22 15:28:18 +0000 | [diff] [blame] | 2663 | if (show_mem) { |
Vadim Kochan | bf4ceee | 2015-01-04 22:18:38 +0200 | [diff] [blame] | 2664 | printf("\t"); |
Hannes Frederic Sowa | 51ff9f2 | 2013-02-22 15:28:18 +0000 | [diff] [blame] | 2665 | print_skmeminfo(tb, UNIX_DIAG_MEMINFO); |
| 2666 | } |
Pavel Emelyanov | 5b81604 | 2013-05-17 08:02:14 -0700 | [diff] [blame] | 2667 | if (show_details) { |
| 2668 | if (tb[UNIX_DIAG_SHUTDOWN]) { |
| 2669 | unsigned char mask; |
| 2670 | mask = *(__u8 *)RTA_DATA(tb[UNIX_DIAG_SHUTDOWN]); |
| 2671 | printf(" %c-%c", mask & 1 ? '-' : '<', mask & 2 ? '-' : '>'); |
| 2672 | } |
| 2673 | } |
Vadim Kochan | bf4ceee | 2015-01-04 22:18:38 +0200 | [diff] [blame] | 2674 | if (show_mem || show_details) |
| 2675 | printf("\n"); |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 2676 | |
Pavel Emelyanov | dfbaa90 | 2011-12-15 03:28:15 +0000 | [diff] [blame] | 2677 | return 0; |
| 2678 | } |
| 2679 | |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 2680 | static int handle_netlink_request(struct filter *f, struct nlmsghdr *req, |
| 2681 | size_t size, rtnl_filter_t show_one_sock) |
Pavel Emelyanov | dfbaa90 | 2011-12-15 03:28:15 +0000 | [diff] [blame] | 2682 | { |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 2683 | int ret = -1; |
| 2684 | struct rtnl_handle rth; |
Pavel Emelyanov | dfbaa90 | 2011-12-15 03:28:15 +0000 | [diff] [blame] | 2685 | |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 2686 | if (rtnl_open_byproto(&rth, 0, NETLINK_SOCK_DIAG)) |
Pavel Emelyanov | dfbaa90 | 2011-12-15 03:28:15 +0000 | [diff] [blame] | 2687 | return -1; |
| 2688 | |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 2689 | rth.dump = MAGIC_SEQ; |
Pavel Emelyanov | dfbaa90 | 2011-12-15 03:28:15 +0000 | [diff] [blame] | 2690 | |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 2691 | if (rtnl_send(&rth, req, size) < 0) |
| 2692 | goto Exit; |
Pavel Emelyanov | dfbaa90 | 2011-12-15 03:28:15 +0000 | [diff] [blame] | 2693 | |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 2694 | if (rtnl_dump_filter(&rth, show_one_sock, f)) |
| 2695 | goto Exit; |
Pavel Emelyanov | dfbaa90 | 2011-12-15 03:28:15 +0000 | [diff] [blame] | 2696 | |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 2697 | ret = 0; |
| 2698 | Exit: |
| 2699 | rtnl_close(&rth); |
| 2700 | return ret; |
Pavel Emelyanov | dfbaa90 | 2011-12-15 03:28:15 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 2703 | static int unix_show_netlink(struct filter *f) |
Andrey Vagin | d8402b9 | 2013-06-05 12:41:58 +0400 | [diff] [blame] | 2704 | { |
vadimk | 5fb421d | 2014-10-30 18:49:25 +0200 | [diff] [blame] | 2705 | DIAG_REQUEST(req, struct unix_diag_req r); |
Andrey Vagin | d8402b9 | 2013-06-05 12:41:58 +0400 | [diff] [blame] | 2706 | |
| 2707 | req.r.sdiag_family = AF_UNIX; |
| 2708 | req.r.udiag_states = f->states; |
| 2709 | req.r.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER | UDIAG_SHOW_RQLEN; |
| 2710 | if (show_mem) |
| 2711 | req.r.udiag_show |= UDIAG_SHOW_MEMINFO; |
| 2712 | |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 2713 | return handle_netlink_request(f, &req.nlh, sizeof(req), unix_show_sock); |
Andrey Vagin | d8402b9 | 2013-06-05 12:41:58 +0400 | [diff] [blame] | 2714 | } |
| 2715 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 2716 | static int unix_show(struct filter *f) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2717 | { |
| 2718 | FILE *fp; |
| 2719 | char buf[256]; |
| 2720 | char name[128]; |
| 2721 | int newformat = 0; |
| 2722 | int cnt; |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 2723 | struct sockstat *list = NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2724 | |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 2725 | if (!filter_af_get(f, AF_UNIX)) |
| 2726 | return 0; |
| 2727 | |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 2728 | if (!unix_use_proc() && unix_show_netlink(f) == 0) |
Pavel Emelyanov | dfbaa90 | 2011-12-15 03:28:15 +0000 | [diff] [blame] | 2729 | return 0; |
| 2730 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 2731 | if ((fp = net_unix_open()) == NULL) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2732 | return -1; |
| 2733 | fgets(buf, sizeof(buf)-1, fp); |
| 2734 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 2735 | if (memcmp(buf, "Peer", 4) == 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2736 | newformat = 1; |
| 2737 | cnt = 0; |
| 2738 | |
| 2739 | while (fgets(buf, sizeof(buf)-1, fp)) { |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 2740 | struct sockstat *u, **insp; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2741 | int flags; |
| 2742 | |
willy tarreau | 0ee9052 | 2015-10-06 12:09:33 +0200 | [diff] [blame] | 2743 | if (!(u = calloc(1, sizeof(*u)))) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2744 | break; |
Vadim Kochan | 99bb68f | 2015-07-21 10:53:19 -0700 | [diff] [blame] | 2745 | u->name = NULL; |
| 2746 | u->peer_name = NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2747 | |
| 2748 | if (sscanf(buf, "%x: %x %x %x %x %x %d %s", |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 2749 | &u->rport, &u->rq, &u->wq, &flags, &u->type, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2750 | &u->state, &u->ino, name) < 8) |
| 2751 | name[0] = 0; |
| 2752 | |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 2753 | u->lport = u->ino; |
| 2754 | u->local.family = u->remote.family = AF_UNIX; |
| 2755 | |
| 2756 | if (flags & (1 << 16)) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2757 | u->state = SS_LISTEN; |
| 2758 | } else { |
| 2759 | u->state = unix_state_map[u->state-1]; |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 2760 | if (u->type == SOCK_DGRAM && u->state == SS_CLOSE && u->rport) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2761 | u->state = SS_ESTABLISHED; |
| 2762 | } |
| 2763 | |
| 2764 | if (!newformat) { |
Vadim Kochan | ec4d0d8 | 2015-02-13 22:14:00 +0200 | [diff] [blame] | 2765 | u->rport = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2766 | u->rq = 0; |
| 2767 | u->wq = 0; |
| 2768 | } |
| 2769 | |
| 2770 | insp = &list; |
| 2771 | while (*insp) { |
| 2772 | if (u->type < (*insp)->type || |
| 2773 | (u->type == (*insp)->type && |
| 2774 | u->ino < (*insp)->ino)) |
| 2775 | break; |
| 2776 | insp = &(*insp)->next; |
| 2777 | } |
| 2778 | u->next = *insp; |
| 2779 | *insp = u; |
| 2780 | |
| 2781 | if (name[0]) { |
Vadim Kochan | 99bb68f | 2015-07-21 10:53:19 -0700 | [diff] [blame] | 2782 | if ((u->name = malloc(strlen(name)+1)) == NULL) |
| 2783 | break; |
| 2784 | strcpy(u->name, name); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2785 | } |
| 2786 | if (++cnt > MAX_UNIX_REMEMBER) { |
Vadim Kochan | bf4ceee | 2015-01-04 22:18:38 +0200 | [diff] [blame] | 2787 | unix_stats_print(list, f); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2788 | unix_list_free(list); |
| 2789 | list = NULL; |
| 2790 | cnt = 0; |
| 2791 | } |
| 2792 | } |
Eric Dumazet | a3fd8e5 | 2012-01-30 17:05:45 +0100 | [diff] [blame] | 2793 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2794 | if (list) { |
Vadim Kochan | bf4ceee | 2015-01-04 22:18:38 +0200 | [diff] [blame] | 2795 | unix_stats_print(list, f); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 2796 | unix_list_free(list); |
| 2797 | list = NULL; |
| 2798 | cnt = 0; |
| 2799 | } |
| 2800 | |
| 2801 | return 0; |
| 2802 | } |
| 2803 | |
Vadim Kochan | 89f634f | 2015-02-13 22:13:59 +0200 | [diff] [blame] | 2804 | static int packet_stats_print(struct sockstat *s, const struct filter *f) |
Vadim Kochan | 4a0053b | 2015-01-04 22:18:39 +0200 | [diff] [blame] | 2805 | { |
| 2806 | char *buf = NULL; |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 2807 | const char *addr, *port; |
| 2808 | char ll_name[16]; |
Vadim Kochan | 4a0053b | 2015-01-04 22:18:39 +0200 | [diff] [blame] | 2809 | |
| 2810 | if (f->f) { |
Vadim Kochan | 89f634f | 2015-02-13 22:13:59 +0200 | [diff] [blame] | 2811 | s->local.family = AF_PACKET; |
| 2812 | s->remote.family = AF_PACKET; |
Vadim Kochan | 89f634f | 2015-02-13 22:13:59 +0200 | [diff] [blame] | 2813 | s->local.data[0] = s->prot; |
Vadim Kochan | 89f634f | 2015-02-13 22:13:59 +0200 | [diff] [blame] | 2814 | if (run_ssfilter(f->f, s) == 0) |
Vadim Kochan | 4a0053b | 2015-01-04 22:18:39 +0200 | [diff] [blame] | 2815 | return 1; |
| 2816 | } |
| 2817 | |
Vadim Kochan | 2d791bc | 2015-02-13 22:14:01 +0200 | [diff] [blame] | 2818 | sock_state_print(s, s->type == SOCK_RAW ? "p_raw" : "p_dgr"); |
Vadim Kochan | 4a0053b | 2015-01-04 22:18:39 +0200 | [diff] [blame] | 2819 | |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 2820 | if (s->prot == 3) |
| 2821 | addr = "*"; |
| 2822 | else |
| 2823 | addr = ll_proto_n2a(htons(s->prot), ll_name, sizeof(ll_name)); |
Vadim Kochan | 4a0053b | 2015-01-04 22:18:39 +0200 | [diff] [blame] | 2824 | |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 2825 | if (s->iface == 0) |
| 2826 | port = "*"; |
| 2827 | else |
| 2828 | port = xll_index_to_name(s->iface); |
| 2829 | |
| 2830 | sock_addr_print(addr, ":", port, NULL); |
| 2831 | sock_addr_print("", "*", "", NULL); |
Vadim Kochan | 4a0053b | 2015-01-04 22:18:39 +0200 | [diff] [blame] | 2832 | |
| 2833 | if (show_proc_ctx || show_sock_ctx) { |
| 2834 | if (find_entry(s->ino, &buf, |
| 2835 | (show_proc_ctx & show_sock_ctx) ? |
| 2836 | PROC_SOCK_CTX : PROC_CTX) > 0) { |
| 2837 | printf(" users:(%s)", buf); |
| 2838 | free(buf); |
| 2839 | } |
| 2840 | } else if (show_users) { |
| 2841 | if (find_entry(s->ino, &buf, USERS) > 0) { |
| 2842 | printf(" users:(%s)", buf); |
| 2843 | free(buf); |
| 2844 | } |
| 2845 | } |
| 2846 | |
Vadim Kochan | f1b39e1 | 2015-02-13 22:14:02 +0200 | [diff] [blame] | 2847 | if (show_details) |
| 2848 | sock_details_print(s); |
| 2849 | |
Vadim Kochan | 4a0053b | 2015-01-04 22:18:39 +0200 | [diff] [blame] | 2850 | return 0; |
| 2851 | } |
| 2852 | |
Vadim Kochan | 2631b85 | 2015-05-15 17:19:30 +0300 | [diff] [blame] | 2853 | static void packet_show_ring(struct packet_diag_ring *ring) |
| 2854 | { |
| 2855 | printf("blk_size:%d", ring->pdr_block_size); |
| 2856 | printf(",blk_nr:%d", ring->pdr_block_nr); |
| 2857 | printf(",frm_size:%d", ring->pdr_frame_size); |
| 2858 | printf(",frm_nr:%d", ring->pdr_frame_nr); |
| 2859 | printf(",tmo:%d", ring->pdr_retire_tmo); |
| 2860 | printf(",features:0x%x", ring->pdr_features); |
| 2861 | } |
| 2862 | |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 2863 | static int packet_show_sock(const struct sockaddr_nl *addr, |
| 2864 | struct nlmsghdr *nlh, void *arg) |
Nicolas Dichtel | 372c30d | 2013-05-17 08:42:34 -0700 | [diff] [blame] | 2865 | { |
Vadim Kochan | 4a0053b | 2015-01-04 22:18:39 +0200 | [diff] [blame] | 2866 | const struct filter *f = arg; |
Nicolas Dichtel | 372c30d | 2013-05-17 08:42:34 -0700 | [diff] [blame] | 2867 | struct packet_diag_msg *r = NLMSG_DATA(nlh); |
Vadim Kochan | 2631b85 | 2015-05-15 17:19:30 +0300 | [diff] [blame] | 2868 | struct packet_diag_info *pinfo = NULL; |
| 2869 | struct packet_diag_ring *ring_rx = NULL, *ring_tx = NULL; |
Nicolas Dichtel | 372c30d | 2013-05-17 08:42:34 -0700 | [diff] [blame] | 2870 | struct rtattr *tb[PACKET_DIAG_MAX+1]; |
Vadim Kochan | 89f634f | 2015-02-13 22:13:59 +0200 | [diff] [blame] | 2871 | struct sockstat stat = {}; |
Vadim Kochan | 2631b85 | 2015-05-15 17:19:30 +0300 | [diff] [blame] | 2872 | uint32_t fanout = 0; |
| 2873 | bool has_fanout = false; |
Nicolas Dichtel | 372c30d | 2013-05-17 08:42:34 -0700 | [diff] [blame] | 2874 | |
| 2875 | parse_rtattr(tb, PACKET_DIAG_MAX, (struct rtattr*)(r+1), |
| 2876 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); |
| 2877 | |
| 2878 | /* use /proc/net/packet if all info are not available */ |
| 2879 | if (!tb[PACKET_DIAG_MEMINFO]) |
| 2880 | return -1; |
| 2881 | |
Vadim Kochan | 2d791bc | 2015-02-13 22:14:01 +0200 | [diff] [blame] | 2882 | stat.type = r->pdiag_type; |
| 2883 | stat.prot = r->pdiag_num; |
| 2884 | stat.ino = r->pdiag_ino; |
| 2885 | stat.state = SS_CLOSE; |
Vadim Kochan | f1b39e1 | 2015-02-13 22:14:02 +0200 | [diff] [blame] | 2886 | stat.sk = cookie_sk_get(&r->pdiag_cookie[0]); |
Nicolas Dichtel | 372c30d | 2013-05-17 08:42:34 -0700 | [diff] [blame] | 2887 | |
| 2888 | if (tb[PACKET_DIAG_MEMINFO]) { |
| 2889 | __u32 *skmeminfo = RTA_DATA(tb[PACKET_DIAG_MEMINFO]); |
Vadim Kochan | 4a0053b | 2015-01-04 22:18:39 +0200 | [diff] [blame] | 2890 | stat.rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC]; |
Nicolas Dichtel | 372c30d | 2013-05-17 08:42:34 -0700 | [diff] [blame] | 2891 | } |
Vadim Kochan | 4a0053b | 2015-01-04 22:18:39 +0200 | [diff] [blame] | 2892 | |
Nicolas Dichtel | 372c30d | 2013-05-17 08:42:34 -0700 | [diff] [blame] | 2893 | if (tb[PACKET_DIAG_INFO]) { |
Vadim Kochan | 2631b85 | 2015-05-15 17:19:30 +0300 | [diff] [blame] | 2894 | pinfo = RTA_DATA(tb[PACKET_DIAG_INFO]); |
Vadim Kochan | 2d791bc | 2015-02-13 22:14:01 +0200 | [diff] [blame] | 2895 | stat.lport = stat.iface = pinfo->pdi_index; |
Nicolas Dichtel | 372c30d | 2013-05-17 08:42:34 -0700 | [diff] [blame] | 2896 | } |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 2897 | |
Vadim Kochan | f1b39e1 | 2015-02-13 22:14:02 +0200 | [diff] [blame] | 2898 | if (tb[PACKET_DIAG_UID]) |
| 2899 | stat.uid = *(__u32 *)RTA_DATA(tb[PACKET_DIAG_UID]); |
| 2900 | |
Vadim Kochan | 2631b85 | 2015-05-15 17:19:30 +0300 | [diff] [blame] | 2901 | if (tb[PACKET_DIAG_RX_RING]) |
| 2902 | ring_rx = RTA_DATA(tb[PACKET_DIAG_RX_RING]); |
| 2903 | |
| 2904 | if (tb[PACKET_DIAG_TX_RING]) |
| 2905 | ring_tx = RTA_DATA(tb[PACKET_DIAG_TX_RING]); |
| 2906 | |
| 2907 | if (tb[PACKET_DIAG_FANOUT]) { |
| 2908 | has_fanout = true; |
| 2909 | fanout = *(uint32_t *)RTA_DATA(tb[PACKET_DIAG_FANOUT]); |
| 2910 | } |
| 2911 | |
Vadim Kochan | 4a0053b | 2015-01-04 22:18:39 +0200 | [diff] [blame] | 2912 | if (packet_stats_print(&stat, f)) |
| 2913 | return 0; |
| 2914 | |
Vadim Kochan | 2631b85 | 2015-05-15 17:19:30 +0300 | [diff] [blame] | 2915 | if (show_details) { |
| 2916 | if (pinfo) { |
| 2917 | printf("\n\tver:%d", pinfo->pdi_version); |
| 2918 | printf(" cpy_thresh:%d", pinfo->pdi_copy_thresh); |
| 2919 | printf(" flags( "); |
| 2920 | if (pinfo->pdi_flags & PDI_RUNNING) |
| 2921 | printf("running"); |
| 2922 | if (pinfo->pdi_flags & PDI_AUXDATA) |
| 2923 | printf(" auxdata"); |
| 2924 | if (pinfo->pdi_flags & PDI_ORIGDEV) |
| 2925 | printf(" origdev"); |
| 2926 | if (pinfo->pdi_flags & PDI_VNETHDR) |
| 2927 | printf(" vnethdr"); |
| 2928 | if (pinfo->pdi_flags & PDI_LOSS) |
| 2929 | printf(" loss"); |
| 2930 | if (!pinfo->pdi_flags) |
| 2931 | printf("0"); |
| 2932 | printf(" )"); |
| 2933 | } |
| 2934 | if (ring_rx) { |
| 2935 | printf("\n\tring_rx("); |
| 2936 | packet_show_ring(ring_rx); |
| 2937 | printf(")"); |
| 2938 | } |
| 2939 | if (ring_tx) { |
| 2940 | printf("\n\tring_tx("); |
| 2941 | packet_show_ring(ring_tx); |
| 2942 | printf(")"); |
| 2943 | } |
| 2944 | if (has_fanout) { |
| 2945 | uint16_t type = (fanout >> 16) & 0xffff; |
| 2946 | |
| 2947 | printf("\n\tfanout("); |
| 2948 | printf("id:%d,", fanout & 0xffff); |
| 2949 | printf("type:"); |
| 2950 | |
| 2951 | if (type == 0) |
| 2952 | printf("hash"); |
| 2953 | else if (type == 1) |
| 2954 | printf("lb"); |
| 2955 | else if (type == 2) |
| 2956 | printf("cpu"); |
| 2957 | else if (type == 3) |
| 2958 | printf("roll"); |
| 2959 | else if (type == 4) |
| 2960 | printf("random"); |
| 2961 | else if (type == 5) |
| 2962 | printf("qm"); |
| 2963 | else |
| 2964 | printf("0x%x", type); |
| 2965 | |
| 2966 | printf(")"); |
| 2967 | } |
| 2968 | } |
| 2969 | |
Nicolas Dichtel | 372c30d | 2013-05-17 08:42:34 -0700 | [diff] [blame] | 2970 | if (show_bpf && tb[PACKET_DIAG_FILTER]) { |
| 2971 | struct sock_filter *fil = |
| 2972 | RTA_DATA(tb[PACKET_DIAG_FILTER]); |
| 2973 | int num = RTA_PAYLOAD(tb[PACKET_DIAG_FILTER]) / |
| 2974 | sizeof(struct sock_filter); |
| 2975 | |
| 2976 | printf("\n\tbpf filter (%d): ", num); |
| 2977 | while (num) { |
| 2978 | printf(" 0x%02x %u %u %u,", |
| 2979 | fil->code, fil->jt, fil->jf, fil->k); |
| 2980 | num--; |
| 2981 | fil++; |
| 2982 | } |
| 2983 | } |
| 2984 | printf("\n"); |
| 2985 | return 0; |
| 2986 | } |
| 2987 | |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 2988 | static int packet_show_netlink(struct filter *f) |
Nicolas Dichtel | 372c30d | 2013-05-17 08:42:34 -0700 | [diff] [blame] | 2989 | { |
vadimk | 5fb421d | 2014-10-30 18:49:25 +0200 | [diff] [blame] | 2990 | DIAG_REQUEST(req, struct packet_diag_req r); |
Nicolas Dichtel | 372c30d | 2013-05-17 08:42:34 -0700 | [diff] [blame] | 2991 | |
Nicolas Dichtel | 372c30d | 2013-05-17 08:42:34 -0700 | [diff] [blame] | 2992 | req.r.sdiag_family = AF_PACKET; |
Vadim Kochan | 2631b85 | 2015-05-15 17:19:30 +0300 | [diff] [blame] | 2993 | req.r.pdiag_show = PACKET_SHOW_INFO | PACKET_SHOW_MEMINFO | |
| 2994 | PACKET_SHOW_FILTER | PACKET_SHOW_RING_CFG | PACKET_SHOW_FANOUT; |
Nicolas Dichtel | 372c30d | 2013-05-17 08:42:34 -0700 | [diff] [blame] | 2995 | |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 2996 | return handle_netlink_request(f, &req.nlh, sizeof(req), packet_show_sock); |
Nicolas Dichtel | 372c30d | 2013-05-17 08:42:34 -0700 | [diff] [blame] | 2997 | } |
| 2998 | |
Vadim Kochan | 4a0053b | 2015-01-04 22:18:39 +0200 | [diff] [blame] | 2999 | static int packet_show_line(char *buf, const struct filter *f, int fam) |
| 3000 | { |
| 3001 | unsigned long long sk; |
Vadim Kochan | 89f634f | 2015-02-13 22:13:59 +0200 | [diff] [blame] | 3002 | struct sockstat stat = {}; |
Vadim Kochan | 4a0053b | 2015-01-04 22:18:39 +0200 | [diff] [blame] | 3003 | int type, prot, iface, state, rq, uid, ino; |
| 3004 | |
| 3005 | sscanf(buf, "%llx %*d %d %x %d %d %u %u %u", |
| 3006 | &sk, |
| 3007 | &type, &prot, &iface, &state, |
| 3008 | &rq, &uid, &ino); |
| 3009 | |
| 3010 | if (stat.type == SOCK_RAW && !(f->dbs&(1<<PACKET_R_DB))) |
| 3011 | return 0; |
| 3012 | if (stat.type == SOCK_DGRAM && !(f->dbs&(1<<PACKET_DG_DB))) |
| 3013 | return 0; |
| 3014 | |
| 3015 | stat.type = type; |
| 3016 | stat.prot = prot; |
Vadim Kochan | 2d791bc | 2015-02-13 22:14:01 +0200 | [diff] [blame] | 3017 | stat.lport = stat.iface = iface; |
Vadim Kochan | 4a0053b | 2015-01-04 22:18:39 +0200 | [diff] [blame] | 3018 | stat.state = state; |
| 3019 | stat.rq = rq; |
| 3020 | stat.uid = uid; |
| 3021 | stat.ino = ino; |
Vadim Kochan | 2d791bc | 2015-02-13 22:14:01 +0200 | [diff] [blame] | 3022 | stat.state = SS_CLOSE; |
| 3023 | |
Vadim Kochan | 4a0053b | 2015-01-04 22:18:39 +0200 | [diff] [blame] | 3024 | if (packet_stats_print(&stat, f)) |
| 3025 | return 0; |
| 3026 | |
Vadim Kochan | 4a0053b | 2015-01-04 22:18:39 +0200 | [diff] [blame] | 3027 | printf("\n"); |
| 3028 | return 0; |
| 3029 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3030 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 3031 | static int packet_show(struct filter *f) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3032 | { |
| 3033 | FILE *fp; |
Phil Sutter | b95d28c | 2015-08-06 14:24:34 +0200 | [diff] [blame] | 3034 | int rc = 0; |
Stephen Hemminger | 3d0b743 | 2014-12-20 15:47:17 -0800 | [diff] [blame] | 3035 | |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 3036 | if (!filter_af_get(f, AF_PACKET) || !(f->states & (1 << SS_CLOSE))) |
Vadim Kochan | b9ea445 | 2014-12-20 15:40:55 -0800 | [diff] [blame] | 3037 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3038 | |
Vadim Kochan | 4a0053b | 2015-01-04 22:18:39 +0200 | [diff] [blame] | 3039 | if (!getenv("PROC_NET_PACKET") && !getenv("PROC_ROOT") && |
| 3040 | packet_show_netlink(f) == 0) |
Nicolas Dichtel | 372c30d | 2013-05-17 08:42:34 -0700 | [diff] [blame] | 3041 | return 0; |
| 3042 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 3043 | if ((fp = net_packet_open()) == NULL) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3044 | return -1; |
Vadim Kochan | 4a0053b | 2015-01-04 22:18:39 +0200 | [diff] [blame] | 3045 | if (generic_record_read(fp, packet_show_line, f, AF_PACKET)) |
Phil Sutter | b95d28c | 2015-08-06 14:24:34 +0200 | [diff] [blame] | 3046 | rc = -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3047 | |
Phil Sutter | b95d28c | 2015-08-06 14:24:34 +0200 | [diff] [blame] | 3048 | fclose(fp); |
| 3049 | return rc; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3050 | } |
| 3051 | |
Vadim Kochan | 5f24ec0 | 2015-02-22 22:23:11 +0200 | [diff] [blame] | 3052 | static int netlink_show_one(struct filter *f, |
Andrey Vagin | 129709a | 2013-06-05 12:41:59 +0400 | [diff] [blame] | 3053 | int prot, int pid, unsigned groups, |
Andrey Vagin | f271fe0 | 2013-06-05 12:42:00 +0400 | [diff] [blame] | 3054 | int state, int dst_pid, unsigned dst_group, |
Andrey Vagin | 129709a | 2013-06-05 12:41:59 +0400 | [diff] [blame] | 3055 | int rq, int wq, |
| 3056 | unsigned long long sk, unsigned long long cb) |
| 3057 | { |
Vadim Kochan | 2d791bc | 2015-02-13 22:14:01 +0200 | [diff] [blame] | 3058 | struct sockstat st; |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 3059 | SPRINT_BUF(prot_buf) = {}; |
| 3060 | const char *prot_name; |
| 3061 | char procname[64] = {}; |
vadimk | eef43b5 | 2014-10-30 17:33:36 +0200 | [diff] [blame] | 3062 | |
Vadim Kochan | 2d791bc | 2015-02-13 22:14:01 +0200 | [diff] [blame] | 3063 | st.state = SS_CLOSE; |
| 3064 | st.rq = rq; |
| 3065 | st.wq = wq; |
| 3066 | |
Andrey Vagin | 129709a | 2013-06-05 12:41:59 +0400 | [diff] [blame] | 3067 | if (f->f) { |
Vadim Kochan | 055840f | 2015-02-13 22:13:58 +0200 | [diff] [blame] | 3068 | st.local.family = AF_NETLINK; |
| 3069 | st.remote.family = AF_NETLINK; |
| 3070 | st.rport = -1; |
| 3071 | st.lport = pid; |
| 3072 | st.local.data[0] = prot; |
Vadim Kochan | 055840f | 2015-02-13 22:13:58 +0200 | [diff] [blame] | 3073 | if (run_ssfilter(f->f, &st) == 0) |
Vadim Kochan | 5f24ec0 | 2015-02-22 22:23:11 +0200 | [diff] [blame] | 3074 | return 1; |
Andrey Vagin | 129709a | 2013-06-05 12:41:59 +0400 | [diff] [blame] | 3075 | } |
| 3076 | |
Vadim Kochan | 2d791bc | 2015-02-13 22:14:01 +0200 | [diff] [blame] | 3077 | sock_state_print(&st, "nl"); |
vadimk | eef43b5 | 2014-10-30 17:33:36 +0200 | [diff] [blame] | 3078 | |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 3079 | if (resolve_services) |
| 3080 | prot_name = nl_proto_n2a(prot, prot_buf, sizeof(prot_buf)); |
| 3081 | else |
| 3082 | prot_name = int_to_str(prot, prot_buf); |
vadimk | eef43b5 | 2014-10-30 17:33:36 +0200 | [diff] [blame] | 3083 | |
Andrey Vagin | 129709a | 2013-06-05 12:41:59 +0400 | [diff] [blame] | 3084 | if (pid == -1) { |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 3085 | procname[0] = '*'; |
Andrey Vagin | 129709a | 2013-06-05 12:41:59 +0400 | [diff] [blame] | 3086 | } else if (resolve_services) { |
| 3087 | int done = 0; |
| 3088 | if (!pid) { |
| 3089 | done = 1; |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 3090 | strncpy(procname, "kernel", 6); |
Andrey Vagin | 129709a | 2013-06-05 12:41:59 +0400 | [diff] [blame] | 3091 | } else if (pid > 0) { |
Andrey Vagin | 129709a | 2013-06-05 12:41:59 +0400 | [diff] [blame] | 3092 | FILE *fp; |
willy tarreau | 0ee9052 | 2015-10-06 12:09:33 +0200 | [diff] [blame] | 3093 | snprintf(procname, sizeof(procname), "%s/%d/stat", |
Andrey Vagin | 129709a | 2013-06-05 12:41:59 +0400 | [diff] [blame] | 3094 | getenv("PROC_ROOT") ? : "/proc", pid); |
| 3095 | if ((fp = fopen(procname, "r")) != NULL) { |
| 3096 | if (fscanf(fp, "%*d (%[^)])", procname) == 1) { |
willy tarreau | 0ee9052 | 2015-10-06 12:09:33 +0200 | [diff] [blame] | 3097 | snprintf(procname+strlen(procname), |
| 3098 | sizeof(procname)-strlen(procname), |
| 3099 | "/%d", pid); |
Andrey Vagin | 129709a | 2013-06-05 12:41:59 +0400 | [diff] [blame] | 3100 | done = 1; |
| 3101 | } |
| 3102 | fclose(fp); |
| 3103 | } |
| 3104 | } |
| 3105 | if (!done) |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 3106 | int_to_str(pid, procname); |
Andrey Vagin | 129709a | 2013-06-05 12:41:59 +0400 | [diff] [blame] | 3107 | } else { |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 3108 | int_to_str(pid, procname); |
Andrey Vagin | 129709a | 2013-06-05 12:41:59 +0400 | [diff] [blame] | 3109 | } |
Andrey Vagin | f271fe0 | 2013-06-05 12:42:00 +0400 | [diff] [blame] | 3110 | |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 3111 | sock_addr_print(prot_name, ":", procname, NULL); |
| 3112 | |
Andrey Vagin | f271fe0 | 2013-06-05 12:42:00 +0400 | [diff] [blame] | 3113 | if (state == NETLINK_CONNECTED) { |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 3114 | char dst_group_buf[30]; |
| 3115 | char dst_pid_buf[30]; |
| 3116 | sock_addr_print(int_to_str(dst_group, dst_group_buf), ":", |
| 3117 | int_to_str(dst_pid, dst_pid_buf), NULL); |
Andrey Vagin | f271fe0 | 2013-06-05 12:42:00 +0400 | [diff] [blame] | 3118 | } else { |
Vadim Kochan | b217df1 | 2015-02-13 22:14:03 +0200 | [diff] [blame] | 3119 | sock_addr_print("", "*", "", NULL); |
Andrey Vagin | f271fe0 | 2013-06-05 12:42:00 +0400 | [diff] [blame] | 3120 | } |
Andrey Vagin | 129709a | 2013-06-05 12:41:59 +0400 | [diff] [blame] | 3121 | |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 3122 | char *pid_context = NULL; |
| 3123 | if (show_proc_ctx) { |
| 3124 | /* The pid value will either be: |
| 3125 | * 0 if destination kernel - show kernel initial context. |
| 3126 | * A valid process pid - use getpidcon. |
| 3127 | * A unique value allocated by the kernel or netlink user |
| 3128 | * to the process - show context as "not available". |
| 3129 | */ |
| 3130 | if (!pid) |
| 3131 | security_get_initial_context("kernel", &pid_context); |
| 3132 | else if (pid > 0) |
| 3133 | getpidcon(pid, &pid_context); |
| 3134 | |
| 3135 | if (pid_context != NULL) { |
| 3136 | printf("proc_ctx=%-*s ", serv_width, pid_context); |
| 3137 | free(pid_context); |
| 3138 | } else { |
| 3139 | printf("proc_ctx=%-*s ", serv_width, "unavailable"); |
| 3140 | } |
| 3141 | } |
| 3142 | |
Andrey Vagin | 129709a | 2013-06-05 12:41:59 +0400 | [diff] [blame] | 3143 | if (show_details) { |
| 3144 | printf(" sk=%llx cb=%llx groups=0x%08x", sk, cb, groups); |
| 3145 | } |
| 3146 | printf("\n"); |
| 3147 | |
Vadim Kochan | 5f24ec0 | 2015-02-22 22:23:11 +0200 | [diff] [blame] | 3148 | return 0; |
Andrey Vagin | 129709a | 2013-06-05 12:41:59 +0400 | [diff] [blame] | 3149 | } |
| 3150 | |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 3151 | static int netlink_show_sock(const struct sockaddr_nl *addr, |
| 3152 | struct nlmsghdr *nlh, void *arg) |
Andrey Vagin | ecb928c | 2013-06-05 12:42:01 +0400 | [diff] [blame] | 3153 | { |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 3154 | struct filter *f = (struct filter *)arg; |
Andrey Vagin | ecb928c | 2013-06-05 12:42:01 +0400 | [diff] [blame] | 3155 | struct netlink_diag_msg *r = NLMSG_DATA(nlh); |
| 3156 | struct rtattr *tb[NETLINK_DIAG_MAX+1]; |
| 3157 | int rq = 0, wq = 0; |
| 3158 | unsigned long groups = 0; |
| 3159 | |
| 3160 | parse_rtattr(tb, NETLINK_DIAG_MAX, (struct rtattr*)(r+1), |
| 3161 | nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); |
| 3162 | |
| 3163 | if (tb[NETLINK_DIAG_GROUPS] && RTA_PAYLOAD(tb[NETLINK_DIAG_GROUPS])) |
| 3164 | groups = *(unsigned long *) RTA_DATA(tb[NETLINK_DIAG_GROUPS]); |
| 3165 | |
| 3166 | if (tb[NETLINK_DIAG_MEMINFO]) { |
| 3167 | const __u32 *skmeminfo; |
| 3168 | skmeminfo = RTA_DATA(tb[NETLINK_DIAG_MEMINFO]); |
| 3169 | |
| 3170 | rq = skmeminfo[SK_MEMINFO_RMEM_ALLOC]; |
| 3171 | wq = skmeminfo[SK_MEMINFO_WMEM_ALLOC]; |
| 3172 | } |
| 3173 | |
Vadim Kochan | 5f24ec0 | 2015-02-22 22:23:11 +0200 | [diff] [blame] | 3174 | if (netlink_show_one(f, r->ndiag_protocol, r->ndiag_portid, groups, |
Andrey Vagin | ecb928c | 2013-06-05 12:42:01 +0400 | [diff] [blame] | 3175 | r->ndiag_state, r->ndiag_dst_portid, r->ndiag_dst_group, |
Vadim Kochan | 5f24ec0 | 2015-02-22 22:23:11 +0200 | [diff] [blame] | 3176 | rq, wq, 0, 0)) { |
| 3177 | return 0; |
| 3178 | } |
Andrey Vagin | ecb928c | 2013-06-05 12:42:01 +0400 | [diff] [blame] | 3179 | |
| 3180 | if (show_mem) { |
| 3181 | printf("\t"); |
| 3182 | print_skmeminfo(tb, NETLINK_DIAG_MEMINFO); |
| 3183 | printf("\n"); |
| 3184 | } |
| 3185 | |
| 3186 | return 0; |
| 3187 | } |
| 3188 | |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 3189 | static int netlink_show_netlink(struct filter *f) |
Andrey Vagin | ecb928c | 2013-06-05 12:42:01 +0400 | [diff] [blame] | 3190 | { |
vadimk | 5fb421d | 2014-10-30 18:49:25 +0200 | [diff] [blame] | 3191 | DIAG_REQUEST(req, struct netlink_diag_req r); |
Andrey Vagin | ecb928c | 2013-06-05 12:42:01 +0400 | [diff] [blame] | 3192 | |
| 3193 | req.r.sdiag_family = AF_NETLINK; |
| 3194 | req.r.sdiag_protocol = NDIAG_PROTO_ALL; |
| 3195 | req.r.ndiag_show = NDIAG_SHOW_GROUPS | NDIAG_SHOW_MEMINFO; |
| 3196 | |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 3197 | return handle_netlink_request(f, &req.nlh, sizeof(req), netlink_show_sock); |
Andrey Vagin | ecb928c | 2013-06-05 12:42:01 +0400 | [diff] [blame] | 3198 | } |
| 3199 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 3200 | static int netlink_show(struct filter *f) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3201 | { |
| 3202 | FILE *fp; |
| 3203 | char buf[256]; |
| 3204 | int prot, pid; |
| 3205 | unsigned groups; |
| 3206 | int rq, wq, rc; |
| 3207 | unsigned long long sk, cb; |
| 3208 | |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 3209 | if (!filter_af_get(f, AF_NETLINK) || !(f->states & (1 << SS_CLOSE))) |
Vadim Kochan | b9ea445 | 2014-12-20 15:40:55 -0800 | [diff] [blame] | 3210 | return 0; |
| 3211 | |
Andrey Vagin | 129709a | 2013-06-05 12:41:59 +0400 | [diff] [blame] | 3212 | if (!getenv("PROC_NET_NETLINK") && !getenv("PROC_ROOT") && |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 3213 | netlink_show_netlink(f) == 0) |
Andrey Vagin | 129709a | 2013-06-05 12:41:59 +0400 | [diff] [blame] | 3214 | return 0; |
| 3215 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 3216 | if ((fp = net_netlink_open()) == NULL) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3217 | return -1; |
| 3218 | fgets(buf, sizeof(buf)-1, fp); |
| 3219 | |
| 3220 | while (fgets(buf, sizeof(buf)-1, fp)) { |
| 3221 | sscanf(buf, "%llx %d %d %x %d %d %llx %d", |
| 3222 | &sk, |
| 3223 | &prot, &pid, &groups, &rq, &wq, &cb, &rc); |
| 3224 | |
Andrey Vagin | f271fe0 | 2013-06-05 12:42:00 +0400 | [diff] [blame] | 3225 | netlink_show_one(f, prot, pid, groups, 0, 0, 0, rq, wq, sk, cb); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3226 | } |
| 3227 | |
Phil Sutter | b95d28c | 2015-08-06 14:24:34 +0200 | [diff] [blame] | 3228 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3229 | return 0; |
| 3230 | } |
| 3231 | |
Craig Gallek | 6885e3b | 2015-06-17 11:14:48 -0400 | [diff] [blame] | 3232 | struct sock_diag_msg { |
| 3233 | __u8 sdiag_family; |
| 3234 | }; |
| 3235 | |
| 3236 | static int generic_show_sock(const struct sockaddr_nl *addr, |
| 3237 | struct nlmsghdr *nlh, void *arg) |
| 3238 | { |
| 3239 | struct sock_diag_msg *r = NLMSG_DATA(nlh); |
| 3240 | struct inet_diag_arg inet_arg = { .f = arg, .protocol = IPPROTO_MAX }; |
| 3241 | |
| 3242 | switch (r->sdiag_family) { |
| 3243 | case AF_INET: |
| 3244 | case AF_INET6: |
| 3245 | return show_one_inet_sock(addr, nlh, &inet_arg); |
| 3246 | case AF_UNIX: |
| 3247 | return unix_show_sock(addr, nlh, arg); |
| 3248 | case AF_PACKET: |
| 3249 | return packet_show_sock(addr, nlh, arg); |
| 3250 | case AF_NETLINK: |
| 3251 | return netlink_show_sock(addr, nlh, arg); |
| 3252 | default: |
| 3253 | return -1; |
| 3254 | } |
| 3255 | } |
| 3256 | |
| 3257 | static int handle_follow_request(struct filter *f) |
| 3258 | { |
| 3259 | int ret = -1; |
| 3260 | int groups = 0; |
| 3261 | struct rtnl_handle rth; |
| 3262 | |
| 3263 | if (f->families & (1 << AF_INET) && f->dbs & (1 << TCP_DB)) |
| 3264 | groups |= 1 << (SKNLGRP_INET_TCP_DESTROY - 1); |
| 3265 | if (f->families & (1 << AF_INET) && f->dbs & (1 << UDP_DB)) |
| 3266 | groups |= 1 << (SKNLGRP_INET_UDP_DESTROY - 1); |
| 3267 | if (f->families & (1 << AF_INET6) && f->dbs & (1 << TCP_DB)) |
| 3268 | groups |= 1 << (SKNLGRP_INET6_TCP_DESTROY - 1); |
| 3269 | if (f->families & (1 << AF_INET6) && f->dbs & (1 << UDP_DB)) |
| 3270 | groups |= 1 << (SKNLGRP_INET6_UDP_DESTROY - 1); |
| 3271 | |
| 3272 | if (groups == 0) |
| 3273 | return -1; |
| 3274 | |
| 3275 | if (rtnl_open_byproto(&rth, groups, NETLINK_SOCK_DIAG)) |
| 3276 | return -1; |
| 3277 | |
| 3278 | rth.dump = 0; |
| 3279 | rth.local.nl_pid = 0; |
| 3280 | |
| 3281 | if (rtnl_dump_filter(&rth, generic_show_sock, f)) |
| 3282 | goto Exit; |
| 3283 | |
| 3284 | ret = 0; |
| 3285 | Exit: |
| 3286 | rtnl_close(&rth); |
| 3287 | return ret; |
| 3288 | } |
| 3289 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3290 | struct snmpstat |
| 3291 | { |
| 3292 | int tcp_estab; |
| 3293 | }; |
| 3294 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 3295 | static int get_snmp_int(char *proto, char *key, int *result) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3296 | { |
| 3297 | char buf[1024]; |
| 3298 | FILE *fp; |
| 3299 | int protolen = strlen(proto); |
| 3300 | int keylen = strlen(key); |
| 3301 | |
| 3302 | *result = 0; |
| 3303 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 3304 | if ((fp = net_snmp_open()) == NULL) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3305 | return -1; |
| 3306 | |
| 3307 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 3308 | char *p = buf; |
| 3309 | int pos = 0; |
| 3310 | if (memcmp(buf, proto, protolen)) |
| 3311 | continue; |
| 3312 | while ((p = strchr(p, ' ')) != NULL) { |
| 3313 | pos++; |
| 3314 | p++; |
| 3315 | if (memcmp(p, key, keylen) == 0 && |
| 3316 | (p[keylen] == ' ' || p[keylen] == '\n')) |
| 3317 | break; |
| 3318 | } |
| 3319 | if (fgets(buf, sizeof(buf), fp) == NULL) |
| 3320 | break; |
| 3321 | if (memcmp(buf, proto, protolen)) |
| 3322 | break; |
| 3323 | p = buf; |
| 3324 | while ((p = strchr(p, ' ')) != NULL) { |
| 3325 | p++; |
| 3326 | if (--pos == 0) { |
| 3327 | sscanf(p, "%d", result); |
| 3328 | fclose(fp); |
| 3329 | return 0; |
| 3330 | } |
| 3331 | } |
| 3332 | } |
| 3333 | |
| 3334 | fclose(fp); |
| 3335 | errno = ESRCH; |
| 3336 | return -1; |
| 3337 | } |
| 3338 | |
| 3339 | |
| 3340 | /* Get stats from sockstat */ |
| 3341 | |
Vadim Kochan | 055840f | 2015-02-13 22:13:58 +0200 | [diff] [blame] | 3342 | struct ssummary |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3343 | { |
| 3344 | int socks; |
| 3345 | int tcp_mem; |
| 3346 | int tcp_total; |
| 3347 | int tcp_orphans; |
| 3348 | int tcp_tws; |
| 3349 | int tcp4_hashed; |
| 3350 | int udp4; |
| 3351 | int raw4; |
| 3352 | int frag4; |
| 3353 | int frag4_mem; |
| 3354 | int tcp6_hashed; |
| 3355 | int udp6; |
| 3356 | int raw6; |
| 3357 | int frag6; |
| 3358 | int frag6_mem; |
| 3359 | }; |
| 3360 | |
Vadim Kochan | 055840f | 2015-02-13 22:13:58 +0200 | [diff] [blame] | 3361 | static void get_sockstat_line(char *line, struct ssummary *s) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3362 | { |
| 3363 | char id[256], rem[256]; |
| 3364 | |
| 3365 | if (sscanf(line, "%[^ ] %[^\n]\n", id, rem) != 2) |
| 3366 | return; |
| 3367 | |
| 3368 | if (strcmp(id, "sockets:") == 0) |
| 3369 | sscanf(rem, "%*s%d", &s->socks); |
| 3370 | else if (strcmp(id, "UDP:") == 0) |
| 3371 | sscanf(rem, "%*s%d", &s->udp4); |
| 3372 | else if (strcmp(id, "UDP6:") == 0) |
| 3373 | sscanf(rem, "%*s%d", &s->udp6); |
| 3374 | else if (strcmp(id, "RAW:") == 0) |
| 3375 | sscanf(rem, "%*s%d", &s->raw4); |
| 3376 | else if (strcmp(id, "RAW6:") == 0) |
| 3377 | sscanf(rem, "%*s%d", &s->raw6); |
| 3378 | else if (strcmp(id, "TCP6:") == 0) |
| 3379 | sscanf(rem, "%*s%d", &s->tcp6_hashed); |
| 3380 | else if (strcmp(id, "FRAG:") == 0) |
| 3381 | sscanf(rem, "%*s%d%*s%d", &s->frag4, &s->frag4_mem); |
| 3382 | else if (strcmp(id, "FRAG6:") == 0) |
| 3383 | sscanf(rem, "%*s%d%*s%d", &s->frag6, &s->frag6_mem); |
| 3384 | else if (strcmp(id, "TCP:") == 0) |
| 3385 | sscanf(rem, "%*s%d%*s%d%*s%d%*s%d%*s%d", |
| 3386 | &s->tcp4_hashed, |
| 3387 | &s->tcp_orphans, &s->tcp_tws, &s->tcp_total, &s->tcp_mem); |
| 3388 | } |
| 3389 | |
Vadim Kochan | 055840f | 2015-02-13 22:13:58 +0200 | [diff] [blame] | 3390 | static int get_sockstat(struct ssummary *s) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3391 | { |
| 3392 | char buf[256]; |
| 3393 | FILE *fp; |
| 3394 | |
| 3395 | memset(s, 0, sizeof(*s)); |
| 3396 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 3397 | if ((fp = net_sockstat_open()) == NULL) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3398 | return -1; |
| 3399 | while(fgets(buf, sizeof(buf), fp) != NULL) |
| 3400 | get_sockstat_line(buf, s); |
| 3401 | fclose(fp); |
| 3402 | |
Stephen Hemminger | ab01dbb | 2007-07-18 15:31:29 +0100 | [diff] [blame] | 3403 | if ((fp = net_sockstat6_open()) == NULL) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3404 | return 0; |
| 3405 | while(fgets(buf, sizeof(buf), fp) != NULL) |
| 3406 | get_sockstat_line(buf, s); |
| 3407 | fclose(fp); |
| 3408 | |
| 3409 | return 0; |
| 3410 | } |
| 3411 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 3412 | static int print_summary(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3413 | { |
Vadim Kochan | 055840f | 2015-02-13 22:13:58 +0200 | [diff] [blame] | 3414 | struct ssummary s; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3415 | struct snmpstat sn; |
| 3416 | |
| 3417 | if (get_sockstat(&s) < 0) |
| 3418 | perror("ss: get_sockstat"); |
| 3419 | if (get_snmp_int("Tcp:", "CurrEstab", &sn.tcp_estab) < 0) |
| 3420 | perror("ss: get_snmpstat"); |
| 3421 | |
Bryton Lee | a221d62 | 2015-02-12 14:16:04 +0800 | [diff] [blame] | 3422 | get_slabstat(&slabstat); |
| 3423 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3424 | printf("Total: %d (kernel %d)\n", s.socks, slabstat.socks); |
| 3425 | |
| 3426 | printf("TCP: %d (estab %d, closed %d, orphaned %d, synrecv %d, timewait %d/%d), ports %d\n", |
| 3427 | s.tcp_total + slabstat.tcp_syns + s.tcp_tws, |
| 3428 | sn.tcp_estab, |
| 3429 | s.tcp_total - (s.tcp4_hashed+s.tcp6_hashed-s.tcp_tws), |
| 3430 | s.tcp_orphans, |
| 3431 | slabstat.tcp_syns, |
| 3432 | s.tcp_tws, slabstat.tcp_tws, |
| 3433 | slabstat.tcp_ports |
| 3434 | ); |
| 3435 | |
| 3436 | printf("\n"); |
| 3437 | printf("Transport Total IP IPv6\n"); |
| 3438 | printf("* %-9d %-9s %-9s\n", slabstat.socks, "-", "-"); |
| 3439 | printf("RAW %-9d %-9d %-9d\n", s.raw4+s.raw6, s.raw4, s.raw6); |
| 3440 | printf("UDP %-9d %-9d %-9d\n", s.udp4+s.udp6, s.udp4, s.udp6); |
| 3441 | 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] | 3442 | printf("INET %-9d %-9d %-9d\n", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3443 | s.raw4+s.udp4+s.tcp4_hashed+ |
| 3444 | s.raw6+s.udp6+s.tcp6_hashed, |
| 3445 | s.raw4+s.udp4+s.tcp4_hashed, |
| 3446 | s.raw6+s.udp6+s.tcp6_hashed); |
| 3447 | printf("FRAG %-9d %-9d %-9d\n", s.frag4+s.frag6, s.frag4, s.frag6); |
| 3448 | |
| 3449 | printf("\n"); |
| 3450 | |
| 3451 | return 0; |
| 3452 | } |
| 3453 | |
Andreas Henriksson | 7a96e19 | 2009-12-07 13:12:36 +0100 | [diff] [blame] | 3454 | static void _usage(FILE *dest) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3455 | { |
Andreas Henriksson | 7a96e19 | 2009-12-07 13:12:36 +0100 | [diff] [blame] | 3456 | fprintf(dest, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3457 | "Usage: ss [ OPTIONS ]\n" |
| 3458 | " ss [ OPTIONS ] [ FILTER ]\n" |
Vadim Kochan | ff041f1 | 2015-01-08 04:30:43 +0200 | [diff] [blame] | 3459 | " -h, --help this message\n" |
| 3460 | " -V, --version output version information\n" |
| 3461 | " -n, --numeric don't resolve service names\n" |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 3462 | " -r, --resolve resolve host names\n" |
Vadim Kochan | ff041f1 | 2015-01-08 04:30:43 +0200 | [diff] [blame] | 3463 | " -a, --all display all sockets\n" |
| 3464 | " -l, --listening display listening sockets\n" |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 3465 | " -o, --options show timer information\n" |
| 3466 | " -e, --extended show detailed socket information\n" |
| 3467 | " -m, --memory show socket memory usage\n" |
Vadim Kochan | ff041f1 | 2015-01-08 04:30:43 +0200 | [diff] [blame] | 3468 | " -p, --processes show process using socket\n" |
| 3469 | " -i, --info show internal TCP information\n" |
| 3470 | " -s, --summary show socket usage summary\n" |
Rami Rosen | b0f01cf | 2013-05-24 12:42:40 +0300 | [diff] [blame] | 3471 | " -b, --bpf show bpf filter socket information\n" |
Craig Gallek | 6885e3b | 2015-06-17 11:14:48 -0400 | [diff] [blame] | 3472 | " -E, --events continually display sockets as they are destroyed\n" |
Vadim Kochan | ff041f1 | 2015-01-08 04:30:43 +0200 | [diff] [blame] | 3473 | " -Z, --context display process SELinux security contexts\n" |
| 3474 | " -z, --contexts display process and socket SELinux security contexts\n" |
Vadim Kochan | 95ce04b | 2015-02-08 08:58:43 +0200 | [diff] [blame] | 3475 | " -N, --net switch to the specified network namespace name\n" |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 3476 | "\n" |
| 3477 | " -4, --ipv4 display only IP version 4 sockets\n" |
| 3478 | " -6, --ipv6 display only IP version 6 sockets\n" |
Vadim Kochan | ff041f1 | 2015-01-08 04:30:43 +0200 | [diff] [blame] | 3479 | " -0, --packet display PACKET sockets\n" |
| 3480 | " -t, --tcp display only TCP sockets\n" |
| 3481 | " -u, --udp display only UDP sockets\n" |
| 3482 | " -d, --dccp display only DCCP sockets\n" |
| 3483 | " -w, --raw display only RAW sockets\n" |
| 3484 | " -x, --unix display only Unix domain sockets\n" |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 3485 | " -f, --family=FAMILY display sockets of type FAMILY\n" |
| 3486 | "\n" |
Petr Sabata | 583de14 | 2011-10-06 14:45:32 +0200 | [diff] [blame] | 3487 | " -A, --query=QUERY, --socket=QUERY\n" |
Masatake YAMATO | 56dee73 | 2014-01-08 20:13:48 +0900 | [diff] [blame] | 3488 | " QUERY := {all|inet|tcp|udp|raw|unix|unix_dgram|unix_stream|unix_seqpacket|packet|netlink}[,QUERY]\n" |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 3489 | "\n" |
Petr Sabata | 583de14 | 2011-10-06 14:45:32 +0200 | [diff] [blame] | 3490 | " -D, --diag=FILE Dump raw information about TCP sockets to FILE\n" |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 3491 | " -F, --filter=FILE read filter information from FILE\n" |
Vadim Kochan | ff041f1 | 2015-01-08 04:30:43 +0200 | [diff] [blame] | 3492 | " FILTER := [ state STATE-FILTER ] [ EXPRESSION ]\n" |
| 3493 | " STATE-FILTER := {all|connected|synchronized|bucket|big|TCP-STATES}\n" |
| 3494 | " TCP-STATES := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|closed|close-wait|last-ack|listen|closing}\n" |
| 3495 | " connected := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n" |
| 3496 | " synchronized := {established|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}\n" |
| 3497 | " bucket := {syn-recv|time-wait}\n" |
| 3498 | " big := {established|syn-sent|fin-wait-{1,2}|closed|close-wait|last-ack|listen|closing}\n" |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 3499 | ); |
Andreas Henriksson | 7a96e19 | 2009-12-07 13:12:36 +0100 | [diff] [blame] | 3500 | } |
| 3501 | |
| 3502 | static void help(void) __attribute__((noreturn)); |
| 3503 | static void help(void) |
| 3504 | { |
| 3505 | _usage(stdout); |
| 3506 | exit(0); |
| 3507 | } |
| 3508 | |
| 3509 | static void usage(void) __attribute__((noreturn)); |
| 3510 | static void usage(void) |
| 3511 | { |
| 3512 | _usage(stderr); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3513 | exit(-1); |
| 3514 | } |
| 3515 | |
| 3516 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 3517 | static int scan_state(const char *state) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3518 | { |
| 3519 | int i; |
| 3520 | if (strcasecmp(state, "close") == 0 || |
| 3521 | strcasecmp(state, "closed") == 0) |
| 3522 | return (1<<SS_CLOSE); |
| 3523 | if (strcasecmp(state, "syn-rcv") == 0) |
| 3524 | return (1<<SS_SYN_RECV); |
osdl.net!shemminger | 1a5bad5 | 2004-07-07 17:06:35 +0000 | [diff] [blame] | 3525 | if (strcasecmp(state, "established") == 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3526 | return (1<<SS_ESTABLISHED); |
| 3527 | if (strcasecmp(state, "all") == 0) |
| 3528 | return SS_ALL; |
| 3529 | if (strcasecmp(state, "connected") == 0) |
| 3530 | return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)); |
osdl.net!shemminger | 1a5bad5 | 2004-07-07 17:06:35 +0000 | [diff] [blame] | 3531 | if (strcasecmp(state, "synchronized") == 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3532 | return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)|(1<<SS_SYN_SENT)); |
| 3533 | if (strcasecmp(state, "bucket") == 0) |
| 3534 | return (1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT); |
| 3535 | if (strcasecmp(state, "big") == 0) |
| 3536 | return SS_ALL & ~((1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT)); |
| 3537 | for (i=0; i<SS_MAX; i++) { |
osdl.net!shemminger | 1a5bad5 | 2004-07-07 17:06:35 +0000 | [diff] [blame] | 3538 | if (strcasecmp(state, sstate_namel[i]) == 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3539 | return (1<<i); |
| 3540 | } |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 3541 | |
| 3542 | fprintf(stderr, "ss: wrong state name: %s\n", state); |
| 3543 | exit(-1); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3544 | } |
| 3545 | |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 3546 | static const struct option long_opts[] = { |
| 3547 | { "numeric", 0, 0, 'n' }, |
| 3548 | { "resolve", 0, 0, 'r' }, |
| 3549 | { "options", 0, 0, 'o' }, |
| 3550 | { "extended", 0, 0, 'e' }, |
| 3551 | { "memory", 0, 0, 'm' }, |
| 3552 | { "info", 0, 0, 'i' }, |
| 3553 | { "processes", 0, 0, 'p' }, |
Nicolas Dichtel | 372c30d | 2013-05-17 08:42:34 -0700 | [diff] [blame] | 3554 | { "bpf", 0, 0, 'b' }, |
Craig Gallek | 6885e3b | 2015-06-17 11:14:48 -0400 | [diff] [blame] | 3555 | { "events", 0, 0, 'E' }, |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 3556 | { "dccp", 0, 0, 'd' }, |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 3557 | { "tcp", 0, 0, 't' }, |
| 3558 | { "udp", 0, 0, 'u' }, |
| 3559 | { "raw", 0, 0, 'w' }, |
| 3560 | { "unix", 0, 0, 'x' }, |
| 3561 | { "all", 0, 0, 'a' }, |
| 3562 | { "listening", 0, 0, 'l' }, |
| 3563 | { "ipv4", 0, 0, '4' }, |
| 3564 | { "ipv6", 0, 0, '6' }, |
| 3565 | { "packet", 0, 0, '0' }, |
| 3566 | { "family", 1, 0, 'f' }, |
| 3567 | { "socket", 1, 0, 'A' }, |
Petr Sabata | 583de14 | 2011-10-06 14:45:32 +0200 | [diff] [blame] | 3568 | { "query", 1, 0, 'A' }, |
osdl.net!shemminger | c3f346b | 2005-01-18 22:31:42 +0000 | [diff] [blame] | 3569 | { "summary", 0, 0, 's' }, |
Petr Sabata | 583de14 | 2011-10-06 14:45:32 +0200 | [diff] [blame] | 3570 | { "diag", 1, 0, 'D' }, |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 3571 | { "filter", 1, 0, 'F' }, |
| 3572 | { "version", 0, 0, 'V' }, |
| 3573 | { "help", 0, 0, 'h' }, |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 3574 | { "context", 0, 0, 'Z' }, |
| 3575 | { "contexts", 0, 0, 'z' }, |
Vadim Kochan | 95ce04b | 2015-02-08 08:58:43 +0200 | [diff] [blame] | 3576 | { "net", 1, 0, 'N' }, |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 3577 | { 0 } |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 3578 | |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 3579 | }; |
| 3580 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3581 | int main(int argc, char *argv[]) |
| 3582 | { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3583 | int saw_states = 0; |
| 3584 | int saw_query = 0; |
| 3585 | int do_summary = 0; |
osdl.org!shemminger | 7d105b5 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 3586 | const char *dump_tcpdiag = NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3587 | FILE *filter_fp = NULL; |
| 3588 | int ch; |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 3589 | int state_filter = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3590 | |
Craig Gallek | 6885e3b | 2015-06-17 11:14:48 -0400 | [diff] [blame] | 3591 | while ((ch = getopt_long(argc, argv, "dhaletuwxnro460spbEf:miA:D:F:vVzZN:", |
osdl.org!shemminger | ab61159 | 2004-06-09 21:30:13 +0000 | [diff] [blame] | 3592 | long_opts, NULL)) != EOF) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3593 | switch(ch) { |
| 3594 | case 'n': |
| 3595 | resolve_services = 0; |
| 3596 | break; |
| 3597 | case 'r': |
| 3598 | resolve_hosts = 1; |
| 3599 | break; |
| 3600 | case 'o': |
| 3601 | show_options = 1; |
| 3602 | break; |
| 3603 | case 'e': |
| 3604 | show_options = 1; |
| 3605 | show_details++; |
| 3606 | break; |
| 3607 | case 'm': |
| 3608 | show_mem = 1; |
| 3609 | break; |
| 3610 | case 'i': |
| 3611 | show_tcpinfo = 1; |
| 3612 | break; |
| 3613 | case 'p': |
| 3614 | show_users++; |
Steve Fink | fbc0f87 | 2010-06-09 11:42:38 -0700 | [diff] [blame] | 3615 | user_ent_hash_build(); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3616 | break; |
Nicolas Dichtel | 372c30d | 2013-05-17 08:42:34 -0700 | [diff] [blame] | 3617 | case 'b': |
| 3618 | show_options = 1; |
| 3619 | show_bpf++; |
| 3620 | break; |
Craig Gallek | 6885e3b | 2015-06-17 11:14:48 -0400 | [diff] [blame] | 3621 | case 'E': |
| 3622 | follow_events = 1; |
| 3623 | break; |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 3624 | case 'd': |
Vadim Kochan | 57ff5a1 | 2015-04-30 07:30:24 +0300 | [diff] [blame] | 3625 | filter_db_set(¤t_filter, DCCP_DB); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 3626 | break; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3627 | case 't': |
Vadim Kochan | 57ff5a1 | 2015-04-30 07:30:24 +0300 | [diff] [blame] | 3628 | filter_db_set(¤t_filter, TCP_DB); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3629 | break; |
| 3630 | case 'u': |
Vadim Kochan | 57ff5a1 | 2015-04-30 07:30:24 +0300 | [diff] [blame] | 3631 | filter_db_set(¤t_filter, UDP_DB); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3632 | break; |
| 3633 | case 'w': |
Vadim Kochan | 57ff5a1 | 2015-04-30 07:30:24 +0300 | [diff] [blame] | 3634 | filter_db_set(¤t_filter, RAW_DB); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3635 | break; |
| 3636 | case 'x': |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 3637 | filter_af_set(¤t_filter, AF_UNIX); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3638 | break; |
| 3639 | case 'a': |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 3640 | state_filter = SS_ALL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3641 | break; |
| 3642 | case 'l': |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 3643 | state_filter = (1 << SS_LISTEN) | (1 << SS_CLOSE); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3644 | break; |
| 3645 | case '4': |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 3646 | filter_af_set(¤t_filter, AF_INET); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3647 | break; |
| 3648 | case '6': |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 3649 | filter_af_set(¤t_filter, AF_INET6); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3650 | break; |
| 3651 | case '0': |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 3652 | filter_af_set(¤t_filter, AF_PACKET); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3653 | break; |
| 3654 | case 'f': |
| 3655 | if (strcmp(optarg, "inet") == 0) |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 3656 | filter_af_set(¤t_filter, AF_INET); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3657 | else if (strcmp(optarg, "inet6") == 0) |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 3658 | filter_af_set(¤t_filter, AF_INET6); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3659 | else if (strcmp(optarg, "link") == 0) |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 3660 | filter_af_set(¤t_filter, AF_PACKET); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3661 | else if (strcmp(optarg, "unix") == 0) |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 3662 | filter_af_set(¤t_filter, AF_UNIX); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3663 | else if (strcmp(optarg, "netlink") == 0) |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 3664 | filter_af_set(¤t_filter, AF_NETLINK); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3665 | else if (strcmp(optarg, "help") == 0) |
Andreas Henriksson | 7a96e19 | 2009-12-07 13:12:36 +0100 | [diff] [blame] | 3666 | help(); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3667 | else { |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 3668 | fprintf(stderr, "ss: \"%s\" is invalid family\n", |
| 3669 | optarg); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3670 | usage(); |
| 3671 | } |
| 3672 | break; |
| 3673 | case 'A': |
| 3674 | { |
| 3675 | char *p, *p1; |
| 3676 | if (!saw_query) { |
| 3677 | current_filter.dbs = 0; |
Phil Sutter | 7f9dddb | 2015-08-07 15:31:27 +0200 | [diff] [blame] | 3678 | state_filter = state_filter ? |
| 3679 | state_filter : SS_CONN; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3680 | saw_query = 1; |
| 3681 | do_default = 0; |
| 3682 | } |
| 3683 | p = p1 = optarg; |
| 3684 | do { |
| 3685 | if ((p1 = strchr(p, ',')) != NULL) |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 3686 | *p1 = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3687 | if (strcmp(p, "all") == 0) { |
Vadim Kochan | 57ff5a1 | 2015-04-30 07:30:24 +0300 | [diff] [blame] | 3688 | filter_default_dbs(¤t_filter); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3689 | } else if (strcmp(p, "inet") == 0) { |
Vadim Kochan | 57ff5a1 | 2015-04-30 07:30:24 +0300 | [diff] [blame] | 3690 | filter_db_set(¤t_filter, UDP_DB); |
| 3691 | filter_db_set(¤t_filter, DCCP_DB); |
| 3692 | filter_db_set(¤t_filter, TCP_DB); |
| 3693 | filter_db_set(¤t_filter, RAW_DB); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3694 | } else if (strcmp(p, "udp") == 0) { |
Vadim Kochan | 57ff5a1 | 2015-04-30 07:30:24 +0300 | [diff] [blame] | 3695 | filter_db_set(¤t_filter, UDP_DB); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 3696 | } else if (strcmp(p, "dccp") == 0) { |
Vadim Kochan | 57ff5a1 | 2015-04-30 07:30:24 +0300 | [diff] [blame] | 3697 | filter_db_set(¤t_filter, DCCP_DB); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3698 | } else if (strcmp(p, "tcp") == 0) { |
Vadim Kochan | 57ff5a1 | 2015-04-30 07:30:24 +0300 | [diff] [blame] | 3699 | filter_db_set(¤t_filter, TCP_DB); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3700 | } else if (strcmp(p, "raw") == 0) { |
Vadim Kochan | 57ff5a1 | 2015-04-30 07:30:24 +0300 | [diff] [blame] | 3701 | filter_db_set(¤t_filter, RAW_DB); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3702 | } else if (strcmp(p, "unix") == 0) { |
Vadim Kochan | 57ff5a1 | 2015-04-30 07:30:24 +0300 | [diff] [blame] | 3703 | filter_db_set(¤t_filter, UNIX_ST_DB); |
| 3704 | filter_db_set(¤t_filter, UNIX_DG_DB); |
| 3705 | filter_db_set(¤t_filter, UNIX_SQ_DB); |
osdl.net!shemminger | 1a5bad5 | 2004-07-07 17:06:35 +0000 | [diff] [blame] | 3706 | } else if (strcasecmp(p, "unix_stream") == 0 || |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3707 | strcmp(p, "u_str") == 0) { |
Vadim Kochan | 57ff5a1 | 2015-04-30 07:30:24 +0300 | [diff] [blame] | 3708 | filter_db_set(¤t_filter, UNIX_ST_DB); |
osdl.net!shemminger | 1a5bad5 | 2004-07-07 17:06:35 +0000 | [diff] [blame] | 3709 | } else if (strcasecmp(p, "unix_dgram") == 0 || |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3710 | strcmp(p, "u_dgr") == 0) { |
Vadim Kochan | 57ff5a1 | 2015-04-30 07:30:24 +0300 | [diff] [blame] | 3711 | filter_db_set(¤t_filter, UNIX_DG_DB); |
Masatake YAMATO | 30b669d | 2014-01-08 20:13:46 +0900 | [diff] [blame] | 3712 | } else if (strcasecmp(p, "unix_seqpacket") == 0 || |
| 3713 | strcmp(p, "u_seq") == 0) { |
Vadim Kochan | 57ff5a1 | 2015-04-30 07:30:24 +0300 | [diff] [blame] | 3714 | filter_db_set(¤t_filter, UNIX_SQ_DB); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3715 | } else if (strcmp(p, "packet") == 0) { |
Vadim Kochan | 57ff5a1 | 2015-04-30 07:30:24 +0300 | [diff] [blame] | 3716 | filter_db_set(¤t_filter, PACKET_R_DB); |
| 3717 | filter_db_set(¤t_filter, PACKET_DG_DB); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3718 | } else if (strcmp(p, "packet_raw") == 0 || |
| 3719 | strcmp(p, "p_raw") == 0) { |
Vadim Kochan | 57ff5a1 | 2015-04-30 07:30:24 +0300 | [diff] [blame] | 3720 | filter_db_set(¤t_filter, PACKET_R_DB); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3721 | } else if (strcmp(p, "packet_dgram") == 0 || |
| 3722 | strcmp(p, "p_dgr") == 0) { |
Vadim Kochan | 57ff5a1 | 2015-04-30 07:30:24 +0300 | [diff] [blame] | 3723 | filter_db_set(¤t_filter, PACKET_DG_DB); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3724 | } else if (strcmp(p, "netlink") == 0) { |
Vadim Kochan | 57ff5a1 | 2015-04-30 07:30:24 +0300 | [diff] [blame] | 3725 | filter_db_set(¤t_filter, NETLINK_DB); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3726 | } else { |
| 3727 | fprintf(stderr, "ss: \"%s\" is illegal socket table id\n", p); |
| 3728 | usage(); |
| 3729 | } |
| 3730 | p = p1 + 1; |
| 3731 | } while (p1); |
| 3732 | break; |
| 3733 | } |
| 3734 | case 's': |
| 3735 | do_summary = 1; |
| 3736 | break; |
| 3737 | case 'D': |
| 3738 | dump_tcpdiag = optarg; |
| 3739 | break; |
| 3740 | case 'F': |
| 3741 | if (filter_fp) { |
| 3742 | fprintf(stderr, "More than one filter file\n"); |
| 3743 | exit(-1); |
| 3744 | } |
| 3745 | if (optarg[0] == '-') |
| 3746 | filter_fp = stdin; |
| 3747 | else |
| 3748 | filter_fp = fopen(optarg, "r"); |
| 3749 | if (!filter_fp) { |
| 3750 | perror("fopen filter file"); |
| 3751 | exit(-1); |
| 3752 | } |
| 3753 | break; |
| 3754 | case 'v': |
| 3755 | case 'V': |
| 3756 | printf("ss utility, iproute2-ss%s\n", SNAPSHOT); |
| 3757 | exit(0); |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 3758 | case 'z': |
| 3759 | show_sock_ctx++; |
| 3760 | case 'Z': |
| 3761 | if (is_selinux_enabled() <= 0) { |
| 3762 | fprintf(stderr, "ss: SELinux is not enabled.\n"); |
| 3763 | exit(1); |
| 3764 | } |
| 3765 | show_proc_ctx++; |
| 3766 | user_ent_hash_build(); |
| 3767 | break; |
Vadim Kochan | 95ce04b | 2015-02-08 08:58:43 +0200 | [diff] [blame] | 3768 | case 'N': |
| 3769 | if (netns_switch(optarg)) |
| 3770 | exit(1); |
| 3771 | break; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3772 | case 'h': |
Andreas Henriksson | 7a96e19 | 2009-12-07 13:12:36 +0100 | [diff] [blame] | 3773 | help(); |
Phil Sutter | f73105a | 2015-10-15 21:01:16 +0200 | [diff] [blame^] | 3774 | case '?': |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3775 | default: |
| 3776 | usage(); |
| 3777 | } |
| 3778 | } |
| 3779 | |
| 3780 | argc -= optind; |
| 3781 | argv += optind; |
| 3782 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3783 | if (do_summary) { |
| 3784 | print_summary(); |
| 3785 | if (do_default && argc == 0) |
| 3786 | exit(0); |
| 3787 | } |
| 3788 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3789 | while (argc > 0) { |
| 3790 | if (strcmp(*argv, "state") == 0) { |
| 3791 | NEXT_ARG(); |
| 3792 | if (!saw_states) |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 3793 | state_filter = 0; |
| 3794 | state_filter |= scan_state(*argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3795 | saw_states = 1; |
| 3796 | } else if (strcmp(*argv, "exclude") == 0 || |
| 3797 | strcmp(*argv, "excl") == 0) { |
| 3798 | NEXT_ARG(); |
| 3799 | if (!saw_states) |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 3800 | state_filter = SS_ALL; |
| 3801 | state_filter &= ~scan_state(*argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3802 | saw_states = 1; |
| 3803 | } else { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3804 | break; |
| 3805 | } |
| 3806 | argc--; argv++; |
| 3807 | } |
| 3808 | |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 3809 | if (do_default) { |
| 3810 | state_filter = state_filter ? state_filter : SS_CONN; |
| 3811 | filter_default_dbs(¤t_filter); |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 3812 | } |
| 3813 | |
Vadim Kochan | 57ff5a1 | 2015-04-30 07:30:24 +0300 | [diff] [blame] | 3814 | filter_states_set(¤t_filter, state_filter); |
| 3815 | filter_merge_defaults(¤t_filter); |
| 3816 | |
Vadim Kochan | 9db7bf1 | 2015-01-04 22:18:40 +0200 | [diff] [blame] | 3817 | if (resolve_services && resolve_hosts && |
| 3818 | (current_filter.dbs&(UNIX_DBM|(1<<TCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB)))) |
| 3819 | init_service_resolver(); |
| 3820 | |
| 3821 | |
| 3822 | if (current_filter.dbs == 0) { |
| 3823 | fprintf(stderr, "ss: no socket tables to show with such filter.\n"); |
| 3824 | exit(0); |
| 3825 | } |
| 3826 | if (current_filter.families == 0) { |
| 3827 | fprintf(stderr, "ss: no families to show with such filter.\n"); |
| 3828 | exit(0); |
| 3829 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3830 | if (current_filter.states == 0) { |
| 3831 | fprintf(stderr, "ss: no socket states to show with such filter.\n"); |
| 3832 | exit(0); |
| 3833 | } |
| 3834 | |
| 3835 | if (dump_tcpdiag) { |
| 3836 | FILE *dump_fp = stdout; |
| 3837 | if (!(current_filter.dbs & (1<<TCP_DB))) { |
| 3838 | fprintf(stderr, "ss: tcpdiag dump requested and no tcp in filter.\n"); |
| 3839 | exit(0); |
| 3840 | } |
| 3841 | if (dump_tcpdiag[0] != '-') { |
| 3842 | dump_fp = fopen(dump_tcpdiag, "w"); |
| 3843 | if (!dump_tcpdiag) { |
| 3844 | perror("fopen dump file"); |
| 3845 | exit(-1); |
| 3846 | } |
| 3847 | } |
Pavel Emelyanov | 3fe5b53 | 2012-10-25 03:18:31 +0000 | [diff] [blame] | 3848 | inet_show_netlink(¤t_filter, dump_fp, IPPROTO_TCP); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3849 | fflush(dump_fp); |
| 3850 | exit(0); |
| 3851 | } |
| 3852 | |
Vadim Kochan | 1527a17 | 2015-02-13 13:01:08 +0200 | [diff] [blame] | 3853 | if (ssfilter_parse(¤t_filter.f, argc, argv, filter_fp)) |
| 3854 | usage(); |
| 3855 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3856 | netid_width = 0; |
| 3857 | if (current_filter.dbs&(current_filter.dbs-1)) |
| 3858 | netid_width = 5; |
| 3859 | |
| 3860 | state_width = 0; |
| 3861 | if (current_filter.states&(current_filter.states-1)) |
| 3862 | state_width = 10; |
| 3863 | |
| 3864 | screen_width = 80; |
| 3865 | if (isatty(STDOUT_FILENO)) { |
| 3866 | struct winsize w; |
| 3867 | |
| 3868 | if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) { |
| 3869 | if (w.ws_col > 0) |
| 3870 | screen_width = w.ws_col; |
| 3871 | } |
| 3872 | } |
| 3873 | |
| 3874 | addrp_width = screen_width; |
| 3875 | addrp_width -= netid_width+1; |
| 3876 | addrp_width -= state_width+1; |
| 3877 | addrp_width -= 14; |
| 3878 | |
| 3879 | if (addrp_width&1) { |
| 3880 | if (netid_width) |
| 3881 | netid_width++; |
| 3882 | else if (state_width) |
| 3883 | state_width++; |
| 3884 | } |
| 3885 | |
| 3886 | addrp_width /= 2; |
| 3887 | addrp_width--; |
| 3888 | |
| 3889 | serv_width = resolve_services ? 7 : 5; |
| 3890 | |
| 3891 | if (addrp_width < 15+serv_width+1) |
| 3892 | addrp_width = 15+serv_width+1; |
| 3893 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 3894 | addr_width = addrp_width - serv_width - 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3895 | |
| 3896 | if (netid_width) |
| 3897 | printf("%-*s ", netid_width, "Netid"); |
| 3898 | if (state_width) |
| 3899 | printf("%-*s ", state_width, "State"); |
| 3900 | printf("%-6s %-6s ", "Recv-Q", "Send-Q"); |
| 3901 | |
vadimk | d68e00f | 2014-12-05 19:19:11 +0200 | [diff] [blame] | 3902 | /* Make enough space for the local/remote port field */ |
| 3903 | addr_width -= 13; |
| 3904 | serv_width += 13; |
vadimk | 2dc8548 | 2014-10-18 20:46:29 +0300 | [diff] [blame] | 3905 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3906 | printf("%*s:%-*s %*s:%-*s\n", |
| 3907 | addr_width, "Local Address", serv_width, "Port", |
vadimk | d68e00f | 2014-12-05 19:19:11 +0200 | [diff] [blame] | 3908 | addr_width, "Peer Address", serv_width, "Port"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3909 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3910 | fflush(stdout); |
| 3911 | |
Craig Gallek | 6885e3b | 2015-06-17 11:14:48 -0400 | [diff] [blame] | 3912 | if (follow_events) |
| 3913 | exit(handle_follow_request(¤t_filter)); |
| 3914 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3915 | if (current_filter.dbs & (1<<NETLINK_DB)) |
| 3916 | netlink_show(¤t_filter); |
| 3917 | if (current_filter.dbs & PACKET_DBM) |
| 3918 | packet_show(¤t_filter); |
| 3919 | if (current_filter.dbs & UNIX_DBM) |
| 3920 | unix_show(¤t_filter); |
| 3921 | if (current_filter.dbs & (1<<RAW_DB)) |
| 3922 | raw_show(¤t_filter); |
| 3923 | if (current_filter.dbs & (1<<UDP_DB)) |
| 3924 | udp_show(¤t_filter); |
| 3925 | if (current_filter.dbs & (1<<TCP_DB)) |
Pavel Emelyanov | 3fe5b53 | 2012-10-25 03:18:31 +0000 | [diff] [blame] | 3926 | tcp_show(¤t_filter, IPPROTO_TCP); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 3927 | if (current_filter.dbs & (1<<DCCP_DB)) |
Pavel Emelyanov | 3fe5b53 | 2012-10-25 03:18:31 +0000 | [diff] [blame] | 3928 | tcp_show(¤t_filter, IPPROTO_DCCP); |
Richard Haines | 116ac92 | 2014-03-07 10:36:52 +0000 | [diff] [blame] | 3929 | |
| 3930 | if (show_users || show_proc_ctx || show_sock_ctx) |
| 3931 | user_ent_destroy(); |
| 3932 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 3933 | return 0; |
| 3934 | } |