]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_snmp.c
zebra: Refactor kernel_rtm to be a bit smarter about how it handles options
[mirror_frr.git] / bgpd / bgp_snmp.c
CommitLineData
718e3744 1/* BGP4 SNMP support
896014f4
DL
2 * Copyright (C) 1999, 2000 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
718e3744 20
21#include <zebra.h>
22
07661cb5 23#include <net-snmp/net-snmp-config.h>
fb62a3ce 24#include <net-snmp/net-snmp-includes.h>
718e3744 25
26#include "if.h"
27#include "log.h"
28#include "prefix.h"
29#include "command.h"
30#include "thread.h"
31#include "smux.h"
039f3a34 32#include "filter.h"
5986b66b
DL
33#include "hook.h"
34#include "libfrr.h"
35#include "version.h"
718e3744 36
37#include "bgpd/bgpd.h"
38#include "bgpd/bgp_table.h"
39#include "bgpd/bgp_aspath.h"
40#include "bgpd/bgp_attr.h"
41#include "bgpd/bgp_route.h"
42#include "bgpd/bgp_fsm.h"
6b0655a2 43
718e3744 44/* BGP4-MIB described in RFC1657. */
45#define BGP4MIB 1,3,6,1,2,1,15
46
020709f9 47/* BGP TRAP. */
48#define BGPESTABLISHED 1
49#define BGPBACKWARDTRANSITION 2
50
718e3744 51/* BGP MIB bgpVersion. */
52#define BGPVERSION 0
53
54/* BGP MIB bgpLocalAs. */
55#define BGPLOCALAS 0
56
57/* BGP MIB bgpPeerTable. */
58#define BGPPEERIDENTIFIER 1
59#define BGPPEERSTATE 2
60#define BGPPEERADMINSTATUS 3
61#define BGPPEERNEGOTIATEDVERSION 4
62#define BGPPEERLOCALADDR 5
63#define BGPPEERLOCALPORT 6
64#define BGPPEERREMOTEADDR 7
65#define BGPPEERREMOTEPORT 8
66#define BGPPEERREMOTEAS 9
67#define BGPPEERINUPDATES 10
68#define BGPPEEROUTUPDATES 11
69#define BGPPEERINTOTALMESSAGES 12
70#define BGPPEEROUTTOTALMESSAGES 13
71#define BGPPEERLASTERROR 14
72#define BGPPEERFSMESTABLISHEDTRANSITIONS 15
73#define BGPPEERFSMESTABLISHEDTIME 16
74#define BGPPEERCONNECTRETRYINTERVAL 17
75#define BGPPEERHOLDTIME 18
76#define BGPPEERKEEPALIVE 19
77#define BGPPEERHOLDTIMECONFIGURED 20
78#define BGPPEERKEEPALIVECONFIGURED 21
bd4b893f
DW
79#define BGPPEERMINROUTEADVERTISEMENTINTERVAL 22
80#define BGPPEERINUPDATEELAPSEDTIME 23
718e3744 81
82/* BGP MIB bgpIdentifier. */
83#define BGPIDENTIFIER 0
84
85/* BGP MIB bgpRcvdPathAttrTable */
86#define BGPPATHATTRPEER 1
87#define BGPPATHATTRDESTNETWORK 2
88#define BGPPATHATTRORIGIN 3
89#define BGPPATHATTRASPATH 4
90#define BGPPATHATTRNEXTHOP 5
91#define BGPPATHATTRINTERASMETRIC 6
92
93/* BGP MIB bgp4PathAttrTable. */
94#define BGP4PATHATTRPEER 1
95#define BGP4PATHATTRIPADDRPREFIXLEN 2
96#define BGP4PATHATTRIPADDRPREFIX 3
97#define BGP4PATHATTRORIGIN 4
98#define BGP4PATHATTRASPATHSEGMENT 5
99#define BGP4PATHATTRNEXTHOP 6
100#define BGP4PATHATTRMULTIEXITDISC 7
101#define BGP4PATHATTRLOCALPREF 8
102#define BGP4PATHATTRATOMICAGGREGATE 9
103#define BGP4PATHATTRAGGREGATORAS 10
104#define BGP4PATHATTRAGGREGATORADDR 11
105#define BGP4PATHATTRCALCLOCALPREF 12
106#define BGP4PATHATTRBEST 13
107#define BGP4PATHATTRUNKNOWN 14
108
109/* SNMP value hack. */
110#define INTEGER ASN_INTEGER
111#define INTEGER32 ASN_INTEGER
112#define COUNTER32 ASN_COUNTER
113#define OCTET_STRING ASN_OCTET_STR
114#define IPADDRESS ASN_IPADDRESS
115#define GAUGE32 ASN_UNSIGNED
6b0655a2 116
718e3744 117/* Declare static local variables for convenience. */
118SNMP_LOCAL_VARIABLES
119
120/* BGP-MIB instances. */
d62a17ae 121static oid bgp_oid[] = {BGP4MIB};
122static oid bgp_trap_oid[] = {BGP4MIB, 0};
718e3744 123
124/* IP address 0.0.0.0. */
d62a17ae 125static struct in_addr bgp_empty_addr = {.s_addr = 0};
718e3744 126
127/* Hook functions. */
d7c0a89a
QY
128static uint8_t *bgpVersion(struct variable *, oid[], size_t *, int, size_t *,
129 WriteMethod **);
130static uint8_t *bgpLocalAs(struct variable *, oid[], size_t *, int, size_t *,
131 WriteMethod **);
132static uint8_t *bgpPeerTable(struct variable *, oid[], size_t *, int, size_t *,
d62a17ae 133 WriteMethod **);
d7c0a89a
QY
134static uint8_t *bgpRcvdPathAttrTable(struct variable *, oid[], size_t *, int,
135 size_t *, WriteMethod **);
136static uint8_t *bgpIdentifier(struct variable *, oid[], size_t *, int, size_t *,
137 WriteMethod **);
138static uint8_t *bgp4PathAttrTable(struct variable *, oid[], size_t *, int,
139 size_t *, WriteMethod **);
140/* static uint8_t *bgpTraps (); */
718e3744 141
d62a17ae 142static struct variable bgp_variables[] = {
143 /* BGP version. */
144 {BGPVERSION, OCTET_STRING, RONLY, bgpVersion, 1, {1}},
145 /* BGP local AS. */
146 {BGPLOCALAS, INTEGER, RONLY, bgpLocalAs, 1, {2}},
147 /* BGP peer table. */
148 {BGPPEERIDENTIFIER, IPADDRESS, RONLY, bgpPeerTable, 3, {3, 1, 1}},
149 {BGPPEERSTATE, INTEGER, RONLY, bgpPeerTable, 3, {3, 1, 2}},
150 {BGPPEERADMINSTATUS, INTEGER, RWRITE, bgpPeerTable, 3, {3, 1, 3}},
151 {BGPPEERNEGOTIATEDVERSION,
152 INTEGER32,
153 RONLY,
154 bgpPeerTable,
155 3,
156 {3, 1, 4}},
157 {BGPPEERLOCALADDR, IPADDRESS, RONLY, bgpPeerTable, 3, {3, 1, 5}},
158 {BGPPEERLOCALPORT, INTEGER, RONLY, bgpPeerTable, 3, {3, 1, 6}},
159 {BGPPEERREMOTEADDR, IPADDRESS, RONLY, bgpPeerTable, 3, {3, 1, 7}},
160 {BGPPEERREMOTEPORT, INTEGER, RONLY, bgpPeerTable, 3, {3, 1, 8}},
161 {BGPPEERREMOTEAS, INTEGER, RONLY, bgpPeerTable, 3, {3, 1, 9}},
162 {BGPPEERINUPDATES, COUNTER32, RONLY, bgpPeerTable, 3, {3, 1, 10}},
163 {BGPPEEROUTUPDATES, COUNTER32, RONLY, bgpPeerTable, 3, {3, 1, 11}},
164 {BGPPEERINTOTALMESSAGES, COUNTER32, RONLY, bgpPeerTable, 3, {3, 1, 12}},
165 {BGPPEEROUTTOTALMESSAGES,
166 COUNTER32,
167 RONLY,
168 bgpPeerTable,
169 3,
170 {3, 1, 13}},
171 {BGPPEERLASTERROR, OCTET_STRING, RONLY, bgpPeerTable, 3, {3, 1, 14}},
172 {BGPPEERFSMESTABLISHEDTRANSITIONS,
173 COUNTER32,
174 RONLY,
175 bgpPeerTable,
176 3,
177 {3, 1, 15}},
178 {BGPPEERFSMESTABLISHEDTIME,
179 GAUGE32,
180 RONLY,
181 bgpPeerTable,
182 3,
183 {3, 1, 16}},
184 {BGPPEERCONNECTRETRYINTERVAL,
185 INTEGER,
186 RWRITE,
187 bgpPeerTable,
188 3,
189 {3, 1, 17}},
190 {BGPPEERHOLDTIME, INTEGER, RONLY, bgpPeerTable, 3, {3, 1, 18}},
191 {BGPPEERKEEPALIVE, INTEGER, RONLY, bgpPeerTable, 3, {3, 1, 19}},
192 {BGPPEERHOLDTIMECONFIGURED,
193 INTEGER,
194 RWRITE,
195 bgpPeerTable,
196 3,
197 {3, 1, 20}},
198 {BGPPEERKEEPALIVECONFIGURED,
199 INTEGER,
200 RWRITE,
201 bgpPeerTable,
202 3,
203 {3, 1, 21}},
204 {BGPPEERMINROUTEADVERTISEMENTINTERVAL,
205 INTEGER,
206 RWRITE,
207 bgpPeerTable,
208 3,
209 {3, 1, 23}},
210 {BGPPEERINUPDATEELAPSEDTIME,
211 GAUGE32,
212 RONLY,
213 bgpPeerTable,
214 3,
215 {3, 1, 24}},
216 /* BGP identifier. */
217 {BGPIDENTIFIER, IPADDRESS, RONLY, bgpIdentifier, 1, {4}},
218 /* BGP received path attribute table. */
219 {BGPPATHATTRPEER, IPADDRESS, RONLY, bgpRcvdPathAttrTable, 3, {5, 1, 1}},
220 {BGPPATHATTRDESTNETWORK,
221 IPADDRESS,
222 RONLY,
223 bgpRcvdPathAttrTable,
224 3,
225 {5, 1, 2}},
226 {BGPPATHATTRORIGIN, INTEGER, RONLY, bgpRcvdPathAttrTable, 3, {5, 1, 3}},
227 {BGPPATHATTRASPATH,
228 OCTET_STRING,
229 RONLY,
230 bgpRcvdPathAttrTable,
231 3,
232 {5, 1, 4}},
233 {BGPPATHATTRNEXTHOP,
234 IPADDRESS,
235 RONLY,
236 bgpRcvdPathAttrTable,
237 3,
238 {5, 1, 5}},
239 {BGPPATHATTRINTERASMETRIC,
240 INTEGER32,
241 RONLY,
242 bgpRcvdPathAttrTable,
243 3,
244 {5, 1, 6}},
245 /* BGP-4 received path attribute table. */
246 {BGP4PATHATTRPEER, IPADDRESS, RONLY, bgp4PathAttrTable, 3, {6, 1, 1}},
247 {BGP4PATHATTRIPADDRPREFIXLEN,
248 INTEGER,
249 RONLY,
250 bgp4PathAttrTable,
251 3,
252 {6, 1, 2}},
253 {BGP4PATHATTRIPADDRPREFIX,
254 IPADDRESS,
255 RONLY,
256 bgp4PathAttrTable,
257 3,
258 {6, 1, 3}},
259 {BGP4PATHATTRORIGIN, INTEGER, RONLY, bgp4PathAttrTable, 3, {6, 1, 4}},
260 {BGP4PATHATTRASPATHSEGMENT,
261 OCTET_STRING,
262 RONLY,
263 bgp4PathAttrTable,
264 3,
265 {6, 1, 5}},
266 {BGP4PATHATTRNEXTHOP,
267 IPADDRESS,
268 RONLY,
269 bgp4PathAttrTable,
270 3,
271 {6, 1, 6}},
272 {BGP4PATHATTRMULTIEXITDISC,
273 INTEGER,
274 RONLY,
275 bgp4PathAttrTable,
276 3,
277 {6, 1, 7}},
278 {BGP4PATHATTRLOCALPREF,
279 INTEGER,
280 RONLY,
281 bgp4PathAttrTable,
282 3,
283 {6, 1, 8}},
284 {BGP4PATHATTRATOMICAGGREGATE,
285 INTEGER,
286 RONLY,
287 bgp4PathAttrTable,
288 3,
289 {6, 1, 9}},
290 {BGP4PATHATTRAGGREGATORAS,
291 INTEGER,
292 RONLY,
293 bgp4PathAttrTable,
294 3,
295 {6, 1, 10}},
296 {BGP4PATHATTRAGGREGATORADDR,
297 IPADDRESS,
298 RONLY,
299 bgp4PathAttrTable,
300 3,
301 {6, 1, 11}},
302 {BGP4PATHATTRCALCLOCALPREF,
303 INTEGER,
304 RONLY,
305 bgp4PathAttrTable,
306 3,
307 {6, 1, 12}},
308 {BGP4PATHATTRBEST, INTEGER, RONLY, bgp4PathAttrTable, 3, {6, 1, 13}},
309 {BGP4PATHATTRUNKNOWN,
310 OCTET_STRING,
311 RONLY,
312 bgp4PathAttrTable,
313 3,
314 {6, 1, 14}},
718e3744 315};
dd488a78 316
6b0655a2 317
d7c0a89a
QY
318static uint8_t *bgpVersion(struct variable *v, oid name[], size_t *length,
319 int exact, size_t *var_len,
320 WriteMethod **write_method)
718e3744 321{
d7c0a89a 322 static uint8_t version;
718e3744 323
d62a17ae 324 if (smux_header_generic(v, name, length, exact, var_len, write_method)
325 == MATCH_FAILED)
326 return NULL;
718e3744 327
d62a17ae 328 /* Retrun BGP version. Zebra bgpd only support version 4. */
329 version = (0x80 >> (BGP_VERSION_4 - 1));
718e3744 330
d62a17ae 331 /* Return octet string length 1. */
332 *var_len = 1;
d7c0a89a 333 return (uint8_t *)&version;
718e3744 334}
335
d7c0a89a
QY
336static uint8_t *bgpLocalAs(struct variable *v, oid name[], size_t *length,
337 int exact, size_t *var_len,
338 WriteMethod **write_method)
718e3744 339{
d62a17ae 340 struct bgp *bgp;
718e3744 341
d62a17ae 342 if (smux_header_generic(v, name, length, exact, var_len, write_method)
343 == MATCH_FAILED)
344 return NULL;
718e3744 345
d62a17ae 346 /* Get BGP structure. */
347 bgp = bgp_get_default();
348 if (!bgp)
349 return NULL;
718e3744 350
d62a17ae 351 return SNMP_INTEGER(bgp->as);
718e3744 352}
353
d62a17ae 354static struct peer *peer_lookup_addr_ipv4(struct in_addr *src)
718e3744 355{
d62a17ae 356 struct bgp *bgp;
357 struct peer *peer;
358 struct listnode *node;
d62a17ae 359
360 bgp = bgp_get_default();
361 if (!bgp)
362 return NULL;
363
364 for (ALL_LIST_ELEMENTS_RO(bgp->peer, node, peer)) {
2b8e62f2
PM
365 if (sockunion_family(&peer->su) != AF_INET)
366 continue;
367
368 if (sockunion2ip(&peer->su) == src->s_addr)
369 return peer;
718e3744 370 }
2b8e62f2 371
d62a17ae 372 return NULL;
718e3744 373}
374
d62a17ae 375static struct peer *bgp_peer_lookup_next(struct in_addr *src)
718e3744 376{
d62a17ae 377 struct bgp *bgp;
378 struct peer *peer;
2b8e62f2 379 struct peer *next_peer = NULL;
d62a17ae 380 struct listnode *node;
d62a17ae 381
382 bgp = bgp_get_default();
383 if (!bgp)
384 return NULL;
385
386 for (ALL_LIST_ELEMENTS_RO(bgp->peer, node, peer)) {
2b8e62f2
PM
387 if (sockunion_family(&peer->su) != AF_INET)
388 continue;
389 if (ntohl(sockunion2ip(&peer->su)) <= ntohl(src->s_addr))
390 continue;
391
392 if (!next_peer
393 || ntohl(sockunion2ip(&next_peer->su))
394 > ntohl(sockunion2ip(&peer->su))) {
395 next_peer = peer;
d62a17ae 396 }
718e3744 397 }
2b8e62f2
PM
398
399 if (next_peer) {
400 src->s_addr = sockunion2ip(&next_peer->su);
401 return next_peer;
402 }
403
d62a17ae 404 return NULL;
718e3744 405}
406
9a8382f1
DL
407/* 1.3.6.1.2.1.15.3.1.x = 10 */
408#define PEERTAB_NAMELEN 10
409
d62a17ae 410static struct peer *bgpPeerTable_lookup(struct variable *v, oid name[],
411 size_t *length, struct in_addr *addr,
412 int exact)
718e3744 413{
d62a17ae 414 struct peer *peer = NULL;
415 size_t namelen = v ? v->namelen : PEERTAB_NAMELEN;
416 int len;
718e3744 417
d62a17ae 418 if (exact) {
419 /* Check the length. */
420 if (*length - namelen != sizeof(struct in_addr))
421 return NULL;
422
423 oid2in_addr(name + namelen, IN_ADDR_SIZE, addr);
424
425 peer = peer_lookup_addr_ipv4(addr);
426 return peer;
427 } else {
428 len = *length - namelen;
429 if (len > 4)
430 len = 4;
718e3744 431
d62a17ae 432 oid2in_addr(name + namelen, len, addr);
718e3744 433
d62a17ae 434 peer = bgp_peer_lookup_next(addr);
435
436 if (peer == NULL)
437 return NULL;
438
439 oid_copy_addr(name + namelen, addr, sizeof(struct in_addr));
440 *length = sizeof(struct in_addr) + namelen;
441
442 return peer;
443 }
444 return NULL;
718e3744 445}
446
447/* BGP write methods. */
d7c0a89a
QY
448static int write_bgpPeerTable(int action, uint8_t *var_val,
449 uint8_t var_val_type, size_t var_val_len,
450 uint8_t *statP, oid *name, size_t length)
718e3744 451{
d62a17ae 452 struct in_addr addr;
453 struct peer *peer;
454 long intval;
9a8382f1 455
d62a17ae 456 if (var_val_type != ASN_INTEGER) {
457 return SNMP_ERR_WRONGTYPE;
458 }
459 if (var_val_len != sizeof(long)) {
460 return SNMP_ERR_WRONGLENGTH;
461 }
718e3744 462
d62a17ae 463 intval = *(long *)var_val;
718e3744 464
d62a17ae 465 memset(&addr, 0, sizeof(struct in_addr));
718e3744 466
d62a17ae 467 peer = bgpPeerTable_lookup(NULL, name, &length, &addr, 1);
468 if (!peer)
469 return SNMP_ERR_NOSUCHNAME;
718e3744 470
d62a17ae 471 if (action != SNMP_MSG_INTERNAL_SET_COMMIT)
472 return SNMP_ERR_NOERROR;
9a8382f1 473
d62a17ae 474 zlog_info("%s: SNMP write .%ld = %ld", peer->host,
475 (long)name[PEERTAB_NAMELEN - 1], intval);
718e3744 476
d62a17ae 477 switch (name[PEERTAB_NAMELEN - 1]) {
478 case BGPPEERADMINSTATUS:
718e3744 479#define BGP_PeerAdmin_stop 1
480#define BGP_PeerAdmin_start 2
d62a17ae 481 /* When the peer is established, */
482 if (intval == BGP_PeerAdmin_stop)
483 BGP_EVENT_ADD(peer, BGP_Stop);
484 else if (intval == BGP_PeerAdmin_start)
485 ; /* Do nothing. */
486 else
487 return SNMP_ERR_NOSUCHNAME;
488 break;
489 case BGPPEERCONNECTRETRYINTERVAL:
b90a8e13 490 peer_flag_set(peer, PEER_FLAG_TIMER_CONNECT);
d62a17ae 491 peer->connect = intval;
492 peer->v_connect = intval;
493 break;
494 case BGPPEERHOLDTIMECONFIGURED:
b90a8e13 495 peer_flag_set(peer, PEER_FLAG_TIMER);
d62a17ae 496 peer->holdtime = intval;
497 peer->v_holdtime = intval;
498 break;
499 case BGPPEERKEEPALIVECONFIGURED:
b90a8e13 500 peer_flag_set(peer, PEER_FLAG_TIMER);
d62a17ae 501 peer->keepalive = intval;
502 peer->v_keepalive = intval;
503 break;
504 case BGPPEERMINROUTEADVERTISEMENTINTERVAL:
505 peer->v_routeadv = intval;
506 break;
507 }
508 return SNMP_ERR_NOERROR;
718e3744 509}
510
d7c0a89a
QY
511static uint8_t *bgpPeerTable(struct variable *v, oid name[], size_t *length,
512 int exact, size_t *var_len,
513 WriteMethod **write_method)
718e3744 514{
d62a17ae 515 static struct in_addr addr;
516 struct peer *peer;
0112e9e0 517 uint32_t ui, uo;
d62a17ae 518
519 if (smux_header_table(v, name, length, exact, var_len, write_method)
520 == MATCH_FAILED)
521 return NULL;
522 memset(&addr, 0, sizeof(struct in_addr));
523
524 peer = bgpPeerTable_lookup(v, name, length, &addr, exact);
525 if (!peer)
526 return NULL;
527
528 switch (v->magic) {
529 case BGPPEERIDENTIFIER:
530 return SNMP_IPADDRESS(peer->remote_id);
531 break;
532 case BGPPEERSTATE:
533 return SNMP_INTEGER(peer->status);
534 break;
535 case BGPPEERADMINSTATUS:
536 *write_method = write_bgpPeerTable;
718e3744 537#define BGP_PeerAdmin_stop 1
538#define BGP_PeerAdmin_start 2
d62a17ae 539 if (CHECK_FLAG(peer->flags, PEER_FLAG_SHUTDOWN))
540 return SNMP_INTEGER(BGP_PeerAdmin_stop);
541 else
542 return SNMP_INTEGER(BGP_PeerAdmin_start);
543 break;
544 case BGPPEERNEGOTIATEDVERSION:
545 return SNMP_INTEGER(BGP_VERSION_4);
546 break;
547 case BGPPEERLOCALADDR:
548 if (peer->su_local)
549 return SNMP_IPADDRESS(peer->su_local->sin.sin_addr);
550 else
551 return SNMP_IPADDRESS(bgp_empty_addr);
552 break;
553 case BGPPEERLOCALPORT:
554 if (peer->su_local)
555 return SNMP_INTEGER(
556 ntohs(peer->su_local->sin.sin_port));
557 else
558 return SNMP_INTEGER(0);
559 break;
560 case BGPPEERREMOTEADDR:
561 if (peer->su_remote)
562 return SNMP_IPADDRESS(peer->su_remote->sin.sin_addr);
563 else
564 return SNMP_IPADDRESS(bgp_empty_addr);
565 break;
566 case BGPPEERREMOTEPORT:
567 if (peer->su_remote)
568 return SNMP_INTEGER(
569 ntohs(peer->su_remote->sin.sin_port));
570 else
571 return SNMP_INTEGER(0);
572 break;
573 case BGPPEERREMOTEAS:
574 return SNMP_INTEGER(peer->as);
575 break;
576 case BGPPEERINUPDATES:
0112e9e0
QY
577 ui = atomic_load_explicit(&peer->update_in,
578 memory_order_relaxed);
579 return SNMP_INTEGER(ui);
d62a17ae 580 break;
581 case BGPPEEROUTUPDATES:
0112e9e0
QY
582 uo = atomic_load_explicit(&peer->update_out,
583 memory_order_relaxed);
584 return SNMP_INTEGER(uo);
d62a17ae 585 break;
586 case BGPPEERINTOTALMESSAGES:
0112e9e0 587 return SNMP_INTEGER(PEER_TOTAL_RX(peer));
d62a17ae 588 break;
589 case BGPPEEROUTTOTALMESSAGES:
0112e9e0 590 return SNMP_INTEGER(PEER_TOTAL_TX(peer));
d62a17ae 591 break;
592 case BGPPEERLASTERROR: {
d7c0a89a 593 static uint8_t lasterror[2];
d62a17ae 594 lasterror[0] = peer->notify.code;
595 lasterror[1] = peer->notify.subcode;
596 *var_len = 2;
d7c0a89a 597 return (uint8_t *)&lasterror;
d62a17ae 598 } break;
599 case BGPPEERFSMESTABLISHEDTRANSITIONS:
600 return SNMP_INTEGER(peer->established);
601 break;
602 case BGPPEERFSMESTABLISHEDTIME:
603 if (peer->uptime == 0)
604 return SNMP_INTEGER(0);
605 else
606 return SNMP_INTEGER(bgp_clock() - peer->uptime);
607 break;
608 case BGPPEERCONNECTRETRYINTERVAL:
609 *write_method = write_bgpPeerTable;
610 return SNMP_INTEGER(peer->v_connect);
611 break;
612 case BGPPEERHOLDTIME:
613 return SNMP_INTEGER(peer->v_holdtime);
614 break;
615 case BGPPEERKEEPALIVE:
616 return SNMP_INTEGER(peer->v_keepalive);
617 break;
618 case BGPPEERHOLDTIMECONFIGURED:
619 *write_method = write_bgpPeerTable;
b90a8e13 620 if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER))
d62a17ae 621 return SNMP_INTEGER(peer->holdtime);
622 else
623 return SNMP_INTEGER(peer->v_holdtime);
624 break;
625 case BGPPEERKEEPALIVECONFIGURED:
626 *write_method = write_bgpPeerTable;
b90a8e13 627 if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER))
d62a17ae 628 return SNMP_INTEGER(peer->keepalive);
629 else
630 return SNMP_INTEGER(peer->v_keepalive);
631 break;
632 case BGPPEERMINROUTEADVERTISEMENTINTERVAL:
633 *write_method = write_bgpPeerTable;
634 return SNMP_INTEGER(peer->v_routeadv);
635 break;
636 case BGPPEERINUPDATEELAPSEDTIME:
637 if (peer->update_time == 0)
638 return SNMP_INTEGER(0);
639 else
640 return SNMP_INTEGER(bgp_clock() - peer->update_time);
641 break;
642 default:
643 return NULL;
644 break;
645 }
646 return NULL;
718e3744 647}
648
d7c0a89a
QY
649static uint8_t *bgpIdentifier(struct variable *v, oid name[], size_t *length,
650 int exact, size_t *var_len,
651 WriteMethod **write_method)
718e3744 652{
d62a17ae 653 struct bgp *bgp;
718e3744 654
d62a17ae 655 if (smux_header_generic(v, name, length, exact, var_len, write_method)
656 == MATCH_FAILED)
657 return NULL;
718e3744 658
d62a17ae 659 bgp = bgp_get_default();
660 if (!bgp)
661 return NULL;
718e3744 662
d62a17ae 663 return SNMP_IPADDRESS(bgp->router_id);
718e3744 664}
665
d7c0a89a
QY
666static uint8_t *bgpRcvdPathAttrTable(struct variable *v, oid name[],
667 size_t *length, int exact, size_t *var_len,
668 WriteMethod **write_method)
718e3744 669{
d62a17ae 670 /* Received Path Attribute Table. This table contains, one entry
671 per path to a network, path attributes received from all peers
672 running BGP version 3 or less. This table is obsolete, having
673 been replaced in functionality with the bgp4PathAttrTable. */
674 return NULL;
718e3744 675}
676
4b7e6066
DS
677static struct bgp_path_info *bgp4PathAttrLookup(struct variable *v, oid name[],
678 size_t *length, struct bgp *bgp,
679 struct prefix_ipv4 *addr,
680 int exact)
718e3744 681{
d62a17ae 682 oid *offset;
683 int offsetlen;
9b6d8fcf 684 struct bgp_path_info *path;
4b7e6066 685 struct bgp_path_info *min;
d62a17ae 686 struct bgp_node *rn;
687 union sockunion su;
688 unsigned int len;
689 struct in_addr paddr;
690
691 sockunion_init(&su);
692
693#define BGP_PATHATTR_ENTRY_OFFSET (IN_ADDR_SIZE + 1 + IN_ADDR_SIZE)
694
695 if (exact) {
696 if (*length - v->namelen != BGP_PATHATTR_ENTRY_OFFSET)
697 return NULL;
698
699 /* Set OID offset for prefix. */
700 offset = name + v->namelen;
701 oid2in_addr(offset, IN_ADDR_SIZE, &addr->prefix);
702 offset += IN_ADDR_SIZE;
703
704 /* Prefix length. */
705 addr->prefixlen = *offset;
706 offset++;
707
708 /* Peer address. */
709 su.sin.sin_family = AF_INET;
710 oid2in_addr(offset, IN_ADDR_SIZE, &su.sin.sin_addr);
711
712 /* Lookup node. */
713 rn = bgp_node_lookup(bgp->rib[AFI_IP][SAFI_UNICAST],
714 (struct prefix *)addr);
715 if (rn) {
716 bgp_unlock_node(rn);
717
2b964e86 718 for (path = bgp_node_get_bgp_path_info(rn); path;
6f94b685 719 path = path->next)
9b6d8fcf
DS
720 if (sockunion_same(&path->peer->su, &su))
721 return path;
d62a17ae 722 }
723 } else {
724 offset = name + v->namelen;
725 offsetlen = *length - v->namelen;
726 len = offsetlen;
718e3744 727
d62a17ae 728 if (offsetlen == 0)
729 rn = bgp_table_top(bgp->rib[AFI_IP][SAFI_UNICAST]);
730 else {
731 if (len > IN_ADDR_SIZE)
732 len = IN_ADDR_SIZE;
718e3744 733
d62a17ae 734 oid2in_addr(offset, len, &addr->prefix);
718e3744 735
d62a17ae 736 offset += IN_ADDR_SIZE;
737 offsetlen -= IN_ADDR_SIZE;
718e3744 738
d62a17ae 739 if (offsetlen > 0)
740 addr->prefixlen = *offset;
741 else
742 addr->prefixlen = len * 8;
718e3744 743
d62a17ae 744 rn = bgp_node_get(bgp->rib[AFI_IP][SAFI_UNICAST],
745 (struct prefix *)addr);
718e3744 746
d62a17ae 747 offset++;
748 offsetlen--;
749 }
718e3744 750
d62a17ae 751 if (offsetlen > 0) {
752 len = offsetlen;
753 if (len > IN_ADDR_SIZE)
754 len = IN_ADDR_SIZE;
755
756 oid2in_addr(offset, len, &paddr);
757 } else
758 paddr.s_addr = 0;
759
760 if (!rn)
761 return NULL;
762
763 do {
764 min = NULL;
765
2b964e86 766 for (path = bgp_node_get_bgp_path_info(rn); path;
6f94b685 767 path = path->next) {
9b6d8fcf 768 if (path->peer->su.sin.sin_family == AF_INET
d62a17ae 769 && ntohl(paddr.s_addr)
9b6d8fcf 770 < ntohl(path->peer->su.sin
d62a17ae 771 .sin_addr
772 .s_addr)) {
773 if (min) {
9b6d8fcf 774 if (ntohl(path->peer->su.sin
d62a17ae 775 .sin_addr
776 .s_addr)
777 < ntohl(min->peer->su.sin
778 .sin_addr
779 .s_addr))
9b6d8fcf 780 min = path;
d62a17ae 781 } else
9b6d8fcf 782 min = path;
d62a17ae 783 }
784 }
785
786 if (min) {
787 *length =
788 v->namelen + BGP_PATHATTR_ENTRY_OFFSET;
789
790 offset = name + v->namelen;
791 oid_copy_addr(offset, &rn->p.u.prefix4,
792 IN_ADDR_SIZE);
793 offset += IN_ADDR_SIZE;
794 *offset = rn->p.prefixlen;
795 offset++;
796 oid_copy_addr(offset,
797 &min->peer->su.sin.sin_addr,
798 IN_ADDR_SIZE);
799 addr->prefix = rn->p.u.prefix4;
800 addr->prefixlen = rn->p.prefixlen;
801
802 bgp_unlock_node(rn);
803
804 return min;
805 }
806
807 paddr.s_addr = 0;
808 } while ((rn = bgp_route_next(rn)) != NULL);
718e3744 809 }
d62a17ae 810 return NULL;
718e3744 811}
812
d7c0a89a
QY
813static uint8_t *bgp4PathAttrTable(struct variable *v, oid name[],
814 size_t *length, int exact, size_t *var_len,
815 WriteMethod **write_method)
718e3744 816{
d62a17ae 817 struct bgp *bgp;
9b6d8fcf 818 struct bgp_path_info *path;
d62a17ae 819 struct prefix_ipv4 addr;
820
821 bgp = bgp_get_default();
822 if (!bgp)
823 return NULL;
824
825 if (smux_header_table(v, name, length, exact, var_len, write_method)
826 == MATCH_FAILED)
827 return NULL;
828 memset(&addr, 0, sizeof(struct prefix_ipv4));
829
9b6d8fcf
DS
830 path = bgp4PathAttrLookup(v, name, length, bgp, &addr, exact);
831 if (!path)
d62a17ae 832 return NULL;
833
834 switch (v->magic) {
835 case BGP4PATHATTRPEER: /* 1 */
9b6d8fcf 836 return SNMP_IPADDRESS(path->peer->su.sin.sin_addr);
d62a17ae 837 break;
838 case BGP4PATHATTRIPADDRPREFIXLEN: /* 2 */
839 return SNMP_INTEGER(addr.prefixlen);
840 break;
841 case BGP4PATHATTRIPADDRPREFIX: /* 3 */
842 return SNMP_IPADDRESS(addr.prefix);
843 break;
844 case BGP4PATHATTRORIGIN: /* 4 */
9b6d8fcf 845 return SNMP_INTEGER(path->attr->origin);
d62a17ae 846 break;
847 case BGP4PATHATTRASPATHSEGMENT: /* 5 */
9b6d8fcf 848 return aspath_snmp_pathseg(path->attr->aspath, var_len);
d62a17ae 849 break;
850 case BGP4PATHATTRNEXTHOP: /* 6 */
9b6d8fcf 851 return SNMP_IPADDRESS(path->attr->nexthop);
d62a17ae 852 break;
853 case BGP4PATHATTRMULTIEXITDISC: /* 7 */
9b6d8fcf 854 return SNMP_INTEGER(path->attr->med);
d62a17ae 855 break;
856 case BGP4PATHATTRLOCALPREF: /* 8 */
9b6d8fcf 857 return SNMP_INTEGER(path->attr->local_pref);
d62a17ae 858 break;
859 case BGP4PATHATTRATOMICAGGREGATE: /* 9 */
860 return SNMP_INTEGER(1);
861 break;
862 case BGP4PATHATTRAGGREGATORAS: /* 10 */
9b6d8fcf 863 return SNMP_INTEGER(path->attr->aggregator_as);
d62a17ae 864 break;
865 case BGP4PATHATTRAGGREGATORADDR: /* 11 */
9b6d8fcf 866 return SNMP_IPADDRESS(path->attr->aggregator_addr);
d62a17ae 867 break;
868 case BGP4PATHATTRCALCLOCALPREF: /* 12 */
869 return SNMP_INTEGER(-1);
870 break;
871 case BGP4PATHATTRBEST: /* 13 */
718e3744 872#define BGP4_PathAttrBest_false 1
873#define BGP4_PathAttrBest_true 2
a9a0989b 874 if (CHECK_FLAG(path->flags, BGP_PATH_SELECTED))
d62a17ae 875 return SNMP_INTEGER(BGP4_PathAttrBest_true);
876 else
877 return SNMP_INTEGER(BGP4_PathAttrBest_false);
878 break;
879 case BGP4PATHATTRUNKNOWN: /* 14 */
880 *var_len = 0;
881 return NULL;
882 break;
883 }
884 return NULL;
718e3744 885}
6b0655a2 886
718e3744 887/* BGP Traps. */
d62a17ae 888static struct trap_object bgpTrapList[] = {{3, {3, 1, BGPPEERLASTERROR}},
889 {3, {3, 1, BGPPEERSTATE}}};
718e3744 890
d62a17ae 891static int bgpTrapEstablished(struct peer *peer)
718e3744 892{
d62a17ae 893 int ret;
894 struct in_addr addr;
895 oid index[sizeof(oid) * IN_ADDR_SIZE];
896
897 ret = inet_aton(peer->host, &addr);
898 if (ret == 0)
899 return 0;
900
901 oid_copy_addr(index, &addr, IN_ADDR_SIZE);
902
903 smux_trap(bgp_variables, sizeof bgp_variables / sizeof(struct variable),
904 bgp_trap_oid, sizeof bgp_trap_oid / sizeof(oid), bgp_oid,
905 sizeof bgp_oid / sizeof(oid), index, IN_ADDR_SIZE,
906 bgpTrapList, sizeof bgpTrapList / sizeof(struct trap_object),
907 BGPESTABLISHED);
908 return 0;
718e3744 909}
910
d62a17ae 911static int bgpTrapBackwardTransition(struct peer *peer)
718e3744 912{
d62a17ae 913 int ret;
914 struct in_addr addr;
915 oid index[sizeof(oid) * IN_ADDR_SIZE];
916
917 ret = inet_aton(peer->host, &addr);
918 if (ret == 0)
919 return 0;
920
921 oid_copy_addr(index, &addr, IN_ADDR_SIZE);
922
923 smux_trap(bgp_variables, sizeof bgp_variables / sizeof(struct variable),
924 bgp_trap_oid, sizeof bgp_trap_oid / sizeof(oid), bgp_oid,
925 sizeof bgp_oid / sizeof(oid), index, IN_ADDR_SIZE,
926 bgpTrapList, sizeof bgpTrapList / sizeof(struct trap_object),
927 BGPBACKWARDTRANSITION);
928 return 0;
718e3744 929}
930
d62a17ae 931static int bgp_snmp_init(struct thread_master *tm)
5986b66b 932{
d62a17ae 933 smux_init(tm);
934 REGISTER_MIB("mibII/bgp", bgp_variables, variable, bgp_oid);
935 return 0;
5986b66b 936}
3012671f 937
d62a17ae 938static int bgp_snmp_module_init(void)
718e3744 939{
d62a17ae 940 hook_register(peer_established, bgpTrapEstablished);
941 hook_register(peer_backward_transition, bgpTrapBackwardTransition);
942 hook_register(frr_late_init, bgp_snmp_init);
943 return 0;
718e3744 944}
5986b66b 945
d62a17ae 946FRR_MODULE_SETUP(.name = "bgpd_snmp", .version = FRR_VERSION,
947 .description = "bgpd AgentX SNMP module",
948 .init = bgp_snmp_module_init)