Bug #362 is fixed now.
[jleu-quagga.git] / bgpd / bgp_snmp.c
blob3d26890ebb3762dcd5936aeefee3e0026158187d
1 /* BGP4 SNMP support
2 Copyright (C) 1999, 2000 Kunihiro Ishiguro
4 This file is part of GNU Zebra.
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.
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.
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. */
21 #include <zebra.h>
23 #ifdef HAVE_SNMP
24 #ifdef HAVE_NETSNMP
25 #include <net-snmp/net-snmp-config.h>
26 #endif
27 #include <asn1.h>
28 #include <snmp.h>
29 #include <snmp_impl.h>
31 #include "if.h"
32 #include "log.h"
33 #include "prefix.h"
34 #include "command.h"
35 #include "thread.h"
36 #include "smux.h"
38 #include "bgpd/bgpd.h"
39 #include "bgpd/bgp_table.h"
40 #include "bgpd/bgp_aspath.h"
41 #include "bgpd/bgp_attr.h"
42 #include "bgpd/bgp_route.h"
43 #include "bgpd/bgp_fsm.h"
45 /* BGP4-MIB described in RFC1657. */
46 #define BGP4MIB 1,3,6,1,2,1,15
48 /* BGP TRAP. */
49 #define BGPESTABLISHED 1
50 #define BGPBACKWARDTRANSITION 2
52 /* BGP MIB bgpVersion. */
53 #define BGPVERSION 0
55 /* BGP MIB bgpLocalAs. */
56 #define BGPLOCALAS 0
58 /* BGP MIB bgpPeerTable. */
59 #define BGPPEERIDENTIFIER 1
60 #define BGPPEERSTATE 2
61 #define BGPPEERADMINSTATUS 3
62 #define BGPPEERNEGOTIATEDVERSION 4
63 #define BGPPEERLOCALADDR 5
64 #define BGPPEERLOCALPORT 6
65 #define BGPPEERREMOTEADDR 7
66 #define BGPPEERREMOTEPORT 8
67 #define BGPPEERREMOTEAS 9
68 #define BGPPEERINUPDATES 10
69 #define BGPPEEROUTUPDATES 11
70 #define BGPPEERINTOTALMESSAGES 12
71 #define BGPPEEROUTTOTALMESSAGES 13
72 #define BGPPEERLASTERROR 14
73 #define BGPPEERFSMESTABLISHEDTRANSITIONS 15
74 #define BGPPEERFSMESTABLISHEDTIME 16
75 #define BGPPEERCONNECTRETRYINTERVAL 17
76 #define BGPPEERHOLDTIME 18
77 #define BGPPEERKEEPALIVE 19
78 #define BGPPEERHOLDTIMECONFIGURED 20
79 #define BGPPEERKEEPALIVECONFIGURED 21
80 #define BGPPEERMINASORIGINATIONINTERVAL 22
81 #define BGPPEERMINROUTEADVERTISEMENTINTERVAL 23
82 #define BGPPEERINUPDATEELAPSEDTIME 24
84 /* BGP MIB bgpIdentifier. */
85 #define BGPIDENTIFIER 0
87 /* BGP MIB bgpRcvdPathAttrTable */
88 #define BGPPATHATTRPEER 1
89 #define BGPPATHATTRDESTNETWORK 2
90 #define BGPPATHATTRORIGIN 3
91 #define BGPPATHATTRASPATH 4
92 #define BGPPATHATTRNEXTHOP 5
93 #define BGPPATHATTRINTERASMETRIC 6
95 /* BGP MIB bgp4PathAttrTable. */
96 #define BGP4PATHATTRPEER 1
97 #define BGP4PATHATTRIPADDRPREFIXLEN 2
98 #define BGP4PATHATTRIPADDRPREFIX 3
99 #define BGP4PATHATTRORIGIN 4
100 #define BGP4PATHATTRASPATHSEGMENT 5
101 #define BGP4PATHATTRNEXTHOP 6
102 #define BGP4PATHATTRMULTIEXITDISC 7
103 #define BGP4PATHATTRLOCALPREF 8
104 #define BGP4PATHATTRATOMICAGGREGATE 9
105 #define BGP4PATHATTRAGGREGATORAS 10
106 #define BGP4PATHATTRAGGREGATORADDR 11
107 #define BGP4PATHATTRCALCLOCALPREF 12
108 #define BGP4PATHATTRBEST 13
109 #define BGP4PATHATTRUNKNOWN 14
111 /* SNMP value hack. */
112 #define INTEGER ASN_INTEGER
113 #define INTEGER32 ASN_INTEGER
114 #define COUNTER32 ASN_COUNTER
115 #define OCTET_STRING ASN_OCTET_STR
116 #define IPADDRESS ASN_IPADDRESS
117 #define GAUGE32 ASN_UNSIGNED
119 /* Declare static local variables for convenience. */
120 SNMP_LOCAL_VARIABLES
122 /* BGP-MIB instances. */
123 oid bgp_oid [] = { BGP4MIB };
125 /* IP address 0.0.0.0. */
126 static struct in_addr bgp_empty_addr = {0};
128 /* Hook functions. */
129 static u_char *bgpVersion ();
130 static u_char *bgpLocalAs ();
131 static u_char *bgpPeerTable ();
132 static u_char *bgpRcvdPathAttrTable ();
133 static u_char *bgpIdentifier ();
134 static u_char *bgp4PathAttrTable ();
135 /* static u_char *bgpTraps (); */
137 struct variable bgp_variables[] =
139 /* BGP version. */
140 {BGPVERSION, OCTET_STRING, RONLY, bgpVersion,
141 1, {1}},
142 /* BGP local AS. */
143 {BGPLOCALAS, INTEGER, RONLY, bgpLocalAs,
144 1, {2}},
145 /* BGP peer table. */
146 {BGPPEERIDENTIFIER, IPADDRESS, RONLY, bgpPeerTable,
147 3, {3, 1, 1}},
148 {BGPPEERSTATE, INTEGER, RONLY, bgpPeerTable,
149 3, {3, 1, 2}},
150 {BGPPEERADMINSTATUS, INTEGER, RWRITE, bgpPeerTable,
151 3, {3, 1, 3}},
152 {BGPPEERNEGOTIATEDVERSION, INTEGER32, RONLY, bgpPeerTable,
153 3, {3, 1, 4}},
154 {BGPPEERLOCALADDR, IPADDRESS, RONLY, bgpPeerTable,
155 3, {3, 1, 5}},
156 {BGPPEERLOCALPORT, INTEGER, RONLY, bgpPeerTable,
157 3, {3, 1, 6}},
158 {BGPPEERREMOTEADDR, IPADDRESS, RONLY, bgpPeerTable,
159 3, {3, 1, 7}},
160 {BGPPEERREMOTEPORT, INTEGER, RONLY, bgpPeerTable,
161 3, {3, 1, 8}},
162 {BGPPEERREMOTEAS, INTEGER, RONLY, bgpPeerTable,
163 3, {3, 1, 9}},
164 {BGPPEERINUPDATES, COUNTER32, RONLY, bgpPeerTable,
165 3, {3, 1, 10}},
166 {BGPPEEROUTUPDATES, COUNTER32, RONLY, bgpPeerTable,
167 3, {3, 1, 11}},
168 {BGPPEERINTOTALMESSAGES, COUNTER32, RONLY, bgpPeerTable,
169 3, {3, 1, 12}},
170 {BGPPEEROUTTOTALMESSAGES, COUNTER32, RONLY, bgpPeerTable,
171 3, {3, 1, 13}},
172 {BGPPEERLASTERROR, OCTET_STRING, RONLY, bgpPeerTable,
173 3, {3, 1, 14}},
174 {BGPPEERFSMESTABLISHEDTRANSITIONS, COUNTER32, RONLY, bgpPeerTable,
175 3, {3, 1, 15}},
176 {BGPPEERFSMESTABLISHEDTIME, GAUGE32, RONLY, bgpPeerTable,
177 3, {3, 1, 16}},
178 {BGPPEERCONNECTRETRYINTERVAL, INTEGER, RWRITE, bgpPeerTable,
179 3, {3, 1, 17}},
180 {BGPPEERHOLDTIME, INTEGER, RONLY, bgpPeerTable,
181 3, {3, 1, 18}},
182 {BGPPEERKEEPALIVE, INTEGER, RONLY, bgpPeerTable,
183 3, {3, 1, 19}},
184 {BGPPEERHOLDTIMECONFIGURED, INTEGER, RWRITE, bgpPeerTable,
185 3, {3, 1, 20}},
186 {BGPPEERKEEPALIVECONFIGURED, INTEGER, RWRITE, bgpPeerTable,
187 3, {3, 1, 21}},
188 {BGPPEERMINASORIGINATIONINTERVAL, INTEGER, RWRITE, bgpPeerTable,
189 3, {3, 1, 22}},
190 {BGPPEERMINROUTEADVERTISEMENTINTERVAL, INTEGER, RWRITE, bgpPeerTable,
191 3, {3, 1, 23}},
192 {BGPPEERINUPDATEELAPSEDTIME, GAUGE32, RONLY, bgpPeerTable,
193 3, {3, 1, 24}},
194 /* BGP identifier. */
195 {BGPIDENTIFIER, IPADDRESS, RONLY, bgpIdentifier,
196 1, {4}},
197 /* BGP received path attribute table. */
198 {BGPPATHATTRPEER, IPADDRESS, RONLY, bgpRcvdPathAttrTable,
199 3, {5, 1, 1}},
200 {BGPPATHATTRDESTNETWORK, IPADDRESS, RONLY, bgpRcvdPathAttrTable,
201 3, {5, 1, 2}},
202 {BGPPATHATTRORIGIN, INTEGER, RONLY, bgpRcvdPathAttrTable,
203 3, {5, 1, 3}},
204 {BGPPATHATTRASPATH, OCTET_STRING, RONLY, bgpRcvdPathAttrTable,
205 3, {5, 1, 4}},
206 {BGPPATHATTRNEXTHOP, IPADDRESS, RONLY, bgpRcvdPathAttrTable,
207 3, {5, 1, 5}},
208 {BGPPATHATTRINTERASMETRIC, INTEGER32, RONLY, bgpRcvdPathAttrTable,
209 3, {5, 1, 6}},
210 /* BGP-4 received path attribute table. */
211 {BGP4PATHATTRPEER, IPADDRESS, RONLY, bgp4PathAttrTable,
212 3, {6, 1, 1}},
213 {BGP4PATHATTRIPADDRPREFIXLEN, INTEGER, RONLY, bgp4PathAttrTable,
214 3, {6, 1, 2}},
215 {BGP4PATHATTRIPADDRPREFIX, IPADDRESS, RONLY, bgp4PathAttrTable,
216 3, {6, 1, 3}},
217 {BGP4PATHATTRORIGIN, INTEGER, RONLY, bgp4PathAttrTable,
218 3, {6, 1, 4}},
219 {BGP4PATHATTRASPATHSEGMENT, OCTET_STRING, RONLY, bgp4PathAttrTable,
220 3, {6, 1, 5}},
221 {BGP4PATHATTRNEXTHOP, IPADDRESS, RONLY, bgp4PathAttrTable,
222 3, {6, 1, 6}},
223 {BGP4PATHATTRMULTIEXITDISC, INTEGER, RONLY, bgp4PathAttrTable,
224 3, {6, 1, 7}},
225 {BGP4PATHATTRLOCALPREF, INTEGER, RONLY, bgp4PathAttrTable,
226 3, {6, 1, 8}},
227 {BGP4PATHATTRATOMICAGGREGATE, INTEGER, RONLY, bgp4PathAttrTable,
228 3, {6, 1, 9}},
229 {BGP4PATHATTRAGGREGATORAS, INTEGER, RONLY, bgp4PathAttrTable,
230 3, {6, 1, 10}},
231 {BGP4PATHATTRAGGREGATORADDR, IPADDRESS, RONLY, bgp4PathAttrTable,
232 3, {6, 1, 11}},
233 {BGP4PATHATTRCALCLOCALPREF, INTEGER, RONLY, bgp4PathAttrTable,
234 3, {6, 1, 12}},
235 {BGP4PATHATTRBEST, INTEGER, RONLY, bgp4PathAttrTable,
236 3, {6, 1, 13}},
237 {BGP4PATHATTRUNKNOWN, OCTET_STRING, RONLY, bgp4PathAttrTable,
238 3, {6, 1, 14}},
242 static u_char *
243 bgpVersion (struct variable *v, oid name[], size_t *length, int exact,
244 size_t *var_len, WriteMethod **write_method)
246 static u_char version;
248 if (smux_header_generic(v, name, length, exact, var_len, write_method)
249 == MATCH_FAILED)
250 return NULL;
252 /* Retrun BGP version. Zebra bgpd only support version 4. */
253 version = (0x80 >> (BGP_VERSION_4 - 1));
255 /* Return octet string length 1. */
256 *var_len = 1;
257 return (u_char *)&version;
260 static u_char *
261 bgpLocalAs (struct variable *v, oid name[], size_t *length,
262 int exact, size_t *var_len, WriteMethod **write_method)
264 struct bgp *bgp;
266 if (smux_header_generic(v, name, length, exact, var_len, write_method)
267 == MATCH_FAILED)
268 return NULL;
270 /* Get BGP structure. */
271 bgp = bgp_get_default ();
272 if (! bgp)
273 return NULL;
275 return SNMP_INTEGER (bgp->as);
278 struct peer *
279 peer_lookup_addr_ipv4 (struct in_addr *src)
281 struct bgp *bgp;
282 struct peer *peer;
283 struct listnode *node;
284 struct in_addr addr;
285 int ret;
287 bgp = bgp_get_default ();
288 if (! bgp)
289 return NULL;
291 for (ALL_LIST_ELEMENTS_RO (bgp->peer, node, peer))
293 ret = inet_pton (AF_INET, peer->host, &addr);
294 if (ret > 0)
296 if (IPV4_ADDR_SAME (&addr, src))
297 return peer;
300 return NULL;
303 struct peer *
304 bgp_peer_lookup_next (struct in_addr *src)
306 struct bgp *bgp;
307 struct peer *peer;
308 struct listnode *node;
309 struct in_addr *p;
310 union sockunion su;
311 int ret;
313 memset (&su, 0, sizeof (union sockunion));
315 bgp = bgp_get_default ();
316 if (! bgp)
317 return NULL;
319 for (ALL_LIST_ELEMENTS_RO (bgp->peer, node, peer))
321 ret = inet_pton (AF_INET, peer->host, &su.sin.sin_addr);
322 if (ret > 0)
324 p = &su.sin.sin_addr;
326 if (ntohl (p->s_addr) > ntohl (src->s_addr))
328 src->s_addr = p->s_addr;
329 return peer;
333 return NULL;
336 struct peer *
337 bgpPeerTable_lookup (struct variable *v, oid name[], size_t *length,
338 struct in_addr *addr, int exact)
340 struct peer *peer = NULL;
341 int len;
343 if (exact)
345 /* Check the length. */
346 if (*length - v->namelen != sizeof (struct in_addr))
347 return NULL;
349 oid2in_addr (name + v->namelen, IN_ADDR_SIZE, addr);
351 peer = peer_lookup_addr_ipv4 (addr);
352 return peer;
354 else
356 len = *length - v->namelen;
357 if (len > 4) len = 4;
359 oid2in_addr (name + v->namelen, len, addr);
361 peer = bgp_peer_lookup_next (addr);
363 if (peer == NULL)
364 return NULL;
366 oid_copy_addr (name + v->namelen, addr, sizeof (struct in_addr));
367 *length = sizeof (struct in_addr) + v->namelen;
369 return peer;
371 return NULL;
374 /* BGP write methods. */
376 write_bgpPeerTable (int action, u_char *var_val,
377 u_char var_val_type, size_t var_val_len,
378 u_char *statP, oid *name, size_t length,
379 struct variable *v)
381 struct in_addr addr;
382 struct peer *peer;
383 long intval;
384 int bigsize = SNMP_MAX_LEN;
386 if (var_val_type != ASN_INTEGER)
388 return SNMP_ERR_WRONGTYPE;
390 if (var_val_len != sizeof (long))
392 return SNMP_ERR_WRONGLENGTH;
395 if (! asn_parse_int(var_val, &bigsize, &var_val_type,
396 &intval, sizeof(long)))
398 return SNMP_ERR_WRONGENCODING;
401 memset (&addr, 0, sizeof (struct in_addr));
403 peer = bgpPeerTable_lookup (v, name, &length, &addr, 1);
404 if (! peer)
405 return SNMP_ERR_NOSUCHNAME;
407 printf ("val: %ld\n", intval);
409 switch (v->magic)
411 case BGPPEERADMINSTATUS:
412 #define BGP_PeerAdmin_stop 1
413 #define BGP_PeerAdmin_start 2
414 /* When the peer is established, */
415 if (intval == BGP_PeerAdmin_stop)
416 BGP_EVENT_ADD (peer, BGP_Stop);
417 else if (intval == BGP_PeerAdmin_start)
418 ; /* Do nothing. */
419 else
420 return SNMP_ERR_NOSUCHNAME;
421 break;
422 case BGPPEERCONNECTRETRYINTERVAL:
423 SET_FLAG (peer->config, PEER_CONFIG_CONNECT);
424 peer->connect = intval;
425 peer->v_connect = intval;
426 break;
427 case BGPPEERHOLDTIMECONFIGURED:
428 SET_FLAG (peer->config, PEER_CONFIG_TIMER);
429 peer->holdtime = intval;
430 peer->v_holdtime = intval;
431 break;
432 case BGPPEERKEEPALIVECONFIGURED:
433 SET_FLAG (peer->config, PEER_CONFIG_TIMER);
434 peer->keepalive = intval;
435 peer->v_keepalive = intval;
436 break;
437 case BGPPEERMINASORIGINATIONINTERVAL:
438 peer->v_asorig = intval;
439 break;
440 case BGPPEERMINROUTEADVERTISEMENTINTERVAL:
441 peer->v_routeadv = intval;
442 break;
444 return SNMP_ERR_NOERROR;
447 u_char *
448 bgpPeerTable (struct variable *v, oid name[], size_t *length,
449 int exact, size_t *var_len, WriteMethod **write_method)
451 static struct in_addr addr;
452 struct peer *peer;
454 *write_method = NULL;
455 memset (&addr, 0, sizeof (struct in_addr));
457 peer = bgpPeerTable_lookup (v, name, length, &addr, exact);
458 if (! peer)
459 return NULL;
461 switch (v->magic)
463 case BGPPEERIDENTIFIER:
464 return SNMP_IPADDRESS (peer->remote_id);
465 break;
466 case BGPPEERSTATE:
467 return SNMP_INTEGER (peer->status);
468 break;
469 case BGPPEERADMINSTATUS:
470 *write_method = write_bgpPeerTable;
471 #define BGP_PeerAdmin_stop 1
472 #define BGP_PeerAdmin_start 2
473 if (CHECK_FLAG (peer->flags, PEER_FLAG_SHUTDOWN))
474 return SNMP_INTEGER (BGP_PeerAdmin_stop);
475 else
476 return SNMP_INTEGER (BGP_PeerAdmin_start);
477 break;
478 case BGPPEERNEGOTIATEDVERSION:
479 return SNMP_INTEGER (BGP_VERSION_4);
480 break;
481 case BGPPEERLOCALADDR:
482 if (peer->su_local)
483 return SNMP_IPADDRESS (peer->su_local->sin.sin_addr);
484 else
485 return SNMP_IPADDRESS (bgp_empty_addr);
486 break;
487 case BGPPEERLOCALPORT:
488 if (peer->su_local)
489 return SNMP_INTEGER (ntohs (peer->su_local->sin.sin_port));
490 else
491 return SNMP_INTEGER (0);
492 break;
493 case BGPPEERREMOTEADDR:
494 if (peer->su_remote)
495 return SNMP_IPADDRESS (peer->su_remote->sin.sin_addr);
496 else
497 return SNMP_IPADDRESS (bgp_empty_addr);
498 break;
499 case BGPPEERREMOTEPORT:
500 if (peer->su_remote)
501 return SNMP_INTEGER (ntohs (peer->su_remote->sin.sin_port));
502 else
503 return SNMP_INTEGER (0);
504 break;
505 case BGPPEERREMOTEAS:
506 return SNMP_INTEGER (peer->as);
507 break;
508 case BGPPEERINUPDATES:
509 return SNMP_INTEGER (peer->update_in);
510 break;
511 case BGPPEEROUTUPDATES:
512 return SNMP_INTEGER (peer->update_out);
513 break;
514 case BGPPEERINTOTALMESSAGES:
515 return SNMP_INTEGER (peer->open_in + peer->update_in
516 + peer->keepalive_in + peer->notify_in
517 + peer->refresh_in + peer->dynamic_cap_in);
518 break;
519 case BGPPEEROUTTOTALMESSAGES:
520 return SNMP_INTEGER (peer->open_out + peer->update_out
521 + peer->keepalive_out + peer->notify_out
522 + peer->refresh_out + peer->dynamic_cap_out);
523 break;
524 case BGPPEERLASTERROR:
526 static u_char lasterror[2];
527 lasterror[0] = peer->notify.code;
528 lasterror[1] = peer->notify.subcode;
529 *var_len = 2;
530 return (u_char *)&lasterror;
532 break;
533 case BGPPEERFSMESTABLISHEDTRANSITIONS:
534 return SNMP_INTEGER (peer->established);
535 break;
536 case BGPPEERFSMESTABLISHEDTIME:
537 if (peer->uptime == 0)
538 return SNMP_INTEGER (0);
539 else
540 return SNMP_INTEGER (time (NULL) - peer->uptime);
541 break;
542 case BGPPEERCONNECTRETRYINTERVAL:
543 *write_method = write_bgpPeerTable;
544 return SNMP_INTEGER (peer->v_connect);
545 break;
546 case BGPPEERHOLDTIME:
547 return SNMP_INTEGER (peer->v_holdtime);
548 break;
549 case BGPPEERKEEPALIVE:
550 return SNMP_INTEGER (peer->v_keepalive);
551 break;
552 case BGPPEERHOLDTIMECONFIGURED:
553 *write_method = write_bgpPeerTable;
554 if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
555 return SNMP_INTEGER (peer->holdtime);
556 else
557 return SNMP_INTEGER (peer->v_holdtime);
558 break;
559 case BGPPEERKEEPALIVECONFIGURED:
560 *write_method = write_bgpPeerTable;
561 if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
562 return SNMP_INTEGER (peer->keepalive);
563 else
564 return SNMP_INTEGER (peer->v_keepalive);
565 break;
566 case BGPPEERMINASORIGINATIONINTERVAL:
567 *write_method = write_bgpPeerTable;
568 return SNMP_INTEGER (peer->v_asorig);
569 break;
570 case BGPPEERMINROUTEADVERTISEMENTINTERVAL:
571 *write_method = write_bgpPeerTable;
572 return SNMP_INTEGER (peer->v_routeadv);
573 break;
574 case BGPPEERINUPDATEELAPSEDTIME:
575 if (peer->update_time == 0)
576 return SNMP_INTEGER (0);
577 else
578 return SNMP_INTEGER (time (NULL) - peer->update_time);
579 break;
580 default:
581 return NULL;
582 break;
584 return NULL;
587 u_char *
588 bgpIdentifier (struct variable *v, oid name[], size_t *length,
589 int exact, size_t *var_len, WriteMethod **write_method)
591 struct bgp *bgp;
593 if (smux_header_generic(v, name, length, exact, var_len, write_method)
594 == MATCH_FAILED)
595 return NULL;
597 bgp = bgp_get_default ();
598 if (!bgp)
599 return NULL;
601 return SNMP_IPADDRESS (bgp->router_id);
604 u_char *
605 bgpRcvdPathAttrTable (struct variable *v, oid name[], size_t *length,
606 int exact, size_t *var_len, WriteMethod **write_method)
608 /* Received Path Attribute Table. This table contains, one entry
609 per path to a network, path attributes received from all peers
610 running BGP version 3 or less. This table is obsolete, having
611 been replaced in functionality with the bgp4PathAttrTable. */
612 return NULL;
615 struct bgp_info *
616 bgp4PathAttrLookup (struct variable *v, oid name[], size_t *length,
617 struct bgp *bgp, struct prefix_ipv4 *addr, int exact)
619 oid *offset;
620 int offsetlen;
621 struct bgp_info *binfo;
622 struct bgp_info *min;
623 struct bgp_node *rn;
624 union sockunion su;
625 unsigned int len;
626 struct in_addr paddr;
628 #define BGP_PATHATTR_ENTRY_OFFSET \
629 (IN_ADDR_SIZE + 1 + IN_ADDR_SIZE)
631 if (exact)
633 if (*length - v->namelen != BGP_PATHATTR_ENTRY_OFFSET)
634 return NULL;
636 /* Set OID offset for prefix. */
637 offset = name + v->namelen;
638 oid2in_addr (offset, IN_ADDR_SIZE, &addr->prefix);
639 offset += IN_ADDR_SIZE;
641 /* Prefix length. */
642 addr->prefixlen = *offset;
643 offset++;
645 /* Peer address. */
646 su.sin.sin_family = AF_INET;
647 oid2in_addr (offset, IN_ADDR_SIZE, &su.sin.sin_addr);
649 /* Lookup node. */
650 rn = bgp_node_lookup (bgp->rib[AFI_IP][SAFI_UNICAST],
651 (struct prefix *) addr);
652 if (rn)
654 bgp_unlock_node (rn);
656 for (binfo = rn->info; binfo; binfo = binfo->next)
657 if (sockunion_same (&binfo->peer->su, &su))
658 return binfo;
661 else
663 offset = name + v->namelen;
664 offsetlen = *length - v->namelen;
665 len = offsetlen;
667 if (offsetlen == 0)
668 rn = bgp_table_top (bgp->rib[AFI_IP][SAFI_UNICAST]);
669 else
671 if (len > IN_ADDR_SIZE)
672 len = IN_ADDR_SIZE;
674 oid2in_addr (offset, len, &addr->prefix);
676 offset += IN_ADDR_SIZE;
677 offsetlen -= IN_ADDR_SIZE;
679 if (offsetlen > 0)
680 addr->prefixlen = *offset;
681 else
682 addr->prefixlen = len * 8;
684 rn = bgp_node_get (bgp->rib[AFI_IP][SAFI_UNICAST],
685 (struct prefix *) addr);
687 offset++;
688 offsetlen--;
691 if (offsetlen > 0)
693 len = offsetlen;
694 if (len > IN_ADDR_SIZE)
695 len = IN_ADDR_SIZE;
697 oid2in_addr (offset, len, &paddr);
699 else
700 paddr.s_addr = 0;
702 if (! rn)
703 return NULL;
707 min = NULL;
709 for (binfo = rn->info; binfo; binfo = binfo->next)
711 if (binfo->peer->su.sin.sin_family == AF_INET
712 && ntohl (paddr.s_addr)
713 < ntohl (binfo->peer->su.sin.sin_addr.s_addr))
715 if (min)
717 if (ntohl (binfo->peer->su.sin.sin_addr.s_addr)
718 < ntohl (min->peer->su.sin.sin_addr.s_addr))
719 min = binfo;
721 else
722 min = binfo;
726 if (min)
728 *length = v->namelen + BGP_PATHATTR_ENTRY_OFFSET;
730 offset = name + v->namelen;
731 oid_copy_addr (offset, &rn->p.u.prefix4, IN_ADDR_SIZE);
732 offset += IN_ADDR_SIZE;
733 *offset = rn->p.prefixlen;
734 offset++;
735 oid_copy_addr (offset, &min->peer->su.sin.sin_addr,
736 IN_ADDR_SIZE);
737 addr->prefix = rn->p.u.prefix4;
738 addr->prefixlen = rn->p.prefixlen;
740 bgp_unlock_node (rn);
742 return min;
745 paddr.s_addr = 0;
747 while ((rn = bgp_route_next (rn)) != NULL);
749 return NULL;
752 u_char *
753 bgp4PathAttrTable (struct variable *v, oid name[], size_t *length,
754 int exact, size_t *var_len, WriteMethod **write_method)
756 struct bgp *bgp;
757 struct bgp_info *binfo;
758 struct prefix_ipv4 addr;
760 bgp = bgp_get_default ();
761 if (! bgp)
762 return NULL;
764 memset (&addr, 0, sizeof (struct prefix_ipv4));
766 binfo = bgp4PathAttrLookup (v, name, length, bgp, &addr, exact);
767 if (! binfo)
768 return NULL;
770 switch (v->magic)
772 case BGP4PATHATTRPEER: /* 1 */
773 return SNMP_IPADDRESS (binfo->peer->su.sin.sin_addr);
774 break;
775 case BGP4PATHATTRIPADDRPREFIXLEN: /* 2 */
776 return SNMP_INTEGER (addr.prefixlen);
777 break;
778 case BGP4PATHATTRIPADDRPREFIX: /* 3 */
779 return SNMP_IPADDRESS (addr.prefix);
780 break;
781 case BGP4PATHATTRORIGIN: /* 4 */
782 return SNMP_INTEGER (binfo->attr->origin);
783 break;
784 case BGP4PATHATTRASPATHSEGMENT: /* 5 */
785 return aspath_snmp_pathseg (binfo->attr->aspath, var_len);
786 break;
787 case BGP4PATHATTRNEXTHOP: /* 6 */
788 return SNMP_IPADDRESS (binfo->attr->nexthop);
789 break;
790 case BGP4PATHATTRMULTIEXITDISC: /* 7 */
791 return SNMP_INTEGER (binfo->attr->med);
792 break;
793 case BGP4PATHATTRLOCALPREF: /* 8 */
794 return SNMP_INTEGER (binfo->attr->local_pref);
795 break;
796 case BGP4PATHATTRATOMICAGGREGATE: /* 9 */
797 return SNMP_INTEGER (1);
798 break;
799 case BGP4PATHATTRAGGREGATORAS: /* 10 */
800 if (binfo->attr->extra)
801 return SNMP_INTEGER (binfo->attr->extra->aggregator_as);
802 else
803 return SNMP_INTEGER (0);
804 break;
805 case BGP4PATHATTRAGGREGATORADDR: /* 11 */
806 if (binfo->attr->extra)
807 return SNMP_IPADDRESS (binfo->attr->extra->aggregator_addr);
808 else
809 return SNMP_INTEGER (0);
810 break;
811 case BGP4PATHATTRCALCLOCALPREF: /* 12 */
812 return SNMP_INTEGER (-1);
813 break;
814 case BGP4PATHATTRBEST: /* 13 */
815 #define BGP4_PathAttrBest_false 1
816 #define BGP4_PathAttrBest_true 2
817 if (CHECK_FLAG (binfo->flags, BGP_INFO_SELECTED))
818 return SNMP_INTEGER (BGP4_PathAttrBest_true);
819 else
820 return SNMP_INTEGER (BGP4_PathAttrBest_false);
821 break;
822 case BGP4PATHATTRUNKNOWN: /* 14 */
823 *var_len = 0;
824 return NULL;
825 break;
827 return NULL;
830 /* BGP Traps. */
831 struct trap_object bgpTrapList[] =
833 {bgpPeerTable, 3, {3, 1, BGPPEERLASTERROR}},
834 {bgpPeerTable, 3, {3, 1, BGPPEERSTATE}}
837 void
838 bgpTrapEstablished (struct peer *peer)
840 int ret;
841 struct in_addr addr;
842 oid index[sizeof (oid) * IN_ADDR_SIZE];
844 ret = inet_aton (peer->host, &addr);
845 if (ret == 0)
846 return;
848 oid_copy_addr (index, &addr, IN_ADDR_SIZE);
850 smux_trap (bgp_oid, sizeof bgp_oid / sizeof (oid),
851 index, IN_ADDR_SIZE,
852 bgpTrapList, sizeof bgpTrapList / sizeof (struct trap_object),
853 bm->start_time - time (NULL), BGPESTABLISHED);
856 void
857 bgpTrapBackwardTransition (struct peer *peer)
859 int ret;
860 struct in_addr addr;
861 oid index[sizeof (oid) * IN_ADDR_SIZE];
863 ret = inet_aton (peer->host, &addr);
864 if (ret == 0)
865 return;
867 oid_copy_addr (index, &addr, IN_ADDR_SIZE);
869 smux_trap (bgp_oid, sizeof bgp_oid / sizeof (oid),
870 index, IN_ADDR_SIZE,
871 bgpTrapList, sizeof bgpTrapList / sizeof (struct trap_object),
872 bm->start_time - time (NULL), BGPBACKWARDTRANSITION);
875 void
876 bgp_snmp_init ()
878 smux_init (bm->master);
879 REGISTER_MIB("mibII/bgp", bgp_variables, variable, bgp_oid);
881 #endif /* HAVE_SNMP */