| 1 |
/* |
| 2 |
* standard Net-SNMP includes |
| 3 |
*/ |
| 4 |
#include <net-snmp/net-snmp-config.h> |
| 5 |
#include <net-snmp/net-snmp-includes.h> |
| 6 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 7 |
|
| 8 |
#ifdef HAVE_UNISTD_H |
| 9 |
#include <unistd.h> |
| 10 |
#endif |
| 11 |
|
| 12 |
/* |
| 13 |
* include our parent header |
| 14 |
*/ |
| 15 |
#include "rmon-mib/etherStatsTable/etherStatsTable.h" |
| 16 |
#include "rmon-mib/etherStatsTable/etherStatsTable_data_access.h" |
| 17 |
#include "rmon-mib/etherStatsTable/ioctl_imp_common.h" |
| 18 |
|
| 19 |
/* |
| 20 |
* @retval 0 success |
| 21 |
* @retval -1 getifaddrs failed |
| 22 |
* @retval -2 memory allocation failed |
| 23 |
*/ |
| 24 |
|
| 25 |
struct ifname * |
| 26 |
etherstats_interface_name_list_get (struct ifname *list_head, int *retval) |
| 27 |
{ |
| 28 |
struct ifaddrs *addrs = NULL, *p = NULL; |
| 29 |
struct ifname *nameptr1=NULL, *nameptr2 = NULL; |
| 30 |
|
| 31 |
DEBUGMSGTL(("access:etherStatsTable:interface_name_list_get", |
| 32 |
"called\n")); |
| 33 |
|
| 34 |
if ((getifaddrs(&addrs)) < 0) { |
| 35 |
DEBUGMSGTL(("access:etherStatsTable:interface_name_list_get", |
| 36 |
"getifaddrs failed\n")); |
| 37 |
snmp_log (LOG_ERR, "access:etherStatsTable,interface_name_list_get, getifaddrs failed\n"); |
| 38 |
*retval = -1; |
| 39 |
return NULL; |
| 40 |
} |
| 41 |
|
| 42 |
for (p = addrs; p; p = p->ifa_next) { |
| 43 |
|
| 44 |
if (!list_head) { |
| 45 |
list_head = malloc(sizeof(struct ifname)); |
| 46 |
if (!list_head) { |
| 47 |
DEBUGMSGTL(("access:etherStatsTable:interface_name_list_get", |
| 48 |
"memory allocation failed\n")); |
| 49 |
snmp_log (LOG_ERR, "access:etherStatsTable,interface_name_list_get, memory allocation failed\n"); |
| 50 |
freeifaddrs(addrs); |
| 51 |
*retval = -2; |
| 52 |
return NULL; |
| 53 |
} |
| 54 |
memset(list_head, 0, sizeof(struct ifname)); |
| 55 |
strlcpy(list_head->name, p->ifa_name, IF_NAMESIZE); |
| 56 |
continue; |
| 57 |
} |
| 58 |
for (nameptr1 = list_head; nameptr1; nameptr2 = nameptr1, nameptr1 = nameptr1->ifn_next) |
| 59 |
if (!strncmp(p->ifa_name, nameptr1->name, IF_NAMESIZE)) |
| 60 |
break; |
| 61 |
|
| 62 |
if (nameptr1) |
| 63 |
continue; |
| 64 |
|
| 65 |
nameptr2->ifn_next = malloc(sizeof(struct ifname)); |
| 66 |
if (!nameptr2->ifn_next) { |
| 67 |
DEBUGMSGTL(("access:etherStatsTable:interface_name_list_get", |
| 68 |
"memory allocation failed\n")); |
| 69 |
snmp_log (LOG_ERR, "access:etherStatsTable,interface_name_list_get, memory allocation failed\n"); |
| 70 |
etherstats_interface_name_list_free (list_head); |
| 71 |
freeifaddrs(addrs); |
| 72 |
*retval = -2; |
| 73 |
return NULL; |
| 74 |
} |
| 75 |
nameptr2 = nameptr2->ifn_next; |
| 76 |
memset(nameptr2, 0, sizeof(struct ifname)); |
| 77 |
strlcpy(nameptr2->name, p->ifa_name, IF_NAMESIZE); |
| 78 |
continue; |
| 79 |
} |
| 80 |
|
| 81 |
freeifaddrs(addrs); |
| 82 |
*retval = 0; |
| 83 |
return list_head; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
/* |
| 88 |
* @retval 0 success |
| 89 |
* @retval -1 invalid pointer |
| 90 |
*/ |
| 91 |
|
| 92 |
int |
| 93 |
etherstats_interface_name_list_free (struct ifname *list_head) |
| 94 |
{ |
| 95 |
struct ifname *nameptr1 = NULL, *nameptr2 = NULL; |
| 96 |
|
| 97 |
DEBUGMSGTL(("access:etherStatsTable:interface_name_list_free", |
| 98 |
"called\n")); |
| 99 |
|
| 100 |
if (!list_head) { |
| 101 |
snmp_log (LOG_ERR, "access:etherStatsTable:interface_name_list_free: invalid pointer list_head"); |
| 102 |
DEBUGMSGTL(("access:etherStatsTable:interface_name_list_free", |
| 103 |
"invalid pointer list_head\n")); |
| 104 |
return -1; |
| 105 |
} |
| 106 |
|
| 107 |
for (nameptr1 = list_head; nameptr1; nameptr1 = nameptr2) { |
| 108 |
nameptr2 = nameptr1->ifn_next; |
| 109 |
free (nameptr1); |
| 110 |
} |
| 111 |
|
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
/* |
| 116 |
* @retval 0 : not found |
| 117 |
* @retval !0 : ifIndex |
| 118 |
*/ |
| 119 |
|
| 120 |
int |
| 121 |
etherstats_interface_ioctl_ifindex_get (int fd, const char *name) { |
| 122 |
#ifndef SIOCGIFINDEX |
| 123 |
return 0; |
| 124 |
#else |
| 125 |
struct ifreq ifrq; |
| 126 |
int rc = 0; |
| 127 |
|
| 128 |
DEBUGMSGTL(("access:etherStatsTable:ioctl", "ifindex_get\n")); |
| 129 |
|
| 130 |
rc = _etherStats_ioctl_get(fd, SIOCGIFINDEX, &ifrq, name); |
| 131 |
if (rc < 0) { |
| 132 |
DEBUGMSGTL(("access:etherStats:ioctl", |
| 133 |
"ifindex_get error on interface '%s'\n", name)); |
| 134 |
snmp_log (LOG_ERR, "access:etherStatsTable:ioctl, ifindex_get error on interface '%s'\n", name); |
| 135 |
return 0; |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
return ifrq.ifr_ifindex; |
| 140 |
#endif /* SIOCGIFINDEX */ |
| 141 |
} |
| 142 |
|
| 143 |
/* |
| 144 |
* @retval 0 success |
| 145 |
* @retval -1 cannot get ETHTOOL_DRVINFO failed |
| 146 |
* @retval -2 n_stats zero - no statistics available |
| 147 |
* @retval -3 memory allocation for holding the statistics failed |
| 148 |
* @retval -4 cannot get ETHTOOL_GSTRINGS information |
| 149 |
* @retval -5 cannot get ETHTOOL_GSTATS information |
| 150 |
* @retval -6 function not supported if HAVE_LINUX_ETHTOOL_H not defined |
| 151 |
*/ |
| 152 |
|
| 153 |
int |
| 154 |
interface_ioctl_etherstats_get (etherStatsTable_rowreq_ctx *rowreq_ctx , int fd, const char *name) { |
| 155 |
|
| 156 |
#ifdef HAVE_LINUX_ETHTOOL_H |
| 157 |
|
| 158 |
etherStatsTable_data *data = &rowreq_ctx->data; |
| 159 |
struct ethtool_drvinfo driver_info; |
| 160 |
struct ethtool_gstrings *eth_strings; |
| 161 |
struct ethtool_stats *eth_stats; |
| 162 |
struct ifreq ifr; |
| 163 |
unsigned int nstats, size_str, i; |
| 164 |
int err; |
| 165 |
|
| 166 |
DEBUGMSGTL(("access:etherStatsTable:interface_ioctl_etherstats_get", |
| 167 |
"called\n")); |
| 168 |
|
| 169 |
memset(&ifr, 0, sizeof(ifr)); |
| 170 |
strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); |
| 171 |
|
| 172 |
memset(&driver_info, 0, sizeof(driver_info)); |
| 173 |
driver_info.cmd = ETHTOOL_GDRVINFO; |
| 174 |
ifr.ifr_data = (char *)&driver_info; |
| 175 |
|
| 176 |
err = _etherStats_ioctl_get(fd, SIOCETHTOOL, &ifr, name); |
| 177 |
if (err < 0) { |
| 178 |
DEBUGMSGTL(("access:etherStatsTable:interface_ioctl_etherstats_get", |
| 179 |
"ETHTOOL_GETDRVINFO failed on interface |%s| \n", name)); |
| 180 |
return -1; |
| 181 |
} |
| 182 |
|
| 183 |
nstats = driver_info.n_stats; |
| 184 |
if (nstats < 1) { |
| 185 |
DEBUGMSGTL(("access:etherStatsTable:interface_ioctl_etherstats_get", |
| 186 |
"no stats available for interface |%s| \n", name)); |
| 187 |
return -2; |
| 188 |
} |
| 189 |
|
| 190 |
size_str = nstats * ETH_GSTRING_LEN; |
| 191 |
|
| 192 |
eth_strings = malloc(size_str + sizeof (struct ethtool_gstrings)); |
| 193 |
if (!eth_strings) { |
| 194 |
DEBUGMSGTL(("access:etherStatsTable:interface_ioctl_etherstats_get", |
| 195 |
"no memory available\n")); |
| 196 |
snmp_log (LOG_ERR, "access:etherStatsTable,interface_ioctl_etherstats_get, no memory available\n"); |
| 197 |
|
| 198 |
return -3; |
| 199 |
} |
| 200 |
memset (eth_strings, 0, (size_str + sizeof (struct ethtool_gstrings))); |
| 201 |
|
| 202 |
eth_stats = malloc (size_str + sizeof (struct ethtool_stats)); |
| 203 |
if (!eth_stats) { |
| 204 |
free (eth_strings); |
| 205 |
DEBUGMSGTL(("access:etherStatsTable:interface_ioctl_etherstats_get", |
| 206 |
"no memory available\n")); |
| 207 |
snmp_log (LOG_ERR, "access:etherStatsTable,interface_ioctl_etherstats_get, no memory available\n"); |
| 208 |
|
| 209 |
return -3; |
| 210 |
} |
| 211 |
memset (eth_stats, 0, (size_str + sizeof (struct ethtool_stats))); |
| 212 |
|
| 213 |
eth_strings->cmd = ETHTOOL_GSTRINGS; |
| 214 |
eth_strings->string_set = ETH_SS_STATS; |
| 215 |
eth_strings->len = nstats; |
| 216 |
ifr.ifr_data = (char *) eth_strings; |
| 217 |
|
| 218 |
err = _etherStats_ioctl_get(fd, SIOCETHTOOL, &ifr, name); |
| 219 |
if (err < 0) { |
| 220 |
DEBUGMSGTL(("access:etherStatsTable:interface_ioctl_etherstats_get", |
| 221 |
"cannot get stats strings information for interface |%s| \n", name)); |
| 222 |
snmp_log (LOG_ERR, "access:etherStatsTable,interface_ioctl_etherstats_get, cannot get stats strings information for interface |%s| \n", name); |
| 223 |
|
| 224 |
free(eth_strings); |
| 225 |
free(eth_stats); |
| 226 |
return -4; |
| 227 |
} |
| 228 |
|
| 229 |
eth_stats->cmd = ETHTOOL_GSTATS; |
| 230 |
eth_stats->n_stats = nstats; |
| 231 |
ifr.ifr_data = (char *) eth_stats; |
| 232 |
err = _etherStats_ioctl_get(fd, SIOCETHTOOL, &ifr, name); |
| 233 |
if (err < 0) { |
| 234 |
DEBUGMSGTL(("access:etherStatsTable:interface_ioctl_etherstats_get", |
| 235 |
"cannot get stats strings information for interface |%s| \n", name)); |
| 236 |
snmp_log (LOG_ERR, "access:etherStatsTable,interface_ioctl_etherstats_get, cannot get stats information for interface |%s| \n", name); |
| 237 |
|
| 238 |
free(eth_strings); |
| 239 |
free(eth_stats); |
| 240 |
return -5; |
| 241 |
} |
| 242 |
|
| 243 |
for (i = 0; i < nstats; i++) { |
| 244 |
char s[ETH_GSTRING_LEN]; |
| 245 |
|
| 246 |
strlcpy(s, (const char *) ð_strings->data[i * ETH_GSTRING_LEN], |
| 247 |
sizeof(s)); |
| 248 |
|
| 249 |
if (ETHERSTATSOCTETS(s)) { |
| 250 |
data->etherStatsOctets += (u_long)eth_stats->data[i]; |
| 251 |
rowreq_ctx->column_exists_flags |= COLUMN_ETHERSTATSOCTETS_FLAG; |
| 252 |
} |
| 253 |
if (ETHERSTATSPKTS(s)) { |
| 254 |
data->etherStatsPkts += (u_long)eth_stats->data[i]; |
| 255 |
rowreq_ctx->column_exists_flags |= COLUMN_ETHERSTATSPKTS_FLAG; |
| 256 |
} |
| 257 |
if (ETHERSTATSBROADCASTPKTS(s)) { |
| 258 |
data->etherStatsBroadcastPkts += (u_long)eth_stats->data[i]; |
| 259 |
rowreq_ctx->column_exists_flags |= COLUMN_ETHERSTATSBROADCASTPKTS_FLAG; |
| 260 |
} |
| 261 |
if (ETHERSTATSMULTICASTPKTS(s)) { |
| 262 |
data->etherStatsMulticastPkts = (u_long)eth_stats->data[i]; |
| 263 |
rowreq_ctx->column_exists_flags |= COLUMN_ETHERSTATSMULTICASTPKTS_FLAG; |
| 264 |
} |
| 265 |
if (ETHERSTATSCRCALIGNERRORS(s)) { |
| 266 |
data->etherStatsCRCAlignErrors += (u_long)eth_stats->data[i]; |
| 267 |
rowreq_ctx->column_exists_flags |= COLUMN_ETHERSTATSCRCALIGNERRORS_FLAG; |
| 268 |
} |
| 269 |
if (ETHERSTATSUNDERSIZEPKTS(s)) { |
| 270 |
data->etherStatsUndersizePkts += (u_long)eth_stats->data[i]; |
| 271 |
rowreq_ctx->column_exists_flags |= COLUMN_ETHERSTATSUNDERSIZEPKTS_FLAG; |
| 272 |
} |
| 273 |
if (ETHERSTATSOVERSIZEPKTS(s)) { |
| 274 |
data->etherStatsOversizePkts += (u_long)eth_stats->data[i]; |
| 275 |
rowreq_ctx->column_exists_flags |= COLUMN_ETHERSTATSOVERSIZEPKTS_FLAG; |
| 276 |
} |
| 277 |
if (ETHERSTATSFRAGMENTS(s)) { |
| 278 |
data->etherStatsFragments += (u_long)eth_stats->data[i]; |
| 279 |
rowreq_ctx->column_exists_flags |= COLUMN_ETHERSTATSFRAGMENTS_FLAG; |
| 280 |
} |
| 281 |
if (ETHERSTATSJABBERS(s)) { |
| 282 |
data->etherStatsJabbers += (u_long)eth_stats->data[i]; |
| 283 |
rowreq_ctx->column_exists_flags |= COLUMN_ETHERSTATSJABBERS_FLAG; |
| 284 |
} |
| 285 |
if (ETHERSTATSCOLLISIONS(s)) { |
| 286 |
data->etherStatsCollisions += (u_long)eth_stats->data[i]; |
| 287 |
rowreq_ctx->column_exists_flags |= COLUMN_ETHERSTATSCOLLISIONS_FLAG; |
| 288 |
} |
| 289 |
if (ETHERSTATSPKTS64OCTETS(s)) { |
| 290 |
data->etherStatsPkts64Octets += (u_long)eth_stats->data[i]; |
| 291 |
rowreq_ctx->column_exists_flags |= COLUMN_ETHERSTATSPKTS64OCTETS_FLAG; |
| 292 |
} |
| 293 |
if (ETHERSTATSPKTS65TO127OCTETS(s)) { |
| 294 |
data->etherStatsPkts65to127Octets += (u_long)eth_stats->data[i]; |
| 295 |
rowreq_ctx->column_exists_flags |= COLUMN_ETHERSTATSPKTS65TO127OCTETS_FLAG; |
| 296 |
} |
| 297 |
if (ETHERSTATSPKTS128TO255OCTETS(s)) { |
| 298 |
data->etherStatsPkts128to255Octets += (u_long)eth_stats->data[i]; |
| 299 |
rowreq_ctx->column_exists_flags |= COLUMN_ETHERSTATSPKTS128TO255OCTETS_FLAG; |
| 300 |
} |
| 301 |
if (ETHERSTATSPKTS256TO511OCTETS(s)) { |
| 302 |
data->etherStatsPkts256to511Octets += (u_long)eth_stats->data[i]; |
| 303 |
rowreq_ctx->column_exists_flags |= COLUMN_ETHERSTATSPKTS256TO511OCTETS_FLAG; |
| 304 |
} |
| 305 |
if (ETHERSTATSPKTS512TO1023OCTETS(s)) { |
| 306 |
data->etherStatsPkts512to1023Octets += (u_long)eth_stats->data[i]; |
| 307 |
rowreq_ctx->column_exists_flags |= COLUMN_ETHERSTATSPKTS512TO1023OCTETS_FLAG; |
| 308 |
} |
| 309 |
if (ETHERSTATSPKTS1024TO1518OCTETS(s)) { |
| 310 |
data->etherStatsPkts1024to1518Octets += (u_long)eth_stats->data[i]; |
| 311 |
rowreq_ctx->column_exists_flags |= COLUMN_ETHERSTATSPKTS1024TO1518OCTETS_FLAG; |
| 312 |
} |
| 313 |
} |
| 314 |
free(eth_strings); |
| 315 |
free(eth_stats); |
| 316 |
|
| 317 |
return 0; |
| 318 |
#else |
| 319 |
return -6; |
| 320 |
#endif |
| 321 |
|
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
/* ioctl wrapper |
| 326 |
* |
| 327 |
* @param fd : socket fd to use w/ioctl, or -1 to open/close one |
| 328 |
* @param which |
| 329 |
* @param ifrq |
| 330 |
* param ifentry : ifentry to update |
| 331 |
* @param name |
| 332 |
* |
| 333 |
* @retval 0 : success |
| 334 |
* @retval -1 : invalid parameters |
| 335 |
* @retval -2 : couldn't create socket |
| 336 |
* @retval -3 : ioctl call failed |
| 337 |
*/ |
| 338 |
int |
| 339 |
_etherStats_ioctl_get(int fd, int which, struct ifreq *ifrq, const char* name) |
| 340 |
{ |
| 341 |
int ourfd = -1, rc = 0; |
| 342 |
|
| 343 |
DEBUGMSGTL(("access:etherStatsTable:ioctl", "_etherStats_ioctl_get\n")); |
| 344 |
/* |
| 345 |
* sanity checks |
| 346 |
*/ |
| 347 |
if(NULL == name) { |
| 348 |
DEBUGMSGTL(("access:etherStatsTable:ioctl", |
| 349 |
"_etherStats_ioctl_get interface name is NULL")); |
| 350 |
snmp_log (LOG_ERR, "access:etherStatsTable:ioctl, _etherStats_ioctl_get interface name is NULL"); |
| 351 |
return -1; |
| 352 |
} |
| 353 |
|
| 354 |
/* |
| 355 |
* create socket for ioctls |
| 356 |
*/ |
| 357 |
if(fd < 0) { |
| 358 |
fd = ourfd = socket(AF_INET, SOCK_DGRAM, 0); |
| 359 |
if(ourfd < 0) { |
| 360 |
DEBUGMSGTL(("access:etherStatsTable:ioctl", |
| 361 |
"_etherStats_ioctl_get couldn't create a socket\n")); |
| 362 |
snmp_log (LOG_ERR, "access:etherStatsTable:ioctl, _etherStats_ioctl_get error on interface '%s'\n", name); |
| 363 |
|
| 364 |
return -2; |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
strlcpy(ifrq->ifr_name, name, sizeof(ifrq->ifr_name)); |
| 369 |
rc = ioctl(fd, which, ifrq); |
| 370 |
if (rc < 0) { |
| 371 |
DEBUGMSGTL(("access:etherStatsTable:ioctl", |
| 372 |
"_etherStats_ioctl_get ioctl %d returned %d\n", which, rc)); |
| 373 |
rc = -3; |
| 374 |
} |
| 375 |
|
| 376 |
if(ourfd >= 0) |
| 377 |
close(ourfd); |
| 378 |
|
| 379 |
return rc; |
| 380 |
} |
| 381 |
|