| 1 |
/* |
| 2 |
* udp_endpointTable 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/ipaddress.h> |
| 9 |
#include <net-snmp/data_access/udp_endpoint.h> |
| 10 |
#include <net-snmp/agent/auto_nlist.h> |
| 11 |
|
| 12 |
#include "udp-mib/udpEndpointTable/udpEndpointTable_constants.h" |
| 13 |
#include "udp-mib/data_access/udp_endpoint_private.h" |
| 14 |
|
| 15 |
#include "mibII/mibII_common.h" |
| 16 |
|
| 17 |
#ifdef HAVE_NETINET_UDP_H |
| 18 |
#include <netinet/udp.h> |
| 19 |
#endif |
| 20 |
#ifdef HAVE_NETINET_UDP_VAR_H |
| 21 |
#include <netinet/udp_var.h> |
| 22 |
#endif |
| 23 |
|
| 24 |
#ifdef HAVE_KVM_GETFILES |
| 25 |
#if defined(HAVE_KVM_GETFILE2) || !defined(openbsd5) |
| 26 |
#undef HAVE_KVM_GETFILES |
| 27 |
#endif |
| 28 |
#endif |
| 29 |
|
| 30 |
#ifdef HAVE_KVM_GETFILES |
| 31 |
#include <kvm.h> |
| 32 |
#include <sys/sysctl.h> |
| 33 |
#define _KERNEL /* for DTYPE_SOCKET */ |
| 34 |
#include <sys/file.h> |
| 35 |
#undef _KERNEL |
| 36 |
#endif |
| 37 |
|
| 38 |
#ifdef HAVE_KVM_GETFILES |
| 39 |
static int _kvmload(netsnmp_container *container, u_int flags); |
| 40 |
#else |
| 41 |
static int _load(netsnmp_container *container, u_int flags); |
| 42 |
#endif |
| 43 |
|
| 44 |
/* |
| 45 |
* initialize arch specific storage |
| 46 |
* |
| 47 |
* @retval 0: success |
| 48 |
* @retval <0: error |
| 49 |
*/ |
| 50 |
int |
| 51 |
netsnmp_arch_udp_endpoint_entry_init(netsnmp_udp_endpoint_entry *entry) |
| 52 |
{ |
| 53 |
/* |
| 54 |
* init |
| 55 |
*/ |
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
/* |
| 60 |
* cleanup arch specific storage |
| 61 |
*/ |
| 62 |
void |
| 63 |
netsnmp_arch_udp_endpoint_entry_cleanup(netsnmp_udp_endpoint_entry *entry) |
| 64 |
{ |
| 65 |
/* |
| 66 |
* cleanup |
| 67 |
*/ |
| 68 |
} |
| 69 |
|
| 70 |
/* |
| 71 |
* copy arch specific storage |
| 72 |
*/ |
| 73 |
int |
| 74 |
netsnmp_arch_udp_endpoint_entry_copy(netsnmp_udp_endpoint_entry *lhs, |
| 75 |
netsnmp_udp_endpoint_entry *rhs) |
| 76 |
{ |
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
/* |
| 81 |
* delete an entry |
| 82 |
*/ |
| 83 |
int |
| 84 |
netsnmp_arch_udp_endpoint_entry_delete(netsnmp_udp_endpoint_entry *entry) |
| 85 |
{ |
| 86 |
if (NULL == entry) |
| 87 |
return -1; |
| 88 |
/** xxx-rks:9 udp_endpoint delete not implemented */ |
| 89 |
return -1; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
/** |
| 94 |
* |
| 95 |
* @retval 0 no errors |
| 96 |
* @retval !0 errors |
| 97 |
*/ |
| 98 |
int |
| 99 |
netsnmp_arch_udp_endpoint_container_load(netsnmp_container *container, |
| 100 |
u_int load_flags ) |
| 101 |
{ |
| 102 |
int rc = 0; |
| 103 |
|
| 104 |
DEBUGMSGTL(("access:udp_endpoint:container", |
| 105 |
"udp_endpoint_container_arch_load (flags %x)\n", load_flags)); |
| 106 |
|
| 107 |
if (NULL == container) { |
| 108 |
snmp_log(LOG_ERR, "no container specified/found for access_udp_endpoint\n"); |
| 109 |
return -1; |
| 110 |
} |
| 111 |
|
| 112 |
#ifdef HAVE_KVM_GETFILES |
| 113 |
rc = _kvmload(container, load_flags); |
| 114 |
#else |
| 115 |
rc = _load(container, load_flags); |
| 116 |
#endif |
| 117 |
|
| 118 |
return rc; |
| 119 |
} |
| 120 |
|
| 121 |
#ifdef HAVE_KVM_GETFILES |
| 122 |
/** |
| 123 |
* |
| 124 |
* @retval 0 no errors |
| 125 |
* @retval !0 errors |
| 126 |
*/ |
| 127 |
static int |
| 128 |
_kvmload(netsnmp_container *container, u_int load_flags) |
| 129 |
{ |
| 130 |
netsnmp_udp_endpoint_entry *entry; |
| 131 |
struct kinfo_file *kf; |
| 132 |
int count; |
| 133 |
int rc = 0; |
| 134 |
|
| 135 |
kf = kvm_getfiles(kd, KERN_FILE_BYFILE, DTYPE_SOCKET, sizeof(struct kinfo_file), &count); |
| 136 |
|
| 137 |
while (count--) { |
| 138 |
if (kf->so_protocol != IPPROTO_UDP) |
| 139 |
goto skip; |
| 140 |
#if !defined(NETSNMP_ENABLE_IPV6) |
| 141 |
if (kf->so_family == AF_INET6) |
| 142 |
goto skip; |
| 143 |
#endif |
| 144 |
|
| 145 |
entry = netsnmp_access_udp_endpoint_entry_create(); |
| 146 |
if(NULL == entry) { |
| 147 |
rc = -3; |
| 148 |
break; |
| 149 |
} |
| 150 |
|
| 151 |
/** oddly enough, these appear to already be in network order */ |
| 152 |
entry->loc_port = ntohs(kf->inp_lport); |
| 153 |
entry->rmt_port = ntohs(kf->inp_fport); |
| 154 |
entry->pid = kf->p_pid; |
| 155 |
|
| 156 |
/** the addr string may need work */ |
| 157 |
if (kf->so_family == AF_INET6) { |
| 158 |
entry->loc_addr_len = entry->rmt_addr_len = 16; |
| 159 |
memcpy(entry->loc_addr, &kf->inp_laddru, 16); |
| 160 |
memcpy(entry->rmt_addr, &kf->inp_faddru, 16); |
| 161 |
} |
| 162 |
else { |
| 163 |
entry->loc_addr_len = entry->rmt_addr_len = 4; |
| 164 |
memcpy(entry->loc_addr, &kf->inp_laddru[0], 4); |
| 165 |
memcpy(entry->rmt_addr, &kf->inp_faddru[0], 4); |
| 166 |
} |
| 167 |
DEBUGMSGTL(("udp-mib/data_access", "udp %d %d %d\n", |
| 168 |
entry->loc_addr_len, entry->loc_port, entry->rmt_port)); |
| 169 |
|
| 170 |
/* |
| 171 |
* add entry to container |
| 172 |
*/ |
| 173 |
entry->index = CONTAINER_SIZE(container) + 1; |
| 174 |
CONTAINER_INSERT(container, entry); |
| 175 |
skip: |
| 176 |
kf++; |
| 177 |
} |
| 178 |
|
| 179 |
if (rc < 0) |
| 180 |
return rc; |
| 181 |
return 0; |
| 182 |
} |
| 183 |
|
| 184 |
#else /* HAVE_KVM_GETFILES */ |
| 185 |
|
| 186 |
/** |
| 187 |
* |
| 188 |
* @retval 0 no errors |
| 189 |
* @retval !0 errors |
| 190 |
*/ |
| 191 |
static int |
| 192 |
_load(netsnmp_container *container, u_int load_flags) |
| 193 |
{ |
| 194 |
struct inpcbtable table; |
| 195 |
struct inpcb *next, *prev; |
| 196 |
struct inpcb inpcb, previnpcb; |
| 197 |
netsnmp_udp_endpoint_entry *entry; |
| 198 |
int rc = 0; |
| 199 |
|
| 200 |
/* |
| 201 |
* Read in the buffer containing the TCP table data |
| 202 |
*/ |
| 203 |
if (!auto_nlist(UDB_SYMBOL, (char *) &table, sizeof(table))) { |
| 204 |
DEBUGMSGTL(("udp-mib/udp_endpoint_openbsd", "Failed to read udp_symbol\n")); |
| 205 |
return -1; |
| 206 |
} |
| 207 |
|
| 208 |
next = (struct inpcb *)&TAILQ_FIRST(&table.inpt_queue); |
| 209 |
prev = NULL; |
| 210 |
|
| 211 |
while (next) { |
| 212 |
NETSNMP_KLOOKUP(next, (char *)&inpcb, sizeof(inpcb)); |
| 213 |
if (prev != NULL) { |
| 214 |
if (!NETSNMP_KLOOKUP(prev, (char *)&previnpcb, |
| 215 |
sizeof(previnpcb))) { |
| 216 |
DEBUGMSGTL(("udp-mib/data_access/udpConn", |
| 217 |
"klookup previnpcb failed\n")); |
| 218 |
break; |
| 219 |
} |
| 220 |
if (TAILQ_NEXT(&previnpcb, inp_queue) != next) { |
| 221 |
snmp_log(LOG_ERR,"udptable link error\n"); |
| 222 |
break; |
| 223 |
} |
| 224 |
} |
| 225 |
prev = next; |
| 226 |
next = TAILQ_NEXT(&inpcb, inp_queue); |
| 227 |
|
| 228 |
#if !defined(NETSNMP_ENABLE_IPV6) |
| 229 |
if (inpcb.inp_flags & INP_IPV6) |
| 230 |
continue; |
| 231 |
#endif |
| 232 |
entry = netsnmp_access_udp_endpoint_entry_create(); |
| 233 |
if (NULL == entry) { |
| 234 |
rc = -3; |
| 235 |
break; |
| 236 |
} |
| 237 |
|
| 238 |
/** oddly enough, these appear to already be in network order */ |
| 239 |
entry->loc_port = ntohs(inpcb.inp_lport); |
| 240 |
entry->rmt_port = ntohs(inpcb.inp_fport); |
| 241 |
entry->pid = 0; |
| 242 |
|
| 243 |
/** the addr string may need work */ |
| 244 |
if (inpcb.inp_flags & INP_IPV6) { |
| 245 |
entry->loc_addr_len = entry->rmt_addr_len = 16; |
| 246 |
memcpy(entry->loc_addr, &inpcb.inp_laddr6, 16); |
| 247 |
memcpy(entry->rmt_addr, &inpcb.inp_faddr6, 16); |
| 248 |
} |
| 249 |
else { |
| 250 |
entry->loc_addr_len = entry->rmt_addr_len = 4; |
| 251 |
memcpy(entry->loc_addr, &inpcb.inp_laddr, 4); |
| 252 |
memcpy(entry->rmt_addr, &inpcb.inp_faddr, 4); |
| 253 |
} |
| 254 |
|
| 255 |
/* |
| 256 |
* add entry to container |
| 257 |
*/ |
| 258 |
entry->index = CONTAINER_SIZE(container) + 1; |
| 259 |
CONTAINER_INSERT(container, entry); |
| 260 |
} |
| 261 |
|
| 262 |
if (rc < 0) |
| 263 |
return rc; |
| 264 |
|
| 265 |
return 0; |
| 266 |
} |
| 267 |
#endif /* HAVE_KVM_GETFILES */ |