blob: 662186b591a965ac5752a6f81c989c66278dd71a [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
Daniel Walton3ff77fe2015-10-21 06:42:48 -070077#define BGPPEERMINROUTEADVERTISEMENTINTERVAL 22
78#define BGPPEERINUPDATEELAPSEDTIME 23
paul718e3742002-12-13 20:15:29 +000079
80/* BGP MIB bgpIdentifier. */
81#define BGPIDENTIFIER 0
82
83/* BGP MIB bgpRcvdPathAttrTable */
84#define BGPPATHATTRPEER 1
85#define BGPPATHATTRDESTNETWORK 2
86#define BGPPATHATTRORIGIN 3
87#define BGPPATHATTRASPATH 4
88#define BGPPATHATTRNEXTHOP 5
89#define BGPPATHATTRINTERASMETRIC 6
90
91/* BGP MIB bgp4PathAttrTable. */
92#define BGP4PATHATTRPEER 1
93#define BGP4PATHATTRIPADDRPREFIXLEN 2
94#define BGP4PATHATTRIPADDRPREFIX 3
95#define BGP4PATHATTRORIGIN 4
96#define BGP4PATHATTRASPATHSEGMENT 5
97#define BGP4PATHATTRNEXTHOP 6
98#define BGP4PATHATTRMULTIEXITDISC 7
99#define BGP4PATHATTRLOCALPREF 8
100#define BGP4PATHATTRATOMICAGGREGATE 9
101#define BGP4PATHATTRAGGREGATORAS 10
102#define BGP4PATHATTRAGGREGATORADDR 11
103#define BGP4PATHATTRCALCLOCALPREF 12
104#define BGP4PATHATTRBEST 13
105#define BGP4PATHATTRUNKNOWN 14
106
107/* SNMP value hack. */
108#define INTEGER ASN_INTEGER
109#define INTEGER32 ASN_INTEGER
110#define COUNTER32 ASN_COUNTER
111#define OCTET_STRING ASN_OCTET_STR
112#define IPADDRESS ASN_IPADDRESS
113#define GAUGE32 ASN_UNSIGNED
David Lamparter6b0655a2014-06-04 06:53:35 +0200114
paul718e3742002-12-13 20:15:29 +0000115/* Declare static local variables for convenience. */
116SNMP_LOCAL_VARIABLES
117
118/* BGP-MIB instances. */
119oid bgp_oid [] = { BGP4MIB };
Vincent Bernatb7c0d062012-05-25 11:17:01 +0200120oid bgp_trap_oid [] = { BGP4MIB, 0 };
paul718e3742002-12-13 20:15:29 +0000121
122/* IP address 0.0.0.0. */
David Lamparter5181a022015-09-15 03:00:09 -0700123static struct in_addr bgp_empty_addr = { .s_addr = 0 };
paul718e3742002-12-13 20:15:29 +0000124
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}},
paul718e3742002-12-13 20:15:29 +0000191 {BGPPEERMINROUTEADVERTISEMENTINTERVAL, INTEGER, RWRITE, bgpPeerTable,
192 3, {3, 1, 23}},
193 {BGPPEERINUPDATEELAPSEDTIME, GAUGE32, RONLY, bgpPeerTable,
194 3, {3, 1, 24}},
195 /* BGP identifier. */
196 {BGPIDENTIFIER, IPADDRESS, RONLY, bgpIdentifier,
197 1, {4}},
198 /* BGP received path attribute table. */
199 {BGPPATHATTRPEER, IPADDRESS, RONLY, bgpRcvdPathAttrTable,
200 3, {5, 1, 1}},
201 {BGPPATHATTRDESTNETWORK, IPADDRESS, RONLY, bgpRcvdPathAttrTable,
202 3, {5, 1, 2}},
203 {BGPPATHATTRORIGIN, INTEGER, RONLY, bgpRcvdPathAttrTable,
204 3, {5, 1, 3}},
205 {BGPPATHATTRASPATH, OCTET_STRING, RONLY, bgpRcvdPathAttrTable,
206 3, {5, 1, 4}},
207 {BGPPATHATTRNEXTHOP, IPADDRESS, RONLY, bgpRcvdPathAttrTable,
208 3, {5, 1, 5}},
209 {BGPPATHATTRINTERASMETRIC, INTEGER32, RONLY, bgpRcvdPathAttrTable,
210 3, {5, 1, 6}},
211 /* BGP-4 received path attribute table. */
212 {BGP4PATHATTRPEER, IPADDRESS, RONLY, bgp4PathAttrTable,
213 3, {6, 1, 1}},
214 {BGP4PATHATTRIPADDRPREFIXLEN, INTEGER, RONLY, bgp4PathAttrTable,
215 3, {6, 1, 2}},
216 {BGP4PATHATTRIPADDRPREFIX, IPADDRESS, RONLY, bgp4PathAttrTable,
217 3, {6, 1, 3}},
218 {BGP4PATHATTRORIGIN, INTEGER, RONLY, bgp4PathAttrTable,
219 3, {6, 1, 4}},
220 {BGP4PATHATTRASPATHSEGMENT, OCTET_STRING, RONLY, bgp4PathAttrTable,
221 3, {6, 1, 5}},
222 {BGP4PATHATTRNEXTHOP, IPADDRESS, RONLY, bgp4PathAttrTable,
223 3, {6, 1, 6}},
224 {BGP4PATHATTRMULTIEXITDISC, INTEGER, RONLY, bgp4PathAttrTable,
225 3, {6, 1, 7}},
226 {BGP4PATHATTRLOCALPREF, INTEGER, RONLY, bgp4PathAttrTable,
227 3, {6, 1, 8}},
228 {BGP4PATHATTRATOMICAGGREGATE, INTEGER, RONLY, bgp4PathAttrTable,
229 3, {6, 1, 9}},
230 {BGP4PATHATTRAGGREGATORAS, INTEGER, RONLY, bgp4PathAttrTable,
231 3, {6, 1, 10}},
232 {BGP4PATHATTRAGGREGATORADDR, IPADDRESS, RONLY, bgp4PathAttrTable,
233 3, {6, 1, 11}},
234 {BGP4PATHATTRCALCLOCALPREF, INTEGER, RONLY, bgp4PathAttrTable,
235 3, {6, 1, 12}},
236 {BGP4PATHATTRBEST, INTEGER, RONLY, bgp4PathAttrTable,
237 3, {6, 1, 13}},
238 {BGP4PATHATTRUNKNOWN, OCTET_STRING, RONLY, bgp4PathAttrTable,
239 3, {6, 1, 14}},
240};
pauldd488a72003-06-19 01:21:07 +0000241
David Lamparter6b0655a2014-06-04 06:53:35 +0200242
paul718e3742002-12-13 20:15:29 +0000243static u_char *
244bgpVersion (struct variable *v, oid name[], size_t *length, int exact,
245 size_t *var_len, WriteMethod **write_method)
246{
247 static u_char version;
248
249 if (smux_header_generic(v, name, length, exact, var_len, write_method)
250 == MATCH_FAILED)
251 return NULL;
252
253 /* Retrun BGP version. Zebra bgpd only support version 4. */
254 version = (0x80 >> (BGP_VERSION_4 - 1));
255
256 /* Return octet string length 1. */
257 *var_len = 1;
258 return (u_char *)&version;
259}
260
261static u_char *
262bgpLocalAs (struct variable *v, oid name[], size_t *length,
263 int exact, size_t *var_len, WriteMethod **write_method)
264{
265 struct bgp *bgp;
266
267 if (smux_header_generic(v, name, length, exact, var_len, write_method)
268 == MATCH_FAILED)
269 return NULL;
270
271 /* Get BGP structure. */
272 bgp = bgp_get_default ();
273 if (! bgp)
274 return NULL;
275
276 return SNMP_INTEGER (bgp->as);
277}
278
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100279static struct peer *
paul718e3742002-12-13 20:15:29 +0000280peer_lookup_addr_ipv4 (struct in_addr *src)
281{
282 struct bgp *bgp;
283 struct peer *peer;
paul1eb8ef22005-04-07 07:30:20 +0000284 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000285 struct in_addr addr;
286 int ret;
287
288 bgp = bgp_get_default ();
289 if (! bgp)
290 return NULL;
291
paul1eb8ef22005-04-07 07:30:20 +0000292 for (ALL_LIST_ELEMENTS_RO (bgp->peer, node, peer))
paul718e3742002-12-13 20:15:29 +0000293 {
294 ret = inet_pton (AF_INET, peer->host, &addr);
295 if (ret > 0)
296 {
297 if (IPV4_ADDR_SAME (&addr, src))
298 return peer;
299 }
300 }
301 return NULL;
302}
303
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100304static struct peer *
paul718e3742002-12-13 20:15:29 +0000305bgp_peer_lookup_next (struct in_addr *src)
306{
307 struct bgp *bgp;
308 struct peer *peer;
paul1eb8ef22005-04-07 07:30:20 +0000309 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000310 struct in_addr *p;
311 union sockunion su;
312 int ret;
313
314 memset (&su, 0, sizeof (union sockunion));
315
316 bgp = bgp_get_default ();
317 if (! bgp)
318 return NULL;
319
paul1eb8ef22005-04-07 07:30:20 +0000320 for (ALL_LIST_ELEMENTS_RO (bgp->peer, node, peer))
paul718e3742002-12-13 20:15:29 +0000321 {
322 ret = inet_pton (AF_INET, peer->host, &su.sin.sin_addr);
323 if (ret > 0)
324 {
325 p = &su.sin.sin_addr;
326
327 if (ntohl (p->s_addr) > ntohl (src->s_addr))
328 {
329 src->s_addr = p->s_addr;
330 return peer;
331 }
332 }
333 }
334 return NULL;
335}
336
David Lamparterd689d1a2015-03-03 13:54:30 +0100337/* 1.3.6.1.2.1.15.3.1.x = 10 */
338#define PEERTAB_NAMELEN 10
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;
David Lamparterd689d1a2015-03-03 13:54:30 +0100345 size_t namelen = v ? v->namelen : PEERTAB_NAMELEN;
paul718e3742002-12-13 20:15:29 +0000346 int len;
347
348 if (exact)
349 {
350 /* Check the length. */
David Lamparterd689d1a2015-03-03 13:54:30 +0100351 if (*length - namelen != sizeof (struct in_addr))
paul718e3742002-12-13 20:15:29 +0000352 return NULL;
353
David Lamparterd689d1a2015-03-03 13:54:30 +0100354 oid2in_addr (name + namelen, IN_ADDR_SIZE, addr);
paul718e3742002-12-13 20:15:29 +0000355
356 peer = peer_lookup_addr_ipv4 (addr);
357 return peer;
358 }
359 else
360 {
David Lamparterd689d1a2015-03-03 13:54:30 +0100361 len = *length - namelen;
paul718e3742002-12-13 20:15:29 +0000362 if (len > 4) len = 4;
363
David Lamparterd689d1a2015-03-03 13:54:30 +0100364 oid2in_addr (name + namelen, len, addr);
paul718e3742002-12-13 20:15:29 +0000365
366 peer = bgp_peer_lookup_next (addr);
367
368 if (peer == NULL)
369 return NULL;
370
David Lamparterd689d1a2015-03-03 13:54:30 +0100371 oid_copy_addr (name + namelen, addr, sizeof (struct in_addr));
372 *length = sizeof (struct in_addr) + namelen;
paul718e3742002-12-13 20:15:29 +0000373
374 return peer;
375 }
376 return NULL;
377}
378
379/* BGP write methods. */
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100380static int
paul718e3742002-12-13 20:15:29 +0000381write_bgpPeerTable (int action, u_char *var_val,
382 u_char var_val_type, size_t var_val_len,
David Lamparterd689d1a2015-03-03 13:54:30 +0100383 u_char *statP, oid *name, size_t length)
paul718e3742002-12-13 20:15:29 +0000384{
385 struct in_addr addr;
386 struct peer *peer;
387 long intval;
David Lamparterd689d1a2015-03-03 13:54:30 +0100388
paul718e3742002-12-13 20:15:29 +0000389 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
David Lamparterd689d1a2015-03-03 13:54:30 +0100398 intval = *(long *)var_val;
paul718e3742002-12-13 20:15:29 +0000399
400 memset (&addr, 0, sizeof (struct in_addr));
401
David Lamparterd689d1a2015-03-03 13:54:30 +0100402 peer = bgpPeerTable_lookup (NULL, name, &length, &addr, 1);
paul718e3742002-12-13 20:15:29 +0000403 if (! peer)
404 return SNMP_ERR_NOSUCHNAME;
405
David Lamparterd689d1a2015-03-03 13:54:30 +0100406 if (action != SNMP_MSG_INTERNAL_SET_COMMIT)
407 return SNMP_ERR_NOERROR;
paul718e3742002-12-13 20:15:29 +0000408
David Lamparterd689d1a2015-03-03 13:54:30 +0100409 zlog_info ("%s: SNMP write .%ld = %ld",
410 peer->host, (long)name[PEERTAB_NAMELEN - 1], intval);
411
412 switch (name[PEERTAB_NAMELEN - 1])
paul718e3742002-12-13 20:15:29 +0000413 {
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;
paul718e3742002-12-13 20:15:29 +0000440 case BGPPEERMINROUTEADVERTISEMENTINTERVAL:
441 peer->v_routeadv = intval;
442 break;
443 }
444 return SNMP_ERR_NOERROR;
445}
446
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100447static u_char *
paul718e3742002-12-13 20:15:29 +0000448bgpPeerTable (struct variable *v, oid name[], size_t *length,
449 int exact, size_t *var_len, WriteMethod **write_method)
450{
451 static struct in_addr addr;
452 struct peer *peer;
453
Vincent Bernat8046ba62012-05-31 13:30:28 +0200454 if (smux_header_table(v, name, length, exact, var_len, write_method)
455 == MATCH_FAILED)
456 return NULL;
paul718e3742002-12-13 20:15:29 +0000457 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
Stephen Hemminger65957882010-01-15 16:22:10 +0300542 return SNMP_INTEGER (bgp_clock () - peer->uptime);
paul718e3742002-12-13 20:15:29 +0000543 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;
paul718e3742002-12-13 20:15:29 +0000568 case BGPPEERMINROUTEADVERTISEMENTINTERVAL:
569 *write_method = write_bgpPeerTable;
570 return SNMP_INTEGER (peer->v_routeadv);
571 break;
572 case BGPPEERINUPDATEELAPSEDTIME:
573 if (peer->update_time == 0)
574 return SNMP_INTEGER (0);
575 else
Stephen Hemminger65957882010-01-15 16:22:10 +0300576 return SNMP_INTEGER (bgp_clock () - peer->update_time);
paul718e3742002-12-13 20:15:29 +0000577 break;
578 default:
579 return NULL;
580 break;
581 }
582 return NULL;
583}
584
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100585static u_char *
paul718e3742002-12-13 20:15:29 +0000586bgpIdentifier (struct variable *v, oid name[], size_t *length,
587 int exact, size_t *var_len, WriteMethod **write_method)
588{
589 struct bgp *bgp;
590
591 if (smux_header_generic(v, name, length, exact, var_len, write_method)
592 == MATCH_FAILED)
593 return NULL;
594
595 bgp = bgp_get_default ();
596 if (!bgp)
597 return NULL;
598
599 return SNMP_IPADDRESS (bgp->router_id);
600}
601
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100602static u_char *
paul718e3742002-12-13 20:15:29 +0000603bgpRcvdPathAttrTable (struct variable *v, oid name[], size_t *length,
604 int exact, size_t *var_len, WriteMethod **write_method)
605{
606 /* Received Path Attribute Table. This table contains, one entry
607 per path to a network, path attributes received from all peers
608 running BGP version 3 or less. This table is obsolete, having
609 been replaced in functionality with the bgp4PathAttrTable. */
610 return NULL;
611}
612
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100613static struct bgp_info *
paul718e3742002-12-13 20:15:29 +0000614bgp4PathAttrLookup (struct variable *v, oid name[], size_t *length,
615 struct bgp *bgp, struct prefix_ipv4 *addr, int exact)
616{
617 oid *offset;
618 int offsetlen;
619 struct bgp_info *binfo;
620 struct bgp_info *min;
621 struct bgp_node *rn;
622 union sockunion su;
paulfd79ac92004-10-13 05:06:08 +0000623 unsigned int len;
paul718e3742002-12-13 20:15:29 +0000624 struct in_addr paddr;
625
626#define BGP_PATHATTR_ENTRY_OFFSET \
627 (IN_ADDR_SIZE + 1 + IN_ADDR_SIZE)
628
629 if (exact)
630 {
631 if (*length - v->namelen != BGP_PATHATTR_ENTRY_OFFSET)
632 return NULL;
633
634 /* Set OID offset for prefix. */
635 offset = name + v->namelen;
636 oid2in_addr (offset, IN_ADDR_SIZE, &addr->prefix);
637 offset += IN_ADDR_SIZE;
638
639 /* Prefix length. */
640 addr->prefixlen = *offset;
641 offset++;
642
643 /* Peer address. */
644 su.sin.sin_family = AF_INET;
645 oid2in_addr (offset, IN_ADDR_SIZE, &su.sin.sin_addr);
646
647 /* Lookup node. */
648 rn = bgp_node_lookup (bgp->rib[AFI_IP][SAFI_UNICAST],
649 (struct prefix *) addr);
650 if (rn)
651 {
652 bgp_unlock_node (rn);
653
654 for (binfo = rn->info; binfo; binfo = binfo->next)
655 if (sockunion_same (&binfo->peer->su, &su))
656 return binfo;
657 }
658 }
659 else
660 {
661 offset = name + v->namelen;
662 offsetlen = *length - v->namelen;
663 len = offsetlen;
664
665 if (offsetlen == 0)
666 rn = bgp_table_top (bgp->rib[AFI_IP][SAFI_UNICAST]);
667 else
668 {
669 if (len > IN_ADDR_SIZE)
670 len = IN_ADDR_SIZE;
671
672 oid2in_addr (offset, len, &addr->prefix);
673
674 offset += IN_ADDR_SIZE;
675 offsetlen -= IN_ADDR_SIZE;
676
677 if (offsetlen > 0)
678 addr->prefixlen = *offset;
679 else
680 addr->prefixlen = len * 8;
681
682 rn = bgp_node_get (bgp->rib[AFI_IP][SAFI_UNICAST],
683 (struct prefix *) addr);
684
685 offset++;
686 offsetlen--;
687 }
688
689 if (offsetlen > 0)
690 {
691 len = offsetlen;
692 if (len > IN_ADDR_SIZE)
693 len = IN_ADDR_SIZE;
694
695 oid2in_addr (offset, len, &paddr);
696 }
697 else
698 paddr.s_addr = 0;
699
700 if (! rn)
701 return NULL;
702
703 do
704 {
705 min = NULL;
706
707 for (binfo = rn->info; binfo; binfo = binfo->next)
708 {
709 if (binfo->peer->su.sin.sin_family == AF_INET
710 && ntohl (paddr.s_addr)
711 < ntohl (binfo->peer->su.sin.sin_addr.s_addr))
712 {
713 if (min)
714 {
715 if (ntohl (binfo->peer->su.sin.sin_addr.s_addr)
716 < ntohl (min->peer->su.sin.sin_addr.s_addr))
717 min = binfo;
718 }
719 else
720 min = binfo;
721 }
722 }
723
724 if (min)
725 {
726 *length = v->namelen + BGP_PATHATTR_ENTRY_OFFSET;
727
728 offset = name + v->namelen;
729 oid_copy_addr (offset, &rn->p.u.prefix4, IN_ADDR_SIZE);
730 offset += IN_ADDR_SIZE;
731 *offset = rn->p.prefixlen;
732 offset++;
733 oid_copy_addr (offset, &min->peer->su.sin.sin_addr,
734 IN_ADDR_SIZE);
735 addr->prefix = rn->p.u.prefix4;
736 addr->prefixlen = rn->p.prefixlen;
737
738 bgp_unlock_node (rn);
739
740 return min;
741 }
742
743 paddr.s_addr = 0;
744 }
745 while ((rn = bgp_route_next (rn)) != NULL);
746 }
747 return NULL;
748}
749
Chris Caputo0be8dfb2009-06-02 18:40:07 +0100750static u_char *
paul718e3742002-12-13 20:15:29 +0000751bgp4PathAttrTable (struct variable *v, oid name[], size_t *length,
752 int exact, size_t *var_len, WriteMethod **write_method)
753{
754 struct bgp *bgp;
755 struct bgp_info *binfo;
756 struct prefix_ipv4 addr;
757
758 bgp = bgp_get_default ();
759 if (! bgp)
760 return NULL;
761
Vincent Bernat8046ba62012-05-31 13:30:28 +0200762 if (smux_header_table(v, name, length, exact, var_len, write_method)
763 == MATCH_FAILED)
764 return NULL;
paul718e3742002-12-13 20:15:29 +0000765 memset (&addr, 0, sizeof (struct prefix_ipv4));
766
767 binfo = bgp4PathAttrLookup (v, name, length, bgp, &addr, exact);
768 if (! binfo)
769 return NULL;
770
771 switch (v->magic)
772 {
773 case BGP4PATHATTRPEER: /* 1 */
774 return SNMP_IPADDRESS (binfo->peer->su.sin.sin_addr);
775 break;
776 case BGP4PATHATTRIPADDRPREFIXLEN: /* 2 */
777 return SNMP_INTEGER (addr.prefixlen);
778 break;
779 case BGP4PATHATTRIPADDRPREFIX: /* 3 */
780 return SNMP_IPADDRESS (addr.prefix);
781 break;
782 case BGP4PATHATTRORIGIN: /* 4 */
783 return SNMP_INTEGER (binfo->attr->origin);
784 break;
785 case BGP4PATHATTRASPATHSEGMENT: /* 5 */
paulfe69a502005-09-10 16:55:02 +0000786 return aspath_snmp_pathseg (binfo->attr->aspath, var_len);
paul718e3742002-12-13 20:15:29 +0000787 break;
788 case BGP4PATHATTRNEXTHOP: /* 6 */
789 return SNMP_IPADDRESS (binfo->attr->nexthop);
790 break;
791 case BGP4PATHATTRMULTIEXITDISC: /* 7 */
792 return SNMP_INTEGER (binfo->attr->med);
793 break;
794 case BGP4PATHATTRLOCALPREF: /* 8 */
795 return SNMP_INTEGER (binfo->attr->local_pref);
796 break;
797 case BGP4PATHATTRATOMICAGGREGATE: /* 9 */
798 return SNMP_INTEGER (1);
799 break;
800 case BGP4PATHATTRAGGREGATORAS: /* 10 */
Paul Jakmafb982c22007-05-04 20:15:47 +0000801 if (binfo->attr->extra)
802 return SNMP_INTEGER (binfo->attr->extra->aggregator_as);
803 else
804 return SNMP_INTEGER (0);
paul718e3742002-12-13 20:15:29 +0000805 break;
806 case BGP4PATHATTRAGGREGATORADDR: /* 11 */
Paul Jakmafb982c22007-05-04 20:15:47 +0000807 if (binfo->attr->extra)
808 return SNMP_IPADDRESS (binfo->attr->extra->aggregator_addr);
809 else
810 return SNMP_INTEGER (0);
paul718e3742002-12-13 20:15:29 +0000811 break;
812 case BGP4PATHATTRCALCLOCALPREF: /* 12 */
813 return SNMP_INTEGER (-1);
814 break;
815 case BGP4PATHATTRBEST: /* 13 */
816#define BGP4_PathAttrBest_false 1
817#define BGP4_PathAttrBest_true 2
818 if (CHECK_FLAG (binfo->flags, BGP_INFO_SELECTED))
819 return SNMP_INTEGER (BGP4_PathAttrBest_true);
820 else
821 return SNMP_INTEGER (BGP4_PathAttrBest_false);
822 break;
823 case BGP4PATHATTRUNKNOWN: /* 14 */
824 *var_len = 0;
825 return NULL;
826 break;
827 }
828 return NULL;
829}
David Lamparter6b0655a2014-06-04 06:53:35 +0200830
paul718e3742002-12-13 20:15:29 +0000831/* BGP Traps. */
832struct trap_object bgpTrapList[] =
833{
Vincent Bernatb8cf46b2012-05-25 08:56:44 +0200834 {3, {3, 1, BGPPEERLASTERROR}},
835 {3, {3, 1, BGPPEERSTATE}}
paul718e3742002-12-13 20:15:29 +0000836};
837
838void
839bgpTrapEstablished (struct peer *peer)
840{
841 int ret;
842 struct in_addr addr;
843 oid index[sizeof (oid) * IN_ADDR_SIZE];
844
845 ret = inet_aton (peer->host, &addr);
846 if (ret == 0)
847 return;
848
849 oid_copy_addr (index, &addr, IN_ADDR_SIZE);
850
Vincent Bernatb7c0d062012-05-25 11:17:01 +0200851 smux_trap (bgp_variables, sizeof bgp_variables / sizeof (struct variable),
852 bgp_trap_oid, sizeof bgp_trap_oid / sizeof (oid),
853 bgp_oid, sizeof bgp_oid / sizeof (oid),
paul718e3742002-12-13 20:15:29 +0000854 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
Vincent Bernatb7c0d062012-05-25 11:17:01 +0200872 smux_trap (bgp_variables, sizeof bgp_variables / sizeof (struct variable),
873 bgp_trap_oid, sizeof bgp_trap_oid / sizeof (oid),
874 bgp_oid, sizeof bgp_oid / sizeof (oid),
paul718e3742002-12-13 20:15:29 +0000875 index, IN_ADDR_SIZE,
876 bgpTrapList, sizeof bgpTrapList / sizeof (struct trap_object),
Vincent Bernat4b89e452012-05-24 21:22:01 +0200877 BGPBACKWARDTRANSITION);
paul718e3742002-12-13 20:15:29 +0000878}
879
880void
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800881bgp_snmp_init (void)
paul718e3742002-12-13 20:15:29 +0000882{
hassoc75105a2004-10-13 10:33:26 +0000883 smux_init (bm->master);
paul718e3742002-12-13 20:15:29 +0000884 REGISTER_MIB("mibII/bgp", bgp_variables, variable, bgp_oid);
paul718e3742002-12-13 20:15:29 +0000885}
886#endif /* HAVE_SNMP */