paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* BGP4 SNMP support |
| 2 | Copyright (C) 1999, 2000 Kunihiro Ishiguro |
| 3 | |
| 4 | This file is part of GNU Zebra. |
| 5 | |
| 6 | GNU Zebra is free software; you can redistribute it and/or modify it |
| 7 | under the terms of the GNU General Public License as published by the |
| 8 | Free Software Foundation; either version 2, or (at your option) any |
| 9 | later version. |
| 10 | |
| 11 | GNU Zebra is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 18 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 | 02111-1307, USA. */ |
| 20 | |
| 21 | #include <zebra.h> |
| 22 | |
| 23 | #ifdef HAVE_SNMP |
| 24 | #include <asn1.h> |
| 25 | #include <snmp.h> |
| 26 | #include <snmp_impl.h> |
| 27 | |
| 28 | #include "if.h" |
| 29 | #include "log.h" |
| 30 | #include "prefix.h" |
| 31 | #include "command.h" |
| 32 | #include "thread.h" |
| 33 | #include "smux.h" |
| 34 | |
| 35 | #include "bgpd/bgpd.h" |
| 36 | #include "bgpd/bgp_table.h" |
| 37 | #include "bgpd/bgp_aspath.h" |
| 38 | #include "bgpd/bgp_attr.h" |
| 39 | #include "bgpd/bgp_route.h" |
| 40 | #include "bgpd/bgp_fsm.h" |
| 41 | |
| 42 | /* BGP4-MIB described in RFC1657. */ |
| 43 | #define BGP4MIB 1,3,6,1,2,1,15 |
| 44 | |
| 45 | /* Zebra enterprise BGP MIB. This variable is used for register |
| 46 | OSPF MIB to SNMP agent under SMUX protocol. */ |
| 47 | #define BGPDMIB 1,3,6,1,4,1,3317,1,2,2 |
| 48 | |
| 49 | /* BGP MIB bgpVersion. */ |
| 50 | #define BGPVERSION 0 |
| 51 | |
| 52 | /* BGP MIB bgpLocalAs. */ |
| 53 | #define BGPLOCALAS 0 |
| 54 | |
| 55 | /* BGP MIB bgpPeerTable. */ |
| 56 | #define BGPPEERIDENTIFIER 1 |
| 57 | #define BGPPEERSTATE 2 |
| 58 | #define BGPPEERADMINSTATUS 3 |
| 59 | #define BGPPEERNEGOTIATEDVERSION 4 |
| 60 | #define BGPPEERLOCALADDR 5 |
| 61 | #define BGPPEERLOCALPORT 6 |
| 62 | #define BGPPEERREMOTEADDR 7 |
| 63 | #define BGPPEERREMOTEPORT 8 |
| 64 | #define BGPPEERREMOTEAS 9 |
| 65 | #define BGPPEERINUPDATES 10 |
| 66 | #define BGPPEEROUTUPDATES 11 |
| 67 | #define BGPPEERINTOTALMESSAGES 12 |
| 68 | #define BGPPEEROUTTOTALMESSAGES 13 |
| 69 | #define BGPPEERLASTERROR 14 |
| 70 | #define BGPPEERFSMESTABLISHEDTRANSITIONS 15 |
| 71 | #define BGPPEERFSMESTABLISHEDTIME 16 |
| 72 | #define BGPPEERCONNECTRETRYINTERVAL 17 |
| 73 | #define BGPPEERHOLDTIME 18 |
| 74 | #define BGPPEERKEEPALIVE 19 |
| 75 | #define BGPPEERHOLDTIMECONFIGURED 20 |
| 76 | #define BGPPEERKEEPALIVECONFIGURED 21 |
| 77 | #define BGPPEERMINASORIGINATIONINTERVAL 22 |
| 78 | #define BGPPEERMINROUTEADVERTISEMENTINTERVAL 23 |
| 79 | #define BGPPEERINUPDATEELAPSEDTIME 24 |
| 80 | |
| 81 | /* BGP MIB bgpIdentifier. */ |
| 82 | #define BGPIDENTIFIER 0 |
| 83 | |
| 84 | /* BGP MIB bgpRcvdPathAttrTable */ |
| 85 | #define BGPPATHATTRPEER 1 |
| 86 | #define BGPPATHATTRDESTNETWORK 2 |
| 87 | #define BGPPATHATTRORIGIN 3 |
| 88 | #define BGPPATHATTRASPATH 4 |
| 89 | #define BGPPATHATTRNEXTHOP 5 |
| 90 | #define BGPPATHATTRINTERASMETRIC 6 |
| 91 | |
| 92 | /* BGP MIB bgp4PathAttrTable. */ |
| 93 | #define BGP4PATHATTRPEER 1 |
| 94 | #define BGP4PATHATTRIPADDRPREFIXLEN 2 |
| 95 | #define BGP4PATHATTRIPADDRPREFIX 3 |
| 96 | #define BGP4PATHATTRORIGIN 4 |
| 97 | #define BGP4PATHATTRASPATHSEGMENT 5 |
| 98 | #define BGP4PATHATTRNEXTHOP 6 |
| 99 | #define BGP4PATHATTRMULTIEXITDISC 7 |
| 100 | #define BGP4PATHATTRLOCALPREF 8 |
| 101 | #define BGP4PATHATTRATOMICAGGREGATE 9 |
| 102 | #define BGP4PATHATTRAGGREGATORAS 10 |
| 103 | #define BGP4PATHATTRAGGREGATORADDR 11 |
| 104 | #define BGP4PATHATTRCALCLOCALPREF 12 |
| 105 | #define BGP4PATHATTRBEST 13 |
| 106 | #define BGP4PATHATTRUNKNOWN 14 |
| 107 | |
| 108 | /* SNMP value hack. */ |
| 109 | #define INTEGER ASN_INTEGER |
| 110 | #define INTEGER32 ASN_INTEGER |
| 111 | #define COUNTER32 ASN_COUNTER |
| 112 | #define OCTET_STRING ASN_OCTET_STR |
| 113 | #define IPADDRESS ASN_IPADDRESS |
| 114 | #define GAUGE32 ASN_UNSIGNED |
| 115 | |
| 116 | /* Declare static local variables for convenience. */ |
| 117 | SNMP_LOCAL_VARIABLES |
| 118 | |
| 119 | /* BGP-MIB instances. */ |
| 120 | oid bgp_oid [] = { BGP4MIB }; |
| 121 | oid bgpd_oid [] = { BGPDMIB }; |
| 122 | |
| 123 | /* IP address 0.0.0.0. */ |
| 124 | static struct in_addr bgp_empty_addr = {0}; |
| 125 | |
| 126 | /* Hook functions. */ |
| 127 | static u_char *bgpVersion (); |
| 128 | static u_char *bgpLocalAs (); |
| 129 | static u_char *bgpPeerTable (); |
| 130 | static u_char *bgpRcvdPathAttrTable (); |
| 131 | static u_char *bgpIdentifier (); |
| 132 | static u_char *bgp4PathAttrTable (); |
| 133 | /* static u_char *bgpTraps (); */ |
| 134 | |
| 135 | struct variable bgp_variables[] = |
| 136 | { |
| 137 | /* BGP version. */ |
| 138 | {BGPVERSION, OCTET_STRING, RONLY, bgpVersion, |
| 139 | 1, {1}}, |
| 140 | /* BGP local AS. */ |
| 141 | {BGPLOCALAS, INTEGER, RONLY, bgpLocalAs, |
| 142 | 1, {2}}, |
| 143 | /* BGP peer table. */ |
| 144 | {BGPPEERIDENTIFIER, IPADDRESS, RONLY, bgpPeerTable, |
| 145 | 3, {3, 1, 1}}, |
| 146 | {BGPPEERSTATE, INTEGER, RONLY, bgpPeerTable, |
| 147 | 3, {3, 1, 2}}, |
| 148 | {BGPPEERADMINSTATUS, INTEGER, RWRITE, bgpPeerTable, |
| 149 | 3, {3, 1, 3}}, |
| 150 | {BGPPEERNEGOTIATEDVERSION, INTEGER32, RONLY, bgpPeerTable, |
| 151 | 3, {3, 1, 4}}, |
| 152 | {BGPPEERLOCALADDR, IPADDRESS, RONLY, bgpPeerTable, |
| 153 | 3, {3, 1, 5}}, |
| 154 | {BGPPEERLOCALPORT, INTEGER, RONLY, bgpPeerTable, |
| 155 | 3, {3, 1, 6}}, |
| 156 | {BGPPEERREMOTEADDR, IPADDRESS, RONLY, bgpPeerTable, |
| 157 | 3, {3, 1, 7}}, |
| 158 | {BGPPEERREMOTEPORT, INTEGER, RONLY, bgpPeerTable, |
| 159 | 3, {3, 1, 8}}, |
| 160 | {BGPPEERREMOTEAS, INTEGER, RONLY, bgpPeerTable, |
| 161 | 3, {3, 1, 9}}, |
| 162 | {BGPPEERINUPDATES, COUNTER32, RONLY, bgpPeerTable, |
| 163 | 3, {3, 1, 10}}, |
| 164 | {BGPPEEROUTUPDATES, COUNTER32, RONLY, bgpPeerTable, |
| 165 | 3, {3, 1, 11}}, |
| 166 | {BGPPEERINTOTALMESSAGES, COUNTER32, RONLY, bgpPeerTable, |
| 167 | 3, {3, 1, 12}}, |
| 168 | {BGPPEEROUTTOTALMESSAGES, COUNTER32, RONLY, bgpPeerTable, |
| 169 | 3, {3, 1, 13}}, |
| 170 | {BGPPEERLASTERROR, OCTET_STRING, RONLY, bgpPeerTable, |
| 171 | 3, {3, 1, 14}}, |
| 172 | {BGPPEERFSMESTABLISHEDTRANSITIONS, COUNTER32, RONLY, bgpPeerTable, |
| 173 | 3, {3, 1, 15}}, |
| 174 | {BGPPEERFSMESTABLISHEDTIME, GAUGE32, RONLY, bgpPeerTable, |
| 175 | 3, {3, 1, 16}}, |
| 176 | {BGPPEERCONNECTRETRYINTERVAL, INTEGER, RWRITE, bgpPeerTable, |
| 177 | 3, {3, 1, 17}}, |
| 178 | {BGPPEERHOLDTIME, INTEGER, RONLY, bgpPeerTable, |
| 179 | 3, {3, 1, 18}}, |
| 180 | {BGPPEERKEEPALIVE, INTEGER, RONLY, bgpPeerTable, |
| 181 | 3, {3, 1, 19}}, |
| 182 | {BGPPEERHOLDTIMECONFIGURED, INTEGER, RWRITE, bgpPeerTable, |
| 183 | 3, {3, 1, 20}}, |
| 184 | {BGPPEERKEEPALIVECONFIGURED, INTEGER, RWRITE, bgpPeerTable, |
| 185 | 3, {3, 1, 21}}, |
| 186 | {BGPPEERMINASORIGINATIONINTERVAL, INTEGER, RWRITE, bgpPeerTable, |
| 187 | 3, {3, 1, 22}}, |
| 188 | {BGPPEERMINROUTEADVERTISEMENTINTERVAL, INTEGER, RWRITE, bgpPeerTable, |
| 189 | 3, {3, 1, 23}}, |
| 190 | {BGPPEERINUPDATEELAPSEDTIME, GAUGE32, RONLY, bgpPeerTable, |
| 191 | 3, {3, 1, 24}}, |
| 192 | /* BGP identifier. */ |
| 193 | {BGPIDENTIFIER, IPADDRESS, RONLY, bgpIdentifier, |
| 194 | 1, {4}}, |
| 195 | /* BGP received path attribute table. */ |
| 196 | {BGPPATHATTRPEER, IPADDRESS, RONLY, bgpRcvdPathAttrTable, |
| 197 | 3, {5, 1, 1}}, |
| 198 | {BGPPATHATTRDESTNETWORK, IPADDRESS, RONLY, bgpRcvdPathAttrTable, |
| 199 | 3, {5, 1, 2}}, |
| 200 | {BGPPATHATTRORIGIN, INTEGER, RONLY, bgpRcvdPathAttrTable, |
| 201 | 3, {5, 1, 3}}, |
| 202 | {BGPPATHATTRASPATH, OCTET_STRING, RONLY, bgpRcvdPathAttrTable, |
| 203 | 3, {5, 1, 4}}, |
| 204 | {BGPPATHATTRNEXTHOP, IPADDRESS, RONLY, bgpRcvdPathAttrTable, |
| 205 | 3, {5, 1, 5}}, |
| 206 | {BGPPATHATTRINTERASMETRIC, INTEGER32, RONLY, bgpRcvdPathAttrTable, |
| 207 | 3, {5, 1, 6}}, |
| 208 | /* BGP-4 received path attribute table. */ |
| 209 | {BGP4PATHATTRPEER, IPADDRESS, RONLY, bgp4PathAttrTable, |
| 210 | 3, {6, 1, 1}}, |
| 211 | {BGP4PATHATTRIPADDRPREFIXLEN, INTEGER, RONLY, bgp4PathAttrTable, |
| 212 | 3, {6, 1, 2}}, |
| 213 | {BGP4PATHATTRIPADDRPREFIX, IPADDRESS, RONLY, bgp4PathAttrTable, |
| 214 | 3, {6, 1, 3}}, |
| 215 | {BGP4PATHATTRORIGIN, INTEGER, RONLY, bgp4PathAttrTable, |
| 216 | 3, {6, 1, 4}}, |
| 217 | {BGP4PATHATTRASPATHSEGMENT, OCTET_STRING, RONLY, bgp4PathAttrTable, |
| 218 | 3, {6, 1, 5}}, |
| 219 | {BGP4PATHATTRNEXTHOP, IPADDRESS, RONLY, bgp4PathAttrTable, |
| 220 | 3, {6, 1, 6}}, |
| 221 | {BGP4PATHATTRMULTIEXITDISC, INTEGER, RONLY, bgp4PathAttrTable, |
| 222 | 3, {6, 1, 7}}, |
| 223 | {BGP4PATHATTRLOCALPREF, INTEGER, RONLY, bgp4PathAttrTable, |
| 224 | 3, {6, 1, 8}}, |
| 225 | {BGP4PATHATTRATOMICAGGREGATE, INTEGER, RONLY, bgp4PathAttrTable, |
| 226 | 3, {6, 1, 9}}, |
| 227 | {BGP4PATHATTRAGGREGATORAS, INTEGER, RONLY, bgp4PathAttrTable, |
| 228 | 3, {6, 1, 10}}, |
| 229 | {BGP4PATHATTRAGGREGATORADDR, IPADDRESS, RONLY, bgp4PathAttrTable, |
| 230 | 3, {6, 1, 11}}, |
| 231 | {BGP4PATHATTRCALCLOCALPREF, INTEGER, RONLY, bgp4PathAttrTable, |
| 232 | 3, {6, 1, 12}}, |
| 233 | {BGP4PATHATTRBEST, INTEGER, RONLY, bgp4PathAttrTable, |
| 234 | 3, {6, 1, 13}}, |
| 235 | {BGP4PATHATTRUNKNOWN, OCTET_STRING, RONLY, bgp4PathAttrTable, |
| 236 | 3, {6, 1, 14}}, |
| 237 | }; |
| 238 | |
| 239 | static u_char * |
| 240 | bgpVersion (struct variable *v, oid name[], size_t *length, int exact, |
| 241 | size_t *var_len, WriteMethod **write_method) |
| 242 | { |
| 243 | static u_char version; |
| 244 | |
| 245 | if (smux_header_generic(v, name, length, exact, var_len, write_method) |
| 246 | == MATCH_FAILED) |
| 247 | return NULL; |
| 248 | |
| 249 | /* Retrun BGP version. Zebra bgpd only support version 4. */ |
| 250 | version = (0x80 >> (BGP_VERSION_4 - 1)); |
| 251 | |
| 252 | /* Return octet string length 1. */ |
| 253 | *var_len = 1; |
| 254 | return (u_char *)&version; |
| 255 | } |
| 256 | |
| 257 | static u_char * |
| 258 | bgpLocalAs (struct variable *v, oid name[], size_t *length, |
| 259 | int exact, size_t *var_len, WriteMethod **write_method) |
| 260 | { |
| 261 | struct bgp *bgp; |
| 262 | |
| 263 | if (smux_header_generic(v, name, length, exact, var_len, write_method) |
| 264 | == MATCH_FAILED) |
| 265 | return NULL; |
| 266 | |
| 267 | /* Get BGP structure. */ |
| 268 | bgp = bgp_get_default (); |
| 269 | if (! bgp) |
| 270 | return NULL; |
| 271 | |
| 272 | return SNMP_INTEGER (bgp->as); |
| 273 | } |
| 274 | |
| 275 | struct peer * |
| 276 | peer_lookup_addr_ipv4 (struct in_addr *src) |
| 277 | { |
| 278 | struct bgp *bgp; |
| 279 | struct peer *peer; |
| 280 | struct listnode *nn; |
| 281 | struct in_addr addr; |
| 282 | int ret; |
| 283 | |
| 284 | bgp = bgp_get_default (); |
| 285 | if (! bgp) |
| 286 | return NULL; |
| 287 | |
| 288 | LIST_LOOP (bgp->peer, peer, nn) |
| 289 | { |
| 290 | ret = inet_pton (AF_INET, peer->host, &addr); |
| 291 | if (ret > 0) |
| 292 | { |
| 293 | if (IPV4_ADDR_SAME (&addr, src)) |
| 294 | return peer; |
| 295 | } |
| 296 | } |
| 297 | return NULL; |
| 298 | } |
| 299 | |
| 300 | struct peer * |
| 301 | bgp_peer_lookup_next (struct in_addr *src) |
| 302 | { |
| 303 | struct bgp *bgp; |
| 304 | struct peer *peer; |
| 305 | struct listnode *nn; |
| 306 | struct in_addr *p; |
| 307 | union sockunion su; |
| 308 | int ret; |
| 309 | |
| 310 | memset (&su, 0, sizeof (union sockunion)); |
| 311 | |
| 312 | bgp = bgp_get_default (); |
| 313 | if (! bgp) |
| 314 | return NULL; |
| 315 | |
| 316 | LIST_LOOP (bgp->peer, peer, nn) |
| 317 | { |
| 318 | ret = inet_pton (AF_INET, peer->host, &su.sin.sin_addr); |
| 319 | if (ret > 0) |
| 320 | { |
| 321 | p = &su.sin.sin_addr; |
| 322 | |
| 323 | if (ntohl (p->s_addr) > ntohl (src->s_addr)) |
| 324 | { |
| 325 | src->s_addr = p->s_addr; |
| 326 | return peer; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | return NULL; |
| 331 | } |
| 332 | |
| 333 | struct peer * |
| 334 | bgpPeerTable_lookup (struct variable *v, oid name[], size_t *length, |
| 335 | struct in_addr *addr, int exact) |
| 336 | { |
| 337 | struct peer *peer = NULL; |
| 338 | int len; |
| 339 | |
| 340 | if (exact) |
| 341 | { |
| 342 | /* Check the length. */ |
| 343 | if (*length - v->namelen != sizeof (struct in_addr)) |
| 344 | return NULL; |
| 345 | |
| 346 | oid2in_addr (name + v->namelen, IN_ADDR_SIZE, addr); |
| 347 | |
| 348 | peer = peer_lookup_addr_ipv4 (addr); |
| 349 | return peer; |
| 350 | } |
| 351 | else |
| 352 | { |
| 353 | len = *length - v->namelen; |
| 354 | if (len > 4) len = 4; |
| 355 | |
| 356 | oid2in_addr (name + v->namelen, len, addr); |
| 357 | |
| 358 | peer = bgp_peer_lookup_next (addr); |
| 359 | |
| 360 | if (peer == NULL) |
| 361 | return NULL; |
| 362 | |
| 363 | oid_copy_addr (name + v->namelen, addr, sizeof (struct in_addr)); |
| 364 | *length = sizeof (struct in_addr) + v->namelen; |
| 365 | |
| 366 | return peer; |
| 367 | } |
| 368 | return NULL; |
| 369 | } |
| 370 | |
| 371 | /* BGP write methods. */ |
| 372 | int |
| 373 | write_bgpPeerTable (int action, u_char *var_val, |
| 374 | u_char var_val_type, size_t var_val_len, |
| 375 | u_char *statP, oid *name, size_t length, |
| 376 | struct variable *v) |
| 377 | { |
| 378 | struct in_addr addr; |
| 379 | struct peer *peer; |
| 380 | long intval; |
| 381 | int bigsize = SNMP_MAX_LEN; |
| 382 | |
| 383 | if (var_val_type != ASN_INTEGER) |
| 384 | { |
| 385 | return SNMP_ERR_WRONGTYPE; |
| 386 | } |
| 387 | if (var_val_len != sizeof (long)) |
| 388 | { |
| 389 | return SNMP_ERR_WRONGLENGTH; |
| 390 | } |
| 391 | |
| 392 | if (! asn_parse_int(var_val, &bigsize, &var_val_type, |
| 393 | &intval, sizeof(long))) |
| 394 | { |
| 395 | return SNMP_ERR_WRONGENCODING; |
| 396 | } |
| 397 | |
| 398 | memset (&addr, 0, sizeof (struct in_addr)); |
| 399 | |
| 400 | peer = bgpPeerTable_lookup (v, name, &length, &addr, 1); |
| 401 | if (! peer) |
| 402 | return SNMP_ERR_NOSUCHNAME; |
| 403 | |
| 404 | printf ("val: %ld\n", intval); |
| 405 | |
| 406 | switch (v->magic) |
| 407 | { |
| 408 | case BGPPEERADMINSTATUS: |
| 409 | #define BGP_PeerAdmin_stop 1 |
| 410 | #define BGP_PeerAdmin_start 2 |
| 411 | /* When the peer is established, */ |
| 412 | if (intval == BGP_PeerAdmin_stop) |
| 413 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 414 | else if (intval == BGP_PeerAdmin_start) |
| 415 | ; /* Do nothing. */ |
| 416 | else |
| 417 | return SNMP_ERR_NOSUCHNAME; |
| 418 | break; |
| 419 | case BGPPEERCONNECTRETRYINTERVAL: |
| 420 | SET_FLAG (peer->config, PEER_CONFIG_CONNECT); |
| 421 | peer->connect = intval; |
| 422 | peer->v_connect = intval; |
| 423 | break; |
| 424 | case BGPPEERHOLDTIMECONFIGURED: |
| 425 | SET_FLAG (peer->config, PEER_CONFIG_TIMER); |
| 426 | peer->holdtime = intval; |
| 427 | peer->v_holdtime = intval; |
| 428 | break; |
| 429 | case BGPPEERKEEPALIVECONFIGURED: |
| 430 | SET_FLAG (peer->config, PEER_CONFIG_TIMER); |
| 431 | peer->keepalive = intval; |
| 432 | peer->v_keepalive = intval; |
| 433 | break; |
| 434 | case BGPPEERMINASORIGINATIONINTERVAL: |
| 435 | peer->v_asorig = intval; |
| 436 | break; |
| 437 | case BGPPEERMINROUTEADVERTISEMENTINTERVAL: |
| 438 | peer->v_routeadv = intval; |
| 439 | break; |
| 440 | } |
| 441 | return SNMP_ERR_NOERROR; |
| 442 | } |
| 443 | |
| 444 | u_char * |
| 445 | bgpPeerTable (struct variable *v, oid name[], size_t *length, |
| 446 | int exact, size_t *var_len, WriteMethod **write_method) |
| 447 | { |
| 448 | static struct in_addr addr; |
| 449 | struct peer *peer; |
| 450 | |
| 451 | *write_method = NULL; |
| 452 | memset (&addr, 0, sizeof (struct in_addr)); |
| 453 | |
| 454 | peer = bgpPeerTable_lookup (v, name, length, &addr, exact); |
| 455 | if (! peer) |
| 456 | return NULL; |
| 457 | |
| 458 | switch (v->magic) |
| 459 | { |
| 460 | case BGPPEERIDENTIFIER: |
| 461 | return SNMP_IPADDRESS (peer->remote_id); |
| 462 | break; |
| 463 | case BGPPEERSTATE: |
| 464 | return SNMP_INTEGER (peer->status); |
| 465 | break; |
| 466 | case BGPPEERADMINSTATUS: |
| 467 | *write_method = write_bgpPeerTable; |
| 468 | #define BGP_PeerAdmin_stop 1 |
| 469 | #define BGP_PeerAdmin_start 2 |
| 470 | if (CHECK_FLAG (peer->flags, PEER_FLAG_SHUTDOWN)) |
| 471 | return SNMP_INTEGER (BGP_PeerAdmin_stop); |
| 472 | else |
| 473 | return SNMP_INTEGER (BGP_PeerAdmin_start); |
| 474 | break; |
| 475 | case BGPPEERNEGOTIATEDVERSION: |
| 476 | return SNMP_INTEGER (peer->version); |
| 477 | break; |
| 478 | case BGPPEERLOCALADDR: |
| 479 | if (peer->su_local) |
| 480 | return SNMP_IPADDRESS (peer->su_local->sin.sin_addr); |
| 481 | else |
| 482 | return SNMP_IPADDRESS (bgp_empty_addr); |
| 483 | break; |
| 484 | case BGPPEERLOCALPORT: |
| 485 | if (peer->su_local) |
| 486 | return SNMP_INTEGER (ntohs (peer->su_local->sin.sin_port)); |
| 487 | else |
| 488 | return SNMP_INTEGER (0); |
| 489 | break; |
| 490 | case BGPPEERREMOTEADDR: |
| 491 | if (peer->su_remote) |
| 492 | return SNMP_IPADDRESS (peer->su_remote->sin.sin_addr); |
| 493 | else |
| 494 | return SNMP_IPADDRESS (bgp_empty_addr); |
| 495 | break; |
| 496 | case BGPPEERREMOTEPORT: |
| 497 | if (peer->su_remote) |
| 498 | return SNMP_INTEGER (ntohs (peer->su_remote->sin.sin_port)); |
| 499 | else |
| 500 | return SNMP_INTEGER (0); |
| 501 | break; |
| 502 | case BGPPEERREMOTEAS: |
| 503 | return SNMP_INTEGER (peer->as); |
| 504 | break; |
| 505 | case BGPPEERINUPDATES: |
| 506 | return SNMP_INTEGER (peer->update_in); |
| 507 | break; |
| 508 | case BGPPEEROUTUPDATES: |
| 509 | return SNMP_INTEGER (peer->update_out); |
| 510 | break; |
| 511 | case BGPPEERINTOTALMESSAGES: |
| 512 | return SNMP_INTEGER (peer->open_in + peer->update_in |
| 513 | + peer->keepalive_in + peer->notify_in |
| 514 | + peer->refresh_in + peer->dynamic_cap_in); |
| 515 | break; |
| 516 | case BGPPEEROUTTOTALMESSAGES: |
| 517 | return SNMP_INTEGER (peer->open_out + peer->update_out |
| 518 | + peer->keepalive_out + peer->notify_out |
| 519 | + peer->refresh_out, peer->dynamic_cap_out); |
| 520 | break; |
| 521 | case BGPPEERLASTERROR: |
| 522 | { |
| 523 | static u_char lasterror[2]; |
| 524 | lasterror[0] = peer->notify.code; |
| 525 | lasterror[1] = peer->notify.subcode; |
| 526 | *var_len = 2; |
| 527 | return (u_char *)&lasterror; |
| 528 | } |
| 529 | break; |
| 530 | case BGPPEERFSMESTABLISHEDTRANSITIONS: |
| 531 | return SNMP_INTEGER (peer->established); |
| 532 | break; |
| 533 | case BGPPEERFSMESTABLISHEDTIME: |
| 534 | if (peer->uptime == 0) |
| 535 | return SNMP_INTEGER (0); |
| 536 | else |
| 537 | return SNMP_INTEGER (time (NULL) - peer->uptime); |
| 538 | break; |
| 539 | case BGPPEERCONNECTRETRYINTERVAL: |
| 540 | *write_method = write_bgpPeerTable; |
| 541 | return SNMP_INTEGER (peer->v_connect); |
| 542 | break; |
| 543 | case BGPPEERHOLDTIME: |
| 544 | return SNMP_INTEGER (peer->v_holdtime); |
| 545 | break; |
| 546 | case BGPPEERKEEPALIVE: |
| 547 | return SNMP_INTEGER (peer->v_keepalive); |
| 548 | break; |
| 549 | case BGPPEERHOLDTIMECONFIGURED: |
| 550 | *write_method = write_bgpPeerTable; |
| 551 | if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER)) |
| 552 | return SNMP_INTEGER (peer->holdtime); |
| 553 | else |
| 554 | return SNMP_INTEGER (peer->v_holdtime); |
| 555 | break; |
| 556 | case BGPPEERKEEPALIVECONFIGURED: |
| 557 | *write_method = write_bgpPeerTable; |
| 558 | if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER)) |
| 559 | return SNMP_INTEGER (peer->keepalive); |
| 560 | else |
| 561 | return SNMP_INTEGER (peer->v_keepalive); |
| 562 | break; |
| 563 | case BGPPEERMINASORIGINATIONINTERVAL: |
| 564 | *write_method = write_bgpPeerTable; |
| 565 | return SNMP_INTEGER (peer->v_asorig); |
| 566 | break; |
| 567 | case BGPPEERMINROUTEADVERTISEMENTINTERVAL: |
| 568 | *write_method = write_bgpPeerTable; |
| 569 | return SNMP_INTEGER (peer->v_routeadv); |
| 570 | break; |
| 571 | case BGPPEERINUPDATEELAPSEDTIME: |
| 572 | if (peer->update_time == 0) |
| 573 | return SNMP_INTEGER (0); |
| 574 | else |
| 575 | return SNMP_INTEGER (time (NULL) - peer->update_time); |
| 576 | break; |
| 577 | default: |
| 578 | return NULL; |
| 579 | break; |
| 580 | } |
| 581 | return NULL; |
| 582 | } |
| 583 | |
| 584 | u_char * |
| 585 | bgpIdentifier (struct variable *v, oid name[], size_t *length, |
| 586 | int exact, size_t *var_len, WriteMethod **write_method) |
| 587 | { |
| 588 | struct bgp *bgp; |
| 589 | |
| 590 | if (smux_header_generic(v, name, length, exact, var_len, write_method) |
| 591 | == MATCH_FAILED) |
| 592 | return NULL; |
| 593 | |
| 594 | bgp = bgp_get_default (); |
| 595 | if (!bgp) |
| 596 | return NULL; |
| 597 | |
| 598 | return SNMP_IPADDRESS (bgp->router_id); |
| 599 | } |
| 600 | |
| 601 | u_char * |
| 602 | bgpRcvdPathAttrTable (struct variable *v, oid name[], size_t *length, |
| 603 | int exact, size_t *var_len, WriteMethod **write_method) |
| 604 | { |
| 605 | /* Received Path Attribute Table. This table contains, one entry |
| 606 | per path to a network, path attributes received from all peers |
| 607 | running BGP version 3 or less. This table is obsolete, having |
| 608 | been replaced in functionality with the bgp4PathAttrTable. */ |
| 609 | return NULL; |
| 610 | } |
| 611 | |
| 612 | struct bgp_info * |
| 613 | bgp4PathAttrLookup (struct variable *v, oid name[], size_t *length, |
| 614 | struct bgp *bgp, struct prefix_ipv4 *addr, int exact) |
| 615 | { |
| 616 | oid *offset; |
| 617 | int offsetlen; |
| 618 | struct bgp_info *binfo; |
| 619 | struct bgp_info *min; |
| 620 | struct bgp_node *rn; |
| 621 | union sockunion su; |
| 622 | int len; |
| 623 | struct in_addr paddr; |
| 624 | |
| 625 | #define BGP_PATHATTR_ENTRY_OFFSET \ |
| 626 | (IN_ADDR_SIZE + 1 + IN_ADDR_SIZE) |
| 627 | |
| 628 | if (exact) |
| 629 | { |
| 630 | if (*length - v->namelen != BGP_PATHATTR_ENTRY_OFFSET) |
| 631 | return NULL; |
| 632 | |
| 633 | /* Set OID offset for prefix. */ |
| 634 | offset = name + v->namelen; |
| 635 | oid2in_addr (offset, IN_ADDR_SIZE, &addr->prefix); |
| 636 | offset += IN_ADDR_SIZE; |
| 637 | |
| 638 | /* Prefix length. */ |
| 639 | addr->prefixlen = *offset; |
| 640 | offset++; |
| 641 | |
| 642 | /* Peer address. */ |
| 643 | su.sin.sin_family = AF_INET; |
| 644 | oid2in_addr (offset, IN_ADDR_SIZE, &su.sin.sin_addr); |
| 645 | |
| 646 | /* Lookup node. */ |
| 647 | rn = bgp_node_lookup (bgp->rib[AFI_IP][SAFI_UNICAST], |
| 648 | (struct prefix *) addr); |
| 649 | if (rn) |
| 650 | { |
| 651 | bgp_unlock_node (rn); |
| 652 | |
| 653 | for (binfo = rn->info; binfo; binfo = binfo->next) |
| 654 | if (sockunion_same (&binfo->peer->su, &su)) |
| 655 | return binfo; |
| 656 | } |
| 657 | } |
| 658 | else |
| 659 | { |
| 660 | offset = name + v->namelen; |
| 661 | offsetlen = *length - v->namelen; |
| 662 | len = offsetlen; |
| 663 | |
| 664 | if (offsetlen == 0) |
| 665 | rn = bgp_table_top (bgp->rib[AFI_IP][SAFI_UNICAST]); |
| 666 | else |
| 667 | { |
| 668 | if (len > IN_ADDR_SIZE) |
| 669 | len = IN_ADDR_SIZE; |
| 670 | |
| 671 | oid2in_addr (offset, len, &addr->prefix); |
| 672 | |
| 673 | offset += IN_ADDR_SIZE; |
| 674 | offsetlen -= IN_ADDR_SIZE; |
| 675 | |
| 676 | if (offsetlen > 0) |
| 677 | addr->prefixlen = *offset; |
| 678 | else |
| 679 | addr->prefixlen = len * 8; |
| 680 | |
| 681 | rn = bgp_node_get (bgp->rib[AFI_IP][SAFI_UNICAST], |
| 682 | (struct prefix *) addr); |
| 683 | |
| 684 | offset++; |
| 685 | offsetlen--; |
| 686 | } |
| 687 | |
| 688 | if (offsetlen > 0) |
| 689 | { |
| 690 | len = offsetlen; |
| 691 | if (len > IN_ADDR_SIZE) |
| 692 | len = IN_ADDR_SIZE; |
| 693 | |
| 694 | oid2in_addr (offset, len, &paddr); |
| 695 | } |
| 696 | else |
| 697 | paddr.s_addr = 0; |
| 698 | |
| 699 | if (! rn) |
| 700 | return NULL; |
| 701 | |
| 702 | do |
| 703 | { |
| 704 | min = NULL; |
| 705 | |
| 706 | for (binfo = rn->info; binfo; binfo = binfo->next) |
| 707 | { |
| 708 | if (binfo->peer->su.sin.sin_family == AF_INET |
| 709 | && ntohl (paddr.s_addr) |
| 710 | < ntohl (binfo->peer->su.sin.sin_addr.s_addr)) |
| 711 | { |
| 712 | if (min) |
| 713 | { |
| 714 | if (ntohl (binfo->peer->su.sin.sin_addr.s_addr) |
| 715 | < ntohl (min->peer->su.sin.sin_addr.s_addr)) |
| 716 | min = binfo; |
| 717 | } |
| 718 | else |
| 719 | min = binfo; |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | if (min) |
| 724 | { |
| 725 | *length = v->namelen + BGP_PATHATTR_ENTRY_OFFSET; |
| 726 | |
| 727 | offset = name + v->namelen; |
| 728 | oid_copy_addr (offset, &rn->p.u.prefix4, IN_ADDR_SIZE); |
| 729 | offset += IN_ADDR_SIZE; |
| 730 | *offset = rn->p.prefixlen; |
| 731 | offset++; |
| 732 | oid_copy_addr (offset, &min->peer->su.sin.sin_addr, |
| 733 | IN_ADDR_SIZE); |
| 734 | addr->prefix = rn->p.u.prefix4; |
| 735 | addr->prefixlen = rn->p.prefixlen; |
| 736 | |
| 737 | bgp_unlock_node (rn); |
| 738 | |
| 739 | return min; |
| 740 | } |
| 741 | |
| 742 | paddr.s_addr = 0; |
| 743 | } |
| 744 | while ((rn = bgp_route_next (rn)) != NULL); |
| 745 | } |
| 746 | return NULL; |
| 747 | } |
| 748 | |
| 749 | u_char * |
| 750 | bgp4PathAttrTable (struct variable *v, oid name[], size_t *length, |
| 751 | int exact, size_t *var_len, WriteMethod **write_method) |
| 752 | { |
| 753 | struct bgp *bgp; |
| 754 | struct bgp_info *binfo; |
| 755 | struct prefix_ipv4 addr; |
| 756 | |
| 757 | bgp = bgp_get_default (); |
| 758 | if (! bgp) |
| 759 | return NULL; |
| 760 | |
| 761 | memset (&addr, 0, sizeof (struct prefix_ipv4)); |
| 762 | |
| 763 | binfo = bgp4PathAttrLookup (v, name, length, bgp, &addr, exact); |
| 764 | if (! binfo) |
| 765 | return NULL; |
| 766 | |
| 767 | switch (v->magic) |
| 768 | { |
| 769 | case BGP4PATHATTRPEER: /* 1 */ |
| 770 | return SNMP_IPADDRESS (binfo->peer->su.sin.sin_addr); |
| 771 | break; |
| 772 | case BGP4PATHATTRIPADDRPREFIXLEN: /* 2 */ |
| 773 | return SNMP_INTEGER (addr.prefixlen); |
| 774 | break; |
| 775 | case BGP4PATHATTRIPADDRPREFIX: /* 3 */ |
| 776 | return SNMP_IPADDRESS (addr.prefix); |
| 777 | break; |
| 778 | case BGP4PATHATTRORIGIN: /* 4 */ |
| 779 | return SNMP_INTEGER (binfo->attr->origin); |
| 780 | break; |
| 781 | case BGP4PATHATTRASPATHSEGMENT: /* 5 */ |
| 782 | *var_len = binfo->attr->aspath->length; |
| 783 | return (u_char *) binfo->attr->aspath->data; |
| 784 | break; |
| 785 | case BGP4PATHATTRNEXTHOP: /* 6 */ |
| 786 | return SNMP_IPADDRESS (binfo->attr->nexthop); |
| 787 | break; |
| 788 | case BGP4PATHATTRMULTIEXITDISC: /* 7 */ |
| 789 | return SNMP_INTEGER (binfo->attr->med); |
| 790 | break; |
| 791 | case BGP4PATHATTRLOCALPREF: /* 8 */ |
| 792 | return SNMP_INTEGER (binfo->attr->local_pref); |
| 793 | break; |
| 794 | case BGP4PATHATTRATOMICAGGREGATE: /* 9 */ |
| 795 | return SNMP_INTEGER (1); |
| 796 | break; |
| 797 | case BGP4PATHATTRAGGREGATORAS: /* 10 */ |
| 798 | return SNMP_INTEGER (binfo->attr->aggregator_as); |
| 799 | break; |
| 800 | case BGP4PATHATTRAGGREGATORADDR: /* 11 */ |
| 801 | return SNMP_IPADDRESS (binfo->attr->aggregator_addr); |
| 802 | break; |
| 803 | case BGP4PATHATTRCALCLOCALPREF: /* 12 */ |
| 804 | return SNMP_INTEGER (-1); |
| 805 | break; |
| 806 | case BGP4PATHATTRBEST: /* 13 */ |
| 807 | #define BGP4_PathAttrBest_false 1 |
| 808 | #define BGP4_PathAttrBest_true 2 |
| 809 | if (CHECK_FLAG (binfo->flags, BGP_INFO_SELECTED)) |
| 810 | return SNMP_INTEGER (BGP4_PathAttrBest_true); |
| 811 | else |
| 812 | return SNMP_INTEGER (BGP4_PathAttrBest_false); |
| 813 | break; |
| 814 | case BGP4PATHATTRUNKNOWN: /* 14 */ |
| 815 | *var_len = 0; |
| 816 | return NULL; |
| 817 | break; |
| 818 | } |
| 819 | return NULL; |
| 820 | } |
| 821 | |
| 822 | /* BGP Traps. */ |
| 823 | struct trap_object bgpTrapList[] = |
| 824 | { |
| 825 | {bgpPeerTable, 3, {3, 1, BGPPEERREMOTEADDR}}, |
| 826 | {bgpPeerTable, 3, {3, 1, BGPPEERLASTERROR}}, |
| 827 | {bgpPeerTable, 3, {3, 1, BGPPEERSTATE}} |
| 828 | }; |
| 829 | |
| 830 | void |
| 831 | bgpTrapEstablished (struct peer *peer) |
| 832 | { |
| 833 | int ret; |
| 834 | struct in_addr addr; |
| 835 | oid index[sizeof (oid) * IN_ADDR_SIZE]; |
| 836 | |
| 837 | ret = inet_aton (peer->host, &addr); |
| 838 | if (ret == 0) |
| 839 | return; |
| 840 | |
| 841 | oid_copy_addr (index, &addr, IN_ADDR_SIZE); |
| 842 | |
| 843 | smux_trap (bgp_oid, sizeof bgp_oid / sizeof (oid), |
| 844 | index, IN_ADDR_SIZE, |
| 845 | bgpTrapList, sizeof bgpTrapList / sizeof (struct trap_object), |
| 846 | bm->start_time - time (NULL)); |
| 847 | } |
| 848 | |
| 849 | void |
| 850 | bgpTrapBackwardTransition (struct peer *peer) |
| 851 | { |
| 852 | int ret; |
| 853 | struct in_addr addr; |
| 854 | oid index[sizeof (oid) * IN_ADDR_SIZE]; |
| 855 | |
| 856 | ret = inet_aton (peer->host, &addr); |
| 857 | if (ret == 0) |
| 858 | return; |
| 859 | |
| 860 | oid_copy_addr (index, &addr, IN_ADDR_SIZE); |
| 861 | |
| 862 | smux_trap (bgp_oid, sizeof bgp_oid / sizeof (oid), |
| 863 | index, IN_ADDR_SIZE, |
| 864 | bgpTrapList, sizeof bgpTrapList / sizeof (struct trap_object), |
| 865 | bm->start_time - time (NULL)); |
| 866 | } |
| 867 | |
| 868 | void |
| 869 | bgp_snmp_init () |
| 870 | { |
| 871 | smux_init (bgpd_oid, sizeof bgpd_oid / sizeof (oid)); |
| 872 | REGISTER_MIB("mibII/bgp", bgp_variables, variable, bgp_oid); |
| 873 | smux_start (); |
| 874 | } |
| 875 | #endif /* HAVE_SNMP */ |