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