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