bgp_snmp.c 24 KB

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