blob: 822036c1813a28bd345906aa62d2367cdad6a804 [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
48/* Zebra enterprise BGP MIB. This variable is used for register
49 OSPF MIB to SNMP agent under SMUX protocol. */
50#define BGPDMIB 1,3,6,1,4,1,3317,1,2,2
51
52/* BGP MIB bgpVersion. */
53#define BGPVERSION 0
54
55/* BGP MIB bgpLocalAs. */
56#define BGPLOCALAS 0
57
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
83
84/* BGP MIB bgpIdentifier. */
85#define BGPIDENTIFIER 0
86
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
94
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
110
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
118
119/* Declare static local variables for convenience. */
120SNMP_LOCAL_VARIABLES
121
122/* BGP-MIB instances. */
123oid bgp_oid [] = { BGP4MIB };
124oid bgpd_oid [] = { BGPDMIB };
125
126/* IP address 0.0.0.0. */
127static struct in_addr bgp_empty_addr = {0};
128
129/* Hook functions. */
130static u_char *bgpVersion ();
131static u_char *bgpLocalAs ();
132static u_char *bgpPeerTable ();
133static u_char *bgpRcvdPathAttrTable ();
134static u_char *bgpIdentifier ();
135static u_char *bgp4PathAttrTable ();
136/* static u_char *bgpTraps (); */
137
138struct variable bgp_variables[] =
139{
140 /* BGP version. */
141 {BGPVERSION, OCTET_STRING, RONLY, bgpVersion,
142 1, {1}},
143 /* BGP local AS. */
144 {BGPLOCALAS, INTEGER, RONLY, bgpLocalAs,
145 1, {2}},
146 /* BGP peer table. */
147 {BGPPEERIDENTIFIER, IPADDRESS, RONLY, bgpPeerTable,
148 3, {3, 1, 1}},
149 {BGPPEERSTATE, INTEGER, RONLY, bgpPeerTable,
150 3, {3, 1, 2}},
151 {BGPPEERADMINSTATUS, INTEGER, RWRITE, bgpPeerTable,
152 3, {3, 1, 3}},
153 {BGPPEERNEGOTIATEDVERSION, INTEGER32, RONLY, bgpPeerTable,
154 3, {3, 1, 4}},
155 {BGPPEERLOCALADDR, IPADDRESS, RONLY, bgpPeerTable,
156 3, {3, 1, 5}},
157 {BGPPEERLOCALPORT, INTEGER, RONLY, bgpPeerTable,
158 3, {3, 1, 6}},
159 {BGPPEERREMOTEADDR, IPADDRESS, RONLY, bgpPeerTable,
160 3, {3, 1, 7}},
161 {BGPPEERREMOTEPORT, INTEGER, RONLY, bgpPeerTable,
162 3, {3, 1, 8}},
163 {BGPPEERREMOTEAS, INTEGER, RONLY, bgpPeerTable,
164 3, {3, 1, 9}},
165 {BGPPEERINUPDATES, COUNTER32, RONLY, bgpPeerTable,
166 3, {3, 1, 10}},
167 {BGPPEEROUTUPDATES, COUNTER32, RONLY, bgpPeerTable,
168 3, {3, 1, 11}},
169 {BGPPEERINTOTALMESSAGES, COUNTER32, RONLY, bgpPeerTable,
170 3, {3, 1, 12}},
171 {BGPPEEROUTTOTALMESSAGES, COUNTER32, RONLY, bgpPeerTable,
172 3, {3, 1, 13}},
173 {BGPPEERLASTERROR, OCTET_STRING, RONLY, bgpPeerTable,
174 3, {3, 1, 14}},
175 {BGPPEERFSMESTABLISHEDTRANSITIONS, COUNTER32, RONLY, bgpPeerTable,
176 3, {3, 1, 15}},
177 {BGPPEERFSMESTABLISHEDTIME, GAUGE32, RONLY, bgpPeerTable,
178 3, {3, 1, 16}},
179 {BGPPEERCONNECTRETRYINTERVAL, INTEGER, RWRITE, bgpPeerTable,
180 3, {3, 1, 17}},
181 {BGPPEERHOLDTIME, INTEGER, RONLY, bgpPeerTable,
182 3, {3, 1, 18}},
183 {BGPPEERKEEPALIVE, INTEGER, RONLY, bgpPeerTable,
184 3, {3, 1, 19}},
185 {BGPPEERHOLDTIMECONFIGURED, INTEGER, RWRITE, bgpPeerTable,
186 3, {3, 1, 20}},
187 {BGPPEERKEEPALIVECONFIGURED, INTEGER, RWRITE, bgpPeerTable,
188 3, {3, 1, 21}},
189 {BGPPEERMINASORIGINATIONINTERVAL, INTEGER, RWRITE, bgpPeerTable,
190 3, {3, 1, 22}},
191 {BGPPEERMINROUTEADVERTISEMENTINTERVAL, INTEGER, RWRITE, bgpPeerTable,
192 3, {3, 1, 23}},
193 {BGPPEERINUPDATEELAPSEDTIME, GAUGE32, RONLY, bgpPeerTable,
194 3, {3, 1, 24}},
195 /* BGP identifier. */
196 {BGPIDENTIFIER, IPADDRESS, RONLY, bgpIdentifier,
197 1, {4}},
198 /* BGP received path attribute table. */
199 {BGPPATHATTRPEER, IPADDRESS, RONLY, bgpRcvdPathAttrTable,
200 3, {5, 1, 1}},
201 {BGPPATHATTRDESTNETWORK, IPADDRESS, RONLY, bgpRcvdPathAttrTable,
202 3, {5, 1, 2}},
203 {BGPPATHATTRORIGIN, INTEGER, RONLY, bgpRcvdPathAttrTable,
204 3, {5, 1, 3}},
205 {BGPPATHATTRASPATH, OCTET_STRING, RONLY, bgpRcvdPathAttrTable,
206 3, {5, 1, 4}},
207 {BGPPATHATTRNEXTHOP, IPADDRESS, RONLY, bgpRcvdPathAttrTable,
208 3, {5, 1, 5}},
209 {BGPPATHATTRINTERASMETRIC, INTEGER32, RONLY, bgpRcvdPathAttrTable,
210 3, {5, 1, 6}},
211 /* BGP-4 received path attribute table. */
212 {BGP4PATHATTRPEER, IPADDRESS, RONLY, bgp4PathAttrTable,
213 3, {6, 1, 1}},
214 {BGP4PATHATTRIPADDRPREFIXLEN, INTEGER, RONLY, bgp4PathAttrTable,
215 3, {6, 1, 2}},
216 {BGP4PATHATTRIPADDRPREFIX, IPADDRESS, RONLY, bgp4PathAttrTable,
217 3, {6, 1, 3}},
218 {BGP4PATHATTRORIGIN, INTEGER, RONLY, bgp4PathAttrTable,
219 3, {6, 1, 4}},
220 {BGP4PATHATTRASPATHSEGMENT, OCTET_STRING, RONLY, bgp4PathAttrTable,
221 3, {6, 1, 5}},
222 {BGP4PATHATTRNEXTHOP, IPADDRESS, RONLY, bgp4PathAttrTable,
223 3, {6, 1, 6}},
224 {BGP4PATHATTRMULTIEXITDISC, INTEGER, RONLY, bgp4PathAttrTable,
225 3, {6, 1, 7}},
226 {BGP4PATHATTRLOCALPREF, INTEGER, RONLY, bgp4PathAttrTable,
227 3, {6, 1, 8}},
228 {BGP4PATHATTRATOMICAGGREGATE, INTEGER, RONLY, bgp4PathAttrTable,
229 3, {6, 1, 9}},
230 {BGP4PATHATTRAGGREGATORAS, INTEGER, RONLY, bgp4PathAttrTable,
231 3, {6, 1, 10}},
232 {BGP4PATHATTRAGGREGATORADDR, IPADDRESS, RONLY, bgp4PathAttrTable,
233 3, {6, 1, 11}},
234 {BGP4PATHATTRCALCLOCALPREF, INTEGER, RONLY, bgp4PathAttrTable,
235 3, {6, 1, 12}},
236 {BGP4PATHATTRBEST, INTEGER, RONLY, bgp4PathAttrTable,
237 3, {6, 1, 13}},
238 {BGP4PATHATTRUNKNOWN, OCTET_STRING, RONLY, bgp4PathAttrTable,
239 3, {6, 1, 14}},
240};
241
242static u_char *
243bgpVersion (struct variable *v, oid name[], size_t *length, int exact,
244 size_t *var_len, WriteMethod **write_method)
245{
246 static u_char version;
247
248 if (smux_header_generic(v, name, length, exact, var_len, write_method)
249 == MATCH_FAILED)
250 return NULL;
251
252 /* Retrun BGP version. Zebra bgpd only support version 4. */
253 version = (0x80 >> (BGP_VERSION_4 - 1));
254
255 /* Return octet string length 1. */
256 *var_len = 1;
257 return (u_char *)&version;
258}
259
260static u_char *
261bgpLocalAs (struct variable *v, oid name[], size_t *length,
262 int exact, size_t *var_len, WriteMethod **write_method)
263{
264 struct bgp *bgp;
265
266 if (smux_header_generic(v, name, length, exact, var_len, write_method)
267 == MATCH_FAILED)
268 return NULL;
269
270 /* Get BGP structure. */
271 bgp = bgp_get_default ();
272 if (! bgp)
273 return NULL;
274
275 return SNMP_INTEGER (bgp->as);
276}
277
278struct peer *
279peer_lookup_addr_ipv4 (struct in_addr *src)
280{
281 struct bgp *bgp;
282 struct peer *peer;
283 struct listnode *nn;
284 struct in_addr addr;
285 int ret;
286
287 bgp = bgp_get_default ();
288 if (! bgp)
289 return NULL;
290
291 LIST_LOOP (bgp->peer, peer, nn)
292 {
293 ret = inet_pton (AF_INET, peer->host, &addr);
294 if (ret > 0)
295 {
296 if (IPV4_ADDR_SAME (&addr, src))
297 return peer;
298 }
299 }
300 return NULL;
301}
302
303struct peer *
304bgp_peer_lookup_next (struct in_addr *src)
305{
306 struct bgp *bgp;
307 struct peer *peer;
308 struct listnode *nn;
309 struct in_addr *p;
310 union sockunion su;
311 int ret;
312
313 memset (&su, 0, sizeof (union sockunion));
314
315 bgp = bgp_get_default ();
316 if (! bgp)
317 return NULL;
318
319 LIST_LOOP (bgp->peer, peer, nn)
320 {
321 ret = inet_pton (AF_INET, peer->host, &su.sin.sin_addr);
322 if (ret > 0)
323 {
324 p = &su.sin.sin_addr;
325
326 if (ntohl (p->s_addr) > ntohl (src->s_addr))
327 {
328 src->s_addr = p->s_addr;
329 return peer;
330 }
331 }
332 }
333 return NULL;
334}
335
336struct peer *
337bgpPeerTable_lookup (struct variable *v, oid name[], size_t *length,
338 struct in_addr *addr, int exact)
339{
340 struct peer *peer = NULL;
341 int len;
342
343 if (exact)
344 {
345 /* Check the length. */
346 if (*length - v->namelen != sizeof (struct in_addr))
347 return NULL;
348
349 oid2in_addr (name + v->namelen, IN_ADDR_SIZE, addr);
350
351 peer = peer_lookup_addr_ipv4 (addr);
352 return peer;
353 }
354 else
355 {
356 len = *length - v->namelen;
357 if (len > 4) len = 4;
358
359 oid2in_addr (name + v->namelen, len, addr);
360
361 peer = bgp_peer_lookup_next (addr);
362
363 if (peer == NULL)
364 return NULL;
365
366 oid_copy_addr (name + v->namelen, addr, sizeof (struct in_addr));
367 *length = sizeof (struct in_addr) + v->namelen;
368
369 return peer;
370 }
371 return NULL;
372}
373
374/* BGP write methods. */
375int
376write_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)
380{
381 struct in_addr addr;
382 struct peer *peer;
383 long intval;
384 int bigsize = SNMP_MAX_LEN;
385
386 if (var_val_type != ASN_INTEGER)
387 {
388 return SNMP_ERR_WRONGTYPE;
389 }
390 if (var_val_len != sizeof (long))
391 {
392 return SNMP_ERR_WRONGLENGTH;
393 }
394
395 if (! asn_parse_int(var_val, &bigsize, &var_val_type,
396 &intval, sizeof(long)))
397 {
398 return SNMP_ERR_WRONGENCODING;
399 }
400
401 memset (&addr, 0, sizeof (struct in_addr));
402
403 peer = bgpPeerTable_lookup (v, name, &length, &addr, 1);
404 if (! peer)
405 return SNMP_ERR_NOSUCHNAME;
406
407 printf ("val: %ld\n", intval);
408
409 switch (v->magic)
410 {
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;
443 }
444 return SNMP_ERR_NOERROR;
445}
446
447u_char *
448bgpPeerTable (struct variable *v, oid name[], size_t *length,
449 int exact, size_t *var_len, WriteMethod **write_method)
450{
451 static struct in_addr addr;
452 struct peer *peer;
453
454 *write_method = NULL;
455 memset (&addr, 0, sizeof (struct in_addr));
456
457 peer = bgpPeerTable_lookup (v, name, length, &addr, exact);
458 if (! peer)
459 return NULL;
460
461 switch (v->magic)
462 {
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 (peer->version);
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
paul07661cb2003-03-18 00:03:05 +0000522 + peer->refresh_out + peer->dynamic_cap_out);
paul718e3742002-12-13 20:15:29 +0000523 break;
524 case BGPPEERLASTERROR:
525 {
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;
531 }
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;
583 }
584 return NULL;
585}
586
587u_char *
588bgpIdentifier (struct variable *v, oid name[], size_t *length,
589 int exact, size_t *var_len, WriteMethod **write_method)
590{
591 struct bgp *bgp;
592
593 if (smux_header_generic(v, name, length, exact, var_len, write_method)
594 == MATCH_FAILED)
595 return NULL;
596
597 bgp = bgp_get_default ();
598 if (!bgp)
599 return NULL;
600
601 return SNMP_IPADDRESS (bgp->router_id);
602}
603
604u_char *
605bgpRcvdPathAttrTable (struct variable *v, oid name[], size_t *length,
606 int exact, size_t *var_len, WriteMethod **write_method)
607{
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;
613}
614
615struct bgp_info *
616bgp4PathAttrLookup (struct variable *v, oid name[], size_t *length,
617 struct bgp *bgp, struct prefix_ipv4 *addr, int exact)
618{
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 int len;
626 struct in_addr paddr;
627
628#define BGP_PATHATTR_ENTRY_OFFSET \
629 (IN_ADDR_SIZE + 1 + IN_ADDR_SIZE)
630
631 if (exact)
632 {
633 if (*length - v->namelen != BGP_PATHATTR_ENTRY_OFFSET)
634 return NULL;
635
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;
640
641 /* Prefix length. */
642 addr->prefixlen = *offset;
643 offset++;
644
645 /* Peer address. */
646 su.sin.sin_family = AF_INET;
647 oid2in_addr (offset, IN_ADDR_SIZE, &su.sin.sin_addr);
648
649 /* Lookup node. */
650 rn = bgp_node_lookup (bgp->rib[AFI_IP][SAFI_UNICAST],
651 (struct prefix *) addr);
652 if (rn)
653 {
654 bgp_unlock_node (rn);
655
656 for (binfo = rn->info; binfo; binfo = binfo->next)
657 if (sockunion_same (&binfo->peer->su, &su))
658 return binfo;
659 }
660 }
661 else
662 {
663 offset = name + v->namelen;
664 offsetlen = *length - v->namelen;
665 len = offsetlen;
666
667 if (offsetlen == 0)
668 rn = bgp_table_top (bgp->rib[AFI_IP][SAFI_UNICAST]);
669 else
670 {
671 if (len > IN_ADDR_SIZE)
672 len = IN_ADDR_SIZE;
673
674 oid2in_addr (offset, len, &addr->prefix);
675
676 offset += IN_ADDR_SIZE;
677 offsetlen -= IN_ADDR_SIZE;
678
679 if (offsetlen > 0)
680 addr->prefixlen = *offset;
681 else
682 addr->prefixlen = len * 8;
683
684 rn = bgp_node_get (bgp->rib[AFI_IP][SAFI_UNICAST],
685 (struct prefix *) addr);
686
687 offset++;
688 offsetlen--;
689 }
690
691 if (offsetlen > 0)
692 {
693 len = offsetlen;
694 if (len > IN_ADDR_SIZE)
695 len = IN_ADDR_SIZE;
696
697 oid2in_addr (offset, len, &paddr);
698 }
699 else
700 paddr.s_addr = 0;
701
702 if (! rn)
703 return NULL;
704
705 do
706 {
707 min = NULL;
708
709 for (binfo = rn->info; binfo; binfo = binfo->next)
710 {
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))
714 {
715 if (min)
716 {
717 if (ntohl (binfo->peer->su.sin.sin_addr.s_addr)
718 < ntohl (min->peer->su.sin.sin_addr.s_addr))
719 min = binfo;
720 }
721 else
722 min = binfo;
723 }
724 }
725
726 if (min)
727 {
728 *length = v->namelen + BGP_PATHATTR_ENTRY_OFFSET;
729
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;
739
740 bgp_unlock_node (rn);
741
742 return min;
743 }
744
745 paddr.s_addr = 0;
746 }
747 while ((rn = bgp_route_next (rn)) != NULL);
748 }
749 return NULL;
750}
751
752u_char *
753bgp4PathAttrTable (struct variable *v, oid name[], size_t *length,
754 int exact, size_t *var_len, WriteMethod **write_method)
755{
756 struct bgp *bgp;
757 struct bgp_info *binfo;
758 struct prefix_ipv4 addr;
759
760 bgp = bgp_get_default ();
761 if (! bgp)
762 return NULL;
763
764 memset (&addr, 0, sizeof (struct prefix_ipv4));
765
766 binfo = bgp4PathAttrLookup (v, name, length, bgp, &addr, exact);
767 if (! binfo)
768 return NULL;
769
770 switch (v->magic)
771 {
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 *var_len = binfo->attr->aspath->length;
786 return (u_char *) binfo->attr->aspath->data;
787 break;
788 case BGP4PATHATTRNEXTHOP: /* 6 */
789 return SNMP_IPADDRESS (binfo->attr->nexthop);
790 break;
791 case BGP4PATHATTRMULTIEXITDISC: /* 7 */
792 return SNMP_INTEGER (binfo->attr->med);
793 break;
794 case BGP4PATHATTRLOCALPREF: /* 8 */
795 return SNMP_INTEGER (binfo->attr->local_pref);
796 break;
797 case BGP4PATHATTRATOMICAGGREGATE: /* 9 */
798 return SNMP_INTEGER (1);
799 break;
800 case BGP4PATHATTRAGGREGATORAS: /* 10 */
801 return SNMP_INTEGER (binfo->attr->aggregator_as);
802 break;
803 case BGP4PATHATTRAGGREGATORADDR: /* 11 */
804 return SNMP_IPADDRESS (binfo->attr->aggregator_addr);
805 break;
806 case BGP4PATHATTRCALCLOCALPREF: /* 12 */
807 return SNMP_INTEGER (-1);
808 break;
809 case BGP4PATHATTRBEST: /* 13 */
810#define BGP4_PathAttrBest_false 1
811#define BGP4_PathAttrBest_true 2
812 if (CHECK_FLAG (binfo->flags, BGP_INFO_SELECTED))
813 return SNMP_INTEGER (BGP4_PathAttrBest_true);
814 else
815 return SNMP_INTEGER (BGP4_PathAttrBest_false);
816 break;
817 case BGP4PATHATTRUNKNOWN: /* 14 */
818 *var_len = 0;
819 return NULL;
820 break;
821 }
822 return NULL;
823}
824
825/* BGP Traps. */
826struct trap_object bgpTrapList[] =
827{
828 {bgpPeerTable, 3, {3, 1, BGPPEERREMOTEADDR}},
829 {bgpPeerTable, 3, {3, 1, BGPPEERLASTERROR}},
830 {bgpPeerTable, 3, {3, 1, BGPPEERSTATE}}
831};
832
833void
834bgpTrapEstablished (struct peer *peer)
835{
836 int ret;
837 struct in_addr addr;
838 oid index[sizeof (oid) * IN_ADDR_SIZE];
839
840 ret = inet_aton (peer->host, &addr);
841 if (ret == 0)
842 return;
843
844 oid_copy_addr (index, &addr, IN_ADDR_SIZE);
845
846 smux_trap (bgp_oid, sizeof bgp_oid / sizeof (oid),
847 index, IN_ADDR_SIZE,
848 bgpTrapList, sizeof bgpTrapList / sizeof (struct trap_object),
849 bm->start_time - time (NULL));
850}
851
852void
853bgpTrapBackwardTransition (struct peer *peer)
854{
855 int ret;
856 struct in_addr addr;
857 oid index[sizeof (oid) * IN_ADDR_SIZE];
858
859 ret = inet_aton (peer->host, &addr);
860 if (ret == 0)
861 return;
862
863 oid_copy_addr (index, &addr, IN_ADDR_SIZE);
864
865 smux_trap (bgp_oid, sizeof bgp_oid / sizeof (oid),
866 index, IN_ADDR_SIZE,
867 bgpTrapList, sizeof bgpTrapList / sizeof (struct trap_object),
868 bm->start_time - time (NULL));
869}
870
871void
872bgp_snmp_init ()
873{
874 smux_init (bgpd_oid, sizeof bgpd_oid / sizeof (oid));
875 REGISTER_MIB("mibII/bgp", bgp_variables, variable, bgp_oid);
876 smux_start ();
877}
878#endif /* HAVE_SNMP */