| 1 |
/* |
| 2 |
* tunnel.c -- |
| 3 |
* |
| 4 |
* An implementation of the TUNNEL-MIB for the UCD-SNMP 4.2 |
| 5 |
* agent running on Linux 2.2.x. |
| 6 |
* |
| 7 |
* Copyright (c) 2000 Frank Strauss <[email protected]> |
| 8 |
* |
| 9 |
* All Rights Reserved |
| 10 |
* |
| 11 |
* Permission to use, copy, modify and distribute this software and its |
| 12 |
* documentation for any purpose and without fee is hereby granted, |
| 13 |
* provided that the above copyright notice appears in all copies and |
| 14 |
* that both that copyright notice and this permission notice appear in |
| 15 |
* supporting documentation, and that the name of the author and CMU and |
| 16 |
* The Regents of the University of California not be used in advertising |
| 17 |
* or publicity pertaining to distribution of the software without |
| 18 |
* specific written permission. |
| 19 |
* |
| 20 |
* THE AUTHOR AND CMU AND THE REGENTS OF THE UNIVERSITY OF CALIFORNIA |
| 21 |
* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL |
| 22 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL |
| 23 |
* THE AUTHOR OR CMU OR THE REGENTS OF THE UNIVERSITY OF CALIFORNIA BE |
| 24 |
* LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY |
| 25 |
* DAMAGES WHATSOEVER RESULTING FROM THE LOSS OF USE, DATA OR PROFITS, |
| 26 |
* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, |
| 27 |
* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 28 |
* SOFTWARE. |
| 29 |
* |
| 30 |
*/ |
| 31 |
|
| 32 |
/* |
| 33 |
* NOTE: This TUNNEL-MIB implementation |
| 34 |
* |
| 35 |
* (a) DOES NOT implement write access on the tunnelConfigTable, |
| 36 |
* i.e. no new tunnels can be created and no existing tunnels |
| 37 |
* can be removed through SET operations. |
| 38 |
* |
| 39 |
* (b) DOES implement write access on some tunnelIfTable objects |
| 40 |
* to allow reconfiguring established tunnels. This violates |
| 41 |
* RFC 2667! However, the author thinks it makes sense. ;-) |
| 42 |
*/ |
| 43 |
|
| 44 |
#include <string.h> |
| 45 |
#include <stdio.h> |
| 46 |
#include <stdlib.h> |
| 47 |
#include <unistd.h> |
| 48 |
#include <sys/types.h> |
| 49 |
#include <sys/socket.h> |
| 50 |
#include <sys/time.h> |
| 51 |
#include <sys/ioctl.h> |
| 52 |
#include <signal.h> |
| 53 |
#include <netinet/in.h> |
| 54 |
#include <arpa/inet.h> |
| 55 |
|
| 56 |
#include <linux/if.h> |
| 57 |
#include <linux/ip.h> |
| 58 |
#include <linux/sockios.h> |
| 59 |
#include <linux/if_tunnel.h> |
| 60 |
#include <linux/if_arp.h> |
| 61 |
|
| 62 |
#include <net-snmp/net-snmp-config.h> |
| 63 |
#include <net-snmp/net-snmp-includes.h> |
| 64 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 65 |
#include <net-snmp/agent/sysORTable.h> |
| 66 |
|
| 67 |
#include "tunnel.h" |
| 68 |
|
| 69 |
#ifndef MIN |
| 70 |
#define MIN(a, b) (((a) < (b)) ? (a) : (b)) |
| 71 |
#endif |
| 72 |
#ifndef MAX |
| 73 |
#define MAX(a, b) (((a) > (b)) ? (a) : (b)) |
| 74 |
#endif |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
#ifdef USING_IF_MIB_IFTABLE_IFTABLE_MODULE |
| 79 |
#include "if-mib/ifTable/ifTable.h" |
| 80 |
#include "if-mib/ifTable/ifTable_defs.h" |
| 81 |
#endif |
| 82 |
|
| 83 |
|
| 84 |
/* |
| 85 |
* tunnel_variables_oid: |
| 86 |
* this is the top level oid that we want to register under. This |
| 87 |
* is essentially a prefix, with the suffix appearing in the |
| 88 |
* variable below. |
| 89 |
*/ |
| 90 |
oid tunnel_variables_oid[] = |
| 91 |
{ 1, 3, 6, 1, 2, 1, 10, 131, 1, 1 }; |
| 92 |
const int tunnel_len = 10; |
| 93 |
|
| 94 |
oid tunnel_configEntry_oid[] = |
| 95 |
{ 1, 3, 6, 1, 2, 1, 10, 131, 1, 1, 2, 1 }; |
| 96 |
const int tunnel_configEntry_len = 12; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
struct tunnel { |
| 101 |
oid ifindex; |
| 102 |
int id; |
| 103 |
char *ifname; |
| 104 |
int active; |
| 105 |
unsigned long local; |
| 106 |
unsigned long remote; |
| 107 |
int encaps; |
| 108 |
int hoplimit; |
| 109 |
int security; |
| 110 |
int tos; |
| 111 |
oid config_name[MAX_OID_LEN]; |
| 112 |
size_t config_length; |
| 113 |
struct tunnel *next; |
| 114 |
}; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
/* |
| 119 |
* variable4 tunnel_variables: |
| 120 |
* this variable defines function callbacks and type return information |
| 121 |
* for the tunnel mib section |
| 122 |
*/ |
| 123 |
|
| 124 |
struct variable4 tunnel_variables[] = { |
| 125 |
/* |
| 126 |
* magic number , variable type , ro/rw , callback fn , L, oidsuffix |
| 127 |
*/ |
| 128 |
#define LOCALADDRESS 1 |
| 129 |
{LOCALADDRESS, ASN_IPADDRESS, NETSNMP_OLDAPI_RWRITE, |
| 130 |
var_tunnelIfEntry, 3, {1, 1, 1}}, |
| 131 |
#define REMOTEADDRESS 2 |
| 132 |
{REMOTEADDRESS, ASN_IPADDRESS, NETSNMP_OLDAPI_RWRITE, |
| 133 |
var_tunnelIfEntry, 3, {1, 1, 2}}, |
| 134 |
#define ENCAPSMETHOD 3 |
| 135 |
{ENCAPSMETHOD, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 136 |
var_tunnelIfEntry, 3, {1, 1, 3}}, |
| 137 |
#define HOPLIMIT 4 |
| 138 |
{HOPLIMIT, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE, |
| 139 |
var_tunnelIfEntry, 3, {1, 1, 4}}, |
| 140 |
#define SECURITY 5 |
| 141 |
{SECURITY, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 142 |
var_tunnelIfEntry, 3, {1, 1, 5}}, |
| 143 |
#define TOS 6 |
| 144 |
{TOS, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE, |
| 145 |
var_tunnelIfEntry, 3, {1, 1, 6}}, |
| 146 |
|
| 147 |
#define IFINDEX 7 |
| 148 |
{IFINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 149 |
var_tunnelConfigEntry, 3, {2, 1, 5}}, |
| 150 |
#define ROWSTATUS 8 |
| 151 |
{ROWSTATUS, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE, |
| 152 |
var_tunnelConfigEntry, 3, {2, 1, 6}}, |
| 153 |
}; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
static oid sysORTable_reg[] = { 1, 3, 6, 1, 2, 1, 10, 131 }; |
| 158 |
|
| 159 |
static struct tunnel *tunnels; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
void |
| 164 |
deinit_tunnel(void) |
| 165 |
{ |
| 166 |
UNREGISTER_SYSOR_ENTRY(sysORTable_reg); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
int |
| 172 |
term_tunnel(int majorID, int minorID, void *serverarg, void *clientarg) |
| 173 |
{ |
| 174 |
deinit_tunnel(); |
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
void |
| 181 |
init_tunnel(void) |
| 182 |
{ |
| 183 |
REGISTER_SYSOR_ENTRY(sysORTable_reg, |
| 184 |
"RFC 2667 TUNNEL-MIB implementation for " |
| 185 |
"Linux 2.2.x kernels."); |
| 186 |
|
| 187 |
/* |
| 188 |
* register ourselves with the agent to handle our mib tree |
| 189 |
*/ |
| 190 |
REGISTER_MIB("tunnel", tunnel_variables, variable4, |
| 191 |
tunnel_variables_oid); |
| 192 |
|
| 193 |
snmp_register_callback(SNMP_CALLBACK_LIBRARY, |
| 194 |
SNMP_CALLBACK_SHUTDOWN, term_tunnel, NULL); |
| 195 |
|
| 196 |
tunnels = NULL; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
static int |
| 202 |
getType(int index) |
| 203 |
{ |
| 204 |
#ifndef USING_IF_MIB_IFTABLE_IFTABLE_MODULE |
| 205 |
oid name[MAX_OID_LEN] = { 1, 3, 6, 1, 2, 1, 2, 2, 1, 3 }; |
| 206 |
size_t length = 10; |
| 207 |
struct variable ifType_variable = |
| 208 |
{ 3, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 209 |
var_ifEntry, 10, {1, 3, 6, 1, 2, 1, 2, 2, 1, 3} |
| 210 |
}; |
| 211 |
unsigned char *p; |
| 212 |
size_t var_len; |
| 213 |
WriteMethod *write_method; |
| 214 |
|
| 215 |
name[length] = index; |
| 216 |
length++; |
| 217 |
|
| 218 |
p = var_ifEntry(&ifType_variable, |
| 219 |
name, &length, |
| 220 |
1 /* exact */ , &var_len, &write_method); |
| 221 |
if (!p) |
| 222 |
return 0; |
| 223 |
|
| 224 |
return *(int *) p; |
| 225 |
#else |
| 226 |
ifTable_mib_index imi; |
| 227 |
ifTable_rowreq_ctx *rr; |
| 228 |
|
| 229 |
imi.ifIndex = index; |
| 230 |
rr = ifTable_row_find_by_mib_index(&imi); |
| 231 |
if (NULL == rr) |
| 232 |
return 0; |
| 233 |
|
| 234 |
return rr->data.ifType; |
| 235 |
#endif |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
static const char * |
| 241 |
getName(int index) |
| 242 |
{ |
| 243 |
#ifndef USING_IF_MIB_IFTABLE_IFTABLE_MODULE |
| 244 |
oid name[MAX_OID_LEN] = { 1, 3, 6, 1, 2, 1, 2, 2, 1, 2 }; |
| 245 |
size_t length = 10; |
| 246 |
struct variable ifName_variable = |
| 247 |
{ 2, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 248 |
var_ifEntry, 10, {1, 3, 6, 1, 2, 1, 2, 2, 1, 2} |
| 249 |
}; |
| 250 |
unsigned char *p; |
| 251 |
size_t var_len; |
| 252 |
WriteMethod *write_method; |
| 253 |
|
| 254 |
name[length] = index; |
| 255 |
length++; |
| 256 |
|
| 257 |
p = var_ifEntry(&ifName_variable, |
| 258 |
name, &length, |
| 259 |
1 /* exact */ , &var_len, &write_method); |
| 260 |
if (!p) |
| 261 |
return NULL; |
| 262 |
|
| 263 |
return p; |
| 264 |
#else |
| 265 |
return netsnmp_access_interface_name_find(index); |
| 266 |
#endif |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
static struct ip_tunnel_parm * |
| 272 |
getTunnelParm(char *ifname) |
| 273 |
{ |
| 274 |
struct ifreq ifrq; |
| 275 |
int fd; |
| 276 |
static struct ip_tunnel_parm parm; |
| 277 |
|
| 278 |
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 279 |
return NULL; |
| 280 |
} |
| 281 |
|
| 282 |
memset(&parm, 0, sizeof(struct ip_tunnel_parm)); |
| 283 |
strlcpy(ifrq.ifr_name, ifname, sizeof(ifrq.ifr_name)); |
| 284 |
ifrq.ifr_ifru.ifru_data = (void *) &parm; |
| 285 |
if (ioctl(fd, SIOCGETTUNNEL, &ifrq) < 0) { |
| 286 |
/* |
| 287 |
* try again with the last char of the device name cut off. |
| 288 |
* it might have been a zero digit appended by the agent. |
| 289 |
*/ |
| 290 |
ifrq.ifr_name[strlen(ifrq.ifr_name) - 1] = 0; |
| 291 |
if (ioctl(fd, SIOCGETTUNNEL, &ifrq) < 0) { |
| 292 |
close(fd); |
| 293 |
return NULL; |
| 294 |
} |
| 295 |
ifname[strlen(ifname) - 1] = 0; |
| 296 |
} |
| 297 |
|
| 298 |
close(fd); |
| 299 |
|
| 300 |
return &parm; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
int |
| 306 |
setTunnelParm(char *ifname, struct ip_tunnel_parm *parm) |
| 307 |
{ |
| 308 |
struct ifreq ifrq; |
| 309 |
int fd; |
| 310 |
int err; |
| 311 |
|
| 312 |
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 313 |
return -1; |
| 314 |
} |
| 315 |
|
| 316 |
strlcpy(ifrq.ifr_name, ifname, sizeof(ifrq.ifr_name)); |
| 317 |
ifrq.ifr_ifru.ifru_data = (void *) parm; |
| 318 |
err = ioctl(fd, SIOCCHGTUNNEL, &ifrq); |
| 319 |
close(fd); |
| 320 |
|
| 321 |
return err; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
/* |
| 327 |
* update a struct tunnel. its index and ifname elements have to be set. |
| 328 |
*/ |
| 329 |
static struct tunnel * |
| 330 |
updateTunnel(struct tunnel *tunnel) |
| 331 |
{ |
| 332 |
struct ip_tunnel_parm *parm; |
| 333 |
int fd; |
| 334 |
struct ifreq ifrq; |
| 335 |
|
| 336 |
/* |
| 337 |
* NOTE: getTunnelParm() may adjust the passed ifname. |
| 338 |
*/ |
| 339 |
parm = getTunnelParm(tunnel->ifname); |
| 340 |
if (!parm) { |
| 341 |
DEBUGMSGTL(("tunnel", |
| 342 |
"updateTunnel(): getTunnelParm(\"%s\") returned NULL\n", |
| 343 |
tunnel->ifname)); |
| 344 |
tunnel->active = 0; |
| 345 |
return NULL; |
| 346 |
} |
| 347 |
|
| 348 |
tunnel->active = 1; |
| 349 |
|
| 350 |
tunnel->local = parm->iph.saddr; |
| 351 |
tunnel->remote = parm->iph.daddr; |
| 352 |
|
| 353 |
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 354 |
DEBUGMSGTL(("snmpd", "socket open failure in updateTunnels()\n")); |
| 355 |
return NULL; |
| 356 |
} else { |
| 357 |
/* |
| 358 |
* NOTE: this ioctl does not guarantee 6 bytes of a physaddr. |
| 359 |
* In particular, a 'sit0' interface only appears to get back |
| 360 |
* 4 bytes of sa_data. We don't use sa_data here, or we'd |
| 361 |
* need to memset it to 0 before the ioct. |
| 362 |
*/ |
| 363 |
strlcpy(ifrq.ifr_name, tunnel->ifname, sizeof(ifrq.ifr_name)); |
| 364 |
if (ioctl(fd, SIOCGIFHWADDR, &ifrq) == 0) |
| 365 |
switch (ifrq.ifr_hwaddr.sa_family) { |
| 366 |
case ARPHRD_TUNNEL: |
| 367 |
tunnel->encaps = 2; |
| 368 |
break; /* direct */ |
| 369 |
case ARPHRD_TUNNEL6: |
| 370 |
tunnel->encaps = 2; |
| 371 |
break; /* direct */ |
| 372 |
case ARPHRD_IPGRE: |
| 373 |
tunnel->encaps = 3; |
| 374 |
break; /* gre */ |
| 375 |
case ARPHRD_SIT: |
| 376 |
tunnel->encaps = 2; |
| 377 |
break; /* direct */ |
| 378 |
default: |
| 379 |
tunnel->encaps = 1; /* other */ |
| 380 |
} |
| 381 |
close(fd); |
| 382 |
} |
| 383 |
|
| 384 |
tunnel->hoplimit = parm->iph.ttl; |
| 385 |
tunnel->security = 1; |
| 386 |
tunnel->tos = (parm->iph.tos & 1) ? -1 : parm->iph.tos; |
| 387 |
/* |
| 388 |
* XXX: adjust tos mapping (kernel <-> TUNNEL-MIB::tunnelIfTOS) |
| 389 |
*/ |
| 390 |
|
| 391 |
return tunnel; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
static void |
| 397 |
updateTunnels(void) |
| 398 |
{ |
| 399 |
static int max_index = 1; |
| 400 |
static struct tunnel *last_tunnel = NULL; |
| 401 |
struct tunnel *tunnel; |
| 402 |
const char *ifname; |
| 403 |
int type; |
| 404 |
|
| 405 |
/* |
| 406 |
* uptime the tunnels we have so far |
| 407 |
*/ |
| 408 |
for (tunnel = tunnels; tunnel; tunnel = tunnel->next) { |
| 409 |
DEBUGMSG(("tunnel", |
| 410 |
"updateTunnels(): updating %s (index=%" NETSNMP_PRIo "u)\n", |
| 411 |
tunnel->ifname, tunnel->ifindex)); |
| 412 |
updateTunnel(tunnel); |
| 413 |
} |
| 414 |
|
| 415 |
/* |
| 416 |
* look for new tunnels |
| 417 |
*/ |
| 418 |
for (; max_index < 256; max_index++) { |
| 419 |
DEBUGMSG(("tunnel", |
| 420 |
"updateTunnels(): looking for new index=%d\n", |
| 421 |
max_index)); |
| 422 |
type = getType(max_index); |
| 423 |
if (type == 131) { |
| 424 |
tunnel = (struct tunnel *) malloc(sizeof(struct tunnel)); |
| 425 |
if (!tunnel) |
| 426 |
continue; |
| 427 |
|
| 428 |
tunnel->ifindex = max_index; |
| 429 |
tunnel->id = 1; |
| 430 |
|
| 431 |
ifname = getName(max_index); |
| 432 |
if (!ifname) { |
| 433 |
free(tunnel); |
| 434 |
continue; |
| 435 |
} |
| 436 |
|
| 437 |
tunnel->ifname = strdup(ifname); |
| 438 |
if (!tunnel->ifname) { |
| 439 |
free(tunnel); |
| 440 |
continue; |
| 441 |
} |
| 442 |
|
| 443 |
if (!updateTunnel(tunnel)) { |
| 444 |
free(tunnel->ifname); |
| 445 |
free(tunnel); |
| 446 |
continue; |
| 447 |
} |
| 448 |
|
| 449 |
if (last_tunnel) |
| 450 |
last_tunnel->next = tunnel; |
| 451 |
if (!tunnels) |
| 452 |
tunnels = last_tunnel = tunnel; |
| 453 |
tunnel->next = NULL; |
| 454 |
last_tunnel = tunnel; |
| 455 |
|
| 456 |
DEBUGMSG(("tunnel", |
| 457 |
"updateTunnels(): added %s (index=%" NETSNMP_PRIo |
| 458 |
"u state=%d)\n", |
| 459 |
tunnel->ifname, tunnel->ifindex, tunnel->active)); |
| 460 |
} |
| 461 |
if (type == 0) |
| 462 |
break; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
static struct tunnel * |
| 469 |
getTunnelByIfIndex(int index) |
| 470 |
{ |
| 471 |
struct tunnel *tunnel; |
| 472 |
|
| 473 |
DEBUGMSG(("tunnel", "getTunnelByIfIndex(%d): ", index)); |
| 474 |
|
| 475 |
for (tunnel = tunnels; tunnel; tunnel = tunnel->next) { |
| 476 |
if (tunnel->ifindex == index) { |
| 477 |
if (!tunnel->active) |
| 478 |
break; |
| 479 |
DEBUGMSG(("tunnel", "%s (index=%" NETSNMP_PRIo "u)\n", |
| 480 |
tunnel->ifname, tunnel->ifindex)); |
| 481 |
return tunnel; |
| 482 |
} |
| 483 |
} |
| 484 |
DEBUGMSG(("tunnel", "NONE\n")); |
| 485 |
return NULL; |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
static struct tunnel * |
| 491 |
getNextTunnelByIfIndex(int index) |
| 492 |
{ |
| 493 |
struct tunnel *tunnel; |
| 494 |
|
| 495 |
DEBUGMSG(("tunnel", "getNextTunnelByIfIndex(%d): ", index)); |
| 496 |
|
| 497 |
for (tunnel = tunnels; tunnel; tunnel = tunnel->next) { |
| 498 |
if (tunnel->ifindex > index) { |
| 499 |
if (!tunnel->active) |
| 500 |
continue; |
| 501 |
DEBUGMSG(("tunnel", "%s (index=%" NETSNMP_PRIo "u)\n", |
| 502 |
tunnel->ifname, tunnel->ifindex)); |
| 503 |
return tunnel; |
| 504 |
} |
| 505 |
} |
| 506 |
DEBUGMSG(("tunnel", "NONE\n")); |
| 507 |
return NULL; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
static void |
| 513 |
fillConfigOid(oid * name, struct tunnel *tunnel) |
| 514 |
{ |
| 515 |
name[0] = ((unsigned char *) &tunnel->local)[0]; |
| 516 |
name[1] = ((unsigned char *) &tunnel->local)[1]; |
| 517 |
name[2] = ((unsigned char *) &tunnel->local)[2]; |
| 518 |
name[3] = ((unsigned char *) &tunnel->local)[3]; |
| 519 |
name[4] = ((unsigned char *) &tunnel->remote)[0]; |
| 520 |
name[5] = ((unsigned char *) &tunnel->remote)[1]; |
| 521 |
name[6] = ((unsigned char *) &tunnel->remote)[2]; |
| 522 |
name[7] = ((unsigned char *) &tunnel->remote)[3]; |
| 523 |
name[8] = tunnel->encaps; |
| 524 |
name[9] = tunnel->id; |
| 525 |
DEBUGMSGOID(("tunnel", name, 10)); |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
static struct tunnel * |
| 531 |
getTunnelByConfigOid(oid * name, size_t * length) |
| 532 |
{ |
| 533 |
struct tunnel *tunnel; |
| 534 |
oid tname[4 + 4 + 1 + 1]; |
| 535 |
|
| 536 |
DEBUGMSG(("tunnel", "getTunnelByConfigOid(): ")); |
| 537 |
|
| 538 |
for (tunnel = tunnels; tunnel; tunnel = tunnel->next) { |
| 539 |
fillConfigOid(tname, tunnel); |
| 540 |
if (!snmp_oid_compare(tname, 4 + 4 + 1 + 1, |
| 541 |
&name[tunnel_len + 3], |
| 542 |
(*length) - tunnel_len - 3)) { |
| 543 |
if (!tunnel->active) |
| 544 |
break; |
| 545 |
DEBUGMSG(("tunnel", "%s (index=%" NETSNMP_PRIo "u)\n", |
| 546 |
tunnel->ifname, tunnel->ifindex)); |
| 547 |
return tunnel; |
| 548 |
} |
| 549 |
} |
| 550 |
DEBUGMSG(("tunnel", "NONE\n")); |
| 551 |
return NULL; |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
static struct tunnel * |
| 557 |
getNextTunnelByConfigOid(oid * name, size_t * length) |
| 558 |
{ |
| 559 |
struct tunnel *tunnel, *last_tunnel; |
| 560 |
oid tname[10], last_tname[10]; |
| 561 |
|
| 562 |
DEBUGMSG(("tunnel", "getNextTunnelByConfigOid(")); |
| 563 |
DEBUGMSGOID(("tunnel", name, *length)); |
| 564 |
DEBUGMSG(("tunnel", "): ")); |
| 565 |
|
| 566 |
last_tunnel = NULL; |
| 567 |
for (tunnel = tunnels; tunnel; tunnel = tunnel->next) { |
| 568 |
if (!tunnel->active) |
| 569 |
continue; |
| 570 |
fillConfigOid(tname, tunnel); |
| 571 |
if (snmp_oid_compare(tname, 10, |
| 572 |
&name[tunnel_len + 3], |
| 573 |
(*length) - tunnel_len - 3) > 0) { |
| 574 |
if (!last_tunnel) { |
| 575 |
last_tunnel = tunnel; |
| 576 |
memcpy((char *) last_tname, (char *) tname, |
| 577 |
10 * sizeof(oid)); |
| 578 |
} else { |
| 579 |
if (snmp_oid_compare(tname, 10, last_tname, 10) < 0) { |
| 580 |
last_tunnel = tunnel; |
| 581 |
memcpy((char *) last_tname, (char *) tname, |
| 582 |
10 * sizeof(oid)); |
| 583 |
} |
| 584 |
} |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
if (last_tunnel) { |
| 589 |
DEBUGMSG(("tunnel", "%s (index=%" NETSNMP_PRIo "u)\n", |
| 590 |
last_tunnel->ifname, last_tunnel->ifindex)); |
| 591 |
} else { |
| 592 |
DEBUGMSG(("tunnel", "NONE\n")); |
| 593 |
} |
| 594 |
|
| 595 |
return last_tunnel; |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
static int |
| 601 |
writeLocalAddress(int action, unsigned char *var_val, |
| 602 |
unsigned char var_val_type, size_t var_val_len, |
| 603 |
unsigned char *statP, oid * name, size_t name_len) |
| 604 |
{ |
| 605 |
static struct tunnel *tunnel; |
| 606 |
struct ip_tunnel_parm *parm; |
| 607 |
|
| 608 |
switch (action) { |
| 609 |
case RESERVE1: |
| 610 |
if (var_val_type != ASN_IPADDRESS) { |
| 611 |
return SNMP_ERR_WRONGTYPE; |
| 612 |
} |
| 613 |
if (var_val_len != 4) { |
| 614 |
return SNMP_ERR_WRONGLENGTH; |
| 615 |
} |
| 616 |
/* FALL THROUGH */ |
| 617 |
case RESERVE2: |
| 618 |
tunnel = getTunnelByIfIndex((int) name[name_len - 1]); |
| 619 |
if (!tunnel) { |
| 620 |
return SNMP_ERR_NOSUCHNAME; |
| 621 |
} |
| 622 |
case FREE: |
| 623 |
break; |
| 624 |
case ACTION: |
| 625 |
break; |
| 626 |
case UNDO: |
| 627 |
break; |
| 628 |
case COMMIT: |
| 629 |
if (!tunnel) { |
| 630 |
return SNMP_ERR_NOSUCHNAME; |
| 631 |
} |
| 632 |
parm = getTunnelParm(tunnel->ifname); |
| 633 |
if (!parm) { |
| 634 |
return SNMP_ERR_NOSUCHNAME; |
| 635 |
} |
| 636 |
parm->iph.saddr = *(unsigned long *) var_val; |
| 637 |
setTunnelParm(tunnel->ifname, parm); |
| 638 |
break; |
| 639 |
} |
| 640 |
|
| 641 |
return SNMP_ERR_NOERROR; |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
static int |
| 647 |
writeRemoteAddress(int action, unsigned char *var_val, |
| 648 |
unsigned char var_val_type, size_t var_val_len, |
| 649 |
unsigned char *statP, oid * name, size_t name_len) |
| 650 |
{ |
| 651 |
static struct tunnel *tunnel; |
| 652 |
struct ip_tunnel_parm *parm; |
| 653 |
|
| 654 |
switch (action) { |
| 655 |
case RESERVE1: |
| 656 |
if (var_val_type != ASN_IPADDRESS) { |
| 657 |
return SNMP_ERR_WRONGTYPE; |
| 658 |
} |
| 659 |
if (var_val_len != 4) { |
| 660 |
return SNMP_ERR_WRONGLENGTH; |
| 661 |
} |
| 662 |
/* FALL THROUGH */ |
| 663 |
case RESERVE2: |
| 664 |
tunnel = getTunnelByIfIndex((int) name[name_len - 1]); |
| 665 |
if (!tunnel) { |
| 666 |
return SNMP_ERR_NOSUCHNAME; |
| 667 |
} |
| 668 |
break; |
| 669 |
case FREE: |
| 670 |
break; |
| 671 |
case ACTION: |
| 672 |
break; |
| 673 |
case UNDO: |
| 674 |
break; |
| 675 |
case COMMIT: |
| 676 |
if (!tunnel) { |
| 677 |
return SNMP_ERR_NOSUCHNAME; |
| 678 |
} |
| 679 |
parm = getTunnelParm(tunnel->ifname); |
| 680 |
if (!parm) { |
| 681 |
return SNMP_ERR_NOSUCHNAME; |
| 682 |
} |
| 683 |
parm->iph.daddr = *(unsigned long *) var_val; |
| 684 |
setTunnelParm(tunnel->ifname, parm); |
| 685 |
break; |
| 686 |
} |
| 687 |
|
| 688 |
return SNMP_ERR_NOERROR; |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
static int |
| 694 |
writeHopLimit(int action, unsigned char *var_val, |
| 695 |
unsigned char var_val_type, size_t var_val_len, |
| 696 |
unsigned char *statP, oid * name, size_t name_len) |
| 697 |
{ |
| 698 |
static struct tunnel *tunnel; |
| 699 |
struct ip_tunnel_parm *parm; |
| 700 |
|
| 701 |
switch (action) { |
| 702 |
case RESERVE1: |
| 703 |
if (var_val_type != ASN_INTEGER) { |
| 704 |
return SNMP_ERR_WRONGTYPE; |
| 705 |
} |
| 706 |
if (var_val_len > sizeof(long)) { |
| 707 |
return SNMP_ERR_WRONGLENGTH; |
| 708 |
} |
| 709 |
/* FALL THROUGH */ |
| 710 |
case RESERVE2: |
| 711 |
tunnel = getTunnelByIfIndex((int) name[name_len - 1]); |
| 712 |
if (!tunnel) { |
| 713 |
return SNMP_ERR_NOSUCHNAME; |
| 714 |
} |
| 715 |
break; |
| 716 |
case FREE: |
| 717 |
break; |
| 718 |
case ACTION: |
| 719 |
break; |
| 720 |
case UNDO: |
| 721 |
break; |
| 722 |
case COMMIT: |
| 723 |
if (!tunnel) { |
| 724 |
return SNMP_ERR_NOSUCHNAME; |
| 725 |
} |
| 726 |
parm = getTunnelParm(tunnel->ifname); |
| 727 |
if (!parm) { |
| 728 |
return SNMP_ERR_NOSUCHNAME; |
| 729 |
} |
| 730 |
parm->iph.ttl = *(long *) var_val; |
| 731 |
setTunnelParm(tunnel->ifname, parm); |
| 732 |
break; |
| 733 |
} |
| 734 |
|
| 735 |
return SNMP_ERR_NOERROR; |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
static int |
| 741 |
writeTOS(int action, unsigned char *var_val, |
| 742 |
unsigned char var_val_type, size_t var_val_len, |
| 743 |
unsigned char *statP, oid * name, size_t name_len) |
| 744 |
{ |
| 745 |
static struct tunnel *tunnel; |
| 746 |
struct ip_tunnel_parm *parm; |
| 747 |
|
| 748 |
switch (action) { |
| 749 |
case RESERVE1: |
| 750 |
if (var_val_type != ASN_INTEGER) { |
| 751 |
return SNMP_ERR_WRONGTYPE; |
| 752 |
} |
| 753 |
if (var_val_len > sizeof(long)) { |
| 754 |
return SNMP_ERR_WRONGLENGTH; |
| 755 |
} |
| 756 |
/* FALL THROUGH */ |
| 757 |
case RESERVE2: |
| 758 |
tunnel = getTunnelByIfIndex((int) name[name_len - 1]); |
| 759 |
if (!tunnel) { |
| 760 |
return SNMP_ERR_NOSUCHNAME; |
| 761 |
} |
| 762 |
break; |
| 763 |
case FREE: |
| 764 |
break; |
| 765 |
case ACTION: |
| 766 |
break; |
| 767 |
case UNDO: |
| 768 |
break; |
| 769 |
case COMMIT: |
| 770 |
if (!tunnel) { |
| 771 |
return SNMP_ERR_NOSUCHNAME; |
| 772 |
} |
| 773 |
parm = getTunnelParm(tunnel->ifname); |
| 774 |
if (!parm) { |
| 775 |
return SNMP_ERR_NOSUCHNAME; |
| 776 |
} |
| 777 |
/* |
| 778 |
* this does not cover all meaningful values: |
| 779 |
*/ |
| 780 |
parm->iph.tos = (*(long *) var_val == -1) ? 1 : *(long *) var_val; |
| 781 |
setTunnelParm(tunnel->ifname, parm); |
| 782 |
break; |
| 783 |
} |
| 784 |
|
| 785 |
return SNMP_ERR_NOERROR; |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
unsigned char * |
| 791 |
var_tunnelIfEntry(struct variable *vp, |
| 792 |
oid * name, size_t * length, |
| 793 |
int exact, size_t * var_len, WriteMethod ** write_method) |
| 794 |
{ |
| 795 |
static unsigned long ret_addr; |
| 796 |
static long ret_int; |
| 797 |
struct tunnel *tunnel; |
| 798 |
|
| 799 |
DEBUGMSGTL(("tunnel", "var_tunnelIfEntry: ")); |
| 800 |
DEBUGMSGOID(("tunnel", name, *length)); |
| 801 |
DEBUGMSG(("tunnel", " %d\n", exact)); |
| 802 |
|
| 803 |
updateTunnels(); |
| 804 |
|
| 805 |
if (exact) { |
| 806 |
if (*length != tunnel_len + 3 + 1) { |
| 807 |
return NULL; |
| 808 |
} |
| 809 |
tunnel = getTunnelByIfIndex((int) name[*length - 1]); |
| 810 |
} else { |
| 811 |
if ((*length) < tunnel_len) { |
| 812 |
memcpy((char *) name, (char *) tunnel_variables_oid, |
| 813 |
tunnel_len * sizeof(oid)); |
| 814 |
} |
| 815 |
if ((*length) < tunnel_len + 1) { |
| 816 |
name[tunnel_len] = 1; |
| 817 |
} |
| 818 |
if ((*length) < tunnel_len + 2) { |
| 819 |
name[tunnel_len + 1] = 1; |
| 820 |
} |
| 821 |
if ((*length) < tunnel_len + 3) { |
| 822 |
name[tunnel_len + 2] = 1; |
| 823 |
} |
| 824 |
if ((*length) < tunnel_len + 4) { |
| 825 |
name[tunnel_len + 3] = 0; |
| 826 |
} |
| 827 |
*length = tunnel_len + 4; |
| 828 |
|
| 829 |
tunnel = getNextTunnelByIfIndex(name[*length - 1]); |
| 830 |
if (!tunnel) { |
| 831 |
/* |
| 832 |
* end of column, continue with first row of next column |
| 833 |
*/ |
| 834 |
tunnel = tunnels; |
| 835 |
name[tunnel_len + 2]++; |
| 836 |
if (name[tunnel_len + 2] > 6) { |
| 837 |
/* |
| 838 |
* there is no next column |
| 839 |
*/ |
| 840 |
return NULL; |
| 841 |
} |
| 842 |
if (!tunnel) { |
| 843 |
/* |
| 844 |
* there is no (next) row |
| 845 |
*/ |
| 846 |
return NULL; |
| 847 |
} |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
if (!tunnel) { |
| 852 |
return NULL; |
| 853 |
} |
| 854 |
|
| 855 |
name[*length - 1] = tunnel->ifindex; |
| 856 |
|
| 857 |
DEBUGMSGTL(("tunnel", "var_tunnelIfEntry: using")); |
| 858 |
DEBUGMSGOID(("tunnel", name, *length)); |
| 859 |
DEBUGMSG(("tunnel", "\n")); |
| 860 |
|
| 861 |
switch (name[tunnel_len + 2]) { |
| 862 |
case 1: /* tunnelIfLocalAddress */ |
| 863 |
ret_addr = tunnel->local; |
| 864 |
*var_len = 4; |
| 865 |
vp->type = ASN_IPADDRESS; |
| 866 |
*write_method = writeLocalAddress; |
| 867 |
return (u_char *) & ret_addr; |
| 868 |
case 2: /* tunnelIfRemoteAddress */ |
| 869 |
ret_addr = tunnel->remote; |
| 870 |
*var_len = 4; |
| 871 |
vp->type = ASN_IPADDRESS; |
| 872 |
*write_method = writeRemoteAddress; |
| 873 |
return (u_char *) & ret_addr; |
| 874 |
case 3: /* tunnelIfEncapsMethod */ |
| 875 |
ret_int = tunnel->encaps; |
| 876 |
*var_len = sizeof(ret_int); |
| 877 |
vp->type = ASN_INTEGER; |
| 878 |
return (u_char *) & ret_int; |
| 879 |
case 4: /* tunnelIfHopLimit */ |
| 880 |
ret_int = tunnel->hoplimit; |
| 881 |
*var_len = sizeof(ret_int); |
| 882 |
vp->type = ASN_INTEGER; |
| 883 |
*write_method = writeHopLimit; |
| 884 |
return (u_char *) & ret_int; |
| 885 |
case 5: /* tunnelIfSecurity */ |
| 886 |
ret_int = tunnel->security; |
| 887 |
*var_len = sizeof(ret_int); |
| 888 |
vp->type = ASN_INTEGER; |
| 889 |
return (u_char *) & ret_int; |
| 890 |
case 6: /* tunnelIfTOS */ |
| 891 |
ret_int = tunnel->tos; |
| 892 |
*var_len = sizeof(ret_int); |
| 893 |
vp->type = ASN_INTEGER; |
| 894 |
*write_method = writeTOS; |
| 895 |
return (u_char *) & ret_int; |
| 896 |
default: |
| 897 |
return NULL; |
| 898 |
} |
| 899 |
|
| 900 |
return NULL; |
| 901 |
} |
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
unsigned char * |
| 906 |
var_tunnelConfigEntry(struct variable *vp, |
| 907 |
oid * name, size_t * length, |
| 908 |
int exact, size_t * var_len, |
| 909 |
WriteMethod ** write_method) |
| 910 |
{ |
| 911 |
static long ret_int; |
| 912 |
struct tunnel *tunnel; |
| 913 |
int i; |
| 914 |
|
| 915 |
DEBUGMSGTL(("tunnel", "var_tunnelConfigEntry: ")); |
| 916 |
DEBUGMSGOID(("tunnel", name, *length)); |
| 917 |
DEBUGMSG(("tunnel", " %d\n", exact)); |
| 918 |
|
| 919 |
updateTunnels(); |
| 920 |
|
| 921 |
if (exact) { |
| 922 |
if (*length != tunnel_len + 3 + 4 + 4 + 1 + 1) { |
| 923 |
return NULL; |
| 924 |
} |
| 925 |
tunnel = getTunnelByConfigOid(name, length); |
| 926 |
} else { |
| 927 |
if (snmp_oid_compare(name, *length, |
| 928 |
tunnel_configEntry_oid, |
| 929 |
tunnel_configEntry_len) < 0) { |
| 930 |
*length = 0; |
| 931 |
} |
| 932 |
if ((*length) < tunnel_len) { |
| 933 |
memcpy((char *) name, (char *) tunnel_variables_oid, |
| 934 |
tunnel_len * sizeof(oid)); |
| 935 |
} |
| 936 |
if ((*length) < tunnel_len + 1) { |
| 937 |
name[tunnel_len] = 2; |
| 938 |
} |
| 939 |
if ((*length) < tunnel_len + 2) { |
| 940 |
name[tunnel_len + 1] = 1; |
| 941 |
} |
| 942 |
if ((*length) < tunnel_len + 3) { |
| 943 |
name[tunnel_len + 2] = 5; |
| 944 |
} |
| 945 |
for (i = MAX(*length, tunnel_len + 3); |
| 946 |
i < tunnel_len + 3 + 4 + 4 + 1 + 1; i++) { |
| 947 |
name[i] = 0; |
| 948 |
} |
| 949 |
*length = tunnel_len + 3 + 4 + 4 + 1 + 1; |
| 950 |
tunnel = getNextTunnelByConfigOid(name, length); |
| 951 |
if (!tunnel) { |
| 952 |
/* |
| 953 |
* end of column, continue with first row of next column |
| 954 |
*/ |
| 955 |
tunnel = tunnels; |
| 956 |
name[tunnel_len + 2]++; |
| 957 |
if (name[tunnel_len + 2] > 6) { |
| 958 |
/* |
| 959 |
* there is no next column |
| 960 |
*/ |
| 961 |
return NULL; |
| 962 |
} |
| 963 |
if (!tunnel) { |
| 964 |
/* |
| 965 |
* there is no (next) row |
| 966 |
*/ |
| 967 |
return NULL; |
| 968 |
} |
| 969 |
} |
| 970 |
} |
| 971 |
|
| 972 |
if (!tunnel) { |
| 973 |
return NULL; |
| 974 |
} |
| 975 |
|
| 976 |
fillConfigOid(&name[tunnel_len + 3], tunnel); |
| 977 |
|
| 978 |
DEBUGMSGTL(("tunnel", "var_tunnelConfigEntry: using ")); |
| 979 |
DEBUGMSGOID(("tunnel", name, *length)); |
| 980 |
DEBUGMSG(("tunnel", "\n")); |
| 981 |
|
| 982 |
switch (name[tunnel_len + 2]) { |
| 983 |
case 5: /* tunnelConfigIfIndex */ |
| 984 |
ret_int = tunnel->ifindex; |
| 985 |
*var_len = sizeof(ret_int); |
| 986 |
vp->type = ASN_INTEGER; |
| 987 |
return (u_char *) & ret_int; |
| 988 |
case 6: /* tunnelConfigStatus */ |
| 989 |
ret_int = 1; /* active */ |
| 990 |
*var_len = sizeof(ret_int); |
| 991 |
vp->type = ASN_INTEGER; |
| 992 |
return (u_char *) & ret_int; |
| 993 |
default: |
| 994 |
return NULL; |
| 995 |
} |
| 996 |
|
| 997 |
return NULL; |
| 998 |
} |