bgp_snmp.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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. #include <net-snmp/net-snmp-config.h>
  19. #include <net-snmp/net-snmp-includes.h>
  20. #include "if.h"
  21. #include "log.h"
  22. #include "prefix.h"
  23. #include "command.h"
  24. #include "thread.h"
  25. #include "smux.h"
  26. #include "bgpd/bgpd.h"
  27. #include "bgpd/bgp_table.h"
  28. #include "bgpd/bgp_aspath.h"
  29. #include "bgpd/bgp_attr.h"
  30. #include "bgpd/bgp_route.h"
  31. #include "bgpd/bgp_fsm.h"
  32. #include "bgpd/bgp_snmp.h"
  33. /* BGP4-MIB described in RFC1657. */
  34. #define BGP4MIB 1,3,6,1,2,1,15
  35. /* BGP TRAP. */
  36. #define BGPESTABLISHED 1
  37. #define BGPBACKWARDTRANSITION 2
  38. /* BGP MIB bgpVersion. */
  39. #define BGPVERSION 0
  40. /* BGP MIB bgpLocalAs. */
  41. #define BGPLOCALAS 0
  42. /* BGP MIB bgpPeerTable. */
  43. #define BGPPEERIDENTIFIER 1
  44. #define BGPPEERSTATE 2
  45. #define BGPPEERADMINSTATUS 3
  46. #define BGPPEERNEGOTIATEDVERSION 4
  47. #define BGPPEERLOCALADDR 5
  48. #define BGPPEERLOCALPORT 6
  49. #define BGPPEERREMOTEADDR 7
  50. #define BGPPEERREMOTEPORT 8
  51. #define BGPPEERREMOTEAS 9
  52. #define BGPPEERINUPDATES 10
  53. #define BGPPEEROUTUPDATES 11
  54. #define BGPPEERINTOTALMESSAGES 12
  55. #define BGPPEEROUTTOTALMESSAGES 13
  56. #define BGPPEERLASTERROR 14
  57. #define BGPPEERFSMESTABLISHEDTRANSITIONS 15
  58. #define BGPPEERFSMESTABLISHEDTIME 16
  59. #define BGPPEERCONNECTRETRYINTERVAL 17
  60. #define BGPPEERHOLDTIME 18
  61. #define BGPPEERKEEPALIVE 19
  62. #define BGPPEERHOLDTIMECONFIGURED 20
  63. #define BGPPEERKEEPALIVECONFIGURED 21
  64. #define BGPPEERMINASORIGINATIONINTERVAL 22
  65. #define BGPPEERMINROUTEADVERTISEMENTINTERVAL 23
  66. #define BGPPEERINUPDATEELAPSEDTIME 24
  67. /* BGP MIB bgpIdentifier. */
  68. #define BGPIDENTIFIER 0
  69. /* BGP MIB bgpRcvdPathAttrTable */
  70. #define BGPPATHATTRPEER 1
  71. #define BGPPATHATTRDESTNETWORK 2
  72. #define BGPPATHATTRORIGIN 3
  73. #define BGPPATHATTRASPATH 4
  74. #define BGPPATHATTRNEXTHOP 5
  75. #define BGPPATHATTRINTERASMETRIC 6
  76. /* BGP MIB bgp4PathAttrTable. */
  77. #define BGP4PATHATTRPEER 1
  78. #define BGP4PATHATTRIPADDRPREFIXLEN 2
  79. #define BGP4PATHATTRIPADDRPREFIX 3
  80. #define BGP4PATHATTRORIGIN 4
  81. #define BGP4PATHATTRASPATHSEGMENT 5
  82. #define BGP4PATHATTRNEXTHOP 6
  83. #define BGP4PATHATTRMULTIEXITDISC 7
  84. #define BGP4PATHATTRLOCALPREF 8
  85. #define BGP4PATHATTRATOMICAGGREGATE 9
  86. #define BGP4PATHATTRAGGREGATORAS 10
  87. #define BGP4PATHATTRAGGREGATORADDR 11
  88. #define BGP4PATHATTRCALCLOCALPREF 12
  89. #define BGP4PATHATTRBEST 13
  90. #define BGP4PATHATTRUNKNOWN 14
  91. /* SNMP value hack. */
  92. #define INTEGER ASN_INTEGER
  93. #define INTEGER32 ASN_INTEGER
  94. #define COUNTER32 ASN_COUNTER
  95. #define OCTET_STRING ASN_OCTET_STR
  96. #define IPADDRESS ASN_IPADDRESS
  97. #define GAUGE32 ASN_UNSIGNED
  98. /* Declare static local variables for convenience. */
  99. SNMP_LOCAL_VARIABLES
  100. /* BGP-MIB instances. */
  101. oid bgp_oid [] = { BGP4MIB };
  102. oid bgp_trap_oid [] = { BGP4MIB, 0 };
  103. /* IP address 0.0.0.0. */
  104. static struct in_addr bgp_empty_addr = {0};
  105. /* Hook functions. */
  106. static u_char *bgpVersion (struct variable *, oid [], size_t *, int,
  107. size_t *, WriteMethod **);
  108. static u_char *bgpLocalAs (struct variable *, oid [], size_t *,
  109. int, size_t *, WriteMethod **);
  110. static u_char *bgpPeerTable (struct variable *, oid [], size_t *,
  111. int, size_t *, WriteMethod **);
  112. static u_char *bgpRcvdPathAttrTable (struct variable *, oid [], size_t *,
  113. int, size_t *, WriteMethod **);
  114. static u_char *bgpIdentifier (struct variable *, oid [], size_t *,
  115. int, size_t *, WriteMethod **);
  116. static u_char *bgp4PathAttrTable (struct variable *, oid [], size_t *,
  117. int, size_t *, WriteMethod **);
  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. static struct peer *
  251. peer_lookup_addr_ipv4 (struct in_addr *src)
  252. {
  253. struct bgp *bgp;
  254. struct peer *peer;
  255. struct listnode *node;
  256. struct in_addr addr;
  257. int ret;
  258. bgp = bgp_get_default ();
  259. if (! bgp)
  260. return NULL;
  261. for (ALL_LIST_ELEMENTS_RO (bgp->peer, node, peer))
  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. static struct peer *
  273. bgp_peer_lookup_next (struct in_addr *src)
  274. {
  275. struct bgp *bgp;
  276. struct peer *peer;
  277. struct listnode *node;
  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. for (ALL_LIST_ELEMENTS_RO (bgp->peer, node, peer))
  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. static 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. static 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. size_t 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. static 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. if (smux_header_table(v, name, length, exact, var_len, write_method)
  402. == MATCH_FAILED)
  403. return NULL;
  404. memset (&addr, 0, sizeof (struct in_addr));
  405. peer = bgpPeerTable_lookup (v, name, length, &addr, exact);
  406. if (! peer)
  407. return NULL;
  408. switch (v->magic)
  409. {
  410. case BGPPEERIDENTIFIER:
  411. return SNMP_IPADDRESS (peer->remote_id);
  412. break;
  413. case BGPPEERSTATE:
  414. return SNMP_INTEGER (peer->status);
  415. break;
  416. case BGPPEERADMINSTATUS:
  417. *write_method = write_bgpPeerTable;
  418. #define BGP_PeerAdmin_stop 1
  419. #define BGP_PeerAdmin_start 2
  420. if (CHECK_FLAG (peer->flags, PEER_FLAG_SHUTDOWN))
  421. return SNMP_INTEGER (BGP_PeerAdmin_stop);
  422. else
  423. return SNMP_INTEGER (BGP_PeerAdmin_start);
  424. break;
  425. case BGPPEERNEGOTIATEDVERSION:
  426. return SNMP_INTEGER (BGP_VERSION_4);
  427. break;
  428. case BGPPEERLOCALADDR:
  429. if (peer->su_local)
  430. return SNMP_IPADDRESS (peer->su_local->sin.sin_addr);
  431. else
  432. return SNMP_IPADDRESS (bgp_empty_addr);
  433. break;
  434. case BGPPEERLOCALPORT:
  435. if (peer->su_local)
  436. return SNMP_INTEGER (ntohs (peer->su_local->sin.sin_port));
  437. else
  438. return SNMP_INTEGER (0);
  439. break;
  440. case BGPPEERREMOTEADDR:
  441. if (peer->su_remote)
  442. return SNMP_IPADDRESS (peer->su_remote->sin.sin_addr);
  443. else
  444. return SNMP_IPADDRESS (bgp_empty_addr);
  445. break;
  446. case BGPPEERREMOTEPORT:
  447. if (peer->su_remote)
  448. return SNMP_INTEGER (ntohs (peer->su_remote->sin.sin_port));
  449. else
  450. return SNMP_INTEGER (0);
  451. break;
  452. case BGPPEERREMOTEAS:
  453. return SNMP_INTEGER (peer->as);
  454. break;
  455. case BGPPEERINUPDATES:
  456. return SNMP_INTEGER (peer->update_in);
  457. break;
  458. case BGPPEEROUTUPDATES:
  459. return SNMP_INTEGER (peer->update_out);
  460. break;
  461. case BGPPEERINTOTALMESSAGES:
  462. return SNMP_INTEGER (peer->open_in + peer->update_in
  463. + peer->keepalive_in + peer->notify_in
  464. + peer->refresh_in + peer->dynamic_cap_in);
  465. break;
  466. case BGPPEEROUTTOTALMESSAGES:
  467. return SNMP_INTEGER (peer->open_out + peer->update_out
  468. + peer->keepalive_out + peer->notify_out
  469. + peer->refresh_out + peer->dynamic_cap_out);
  470. break;
  471. case BGPPEERLASTERROR:
  472. {
  473. static u_char lasterror[2];
  474. lasterror[0] = peer->notify.code;
  475. lasterror[1] = peer->notify.subcode;
  476. *var_len = 2;
  477. return (u_char *)&lasterror;
  478. }
  479. break;
  480. case BGPPEERFSMESTABLISHEDTRANSITIONS:
  481. return SNMP_INTEGER (peer->established);
  482. break;
  483. case BGPPEERFSMESTABLISHEDTIME:
  484. if (peer->uptime == 0)
  485. return SNMP_INTEGER (0);
  486. else
  487. return SNMP_INTEGER (bgp_clock () - peer->uptime);
  488. break;
  489. case BGPPEERCONNECTRETRYINTERVAL:
  490. *write_method = write_bgpPeerTable;
  491. return SNMP_INTEGER (peer->v_connect);
  492. break;
  493. case BGPPEERHOLDTIME:
  494. return SNMP_INTEGER (peer->v_holdtime);
  495. break;
  496. case BGPPEERKEEPALIVE:
  497. return SNMP_INTEGER (peer->v_keepalive);
  498. break;
  499. case BGPPEERHOLDTIMECONFIGURED:
  500. *write_method = write_bgpPeerTable;
  501. if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
  502. return SNMP_INTEGER (peer->holdtime);
  503. else
  504. return SNMP_INTEGER (peer->v_holdtime);
  505. break;
  506. case BGPPEERKEEPALIVECONFIGURED:
  507. *write_method = write_bgpPeerTable;
  508. if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
  509. return SNMP_INTEGER (peer->keepalive);
  510. else
  511. return SNMP_INTEGER (peer->v_keepalive);
  512. break;
  513. case BGPPEERMINASORIGINATIONINTERVAL:
  514. *write_method = write_bgpPeerTable;
  515. return SNMP_INTEGER (peer->v_asorig);
  516. break;
  517. case BGPPEERMINROUTEADVERTISEMENTINTERVAL:
  518. *write_method = write_bgpPeerTable;
  519. return SNMP_INTEGER (peer->v_routeadv);
  520. break;
  521. case BGPPEERINUPDATEELAPSEDTIME:
  522. if (peer->update_time == 0)
  523. return SNMP_INTEGER (0);
  524. else
  525. return SNMP_INTEGER (bgp_clock () - peer->update_time);
  526. break;
  527. default:
  528. return NULL;
  529. break;
  530. }
  531. return NULL;
  532. }
  533. static u_char *
  534. bgpIdentifier (struct variable *v, oid name[], size_t *length,
  535. int exact, size_t *var_len, WriteMethod **write_method)
  536. {
  537. struct bgp *bgp;
  538. if (smux_header_generic(v, name, length, exact, var_len, write_method)
  539. == MATCH_FAILED)
  540. return NULL;
  541. bgp = bgp_get_default ();
  542. if (!bgp)
  543. return NULL;
  544. return SNMP_IPADDRESS (bgp->router_id);
  545. }
  546. static u_char *
  547. bgpRcvdPathAttrTable (struct variable *v, oid name[], size_t *length,
  548. int exact, size_t *var_len, WriteMethod **write_method)
  549. {
  550. /* Received Path Attribute Table. This table contains, one entry
  551. per path to a network, path attributes received from all peers
  552. running BGP version 3 or less. This table is obsolete, having
  553. been replaced in functionality with the bgp4PathAttrTable. */
  554. return NULL;
  555. }
  556. static struct bgp_info *
  557. bgp4PathAttrLookup (struct variable *v, oid name[], size_t *length,
  558. struct bgp *bgp, struct prefix_ipv4 *addr, int exact)
  559. {
  560. oid *offset;
  561. int offsetlen;
  562. struct bgp_info *binfo;
  563. struct bgp_info *min;
  564. struct bgp_node *rn;
  565. union sockunion su;
  566. unsigned int len;
  567. struct in_addr paddr;
  568. #define BGP_PATHATTR_ENTRY_OFFSET \
  569. (IN_ADDR_SIZE + 1 + IN_ADDR_SIZE)
  570. if (exact)
  571. {
  572. if (*length - v->namelen != BGP_PATHATTR_ENTRY_OFFSET)
  573. return NULL;
  574. /* Set OID offset for prefix. */
  575. offset = name + v->namelen;
  576. oid2in_addr (offset, IN_ADDR_SIZE, &addr->prefix);
  577. offset += IN_ADDR_SIZE;
  578. /* Prefix length. */
  579. addr->prefixlen = *offset;
  580. offset++;
  581. /* Peer address. */
  582. su.sin.sin_family = AF_INET;
  583. oid2in_addr (offset, IN_ADDR_SIZE, &su.sin.sin_addr);
  584. /* Lookup node. */
  585. rn = bgp_node_lookup (bgp->rib[AFI_IP][SAFI_UNICAST],
  586. (struct prefix *) addr);
  587. if (rn)
  588. {
  589. bgp_unlock_node (rn);
  590. for (binfo = rn->info; binfo; binfo = binfo->next)
  591. if (sockunion_same (&binfo->peer->su, &su))
  592. return binfo;
  593. }
  594. }
  595. else
  596. {
  597. offset = name + v->namelen;
  598. offsetlen = *length - v->namelen;
  599. len = offsetlen;
  600. if (offsetlen == 0)
  601. rn = bgp_table_top (bgp->rib[AFI_IP][SAFI_UNICAST]);
  602. else
  603. {
  604. if (len > IN_ADDR_SIZE)
  605. len = IN_ADDR_SIZE;
  606. oid2in_addr (offset, len, &addr->prefix);
  607. offset += IN_ADDR_SIZE;
  608. offsetlen -= IN_ADDR_SIZE;
  609. if (offsetlen > 0)
  610. addr->prefixlen = *offset;
  611. else
  612. addr->prefixlen = len * 8;
  613. rn = bgp_node_get (bgp->rib[AFI_IP][SAFI_UNICAST],
  614. (struct prefix *) addr);
  615. offset++;
  616. offsetlen--;
  617. }
  618. if (offsetlen > 0)
  619. {
  620. len = offsetlen;
  621. if (len > IN_ADDR_SIZE)
  622. len = IN_ADDR_SIZE;
  623. oid2in_addr (offset, len, &paddr);
  624. }
  625. else
  626. paddr.s_addr = 0;
  627. if (! rn)
  628. return NULL;
  629. do
  630. {
  631. min = NULL;
  632. for (binfo = rn->info; binfo; binfo = binfo->next)
  633. {
  634. if (binfo->peer->su.sin.sin_family == AF_INET
  635. && ntohl (paddr.s_addr)
  636. < ntohl (binfo->peer->su.sin.sin_addr.s_addr))
  637. {
  638. if (min)
  639. {
  640. if (ntohl (binfo->peer->su.sin.sin_addr.s_addr)
  641. < ntohl (min->peer->su.sin.sin_addr.s_addr))
  642. min = binfo;
  643. }
  644. else
  645. min = binfo;
  646. }
  647. }
  648. if (min)
  649. {
  650. *length = v->namelen + BGP_PATHATTR_ENTRY_OFFSET;
  651. offset = name + v->namelen;
  652. oid_copy_addr (offset, &rn->p.u.prefix4, IN_ADDR_SIZE);
  653. offset += IN_ADDR_SIZE;
  654. *offset = rn->p.prefixlen;
  655. offset++;
  656. oid_copy_addr (offset, &min->peer->su.sin.sin_addr,
  657. IN_ADDR_SIZE);
  658. addr->prefix = rn->p.u.prefix4;
  659. addr->prefixlen = rn->p.prefixlen;
  660. bgp_unlock_node (rn);
  661. return min;
  662. }
  663. paddr.s_addr = 0;
  664. }
  665. while ((rn = bgp_route_next (rn)) != NULL);
  666. }
  667. return NULL;
  668. }
  669. static u_char *
  670. bgp4PathAttrTable (struct variable *v, oid name[], size_t *length,
  671. int exact, size_t *var_len, WriteMethod **write_method)
  672. {
  673. struct bgp *bgp;
  674. struct bgp_info *binfo;
  675. struct prefix_ipv4 addr;
  676. bgp = bgp_get_default ();
  677. if (! bgp)
  678. return NULL;
  679. if (smux_header_table(v, name, length, exact, var_len, write_method)
  680. == MATCH_FAILED)
  681. return NULL;
  682. memset (&addr, 0, sizeof (struct prefix_ipv4));
  683. binfo = bgp4PathAttrLookup (v, name, length, bgp, &addr, exact);
  684. if (! binfo)
  685. return NULL;
  686. switch (v->magic)
  687. {
  688. case BGP4PATHATTRPEER: /* 1 */
  689. return SNMP_IPADDRESS (binfo->peer->su.sin.sin_addr);
  690. break;
  691. case BGP4PATHATTRIPADDRPREFIXLEN: /* 2 */
  692. return SNMP_INTEGER (addr.prefixlen);
  693. break;
  694. case BGP4PATHATTRIPADDRPREFIX: /* 3 */
  695. return SNMP_IPADDRESS (addr.prefix);
  696. break;
  697. case BGP4PATHATTRORIGIN: /* 4 */
  698. return SNMP_INTEGER (binfo->attr->origin);
  699. break;
  700. case BGP4PATHATTRASPATHSEGMENT: /* 5 */
  701. return aspath_snmp_pathseg (binfo->attr->aspath, var_len);
  702. break;
  703. case BGP4PATHATTRNEXTHOP: /* 6 */
  704. return SNMP_IPADDRESS (binfo->attr->nexthop);
  705. break;
  706. case BGP4PATHATTRMULTIEXITDISC: /* 7 */
  707. return SNMP_INTEGER (binfo->attr->med);
  708. break;
  709. case BGP4PATHATTRLOCALPREF: /* 8 */
  710. return SNMP_INTEGER (binfo->attr->local_pref);
  711. break;
  712. case BGP4PATHATTRATOMICAGGREGATE: /* 9 */
  713. return SNMP_INTEGER (1);
  714. break;
  715. case BGP4PATHATTRAGGREGATORAS: /* 10 */
  716. if (binfo->attr->extra)
  717. return SNMP_INTEGER (binfo->attr->extra->aggregator_as);
  718. else
  719. return SNMP_INTEGER (0);
  720. break;
  721. case BGP4PATHATTRAGGREGATORADDR: /* 11 */
  722. if (binfo->attr->extra)
  723. return SNMP_IPADDRESS (binfo->attr->extra->aggregator_addr);
  724. else
  725. return SNMP_INTEGER (0);
  726. break;
  727. case BGP4PATHATTRCALCLOCALPREF: /* 12 */
  728. return SNMP_INTEGER (-1);
  729. break;
  730. case BGP4PATHATTRBEST: /* 13 */
  731. #define BGP4_PathAttrBest_false 1
  732. #define BGP4_PathAttrBest_true 2
  733. if (CHECK_FLAG (binfo->flags, BGP_INFO_SELECTED))
  734. return SNMP_INTEGER (BGP4_PathAttrBest_true);
  735. else
  736. return SNMP_INTEGER (BGP4_PathAttrBest_false);
  737. break;
  738. case BGP4PATHATTRUNKNOWN: /* 14 */
  739. *var_len = 0;
  740. return NULL;
  741. break;
  742. }
  743. return NULL;
  744. }
  745. /* BGP Traps. */
  746. struct trap_object bgpTrapList[] =
  747. {
  748. {3, {3, 1, BGPPEERLASTERROR}},
  749. {3, {3, 1, BGPPEERSTATE}}
  750. };
  751. void
  752. bgpTrapEstablished (struct peer *peer)
  753. {
  754. int ret;
  755. struct in_addr addr;
  756. oid index[sizeof (oid) * IN_ADDR_SIZE];
  757. ret = inet_aton (peer->host, &addr);
  758. if (ret == 0)
  759. return;
  760. oid_copy_addr (index, &addr, IN_ADDR_SIZE);
  761. smux_trap (bgp_variables, sizeof bgp_variables / sizeof (struct variable),
  762. bgp_trap_oid, sizeof bgp_trap_oid / sizeof (oid),
  763. bgp_oid, sizeof bgp_oid / sizeof (oid),
  764. index, IN_ADDR_SIZE,
  765. bgpTrapList, sizeof bgpTrapList / sizeof (struct trap_object),
  766. BGPESTABLISHED);
  767. }
  768. void
  769. bgpTrapBackwardTransition (struct peer *peer)
  770. {
  771. int ret;
  772. struct in_addr addr;
  773. oid index[sizeof (oid) * IN_ADDR_SIZE];
  774. ret = inet_aton (peer->host, &addr);
  775. if (ret == 0)
  776. return;
  777. oid_copy_addr (index, &addr, IN_ADDR_SIZE);
  778. smux_trap (bgp_variables, sizeof bgp_variables / sizeof (struct variable),
  779. bgp_trap_oid, sizeof bgp_trap_oid / sizeof (oid),
  780. bgp_oid, sizeof bgp_oid / sizeof (oid),
  781. index, IN_ADDR_SIZE,
  782. bgpTrapList, sizeof bgpTrapList / sizeof (struct trap_object),
  783. BGPBACKWARDTRANSITION);
  784. }
  785. void
  786. bgp_snmp_init (void)
  787. {
  788. smux_init (bm->master);
  789. REGISTER_MIB("mibII/bgp", bgp_variables, variable, bgp_oid);
  790. }
  791. #endif /* HAVE_SNMP */