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