blob: 8087a403bc6fca9e84c63971c7466c27aafe4d02 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* BGP-4 dump routine
2 Copyright (C) 1999 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA. */
20
21#include <zebra.h>
22
23#include "log.h"
24#include "stream.h"
25#include "sockunion.h"
26#include "command.h"
27#include "prefix.h"
28#include "thread.h"
Paul Jakma0b2aa3a2007-10-14 22:32:21 +000029#include "linklist.h"
paul718e3742002-12-13 20:15:29 +000030#include "bgpd/bgp_table.h"
31
32#include "bgpd/bgpd.h"
33#include "bgpd/bgp_route.h"
34#include "bgpd/bgp_attr.h"
35#include "bgpd/bgp_dump.h"
36
37enum bgp_dump_type
38{
39 BGP_DUMP_ALL,
40 BGP_DUMP_UPDATES,
41 BGP_DUMP_ROUTES
42};
43
44enum MRT_MSG_TYPES {
45 MSG_NULL,
46 MSG_START, /* sender is starting up */
47 MSG_DIE, /* receiver should shut down */
48 MSG_I_AM_DEAD, /* sender is shutting down */
49 MSG_PEER_DOWN, /* sender's peer is down */
50 MSG_PROTOCOL_BGP, /* msg is a BGP packet */
51 MSG_PROTOCOL_RIP, /* msg is a RIP packet */
52 MSG_PROTOCOL_IDRP, /* msg is an IDRP packet */
53 MSG_PROTOCOL_RIPNG, /* msg is a RIPNG packet */
54 MSG_PROTOCOL_BGP4PLUS, /* msg is a BGP4+ packet */
55 MSG_PROTOCOL_BGP4PLUS_01, /* msg is a BGP4+ (draft 01) packet */
56 MSG_PROTOCOL_OSPF, /* msg is an OSPF packet */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +000057 MSG_TABLE_DUMP, /* routing table dump */
58 MSG_TABLE_DUMP_V2 /* routing table dump, version 2 */
paul718e3742002-12-13 20:15:29 +000059};
60
paul1f8ae702005-09-09 23:49:49 +000061static int bgp_dump_interval_func (struct thread *);
62
paul718e3742002-12-13 20:15:29 +000063struct bgp_dump
64{
65 enum bgp_dump_type type;
66
67 char *filename;
68
69 FILE *fp;
70
71 unsigned int interval;
72
73 char *interval_str;
74
75 struct thread *t_interval;
76};
77
78/* BGP packet dump output buffer. */
79struct stream *bgp_dump_obuf;
80
81/* BGP dump strucuture for 'dump bgp all' */
82struct bgp_dump bgp_dump_all;
83
84/* BGP dump structure for 'dump bgp updates' */
85struct bgp_dump bgp_dump_updates;
86
87/* BGP dump structure for 'dump bgp routes' */
88struct bgp_dump bgp_dump_routes;
89
90/* Dump whole BGP table is very heavy process. */
91struct thread *t_bgp_dump_routes;
92
93/* Some define for BGP packet dump. */
paul94f2b392005-06-28 12:44:16 +000094static FILE *
paul718e3742002-12-13 20:15:29 +000095bgp_dump_open_file (struct bgp_dump *bgp_dump)
96{
97 int ret;
98 time_t clock;
99 struct tm *tm;
100 char fullpath[MAXPATHLEN];
101 char realpath[MAXPATHLEN];
gdtaa593d52003-12-22 20:15:53 +0000102 mode_t oldumask;
paul718e3742002-12-13 20:15:29 +0000103
104 time (&clock);
105 tm = localtime (&clock);
106
107 if (bgp_dump->filename[0] != DIRECTORY_SEP)
108 {
109 sprintf (fullpath, "%s/%s", vty_get_cwd (), bgp_dump->filename);
110 ret = strftime (realpath, MAXPATHLEN, fullpath, tm);
111 }
112 else
113 ret = strftime (realpath, MAXPATHLEN, bgp_dump->filename, tm);
114
115 if (ret == 0)
116 {
117 zlog_warn ("bgp_dump_open_file: strftime error");
118 return NULL;
119 }
120
121 if (bgp_dump->fp)
122 fclose (bgp_dump->fp);
123
124
gdtaa593d52003-12-22 20:15:53 +0000125 oldumask = umask(0777 & ~LOGFILE_MASK);
paul718e3742002-12-13 20:15:29 +0000126 bgp_dump->fp = fopen (realpath, "w");
127
128 if (bgp_dump->fp == NULL)
gdtaa593d52003-12-22 20:15:53 +0000129 {
Paul Jakma45ad5922007-07-31 17:35:36 +0000130 zlog_warn ("bgp_dump_open_file: %s: %s", realpath, strerror (errno));
gdtaa593d52003-12-22 20:15:53 +0000131 umask(oldumask);
132 return NULL;
133 }
134 umask(oldumask);
paul718e3742002-12-13 20:15:29 +0000135
136 return bgp_dump->fp;
137}
138
paul94f2b392005-06-28 12:44:16 +0000139static int
paul718e3742002-12-13 20:15:29 +0000140bgp_dump_interval_add (struct bgp_dump *bgp_dump, int interval)
141{
Paul Jakma45ad5922007-07-31 17:35:36 +0000142 int secs_into_day;
paul9834cd02003-10-18 01:01:19 +0000143 time_t t;
144 struct tm *tm;
paul718e3742002-12-13 20:15:29 +0000145
Paul Jakma45ad5922007-07-31 17:35:36 +0000146 if (interval > 0)
paul9834cd02003-10-18 01:01:19 +0000147 {
Paul Jakma45ad5922007-07-31 17:35:36 +0000148 /* Periodic dump every interval seconds */
paul9834cd02003-10-18 01:01:19 +0000149 if ((interval < 86400) && ((86400 % interval) == 0))
150 {
Paul Jakma45ad5922007-07-31 17:35:36 +0000151 /* Dump at predictable times: if a day has a whole number of
152 * intervals, dump every interval seconds starting from midnight
153 */
paul9834cd02003-10-18 01:01:19 +0000154 (void) time(&t);
155 tm = localtime(&t);
156 secs_into_day = tm->tm_sec + 60*tm->tm_min + 60*60*tm->tm_hour;
Paul Jakma45ad5922007-07-31 17:35:36 +0000157 interval = interval - secs_into_day % interval; /* always > 0 */
paul9834cd02003-10-18 01:01:19 +0000158 }
159 bgp_dump->t_interval = thread_add_timer (master, bgp_dump_interval_func,
Paul Jakma45ad5922007-07-31 17:35:36 +0000160 bgp_dump, interval);
paul9834cd02003-10-18 01:01:19 +0000161 }
paulfba3d222003-05-10 18:33:28 +0000162 else
paul9834cd02003-10-18 01:01:19 +0000163 {
Paul Jakma45ad5922007-07-31 17:35:36 +0000164 /* One-off dump: execute immediately, don't affect any scheduled dumps */
paul9834cd02003-10-18 01:01:19 +0000165 bgp_dump->t_interval = thread_add_event (master, bgp_dump_interval_func,
166 bgp_dump, 0);
167 }
paulfba3d222003-05-10 18:33:28 +0000168
paul718e3742002-12-13 20:15:29 +0000169 return 0;
170}
171
172/* Dump common header. */
paul94f2b392005-06-28 12:44:16 +0000173static void
paul718e3742002-12-13 20:15:29 +0000174bgp_dump_header (struct stream *obuf, int type, int subtype)
175{
176 time_t now;
177
178 /* Set header. */
179 time (&now);
180
181 /* Put dump packet header. */
182 stream_putl (obuf, now);
183 stream_putw (obuf, type);
184 stream_putw (obuf, subtype);
185
186 stream_putl (obuf, 0); /* len */
187}
188
paul94f2b392005-06-28 12:44:16 +0000189static void
paul718e3742002-12-13 20:15:29 +0000190bgp_dump_set_size (struct stream *s, int type)
191{
paul9985f832005-02-09 15:51:56 +0000192 stream_putl_at (s, 8, stream_get_endp (s) - BGP_DUMP_HEADER_SIZE);
paul718e3742002-12-13 20:15:29 +0000193}
194
paul94f2b392005-06-28 12:44:16 +0000195static void
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000196bgp_dump_routes_index_table(struct bgp *bgp)
paul718e3742002-12-13 20:15:29 +0000197{
paul718e3742002-12-13 20:15:29 +0000198 struct peer *peer;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000199 struct listnode *node;
200 uint16_t peerno = 0;
201 struct stream *obuf;
paul718e3742002-12-13 20:15:29 +0000202
paul718e3742002-12-13 20:15:29 +0000203 obuf = bgp_dump_obuf;
204 stream_reset (obuf);
205
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000206 /* MRT header */
207 bgp_dump_header (obuf, MSG_TABLE_DUMP_V2, TABLE_DUMP_V2_PEER_INDEX_TABLE);
paul718e3742002-12-13 20:15:29 +0000208
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000209 /* Collector BGP ID */
210 stream_put_in_addr (obuf, &bgp->router_id);
211
212 /* View name */
213 if(bgp->name)
paul718e3742002-12-13 20:15:29 +0000214 {
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000215 stream_putw (obuf, strlen(bgp->name));
216 stream_put(obuf, bgp->name, strlen(bgp->name));
paul718e3742002-12-13 20:15:29 +0000217 }
218 else
219 {
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000220 stream_putw(obuf, 0);
paul718e3742002-12-13 20:15:29 +0000221 }
222
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000223 /* Peer count */
224 stream_putw (obuf, listcount(bgp->peer));
225
226 /* Walk down all peers */
227 for(ALL_LIST_ELEMENTS_RO (bgp->peer, node, peer))
paul718e3742002-12-13 20:15:29 +0000228 {
paul718e3742002-12-13 20:15:29 +0000229
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000230 /* Peer's type */
231 if (sockunion_family(&peer->su) == AF_INET)
232 {
233 stream_putc (obuf, TABLE_DUMP_V2_PEER_INDEX_TABLE_AS4+TABLE_DUMP_V2_PEER_INDEX_TABLE_IP);
234 }
paul718e3742002-12-13 20:15:29 +0000235#ifdef HAVE_IPV6
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000236 else if (sockunion_family(&peer->su) == AF_INET6)
237 {
238 stream_putc (obuf, TABLE_DUMP_V2_PEER_INDEX_TABLE_AS4+TABLE_DUMP_V2_PEER_INDEX_TABLE_IP6);
239 }
paul718e3742002-12-13 20:15:29 +0000240#endif /* HAVE_IPV6 */
241
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000242 /* Peer's BGP ID */
243 stream_put_in_addr (obuf, &peer->remote_id);
244
245 /* Peer's IP address */
246 if (sockunion_family(&peer->su) == AF_INET)
247 {
248 stream_put_in_addr (obuf, &peer->su.sin.sin_addr);
249 }
250#ifdef HAVE_IPV6
251 else if (sockunion_family(&peer->su) == AF_INET6)
252 {
253 stream_write (obuf, (u_char *)&peer->su.sin6.sin6_addr,
254 IPV6_MAX_BYTELEN);
255 }
256#endif /* HAVE_IPV6 */
257
258 /* Peer's AS number. */
259 /* Note that, as this is an AS4 compliant quagga, the RIB is always AS4 */
260 stream_putl (obuf, peer->as);
261
262 /* Store the peer number for this peer */
263 peer->table_dump_index = peerno;
264 peerno++;
265 }
266
267 bgp_dump_set_size(obuf, MSG_TABLE_DUMP_V2);
paul718e3742002-12-13 20:15:29 +0000268
paul9985f832005-02-09 15:51:56 +0000269 fwrite (STREAM_DATA (obuf), stream_get_endp (obuf), 1, bgp_dump_routes.fp);
paul718e3742002-12-13 20:15:29 +0000270 fflush (bgp_dump_routes.fp);
271}
272
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000273
paul718e3742002-12-13 20:15:29 +0000274/* Runs under child process. */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000275static unsigned int
276bgp_dump_routes_func (int afi, int first_run, unsigned int seq)
paul718e3742002-12-13 20:15:29 +0000277{
278 struct stream *obuf;
paul718e3742002-12-13 20:15:29 +0000279 struct bgp_info *info;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000280 struct bgp_node *rn;
paul718e3742002-12-13 20:15:29 +0000281 struct bgp *bgp;
282 struct bgp_table *table;
paul718e3742002-12-13 20:15:29 +0000283
284 bgp = bgp_get_default ();
285 if (!bgp)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000286 return seq;
paul718e3742002-12-13 20:15:29 +0000287
288 if (bgp_dump_routes.fp == NULL)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000289 return seq;
290
291 /* Note that bgp_dump_routes_index_table will do ipv4 and ipv6 peers,
292 so this should only be done on the first call to bgp_dump_routes_func.
293 ( this function will be called once for ipv4 and once for ipv6 ) */
294 if(first_run)
295 bgp_dump_routes_index_table(bgp);
296
297 obuf = bgp_dump_obuf;
298 stream_reset(obuf);
paul718e3742002-12-13 20:15:29 +0000299
300 /* Walk down each BGP route. */
301 table = bgp->rib[afi][SAFI_UNICAST];
302
303 for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000304 {
305 if(!rn->info)
306 continue;
307
308 stream_reset(obuf);
309
310 /* MRT header */
311 if (afi == AFI_IP)
312 {
313 bgp_dump_header (obuf, MSG_TABLE_DUMP_V2, TABLE_DUMP_V2_RIB_IPV4_UNICAST);
314 }
315#ifdef HAVE_IPV6
316 else if (afi == AFI_IP6)
317 {
318 bgp_dump_header (obuf, MSG_TABLE_DUMP_V2, TABLE_DUMP_V2_RIB_IPV6_UNICAST);
319 }
320#endif /* HAVE_IPV6 */
321
322 /* Sequence number */
323 stream_putl(obuf, seq);
324
325 /* Prefix length */
326 stream_putc (obuf, rn->p.prefixlen);
327
328 /* Prefix */
329 if (afi == AFI_IP)
330 {
331 /* We'll dump only the useful bits (those not 0), but have to align on 8 bits */
332 stream_write(obuf, (u_char *)&rn->p.u.prefix4, (rn->p.prefixlen+7)/8);
333 }
334#ifdef HAVE_IPV6
335 else if (afi == AFI_IP6)
336 {
337 /* We'll dump only the useful bits (those not 0), but have to align on 8 bits */
338 stream_write (obuf, (u_char *)&rn->p.u.prefix6, (rn->p.prefixlen+7)/8);
339 }
340#endif /* HAVE_IPV6 */
341
342 /* Save where we are now, so we can overwride the entry count later */
343 int sizep = stream_get_endp(obuf);
344
345 /* Entry count */
346 uint16_t entry_count = 0;
347
348 /* Entry count, note that this is overwritten later */
349 stream_putw(obuf, 0);
350
351 for (info = rn->info; info; info = info->next)
352 {
353 entry_count++;
354
355 /* Peer index */
356 stream_putw(obuf, info->peer->table_dump_index);
357
358 /* Originated */
359 stream_putl (obuf, info->uptime);
360
361 /* Dump attribute. */
362 /* Skip prefix & AFI/SAFI for MP_NLRI */
363 bgp_dump_routes_attr (obuf, info->attr, &rn->p);
364 }
365
366 /* Overwrite the entry count, now that we know the right number */
367 stream_putw_at (obuf, sizep, entry_count);
368
369 seq++;
370
371 bgp_dump_set_size(obuf, MSG_TABLE_DUMP_V2);
372 fwrite (STREAM_DATA (obuf), stream_get_endp (obuf), 1, bgp_dump_routes.fp);
373
374 }
375
376 fflush (bgp_dump_routes.fp);
377
378 return seq;
paul718e3742002-12-13 20:15:29 +0000379}
380
paul94f2b392005-06-28 12:44:16 +0000381static int
paul718e3742002-12-13 20:15:29 +0000382bgp_dump_interval_func (struct thread *t)
383{
384 struct bgp_dump *bgp_dump;
paul718e3742002-12-13 20:15:29 +0000385 bgp_dump = THREAD_ARG (t);
386 bgp_dump->t_interval = NULL;
387
paul9834cd02003-10-18 01:01:19 +0000388 /* Reschedule dump even if file couldn't be opened this time... */
389 if (bgp_dump_open_file (bgp_dump) != NULL)
paul718e3742002-12-13 20:15:29 +0000390 {
paul9834cd02003-10-18 01:01:19 +0000391 /* In case of bgp_dump_routes, we need special route dump function. */
392 if (bgp_dump->type == BGP_DUMP_ROUTES)
393 {
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000394 unsigned int seq = bgp_dump_routes_func (AFI_IP, 1, 0);
paula3845922003-10-18 01:30:50 +0000395#ifdef HAVE_IPV6
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000396 bgp_dump_routes_func (AFI_IP6, 0, seq);
paula3845922003-10-18 01:30:50 +0000397#endif /* HAVE_IPV6 */
paul9834cd02003-10-18 01:01:19 +0000398 /* Close the file now. For a RIB dump there's no point in leaving
399 * it open until the next scheduled dump starts. */
400 fclose(bgp_dump->fp); bgp_dump->fp = NULL;
401 }
paul718e3742002-12-13 20:15:29 +0000402 }
403
paulfba3d222003-05-10 18:33:28 +0000404 /* if interval is set reschedule */
405 if (bgp_dump->interval > 0)
406 bgp_dump_interval_add (bgp_dump, bgp_dump->interval);
407
paul718e3742002-12-13 20:15:29 +0000408 return 0;
409}
410
411/* Dump common information. */
paul94f2b392005-06-28 12:44:16 +0000412static void
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000413bgp_dump_common (struct stream *obuf, struct peer *peer, int forceas4)
paul718e3742002-12-13 20:15:29 +0000414{
415 char empty[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
416
417 /* Source AS number and Destination AS number. */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000418 if (forceas4 || CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV) )
419 {
420 stream_putl (obuf, peer->as);
421 stream_putl (obuf, peer->local_as);
422 }
423 else
424 {
425 stream_putw (obuf, peer->as);
426 stream_putw (obuf, peer->local_as);
427 }
paul718e3742002-12-13 20:15:29 +0000428
paula3845922003-10-18 01:30:50 +0000429 if (peer->su.sa.sa_family == AF_INET)
paul718e3742002-12-13 20:15:29 +0000430 {
431 stream_putw (obuf, peer->ifindex);
432 stream_putw (obuf, AFI_IP);
433
434 stream_put (obuf, &peer->su.sin.sin_addr, IPV4_MAX_BYTELEN);
435
436 if (peer->su_local)
437 stream_put (obuf, &peer->su_local->sin.sin_addr, IPV4_MAX_BYTELEN);
438 else
439 stream_put (obuf, empty, IPV4_MAX_BYTELEN);
440 }
441#ifdef HAVE_IPV6
paula3845922003-10-18 01:30:50 +0000442 else if (peer->su.sa.sa_family == AF_INET6)
paul718e3742002-12-13 20:15:29 +0000443 {
444 /* Interface Index and Address family. */
445 stream_putw (obuf, peer->ifindex);
446 stream_putw (obuf, AFI_IP6);
447
448 /* Source IP Address and Destination IP Address. */
449 stream_put (obuf, &peer->su.sin6.sin6_addr, IPV6_MAX_BYTELEN);
450
451 if (peer->su_local)
452 stream_put (obuf, &peer->su_local->sin6.sin6_addr, IPV6_MAX_BYTELEN);
453 else
454 stream_put (obuf, empty, IPV6_MAX_BYTELEN);
455 }
456#endif /* HAVE_IPV6 */
457}
458
459/* Dump BGP status change. */
460void
461bgp_dump_state (struct peer *peer, int status_old, int status_new)
462{
463 struct stream *obuf;
464
465 /* If dump file pointer is disabled return immediately. */
466 if (bgp_dump_all.fp == NULL)
467 return;
468
469 /* Make dump stream. */
470 obuf = bgp_dump_obuf;
471 stream_reset (obuf);
472
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000473 bgp_dump_header (obuf, MSG_PROTOCOL_BGP4MP, BGP4MP_STATE_CHANGE_AS4);
474 bgp_dump_common (obuf, peer, 1);/* force this in as4speak*/
paul718e3742002-12-13 20:15:29 +0000475
476 stream_putw (obuf, status_old);
477 stream_putw (obuf, status_new);
478
479 /* Set length. */
480 bgp_dump_set_size (obuf, MSG_PROTOCOL_BGP4MP);
481
482 /* Write to the stream. */
paul9985f832005-02-09 15:51:56 +0000483 fwrite (STREAM_DATA (obuf), stream_get_endp (obuf), 1, bgp_dump_all.fp);
paul718e3742002-12-13 20:15:29 +0000484 fflush (bgp_dump_all.fp);
485}
486
paul94f2b392005-06-28 12:44:16 +0000487static void
paul718e3742002-12-13 20:15:29 +0000488bgp_dump_packet_func (struct bgp_dump *bgp_dump, struct peer *peer,
489 struct stream *packet)
490{
491 struct stream *obuf;
492
493 /* If dump file pointer is disabled return immediately. */
494 if (bgp_dump->fp == NULL)
495 return;
496
497 /* Make dump stream. */
498 obuf = bgp_dump_obuf;
499 stream_reset (obuf);
500
501 /* Dump header and common part. */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000502 if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV) )
503 {
504 bgp_dump_header (obuf, MSG_PROTOCOL_BGP4MP, BGP4MP_MESSAGE_AS4);
505 }
506 else
507 {
508 bgp_dump_header (obuf, MSG_PROTOCOL_BGP4MP, BGP4MP_MESSAGE);
509 }
510 bgp_dump_common (obuf, peer, 0);
paul718e3742002-12-13 20:15:29 +0000511
512 /* Packet contents. */
513 stream_put (obuf, STREAM_DATA (packet), stream_get_endp (packet));
514
515 /* Set length. */
516 bgp_dump_set_size (obuf, MSG_PROTOCOL_BGP4MP);
517
518 /* Write to the stream. */
paul9985f832005-02-09 15:51:56 +0000519 fwrite (STREAM_DATA (obuf), stream_get_endp (obuf), 1, bgp_dump->fp);
paul718e3742002-12-13 20:15:29 +0000520 fflush (bgp_dump->fp);
521}
522
523/* Called from bgp_packet.c when BGP packet is received. */
524void
525bgp_dump_packet (struct peer *peer, int type, struct stream *packet)
526{
527 /* bgp_dump_all. */
528 bgp_dump_packet_func (&bgp_dump_all, peer, packet);
529
530 /* bgp_dump_updates. */
531 if (type == BGP_MSG_UPDATE)
532 bgp_dump_packet_func (&bgp_dump_updates, peer, packet);
533}
534
paul94f2b392005-06-28 12:44:16 +0000535static unsigned int
paulfd79ac92004-10-13 05:06:08 +0000536bgp_dump_parse_time (const char *str)
paul718e3742002-12-13 20:15:29 +0000537{
538 int i;
539 int len;
540 int seen_h;
541 int seen_m;
542 int time;
543 unsigned int total;
544
545 time = 0;
546 total = 0;
547 seen_h = 0;
548 seen_m = 0;
549 len = strlen (str);
550
551 for (i = 0; i < len; i++)
552 {
553 if (isdigit ((int) str[i]))
554 {
555 time *= 10;
556 time += str[i] - '0';
557 }
558 else if (str[i] == 'H' || str[i] == 'h')
559 {
560 if (seen_h)
561 return 0;
562 if (seen_m)
563 return 0;
564 total += time * 60 *60;
565 time = 0;
566 seen_h = 1;
567 }
568 else if (str[i] == 'M' || str[i] == 'm')
569 {
570 if (seen_m)
571 return 0;
572 total += time * 60;
573 time = 0;
574 seen_h = 1;
575 }
576 else
577 return 0;
578 }
579 return total + time;
580}
581
paul94f2b392005-06-28 12:44:16 +0000582static int
Paul Jakma45ad5922007-07-31 17:35:36 +0000583bgp_dump_set (struct vty *vty, struct bgp_dump *bgp_dump,
584 enum bgp_dump_type type, const char *path,
585 const char *interval_str)
paul718e3742002-12-13 20:15:29 +0000586{
paulfba3d222003-05-10 18:33:28 +0000587 unsigned int interval;
588
paul718e3742002-12-13 20:15:29 +0000589 if (interval_str)
590 {
paulfba3d222003-05-10 18:33:28 +0000591
paul718e3742002-12-13 20:15:29 +0000592 /* Check interval string. */
593 interval = bgp_dump_parse_time (interval_str);
594 if (interval == 0)
595 {
596 vty_out (vty, "Malformed interval string%s", VTY_NEWLINE);
597 return CMD_WARNING;
598 }
Paul Jakma45ad5922007-07-31 17:35:36 +0000599
600 /* Don't schedule duplicate dumps if the dump command is given twice */
601 if (interval == bgp_dump->interval &&
602 type == bgp_dump->type &&
603 path && bgp_dump->filename && !strcmp (path, bgp_dump->filename))
604 {
605 return CMD_SUCCESS;
606 }
607
paul718e3742002-12-13 20:15:29 +0000608 /* Set interval. */
609 bgp_dump->interval = interval;
610 if (bgp_dump->interval_str)
611 free (bgp_dump->interval_str);
612 bgp_dump->interval_str = strdup (interval_str);
paulfba3d222003-05-10 18:33:28 +0000613
paul718e3742002-12-13 20:15:29 +0000614 }
paulfba3d222003-05-10 18:33:28 +0000615 else
616 {
617 interval = 0;
618 }
619
620 /* Create interval thread. */
621 bgp_dump_interval_add (bgp_dump, interval);
paul718e3742002-12-13 20:15:29 +0000622
623 /* Set type. */
624 bgp_dump->type = type;
625
626 /* Set file name. */
627 if (bgp_dump->filename)
628 free (bgp_dump->filename);
629 bgp_dump->filename = strdup (path);
630
631 /* This should be called when interval is expired. */
632 bgp_dump_open_file (bgp_dump);
633
634 return CMD_SUCCESS;
635}
636
paul94f2b392005-06-28 12:44:16 +0000637static int
paul718e3742002-12-13 20:15:29 +0000638bgp_dump_unset (struct vty *vty, struct bgp_dump *bgp_dump)
639{
640 /* Set file name. */
641 if (bgp_dump->filename)
642 {
643 free (bgp_dump->filename);
644 bgp_dump->filename = NULL;
645 }
646
647 /* This should be called when interval is expired. */
648 if (bgp_dump->fp)
649 {
650 fclose (bgp_dump->fp);
651 bgp_dump->fp = NULL;
652 }
653
654 /* Create interval thread. */
655 if (bgp_dump->t_interval)
656 {
657 thread_cancel (bgp_dump->t_interval);
658 bgp_dump->t_interval = NULL;
659 }
660
661 bgp_dump->interval = 0;
662
663 if (bgp_dump->interval_str)
664 {
665 free (bgp_dump->interval_str);
666 bgp_dump->interval_str = NULL;
667 }
668
669
670 return CMD_SUCCESS;
671}
672
673DEFUN (dump_bgp_all,
674 dump_bgp_all_cmd,
675 "dump bgp all PATH",
676 "Dump packet\n"
677 "BGP packet dump\n"
678 "Dump all BGP packets\n"
679 "Output filename\n")
680{
681 return bgp_dump_set (vty, &bgp_dump_all, BGP_DUMP_ALL, argv[0], NULL);
682}
683
684DEFUN (dump_bgp_all_interval,
685 dump_bgp_all_interval_cmd,
686 "dump bgp all PATH INTERVAL",
687 "Dump packet\n"
688 "BGP packet dump\n"
689 "Dump all BGP packets\n"
690 "Output filename\n"
691 "Interval of output\n")
692{
693 return bgp_dump_set (vty, &bgp_dump_all, BGP_DUMP_ALL, argv[0], argv[1]);
694}
695
696DEFUN (no_dump_bgp_all,
697 no_dump_bgp_all_cmd,
698 "no dump bgp all [PATH] [INTERVAL]",
699 NO_STR
700 "Dump packet\n"
701 "BGP packet dump\n"
702 "Dump all BGP packets\n")
703{
704 return bgp_dump_unset (vty, &bgp_dump_all);
705}
706
707DEFUN (dump_bgp_updates,
708 dump_bgp_updates_cmd,
709 "dump bgp updates PATH",
710 "Dump packet\n"
711 "BGP packet dump\n"
712 "Dump BGP updates only\n"
713 "Output filename\n")
714{
715 return bgp_dump_set (vty, &bgp_dump_updates, BGP_DUMP_UPDATES, argv[0], NULL);
716}
717
718DEFUN (dump_bgp_updates_interval,
719 dump_bgp_updates_interval_cmd,
720 "dump bgp updates PATH INTERVAL",
721 "Dump packet\n"
722 "BGP packet dump\n"
723 "Dump BGP updates only\n"
724 "Output filename\n"
725 "Interval of output\n")
726{
727 return bgp_dump_set (vty, &bgp_dump_updates, BGP_DUMP_UPDATES, argv[0], argv[1]);
728}
729
730DEFUN (no_dump_bgp_updates,
731 no_dump_bgp_updates_cmd,
732 "no dump bgp updates [PATH] [INTERVAL]",
733 NO_STR
734 "Dump packet\n"
735 "BGP packet dump\n"
736 "Dump BGP updates only\n")
737{
738 return bgp_dump_unset (vty, &bgp_dump_updates);
739}
740
741DEFUN (dump_bgp_routes,
742 dump_bgp_routes_cmd,
743 "dump bgp routes-mrt PATH",
744 "Dump packet\n"
745 "BGP packet dump\n"
746 "Dump whole BGP routing table\n"
747 "Output filename\n")
748{
749 return bgp_dump_set (vty, &bgp_dump_routes, BGP_DUMP_ROUTES, argv[0], NULL);
750}
751
752DEFUN (dump_bgp_routes_interval,
753 dump_bgp_routes_interval_cmd,
754 "dump bgp routes-mrt PATH INTERVAL",
755 "Dump packet\n"
756 "BGP packet dump\n"
757 "Dump whole BGP routing table\n"
758 "Output filename\n"
759 "Interval of output\n")
760{
761 return bgp_dump_set (vty, &bgp_dump_routes, BGP_DUMP_ROUTES, argv[0], argv[1]);
762}
763
764DEFUN (no_dump_bgp_routes,
765 no_dump_bgp_routes_cmd,
766 "no dump bgp routes-mrt [PATH] [INTERVAL]",
767 NO_STR
768 "Dump packet\n"
769 "BGP packet dump\n"
770 "Dump whole BGP routing table\n")
771{
772 return bgp_dump_unset (vty, &bgp_dump_routes);
773}
774
775/* BGP node structure. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -0800776static struct cmd_node bgp_dump_node =
paul718e3742002-12-13 20:15:29 +0000777{
778 DUMP_NODE,
779 "",
hasso501ba492004-10-13 21:32:46 +0000780 1
paul718e3742002-12-13 20:15:29 +0000781};
782
783#if 0
784char *
785config_time2str (unsigned int interval)
786{
787 static char buf[BUFSIZ];
788
789 buf[0] = '\0';
790
791 if (interval / 3600)
792 {
793 sprintf (buf, "%dh", interval / 3600);
794 interval %= 3600;
795 }
796 if (interval / 60)
797 {
798 sprintf (buf + strlen (buf), "%dm", interval /60);
799 interval %= 60;
800 }
801 if (interval)
802 {
803 sprintf (buf + strlen (buf), "%d", interval);
804 }
805 return buf;
806}
807#endif
808
paul94f2b392005-06-28 12:44:16 +0000809static int
paul718e3742002-12-13 20:15:29 +0000810config_write_bgp_dump (struct vty *vty)
811{
812 if (bgp_dump_all.filename)
813 {
814 if (bgp_dump_all.interval_str)
815 vty_out (vty, "dump bgp all %s %s%s",
816 bgp_dump_all.filename, bgp_dump_all.interval_str,
817 VTY_NEWLINE);
818 else
819 vty_out (vty, "dump bgp all %s%s",
820 bgp_dump_all.filename, VTY_NEWLINE);
821 }
822 if (bgp_dump_updates.filename)
823 {
824 if (bgp_dump_updates.interval_str)
825 vty_out (vty, "dump bgp updates %s %s%s",
826 bgp_dump_updates.filename, bgp_dump_updates.interval_str,
827 VTY_NEWLINE);
828 else
829 vty_out (vty, "dump bgp updates %s%s",
830 bgp_dump_updates.filename, VTY_NEWLINE);
831 }
832 if (bgp_dump_routes.filename)
833 {
834 if (bgp_dump_routes.interval_str)
835 vty_out (vty, "dump bgp routes-mrt %s %s%s",
836 bgp_dump_routes.filename, bgp_dump_routes.interval_str,
837 VTY_NEWLINE);
838 else
839 vty_out (vty, "dump bgp routes-mrt %s%s",
840 bgp_dump_routes.filename, VTY_NEWLINE);
841 }
842 return 0;
843}
844
845/* Initialize BGP packet dump functionality. */
846void
paul94f2b392005-06-28 12:44:16 +0000847bgp_dump_init (void)
paul718e3742002-12-13 20:15:29 +0000848{
849 memset (&bgp_dump_all, 0, sizeof (struct bgp_dump));
850 memset (&bgp_dump_updates, 0, sizeof (struct bgp_dump));
851 memset (&bgp_dump_routes, 0, sizeof (struct bgp_dump));
852
paul9834cd02003-10-18 01:01:19 +0000853 bgp_dump_obuf = stream_new (BGP_MAX_PACKET_SIZE + BGP_DUMP_MSG_HEADER
854 + BGP_DUMP_HEADER_SIZE);
paul718e3742002-12-13 20:15:29 +0000855
856 install_node (&bgp_dump_node, config_write_bgp_dump);
857
858 install_element (CONFIG_NODE, &dump_bgp_all_cmd);
859 install_element (CONFIG_NODE, &dump_bgp_all_interval_cmd);
860 install_element (CONFIG_NODE, &no_dump_bgp_all_cmd);
861 install_element (CONFIG_NODE, &dump_bgp_updates_cmd);
862 install_element (CONFIG_NODE, &dump_bgp_updates_interval_cmd);
863 install_element (CONFIG_NODE, &no_dump_bgp_updates_cmd);
864 install_element (CONFIG_NODE, &dump_bgp_routes_cmd);
865 install_element (CONFIG_NODE, &dump_bgp_routes_interval_cmd);
866 install_element (CONFIG_NODE, &no_dump_bgp_routes_cmd);
867}
Chris Caputo228da422009-07-18 05:44:03 +0000868
869void
870bgp_dump_finish (void)
871{
872 stream_free (bgp_dump_obuf);
873 bgp_dump_obuf = NULL;
874}