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