Merge tag 'upstream/1.2.3'
[quagga-debian.git] / bgpd / bgp_debug.c
1 /* BGP-4, BGP-4+ packet debug routine
2    Copyright (C) 1996, 97, 99 Kunihiro Ishiguro
3
4 This file is part of GNU Zebra.
5
6 GNU Zebra is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
10
11 GNU Zebra is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Zebra; see the file COPYING.  If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.  */
20
21 #include <zebra.h>
22
23 #include <lib/version.h>
24 #include "prefix.h"
25 #include "linklist.h"
26 #include "stream.h"
27 #include "command.h"
28 #include "str.h"
29 #include "log.h"
30 #include "sockunion.h"
31 #include "filter.h"
32 #include "memory.h"
33
34 #include "bgpd/bgpd.h"
35 #include "bgpd/bgp_aspath.h"
36 #include "bgpd/bgp_route.h"
37 #include "bgpd/bgp_attr.h"
38 #include "bgpd/bgp_debug.h"
39 #include "bgpd/bgp_community.h"
40
41 unsigned long conf_bgp_debug_as4;
42 unsigned long conf_bgp_debug_fsm;
43 unsigned long conf_bgp_debug_events;
44 unsigned long conf_bgp_debug_packet;
45 unsigned long conf_bgp_debug_filter;
46 unsigned long conf_bgp_debug_keepalive;
47 unsigned long conf_bgp_debug_update;
48 unsigned long conf_bgp_debug_normal;
49 unsigned long conf_bgp_debug_zebra;
50 unsigned long conf_bgp_debug_allow_martians;
51 unsigned long conf_bgp_debug_nht;
52
53 unsigned long term_bgp_debug_as4;
54 unsigned long term_bgp_debug_fsm;
55 unsigned long term_bgp_debug_events;
56 unsigned long term_bgp_debug_packet;
57 unsigned long term_bgp_debug_filter;
58 unsigned long term_bgp_debug_keepalive;
59 unsigned long term_bgp_debug_update;
60 unsigned long term_bgp_debug_normal;
61 unsigned long term_bgp_debug_zebra;
62 unsigned long term_bgp_debug_allow_martians;
63 unsigned long term_bgp_debug_nht;
64
65 /* messages for BGP-4 status */
66 const struct message bgp_status_msg[] = 
67 {
68   { Idle, "Idle" },
69   { Connect, "Connect" },
70   { Active, "Active" },
71   { OpenSent, "OpenSent" },
72   { OpenConfirm, "OpenConfirm" },
73   { Established, "Established" },
74   { Clearing,    "Clearing"    },
75   { Deleted,     "Deleted"     },
76 };
77 #define BGP_DEBUG_MSG_MAX(msg) const int msg ## _max = array_size (msg)
78 BGP_DEBUG_MSG_MAX (bgp_status_msg);
79
80 /* BGP message type string. */
81 const char *bgp_type_str[] =
82 {
83   NULL,
84   "OPEN",
85   "UPDATE",
86   "NOTIFICATION",
87   "KEEPALIVE",
88   "ROUTE-REFRESH",
89   "CAPABILITY",
90   NULL,
91 };
92
93 /* message for BGP-4 Notify */
94 static const struct message bgp_notify_msg[] = 
95 {
96   { BGP_NOTIFY_HEADER_ERR, "Message Header Error"},
97   { BGP_NOTIFY_OPEN_ERR, "OPEN Message Error"},
98   { BGP_NOTIFY_UPDATE_ERR, "UPDATE Message Error"},
99   { BGP_NOTIFY_HOLD_ERR, "Hold Timer Expired"},
100   { BGP_NOTIFY_FSM_ERR, "Finite State Machine Error"},
101   { BGP_NOTIFY_CEASE, "Cease"},
102   { BGP_NOTIFY_CAPABILITY_ERR, "CAPABILITY Message Error"},
103 };
104 BGP_DEBUG_MSG_MAX (bgp_notify_msg);
105
106 static const struct message bgp_notify_head_msg[] = 
107 {
108   { BGP_NOTIFY_HEADER_NOT_SYNC, "/Connection Not Synchronized"},
109   { BGP_NOTIFY_HEADER_BAD_MESLEN, "/Bad Message Length"},
110   { BGP_NOTIFY_HEADER_BAD_MESTYPE, "/Bad Message Type"},
111 };
112 BGP_DEBUG_MSG_MAX (bgp_notify_head_msg);
113
114 static const struct message bgp_notify_open_msg[] = 
115 {
116   { BGP_NOTIFY_SUBCODE_UNSPECIFIC, "/Unspecific"},
117   { BGP_NOTIFY_OPEN_UNSUP_VERSION, "/Unsupported Version Number" },
118   { BGP_NOTIFY_OPEN_BAD_PEER_AS, "/Bad Peer AS"},
119   { BGP_NOTIFY_OPEN_BAD_BGP_IDENT, "/Bad BGP Identifier"},
120   { BGP_NOTIFY_OPEN_UNSUP_PARAM, "/Unsupported Optional Parameter"},
121   { BGP_NOTIFY_OPEN_AUTH_FAILURE, "/Authentication Failure"},
122   { BGP_NOTIFY_OPEN_UNACEP_HOLDTIME, "/Unacceptable Hold Time"}, 
123   { BGP_NOTIFY_OPEN_UNSUP_CAPBL, "/Unsupported Capability"},
124 };
125 BGP_DEBUG_MSG_MAX (bgp_notify_open_msg);
126
127 static const struct message bgp_notify_update_msg[] = 
128 {
129   { BGP_NOTIFY_SUBCODE_UNSPECIFIC, "/Unspecific"},
130   { BGP_NOTIFY_UPDATE_MAL_ATTR, "/Malformed Attribute List"},
131   { BGP_NOTIFY_UPDATE_UNREC_ATTR, "/Unrecognized Well-known Attribute"},
132   { BGP_NOTIFY_UPDATE_MISS_ATTR, "/Missing Well-known Attribute"},
133   { BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR, "/Attribute Flags Error"},
134   { BGP_NOTIFY_UPDATE_ATTR_LENG_ERR, "/Attribute Length Error"},
135   { BGP_NOTIFY_UPDATE_INVAL_ORIGIN, "/Invalid ORIGIN Attribute"},
136   { BGP_NOTIFY_UPDATE_AS_ROUTE_LOOP, "/AS Routing Loop"},
137   { BGP_NOTIFY_UPDATE_INVAL_NEXT_HOP, "/Invalid NEXT_HOP Attribute"},
138   { BGP_NOTIFY_UPDATE_OPT_ATTR_ERR, "/Optional Attribute Error"},
139   { BGP_NOTIFY_UPDATE_INVAL_NETWORK, "/Invalid Network Field"},
140   { BGP_NOTIFY_UPDATE_MAL_AS_PATH, "/Malformed AS_PATH"},
141 };
142 BGP_DEBUG_MSG_MAX (bgp_notify_update_msg);
143
144 static const struct message bgp_notify_cease_msg[] =
145 {
146   { BGP_NOTIFY_SUBCODE_UNSPECIFIC, "/Unspecific"},
147   { BGP_NOTIFY_CEASE_MAX_PREFIX, "/Maximum Number of Prefixes Reached"},
148   { BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN, "/Administratively Shutdown"},
149   { BGP_NOTIFY_CEASE_PEER_UNCONFIG, "/Peer Unconfigured"},
150   { BGP_NOTIFY_CEASE_ADMIN_RESET, "/Administratively Reset"},
151   { BGP_NOTIFY_CEASE_CONNECT_REJECT, "/Connection Rejected"},
152   { BGP_NOTIFY_CEASE_CONFIG_CHANGE, "/Other Configuration Change"},
153   { BGP_NOTIFY_CEASE_COLLISION_RESOLUTION, "/Connection collision resolution"},
154   { BGP_NOTIFY_CEASE_OUT_OF_RESOURCE, "/Out of Resource"},
155 };
156 BGP_DEBUG_MSG_MAX (bgp_notify_cease_msg);
157
158 static const struct message bgp_notify_capability_msg[] = 
159 {
160   { BGP_NOTIFY_SUBCODE_UNSPECIFIC, "/Unspecific"},
161   { BGP_NOTIFY_CAPABILITY_INVALID_ACTION, "/Invalid Action Value" },
162   { BGP_NOTIFY_CAPABILITY_INVALID_LENGTH, "/Invalid Capability Length"},
163   { BGP_NOTIFY_CAPABILITY_MALFORMED_CODE, "/Malformed Capability Value"},
164 };
165 BGP_DEBUG_MSG_MAX (bgp_notify_capability_msg);
166
167 /* Origin strings. */
168 const char *bgp_origin_str[] = {"i","e","?"};
169 const char *bgp_origin_long_str[] = {"IGP","EGP","incomplete"};
170
171 /* Dump attribute. */
172 int
173 bgp_dump_attr (struct peer *peer, struct attr *attr, char *buf, size_t size)
174 {
175   if (! attr)
176     return 0;
177
178   if (CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP)))
179     snprintf (buf, size, "nexthop %s", inet_ntoa (attr->nexthop));
180
181   if (CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_ORIGIN)))
182     snprintf (buf + strlen (buf), size - strlen (buf), ", origin %s",
183               bgp_origin_str[attr->origin]);
184
185   if (attr->extra)
186     {
187       char addrbuf[BUFSIZ];
188
189       /* Add MP case. */
190       if (attr->extra->mp_nexthop_len == 16 
191           || attr->extra->mp_nexthop_len == 32)
192         snprintf (buf + strlen (buf), size - strlen (buf), ", mp_nexthop %s",
193                   inet_ntop (AF_INET6, &attr->extra->mp_nexthop_global, 
194                              addrbuf, BUFSIZ));
195
196       if (attr->extra->mp_nexthop_len == 32)
197         snprintf (buf + strlen (buf), size - strlen (buf), "(%s)",
198                   inet_ntop (AF_INET6, &attr->extra->mp_nexthop_local, 
199                              addrbuf, BUFSIZ));
200     }
201
202   if (CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF)))
203     snprintf (buf + strlen (buf), size - strlen (buf), ", localpref %u",
204               attr->local_pref);
205
206   if (CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC))) 
207     snprintf (buf + strlen (buf), size - strlen (buf), ", metric %u",
208               attr->med);
209
210   if (CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES))) 
211     snprintf (buf + strlen (buf), size - strlen (buf), ", community %s",
212               community_str (attr->community));
213
214   if (CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE)))
215     snprintf (buf + strlen (buf), size - strlen (buf), ", atomic-aggregate");
216
217   if (CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR)))
218     snprintf (buf + strlen (buf), size - strlen (buf), ", aggregated by %u %s",
219               attr->extra->aggregator_as,
220               inet_ntoa (attr->extra->aggregator_addr));
221
222   if (CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_ORIGINATOR_ID)))
223     snprintf (buf + strlen (buf), size - strlen (buf), ", originator %s",
224               inet_ntoa (attr->extra->originator_id));
225
226   if (CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_CLUSTER_LIST)))
227     {
228       int i;
229
230       snprintf (buf + strlen (buf), size - strlen (buf), ", clusterlist");
231       for (i = 0; i < attr->extra->cluster->length / 4; i++)
232         snprintf (buf + strlen (buf), size - strlen (buf), " %s",
233                   inet_ntoa (attr->extra->cluster->list[i]));
234     }
235
236   if (CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_AS_PATH))) 
237     snprintf (buf + strlen (buf), size - strlen (buf), ", path %s",
238               aspath_print (attr->aspath));
239
240   if (strlen (buf) > 1)
241     return 1;
242   else
243     return 0;
244 }
245
246 /* dump notify packet */
247 void
248 bgp_notify_print(struct peer *peer, struct bgp_notify *bgp_notify, 
249                  const char *direct)
250 {
251   const char *subcode_str;
252   const char *code_str;
253
254   subcode_str = "";
255   code_str = LOOKUP_DEF (bgp_notify_msg, bgp_notify->code,
256                          "Unrecognized Error Code");
257
258   switch (bgp_notify->code)
259     {
260     case BGP_NOTIFY_HEADER_ERR:
261       subcode_str = LOOKUP_DEF (bgp_notify_head_msg, bgp_notify->subcode,
262                                 "Unrecognized Error Subcode");
263       break;
264     case BGP_NOTIFY_OPEN_ERR:
265       subcode_str = LOOKUP_DEF (bgp_notify_open_msg, bgp_notify->subcode,
266                                 "Unrecognized Error Subcode");
267       break;
268     case BGP_NOTIFY_UPDATE_ERR:
269       subcode_str = LOOKUP_DEF (bgp_notify_update_msg, bgp_notify->subcode,
270                                 "Unrecognized Error Subcode");
271       break;
272     case BGP_NOTIFY_HOLD_ERR:
273       break;
274     case BGP_NOTIFY_FSM_ERR:
275       break;
276     case BGP_NOTIFY_CEASE:
277       subcode_str = LOOKUP_DEF (bgp_notify_cease_msg, bgp_notify->subcode,
278                                 "Unrecognized Error Subcode");
279       break;
280     case BGP_NOTIFY_CAPABILITY_ERR:
281       subcode_str = LOOKUP_DEF (bgp_notify_capability_msg, bgp_notify->subcode,
282                                 "Unrecognized Error Subcode");
283       break;
284     }
285
286   if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES))
287     zlog_info ("%%NOTIFICATION: %s neighbor %s %d/%d (%s%s) %d bytes %s",
288               strcmp (direct, "received") == 0 ? "received from" : "sent to",
289               peer->host, bgp_notify->code, bgp_notify->subcode,
290               code_str, subcode_str, bgp_notify->length,
291               bgp_notify->data ? bgp_notify->data : "");
292   else if (BGP_DEBUG (normal, NORMAL))
293     plog_debug (peer->log, "%s %s NOTIFICATION %d/%d (%s%s) %d bytes %s",
294                peer ? peer->host : "",
295                direct, bgp_notify->code, bgp_notify->subcode,
296                code_str, subcode_str, bgp_notify->length,
297                bgp_notify->data ? bgp_notify->data : "");
298 }
299
300 /* Debug option setting interface. */
301 unsigned long bgp_debug_option = 0;
302
303 int  
304 debug (unsigned int option)
305 {
306   return bgp_debug_option & option; 
307 }
308
309 DEFUN (debug_bgp_as4,
310        debug_bgp_as4_cmd,
311        "debug bgp as4",
312        DEBUG_STR
313        BGP_STR
314        "BGP AS4 actions\n")
315 {
316   if (vty->node == CONFIG_NODE)
317     DEBUG_ON (as4, AS4);
318   else
319     {
320       TERM_DEBUG_ON (as4, AS4);
321       vty_out (vty, "BGP as4 debugging is on%s", VTY_NEWLINE);
322     }
323   return CMD_SUCCESS;
324 }
325
326 DEFUN (no_debug_bgp_as4,
327        no_debug_bgp_as4_cmd,
328        "no debug bgp as4",
329        NO_STR
330        DEBUG_STR
331        BGP_STR
332        "BGP AS4 actions\n")
333 {
334   if (vty->node == CONFIG_NODE)
335     DEBUG_OFF (as4, AS4);
336   else
337     {
338       TERM_DEBUG_OFF (as4, AS4);
339       vty_out (vty, "BGP as4 debugging is off%s", VTY_NEWLINE);
340     }
341   return CMD_SUCCESS;
342 }
343
344 ALIAS (no_debug_bgp_as4,
345        undebug_bgp_as4_cmd,
346        "undebug bgp as4",
347        UNDEBUG_STR
348        BGP_STR
349        "BGP AS4 actions\n")
350
351 DEFUN (debug_bgp_as4_segment,
352        debug_bgp_as4_segment_cmd,
353        "debug bgp as4 segment",
354        DEBUG_STR
355        BGP_STR
356        "BGP AS4 actions\n"
357        "BGP AS4 aspath segment handling\n")
358 {
359   if (vty->node == CONFIG_NODE)
360     DEBUG_ON (as4, AS4_SEGMENT);
361   else
362     {
363       TERM_DEBUG_ON (as4, AS4_SEGMENT);
364       vty_out (vty, "BGP as4 segment debugging is on%s", VTY_NEWLINE);
365     }
366   return CMD_SUCCESS;
367 }
368
369 DEFUN (no_debug_bgp_as4_segment,
370        no_debug_bgp_as4_segment_cmd,
371        "no debug bgp as4 segment",
372        NO_STR
373        DEBUG_STR
374        BGP_STR
375        "BGP AS4 actions\n"
376        "BGP AS4 aspath segment handling\n")
377 {
378   if (vty->node == CONFIG_NODE)
379     DEBUG_OFF (as4, AS4_SEGMENT);
380   else
381     {
382       TERM_DEBUG_OFF (as4, AS4_SEGMENT);
383       vty_out (vty, "BGP as4 segment debugging is off%s", VTY_NEWLINE);
384     }
385   return CMD_SUCCESS;
386 }
387
388 ALIAS (no_debug_bgp_as4_segment,
389        undebug_bgp_as4_segment_cmd,
390        "undebug bgp as4 segment",
391        UNDEBUG_STR
392        BGP_STR
393        "BGP AS4 actions\n"
394        "BGP AS4 aspath segment handling\n")
395
396 DEFUN (debug_bgp_fsm,
397        debug_bgp_fsm_cmd,
398        "debug bgp fsm",
399        DEBUG_STR
400        BGP_STR
401        "BGP Finite State Machine\n")
402 {
403   if (vty->node == CONFIG_NODE)
404     DEBUG_ON (fsm, FSM);
405   else
406     {
407       TERM_DEBUG_ON (fsm, FSM);
408       vty_out (vty, "BGP fsm debugging is on%s", VTY_NEWLINE);
409     }
410   return CMD_SUCCESS;
411 }
412
413 DEFUN (no_debug_bgp_fsm,
414        no_debug_bgp_fsm_cmd,
415        "no debug bgp fsm",
416        NO_STR
417        DEBUG_STR
418        BGP_STR
419        "Finite State Machine\n")
420 {
421   if (vty->node == CONFIG_NODE)
422     DEBUG_OFF (fsm, FSM);
423   else
424     {
425       TERM_DEBUG_OFF (fsm, FSM);
426       vty_out (vty, "BGP fsm debugging is off%s", VTY_NEWLINE);
427     }
428   return CMD_SUCCESS;
429 }
430
431 ALIAS (no_debug_bgp_fsm,
432        undebug_bgp_fsm_cmd,
433        "undebug bgp fsm",
434        UNDEBUG_STR
435        BGP_STR
436        "Finite State Machine\n")
437
438 DEFUN (debug_bgp_events,
439        debug_bgp_events_cmd,
440        "debug bgp events",
441        DEBUG_STR
442        BGP_STR
443        "BGP events\n")
444 {
445   if (vty->node == CONFIG_NODE)
446     DEBUG_ON (events, EVENTS);
447   else
448     {
449       TERM_DEBUG_ON (events, EVENTS);
450       vty_out (vty, "BGP events debugging is on%s", VTY_NEWLINE);
451     }
452   return CMD_SUCCESS;
453 }
454
455 DEFUN (no_debug_bgp_events,
456        no_debug_bgp_events_cmd,
457        "no debug bgp events",
458        NO_STR
459        DEBUG_STR
460        BGP_STR
461        "BGP events\n")
462 {
463   if (vty->node == CONFIG_NODE)
464     DEBUG_OFF (events, EVENTS);
465   else
466     {
467       TERM_DEBUG_OFF (events, EVENTS);
468       vty_out (vty, "BGP events debugging is off%s", VTY_NEWLINE);
469     }
470   return CMD_SUCCESS;
471 }
472
473 ALIAS (no_debug_bgp_events,
474        undebug_bgp_events_cmd,
475        "undebug bgp events",
476        UNDEBUG_STR
477        BGP_STR
478        "BGP events\n")
479
480 DEFUN (debug_bgp_nht,
481        debug_bgp_nht_cmd,
482        "debug bgp nht",
483        DEBUG_STR
484        BGP_STR
485        "BGP nexthop tracking events\n")
486 {
487   if (vty->node == CONFIG_NODE)
488     DEBUG_ON (nht, NHT);
489   else
490     {
491       TERM_DEBUG_ON (nht, NHT);
492       vty_out (vty, "BGP nexthop tracking debugging is on%s", VTY_NEWLINE);
493     }
494   return CMD_SUCCESS;
495 }
496
497 DEFUN (no_debug_bgp_nht,
498        no_debug_bgp_nht_cmd,
499        "no debug bgp nht",
500        NO_STR
501        DEBUG_STR
502        BGP_STR
503        "BGP nexthop tracking events\n")
504 {
505   if (vty->node == CONFIG_NODE)
506     DEBUG_OFF (nht, NHT);
507   else
508     {
509       TERM_DEBUG_OFF (nht, NHT);
510       vty_out (vty, "BGP nexthop tracking debugging is off%s", VTY_NEWLINE);
511     }
512   return CMD_SUCCESS;
513 }
514
515 ALIAS (no_debug_bgp_nht,
516        undebug_bgp_nht_cmd,
517        "undebug bgp nht",
518        UNDEBUG_STR
519        BGP_STR
520        "BGP next-hop tracking updates\n")
521
522 DEFUN (debug_bgp_filter,
523        debug_bgp_filter_cmd,
524        "debug bgp filters",
525        DEBUG_STR
526        BGP_STR
527        "BGP filters\n")
528 {
529   if (vty->node == CONFIG_NODE)
530     DEBUG_ON (filter, FILTER);
531   else
532     {
533       TERM_DEBUG_ON (filter, FILTER);
534       vty_out (vty, "BGP filters debugging is on%s", VTY_NEWLINE);
535     }
536   return CMD_SUCCESS;
537 }
538
539 DEFUN (no_debug_bgp_filter,
540        no_debug_bgp_filter_cmd,
541        "no debug bgp filters",
542        NO_STR
543        DEBUG_STR
544        BGP_STR
545        "BGP filters\n")
546 {
547   if (vty->node == CONFIG_NODE)
548     DEBUG_OFF (filter, FILTER);
549   else
550     {
551       TERM_DEBUG_OFF (filter, FILTER);
552       vty_out (vty, "BGP filters debugging is off%s", VTY_NEWLINE);
553     }
554   return CMD_SUCCESS;
555 }
556
557 ALIAS (no_debug_bgp_filter,
558        undebug_bgp_filter_cmd,
559        "undebug bgp filters",
560        UNDEBUG_STR
561        BGP_STR
562        "BGP filters\n")
563
564 DEFUN (debug_bgp_keepalive,
565        debug_bgp_keepalive_cmd,
566        "debug bgp keepalives",
567        DEBUG_STR
568        BGP_STR
569        "BGP keepalives\n")
570 {
571   if (vty->node == CONFIG_NODE)
572     DEBUG_ON (keepalive, KEEPALIVE);
573   else
574     {
575       TERM_DEBUG_ON (keepalive, KEEPALIVE);
576       vty_out (vty, "BGP keepalives debugging is on%s", VTY_NEWLINE);
577     }
578   return CMD_SUCCESS;
579 }
580
581 DEFUN (no_debug_bgp_keepalive,
582        no_debug_bgp_keepalive_cmd,
583        "no debug bgp keepalives",
584        NO_STR
585        DEBUG_STR
586        BGP_STR
587        "BGP keepalives\n")
588 {
589   if (vty->node == CONFIG_NODE)
590     DEBUG_OFF (keepalive, KEEPALIVE);
591   else
592     {
593       TERM_DEBUG_OFF (keepalive, KEEPALIVE);
594       vty_out (vty, "BGP keepalives debugging is off%s", VTY_NEWLINE);
595     }
596   return CMD_SUCCESS;
597 }
598
599 ALIAS (no_debug_bgp_keepalive,
600        undebug_bgp_keepalive_cmd,
601        "undebug bgp keepalives",
602        UNDEBUG_STR
603        BGP_STR
604        "BGP keepalives\n")
605
606 DEFUN (debug_bgp_update,
607        debug_bgp_update_cmd,
608        "debug bgp updates",
609        DEBUG_STR
610        BGP_STR
611        "BGP updates\n")
612 {
613   if (vty->node == CONFIG_NODE)
614     {
615       DEBUG_ON (update, UPDATE_IN);
616       DEBUG_ON (update, UPDATE_OUT);
617     }
618   else
619     {
620       TERM_DEBUG_ON (update, UPDATE_IN);
621       TERM_DEBUG_ON (update, UPDATE_OUT);
622       vty_out (vty, "BGP updates debugging is on%s", VTY_NEWLINE);
623     }
624   return CMD_SUCCESS;
625 }
626
627 DEFUN (debug_bgp_update_direct,
628        debug_bgp_update_direct_cmd,
629        "debug bgp updates (in|out)",
630        DEBUG_STR
631        BGP_STR
632        "BGP updates\n"
633        "Inbound updates\n"
634        "Outbound updates\n")
635 {
636   if (vty->node == CONFIG_NODE)
637     {
638       if (strncmp ("i", argv[0], 1) == 0)
639         {
640           DEBUG_OFF (update, UPDATE_OUT);
641           DEBUG_ON (update, UPDATE_IN);
642         }
643       else
644         {       
645           DEBUG_OFF (update, UPDATE_IN);
646           DEBUG_ON (update, UPDATE_OUT);
647         }
648     }
649   else
650     {
651       if (strncmp ("i", argv[0], 1) == 0)
652         {
653           TERM_DEBUG_OFF (update, UPDATE_OUT);
654           TERM_DEBUG_ON (update, UPDATE_IN);
655           vty_out (vty, "BGP updates debugging is on (inbound)%s", VTY_NEWLINE);
656         }
657       else
658         {
659           TERM_DEBUG_OFF (update, UPDATE_IN);
660           TERM_DEBUG_ON (update, UPDATE_OUT);
661           vty_out (vty, "BGP updates debugging is on (outbound)%s", VTY_NEWLINE);
662         }
663     }
664   return CMD_SUCCESS;
665 }
666
667 DEFUN (no_debug_bgp_update,
668        no_debug_bgp_update_cmd,
669        "no debug bgp updates",
670        NO_STR
671        DEBUG_STR
672        BGP_STR
673        "BGP updates\n")
674 {
675   if (vty->node == CONFIG_NODE)
676     {
677       DEBUG_OFF (update, UPDATE_IN);
678       DEBUG_OFF (update, UPDATE_OUT);
679     }
680   else
681     {
682       TERM_DEBUG_OFF (update, UPDATE_IN);
683       TERM_DEBUG_OFF (update, UPDATE_OUT);
684       vty_out (vty, "BGP updates debugging is off%s", VTY_NEWLINE);
685     }
686   return CMD_SUCCESS;
687 }
688
689 ALIAS (no_debug_bgp_update,
690        undebug_bgp_update_cmd,
691        "undebug bgp updates",
692        UNDEBUG_STR
693        BGP_STR
694        "BGP updates\n")
695
696 DEFUN (debug_bgp_normal,
697        debug_bgp_normal_cmd,
698        "debug bgp",
699        DEBUG_STR
700        BGP_STR)
701 {
702   if (vty->node == CONFIG_NODE)
703     DEBUG_ON (normal, NORMAL);
704   else
705     {
706       TERM_DEBUG_ON (normal, NORMAL);
707       vty_out (vty, "BGP debugging is on%s", VTY_NEWLINE);
708     }
709   return CMD_SUCCESS;
710 }
711
712 DEFUN (no_debug_bgp_normal,
713        no_debug_bgp_normal_cmd,
714        "no debug bgp",
715        NO_STR
716        DEBUG_STR
717        BGP_STR)
718 {
719   if (vty->node == CONFIG_NODE)
720     DEBUG_OFF (normal, NORMAL);
721   else
722     {
723       TERM_DEBUG_OFF (normal, NORMAL);
724       vty_out (vty, "BGP debugging is off%s", VTY_NEWLINE);
725     }
726   return CMD_SUCCESS;
727 }
728
729 ALIAS (no_debug_bgp_normal,
730        undebug_bgp_normal_cmd,
731        "undebug bgp",
732        UNDEBUG_STR
733        BGP_STR)
734
735 DEFUN (debug_bgp_zebra,
736        debug_bgp_zebra_cmd,
737        "debug bgp zebra",
738        DEBUG_STR
739        BGP_STR
740        "BGP Zebra messages\n")
741 {
742   if (vty->node == CONFIG_NODE)
743     DEBUG_ON (zebra, ZEBRA);
744   else
745     {
746       TERM_DEBUG_ON (zebra, ZEBRA);
747       vty_out (vty, "BGP zebra debugging is on%s", VTY_NEWLINE);
748     }
749   return CMD_SUCCESS;
750 }
751
752 DEFUN (no_debug_bgp_zebra,
753        no_debug_bgp_zebra_cmd,
754        "no debug bgp zebra",
755        NO_STR
756        DEBUG_STR
757        BGP_STR
758        "BGP Zebra messages\n")
759 {
760   if (vty->node == CONFIG_NODE)
761     DEBUG_OFF (zebra, ZEBRA);
762   else
763     {
764       TERM_DEBUG_OFF (zebra, ZEBRA);
765       vty_out (vty, "BGP zebra debugging is off%s", VTY_NEWLINE);
766     }
767   return CMD_SUCCESS;
768 }
769
770 ALIAS (no_debug_bgp_zebra,
771        undebug_bgp_zebra_cmd,
772        "undebug bgp zebra",
773        UNDEBUG_STR
774        BGP_STR
775        "BGP Zebra messages\n")
776
777 DEFUN (debug_bgp_allow_martians,
778        debug_bgp_allow_martians_cmd,
779        "debug bgp allow-martians",
780        DEBUG_STR
781        BGP_STR
782        "BGP allow martian next hops\n")
783 {
784   if (vty->node == CONFIG_NODE)
785     DEBUG_ON (allow_martians, ALLOW_MARTIANS);
786   else
787     {
788       TERM_DEBUG_ON (allow_martians, ALLOW_MARTIANS);
789       vty_out (vty, "BGP allow_martian next hop debugging is on%s", VTY_NEWLINE);
790     }
791   return CMD_SUCCESS;
792 }
793
794 DEFUN (no_debug_bgp_allow_martians,
795        no_debug_bgp_allow_martians_cmd,
796        "no debug bgp allow-martians",
797        NO_STR
798        DEBUG_STR
799        BGP_STR
800        "BGP allow martian next hops\n")
801 {
802   if (vty->node == CONFIG_NODE)
803     DEBUG_OFF (allow_martians, ALLOW_MARTIANS);
804   else
805     {
806       TERM_DEBUG_OFF (allow_martians, ALLOW_MARTIANS);
807       vty_out (vty, "BGP allow martian next hop debugging is off%s", VTY_NEWLINE);
808     }
809   return CMD_SUCCESS;
810 }
811
812 ALIAS (no_debug_bgp_allow_martians,
813        undebug_bgp_allow_martians_cmd,
814        "undebug bgp allow-martians",
815        UNDEBUG_STR
816        BGP_STR
817        "BGP allow martian next hops\n")
818
819 DEFUN (no_debug_bgp_all,
820        no_debug_bgp_all_cmd,
821        "no debug all bgp",
822        NO_STR
823        DEBUG_STR
824        "Enable all debugging\n"
825        BGP_STR)
826 {
827   TERM_DEBUG_OFF (normal, NORMAL);
828   TERM_DEBUG_OFF (events, EVENTS);
829   TERM_DEBUG_OFF (keepalive, KEEPALIVE);
830   TERM_DEBUG_OFF (update, UPDATE_IN);
831   TERM_DEBUG_OFF (update, UPDATE_OUT);
832   TERM_DEBUG_OFF (as4, AS4);
833   TERM_DEBUG_OFF (as4, AS4_SEGMENT);
834   TERM_DEBUG_OFF (fsm, FSM);
835   TERM_DEBUG_OFF (filter, FILTER);
836   TERM_DEBUG_OFF (zebra, ZEBRA);
837   TERM_DEBUG_OFF (allow_martians, ALLOW_MARTIANS);
838   vty_out (vty, "All possible debugging has been turned off%s", VTY_NEWLINE);
839       
840   return CMD_SUCCESS;
841 }
842
843 ALIAS (no_debug_bgp_all,
844        undebug_bgp_all_cmd,
845        "undebug all bgp",
846        UNDEBUG_STR
847        "Enable all debugging\n"
848        BGP_STR)
849
850 DEFUN (show_debugging_bgp,
851        show_debugging_bgp_cmd,
852        "show debugging bgp",
853        SHOW_STR
854        DEBUG_STR
855        BGP_STR)
856 {
857   vty_out (vty, "BGP debugging status:%s", VTY_NEWLINE);
858
859   if (BGP_DEBUG (normal, NORMAL))
860     vty_out (vty, "  BGP debugging is on%s", VTY_NEWLINE);
861   if (BGP_DEBUG (events, EVENTS))
862     vty_out (vty, "  BGP events debugging is on%s", VTY_NEWLINE);
863   if (BGP_DEBUG (keepalive, KEEPALIVE))
864     vty_out (vty, "  BGP keepalives debugging is on%s", VTY_NEWLINE);
865   if (BGP_DEBUG (update, UPDATE_IN) && BGP_DEBUG (update, UPDATE_OUT))
866     vty_out (vty, "  BGP updates debugging is on%s", VTY_NEWLINE);
867   else if (BGP_DEBUG (update, UPDATE_IN))
868     vty_out (vty, "  BGP updates debugging is on (inbound)%s", VTY_NEWLINE);
869   else if (BGP_DEBUG (update, UPDATE_OUT))
870     vty_out (vty, "  BGP updates debugging is on (outbound)%s", VTY_NEWLINE);
871   if (BGP_DEBUG (fsm, FSM))
872     vty_out (vty, "  BGP fsm debugging is on%s", VTY_NEWLINE);
873   if (BGP_DEBUG (filter, FILTER))
874     vty_out (vty, "  BGP filter debugging is on%s", VTY_NEWLINE);
875   if (BGP_DEBUG (zebra, ZEBRA))
876     vty_out (vty, "  BGP zebra debugging is on%s", VTY_NEWLINE);
877   if (BGP_DEBUG (as4, AS4))
878     vty_out (vty, "  BGP as4 debugging is on%s", VTY_NEWLINE);
879   if (BGP_DEBUG (as4, AS4_SEGMENT))
880     vty_out (vty, "  BGP as4 aspath segment debugging is on%s", VTY_NEWLINE);
881   if (BGP_DEBUG (allow_martians, ALLOW_MARTIANS))
882     vty_out (vty, "  BGP allow martian next hop debugging is on%s", VTY_NEWLINE);
883   if (BGP_DEBUG (nht, NHT))
884     vty_out (vty, "  BGP next-hop tracking debugging is on%s", VTY_NEWLINE);
885   vty_out (vty, "%s", VTY_NEWLINE);
886   return CMD_SUCCESS;
887 }
888
889 static int
890 bgp_config_write_debug (struct vty *vty)
891 {
892   int write = 0;
893
894   if (CONF_BGP_DEBUG (normal, NORMAL))
895     {
896       vty_out (vty, "debug bgp%s", VTY_NEWLINE);
897       write++;
898     }
899
900   if (CONF_BGP_DEBUG (as4, AS4))
901     {
902       vty_out (vty, "debug bgp as4%s", VTY_NEWLINE);
903       write++;
904     }
905
906   if (CONF_BGP_DEBUG (as4, AS4_SEGMENT))
907     {
908       vty_out (vty, "debug bgp as4 segment%s", VTY_NEWLINE);
909       write++;
910     }
911
912   if (CONF_BGP_DEBUG (events, EVENTS))
913     {
914       vty_out (vty, "debug bgp events%s", VTY_NEWLINE);
915       write++;
916     }
917
918   if (CONF_BGP_DEBUG (keepalive, KEEPALIVE))
919     {
920       vty_out (vty, "debug bgp keepalives%s", VTY_NEWLINE);
921       write++;
922     }
923
924   if (CONF_BGP_DEBUG (update, UPDATE_IN) && CONF_BGP_DEBUG (update, UPDATE_OUT))
925     {
926       vty_out (vty, "debug bgp updates%s", VTY_NEWLINE);
927       write++;
928     }
929   else if (CONF_BGP_DEBUG (update, UPDATE_IN))
930     {
931       vty_out (vty, "debug bgp updates in%s", VTY_NEWLINE);
932       write++;
933     }
934   else if (CONF_BGP_DEBUG (update, UPDATE_OUT))
935     {
936       vty_out (vty, "debug bgp updates out%s", VTY_NEWLINE);
937       write++;
938     }
939
940   if (CONF_BGP_DEBUG (fsm, FSM))
941     {
942       vty_out (vty, "debug bgp fsm%s", VTY_NEWLINE);
943       write++;
944     }
945
946   if (CONF_BGP_DEBUG (filter, FILTER))
947     {
948       vty_out (vty, "debug bgp filters%s", VTY_NEWLINE);
949       write++;
950     }
951
952   if (CONF_BGP_DEBUG (zebra, ZEBRA))
953     {
954       vty_out (vty, "debug bgp zebra%s", VTY_NEWLINE);
955       write++;
956     }
957
958   if (CONF_BGP_DEBUG (allow_martians, ALLOW_MARTIANS))
959     {
960       vty_out (vty, "debug bgp allow-martians%s", VTY_NEWLINE);
961       write++;
962     }
963   
964   if (CONF_BGP_DEBUG (nht, NHT))
965     {
966       vty_out (vty, "debug bgp nht%s", VTY_NEWLINE);
967       write++;
968     }
969
970   return write;
971 }
972
973 static struct cmd_node debug_node =
974 {
975   DEBUG_NODE,
976   "",
977   1
978 };
979
980 void
981 bgp_debug_init (void)
982 {
983   install_node (&debug_node, bgp_config_write_debug);
984
985   install_element (ENABLE_NODE, &show_debugging_bgp_cmd);
986
987   install_element (ENABLE_NODE, &debug_bgp_as4_cmd);
988   install_element (CONFIG_NODE, &debug_bgp_as4_cmd);
989   install_element (ENABLE_NODE, &debug_bgp_as4_segment_cmd);
990   install_element (CONFIG_NODE, &debug_bgp_as4_segment_cmd);
991
992   install_element (ENABLE_NODE, &debug_bgp_fsm_cmd);
993   install_element (CONFIG_NODE, &debug_bgp_fsm_cmd);
994   install_element (ENABLE_NODE, &debug_bgp_events_cmd);
995   install_element (CONFIG_NODE, &debug_bgp_events_cmd);
996   install_element (ENABLE_NODE, &debug_bgp_nht_cmd);
997   install_element (CONFIG_NODE, &debug_bgp_nht_cmd);
998   install_element (ENABLE_NODE, &debug_bgp_filter_cmd);
999   install_element (CONFIG_NODE, &debug_bgp_filter_cmd);
1000   install_element (ENABLE_NODE, &debug_bgp_keepalive_cmd);
1001   install_element (CONFIG_NODE, &debug_bgp_keepalive_cmd);
1002   install_element (ENABLE_NODE, &debug_bgp_update_cmd);
1003   install_element (CONFIG_NODE, &debug_bgp_update_cmd);
1004   install_element (ENABLE_NODE, &debug_bgp_update_direct_cmd);
1005   install_element (CONFIG_NODE, &debug_bgp_update_direct_cmd);
1006   install_element (ENABLE_NODE, &debug_bgp_normal_cmd);
1007   install_element (CONFIG_NODE, &debug_bgp_normal_cmd);
1008   install_element (ENABLE_NODE, &debug_bgp_zebra_cmd);
1009   install_element (CONFIG_NODE, &debug_bgp_zebra_cmd);
1010   install_element (ENABLE_NODE, &debug_bgp_allow_martians_cmd);
1011   install_element (CONFIG_NODE, &debug_bgp_allow_martians_cmd);
1012
1013   install_element (ENABLE_NODE, &no_debug_bgp_as4_cmd);
1014   install_element (ENABLE_NODE, &undebug_bgp_as4_cmd);
1015   install_element (CONFIG_NODE, &no_debug_bgp_as4_cmd);
1016   install_element (ENABLE_NODE, &no_debug_bgp_as4_segment_cmd);
1017   install_element (ENABLE_NODE, &undebug_bgp_as4_segment_cmd);
1018   install_element (CONFIG_NODE, &no_debug_bgp_as4_segment_cmd);
1019
1020   install_element (ENABLE_NODE, &no_debug_bgp_fsm_cmd);
1021   install_element (ENABLE_NODE, &undebug_bgp_fsm_cmd);
1022   install_element (CONFIG_NODE, &no_debug_bgp_fsm_cmd);
1023   install_element (ENABLE_NODE, &no_debug_bgp_events_cmd);
1024   install_element (ENABLE_NODE, &undebug_bgp_events_cmd);
1025   install_element (CONFIG_NODE, &no_debug_bgp_events_cmd);
1026   install_element (ENABLE_NODE, &no_debug_bgp_nht_cmd);
1027   install_element (ENABLE_NODE, &undebug_bgp_nht_cmd);
1028   install_element (CONFIG_NODE, &no_debug_bgp_nht_cmd);
1029   install_element (ENABLE_NODE, &no_debug_bgp_filter_cmd);
1030   install_element (ENABLE_NODE, &undebug_bgp_filter_cmd);
1031   install_element (CONFIG_NODE, &no_debug_bgp_filter_cmd);
1032   install_element (ENABLE_NODE, &no_debug_bgp_keepalive_cmd);
1033   install_element (ENABLE_NODE, &undebug_bgp_keepalive_cmd);
1034   install_element (CONFIG_NODE, &no_debug_bgp_keepalive_cmd);
1035   install_element (ENABLE_NODE, &no_debug_bgp_update_cmd);
1036   install_element (ENABLE_NODE, &undebug_bgp_update_cmd);
1037   install_element (CONFIG_NODE, &no_debug_bgp_update_cmd);
1038   install_element (ENABLE_NODE, &no_debug_bgp_normal_cmd);
1039   install_element (ENABLE_NODE, &undebug_bgp_normal_cmd);
1040   install_element (CONFIG_NODE, &no_debug_bgp_normal_cmd);
1041   install_element (ENABLE_NODE, &no_debug_bgp_zebra_cmd);
1042   install_element (ENABLE_NODE, &undebug_bgp_zebra_cmd);
1043   install_element (CONFIG_NODE, &no_debug_bgp_zebra_cmd);
1044   install_element (ENABLE_NODE, &no_debug_bgp_allow_martians_cmd);
1045   install_element (ENABLE_NODE, &undebug_bgp_allow_martians_cmd);
1046   install_element (CONFIG_NODE, &no_debug_bgp_allow_martians_cmd);
1047   install_element (ENABLE_NODE, &no_debug_bgp_all_cmd);
1048   install_element (ENABLE_NODE, &undebug_bgp_all_cmd);
1049 }