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