| 1 |
/* |
| 2 |
* tcpConnTable MIB architecture support |
| 3 |
* |
| 4 |
* $Id$ |
| 5 |
*/ |
| 6 |
#include <net-snmp/net-snmp-config.h> |
| 7 |
#include <net-snmp/net-snmp-includes.h> |
| 8 |
|
| 9 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 10 |
#include <net-snmp/data_access/tcpConn.h> |
| 11 |
|
| 12 |
#include "tcp-mib/tcpConnectionTable/tcpConnectionTable_constants.h" |
| 13 |
#include "tcp-mib/data_access/tcpConn_private.h" |
| 14 |
#include "mibgroup/util_funcs/get_pid_from_inode.h" |
| 15 |
static int |
| 16 |
linux_states[12] = { 1, 5, 3, 4, 6, 7, 11, 1, 8, 9, 2, 10 }; |
| 17 |
|
| 18 |
static int _load4(netsnmp_container *container, u_int flags); |
| 19 |
#if defined (NETSNMP_ENABLE_IPV6) |
| 20 |
static int _load6(netsnmp_container *container, u_int flags); |
| 21 |
#endif |
| 22 |
|
| 23 |
/* |
| 24 |
* initialize arch specific storage |
| 25 |
* |
| 26 |
* @retval 0: success |
| 27 |
* @retval <0: error |
| 28 |
*/ |
| 29 |
int |
| 30 |
netsnmp_arch_tcpconn_entry_init(netsnmp_tcpconn_entry *entry) |
| 31 |
{ |
| 32 |
/* |
| 33 |
* init |
| 34 |
*/ |
| 35 |
return 0; |
| 36 |
} |
| 37 |
|
| 38 |
/* |
| 39 |
* cleanup arch specific storage |
| 40 |
*/ |
| 41 |
void |
| 42 |
netsnmp_arch_tcpconn_entry_cleanup(netsnmp_tcpconn_entry *entry) |
| 43 |
{ |
| 44 |
/* |
| 45 |
* cleanup |
| 46 |
*/ |
| 47 |
} |
| 48 |
|
| 49 |
/* |
| 50 |
* copy arch specific storage |
| 51 |
*/ |
| 52 |
int |
| 53 |
netsnmp_arch_tcpconn_entry_copy(netsnmp_tcpconn_entry *lhs, |
| 54 |
netsnmp_tcpconn_entry *rhs) |
| 55 |
{ |
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
#ifdef TCPCONN_DELETE_SUPPORTED |
| 60 |
/* |
| 61 |
* delete an entry |
| 62 |
*/ |
| 63 |
int |
| 64 |
netsnmp_arch_tcpconn_entry_delete(netsnmp_tcpconn_entry *entry) |
| 65 |
{ |
| 66 |
if (NULL == entry) |
| 67 |
return -1; |
| 68 |
/** xxx-rks:9 tcpConn delete not implemented */ |
| 69 |
return -1; |
| 70 |
} |
| 71 |
#endif /* TCPCONN_DELETE_SUPPORTED */ |
| 72 |
|
| 73 |
/** |
| 74 |
* |
| 75 |
* @retval 0 no errors |
| 76 |
* @retval !0 errors |
| 77 |
*/ |
| 78 |
int |
| 79 |
netsnmp_arch_tcpconn_container_load(netsnmp_container *container, |
| 80 |
u_int load_flags ) |
| 81 |
{ |
| 82 |
int rc = 0; |
| 83 |
|
| 84 |
DEBUGMSGTL(("access:tcpconn:container", |
| 85 |
"tcpconn_container_arch_load (flags %x)\n", load_flags)); |
| 86 |
|
| 87 |
/* Setup the pid_from_inode table, and fill it.*/ |
| 88 |
netsnmp_get_pid_from_inode_init(); |
| 89 |
|
| 90 |
if (NULL == container) { |
| 91 |
snmp_log(LOG_ERR, "no container specified/found for access_tcpconn\n"); |
| 92 |
return -1; |
| 93 |
} |
| 94 |
|
| 95 |
rc = _load4(container, load_flags); |
| 96 |
|
| 97 |
#if defined (NETSNMP_ENABLE_IPV6) |
| 98 |
if((0 != rc) || (load_flags & NETSNMP_ACCESS_TCPCONN_LOAD_IPV4_ONLY)) |
| 99 |
return rc; |
| 100 |
|
| 101 |
/* |
| 102 |
* load ipv6. ipv6 module might not be loaded, |
| 103 |
* so ignore -2 err (file not found) |
| 104 |
*/ |
| 105 |
rc = _load6(container, load_flags); |
| 106 |
if (-2 == rc) |
| 107 |
rc = 0; |
| 108 |
#endif |
| 109 |
|
| 110 |
return rc; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* |
| 115 |
* @retval 0 no errors |
| 116 |
* @retval !0 errors |
| 117 |
*/ |
| 118 |
static int |
| 119 |
_load4(netsnmp_container *container, u_int load_flags) |
| 120 |
{ |
| 121 |
int rc = 0; |
| 122 |
FILE *in; |
| 123 |
char line[160]; |
| 124 |
enum { rbufsize = 65536 }; |
| 125 |
void *rbuf = alloca(rbufsize); |
| 126 |
|
| 127 |
netsnmp_assert(NULL != container); |
| 128 |
|
| 129 |
#define PROCFILE "/proc/net/tcp" |
| 130 |
if (!(in = fopen(PROCFILE, "r"))) { |
| 131 |
snmp_log(LOG_ERR,"could not open " PROCFILE "\n"); |
| 132 |
return -2; |
| 133 |
} |
| 134 |
|
| 135 |
setvbuf(in, rbuf, _IOFBF, rbufsize); |
| 136 |
NETSNMP_IGNORE_RESULT(fgets(line, sizeof(line), in)); /* skip header */ |
| 137 |
|
| 138 |
/* |
| 139 |
* sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode |
| 140 |
* 0: 00000000:8000 00000000:0000 0A 00000000:00000000 00:00000000 00000000 29 0 1028 1 df7b1b80 300 0 0 2 -1 |
| 141 |
*/ |
| 142 |
while (fgets(line, sizeof(line), in)) { |
| 143 |
netsnmp_tcpconn_entry *entry; |
| 144 |
unsigned int state, local_port, remote_port, tmp_state; |
| 145 |
unsigned long long inode; |
| 146 |
size_t buf_len, offset; |
| 147 |
char local_addr[10], remote_addr[10]; |
| 148 |
u_char *tmp_ptr; |
| 149 |
|
| 150 |
if (6 != (rc = sscanf(line, "%*d: %8[0-9A-Z]:%x %8[0-9A-Z]:%x %x %*x:%*x %*x:%*x %*x %*x %*x %llu", |
| 151 |
local_addr, &local_port, |
| 152 |
remote_addr, &remote_port, &tmp_state, &inode))) { |
| 153 |
DEBUGMSGT(("access:tcpconn:container", |
| 154 |
"error parsing line (%d != 6)\n", rc)); |
| 155 |
DEBUGMSGT(("access:tcpconn:container"," line '%s'\n", line)); |
| 156 |
snmp_log(LOG_ERR, "tcp:_load4: bad line in " PROCFILE ": %s\n", line); |
| 157 |
rc = 0; |
| 158 |
continue; |
| 159 |
} |
| 160 |
DEBUGMSGT(("verbose:access:tcpconn:container"," line '%s'\n", line)); |
| 161 |
|
| 162 |
/* |
| 163 |
* check if we care about listen state |
| 164 |
*/ |
| 165 |
state = (tmp_state & 0xf) < 12 ? linux_states[tmp_state & 0xf] : 2; |
| 166 |
if (load_flags) { |
| 167 |
if (TCPCONNECTIONSTATE_LISTEN == state) { |
| 168 |
if (load_flags & NETSNMP_ACCESS_TCPCONN_LOAD_NOLISTEN) { |
| 169 |
DEBUGMSGT(("verbose:access:tcpconn:container", |
| 170 |
" skipping listen\n")); |
| 171 |
continue; |
| 172 |
} |
| 173 |
} |
| 174 |
else if (load_flags & NETSNMP_ACCESS_TCPCONN_LOAD_ONLYLISTEN) { |
| 175 |
DEBUGMSGT(("verbose:access:tcpconn:container", |
| 176 |
" skipping non-listen\n")); |
| 177 |
continue; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
/* |
| 182 |
*/ |
| 183 |
entry = netsnmp_access_tcpconn_entry_create(); |
| 184 |
if(NULL == entry) { |
| 185 |
rc = -3; |
| 186 |
break; |
| 187 |
} |
| 188 |
|
| 189 |
/** oddly enough, these appear to already be in network order */ |
| 190 |
entry->loc_port = (unsigned short) local_port; |
| 191 |
entry->rmt_port = (unsigned short) remote_port; |
| 192 |
entry->tcpConnState = state; |
| 193 |
entry->pid = netsnmp_get_pid_from_inode(inode); |
| 194 |
|
| 195 |
/** the addr string may need work */ |
| 196 |
buf_len = strlen(local_addr); |
| 197 |
if ((8 != buf_len) || |
| 198 |
(-1 == netsnmp_addrstr_hton(local_addr, 8))) { |
| 199 |
DEBUGMSGT(("verbose:access:tcpconn:container", |
| 200 |
" error processing local address\n")); |
| 201 |
netsnmp_access_tcpconn_entry_free(entry); |
| 202 |
continue; |
| 203 |
} |
| 204 |
offset = 0; |
| 205 |
tmp_ptr = entry->loc_addr; |
| 206 |
rc = netsnmp_hex_to_binary(&tmp_ptr, &buf_len, |
| 207 |
&offset, 0, local_addr, NULL); |
| 208 |
entry->loc_addr_len = offset; |
| 209 |
if ( 4 != entry->loc_addr_len ) { |
| 210 |
DEBUGMSGT(("access:tcpconn:container", |
| 211 |
"error parsing local addr (%d != 4)\n", |
| 212 |
entry->loc_addr_len)); |
| 213 |
DEBUGMSGT(("access:tcpconn:container"," line '%s'\n", line)); |
| 214 |
netsnmp_access_tcpconn_entry_free(entry); |
| 215 |
continue; |
| 216 |
} |
| 217 |
|
| 218 |
/** the addr string may need work */ |
| 219 |
buf_len = strlen((char*)remote_addr); |
| 220 |
if ((8 != buf_len) || |
| 221 |
(-1 == netsnmp_addrstr_hton(remote_addr, 8))) { |
| 222 |
DEBUGMSGT(("verbose:access:tcpconn:container", |
| 223 |
" error processing remote address\n")); |
| 224 |
netsnmp_access_tcpconn_entry_free(entry); |
| 225 |
continue; |
| 226 |
} |
| 227 |
offset = 0; |
| 228 |
tmp_ptr = entry->rmt_addr; |
| 229 |
rc = netsnmp_hex_to_binary(&tmp_ptr, &buf_len, |
| 230 |
&offset, 0, remote_addr, NULL); |
| 231 |
entry->rmt_addr_len = offset; |
| 232 |
if ( 4 != entry->rmt_addr_len ) { |
| 233 |
DEBUGMSGT(("access:tcpconn:container", |
| 234 |
"error parsing remote addr (%d != 4)\n", |
| 235 |
entry->rmt_addr_len)); |
| 236 |
DEBUGMSGT(("access:tcpconn:container"," line '%s'\n", line)); |
| 237 |
netsnmp_access_tcpconn_entry_free(entry); |
| 238 |
continue; |
| 239 |
} |
| 240 |
|
| 241 |
/* |
| 242 |
* add entry to container |
| 243 |
*/ |
| 244 |
entry->arbitrary_index = CONTAINER_SIZE(container) + 1; |
| 245 |
if (CONTAINER_INSERT(container, entry) < 0) { |
| 246 |
netsnmp_access_tcpconn_entry_free(entry); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
fclose(in); |
| 251 |
|
| 252 |
if(rc<0) |
| 253 |
return rc; |
| 254 |
|
| 255 |
return 0; |
| 256 |
} |
| 257 |
|
| 258 |
#if defined (NETSNMP_ENABLE_IPV6) |
| 259 |
/** |
| 260 |
* |
| 261 |
* @retval 0 no errors |
| 262 |
* @retval !0 errors |
| 263 |
*/ |
| 264 |
static int |
| 265 |
_load6(netsnmp_container *container, u_int load_flags) |
| 266 |
{ |
| 267 |
int rc = 0; |
| 268 |
FILE *in; |
| 269 |
char line[360]; |
| 270 |
enum { rbufsize = 65536 }; |
| 271 |
void *rbuf = alloca(rbufsize); |
| 272 |
|
| 273 |
netsnmp_assert(NULL != container); |
| 274 |
|
| 275 |
#undef PROCFILE |
| 276 |
#define PROCFILE "/proc/net/tcp6" |
| 277 |
if (!(in = fopen(PROCFILE, "r"))) { |
| 278 |
DEBUGMSGTL(("access:tcpconn:container","could not open " PROCFILE "\n")); |
| 279 |
return -2; |
| 280 |
} |
| 281 |
|
| 282 |
setvbuf(in, rbuf, _IOFBF, rbufsize); |
| 283 |
NETSNMP_IGNORE_RESULT(fgets(line, sizeof(line), in)); /* skip header */ |
| 284 |
|
| 285 |
/* |
| 286 |
* Note: PPC (big endian) |
| 287 |
* |
| 288 |
* sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode |
| 289 |
* 0: 00000000000000000000000000000001:1466 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 500 0 326699 1 efb81580 3000 0 0 2 -1 |
| 290 |
*/ |
| 291 |
while (fgets(line, sizeof(line), in)) { |
| 292 |
netsnmp_tcpconn_entry *entry; |
| 293 |
int state, local_port, remote_port, tmp_state; |
| 294 |
unsigned long long inode; |
| 295 |
size_t buf_len, offset; |
| 296 |
char local_addr[48], remote_addr[48]; |
| 297 |
u_char *tmp_ptr; |
| 298 |
|
| 299 |
if (6 != (rc = sscanf(line, "%*d: %47[0-9A-Z]:%x %47[0-9A-Z]:%x %x %*x:%*x %*x:%*x %*x %*x %*x %llu", |
| 300 |
local_addr, &local_port, |
| 301 |
remote_addr, &remote_port, &tmp_state, &inode))) { |
| 302 |
DEBUGMSGT(("access:tcpconn:container", |
| 303 |
"error parsing line (%d != 6)\n", rc)); |
| 304 |
DEBUGMSGT(("access:tcpconn:container"," line '%s'\n", line)); |
| 305 |
snmp_log(LOG_ERR, "tcp:_load6: bad line in " PROCFILE ": %s\n", line); |
| 306 |
rc = 0; |
| 307 |
continue; |
| 308 |
} |
| 309 |
DEBUGMSGT(("verbose:access:tcpconn:container"," line '%s'\n", line)); |
| 310 |
|
| 311 |
/* |
| 312 |
* check if we care about listen state |
| 313 |
*/ |
| 314 |
state = (tmp_state & 0xf) < 12 ? linux_states[tmp_state & 0xf] : 2; |
| 315 |
if (load_flags) { |
| 316 |
if (TCPCONNECTIONSTATE_LISTEN == state) { |
| 317 |
if (load_flags & NETSNMP_ACCESS_TCPCONN_LOAD_NOLISTEN) { |
| 318 |
DEBUGMSGT(("verbose:access:tcpconn:container", |
| 319 |
" skipping listen\n")); |
| 320 |
continue; |
| 321 |
} |
| 322 |
} |
| 323 |
else if (load_flags & NETSNMP_ACCESS_TCPCONN_LOAD_ONLYLISTEN) { |
| 324 |
DEBUGMSGT(("verbose:access:tcpconn:container", |
| 325 |
" skipping non-listen\n")); |
| 326 |
continue; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
/* |
| 331 |
*/ |
| 332 |
entry = netsnmp_access_tcpconn_entry_create(); |
| 333 |
if(NULL == entry) { |
| 334 |
rc = -3; |
| 335 |
break; |
| 336 |
} |
| 337 |
|
| 338 |
/** oddly enough, these appear to already be in network order */ |
| 339 |
entry->loc_port = (unsigned short) local_port; |
| 340 |
entry->rmt_port = (unsigned short) remote_port; |
| 341 |
entry->tcpConnState = state; |
| 342 |
entry->pid = netsnmp_get_pid_from_inode(inode); |
| 343 |
|
| 344 |
/** the addr string may need work */ |
| 345 |
buf_len = strlen(local_addr); |
| 346 |
if ((32 != buf_len) || |
| 347 |
(-1 == netsnmp_addrstr_hton(local_addr, 32))) { |
| 348 |
DEBUGMSGT(("verbose:access:tcpconn:container", |
| 349 |
" error processing local address\n")); |
| 350 |
netsnmp_access_tcpconn_entry_free(entry); |
| 351 |
continue; |
| 352 |
} |
| 353 |
offset = 0; |
| 354 |
tmp_ptr = entry->loc_addr; |
| 355 |
rc = netsnmp_hex_to_binary(&tmp_ptr, &buf_len, |
| 356 |
&offset, 0, local_addr, NULL); |
| 357 |
entry->loc_addr_len = offset; |
| 358 |
if (( 16 != entry->loc_addr_len ) && ( 20 != entry->loc_addr_len )) { |
| 359 |
DEBUGMSGT(("access:tcpconn:container", |
| 360 |
"error parsing local addr (%d != 16|20)\n", |
| 361 |
entry->loc_addr_len)); |
| 362 |
DEBUGMSGT(("access:tcpconn:container"," line '%s'\n", line)); |
| 363 |
netsnmp_access_tcpconn_entry_free(entry); |
| 364 |
continue; |
| 365 |
} |
| 366 |
|
| 367 |
buf_len = strlen((char*)remote_addr); |
| 368 |
if ((32 != buf_len) || |
| 369 |
(-1 == netsnmp_addrstr_hton(remote_addr, 32))) { |
| 370 |
DEBUGMSGT(("verbose:access:tcpconn:container", |
| 371 |
" error processing remote address\n")); |
| 372 |
netsnmp_access_tcpconn_entry_free(entry); |
| 373 |
continue; |
| 374 |
} |
| 375 |
offset = 0; |
| 376 |
tmp_ptr = entry->rmt_addr; |
| 377 |
rc = netsnmp_hex_to_binary(&tmp_ptr, &buf_len, |
| 378 |
&offset, 0, remote_addr, NULL); |
| 379 |
entry->rmt_addr_len = offset; |
| 380 |
if (( 16 != entry->rmt_addr_len ) && ( 20 != entry->rmt_addr_len )) { |
| 381 |
DEBUGMSGT(("access:tcpconn:container", |
| 382 |
"error parsing remote addr (%d != 16|20)\n", |
| 383 |
entry->rmt_addr_len)); |
| 384 |
DEBUGMSGT(("access:tcpconn:container"," line '%s'\n", line)); |
| 385 |
netsnmp_access_tcpconn_entry_free(entry); |
| 386 |
continue; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
/* |
| 391 |
* add entry to container |
| 392 |
*/ |
| 393 |
entry->arbitrary_index = CONTAINER_SIZE(container) + 1; |
| 394 |
if (CONTAINER_INSERT(container, entry) < 0) { |
| 395 |
netsnmp_access_tcpconn_entry_free(entry); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
fclose(in); |
| 400 |
|
| 401 |
if(rc<0) |
| 402 |
return rc; |
| 403 |
|
| 404 |
return 0; |
| 405 |
} |
| 406 |
#endif /* NETSNMP_ENABLE_IPV6 */ |