| 1 |
/**************************************************************************** |
| 2 |
* Module for ucd-snmpd reading IP Firewall accounting rules. * |
| 3 |
* It reads "/proc/net/ip_acct". If the file has a wrong format it silently * |
| 4 |
* returns erroneous data but doesn't do anything harmfull. Based (on the * |
| 5 |
* output of) mib2c, wombat.c, proc.c and the Linux kernel. * |
| 6 |
* Author: [email protected] * |
| 7 |
***************************************************************************/ |
| 8 |
|
| 9 |
#include <net-snmp/net-snmp-config.h> |
| 10 |
|
| 11 |
#ifdef TIME_WITH_SYS_TIME |
| 12 |
# include <sys/time.h> |
| 13 |
# include <time.h> |
| 14 |
#else |
| 15 |
# ifdef HAVE_SYS_TIME_H |
| 16 |
# include <sys/time.h> |
| 17 |
# else |
| 18 |
# include <time.h> |
| 19 |
# endif |
| 20 |
#endif |
| 21 |
|
| 22 |
#include <net-snmp/net-snmp-includes.h> |
| 23 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 24 |
|
| 25 |
#include "util_funcs/header_simple_table.h" |
| 26 |
#include "ipfwacc.h" |
| 27 |
|
| 28 |
/* |
| 29 |
* According to the 2.0.33 Linux kernel, assuming we use ipv4 any line from |
| 30 |
* * "/proc/net/ip_acct should fit into |
| 31 |
* * 8+1+8+2+8+1+8+1+16+1+8+1+4+1+2+1+2+1+20+20+10*(1+5)+2+2+2+2=182 |
| 32 |
* * characters+ newline. |
| 33 |
*/ |
| 34 |
#define IPFWRULELEN 200 |
| 35 |
|
| 36 |
#define IP_FW_F_ALL 0x0000 /* This is a universal packet firewall */ |
| 37 |
#define IP_FW_F_TCP 0x0001 /* This is a TCP packet firewall */ |
| 38 |
#define IP_FW_F_UDP 0x0002 /* This is a UDP packet firewall */ |
| 39 |
#define IP_FW_F_ICMP 0x0003 /* This is a ICMP packet firewall */ |
| 40 |
#define IP_FW_F_KIND 0x0003 /* Mask to isolate firewall kind */ |
| 41 |
#define IP_FW_F_SRNG 0x0008 /* The first two src ports are a min * |
| 42 |
* and max range (stored in host byte * |
| 43 |
* order). */ |
| 44 |
#define IP_FW_F_DRNG 0x0010 /* The first two dst ports are a min * |
| 45 |
* and max range (stored in host byte * |
| 46 |
* order). * |
| 47 |
* (ports[0] <= port <= ports[1]) */ |
| 48 |
#define IP_FW_F_BIDIR 0x0040 /* For bidirectional firewalls */ |
| 49 |
#define IP_FW_F_ACCTIN 0x1000 /* Account incoming packets only. */ |
| 50 |
#define IP_FW_F_ACCTOUT 0x2000 /* Account outgoing packets only. */ |
| 51 |
|
| 52 |
static unsigned char rule[IPFWRULELEN]; /*Buffer for reading a line from |
| 53 |
* /proc/net/ip_acct. Care has been taken |
| 54 |
* not to read beyond the end of this |
| 55 |
* buffer, even if rules are in an |
| 56 |
* unexpected format |
| 57 |
*/ |
| 58 |
|
| 59 |
/* |
| 60 |
* This function reads the rule with the given number into the buffer. It |
| 61 |
* * returns the number of rule read or 0 if the number is invalid or other |
| 62 |
* * problems occur. If the argument is 0 it returns the number of accounting |
| 63 |
* * rules. No caching of rules is done. |
| 64 |
*/ |
| 65 |
|
| 66 |
static int |
| 67 |
readrule(unsigned int number) |
| 68 |
{ |
| 69 |
int i; |
| 70 |
FILE *f = fopen("/proc/net/ip_acct", "rt"); |
| 71 |
|
| 72 |
if (!f) |
| 73 |
return 0; |
| 74 |
/* |
| 75 |
* get rid of "IP accounting rules" line |
| 76 |
*/ |
| 77 |
if (!fgets((char *) rule, sizeof(rule), f)) { |
| 78 |
fclose(f); |
| 79 |
return 0; |
| 80 |
} |
| 81 |
for (i = 1; i != number; i++) |
| 82 |
if (!fgets((char *) rule, sizeof(rule), f)) { |
| 83 |
fclose(f); |
| 84 |
return (number ? 0 : (i - 1)); |
| 85 |
} |
| 86 |
if (!fgets((char *) rule, sizeof(rule), f)) { |
| 87 |
fclose(f); |
| 88 |
return 0; |
| 89 |
} |
| 90 |
fclose(f); |
| 91 |
return i; |
| 92 |
} |
| 93 |
|
| 94 |
static unsigned long ret_val; /* Used by var_ipfwacc to return ulongs */ |
| 95 |
|
| 96 |
/* |
| 97 |
* This function converts the hexadecimal representation of an IP address from |
| 98 |
* * the rule buffer to an unsigned long. The result is stored in the ret_val |
| 99 |
* * variable. The parameter indicates the position where the address starts. It |
| 100 |
* * only works with uppercase letters and assumes input is correct. Had to use |
| 101 |
* * this because stol returns a signed long. |
| 102 |
*/ |
| 103 |
|
| 104 |
NETSNMP_STATIC_INLINE void |
| 105 |
atoip(int pos) |
| 106 |
{ |
| 107 |
int i; |
| 108 |
|
| 109 |
ret_val = 0; |
| 110 |
for (i = 0; i < 32; i += 8) { |
| 111 |
unsigned long value = (((rule[pos]) >= '0' && rule[pos] <= '9') ? |
| 112 |
rule[pos] - '0' : rule[pos] - 'A' + 10); |
| 113 |
pos++; |
| 114 |
value = (value << 4) + (((rule[pos]) >= '0' && rule[pos] <= '9') ? |
| 115 |
rule[pos] - '0' : rule[pos] - 'A' + 10); |
| 116 |
pos++; |
| 117 |
ret_val |= (value << i); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/* |
| 122 |
* This function parses the flags field from the line in the buffer |
| 123 |
*/ |
| 124 |
|
| 125 |
static unsigned long int |
| 126 |
getflags(void) |
| 127 |
{ |
| 128 |
unsigned long int flags; |
| 129 |
int i = 37; /* position in the rule */ |
| 130 |
|
| 131 |
/* |
| 132 |
* skipping via name |
| 133 |
*/ |
| 134 |
while (rule[i] != ' ' && i < IPFWRULELEN - 12) |
| 135 |
i++; |
| 136 |
/* |
| 137 |
* skipping via address |
| 138 |
*/ |
| 139 |
i += 10; |
| 140 |
for (flags = 0; rule[i] != ' ' && i < IPFWRULELEN - 1; i++) { |
| 141 |
int value = (((rule[i]) >= '0' && rule[i] <= '9') ? |
| 142 |
rule[i] - '0' : rule[i] - 'A' + 10); |
| 143 |
flags = (flags << 4) + value; |
| 144 |
} |
| 145 |
return flags; |
| 146 |
} |
| 147 |
|
| 148 |
/* |
| 149 |
* This function reads into ret_val a field from the rule buffer. The field |
| 150 |
* * is a base 10 long integer and the parameter skip tells us how many fields |
| 151 |
* * to skip after the "via addrress" field (including the flag field) |
| 152 |
*/ |
| 153 |
|
| 154 |
static void |
| 155 |
getnumeric(int skip) |
| 156 |
{ |
| 157 |
int i = 37; /* position in the rule */ |
| 158 |
|
| 159 |
/* |
| 160 |
* skipping via name |
| 161 |
*/ |
| 162 |
while (rule[i] != ' ' && i < IPFWRULELEN - 12) |
| 163 |
i++; |
| 164 |
/* |
| 165 |
* skipping via address |
| 166 |
*/ |
| 167 |
i += 10; |
| 168 |
while (skip > 0) { |
| 169 |
skip--; |
| 170 |
/* |
| 171 |
* skipping field, than subsequent spaces |
| 172 |
*/ |
| 173 |
while (rule[i] != ' ' && i < IPFWRULELEN - 2) |
| 174 |
i++; |
| 175 |
while (rule[i] == ' ' && i < IPFWRULELEN - 1) |
| 176 |
i++; |
| 177 |
} |
| 178 |
for (ret_val = 0; rule[i] != ' ' && i < IPFWRULELEN - 1; i++) |
| 179 |
ret_val = ret_val * 10 + rule[i] - '0'; |
| 180 |
} |
| 181 |
|
| 182 |
/* |
| 183 |
* this variable defines function callbacks and type return information |
| 184 |
* for the ipfwaccounting mib |
| 185 |
*/ |
| 186 |
|
| 187 |
struct variable2 ipfwacc_variables[] = { |
| 188 |
{IPFWACCINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 189 |
var_ipfwacc, 1, {IPFWACCINDEX}}, |
| 190 |
{IPFWACCSRCADDR, ASN_IPADDRESS, NETSNMP_OLDAPI_RONLY, |
| 191 |
var_ipfwacc, 1, {IPFWACCSRCADDR}}, |
| 192 |
{IPFWACCSRCNM, ASN_IPADDRESS, NETSNMP_OLDAPI_RONLY, |
| 193 |
var_ipfwacc, 1, {IPFWACCSRCNM}}, |
| 194 |
{IPFWACCDSTADDR, ASN_IPADDRESS, NETSNMP_OLDAPI_RONLY, |
| 195 |
var_ipfwacc, 1, {IPFWACCDSTADDR}}, |
| 196 |
{IPFWACCDSTNM, ASN_IPADDRESS, NETSNMP_OLDAPI_RONLY, |
| 197 |
var_ipfwacc, 1, {IPFWACCDSTNM}}, |
| 198 |
{IPFWACCVIANAME, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY, |
| 199 |
var_ipfwacc, 1, {IPFWACCVIANAME}}, |
| 200 |
{IPFWACCVIAADDR, ASN_IPADDRESS, NETSNMP_OLDAPI_RONLY, |
| 201 |
var_ipfwacc, 1, {IPFWACCVIAADDR}}, |
| 202 |
{IPFWACCPROTO, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 203 |
var_ipfwacc, 1, {IPFWACCPROTO}}, |
| 204 |
{IPFWACCBIDIR, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 205 |
var_ipfwacc, 1, {IPFWACCBIDIR}}, |
| 206 |
{IPFWACCDIR, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 207 |
var_ipfwacc, 1, {IPFWACCDIR}}, |
| 208 |
{IPFWACCBYTES, ASN_COUNTER, NETSNMP_OLDAPI_RONLY, |
| 209 |
var_ipfwacc, 1, {IPFWACCBYTES}}, |
| 210 |
{IPFWACCPACKETS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY, |
| 211 |
var_ipfwacc, 1, {IPFWACCPACKETS}}, |
| 212 |
{IPFWACCNSRCPRTS, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 213 |
var_ipfwacc, 1, {IPFWACCNSRCPRTS}}, |
| 214 |
{IPFWACCNDSTPRTS, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 215 |
var_ipfwacc, 1, {IPFWACCNDSTPRTS}}, |
| 216 |
{IPFWACCSRCISRNG, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 217 |
var_ipfwacc, 1, {IPFWACCSRCISRNG}}, |
| 218 |
{IPFWACCDSTISRNG, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 219 |
var_ipfwacc, 1, {IPFWACCDSTISRNG}}, |
| 220 |
{IPFWACCPORT1, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 221 |
var_ipfwacc, 1, {IPFWACCPORT1}}, |
| 222 |
{IPFWACCPORT2, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 223 |
var_ipfwacc, 1, {IPFWACCPORT2}}, |
| 224 |
{IPFWACCPORT3, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 225 |
var_ipfwacc, 1, {IPFWACCPORT3}}, |
| 226 |
{IPFWACCPORT4, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 227 |
var_ipfwacc, 1, {IPFWACCPORT4}}, |
| 228 |
{IPFWACCPORT5, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 229 |
var_ipfwacc, 1, {IPFWACCPORT5}}, |
| 230 |
{IPFWACCPORT6, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 231 |
var_ipfwacc, 1, {IPFWACCPORT6}}, |
| 232 |
{IPFWACCPORT7, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 233 |
var_ipfwacc, 1, {IPFWACCPORT7}}, |
| 234 |
{IPFWACCPORT8, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 235 |
var_ipfwacc, 1, {IPFWACCPORT8}}, |
| 236 |
{IPFWACCPORT9, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 237 |
var_ipfwacc, 1, {IPFWACCPORT9}}, |
| 238 |
{IPFWACCPORT10, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 239 |
var_ipfwacc, 1, {IPFWACCPORT10}} |
| 240 |
}; |
| 241 |
|
| 242 |
oid ipfwacc_variables_oid[] = |
| 243 |
{ 1, 3, 6, 1, 4, 1, 2021, 13, 1, 1, 1 }; |
| 244 |
|
| 245 |
void |
| 246 |
init_ipfwacc(void) |
| 247 |
{ |
| 248 |
REGISTER_MIB("misc/ipfwacc", ipfwacc_variables, variable2, |
| 249 |
ipfwacc_variables_oid); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
u_char * |
| 254 |
var_ipfwacc(struct variable *vp, |
| 255 |
oid * name, |
| 256 |
size_t * length, |
| 257 |
int exact, size_t * var_len, WriteMethod ** write_method) |
| 258 |
{ |
| 259 |
*write_method = NULL; /* assume it isnt writable for the time being */ |
| 260 |
*var_len = sizeof(ret_val); /* assume an integer and change later if not */ |
| 261 |
|
| 262 |
if (header_simple_table |
| 263 |
(vp, name, length, exact, var_len, write_method, readrule(0))) |
| 264 |
return (NULL); |
| 265 |
|
| 266 |
if (readrule(name[*length - 1])) { |
| 267 |
/* |
| 268 |
* this is where we do the value assignments for the mib results. |
| 269 |
*/ |
| 270 |
switch (vp->magic) { |
| 271 |
case IPFWACCINDEX: |
| 272 |
ret_val = name[*length - 1]; |
| 273 |
return ((u_char *) (&ret_val)); |
| 274 |
case IPFWACCSRCADDR: |
| 275 |
atoip(0); |
| 276 |
return ((u_char *) (&ret_val)); |
| 277 |
case IPFWACCSRCNM: |
| 278 |
atoip(9); |
| 279 |
return ((u_char *) (&ret_val)); |
| 280 |
case IPFWACCDSTADDR: |
| 281 |
atoip(19); |
| 282 |
return ((u_char *) (&ret_val)); |
| 283 |
case IPFWACCDSTNM: |
| 284 |
atoip(28); |
| 285 |
return ((u_char *) (&ret_val)); |
| 286 |
case IPFWACCVIANAME: |
| 287 |
{ |
| 288 |
int i = 37; /* position in the rule */ |
| 289 |
while (rule[i] != ' ' && i < IPFWRULELEN - 1) |
| 290 |
i++; |
| 291 |
rule[i] = 0; |
| 292 |
return (rule + 37); |
| 293 |
} |
| 294 |
case IPFWACCVIAADDR: |
| 295 |
{ |
| 296 |
int i = 37; /* position in the rule */ |
| 297 |
while (rule[i] != ' ' && i < IPFWRULELEN - 9) |
| 298 |
i++; |
| 299 |
atoip(i + 1); |
| 300 |
return ((u_char *) (&ret_val)); |
| 301 |
} |
| 302 |
case IPFWACCPROTO: |
| 303 |
switch (getflags() & IP_FW_F_KIND) { |
| 304 |
case IP_FW_F_ALL: |
| 305 |
ret_val = 2; |
| 306 |
return ((u_char *) (&ret_val)); |
| 307 |
case IP_FW_F_TCP: |
| 308 |
ret_val = 3; |
| 309 |
return ((u_char *) (&ret_val)); |
| 310 |
case IP_FW_F_UDP: |
| 311 |
ret_val = 4; |
| 312 |
return ((u_char *) (&ret_val)); |
| 313 |
case IP_FW_F_ICMP: |
| 314 |
ret_val = 5; |
| 315 |
return ((u_char *) (&ret_val)); |
| 316 |
default: |
| 317 |
ret_val = 1; |
| 318 |
return ((u_char *) (&ret_val)); |
| 319 |
} |
| 320 |
case IPFWACCBIDIR: |
| 321 |
ret_val = ((getflags() & IP_FW_F_BIDIR) ? 2 : 1); |
| 322 |
return ((u_char *) (&ret_val)); |
| 323 |
case IPFWACCDIR: |
| 324 |
ret_val = (getflags() & (IP_FW_F_ACCTIN | IP_FW_F_ACCTOUT)); |
| 325 |
if (ret_val == IP_FW_F_ACCTIN) |
| 326 |
ret_val = 2; |
| 327 |
else if (ret_val == IP_FW_F_ACCTOUT) |
| 328 |
ret_val = 3; |
| 329 |
else |
| 330 |
ret_val = 1; |
| 331 |
return ((u_char *) (&ret_val)); |
| 332 |
case IPFWACCBYTES: |
| 333 |
getnumeric(4); |
| 334 |
return ((u_char *) (&ret_val)); |
| 335 |
case IPFWACCPACKETS: |
| 336 |
getnumeric(3); |
| 337 |
return ((u_char *) (&ret_val)); |
| 338 |
case IPFWACCNSRCPRTS: |
| 339 |
getnumeric(1); |
| 340 |
return ((u_char *) (&ret_val)); |
| 341 |
case IPFWACCNDSTPRTS: |
| 342 |
getnumeric(2); |
| 343 |
return ((u_char *) (&ret_val)); |
| 344 |
case IPFWACCSRCISRNG: |
| 345 |
ret_val = ((getflags() & IP_FW_F_SRNG) ? 1 : 2); |
| 346 |
return ((u_char *) (&ret_val)); |
| 347 |
case IPFWACCDSTISRNG: |
| 348 |
ret_val = ((getflags() & IP_FW_F_DRNG) ? 1 : 2); |
| 349 |
return ((u_char *) (&ret_val)); |
| 350 |
case IPFWACCPORT1: |
| 351 |
case IPFWACCPORT2: |
| 352 |
case IPFWACCPORT3: |
| 353 |
case IPFWACCPORT4: |
| 354 |
case IPFWACCPORT5: |
| 355 |
case IPFWACCPORT6: |
| 356 |
case IPFWACCPORT7: |
| 357 |
case IPFWACCPORT8: |
| 358 |
case IPFWACCPORT9: |
| 359 |
case IPFWACCPORT10: |
| 360 |
getnumeric(5 + (vp->magic) - IPFWACCPORT1); |
| 361 |
return ((u_char *) (&ret_val)); |
| 362 |
} |
| 363 |
} |
| 364 |
return NULL; |
| 365 |
} |