| 1 |
/* |
| 2 |
* Interface MIB architecture support |
| 3 |
* |
| 4 |
* $Id$ |
| 5 |
*/ |
| 6 |
#include <net-snmp/net-snmp-config.h> |
| 7 |
#include <net-snmp/net-snmp-includes.h> |
| 8 |
#include "mibII/mibII_common.h" |
| 9 |
|
| 10 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 11 |
#include <net-snmp/data_access/interface.h> |
| 12 |
#include <net-snmp/data_access/route.h> |
| 13 |
#include <net-snmp/data_access/ipaddress.h> |
| 14 |
|
| 15 |
#include "ip-forward-mib/data_access/route_ioctl.h" |
| 16 |
#include "ip-forward-mib/inetCidrRouteTable/inetCidrRouteTable_constants.h" |
| 17 |
#include "if-mib/data_access/interface_ioctl.h" |
| 18 |
#include "route.h" |
| 19 |
#include "route_private.h" |
| 20 |
|
| 21 |
static int |
| 22 |
_type_from_flags(unsigned int flags) |
| 23 |
{ |
| 24 |
/* |
| 25 |
* RTF_GATEWAY RTF_UP RTF_DYNAMIC RTF_CACHE |
| 26 |
* RTF_MODIFIED RTF_EXPIRES RTF_NONEXTHOP |
| 27 |
* RTF_DYNAMIC RTF_LOCAL RTF_PREFIX_RT |
| 28 |
* |
| 29 |
* xxx: can we distinguish between reject & blackhole? |
| 30 |
*/ |
| 31 |
if (flags & RTF_UP) { |
| 32 |
if (flags & RTF_GATEWAY) |
| 33 |
return INETCIDRROUTETYPE_REMOTE; |
| 34 |
else /*if (flags & RTF_LOCAL) */ |
| 35 |
return INETCIDRROUTETYPE_LOCAL; |
| 36 |
} else |
| 37 |
return 0; /* route not up */ |
| 38 |
|
| 39 |
} |
| 40 |
static int |
| 41 |
_load_ipv4(netsnmp_container* container, u_long *index ) |
| 42 |
{ |
| 43 |
FILE *in; |
| 44 |
char line[256]; |
| 45 |
netsnmp_route_entry *entry = NULL; |
| 46 |
char name[16]; |
| 47 |
int fd; |
| 48 |
|
| 49 |
DEBUGMSGTL(("access:route:container", |
| 50 |
"route_container_arch_load ipv4\n")); |
| 51 |
|
| 52 |
netsnmp_assert(NULL != container); |
| 53 |
|
| 54 |
/* |
| 55 |
* fetch routes from the proc file-system: |
| 56 |
*/ |
| 57 |
if (!(in = fopen("/proc/net/route", "r"))) { |
| 58 |
NETSNMP_LOGONCE((LOG_ERR, "cannot open /proc/net/route\n")); |
| 59 |
return -2; |
| 60 |
} |
| 61 |
|
| 62 |
/* |
| 63 |
* create socket for ioctls (see NOTE[1], below) |
| 64 |
*/ |
| 65 |
fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 66 |
if(fd < 0) { |
| 67 |
snmp_log(LOG_ERR, "could not create socket\n"); |
| 68 |
fclose(in); |
| 69 |
return -2; |
| 70 |
} |
| 71 |
|
| 72 |
NETSNMP_IGNORE_RESULT(fgets(line, sizeof(line), in)); /* skip header */ |
| 73 |
|
| 74 |
while (fgets(line, sizeof(line), in)) { |
| 75 |
char rtent_name[32]; |
| 76 |
int refcnt, rc; |
| 77 |
uint32_t dest, nexthop, mask; |
| 78 |
unsigned flags, use; |
| 79 |
|
| 80 |
entry = netsnmp_access_route_entry_create(); |
| 81 |
|
| 82 |
/* |
| 83 |
* as with 1.99.14: |
| 84 |
* Iface Dest GW Flags RefCnt Use Met Mask MTU Win IRTT |
| 85 |
* BE eth0 00000000 C0A80101 0003 0 0 0 FFFFFFFF 1500 0 0 |
| 86 |
* LE eth0 00000000 0101A8C0 0003 0 0 0 00FFFFFF 0 0 0 |
| 87 |
*/ |
| 88 |
rc = sscanf(line, "%s %x %x %x %d %u %d %x %*d %*d %*d\n", |
| 89 |
rtent_name, &dest, &nexthop, |
| 90 |
/* |
| 91 |
* XXX: fix type of the args |
| 92 |
*/ |
| 93 |
&flags, &refcnt, &use, &entry->rt_metric1, |
| 94 |
&mask); |
| 95 |
DEBUGMSGTL(("9:access:route:container", "line |%s|\n", line)); |
| 96 |
if (8 != rc) { |
| 97 |
snmp_log(LOG_ERR, |
| 98 |
"/proc/net/route data format error (%d!=8), line ==|%s|", |
| 99 |
rc, line); |
| 100 |
goto free_entry; |
| 101 |
} |
| 102 |
|
| 103 |
/* |
| 104 |
* temporary null terminated name |
| 105 |
*/ |
| 106 |
strlcpy(name, rtent_name, sizeof(name)); |
| 107 |
|
| 108 |
/* |
| 109 |
* don't bother to try and get the ifindex for routes with |
| 110 |
* no interface name. |
| 111 |
* NOTE[1]: normally we'd use netsnmp_access_interface_index_find, |
| 112 |
* but since that will open/close a socket, and we might |
| 113 |
* have a lot of routes, call the ioctl routine directly. |
| 114 |
*/ |
| 115 |
if ('*' != name[0]) { |
| 116 |
entry->if_index = |
| 117 |
netsnmp_access_interface_ioctl_ifindex_get(fd,name); |
| 118 |
if (entry->if_index == 0) |
| 119 |
goto free_entry; |
| 120 |
} |
| 121 |
|
| 122 |
/* |
| 123 |
* arbitrary index |
| 124 |
*/ |
| 125 |
entry->ns_rt_index = ++(*index); |
| 126 |
|
| 127 |
#ifdef USING_IP_FORWARD_MIB_IPCIDRROUTETABLE_IPCIDRROUTETABLE_MODULE |
| 128 |
memcpy(&entry->rt_mask, &mask, 4); |
| 129 |
/** entry->rt_tos = XXX; */ |
| 130 |
/** rt info ?? */ |
| 131 |
#endif |
| 132 |
/* |
| 133 |
* copy dest & next hop |
| 134 |
*/ |
| 135 |
entry->rt_dest_type = INETADDRESSTYPE_IPV4; |
| 136 |
entry->rt_dest_len = 4; |
| 137 |
memcpy(entry->rt_dest, &dest, 4); |
| 138 |
|
| 139 |
entry->rt_nexthop_type = INETADDRESSTYPE_IPV4; |
| 140 |
entry->rt_nexthop_len = 4; |
| 141 |
memcpy(entry->rt_nexthop, &nexthop, 4); |
| 142 |
|
| 143 |
/* |
| 144 |
* count bits in mask |
| 145 |
*/ |
| 146 |
entry->rt_pfx_len = netsnmp_ipaddress_ipv4_prefix_len(mask); |
| 147 |
|
| 148 |
#ifdef USING_IP_FORWARD_MIB_INETCIDRROUTETABLE_INETCIDRROUTETABLE_MODULE |
| 149 |
/* |
| 150 |
inetCidrRoutePolicy OBJECT-TYPE |
| 151 |
SYNTAX OBJECT IDENTIFIER |
| 152 |
MAX-ACCESS not-accessible |
| 153 |
STATUS current |
| 154 |
DESCRIPTION |
| 155 |
"This object is an opaque object without any defined |
| 156 |
semantics. Its purpose is to serve as an additional |
| 157 |
index which may delineate between multiple entries to |
| 158 |
the same destination. The value { 0 0 } shall be used |
| 159 |
as the default value for this object." |
| 160 |
*/ |
| 161 |
/* |
| 162 |
* on linux, default routes all look alike, and would have the same |
| 163 |
* indexed based on dest and next hop. So we use the if index |
| 164 |
* as the policy, to distinguise between them. Hopefully this is |
| 165 |
* unique. |
| 166 |
* xxx-rks: It should really only be for the duplicate case, but that |
| 167 |
* would be more complicated than I want to get into now. Fix later. |
| 168 |
*/ |
| 169 |
if (0 == nexthop) { |
| 170 |
entry->rt_policy = calloc(3, sizeof(oid)); |
| 171 |
entry->rt_policy[2] = entry->if_index; |
| 172 |
entry->rt_policy_len = sizeof(oid)*3; |
| 173 |
} |
| 174 |
#endif |
| 175 |
|
| 176 |
/* |
| 177 |
* get protocol and type from flags |
| 178 |
*/ |
| 179 |
entry->rt_type = _type_from_flags(flags); |
| 180 |
|
| 181 |
entry->rt_proto = (flags & RTF_DYNAMIC) |
| 182 |
? IANAIPROUTEPROTOCOL_ICMP : IANAIPROUTEPROTOCOL_LOCAL; |
| 183 |
|
| 184 |
/* |
| 185 |
* insert into container |
| 186 |
*/ |
| 187 |
if (CONTAINER_INSERT(container, entry) < 0) |
| 188 |
{ |
| 189 |
DEBUGMSGTL(("access:route:container", "error with route_entry: insert into container failed.\n")); |
| 190 |
free_entry: |
| 191 |
netsnmp_access_route_entry_free(entry); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
fclose(in); |
| 196 |
close(fd); |
| 197 |
return 0; |
| 198 |
} |
| 199 |
|
| 200 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 201 |
static int |
| 202 |
_load_ipv6(netsnmp_container* container, u_long *index ) |
| 203 |
{ |
| 204 |
FILE *in; |
| 205 |
char line[256]; |
| 206 |
netsnmp_route_entry *entry = NULL; |
| 207 |
|
| 208 |
DEBUGMSGTL(("access:route:container", |
| 209 |
"route_container_arch_load ipv6\n")); |
| 210 |
|
| 211 |
netsnmp_assert(NULL != container); |
| 212 |
|
| 213 |
/* |
| 214 |
* fetch routes from the proc file-system: |
| 215 |
*/ |
| 216 |
if (!(in = fopen("/proc/net/ipv6_route", "r"))) { |
| 217 |
DEBUGMSGTL(("9:access:route:container", "cannot open /proc/net/ipv6_route\n")); |
| 218 |
return -2; |
| 219 |
} |
| 220 |
|
| 221 |
while (fgets(line, sizeof(line), in)) { |
| 222 |
char c_name[IFNAMSIZ+1]; |
| 223 |
char c_dest[33], c_src[33], c_next[33]; |
| 224 |
int rc; |
| 225 |
unsigned int dest_pfx, flags; |
| 226 |
size_t buf_len, buf_offset; |
| 227 |
u_char *temp_uchar_ptr; |
| 228 |
|
| 229 |
entry = netsnmp_access_route_entry_create(); |
| 230 |
|
| 231 |
/* |
| 232 |
* based on /usr/src/linux/net/ipv6/route.c, kernel 2.6.7: |
| 233 |
* |
| 234 |
* [ Dest addr / plen ] |
| 235 |
* fe80000000000000025056fffec00008 80 \ |
| 236 |
* |
| 237 |
* [ (?subtree) : src addr/plen : 0/0] |
| 238 |
* 00000000000000000000000000000000 00 \ |
| 239 |
* |
| 240 |
* [ next hop ][ metric ][ref ctn][ use ] |
| 241 |
* 00000000000000000000000000000000 00000000 00000000 00000000 \ |
| 242 |
* |
| 243 |
* [ flags ][dev name] |
| 244 |
* 80200001 lo |
| 245 |
*/ |
| 246 |
rc = sscanf(line, "%32s %2x %32s %*x %32s %x %*x %*x %x %" |
| 247 |
SNMP_MACRO_VAL_TO_STR(IFNAMSIZ) "s\n", |
| 248 |
c_dest, &dest_pfx, c_src, /*src_pfx,*/ c_next, |
| 249 |
&entry->rt_metric1, /** ref,*/ /* use, */ &flags, c_name); |
| 250 |
DEBUGMSGTL(("9:access:route:container", "line |%s|\n", line)); |
| 251 |
if (7 != rc) { |
| 252 |
snmp_log(LOG_ERR, |
| 253 |
"/proc/net/ipv6_route data format error (%d!=8), " |
| 254 |
"line ==|%s|", rc, line); |
| 255 |
free(entry); |
| 256 |
continue; |
| 257 |
} |
| 258 |
|
| 259 |
/* |
| 260 |
* temporary null terminated name |
| 261 |
*/ |
| 262 |
c_name[ sizeof(c_name)-1 ] = 0; |
| 263 |
entry->if_index = se_find_value_in_slist("interfaces", c_name); |
| 264 |
if(SE_DNE == entry->if_index) { |
| 265 |
snmp_log(LOG_ERR,"unknown interface in /proc/net/ipv6_route " |
| 266 |
"('%s')\n", c_name); |
| 267 |
netsnmp_access_route_entry_free(entry); |
| 268 |
continue; |
| 269 |
} |
| 270 |
/* |
| 271 |
* arbitrary index |
| 272 |
*/ |
| 273 |
entry->ns_rt_index = ++(*index); |
| 274 |
|
| 275 |
#ifdef USING_IP_FORWARD_MIB_IPCIDRROUTETABLE_IPCIDRROUTETABLE_MODULE |
| 276 |
/** entry->rt_mask = mask; */ /* IPv4 only */ |
| 277 |
/** entry->rt_tos = XXX; */ |
| 278 |
/** rt info ?? */ |
| 279 |
#endif |
| 280 |
/* |
| 281 |
* convert hex addresses to binary |
| 282 |
*/ |
| 283 |
entry->rt_dest_type = INETADDRESSTYPE_IPV6; |
| 284 |
entry->rt_dest_len = 16; |
| 285 |
buf_len = sizeof(entry->rt_dest); |
| 286 |
buf_offset = 0; |
| 287 |
temp_uchar_ptr = entry->rt_dest; |
| 288 |
netsnmp_hex_to_binary(&temp_uchar_ptr, &buf_len, &buf_offset, 0, |
| 289 |
c_dest, NULL); |
| 290 |
|
| 291 |
entry->rt_nexthop_type = INETADDRESSTYPE_IPV6; |
| 292 |
entry->rt_nexthop_len = 16; |
| 293 |
buf_len = sizeof(entry->rt_nexthop); |
| 294 |
buf_offset = 0; |
| 295 |
temp_uchar_ptr = entry->rt_nexthop; |
| 296 |
netsnmp_hex_to_binary(&temp_uchar_ptr, &buf_len, &buf_offset, 0, |
| 297 |
c_next, NULL); |
| 298 |
|
| 299 |
entry->rt_pfx_len = dest_pfx; |
| 300 |
|
| 301 |
#ifdef USING_IP_FORWARD_MIB_INETCIDRROUTETABLE_INETCIDRROUTETABLE_MODULE |
| 302 |
/* |
| 303 |
inetCidrRoutePolicy OBJECT-TYPE |
| 304 |
SYNTAX OBJECT IDENTIFIER |
| 305 |
MAX-ACCESS not-accessible |
| 306 |
STATUS current |
| 307 |
DESCRIPTION |
| 308 |
"This object is an opaque object without any defined |
| 309 |
semantics. Its purpose is to serve as an additional |
| 310 |
index which may delineate between multiple entries to |
| 311 |
the same destination. The value { 0 0 } shall be used |
| 312 |
as the default value for this object." |
| 313 |
*/ |
| 314 |
/* |
| 315 |
* on linux, default routes all look alike, and would have the same |
| 316 |
* indexed based on dest and next hop. So we use our arbitrary index |
| 317 |
* as the policy, to distinguish between them. |
| 318 |
*/ |
| 319 |
entry->rt_policy = calloc(3, sizeof(oid)); |
| 320 |
entry->rt_policy[2] = entry->ns_rt_index; |
| 321 |
entry->rt_policy_len = sizeof(oid)*3; |
| 322 |
#endif |
| 323 |
|
| 324 |
/* |
| 325 |
* get protocol and type from flags |
| 326 |
*/ |
| 327 |
entry->rt_type = _type_from_flags(flags); |
| 328 |
|
| 329 |
entry->rt_proto = (flags & RTF_DYNAMIC) |
| 330 |
? IANAIPROUTEPROTOCOL_ICMP : IANAIPROUTEPROTOCOL_LOCAL; |
| 331 |
|
| 332 |
/* |
| 333 |
* insert into container |
| 334 |
*/ |
| 335 |
CONTAINER_INSERT(container, entry); |
| 336 |
} |
| 337 |
|
| 338 |
fclose(in); |
| 339 |
return 0; |
| 340 |
} |
| 341 |
#endif |
| 342 |
|
| 343 |
/** arch specific load |
| 344 |
* @internal |
| 345 |
* |
| 346 |
* @retval 0 success |
| 347 |
* @retval -1 no container specified |
| 348 |
* @retval -2 could not open data file |
| 349 |
*/ |
| 350 |
int |
| 351 |
netsnmp_access_route_container_arch_load(netsnmp_container* container, |
| 352 |
u_int load_flags) |
| 353 |
{ |
| 354 |
u_long count = 0; |
| 355 |
int rc; |
| 356 |
|
| 357 |
DEBUGMSGTL(("access:route:container", |
| 358 |
"route_container_arch_load (flags %x)\n", load_flags)); |
| 359 |
|
| 360 |
if (NULL == container) { |
| 361 |
snmp_log(LOG_ERR, "no container specified/found for access_route\n"); |
| 362 |
return -1; |
| 363 |
} |
| 364 |
|
| 365 |
rc = _load_ipv4(container, &count); |
| 366 |
|
| 367 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 368 |
if((0 != rc) || (load_flags & NETSNMP_ACCESS_ROUTE_LOAD_IPV4_ONLY)) |
| 369 |
return rc; |
| 370 |
|
| 371 |
/* |
| 372 |
* load ipv6. ipv6 module might not be loaded, |
| 373 |
* so ignore -2 err (file not found) |
| 374 |
*/ |
| 375 |
rc = _load_ipv6(container, &count); |
| 376 |
if (-2 == rc) |
| 377 |
rc = 0; |
| 378 |
#endif |
| 379 |
|
| 380 |
return rc; |
| 381 |
} |
| 382 |
|
| 383 |
/* |
| 384 |
* create a new entry |
| 385 |
*/ |
| 386 |
int |
| 387 |
netsnmp_arch_route_create(netsnmp_route_entry *entry) |
| 388 |
{ |
| 389 |
if (NULL == entry) |
| 390 |
return -1; |
| 391 |
|
| 392 |
if (4 != entry->rt_dest_len) { |
| 393 |
DEBUGMSGT(("access:route:create", "only ipv4 supported\n")); |
| 394 |
return -2; |
| 395 |
} |
| 396 |
|
| 397 |
return _netsnmp_ioctl_route_set_v4(entry); |
| 398 |
} |
| 399 |
|
| 400 |
/* |
| 401 |
* create a new entry |
| 402 |
*/ |
| 403 |
int |
| 404 |
netsnmp_arch_route_delete(netsnmp_route_entry *entry) |
| 405 |
{ |
| 406 |
if (NULL == entry) |
| 407 |
return -1; |
| 408 |
|
| 409 |
if (4 != entry->rt_dest_len) { |
| 410 |
DEBUGMSGT(("access:route:create", "only ipv4 supported\n")); |
| 411 |
return -2; |
| 412 |
} |
| 413 |
|
| 414 |
return _netsnmp_ioctl_route_delete_v4(entry); |
| 415 |
} |
| 416 |
|
| 417 |
|