| 1 |
/* |
| 2 |
* tcpConnTable MIB architecture support for OpenBSD |
| 3 |
*/ |
| 4 |
#include <net-snmp/net-snmp-config.h> |
| 5 |
#include <net-snmp/net-snmp-includes.h> |
| 6 |
|
| 7 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 8 |
#include <net-snmp/data_access/tcpConn.h> |
| 9 |
#include <net-snmp/agent/auto_nlist.h> |
| 10 |
|
| 11 |
#include "tcp-mib/tcpConnectionTable/tcpConnectionTable_constants.h" |
| 12 |
#include "tcp-mib/data_access/tcpConn_private.h" |
| 13 |
|
| 14 |
#include "mibII/mibII_common.h" |
| 15 |
|
| 16 |
#ifdef HAVE_NETINET_TCP_H |
| 17 |
#include <netinet/tcp.h> |
| 18 |
#endif |
| 19 |
#ifdef HAVE_NETINET_TCP_TIMER_H |
| 20 |
#include <netinet/tcp_timer.h> |
| 21 |
#endif |
| 22 |
#ifdef HAVE_NETINET_TCPIP_H |
| 23 |
#include <netinet/tcpip.h> |
| 24 |
#endif |
| 25 |
#ifdef HAVE_NETINET_TCP_VAR_H |
| 26 |
#include <netinet/tcp_var.h> |
| 27 |
#endif |
| 28 |
|
| 29 |
#ifdef HAVE_KVM_GETFILES |
| 30 |
#if defined(HAVE_KVM_GETFILE2) || !defined(openbsd5) |
| 31 |
#undef HAVE_KVM_GETFILES |
| 32 |
#endif |
| 33 |
#endif |
| 34 |
|
| 35 |
#ifdef HAVE_KVM_GETFILES |
| 36 |
#include <kvm.h> |
| 37 |
#include <sys/sysctl.h> |
| 38 |
#define _KERNEL /* for DTYPE_SOCKET */ |
| 39 |
#include <sys/file.h> |
| 40 |
#undef _KERNEL |
| 41 |
#endif |
| 42 |
|
| 43 |
#ifdef HAVE_KVM_GETFILES |
| 44 |
static int _kvmload(netsnmp_container *container, u_int flags); |
| 45 |
#else |
| 46 |
static int _load(netsnmp_container *container, u_int flags); |
| 47 |
#endif |
| 48 |
|
| 49 |
/* |
| 50 |
* initialize arch specific storage |
| 51 |
* |
| 52 |
* @retval 0: success |
| 53 |
* @retval <0: error |
| 54 |
*/ |
| 55 |
int |
| 56 |
netsnmp_arch_tcpconn_entry_init(netsnmp_tcpconn_entry *entry) |
| 57 |
{ |
| 58 |
/* |
| 59 |
* init |
| 60 |
*/ |
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
/* |
| 65 |
* cleanup arch specific storage |
| 66 |
*/ |
| 67 |
void |
| 68 |
netsnmp_arch_tcpconn_entry_cleanup(netsnmp_tcpconn_entry *entry) |
| 69 |
{ |
| 70 |
/* |
| 71 |
* cleanup |
| 72 |
*/ |
| 73 |
} |
| 74 |
|
| 75 |
/* |
| 76 |
* copy arch specific storage |
| 77 |
*/ |
| 78 |
int |
| 79 |
netsnmp_arch_tcpconn_entry_copy(netsnmp_tcpconn_entry *lhs, |
| 80 |
netsnmp_tcpconn_entry *rhs) |
| 81 |
{ |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
/* |
| 86 |
* delete an entry |
| 87 |
*/ |
| 88 |
int |
| 89 |
netsnmp_arch_tcpconn_entry_delete(netsnmp_tcpconn_entry *entry) |
| 90 |
{ |
| 91 |
if (NULL == entry) |
| 92 |
return -1; |
| 93 |
/** xxx-rks:9 tcpConn delete not implemented */ |
| 94 |
return -1; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
/** |
| 99 |
* |
| 100 |
* @retval 0 no errors |
| 101 |
* @retval !0 errors |
| 102 |
*/ |
| 103 |
int |
| 104 |
netsnmp_arch_tcpconn_container_load(netsnmp_container *container, |
| 105 |
u_int load_flags ) |
| 106 |
{ |
| 107 |
int rc = 0; |
| 108 |
|
| 109 |
DEBUGMSGTL(("access:tcpconn:container", |
| 110 |
"tcpconn_container_arch_load (flags %x)\n", load_flags)); |
| 111 |
|
| 112 |
if (NULL == container) { |
| 113 |
snmp_log(LOG_ERR, "no container specified/found for access_tcpconn\n"); |
| 114 |
return -1; |
| 115 |
} |
| 116 |
|
| 117 |
#ifdef HAVE_KVM_GETFILES |
| 118 |
rc = _kvmload(container, load_flags); |
| 119 |
#else |
| 120 |
rc = _load(container, load_flags); |
| 121 |
#endif |
| 122 |
|
| 123 |
return rc; |
| 124 |
} |
| 125 |
|
| 126 |
#ifdef HAVE_KVM_GETFILES |
| 127 |
/** |
| 128 |
* |
| 129 |
* @retval 0 no errors |
| 130 |
* @retval !0 errors |
| 131 |
*/ |
| 132 |
static int |
| 133 |
_kvmload(netsnmp_container *container, u_int load_flags) |
| 134 |
{ |
| 135 |
int StateMap[] = { 1, 2, 3, 4, 5, 8, 6, 10, 9, 7, 11 }; |
| 136 |
netsnmp_tcpconn_entry *entry; |
| 137 |
struct kinfo_file *kf; |
| 138 |
int count; |
| 139 |
int state; |
| 140 |
int rc = 0; |
| 141 |
|
| 142 |
kf = kvm_getfiles(kd, KERN_FILE_BYFILE, DTYPE_SOCKET, sizeof(struct kinfo_file), &count); |
| 143 |
|
| 144 |
while (count--) { |
| 145 |
if (kf->so_protocol != IPPROTO_TCP) |
| 146 |
goto skip; |
| 147 |
|
| 148 |
state = StateMap[kf->t_state]; |
| 149 |
|
| 150 |
if (load_flags) { |
| 151 |
if (state == TCPCONNECTIONSTATE_LISTEN) { |
| 152 |
if (load_flags & NETSNMP_ACCESS_TCPCONN_LOAD_NOLISTEN) { |
| 153 |
DEBUGMSGT(("verbose:access:tcpconn:container", |
| 154 |
" skipping listen\n")); |
| 155 |
goto skip; |
| 156 |
} |
| 157 |
} |
| 158 |
else if (load_flags & NETSNMP_ACCESS_TCPCONN_LOAD_ONLYLISTEN) { |
| 159 |
DEBUGMSGT(("verbose:access:tcpconn:container", |
| 160 |
" skipping non-listen\n")); |
| 161 |
goto skip; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
#if !defined(NETSNMP_ENABLE_IPV6) |
| 166 |
if (kf->so_family == AF_INET6) |
| 167 |
goto skip; |
| 168 |
#endif |
| 169 |
|
| 170 |
entry = netsnmp_access_tcpconn_entry_create(); |
| 171 |
if(NULL == entry) { |
| 172 |
rc = -3; |
| 173 |
break; |
| 174 |
} |
| 175 |
|
| 176 |
/** oddly enough, these appear to already be in network order */ |
| 177 |
entry->loc_port = ntohs(kf->inp_lport); |
| 178 |
entry->rmt_port = ntohs(kf->inp_fport); |
| 179 |
entry->tcpConnState = StateMap[kf->t_state]; |
| 180 |
entry->pid = kf->p_pid; |
| 181 |
|
| 182 |
/** the addr string may need work */ |
| 183 |
if (kf->so_family == AF_INET6) { |
| 184 |
entry->loc_addr_len = entry->rmt_addr_len = 16; |
| 185 |
memcpy(entry->loc_addr, &kf->inp_laddru, 16); |
| 186 |
memcpy(entry->rmt_addr, &kf->inp_faddru, 16); |
| 187 |
} |
| 188 |
else { |
| 189 |
entry->loc_addr_len = entry->rmt_addr_len = 4; |
| 190 |
memcpy(entry->loc_addr, &kf->inp_laddru, 4); |
| 191 |
memcpy(entry->rmt_addr, &kf->inp_faddru, 4); |
| 192 |
} |
| 193 |
DEBUGMSGTL(("tcp-mib/data_access", "tcp %d %d %d\n", |
| 194 |
entry->loc_addr_len, entry->loc_port, entry->rmt_port)); |
| 195 |
|
| 196 |
/* |
| 197 |
* add entry to container |
| 198 |
*/ |
| 199 |
entry->arbitrary_index = CONTAINER_SIZE(container) + 1; |
| 200 |
CONTAINER_INSERT(container, entry); |
| 201 |
skip: |
| 202 |
kf++; |
| 203 |
} |
| 204 |
|
| 205 |
if (rc < 0) |
| 206 |
return rc; |
| 207 |
return 0; |
| 208 |
} |
| 209 |
|
| 210 |
#else /* HAVE_KVM_GETFILES */ |
| 211 |
|
| 212 |
/** |
| 213 |
* |
| 214 |
* @retval 0 no errors |
| 215 |
* @retval !0 errors |
| 216 |
*/ |
| 217 |
static int |
| 218 |
_load(netsnmp_container *container, u_int load_flags) |
| 219 |
{ |
| 220 |
struct inpcbtable table; |
| 221 |
struct inpcb *next, *prev; |
| 222 |
struct inpcb inpcb, previnpcb; |
| 223 |
struct tcpcb tcpcb; |
| 224 |
int StateMap[] = { 1, 2, 3, 4, 5, 8, 6, 10, 9, 7, 11 }; |
| 225 |
netsnmp_tcpconn_entry *entry; |
| 226 |
int state; |
| 227 |
int rc = 0; |
| 228 |
|
| 229 |
/* |
| 230 |
* Read in the buffer containing the TCP table data |
| 231 |
*/ |
| 232 |
if (!auto_nlist(TCP_SYMBOL, (char *)&table, sizeof(table))) { |
| 233 |
DEBUGMSGTL(("tcp-mib/tcpConn_openbsd", "Failed to read tcp_symbol\n")); |
| 234 |
return -1; |
| 235 |
} |
| 236 |
|
| 237 |
next = (struct inpcb *)&TAILQ_FIRST(&table.inpt_queue); |
| 238 |
prev = NULL; |
| 239 |
|
| 240 |
while (next) { |
| 241 |
if (!NETSNMP_KLOOKUP(next, (char *)&inpcb, sizeof(inpcb))) { |
| 242 |
DEBUGMSGTL(("tcp-mib/data_access/tcpConn", "klookup inpcb failed\n")); |
| 243 |
break; |
| 244 |
} |
| 245 |
if (prev != NULL) { |
| 246 |
if (!NETSNMP_KLOOKUP(prev, (char *)&previnpcb, |
| 247 |
sizeof(previnpcb))) { |
| 248 |
DEBUGMSGTL(("tcp-mib/data_access/tcpConn", |
| 249 |
"klookup previnpcb failed\n")); |
| 250 |
break; |
| 251 |
} |
| 252 |
if (TAILQ_NEXT(&previnpcb, inp_queue) != next) { |
| 253 |
snmp_log(LOG_ERR,"tcbtable link error\n"); |
| 254 |
break; |
| 255 |
} |
| 256 |
} |
| 257 |
prev = next; |
| 258 |
next = TAILQ_NEXT(&inpcb, inp_queue); |
| 259 |
if (!NETSNMP_KLOOKUP(inpcb.inp_ppcb, (char *)&tcpcb, sizeof(tcpcb))) { |
| 260 |
DEBUGMSGTL(("tcp-mib/data_access/tcpConn", "klookup tcpcb failed\n")); |
| 261 |
break; |
| 262 |
} |
| 263 |
state = StateMap[tcpcb.t_state]; |
| 264 |
|
| 265 |
if (load_flags) { |
| 266 |
if (state == TCPCONNECTIONSTATE_LISTEN) { |
| 267 |
if (load_flags & NETSNMP_ACCESS_TCPCONN_LOAD_NOLISTEN) { |
| 268 |
DEBUGMSGT(("verbose:access:tcpconn:container", |
| 269 |
" skipping listen\n")); |
| 270 |
continue; |
| 271 |
} |
| 272 |
} |
| 273 |
else if (load_flags & NETSNMP_ACCESS_TCPCONN_LOAD_ONLYLISTEN) { |
| 274 |
DEBUGMSGT(("verbose:access:tcpconn:container", |
| 275 |
" skipping non-listen\n")); |
| 276 |
continue; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
#if !defined(NETSNMP_ENABLE_IPV6) |
| 281 |
if (inpcb.inp_flags & INP_IPV6) |
| 282 |
continue; |
| 283 |
#endif |
| 284 |
|
| 285 |
entry = netsnmp_access_tcpconn_entry_create(); |
| 286 |
if(NULL == entry) { |
| 287 |
rc = -3; |
| 288 |
break; |
| 289 |
} |
| 290 |
|
| 291 |
/** oddly enough, these appear to already be in network order */ |
| 292 |
entry->loc_port = ntohs(inpcb.inp_lport); |
| 293 |
entry->rmt_port = ntohs(inpcb.inp_fport); |
| 294 |
entry->tcpConnState = state; |
| 295 |
entry->pid = 0; |
| 296 |
|
| 297 |
/** the addr string may need work */ |
| 298 |
if (inpcb.inp_flags & INP_IPV6) { |
| 299 |
entry->loc_addr_len = entry->rmt_addr_len = 16; |
| 300 |
memcpy(entry->loc_addr, &inpcb.inp_laddr6, 16); |
| 301 |
memcpy(entry->rmt_addr, &inpcb.inp_faddr6, 16); |
| 302 |
} |
| 303 |
else { |
| 304 |
entry->loc_addr_len = entry->rmt_addr_len = 4; |
| 305 |
memcpy(entry->loc_addr, &inpcb.inp_laddr, 4); |
| 306 |
memcpy(entry->rmt_addr, &inpcb.inp_faddr, 4); |
| 307 |
} |
| 308 |
DEBUGMSGTL(("tcp-mib/data_access", "tcp %d %d %d\n", |
| 309 |
entry->loc_addr_len, entry->loc_port, entry->rmt_port)); |
| 310 |
|
| 311 |
/* |
| 312 |
* add entry to container |
| 313 |
*/ |
| 314 |
entry->arbitrary_index = CONTAINER_SIZE(container) + 1; |
| 315 |
CONTAINER_INSERT(container, entry); |
| 316 |
} |
| 317 |
|
| 318 |
if(rc<0) |
| 319 |
return rc; |
| 320 |
|
| 321 |
return 0; |
| 322 |
} |
| 323 |
#endif /* HAVE_KVM_GETFILES */ |