| 1 |
#include <net-snmp/net-snmp-config.h> |
| 2 |
#include <net-snmp/net-snmp-includes.h> |
| 3 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 4 |
|
| 5 |
#include "sctpAssocTable.h" |
| 6 |
#include "sctpAssocLocalAddrTable.h" |
| 7 |
#include "sctpAssocRemAddrTable.h" |
| 8 |
#include "sctpTables_common.h" |
| 9 |
|
| 10 |
#include <util_funcs.h> |
| 11 |
|
| 12 |
#include <stdio.h> |
| 13 |
#include <errno.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <unistd.h> |
| 16 |
#include <sys/types.h> |
| 17 |
#include <sys/sysctl.h> |
| 18 |
#include <stdint.h> |
| 19 |
#include <sys/socket.h> |
| 20 |
#include <netinet/in.h> |
| 21 |
#include <netinet/sctp.h> |
| 22 |
#include <netinet/sctp_constants.h> |
| 23 |
|
| 24 |
|
| 25 |
static int |
| 26 |
parse_assoc_local_addresses(sctpTables_containers * containers, |
| 27 |
struct xsctp_laddr *xladdr) |
| 28 |
{ |
| 29 |
int ret; |
| 30 |
sctpAssocLocalAddrTable_entry *entry; |
| 31 |
entry = sctpAssocLocalAddrTable_entry_create(); |
| 32 |
if (entry == NULL) |
| 33 |
return SNMP_ERR_GENERR; |
| 34 |
|
| 35 |
entry->sctpAssocId = 0; |
| 36 |
if (xladdr->address.sa.sa_family == AF_INET) { |
| 37 |
entry->sctpAssocLocalAddrType = INETADDRESSTYPE_IPV4; |
| 38 |
entry->sctpAssocLocalAddr_len = 4; |
| 39 |
memcpy(entry->sctpAssocLocalAddr, |
| 40 |
&xladdr->address.sin.sin_addr, |
| 41 |
entry->sctpAssocLocalAddr_len); |
| 42 |
} else if (xladdr->address.sa.sa_family == AF_INET6) { |
| 43 |
entry->sctpAssocLocalAddrType = INETADDRESSTYPE_IPV6; |
| 44 |
entry->sctpAssocLocalAddr_len = 16; |
| 45 |
memcpy(entry->sctpAssocLocalAddr, |
| 46 |
&xladdr->address.sin6.sin6_addr, |
| 47 |
entry->sctpAssocLocalAddr_len); |
| 48 |
} |
| 49 |
entry->sctpAssocLocalAddrStartTime = xladdr->start_time.tv_sec; |
| 50 |
ret = sctpAssocLocalAddrTable_add_or_update(containers->sctpAssocLocalAddrTable, |
| 51 |
entry); |
| 52 |
if (ret != SNMP_ERR_NOERROR) |
| 53 |
return SNMP_ERR_GENERR; |
| 54 |
|
| 55 |
return SNMP_ERR_NOERROR; |
| 56 |
} |
| 57 |
|
| 58 |
static int |
| 59 |
parse_assoc_xstcb(sctpTables_containers * containers, struct xsctp_tcb *xstcb) |
| 60 |
{ |
| 61 |
int ret; |
| 62 |
sctpAssocTable_entry *entry; |
| 63 |
|
| 64 |
entry = sctpAssocTable_entry_create(); |
| 65 |
if (entry == NULL) |
| 66 |
return SNMP_ERR_GENERR; |
| 67 |
|
| 68 |
switch (xstcb->state) { |
| 69 |
case SCTP_STATE_INUSE: |
| 70 |
entry->sctpAssocState = SCTPASSOCSTATE_DELETETCB; |
| 71 |
break; |
| 72 |
case SCTP_STATE_COOKIE_WAIT: |
| 73 |
entry->sctpAssocState = SCTPASSOCSTATE_COOKIEWAIT; |
| 74 |
break; |
| 75 |
case SCTP_STATE_COOKIE_ECHOED: |
| 76 |
entry->sctpAssocState = SCTPASSOCSTATE_COOKIEECHOED; |
| 77 |
break; |
| 78 |
case SCTP_STATE_OPEN: |
| 79 |
entry->sctpAssocState = SCTPASSOCSTATE_ESTABLISHED; |
| 80 |
break; |
| 81 |
case SCTP_STATE_SHUTDOWN_SENT: |
| 82 |
entry->sctpAssocState = SCTPASSOCSTATE_SHUTDOWNSENT; |
| 83 |
break; |
| 84 |
case SCTP_STATE_SHUTDOWN_RECEIVED: |
| 85 |
entry->sctpAssocState = SCTPASSOCSTATE_SHUTDOWNRECEIVED; |
| 86 |
break; |
| 87 |
case SCTP_STATE_SHUTDOWN_ACK_SENT: |
| 88 |
entry->sctpAssocState = SCTPASSOCSTATE_SHUTDOWNACKSENT; |
| 89 |
break; |
| 90 |
default: |
| 91 |
case SCTP_STATE_EMPTY: |
| 92 |
entry->sctpAssocState = SCTPASSOCSTATE_CLOSED; |
| 93 |
break; |
| 94 |
}; |
| 95 |
entry->sctpAssocHeartBeatInterval = xstcb->heartbeat_interval; |
| 96 |
entry->sctpAssocId = 0; |
| 97 |
entry->sctpAssocPrimProcess = xstcb->primary_process; |
| 98 |
entry->sctpAssocLocalPort = xstcb->local_port; |
| 99 |
entry->sctpAssocRemPort = xstcb->remote_port; |
| 100 |
entry->sctpAssocHeartBeatInterval = xstcb->heartbeat_interval; |
| 101 |
entry->sctpAssocInStreams = xstcb->in_streams; |
| 102 |
entry->sctpAssocOutStreams = xstcb->out_streams; |
| 103 |
entry->sctpAssocMaxRetr = xstcb->max_nr_retrans; |
| 104 |
entry->sctpAssocT1expireds = xstcb->T1_expireries; |
| 105 |
entry->sctpAssocRtxChunks = xstcb->retransmitted_tsns; |
| 106 |
entry->sctpAssocT2expireds = xstcb->T2_expireries; |
| 107 |
entry->sctpAssocRemHostName[0] = 0; |
| 108 |
entry->sctpAssocRemHostName_len = 0; |
| 109 |
entry->sctpAssocDiscontinuityTime = xstcb->discontinuity_time.tv_sec; |
| 110 |
entry->sctpAssocStartTime = xstcb->start_time.tv_sec; |
| 111 |
|
| 112 |
ret = sctpAssocTable_add_or_update(containers->sctpAssocTable, entry); |
| 113 |
if (ret != SNMP_ERR_NOERROR) { |
| 114 |
return ret; |
| 115 |
} |
| 116 |
|
| 117 |
return SNMP_ERR_NOERROR; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
static int |
| 122 |
parse_remaddr_xraddr(sctpTables_containers * containers, |
| 123 |
struct xsctp_raddr *xraddr) |
| 124 |
{ |
| 125 |
int ret; |
| 126 |
sctpAssocRemAddrTable_entry *entry; |
| 127 |
|
| 128 |
entry = sctpAssocRemAddrTable_entry_create(); |
| 129 |
if (entry == NULL) |
| 130 |
return SNMP_ERR_GENERR; |
| 131 |
|
| 132 |
|
| 133 |
entry->sctpAssocId = 0; |
| 134 |
|
| 135 |
if(xraddr->active) |
| 136 |
entry->sctpAssocRemAddrActive = TRUTHVALUE_TRUE; |
| 137 |
else |
| 138 |
entry->sctpAssocRemAddrActive = TRUTHVALUE_FALSE; |
| 139 |
|
| 140 |
if (xraddr->heartbeat_enabled) |
| 141 |
entry->sctpAssocRemAddrHBActive = TRUTHVALUE_TRUE; |
| 142 |
else |
| 143 |
entry->sctpAssocRemAddrHBActive = TRUTHVALUE_FALSE; |
| 144 |
|
| 145 |
entry->sctpAssocRemAddrRTO = xraddr->rto; |
| 146 |
entry->sctpAssocRemAddrMaxPathRtx = xraddr->max_path_rtx; |
| 147 |
entry->sctpAssocRemAddrRtx = xraddr->rtx; |
| 148 |
entry->sctpAssocRemAddrStartTime = xraddr->start_time.tv_sec; |
| 149 |
|
| 150 |
if (xraddr->address.sa.sa_family == AF_INET) { |
| 151 |
entry->sctpAssocRemAddrType = INETADDRESSTYPE_IPV4; |
| 152 |
entry->sctpAssocRemAddr_len = 4; |
| 153 |
memcpy(entry->sctpAssocRemAddr, |
| 154 |
&xraddr->address.sin.sin_addr, |
| 155 |
entry->sctpAssocRemAddr_len); |
| 156 |
} else if (xraddr->address.sa.sa_family == AF_INET6) { |
| 157 |
entry->sctpAssocRemAddrType = INETADDRESSTYPE_IPV6; |
| 158 |
entry->sctpAssocRemAddr_len = 16; |
| 159 |
memcpy(entry->sctpAssocRemAddr, |
| 160 |
&xraddr->address.sin6.sin6_addr, |
| 161 |
entry->sctpAssocRemAddr_len); |
| 162 |
} |
| 163 |
ret = |
| 164 |
sctpAssocRemAddrTable_add_or_update(containers-> |
| 165 |
sctpAssocRemAddrTable, entry); |
| 166 |
if (ret != SNMP_ERR_NOERROR) { |
| 167 |
return ret; |
| 168 |
} |
| 169 |
return SNMP_ERR_NOERROR; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
int |
| 174 |
sctpTables_arch_load(sctpTables_containers * containers, u_long * flags) |
| 175 |
{ |
| 176 |
int ret = SNMP_ERR_NOERROR; |
| 177 |
size_t len; |
| 178 |
caddr_t buf; |
| 179 |
unsigned int offset; |
| 180 |
struct xsctp_inpcb *xinp; |
| 181 |
struct xsctp_tcb *xstcb; |
| 182 |
struct xsctp_laddr *xladdr; |
| 183 |
struct xsctp_raddr *xraddr; |
| 184 |
|
| 185 |
|
| 186 |
*flags |= SCTP_TABLES_LOAD_FLAG_DELETE_INVALID; |
| 187 |
*flags |= SCTP_TABLES_LOAD_FLAG_AUTO_LOOKUP; |
| 188 |
len = 0; |
| 189 |
if (sysctlbyname("net.inet.sctp.assoclist", 0, &len, 0, 0) < 0) { |
| 190 |
printf("Error %d (%s) could not get the assoclist\n", errno, strerror(errno)); |
| 191 |
return(-1); |
| 192 |
} |
| 193 |
if ((buf = (caddr_t)malloc(len)) == 0) { |
| 194 |
printf("malloc %lu bytes failed.\n", (long unsigned)len); |
| 195 |
return(-1); |
| 196 |
} |
| 197 |
if (sysctlbyname("net.inet.sctp.assoclist", buf, &len, 0, 0) < 0) { |
| 198 |
printf("Error %d (%s) could not get the assoclist\n", errno, strerror(errno)); |
| 199 |
free(buf); |
| 200 |
return(-1); |
| 201 |
} |
| 202 |
offset = 0; |
| 203 |
xinp = (struct xsctp_inpcb *)(buf + offset); |
| 204 |
while (xinp->last == 0) { |
| 205 |
/* for each INP */ |
| 206 |
offset += sizeof(struct xsctp_inpcb); |
| 207 |
/* Local addresses */ |
| 208 |
xladdr = (struct xsctp_laddr *)(buf + offset); |
| 209 |
while (xladdr->last == 0) { |
| 210 |
offset += sizeof(struct xsctp_laddr); |
| 211 |
xladdr = (struct xsctp_laddr *)(buf + offset); |
| 212 |
} |
| 213 |
offset += sizeof(struct xsctp_laddr); |
| 214 |
/* Associations */ |
| 215 |
xstcb = (struct xsctp_tcb *)(buf + offset); |
| 216 |
while (xstcb->last == 0) { |
| 217 |
xstcb = (struct xsctp_tcb *)(buf + offset); |
| 218 |
offset += sizeof(struct xsctp_tcb); |
| 219 |
parse_assoc_xstcb(containers, xstcb); |
| 220 |
/* Local addresses */ |
| 221 |
xladdr = (struct xsctp_laddr *)(buf + offset); |
| 222 |
while (xladdr->last == 0) { |
| 223 |
parse_assoc_local_addresses(containers, xladdr); |
| 224 |
offset += sizeof(struct xsctp_laddr); |
| 225 |
xladdr = (struct xsctp_laddr *)(buf + offset); |
| 226 |
} |
| 227 |
offset += sizeof(struct xsctp_laddr); |
| 228 |
|
| 229 |
/* Remote addresses */ |
| 230 |
xraddr = (struct xsctp_raddr *)(buf + offset); |
| 231 |
while (xraddr->last == 0) { |
| 232 |
parse_remaddr_xraddr(containers, xraddr); |
| 233 |
offset += sizeof(struct xsctp_raddr); |
| 234 |
xraddr = (struct xsctp_raddr *)(buf + offset); |
| 235 |
} |
| 236 |
offset += sizeof(struct xsctp_raddr); |
| 237 |
xstcb = (struct xsctp_tcb *)(buf + offset); |
| 238 |
} |
| 239 |
offset += sizeof(struct xsctp_tcb); |
| 240 |
xinp = (struct xsctp_inpcb *)(buf + offset); |
| 241 |
} |
| 242 |
free(buf); |
| 243 |
return ret; |
| 244 |
} |