bgp_dump.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /* BGP-4 dump routine
  2. Copyright (C) 1999 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. #include "log.h"
  18. #include "stream.h"
  19. #include "sockunion.h"
  20. #include "command.h"
  21. #include "prefix.h"
  22. #include "thread.h"
  23. #include "linklist.h"
  24. #include "bgpd/bgp_table.h"
  25. #include "bgpd/bgpd.h"
  26. #include "bgpd/bgp_route.h"
  27. #include "bgpd/bgp_attr.h"
  28. #include "bgpd/bgp_dump.h"
  29. enum bgp_dump_type
  30. {
  31. BGP_DUMP_ALL,
  32. BGP_DUMP_ALL_ET,
  33. BGP_DUMP_UPDATES,
  34. BGP_DUMP_UPDATES_ET,
  35. BGP_DUMP_ROUTES
  36. };
  37. static const struct bgp_dump_type_map {
  38. enum bgp_dump_type type;
  39. const char *str;
  40. } bgp_dump_type_map[] =
  41. {
  42. {BGP_DUMP_ALL, "all"},
  43. {BGP_DUMP_ALL_ET, "all-et"},
  44. {BGP_DUMP_UPDATES, "updates"},
  45. {BGP_DUMP_UPDATES_ET, "updates-et"},
  46. {BGP_DUMP_ROUTES, "routes-mrt"},
  47. {0, NULL},
  48. };
  49. enum MRT_MSG_TYPES {
  50. MSG_NULL,
  51. MSG_START, /* sender is starting up */
  52. MSG_DIE, /* receiver should shut down */
  53. MSG_I_AM_DEAD, /* sender is shutting down */
  54. MSG_PEER_DOWN, /* sender's peer is down */
  55. MSG_PROTOCOL_BGP, /* msg is a BGP packet */
  56. MSG_PROTOCOL_RIP, /* msg is a RIP packet */
  57. MSG_PROTOCOL_IDRP, /* msg is an IDRP packet */
  58. MSG_PROTOCOL_RIPNG, /* msg is a RIPNG packet */
  59. MSG_PROTOCOL_BGP4PLUS, /* msg is a BGP4+ packet */
  60. MSG_PROTOCOL_BGP4PLUS_01, /* msg is a BGP4+ (draft 01) packet */
  61. MSG_PROTOCOL_OSPF, /* msg is an OSPF packet */
  62. MSG_TABLE_DUMP, /* routing table dump */
  63. MSG_TABLE_DUMP_V2 /* routing table dump, version 2 */
  64. };
  65. struct bgp_dump
  66. {
  67. enum bgp_dump_type type;
  68. char *filename;
  69. FILE *fp;
  70. unsigned int interval;
  71. char *interval_str;
  72. struct thread *t_interval;
  73. };
  74. static int bgp_dump_unset (struct vty *vty, struct bgp_dump *bgp_dump);
  75. static int bgp_dump_interval_func (struct thread *);
  76. /* BGP packet dump output buffer. */
  77. struct stream *bgp_dump_obuf;
  78. /* BGP dump strucuture for 'dump bgp all' */
  79. struct bgp_dump bgp_dump_all;
  80. /* BGP dump structure for 'dump bgp updates' */
  81. struct bgp_dump bgp_dump_updates;
  82. /* BGP dump structure for 'dump bgp routes' */
  83. struct bgp_dump bgp_dump_routes;
  84. static FILE *
  85. bgp_dump_open_file (struct bgp_dump *bgp_dump)
  86. {
  87. int ret;
  88. time_t clock;
  89. struct tm *tm;
  90. char fullpath[MAXPATHLEN];
  91. char realpath[MAXPATHLEN];
  92. mode_t oldumask;
  93. time (&clock);
  94. tm = localtime (&clock);
  95. if (bgp_dump->filename[0] != DIRECTORY_SEP)
  96. {
  97. sprintf (fullpath, "%s/%s", vty_get_cwd (), bgp_dump->filename);
  98. ret = strftime (realpath, MAXPATHLEN, fullpath, tm);
  99. }
  100. else
  101. ret = strftime (realpath, MAXPATHLEN, bgp_dump->filename, tm);
  102. if (ret == 0)
  103. {
  104. zlog_warn ("bgp_dump_open_file: strftime error");
  105. return NULL;
  106. }
  107. if (bgp_dump->fp)
  108. fclose (bgp_dump->fp);
  109. oldumask = umask(0777 & ~LOGFILE_MASK);
  110. bgp_dump->fp = fopen (realpath, "w");
  111. if (bgp_dump->fp == NULL)
  112. {
  113. zlog_warn ("bgp_dump_open_file: %s: %s", realpath, strerror (errno));
  114. umask(oldumask);
  115. return NULL;
  116. }
  117. umask(oldumask);
  118. return bgp_dump->fp;
  119. }
  120. static int
  121. bgp_dump_interval_add (struct bgp_dump *bgp_dump, int interval)
  122. {
  123. int secs_into_day;
  124. time_t t;
  125. struct tm *tm;
  126. if (interval > 0)
  127. {
  128. /* Periodic dump every interval seconds */
  129. if ((interval < 86400) && ((86400 % interval) == 0))
  130. {
  131. /* Dump at predictable times: if a day has a whole number of
  132. * intervals, dump every interval seconds starting from midnight
  133. */
  134. (void) time(&t);
  135. tm = localtime(&t);
  136. secs_into_day = tm->tm_sec + 60*tm->tm_min + 60*60*tm->tm_hour;
  137. interval = interval - secs_into_day % interval; /* always > 0 */
  138. }
  139. bgp_dump->t_interval = thread_add_timer (bm->master, bgp_dump_interval_func,
  140. bgp_dump, interval);
  141. }
  142. else
  143. {
  144. /* One-off dump: execute immediately, don't affect any scheduled dumps */
  145. bgp_dump->t_interval = thread_add_event (bm->master, bgp_dump_interval_func,
  146. bgp_dump, 0);
  147. }
  148. return 0;
  149. }
  150. /* Dump common header. */
  151. static void
  152. bgp_dump_header (struct stream *obuf, int type, int subtype, int dump_type)
  153. {
  154. struct timeval clock;
  155. long msecs;
  156. time_t secs;
  157. if ((dump_type == BGP_DUMP_ALL_ET || dump_type == BGP_DUMP_UPDATES_ET)
  158. && type == MSG_PROTOCOL_BGP4MP)
  159. type = MSG_PROTOCOL_BGP4MP_ET;
  160. gettimeofday(&clock, NULL);
  161. secs = clock.tv_sec;
  162. msecs = clock.tv_usec;
  163. /* Put dump packet header. */
  164. stream_putl (obuf, secs);
  165. stream_putw (obuf, type);
  166. stream_putw (obuf, subtype);
  167. stream_putl (obuf, 0); /* len */
  168. /* Adding microseconds for the MRT Extended Header */
  169. if (type == MSG_PROTOCOL_BGP4MP_ET)
  170. stream_putl (obuf, msecs);
  171. }
  172. static void
  173. bgp_dump_set_size (struct stream *s, int type)
  174. {
  175. /*
  176. * The BGP_DUMP_HEADER_SIZE stay at 12 event when ET:
  177. * "The Microsecond Timestamp is included in the computation
  178. * of the Length field value." (RFC6396 2011)
  179. */
  180. stream_putl_at (s, 8, stream_get_endp (s) - BGP_DUMP_HEADER_SIZE);
  181. }
  182. static void
  183. bgp_dump_routes_index_table(struct bgp *bgp)
  184. {
  185. struct peer *peer;
  186. struct listnode *node;
  187. uint16_t peerno = 0;
  188. struct stream *obuf;
  189. obuf = bgp_dump_obuf;
  190. stream_reset (obuf);
  191. /* MRT header */
  192. bgp_dump_header (obuf, MSG_TABLE_DUMP_V2, TABLE_DUMP_V2_PEER_INDEX_TABLE,
  193. BGP_DUMP_ROUTES);
  194. /* Collector BGP ID */
  195. stream_put_in_addr (obuf, &bgp->router_id);
  196. /* View name */
  197. if(bgp->name)
  198. {
  199. stream_putw (obuf, strlen(bgp->name));
  200. stream_put(obuf, bgp->name, strlen(bgp->name));
  201. }
  202. else
  203. {
  204. stream_putw(obuf, 0);
  205. }
  206. /* Peer count */
  207. stream_putw (obuf, listcount(bgp->peer));
  208. /* Walk down all peers */
  209. for(ALL_LIST_ELEMENTS_RO (bgp->peer, node, peer))
  210. {
  211. /* Peer's type */
  212. if (sockunion_family(&peer->su) == AF_INET)
  213. {
  214. stream_putc (obuf, TABLE_DUMP_V2_PEER_INDEX_TABLE_AS4+TABLE_DUMP_V2_PEER_INDEX_TABLE_IP);
  215. }
  216. else if (sockunion_family(&peer->su) == AF_INET6)
  217. {
  218. stream_putc (obuf, TABLE_DUMP_V2_PEER_INDEX_TABLE_AS4+TABLE_DUMP_V2_PEER_INDEX_TABLE_IP6);
  219. }
  220. /* Peer's BGP ID */
  221. stream_put_in_addr (obuf, &peer->remote_id);
  222. /* Peer's IP address */
  223. if (sockunion_family(&peer->su) == AF_INET)
  224. {
  225. stream_put_in_addr (obuf, &peer->su.sin.sin_addr);
  226. }
  227. else if (sockunion_family(&peer->su) == AF_INET6)
  228. {
  229. stream_write (obuf, (u_char *)&peer->su.sin6.sin6_addr,
  230. IPV6_MAX_BYTELEN);
  231. }
  232. /* Peer's AS number. */
  233. /* Note that, as this is an AS4 compliant quagga, the RIB is always AS4 */
  234. stream_putl (obuf, peer->as);
  235. /* Store the peer number for this peer */
  236. peer->table_dump_index = peerno;
  237. peerno++;
  238. }
  239. bgp_dump_set_size(obuf, MSG_TABLE_DUMP_V2);
  240. fwrite (STREAM_DATA (obuf), stream_get_endp (obuf), 1, bgp_dump_routes.fp);
  241. fflush (bgp_dump_routes.fp);
  242. }
  243. /* Runs under child process. */
  244. static unsigned int
  245. bgp_dump_routes_func (int afi, int first_run, unsigned int seq)
  246. {
  247. struct stream *obuf;
  248. struct bgp_info *info;
  249. struct bgp_node *rn;
  250. struct bgp *bgp;
  251. struct bgp_table *table;
  252. bgp = bgp_get_default ();
  253. if (!bgp)
  254. return seq;
  255. if (bgp_dump_routes.fp == NULL)
  256. return seq;
  257. /* Note that bgp_dump_routes_index_table will do ipv4 and ipv6 peers,
  258. so this should only be done on the first call to bgp_dump_routes_func.
  259. ( this function will be called once for ipv4 and once for ipv6 ) */
  260. if(first_run)
  261. bgp_dump_routes_index_table(bgp);
  262. obuf = bgp_dump_obuf;
  263. stream_reset(obuf);
  264. /* Walk down each BGP route. */
  265. table = bgp->rib[afi][SAFI_UNICAST];
  266. for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
  267. {
  268. if(!rn->info)
  269. continue;
  270. stream_reset(obuf);
  271. /* MRT header */
  272. if (afi == AFI_IP)
  273. bgp_dump_header (obuf, MSG_TABLE_DUMP_V2, TABLE_DUMP_V2_RIB_IPV4_UNICAST,
  274. BGP_DUMP_ROUTES);
  275. else if (afi == AFI_IP6)
  276. bgp_dump_header (obuf, MSG_TABLE_DUMP_V2, TABLE_DUMP_V2_RIB_IPV6_UNICAST,
  277. BGP_DUMP_ROUTES);
  278. /* Sequence number */
  279. stream_putl(obuf, seq);
  280. /* Prefix length */
  281. stream_putc (obuf, rn->p.prefixlen);
  282. /* Prefix */
  283. if (afi == AFI_IP)
  284. {
  285. /* We'll dump only the useful bits (those not 0), but have to align on 8 bits */
  286. stream_write(obuf, (u_char *)&rn->p.u.prefix4, (rn->p.prefixlen+7)/8);
  287. }
  288. else if (afi == AFI_IP6)
  289. {
  290. /* We'll dump only the useful bits (those not 0), but have to align on 8 bits */
  291. stream_write (obuf, (u_char *)&rn->p.u.prefix6, (rn->p.prefixlen+7)/8);
  292. }
  293. /* Save where we are now, so we can overwride the entry count later */
  294. int sizep = stream_get_endp(obuf);
  295. /* Entry count */
  296. uint16_t entry_count = 0;
  297. /* Entry count, note that this is overwritten later */
  298. stream_putw(obuf, 0);
  299. for (info = rn->info; info; info = info->next)
  300. {
  301. entry_count++;
  302. /* Peer index */
  303. stream_putw(obuf, info->peer->table_dump_index);
  304. /* Originated */
  305. #ifdef HAVE_CLOCK_MONOTONIC
  306. stream_putl (obuf, time(NULL) - (bgp_clock() - info->uptime));
  307. #else
  308. stream_putl (obuf, info->uptime);
  309. #endif /* HAVE_CLOCK_MONOTONIC */
  310. /* Dump attribute. */
  311. /* Skip prefix & AFI/SAFI for MP_NLRI */
  312. bgp_dump_routes_attr (obuf, info->attr, &rn->p);
  313. }
  314. /* Overwrite the entry count, now that we know the right number */
  315. stream_putw_at (obuf, sizep, entry_count);
  316. seq++;
  317. bgp_dump_set_size(obuf, MSG_TABLE_DUMP_V2);
  318. fwrite (STREAM_DATA (obuf), stream_get_endp (obuf), 1, bgp_dump_routes.fp);
  319. }
  320. fflush (bgp_dump_routes.fp);
  321. return seq;
  322. }
  323. static int
  324. bgp_dump_interval_func (struct thread *t)
  325. {
  326. struct bgp_dump *bgp_dump;
  327. bgp_dump = THREAD_ARG (t);
  328. bgp_dump->t_interval = NULL;
  329. /* Reschedule dump even if file couldn't be opened this time... */
  330. if (bgp_dump_open_file (bgp_dump) != NULL)
  331. {
  332. /* In case of bgp_dump_routes, we need special route dump function. */
  333. if (bgp_dump->type == BGP_DUMP_ROUTES)
  334. {
  335. unsigned int seq = bgp_dump_routes_func (AFI_IP, 1, 0);
  336. bgp_dump_routes_func (AFI_IP6, 0, seq);
  337. /* Close the file now. For a RIB dump there's no point in leaving
  338. * it open until the next scheduled dump starts. */
  339. fclose(bgp_dump->fp); bgp_dump->fp = NULL;
  340. }
  341. }
  342. /* if interval is set reschedule */
  343. if (bgp_dump->interval > 0)
  344. bgp_dump_interval_add (bgp_dump, bgp_dump->interval);
  345. return 0;
  346. }
  347. /* Dump common information. */
  348. static void
  349. bgp_dump_common (struct stream *obuf, struct peer *peer, int forceas4)
  350. {
  351. char empty[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  352. /* Source AS number and Destination AS number. */
  353. if (forceas4 || CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV) )
  354. {
  355. stream_putl (obuf, peer->as);
  356. stream_putl (obuf, peer->local_as);
  357. }
  358. else
  359. {
  360. stream_putw (obuf, peer->as);
  361. stream_putw (obuf, peer->local_as);
  362. }
  363. if (peer->su.sa.sa_family == AF_INET)
  364. {
  365. stream_putw (obuf, peer->ifindex);
  366. stream_putw (obuf, AFI_IP);
  367. stream_put (obuf, &peer->su.sin.sin_addr, IPV4_MAX_BYTELEN);
  368. if (peer->su_local)
  369. stream_put (obuf, &peer->su_local->sin.sin_addr, IPV4_MAX_BYTELEN);
  370. else
  371. stream_put (obuf, empty, IPV4_MAX_BYTELEN);
  372. }
  373. else if (peer->su.sa.sa_family == AF_INET6)
  374. {
  375. /* Interface Index and Address family. */
  376. stream_putw (obuf, peer->ifindex);
  377. stream_putw (obuf, AFI_IP6);
  378. /* Source IP Address and Destination IP Address. */
  379. stream_put (obuf, &peer->su.sin6.sin6_addr, IPV6_MAX_BYTELEN);
  380. if (peer->su_local)
  381. stream_put (obuf, &peer->su_local->sin6.sin6_addr, IPV6_MAX_BYTELEN);
  382. else
  383. stream_put (obuf, empty, IPV6_MAX_BYTELEN);
  384. }
  385. }
  386. /* Dump BGP status change. */
  387. void
  388. bgp_dump_state (struct peer *peer, int status_old, int status_new)
  389. {
  390. struct stream *obuf;
  391. /* If dump file pointer is disabled return immediately. */
  392. if (bgp_dump_all.fp == NULL)
  393. return;
  394. /* Make dump stream. */
  395. obuf = bgp_dump_obuf;
  396. stream_reset (obuf);
  397. bgp_dump_header (obuf, MSG_PROTOCOL_BGP4MP, BGP4MP_STATE_CHANGE_AS4,
  398. bgp_dump_all.type);
  399. bgp_dump_common (obuf, peer, 1);/* force this in as4speak*/
  400. stream_putw (obuf, status_old);
  401. stream_putw (obuf, status_new);
  402. /* Set length. */
  403. bgp_dump_set_size (obuf, MSG_PROTOCOL_BGP4MP);
  404. /* Write to the stream. */
  405. fwrite (STREAM_DATA (obuf), stream_get_endp (obuf), 1, bgp_dump_all.fp);
  406. fflush (bgp_dump_all.fp);
  407. }
  408. static void
  409. bgp_dump_packet_func (struct bgp_dump *bgp_dump, struct peer *peer,
  410. struct stream *packet)
  411. {
  412. struct stream *obuf;
  413. /* If dump file pointer is disabled return immediately. */
  414. if (bgp_dump->fp == NULL)
  415. return;
  416. /* Make dump stream. */
  417. obuf = bgp_dump_obuf;
  418. stream_reset (obuf);
  419. /* Dump header and common part. */
  420. if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV) )
  421. {
  422. bgp_dump_header (obuf, MSG_PROTOCOL_BGP4MP, BGP4MP_MESSAGE_AS4,
  423. bgp_dump->type);
  424. }
  425. else
  426. {
  427. bgp_dump_header (obuf, MSG_PROTOCOL_BGP4MP, BGP4MP_MESSAGE,
  428. bgp_dump->type);
  429. }
  430. bgp_dump_common (obuf, peer, 0);
  431. /* Packet contents. */
  432. stream_put (obuf, STREAM_DATA (packet), stream_get_endp (packet));
  433. /* Set length. */
  434. bgp_dump_set_size (obuf, MSG_PROTOCOL_BGP4MP);
  435. /* Write to the stream. */
  436. fwrite (STREAM_DATA (obuf), stream_get_endp (obuf), 1, bgp_dump->fp);
  437. fflush (bgp_dump->fp);
  438. }
  439. /* Called from bgp_packet.c when BGP packet is received. */
  440. void
  441. bgp_dump_packet (struct peer *peer, int type, struct stream *packet)
  442. {
  443. /* bgp_dump_all. */
  444. bgp_dump_packet_func (&bgp_dump_all, peer, packet);
  445. /* bgp_dump_updates. */
  446. if (type == BGP_MSG_UPDATE)
  447. bgp_dump_packet_func (&bgp_dump_updates, peer, packet);
  448. }
  449. static unsigned int
  450. bgp_dump_parse_time (const char *str)
  451. {
  452. int i;
  453. int len;
  454. int seen_h;
  455. int seen_m;
  456. int time;
  457. unsigned int total;
  458. time = 0;
  459. total = 0;
  460. seen_h = 0;
  461. seen_m = 0;
  462. len = strlen (str);
  463. for (i = 0; i < len; i++)
  464. {
  465. if (isdigit ((int) str[i]))
  466. {
  467. time *= 10;
  468. time += str[i] - '0';
  469. }
  470. else if (str[i] == 'H' || str[i] == 'h')
  471. {
  472. if (seen_h)
  473. return 0;
  474. if (seen_m)
  475. return 0;
  476. total += time * 60 *60;
  477. time = 0;
  478. seen_h = 1;
  479. }
  480. else if (str[i] == 'M' || str[i] == 'm')
  481. {
  482. if (seen_m)
  483. return 0;
  484. total += time * 60;
  485. time = 0;
  486. seen_h = 1;
  487. }
  488. else
  489. return 0;
  490. }
  491. return total + time;
  492. }
  493. static int
  494. bgp_dump_set (struct vty *vty, struct bgp_dump *bgp_dump,
  495. enum bgp_dump_type type, const char *path,
  496. const char *interval_str)
  497. {
  498. unsigned int interval;
  499. /* Don't schedule duplicate dumps if the dump command is given twice */
  500. if (bgp_dump->filename && strcmp(path, bgp_dump->filename) == 0
  501. && type == bgp_dump->type)
  502. {
  503. if (interval_str)
  504. {
  505. if (bgp_dump->interval_str &&
  506. strcmp(bgp_dump->interval_str, interval_str) == 0)
  507. return CMD_SUCCESS;
  508. }
  509. else
  510. {
  511. if (!bgp_dump->interval_str)
  512. return CMD_SUCCESS;
  513. }
  514. }
  515. /* Removing previous config */
  516. bgp_dump_unset(vty, bgp_dump);
  517. if (interval_str)
  518. {
  519. /* Check interval string. */
  520. interval = bgp_dump_parse_time (interval_str);
  521. if (interval == 0)
  522. {
  523. vty_out (vty, "Malformed interval string%s", VTY_NEWLINE);
  524. return CMD_WARNING;
  525. }
  526. /* Setting interval string */
  527. bgp_dump->interval_str = strdup (interval_str);
  528. }
  529. else
  530. {
  531. interval = 0;
  532. }
  533. /* Set type. */
  534. bgp_dump->type = type;
  535. /* Set interval */
  536. bgp_dump->interval = interval;
  537. /* Set file name. */
  538. bgp_dump->filename = strdup (path);
  539. /* Create interval thread. */
  540. bgp_dump_interval_add (bgp_dump, interval);
  541. /* This should be called when interval is expired. */
  542. bgp_dump_open_file (bgp_dump);
  543. return CMD_SUCCESS;
  544. }
  545. static int
  546. bgp_dump_unset (struct vty *vty, struct bgp_dump *bgp_dump)
  547. {
  548. /* Removing file name. */
  549. if (bgp_dump->filename)
  550. {
  551. free (bgp_dump->filename);
  552. bgp_dump->filename = NULL;
  553. }
  554. /* Closing file. */
  555. if (bgp_dump->fp)
  556. {
  557. fclose (bgp_dump->fp);
  558. bgp_dump->fp = NULL;
  559. }
  560. /* Removing interval thread. */
  561. if (bgp_dump->t_interval)
  562. {
  563. thread_cancel (bgp_dump->t_interval);
  564. bgp_dump->t_interval = NULL;
  565. }
  566. bgp_dump->interval = 0;
  567. /* Removing interval string. */
  568. if (bgp_dump->interval_str)
  569. {
  570. free (bgp_dump->interval_str);
  571. bgp_dump->interval_str = NULL;
  572. }
  573. return CMD_SUCCESS;
  574. }
  575. DEFUN (dump_bgp_all,
  576. dump_bgp_all_cmd,
  577. "dump bgp (all|all-et|updates|updates-et|routes-mrt) PATH [INTERVAL]",
  578. "Dump packet\n"
  579. "BGP packet dump\n"
  580. "Dump all BGP packets\nDump all BGP packets (Extended Tiemstamp Header)\n"
  581. "Dump BGP updates only\nDump BGP updates only (Extended Tiemstamp Header)\n"
  582. "Dump whole BGP routing table\n"
  583. "Output filename\n"
  584. "Interval of output\n")
  585. {
  586. int bgp_dump_type = 0;
  587. const char *interval = NULL;
  588. struct bgp_dump *bgp_dump_struct = NULL;
  589. const struct bgp_dump_type_map *map = NULL;
  590. for (map = bgp_dump_type_map; map->str; map++)
  591. if (strcmp(argv[0], map->str) == 0)
  592. bgp_dump_type = map->type;
  593. switch (bgp_dump_type)
  594. {
  595. case BGP_DUMP_ALL:
  596. case BGP_DUMP_ALL_ET:
  597. bgp_dump_struct = &bgp_dump_all;
  598. break;
  599. case BGP_DUMP_UPDATES:
  600. case BGP_DUMP_UPDATES_ET:
  601. bgp_dump_struct = &bgp_dump_updates;
  602. break;
  603. case BGP_DUMP_ROUTES:
  604. default:
  605. bgp_dump_struct = &bgp_dump_routes;
  606. break;
  607. }
  608. /* When an interval is given */
  609. if (argc == 3)
  610. interval = argv[2];
  611. return bgp_dump_set (vty, bgp_dump_struct, bgp_dump_type,
  612. argv[1], interval);
  613. }
  614. DEFUN (no_dump_bgp_all,
  615. no_dump_bgp_all_cmd,
  616. "no dump bgp (all|updates|routes-mrt) [PATH] [INTERVAL]",
  617. NO_STR
  618. "Stop dump packet\n"
  619. "Stop BGP packet dump\n"
  620. "Stop dump process all/all-et\n"
  621. "Stop dump process updates/updates-et\n"
  622. "Stop dump process route-mrt\n")
  623. {
  624. return bgp_dump_unset (vty, &bgp_dump_all);
  625. }
  626. /* BGP node structure. */
  627. static struct cmd_node bgp_dump_node =
  628. {
  629. DUMP_NODE,
  630. "",
  631. 1
  632. };
  633. #if 0
  634. char *
  635. config_time2str (unsigned int interval)
  636. {
  637. static char buf[BUFSIZ];
  638. buf[0] = '\0';
  639. if (interval / 3600)
  640. {
  641. sprintf (buf, "%dh", interval / 3600);
  642. interval %= 3600;
  643. }
  644. if (interval / 60)
  645. {
  646. sprintf (buf + strlen (buf), "%dm", interval /60);
  647. interval %= 60;
  648. }
  649. if (interval)
  650. {
  651. sprintf (buf + strlen (buf), "%d", interval);
  652. }
  653. return buf;
  654. }
  655. #endif
  656. static int
  657. config_write_bgp_dump (struct vty *vty)
  658. {
  659. if (bgp_dump_all.filename)
  660. {
  661. const char *type_str = "all";
  662. if (bgp_dump_all.type == BGP_DUMP_ALL_ET)
  663. type_str = "all-et";
  664. if (bgp_dump_all.interval_str)
  665. vty_out (vty, "dump bgp %s %s %s%s", type_str,
  666. bgp_dump_all.filename, bgp_dump_all.interval_str,
  667. VTY_NEWLINE);
  668. else
  669. vty_out (vty, "dump bgp %s %s%s", type_str,
  670. bgp_dump_all.filename, VTY_NEWLINE);
  671. }
  672. if (bgp_dump_updates.filename)
  673. {
  674. const char *type_str = "updates";
  675. if (bgp_dump_updates.type == BGP_DUMP_UPDATES_ET)
  676. type_str = "updates-et";
  677. if (bgp_dump_updates.interval_str)
  678. vty_out (vty, "dump bgp %s %s %s%s", type_str,
  679. bgp_dump_updates.filename, bgp_dump_updates.interval_str,
  680. VTY_NEWLINE);
  681. else
  682. vty_out (vty, "dump bgp updates %s%s",
  683. bgp_dump_updates.filename, VTY_NEWLINE);
  684. }
  685. if (bgp_dump_routes.filename)
  686. {
  687. if (bgp_dump_routes.interval_str)
  688. vty_out (vty, "dump bgp routes-mrt %s %s%s",
  689. bgp_dump_routes.filename, bgp_dump_routes.interval_str,
  690. VTY_NEWLINE);
  691. }
  692. return 0;
  693. }
  694. /* Initialize BGP packet dump functionality. */
  695. void
  696. bgp_dump_init (void)
  697. {
  698. memset (&bgp_dump_all, 0, sizeof (struct bgp_dump));
  699. memset (&bgp_dump_updates, 0, sizeof (struct bgp_dump));
  700. memset (&bgp_dump_routes, 0, sizeof (struct bgp_dump));
  701. bgp_dump_obuf = stream_new (BGP_MAX_PACKET_SIZE + BGP_DUMP_MSG_HEADER
  702. + BGP_DUMP_HEADER_SIZE);
  703. install_node (&bgp_dump_node, config_write_bgp_dump);
  704. install_element (CONFIG_NODE, &dump_bgp_all_cmd);
  705. install_element (CONFIG_NODE, &no_dump_bgp_all_cmd);
  706. }
  707. void
  708. bgp_dump_finish (void)
  709. {
  710. stream_free (bgp_dump_obuf);
  711. bgp_dump_obuf = NULL;
  712. }