blob: 0f4aeec8b967d7416f01ad50628bf83e02293b0e [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"
David Lamparter6b0655a2014-06-04 06:53:35 +020041
paul718e3742002-12-13 20:15:29 +000042/* 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
David Lamparter6b0655a2014-06-04 06:53:35 +0200115
paul718e3742002-12-13 20:15:29 +0000116/* 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
David Lamparter6b0655a2014-06-04 06:53:35 +0200245
paul718e3742002-12-13 20:15:29 +0000246static 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
David Lamparterd689d1a2015-03-03 13:54:30 +0100340/* 1.3.6.1.2.1.15.3.1.x = 10 */
341#define PEERTAB_NAMELEN 10
342
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100343static struct peer *
paul718e3742002-12-13 20:15:29 +0000344bgpPeerTable_lookup (struct variable *v, oid name[], size_t *length,
345 struct in_addr *addr, int exact)
346{
347 struct peer *peer = NULL;
David Lamparterd689d1a2015-03-03 13:54:30 +0100348 size_t namelen = v ? v->namelen : PEERTAB_NAMELEN;
paul718e3742002-12-13 20:15:29 +0000349 int len;
350
351 if (exact)
352 {
353 /* Check the length. */
David Lamparterd689d1a2015-03-03 13:54:30 +0100354 if (*length - namelen != sizeof (struct in_addr))
paul718e3742002-12-13 20:15:29 +0000355 return NULL;
356
David Lamparterd689d1a2015-03-03 13:54:30 +0100357 oid2in_addr (name + namelen, IN_ADDR_SIZE, addr);
paul718e3742002-12-13 20:15:29 +0000358
359 peer = peer_lookup_addr_ipv4 (addr);
360 return peer;
361 }
362 else
363 {
David Lamparterd689d1a2015-03-03 13:54:30 +0100364 len = *length - namelen;
paul718e3742002-12-13 20:15:29 +0000365 if (len > 4) len = 4;
366
David Lamparterd689d1a2015-03-03 13:54:30 +0100367 oid2in_addr (name + namelen, len, addr);
paul718e3742002-12-13 20:15:29 +0000368
369 peer = bgp_peer_lookup_next (addr);
370
371 if (peer == NULL)
372 return NULL;
373
David Lamparterd689d1a2015-03-03 13:54:30 +0100374 oid_copy_addr (name + namelen, addr, sizeof (struct in_addr));
375 *length = sizeof (struct in_addr) + namelen;
paul718e3742002-12-13 20:15:29 +0000376
377 return peer;
378 }
379 return NULL;
380}
381
382/* BGP write methods. */
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100383static int
paul718e3742002-12-13 20:15:29 +0000384write_bgpPeerTable (int action, u_char *var_val,
385 u_char var_val_type, size_t var_val_len,
David Lamparterd689d1a2015-03-03 13:54:30 +0100386 u_char *statP, oid *name, size_t length)
paul718e3742002-12-13 20:15:29 +0000387{
388 struct in_addr addr;
389 struct peer *peer;
390 long intval;
David Lamparterd689d1a2015-03-03 13:54:30 +0100391
paul718e3742002-12-13 20:15:29 +0000392 if (var_val_type != ASN_INTEGER)
393 {
394 return SNMP_ERR_WRONGTYPE;
395 }
396 if (var_val_len != sizeof (long))
397 {
398 return SNMP_ERR_WRONGLENGTH;
399 }
400
David Lamparterd689d1a2015-03-03 13:54:30 +0100401 intval = *(long *)var_val;
paul718e3742002-12-13 20:15:29 +0000402
403 memset (&addr, 0, sizeof (struct in_addr));
404
David Lamparterd689d1a2015-03-03 13:54:30 +0100405 peer = bgpPeerTable_lookup (NULL, name, &length, &addr, 1);
paul718e3742002-12-13 20:15:29 +0000406 if (! peer)
407 return SNMP_ERR_NOSUCHNAME;
408
David Lamparterd689d1a2015-03-03 13:54:30 +0100409 if (action != SNMP_MSG_INTERNAL_SET_COMMIT)
410 return SNMP_ERR_NOERROR;
paul718e3742002-12-13 20:15:29 +0000411
David Lamparterd689d1a2015-03-03 13:54:30 +0100412 zlog_info ("%s: SNMP write .%ld = %ld",
413 peer->host, (long)name[PEERTAB_NAMELEN - 1], intval);
414
415 switch (name[PEERTAB_NAMELEN - 1])
paul718e3742002-12-13 20:15:29 +0000416 {
417 case BGPPEERADMINSTATUS:
418#define BGP_PeerAdmin_stop 1
419#define BGP_PeerAdmin_start 2
420 /* When the peer is established, */
421 if (intval == BGP_PeerAdmin_stop)
422 BGP_EVENT_ADD (peer, BGP_Stop);
423 else if (intval == BGP_PeerAdmin_start)
424 ; /* Do nothing. */
425 else
426 return SNMP_ERR_NOSUCHNAME;
427 break;
428 case BGPPEERCONNECTRETRYINTERVAL:
429 SET_FLAG (peer->config, PEER_CONFIG_CONNECT);
430 peer->connect = intval;
431 peer->v_connect = intval;
432 break;
433 case BGPPEERHOLDTIMECONFIGURED:
434 SET_FLAG (peer->config, PEER_CONFIG_TIMER);
435 peer->holdtime = intval;
436 peer->v_holdtime = intval;
437 break;
438 case BGPPEERKEEPALIVECONFIGURED:
439 SET_FLAG (peer->config, PEER_CONFIG_TIMER);
440 peer->keepalive = intval;
441 peer->v_keepalive = intval;
442 break;
443 case BGPPEERMINASORIGINATIONINTERVAL:
444 peer->v_asorig = intval;
445 break;
446 case BGPPEERMINROUTEADVERTISEMENTINTERVAL:
447 peer->v_routeadv = intval;
448 break;
449 }
450 return SNMP_ERR_NOERROR;
451}
452
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100453static u_char *
paul718e3742002-12-13 20:15:29 +0000454bgpPeerTable (struct variable *v, oid name[], size_t *length,
455 int exact, size_t *var_len, WriteMethod **write_method)
456{
457 static struct in_addr addr;
458 struct peer *peer;
459
Vincent Bernat8046ba62012-05-31 13:30:28 +0200460 if (smux_header_table(v, name, length, exact, var_len, write_method)
461 == MATCH_FAILED)
462 return NULL;
paul718e3742002-12-13 20:15:29 +0000463 memset (&addr, 0, sizeof (struct in_addr));
464
465 peer = bgpPeerTable_lookup (v, name, length, &addr, exact);
466 if (! peer)
467 return NULL;
468
469 switch (v->magic)
470 {
471 case BGPPEERIDENTIFIER:
472 return SNMP_IPADDRESS (peer->remote_id);
473 break;
474 case BGPPEERSTATE:
475 return SNMP_INTEGER (peer->status);
476 break;
477 case BGPPEERADMINSTATUS:
478 *write_method = write_bgpPeerTable;
479#define BGP_PeerAdmin_stop 1
480#define BGP_PeerAdmin_start 2
481 if (CHECK_FLAG (peer->flags, PEER_FLAG_SHUTDOWN))
482 return SNMP_INTEGER (BGP_PeerAdmin_stop);
483 else
484 return SNMP_INTEGER (BGP_PeerAdmin_start);
485 break;
486 case BGPPEERNEGOTIATEDVERSION:
hasso3d515fd2005-02-01 21:30:04 +0000487 return SNMP_INTEGER (BGP_VERSION_4);
paul718e3742002-12-13 20:15:29 +0000488 break;
489 case BGPPEERLOCALADDR:
490 if (peer->su_local)
491 return SNMP_IPADDRESS (peer->su_local->sin.sin_addr);
492 else
493 return SNMP_IPADDRESS (bgp_empty_addr);
494 break;
495 case BGPPEERLOCALPORT:
496 if (peer->su_local)
497 return SNMP_INTEGER (ntohs (peer->su_local->sin.sin_port));
498 else
499 return SNMP_INTEGER (0);
500 break;
501 case BGPPEERREMOTEADDR:
502 if (peer->su_remote)
503 return SNMP_IPADDRESS (peer->su_remote->sin.sin_addr);
504 else
505 return SNMP_IPADDRESS (bgp_empty_addr);
506 break;
507 case BGPPEERREMOTEPORT:
508 if (peer->su_remote)
509 return SNMP_INTEGER (ntohs (peer->su_remote->sin.sin_port));
510 else
511 return SNMP_INTEGER (0);
512 break;
513 case BGPPEERREMOTEAS:
514 return SNMP_INTEGER (peer->as);
515 break;
516 case BGPPEERINUPDATES:
517 return SNMP_INTEGER (peer->update_in);
518 break;
519 case BGPPEEROUTUPDATES:
520 return SNMP_INTEGER (peer->update_out);
521 break;
522 case BGPPEERINTOTALMESSAGES:
523 return SNMP_INTEGER (peer->open_in + peer->update_in
524 + peer->keepalive_in + peer->notify_in
525 + peer->refresh_in + peer->dynamic_cap_in);
526 break;
527 case BGPPEEROUTTOTALMESSAGES:
528 return SNMP_INTEGER (peer->open_out + peer->update_out
529 + peer->keepalive_out + peer->notify_out
paul07661cb2003-03-18 00:03:05 +0000530 + peer->refresh_out + peer->dynamic_cap_out);
paul718e3742002-12-13 20:15:29 +0000531 break;
532 case BGPPEERLASTERROR:
533 {
534 static u_char lasterror[2];
535 lasterror[0] = peer->notify.code;
536 lasterror[1] = peer->notify.subcode;
537 *var_len = 2;
538 return (u_char *)&lasterror;
539 }
540 break;
541 case BGPPEERFSMESTABLISHEDTRANSITIONS:
542 return SNMP_INTEGER (peer->established);
543 break;
544 case BGPPEERFSMESTABLISHEDTIME:
545 if (peer->uptime == 0)
546 return SNMP_INTEGER (0);
547 else
Stephen Hemminger65957882010-01-15 16:22:10 +0300548 return SNMP_INTEGER (bgp_clock () - peer->uptime);
paul718e3742002-12-13 20:15:29 +0000549 break;
550 case BGPPEERCONNECTRETRYINTERVAL:
551 *write_method = write_bgpPeerTable;
552 return SNMP_INTEGER (peer->v_connect);
553 break;
554 case BGPPEERHOLDTIME:
555 return SNMP_INTEGER (peer->v_holdtime);
556 break;
557 case BGPPEERKEEPALIVE:
558 return SNMP_INTEGER (peer->v_keepalive);
559 break;
560 case BGPPEERHOLDTIMECONFIGURED:
561 *write_method = write_bgpPeerTable;
562 if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
563 return SNMP_INTEGER (peer->holdtime);
564 else
565 return SNMP_INTEGER (peer->v_holdtime);
566 break;
567 case BGPPEERKEEPALIVECONFIGURED:
568 *write_method = write_bgpPeerTable;
569 if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
570 return SNMP_INTEGER (peer->keepalive);
571 else
572 return SNMP_INTEGER (peer->v_keepalive);
573 break;
574 case BGPPEERMINASORIGINATIONINTERVAL:
575 *write_method = write_bgpPeerTable;
576 return SNMP_INTEGER (peer->v_asorig);
577 break;
578 case BGPPEERMINROUTEADVERTISEMENTINTERVAL:
579 *write_method = write_bgpPeerTable;
580 return SNMP_INTEGER (peer->v_routeadv);
581 break;
582 case BGPPEERINUPDATEELAPSEDTIME:
583 if (peer->update_time == 0)
584 return SNMP_INTEGER (0);
585 else
Stephen Hemminger65957882010-01-15 16:22:10 +0300586 return SNMP_INTEGER (bgp_clock () - peer->update_time);
paul718e3742002-12-13 20:15:29 +0000587 break;
588 default:
589 return NULL;
590 break;
591 }
592 return NULL;
593}
594
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100595static u_char *
paul718e3742002-12-13 20:15:29 +0000596bgpIdentifier (struct variable *v, oid name[], size_t *length,
597 int exact, size_t *var_len, WriteMethod **write_method)
598{
599 struct bgp *bgp;
600
601 if (smux_header_generic(v, name, length, exact, var_len, write_method)
602 == MATCH_FAILED)
603 return NULL;
604
605 bgp = bgp_get_default ();
606 if (!bgp)
607 return NULL;
608
609 return SNMP_IPADDRESS (bgp->router_id);
610}
611
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100612static u_char *
paul718e3742002-12-13 20:15:29 +0000613bgpRcvdPathAttrTable (struct variable *v, oid name[], size_t *length,
614 int exact, size_t *var_len, WriteMethod **write_method)
615{
616 /* Received Path Attribute Table. This table contains, one entry
617 per path to a network, path attributes received from all peers
618 running BGP version 3 or less. This table is obsolete, having
619 been replaced in functionality with the bgp4PathAttrTable. */
620 return NULL;
621}
622
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100623static struct bgp_info *
paul718e3742002-12-13 20:15:29 +0000624bgp4PathAttrLookup (struct variable *v, oid name[], size_t *length,
625 struct bgp *bgp, struct prefix_ipv4 *addr, int exact)
626{
627 oid *offset;
628 int offsetlen;
629 struct bgp_info *binfo;
630 struct bgp_info *min;
631 struct bgp_node *rn;
632 union sockunion su;
paulfd79ac92004-10-13 05:06:08 +0000633 unsigned int len;
paul718e3742002-12-13 20:15:29 +0000634 struct in_addr paddr;
635
636#define BGP_PATHATTR_ENTRY_OFFSET \
637 (IN_ADDR_SIZE + 1 + IN_ADDR_SIZE)
638
639 if (exact)
640 {
641 if (*length - v->namelen != BGP_PATHATTR_ENTRY_OFFSET)
642 return NULL;
643
644 /* Set OID offset for prefix. */
645 offset = name + v->namelen;
646 oid2in_addr (offset, IN_ADDR_SIZE, &addr->prefix);
647 offset += IN_ADDR_SIZE;
648
649 /* Prefix length. */
650 addr->prefixlen = *offset;
651 offset++;
652
653 /* Peer address. */
654 su.sin.sin_family = AF_INET;
655 oid2in_addr (offset, IN_ADDR_SIZE, &su.sin.sin_addr);
656
657 /* Lookup node. */
658 rn = bgp_node_lookup (bgp->rib[AFI_IP][SAFI_UNICAST],
659 (struct prefix *) addr);
660 if (rn)
661 {
662 bgp_unlock_node (rn);
663
664 for (binfo = rn->info; binfo; binfo = binfo->next)
665 if (sockunion_same (&binfo->peer->su, &su))
666 return binfo;
667 }
668 }
669 else
670 {
671 offset = name + v->namelen;
672 offsetlen = *length - v->namelen;
673 len = offsetlen;
674
675 if (offsetlen == 0)
676 rn = bgp_table_top (bgp->rib[AFI_IP][SAFI_UNICAST]);
677 else
678 {
679 if (len > IN_ADDR_SIZE)
680 len = IN_ADDR_SIZE;
681
682 oid2in_addr (offset, len, &addr->prefix);
683
684 offset += IN_ADDR_SIZE;
685 offsetlen -= IN_ADDR_SIZE;
686
687 if (offsetlen > 0)
688 addr->prefixlen = *offset;
689 else
690 addr->prefixlen = len * 8;
691
692 rn = bgp_node_get (bgp->rib[AFI_IP][SAFI_UNICAST],
693 (struct prefix *) addr);
694
695 offset++;
696 offsetlen--;
697 }
698
699 if (offsetlen > 0)
700 {
701 len = offsetlen;
702 if (len > IN_ADDR_SIZE)
703 len = IN_ADDR_SIZE;
704
705 oid2in_addr (offset, len, &paddr);
706 }
707 else
708 paddr.s_addr = 0;
709
710 if (! rn)
711 return NULL;
712
713 do
714 {
715 min = NULL;
716
717 for (binfo = rn->info; binfo; binfo = binfo->next)
718 {
719 if (binfo->peer->su.sin.sin_family == AF_INET
720 && ntohl (paddr.s_addr)
721 < ntohl (binfo->peer->su.sin.sin_addr.s_addr))
722 {
723 if (min)
724 {
725 if (ntohl (binfo->peer->su.sin.sin_addr.s_addr)
726 < ntohl (min->peer->su.sin.sin_addr.s_addr))
727 min = binfo;
728 }
729 else
730 min = binfo;
731 }
732 }
733
734 if (min)
735 {
736 *length = v->namelen + BGP_PATHATTR_ENTRY_OFFSET;
737
738 offset = name + v->namelen;
739 oid_copy_addr (offset, &rn->p.u.prefix4, IN_ADDR_SIZE);
740 offset += IN_ADDR_SIZE;
741 *offset = rn->p.prefixlen;
742 offset++;
743 oid_copy_addr (offset, &min->peer->su.sin.sin_addr,
744 IN_ADDR_SIZE);
745 addr->prefix = rn->p.u.prefix4;
746 addr->prefixlen = rn->p.prefixlen;
747
748 bgp_unlock_node (rn);
749
750 return min;
751 }
752
753 paddr.s_addr = 0;
754 }
755 while ((rn = bgp_route_next (rn)) != NULL);
756 }
757 return NULL;
758}
759
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100760static u_char *
paul718e3742002-12-13 20:15:29 +0000761bgp4PathAttrTable (struct variable *v, oid name[], size_t *length,
762 int exact, size_t *var_len, WriteMethod **write_method)
763{
764 struct bgp *bgp;
765 struct bgp_info *binfo;
766 struct prefix_ipv4 addr;
767
768 bgp = bgp_get_default ();
769 if (! bgp)
770 return NULL;
771
Vincent Bernat8046ba62012-05-31 13:30:28 +0200772 if (smux_header_table(v, name, length, exact, var_len, write_method)
773 == MATCH_FAILED)
774 return NULL;
paul718e3742002-12-13 20:15:29 +0000775 memset (&addr, 0, sizeof (struct prefix_ipv4));
776
777 binfo = bgp4PathAttrLookup (v, name, length, bgp, &addr, exact);
778 if (! binfo)
779 return NULL;
780
781 switch (v->magic)
782 {
783 case BGP4PATHATTRPEER: /* 1 */
784 return SNMP_IPADDRESS (binfo->peer->su.sin.sin_addr);
785 break;
786 case BGP4PATHATTRIPADDRPREFIXLEN: /* 2 */
787 return SNMP_INTEGER (addr.prefixlen);
788 break;
789 case BGP4PATHATTRIPADDRPREFIX: /* 3 */
790 return SNMP_IPADDRESS (addr.prefix);
791 break;
792 case BGP4PATHATTRORIGIN: /* 4 */
793 return SNMP_INTEGER (binfo->attr->origin);
794 break;
795 case BGP4PATHATTRASPATHSEGMENT: /* 5 */
paulfe69a502005-09-10 16:55:02 +0000796 return aspath_snmp_pathseg (binfo->attr->aspath, var_len);
paul718e3742002-12-13 20:15:29 +0000797 break;
798 case BGP4PATHATTRNEXTHOP: /* 6 */
799 return SNMP_IPADDRESS (binfo->attr->nexthop);
800 break;
801 case BGP4PATHATTRMULTIEXITDISC: /* 7 */
802 return SNMP_INTEGER (binfo->attr->med);
803 break;
804 case BGP4PATHATTRLOCALPREF: /* 8 */
805 return SNMP_INTEGER (binfo->attr->local_pref);
806 break;
807 case BGP4PATHATTRATOMICAGGREGATE: /* 9 */
808 return SNMP_INTEGER (1);
809 break;
810 case BGP4PATHATTRAGGREGATORAS: /* 10 */
Paul Jakmafb982c22007-05-04 20:15:47 +0000811 if (binfo->attr->extra)
812 return SNMP_INTEGER (binfo->attr->extra->aggregator_as);
813 else
814 return SNMP_INTEGER (0);
paul718e3742002-12-13 20:15:29 +0000815 break;
816 case BGP4PATHATTRAGGREGATORADDR: /* 11 */
Paul Jakmafb982c22007-05-04 20:15:47 +0000817 if (binfo->attr->extra)
818 return SNMP_IPADDRESS (binfo->attr->extra->aggregator_addr);
819 else
820 return SNMP_INTEGER (0);
paul718e3742002-12-13 20:15:29 +0000821 break;
822 case BGP4PATHATTRCALCLOCALPREF: /* 12 */
823 return SNMP_INTEGER (-1);
824 break;
825 case BGP4PATHATTRBEST: /* 13 */
826#define BGP4_PathAttrBest_false 1
827#define BGP4_PathAttrBest_true 2
828 if (CHECK_FLAG (binfo->flags, BGP_INFO_SELECTED))
829 return SNMP_INTEGER (BGP4_PathAttrBest_true);
830 else
831 return SNMP_INTEGER (BGP4_PathAttrBest_false);
832 break;
833 case BGP4PATHATTRUNKNOWN: /* 14 */
834 *var_len = 0;
835 return NULL;
836 break;
837 }
838 return NULL;
839}
David Lamparter6b0655a2014-06-04 06:53:35 +0200840
paul718e3742002-12-13 20:15:29 +0000841/* BGP Traps. */
842struct trap_object bgpTrapList[] =
843{
Vincent Bernatb8cf46b2012-05-25 08:56:44 +0200844 {3, {3, 1, BGPPEERLASTERROR}},
845 {3, {3, 1, BGPPEERSTATE}}
paul718e3742002-12-13 20:15:29 +0000846};
847
848void
849bgpTrapEstablished (struct peer *peer)
850{
851 int ret;
852 struct in_addr addr;
853 oid index[sizeof (oid) * IN_ADDR_SIZE];
854
855 ret = inet_aton (peer->host, &addr);
856 if (ret == 0)
857 return;
858
859 oid_copy_addr (index, &addr, IN_ADDR_SIZE);
860
Vincent Bernatb7c0d062012-05-25 11:17:01 +0200861 smux_trap (bgp_variables, sizeof bgp_variables / sizeof (struct variable),
862 bgp_trap_oid, sizeof bgp_trap_oid / sizeof (oid),
863 bgp_oid, sizeof bgp_oid / sizeof (oid),
paul718e3742002-12-13 20:15:29 +0000864 index, IN_ADDR_SIZE,
865 bgpTrapList, sizeof bgpTrapList / sizeof (struct trap_object),
Vincent Bernat4b89e452012-05-24 21:22:01 +0200866 BGPESTABLISHED);
paul718e3742002-12-13 20:15:29 +0000867}
868
869void
870bgpTrapBackwardTransition (struct peer *peer)
871{
872 int ret;
873 struct in_addr addr;
874 oid index[sizeof (oid) * IN_ADDR_SIZE];
875
876 ret = inet_aton (peer->host, &addr);
877 if (ret == 0)
878 return;
879
880 oid_copy_addr (index, &addr, IN_ADDR_SIZE);
881
Vincent Bernatb7c0d062012-05-25 11:17:01 +0200882 smux_trap (bgp_variables, sizeof bgp_variables / sizeof (struct variable),
883 bgp_trap_oid, sizeof bgp_trap_oid / sizeof (oid),
884 bgp_oid, sizeof bgp_oid / sizeof (oid),
paul718e3742002-12-13 20:15:29 +0000885 index, IN_ADDR_SIZE,
886 bgpTrapList, sizeof bgpTrapList / sizeof (struct trap_object),
Vincent Bernat4b89e452012-05-24 21:22:01 +0200887 BGPBACKWARDTRANSITION);
paul718e3742002-12-13 20:15:29 +0000888}
889
890void
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800891bgp_snmp_init (void)
paul718e3742002-12-13 20:15:29 +0000892{
hassoc75105a2004-10-13 10:33:26 +0000893 smux_init (bm->master);
paul718e3742002-12-13 20:15:29 +0000894 REGISTER_MIB("mibII/bgp", bgp_variables, variable, bgp_oid);
paul718e3742002-12-13 20:15:29 +0000895}
896#endif /* HAVE_SNMP */