blob: 36fd4ef44d127561c90d1d12dc51c53945749e6a [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#include <net-snmp/net-snmp-config.h>
Joakim Tjernlundfb62a3c2008-05-13 20:03:32 +020025#include <net-snmp/net-snmp-includes.h>
paul718e3742002-12-13 20:15:29 +000026
27#include "if.h"
28#include "log.h"
29#include "prefix.h"
30#include "command.h"
31#include "thread.h"
32#include "smux.h"
33
34#include "bgpd/bgpd.h"
35#include "bgpd/bgp_table.h"
36#include "bgpd/bgp_aspath.h"
37#include "bgpd/bgp_attr.h"
38#include "bgpd/bgp_route.h"
39#include "bgpd/bgp_fsm.h"
Chris Caputo0be8dfb2009-06-02 18:40:07 +010040#include "bgpd/bgp_snmp.h"
paul718e3742002-12-13 20:15:29 +000041
42/* BGP4-MIB described in RFC1657. */
43#define BGP4MIB 1,3,6,1,2,1,15
44
paul020709f2003-04-04 02:44:16 +000045/* BGP TRAP. */
46#define BGPESTABLISHED 1
47#define BGPBACKWARDTRANSITION 2
48
paul718e3742002-12-13 20:15:29 +000049/* BGP MIB bgpVersion. */
50#define BGPVERSION 0
51
52/* BGP MIB bgpLocalAs. */
53#define BGPLOCALAS 0
54
55/* BGP MIB bgpPeerTable. */
56#define BGPPEERIDENTIFIER 1
57#define BGPPEERSTATE 2
58#define BGPPEERADMINSTATUS 3
59#define BGPPEERNEGOTIATEDVERSION 4
60#define BGPPEERLOCALADDR 5
61#define BGPPEERLOCALPORT 6
62#define BGPPEERREMOTEADDR 7
63#define BGPPEERREMOTEPORT 8
64#define BGPPEERREMOTEAS 9
65#define BGPPEERINUPDATES 10
66#define BGPPEEROUTUPDATES 11
67#define BGPPEERINTOTALMESSAGES 12
68#define BGPPEEROUTTOTALMESSAGES 13
69#define BGPPEERLASTERROR 14
70#define BGPPEERFSMESTABLISHEDTRANSITIONS 15
71#define BGPPEERFSMESTABLISHEDTIME 16
72#define BGPPEERCONNECTRETRYINTERVAL 17
73#define BGPPEERHOLDTIME 18
74#define BGPPEERKEEPALIVE 19
75#define BGPPEERHOLDTIMECONFIGURED 20
76#define BGPPEERKEEPALIVECONFIGURED 21
77#define BGPPEERMINASORIGINATIONINTERVAL 22
78#define BGPPEERMINROUTEADVERTISEMENTINTERVAL 23
79#define BGPPEERINUPDATEELAPSEDTIME 24
80
81/* BGP MIB bgpIdentifier. */
82#define BGPIDENTIFIER 0
83
84/* BGP MIB bgpRcvdPathAttrTable */
85#define BGPPATHATTRPEER 1
86#define BGPPATHATTRDESTNETWORK 2
87#define BGPPATHATTRORIGIN 3
88#define BGPPATHATTRASPATH 4
89#define BGPPATHATTRNEXTHOP 5
90#define BGPPATHATTRINTERASMETRIC 6
91
92/* BGP MIB bgp4PathAttrTable. */
93#define BGP4PATHATTRPEER 1
94#define BGP4PATHATTRIPADDRPREFIXLEN 2
95#define BGP4PATHATTRIPADDRPREFIX 3
96#define BGP4PATHATTRORIGIN 4
97#define BGP4PATHATTRASPATHSEGMENT 5
98#define BGP4PATHATTRNEXTHOP 6
99#define BGP4PATHATTRMULTIEXITDISC 7
100#define BGP4PATHATTRLOCALPREF 8
101#define BGP4PATHATTRATOMICAGGREGATE 9
102#define BGP4PATHATTRAGGREGATORAS 10
103#define BGP4PATHATTRAGGREGATORADDR 11
104#define BGP4PATHATTRCALCLOCALPREF 12
105#define BGP4PATHATTRBEST 13
106#define BGP4PATHATTRUNKNOWN 14
107
108/* SNMP value hack. */
109#define INTEGER ASN_INTEGER
110#define INTEGER32 ASN_INTEGER
111#define COUNTER32 ASN_COUNTER
112#define OCTET_STRING ASN_OCTET_STR
113#define IPADDRESS ASN_IPADDRESS
114#define GAUGE32 ASN_UNSIGNED
115
116/* Declare static local variables for convenience. */
117SNMP_LOCAL_VARIABLES
118
119/* BGP-MIB instances. */
120oid bgp_oid [] = { BGP4MIB };
Vincent Bernatb7c0d062012-05-25 11:17:01 +0200121oid bgp_trap_oid [] = { BGP4MIB, 0 };
paul718e3742002-12-13 20:15:29 +0000122
123/* IP address 0.0.0.0. */
124static struct in_addr bgp_empty_addr = {0};
125
126/* Hook functions. */
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100127static u_char *bgpVersion (struct variable *, oid [], size_t *, int,
128 size_t *, WriteMethod **);
129static u_char *bgpLocalAs (struct variable *, oid [], size_t *,
130 int, size_t *, WriteMethod **);
131static u_char *bgpPeerTable (struct variable *, oid [], size_t *,
132 int, size_t *, WriteMethod **);
133static u_char *bgpRcvdPathAttrTable (struct variable *, oid [], size_t *,
134 int, size_t *, WriteMethod **);
135static u_char *bgpIdentifier (struct variable *, oid [], size_t *,
136 int, size_t *, WriteMethod **);
137static u_char *bgp4PathAttrTable (struct variable *, oid [], size_t *,
138 int, size_t *, WriteMethod **);
paul718e3742002-12-13 20:15:29 +0000139/* static u_char *bgpTraps (); */
140
141struct variable bgp_variables[] =
142{
143 /* BGP version. */
144 {BGPVERSION, OCTET_STRING, RONLY, bgpVersion,
145 1, {1}},
146 /* BGP local AS. */
147 {BGPLOCALAS, INTEGER, RONLY, bgpLocalAs,
148 1, {2}},
149 /* BGP peer table. */
150 {BGPPEERIDENTIFIER, IPADDRESS, RONLY, bgpPeerTable,
151 3, {3, 1, 1}},
152 {BGPPEERSTATE, INTEGER, RONLY, bgpPeerTable,
153 3, {3, 1, 2}},
154 {BGPPEERADMINSTATUS, INTEGER, RWRITE, bgpPeerTable,
155 3, {3, 1, 3}},
156 {BGPPEERNEGOTIATEDVERSION, INTEGER32, RONLY, bgpPeerTable,
157 3, {3, 1, 4}},
158 {BGPPEERLOCALADDR, IPADDRESS, RONLY, bgpPeerTable,
159 3, {3, 1, 5}},
160 {BGPPEERLOCALPORT, INTEGER, RONLY, bgpPeerTable,
161 3, {3, 1, 6}},
162 {BGPPEERREMOTEADDR, IPADDRESS, RONLY, bgpPeerTable,
163 3, {3, 1, 7}},
164 {BGPPEERREMOTEPORT, INTEGER, RONLY, bgpPeerTable,
165 3, {3, 1, 8}},
166 {BGPPEERREMOTEAS, INTEGER, RONLY, bgpPeerTable,
167 3, {3, 1, 9}},
168 {BGPPEERINUPDATES, COUNTER32, RONLY, bgpPeerTable,
169 3, {3, 1, 10}},
170 {BGPPEEROUTUPDATES, COUNTER32, RONLY, bgpPeerTable,
171 3, {3, 1, 11}},
172 {BGPPEERINTOTALMESSAGES, COUNTER32, RONLY, bgpPeerTable,
173 3, {3, 1, 12}},
174 {BGPPEEROUTTOTALMESSAGES, COUNTER32, RONLY, bgpPeerTable,
175 3, {3, 1, 13}},
176 {BGPPEERLASTERROR, OCTET_STRING, RONLY, bgpPeerTable,
177 3, {3, 1, 14}},
178 {BGPPEERFSMESTABLISHEDTRANSITIONS, COUNTER32, RONLY, bgpPeerTable,
179 3, {3, 1, 15}},
180 {BGPPEERFSMESTABLISHEDTIME, GAUGE32, RONLY, bgpPeerTable,
181 3, {3, 1, 16}},
182 {BGPPEERCONNECTRETRYINTERVAL, INTEGER, RWRITE, bgpPeerTable,
183 3, {3, 1, 17}},
184 {BGPPEERHOLDTIME, INTEGER, RONLY, bgpPeerTable,
185 3, {3, 1, 18}},
186 {BGPPEERKEEPALIVE, INTEGER, RONLY, bgpPeerTable,
187 3, {3, 1, 19}},
188 {BGPPEERHOLDTIMECONFIGURED, INTEGER, RWRITE, bgpPeerTable,
189 3, {3, 1, 20}},
190 {BGPPEERKEEPALIVECONFIGURED, INTEGER, RWRITE, bgpPeerTable,
191 3, {3, 1, 21}},
192 {BGPPEERMINASORIGINATIONINTERVAL, INTEGER, RWRITE, bgpPeerTable,
193 3, {3, 1, 22}},
194 {BGPPEERMINROUTEADVERTISEMENTINTERVAL, INTEGER, RWRITE, bgpPeerTable,
195 3, {3, 1, 23}},
196 {BGPPEERINUPDATEELAPSEDTIME, GAUGE32, RONLY, bgpPeerTable,
197 3, {3, 1, 24}},
198 /* BGP identifier. */
199 {BGPIDENTIFIER, IPADDRESS, RONLY, bgpIdentifier,
200 1, {4}},
201 /* BGP received path attribute table. */
202 {BGPPATHATTRPEER, IPADDRESS, RONLY, bgpRcvdPathAttrTable,
203 3, {5, 1, 1}},
204 {BGPPATHATTRDESTNETWORK, IPADDRESS, RONLY, bgpRcvdPathAttrTable,
205 3, {5, 1, 2}},
206 {BGPPATHATTRORIGIN, INTEGER, RONLY, bgpRcvdPathAttrTable,
207 3, {5, 1, 3}},
208 {BGPPATHATTRASPATH, OCTET_STRING, RONLY, bgpRcvdPathAttrTable,
209 3, {5, 1, 4}},
210 {BGPPATHATTRNEXTHOP, IPADDRESS, RONLY, bgpRcvdPathAttrTable,
211 3, {5, 1, 5}},
212 {BGPPATHATTRINTERASMETRIC, INTEGER32, RONLY, bgpRcvdPathAttrTable,
213 3, {5, 1, 6}},
214 /* BGP-4 received path attribute table. */
215 {BGP4PATHATTRPEER, IPADDRESS, RONLY, bgp4PathAttrTable,
216 3, {6, 1, 1}},
217 {BGP4PATHATTRIPADDRPREFIXLEN, INTEGER, RONLY, bgp4PathAttrTable,
218 3, {6, 1, 2}},
219 {BGP4PATHATTRIPADDRPREFIX, IPADDRESS, RONLY, bgp4PathAttrTable,
220 3, {6, 1, 3}},
221 {BGP4PATHATTRORIGIN, INTEGER, RONLY, bgp4PathAttrTable,
222 3, {6, 1, 4}},
223 {BGP4PATHATTRASPATHSEGMENT, OCTET_STRING, RONLY, bgp4PathAttrTable,
224 3, {6, 1, 5}},
225 {BGP4PATHATTRNEXTHOP, IPADDRESS, RONLY, bgp4PathAttrTable,
226 3, {6, 1, 6}},
227 {BGP4PATHATTRMULTIEXITDISC, INTEGER, RONLY, bgp4PathAttrTable,
228 3, {6, 1, 7}},
229 {BGP4PATHATTRLOCALPREF, INTEGER, RONLY, bgp4PathAttrTable,
230 3, {6, 1, 8}},
231 {BGP4PATHATTRATOMICAGGREGATE, INTEGER, RONLY, bgp4PathAttrTable,
232 3, {6, 1, 9}},
233 {BGP4PATHATTRAGGREGATORAS, INTEGER, RONLY, bgp4PathAttrTable,
234 3, {6, 1, 10}},
235 {BGP4PATHATTRAGGREGATORADDR, IPADDRESS, RONLY, bgp4PathAttrTable,
236 3, {6, 1, 11}},
237 {BGP4PATHATTRCALCLOCALPREF, INTEGER, RONLY, bgp4PathAttrTable,
238 3, {6, 1, 12}},
239 {BGP4PATHATTRBEST, INTEGER, RONLY, bgp4PathAttrTable,
240 3, {6, 1, 13}},
241 {BGP4PATHATTRUNKNOWN, OCTET_STRING, RONLY, bgp4PathAttrTable,
242 3, {6, 1, 14}},
243};
pauldd488a72003-06-19 01:21:07 +0000244
paul718e3742002-12-13 20:15:29 +0000245
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
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100282static struct peer *
paul718e3742002-12-13 20:15:29 +0000283peer_lookup_addr_ipv4 (struct in_addr *src)
284{
285 struct bgp *bgp;
286 struct peer *peer;
paul1eb8ef22005-04-07 07:30:20 +0000287 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000288 struct in_addr addr;
289 int ret;
290
291 bgp = bgp_get_default ();
292 if (! bgp)
293 return NULL;
294
paul1eb8ef22005-04-07 07:30:20 +0000295 for (ALL_LIST_ELEMENTS_RO (bgp->peer, node, peer))
paul718e3742002-12-13 20:15:29 +0000296 {
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
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100307static struct peer *
paul718e3742002-12-13 20:15:29 +0000308bgp_peer_lookup_next (struct in_addr *src)
309{
310 struct bgp *bgp;
311 struct peer *peer;
paul1eb8ef22005-04-07 07:30:20 +0000312 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000313 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
paul1eb8ef22005-04-07 07:30:20 +0000323 for (ALL_LIST_ELEMENTS_RO (bgp->peer, node, peer))
paul718e3742002-12-13 20:15:29 +0000324 {
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
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100340static struct peer *
paul718e3742002-12-13 20:15:29 +0000341bgpPeerTable_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. */
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100379static int
paul718e3742002-12-13 20:15:29 +0000380write_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;
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100388 size_t bigsize = SNMP_MAX_LEN;
paul718e3742002-12-13 20:15:29 +0000389
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
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100451static u_char *
paul718e3742002-12-13 20:15:29 +0000452bgpPeerTable (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:
hasso3d515fd2005-02-01 21:30:04 +0000483 return SNMP_INTEGER (BGP_VERSION_4);
paul718e3742002-12-13 20:15:29 +0000484 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
Stephen Hemminger65957882010-01-15 16:22:10 +0300544 return SNMP_INTEGER (bgp_clock () - peer->uptime);
paul718e3742002-12-13 20:15:29 +0000545 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
Stephen Hemminger65957882010-01-15 16:22:10 +0300582 return SNMP_INTEGER (bgp_clock () - peer->update_time);
paul718e3742002-12-13 20:15:29 +0000583 break;
584 default:
585 return NULL;
586 break;
587 }
588 return NULL;
589}
590
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100591static u_char *
paul718e3742002-12-13 20:15:29 +0000592bgpIdentifier (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
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100608static u_char *
paul718e3742002-12-13 20:15:29 +0000609bgpRcvdPathAttrTable (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
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100619static struct bgp_info *
paul718e3742002-12-13 20:15:29 +0000620bgp4PathAttrLookup (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;
paulfd79ac92004-10-13 05:06:08 +0000629 unsigned int len;
paul718e3742002-12-13 20:15:29 +0000630 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
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100756static u_char *
paul718e3742002-12-13 20:15:29 +0000757bgp4PathAttrTable (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 */
paulfe69a502005-09-10 16:55:02 +0000789 return aspath_snmp_pathseg (binfo->attr->aspath, var_len);
paul718e3742002-12-13 20:15:29 +0000790 break;
791 case BGP4PATHATTRNEXTHOP: /* 6 */
792 return SNMP_IPADDRESS (binfo->attr->nexthop);
793 break;
794 case BGP4PATHATTRMULTIEXITDISC: /* 7 */
795 return SNMP_INTEGER (binfo->attr->med);
796 break;
797 case BGP4PATHATTRLOCALPREF: /* 8 */
798 return SNMP_INTEGER (binfo->attr->local_pref);
799 break;
800 case BGP4PATHATTRATOMICAGGREGATE: /* 9 */
801 return SNMP_INTEGER (1);
802 break;
803 case BGP4PATHATTRAGGREGATORAS: /* 10 */
Paul Jakmafb982c22007-05-04 20:15:47 +0000804 if (binfo->attr->extra)
805 return SNMP_INTEGER (binfo->attr->extra->aggregator_as);
806 else
807 return SNMP_INTEGER (0);
paul718e3742002-12-13 20:15:29 +0000808 break;
809 case BGP4PATHATTRAGGREGATORADDR: /* 11 */
Paul Jakmafb982c22007-05-04 20:15:47 +0000810 if (binfo->attr->extra)
811 return SNMP_IPADDRESS (binfo->attr->extra->aggregator_addr);
812 else
813 return SNMP_INTEGER (0);
paul718e3742002-12-13 20:15:29 +0000814 break;
815 case BGP4PATHATTRCALCLOCALPREF: /* 12 */
816 return SNMP_INTEGER (-1);
817 break;
818 case BGP4PATHATTRBEST: /* 13 */
819#define BGP4_PathAttrBest_false 1
820#define BGP4_PathAttrBest_true 2
821 if (CHECK_FLAG (binfo->flags, BGP_INFO_SELECTED))
822 return SNMP_INTEGER (BGP4_PathAttrBest_true);
823 else
824 return SNMP_INTEGER (BGP4_PathAttrBest_false);
825 break;
826 case BGP4PATHATTRUNKNOWN: /* 14 */
827 *var_len = 0;
828 return NULL;
829 break;
830 }
831 return NULL;
832}
833
834/* BGP Traps. */
835struct trap_object bgpTrapList[] =
836{
Vincent Bernatb8cf46b2012-05-25 08:56:44 +0200837 {3, {3, 1, BGPPEERLASTERROR}},
838 {3, {3, 1, BGPPEERSTATE}}
paul718e3742002-12-13 20:15:29 +0000839};
840
841void
842bgpTrapEstablished (struct peer *peer)
843{
844 int ret;
845 struct in_addr addr;
846 oid index[sizeof (oid) * IN_ADDR_SIZE];
847
848 ret = inet_aton (peer->host, &addr);
849 if (ret == 0)
850 return;
851
852 oid_copy_addr (index, &addr, IN_ADDR_SIZE);
853
Vincent Bernatb7c0d062012-05-25 11:17:01 +0200854 smux_trap (bgp_variables, sizeof bgp_variables / sizeof (struct variable),
855 bgp_trap_oid, sizeof bgp_trap_oid / sizeof (oid),
856 bgp_oid, sizeof bgp_oid / sizeof (oid),
paul718e3742002-12-13 20:15:29 +0000857 index, IN_ADDR_SIZE,
858 bgpTrapList, sizeof bgpTrapList / sizeof (struct trap_object),
Vincent Bernat4b89e452012-05-24 21:22:01 +0200859 BGPESTABLISHED);
paul718e3742002-12-13 20:15:29 +0000860}
861
862void
863bgpTrapBackwardTransition (struct peer *peer)
864{
865 int ret;
866 struct in_addr addr;
867 oid index[sizeof (oid) * IN_ADDR_SIZE];
868
869 ret = inet_aton (peer->host, &addr);
870 if (ret == 0)
871 return;
872
873 oid_copy_addr (index, &addr, IN_ADDR_SIZE);
874
Vincent Bernatb7c0d062012-05-25 11:17:01 +0200875 smux_trap (bgp_variables, sizeof bgp_variables / sizeof (struct variable),
876 bgp_trap_oid, sizeof bgp_trap_oid / sizeof (oid),
877 bgp_oid, sizeof bgp_oid / sizeof (oid),
paul718e3742002-12-13 20:15:29 +0000878 index, IN_ADDR_SIZE,
879 bgpTrapList, sizeof bgpTrapList / sizeof (struct trap_object),
Vincent Bernat4b89e452012-05-24 21:22:01 +0200880 BGPBACKWARDTRANSITION);
paul718e3742002-12-13 20:15:29 +0000881}
882
883void
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800884bgp_snmp_init (void)
paul718e3742002-12-13 20:15:29 +0000885{
hassoc75105a2004-10-13 10:33:26 +0000886 smux_init (bm->master);
paul718e3742002-12-13 20:15:29 +0000887 REGISTER_MIB("mibII/bgp", bgp_variables, variable, bgp_oid);
paul718e3742002-12-13 20:15:29 +0000888}
889#endif /* HAVE_SNMP */