1/* BGP routing information
2   Copyright (C) 1996, 97, 98, 99 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 "prefix.h"
24#include "linklist.h"
25#include "memory.h"
26#include "command.h"
27#include "stream.h"
28#include "filter.h"
29#include "str.h"
30#include "log.h"
31#include "routemap.h"
32#include "buffer.h"
33#include "sockunion.h"
34#include "plist.h"
35#include "thread.h"
36#include "workqueue.h"
37
38#include "bgpd/bgpd.h"
39#include "bgpd/bgp_table.h"
40#include "bgpd/bgp_route.h"
41#include "bgpd/bgp_attr.h"
42#include "bgpd/bgp_debug.h"
43#include "bgpd/bgp_aspath.h"
44#include "bgpd/bgp_regex.h"
45#include "bgpd/bgp_community.h"
46#include "bgpd/bgp_ecommunity.h"
47#include "bgpd/bgp_clist.h"
48#include "bgpd/bgp_packet.h"
49#include "bgpd/bgp_filter.h"
50#include "bgpd/bgp_fsm.h"
51#include "bgpd/bgp_mplsvpn.h"
52#include "bgpd/bgp_nexthop.h"
53#include "bgpd/bgp_damp.h"
54#include "bgpd/bgp_advertise.h"
55#include "bgpd/bgp_zebra.h"
56#include "bgpd/bgp_vty.h"
57#include "bgpd/bgp_mpath.h"
58
59/* Extern from bgp_dump.c */
60extern const char *bgp_origin_str[];
61extern const char *bgp_origin_long_str[];
62
63static struct bgp_node *
64bgp_afi_node_get (struct bgp_table *table, afi_t afi, safi_t safi, struct prefix *p,
65		  struct prefix_rd *prd)
66{
67  struct bgp_node *rn;
68  struct bgp_node *prn = NULL;
69
70  assert (table);
71  if (!table)
72    return NULL;
73
74  if (safi == SAFI_MPLS_VPN)
75    {
76      prn = bgp_node_get (table, (struct prefix *) prd);
77
78      if (prn->info == NULL)
79	prn->info = bgp_table_init (afi, safi);
80      else
81	bgp_unlock_node (prn);
82      table = prn->info;
83    }
84
85  rn = bgp_node_get (table, p);
86
87  if (safi == SAFI_MPLS_VPN)
88    rn->prn = prn;
89
90  return rn;
91}
92
93/* Allocate bgp_info_extra */
94static struct bgp_info_extra *
95bgp_info_extra_new (void)
96{
97  struct bgp_info_extra *new;
98  new = XCALLOC (MTYPE_BGP_ROUTE_EXTRA, sizeof (struct bgp_info_extra));
99  return new;
100}
101
102static void
103bgp_info_extra_free (struct bgp_info_extra **extra)
104{
105  if (extra && *extra)
106    {
107      if ((*extra)->damp_info)
108        bgp_damp_info_free ((*extra)->damp_info, 0);
109
110      (*extra)->damp_info = NULL;
111
112      XFREE (MTYPE_BGP_ROUTE_EXTRA, *extra);
113
114      *extra = NULL;
115    }
116}
117
118/* Get bgp_info extra information for the given bgp_info, lazy allocated
119 * if required.
120 */
121struct bgp_info_extra *
122bgp_info_extra_get (struct bgp_info *ri)
123{
124  if (!ri->extra)
125    ri->extra = bgp_info_extra_new();
126  return ri->extra;
127}
128
129/* Allocate new bgp info structure. */
130static struct bgp_info *
131bgp_info_new (void)
132{
133  return XCALLOC (MTYPE_BGP_ROUTE, sizeof (struct bgp_info));
134}
135
136/* Free bgp route information. */
137static void
138bgp_info_free (struct bgp_info *binfo)
139{
140  if (binfo->attr)
141    bgp_attr_unintern (&binfo->attr);
142
143  bgp_info_extra_free (&binfo->extra);
144  bgp_info_mpath_free (&binfo->mpath);
145
146  peer_unlock (binfo->peer); /* bgp_info peer reference */
147
148  XFREE (MTYPE_BGP_ROUTE, binfo);
149}
150
151struct bgp_info *
152bgp_info_lock (struct bgp_info *binfo)
153{
154  binfo->lock++;
155  return binfo;
156}
157
158struct bgp_info *
159bgp_info_unlock (struct bgp_info *binfo)
160{
161  assert (binfo && binfo->lock > 0);
162  binfo->lock--;
163
164  if (binfo->lock == 0)
165    {
166#if 0
167      zlog_debug ("%s: unlocked and freeing", __func__);
168      zlog_backtrace (LOG_DEBUG);
169#endif
170      bgp_info_free (binfo);
171      return NULL;
172    }
173
174#if 0
175  if (binfo->lock == 1)
176    {
177      zlog_debug ("%s: unlocked to 1", __func__);
178      zlog_backtrace (LOG_DEBUG);
179    }
180#endif
181
182  return binfo;
183}
184
185void
186bgp_info_add (struct bgp_node *rn, struct bgp_info *ri)
187{
188  struct bgp_info *top;
189
190  top = rn->info;
191
192  ri->next = rn->info;
193  ri->prev = NULL;
194  if (top)
195    top->prev = ri;
196  rn->info = ri;
197
198  bgp_info_lock (ri);
199  bgp_lock_node (rn);
200  peer_lock (ri->peer); /* bgp_info peer reference */
201}
202
203/* Do the actual removal of info from RIB, for use by bgp_process
204   completion callback *only* */
205static void
206bgp_info_reap (struct bgp_node *rn, struct bgp_info *ri)
207{
208  if (ri->next)
209    ri->next->prev = ri->prev;
210  if (ri->prev)
211    ri->prev->next = ri->next;
212  else
213    rn->info = ri->next;
214
215  bgp_info_mpath_dequeue (ri);
216  bgp_info_unlock (ri);
217  bgp_unlock_node (rn);
218}
219
220void
221bgp_info_delete (struct bgp_node *rn, struct bgp_info *ri)
222{
223  bgp_info_set_flag (rn, ri, BGP_INFO_REMOVED);
224  /* set of previous already took care of pcount */
225  UNSET_FLAG (ri->flags, BGP_INFO_VALID);
226}
227
228/* undo the effects of a previous call to bgp_info_delete; typically
229   called when a route is deleted and then quickly re-added before the
230   deletion has been processed */
231static void
232bgp_info_restore (struct bgp_node *rn, struct bgp_info *ri)
233{
234  bgp_info_unset_flag (rn, ri, BGP_INFO_REMOVED);
235  /* unset of previous already took care of pcount */
236  SET_FLAG (ri->flags, BGP_INFO_VALID);
237}
238
239/* Adjust pcount as required */
240static void
241bgp_pcount_adjust (struct bgp_node *rn, struct bgp_info *ri)
242{
243  struct bgp_table *table;
244
245  assert (rn && bgp_node_table (rn));
246  assert (ri && ri->peer && ri->peer->bgp);
247
248  table = bgp_node_table (rn);
249
250  /* Ignore 'pcount' for RS-client tables */
251  if (table->type != BGP_TABLE_MAIN
252      || ri->peer == ri->peer->bgp->peer_self)
253    return;
254
255  if (BGP_INFO_HOLDDOWN (ri)
256      && CHECK_FLAG (ri->flags, BGP_INFO_COUNTED))
257    {
258
259      UNSET_FLAG (ri->flags, BGP_INFO_COUNTED);
260
261      /* slight hack, but more robust against errors. */
262      if (ri->peer->pcount[table->afi][table->safi])
263        ri->peer->pcount[table->afi][table->safi]--;
264      else
265        {
266          zlog_warn ("%s: Asked to decrement 0 prefix count for peer %s",
267                     __func__, ri->peer->host);
268          zlog_backtrace (LOG_WARNING);
269          zlog_warn ("%s: Please report to Quagga bugzilla", __func__);
270        }
271    }
272  else if (!BGP_INFO_HOLDDOWN (ri)
273           && !CHECK_FLAG (ri->flags, BGP_INFO_COUNTED))
274    {
275      SET_FLAG (ri->flags, BGP_INFO_COUNTED);
276      ri->peer->pcount[table->afi][table->safi]++;
277    }
278}
279
280
281/* Set/unset bgp_info flags, adjusting any other state as needed.
282 * This is here primarily to keep prefix-count in check.
283 */
284void
285bgp_info_set_flag (struct bgp_node *rn, struct bgp_info *ri, u_int32_t flag)
286{
287  SET_FLAG (ri->flags, flag);
288
289  /* early bath if we know it's not a flag that changes useability state */
290  if (!CHECK_FLAG (flag, BGP_INFO_VALID|BGP_INFO_UNUSEABLE))
291    return;
292
293  bgp_pcount_adjust (rn, ri);
294}
295
296void
297bgp_info_unset_flag (struct bgp_node *rn, struct bgp_info *ri, u_int32_t flag)
298{
299  UNSET_FLAG (ri->flags, flag);
300
301  /* early bath if we know it's not a flag that changes useability state */
302  if (!CHECK_FLAG (flag, BGP_INFO_VALID|BGP_INFO_UNUSEABLE))
303    return;
304
305  bgp_pcount_adjust (rn, ri);
306}
307
308/* Get MED value.  If MED value is missing and "bgp bestpath
309   missing-as-worst" is specified, treat it as the worst value. */
310static u_int32_t
311bgp_med_value (struct attr *attr, struct bgp *bgp)
312{
313  if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC))
314    return attr->med;
315  else
316    {
317      if (bgp_flag_check (bgp, BGP_FLAG_MED_MISSING_AS_WORST))
318	return BGP_MED_MAX;
319      else
320	return 0;
321    }
322}
323
324/* Compare two bgp route entity.  br is preferable then return 1. */
325static int
326bgp_info_cmp (struct bgp *bgp, struct bgp_info *new, struct bgp_info *exist,
327	      int *paths_eq)
328{
329  struct attr *newattr, *existattr;
330  struct attr_extra *newattre, *existattre;
331  bgp_peer_sort_t new_sort;
332  bgp_peer_sort_t exist_sort;
333  u_int32_t new_pref;
334  u_int32_t exist_pref;
335  u_int32_t new_med;
336  u_int32_t exist_med;
337  u_int32_t new_weight;
338  u_int32_t exist_weight;
339  uint32_t newm, existm;
340  struct in_addr new_id;
341  struct in_addr exist_id;
342  int new_cluster;
343  int exist_cluster;
344  int internal_as_route;
345  int confed_as_route;
346  int ret;
347
348  *paths_eq = 0;
349
350  /* 0. Null check. */
351  if (new == NULL)
352    return 0;
353  if (exist == NULL)
354    return 1;
355
356  newattr = new->attr;
357  existattr = exist->attr;
358  newattre = newattr->extra;
359  existattre = existattr->extra;
360
361  /* 1. Weight check. */
362  new_weight = exist_weight = 0;
363
364  if (newattre)
365    new_weight = newattre->weight;
366  if (existattre)
367    exist_weight = existattre->weight;
368
369  if (new_weight > exist_weight)
370    return 1;
371  if (new_weight < exist_weight)
372    return 0;
373
374  /* 2. Local preference check. */
375  new_pref = exist_pref = bgp->default_local_pref;
376
377  if (newattr->flag & ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF))
378    new_pref = newattr->local_pref;
379  if (existattr->flag & ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF))
380    exist_pref = existattr->local_pref;
381
382  if (new_pref > exist_pref)
383    return 1;
384  if (new_pref < exist_pref)
385    return 0;
386
387  /* 3. Local route check. We prefer:
388   *  - BGP_ROUTE_STATIC
389   *  - BGP_ROUTE_AGGREGATE
390   *  - BGP_ROUTE_REDISTRIBUTE
391   */
392  if (! (new->sub_type == BGP_ROUTE_NORMAL))
393     return 1;
394  if (! (exist->sub_type == BGP_ROUTE_NORMAL))
395     return 0;
396
397  /* 4. AS path length check. */
398  if (! bgp_flag_check (bgp, BGP_FLAG_ASPATH_IGNORE))
399    {
400      int exist_hops = aspath_count_hops (existattr->aspath);
401      int exist_confeds = aspath_count_confeds (existattr->aspath);
402
403      if (bgp_flag_check (bgp, BGP_FLAG_ASPATH_CONFED))
404	{
405	  int aspath_hops;
406
407	  aspath_hops = aspath_count_hops (newattr->aspath);
408          aspath_hops += aspath_count_confeds (newattr->aspath);
409
410	  if ( aspath_hops < (exist_hops + exist_confeds))
411	    return 1;
412	  if ( aspath_hops > (exist_hops + exist_confeds))
413	    return 0;
414	}
415      else
416	{
417	  int newhops = aspath_count_hops (newattr->aspath);
418
419	  if (newhops < exist_hops)
420	    return 1;
421          if (newhops > exist_hops)
422	    return 0;
423	}
424    }
425
426  /* 5. Origin check. */
427  if (newattr->origin < existattr->origin)
428    return 1;
429  if (newattr->origin > existattr->origin)
430    return 0;
431
432  /* 6. MED check. */
433  internal_as_route = (aspath_count_hops (newattr->aspath) == 0
434		      && aspath_count_hops (existattr->aspath) == 0);
435  confed_as_route = (aspath_count_confeds (newattr->aspath) > 0
436		    && aspath_count_confeds (existattr->aspath) > 0
437		    && aspath_count_hops (newattr->aspath) == 0
438		    && aspath_count_hops (existattr->aspath) == 0);
439
440  if (bgp_flag_check (bgp, BGP_FLAG_ALWAYS_COMPARE_MED)
441      || (bgp_flag_check (bgp, BGP_FLAG_MED_CONFED)
442	 && confed_as_route)
443      || aspath_cmp_left (newattr->aspath, existattr->aspath)
444      || aspath_cmp_left_confed (newattr->aspath, existattr->aspath)
445      || internal_as_route)
446    {
447      new_med = bgp_med_value (new->attr, bgp);
448      exist_med = bgp_med_value (exist->attr, bgp);
449
450      if (new_med < exist_med)
451	return 1;
452      if (new_med > exist_med)
453	return 0;
454    }
455
456  /* 7. Peer type check. */
457  new_sort = new->peer->sort;
458  exist_sort = exist->peer->sort;
459
460  if (new_sort == BGP_PEER_EBGP
461      && (exist_sort == BGP_PEER_IBGP || exist_sort == BGP_PEER_CONFED))
462    return 1;
463  if (exist_sort == BGP_PEER_EBGP
464      && (new_sort == BGP_PEER_IBGP || new_sort == BGP_PEER_CONFED))
465    return 0;
466
467  /* 8. IGP metric check. */
468  newm = existm = 0;
469
470  if (new->extra)
471    newm = new->extra->igpmetric;
472  if (exist->extra)
473    existm = exist->extra->igpmetric;
474
475  if (newm < existm)
476    ret = 1;
477  if (newm > existm)
478    ret = 0;
479
480  /* 9. Maximum path check. */
481  if (newm == existm)
482    {
483      if (bgp_flag_check(bgp, BGP_FLAG_ASPATH_MULTIPATH_RELAX))
484        {
485
486	  /*
487	   * For the two paths, all comparison steps till IGP metric
488	   * have succeeded - including AS_PATH hop count. Since 'bgp
489	   * bestpath as-path multipath-relax' knob is on, we don't need
490	   * an exact match of AS_PATH. Thus, mark the paths are equal.
491	   * That will trigger both these paths to get into the multipath
492	   * array.
493	   */
494	  *paths_eq = 1;
495        }
496      else if (new->peer->sort == BGP_PEER_IBGP)
497	{
498	  if (aspath_cmp (new->attr->aspath, exist->attr->aspath))
499	    *paths_eq = 1;
500	}
501      else if (new->peer->as == exist->peer->as)
502	*paths_eq = 1;
503    }
504  else
505    {
506      /*
507       * TODO: If unequal cost ibgp multipath is enabled we can
508       * mark the paths as equal here instead of returning
509       */
510      return ret;
511    }
512
513  /* 10. If both paths are external, prefer the path that was received
514     first (the oldest one).  This step minimizes route-flap, since a
515     newer path won't displace an older one, even if it was the
516     preferred route based on the additional decision criteria below.  */
517  if (! bgp_flag_check (bgp, BGP_FLAG_COMPARE_ROUTER_ID)
518      && new_sort == BGP_PEER_EBGP
519      && exist_sort == BGP_PEER_EBGP)
520    {
521      if (CHECK_FLAG (new->flags, BGP_INFO_SELECTED))
522	return 1;
523      if (CHECK_FLAG (exist->flags, BGP_INFO_SELECTED))
524	return 0;
525    }
526
527  /* 11. Rourter-ID comparision. */
528  if (newattr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
529    new_id.s_addr = newattre->originator_id.s_addr;
530  else
531    new_id.s_addr = new->peer->remote_id.s_addr;
532  if (existattr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
533    exist_id.s_addr = existattre->originator_id.s_addr;
534  else
535    exist_id.s_addr = exist->peer->remote_id.s_addr;
536
537  if (ntohl (new_id.s_addr) < ntohl (exist_id.s_addr))
538    return 1;
539  if (ntohl (new_id.s_addr) > ntohl (exist_id.s_addr))
540    return 0;
541
542  /* 12. Cluster length comparision. */
543  new_cluster = exist_cluster = 0;
544
545  if (newattr->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST))
546    new_cluster = newattre->cluster->length;
547  if (existattr->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST))
548    exist_cluster = existattre->cluster->length;
549
550  if (new_cluster < exist_cluster)
551    return 1;
552  if (new_cluster > exist_cluster)
553    return 0;
554
555  /* 13. Neighbor address comparision. */
556  ret = sockunion_cmp (new->peer->su_remote, exist->peer->su_remote);
557
558  if (ret == 1)
559    return 0;
560  if (ret == -1)
561    return 1;
562
563  return 1;
564}
565
566static enum filter_type
567bgp_input_filter (struct peer *peer, struct prefix *p, struct attr *attr,
568		  afi_t afi, safi_t safi)
569{
570  struct bgp_filter *filter;
571
572  filter = &peer->filter[afi][safi];
573
574#define FILTER_EXIST_WARN(F,f,filter) \
575  if (BGP_DEBUG (update, UPDATE_IN) \
576      && !(F ## _IN (filter))) \
577    plog_warn (peer->log, "%s: Could not find configured input %s-list %s!", \
578               peer->host, #f, F ## _IN_NAME(filter));
579
580  if (DISTRIBUTE_IN_NAME (filter)) {
581    FILTER_EXIST_WARN(DISTRIBUTE, distribute, filter);
582
583    if (access_list_apply (DISTRIBUTE_IN (filter), p) == FILTER_DENY)
584      return FILTER_DENY;
585  }
586
587  if (PREFIX_LIST_IN_NAME (filter)) {
588    FILTER_EXIST_WARN(PREFIX_LIST, prefix, filter);
589
590    if (prefix_list_apply (PREFIX_LIST_IN (filter), p) == PREFIX_DENY)
591      return FILTER_DENY;
592  }
593
594  if (FILTER_LIST_IN_NAME (filter)) {
595    FILTER_EXIST_WARN(FILTER_LIST, as, filter);
596
597    if (as_list_apply (FILTER_LIST_IN (filter), attr->aspath)== AS_FILTER_DENY)
598      return FILTER_DENY;
599  }
600
601  return FILTER_PERMIT;
602#undef FILTER_EXIST_WARN
603}
604
605static enum filter_type
606bgp_output_filter (struct peer *peer, struct prefix *p, struct attr *attr,
607		   afi_t afi, safi_t safi)
608{
609  struct bgp_filter *filter;
610
611  filter = &peer->filter[afi][safi];
612
613#define FILTER_EXIST_WARN(F,f,filter) \
614  if (BGP_DEBUG (update, UPDATE_OUT) \
615      && !(F ## _OUT (filter))) \
616    plog_warn (peer->log, "%s: Could not find configured output %s-list %s!", \
617               peer->host, #f, F ## _OUT_NAME(filter));
618
619  if (DISTRIBUTE_OUT_NAME (filter)) {
620    FILTER_EXIST_WARN(DISTRIBUTE, distribute, filter);
621
622    if (access_list_apply (DISTRIBUTE_OUT (filter), p) == FILTER_DENY)
623      return FILTER_DENY;
624  }
625
626  if (PREFIX_LIST_OUT_NAME (filter)) {
627    FILTER_EXIST_WARN(PREFIX_LIST, prefix, filter);
628
629    if (prefix_list_apply (PREFIX_LIST_OUT (filter), p) == PREFIX_DENY)
630      return FILTER_DENY;
631  }
632
633  if (FILTER_LIST_OUT_NAME (filter)) {
634    FILTER_EXIST_WARN(FILTER_LIST, as, filter);
635
636    if (as_list_apply (FILTER_LIST_OUT (filter), attr->aspath) == AS_FILTER_DENY)
637      return FILTER_DENY;
638  }
639
640  return FILTER_PERMIT;
641#undef FILTER_EXIST_WARN
642}
643
644/* If community attribute includes no_export then return 1. */
645static int
646bgp_community_filter (struct peer *peer, struct attr *attr)
647{
648  if (attr->community)
649    {
650      /* NO_ADVERTISE check. */
651      if (community_include (attr->community, COMMUNITY_NO_ADVERTISE))
652	return 1;
653
654      /* NO_EXPORT check. */
655      if (peer->sort == BGP_PEER_EBGP &&
656	  community_include (attr->community, COMMUNITY_NO_EXPORT))
657	return 1;
658
659      /* NO_EXPORT_SUBCONFED check. */
660      if (peer->sort == BGP_PEER_EBGP
661	  || peer->sort == BGP_PEER_CONFED)
662	if (community_include (attr->community, COMMUNITY_NO_EXPORT_SUBCONFED))
663	  return 1;
664    }
665  return 0;
666}
667
668/* Route reflection loop check.  */
669static int
670bgp_cluster_filter (struct peer *peer, struct attr *attr)
671{
672  struct in_addr cluster_id;
673
674  if (attr->extra && attr->extra->cluster)
675    {
676      if (peer->bgp->config & BGP_CONFIG_CLUSTER_ID)
677	cluster_id = peer->bgp->cluster_id;
678      else
679	cluster_id = peer->bgp->router_id;
680
681      if (cluster_loop_check (attr->extra->cluster, cluster_id))
682	return 1;
683    }
684  return 0;
685}
686
687static int
688bgp_input_modifier (struct peer *peer, struct prefix *p, struct attr *attr,
689		    afi_t afi, safi_t safi)
690{
691  struct bgp_filter *filter;
692  struct bgp_info info;
693  route_map_result_t ret;
694
695  filter = &peer->filter[afi][safi];
696
697  /* Apply default weight value. */
698  if (peer->weight)
699    (bgp_attr_extra_get (attr))->weight = peer->weight;
700
701  /* Route map apply. */
702  if (ROUTE_MAP_IN_NAME (filter))
703    {
704      /* Duplicate current value to new strucutre for modification. */
705      info.peer = peer;
706      info.attr = attr;
707
708      SET_FLAG (peer->rmap_type, PEER_RMAP_TYPE_IN);
709
710      /* Apply BGP route map to the attribute. */
711      ret = route_map_apply (ROUTE_MAP_IN (filter), p, RMAP_BGP, &info);
712
713      peer->rmap_type = 0;
714
715      if (ret == RMAP_DENYMATCH)
716	/* caller has multiple error paths with bgp_attr_flush() */
717	return RMAP_DENY;
718    }
719  return RMAP_PERMIT;
720}
721
722static int
723bgp_export_modifier (struct peer *rsclient, struct peer *peer,
724        struct prefix *p, struct attr *attr, afi_t afi, safi_t safi)
725{
726  struct bgp_filter *filter;
727  struct bgp_info info;
728  route_map_result_t ret;
729
730  filter = &peer->filter[afi][safi];
731
732  /* Route map apply. */
733  if (ROUTE_MAP_EXPORT_NAME (filter))
734    {
735      /* Duplicate current value to new strucutre for modification. */
736      info.peer = rsclient;
737      info.attr = attr;
738
739      SET_FLAG (rsclient->rmap_type, PEER_RMAP_TYPE_EXPORT);
740
741      /* Apply BGP route map to the attribute. */
742      ret = route_map_apply (ROUTE_MAP_EXPORT (filter), p, RMAP_BGP, &info);
743
744      rsclient->rmap_type = 0;
745
746      if (ret == RMAP_DENYMATCH)
747        {
748          /* Free newly generated AS path and community by route-map. */
749          bgp_attr_flush (attr);
750          return RMAP_DENY;
751        }
752    }
753  return RMAP_PERMIT;
754}
755
756static int
757bgp_import_modifier (struct peer *rsclient, struct peer *peer,
758        struct prefix *p, struct attr *attr, afi_t afi, safi_t safi)
759{
760  struct bgp_filter *filter;
761  struct bgp_info info;
762  route_map_result_t ret;
763
764  filter = &rsclient->filter[afi][safi];
765
766  /* Apply default weight value. */
767  if (peer->weight)
768    (bgp_attr_extra_get (attr))->weight = peer->weight;
769
770  /* Route map apply. */
771  if (ROUTE_MAP_IMPORT_NAME (filter))
772    {
773      /* Duplicate current value to new strucutre for modification. */
774      info.peer = peer;
775      info.attr = attr;
776
777      SET_FLAG (peer->rmap_type, PEER_RMAP_TYPE_IMPORT);
778
779      /* Apply BGP route map to the attribute. */
780      ret = route_map_apply (ROUTE_MAP_IMPORT (filter), p, RMAP_BGP, &info);
781
782      peer->rmap_type = 0;
783
784      if (ret == RMAP_DENYMATCH)
785        {
786          /* Free newly generated AS path and community by route-map. */
787          bgp_attr_flush (attr);
788          return RMAP_DENY;
789        }
790    }
791  return RMAP_PERMIT;
792}
793
794static int
795bgp_announce_check (struct bgp_info *ri, struct peer *peer, struct prefix *p,
796		    struct attr *attr, afi_t afi, safi_t safi)
797{
798  int ret;
799  char buf[SU_ADDRSTRLEN];
800  struct bgp_filter *filter;
801  struct peer *from;
802  struct bgp *bgp;
803  int transparent;
804  int reflect;
805  struct attr *riattr;
806
807  from = ri->peer;
808  filter = &peer->filter[afi][safi];
809  bgp = peer->bgp;
810  riattr = bgp_info_mpath_count (ri) ? bgp_info_mpath_attr (ri) : ri->attr;
811
812  if (DISABLE_BGP_ANNOUNCE)
813    return 0;
814
815  /* Do not send announces to RS-clients from the 'normal' bgp_table. */
816  if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT))
817    return 0;
818
819  /* Do not send back route to sender. */
820  if (from == peer)
821    return 0;
822
823  /* Aggregate-address suppress check. */
824  if (ri->extra && ri->extra->suppress)
825    if (! UNSUPPRESS_MAP_NAME (filter))
826      return 0;
827
828  /* Default route check.  */
829  if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_DEFAULT_ORIGINATE))
830    {
831      if (p->family == AF_INET && p->u.prefix4.s_addr == INADDR_ANY)
832	return 0;
833#ifdef HAVE_IPV6
834      else if (p->family == AF_INET6 && p->prefixlen == 0)
835	return 0;
836#endif /* HAVE_IPV6 */
837    }
838
839  /* Transparency check. */
840  if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT)
841      && CHECK_FLAG (from->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT))
842    transparent = 1;
843  else
844    transparent = 0;
845
846  /* If community is not disabled check the no-export and local. */
847  if (! transparent && bgp_community_filter (peer, riattr))
848    return 0;
849
850  /* If the attribute has originator-id and it is same as remote
851     peer's id. */
852  if (riattr->flag & ATTR_FLAG_BIT (BGP_ATTR_ORIGINATOR_ID))
853    {
854      if (IPV4_ADDR_SAME (&peer->remote_id, &riattr->extra->originator_id))
855	{
856	  if (BGP_DEBUG (filter, FILTER))
857	    zlog (peer->log, LOG_DEBUG,
858		  "%s [Update:SEND] %s/%d originator-id is same as remote router-id",
859		  peer->host,
860		  inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
861		  p->prefixlen);
862	  return 0;
863	}
864    }
865
866  /* ORF prefix-list filter check */
867  if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV)
868      && (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV)
869	  || CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_OLD_RCV)))
870    if (peer->orf_plist[afi][safi])
871      {
872	if (prefix_list_apply (peer->orf_plist[afi][safi], p) == PREFIX_DENY)
873          return 0;
874      }
875
876  /* Output filter check. */
877  if (bgp_output_filter (peer, p, riattr, afi, safi) == FILTER_DENY)
878    {
879      if (BGP_DEBUG (filter, FILTER))
880	zlog (peer->log, LOG_DEBUG,
881	      "%s [Update:SEND] %s/%d is filtered",
882	      peer->host,
883	      inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
884	      p->prefixlen);
885      return 0;
886    }
887
888#ifdef BGP_SEND_ASPATH_CHECK
889  /* AS path loop check. */
890  if (aspath_loop_check (riattr->aspath, peer->as))
891    {
892      if (BGP_DEBUG (filter, FILTER))
893        zlog (peer->log, LOG_DEBUG,
894	      "%s [Update:SEND] suppress announcement to peer AS %u is AS path.",
895	      peer->host, peer->as);
896      return 0;
897    }
898#endif /* BGP_SEND_ASPATH_CHECK */
899
900  /* If we're a CONFED we need to loop check the CONFED ID too */
901  if (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION))
902    {
903      if (aspath_loop_check(riattr->aspath, bgp->confed_id))
904	{
905	  if (BGP_DEBUG (filter, FILTER))
906	    zlog (peer->log, LOG_DEBUG,
907		  "%s [Update:SEND] suppress announcement to peer AS %u is AS path.",
908		  peer->host,
909		  bgp->confed_id);
910	  return 0;
911	}
912    }
913
914  /* Route-Reflect check. */
915  if (from->sort == BGP_PEER_IBGP && peer->sort == BGP_PEER_IBGP)
916    reflect = 1;
917  else
918    reflect = 0;
919
920  /* IBGP reflection check. */
921  if (reflect)
922    {
923      /* A route from a Client peer. */
924      if (CHECK_FLAG (from->af_flags[afi][safi], PEER_FLAG_REFLECTOR_CLIENT))
925	{
926	  /* Reflect to all the Non-Client peers and also to the
927             Client peers other than the originator.  Originator check
928             is already done.  So there is noting to do. */
929	  /* no bgp client-to-client reflection check. */
930	  if (bgp_flag_check (bgp, BGP_FLAG_NO_CLIENT_TO_CLIENT))
931	    if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_REFLECTOR_CLIENT))
932	      return 0;
933	}
934      else
935	{
936	  /* A route from a Non-client peer. Reflect to all other
937	     clients. */
938	  if (! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_REFLECTOR_CLIENT))
939	    return 0;
940	}
941    }
942
943  /* For modify attribute, copy it to temporary structure. */
944  bgp_attr_dup (attr, riattr);
945
946  /* If local-preference is not set. */
947  if ((peer->sort == BGP_PEER_IBGP
948       || peer->sort == BGP_PEER_CONFED)
949      && (! (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF))))
950    {
951      attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF);
952      attr->local_pref = bgp->default_local_pref;
953    }
954
955  /* If originator-id is not set and the route is to be reflected,
956     set the originator id */
957  if (peer && from && peer->sort == BGP_PEER_IBGP &&
958      from->sort == BGP_PEER_IBGP &&
959      (! (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))))
960    {
961      attr->extra = bgp_attr_extra_get(attr);
962      IPV4_ADDR_COPY(&(attr->extra->originator_id), &(from->remote_id));
963      SET_FLAG(attr->flag, BGP_ATTR_ORIGINATOR_ID);
964    }
965
966  /* Remove MED if its an EBGP peer - will get overwritten by route-maps */
967  if (peer->sort == BGP_PEER_EBGP
968      && attr->flag & ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC))
969    {
970      if (ri->peer != bgp->peer_self && ! transparent
971	  && ! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MED_UNCHANGED))
972	attr->flag &= ~(ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC));
973    }
974
975  /* next-hop-set */
976  if (transparent
977      || (reflect && ! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_NEXTHOP_SELF_ALL))
978      || (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_NEXTHOP_UNCHANGED)
979	  && ((p->family == AF_INET && attr->nexthop.s_addr)
980#ifdef HAVE_IPV6
981	      || (p->family == AF_INET6 &&
982                  ! IN6_IS_ADDR_UNSPECIFIED(&attr->extra->mp_nexthop_global))
983#endif /* HAVE_IPV6 */
984	      )))
985    {
986      /* NEXT-HOP Unchanged. */
987    }
988  else if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_NEXTHOP_SELF)
989	   || (p->family == AF_INET && attr->nexthop.s_addr == 0)
990#ifdef HAVE_IPV6
991	   || (p->family == AF_INET6 &&
992               IN6_IS_ADDR_UNSPECIFIED(&attr->extra->mp_nexthop_global))
993#endif /* HAVE_IPV6 */
994	   || (peer->sort == BGP_PEER_EBGP
995	       && bgp_multiaccess_check_v4 (attr->nexthop, peer->host) == 0))
996    {
997      /* Set IPv4 nexthop. */
998      if (p->family == AF_INET)
999	{
1000	  if (safi == SAFI_MPLS_VPN)
1001	    memcpy (&attr->extra->mp_nexthop_global_in, &peer->nexthop.v4,
1002	            IPV4_MAX_BYTELEN);
1003	  else
1004	    memcpy (&attr->nexthop, &peer->nexthop.v4, IPV4_MAX_BYTELEN);
1005	}
1006#ifdef HAVE_IPV6
1007      /* Set IPv6 nexthop. */
1008      if (p->family == AF_INET6)
1009	{
1010	  /* IPv6 global nexthop must be included. */
1011	  memcpy (&attr->extra->mp_nexthop_global, &peer->nexthop.v6_global,
1012		  IPV6_MAX_BYTELEN);
1013	  attr->extra->mp_nexthop_len = 16;
1014	}
1015#endif /* HAVE_IPV6 */
1016    }
1017
1018#ifdef HAVE_IPV6
1019  if (p->family == AF_INET6)
1020    {
1021      /* Left nexthop_local unchanged if so configured. */
1022      if ( CHECK_FLAG (peer->af_flags[afi][safi],
1023           PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED) )
1024        {
1025          if ( IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_local) )
1026            attr->extra->mp_nexthop_len=32;
1027          else
1028            attr->extra->mp_nexthop_len=16;
1029        }
1030
1031      /* Default nexthop_local treatment for non-RS-Clients */
1032      else
1033        {
1034      /* Link-local address should not be transit to different peer. */
1035      attr->extra->mp_nexthop_len = 16;
1036
1037      /* Set link-local address for shared network peer. */
1038      if (peer->shared_network
1039	  && ! IN6_IS_ADDR_UNSPECIFIED (&peer->nexthop.v6_local))
1040	{
1041	  memcpy (&attr->extra->mp_nexthop_local, &peer->nexthop.v6_local,
1042		  IPV6_MAX_BYTELEN);
1043	  attr->extra->mp_nexthop_len = 32;
1044	}
1045
1046      /* If bgpd act as BGP-4+ route-reflector, do not send link-local
1047	 address.*/
1048      if (reflect)
1049	attr->extra->mp_nexthop_len = 16;
1050
1051      /* If BGP-4+ link-local nexthop is not link-local nexthop. */
1052      if (! IN6_IS_ADDR_LINKLOCAL (&peer->nexthop.v6_local))
1053	attr->extra->mp_nexthop_len = 16;
1054    }
1055
1056    }
1057#endif /* HAVE_IPV6 */
1058
1059  /* If this is EBGP peer and remove-private-AS is set.  */
1060  if (peer->sort == BGP_PEER_EBGP
1061      && peer_af_flag_check (peer, afi, safi, PEER_FLAG_REMOVE_PRIVATE_AS)
1062      && aspath_private_as_check (attr->aspath))
1063    attr->aspath = aspath_empty_get ();
1064
1065  /* Route map & unsuppress-map apply. */
1066  if (ROUTE_MAP_OUT_NAME (filter)
1067      || (ri->extra && ri->extra->suppress) )
1068    {
1069      struct bgp_info info;
1070      struct attr dummy_attr;
1071      struct attr_extra dummy_extra;
1072
1073      dummy_attr.extra = &dummy_extra;
1074
1075      info.peer = peer;
1076      info.attr = attr;
1077
1078      /* The route reflector is not allowed to modify the attributes
1079	 of the reflected IBGP routes. */
1080      if (from->sort == BGP_PEER_IBGP
1081	  && peer->sort == BGP_PEER_IBGP)
1082	{
1083	  bgp_attr_dup (&dummy_attr, attr);
1084	  info.attr = &dummy_attr;
1085	}
1086
1087      SET_FLAG (peer->rmap_type, PEER_RMAP_TYPE_OUT);
1088
1089      if (ri->extra && ri->extra->suppress)
1090	ret = route_map_apply (UNSUPPRESS_MAP (filter), p, RMAP_BGP, &info);
1091      else
1092	ret = route_map_apply (ROUTE_MAP_OUT (filter), p, RMAP_BGP, &info);
1093
1094      peer->rmap_type = 0;
1095
1096      if (ret == RMAP_DENYMATCH)
1097	{
1098	  bgp_attr_flush (attr);
1099	  return 0;
1100	}
1101    }
1102  return 1;
1103}
1104
1105static int
1106bgp_announce_check_rsclient (struct bgp_info *ri, struct peer *rsclient,
1107        struct prefix *p, struct attr *attr, afi_t afi, safi_t safi)
1108{
1109  int ret;
1110  char buf[SU_ADDRSTRLEN];
1111  struct bgp_filter *filter;
1112  struct bgp_info info;
1113  struct peer *from;
1114  struct attr *riattr;
1115
1116  from = ri->peer;
1117  filter = &rsclient->filter[afi][safi];
1118  riattr = bgp_info_mpath_count (ri) ? bgp_info_mpath_attr (ri) : ri->attr;
1119
1120  if (DISABLE_BGP_ANNOUNCE)
1121    return 0;
1122
1123  /* Do not send back route to sender. */
1124  if (from == rsclient)
1125    return 0;
1126
1127  /* Aggregate-address suppress check. */
1128  if (ri->extra && ri->extra->suppress)
1129    if (! UNSUPPRESS_MAP_NAME (filter))
1130      return 0;
1131
1132  /* Default route check.  */
1133  if (CHECK_FLAG (rsclient->af_sflags[afi][safi],
1134          PEER_STATUS_DEFAULT_ORIGINATE))
1135    {
1136      if (p->family == AF_INET && p->u.prefix4.s_addr == INADDR_ANY)
1137        return 0;
1138#ifdef HAVE_IPV6
1139      else if (p->family == AF_INET6 && p->prefixlen == 0)
1140        return 0;
1141#endif /* HAVE_IPV6 */
1142    }
1143
1144  /* If the attribute has originator-id and it is same as remote
1145     peer's id. */
1146  if (riattr->flag & ATTR_FLAG_BIT (BGP_ATTR_ORIGINATOR_ID))
1147    {
1148      if (IPV4_ADDR_SAME (&rsclient->remote_id,
1149                          &riattr->extra->originator_id))
1150        {
1151         if (BGP_DEBUG (filter, FILTER))
1152           zlog (rsclient->log, LOG_DEBUG,
1153                 "%s [Update:SEND] %s/%d originator-id is same as remote router-id",
1154                 rsclient->host,
1155                 inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
1156                 p->prefixlen);
1157         return 0;
1158       }
1159    }
1160
1161  /* ORF prefix-list filter check */
1162  if (CHECK_FLAG (rsclient->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV)
1163      && (CHECK_FLAG (rsclient->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV)
1164         || CHECK_FLAG (rsclient->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_OLD_RCV)))
1165    if (rsclient->orf_plist[afi][safi])
1166      {
1167       if (prefix_list_apply (rsclient->orf_plist[afi][safi], p) == PREFIX_DENY)
1168          return 0;
1169      }
1170
1171  /* Output filter check. */
1172  if (bgp_output_filter (rsclient, p, riattr, afi, safi) == FILTER_DENY)
1173    {
1174      if (BGP_DEBUG (filter, FILTER))
1175       zlog (rsclient->log, LOG_DEBUG,
1176             "%s [Update:SEND] %s/%d is filtered",
1177             rsclient->host,
1178             inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
1179             p->prefixlen);
1180      return 0;
1181    }
1182
1183#ifdef BGP_SEND_ASPATH_CHECK
1184  /* AS path loop check. */
1185  if (aspath_loop_check (riattr->aspath, rsclient->as))
1186    {
1187      if (BGP_DEBUG (filter, FILTER))
1188        zlog (rsclient->log, LOG_DEBUG,
1189             "%s [Update:SEND] suppress announcement to peer AS %u is AS path.",
1190             rsclient->host, rsclient->as);
1191      return 0;
1192    }
1193#endif /* BGP_SEND_ASPATH_CHECK */
1194
1195  /* For modify attribute, copy it to temporary structure. */
1196  bgp_attr_dup (attr, riattr);
1197
1198  /* next-hop-set */
1199  if ((p->family == AF_INET && attr->nexthop.s_addr == 0)
1200#ifdef HAVE_IPV6
1201          || (p->family == AF_INET6 &&
1202              IN6_IS_ADDR_UNSPECIFIED(&attr->extra->mp_nexthop_global))
1203#endif /* HAVE_IPV6 */
1204     )
1205  {
1206    /* Set IPv4 nexthop. */
1207    if (p->family == AF_INET)
1208      {
1209        if (safi == SAFI_MPLS_VPN)
1210          memcpy (&attr->extra->mp_nexthop_global_in, &rsclient->nexthop.v4,
1211                  IPV4_MAX_BYTELEN);
1212        else
1213          memcpy (&attr->nexthop, &rsclient->nexthop.v4, IPV4_MAX_BYTELEN);
1214      }
1215#ifdef HAVE_IPV6
1216    /* Set IPv6 nexthop. */
1217    if (p->family == AF_INET6)
1218      {
1219        /* IPv6 global nexthop must be included. */
1220        memcpy (&attr->extra->mp_nexthop_global, &rsclient->nexthop.v6_global,
1221                IPV6_MAX_BYTELEN);
1222        attr->extra->mp_nexthop_len = 16;
1223      }
1224#endif /* HAVE_IPV6 */
1225  }
1226
1227#ifdef HAVE_IPV6
1228  if (p->family == AF_INET6)
1229    {
1230      struct attr_extra *attre = attr->extra;
1231
1232      /* Left nexthop_local unchanged if so configured. */
1233      if ( CHECK_FLAG (rsclient->af_flags[afi][safi],
1234           PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED) )
1235        {
1236          if ( IN6_IS_ADDR_LINKLOCAL (&attre->mp_nexthop_local) )
1237            attre->mp_nexthop_len=32;
1238          else
1239            attre->mp_nexthop_len=16;
1240        }
1241
1242      /* Default nexthop_local treatment for RS-Clients */
1243      else
1244        {
1245          /* Announcer and RS-Client are both in the same network */
1246          if (rsclient->shared_network && from->shared_network &&
1247              (rsclient->ifindex == from->ifindex))
1248            {
1249              if ( IN6_IS_ADDR_LINKLOCAL (&attre->mp_nexthop_local) )
1250                attre->mp_nexthop_len=32;
1251              else
1252                attre->mp_nexthop_len=16;
1253            }
1254
1255          /* Set link-local address for shared network peer. */
1256          else if (rsclient->shared_network
1257              && IN6_IS_ADDR_LINKLOCAL (&rsclient->nexthop.v6_local))
1258            {
1259              memcpy (&attre->mp_nexthop_local, &rsclient->nexthop.v6_local,
1260                      IPV6_MAX_BYTELEN);
1261              attre->mp_nexthop_len = 32;
1262            }
1263
1264          else
1265            attre->mp_nexthop_len = 16;
1266        }
1267
1268    }
1269#endif /* HAVE_IPV6 */
1270
1271
1272  /* If this is EBGP peer and remove-private-AS is set.  */
1273  if (rsclient->sort == BGP_PEER_EBGP
1274      && peer_af_flag_check (rsclient, afi, safi, PEER_FLAG_REMOVE_PRIVATE_AS)
1275      && aspath_private_as_check (attr->aspath))
1276    attr->aspath = aspath_empty_get ();
1277
1278  /* Route map & unsuppress-map apply. */
1279  if (ROUTE_MAP_OUT_NAME (filter) || (ri->extra && ri->extra->suppress) )
1280    {
1281      info.peer = rsclient;
1282      info.attr = attr;
1283
1284      SET_FLAG (rsclient->rmap_type, PEER_RMAP_TYPE_OUT);
1285
1286      if (ri->extra && ri->extra->suppress)
1287        ret = route_map_apply (UNSUPPRESS_MAP (filter), p, RMAP_BGP, &info);
1288      else
1289        ret = route_map_apply (ROUTE_MAP_OUT (filter), p, RMAP_BGP, &info);
1290
1291      rsclient->rmap_type = 0;
1292
1293      if (ret == RMAP_DENYMATCH)
1294       {
1295         bgp_attr_flush (attr);
1296         return 0;
1297       }
1298    }
1299
1300  return 1;
1301}
1302
1303struct bgp_info_pair
1304{
1305  struct bgp_info *old;
1306  struct bgp_info *new;
1307};
1308
1309static void
1310bgp_best_selection (struct bgp *bgp, struct bgp_node *rn,
1311		    struct bgp_maxpaths_cfg *mpath_cfg,
1312		    struct bgp_info_pair *result)
1313{
1314  struct bgp_info *new_select;
1315  struct bgp_info *old_select;
1316  struct bgp_info *ri;
1317  struct bgp_info *ri1;
1318  struct bgp_info *ri2;
1319  struct bgp_info *nextri = NULL;
1320  int paths_eq, do_mpath;
1321  struct list mp_list;
1322
1323  bgp_mp_list_init (&mp_list);
1324  do_mpath = (mpath_cfg->maxpaths_ebgp != BGP_DEFAULT_MAXPATHS ||
1325	      mpath_cfg->maxpaths_ibgp != BGP_DEFAULT_MAXPATHS);
1326
1327  /* bgp deterministic-med */
1328  new_select = NULL;
1329  if (bgp_flag_check (bgp, BGP_FLAG_DETERMINISTIC_MED))
1330    for (ri1 = rn->info; ri1; ri1 = ri1->next)
1331      {
1332	if (CHECK_FLAG (ri1->flags, BGP_INFO_DMED_CHECK))
1333	  continue;
1334	if (BGP_INFO_HOLDDOWN (ri1))
1335	  continue;
1336
1337	new_select = ri1;
1338	if (do_mpath)
1339	  bgp_mp_list_add (&mp_list, ri1);
1340	old_select = CHECK_FLAG (ri1->flags, BGP_INFO_SELECTED) ? ri1 : NULL;
1341	if (ri1->next)
1342	  for (ri2 = ri1->next; ri2; ri2 = ri2->next)
1343	    {
1344	      if (CHECK_FLAG (ri2->flags, BGP_INFO_DMED_CHECK))
1345		continue;
1346	      if (BGP_INFO_HOLDDOWN (ri2))
1347		continue;
1348
1349	      if (aspath_cmp_left (ri1->attr->aspath, ri2->attr->aspath)
1350		  || aspath_cmp_left_confed (ri1->attr->aspath,
1351					     ri2->attr->aspath))
1352		{
1353		  if (CHECK_FLAG (ri2->flags, BGP_INFO_SELECTED))
1354		    old_select = ri2;
1355		  if (bgp_info_cmp (bgp, ri2, new_select, &paths_eq))
1356		    {
1357		      bgp_info_unset_flag (rn, new_select, BGP_INFO_DMED_SELECTED);
1358		      new_select = ri2;
1359		      if (do_mpath && !paths_eq)
1360			{
1361			  bgp_mp_list_clear (&mp_list);
1362			  bgp_mp_list_add (&mp_list, ri2);
1363			}
1364		    }
1365
1366		  if (do_mpath && paths_eq)
1367		    bgp_mp_list_add (&mp_list, ri2);
1368
1369		  bgp_info_set_flag (rn, ri2, BGP_INFO_DMED_CHECK);
1370		}
1371	    }
1372	bgp_info_set_flag (rn, new_select, BGP_INFO_DMED_CHECK);
1373	bgp_info_set_flag (rn, new_select, BGP_INFO_DMED_SELECTED);
1374
1375	bgp_info_mpath_update (rn, new_select, old_select, &mp_list, mpath_cfg);
1376	bgp_mp_list_clear (&mp_list);
1377      }
1378
1379  /* Check old selected route and new selected route. */
1380  old_select = NULL;
1381  new_select = NULL;
1382  for (ri = rn->info; (ri != NULL) && (nextri = ri->next, 1); ri = nextri)
1383    {
1384      if (CHECK_FLAG (ri->flags, BGP_INFO_SELECTED))
1385	old_select = ri;
1386
1387      if (BGP_INFO_HOLDDOWN (ri))
1388        {
1389          /* reap REMOVED routes, if needs be
1390           * selected route must stay for a while longer though
1391           */
1392          if (CHECK_FLAG (ri->flags, BGP_INFO_REMOVED)
1393              && (ri != old_select))
1394              bgp_info_reap (rn, ri);
1395
1396          continue;
1397        }
1398
1399      if (bgp_flag_check (bgp, BGP_FLAG_DETERMINISTIC_MED)
1400          && (! CHECK_FLAG (ri->flags, BGP_INFO_DMED_SELECTED)))
1401	{
1402	  bgp_info_unset_flag (rn, ri, BGP_INFO_DMED_CHECK);
1403	  continue;
1404        }
1405      bgp_info_unset_flag (rn, ri, BGP_INFO_DMED_CHECK);
1406      bgp_info_unset_flag (rn, ri, BGP_INFO_DMED_SELECTED);
1407
1408      if (bgp_info_cmp (bgp, ri, new_select, &paths_eq))
1409	{
1410	  if (do_mpath && bgp_flag_check (bgp, BGP_FLAG_DETERMINISTIC_MED))
1411	    bgp_mp_dmed_deselect (new_select);
1412
1413	  new_select = ri;
1414
1415	  if (do_mpath && !paths_eq)
1416	    {
1417	      bgp_mp_list_clear (&mp_list);
1418	      bgp_mp_list_add (&mp_list, ri);
1419	    }
1420	}
1421      else if (do_mpath && bgp_flag_check (bgp, BGP_FLAG_DETERMINISTIC_MED))
1422	bgp_mp_dmed_deselect (ri);
1423
1424      if (do_mpath && paths_eq)
1425	bgp_mp_list_add (&mp_list, ri);
1426    }
1427
1428
1429  if (!bgp_flag_check (bgp, BGP_FLAG_DETERMINISTIC_MED))
1430    bgp_info_mpath_update (rn, new_select, old_select, &mp_list, mpath_cfg);
1431
1432  bgp_info_mpath_aggregate_update (new_select, old_select);
1433  bgp_mp_list_clear (&mp_list);
1434
1435  result->old = old_select;
1436  result->new = new_select;
1437
1438  return;
1439}
1440
1441static int
1442bgp_process_announce_selected (struct peer *peer, struct bgp_info *selected,
1443                               struct bgp_node *rn, afi_t afi, safi_t safi)
1444{
1445  struct prefix *p;
1446  struct attr attr;
1447  struct attr_extra extra;
1448
1449  p = &rn->p;
1450
1451  /* Announce route to Established peer. */
1452  if (peer->status != Established)
1453    return 0;
1454
1455  /* Address family configuration check. */
1456  if (! peer->afc_nego[afi][safi])
1457    return 0;
1458
1459  /* First update is deferred until ORF or ROUTE-REFRESH is received */
1460  if (CHECK_FLAG (peer->af_sflags[afi][safi],
1461      PEER_STATUS_ORF_WAIT_REFRESH))
1462    return 0;
1463
1464  /* It's initialized in bgp_announce_[check|check_rsclient]() */
1465  attr.extra = &extra;
1466
1467  switch (bgp_node_table (rn)->type)
1468    {
1469      case BGP_TABLE_MAIN:
1470      /* Announcement to peer->conf.  If the route is filtered,
1471         withdraw it. */
1472        if (selected && bgp_announce_check (selected, peer, p, &attr, afi, safi))
1473          bgp_adj_out_set (rn, peer, p, &attr, afi, safi, selected);
1474        else
1475          bgp_adj_out_unset (rn, peer, p, afi, safi);
1476        break;
1477      case BGP_TABLE_RSCLIENT:
1478        /* Announcement to peer->conf.  If the route is filtered,
1479           withdraw it. */
1480        if (selected &&
1481            bgp_announce_check_rsclient (selected, peer, p, &attr, afi, safi))
1482          bgp_adj_out_set (rn, peer, p, &attr, afi, safi, selected);
1483        else
1484	  bgp_adj_out_unset (rn, peer, p, afi, safi);
1485        break;
1486    }
1487
1488  return 0;
1489}
1490
1491struct bgp_process_queue
1492{
1493  struct bgp *bgp;
1494  struct bgp_node *rn;
1495  afi_t afi;
1496  safi_t safi;
1497};
1498
1499static wq_item_status
1500bgp_process_rsclient (struct work_queue *wq, void *data)
1501{
1502  struct bgp_process_queue *pq = data;
1503  struct bgp *bgp = pq->bgp;
1504  struct bgp_node *rn = pq->rn;
1505  afi_t afi = pq->afi;
1506  safi_t safi = pq->safi;
1507  struct bgp_info *new_select;
1508  struct bgp_info *old_select;
1509  struct bgp_info_pair old_and_new;
1510  struct listnode *node, *nnode;
1511  struct peer *rsclient = bgp_node_table (rn)->owner;
1512
1513  /* Best path selection. */
1514  bgp_best_selection (bgp, rn, &bgp->maxpaths[afi][safi], &old_and_new);
1515  new_select = old_and_new.new;
1516  old_select = old_and_new.old;
1517
1518  if (CHECK_FLAG (rsclient->sflags, PEER_STATUS_GROUP))
1519    {
1520      if (rsclient->group)
1521        for (ALL_LIST_ELEMENTS (rsclient->group->peer, node, nnode, rsclient))
1522          {
1523            /* Nothing to do. */
1524            if (old_select && old_select == new_select)
1525              if (!CHECK_FLAG (old_select->flags, BGP_INFO_ATTR_CHANGED))
1526                continue;
1527
1528            if (old_select)
1529              bgp_info_unset_flag (rn, old_select, BGP_INFO_SELECTED);
1530            if (new_select)
1531              {
1532                bgp_info_set_flag (rn, new_select, BGP_INFO_SELECTED);
1533                bgp_info_unset_flag (rn, new_select, BGP_INFO_ATTR_CHANGED);
1534		UNSET_FLAG (new_select->flags, BGP_INFO_MULTIPATH_CHG);
1535             }
1536
1537            bgp_process_announce_selected (rsclient, new_select, rn,
1538                                           afi, safi);
1539          }
1540    }
1541  else
1542    {
1543      if (old_select)
1544	bgp_info_unset_flag (rn, old_select, BGP_INFO_SELECTED);
1545      if (new_select)
1546	{
1547	  bgp_info_set_flag (rn, new_select, BGP_INFO_SELECTED);
1548	  bgp_info_unset_flag (rn, new_select, BGP_INFO_ATTR_CHANGED);
1549	  UNSET_FLAG (new_select->flags, BGP_INFO_MULTIPATH_CHG);
1550	}
1551      bgp_process_announce_selected (rsclient, new_select, rn, afi, safi);
1552    }
1553
1554  if (old_select && CHECK_FLAG (old_select->flags, BGP_INFO_REMOVED))
1555    bgp_info_reap (rn, old_select);
1556
1557  UNSET_FLAG (rn->flags, BGP_NODE_PROCESS_SCHEDULED);
1558  return WQ_SUCCESS;
1559}
1560
1561static wq_item_status
1562bgp_process_main (struct work_queue *wq, void *data)
1563{
1564  struct bgp_process_queue *pq = data;
1565  struct bgp *bgp = pq->bgp;
1566  struct bgp_node *rn = pq->rn;
1567  afi_t afi = pq->afi;
1568  safi_t safi = pq->safi;
1569  struct prefix *p = &rn->p;
1570  struct bgp_info *new_select;
1571  struct bgp_info *old_select;
1572  struct bgp_info_pair old_and_new;
1573  struct listnode *node, *nnode;
1574  struct peer *peer;
1575
1576  /* Best path selection. */
1577  bgp_best_selection (bgp, rn, &bgp->maxpaths[afi][safi], &old_and_new);
1578  old_select = old_and_new.old;
1579  new_select = old_and_new.new;
1580
1581  /* Nothing to do. */
1582  if (old_select && old_select == new_select)
1583    {
1584      if (! CHECK_FLAG (old_select->flags, BGP_INFO_ATTR_CHANGED))
1585        {
1586          if (CHECK_FLAG (old_select->flags, BGP_INFO_IGP_CHANGED) ||
1587	      CHECK_FLAG (old_select->flags, BGP_INFO_MULTIPATH_CHG))
1588            bgp_zebra_announce (p, old_select, bgp, safi);
1589
1590	  UNSET_FLAG (old_select->flags, BGP_INFO_MULTIPATH_CHG);
1591          UNSET_FLAG (rn->flags, BGP_NODE_PROCESS_SCHEDULED);
1592          return WQ_SUCCESS;
1593        }
1594    }
1595
1596  if (old_select)
1597    bgp_info_unset_flag (rn, old_select, BGP_INFO_SELECTED);
1598  if (new_select)
1599    {
1600      bgp_info_set_flag (rn, new_select, BGP_INFO_SELECTED);
1601      bgp_info_unset_flag (rn, new_select, BGP_INFO_ATTR_CHANGED);
1602      UNSET_FLAG (new_select->flags, BGP_INFO_MULTIPATH_CHG);
1603    }
1604
1605
1606  /* Check each BGP peer. */
1607  for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
1608    {
1609      bgp_process_announce_selected (peer, new_select, rn, afi, safi);
1610    }
1611
1612  /* FIB update. */
1613  if ((safi == SAFI_UNICAST || safi == SAFI_MULTICAST) && (! bgp->name &&
1614      ! bgp_option_check (BGP_OPT_NO_FIB)))
1615    {
1616      if (new_select
1617	  && new_select->type == ZEBRA_ROUTE_BGP
1618	  && new_select->sub_type == BGP_ROUTE_NORMAL)
1619	bgp_zebra_announce (p, new_select, bgp, safi);
1620      else
1621	{
1622	  /* Withdraw the route from the kernel. */
1623	  if (old_select
1624	      && old_select->type == ZEBRA_ROUTE_BGP
1625	      && old_select->sub_type == BGP_ROUTE_NORMAL)
1626	    bgp_zebra_withdraw (p, old_select, safi);
1627	}
1628    }
1629
1630  /* Reap old select bgp_info, it it has been removed */
1631  if (old_select && CHECK_FLAG (old_select->flags, BGP_INFO_REMOVED))
1632    bgp_info_reap (rn, old_select);
1633
1634  UNSET_FLAG (rn->flags, BGP_NODE_PROCESS_SCHEDULED);
1635  return WQ_SUCCESS;
1636}
1637
1638static void
1639bgp_processq_del (struct work_queue *wq, void *data)
1640{
1641  struct bgp_process_queue *pq = data;
1642  struct bgp_table *table = bgp_node_table (pq->rn);
1643
1644  bgp_unlock (pq->bgp);
1645  bgp_unlock_node (pq->rn);
1646  bgp_table_unlock (table);
1647  XFREE (MTYPE_BGP_PROCESS_QUEUE, pq);
1648}
1649
1650static void
1651bgp_process_queue_init (void)
1652{
1653  bm->process_main_queue
1654    = work_queue_new (bm->master, "process_main_queue");
1655  bm->process_rsclient_queue
1656    = work_queue_new (bm->master, "process_rsclient_queue");
1657
1658  if ( !(bm->process_main_queue && bm->process_rsclient_queue) )
1659    {
1660      zlog_err ("%s: Failed to allocate work queue", __func__);
1661      exit (1);
1662    }
1663
1664  bm->process_main_queue->spec.workfunc = &bgp_process_main;
1665  bm->process_main_queue->spec.del_item_data = &bgp_processq_del;
1666  bm->process_main_queue->spec.max_retries = 0;
1667  bm->process_main_queue->spec.hold = 50;
1668
1669  memcpy (bm->process_rsclient_queue, bm->process_main_queue,
1670          sizeof (struct work_queue *));
1671  bm->process_rsclient_queue->spec.workfunc = &bgp_process_rsclient;
1672}
1673
1674void
1675bgp_process (struct bgp *bgp, struct bgp_node *rn, afi_t afi, safi_t safi)
1676{
1677  struct bgp_process_queue *pqnode;
1678
1679  /* already scheduled for processing? */
1680  if (CHECK_FLAG (rn->flags, BGP_NODE_PROCESS_SCHEDULED))
1681    return;
1682
1683  if ( (bm->process_main_queue == NULL) ||
1684       (bm->process_rsclient_queue == NULL) )
1685    bgp_process_queue_init ();
1686
1687  pqnode = XCALLOC (MTYPE_BGP_PROCESS_QUEUE,
1688                    sizeof (struct bgp_process_queue));
1689  if (!pqnode)
1690    return;
1691
1692  /* all unlocked in bgp_processq_del */
1693  bgp_table_lock (bgp_node_table (rn));
1694  pqnode->rn = bgp_lock_node (rn);
1695  pqnode->bgp = bgp;
1696  bgp_lock (bgp);
1697  pqnode->afi = afi;
1698  pqnode->safi = safi;
1699
1700  switch (bgp_node_table (rn)->type)
1701    {
1702      case BGP_TABLE_MAIN:
1703        work_queue_add (bm->process_main_queue, pqnode);
1704        break;
1705      case BGP_TABLE_RSCLIENT:
1706        work_queue_add (bm->process_rsclient_queue, pqnode);
1707        break;
1708    }
1709
1710  SET_FLAG (rn->flags, BGP_NODE_PROCESS_SCHEDULED);
1711  return;
1712}
1713
1714static int
1715bgp_maximum_prefix_restart_timer (struct thread *thread)
1716{
1717  struct peer *peer;
1718
1719  peer = THREAD_ARG (thread);
1720  peer->t_pmax_restart = NULL;
1721
1722  if (BGP_DEBUG (events, EVENTS))
1723    zlog_debug ("%s Maximum-prefix restart timer expired, restore peering",
1724		peer->host);
1725
1726  peer_clear (peer);
1727
1728  return 0;
1729}
1730
1731int
1732bgp_maximum_prefix_overflow (struct peer *peer, afi_t afi,
1733                             safi_t safi, int always)
1734{
1735  if (!CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX))
1736    return 0;
1737
1738  if (peer->pcount[afi][safi] > peer->pmax[afi][safi])
1739    {
1740      if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_PREFIX_LIMIT)
1741         && ! always)
1742       return 0;
1743
1744      zlog (peer->log, LOG_INFO,
1745	    "%%MAXPFXEXCEED: No. of %s prefix received from %s %ld exceed, "
1746	    "limit %ld", afi_safi_print (afi, safi), peer->host,
1747	    peer->pcount[afi][safi], peer->pmax[afi][safi]);
1748      SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_PREFIX_LIMIT);
1749
1750      if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING))
1751       return 0;
1752
1753      {
1754       u_int8_t ndata[7];
1755
1756       if (safi == SAFI_MPLS_VPN)
1757         safi = SAFI_MPLS_LABELED_VPN;
1758
1759       ndata[0] = (afi >>  8);
1760       ndata[1] = afi;
1761       ndata[2] = safi;
1762       ndata[3] = (peer->pmax[afi][safi] >> 24);
1763       ndata[4] = (peer->pmax[afi][safi] >> 16);
1764       ndata[5] = (peer->pmax[afi][safi] >> 8);
1765       ndata[6] = (peer->pmax[afi][safi]);
1766
1767       SET_FLAG (peer->sflags, PEER_STATUS_PREFIX_OVERFLOW);
1768       bgp_notify_send_with_data (peer, BGP_NOTIFY_CEASE,
1769                                  BGP_NOTIFY_CEASE_MAX_PREFIX, ndata, 7);
1770      }
1771
1772      /* restart timer start */
1773      if (peer->pmax_restart[afi][safi])
1774	{
1775	  peer->v_pmax_restart = peer->pmax_restart[afi][safi] * 60;
1776
1777	  if (BGP_DEBUG (events, EVENTS))
1778	    zlog_debug ("%s Maximum-prefix restart timer started for %d secs",
1779			peer->host, peer->v_pmax_restart);
1780
1781	  BGP_TIMER_ON (peer->t_pmax_restart, bgp_maximum_prefix_restart_timer,
1782			peer->v_pmax_restart);
1783	}
1784
1785      return 1;
1786    }
1787  else
1788    UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_PREFIX_LIMIT);
1789
1790  if (peer->pcount[afi][safi] > (peer->pmax[afi][safi] * peer->pmax_threshold[afi][safi] / 100))
1791    {
1792      if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_PREFIX_THRESHOLD)
1793         && ! always)
1794       return 0;
1795
1796      zlog (peer->log, LOG_INFO,
1797	    "%%MAXPFX: No. of %s prefix received from %s reaches %ld, max %ld",
1798	    afi_safi_print (afi, safi), peer->host, peer->pcount[afi][safi],
1799	    peer->pmax[afi][safi]);
1800      SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_PREFIX_THRESHOLD);
1801    }
1802  else
1803    UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_PREFIX_THRESHOLD);
1804  return 0;
1805}
1806
1807/* Unconditionally remove the route from the RIB, without taking
1808 * damping into consideration (eg, because the session went down)
1809 */
1810static void
1811bgp_rib_remove (struct bgp_node *rn, struct bgp_info *ri, struct peer *peer,
1812		afi_t afi, safi_t safi)
1813{
1814  bgp_aggregate_decrement (peer->bgp, &rn->p, ri, afi, safi);
1815
1816  if (!CHECK_FLAG (ri->flags, BGP_INFO_HISTORY))
1817    bgp_info_delete (rn, ri); /* keep historical info */
1818
1819  bgp_process (peer->bgp, rn, afi, safi);
1820}
1821
1822static void
1823bgp_rib_withdraw (struct bgp_node *rn, struct bgp_info *ri, struct peer *peer,
1824		  afi_t afi, safi_t safi)
1825{
1826  int status = BGP_DAMP_NONE;
1827
1828  /* apply dampening, if result is suppressed, we'll be retaining
1829   * the bgp_info in the RIB for historical reference.
1830   */
1831  if (CHECK_FLAG (peer->bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING)
1832      && peer->sort == BGP_PEER_EBGP)
1833    if ( (status = bgp_damp_withdraw (ri, rn, afi, safi, 0))
1834         == BGP_DAMP_SUPPRESSED)
1835      {
1836        bgp_aggregate_decrement (peer->bgp, &rn->p, ri, afi, safi);
1837        return;
1838      }
1839
1840  bgp_rib_remove (rn, ri, peer, afi, safi);
1841}
1842
1843static void
1844bgp_update_rsclient (struct peer *rsclient, afi_t afi, safi_t safi,
1845      struct attr *attr, struct peer *peer, struct prefix *p, int type,
1846      int sub_type, struct prefix_rd *prd, u_char *tag)
1847{
1848  struct bgp_node *rn;
1849  struct bgp *bgp;
1850  struct attr new_attr;
1851  struct attr_extra new_extra;
1852  struct attr *attr_new;
1853  struct attr *attr_new2;
1854  struct bgp_info *ri;
1855  struct bgp_info *new;
1856  const char *reason;
1857  char buf[SU_ADDRSTRLEN];
1858
1859  /* Do not insert announces from a rsclient into its own 'bgp_table'. */
1860  if (peer == rsclient)
1861    return;
1862
1863  bgp = peer->bgp;
1864  rn = bgp_afi_node_get (rsclient->rib[afi][safi], afi, safi, p, prd);
1865
1866  /* Check previously received route. */
1867  for (ri = rn->info; ri; ri = ri->next)
1868    if (ri->peer == peer && ri->type == type && ri->sub_type == sub_type)
1869      break;
1870
1871  /* AS path loop check. */
1872  if (aspath_loop_check (attr->aspath, rsclient->as) > rsclient->allowas_in[afi][safi])
1873    {
1874      reason = "as-path contains our own AS;";
1875      goto filtered;
1876    }
1877
1878  /* Route reflector originator ID check.  */
1879  if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_ORIGINATOR_ID)
1880      && IPV4_ADDR_SAME (&rsclient->remote_id, &attr->extra->originator_id))
1881    {
1882      reason = "originator is us;";
1883      goto filtered;
1884    }
1885
1886  new_attr.extra = &new_extra;
1887  bgp_attr_dup (&new_attr, attr);
1888
1889  /* Apply export policy. */
1890  if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT) &&
1891        bgp_export_modifier (rsclient, peer, p, &new_attr, afi, safi) == RMAP_DENY)
1892    {
1893      reason = "export-policy;";
1894      goto filtered;
1895    }
1896
1897  attr_new2 = bgp_attr_intern (&new_attr);
1898
1899  /* Apply import policy. */
1900  if (bgp_import_modifier (rsclient, peer, p, &new_attr, afi, safi) == RMAP_DENY)
1901    {
1902      bgp_attr_unintern (&attr_new2);
1903
1904      reason = "import-policy;";
1905      goto filtered;
1906    }
1907
1908  attr_new = bgp_attr_intern (&new_attr);
1909  bgp_attr_unintern (&attr_new2);
1910
1911  /* IPv4 unicast next hop check.  */
1912  if ((afi == AFI_IP) && ((safi == SAFI_UNICAST) || safi == SAFI_MULTICAST))
1913    {
1914     /* Next hop must not be 0.0.0.0 nor Class D/E address. */
1915      if (new_attr.nexthop.s_addr == 0
1916         || IPV4_CLASS_DE (ntohl (new_attr.nexthop.s_addr)))
1917       {
1918         bgp_attr_unintern (&attr_new);
1919
1920         reason = "martian next-hop;";
1921         goto filtered;
1922       }
1923    }
1924
1925  /* If the update is implicit withdraw. */
1926  if (ri)
1927    {
1928      ri->uptime = bgp_clock ();
1929
1930      /* Same attribute comes in. */
1931      if (!CHECK_FLAG(ri->flags, BGP_INFO_REMOVED)
1932          && attrhash_cmp (ri->attr, attr_new))
1933        {
1934
1935          bgp_info_unset_flag (rn, ri, BGP_INFO_ATTR_CHANGED);
1936
1937          if (BGP_DEBUG (update, UPDATE_IN))
1938            zlog (peer->log, LOG_DEBUG,
1939                    "%s rcvd %s/%d for RS-client %s...duplicate ignored",
1940                    peer->host,
1941                    inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
1942                    p->prefixlen, rsclient->host);
1943
1944          bgp_unlock_node (rn);
1945          bgp_attr_unintern (&attr_new);
1946
1947          return;
1948        }
1949
1950      /* Withdraw/Announce before we fully processed the withdraw */
1951      if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
1952        bgp_info_restore (rn, ri);
1953
1954      /* Received Logging. */
1955      if (BGP_DEBUG (update, UPDATE_IN))
1956        zlog (peer->log, LOG_DEBUG, "%s rcvd %s/%d for RS-client %s",
1957                peer->host,
1958                inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
1959                p->prefixlen, rsclient->host);
1960
1961      /* The attribute is changed. */
1962      bgp_info_set_flag (rn, ri, BGP_INFO_ATTR_CHANGED);
1963
1964      /* Update to new attribute.  */
1965      bgp_attr_unintern (&ri->attr);
1966      ri->attr = attr_new;
1967
1968      /* Update MPLS tag.  */
1969      if (safi == SAFI_MPLS_VPN)
1970        memcpy ((bgp_info_extra_get (ri))->tag, tag, 3);
1971
1972      bgp_info_set_flag (rn, ri, BGP_INFO_VALID);
1973
1974      /* Process change. */
1975      bgp_process (bgp, rn, afi, safi);
1976      bgp_unlock_node (rn);
1977
1978      return;
1979    }
1980
1981  /* Received Logging. */
1982  if (BGP_DEBUG (update, UPDATE_IN))
1983    {
1984      zlog (peer->log, LOG_DEBUG, "%s rcvd %s/%d for RS-client %s",
1985              peer->host,
1986              inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
1987              p->prefixlen, rsclient->host);
1988    }
1989
1990  /* Make new BGP info. */
1991  new = bgp_info_new ();
1992  new->type = type;
1993  new->sub_type = sub_type;
1994  new->peer = peer;
1995  new->attr = attr_new;
1996  new->uptime = bgp_clock ();
1997
1998  /* Update MPLS tag. */
1999  if (safi == SAFI_MPLS_VPN)
2000    memcpy ((bgp_info_extra_get (new))->tag, tag, 3);
2001
2002  bgp_info_set_flag (rn, new, BGP_INFO_VALID);
2003
2004  /* Register new BGP information. */
2005  bgp_info_add (rn, new);
2006
2007  /* route_node_get lock */
2008  bgp_unlock_node (rn);
2009
2010  /* Process change. */
2011  bgp_process (bgp, rn, afi, safi);
2012
2013  return;
2014
2015 filtered:
2016
2017  /* This BGP update is filtered.  Log the reason then update BGP entry.  */
2018  if (BGP_DEBUG (update, UPDATE_IN))
2019        zlog (peer->log, LOG_DEBUG,
2020        "%s rcvd UPDATE about %s/%d -- DENIED for RS-client %s due to: %s",
2021        peer->host,
2022        inet_ntop (p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
2023        p->prefixlen, rsclient->host, reason);
2024
2025  if (ri)
2026    bgp_rib_remove (rn, ri, peer, afi, safi);
2027
2028  bgp_unlock_node (rn);
2029
2030  return;
2031}
2032
2033static void
2034bgp_withdraw_rsclient (struct peer *rsclient, afi_t afi, safi_t safi,
2035      struct peer *peer, struct prefix *p, int type, int sub_type,
2036      struct prefix_rd *prd, u_char *tag)
2037{
2038  struct bgp_node *rn;
2039  struct bgp_info *ri;
2040  char buf[SU_ADDRSTRLEN];
2041
2042  if (rsclient == peer)
2043    return;
2044
2045  rn = bgp_afi_node_get (rsclient->rib[afi][safi], afi, safi, p, prd);
2046
2047  /* Lookup withdrawn route. */
2048  for (ri = rn->info; ri; ri = ri->next)
2049    if (ri->peer == peer && ri->type == type && ri->sub_type == sub_type)
2050      break;
2051
2052  /* Withdraw specified route from routing table. */
2053  if (ri && ! CHECK_FLAG (ri->flags, BGP_INFO_HISTORY))
2054    bgp_rib_withdraw (rn, ri, peer, afi, safi);
2055  else if (BGP_DEBUG (update, UPDATE_IN))
2056    zlog (peer->log, LOG_DEBUG,
2057          "%s Can't find the route %s/%d", peer->host,
2058          inet_ntop (p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
2059          p->prefixlen);
2060
2061  /* Unlock bgp_node_get() lock. */
2062  bgp_unlock_node (rn);
2063}
2064
2065static int
2066bgp_update_main (struct peer *peer, struct prefix *p, struct attr *attr,
2067	    afi_t afi, safi_t safi, int type, int sub_type,
2068	    struct prefix_rd *prd, u_char *tag, int soft_reconfig)
2069{
2070  int ret;
2071  int aspath_loop_count = 0;
2072  struct bgp_node *rn;
2073  struct bgp *bgp;
2074  struct attr new_attr;
2075  struct attr_extra new_extra;
2076  struct attr *attr_new;
2077  struct bgp_info *ri;
2078  struct bgp_info *new;
2079  const char *reason;
2080  char buf[SU_ADDRSTRLEN];
2081
2082  bgp = peer->bgp;
2083  rn = bgp_afi_node_get (bgp->rib[afi][safi], afi, safi, p, prd);
2084
2085  /* When peer's soft reconfiguration enabled.  Record input packet in
2086     Adj-RIBs-In.  */
2087  if (! soft_reconfig && CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SOFT_RECONFIG)
2088      && peer != bgp->peer_self)
2089    bgp_adj_in_set (rn, peer, attr);
2090
2091  /* Check previously received route. */
2092  for (ri = rn->info; ri; ri = ri->next)
2093    if (ri->peer == peer && ri->type == type && ri->sub_type == sub_type)
2094      break;
2095
2096  /* AS path local-as loop check. */
2097  if (peer->change_local_as)
2098    {
2099      if (! CHECK_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND))
2100	aspath_loop_count = 1;
2101
2102      if (aspath_loop_check (attr->aspath, peer->change_local_as) > aspath_loop_count)
2103	{
2104	  reason = "as-path contains our own AS;";
2105	  goto filtered;
2106	}
2107    }
2108
2109  /* AS path loop check. */
2110  if (aspath_loop_check (attr->aspath, bgp->as) > peer->allowas_in[afi][safi]
2111      || (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION)
2112	  && aspath_loop_check(attr->aspath, bgp->confed_id)
2113	  > peer->allowas_in[afi][safi]))
2114    {
2115      reason = "as-path contains our own AS;";
2116      goto filtered;
2117    }
2118
2119  /* Route reflector originator ID check.  */
2120  if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_ORIGINATOR_ID)
2121      && IPV4_ADDR_SAME (&bgp->router_id, &attr->extra->originator_id))
2122    {
2123      reason = "originator is us;";
2124      goto filtered;
2125    }
2126
2127  /* Route reflector cluster ID check.  */
2128  if (bgp_cluster_filter (peer, attr))
2129    {
2130      reason = "reflected from the same cluster;";
2131      goto  filtered;
2132    }
2133
2134  /* Apply incoming filter.  */
2135  if (bgp_input_filter (peer, p, attr, afi, safi) == FILTER_DENY)
2136    {
2137      reason = "filter;";
2138      goto filtered;
2139    }
2140
2141  new_attr.extra = &new_extra;
2142  bgp_attr_dup (&new_attr, attr);
2143
2144  /* Apply incoming route-map.
2145   * NB: new_attr may now contain newly allocated values from route-map "set"
2146   * commands, so we need bgp_attr_flush in the error paths, until we intern
2147   * the attr (which takes over the memory references) */
2148  if (bgp_input_modifier (peer, p, &new_attr, afi, safi) == RMAP_DENY)
2149    {
2150      reason = "route-map;";
2151      bgp_attr_flush (&new_attr);
2152      goto filtered;
2153    }
2154
2155  /* IPv4 unicast next hop check.  */
2156  if (afi == AFI_IP && safi == SAFI_UNICAST)
2157    {
2158      /* If the peer is EBGP and nexthop is not on connected route,
2159	 discard it.  */
2160      if (peer->sort == BGP_PEER_EBGP && peer->ttl == 1
2161	  && ! bgp_nexthop_onlink (afi, &new_attr)
2162	  && ! CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK))
2163	{
2164	  reason = "non-connected next-hop;";
2165	  bgp_attr_flush (&new_attr);
2166	  goto filtered;
2167	}
2168
2169      /* Next hop must not be 0.0.0.0 nor Class D/E address. Next hop
2170	 must not be my own address.  */
2171      if (new_attr.nexthop.s_addr == 0
2172	  || IPV4_CLASS_DE (ntohl (new_attr.nexthop.s_addr))
2173	  || bgp_nexthop_self (&new_attr))
2174	{
2175	  reason = "martian next-hop;";
2176	  bgp_attr_flush (&new_attr);
2177	  goto filtered;
2178	}
2179    }
2180
2181  attr_new = bgp_attr_intern (&new_attr);
2182
2183  /* If the update is implicit withdraw. */
2184  if (ri)
2185    {
2186      ri->uptime = bgp_clock ();
2187
2188      /* Same attribute comes in. */
2189      if (!CHECK_FLAG (ri->flags, BGP_INFO_REMOVED)
2190          && attrhash_cmp (ri->attr, attr_new))
2191	{
2192	  bgp_info_unset_flag (rn, ri, BGP_INFO_ATTR_CHANGED);
2193
2194	  if (CHECK_FLAG (bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING)
2195	      && peer->sort == BGP_PEER_EBGP
2196	      && CHECK_FLAG (ri->flags, BGP_INFO_HISTORY))
2197	    {
2198	      if (BGP_DEBUG (update, UPDATE_IN))
2199		  zlog (peer->log, LOG_DEBUG, "%s rcvd %s/%d",
2200		  peer->host,
2201		  inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
2202		  p->prefixlen);
2203
2204	      if (bgp_damp_update (ri, rn, afi, safi) != BGP_DAMP_SUPPRESSED)
2205	        {
2206                  bgp_aggregate_increment (bgp, p, ri, afi, safi);
2207                  bgp_process (bgp, rn, afi, safi);
2208                }
2209	    }
2210          else /* Duplicate - odd */
2211	    {
2212	      if (BGP_DEBUG (update, UPDATE_IN))
2213		zlog (peer->log, LOG_DEBUG,
2214		"%s rcvd %s/%d...duplicate ignored",
2215		peer->host,
2216		inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
2217		p->prefixlen);
2218
2219	      /* graceful restart STALE flag unset. */
2220	      if (CHECK_FLAG (ri->flags, BGP_INFO_STALE))
2221		{
2222		  bgp_info_unset_flag (rn, ri, BGP_INFO_STALE);
2223		  bgp_process (bgp, rn, afi, safi);
2224		}
2225	    }
2226
2227	  bgp_unlock_node (rn);
2228	  bgp_attr_unintern (&attr_new);
2229
2230	  return 0;
2231	}
2232
2233      /* Withdraw/Announce before we fully processed the withdraw */
2234      if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
2235        {
2236          if (BGP_DEBUG (update, UPDATE_IN))
2237            zlog (peer->log, LOG_DEBUG, "%s rcvd %s/%d, flapped quicker than processing",
2238            peer->host,
2239            inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
2240            p->prefixlen);
2241          bgp_info_restore (rn, ri);
2242        }
2243
2244      /* Received Logging. */
2245      if (BGP_DEBUG (update, UPDATE_IN))
2246	zlog (peer->log, LOG_DEBUG, "%s rcvd %s/%d",
2247	      peer->host,
2248	      inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
2249	      p->prefixlen);
2250
2251      /* graceful restart STALE flag unset. */
2252      if (CHECK_FLAG (ri->flags, BGP_INFO_STALE))
2253	bgp_info_unset_flag (rn, ri, BGP_INFO_STALE);
2254
2255      /* The attribute is changed. */
2256      bgp_info_set_flag (rn, ri, BGP_INFO_ATTR_CHANGED);
2257
2258      /* implicit withdraw, decrement aggregate and pcount here.
2259       * only if update is accepted, they'll increment below.
2260       */
2261      bgp_aggregate_decrement (bgp, p, ri, afi, safi);
2262
2263      /* Update bgp route dampening information.  */
2264      if (CHECK_FLAG (bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING)
2265	  && peer->sort == BGP_PEER_EBGP)
2266	{
2267	  /* This is implicit withdraw so we should update dampening
2268	     information.  */
2269	  if (! CHECK_FLAG (ri->flags, BGP_INFO_HISTORY))
2270	    bgp_damp_withdraw (ri, rn, afi, safi, 1);
2271	}
2272
2273      /* Update to new attribute.  */
2274      bgp_attr_unintern (&ri->attr);
2275      ri->attr = attr_new;
2276
2277      /* Update MPLS tag.  */
2278      if (safi == SAFI_MPLS_VPN)
2279        memcpy ((bgp_info_extra_get (ri))->tag, tag, 3);
2280
2281      /* Update bgp route dampening information.  */
2282      if (CHECK_FLAG (bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING)
2283	  && peer->sort == BGP_PEER_EBGP)
2284	{
2285	  /* Now we do normal update dampening.  */
2286	  ret = bgp_damp_update (ri, rn, afi, safi);
2287	  if (ret == BGP_DAMP_SUPPRESSED)
2288	    {
2289	      bgp_unlock_node (rn);
2290	      return 0;
2291	    }
2292	}
2293
2294      /* Nexthop reachability check. */
2295      if ((afi == AFI_IP || afi == AFI_IP6)
2296	  && safi == SAFI_UNICAST
2297	  && (peer->sort == BGP_PEER_IBGP
2298              || peer->sort == BGP_PEER_CONFED
2299	      || (peer->sort == BGP_PEER_EBGP && peer->ttl != 1)
2300	      || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)))
2301	{
2302	  if (bgp_nexthop_lookup (afi, peer, ri, NULL, NULL))
2303	    bgp_info_set_flag (rn, ri, BGP_INFO_VALID);
2304	  else
2305	    bgp_info_unset_flag (rn, ri, BGP_INFO_VALID);
2306	}
2307      else
2308        bgp_info_set_flag (rn, ri, BGP_INFO_VALID);
2309
2310      /* Process change. */
2311      bgp_aggregate_increment (bgp, p, ri, afi, safi);
2312
2313      bgp_process (bgp, rn, afi, safi);
2314      bgp_unlock_node (rn);
2315
2316      return 0;
2317    }
2318
2319  /* Received Logging. */
2320  if (BGP_DEBUG (update, UPDATE_IN))
2321    {
2322      zlog (peer->log, LOG_DEBUG, "%s rcvd %s/%d",
2323	    peer->host,
2324	    inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
2325	    p->prefixlen);
2326    }
2327
2328  /* Make new BGP info. */
2329  new = bgp_info_new ();
2330  new->type = type;
2331  new->sub_type = sub_type;
2332  new->peer = peer;
2333  new->attr = attr_new;
2334  new->uptime = bgp_clock ();
2335
2336  /* Update MPLS tag. */
2337  if (safi == SAFI_MPLS_VPN)
2338    memcpy ((bgp_info_extra_get (new))->tag, tag, 3);
2339
2340  /* Nexthop reachability check. */
2341  if ((afi == AFI_IP || afi == AFI_IP6)
2342      && safi == SAFI_UNICAST
2343      && (peer->sort == BGP_PEER_IBGP
2344          || peer->sort == BGP_PEER_CONFED
2345	  || (peer->sort == BGP_PEER_EBGP && peer->ttl != 1)
2346	  || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)))
2347    {
2348      if (bgp_nexthop_lookup (afi, peer, new, NULL, NULL))
2349	bgp_info_set_flag (rn, new, BGP_INFO_VALID);
2350      else
2351        bgp_info_unset_flag (rn, new, BGP_INFO_VALID);
2352    }
2353  else
2354    bgp_info_set_flag (rn, new, BGP_INFO_VALID);
2355
2356  /* Increment prefix */
2357  bgp_aggregate_increment (bgp, p, new, afi, safi);
2358
2359  /* Register new BGP information. */
2360  bgp_info_add (rn, new);
2361
2362  /* route_node_get lock */
2363  bgp_unlock_node (rn);
2364
2365  /* If maximum prefix count is configured and current prefix
2366     count exeed it. */
2367  if (bgp_maximum_prefix_overflow (peer, afi, safi, 0))
2368    return -1;
2369
2370  /* Process change. */
2371  bgp_process (bgp, rn, afi, safi);
2372
2373  return 0;
2374
2375  /* This BGP update is filtered.  Log the reason then update BGP
2376     entry.  */
2377 filtered:
2378  if (BGP_DEBUG (update, UPDATE_IN))
2379    zlog (peer->log, LOG_DEBUG,
2380	  "%s rcvd UPDATE about %s/%d -- DENIED due to: %s",
2381	  peer->host,
2382	  inet_ntop (p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
2383	  p->prefixlen, reason);
2384
2385  if (ri)
2386    bgp_rib_remove (rn, ri, peer, afi, safi);
2387
2388  bgp_unlock_node (rn);
2389
2390  return 0;
2391}
2392
2393int
2394bgp_update (struct peer *peer, struct prefix *p, struct attr *attr,
2395            afi_t afi, safi_t safi, int type, int sub_type,
2396            struct prefix_rd *prd, u_char *tag, int soft_reconfig)
2397{
2398  struct peer *rsclient;
2399  struct listnode *node, *nnode;
2400  struct bgp *bgp;
2401  int ret;
2402
2403  ret = bgp_update_main (peer, p, attr, afi, safi, type, sub_type, prd, tag,
2404          soft_reconfig);
2405
2406  bgp = peer->bgp;
2407
2408  /* Process the update for each RS-client. */
2409  for (ALL_LIST_ELEMENTS (bgp->rsclient, node, nnode, rsclient))
2410    {
2411      if (CHECK_FLAG (rsclient->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT))
2412        bgp_update_rsclient (rsclient, afi, safi, attr, peer, p, type,
2413                sub_type, prd, tag);
2414    }
2415
2416  return ret;
2417}
2418
2419int
2420bgp_withdraw (struct peer *peer, struct prefix *p, struct attr *attr,
2421	     afi_t afi, safi_t safi, int type, int sub_type,
2422	     struct prefix_rd *prd, u_char *tag)
2423{
2424  struct bgp *bgp;
2425  char buf[SU_ADDRSTRLEN];
2426  struct bgp_node *rn;
2427  struct bgp_info *ri;
2428  struct peer *rsclient;
2429  struct listnode *node, *nnode;
2430
2431  bgp = peer->bgp;
2432
2433  /* Process the withdraw for each RS-client. */
2434  for (ALL_LIST_ELEMENTS (bgp->rsclient, node, nnode, rsclient))
2435    {
2436      if (CHECK_FLAG (rsclient->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT))
2437        bgp_withdraw_rsclient (rsclient, afi, safi, peer, p, type, sub_type, prd, tag);
2438    }
2439
2440  /* Logging. */
2441  if (BGP_DEBUG (update, UPDATE_IN))
2442    zlog (peer->log, LOG_DEBUG, "%s rcvd UPDATE about %s/%d -- withdrawn",
2443	  peer->host,
2444	  inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
2445	  p->prefixlen);
2446
2447  /* Lookup node. */
2448  rn = bgp_afi_node_get (bgp->rib[afi][safi], afi, safi, p, prd);
2449
2450  /* If peer is soft reconfiguration enabled.  Record input packet for
2451     further calculation. */
2452  if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SOFT_RECONFIG)
2453      && peer != bgp->peer_self)
2454    bgp_adj_in_unset (rn, peer);
2455
2456  /* Lookup withdrawn route. */
2457  for (ri = rn->info; ri; ri = ri->next)
2458    if (ri->peer == peer && ri->type == type && ri->sub_type == sub_type)
2459      break;
2460
2461  /* Withdraw specified route from routing table. */
2462  if (ri && ! CHECK_FLAG (ri->flags, BGP_INFO_HISTORY))
2463    bgp_rib_withdraw (rn, ri, peer, afi, safi);
2464  else if (BGP_DEBUG (update, UPDATE_IN))
2465    zlog (peer->log, LOG_DEBUG,
2466	  "%s Can't find the route %s/%d", peer->host,
2467	  inet_ntop (p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
2468	  p->prefixlen);
2469
2470  /* Unlock bgp_node_get() lock. */
2471  bgp_unlock_node (rn);
2472
2473  return 0;
2474}
2475
2476void
2477bgp_default_originate (struct peer *peer, afi_t afi, safi_t safi, int withdraw)
2478{
2479  struct bgp *bgp;
2480  struct attr attr;
2481  struct aspath *aspath;
2482  struct prefix p;
2483  struct peer *from;
2484  struct bgp_node *rn;
2485  struct bgp_info *ri;
2486  int ret = RMAP_DENYMATCH;
2487
2488  if (!(afi == AFI_IP || afi == AFI_IP6))
2489    return;
2490
2491  bgp = peer->bgp;
2492  from = bgp->peer_self;
2493
2494  bgp_attr_default_set (&attr, BGP_ORIGIN_IGP);
2495  aspath = attr.aspath;
2496  attr.local_pref = bgp->default_local_pref;
2497  memcpy (&attr.nexthop, &peer->nexthop.v4, IPV4_MAX_BYTELEN);
2498
2499  if (afi == AFI_IP)
2500    str2prefix ("0.0.0.0/0", &p);
2501#ifdef HAVE_IPV6
2502  else if (afi == AFI_IP6)
2503    {
2504      struct attr_extra *ae = attr.extra;
2505
2506      str2prefix ("::/0", &p);
2507
2508      /* IPv6 global nexthop must be included. */
2509      memcpy (&ae->mp_nexthop_global, &peer->nexthop.v6_global,
2510	      IPV6_MAX_BYTELEN);
2511	      ae->mp_nexthop_len = 16;
2512
2513      /* If the peer is on shared nextwork and we have link-local
2514	 nexthop set it. */
2515      if (peer->shared_network
2516	  && !IN6_IS_ADDR_UNSPECIFIED (&peer->nexthop.v6_local))
2517	{
2518	  memcpy (&ae->mp_nexthop_local, &peer->nexthop.v6_local,
2519		  IPV6_MAX_BYTELEN);
2520	  ae->mp_nexthop_len = 32;
2521	}
2522    }
2523#endif /* HAVE_IPV6 */
2524
2525  if (peer->default_rmap[afi][safi].name)
2526    {
2527      SET_FLAG (bgp->peer_self->rmap_type, PEER_RMAP_TYPE_DEFAULT);
2528      for (rn = bgp_table_top(bgp->rib[afi][safi]); rn; rn = bgp_route_next(rn))
2529        {
2530          for (ri = rn->info; ri; ri = ri->next)
2531            {
2532              struct attr dummy_attr;
2533              struct attr_extra dummy_extra;
2534              struct bgp_info info;
2535
2536              /* Provide dummy so the route-map can't modify the attributes */
2537              dummy_attr.extra = &dummy_extra;
2538              bgp_attr_dup(&dummy_attr, ri->attr);
2539              info.peer = ri->peer;
2540              info.attr = &dummy_attr;
2541
2542              ret = route_map_apply(peer->default_rmap[afi][safi].map, &rn->p,
2543                                    RMAP_BGP, &info);
2544
2545              /* The route map might have set attributes. If we don't flush them
2546               * here, they will be leaked. */
2547              bgp_attr_flush(&dummy_attr);
2548              if (ret != RMAP_DENYMATCH)
2549                break;
2550            }
2551          if (ret != RMAP_DENYMATCH)
2552            break;
2553        }
2554      bgp->peer_self->rmap_type = 0;
2555
2556      if (ret == RMAP_DENYMATCH)
2557        withdraw = 1;
2558    }
2559
2560  if (withdraw)
2561    {
2562      if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_DEFAULT_ORIGINATE))
2563	bgp_default_withdraw_send (peer, afi, safi);
2564      UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_DEFAULT_ORIGINATE);
2565    }
2566  else
2567    {
2568      if (! CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_DEFAULT_ORIGINATE))
2569        {
2570          SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_DEFAULT_ORIGINATE);
2571          bgp_default_update_send (peer, &attr, afi, safi, from);
2572        }
2573    }
2574
2575  bgp_attr_extra_free (&attr);
2576  aspath_unintern (&aspath);
2577}
2578
2579static void
2580bgp_announce_table (struct peer *peer, afi_t afi, safi_t safi,
2581                   struct bgp_table *table, int rsclient)
2582{
2583  struct bgp_node *rn;
2584  struct bgp_info *ri;
2585  struct attr attr;
2586  struct attr_extra extra;
2587
2588  if (! table)
2589    table = (rsclient) ? peer->rib[afi][safi] : peer->bgp->rib[afi][safi];
2590
2591  if (safi != SAFI_MPLS_VPN
2592      && CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE))
2593    bgp_default_originate (peer, afi, safi, 0);
2594
2595  /* It's initialized in bgp_announce_[check|check_rsclient]() */
2596  attr.extra = &extra;
2597
2598  for (rn = bgp_table_top (table); rn; rn = bgp_route_next(rn))
2599    for (ri = rn->info; ri; ri = ri->next)
2600      if (CHECK_FLAG (ri->flags, BGP_INFO_SELECTED) && ri->peer != peer)
2601	{
2602         if ( (rsclient) ?
2603              (bgp_announce_check_rsclient (ri, peer, &rn->p, &attr, afi, safi))
2604              : (bgp_announce_check (ri, peer, &rn->p, &attr, afi, safi)))
2605	    bgp_adj_out_set (rn, peer, &rn->p, &attr, afi, safi, ri);
2606	  else
2607	    bgp_adj_out_unset (rn, peer, &rn->p, afi, safi);
2608	}
2609}
2610
2611void
2612bgp_announce_route (struct peer *peer, afi_t afi, safi_t safi)
2613{
2614  struct bgp_node *rn;
2615  struct bgp_table *table;
2616
2617  if (peer->status != Established)
2618    return;
2619
2620  if (! peer->afc_nego[afi][safi])
2621    return;
2622
2623  /* First update is deferred until ORF or ROUTE-REFRESH is received */
2624  if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH))
2625    return;
2626
2627  if (safi != SAFI_MPLS_VPN)
2628    bgp_announce_table (peer, afi, safi, NULL, 0);
2629  else
2630    for (rn = bgp_table_top (peer->bgp->rib[afi][safi]); rn;
2631	 rn = bgp_route_next(rn))
2632      if ((table = (rn->info)) != NULL)
2633       bgp_announce_table (peer, afi, safi, table, 0);
2634
2635  if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT))
2636    bgp_announce_table (peer, afi, safi, NULL, 1);
2637}
2638
2639void
2640bgp_announce_route_all (struct peer *peer)
2641{
2642  afi_t afi;
2643  safi_t safi;
2644
2645  for (afi = AFI_IP; afi < AFI_MAX; afi++)
2646    for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
2647      bgp_announce_route (peer, afi, safi);
2648}
2649
2650static void
2651bgp_soft_reconfig_table_rsclient (struct peer *rsclient, afi_t afi,
2652        safi_t safi, struct bgp_table *table, struct prefix_rd *prd)
2653{
2654  struct bgp_node *rn;
2655  struct bgp_adj_in *ain;
2656
2657  if (! table)
2658    table = rsclient->bgp->rib[afi][safi];
2659
2660  for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
2661    for (ain = rn->adj_in; ain; ain = ain->next)
2662      {
2663        struct bgp_info *ri = rn->info;
2664        u_char *tag = (ri && ri->extra) ? ri->extra->tag : NULL;
2665
2666        bgp_update_rsclient (rsclient, afi, safi, ain->attr, ain->peer,
2667                &rn->p, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, prd, tag);
2668      }
2669}
2670
2671void
2672bgp_soft_reconfig_rsclient (struct peer *rsclient, afi_t afi, safi_t safi)
2673{
2674  struct bgp_table *table;
2675  struct bgp_node *rn;
2676
2677  if (safi != SAFI_MPLS_VPN)
2678    bgp_soft_reconfig_table_rsclient (rsclient, afi, safi, NULL, NULL);
2679
2680  else
2681    for (rn = bgp_table_top (rsclient->bgp->rib[afi][safi]); rn;
2682            rn = bgp_route_next (rn))
2683      if ((table = rn->info) != NULL)
2684        {
2685          struct prefix_rd prd;
2686          prd.family = AF_UNSPEC;
2687          prd.prefixlen = 64;
2688          memcpy(&prd.val, rn->p.u.val, 8);
2689
2690          bgp_soft_reconfig_table_rsclient (rsclient, afi, safi, table, &prd);
2691        }
2692}
2693
2694static void
2695bgp_soft_reconfig_table (struct peer *peer, afi_t afi, safi_t safi,
2696			 struct bgp_table *table, struct prefix_rd *prd)
2697{
2698  int ret;
2699  struct bgp_node *rn;
2700  struct bgp_adj_in *ain;
2701
2702  if (! table)
2703    table = peer->bgp->rib[afi][safi];
2704
2705  for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
2706    for (ain = rn->adj_in; ain; ain = ain->next)
2707      {
2708	if (ain->peer == peer)
2709	  {
2710	    struct bgp_info *ri = rn->info;
2711	    u_char *tag = (ri && ri->extra) ? ri->extra->tag : NULL;
2712
2713	    ret = bgp_update (peer, &rn->p, ain->attr, afi, safi,
2714			      ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
2715			      prd, tag, 1);
2716
2717	    if (ret < 0)
2718	      {
2719		bgp_unlock_node (rn);
2720		return;
2721	      }
2722	    continue;
2723	  }
2724      }
2725}
2726
2727void
2728bgp_soft_reconfig_in (struct peer *peer, afi_t afi, safi_t safi)
2729{
2730  struct bgp_node *rn;
2731  struct bgp_table *table;
2732
2733  if (peer->status != Established)
2734    return;
2735
2736  if (safi != SAFI_MPLS_VPN)
2737    bgp_soft_reconfig_table (peer, afi, safi, NULL, NULL);
2738  else
2739    for (rn = bgp_table_top (peer->bgp->rib[afi][safi]); rn;
2740	 rn = bgp_route_next (rn))
2741      if ((table = rn->info) != NULL)
2742        {
2743          struct prefix_rd prd;
2744          prd.family = AF_UNSPEC;
2745          prd.prefixlen = 64;
2746          memcpy(&prd.val, rn->p.u.val, 8);
2747
2748          bgp_soft_reconfig_table (peer, afi, safi, table, &prd);
2749        }
2750}
2751
2752
2753struct bgp_clear_node_queue
2754{
2755  struct bgp_node *rn;
2756  enum bgp_clear_route_type purpose;
2757};
2758
2759static wq_item_status
2760bgp_clear_route_node (struct work_queue *wq, void *data)
2761{
2762  struct bgp_clear_node_queue *cnq = data;
2763  struct bgp_node *rn = cnq->rn;
2764  struct peer *peer = wq->spec.data;
2765  struct bgp_info *ri;
2766  afi_t afi = bgp_node_table (rn)->afi;
2767  safi_t safi = bgp_node_table (rn)->safi;
2768
2769  assert (rn && peer);
2770
2771  for (ri = rn->info; ri; ri = ri->next)
2772    if (ri->peer == peer || cnq->purpose == BGP_CLEAR_ROUTE_MY_RSCLIENT)
2773      {
2774        /* graceful restart STALE flag set. */
2775        if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT)
2776            && peer->nsf[afi][safi]
2777            && ! CHECK_FLAG (ri->flags, BGP_INFO_STALE)
2778            && ! CHECK_FLAG (ri->flags, BGP_INFO_UNUSEABLE))
2779          bgp_info_set_flag (rn, ri, BGP_INFO_STALE);
2780        else
2781          bgp_rib_remove (rn, ri, peer, afi, safi);
2782        break;
2783      }
2784  return WQ_SUCCESS;
2785}
2786
2787static void
2788bgp_clear_node_queue_del (struct work_queue *wq, void *data)
2789{
2790  struct bgp_clear_node_queue *cnq = data;
2791  struct bgp_node *rn = cnq->rn;
2792  struct bgp_table *table = bgp_node_table (rn);
2793
2794  bgp_unlock_node (rn);
2795  bgp_table_unlock (table);
2796  XFREE (MTYPE_BGP_CLEAR_NODE_QUEUE, cnq);
2797}
2798
2799static void
2800bgp_clear_node_complete (struct work_queue *wq)
2801{
2802  struct peer *peer = wq->spec.data;
2803
2804  /* Tickle FSM to start moving again */
2805  BGP_EVENT_ADD (peer, Clearing_Completed);
2806
2807  peer_unlock (peer); /* bgp_clear_route */
2808}
2809
2810static void
2811bgp_clear_node_queue_init (struct peer *peer)
2812{
2813  char wname[sizeof("clear xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx")];
2814
2815  snprintf (wname, sizeof(wname), "clear %s", peer->host);
2816#undef CLEAR_QUEUE_NAME_LEN
2817
2818  if ( (peer->clear_node_queue = work_queue_new (bm->master, wname)) == NULL)
2819    {
2820      zlog_err ("%s: Failed to allocate work queue", __func__);
2821      exit (1);
2822    }
2823  peer->clear_node_queue->spec.hold = 10;
2824  peer->clear_node_queue->spec.workfunc = &bgp_clear_route_node;
2825  peer->clear_node_queue->spec.del_item_data = &bgp_clear_node_queue_del;
2826  peer->clear_node_queue->spec.completion_func = &bgp_clear_node_complete;
2827  peer->clear_node_queue->spec.max_retries = 0;
2828
2829  /* we only 'lock' this peer reference when the queue is actually active */
2830  peer->clear_node_queue->spec.data = peer;
2831}
2832
2833static void
2834bgp_clear_route_table (struct peer *peer, afi_t afi, safi_t safi,
2835                       struct bgp_table *table, struct peer *rsclient,
2836                       enum bgp_clear_route_type purpose)
2837{
2838  struct bgp_node *rn;
2839
2840
2841  if (! table)
2842    table = (rsclient) ? rsclient->rib[afi][safi] : peer->bgp->rib[afi][safi];
2843
2844  /* If still no table => afi/safi isn't configured at all or smth. */
2845  if (! table)
2846    return;
2847
2848  for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
2849    {
2850      struct bgp_info *ri;
2851      struct bgp_adj_in *ain;
2852      struct bgp_adj_out *aout;
2853
2854      /* XXX:TODO: This is suboptimal, every non-empty route_node is
2855       * queued for every clearing peer, regardless of whether it is
2856       * relevant to the peer at hand.
2857       *
2858       * Overview: There are 3 different indices which need to be
2859       * scrubbed, potentially, when a peer is removed:
2860       *
2861       * 1 peer's routes visible via the RIB (ie accepted routes)
2862       * 2 peer's routes visible by the (optional) peer's adj-in index
2863       * 3 other routes visible by the peer's adj-out index
2864       *
2865       * 3 there is no hurry in scrubbing, once the struct peer is
2866       * removed from bgp->peer, we could just GC such deleted peer's
2867       * adj-outs at our leisure.
2868       *
2869       * 1 and 2 must be 'scrubbed' in some way, at least made
2870       * invisible via RIB index before peer session is allowed to be
2871       * brought back up. So one needs to know when such a 'search' is
2872       * complete.
2873       *
2874       * Ideally:
2875       *
2876       * - there'd be a single global queue or a single RIB walker
2877       * - rather than tracking which route_nodes still need to be
2878       *   examined on a peer basis, we'd track which peers still
2879       *   aren't cleared
2880       *
2881       * Given that our per-peer prefix-counts now should be reliable,
2882       * this may actually be achievable. It doesn't seem to be a huge
2883       * problem at this time,
2884       */
2885      for (ain = rn->adj_in; ain; ain = ain->next)
2886        if (ain->peer == peer || purpose == BGP_CLEAR_ROUTE_MY_RSCLIENT)
2887          {
2888            bgp_adj_in_remove (rn, ain);
2889            bgp_unlock_node (rn);
2890            break;
2891          }
2892      for (aout = rn->adj_out; aout; aout = aout->next)
2893        if (aout->peer == peer || purpose == BGP_CLEAR_ROUTE_MY_RSCLIENT)
2894          {
2895            bgp_adj_out_remove (rn, aout, peer, afi, safi);
2896            bgp_unlock_node (rn);
2897            break;
2898          }
2899
2900      for (ri = rn->info; ri; ri = ri->next)
2901        if (ri->peer == peer || purpose == BGP_CLEAR_ROUTE_MY_RSCLIENT)
2902          {
2903            struct bgp_clear_node_queue *cnq;
2904
2905            /* both unlocked in bgp_clear_node_queue_del */
2906            bgp_table_lock (bgp_node_table (rn));
2907            bgp_lock_node (rn);
2908            cnq = XCALLOC (MTYPE_BGP_CLEAR_NODE_QUEUE,
2909                           sizeof (struct bgp_clear_node_queue));
2910            cnq->rn = rn;
2911            cnq->purpose = purpose;
2912            work_queue_add (peer->clear_node_queue, cnq);
2913            break;
2914          }
2915    }
2916  return;
2917}
2918
2919void
2920bgp_clear_route (struct peer *peer, afi_t afi, safi_t safi,
2921                 enum bgp_clear_route_type purpose)
2922{
2923  struct bgp_node *rn;
2924  struct bgp_table *table;
2925  struct peer *rsclient;
2926  struct listnode *node, *nnode;
2927
2928  if (peer->clear_node_queue == NULL)
2929    bgp_clear_node_queue_init (peer);
2930
2931  /* bgp_fsm.c keeps sessions in state Clearing, not transitioning to
2932   * Idle until it receives a Clearing_Completed event. This protects
2933   * against peers which flap faster than we can we clear, which could
2934   * lead to:
2935   *
2936   * a) race with routes from the new session being installed before
2937   *    clear_route_node visits the node (to delete the route of that
2938   *    peer)
2939   * b) resource exhaustion, clear_route_node likely leads to an entry
2940   *    on the process_main queue. Fast-flapping could cause that queue
2941   *    to grow and grow.
2942   */
2943  if (!peer->clear_node_queue->thread)
2944    peer_lock (peer); /* bgp_clear_node_complete */
2945
2946  switch (purpose)
2947    {
2948    case BGP_CLEAR_ROUTE_NORMAL:
2949      if (safi != SAFI_MPLS_VPN)
2950        bgp_clear_route_table (peer, afi, safi, NULL, NULL, purpose);
2951      else
2952        for (rn = bgp_table_top (peer->bgp->rib[afi][safi]); rn;
2953             rn = bgp_route_next (rn))
2954          if ((table = rn->info) != NULL)
2955            bgp_clear_route_table (peer, afi, safi, table, NULL, purpose);
2956
2957      for (ALL_LIST_ELEMENTS (peer->bgp->rsclient, node, nnode, rsclient))
2958        if (CHECK_FLAG(rsclient->af_flags[afi][safi],
2959                       PEER_FLAG_RSERVER_CLIENT))
2960          bgp_clear_route_table (peer, afi, safi, NULL, rsclient, purpose);
2961      break;
2962
2963    case BGP_CLEAR_ROUTE_MY_RSCLIENT:
2964      bgp_clear_route_table (peer, afi, safi, NULL, peer, purpose);
2965      break;
2966
2967    default:
2968      assert (0);
2969      break;
2970    }
2971
2972  /* If no routes were cleared, nothing was added to workqueue, the
2973   * completion function won't be run by workqueue code - call it here.
2974   * XXX: Actually, this assumption doesn't hold, see
2975   * bgp_clear_route_table(), we queue all non-empty nodes.
2976   *
2977   * Additionally, there is a presumption in FSM that clearing is only
2978   * really needed if peer state is Established - peers in
2979   * pre-Established states shouldn't have any route-update state
2980   * associated with them (in or out).
2981   *
2982   * We still can get here in pre-Established though, through
2983   * peer_delete -> bgp_fsm_change_status, so this is a useful sanity
2984   * check to ensure the assumption above holds.
2985   *
2986   * At some future point, this check could be move to the top of the
2987   * function, and do a quick early-return when state is
2988   * pre-Established, avoiding above list and table scans. Once we're
2989   * sure it is safe..
2990   */
2991  if (!peer->clear_node_queue->thread)
2992    bgp_clear_node_complete (peer->clear_node_queue);
2993}
2994
2995void
2996bgp_clear_route_all (struct peer *peer)
2997{
2998  afi_t afi;
2999  safi_t safi;
3000
3001  for (afi = AFI_IP; afi < AFI_MAX; afi++)
3002    for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
3003      bgp_clear_route (peer, afi, safi, BGP_CLEAR_ROUTE_NORMAL);
3004}
3005
3006void
3007bgp_clear_adj_in (struct peer *peer, afi_t afi, safi_t safi)
3008{
3009  struct bgp_table *table;
3010  struct bgp_node *rn;
3011  struct bgp_adj_in *ain;
3012
3013  table = peer->bgp->rib[afi][safi];
3014
3015  for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
3016    for (ain = rn->adj_in; ain ; ain = ain->next)
3017      if (ain->peer == peer)
3018	{
3019          bgp_adj_in_remove (rn, ain);
3020          bgp_unlock_node (rn);
3021          break;
3022	}
3023}
3024
3025void
3026bgp_clear_stale_route (struct peer *peer, afi_t afi, safi_t safi)
3027{
3028  struct bgp_node *rn;
3029  struct bgp_info *ri;
3030  struct bgp_table *table;
3031
3032  table = peer->bgp->rib[afi][safi];
3033
3034  for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
3035    {
3036      for (ri = rn->info; ri; ri = ri->next)
3037	if (ri->peer == peer)
3038	  {
3039	    if (CHECK_FLAG (ri->flags, BGP_INFO_STALE))
3040	      bgp_rib_remove (rn, ri, peer, afi, safi);
3041	    break;
3042	  }
3043    }
3044}
3045
3046/* Delete all kernel routes. */
3047void
3048bgp_cleanup_routes (void)
3049{
3050  struct bgp *bgp;
3051  struct listnode *node, *nnode;
3052  struct bgp_node *rn;
3053  struct bgp_table *table;
3054  struct bgp_info *ri;
3055
3056  for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
3057    {
3058      table = bgp->rib[AFI_IP][SAFI_UNICAST];
3059
3060      for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
3061	for (ri = rn->info; ri; ri = ri->next)
3062	  if (CHECK_FLAG (ri->flags, BGP_INFO_SELECTED)
3063	      && ri->type == ZEBRA_ROUTE_BGP
3064	      && ri->sub_type == BGP_ROUTE_NORMAL)
3065	    bgp_zebra_withdraw (&rn->p, ri,SAFI_UNICAST);
3066
3067      table = bgp->rib[AFI_IP6][SAFI_UNICAST];
3068
3069      for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
3070	for (ri = rn->info; ri; ri = ri->next)
3071	  if (CHECK_FLAG (ri->flags, BGP_INFO_SELECTED)
3072	      && ri->type == ZEBRA_ROUTE_BGP
3073	      && ri->sub_type == BGP_ROUTE_NORMAL)
3074	    bgp_zebra_withdraw (&rn->p, ri,SAFI_UNICAST);
3075    }
3076}
3077
3078void
3079bgp_reset (void)
3080{
3081  vty_reset ();
3082  bgp_zclient_reset ();
3083  access_list_reset ();
3084  prefix_list_reset ();
3085}
3086
3087/* Parse NLRI stream.  Withdraw NLRI is recognized by NULL attr
3088   value. */
3089int
3090bgp_nlri_parse (struct peer *peer, struct attr *attr, struct bgp_nlri *packet)
3091{
3092  u_char *pnt;
3093  u_char *lim;
3094  struct prefix p;
3095  int psize;
3096  int ret;
3097
3098  /* Check peer status. */
3099  if (peer->status != Established)
3100    return 0;
3101
3102  pnt = packet->nlri;
3103  lim = pnt + packet->length;
3104
3105  for (; pnt < lim; pnt += psize)
3106    {
3107      /* Clear prefix structure. */
3108      memset (&p, 0, sizeof (struct prefix));
3109
3110      /* Fetch prefix length. */
3111      p.prefixlen = *pnt++;
3112      p.family = afi2family (packet->afi);
3113
3114      /* Already checked in nlri_sanity_check().  We do double check
3115         here. */
3116      if ((packet->afi == AFI_IP && p.prefixlen > 32)
3117	  || (packet->afi == AFI_IP6 && p.prefixlen > 128))
3118	return -1;
3119
3120      /* Packet size overflow check. */
3121      psize = PSIZE (p.prefixlen);
3122
3123      /* When packet overflow occur return immediately. */
3124      if (pnt + psize > lim)
3125	return -1;
3126
3127      /* Fetch prefix from NLRI packet. */
3128      memcpy (&p.u.prefix, pnt, psize);
3129
3130      /* Check address. */
3131      if (packet->afi == AFI_IP && packet->safi == SAFI_UNICAST)
3132	{
3133	  if (IN_CLASSD (ntohl (p.u.prefix4.s_addr)))
3134	    {
3135	     /*
3136 	      * From draft-ietf-idr-bgp4-22, Section 6.3:
3137	      * If a BGP router receives an UPDATE message with a
3138	      * semantically incorrect NLRI field, in which a prefix is
3139	      * semantically incorrect (eg. an unexpected multicast IP
3140	      * address), it should ignore the prefix.
3141	      */
3142	      zlog (peer->log, LOG_ERR,
3143		    "IPv4 unicast NLRI is multicast address %s",
3144		    inet_ntoa (p.u.prefix4));
3145
3146	      return -1;
3147	    }
3148	}
3149
3150#ifdef HAVE_IPV6
3151      /* Check address. */
3152      if (packet->afi == AFI_IP6 && packet->safi == SAFI_UNICAST)
3153	{
3154	  if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
3155	    {
3156	      char buf[BUFSIZ];
3157
3158	      zlog (peer->log, LOG_WARNING,
3159		    "IPv6 link-local NLRI received %s ignore this NLRI",
3160		    inet_ntop (AF_INET6, &p.u.prefix6, buf, BUFSIZ));
3161
3162	      continue;
3163	    }
3164	}
3165#endif /* HAVE_IPV6 */
3166
3167      /* Normal process. */
3168      if (attr)
3169	ret = bgp_update (peer, &p, attr, packet->afi, packet->safi,
3170			  ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, NULL, NULL, 0);
3171      else
3172	ret = bgp_withdraw (peer, &p, attr, packet->afi, packet->safi,
3173			    ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, NULL, NULL);
3174
3175      /* Address family configuration mismatch or maximum-prefix count
3176         overflow. */
3177      if (ret < 0)
3178	return -1;
3179    }
3180
3181  /* Packet length consistency check. */
3182  if (pnt != lim)
3183    return -1;
3184
3185  return 0;
3186}
3187
3188/* NLRI encode syntax check routine. */
3189int
3190bgp_nlri_sanity_check (struct peer *peer, int afi, u_char *pnt,
3191		       bgp_size_t length)
3192{
3193  u_char *end;
3194  u_char prefixlen;
3195  int psize;
3196
3197  end = pnt + length;
3198
3199  /* RFC1771 6.3 The NLRI field in the UPDATE message is checked for
3200     syntactic validity.  If the field is syntactically incorrect,
3201     then the Error Subcode is set to Invalid Network Field. */
3202
3203  while (pnt < end)
3204    {
3205      prefixlen = *pnt++;
3206
3207      /* Prefix length check. */
3208      if ((afi == AFI_IP && prefixlen > 32)
3209	  || (afi == AFI_IP6 && prefixlen > 128))
3210	{
3211	  plog_err (peer->log,
3212		    "%s [Error] Update packet error (wrong prefix length %d)",
3213		    peer->host, prefixlen);
3214	  bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
3215			   BGP_NOTIFY_UPDATE_INVAL_NETWORK);
3216	  return -1;
3217	}
3218
3219      /* Packet size overflow check. */
3220      psize = PSIZE (prefixlen);
3221
3222      if (pnt + psize > end)
3223	{
3224	  plog_err (peer->log,
3225		    "%s [Error] Update packet error"
3226		    " (prefix data overflow prefix size is %d)",
3227		    peer->host, psize);
3228	  bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
3229			   BGP_NOTIFY_UPDATE_INVAL_NETWORK);
3230	  return -1;
3231	}
3232
3233      pnt += psize;
3234    }
3235
3236  /* Packet length consistency check. */
3237  if (pnt != end)
3238    {
3239      plog_err (peer->log,
3240		"%s [Error] Update packet error"
3241		" (prefix length mismatch with total length)",
3242		peer->host);
3243      bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
3244		       BGP_NOTIFY_UPDATE_INVAL_NETWORK);
3245      return -1;
3246    }
3247  return 0;
3248}
3249
3250static struct bgp_static *
3251bgp_static_new (void)
3252{
3253  return XCALLOC (MTYPE_BGP_STATIC, sizeof (struct bgp_static));
3254}
3255
3256static void
3257bgp_static_free (struct bgp_static *bgp_static)
3258{
3259  if (bgp_static->rmap.name)
3260    free (bgp_static->rmap.name);
3261  XFREE (MTYPE_BGP_STATIC, bgp_static);
3262}
3263
3264static void
3265bgp_static_withdraw_rsclient (struct bgp *bgp, struct peer *rsclient,
3266        struct prefix *p, afi_t afi, safi_t safi)
3267{
3268  struct bgp_node *rn;
3269  struct bgp_info *ri;
3270
3271  rn = bgp_afi_node_get (rsclient->rib[afi][safi], afi, safi, p, NULL);
3272
3273  /* Check selected route and self inserted route. */
3274  for (ri = rn->info; ri; ri = ri->next)
3275    if (ri->peer == bgp->peer_self
3276       && ri->type == ZEBRA_ROUTE_BGP
3277       && ri->sub_type == BGP_ROUTE_STATIC)
3278      break;
3279
3280  /* Withdraw static BGP route from routing table. */
3281  if (ri)
3282    {
3283      bgp_info_delete (rn, ri);
3284      bgp_process (bgp, rn, afi, safi);
3285    }
3286
3287  /* Unlock bgp_node_lookup. */
3288  bgp_unlock_node (rn);
3289}
3290
3291static void
3292bgp_static_update_rsclient (struct peer *rsclient, struct prefix *p,
3293                            struct bgp_static *bgp_static,
3294                            afi_t afi, safi_t safi)
3295{
3296  struct bgp_node *rn;
3297  struct bgp_info *ri;
3298  struct bgp_info *new;
3299  struct bgp_info info;
3300  struct attr *attr_new;
3301  struct attr attr;
3302  struct attr new_attr;
3303  struct attr_extra new_extra;
3304  struct bgp *bgp;
3305  int ret;
3306  char buf[SU_ADDRSTRLEN];
3307
3308  bgp = rsclient->bgp;
3309
3310  assert (bgp_static);
3311  if (!bgp_static)
3312    return;
3313
3314  rn = bgp_afi_node_get (rsclient->rib[afi][safi], afi, safi, p, NULL);
3315
3316  bgp_attr_default_set (&attr, BGP_ORIGIN_IGP);
3317
3318  attr.nexthop = bgp_static->igpnexthop;
3319  attr.med = bgp_static->igpmetric;
3320  attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC);
3321
3322  if (bgp_static->atomic)
3323    attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE);
3324
3325  /* Apply network route-map for export to this rsclient. */
3326  if (bgp_static->rmap.name)
3327    {
3328      struct attr attr_tmp = attr;
3329      info.peer = rsclient;
3330      info.attr = &attr_tmp;
3331
3332      SET_FLAG (rsclient->rmap_type, PEER_RMAP_TYPE_EXPORT);
3333      SET_FLAG (rsclient->rmap_type, PEER_RMAP_TYPE_NETWORK);
3334
3335      ret = route_map_apply (bgp_static->rmap.map, p, RMAP_BGP, &info);
3336
3337      rsclient->rmap_type = 0;
3338
3339      if (ret == RMAP_DENYMATCH)
3340        {
3341          /* Free uninterned attribute. */
3342          bgp_attr_flush (&attr_tmp);
3343
3344          /* Unintern original. */
3345          aspath_unintern (&attr.aspath);
3346          bgp_static_withdraw_rsclient (bgp, rsclient, p, afi, safi);
3347          bgp_attr_extra_free (&attr);
3348
3349          return;
3350        }
3351      attr_new = bgp_attr_intern (&attr_tmp);
3352    }
3353  else
3354    attr_new = bgp_attr_intern (&attr);
3355
3356  new_attr.extra = &new_extra;
3357  bgp_attr_dup(&new_attr, attr_new);
3358
3359  SET_FLAG (bgp->peer_self->rmap_type, PEER_RMAP_TYPE_NETWORK);
3360
3361  if (bgp_import_modifier (rsclient, bgp->peer_self, p, &new_attr, afi, safi)
3362        == RMAP_DENY)
3363    {
3364      /* This BGP update is filtered.  Log the reason then update BGP entry.  */
3365      if (BGP_DEBUG (update, UPDATE_IN))
3366              zlog (rsclient->log, LOG_DEBUG,
3367              "Static UPDATE about %s/%d -- DENIED for RS-client %s due to: import-policy",
3368              inet_ntop (p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
3369              p->prefixlen, rsclient->host);
3370
3371      bgp->peer_self->rmap_type = 0;
3372
3373      bgp_attr_unintern (&attr_new);
3374      aspath_unintern (&attr.aspath);
3375      bgp_attr_extra_free (&attr);
3376
3377      bgp_static_withdraw_rsclient (bgp, rsclient, p, afi, safi);
3378
3379      return;
3380    }
3381
3382  bgp->peer_self->rmap_type = 0;
3383
3384  bgp_attr_unintern (&attr_new);
3385  attr_new = bgp_attr_intern (&new_attr);
3386
3387  for (ri = rn->info; ri; ri = ri->next)
3388    if (ri->peer == bgp->peer_self && ri->type == ZEBRA_ROUTE_BGP
3389            && ri->sub_type == BGP_ROUTE_STATIC)
3390      break;
3391
3392  if (ri)
3393       {
3394      if (attrhash_cmp (ri->attr, attr_new) &&
3395	  !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
3396        {
3397          bgp_unlock_node (rn);
3398          bgp_attr_unintern (&attr_new);
3399          aspath_unintern (&attr.aspath);
3400          bgp_attr_extra_free (&attr);
3401          return;
3402       }
3403      else
3404        {
3405          /* The attribute is changed. */
3406          bgp_info_set_flag (rn, ri, BGP_INFO_ATTR_CHANGED);
3407
3408          /* Rewrite BGP route information. */
3409	  if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
3410	    bgp_info_restore(rn, ri);
3411          bgp_attr_unintern (&ri->attr);
3412          ri->attr = attr_new;
3413          ri->uptime = bgp_clock ();
3414
3415          /* Process change. */
3416          bgp_process (bgp, rn, afi, safi);
3417          bgp_unlock_node (rn);
3418          aspath_unintern (&attr.aspath);
3419          bgp_attr_extra_free (&attr);
3420          return;
3421        }
3422    }
3423
3424  /* Make new BGP info. */
3425  new = bgp_info_new ();
3426  new->type = ZEBRA_ROUTE_BGP;
3427  new->sub_type = BGP_ROUTE_STATIC;
3428  new->peer = bgp->peer_self;
3429  SET_FLAG (new->flags, BGP_INFO_VALID);
3430  new->attr = attr_new;
3431  new->uptime = bgp_clock ();
3432
3433  /* Register new BGP information. */
3434  bgp_info_add (rn, new);
3435
3436  /* route_node_get lock */
3437  bgp_unlock_node (rn);
3438
3439  /* Process change. */
3440  bgp_process (bgp, rn, afi, safi);
3441
3442  /* Unintern original. */
3443  aspath_unintern (&attr.aspath);
3444  bgp_attr_extra_free (&attr);
3445}
3446
3447static void
3448bgp_static_update_main (struct bgp *bgp, struct prefix *p,
3449		   struct bgp_static *bgp_static, afi_t afi, safi_t safi)
3450{
3451  struct bgp_node *rn;
3452  struct bgp_info *ri;
3453  struct bgp_info *new;
3454  struct bgp_info info;
3455  struct attr attr;
3456  struct attr *attr_new;
3457  int ret;
3458
3459  assert (bgp_static);
3460  if (!bgp_static)
3461    return;
3462
3463  rn = bgp_afi_node_get (bgp->rib[afi][safi], afi, safi, p, NULL);
3464
3465  bgp_attr_default_set (&attr, BGP_ORIGIN_IGP);
3466
3467  attr.nexthop = bgp_static->igpnexthop;
3468  attr.med = bgp_static->igpmetric;
3469  attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC);
3470
3471  if (bgp_static->atomic)
3472    attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE);
3473
3474  /* Apply route-map. */
3475  if (bgp_static->rmap.name)
3476    {
3477      struct attr attr_tmp = attr;
3478      info.peer = bgp->peer_self;
3479      info.attr = &attr_tmp;
3480
3481      SET_FLAG (bgp->peer_self->rmap_type, PEER_RMAP_TYPE_NETWORK);
3482
3483      ret = route_map_apply (bgp_static->rmap.map, p, RMAP_BGP, &info);
3484
3485      bgp->peer_self->rmap_type = 0;
3486
3487      if (ret == RMAP_DENYMATCH)
3488	{
3489	  /* Free uninterned attribute. */
3490	  bgp_attr_flush (&attr_tmp);
3491
3492	  /* Unintern original. */
3493	  aspath_unintern (&attr.aspath);
3494	  bgp_attr_extra_free (&attr);
3495	  bgp_static_withdraw (bgp, p, afi, safi);
3496	  return;
3497	}
3498      attr_new = bgp_attr_intern (&attr_tmp);
3499    }
3500  else
3501    attr_new = bgp_attr_intern (&attr);
3502
3503  for (ri = rn->info; ri; ri = ri->next)
3504    if (ri->peer == bgp->peer_self && ri->type == ZEBRA_ROUTE_BGP
3505	&& ri->sub_type == BGP_ROUTE_STATIC)
3506      break;
3507
3508  if (ri)
3509    {
3510      if (attrhash_cmp (ri->attr, attr_new) &&
3511	  !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
3512	{
3513	  bgp_unlock_node (rn);
3514	  bgp_attr_unintern (&attr_new);
3515	  aspath_unintern (&attr.aspath);
3516	  bgp_attr_extra_free (&attr);
3517	  return;
3518	}
3519      else
3520	{
3521	  /* The attribute is changed. */
3522	  bgp_info_set_flag (rn, ri, BGP_INFO_ATTR_CHANGED);
3523
3524	  /* Rewrite BGP route information. */
3525	  if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
3526	    bgp_info_restore(rn, ri);
3527	  else
3528	    bgp_aggregate_decrement (bgp, p, ri, afi, safi);
3529	  bgp_attr_unintern (&ri->attr);
3530	  ri->attr = attr_new;
3531	  ri->uptime = bgp_clock ();
3532
3533	  /* Process change. */
3534	  bgp_aggregate_increment (bgp, p, ri, afi, safi);
3535	  bgp_process (bgp, rn, afi, safi);
3536	  bgp_unlock_node (rn);
3537	  aspath_unintern (&attr.aspath);
3538	  bgp_attr_extra_free (&attr);
3539	  return;
3540	}
3541    }
3542
3543  /* Make new BGP info. */
3544  new = bgp_info_new ();
3545  new->type = ZEBRA_ROUTE_BGP;
3546  new->sub_type = BGP_ROUTE_STATIC;
3547  new->peer = bgp->peer_self;
3548  SET_FLAG (new->flags, BGP_INFO_VALID);
3549  new->attr = attr_new;
3550  new->uptime = bgp_clock ();
3551
3552  /* Aggregate address increment. */
3553  bgp_aggregate_increment (bgp, p, new, afi, safi);
3554
3555  /* Register new BGP information. */
3556  bgp_info_add (rn, new);
3557
3558  /* route_node_get lock */
3559  bgp_unlock_node (rn);
3560
3561  /* Process change. */
3562  bgp_process (bgp, rn, afi, safi);
3563
3564  /* Unintern original. */
3565  aspath_unintern (&attr.aspath);
3566  bgp_attr_extra_free (&attr);
3567}
3568
3569void
3570bgp_static_update (struct bgp *bgp, struct prefix *p,
3571                  struct bgp_static *bgp_static, afi_t afi, safi_t safi)
3572{
3573  struct peer *rsclient;
3574  struct listnode *node, *nnode;
3575
3576  bgp_static_update_main (bgp, p, bgp_static, afi, safi);
3577
3578  for (ALL_LIST_ELEMENTS (bgp->rsclient, node, nnode, rsclient))
3579    {
3580      if (CHECK_FLAG (rsclient->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT))
3581        bgp_static_update_rsclient (rsclient, p, bgp_static, afi, safi);
3582    }
3583}
3584
3585static void
3586bgp_static_update_vpnv4 (struct bgp *bgp, struct prefix *p, afi_t afi,
3587			 safi_t safi, struct prefix_rd *prd, u_char *tag)
3588{
3589  struct bgp_node *rn;
3590  struct bgp_info *new;
3591
3592  rn = bgp_afi_node_get (bgp->rib[afi][safi], afi, safi, p, prd);
3593
3594  /* Make new BGP info. */
3595  new = bgp_info_new ();
3596  new->type = ZEBRA_ROUTE_BGP;
3597  new->sub_type = BGP_ROUTE_STATIC;
3598  new->peer = bgp->peer_self;
3599  new->attr = bgp_attr_default_intern (BGP_ORIGIN_IGP);
3600  SET_FLAG (new->flags, BGP_INFO_VALID);
3601  new->uptime = bgp_clock ();
3602  new->extra = bgp_info_extra_new();
3603  memcpy (new->extra->tag, tag, 3);
3604
3605  /* Aggregate address increment. */
3606  bgp_aggregate_increment (bgp, p, new, afi, safi);
3607
3608  /* Register new BGP information. */
3609  bgp_info_add (rn, new);
3610
3611  /* route_node_get lock */
3612  bgp_unlock_node (rn);
3613
3614  /* Process change. */
3615  bgp_process (bgp, rn, afi, safi);
3616}
3617
3618void
3619bgp_static_withdraw (struct bgp *bgp, struct prefix *p, afi_t afi,
3620		     safi_t safi)
3621{
3622  struct bgp_node *rn;
3623  struct bgp_info *ri;
3624
3625  rn = bgp_afi_node_get (bgp->rib[afi][safi], afi, safi, p, NULL);
3626
3627  /* Check selected route and self inserted route. */
3628  for (ri = rn->info; ri; ri = ri->next)
3629    if (ri->peer == bgp->peer_self
3630	&& ri->type == ZEBRA_ROUTE_BGP
3631	&& ri->sub_type == BGP_ROUTE_STATIC)
3632      break;
3633
3634  /* Withdraw static BGP route from routing table. */
3635  if (ri)
3636    {
3637      bgp_aggregate_decrement (bgp, p, ri, afi, safi);
3638      bgp_info_delete (rn, ri);
3639      bgp_process (bgp, rn, afi, safi);
3640    }
3641
3642  /* Unlock bgp_node_lookup. */
3643  bgp_unlock_node (rn);
3644}
3645
3646void
3647bgp_check_local_routes_rsclient (struct peer *rsclient, afi_t afi, safi_t safi)
3648{
3649  struct bgp_static *bgp_static;
3650  struct bgp *bgp;
3651  struct bgp_node *rn;
3652  struct prefix *p;
3653
3654  bgp = rsclient->bgp;
3655
3656  for (rn = bgp_table_top (bgp->route[afi][safi]); rn; rn = bgp_route_next (rn))
3657    if ((bgp_static = rn->info) != NULL)
3658      {
3659        p = &rn->p;
3660
3661        bgp_static_update_rsclient (rsclient, p, bgp_static,
3662                afi, safi);
3663      }
3664}
3665
3666static void
3667bgp_static_withdraw_vpnv4 (struct bgp *bgp, struct prefix *p, afi_t afi,
3668			   safi_t safi, struct prefix_rd *prd, u_char *tag)
3669{
3670  struct bgp_node *rn;
3671  struct bgp_info *ri;
3672
3673  rn = bgp_afi_node_get (bgp->rib[afi][safi], afi, safi, p, prd);
3674
3675  /* Check selected route and self inserted route. */
3676  for (ri = rn->info; ri; ri = ri->next)
3677    if (ri->peer == bgp->peer_self
3678	&& ri->type == ZEBRA_ROUTE_BGP
3679	&& ri->sub_type == BGP_ROUTE_STATIC)
3680      break;
3681
3682  /* Withdraw static BGP route from routing table. */
3683  if (ri)
3684    {
3685      bgp_aggregate_decrement (bgp, p, ri, afi, safi);
3686      bgp_info_delete (rn, ri);
3687      bgp_process (bgp, rn, afi, safi);
3688    }
3689
3690  /* Unlock bgp_node_lookup. */
3691  bgp_unlock_node (rn);
3692}
3693
3694/* Configure static BGP network.  When user don't run zebra, static
3695   route should be installed as valid.  */
3696static int
3697bgp_static_set (struct vty *vty, struct bgp *bgp, const char *ip_str,
3698                afi_t afi, safi_t safi, const char *rmap, int backdoor)
3699{
3700  int ret;
3701  struct prefix p;
3702  struct bgp_static *bgp_static;
3703  struct bgp_node *rn;
3704  u_char need_update = 0;
3705
3706  /* Convert IP prefix string to struct prefix. */
3707  ret = str2prefix (ip_str, &p);
3708  if (! ret)
3709    {
3710      vty_out (vty, "%% Malformed prefix%s", VTY_NEWLINE);
3711      return CMD_WARNING;
3712    }
3713#ifdef HAVE_IPV6
3714  if (afi == AFI_IP6 && IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
3715    {
3716      vty_out (vty, "%% Malformed prefix (link-local address)%s",
3717	       VTY_NEWLINE);
3718      return CMD_WARNING;
3719    }
3720#endif /* HAVE_IPV6 */
3721
3722  apply_mask (&p);
3723
3724  /* Set BGP static route configuration. */
3725  rn = bgp_node_get (bgp->route[afi][safi], &p);
3726
3727  if (rn->info)
3728    {
3729      /* Configuration change. */
3730      bgp_static = rn->info;
3731
3732      /* Check previous routes are installed into BGP.  */
3733      if (bgp_static->valid && bgp_static->backdoor != backdoor)
3734        need_update = 1;
3735
3736      bgp_static->backdoor = backdoor;
3737
3738      if (rmap)
3739	{
3740	  if (bgp_static->rmap.name)
3741	    free (bgp_static->rmap.name);
3742	  bgp_static->rmap.name = strdup (rmap);
3743	  bgp_static->rmap.map = route_map_lookup_by_name (rmap);
3744	}
3745      else
3746	{
3747	  if (bgp_static->rmap.name)
3748	    free (bgp_static->rmap.name);
3749	  bgp_static->rmap.name = NULL;
3750	  bgp_static->rmap.map = NULL;
3751	  bgp_static->valid = 0;
3752	}
3753      bgp_unlock_node (rn);
3754    }
3755  else
3756    {
3757      /* New configuration. */
3758      bgp_static = bgp_static_new ();
3759      bgp_static->backdoor = backdoor;
3760      bgp_static->valid = 0;
3761      bgp_static->igpmetric = 0;
3762      bgp_static->igpnexthop.s_addr = 0;
3763
3764      if (rmap)
3765	{
3766	  if (bgp_static->rmap.name)
3767	    free (bgp_static->rmap.name);
3768	  bgp_static->rmap.name = strdup (rmap);
3769	  bgp_static->rmap.map = route_map_lookup_by_name (rmap);
3770	}
3771      rn->info = bgp_static;
3772    }
3773
3774  /* If BGP scan is not enabled, we should install this route here.  */
3775  if (! bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK))
3776    {
3777      bgp_static->valid = 1;
3778
3779      if (need_update)
3780	bgp_static_withdraw (bgp, &p, afi, safi);
3781
3782      if (! bgp_static->backdoor)
3783	bgp_static_update (bgp, &p, bgp_static, afi, safi);
3784    }
3785
3786  return CMD_SUCCESS;
3787}
3788
3789/* Configure static BGP network. */
3790static int
3791bgp_static_unset (struct vty *vty, struct bgp *bgp, const char *ip_str,
3792		  afi_t afi, safi_t safi)
3793{
3794  int ret;
3795  struct prefix p;
3796  struct bgp_static *bgp_static;
3797  struct bgp_node *rn;
3798
3799  /* Convert IP prefix string to struct prefix. */
3800  ret = str2prefix (ip_str, &p);
3801  if (! ret)
3802    {
3803      vty_out (vty, "%% Malformed prefix%s", VTY_NEWLINE);
3804      return CMD_WARNING;
3805    }
3806#ifdef HAVE_IPV6
3807  if (afi == AFI_IP6 && IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
3808    {
3809      vty_out (vty, "%% Malformed prefix (link-local address)%s",
3810	       VTY_NEWLINE);
3811      return CMD_WARNING;
3812    }
3813#endif /* HAVE_IPV6 */
3814
3815  apply_mask (&p);
3816
3817  rn = bgp_node_lookup (bgp->route[afi][safi], &p);
3818  if (! rn)
3819    {
3820      vty_out (vty, "%% Can't find specified static route configuration.%s",
3821	       VTY_NEWLINE);
3822      return CMD_WARNING;
3823    }
3824
3825  bgp_static = rn->info;
3826
3827  /* Update BGP RIB. */
3828  if (! bgp_static->backdoor)
3829    bgp_static_withdraw (bgp, &p, afi, safi);
3830
3831  /* Clear configuration. */
3832  bgp_static_free (bgp_static);
3833  rn->info = NULL;
3834  bgp_unlock_node (rn);
3835  bgp_unlock_node (rn);
3836
3837  return CMD_SUCCESS;
3838}
3839
3840/* Called from bgp_delete().  Delete all static routes from the BGP
3841   instance. */
3842void
3843bgp_static_delete (struct bgp *bgp)
3844{
3845  afi_t afi;
3846  safi_t safi;
3847  struct bgp_node *rn;
3848  struct bgp_node *rm;
3849  struct bgp_table *table;
3850  struct bgp_static *bgp_static;
3851
3852  for (afi = AFI_IP; afi < AFI_MAX; afi++)
3853    for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
3854      for (rn = bgp_table_top (bgp->route[afi][safi]); rn; rn = bgp_route_next (rn))
3855	if (rn->info != NULL)
3856	  {
3857	    if (safi == SAFI_MPLS_VPN)
3858	      {
3859		table = rn->info;
3860
3861		for (rm = bgp_table_top (table); rm; rm = bgp_route_next (rm))
3862		  {
3863		    bgp_static = rn->info;
3864		    bgp_static_withdraw_vpnv4 (bgp, &rm->p,
3865					       AFI_IP, SAFI_MPLS_VPN,
3866					       (struct prefix_rd *)&rn->p,
3867					       bgp_static->tag);
3868		    bgp_static_free (bgp_static);
3869		    rn->info = NULL;
3870		    bgp_unlock_node (rn);
3871		  }
3872	      }
3873	    else
3874	      {
3875		bgp_static = rn->info;
3876		bgp_static_withdraw (bgp, &rn->p, afi, safi);
3877		bgp_static_free (bgp_static);
3878		rn->info = NULL;
3879		bgp_unlock_node (rn);
3880	      }
3881	  }
3882}
3883
3884int
3885bgp_static_set_vpnv4 (struct vty *vty, const char *ip_str, const char *rd_str,
3886		      const char *tag_str)
3887{
3888  int ret;
3889  struct prefix p;
3890  struct prefix_rd prd;
3891  struct bgp *bgp;
3892  struct bgp_node *prn;
3893  struct bgp_node *rn;
3894  struct bgp_table *table;
3895  struct bgp_static *bgp_static;
3896  u_char tag[3];
3897
3898  bgp = vty->index;
3899
3900  ret = str2prefix (ip_str, &p);
3901  if (! ret)
3902    {
3903      vty_out (vty, "%% Malformed prefix%s", VTY_NEWLINE);
3904      return CMD_WARNING;
3905    }
3906  apply_mask (&p);
3907
3908  ret = str2prefix_rd (rd_str, &prd);
3909  if (! ret)
3910    {
3911      vty_out (vty, "%% Malformed rd%s", VTY_NEWLINE);
3912      return CMD_WARNING;
3913    }
3914
3915  ret = str2tag (tag_str, tag);
3916  if (! ret)
3917    {
3918      vty_out (vty, "%% Malformed tag%s", VTY_NEWLINE);
3919      return CMD_WARNING;
3920    }
3921
3922  prn = bgp_node_get (bgp->route[AFI_IP][SAFI_MPLS_VPN],
3923			(struct prefix *)&prd);
3924  if (prn->info == NULL)
3925    prn->info = bgp_table_init (AFI_IP, SAFI_MPLS_VPN);
3926  else
3927    bgp_unlock_node (prn);
3928  table = prn->info;
3929
3930  rn = bgp_node_get (table, &p);
3931
3932  if (rn->info)
3933    {
3934      vty_out (vty, "%% Same network configuration exists%s", VTY_NEWLINE);
3935      bgp_unlock_node (rn);
3936    }
3937  else
3938    {
3939      /* New configuration. */
3940      bgp_static = bgp_static_new ();
3941      bgp_static->valid = 1;
3942      memcpy (bgp_static->tag, tag, 3);
3943      rn->info = bgp_static;
3944
3945      bgp_static_update_vpnv4 (bgp, &p, AFI_IP, SAFI_MPLS_VPN, &prd, tag);
3946    }
3947
3948  return CMD_SUCCESS;
3949}
3950
3951/* Configure static BGP network. */
3952int
3953bgp_static_unset_vpnv4 (struct vty *vty, const char *ip_str,
3954                        const char *rd_str, const char *tag_str)
3955{
3956  int ret;
3957  struct bgp *bgp;
3958  struct prefix p;
3959  struct prefix_rd prd;
3960  struct bgp_node *prn;
3961  struct bgp_node *rn;
3962  struct bgp_table *table;
3963  struct bgp_static *bgp_static;
3964  u_char tag[3];
3965
3966  bgp = vty->index;
3967
3968  /* Convert IP prefix string to struct prefix. */
3969  ret = str2prefix (ip_str, &p);
3970  if (! ret)
3971    {
3972      vty_out (vty, "%% Malformed prefix%s", VTY_NEWLINE);
3973      return CMD_WARNING;
3974    }
3975  apply_mask (&p);
3976
3977  ret = str2prefix_rd (rd_str, &prd);
3978  if (! ret)
3979    {
3980      vty_out (vty, "%% Malformed rd%s", VTY_NEWLINE);
3981      return CMD_WARNING;
3982    }
3983
3984  ret = str2tag (tag_str, tag);
3985  if (! ret)
3986    {
3987      vty_out (vty, "%% Malformed tag%s", VTY_NEWLINE);
3988      return CMD_WARNING;
3989    }
3990
3991  prn = bgp_node_get (bgp->route[AFI_IP][SAFI_MPLS_VPN],
3992			(struct prefix *)&prd);
3993  if (prn->info == NULL)
3994    prn->info = bgp_table_init (AFI_IP, SAFI_MPLS_VPN);
3995  else
3996    bgp_unlock_node (prn);
3997  table = prn->info;
3998
3999  rn = bgp_node_lookup (table, &p);
4000
4001  if (rn)
4002    {
4003      bgp_static_withdraw_vpnv4 (bgp, &p, AFI_IP, SAFI_MPLS_VPN, &prd, tag);
4004
4005      bgp_static = rn->info;
4006      bgp_static_free (bgp_static);
4007      rn->info = NULL;
4008      bgp_unlock_node (rn);
4009      bgp_unlock_node (rn);
4010    }
4011  else
4012    vty_out (vty, "%% Can't find the route%s", VTY_NEWLINE);
4013
4014  return CMD_SUCCESS;
4015}
4016
4017DEFUN (bgp_network,
4018       bgp_network_cmd,
4019       "network A.B.C.D/M",
4020       "Specify a network to announce via BGP\n"
4021       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
4022{
4023  return bgp_static_set (vty, vty->index, argv[0],
4024			 AFI_IP, bgp_node_safi (vty), NULL, 0);
4025}
4026
4027DEFUN (bgp_network_route_map,
4028       bgp_network_route_map_cmd,
4029       "network A.B.C.D/M route-map WORD",
4030       "Specify a network to announce via BGP\n"
4031       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
4032       "Route-map to modify the attributes\n"
4033       "Name of the route map\n")
4034{
4035  return bgp_static_set (vty, vty->index, argv[0],
4036			 AFI_IP, bgp_node_safi (vty), argv[1], 0);
4037}
4038
4039DEFUN (bgp_network_backdoor,
4040       bgp_network_backdoor_cmd,
4041       "network A.B.C.D/M backdoor",
4042       "Specify a network to announce via BGP\n"
4043       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
4044       "Specify a BGP backdoor route\n")
4045{
4046  return bgp_static_set (vty, vty->index, argv[0], AFI_IP, SAFI_UNICAST,
4047                         NULL, 1);
4048}
4049
4050DEFUN (bgp_network_mask,
4051       bgp_network_mask_cmd,
4052       "network A.B.C.D mask A.B.C.D",
4053       "Specify a network to announce via BGP\n"
4054       "Network number\n"
4055       "Network mask\n"
4056       "Network mask\n")
4057{
4058  int ret;
4059  char prefix_str[BUFSIZ];
4060
4061  ret = netmask_str2prefix_str (argv[0], argv[1], prefix_str);
4062  if (! ret)
4063    {
4064      vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
4065      return CMD_WARNING;
4066    }
4067
4068  return bgp_static_set (vty, vty->index, prefix_str,
4069			 AFI_IP, bgp_node_safi (vty), NULL, 0);
4070}
4071
4072DEFUN (bgp_network_mask_route_map,
4073       bgp_network_mask_route_map_cmd,
4074       "network A.B.C.D mask A.B.C.D route-map WORD",
4075       "Specify a network to announce via BGP\n"
4076       "Network number\n"
4077       "Network mask\n"
4078       "Network mask\n"
4079       "Route-map to modify the attributes\n"
4080       "Name of the route map\n")
4081{
4082  int ret;
4083  char prefix_str[BUFSIZ];
4084
4085  ret = netmask_str2prefix_str (argv[0], argv[1], prefix_str);
4086  if (! ret)
4087    {
4088      vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
4089      return CMD_WARNING;
4090    }
4091
4092  return bgp_static_set (vty, vty->index, prefix_str,
4093			 AFI_IP, bgp_node_safi (vty), argv[2], 0);
4094}
4095
4096DEFUN (bgp_network_mask_backdoor,
4097       bgp_network_mask_backdoor_cmd,
4098       "network A.B.C.D mask A.B.C.D backdoor",
4099       "Specify a network to announce via BGP\n"
4100       "Network number\n"
4101       "Network mask\n"
4102       "Network mask\n"
4103       "Specify a BGP backdoor route\n")
4104{
4105  int ret;
4106  char prefix_str[BUFSIZ];
4107
4108  ret = netmask_str2prefix_str (argv[0], argv[1], prefix_str);
4109  if (! ret)
4110    {
4111      vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
4112      return CMD_WARNING;
4113    }
4114
4115  return bgp_static_set (vty, vty->index, prefix_str, AFI_IP, SAFI_UNICAST,
4116                         NULL, 1);
4117}
4118
4119DEFUN (bgp_network_mask_natural,
4120       bgp_network_mask_natural_cmd,
4121       "network A.B.C.D",
4122       "Specify a network to announce via BGP\n"
4123       "Network number\n")
4124{
4125  int ret;
4126  char prefix_str[BUFSIZ];
4127
4128  ret = netmask_str2prefix_str (argv[0], NULL, prefix_str);
4129  if (! ret)
4130    {
4131      vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
4132      return CMD_WARNING;
4133    }
4134
4135  return bgp_static_set (vty, vty->index, prefix_str,
4136			 AFI_IP, bgp_node_safi (vty), NULL, 0);
4137}
4138
4139DEFUN (bgp_network_mask_natural_route_map,
4140       bgp_network_mask_natural_route_map_cmd,
4141       "network A.B.C.D route-map WORD",
4142       "Specify a network to announce via BGP\n"
4143       "Network number\n"
4144       "Route-map to modify the attributes\n"
4145       "Name of the route map\n")
4146{
4147  int ret;
4148  char prefix_str[BUFSIZ];
4149
4150  ret = netmask_str2prefix_str (argv[0], NULL, prefix_str);
4151  if (! ret)
4152    {
4153      vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
4154      return CMD_WARNING;
4155    }
4156
4157  return bgp_static_set (vty, vty->index, prefix_str,
4158			 AFI_IP, bgp_node_safi (vty), argv[1], 0);
4159}
4160
4161DEFUN (bgp_network_mask_natural_backdoor,
4162       bgp_network_mask_natural_backdoor_cmd,
4163       "network A.B.C.D backdoor",
4164       "Specify a network to announce via BGP\n"
4165       "Network number\n"
4166       "Specify a BGP backdoor route\n")
4167{
4168  int ret;
4169  char prefix_str[BUFSIZ];
4170
4171  ret = netmask_str2prefix_str (argv[0], NULL, prefix_str);
4172  if (! ret)
4173    {
4174      vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
4175      return CMD_WARNING;
4176    }
4177
4178  return bgp_static_set (vty, vty->index, prefix_str, AFI_IP, SAFI_UNICAST,
4179                         NULL, 1);
4180}
4181
4182DEFUN (no_bgp_network,
4183       no_bgp_network_cmd,
4184       "no network A.B.C.D/M",
4185       NO_STR
4186       "Specify a network to announce via BGP\n"
4187       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
4188{
4189  return bgp_static_unset (vty, vty->index, argv[0], AFI_IP,
4190			   bgp_node_safi (vty));
4191}
4192
4193ALIAS (no_bgp_network,
4194       no_bgp_network_route_map_cmd,
4195       "no network A.B.C.D/M route-map WORD",
4196       NO_STR
4197       "Specify a network to announce via BGP\n"
4198       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
4199       "Route-map to modify the attributes\n"
4200       "Name of the route map\n")
4201
4202ALIAS (no_bgp_network,
4203       no_bgp_network_backdoor_cmd,
4204       "no network A.B.C.D/M backdoor",
4205       NO_STR
4206       "Specify a network to announce via BGP\n"
4207       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
4208       "Specify a BGP backdoor route\n")
4209
4210DEFUN (no_bgp_network_mask,
4211       no_bgp_network_mask_cmd,
4212       "no network A.B.C.D mask A.B.C.D",
4213       NO_STR
4214       "Specify a network to announce via BGP\n"
4215       "Network number\n"
4216       "Network mask\n"
4217       "Network mask\n")
4218{
4219  int ret;
4220  char prefix_str[BUFSIZ];
4221
4222  ret = netmask_str2prefix_str (argv[0], argv[1], prefix_str);
4223  if (! ret)
4224    {
4225      vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
4226      return CMD_WARNING;
4227    }
4228
4229  return bgp_static_unset (vty, vty->index, prefix_str, AFI_IP,
4230			   bgp_node_safi (vty));
4231}
4232
4233ALIAS (no_bgp_network_mask,
4234       no_bgp_network_mask_route_map_cmd,
4235       "no network A.B.C.D mask A.B.C.D route-map WORD",
4236       NO_STR
4237       "Specify a network to announce via BGP\n"
4238       "Network number\n"
4239       "Network mask\n"
4240       "Network mask\n"
4241       "Route-map to modify the attributes\n"
4242       "Name of the route map\n")
4243
4244ALIAS (no_bgp_network_mask,
4245       no_bgp_network_mask_backdoor_cmd,
4246       "no network A.B.C.D mask A.B.C.D backdoor",
4247       NO_STR
4248       "Specify a network to announce via BGP\n"
4249       "Network number\n"
4250       "Network mask\n"
4251       "Network mask\n"
4252       "Specify a BGP backdoor route\n")
4253
4254DEFUN (no_bgp_network_mask_natural,
4255       no_bgp_network_mask_natural_cmd,
4256       "no network A.B.C.D",
4257       NO_STR
4258       "Specify a network to announce via BGP\n"
4259       "Network number\n")
4260{
4261  int ret;
4262  char prefix_str[BUFSIZ];
4263
4264  ret = netmask_str2prefix_str (argv[0], NULL, prefix_str);
4265  if (! ret)
4266    {
4267      vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
4268      return CMD_WARNING;
4269    }
4270
4271  return bgp_static_unset (vty, vty->index, prefix_str, AFI_IP,
4272			   bgp_node_safi (vty));
4273}
4274
4275ALIAS (no_bgp_network_mask_natural,
4276       no_bgp_network_mask_natural_route_map_cmd,
4277       "no network A.B.C.D route-map WORD",
4278       NO_STR
4279       "Specify a network to announce via BGP\n"
4280       "Network number\n"
4281       "Route-map to modify the attributes\n"
4282       "Name of the route map\n")
4283
4284ALIAS (no_bgp_network_mask_natural,
4285       no_bgp_network_mask_natural_backdoor_cmd,
4286       "no network A.B.C.D backdoor",
4287       NO_STR
4288       "Specify a network to announce via BGP\n"
4289       "Network number\n"
4290       "Specify a BGP backdoor route\n")
4291
4292#ifdef HAVE_IPV6
4293DEFUN (ipv6_bgp_network,
4294       ipv6_bgp_network_cmd,
4295       "network X:X::X:X/M",
4296       "Specify a network to announce via BGP\n"
4297       "IPv6 prefix <network>/<length>\n")
4298{
4299  return bgp_static_set (vty, vty->index, argv[0], AFI_IP6, bgp_node_safi(vty),
4300                         NULL, 0);
4301}
4302
4303DEFUN (ipv6_bgp_network_route_map,
4304       ipv6_bgp_network_route_map_cmd,
4305       "network X:X::X:X/M route-map WORD",
4306       "Specify a network to announce via BGP\n"
4307       "IPv6 prefix <network>/<length>\n"
4308       "Route-map to modify the attributes\n"
4309       "Name of the route map\n")
4310{
4311  return bgp_static_set (vty, vty->index, argv[0], AFI_IP6,
4312			 bgp_node_safi (vty), argv[1], 0);
4313}
4314
4315DEFUN (no_ipv6_bgp_network,
4316       no_ipv6_bgp_network_cmd,
4317       "no network X:X::X:X/M",
4318       NO_STR
4319       "Specify a network to announce via BGP\n"
4320       "IPv6 prefix <network>/<length>\n")
4321{
4322  return bgp_static_unset (vty, vty->index, argv[0], AFI_IP6, bgp_node_safi(vty));
4323}
4324
4325ALIAS (no_ipv6_bgp_network,
4326       no_ipv6_bgp_network_route_map_cmd,
4327       "no network X:X::X:X/M route-map WORD",
4328       NO_STR
4329       "Specify a network to announce via BGP\n"
4330       "IPv6 prefix <network>/<length>\n"
4331       "Route-map to modify the attributes\n"
4332       "Name of the route map\n")
4333
4334ALIAS (ipv6_bgp_network,
4335       old_ipv6_bgp_network_cmd,
4336       "ipv6 bgp network X:X::X:X/M",
4337       IPV6_STR
4338       BGP_STR
4339       "Specify a network to announce via BGP\n"
4340       "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n")
4341
4342ALIAS (no_ipv6_bgp_network,
4343       old_no_ipv6_bgp_network_cmd,
4344       "no ipv6 bgp network X:X::X:X/M",
4345       NO_STR
4346       IPV6_STR
4347       BGP_STR
4348       "Specify a network to announce via BGP\n"
4349       "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n")
4350#endif /* HAVE_IPV6 */
4351
4352/* stubs for removed AS-Pathlimit commands, kept for config compatibility */
4353ALIAS_DEPRECATED (bgp_network,
4354       bgp_network_ttl_cmd,
4355       "network A.B.C.D/M pathlimit <0-255>",
4356       "Specify a network to announce via BGP\n"
4357       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
4358       "AS-Path hopcount limit attribute\n"
4359       "AS-Pathlimit TTL, in number of AS-Path hops\n")
4360ALIAS_DEPRECATED (bgp_network_backdoor,
4361       bgp_network_backdoor_ttl_cmd,
4362       "network A.B.C.D/M backdoor pathlimit <0-255>",
4363       "Specify a network to announce via BGP\n"
4364       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
4365       "Specify a BGP backdoor route\n"
4366       "AS-Path hopcount limit attribute\n"
4367       "AS-Pathlimit TTL, in number of AS-Path hops\n")
4368ALIAS_DEPRECATED (bgp_network_mask,
4369       bgp_network_mask_ttl_cmd,
4370       "network A.B.C.D mask A.B.C.D pathlimit <0-255>",
4371       "Specify a network to announce via BGP\n"
4372       "Network number\n"
4373       "Network mask\n"
4374       "Network mask\n"
4375       "AS-Path hopcount limit attribute\n"
4376       "AS-Pathlimit TTL, in number of AS-Path hops\n")
4377ALIAS_DEPRECATED (bgp_network_mask_backdoor,
4378       bgp_network_mask_backdoor_ttl_cmd,
4379       "network A.B.C.D mask A.B.C.D backdoor pathlimit <0-255>",
4380       "Specify a network to announce via BGP\n"
4381       "Network number\n"
4382       "Network mask\n"
4383       "Network mask\n"
4384       "Specify a BGP backdoor route\n"
4385       "AS-Path hopcount limit attribute\n"
4386       "AS-Pathlimit TTL, in number of AS-Path hops\n")
4387ALIAS_DEPRECATED (bgp_network_mask_natural,
4388       bgp_network_mask_natural_ttl_cmd,
4389       "network A.B.C.D pathlimit <0-255>",
4390       "Specify a network to announce via BGP\n"
4391       "Network number\n"
4392       "AS-Path hopcount limit attribute\n"
4393       "AS-Pathlimit TTL, in number of AS-Path hops\n")
4394ALIAS_DEPRECATED (bgp_network_mask_natural_backdoor,
4395       bgp_network_mask_natural_backdoor_ttl_cmd,
4396       "network A.B.C.D backdoor pathlimit <1-255>",
4397       "Specify a network to announce via BGP\n"
4398       "Network number\n"
4399       "Specify a BGP backdoor route\n"
4400       "AS-Path hopcount limit attribute\n"
4401       "AS-Pathlimit TTL, in number of AS-Path hops\n")
4402ALIAS_DEPRECATED (no_bgp_network,
4403       no_bgp_network_ttl_cmd,
4404       "no network A.B.C.D/M pathlimit <0-255>",
4405       NO_STR
4406       "Specify a network to announce via BGP\n"
4407       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
4408       "AS-Path hopcount limit attribute\n"
4409       "AS-Pathlimit TTL, in number of AS-Path hops\n")
4410ALIAS_DEPRECATED (no_bgp_network,
4411       no_bgp_network_backdoor_ttl_cmd,
4412       "no network A.B.C.D/M backdoor pathlimit <0-255>",
4413       NO_STR
4414       "Specify a network to announce via BGP\n"
4415       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
4416       "Specify a BGP backdoor route\n"
4417       "AS-Path hopcount limit attribute\n"
4418       "AS-Pathlimit TTL, in number of AS-Path hops\n")
4419ALIAS_DEPRECATED (no_bgp_network,
4420       no_bgp_network_mask_ttl_cmd,
4421       "no network A.B.C.D mask A.B.C.D pathlimit <0-255>",
4422       NO_STR
4423       "Specify a network to announce via BGP\n"
4424       "Network number\n"
4425       "Network mask\n"
4426       "Network mask\n"
4427       "AS-Path hopcount limit attribute\n"
4428       "AS-Pathlimit TTL, in number of AS-Path hops\n")
4429ALIAS_DEPRECATED (no_bgp_network_mask,
4430       no_bgp_network_mask_backdoor_ttl_cmd,
4431       "no network A.B.C.D mask A.B.C.D  backdoor pathlimit <0-255>",
4432       NO_STR
4433       "Specify a network to announce via BGP\n"
4434       "Network number\n"
4435       "Network mask\n"
4436       "Network mask\n"
4437       "Specify a BGP backdoor route\n"
4438       "AS-Path hopcount limit attribute\n"
4439       "AS-Pathlimit TTL, in number of AS-Path hops\n")
4440ALIAS_DEPRECATED (no_bgp_network_mask_natural,
4441       no_bgp_network_mask_natural_ttl_cmd,
4442       "no network A.B.C.D pathlimit <0-255>",
4443       NO_STR
4444       "Specify a network to announce via BGP\n"
4445       "Network number\n"
4446       "AS-Path hopcount limit attribute\n"
4447       "AS-Pathlimit TTL, in number of AS-Path hops\n")
4448ALIAS_DEPRECATED (no_bgp_network_mask_natural,
4449       no_bgp_network_mask_natural_backdoor_ttl_cmd,
4450       "no network A.B.C.D backdoor pathlimit <0-255>",
4451       NO_STR
4452       "Specify a network to announce via BGP\n"
4453       "Network number\n"
4454       "Specify a BGP backdoor route\n"
4455       "AS-Path hopcount limit attribute\n"
4456       "AS-Pathlimit TTL, in number of AS-Path hops\n")
4457#ifdef HAVE_IPV6
4458ALIAS_DEPRECATED (ipv6_bgp_network,
4459       ipv6_bgp_network_ttl_cmd,
4460       "network X:X::X:X/M pathlimit <0-255>",
4461       "Specify a network to announce via BGP\n"
4462       "IPv6 prefix <network>/<length>\n"
4463       "AS-Path hopcount limit attribute\n"
4464       "AS-Pathlimit TTL, in number of AS-Path hops\n")
4465ALIAS_DEPRECATED (no_ipv6_bgp_network,
4466       no_ipv6_bgp_network_ttl_cmd,
4467       "no network X:X::X:X/M pathlimit <0-255>",
4468       NO_STR
4469       "Specify a network to announce via BGP\n"
4470       "IPv6 prefix <network>/<length>\n"
4471       "AS-Path hopcount limit attribute\n"
4472       "AS-Pathlimit TTL, in number of AS-Path hops\n")
4473#endif /* HAVE_IPV6 */
4474
4475/* Aggreagete address:
4476
4477  advertise-map  Set condition to advertise attribute
4478  as-set         Generate AS set path information
4479  attribute-map  Set attributes of aggregate
4480  route-map      Set parameters of aggregate
4481  summary-only   Filter more specific routes from updates
4482  suppress-map   Conditionally filter more specific routes from updates
4483  <cr>
4484 */
4485struct bgp_aggregate
4486{
4487  /* Summary-only flag. */
4488  u_char summary_only;
4489
4490  /* AS set generation. */
4491  u_char as_set;
4492
4493  /* Route-map for aggregated route. */
4494  struct route_map *map;
4495
4496  /* Suppress-count. */
4497  unsigned long count;
4498
4499  /* SAFI configuration. */
4500  safi_t safi;
4501};
4502
4503static struct bgp_aggregate *
4504bgp_aggregate_new (void)
4505{
4506  return XCALLOC (MTYPE_BGP_AGGREGATE, sizeof (struct bgp_aggregate));
4507}
4508
4509static void
4510bgp_aggregate_free (struct bgp_aggregate *aggregate)
4511{
4512  XFREE (MTYPE_BGP_AGGREGATE, aggregate);
4513}
4514
4515static void
4516bgp_aggregate_route (struct bgp *bgp, struct prefix *p, struct bgp_info *rinew,
4517		     afi_t afi, safi_t safi, struct bgp_info *del,
4518		     struct bgp_aggregate *aggregate)
4519{
4520  struct bgp_table *table;
4521  struct bgp_node *top;
4522  struct bgp_node *rn;
4523  u_char origin;
4524  struct aspath *aspath = NULL;
4525  struct aspath *asmerge = NULL;
4526  struct community *community = NULL;
4527  struct community *commerge = NULL;
4528  struct bgp_info *ri;
4529  struct bgp_info *new;
4530  int first = 1;
4531  unsigned long match = 0;
4532
4533  /* ORIGIN attribute: If at least one route among routes that are
4534     aggregated has ORIGIN with the value INCOMPLETE, then the
4535     aggregated route must have the ORIGIN attribute with the value
4536     INCOMPLETE. Otherwise, if at least one route among routes that
4537     are aggregated has ORIGIN with the value EGP, then the aggregated
4538     route must have the origin attribute with the value EGP. In all
4539     other case the value of the ORIGIN attribute of the aggregated
4540     route is INTERNAL. */
4541  origin = BGP_ORIGIN_IGP;
4542
4543  table = bgp->rib[afi][safi];
4544
4545  top = bgp_node_get (table, p);
4546  for (rn = bgp_node_get (table, p); rn; rn = bgp_route_next_until (rn, top))
4547    if (rn->p.prefixlen > p->prefixlen)
4548      {
4549	match = 0;
4550
4551	for (ri = rn->info; ri; ri = ri->next)
4552	  {
4553	    if (BGP_INFO_HOLDDOWN (ri))
4554	      continue;
4555
4556	    if (del && ri == del)
4557	      continue;
4558
4559	    if (! rinew && first)
4560              first = 0;
4561
4562#ifdef AGGREGATE_NEXTHOP_CHECK
4563	    if (! IPV4_ADDR_SAME (&ri->attr->nexthop, &nexthop)
4564		|| ri->attr->med != med)
4565	      {
4566		if (aspath)
4567		  aspath_free (aspath);
4568		if (community)
4569		  community_free (community);
4570		bgp_unlock_node (rn);
4571		bgp_unlock_node (top);
4572		return;
4573	      }
4574#endif /* AGGREGATE_NEXTHOP_CHECK */
4575
4576	    if (ri->sub_type != BGP_ROUTE_AGGREGATE)
4577	      {
4578		if (aggregate->summary_only)
4579		  {
4580		    (bgp_info_extra_get (ri))->suppress++;
4581		    bgp_info_set_flag (rn, ri, BGP_INFO_ATTR_CHANGED);
4582		    match++;
4583		  }
4584
4585		aggregate->count++;
4586
4587		if (aggregate->as_set)
4588		  {
4589		    if (origin < ri->attr->origin)
4590		      origin = ri->attr->origin;
4591
4592		    if (aspath)
4593		      {
4594			asmerge = aspath_aggregate (aspath, ri->attr->aspath);
4595			aspath_free (aspath);
4596			aspath = asmerge;
4597		      }
4598		    else
4599		      aspath = aspath_dup (ri->attr->aspath);
4600
4601		    if (ri->attr->community)
4602		      {
4603			if (community)
4604			  {
4605			    commerge = community_merge (community,
4606							ri->attr->community);
4607			    community = community_uniq_sort (commerge);
4608			    community_free (commerge);
4609			  }
4610			else
4611			  community = community_dup (ri->attr->community);
4612		      }
4613		  }
4614	      }
4615	  }
4616	if (match)
4617	  bgp_process (bgp, rn, afi, safi);
4618      }
4619  bgp_unlock_node (top);
4620
4621  if (rinew)
4622    {
4623      aggregate->count++;
4624
4625      if (aggregate->summary_only)
4626        (bgp_info_extra_get (rinew))->suppress++;
4627
4628      if (aggregate->as_set)
4629	{
4630	  if (origin < rinew->attr->origin)
4631	    origin = rinew->attr->origin;
4632
4633	  if (aspath)
4634	    {
4635	      asmerge = aspath_aggregate (aspath, rinew->attr->aspath);
4636	      aspath_free (aspath);
4637	      aspath = asmerge;
4638	    }
4639	  else
4640	    aspath = aspath_dup (rinew->attr->aspath);
4641
4642	  if (rinew->attr->community)
4643	    {
4644	      if (community)
4645		{
4646		  commerge = community_merge (community,
4647					      rinew->attr->community);
4648		  community = community_uniq_sort (commerge);
4649		  community_free (commerge);
4650		}
4651	      else
4652		community = community_dup (rinew->attr->community);
4653	    }
4654	}
4655    }
4656
4657  if (aggregate->count > 0)
4658    {
4659      rn = bgp_node_get (table, p);
4660      new = bgp_info_new ();
4661      new->type = ZEBRA_ROUTE_BGP;
4662      new->sub_type = BGP_ROUTE_AGGREGATE;
4663      new->peer = bgp->peer_self;
4664      SET_FLAG (new->flags, BGP_INFO_VALID);
4665      new->attr = bgp_attr_aggregate_intern (bgp, origin, aspath, community, aggregate->as_set);
4666      new->uptime = bgp_clock ();
4667
4668      bgp_info_add (rn, new);
4669      bgp_unlock_node (rn);
4670      bgp_process (bgp, rn, afi, safi);
4671    }
4672  else
4673    {
4674      if (aspath)
4675	aspath_free (aspath);
4676      if (community)
4677	community_free (community);
4678    }
4679}
4680
4681void bgp_aggregate_delete (struct bgp *, struct prefix *, afi_t, safi_t,
4682			   struct bgp_aggregate *);
4683
4684void
4685bgp_aggregate_increment (struct bgp *bgp, struct prefix *p,
4686			 struct bgp_info *ri, afi_t afi, safi_t safi)
4687{
4688  struct bgp_node *child;
4689  struct bgp_node *rn;
4690  struct bgp_aggregate *aggregate;
4691  struct bgp_table *table;
4692
4693  /* MPLS-VPN aggregation is not yet supported. */
4694  if (safi == SAFI_MPLS_VPN)
4695    return;
4696
4697  table = bgp->aggregate[afi][safi];
4698
4699  /* No aggregates configured. */
4700  if (bgp_table_top_nolock (table) == NULL)
4701    return;
4702
4703  if (p->prefixlen == 0)
4704    return;
4705
4706  if (BGP_INFO_HOLDDOWN (ri))
4707    return;
4708
4709  child = bgp_node_get (table, p);
4710
4711  /* Aggregate address configuration check. */
4712  for (rn = child; rn; rn = bgp_node_parent_nolock (rn))
4713    if ((aggregate = rn->info) != NULL && rn->p.prefixlen < p->prefixlen)
4714      {
4715	bgp_aggregate_delete (bgp, &rn->p, afi, safi, aggregate);
4716	bgp_aggregate_route (bgp, &rn->p, ri, afi, safi, NULL, aggregate);
4717      }
4718  bgp_unlock_node (child);
4719}
4720
4721void
4722bgp_aggregate_decrement (struct bgp *bgp, struct prefix *p,
4723			 struct bgp_info *del, afi_t afi, safi_t safi)
4724{
4725  struct bgp_node *child;
4726  struct bgp_node *rn;
4727  struct bgp_aggregate *aggregate;
4728  struct bgp_table *table;
4729
4730  /* MPLS-VPN aggregation is not yet supported. */
4731  if (safi == SAFI_MPLS_VPN)
4732    return;
4733
4734  table = bgp->aggregate[afi][safi];
4735
4736  /* No aggregates configured. */
4737  if (bgp_table_top_nolock (table) == NULL)
4738    return;
4739
4740  if (p->prefixlen == 0)
4741    return;
4742
4743  child = bgp_node_get (table, p);
4744
4745  /* Aggregate address configuration check. */
4746  for (rn = child; rn; rn = bgp_node_parent_nolock (rn))
4747    if ((aggregate = rn->info) != NULL && rn->p.prefixlen < p->prefixlen)
4748      {
4749	bgp_aggregate_delete (bgp, &rn->p, afi, safi, aggregate);
4750	bgp_aggregate_route (bgp, &rn->p, NULL, afi, safi, del, aggregate);
4751      }
4752  bgp_unlock_node (child);
4753}
4754
4755static void
4756bgp_aggregate_add (struct bgp *bgp, struct prefix *p, afi_t afi, safi_t safi,
4757		   struct bgp_aggregate *aggregate)
4758{
4759  struct bgp_table *table;
4760  struct bgp_node *top;
4761  struct bgp_node *rn;
4762  struct bgp_info *new;
4763  struct bgp_info *ri;
4764  unsigned long match;
4765  u_char origin = BGP_ORIGIN_IGP;
4766  struct aspath *aspath = NULL;
4767  struct aspath *asmerge = NULL;
4768  struct community *community = NULL;
4769  struct community *commerge = NULL;
4770
4771  table = bgp->rib[afi][safi];
4772
4773  /* Sanity check. */
4774  if (afi == AFI_IP && p->prefixlen == IPV4_MAX_BITLEN)
4775    return;
4776  if (afi == AFI_IP6 && p->prefixlen == IPV6_MAX_BITLEN)
4777    return;
4778
4779  /* If routes exists below this node, generate aggregate routes. */
4780  top = bgp_node_get (table, p);
4781  for (rn = bgp_node_get (table, p); rn; rn = bgp_route_next_until (rn, top))
4782    if (rn->p.prefixlen > p->prefixlen)
4783      {
4784	match = 0;
4785
4786	for (ri = rn->info; ri; ri = ri->next)
4787	  {
4788	    if (BGP_INFO_HOLDDOWN (ri))
4789	      continue;
4790
4791	    if (ri->sub_type != BGP_ROUTE_AGGREGATE)
4792	      {
4793		/* summary-only aggregate route suppress aggregated
4794		   route announcement.  */
4795		if (aggregate->summary_only)
4796		  {
4797		    (bgp_info_extra_get (ri))->suppress++;
4798		    bgp_info_set_flag (rn, ri, BGP_INFO_ATTR_CHANGED);
4799		    match++;
4800		  }
4801		/* as-set aggregate route generate origin, as path,
4802		   community aggregation.  */
4803		if (aggregate->as_set)
4804		  {
4805		    if (origin < ri->attr->origin)
4806		      origin = ri->attr->origin;
4807
4808		    if (aspath)
4809		      {
4810			asmerge = aspath_aggregate (aspath, ri->attr->aspath);
4811			aspath_free (aspath);
4812			aspath = asmerge;
4813		      }
4814		    else
4815		      aspath = aspath_dup (ri->attr->aspath);
4816
4817		    if (ri->attr->community)
4818		      {
4819			if (community)
4820			  {
4821			    commerge = community_merge (community,
4822							ri->attr->community);
4823			    community = community_uniq_sort (commerge);
4824			    community_free (commerge);
4825			  }
4826			else
4827			  community = community_dup (ri->attr->community);
4828		      }
4829		  }
4830		aggregate->count++;
4831	      }
4832	  }
4833
4834	/* If this node is suppressed, process the change. */
4835	if (match)
4836	  bgp_process (bgp, rn, afi, safi);
4837      }
4838  bgp_unlock_node (top);
4839
4840  /* Add aggregate route to BGP table. */
4841  if (aggregate->count)
4842    {
4843      rn = bgp_node_get (table, p);
4844
4845      new = bgp_info_new ();
4846      new->type = ZEBRA_ROUTE_BGP;
4847      new->sub_type = BGP_ROUTE_AGGREGATE;
4848      new->peer = bgp->peer_self;
4849      SET_FLAG (new->flags, BGP_INFO_VALID);
4850      new->attr = bgp_attr_aggregate_intern (bgp, origin, aspath, community, aggregate->as_set);
4851      new->uptime = bgp_clock ();
4852
4853      bgp_info_add (rn, new);
4854      bgp_unlock_node (rn);
4855
4856      /* Process change. */
4857      bgp_process (bgp, rn, afi, safi);
4858    }
4859}
4860
4861void
4862bgp_aggregate_delete (struct bgp *bgp, struct prefix *p, afi_t afi,
4863		      safi_t safi, struct bgp_aggregate *aggregate)
4864{
4865  struct bgp_table *table;
4866  struct bgp_node *top;
4867  struct bgp_node *rn;
4868  struct bgp_info *ri;
4869  unsigned long match;
4870
4871  table = bgp->rib[afi][safi];
4872
4873  if (afi == AFI_IP && p->prefixlen == IPV4_MAX_BITLEN)
4874    return;
4875  if (afi == AFI_IP6 && p->prefixlen == IPV6_MAX_BITLEN)
4876    return;
4877
4878  /* If routes exists below this node, generate aggregate routes. */
4879  top = bgp_node_get (table, p);
4880  for (rn = bgp_node_get (table, p); rn; rn = bgp_route_next_until (rn, top))
4881    if (rn->p.prefixlen > p->prefixlen)
4882      {
4883	match = 0;
4884
4885	for (ri = rn->info; ri; ri = ri->next)
4886	  {
4887	    if (BGP_INFO_HOLDDOWN (ri))
4888	      continue;
4889
4890	    if (ri->sub_type != BGP_ROUTE_AGGREGATE)
4891	      {
4892		if (aggregate->summary_only && ri->extra)
4893		  {
4894		    ri->extra->suppress--;
4895
4896		    if (ri->extra->suppress == 0)
4897		      {
4898			bgp_info_set_flag (rn, ri, BGP_INFO_ATTR_CHANGED);
4899			match++;
4900		      }
4901		  }
4902		aggregate->count--;
4903	      }
4904	  }
4905
4906	/* If this node was suppressed, process the change. */
4907	if (match)
4908	  bgp_process (bgp, rn, afi, safi);
4909      }
4910  bgp_unlock_node (top);
4911
4912  /* Delete aggregate route from BGP table. */
4913  rn = bgp_node_get (table, p);
4914
4915  for (ri = rn->info; ri; ri = ri->next)
4916    if (ri->peer == bgp->peer_self
4917	&& ri->type == ZEBRA_ROUTE_BGP
4918	&& ri->sub_type == BGP_ROUTE_AGGREGATE)
4919      break;
4920
4921  /* Withdraw static BGP route from routing table. */
4922  if (ri)
4923    {
4924      bgp_info_delete (rn, ri);
4925      bgp_process (bgp, rn, afi, safi);
4926    }
4927
4928  /* Unlock bgp_node_lookup. */
4929  bgp_unlock_node (rn);
4930}
4931
4932/* Aggregate route attribute. */
4933#define AGGREGATE_SUMMARY_ONLY 1
4934#define AGGREGATE_AS_SET       1
4935
4936static int
4937bgp_aggregate_unset (struct vty *vty, const char *prefix_str,
4938                     afi_t afi, safi_t safi)
4939{
4940  int ret;
4941  struct prefix p;
4942  struct bgp_node *rn;
4943  struct bgp *bgp;
4944  struct bgp_aggregate *aggregate;
4945
4946  /* Convert string to prefix structure. */
4947  ret = str2prefix (prefix_str, &p);
4948  if (!ret)
4949    {
4950      vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
4951      return CMD_WARNING;
4952    }
4953  apply_mask (&p);
4954
4955  /* Get BGP structure. */
4956  bgp = vty->index;
4957
4958  /* Old configuration check. */
4959  rn = bgp_node_lookup (bgp->aggregate[afi][safi], &p);
4960  if (! rn)
4961    {
4962      vty_out (vty, "%% There is no aggregate-address configuration.%s",
4963               VTY_NEWLINE);
4964      return CMD_WARNING;
4965    }
4966
4967  aggregate = rn->info;
4968  if (aggregate->safi & SAFI_UNICAST)
4969    bgp_aggregate_delete (bgp, &p, afi, SAFI_UNICAST, aggregate);
4970  if (aggregate->safi & SAFI_MULTICAST)
4971    bgp_aggregate_delete (bgp, &p, afi, SAFI_MULTICAST, aggregate);
4972
4973  /* Unlock aggregate address configuration. */
4974  rn->info = NULL;
4975  bgp_aggregate_free (aggregate);
4976  bgp_unlock_node (rn);
4977  bgp_unlock_node (rn);
4978
4979  return CMD_SUCCESS;
4980}
4981
4982static int
4983bgp_aggregate_set (struct vty *vty, const char *prefix_str,
4984                   afi_t afi, safi_t safi,
4985		   u_char summary_only, u_char as_set)
4986{
4987  int ret;
4988  struct prefix p;
4989  struct bgp_node *rn;
4990  struct bgp *bgp;
4991  struct bgp_aggregate *aggregate;
4992
4993  /* Convert string to prefix structure. */
4994  ret = str2prefix (prefix_str, &p);
4995  if (!ret)
4996    {
4997      vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
4998      return CMD_WARNING;
4999    }
5000  apply_mask (&p);
5001
5002  /* Get BGP structure. */
5003  bgp = vty->index;
5004
5005  /* Old configuration check. */
5006  rn = bgp_node_get (bgp->aggregate[afi][safi], &p);
5007
5008  if (rn->info)
5009    {
5010      vty_out (vty, "There is already same aggregate network.%s", VTY_NEWLINE);
5011      /* try to remove the old entry */
5012      ret = bgp_aggregate_unset (vty, prefix_str, afi, safi);
5013      if (ret)
5014        {
5015          vty_out (vty, "Error deleting aggregate.%s", VTY_NEWLINE);
5016	  bgp_unlock_node (rn);
5017	  return CMD_WARNING;
5018        }
5019    }
5020
5021  /* Make aggregate address structure. */
5022  aggregate = bgp_aggregate_new ();
5023  aggregate->summary_only = summary_only;
5024  aggregate->as_set = as_set;
5025  aggregate->safi = safi;
5026  rn->info = aggregate;
5027
5028  /* Aggregate address insert into BGP routing table. */
5029  if (safi & SAFI_UNICAST)
5030    bgp_aggregate_add (bgp, &p, afi, SAFI_UNICAST, aggregate);
5031  if (safi & SAFI_MULTICAST)
5032    bgp_aggregate_add (bgp, &p, afi, SAFI_MULTICAST, aggregate);
5033
5034  return CMD_SUCCESS;
5035}
5036
5037DEFUN (aggregate_address,
5038       aggregate_address_cmd,
5039       "aggregate-address A.B.C.D/M",
5040       "Configure BGP aggregate entries\n"
5041       "Aggregate prefix\n")
5042{
5043  return bgp_aggregate_set (vty, argv[0], AFI_IP, bgp_node_safi (vty), 0, 0);
5044}
5045
5046DEFUN (aggregate_address_mask,
5047       aggregate_address_mask_cmd,
5048       "aggregate-address A.B.C.D A.B.C.D",
5049       "Configure BGP aggregate entries\n"
5050       "Aggregate address\n"
5051       "Aggregate mask\n")
5052{
5053  int ret;
5054  char prefix_str[BUFSIZ];
5055
5056  ret = netmask_str2prefix_str (argv[0], argv[1], prefix_str);
5057
5058  if (! ret)
5059    {
5060      vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
5061      return CMD_WARNING;
5062    }
5063
5064  return bgp_aggregate_set (vty, prefix_str, AFI_IP, bgp_node_safi (vty),
5065			    0, 0);
5066}
5067
5068DEFUN (aggregate_address_summary_only,
5069       aggregate_address_summary_only_cmd,
5070       "aggregate-address A.B.C.D/M summary-only",
5071       "Configure BGP aggregate entries\n"
5072       "Aggregate prefix\n"
5073       "Filter more specific routes from updates\n")
5074{
5075  return bgp_aggregate_set (vty, argv[0], AFI_IP, bgp_node_safi (vty),
5076			    AGGREGATE_SUMMARY_ONLY, 0);
5077}
5078
5079DEFUN (aggregate_address_mask_summary_only,
5080       aggregate_address_mask_summary_only_cmd,
5081       "aggregate-address A.B.C.D A.B.C.D summary-only",
5082       "Configure BGP aggregate entries\n"
5083       "Aggregate address\n"
5084       "Aggregate mask\n"
5085       "Filter more specific routes from updates\n")
5086{
5087  int ret;
5088  char prefix_str[BUFSIZ];
5089
5090  ret = netmask_str2prefix_str (argv[0], argv[1], prefix_str);
5091
5092  if (! ret)
5093    {
5094      vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
5095      return CMD_WARNING;
5096    }
5097
5098  return bgp_aggregate_set (vty, prefix_str, AFI_IP, bgp_node_safi (vty),
5099			    AGGREGATE_SUMMARY_ONLY, 0);
5100}
5101
5102DEFUN (aggregate_address_as_set,
5103       aggregate_address_as_set_cmd,
5104       "aggregate-address A.B.C.D/M as-set",
5105       "Configure BGP aggregate entries\n"
5106       "Aggregate prefix\n"
5107       "Generate AS set path information\n")
5108{
5109  return bgp_aggregate_set (vty, argv[0], AFI_IP, bgp_node_safi (vty),
5110			    0, AGGREGATE_AS_SET);
5111}
5112
5113DEFUN (aggregate_address_mask_as_set,
5114       aggregate_address_mask_as_set_cmd,
5115       "aggregate-address A.B.C.D A.B.C.D as-set",
5116       "Configure BGP aggregate entries\n"
5117       "Aggregate address\n"
5118       "Aggregate mask\n"
5119       "Generate AS set path information\n")
5120{
5121  int ret;
5122  char prefix_str[BUFSIZ];
5123
5124  ret = netmask_str2prefix_str (argv[0], argv[1], prefix_str);
5125
5126  if (! ret)
5127    {
5128      vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
5129      return CMD_WARNING;
5130    }
5131
5132  return bgp_aggregate_set (vty, prefix_str, AFI_IP, bgp_node_safi (vty),
5133			    0, AGGREGATE_AS_SET);
5134}
5135
5136
5137DEFUN (aggregate_address_as_set_summary,
5138       aggregate_address_as_set_summary_cmd,
5139       "aggregate-address A.B.C.D/M as-set summary-only",
5140       "Configure BGP aggregate entries\n"
5141       "Aggregate prefix\n"
5142       "Generate AS set path information\n"
5143       "Filter more specific routes from updates\n")
5144{
5145  return bgp_aggregate_set (vty, argv[0], AFI_IP, bgp_node_safi (vty),
5146			    AGGREGATE_SUMMARY_ONLY, AGGREGATE_AS_SET);
5147}
5148
5149ALIAS (aggregate_address_as_set_summary,
5150       aggregate_address_summary_as_set_cmd,
5151       "aggregate-address A.B.C.D/M summary-only as-set",
5152       "Configure BGP aggregate entries\n"
5153       "Aggregate prefix\n"
5154       "Filter more specific routes from updates\n"
5155       "Generate AS set path information\n")
5156
5157DEFUN (aggregate_address_mask_as_set_summary,
5158       aggregate_address_mask_as_set_summary_cmd,
5159       "aggregate-address A.B.C.D A.B.C.D as-set summary-only",
5160       "Configure BGP aggregate entries\n"
5161       "Aggregate address\n"
5162       "Aggregate mask\n"
5163       "Generate AS set path information\n"
5164       "Filter more specific routes from updates\n")
5165{
5166  int ret;
5167  char prefix_str[BUFSIZ];
5168
5169  ret = netmask_str2prefix_str (argv[0], argv[1], prefix_str);
5170
5171  if (! ret)
5172    {
5173      vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
5174      return CMD_WARNING;
5175    }
5176
5177  return bgp_aggregate_set (vty, prefix_str, AFI_IP, bgp_node_safi (vty),
5178			    AGGREGATE_SUMMARY_ONLY, AGGREGATE_AS_SET);
5179}
5180
5181ALIAS (aggregate_address_mask_as_set_summary,
5182       aggregate_address_mask_summary_as_set_cmd,
5183       "aggregate-address A.B.C.D A.B.C.D summary-only as-set",
5184       "Configure BGP aggregate entries\n"
5185       "Aggregate address\n"
5186       "Aggregate mask\n"
5187       "Filter more specific routes from updates\n"
5188       "Generate AS set path information\n")
5189
5190DEFUN (no_aggregate_address,
5191       no_aggregate_address_cmd,
5192       "no aggregate-address A.B.C.D/M",
5193       NO_STR
5194       "Configure BGP aggregate entries\n"
5195       "Aggregate prefix\n")
5196{
5197  return bgp_aggregate_unset (vty, argv[0], AFI_IP, bgp_node_safi (vty));
5198}
5199
5200ALIAS (no_aggregate_address,
5201       no_aggregate_address_summary_only_cmd,
5202       "no aggregate-address A.B.C.D/M summary-only",
5203       NO_STR
5204       "Configure BGP aggregate entries\n"
5205       "Aggregate prefix\n"
5206       "Filter more specific routes from updates\n")
5207
5208ALIAS (no_aggregate_address,
5209       no_aggregate_address_as_set_cmd,
5210       "no aggregate-address A.B.C.D/M as-set",
5211       NO_STR
5212       "Configure BGP aggregate entries\n"
5213       "Aggregate prefix\n"
5214       "Generate AS set path information\n")
5215
5216ALIAS (no_aggregate_address,
5217       no_aggregate_address_as_set_summary_cmd,
5218       "no aggregate-address A.B.C.D/M as-set summary-only",
5219       NO_STR
5220       "Configure BGP aggregate entries\n"
5221       "Aggregate prefix\n"
5222       "Generate AS set path information\n"
5223       "Filter more specific routes from updates\n")
5224
5225ALIAS (no_aggregate_address,
5226       no_aggregate_address_summary_as_set_cmd,
5227       "no aggregate-address A.B.C.D/M summary-only as-set",
5228       NO_STR
5229       "Configure BGP aggregate entries\n"
5230       "Aggregate prefix\n"
5231       "Filter more specific routes from updates\n"
5232       "Generate AS set path information\n")
5233
5234DEFUN (no_aggregate_address_mask,
5235       no_aggregate_address_mask_cmd,
5236       "no aggregate-address A.B.C.D A.B.C.D",
5237       NO_STR
5238       "Configure BGP aggregate entries\n"
5239       "Aggregate address\n"
5240       "Aggregate mask\n")
5241{
5242  int ret;
5243  char prefix_str[BUFSIZ];
5244
5245  ret = netmask_str2prefix_str (argv[0], argv[1], prefix_str);
5246
5247  if (! ret)
5248    {
5249      vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
5250      return CMD_WARNING;
5251    }
5252
5253  return bgp_aggregate_unset (vty, prefix_str, AFI_IP, bgp_node_safi (vty));
5254}
5255
5256ALIAS (no_aggregate_address_mask,
5257       no_aggregate_address_mask_summary_only_cmd,
5258       "no aggregate-address A.B.C.D A.B.C.D summary-only",
5259       NO_STR
5260       "Configure BGP aggregate entries\n"
5261       "Aggregate address\n"
5262       "Aggregate mask\n"
5263       "Filter more specific routes from updates\n")
5264
5265ALIAS (no_aggregate_address_mask,
5266       no_aggregate_address_mask_as_set_cmd,
5267       "no aggregate-address A.B.C.D A.B.C.D as-set",
5268       NO_STR
5269       "Configure BGP aggregate entries\n"
5270       "Aggregate address\n"
5271       "Aggregate mask\n"
5272       "Generate AS set path information\n")
5273
5274ALIAS (no_aggregate_address_mask,
5275       no_aggregate_address_mask_as_set_summary_cmd,
5276       "no aggregate-address A.B.C.D A.B.C.D as-set summary-only",
5277       NO_STR
5278       "Configure BGP aggregate entries\n"
5279       "Aggregate address\n"
5280       "Aggregate mask\n"
5281       "Generate AS set path information\n"
5282       "Filter more specific routes from updates\n")
5283
5284ALIAS (no_aggregate_address_mask,
5285       no_aggregate_address_mask_summary_as_set_cmd,
5286       "no aggregate-address A.B.C.D A.B.C.D summary-only as-set",
5287       NO_STR
5288       "Configure BGP aggregate entries\n"
5289       "Aggregate address\n"
5290       "Aggregate mask\n"
5291       "Filter more specific routes from updates\n"
5292       "Generate AS set path information\n")
5293
5294#ifdef HAVE_IPV6
5295DEFUN (ipv6_aggregate_address,
5296       ipv6_aggregate_address_cmd,
5297       "aggregate-address X:X::X:X/M",
5298       "Configure BGP aggregate entries\n"
5299       "Aggregate prefix\n")
5300{
5301  return bgp_aggregate_set (vty, argv[0], AFI_IP6, SAFI_UNICAST, 0, 0);
5302}
5303
5304DEFUN (ipv6_aggregate_address_summary_only,
5305       ipv6_aggregate_address_summary_only_cmd,
5306       "aggregate-address X:X::X:X/M summary-only",
5307       "Configure BGP aggregate entries\n"
5308       "Aggregate prefix\n"
5309       "Filter more specific routes from updates\n")
5310{
5311  return bgp_aggregate_set (vty, argv[0], AFI_IP6, SAFI_UNICAST,
5312			    AGGREGATE_SUMMARY_ONLY, 0);
5313}
5314
5315DEFUN (no_ipv6_aggregate_address,
5316       no_ipv6_aggregate_address_cmd,
5317       "no aggregate-address X:X::X:X/M",
5318       NO_STR
5319       "Configure BGP aggregate entries\n"
5320       "Aggregate prefix\n")
5321{
5322  return bgp_aggregate_unset (vty, argv[0], AFI_IP6, SAFI_UNICAST);
5323}
5324
5325DEFUN (no_ipv6_aggregate_address_summary_only,
5326       no_ipv6_aggregate_address_summary_only_cmd,
5327       "no aggregate-address X:X::X:X/M summary-only",
5328       NO_STR
5329       "Configure BGP aggregate entries\n"
5330       "Aggregate prefix\n"
5331       "Filter more specific routes from updates\n")
5332{
5333  return bgp_aggregate_unset (vty, argv[0], AFI_IP6, SAFI_UNICAST);
5334}
5335
5336ALIAS (ipv6_aggregate_address,
5337       old_ipv6_aggregate_address_cmd,
5338       "ipv6 bgp aggregate-address X:X::X:X/M",
5339       IPV6_STR
5340       BGP_STR
5341       "Configure BGP aggregate entries\n"
5342       "Aggregate prefix\n")
5343
5344ALIAS (ipv6_aggregate_address_summary_only,
5345       old_ipv6_aggregate_address_summary_only_cmd,
5346       "ipv6 bgp aggregate-address X:X::X:X/M summary-only",
5347       IPV6_STR
5348       BGP_STR
5349       "Configure BGP aggregate entries\n"
5350       "Aggregate prefix\n"
5351       "Filter more specific routes from updates\n")
5352
5353ALIAS (no_ipv6_aggregate_address,
5354       old_no_ipv6_aggregate_address_cmd,
5355       "no ipv6 bgp aggregate-address X:X::X:X/M",
5356       NO_STR
5357       IPV6_STR
5358       BGP_STR
5359       "Configure BGP aggregate entries\n"
5360       "Aggregate prefix\n")
5361
5362ALIAS (no_ipv6_aggregate_address_summary_only,
5363       old_no_ipv6_aggregate_address_summary_only_cmd,
5364       "no ipv6 bgp aggregate-address X:X::X:X/M summary-only",
5365       NO_STR
5366       IPV6_STR
5367       BGP_STR
5368       "Configure BGP aggregate entries\n"
5369       "Aggregate prefix\n"
5370       "Filter more specific routes from updates\n")
5371#endif /* HAVE_IPV6 */
5372
5373/* Redistribute route treatment. */
5374void
5375bgp_redistribute_add (struct prefix *p, const struct in_addr *nexthop,
5376		      const struct in6_addr *nexthop6,
5377		      u_int32_t metric, u_char type)
5378{
5379  struct bgp *bgp;
5380  struct listnode *node, *nnode;
5381  struct bgp_info *new;
5382  struct bgp_info *bi;
5383  struct bgp_info info;
5384  struct bgp_node *bn;
5385  struct attr attr;
5386  struct attr *new_attr;
5387  afi_t afi;
5388  int ret;
5389
5390  /* Make default attribute. */
5391  bgp_attr_default_set (&attr, BGP_ORIGIN_INCOMPLETE);
5392  if (nexthop)
5393    attr.nexthop = *nexthop;
5394
5395#ifdef HAVE_IPV6
5396  if (nexthop6)
5397    {
5398      struct attr_extra *extra = bgp_attr_extra_get(&attr);
5399      extra->mp_nexthop_global = *nexthop6;
5400      extra->mp_nexthop_len = 16;
5401    }
5402#endif
5403
5404  attr.med = metric;
5405  attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC);
5406
5407  for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
5408    {
5409      afi = family2afi (p->family);
5410
5411      if (bgp->redist[afi][type])
5412	{
5413	  struct attr attr_new;
5414	  struct attr_extra extra_new;
5415
5416	  /* Copy attribute for modification. */
5417	  attr_new.extra = &extra_new;
5418	  bgp_attr_dup (&attr_new, &attr);
5419
5420	  if (bgp->redist_metric_flag[afi][type])
5421	    attr_new.med = bgp->redist_metric[afi][type];
5422
5423	  /* Apply route-map. */
5424	  if (bgp->rmap[afi][type].map)
5425	    {
5426	      info.peer = bgp->peer_self;
5427	      info.attr = &attr_new;
5428
5429              SET_FLAG (bgp->peer_self->rmap_type, PEER_RMAP_TYPE_REDISTRIBUTE);
5430
5431	      ret = route_map_apply (bgp->rmap[afi][type].map, p, RMAP_BGP,
5432				     &info);
5433
5434              bgp->peer_self->rmap_type = 0;
5435
5436	      if (ret == RMAP_DENYMATCH)
5437		{
5438		  /* Free uninterned attribute. */
5439		  bgp_attr_flush (&attr_new);
5440
5441		  /* Unintern original. */
5442		  aspath_unintern (&attr.aspath);
5443		  bgp_attr_extra_free (&attr);
5444		  bgp_redistribute_delete (p, type);
5445		  return;
5446		}
5447	    }
5448
5449          bn = bgp_afi_node_get (bgp->rib[afi][SAFI_UNICAST],
5450                                 afi, SAFI_UNICAST, p, NULL);
5451
5452	  new_attr = bgp_attr_intern (&attr_new);
5453
5454 	  for (bi = bn->info; bi; bi = bi->next)
5455 	    if (bi->peer == bgp->peer_self
5456 		&& bi->sub_type == BGP_ROUTE_REDISTRIBUTE)
5457 	      break;
5458
5459 	  if (bi)
5460 	    {
5461 	      if (attrhash_cmp (bi->attr, new_attr) &&
5462		  !CHECK_FLAG(bi->flags, BGP_INFO_REMOVED))
5463 		{
5464 		  bgp_attr_unintern (&new_attr);
5465 		  aspath_unintern (&attr.aspath);
5466 		  bgp_attr_extra_free (&attr);
5467 		  bgp_unlock_node (bn);
5468 		  return;
5469 		}
5470 	      else
5471 		{
5472 		  /* The attribute is changed. */
5473 		  bgp_info_set_flag (bn, bi, BGP_INFO_ATTR_CHANGED);
5474
5475 		  /* Rewrite BGP route information. */
5476		  if (CHECK_FLAG(bi->flags, BGP_INFO_REMOVED))
5477		    bgp_info_restore(bn, bi);
5478		  else
5479		    bgp_aggregate_decrement (bgp, p, bi, afi, SAFI_UNICAST);
5480 		  bgp_attr_unintern (&bi->attr);
5481 		  bi->attr = new_attr;
5482 		  bi->uptime = bgp_clock ();
5483
5484 		  /* Process change. */
5485 		  bgp_aggregate_increment (bgp, p, bi, afi, SAFI_UNICAST);
5486 		  bgp_process (bgp, bn, afi, SAFI_UNICAST);
5487 		  bgp_unlock_node (bn);
5488 		  aspath_unintern (&attr.aspath);
5489 		  bgp_attr_extra_free (&attr);
5490 		  return;
5491 		}
5492 	    }
5493
5494	  new = bgp_info_new ();
5495	  new->type = type;
5496	  new->sub_type = BGP_ROUTE_REDISTRIBUTE;
5497	  new->peer = bgp->peer_self;
5498	  SET_FLAG (new->flags, BGP_INFO_VALID);
5499	  new->attr = new_attr;
5500	  new->uptime = bgp_clock ();
5501
5502	  bgp_aggregate_increment (bgp, p, new, afi, SAFI_UNICAST);
5503	  bgp_info_add (bn, new);
5504	  bgp_unlock_node (bn);
5505	  bgp_process (bgp, bn, afi, SAFI_UNICAST);
5506	}
5507    }
5508
5509  /* Unintern original. */
5510  aspath_unintern (&attr.aspath);
5511  bgp_attr_extra_free (&attr);
5512}
5513
5514void
5515bgp_redistribute_delete (struct prefix *p, u_char type)
5516{
5517  struct bgp *bgp;
5518  struct listnode *node, *nnode;
5519  afi_t afi;
5520  struct bgp_node *rn;
5521  struct bgp_info *ri;
5522
5523  for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
5524    {
5525      afi = family2afi (p->family);
5526
5527      if (bgp->redist[afi][type])
5528	{
5529         rn = bgp_afi_node_get (bgp->rib[afi][SAFI_UNICAST], afi, SAFI_UNICAST, p, NULL);
5530
5531	  for (ri = rn->info; ri; ri = ri->next)
5532	    if (ri->peer == bgp->peer_self
5533		&& ri->type == type)
5534	      break;
5535
5536	  if (ri)
5537	    {
5538	      bgp_aggregate_decrement (bgp, p, ri, afi, SAFI_UNICAST);
5539	      bgp_info_delete (rn, ri);
5540	      bgp_process (bgp, rn, afi, SAFI_UNICAST);
5541	    }
5542	  bgp_unlock_node (rn);
5543	}
5544    }
5545}
5546
5547/* Withdraw specified route type's route. */
5548void
5549bgp_redistribute_withdraw (struct bgp *bgp, afi_t afi, int type)
5550{
5551  struct bgp_node *rn;
5552  struct bgp_info *ri;
5553  struct bgp_table *table;
5554
5555  table = bgp->rib[afi][SAFI_UNICAST];
5556
5557  for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
5558    {
5559      for (ri = rn->info; ri; ri = ri->next)
5560	if (ri->peer == bgp->peer_self
5561	    && ri->type == type)
5562	  break;
5563
5564      if (ri)
5565	{
5566	  bgp_aggregate_decrement (bgp, &rn->p, ri, afi, SAFI_UNICAST);
5567	  bgp_info_delete (rn, ri);
5568	  bgp_process (bgp, rn, afi, SAFI_UNICAST);
5569	}
5570    }
5571}
5572
5573/* Static function to display route. */
5574static void
5575route_vty_out_route (struct prefix *p, struct vty *vty)
5576{
5577  int len;
5578  u_int32_t destination;
5579  char buf[BUFSIZ];
5580
5581  if (p->family == AF_INET)
5582    {
5583      len = vty_out (vty, "%s", inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ));
5584      destination = ntohl (p->u.prefix4.s_addr);
5585
5586      if ((IN_CLASSC (destination) && p->prefixlen == 24)
5587	  || (IN_CLASSB (destination) && p->prefixlen == 16)
5588	  || (IN_CLASSA (destination) && p->prefixlen == 8)
5589	  || p->u.prefix4.s_addr == 0)
5590	{
5591	  /* When mask is natural, mask is not displayed. */
5592	}
5593      else
5594	len += vty_out (vty, "/%d", p->prefixlen);
5595    }
5596  else
5597    len = vty_out (vty, "%s/%d", inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
5598		   p->prefixlen);
5599
5600  len = 17 - len;
5601  if (len < 1)
5602    vty_out (vty, "%s%*s", VTY_NEWLINE, 20, " ");
5603  else
5604    vty_out (vty, "%*s", len, " ");
5605}
5606
5607enum bgp_display_type
5608{
5609  normal_list,
5610};
5611
5612/* Print the short form route status for a bgp_info */
5613static void
5614route_vty_short_status_out (struct vty *vty, struct bgp_info *binfo)
5615{
5616 /* Route status display. */
5617  if (CHECK_FLAG (binfo->flags, BGP_INFO_REMOVED))
5618    vty_out (vty, "R");
5619  else if (CHECK_FLAG (binfo->flags, BGP_INFO_STALE))
5620    vty_out (vty, "S");
5621  else if (binfo->extra && binfo->extra->suppress)
5622    vty_out (vty, "s");
5623  else if (! CHECK_FLAG (binfo->flags, BGP_INFO_HISTORY))
5624    vty_out (vty, "*");
5625  else
5626    vty_out (vty, " ");
5627
5628  /* Selected */
5629  if (CHECK_FLAG (binfo->flags, BGP_INFO_HISTORY))
5630    vty_out (vty, "h");
5631  else if (CHECK_FLAG (binfo->flags, BGP_INFO_DAMPED))
5632    vty_out (vty, "d");
5633  else if (CHECK_FLAG (binfo->flags, BGP_INFO_SELECTED))
5634    vty_out (vty, ">");
5635  else if (CHECK_FLAG (binfo->flags, BGP_INFO_MULTIPATH))
5636    vty_out (vty, "=");
5637  else
5638    vty_out (vty, " ");
5639
5640  /* Internal route. */
5641    if ((binfo->peer->as) && (binfo->peer->as == binfo->peer->local_as))
5642      vty_out (vty, "i");
5643    else
5644      vty_out (vty, " ");
5645}
5646
5647/* called from terminal list command */
5648void
5649route_vty_out (struct vty *vty, struct prefix *p,
5650	       struct bgp_info *binfo, int display, safi_t safi)
5651{
5652  struct attr *attr;
5653
5654  /* short status lead text */
5655  route_vty_short_status_out (vty, binfo);
5656
5657  /* print prefix and mask */
5658  if (! display)
5659    route_vty_out_route (p, vty);
5660  else
5661    vty_out (vty, "%*s", 17, " ");
5662
5663  /* Print attribute */
5664  attr = binfo->attr;
5665  if (attr)
5666    {
5667      if (p->family == AF_INET)
5668	{
5669	  if (safi == SAFI_MPLS_VPN)
5670	    vty_out (vty, "%-16s",
5671                     inet_ntoa (attr->extra->mp_nexthop_global_in));
5672	  else
5673	    vty_out (vty, "%-16s", inet_ntoa (attr->nexthop));
5674	}
5675#ifdef HAVE_IPV6
5676      else if (p->family == AF_INET6)
5677	{
5678	  int len;
5679	  char buf[BUFSIZ];
5680
5681	  len = vty_out (vty, "%s",
5682			 inet_ntop (AF_INET6, &attr->extra->mp_nexthop_global,
5683			 buf, BUFSIZ));
5684	  len = 16 - len;
5685	  if (len < 1)
5686	    vty_out (vty, "%s%*s", VTY_NEWLINE, 36, " ");
5687	  else
5688	    vty_out (vty, "%*s", len, " ");
5689	}
5690#endif /* HAVE_IPV6 */
5691
5692      if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC))
5693	vty_out (vty, "%10u", attr->med);
5694      else
5695	vty_out (vty, "          ");
5696
5697      if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF))
5698	vty_out (vty, "%7u", attr->local_pref);
5699      else
5700	vty_out (vty, "       ");
5701
5702      vty_out (vty, "%7u ", (attr->extra ? attr->extra->weight : 0));
5703
5704      /* Print aspath */
5705      if (attr->aspath)
5706        aspath_print_vty (vty, "%s", attr->aspath, " ");
5707
5708      /* Print origin */
5709      vty_out (vty, "%s", bgp_origin_str[attr->origin]);
5710    }
5711  vty_out (vty, "%s", VTY_NEWLINE);
5712}
5713
5714/* called from terminal list command */
5715void
5716route_vty_out_tmp (struct vty *vty, struct prefix *p,
5717		   struct attr *attr, safi_t safi)
5718{
5719  /* Route status display. */
5720  vty_out (vty, "*");
5721  vty_out (vty, ">");
5722  vty_out (vty, " ");
5723
5724  /* print prefix and mask */
5725  route_vty_out_route (p, vty);
5726
5727  /* Print attribute */
5728  if (attr)
5729    {
5730      if (p->family == AF_INET)
5731	{
5732	  if (safi == SAFI_MPLS_VPN)
5733	    vty_out (vty, "%-16s",
5734                     inet_ntoa (attr->extra->mp_nexthop_global_in));
5735	  else
5736	    vty_out (vty, "%-16s", inet_ntoa (attr->nexthop));
5737	}
5738#ifdef HAVE_IPV6
5739      else if (p->family == AF_INET6)
5740        {
5741          int len;
5742          char buf[BUFSIZ];
5743
5744          assert (attr->extra);
5745
5746          len = vty_out (vty, "%s",
5747                         inet_ntop (AF_INET6, &attr->extra->mp_nexthop_global,
5748                         buf, BUFSIZ));
5749          len = 16 - len;
5750          if (len < 1)
5751            vty_out (vty, "%s%*s", VTY_NEWLINE, 36, " ");
5752          else
5753            vty_out (vty, "%*s", len, " ");
5754        }
5755#endif /* HAVE_IPV6 */
5756
5757      if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC))
5758	vty_out (vty, "%10u", attr->med);
5759      else
5760	vty_out (vty, "          ");
5761
5762      if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF))
5763	vty_out (vty, "%7u", attr->local_pref);
5764      else
5765	vty_out (vty, "       ");
5766
5767      vty_out (vty, "%7u ", (attr->extra ? attr->extra->weight : 0));
5768
5769      /* Print aspath */
5770      if (attr->aspath)
5771        aspath_print_vty (vty, "%s", attr->aspath, " ");
5772
5773      /* Print origin */
5774      vty_out (vty, "%s", bgp_origin_str[attr->origin]);
5775    }
5776
5777  vty_out (vty, "%s", VTY_NEWLINE);
5778}
5779
5780void
5781route_vty_out_tag (struct vty *vty, struct prefix *p,
5782		   struct bgp_info *binfo, int display, safi_t safi)
5783{
5784  struct attr *attr;
5785  u_int32_t label = 0;
5786
5787  if (!binfo->extra)
5788    return;
5789
5790  /* short status lead text */
5791  route_vty_short_status_out (vty, binfo);
5792
5793  /* print prefix and mask */
5794  if (! display)
5795    route_vty_out_route (p, vty);
5796  else
5797    vty_out (vty, "%*s", 17, " ");
5798
5799  /* Print attribute */
5800  attr = binfo->attr;
5801  if (attr)
5802    {
5803      if (p->family == AF_INET)
5804	{
5805	  if (safi == SAFI_MPLS_VPN)
5806	    vty_out (vty, "%-16s",
5807                     inet_ntoa (attr->extra->mp_nexthop_global_in));
5808	  else
5809	    vty_out (vty, "%-16s", inet_ntoa (attr->nexthop));
5810	}
5811#ifdef HAVE_IPV6
5812      else if (p->family == AF_INET6)
5813	{
5814	  assert (attr->extra);
5815	  char buf[BUFSIZ];
5816	  char buf1[BUFSIZ];
5817	  if (attr->extra->mp_nexthop_len == 16)
5818	    vty_out (vty, "%s",
5819		     inet_ntop (AF_INET6, &attr->extra->mp_nexthop_global,
5820                     buf, BUFSIZ));
5821	  else if (attr->extra->mp_nexthop_len == 32)
5822	    vty_out (vty, "%s(%s)",
5823		     inet_ntop (AF_INET6, &attr->extra->mp_nexthop_global,
5824		                buf, BUFSIZ),
5825		     inet_ntop (AF_INET6, &attr->extra->mp_nexthop_local,
5826		                buf1, BUFSIZ));
5827
5828	}
5829#endif /* HAVE_IPV6 */
5830    }
5831
5832  label = decode_label (binfo->extra->tag);
5833
5834  vty_out (vty, "notag/%d", label);
5835
5836  vty_out (vty, "%s", VTY_NEWLINE);
5837}
5838
5839/* dampening route */
5840static void
5841damp_route_vty_out (struct vty *vty, struct prefix *p,
5842		    struct bgp_info *binfo, int display, safi_t safi)
5843{
5844  struct attr *attr;
5845  int len;
5846  char timebuf[BGP_UPTIME_LEN];
5847
5848  /* short status lead text */
5849  route_vty_short_status_out (vty, binfo);
5850
5851  /* print prefix and mask */
5852  if (! display)
5853    route_vty_out_route (p, vty);
5854  else
5855    vty_out (vty, "%*s", 17, " ");
5856
5857  len = vty_out (vty, "%s", binfo->peer->host);
5858  len = 17 - len;
5859  if (len < 1)
5860    vty_out (vty, "%s%*s", VTY_NEWLINE, 34, " ");
5861  else
5862    vty_out (vty, "%*s", len, " ");
5863
5864  vty_out (vty, "%s ", bgp_damp_reuse_time_vty (vty, binfo, timebuf, BGP_UPTIME_LEN));
5865
5866  /* Print attribute */
5867  attr = binfo->attr;
5868  if (attr)
5869    {
5870      /* Print aspath */
5871      if (attr->aspath)
5872	aspath_print_vty (vty, "%s", attr->aspath, " ");
5873
5874      /* Print origin */
5875      vty_out (vty, "%s", bgp_origin_str[attr->origin]);
5876    }
5877  vty_out (vty, "%s", VTY_NEWLINE);
5878}
5879
5880/* flap route */
5881static void
5882flap_route_vty_out (struct vty *vty, struct prefix *p,
5883		    struct bgp_info *binfo, int display, safi_t safi)
5884{
5885  struct attr *attr;
5886  struct bgp_damp_info *bdi;
5887  char timebuf[BGP_UPTIME_LEN];
5888  int len;
5889
5890  if (!binfo->extra)
5891    return;
5892
5893  bdi = binfo->extra->damp_info;
5894
5895  /* short status lead text */
5896  route_vty_short_status_out (vty, binfo);
5897
5898  /* print prefix and mask */
5899  if (! display)
5900    route_vty_out_route (p, vty);
5901  else
5902    vty_out (vty, "%*s", 17, " ");
5903
5904  len = vty_out (vty, "%s", binfo->peer->host);
5905  len = 16 - len;
5906  if (len < 1)
5907    vty_out (vty, "%s%*s", VTY_NEWLINE, 33, " ");
5908  else
5909    vty_out (vty, "%*s", len, " ");
5910
5911  len = vty_out (vty, "%d", bdi->flap);
5912  len = 5 - len;
5913  if (len < 1)
5914    vty_out (vty, " ");
5915  else
5916    vty_out (vty, "%*s ", len, " ");
5917
5918  vty_out (vty, "%s ", peer_uptime (bdi->start_time,
5919	   timebuf, BGP_UPTIME_LEN));
5920
5921  if (CHECK_FLAG (binfo->flags, BGP_INFO_DAMPED)
5922      && ! CHECK_FLAG (binfo->flags, BGP_INFO_HISTORY))
5923    vty_out (vty, "%s ", bgp_damp_reuse_time_vty (vty, binfo, timebuf, BGP_UPTIME_LEN));
5924  else
5925    vty_out (vty, "%*s ", 8, " ");
5926
5927  /* Print attribute */
5928  attr = binfo->attr;
5929  if (attr)
5930    {
5931      /* Print aspath */
5932      if (attr->aspath)
5933	aspath_print_vty (vty, "%s", attr->aspath, " ");
5934
5935      /* Print origin */
5936      vty_out (vty, "%s", bgp_origin_str[attr->origin]);
5937    }
5938  vty_out (vty, "%s", VTY_NEWLINE);
5939}
5940
5941static void
5942route_vty_out_detail (struct vty *vty, struct bgp *bgp, struct prefix *p,
5943		      struct bgp_info *binfo, afi_t afi, safi_t safi)
5944{
5945  char buf[INET6_ADDRSTRLEN];
5946  char buf1[BUFSIZ];
5947  struct attr *attr;
5948  int sockunion_vty_out (struct vty *, union sockunion *);
5949#ifdef HAVE_CLOCK_MONOTONIC
5950  time_t tbuf;
5951#endif
5952
5953  attr = binfo->attr;
5954
5955  if (attr)
5956    {
5957      /* Line1 display AS-path, Aggregator */
5958      if (attr->aspath)
5959	{
5960	  vty_out (vty, "  ");
5961	  if (aspath_count_hops (attr->aspath) == 0)
5962	    vty_out (vty, "Local");
5963	  else
5964	    aspath_print_vty (vty, "%s", attr->aspath, "");
5965	}
5966
5967      if (CHECK_FLAG (binfo->flags, BGP_INFO_REMOVED))
5968        vty_out (vty, ", (removed)");
5969      if (CHECK_FLAG (binfo->flags, BGP_INFO_STALE))
5970	vty_out (vty, ", (stale)");
5971      if (CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR)))
5972	vty_out (vty, ", (aggregated by %u %s)",
5973	         attr->extra->aggregator_as,
5974		 inet_ntoa (attr->extra->aggregator_addr));
5975      if (CHECK_FLAG (binfo->peer->af_flags[afi][safi], PEER_FLAG_REFLECTOR_CLIENT))
5976	vty_out (vty, ", (Received from a RR-client)");
5977      if (CHECK_FLAG (binfo->peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT))
5978	vty_out (vty, ", (Received from a RS-client)");
5979      if (CHECK_FLAG (binfo->flags, BGP_INFO_HISTORY))
5980	vty_out (vty, ", (history entry)");
5981      else if (CHECK_FLAG (binfo->flags, BGP_INFO_DAMPED))
5982	vty_out (vty, ", (suppressed due to dampening)");
5983      vty_out (vty, "%s", VTY_NEWLINE);
5984
5985      /* Line2 display Next-hop, Neighbor, Router-id */
5986      if (p->family == AF_INET)
5987	{
5988	  vty_out (vty, "    %s", safi == SAFI_MPLS_VPN ?
5989		   inet_ntoa (attr->extra->mp_nexthop_global_in) :
5990		   inet_ntoa (attr->nexthop));
5991	}
5992#ifdef HAVE_IPV6
5993      else
5994	{
5995	  assert (attr->extra);
5996	  vty_out (vty, "    %s",
5997		   inet_ntop (AF_INET6, &attr->extra->mp_nexthop_global,
5998			      buf, INET6_ADDRSTRLEN));
5999	}
6000#endif /* HAVE_IPV6 */
6001
6002      if (binfo->peer == bgp->peer_self)
6003	{
6004	  vty_out (vty, " from %s ",
6005		   p->family == AF_INET ? "0.0.0.0" : "::");
6006	  vty_out (vty, "(%s)", inet_ntoa(bgp->router_id));
6007	}
6008      else
6009	{
6010	  if (! CHECK_FLAG (binfo->flags, BGP_INFO_VALID))
6011	    vty_out (vty, " (inaccessible)");
6012	  else if (binfo->extra && binfo->extra->igpmetric)
6013	    vty_out (vty, " (metric %u)", binfo->extra->igpmetric);
6014	  vty_out (vty, " from %s", sockunion2str (&binfo->peer->su, buf, SU_ADDRSTRLEN));
6015	  if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
6016	    vty_out (vty, " (%s)", inet_ntoa (attr->extra->originator_id));
6017	  else
6018	    vty_out (vty, " (%s)", inet_ntop (AF_INET, &binfo->peer->remote_id, buf1, BUFSIZ));
6019	}
6020      vty_out (vty, "%s", VTY_NEWLINE);
6021
6022#ifdef HAVE_IPV6
6023      /* display nexthop local */
6024      if (attr->extra && attr->extra->mp_nexthop_len == 32)
6025	{
6026	  vty_out (vty, "    (%s)%s",
6027		   inet_ntop (AF_INET6, &attr->extra->mp_nexthop_local,
6028			      buf, INET6_ADDRSTRLEN),
6029		   VTY_NEWLINE);
6030	}
6031#endif /* HAVE_IPV6 */
6032
6033      /* Line 3 display Origin, Med, Locpref, Weight, valid, Int/Ext/Local, Atomic, best */
6034      vty_out (vty, "      Origin %s", bgp_origin_long_str[attr->origin]);
6035
6036      if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC))
6037	vty_out (vty, ", metric %u", attr->med);
6038
6039      if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
6040	vty_out (vty, ", localpref %u", attr->local_pref);
6041      else
6042	vty_out (vty, ", localpref %u", bgp->default_local_pref);
6043
6044      if (attr->extra && attr->extra->weight != 0)
6045	vty_out (vty, ", weight %u", attr->extra->weight);
6046
6047      if (! CHECK_FLAG (binfo->flags, BGP_INFO_HISTORY))
6048	vty_out (vty, ", valid");
6049
6050      if (binfo->peer != bgp->peer_self)
6051	{
6052	  if (binfo->peer->as == binfo->peer->local_as)
6053	    vty_out (vty, ", internal");
6054	  else
6055	    vty_out (vty, ", %s",
6056		     (bgp_confederation_peers_check(bgp, binfo->peer->as) ? "confed-external" : "external"));
6057	}
6058      else if (binfo->sub_type == BGP_ROUTE_AGGREGATE)
6059	vty_out (vty, ", aggregated, local");
6060      else if (binfo->type != ZEBRA_ROUTE_BGP)
6061	vty_out (vty, ", sourced");
6062      else
6063	vty_out (vty, ", sourced, local");
6064
6065      if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE))
6066	vty_out (vty, ", atomic-aggregate");
6067
6068      if (CHECK_FLAG (binfo->flags, BGP_INFO_MULTIPATH) ||
6069	  (CHECK_FLAG (binfo->flags, BGP_INFO_SELECTED) &&
6070	   bgp_info_mpath_count (binfo)))
6071	vty_out (vty, ", multipath");
6072
6073      if (CHECK_FLAG (binfo->flags, BGP_INFO_SELECTED))
6074	vty_out (vty, ", best");
6075
6076      vty_out (vty, "%s", VTY_NEWLINE);
6077
6078      /* Line 4 display Community */
6079      if (attr->community)
6080	vty_out (vty, "      Community: %s%s", attr->community->str,
6081		 VTY_NEWLINE);
6082
6083      /* Line 5 display Extended-community */
6084      if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES))
6085	vty_out (vty, "      Extended Community: %s%s",
6086	         attr->extra->ecommunity->str, VTY_NEWLINE);
6087
6088      /* Line 6 display Originator, Cluster-id */
6089      if ((attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID)) ||
6090	  (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST)))
6091	{
6092	  assert (attr->extra);
6093	  if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
6094	    vty_out (vty, "      Originator: %s",
6095	             inet_ntoa (attr->extra->originator_id));
6096
6097	  if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST))
6098	    {
6099	      int i;
6100	      vty_out (vty, ", Cluster list: ");
6101	      for (i = 0; i < attr->extra->cluster->length / 4; i++)
6102		vty_out (vty, "%s ",
6103		         inet_ntoa (attr->extra->cluster->list[i]));
6104	    }
6105	  vty_out (vty, "%s", VTY_NEWLINE);
6106	}
6107
6108      if (binfo->extra && binfo->extra->damp_info)
6109	bgp_damp_info_vty (vty, binfo);
6110
6111      /* Line 7 display Uptime */
6112#ifdef HAVE_CLOCK_MONOTONIC
6113      tbuf = time(NULL) - (bgp_clock() - binfo->uptime);
6114      vty_out (vty, "      Last update: %s", ctime(&tbuf));
6115#else
6116      vty_out (vty, "      Last update: %s", ctime(&binfo->uptime));
6117#endif /* HAVE_CLOCK_MONOTONIC */
6118    }
6119  vty_out (vty, "%s", VTY_NEWLINE);
6120}
6121
6122#define BGP_SHOW_SCODE_HEADER "Status codes: s suppressed, d damped, "\
6123			      "h history, * valid, > best, = multipath,%s"\
6124		"              i internal, r RIB-failure, S Stale, R Removed%s"
6125#define BGP_SHOW_OCODE_HEADER "Origin codes: i - IGP, e - EGP, ? - incomplete%s%s"
6126#define BGP_SHOW_HEADER "   Network          Next Hop            Metric LocPrf Weight Path%s"
6127#define BGP_SHOW_DAMP_HEADER "   Network          From             Reuse    Path%s"
6128#define BGP_SHOW_FLAP_HEADER "   Network          From            Flaps Duration Reuse    Path%s"
6129
6130enum bgp_show_type
6131{
6132  bgp_show_type_normal,
6133  bgp_show_type_regexp,
6134  bgp_show_type_prefix_list,
6135  bgp_show_type_filter_list,
6136  bgp_show_type_route_map,
6137  bgp_show_type_neighbor,
6138  bgp_show_type_cidr_only,
6139  bgp_show_type_prefix_longer,
6140  bgp_show_type_community_all,
6141  bgp_show_type_community,
6142  bgp_show_type_community_exact,
6143  bgp_show_type_community_list,
6144  bgp_show_type_community_list_exact,
6145  bgp_show_type_flap_statistics,
6146  bgp_show_type_flap_address,
6147  bgp_show_type_flap_prefix,
6148  bgp_show_type_flap_cidr_only,
6149  bgp_show_type_flap_regexp,
6150  bgp_show_type_flap_filter_list,
6151  bgp_show_type_flap_prefix_list,
6152  bgp_show_type_flap_prefix_longer,
6153  bgp_show_type_flap_route_map,
6154  bgp_show_type_flap_neighbor,
6155  bgp_show_type_dampend_paths,
6156  bgp_show_type_damp_neighbor
6157};
6158
6159static int
6160bgp_show_table (struct vty *vty, struct bgp_table *table, struct in_addr *router_id,
6161	  enum bgp_show_type type, void *output_arg)
6162{
6163  struct bgp_info *ri;
6164  struct bgp_node *rn;
6165  int header = 1;
6166  int display;
6167  unsigned long output_count;
6168
6169  /* This is first entry point, so reset total line. */
6170  output_count = 0;
6171
6172  /* Start processing of routes. */
6173  for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
6174    if (rn->info != NULL)
6175      {
6176	display = 0;
6177
6178	for (ri = rn->info; ri; ri = ri->next)
6179	  {
6180	    if (type == bgp_show_type_flap_statistics
6181		|| type == bgp_show_type_flap_address
6182		|| type == bgp_show_type_flap_prefix
6183		|| type == bgp_show_type_flap_cidr_only
6184		|| type == bgp_show_type_flap_regexp
6185		|| type == bgp_show_type_flap_filter_list
6186		|| type == bgp_show_type_flap_prefix_list
6187		|| type == bgp_show_type_flap_prefix_longer
6188		|| type == bgp_show_type_flap_route_map
6189		|| type == bgp_show_type_flap_neighbor
6190		|| type == bgp_show_type_dampend_paths
6191		|| type == bgp_show_type_damp_neighbor)
6192	      {
6193		if (!(ri->extra && ri->extra->damp_info))
6194		  continue;
6195	      }
6196	    if (type == bgp_show_type_regexp
6197		|| type == bgp_show_type_flap_regexp)
6198	      {
6199		regex_t *regex = output_arg;
6200
6201		if (bgp_regexec (regex, ri->attr->aspath) == REG_NOMATCH)
6202		  continue;
6203	      }
6204	    if (type == bgp_show_type_prefix_list
6205		|| type == bgp_show_type_flap_prefix_list)
6206	      {
6207		struct prefix_list *plist = output_arg;
6208
6209		if (prefix_list_apply (plist, &rn->p) != PREFIX_PERMIT)
6210		  continue;
6211	      }
6212	    if (type == bgp_show_type_filter_list
6213		|| type == bgp_show_type_flap_filter_list)
6214	      {
6215		struct as_list *as_list = output_arg;
6216
6217		if (as_list_apply (as_list, ri->attr->aspath) != AS_FILTER_PERMIT)
6218		  continue;
6219	      }
6220	    if (type == bgp_show_type_route_map
6221		|| type == bgp_show_type_flap_route_map)
6222	      {
6223		struct route_map *rmap = output_arg;
6224		struct bgp_info binfo;
6225		struct attr dummy_attr;
6226		struct attr_extra dummy_extra;
6227		int ret;
6228
6229		dummy_attr.extra = &dummy_extra;
6230		bgp_attr_dup (&dummy_attr, ri->attr);
6231
6232		binfo.peer = ri->peer;
6233		binfo.attr = &dummy_attr;
6234
6235		ret = route_map_apply (rmap, &rn->p, RMAP_BGP, &binfo);
6236		if (ret == RMAP_DENYMATCH)
6237		  continue;
6238	      }
6239	    if (type == bgp_show_type_neighbor
6240		|| type == bgp_show_type_flap_neighbor
6241		|| type == bgp_show_type_damp_neighbor)
6242	      {
6243		union sockunion *su = output_arg;
6244
6245		if (ri->peer->su_remote == NULL || ! sockunion_same(ri->peer->su_remote, su))
6246		  continue;
6247	      }
6248	    if (type == bgp_show_type_cidr_only
6249		|| type == bgp_show_type_flap_cidr_only)
6250	      {
6251		u_int32_t destination;
6252
6253		destination = ntohl (rn->p.u.prefix4.s_addr);
6254		if (IN_CLASSC (destination) && rn->p.prefixlen == 24)
6255		  continue;
6256		if (IN_CLASSB (destination) && rn->p.prefixlen == 16)
6257		  continue;
6258		if (IN_CLASSA (destination) && rn->p.prefixlen == 8)
6259		  continue;
6260	      }
6261	    if (type == bgp_show_type_prefix_longer
6262		|| type == bgp_show_type_flap_prefix_longer)
6263	      {
6264		struct prefix *p = output_arg;
6265
6266		if (! prefix_match (p, &rn->p))
6267		  continue;
6268	      }
6269	    if (type == bgp_show_type_community_all)
6270	      {
6271		if (! ri->attr->community)
6272		  continue;
6273	      }
6274	    if (type == bgp_show_type_community)
6275	      {
6276		struct community *com = output_arg;
6277
6278		if (! ri->attr->community ||
6279		    ! community_match (ri->attr->community, com))
6280		  continue;
6281	      }
6282	    if (type == bgp_show_type_community_exact)
6283	      {
6284		struct community *com = output_arg;
6285
6286		if (! ri->attr->community ||
6287		    ! community_cmp (ri->attr->community, com))
6288		  continue;
6289	      }
6290	    if (type == bgp_show_type_community_list)
6291	      {
6292		struct community_list *list = output_arg;
6293
6294		if (! community_list_match (ri->attr->community, list))
6295		  continue;
6296	      }
6297	    if (type == bgp_show_type_community_list_exact)
6298	      {
6299		struct community_list *list = output_arg;
6300
6301		if (! community_list_exact_match (ri->attr->community, list))
6302		  continue;
6303	      }
6304	    if (type == bgp_show_type_flap_address
6305		|| type == bgp_show_type_flap_prefix)
6306	      {
6307		struct prefix *p = output_arg;
6308
6309		if (! prefix_match (&rn->p, p))
6310		  continue;
6311
6312		if (type == bgp_show_type_flap_prefix)
6313		  if (p->prefixlen != rn->p.prefixlen)
6314		    continue;
6315	      }
6316	    if (type == bgp_show_type_dampend_paths
6317		|| type == bgp_show_type_damp_neighbor)
6318	      {
6319		if (! CHECK_FLAG (ri->flags, BGP_INFO_DAMPED)
6320		    || CHECK_FLAG (ri->flags, BGP_INFO_HISTORY))
6321		  continue;
6322	      }
6323
6324	    if (header)
6325	      {
6326		vty_out (vty, "BGP table version is 0, local router ID is %s%s", inet_ntoa (*router_id), VTY_NEWLINE);
6327		vty_out (vty, BGP_SHOW_SCODE_HEADER, VTY_NEWLINE, VTY_NEWLINE);
6328		vty_out (vty, BGP_SHOW_OCODE_HEADER, VTY_NEWLINE, VTY_NEWLINE);
6329		if (type == bgp_show_type_dampend_paths
6330		    || type == bgp_show_type_damp_neighbor)
6331		  vty_out (vty, BGP_SHOW_DAMP_HEADER, VTY_NEWLINE);
6332		else if (type == bgp_show_type_flap_statistics
6333			 || type == bgp_show_type_flap_address
6334			 || type == bgp_show_type_flap_prefix
6335			 || type == bgp_show_type_flap_cidr_only
6336			 || type == bgp_show_type_flap_regexp
6337			 || type == bgp_show_type_flap_filter_list
6338			 || type == bgp_show_type_flap_prefix_list
6339			 || type == bgp_show_type_flap_prefix_longer
6340			 || type == bgp_show_type_flap_route_map
6341			 || type == bgp_show_type_flap_neighbor)
6342		  vty_out (vty, BGP_SHOW_FLAP_HEADER, VTY_NEWLINE);
6343		else
6344		  vty_out (vty, BGP_SHOW_HEADER, VTY_NEWLINE);
6345		header = 0;
6346	      }
6347
6348	    if (type == bgp_show_type_dampend_paths
6349		|| type == bgp_show_type_damp_neighbor)
6350	      damp_route_vty_out (vty, &rn->p, ri, display, SAFI_UNICAST);
6351	    else if (type == bgp_show_type_flap_statistics
6352		     || type == bgp_show_type_flap_address
6353		     || type == bgp_show_type_flap_prefix
6354		     || type == bgp_show_type_flap_cidr_only
6355		     || type == bgp_show_type_flap_regexp
6356		     || type == bgp_show_type_flap_filter_list
6357		     || type == bgp_show_type_flap_prefix_list
6358		     || type == bgp_show_type_flap_prefix_longer
6359		     || type == bgp_show_type_flap_route_map
6360		     || type == bgp_show_type_flap_neighbor)
6361	      flap_route_vty_out (vty, &rn->p, ri, display, SAFI_UNICAST);
6362	    else
6363	      route_vty_out (vty, &rn->p, ri, display, SAFI_UNICAST);
6364	    display++;
6365	  }
6366	if (display)
6367	  output_count++;
6368      }
6369
6370  /* No route is displayed */
6371  if (output_count == 0)
6372    {
6373      if (type == bgp_show_type_normal)
6374	vty_out (vty, "No BGP network exists%s", VTY_NEWLINE);
6375    }
6376  else
6377    vty_out (vty, "%sTotal number of prefixes %ld%s",
6378	     VTY_NEWLINE, output_count, VTY_NEWLINE);
6379
6380  return CMD_SUCCESS;
6381}
6382
6383static int
6384bgp_show (struct vty *vty, struct bgp *bgp, afi_t afi, safi_t safi,
6385         enum bgp_show_type type, void *output_arg)
6386{
6387  struct bgp_table *table;
6388
6389  if (bgp == NULL) {
6390    bgp = bgp_get_default ();
6391  }
6392
6393  if (bgp == NULL)
6394    {
6395      vty_out (vty, "No BGP process is configured%s", VTY_NEWLINE);
6396      return CMD_WARNING;
6397    }
6398
6399
6400  table = bgp->rib[afi][safi];
6401
6402  return bgp_show_table (vty, table, &bgp->router_id, type, output_arg);
6403}
6404
6405/* Header of detailed BGP route information */
6406static void
6407route_vty_out_detail_header (struct vty *vty, struct bgp *bgp,
6408			     struct bgp_node *rn,
6409                             struct prefix_rd *prd, afi_t afi, safi_t safi)
6410{
6411  struct bgp_info *ri;
6412  struct prefix *p;
6413  struct peer *peer;
6414  struct listnode *node, *nnode;
6415  char buf1[INET6_ADDRSTRLEN];
6416  char buf2[INET6_ADDRSTRLEN];
6417  int count = 0;
6418  int best = 0;
6419  int suppress = 0;
6420  int no_export = 0;
6421  int no_advertise = 0;
6422  int local_as = 0;
6423  int first = 0;
6424
6425  p = &rn->p;
6426  vty_out (vty, "BGP routing table entry for %s%s%s/%d%s",
6427	   (safi == SAFI_MPLS_VPN ?
6428	   prefix_rd2str (prd, buf1, RD_ADDRSTRLEN) : ""),
6429	   safi == SAFI_MPLS_VPN ? ":" : "",
6430	   inet_ntop (p->family, &p->u.prefix, buf2, INET6_ADDRSTRLEN),
6431	   p->prefixlen, VTY_NEWLINE);
6432
6433  for (ri = rn->info; ri; ri = ri->next)
6434    {
6435      count++;
6436      if (CHECK_FLAG (ri->flags, BGP_INFO_SELECTED))
6437	{
6438	  best = count;
6439	  if (ri->extra && ri->extra->suppress)
6440	    suppress = 1;
6441	  if (ri->attr->community != NULL)
6442	    {
6443	      if (community_include (ri->attr->community, COMMUNITY_NO_ADVERTISE))
6444		no_advertise = 1;
6445	      if (community_include (ri->attr->community, COMMUNITY_NO_EXPORT))
6446		no_export = 1;
6447	      if (community_include (ri->attr->community, COMMUNITY_LOCAL_AS))
6448		local_as = 1;
6449	    }
6450	}
6451    }
6452
6453  vty_out (vty, "Paths: (%d available", count);
6454  if (best)
6455    {
6456      vty_out (vty, ", best #%d", best);
6457      if (safi == SAFI_UNICAST)
6458	vty_out (vty, ", table Default-IP-Routing-Table");
6459    }
6460  else
6461    vty_out (vty, ", no best path");
6462  if (no_advertise)
6463    vty_out (vty, ", not advertised to any peer");
6464  else if (no_export)
6465    vty_out (vty, ", not advertised to EBGP peer");
6466  else if (local_as)
6467    vty_out (vty, ", not advertised outside local AS");
6468  if (suppress)
6469    vty_out (vty, ", Advertisements suppressed by an aggregate.");
6470  vty_out (vty, ")%s", VTY_NEWLINE);
6471
6472  /* advertised peer */
6473  for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
6474    {
6475      if (bgp_adj_out_lookup (peer, p, afi, safi, rn))
6476	{
6477	  if (! first)
6478	    vty_out (vty, "  Advertised to non peer-group peers:%s ", VTY_NEWLINE);
6479	  vty_out (vty, " %s", sockunion2str (&peer->su, buf1, SU_ADDRSTRLEN));
6480	  first = 1;
6481	}
6482    }
6483  if (! first)
6484    vty_out (vty, "  Not advertised to any peer");
6485  vty_out (vty, "%s", VTY_NEWLINE);
6486}
6487
6488/* Display specified route of BGP table. */
6489static int
6490bgp_show_route_in_table (struct vty *vty, struct bgp *bgp,
6491                         struct bgp_table *rib, const char *ip_str,
6492                         afi_t afi, safi_t safi, struct prefix_rd *prd,
6493                         int prefix_check)
6494{
6495  int ret;
6496  int header;
6497  int display = 0;
6498  struct prefix match;
6499  struct bgp_node *rn;
6500  struct bgp_node *rm;
6501  struct bgp_info *ri;
6502  struct bgp_table *table;
6503
6504  /* Check IP address argument. */
6505  ret = str2prefix (ip_str, &match);
6506  if (! ret)
6507    {
6508      vty_out (vty, "address is malformed%s", VTY_NEWLINE);
6509      return CMD_WARNING;
6510    }
6511
6512  match.family = afi2family (afi);
6513
6514  if (safi == SAFI_MPLS_VPN)
6515    {
6516      for (rn = bgp_table_top (rib); rn; rn = bgp_route_next (rn))
6517        {
6518          if (prd && memcmp (rn->p.u.val, prd->val, 8) != 0)
6519            continue;
6520
6521          if ((table = rn->info) != NULL)
6522            {
6523              header = 1;
6524
6525              if ((rm = bgp_node_match (table, &match)) != NULL)
6526                {
6527                  if (prefix_check && rm->p.prefixlen != match.prefixlen)
6528                    {
6529                      bgp_unlock_node (rm);
6530                      continue;
6531                    }
6532
6533                  for (ri = rm->info; ri; ri = ri->next)
6534                    {
6535                      if (header)
6536                        {
6537                          route_vty_out_detail_header (vty, bgp, rm, (struct prefix_rd *)&rn->p,
6538                                                       AFI_IP, SAFI_MPLS_VPN);
6539
6540                          header = 0;
6541                        }
6542                      display++;
6543                      route_vty_out_detail (vty, bgp, &rm->p, ri, AFI_IP, SAFI_MPLS_VPN);
6544                    }
6545
6546                  bgp_unlock_node (rm);
6547                }
6548            }
6549        }
6550    }
6551  else
6552    {
6553      header = 1;
6554
6555      if ((rn = bgp_node_match (rib, &match)) != NULL)
6556        {
6557          if (! prefix_check || rn->p.prefixlen == match.prefixlen)
6558            {
6559              for (ri = rn->info; ri; ri = ri->next)
6560                {
6561                  if (header)
6562                    {
6563                      route_vty_out_detail_header (vty, bgp, rn, NULL, afi, safi);
6564                      header = 0;
6565                    }
6566                  display++;
6567                  route_vty_out_detail (vty, bgp, &rn->p, ri, afi, safi);
6568                }
6569            }
6570
6571          bgp_unlock_node (rn);
6572        }
6573    }
6574
6575  if (! display)
6576    {
6577      vty_out (vty, "%% Network not in table%s", VTY_NEWLINE);
6578      return CMD_WARNING;
6579    }
6580
6581  return CMD_SUCCESS;
6582}
6583
6584/* Display specified route of Main RIB */
6585static int
6586bgp_show_route (struct vty *vty, const char *view_name, const char *ip_str,
6587		afi_t afi, safi_t safi, struct prefix_rd *prd,
6588		int prefix_check)
6589{
6590  struct bgp *bgp;
6591
6592  /* BGP structure lookup. */
6593  if (view_name)
6594    {
6595      bgp = bgp_lookup_by_name (view_name);
6596      if (bgp == NULL)
6597	{
6598	  vty_out (vty, "Can't find BGP view %s%s", view_name, VTY_NEWLINE);
6599	  return CMD_WARNING;
6600	}
6601    }
6602  else
6603    {
6604      bgp = bgp_get_default ();
6605      if (bgp == NULL)
6606	{
6607	  vty_out (vty, "No BGP process is configured%s", VTY_NEWLINE);
6608	  return CMD_WARNING;
6609	}
6610    }
6611
6612  return bgp_show_route_in_table (vty, bgp, bgp->rib[afi][safi], ip_str,
6613                                   afi, safi, prd, prefix_check);
6614}
6615
6616/* BGP route print out function. */
6617DEFUN (show_ip_bgp,
6618       show_ip_bgp_cmd,
6619       "show ip bgp",
6620       SHOW_STR
6621       IP_STR
6622       BGP_STR)
6623{
6624  return bgp_show (vty, NULL, AFI_IP, SAFI_UNICAST, bgp_show_type_normal, NULL);
6625}
6626
6627DEFUN (show_ip_bgp_ipv4,
6628       show_ip_bgp_ipv4_cmd,
6629       "show ip bgp ipv4 (unicast|multicast)",
6630       SHOW_STR
6631       IP_STR
6632       BGP_STR
6633       "Address family\n"
6634       "Address Family modifier\n"
6635       "Address Family modifier\n")
6636{
6637  if (strncmp (argv[0], "m", 1) == 0)
6638    return bgp_show (vty, NULL, AFI_IP, SAFI_MULTICAST, bgp_show_type_normal,
6639                     NULL);
6640
6641  return bgp_show (vty, NULL, AFI_IP, SAFI_UNICAST, bgp_show_type_normal, NULL);
6642}
6643
6644ALIAS (show_ip_bgp_ipv4,
6645       show_bgp_ipv4_safi_cmd,
6646       "show bgp ipv4 (unicast|multicast)",
6647       SHOW_STR
6648       BGP_STR
6649       "Address family\n"
6650       "Address Family modifier\n"
6651       "Address Family modifier\n")
6652
6653DEFUN (show_ip_bgp_route,
6654       show_ip_bgp_route_cmd,
6655       "show ip bgp A.B.C.D",
6656       SHOW_STR
6657       IP_STR
6658       BGP_STR
6659       "Network in the BGP routing table to display\n")
6660{
6661  return bgp_show_route (vty, NULL, argv[0], AFI_IP, SAFI_UNICAST, NULL, 0);
6662}
6663
6664DEFUN (show_ip_bgp_ipv4_route,
6665       show_ip_bgp_ipv4_route_cmd,
6666       "show ip bgp ipv4 (unicast|multicast) A.B.C.D",
6667       SHOW_STR
6668       IP_STR
6669       BGP_STR
6670       "Address family\n"
6671       "Address Family modifier\n"
6672       "Address Family modifier\n"
6673       "Network in the BGP routing table to display\n")
6674{
6675  if (strncmp (argv[0], "m", 1) == 0)
6676    return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_MULTICAST, NULL, 0);
6677
6678  return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_UNICAST, NULL, 0);
6679}
6680
6681ALIAS (show_ip_bgp_ipv4_route,
6682       show_bgp_ipv4_safi_route_cmd,
6683       "show bgp ipv4 (unicast|multicast) A.B.C.D",
6684       SHOW_STR
6685       BGP_STR
6686       "Address family\n"
6687       "Address Family modifier\n"
6688       "Address Family modifier\n"
6689       "Network in the BGP routing table to display\n")
6690
6691DEFUN (show_ip_bgp_vpnv4_all_route,
6692       show_ip_bgp_vpnv4_all_route_cmd,
6693       "show ip bgp vpnv4 all A.B.C.D",
6694       SHOW_STR
6695       IP_STR
6696       BGP_STR
6697       "Display VPNv4 NLRI specific information\n"
6698       "Display information about all VPNv4 NLRIs\n"
6699       "Network in the BGP routing table to display\n")
6700{
6701  return bgp_show_route (vty, NULL, argv[0], AFI_IP, SAFI_MPLS_VPN, NULL, 0);
6702}
6703
6704DEFUN (show_ip_bgp_vpnv4_rd_route,
6705       show_ip_bgp_vpnv4_rd_route_cmd,
6706       "show ip bgp vpnv4 rd ASN:nn_or_IP-address:nn A.B.C.D",
6707       SHOW_STR
6708       IP_STR
6709       BGP_STR
6710       "Display VPNv4 NLRI specific information\n"
6711       "Display information for a route distinguisher\n"
6712       "VPN Route Distinguisher\n"
6713       "Network in the BGP routing table to display\n")
6714{
6715  int ret;
6716  struct prefix_rd prd;
6717
6718  ret = str2prefix_rd (argv[0], &prd);
6719  if (! ret)
6720    {
6721      vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
6722      return CMD_WARNING;
6723    }
6724  return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_MPLS_VPN, &prd, 0);
6725}
6726
6727DEFUN (show_ip_bgp_prefix,
6728       show_ip_bgp_prefix_cmd,
6729       "show ip bgp A.B.C.D/M",
6730       SHOW_STR
6731       IP_STR
6732       BGP_STR
6733       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
6734{
6735  return bgp_show_route (vty, NULL, argv[0], AFI_IP, SAFI_UNICAST, NULL, 1);
6736}
6737
6738DEFUN (show_ip_bgp_ipv4_prefix,
6739       show_ip_bgp_ipv4_prefix_cmd,
6740       "show ip bgp ipv4 (unicast|multicast) A.B.C.D/M",
6741       SHOW_STR
6742       IP_STR
6743       BGP_STR
6744       "Address family\n"
6745       "Address Family modifier\n"
6746       "Address Family modifier\n"
6747       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
6748{
6749  if (strncmp (argv[0], "m", 1) == 0)
6750    return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_MULTICAST, NULL, 1);
6751
6752  return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_UNICAST, NULL, 1);
6753}
6754
6755ALIAS (show_ip_bgp_ipv4_prefix,
6756       show_bgp_ipv4_safi_prefix_cmd,
6757       "show bgp ipv4 (unicast|multicast) A.B.C.D/M",
6758       SHOW_STR
6759       BGP_STR
6760       "Address family\n"
6761       "Address Family modifier\n"
6762       "Address Family modifier\n"
6763       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
6764
6765DEFUN (show_ip_bgp_vpnv4_all_prefix,
6766       show_ip_bgp_vpnv4_all_prefix_cmd,
6767       "show ip bgp vpnv4 all A.B.C.D/M",
6768       SHOW_STR
6769       IP_STR
6770       BGP_STR
6771       "Display VPNv4 NLRI specific information\n"
6772       "Display information about all VPNv4 NLRIs\n"
6773       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
6774{
6775  return bgp_show_route (vty, NULL, argv[0], AFI_IP, SAFI_MPLS_VPN, NULL, 1);
6776}
6777
6778DEFUN (show_ip_bgp_vpnv4_rd_prefix,
6779       show_ip_bgp_vpnv4_rd_prefix_cmd,
6780       "show ip bgp vpnv4 rd ASN:nn_or_IP-address:nn A.B.C.D/M",
6781       SHOW_STR
6782       IP_STR
6783       BGP_STR
6784       "Display VPNv4 NLRI specific information\n"
6785       "Display information for a route distinguisher\n"
6786       "VPN Route Distinguisher\n"
6787       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
6788{
6789  int ret;
6790  struct prefix_rd prd;
6791
6792  ret = str2prefix_rd (argv[0], &prd);
6793  if (! ret)
6794    {
6795      vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
6796      return CMD_WARNING;
6797    }
6798  return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_MPLS_VPN, &prd, 1);
6799}
6800
6801DEFUN (show_ip_bgp_view,
6802       show_ip_bgp_view_cmd,
6803       "show ip bgp view WORD",
6804       SHOW_STR
6805       IP_STR
6806       BGP_STR
6807       "BGP view\n"
6808       "View name\n")
6809{
6810  struct bgp *bgp;
6811
6812  /* BGP structure lookup. */
6813  bgp = bgp_lookup_by_name (argv[0]);
6814  if (bgp == NULL)
6815	{
6816	  vty_out (vty, "Can't find BGP view %s%s", argv[0], VTY_NEWLINE);
6817	  return CMD_WARNING;
6818	}
6819
6820  return bgp_show (vty, bgp, AFI_IP, SAFI_UNICAST, bgp_show_type_normal, NULL);
6821}
6822
6823DEFUN (show_ip_bgp_view_route,
6824       show_ip_bgp_view_route_cmd,
6825       "show ip bgp view WORD A.B.C.D",
6826       SHOW_STR
6827       IP_STR
6828       BGP_STR
6829       "BGP view\n"
6830       "View name\n"
6831       "Network in the BGP routing table to display\n")
6832{
6833  return bgp_show_route (vty, argv[0], argv[1], AFI_IP, SAFI_UNICAST, NULL, 0);
6834}
6835
6836DEFUN (show_ip_bgp_view_prefix,
6837       show_ip_bgp_view_prefix_cmd,
6838       "show ip bgp view WORD A.B.C.D/M",
6839       SHOW_STR
6840       IP_STR
6841       BGP_STR
6842       "BGP view\n"
6843       "View name\n"
6844       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
6845{
6846  return bgp_show_route (vty, argv[0], argv[1], AFI_IP, SAFI_UNICAST, NULL, 1);
6847}
6848
6849#ifdef HAVE_IPV6
6850DEFUN (show_bgp,
6851       show_bgp_cmd,
6852       "show bgp",
6853       SHOW_STR
6854       BGP_STR)
6855{
6856  return bgp_show (vty, NULL, AFI_IP6, SAFI_UNICAST, bgp_show_type_normal,
6857                   NULL);
6858}
6859
6860ALIAS (show_bgp,
6861       show_bgp_ipv6_cmd,
6862       "show bgp ipv6",
6863       SHOW_STR
6864       BGP_STR
6865       "Address family\n")
6866
6867DEFUN (show_bgp_ipv6_safi,
6868       show_bgp_ipv6_safi_cmd,
6869       "show bgp ipv6 (unicast|multicast)",
6870       SHOW_STR
6871       BGP_STR
6872       "Address family\n"
6873       "Address Family modifier\n"
6874       "Address Family modifier\n")
6875{
6876  if (strncmp (argv[0], "m", 1) == 0)
6877    return bgp_show (vty, NULL, AFI_IP6, SAFI_MULTICAST, bgp_show_type_normal,
6878                     NULL);
6879
6880  return bgp_show (vty, NULL, AFI_IP6, SAFI_UNICAST, bgp_show_type_normal, NULL);
6881}
6882
6883/* old command */
6884DEFUN (show_ipv6_bgp,
6885       show_ipv6_bgp_cmd,
6886       "show ipv6 bgp",
6887       SHOW_STR
6888       IP_STR
6889       BGP_STR)
6890{
6891  return bgp_show (vty, NULL, AFI_IP6, SAFI_UNICAST, bgp_show_type_normal,
6892                   NULL);
6893}
6894
6895DEFUN (show_bgp_route,
6896       show_bgp_route_cmd,
6897       "show bgp X:X::X:X",
6898       SHOW_STR
6899       BGP_STR
6900       "Network in the BGP routing table to display\n")
6901{
6902  return bgp_show_route (vty, NULL, argv[0], AFI_IP6, SAFI_UNICAST, NULL, 0);
6903}
6904
6905ALIAS (show_bgp_route,
6906       show_bgp_ipv6_route_cmd,
6907       "show bgp ipv6 X:X::X:X",
6908       SHOW_STR
6909       BGP_STR
6910       "Address family\n"
6911       "Network in the BGP routing table to display\n")
6912
6913DEFUN (show_bgp_ipv6_safi_route,
6914       show_bgp_ipv6_safi_route_cmd,
6915       "show bgp ipv6 (unicast|multicast) X:X::X:X",
6916       SHOW_STR
6917       BGP_STR
6918       "Address family\n"
6919       "Address Family modifier\n"
6920       "Address Family modifier\n"
6921       "Network in the BGP routing table to display\n")
6922{
6923  if (strncmp (argv[0], "m", 1) == 0)
6924    return bgp_show_route (vty, NULL, argv[1], AFI_IP6, SAFI_MULTICAST, NULL, 0);
6925
6926  return bgp_show_route (vty, NULL, argv[1], AFI_IP6, SAFI_UNICAST, NULL, 0);
6927}
6928
6929/* old command */
6930DEFUN (show_ipv6_bgp_route,
6931       show_ipv6_bgp_route_cmd,
6932       "show ipv6 bgp X:X::X:X",
6933       SHOW_STR
6934       IP_STR
6935       BGP_STR
6936       "Network in the BGP routing table to display\n")
6937{
6938  return bgp_show_route (vty, NULL, argv[0], AFI_IP6, SAFI_UNICAST, NULL, 0);
6939}
6940
6941DEFUN (show_bgp_prefix,
6942       show_bgp_prefix_cmd,
6943       "show bgp X:X::X:X/M",
6944       SHOW_STR
6945       BGP_STR
6946       "IPv6 prefix <network>/<length>\n")
6947{
6948  return bgp_show_route (vty, NULL, argv[0], AFI_IP6, SAFI_UNICAST, NULL, 1);
6949}
6950
6951ALIAS (show_bgp_prefix,
6952       show_bgp_ipv6_prefix_cmd,
6953       "show bgp ipv6 X:X::X:X/M",
6954       SHOW_STR
6955       BGP_STR
6956       "Address family\n"
6957       "IPv6 prefix <network>/<length>\n")
6958
6959DEFUN (show_bgp_ipv6_safi_prefix,
6960       show_bgp_ipv6_safi_prefix_cmd,
6961       "show bgp ipv6 (unicast|multicast) X:X::X:X/M",
6962       SHOW_STR
6963       BGP_STR
6964       "Address family\n"
6965       "Address Family modifier\n"
6966       "Address Family modifier\n"
6967       "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n")
6968{
6969  if (strncmp (argv[0], "m", 1) == 0)
6970    return bgp_show_route (vty, NULL, argv[1], AFI_IP6, SAFI_MULTICAST, NULL, 1);
6971
6972  return bgp_show_route (vty, NULL, argv[1], AFI_IP6, SAFI_UNICAST, NULL, 1);
6973}
6974
6975/* old command */
6976DEFUN (show_ipv6_bgp_prefix,
6977       show_ipv6_bgp_prefix_cmd,
6978       "show ipv6 bgp X:X::X:X/M",
6979       SHOW_STR
6980       IP_STR
6981       BGP_STR
6982       "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n")
6983{
6984  return bgp_show_route (vty, NULL, argv[0], AFI_IP6, SAFI_UNICAST, NULL, 1);
6985}
6986
6987DEFUN (show_bgp_view,
6988       show_bgp_view_cmd,
6989       "show bgp view WORD",
6990       SHOW_STR
6991       BGP_STR
6992       "BGP view\n"
6993       "View name\n")
6994{
6995  struct bgp *bgp;
6996
6997  /* BGP structure lookup. */
6998  bgp = bgp_lookup_by_name (argv[0]);
6999  if (bgp == NULL)
7000	{
7001	  vty_out (vty, "Can't find BGP view %s%s", argv[0], VTY_NEWLINE);
7002	  return CMD_WARNING;
7003	}
7004
7005  return bgp_show (vty, bgp, AFI_IP6, SAFI_UNICAST, bgp_show_type_normal, NULL);
7006}
7007
7008ALIAS (show_bgp_view,
7009       show_bgp_view_ipv6_cmd,
7010       "show bgp view WORD ipv6",
7011       SHOW_STR
7012       BGP_STR
7013       "BGP view\n"
7014       "View name\n"
7015       "Address family\n")
7016
7017DEFUN (show_bgp_view_route,
7018       show_bgp_view_route_cmd,
7019       "show bgp view WORD X:X::X:X",
7020       SHOW_STR
7021       BGP_STR
7022       "BGP view\n"
7023       "View name\n"
7024       "Network in the BGP routing table to display\n")
7025{
7026  return bgp_show_route (vty, argv[0], argv[1], AFI_IP6, SAFI_UNICAST, NULL, 0);
7027}
7028
7029ALIAS (show_bgp_view_route,
7030       show_bgp_view_ipv6_route_cmd,
7031       "show bgp view WORD ipv6 X:X::X:X",
7032       SHOW_STR
7033       BGP_STR
7034       "BGP view\n"
7035       "View name\n"
7036       "Address family\n"
7037       "Network in the BGP routing table to display\n")
7038
7039DEFUN (show_bgp_view_prefix,
7040       show_bgp_view_prefix_cmd,
7041       "show bgp view WORD X:X::X:X/M",
7042       SHOW_STR
7043       BGP_STR
7044       "BGP view\n"
7045       "View name\n"
7046       "IPv6 prefix <network>/<length>\n")
7047{
7048  return bgp_show_route (vty, argv[0], argv[1], AFI_IP6, SAFI_UNICAST, NULL, 1);
7049}
7050
7051ALIAS (show_bgp_view_prefix,
7052       show_bgp_view_ipv6_prefix_cmd,
7053       "show bgp view WORD ipv6 X:X::X:X/M",
7054       SHOW_STR
7055       BGP_STR
7056       "BGP view\n"
7057       "View name\n"
7058       "Address family\n"
7059       "IPv6 prefix <network>/<length>\n")
7060
7061/* old command */
7062DEFUN (show_ipv6_mbgp,
7063       show_ipv6_mbgp_cmd,
7064       "show ipv6 mbgp",
7065       SHOW_STR
7066       IP_STR
7067       MBGP_STR)
7068{
7069  return bgp_show (vty, NULL, AFI_IP6, SAFI_MULTICAST, bgp_show_type_normal,
7070                   NULL);
7071}
7072
7073/* old command */
7074DEFUN (show_ipv6_mbgp_route,
7075       show_ipv6_mbgp_route_cmd,
7076       "show ipv6 mbgp X:X::X:X",
7077       SHOW_STR
7078       IP_STR
7079       MBGP_STR
7080       "Network in the MBGP routing table to display\n")
7081{
7082  return bgp_show_route (vty, NULL, argv[0], AFI_IP6, SAFI_MULTICAST, NULL, 0);
7083}
7084
7085/* old command */
7086DEFUN (show_ipv6_mbgp_prefix,
7087       show_ipv6_mbgp_prefix_cmd,
7088       "show ipv6 mbgp X:X::X:X/M",
7089       SHOW_STR
7090       IP_STR
7091       MBGP_STR
7092       "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n")
7093{
7094  return bgp_show_route (vty, NULL, argv[0], AFI_IP6, SAFI_MULTICAST, NULL, 1);
7095}
7096#endif
7097
7098
7099static int
7100bgp_show_regexp (struct vty *vty, int argc, const char **argv, afi_t afi,
7101		 safi_t safi, enum bgp_show_type type)
7102{
7103  int i;
7104  struct buffer *b;
7105  char *regstr;
7106  int first;
7107  regex_t *regex;
7108  int rc;
7109
7110  first = 0;
7111  b = buffer_new (1024);
7112  for (i = 0; i < argc; i++)
7113    {
7114      if (first)
7115	buffer_putc (b, ' ');
7116      else
7117	{
7118	  if ((strcmp (argv[i], "unicast") == 0) || (strcmp (argv[i], "multicast") == 0))
7119	    continue;
7120	  first = 1;
7121	}
7122
7123      buffer_putstr (b, argv[i]);
7124    }
7125  buffer_putc (b, '\0');
7126
7127  regstr = buffer_getstr (b);
7128  buffer_free (b);
7129
7130  regex = bgp_regcomp (regstr);
7131  XFREE(MTYPE_TMP, regstr);
7132  if (! regex)
7133    {
7134      vty_out (vty, "Can't compile regexp %s%s", argv[0],
7135	       VTY_NEWLINE);
7136      return CMD_WARNING;
7137    }
7138
7139  rc = bgp_show (vty, NULL, afi, safi, type, regex);
7140  bgp_regex_free (regex);
7141  return rc;
7142}
7143
7144DEFUN (show_ip_bgp_regexp,
7145       show_ip_bgp_regexp_cmd,
7146       "show ip bgp regexp .LINE",
7147       SHOW_STR
7148       IP_STR
7149       BGP_STR
7150       "Display routes matching the AS path regular expression\n"
7151       "A regular-expression to match the BGP AS paths\n")
7152{
7153  return bgp_show_regexp (vty, argc, argv, AFI_IP, SAFI_UNICAST,
7154			  bgp_show_type_regexp);
7155}
7156
7157DEFUN (show_ip_bgp_flap_regexp,
7158       show_ip_bgp_flap_regexp_cmd,
7159       "show ip bgp flap-statistics regexp .LINE",
7160       SHOW_STR
7161       IP_STR
7162       BGP_STR
7163       "Display flap statistics of routes\n"
7164       "Display routes matching the AS path regular expression\n"
7165       "A regular-expression to match the BGP AS paths\n")
7166{
7167  return bgp_show_regexp (vty, argc, argv, AFI_IP, SAFI_UNICAST,
7168			  bgp_show_type_flap_regexp);
7169}
7170
7171DEFUN (show_ip_bgp_ipv4_regexp,
7172       show_ip_bgp_ipv4_regexp_cmd,
7173       "show ip bgp ipv4 (unicast|multicast) regexp .LINE",
7174       SHOW_STR
7175       IP_STR
7176       BGP_STR
7177       "Address family\n"
7178       "Address Family modifier\n"
7179       "Address Family modifier\n"
7180       "Display routes matching the AS path regular expression\n"
7181       "A regular-expression to match the BGP AS paths\n")
7182{
7183  if (strncmp (argv[0], "m", 1) == 0)
7184    return bgp_show_regexp (vty, argc, argv, AFI_IP, SAFI_MULTICAST,
7185			    bgp_show_type_regexp);
7186
7187  return bgp_show_regexp (vty, argc, argv, AFI_IP, SAFI_UNICAST,
7188			  bgp_show_type_regexp);
7189}
7190
7191#ifdef HAVE_IPV6
7192DEFUN (show_bgp_regexp,
7193       show_bgp_regexp_cmd,
7194       "show bgp regexp .LINE",
7195       SHOW_STR
7196       BGP_STR
7197       "Display routes matching the AS path regular expression\n"
7198       "A regular-expression to match the BGP AS paths\n")
7199{
7200  return bgp_show_regexp (vty, argc, argv, AFI_IP6, SAFI_UNICAST,
7201			  bgp_show_type_regexp);
7202}
7203
7204ALIAS (show_bgp_regexp,
7205       show_bgp_ipv6_regexp_cmd,
7206       "show bgp ipv6 regexp .LINE",
7207       SHOW_STR
7208       BGP_STR
7209       "Address family\n"
7210       "Display routes matching the AS path regular expression\n"
7211       "A regular-expression to match the BGP AS paths\n")
7212
7213/* old command */
7214DEFUN (show_ipv6_bgp_regexp,
7215       show_ipv6_bgp_regexp_cmd,
7216       "show ipv6 bgp regexp .LINE",
7217       SHOW_STR
7218       IP_STR
7219       BGP_STR
7220       "Display routes matching the AS path regular expression\n"
7221       "A regular-expression to match the BGP AS paths\n")
7222{
7223  return bgp_show_regexp (vty, argc, argv, AFI_IP6, SAFI_UNICAST,
7224			  bgp_show_type_regexp);
7225}
7226
7227/* old command */
7228DEFUN (show_ipv6_mbgp_regexp,
7229       show_ipv6_mbgp_regexp_cmd,
7230       "show ipv6 mbgp regexp .LINE",
7231       SHOW_STR
7232       IP_STR
7233       BGP_STR
7234       "Display routes matching the AS path regular expression\n"
7235       "A regular-expression to match the MBGP AS paths\n")
7236{
7237  return bgp_show_regexp (vty, argc, argv, AFI_IP6, SAFI_MULTICAST,
7238			  bgp_show_type_regexp);
7239}
7240#endif /* HAVE_IPV6 */
7241
7242static int
7243bgp_show_prefix_list (struct vty *vty, const char *prefix_list_str, afi_t afi,
7244		      safi_t safi, enum bgp_show_type type)
7245{
7246  struct prefix_list *plist;
7247
7248  plist = prefix_list_lookup (afi, prefix_list_str);
7249  if (plist == NULL)
7250    {
7251      vty_out (vty, "%% %s is not a valid prefix-list name%s",
7252               prefix_list_str, VTY_NEWLINE);
7253      return CMD_WARNING;
7254    }
7255
7256  return bgp_show (vty, NULL, afi, safi, type, plist);
7257}
7258
7259DEFUN (show_ip_bgp_prefix_list,
7260       show_ip_bgp_prefix_list_cmd,
7261       "show ip bgp prefix-list WORD",
7262       SHOW_STR
7263       IP_STR
7264       BGP_STR
7265       "Display routes conforming to the prefix-list\n"
7266       "IP prefix-list name\n")
7267{
7268  return bgp_show_prefix_list (vty, argv[0], AFI_IP, SAFI_UNICAST,
7269			       bgp_show_type_prefix_list);
7270}
7271
7272DEFUN (show_ip_bgp_flap_prefix_list,
7273       show_ip_bgp_flap_prefix_list_cmd,
7274       "show ip bgp flap-statistics prefix-list WORD",
7275       SHOW_STR
7276       IP_STR
7277       BGP_STR
7278       "Display flap statistics of routes\n"
7279       "Display routes conforming to the prefix-list\n"
7280       "IP prefix-list name\n")
7281{
7282  return bgp_show_prefix_list (vty, argv[0], AFI_IP, SAFI_UNICAST,
7283			       bgp_show_type_flap_prefix_list);
7284}
7285
7286DEFUN (show_ip_bgp_ipv4_prefix_list,
7287       show_ip_bgp_ipv4_prefix_list_cmd,
7288       "show ip bgp ipv4 (unicast|multicast) prefix-list WORD",
7289       SHOW_STR
7290       IP_STR
7291       BGP_STR
7292       "Address family\n"
7293       "Address Family modifier\n"
7294       "Address Family modifier\n"
7295       "Display routes conforming to the prefix-list\n"
7296       "IP prefix-list name\n")
7297{
7298  if (strncmp (argv[0], "m", 1) == 0)
7299    return bgp_show_prefix_list (vty, argv[1], AFI_IP, SAFI_MULTICAST,
7300			         bgp_show_type_prefix_list);
7301
7302  return bgp_show_prefix_list (vty, argv[1], AFI_IP, SAFI_UNICAST,
7303			       bgp_show_type_prefix_list);
7304}
7305
7306#ifdef HAVE_IPV6
7307DEFUN (show_bgp_prefix_list,
7308       show_bgp_prefix_list_cmd,
7309       "show bgp prefix-list WORD",
7310       SHOW_STR
7311       BGP_STR
7312       "Display routes conforming to the prefix-list\n"
7313       "IPv6 prefix-list name\n")
7314{
7315  return bgp_show_prefix_list (vty, argv[0], AFI_IP6, SAFI_UNICAST,
7316			       bgp_show_type_prefix_list);
7317}
7318
7319ALIAS (show_bgp_prefix_list,
7320       show_bgp_ipv6_prefix_list_cmd,
7321       "show bgp ipv6 prefix-list WORD",
7322       SHOW_STR
7323       BGP_STR
7324       "Address family\n"
7325       "Display routes conforming to the prefix-list\n"
7326       "IPv6 prefix-list name\n")
7327
7328/* old command */
7329DEFUN (show_ipv6_bgp_prefix_list,
7330       show_ipv6_bgp_prefix_list_cmd,
7331       "show ipv6 bgp prefix-list WORD",
7332       SHOW_STR
7333       IPV6_STR
7334       BGP_STR
7335       "Display routes matching the prefix-list\n"
7336       "IPv6 prefix-list name\n")
7337{
7338  return bgp_show_prefix_list (vty, argv[0], AFI_IP6, SAFI_UNICAST,
7339			       bgp_show_type_prefix_list);
7340}
7341
7342/* old command */
7343DEFUN (show_ipv6_mbgp_prefix_list,
7344       show_ipv6_mbgp_prefix_list_cmd,
7345       "show ipv6 mbgp prefix-list WORD",
7346       SHOW_STR
7347       IPV6_STR
7348       MBGP_STR
7349       "Display routes matching the prefix-list\n"
7350       "IPv6 prefix-list name\n")
7351{
7352  return bgp_show_prefix_list (vty, argv[0], AFI_IP6, SAFI_MULTICAST,
7353			       bgp_show_type_prefix_list);
7354}
7355#endif /* HAVE_IPV6 */
7356
7357static int
7358bgp_show_filter_list (struct vty *vty, const char *filter, afi_t afi,
7359		      safi_t safi, enum bgp_show_type type)
7360{
7361  struct as_list *as_list;
7362
7363  as_list = as_list_lookup (filter);
7364  if (as_list == NULL)
7365    {
7366      vty_out (vty, "%% %s is not a valid AS-path access-list name%s", filter, VTY_NEWLINE);
7367      return CMD_WARNING;
7368    }
7369
7370  return bgp_show (vty, NULL, afi, safi, type, as_list);
7371}
7372
7373DEFUN (show_ip_bgp_filter_list,
7374       show_ip_bgp_filter_list_cmd,
7375       "show ip bgp filter-list WORD",
7376       SHOW_STR
7377       IP_STR
7378       BGP_STR
7379       "Display routes conforming to the filter-list\n"
7380       "Regular expression access list name\n")
7381{
7382  return bgp_show_filter_list (vty, argv[0], AFI_IP, SAFI_UNICAST,
7383			       bgp_show_type_filter_list);
7384}
7385
7386DEFUN (show_ip_bgp_flap_filter_list,
7387       show_ip_bgp_flap_filter_list_cmd,
7388       "show ip bgp flap-statistics filter-list WORD",
7389       SHOW_STR
7390       IP_STR
7391       BGP_STR
7392       "Display flap statistics of routes\n"
7393       "Display routes conforming to the filter-list\n"
7394       "Regular expression access list name\n")
7395{
7396  return bgp_show_filter_list (vty, argv[0], AFI_IP, SAFI_UNICAST,
7397			       bgp_show_type_flap_filter_list);
7398}
7399
7400DEFUN (show_ip_bgp_ipv4_filter_list,
7401       show_ip_bgp_ipv4_filter_list_cmd,
7402       "show ip bgp ipv4 (unicast|multicast) filter-list WORD",
7403       SHOW_STR
7404       IP_STR
7405       BGP_STR
7406       "Address family\n"
7407       "Address Family modifier\n"
7408       "Address Family modifier\n"
7409       "Display routes conforming to the filter-list\n"
7410       "Regular expression access list name\n")
7411{
7412  if (strncmp (argv[0], "m", 1) == 0)
7413    return bgp_show_filter_list (vty, argv[1], AFI_IP, SAFI_MULTICAST,
7414			         bgp_show_type_filter_list);
7415
7416  return bgp_show_filter_list (vty, argv[1], AFI_IP, SAFI_UNICAST,
7417			       bgp_show_type_filter_list);
7418}
7419
7420#ifdef HAVE_IPV6
7421DEFUN (show_bgp_filter_list,
7422       show_bgp_filter_list_cmd,
7423       "show bgp filter-list WORD",
7424       SHOW_STR
7425       BGP_STR
7426       "Display routes conforming to the filter-list\n"
7427       "Regular expression access list name\n")
7428{
7429  return bgp_show_filter_list (vty, argv[0], AFI_IP6, SAFI_UNICAST,
7430			       bgp_show_type_filter_list);
7431}
7432
7433ALIAS (show_bgp_filter_list,
7434       show_bgp_ipv6_filter_list_cmd,
7435       "show bgp ipv6 filter-list WORD",
7436       SHOW_STR
7437       BGP_STR
7438       "Address family\n"
7439       "Display routes conforming to the filter-list\n"
7440       "Regular expression access list name\n")
7441
7442/* old command */
7443DEFUN (show_ipv6_bgp_filter_list,
7444       show_ipv6_bgp_filter_list_cmd,
7445       "show ipv6 bgp filter-list WORD",
7446       SHOW_STR
7447       IPV6_STR
7448       BGP_STR
7449       "Display routes conforming to the filter-list\n"
7450       "Regular expression access list name\n")
7451{
7452  return bgp_show_filter_list (vty, argv[0], AFI_IP6, SAFI_UNICAST,
7453			       bgp_show_type_filter_list);
7454}
7455
7456/* old command */
7457DEFUN (show_ipv6_mbgp_filter_list,
7458       show_ipv6_mbgp_filter_list_cmd,
7459       "show ipv6 mbgp filter-list WORD",
7460       SHOW_STR
7461       IPV6_STR
7462       MBGP_STR
7463       "Display routes conforming to the filter-list\n"
7464       "Regular expression access list name\n")
7465{
7466  return bgp_show_filter_list (vty, argv[0], AFI_IP6, SAFI_MULTICAST,
7467			       bgp_show_type_filter_list);
7468}
7469#endif /* HAVE_IPV6 */
7470
7471static int
7472bgp_show_route_map (struct vty *vty, const char *rmap_str, afi_t afi,
7473		    safi_t safi, enum bgp_show_type type)
7474{
7475  struct route_map *rmap;
7476
7477  rmap = route_map_lookup_by_name (rmap_str);
7478  if (! rmap)
7479    {
7480      vty_out (vty, "%% %s is not a valid route-map name%s",
7481	       rmap_str, VTY_NEWLINE);
7482      return CMD_WARNING;
7483    }
7484
7485  return bgp_show (vty, NULL, afi, safi, type, rmap);
7486}
7487
7488DEFUN (show_ip_bgp_route_map,
7489       show_ip_bgp_route_map_cmd,
7490       "show ip bgp route-map WORD",
7491       SHOW_STR
7492       IP_STR
7493       BGP_STR
7494       "Display routes matching the route-map\n"
7495       "A route-map to match on\n")
7496{
7497  return bgp_show_route_map (vty, argv[0], AFI_IP, SAFI_UNICAST,
7498			     bgp_show_type_route_map);
7499}
7500
7501DEFUN (show_ip_bgp_flap_route_map,
7502       show_ip_bgp_flap_route_map_cmd,
7503       "show ip bgp flap-statistics route-map WORD",
7504       SHOW_STR
7505       IP_STR
7506       BGP_STR
7507       "Display flap statistics of routes\n"
7508       "Display routes matching the route-map\n"
7509       "A route-map to match on\n")
7510{
7511  return bgp_show_route_map (vty, argv[0], AFI_IP, SAFI_UNICAST,
7512			     bgp_show_type_flap_route_map);
7513}
7514
7515DEFUN (show_ip_bgp_ipv4_route_map,
7516       show_ip_bgp_ipv4_route_map_cmd,
7517       "show ip bgp ipv4 (unicast|multicast) route-map WORD",
7518       SHOW_STR
7519       IP_STR
7520       BGP_STR
7521       "Address family\n"
7522       "Address Family modifier\n"
7523       "Address Family modifier\n"
7524       "Display routes matching the route-map\n"
7525       "A route-map to match on\n")
7526{
7527  if (strncmp (argv[0], "m", 1) == 0)
7528    return bgp_show_route_map (vty, argv[1], AFI_IP, SAFI_MULTICAST,
7529			       bgp_show_type_route_map);
7530
7531  return bgp_show_route_map (vty, argv[1], AFI_IP, SAFI_UNICAST,
7532			     bgp_show_type_route_map);
7533}
7534
7535DEFUN (show_bgp_route_map,
7536       show_bgp_route_map_cmd,
7537       "show bgp route-map WORD",
7538       SHOW_STR
7539       BGP_STR
7540       "Display routes matching the route-map\n"
7541       "A route-map to match on\n")
7542{
7543  return bgp_show_route_map (vty, argv[0], AFI_IP6, SAFI_UNICAST,
7544			     bgp_show_type_route_map);
7545}
7546
7547ALIAS (show_bgp_route_map,
7548       show_bgp_ipv6_route_map_cmd,
7549       "show bgp ipv6 route-map WORD",
7550       SHOW_STR
7551       BGP_STR
7552       "Address family\n"
7553       "Display routes matching the route-map\n"
7554       "A route-map to match on\n")
7555
7556DEFUN (show_ip_bgp_cidr_only,
7557       show_ip_bgp_cidr_only_cmd,
7558       "show ip bgp cidr-only",
7559       SHOW_STR
7560       IP_STR
7561       BGP_STR
7562       "Display only routes with non-natural netmasks\n")
7563{
7564    return bgp_show (vty, NULL, AFI_IP, SAFI_UNICAST,
7565		     bgp_show_type_cidr_only, NULL);
7566}
7567
7568DEFUN (show_ip_bgp_flap_cidr_only,
7569       show_ip_bgp_flap_cidr_only_cmd,
7570       "show ip bgp flap-statistics cidr-only",
7571       SHOW_STR
7572       IP_STR
7573       BGP_STR
7574       "Display flap statistics of routes\n"
7575       "Display only routes with non-natural netmasks\n")
7576{
7577  return bgp_show (vty, NULL, AFI_IP, SAFI_UNICAST,
7578		   bgp_show_type_flap_cidr_only, NULL);
7579}
7580
7581DEFUN (show_ip_bgp_ipv4_cidr_only,
7582       show_ip_bgp_ipv4_cidr_only_cmd,
7583       "show ip bgp ipv4 (unicast|multicast) cidr-only",
7584       SHOW_STR
7585       IP_STR
7586       BGP_STR
7587       "Address family\n"
7588       "Address Family modifier\n"
7589       "Address Family modifier\n"
7590       "Display only routes with non-natural netmasks\n")
7591{
7592  if (strncmp (argv[0], "m", 1) == 0)
7593    return bgp_show (vty, NULL, AFI_IP, SAFI_MULTICAST,
7594		     bgp_show_type_cidr_only, NULL);
7595
7596  return bgp_show (vty, NULL, AFI_IP, SAFI_UNICAST,
7597		     bgp_show_type_cidr_only, NULL);
7598}
7599
7600DEFUN (show_ip_bgp_community_all,
7601       show_ip_bgp_community_all_cmd,
7602       "show ip bgp community",
7603       SHOW_STR
7604       IP_STR
7605       BGP_STR
7606       "Display routes matching the communities\n")
7607{
7608  return bgp_show (vty, NULL, AFI_IP, SAFI_UNICAST,
7609		     bgp_show_type_community_all, NULL);
7610}
7611
7612DEFUN (show_ip_bgp_ipv4_community_all,
7613       show_ip_bgp_ipv4_community_all_cmd,
7614       "show ip bgp ipv4 (unicast|multicast) community",
7615       SHOW_STR
7616       IP_STR
7617       BGP_STR
7618       "Address family\n"
7619       "Address Family modifier\n"
7620       "Address Family modifier\n"
7621       "Display routes matching the communities\n")
7622{
7623  if (strncmp (argv[0], "m", 1) == 0)
7624    return bgp_show (vty, NULL, AFI_IP, SAFI_MULTICAST,
7625		     bgp_show_type_community_all, NULL);
7626
7627  return bgp_show (vty, NULL, AFI_IP, SAFI_UNICAST,
7628		   bgp_show_type_community_all, NULL);
7629}
7630
7631#ifdef HAVE_IPV6
7632DEFUN (show_bgp_community_all,
7633       show_bgp_community_all_cmd,
7634       "show bgp community",
7635       SHOW_STR
7636       BGP_STR
7637       "Display routes matching the communities\n")
7638{
7639  return bgp_show (vty, NULL, AFI_IP6, SAFI_UNICAST,
7640		   bgp_show_type_community_all, NULL);
7641}
7642
7643ALIAS (show_bgp_community_all,
7644       show_bgp_ipv6_community_all_cmd,
7645       "show bgp ipv6 community",
7646       SHOW_STR
7647       BGP_STR
7648       "Address family\n"
7649       "Display routes matching the communities\n")
7650
7651/* old command */
7652DEFUN (show_ipv6_bgp_community_all,
7653       show_ipv6_bgp_community_all_cmd,
7654       "show ipv6 bgp community",
7655       SHOW_STR
7656       IPV6_STR
7657       BGP_STR
7658       "Display routes matching the communities\n")
7659{
7660  return bgp_show (vty, NULL, AFI_IP6, SAFI_UNICAST,
7661		   bgp_show_type_community_all, NULL);
7662}
7663
7664/* old command */
7665DEFUN (show_ipv6_mbgp_community_all,
7666       show_ipv6_mbgp_community_all_cmd,
7667       "show ipv6 mbgp community",
7668       SHOW_STR
7669       IPV6_STR
7670       MBGP_STR
7671       "Display routes matching the communities\n")
7672{
7673  return bgp_show (vty, NULL, AFI_IP6, SAFI_MULTICAST,
7674		   bgp_show_type_community_all, NULL);
7675}
7676#endif /* HAVE_IPV6 */
7677
7678static int
7679bgp_show_community (struct vty *vty, const char *view_name, int argc,
7680		    const char **argv, int exact, afi_t afi, safi_t safi)
7681{
7682  struct community *com;
7683  struct buffer *b;
7684  struct bgp *bgp;
7685  int i;
7686  char *str;
7687  int first = 0;
7688
7689  /* BGP structure lookup */
7690  if (view_name)
7691    {
7692      bgp = bgp_lookup_by_name (view_name);
7693      if (bgp == NULL)
7694	{
7695	  vty_out (vty, "Can't find BGP view %s%s", view_name, VTY_NEWLINE);
7696	  return CMD_WARNING;
7697	}
7698    }
7699  else
7700    {
7701      bgp = bgp_get_default ();
7702      if (bgp == NULL)
7703	{
7704	  vty_out (vty, "No BGP process is configured%s", VTY_NEWLINE);
7705	  return CMD_WARNING;
7706	}
7707    }
7708
7709  b = buffer_new (1024);
7710  for (i = 0; i < argc; i++)
7711    {
7712      if (first)
7713        buffer_putc (b, ' ');
7714      else
7715	{
7716	  if ((strcmp (argv[i], "unicast") == 0) || (strcmp (argv[i], "multicast") == 0))
7717	    continue;
7718	  first = 1;
7719	}
7720
7721      buffer_putstr (b, argv[i]);
7722    }
7723  buffer_putc (b, '\0');
7724
7725  str = buffer_getstr (b);
7726  buffer_free (b);
7727
7728  com = community_str2com (str);
7729  XFREE (MTYPE_TMP, str);
7730  if (! com)
7731    {
7732      vty_out (vty, "%% Community malformed: %s", VTY_NEWLINE);
7733      return CMD_WARNING;
7734    }
7735
7736  return bgp_show (vty, bgp, afi, safi,
7737                   (exact ? bgp_show_type_community_exact :
7738		            bgp_show_type_community), com);
7739}
7740
7741DEFUN (show_ip_bgp_community,
7742       show_ip_bgp_community_cmd,
7743       "show ip bgp community (AA:NN|local-AS|no-advertise|no-export)",
7744       SHOW_STR
7745       IP_STR
7746       BGP_STR
7747       "Display routes matching the communities\n"
7748       "community number\n"
7749       "Do not send outside local AS (well-known community)\n"
7750       "Do not advertise to any peer (well-known community)\n"
7751       "Do not export to next AS (well-known community)\n")
7752{
7753  return bgp_show_community (vty, NULL, argc, argv, 0, AFI_IP, SAFI_UNICAST);
7754}
7755
7756ALIAS (show_ip_bgp_community,
7757       show_ip_bgp_community2_cmd,
7758       "show ip bgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
7759       SHOW_STR
7760       IP_STR
7761       BGP_STR
7762       "Display routes matching the communities\n"
7763       "community number\n"
7764       "Do not send outside local AS (well-known community)\n"
7765       "Do not advertise to any peer (well-known community)\n"
7766       "Do not export to next AS (well-known community)\n"
7767       "community number\n"
7768       "Do not send outside local AS (well-known community)\n"
7769       "Do not advertise to any peer (well-known community)\n"
7770       "Do not export to next AS (well-known community)\n")
7771
7772ALIAS (show_ip_bgp_community,
7773       show_ip_bgp_community3_cmd,
7774       "show ip bgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
7775       SHOW_STR
7776       IP_STR
7777       BGP_STR
7778       "Display routes matching the communities\n"
7779       "community number\n"
7780       "Do not send outside local AS (well-known community)\n"
7781       "Do not advertise to any peer (well-known community)\n"
7782       "Do not export to next AS (well-known community)\n"
7783       "community number\n"
7784       "Do not send outside local AS (well-known community)\n"
7785       "Do not advertise to any peer (well-known community)\n"
7786       "Do not export to next AS (well-known community)\n"
7787       "community number\n"
7788       "Do not send outside local AS (well-known community)\n"
7789       "Do not advertise to any peer (well-known community)\n"
7790       "Do not export to next AS (well-known community)\n")
7791
7792ALIAS (show_ip_bgp_community,
7793       show_ip_bgp_community4_cmd,
7794       "show ip bgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
7795       SHOW_STR
7796       IP_STR
7797       BGP_STR
7798       "Display routes matching the communities\n"
7799       "community number\n"
7800       "Do not send outside local AS (well-known community)\n"
7801       "Do not advertise to any peer (well-known community)\n"
7802       "Do not export to next AS (well-known community)\n"
7803       "community number\n"
7804       "Do not send outside local AS (well-known community)\n"
7805       "Do not advertise to any peer (well-known community)\n"
7806       "Do not export to next AS (well-known community)\n"
7807       "community number\n"
7808       "Do not send outside local AS (well-known community)\n"
7809       "Do not advertise to any peer (well-known community)\n"
7810       "Do not export to next AS (well-known community)\n"
7811       "community number\n"
7812       "Do not send outside local AS (well-known community)\n"
7813       "Do not advertise to any peer (well-known community)\n"
7814       "Do not export to next AS (well-known community)\n")
7815
7816DEFUN (show_ip_bgp_ipv4_community,
7817       show_ip_bgp_ipv4_community_cmd,
7818       "show ip bgp ipv4 (unicast|multicast) community (AA:NN|local-AS|no-advertise|no-export)",
7819       SHOW_STR
7820       IP_STR
7821       BGP_STR
7822       "Address family\n"
7823       "Address Family modifier\n"
7824       "Address Family modifier\n"
7825       "Display routes matching the communities\n"
7826       "community number\n"
7827       "Do not send outside local AS (well-known community)\n"
7828       "Do not advertise to any peer (well-known community)\n"
7829       "Do not export to next AS (well-known community)\n")
7830{
7831  if (strncmp (argv[0], "m", 1) == 0)
7832    return bgp_show_community (vty, NULL, argc, argv, 0, AFI_IP, SAFI_MULTICAST);
7833
7834  return bgp_show_community (vty, NULL, argc, argv, 0, AFI_IP, SAFI_UNICAST);
7835}
7836
7837ALIAS (show_ip_bgp_ipv4_community,
7838       show_ip_bgp_ipv4_community2_cmd,
7839       "show ip bgp ipv4 (unicast|multicast) community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
7840       SHOW_STR
7841       IP_STR
7842       BGP_STR
7843       "Address family\n"
7844       "Address Family modifier\n"
7845       "Address Family modifier\n"
7846       "Display routes matching the communities\n"
7847       "community number\n"
7848       "Do not send outside local AS (well-known community)\n"
7849       "Do not advertise to any peer (well-known community)\n"
7850       "Do not export to next AS (well-known community)\n"
7851       "community number\n"
7852       "Do not send outside local AS (well-known community)\n"
7853       "Do not advertise to any peer (well-known community)\n"
7854       "Do not export to next AS (well-known community)\n")
7855
7856ALIAS (show_ip_bgp_ipv4_community,
7857       show_ip_bgp_ipv4_community3_cmd,
7858       "show ip bgp ipv4 (unicast|multicast) community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
7859       SHOW_STR
7860       IP_STR
7861       BGP_STR
7862       "Address family\n"
7863       "Address Family modifier\n"
7864       "Address Family modifier\n"
7865       "Display routes matching the communities\n"
7866       "community number\n"
7867       "Do not send outside local AS (well-known community)\n"
7868       "Do not advertise to any peer (well-known community)\n"
7869       "Do not export to next AS (well-known community)\n"
7870       "community number\n"
7871       "Do not send outside local AS (well-known community)\n"
7872       "Do not advertise to any peer (well-known community)\n"
7873       "Do not export to next AS (well-known community)\n"
7874       "community number\n"
7875       "Do not send outside local AS (well-known community)\n"
7876       "Do not advertise to any peer (well-known community)\n"
7877       "Do not export to next AS (well-known community)\n")
7878
7879ALIAS (show_ip_bgp_ipv4_community,
7880       show_ip_bgp_ipv4_community4_cmd,
7881       "show ip bgp ipv4 (unicast|multicast) community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
7882       SHOW_STR
7883       IP_STR
7884       BGP_STR
7885       "Address family\n"
7886       "Address Family modifier\n"
7887       "Address Family modifier\n"
7888       "Display routes matching the communities\n"
7889       "community number\n"
7890       "Do not send outside local AS (well-known community)\n"
7891       "Do not advertise to any peer (well-known community)\n"
7892       "Do not export to next AS (well-known community)\n"
7893       "community number\n"
7894       "Do not send outside local AS (well-known community)\n"
7895       "Do not advertise to any peer (well-known community)\n"
7896       "Do not export to next AS (well-known community)\n"
7897       "community number\n"
7898       "Do not send outside local AS (well-known community)\n"
7899       "Do not advertise to any peer (well-known community)\n"
7900       "Do not export to next AS (well-known community)\n"
7901       "community number\n"
7902       "Do not send outside local AS (well-known community)\n"
7903       "Do not advertise to any peer (well-known community)\n"
7904       "Do not export to next AS (well-known community)\n")
7905
7906DEFUN (show_bgp_view_afi_safi_community_all,
7907       show_bgp_view_afi_safi_community_all_cmd,
7908#ifdef HAVE_IPV6
7909       "show bgp view WORD (ipv4|ipv6) (unicast|multicast) community",
7910#else
7911       "show bgp view WORD ipv4 (unicast|multicast) community",
7912#endif
7913       SHOW_STR
7914       BGP_STR
7915       "BGP view\n"
7916       "View name\n"
7917       "Address family\n"
7918#ifdef HAVE_IPV6
7919       "Address family\n"
7920#endif
7921       "Address Family modifier\n"
7922       "Address Family modifier\n"
7923       "Display routes matching the communities\n")
7924{
7925  int afi;
7926  int safi;
7927  struct bgp *bgp;
7928
7929  /* BGP structure lookup. */
7930  bgp = bgp_lookup_by_name (argv[0]);
7931  if (bgp == NULL)
7932    {
7933      vty_out (vty, "Can't find BGP view %s%s", argv[0], VTY_NEWLINE);
7934      return CMD_WARNING;
7935    }
7936
7937#ifdef HAVE_IPV6
7938  afi = (strncmp (argv[1], "ipv6", 4) == 0) ? AFI_IP6 : AFI_IP;
7939  safi = (strncmp (argv[2], "m", 1) == 0) ? SAFI_MULTICAST : SAFI_UNICAST;
7940#else
7941  afi = AFI_IP;
7942  safi = (strncmp (argv[1], "m", 1) == 0) ? SAFI_MULTICAST : SAFI_UNICAST;
7943#endif
7944  return bgp_show (vty, bgp, afi, safi, bgp_show_type_community_all, NULL);
7945}
7946
7947DEFUN (show_bgp_view_afi_safi_community,
7948       show_bgp_view_afi_safi_community_cmd,
7949#ifdef HAVE_IPV6
7950       "show bgp view WORD (ipv4|ipv6) (unicast|multicast) community (AA:NN|local-AS|no-advertise|no-export)",
7951#else
7952       "show bgp view WORD ipv4 (unicast|multicast) community (AA:NN|local-AS|no-advertise|no-export)",
7953#endif
7954       SHOW_STR
7955       BGP_STR
7956       "BGP view\n"
7957       "View name\n"
7958       "Address family\n"
7959#ifdef HAVE_IPV6
7960       "Address family\n"
7961#endif
7962       "Address family modifier\n"
7963       "Address family modifier\n"
7964       "Display routes matching the communities\n"
7965       "community number\n"
7966       "Do not send outside local AS (well-known community)\n"
7967       "Do not advertise to any peer (well-known community)\n"
7968       "Do not export to next AS (well-known community)\n")
7969{
7970  int afi;
7971  int safi;
7972
7973#ifdef HAVE_IPV6
7974  afi = (strncmp (argv[1], "ipv6", 4) == 0) ? AFI_IP6 : AFI_IP;
7975  safi = (strncmp (argv[2], "m", 1) == 0) ? SAFI_MULTICAST : SAFI_UNICAST;
7976  return bgp_show_community (vty, argv[0], argc-3, &argv[3], 0, afi, safi);
7977#else
7978  afi = AFI_IP;
7979  safi = (strncmp (argv[1], "m", 1) == 0) ? SAFI_MULTICAST : SAFI_UNICAST;
7980  return bgp_show_community (vty, argv[0], argc-2, &argv[2], 0, afi, safi);
7981#endif
7982}
7983
7984ALIAS (show_bgp_view_afi_safi_community,
7985       show_bgp_view_afi_safi_community2_cmd,
7986#ifdef HAVE_IPV6
7987       "show bgp view WORD (ipv4|ipv6) (unicast|multicast) community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
7988#else
7989       "show bgp view WORD ipv4 (unicast|multicast) community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
7990#endif
7991       SHOW_STR
7992       BGP_STR
7993       "BGP view\n"
7994       "View name\n"
7995       "Address family\n"
7996#ifdef HAVE_IPV6
7997       "Address family\n"
7998#endif
7999       "Address family modifier\n"
8000       "Address family modifier\n"
8001       "Display routes matching the communities\n"
8002       "community number\n"
8003       "Do not send outside local AS (well-known community)\n"
8004       "Do not advertise to any peer (well-known community)\n"
8005       "Do not export to next AS (well-known community)\n"
8006       "community number\n"
8007       "Do not send outside local AS (well-known community)\n"
8008       "Do not advertise to any peer (well-known community)\n"
8009       "Do not export to next AS (well-known community)\n")
8010
8011ALIAS (show_bgp_view_afi_safi_community,
8012       show_bgp_view_afi_safi_community3_cmd,
8013#ifdef HAVE_IPV6
8014       "show bgp view WORD (ipv4|ipv6) (unicast|multicast) community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
8015#else
8016       "show bgp view WORD ipv4 (unicast|multicast) community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
8017#endif
8018       SHOW_STR
8019       BGP_STR
8020       "BGP view\n"
8021       "View name\n"
8022       "Address family\n"
8023#ifdef HAVE_IPV6
8024       "Address family\n"
8025#endif
8026       "Address family modifier\n"
8027       "Address family modifier\n"
8028       "Display routes matching the communities\n"
8029       "community number\n"
8030       "Do not send outside local AS (well-known community)\n"
8031       "Do not advertise to any peer (well-known community)\n"
8032       "Do not export to next AS (well-known community)\n"
8033       "community number\n"
8034       "Do not send outside local AS (well-known community)\n"
8035       "Do not advertise to any peer (well-known community)\n"
8036       "Do not export to next AS (well-known community)\n"
8037       "community number\n"
8038       "Do not send outside local AS (well-known community)\n"
8039       "Do not advertise to any peer (well-known community)\n"
8040       "Do not export to next AS (well-known community)\n")
8041
8042ALIAS (show_bgp_view_afi_safi_community,
8043       show_bgp_view_afi_safi_community4_cmd,
8044#ifdef HAVE_IPV6
8045       "show bgp view WORD (ipv4|ipv6) (unicast|multicast) community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
8046#else
8047       "show bgp view WORD ipv4 (unicast|multicast) community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
8048#endif
8049       SHOW_STR
8050       BGP_STR
8051       "BGP view\n"
8052       "View name\n"
8053       "Address family\n"
8054#ifdef HAVE_IPV6
8055       "Address family\n"
8056#endif
8057       "Address family modifier\n"
8058       "Address family modifier\n"
8059       "Display routes matching the communities\n"
8060       "community number\n"
8061       "Do not send outside local AS (well-known community)\n"
8062       "Do not advertise to any peer (well-known community)\n"
8063       "Do not export to next AS (well-known community)\n"
8064       "community number\n"
8065       "Do not send outside local AS (well-known community)\n"
8066       "Do not advertise to any peer (well-known community)\n"
8067       "Do not export to next AS (well-known community)\n"
8068       "community number\n"
8069       "Do not send outside local AS (well-known community)\n"
8070       "Do not advertise to any peer (well-known community)\n"
8071       "Do not export to next AS (well-known community)\n"
8072       "community number\n"
8073       "Do not send outside local AS (well-known community)\n"
8074       "Do not advertise to any peer (well-known community)\n"
8075       "Do not export to next AS (well-known community)\n")
8076
8077DEFUN (show_ip_bgp_community_exact,
8078       show_ip_bgp_community_exact_cmd,
8079       "show ip bgp community (AA:NN|local-AS|no-advertise|no-export) exact-match",
8080       SHOW_STR
8081       IP_STR
8082       BGP_STR
8083       "Display routes matching the communities\n"
8084       "community number\n"
8085       "Do not send outside local AS (well-known community)\n"
8086       "Do not advertise to any peer (well-known community)\n"
8087       "Do not export to next AS (well-known community)\n"
8088       "Exact match of the communities")
8089{
8090  return bgp_show_community (vty, NULL, argc, argv, 1, AFI_IP, SAFI_UNICAST);
8091}
8092
8093ALIAS (show_ip_bgp_community_exact,
8094       show_ip_bgp_community2_exact_cmd,
8095       "show ip bgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) exact-match",
8096       SHOW_STR
8097       IP_STR
8098       BGP_STR
8099       "Display routes matching the communities\n"
8100       "community number\n"
8101       "Do not send outside local AS (well-known community)\n"
8102       "Do not advertise to any peer (well-known community)\n"
8103       "Do not export to next AS (well-known community)\n"
8104       "community number\n"
8105       "Do not send outside local AS (well-known community)\n"
8106       "Do not advertise to any peer (well-known community)\n"
8107       "Do not export to next AS (well-known community)\n"
8108       "Exact match of the communities")
8109
8110ALIAS (show_ip_bgp_community_exact,
8111       show_ip_bgp_community3_exact_cmd,
8112       "show ip bgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) exact-match",
8113       SHOW_STR
8114       IP_STR
8115       BGP_STR
8116       "Display routes matching the communities\n"
8117       "community number\n"
8118       "Do not send outside local AS (well-known community)\n"
8119       "Do not advertise to any peer (well-known community)\n"
8120       "Do not export to next AS (well-known community)\n"
8121       "community number\n"
8122       "Do not send outside local AS (well-known community)\n"
8123       "Do not advertise to any peer (well-known community)\n"
8124       "Do not export to next AS (well-known community)\n"
8125       "community number\n"
8126       "Do not send outside local AS (well-known community)\n"
8127       "Do not advertise to any peer (well-known community)\n"
8128       "Do not export to next AS (well-known community)\n"
8129       "Exact match of the communities")
8130
8131ALIAS (show_ip_bgp_community_exact,
8132       show_ip_bgp_community4_exact_cmd,
8133       "show ip bgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) exact-match",
8134       SHOW_STR
8135       IP_STR
8136       BGP_STR
8137       "Display routes matching the communities\n"
8138       "community number\n"
8139       "Do not send outside local AS (well-known community)\n"
8140       "Do not advertise to any peer (well-known community)\n"
8141       "Do not export to next AS (well-known community)\n"
8142       "community number\n"
8143       "Do not send outside local AS (well-known community)\n"
8144       "Do not advertise to any peer (well-known community)\n"
8145       "Do not export to next AS (well-known community)\n"
8146       "community number\n"
8147       "Do not send outside local AS (well-known community)\n"
8148       "Do not advertise to any peer (well-known community)\n"
8149       "Do not export to next AS (well-known community)\n"
8150       "community number\n"
8151       "Do not send outside local AS (well-known community)\n"
8152       "Do not advertise to any peer (well-known community)\n"
8153       "Do not export to next AS (well-known community)\n"
8154       "Exact match of the communities")
8155
8156DEFUN (show_ip_bgp_ipv4_community_exact,
8157       show_ip_bgp_ipv4_community_exact_cmd,
8158       "show ip bgp ipv4 (unicast|multicast) community (AA:NN|local-AS|no-advertise|no-export) exact-match",
8159       SHOW_STR
8160       IP_STR
8161       BGP_STR
8162       "Address family\n"
8163       "Address Family modifier\n"
8164       "Address Family modifier\n"
8165       "Display routes matching the communities\n"
8166       "community number\n"
8167       "Do not send outside local AS (well-known community)\n"
8168       "Do not advertise to any peer (well-known community)\n"
8169       "Do not export to next AS (well-known community)\n"
8170       "Exact match of the communities")
8171{
8172  if (strncmp (argv[0], "m", 1) == 0)
8173    return bgp_show_community (vty, NULL, argc, argv, 1, AFI_IP, SAFI_MULTICAST);
8174
8175  return bgp_show_community (vty, NULL, argc, argv, 1, AFI_IP, SAFI_UNICAST);
8176}
8177
8178ALIAS (show_ip_bgp_ipv4_community_exact,
8179       show_ip_bgp_ipv4_community2_exact_cmd,
8180       "show ip bgp ipv4 (unicast|multicast) community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) exact-match",
8181       SHOW_STR
8182       IP_STR
8183       BGP_STR
8184       "Address family\n"
8185       "Address Family modifier\n"
8186       "Address Family modifier\n"
8187       "Display routes matching the communities\n"
8188       "community number\n"
8189       "Do not send outside local AS (well-known community)\n"
8190       "Do not advertise to any peer (well-known community)\n"
8191       "Do not export to next AS (well-known community)\n"
8192       "community number\n"
8193       "Do not send outside local AS (well-known community)\n"
8194       "Do not advertise to any peer (well-known community)\n"
8195       "Do not export to next AS (well-known community)\n"
8196       "Exact match of the communities")
8197
8198ALIAS (show_ip_bgp_ipv4_community_exact,
8199       show_ip_bgp_ipv4_community3_exact_cmd,
8200       "show ip bgp ipv4 (unicast|multicast) community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) exact-match",
8201       SHOW_STR
8202       IP_STR
8203       BGP_STR
8204       "Address family\n"
8205       "Address Family modifier\n"
8206       "Address Family modifier\n"
8207       "Display routes matching the communities\n"
8208       "community number\n"
8209       "Do not send outside local AS (well-known community)\n"
8210       "Do not advertise to any peer (well-known community)\n"
8211       "Do not export to next AS (well-known community)\n"
8212       "community number\n"
8213       "Do not send outside local AS (well-known community)\n"
8214       "Do not advertise to any peer (well-known community)\n"
8215       "Do not export to next AS (well-known community)\n"
8216       "community number\n"
8217       "Do not send outside local AS (well-known community)\n"
8218       "Do not advertise to any peer (well-known community)\n"
8219       "Do not export to next AS (well-known community)\n"
8220       "Exact match of the communities")
8221
8222ALIAS (show_ip_bgp_ipv4_community_exact,
8223       show_ip_bgp_ipv4_community4_exact_cmd,
8224       "show ip bgp ipv4 (unicast|multicast) community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) exact-match",
8225       SHOW_STR
8226       IP_STR
8227       BGP_STR
8228       "Address family\n"
8229       "Address Family modifier\n"
8230       "Address Family modifier\n"
8231       "Display routes matching the communities\n"
8232       "community number\n"
8233       "Do not send outside local AS (well-known community)\n"
8234       "Do not advertise to any peer (well-known community)\n"
8235       "Do not export to next AS (well-known community)\n"
8236       "community number\n"
8237       "Do not send outside local AS (well-known community)\n"
8238       "Do not advertise to any peer (well-known community)\n"
8239       "Do not export to next AS (well-known community)\n"
8240       "community number\n"
8241       "Do not send outside local AS (well-known community)\n"
8242       "Do not advertise to any peer (well-known community)\n"
8243       "Do not export to next AS (well-known community)\n"
8244       "community number\n"
8245       "Do not send outside local AS (well-known community)\n"
8246       "Do not advertise to any peer (well-known community)\n"
8247       "Do not export to next AS (well-known community)\n"
8248       "Exact match of the communities")
8249
8250#ifdef HAVE_IPV6
8251DEFUN (show_bgp_community,
8252       show_bgp_community_cmd,
8253       "show bgp community (AA:NN|local-AS|no-advertise|no-export)",
8254       SHOW_STR
8255       BGP_STR
8256       "Display routes matching the communities\n"
8257       "community number\n"
8258       "Do not send outside local AS (well-known community)\n"
8259       "Do not advertise to any peer (well-known community)\n"
8260       "Do not export to next AS (well-known community)\n")
8261{
8262  return bgp_show_community (vty, NULL, argc, argv, 0, AFI_IP6, SAFI_UNICAST);
8263}
8264
8265ALIAS (show_bgp_community,
8266       show_bgp_ipv6_community_cmd,
8267       "show bgp ipv6 community (AA:NN|local-AS|no-advertise|no-export)",
8268       SHOW_STR
8269       BGP_STR
8270       "Address family\n"
8271       "Display routes matching the communities\n"
8272       "community number\n"
8273       "Do not send outside local AS (well-known community)\n"
8274       "Do not advertise to any peer (well-known community)\n"
8275       "Do not export to next AS (well-known community)\n")
8276
8277ALIAS (show_bgp_community,
8278       show_bgp_community2_cmd,
8279       "show bgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
8280       SHOW_STR
8281       BGP_STR
8282       "Display routes matching the communities\n"
8283       "community number\n"
8284       "Do not send outside local AS (well-known community)\n"
8285       "Do not advertise to any peer (well-known community)\n"
8286       "Do not export to next AS (well-known community)\n"
8287       "community number\n"
8288       "Do not send outside local AS (well-known community)\n"
8289       "Do not advertise to any peer (well-known community)\n"
8290       "Do not export to next AS (well-known community)\n")
8291
8292ALIAS (show_bgp_community,
8293       show_bgp_ipv6_community2_cmd,
8294       "show bgp ipv6 community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
8295       SHOW_STR
8296       BGP_STR
8297       "Address family\n"
8298       "Display routes matching the communities\n"
8299       "community number\n"
8300       "Do not send outside local AS (well-known community)\n"
8301       "Do not advertise to any peer (well-known community)\n"
8302       "Do not export to next AS (well-known community)\n"
8303       "community number\n"
8304       "Do not send outside local AS (well-known community)\n"
8305       "Do not advertise to any peer (well-known community)\n"
8306       "Do not export to next AS (well-known community)\n")
8307
8308ALIAS (show_bgp_community,
8309       show_bgp_community3_cmd,
8310       "show bgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
8311       SHOW_STR
8312       BGP_STR
8313       "Display routes matching the communities\n"
8314       "community number\n"
8315       "Do not send outside local AS (well-known community)\n"
8316       "Do not advertise to any peer (well-known community)\n"
8317       "Do not export to next AS (well-known community)\n"
8318       "community number\n"
8319       "Do not send outside local AS (well-known community)\n"
8320       "Do not advertise to any peer (well-known community)\n"
8321       "Do not export to next AS (well-known community)\n"
8322       "community number\n"
8323       "Do not send outside local AS (well-known community)\n"
8324       "Do not advertise to any peer (well-known community)\n"
8325       "Do not export to next AS (well-known community)\n")
8326
8327ALIAS (show_bgp_community,
8328       show_bgp_ipv6_community3_cmd,
8329       "show bgp ipv6 community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
8330       SHOW_STR
8331       BGP_STR
8332       "Address family\n"
8333       "Display routes matching the communities\n"
8334       "community number\n"
8335       "Do not send outside local AS (well-known community)\n"
8336       "Do not advertise to any peer (well-known community)\n"
8337       "Do not export to next AS (well-known community)\n"
8338       "community number\n"
8339       "Do not send outside local AS (well-known community)\n"
8340       "Do not advertise to any peer (well-known community)\n"
8341       "Do not export to next AS (well-known community)\n"
8342       "community number\n"
8343       "Do not send outside local AS (well-known community)\n"
8344       "Do not advertise to any peer (well-known community)\n"
8345       "Do not export to next AS (well-known community)\n")
8346
8347ALIAS (show_bgp_community,
8348       show_bgp_community4_cmd,
8349       "show bgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
8350       SHOW_STR
8351       BGP_STR
8352       "Display routes matching the communities\n"
8353       "community number\n"
8354       "Do not send outside local AS (well-known community)\n"
8355       "Do not advertise to any peer (well-known community)\n"
8356       "Do not export to next AS (well-known community)\n"
8357       "community number\n"
8358       "Do not send outside local AS (well-known community)\n"
8359       "Do not advertise to any peer (well-known community)\n"
8360       "Do not export to next AS (well-known community)\n"
8361       "community number\n"
8362       "Do not send outside local AS (well-known community)\n"
8363       "Do not advertise to any peer (well-known community)\n"
8364       "Do not export to next AS (well-known community)\n"
8365       "community number\n"
8366       "Do not send outside local AS (well-known community)\n"
8367       "Do not advertise to any peer (well-known community)\n"
8368       "Do not export to next AS (well-known community)\n")
8369
8370ALIAS (show_bgp_community,
8371       show_bgp_ipv6_community4_cmd,
8372       "show bgp ipv6 community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
8373       SHOW_STR
8374       BGP_STR
8375       "Address family\n"
8376       "Display routes matching the communities\n"
8377       "community number\n"
8378       "Do not send outside local AS (well-known community)\n"
8379       "Do not advertise to any peer (well-known community)\n"
8380       "Do not export to next AS (well-known community)\n"
8381       "community number\n"
8382       "Do not send outside local AS (well-known community)\n"
8383       "Do not advertise to any peer (well-known community)\n"
8384       "Do not export to next AS (well-known community)\n"
8385       "community number\n"
8386       "Do not send outside local AS (well-known community)\n"
8387       "Do not advertise to any peer (well-known community)\n"
8388       "Do not export to next AS (well-known community)\n"
8389       "community number\n"
8390       "Do not send outside local AS (well-known community)\n"
8391       "Do not advertise to any peer (well-known community)\n"
8392       "Do not export to next AS (well-known community)\n")
8393
8394/* old command */
8395DEFUN (show_ipv6_bgp_community,
8396       show_ipv6_bgp_community_cmd,
8397       "show ipv6 bgp community (AA:NN|local-AS|no-advertise|no-export)",
8398       SHOW_STR
8399       IPV6_STR
8400       BGP_STR
8401       "Display routes matching the communities\n"
8402       "community number\n"
8403       "Do not send outside local AS (well-known community)\n"
8404       "Do not advertise to any peer (well-known community)\n"
8405       "Do not export to next AS (well-known community)\n")
8406{
8407  return bgp_show_community (vty, NULL, argc, argv, 0, AFI_IP6, SAFI_UNICAST);
8408}
8409
8410/* old command */
8411ALIAS (show_ipv6_bgp_community,
8412       show_ipv6_bgp_community2_cmd,
8413       "show ipv6 bgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
8414       SHOW_STR
8415       IPV6_STR
8416       BGP_STR
8417       "Display routes matching the communities\n"
8418       "community number\n"
8419       "Do not send outside local AS (well-known community)\n"
8420       "Do not advertise to any peer (well-known community)\n"
8421       "Do not export to next AS (well-known community)\n"
8422       "community number\n"
8423       "Do not send outside local AS (well-known community)\n"
8424       "Do not advertise to any peer (well-known community)\n"
8425       "Do not export to next AS (well-known community)\n")
8426
8427/* old command */
8428ALIAS (show_ipv6_bgp_community,
8429       show_ipv6_bgp_community3_cmd,
8430       "show ipv6 bgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
8431       SHOW_STR
8432       IPV6_STR
8433       BGP_STR
8434       "Display routes matching the communities\n"
8435       "community number\n"
8436       "Do not send outside local AS (well-known community)\n"
8437       "Do not advertise to any peer (well-known community)\n"
8438       "Do not export to next AS (well-known community)\n"
8439       "community number\n"
8440       "Do not send outside local AS (well-known community)\n"
8441       "Do not advertise to any peer (well-known community)\n"
8442       "Do not export to next AS (well-known community)\n"
8443       "community number\n"
8444       "Do not send outside local AS (well-known community)\n"
8445       "Do not advertise to any peer (well-known community)\n"
8446       "Do not export to next AS (well-known community)\n")
8447
8448/* old command */
8449ALIAS (show_ipv6_bgp_community,
8450       show_ipv6_bgp_community4_cmd,
8451       "show ipv6 bgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
8452       SHOW_STR
8453       IPV6_STR
8454       BGP_STR
8455       "Display routes matching the communities\n"
8456       "community number\n"
8457       "Do not send outside local AS (well-known community)\n"
8458       "Do not advertise to any peer (well-known community)\n"
8459       "Do not export to next AS (well-known community)\n"
8460       "community number\n"
8461       "Do not send outside local AS (well-known community)\n"
8462       "Do not advertise to any peer (well-known community)\n"
8463       "Do not export to next AS (well-known community)\n"
8464       "community number\n"
8465       "Do not send outside local AS (well-known community)\n"
8466       "Do not advertise to any peer (well-known community)\n"
8467       "Do not export to next AS (well-known community)\n"
8468       "community number\n"
8469       "Do not send outside local AS (well-known community)\n"
8470       "Do not advertise to any peer (well-known community)\n"
8471       "Do not export to next AS (well-known community)\n")
8472
8473DEFUN (show_bgp_community_exact,
8474       show_bgp_community_exact_cmd,
8475       "show bgp community (AA:NN|local-AS|no-advertise|no-export) exact-match",
8476       SHOW_STR
8477       BGP_STR
8478       "Display routes matching the communities\n"
8479       "community number\n"
8480       "Do not send outside local AS (well-known community)\n"
8481       "Do not advertise to any peer (well-known community)\n"
8482       "Do not export to next AS (well-known community)\n"
8483       "Exact match of the communities")
8484{
8485  return bgp_show_community (vty, NULL, argc, argv, 1, AFI_IP6, SAFI_UNICAST);
8486}
8487
8488ALIAS (show_bgp_community_exact,
8489       show_bgp_ipv6_community_exact_cmd,
8490       "show bgp ipv6 community (AA:NN|local-AS|no-advertise|no-export) exact-match",
8491       SHOW_STR
8492       BGP_STR
8493       "Address family\n"
8494       "Display routes matching the communities\n"
8495       "community number\n"
8496       "Do not send outside local AS (well-known community)\n"
8497       "Do not advertise to any peer (well-known community)\n"
8498       "Do not export to next AS (well-known community)\n"
8499       "Exact match of the communities")
8500
8501ALIAS (show_bgp_community_exact,
8502       show_bgp_community2_exact_cmd,
8503       "show bgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) exact-match",
8504       SHOW_STR
8505       BGP_STR
8506       "Display routes matching the communities\n"
8507       "community number\n"
8508       "Do not send outside local AS (well-known community)\n"
8509       "Do not advertise to any peer (well-known community)\n"
8510       "Do not export to next AS (well-known community)\n"
8511       "community number\n"
8512       "Do not send outside local AS (well-known community)\n"
8513       "Do not advertise to any peer (well-known community)\n"
8514       "Do not export to next AS (well-known community)\n"
8515       "Exact match of the communities")
8516
8517ALIAS (show_bgp_community_exact,
8518       show_bgp_ipv6_community2_exact_cmd,
8519       "show bgp ipv6 community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) exact-match",
8520       SHOW_STR
8521       BGP_STR
8522       "Address family\n"
8523       "Display routes matching the communities\n"
8524       "community number\n"
8525       "Do not send outside local AS (well-known community)\n"
8526       "Do not advertise to any peer (well-known community)\n"
8527       "Do not export to next AS (well-known community)\n"
8528       "community number\n"
8529       "Do not send outside local AS (well-known community)\n"
8530       "Do not advertise to any peer (well-known community)\n"
8531       "Do not export to next AS (well-known community)\n"
8532       "Exact match of the communities")
8533
8534ALIAS (show_bgp_community_exact,
8535       show_bgp_community3_exact_cmd,
8536       "show bgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) exact-match",
8537       SHOW_STR
8538       BGP_STR
8539       "Display routes matching the communities\n"
8540       "community number\n"
8541       "Do not send outside local AS (well-known community)\n"
8542       "Do not advertise to any peer (well-known community)\n"
8543       "Do not export to next AS (well-known community)\n"
8544       "community number\n"
8545       "Do not send outside local AS (well-known community)\n"
8546       "Do not advertise to any peer (well-known community)\n"
8547       "Do not export to next AS (well-known community)\n"
8548       "community number\n"
8549       "Do not send outside local AS (well-known community)\n"
8550       "Do not advertise to any peer (well-known community)\n"
8551       "Do not export to next AS (well-known community)\n"
8552       "Exact match of the communities")
8553
8554ALIAS (show_bgp_community_exact,
8555       show_bgp_ipv6_community3_exact_cmd,
8556       "show bgp ipv6 community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) exact-match",
8557       SHOW_STR
8558       BGP_STR
8559       "Address family\n"
8560       "Display routes matching the communities\n"
8561       "community number\n"
8562       "Do not send outside local AS (well-known community)\n"
8563       "Do not advertise to any peer (well-known community)\n"
8564       "Do not export to next AS (well-known community)\n"
8565       "community number\n"
8566       "Do not send outside local AS (well-known community)\n"
8567       "Do not advertise to any peer (well-known community)\n"
8568       "Do not export to next AS (well-known community)\n"
8569       "community number\n"
8570       "Do not send outside local AS (well-known community)\n"
8571       "Do not advertise to any peer (well-known community)\n"
8572       "Do not export to next AS (well-known community)\n"
8573       "Exact match of the communities")
8574
8575ALIAS (show_bgp_community_exact,
8576       show_bgp_community4_exact_cmd,
8577       "show bgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) exact-match",
8578       SHOW_STR
8579       BGP_STR
8580       "Display routes matching the communities\n"
8581       "community number\n"
8582       "Do not send outside local AS (well-known community)\n"
8583       "Do not advertise to any peer (well-known community)\n"
8584       "Do not export to next AS (well-known community)\n"
8585       "community number\n"
8586       "Do not send outside local AS (well-known community)\n"
8587       "Do not advertise to any peer (well-known community)\n"
8588       "Do not export to next AS (well-known community)\n"
8589       "community number\n"
8590       "Do not send outside local AS (well-known community)\n"
8591       "Do not advertise to any peer (well-known community)\n"
8592       "Do not export to next AS (well-known community)\n"
8593       "community number\n"
8594       "Do not send outside local AS (well-known community)\n"
8595       "Do not advertise to any peer (well-known community)\n"
8596       "Do not export to next AS (well-known community)\n"
8597       "Exact match of the communities")
8598
8599ALIAS (show_bgp_community_exact,
8600       show_bgp_ipv6_community4_exact_cmd,
8601       "show bgp ipv6 community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) exact-match",
8602       SHOW_STR
8603       BGP_STR
8604       "Address family\n"
8605       "Display routes matching the communities\n"
8606       "community number\n"
8607       "Do not send outside local AS (well-known community)\n"
8608       "Do not advertise to any peer (well-known community)\n"
8609       "Do not export to next AS (well-known community)\n"
8610       "community number\n"
8611       "Do not send outside local AS (well-known community)\n"
8612       "Do not advertise to any peer (well-known community)\n"
8613       "Do not export to next AS (well-known community)\n"
8614       "community number\n"
8615       "Do not send outside local AS (well-known community)\n"
8616       "Do not advertise to any peer (well-known community)\n"
8617       "Do not export to next AS (well-known community)\n"
8618       "community number\n"
8619       "Do not send outside local AS (well-known community)\n"
8620       "Do not advertise to any peer (well-known community)\n"
8621       "Do not export to next AS (well-known community)\n"
8622       "Exact match of the communities")
8623
8624/* old command */
8625DEFUN (show_ipv6_bgp_community_exact,
8626       show_ipv6_bgp_community_exact_cmd,
8627       "show ipv6 bgp community (AA:NN|local-AS|no-advertise|no-export) exact-match",
8628       SHOW_STR
8629       IPV6_STR
8630       BGP_STR
8631       "Display routes matching the communities\n"
8632       "community number\n"
8633       "Do not send outside local AS (well-known community)\n"
8634       "Do not advertise to any peer (well-known community)\n"
8635       "Do not export to next AS (well-known community)\n"
8636       "Exact match of the communities")
8637{
8638  return bgp_show_community (vty, NULL, argc, argv, 1, AFI_IP6, SAFI_UNICAST);
8639}
8640
8641/* old command */
8642ALIAS (show_ipv6_bgp_community_exact,
8643       show_ipv6_bgp_community2_exact_cmd,
8644       "show ipv6 bgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) exact-match",
8645       SHOW_STR
8646       IPV6_STR
8647       BGP_STR
8648       "Display routes matching the communities\n"
8649       "community number\n"
8650       "Do not send outside local AS (well-known community)\n"
8651       "Do not advertise to any peer (well-known community)\n"
8652       "Do not export to next AS (well-known community)\n"
8653       "community number\n"
8654       "Do not send outside local AS (well-known community)\n"
8655       "Do not advertise to any peer (well-known community)\n"
8656       "Do not export to next AS (well-known community)\n"
8657       "Exact match of the communities")
8658
8659/* old command */
8660ALIAS (show_ipv6_bgp_community_exact,
8661       show_ipv6_bgp_community3_exact_cmd,
8662       "show ipv6 bgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) exact-match",
8663       SHOW_STR
8664       IPV6_STR
8665       BGP_STR
8666       "Display routes matching the communities\n"
8667       "community number\n"
8668       "Do not send outside local AS (well-known community)\n"
8669       "Do not advertise to any peer (well-known community)\n"
8670       "Do not export to next AS (well-known community)\n"
8671       "community number\n"
8672       "Do not send outside local AS (well-known community)\n"
8673       "Do not advertise to any peer (well-known community)\n"
8674       "Do not export to next AS (well-known community)\n"
8675       "community number\n"
8676       "Do not send outside local AS (well-known community)\n"
8677       "Do not advertise to any peer (well-known community)\n"
8678       "Do not export to next AS (well-known community)\n"
8679       "Exact match of the communities")
8680
8681/* old command */
8682ALIAS (show_ipv6_bgp_community_exact,
8683       show_ipv6_bgp_community4_exact_cmd,
8684       "show ipv6 bgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) exact-match",
8685       SHOW_STR
8686       IPV6_STR
8687       BGP_STR
8688       "Display routes matching the communities\n"
8689       "community number\n"
8690       "Do not send outside local AS (well-known community)\n"
8691       "Do not advertise to any peer (well-known community)\n"
8692       "Do not export to next AS (well-known community)\n"
8693       "community number\n"
8694       "Do not send outside local AS (well-known community)\n"
8695       "Do not advertise to any peer (well-known community)\n"
8696       "Do not export to next AS (well-known community)\n"
8697       "community number\n"
8698       "Do not send outside local AS (well-known community)\n"
8699       "Do not advertise to any peer (well-known community)\n"
8700       "Do not export to next AS (well-known community)\n"
8701       "community number\n"
8702       "Do not send outside local AS (well-known community)\n"
8703       "Do not advertise to any peer (well-known community)\n"
8704       "Do not export to next AS (well-known community)\n"
8705       "Exact match of the communities")
8706
8707/* old command */
8708DEFUN (show_ipv6_mbgp_community,
8709       show_ipv6_mbgp_community_cmd,
8710       "show ipv6 mbgp community (AA:NN|local-AS|no-advertise|no-export)",
8711       SHOW_STR
8712       IPV6_STR
8713       MBGP_STR
8714       "Display routes matching the communities\n"
8715       "community number\n"
8716       "Do not send outside local AS (well-known community)\n"
8717       "Do not advertise to any peer (well-known community)\n"
8718       "Do not export to next AS (well-known community)\n")
8719{
8720  return bgp_show_community (vty, NULL, argc, argv, 0, AFI_IP6, SAFI_MULTICAST);
8721}
8722
8723/* old command */
8724ALIAS (show_ipv6_mbgp_community,
8725       show_ipv6_mbgp_community2_cmd,
8726       "show ipv6 mbgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
8727       SHOW_STR
8728       IPV6_STR
8729       MBGP_STR
8730       "Display routes matching the communities\n"
8731       "community number\n"
8732       "Do not send outside local AS (well-known community)\n"
8733       "Do not advertise to any peer (well-known community)\n"
8734       "Do not export to next AS (well-known community)\n"
8735       "community number\n"
8736       "Do not send outside local AS (well-known community)\n"
8737       "Do not advertise to any peer (well-known community)\n"
8738       "Do not export to next AS (well-known community)\n")
8739
8740/* old command */
8741ALIAS (show_ipv6_mbgp_community,
8742       show_ipv6_mbgp_community3_cmd,
8743       "show ipv6 mbgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
8744       SHOW_STR
8745       IPV6_STR
8746       MBGP_STR
8747       "Display routes matching the communities\n"
8748       "community number\n"
8749       "Do not send outside local AS (well-known community)\n"
8750       "Do not advertise to any peer (well-known community)\n"
8751       "Do not export to next AS (well-known community)\n"
8752       "community number\n"
8753       "Do not send outside local AS (well-known community)\n"
8754       "Do not advertise to any peer (well-known community)\n"
8755       "Do not export to next AS (well-known community)\n"
8756       "community number\n"
8757       "Do not send outside local AS (well-known community)\n"
8758       "Do not advertise to any peer (well-known community)\n"
8759       "Do not export to next AS (well-known community)\n")
8760
8761/* old command */
8762ALIAS (show_ipv6_mbgp_community,
8763       show_ipv6_mbgp_community4_cmd,
8764       "show ipv6 mbgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export)",
8765       SHOW_STR
8766       IPV6_STR
8767       MBGP_STR
8768       "Display routes matching the communities\n"
8769       "community number\n"
8770       "Do not send outside local AS (well-known community)\n"
8771       "Do not advertise to any peer (well-known community)\n"
8772       "Do not export to next AS (well-known community)\n"
8773       "community number\n"
8774       "Do not send outside local AS (well-known community)\n"
8775       "Do not advertise to any peer (well-known community)\n"
8776       "Do not export to next AS (well-known community)\n"
8777       "community number\n"
8778       "Do not send outside local AS (well-known community)\n"
8779       "Do not advertise to any peer (well-known community)\n"
8780       "Do not export to next AS (well-known community)\n"
8781       "community number\n"
8782       "Do not send outside local AS (well-known community)\n"
8783       "Do not advertise to any peer (well-known community)\n"
8784       "Do not export to next AS (well-known community)\n")
8785
8786/* old command */
8787DEFUN (show_ipv6_mbgp_community_exact,
8788       show_ipv6_mbgp_community_exact_cmd,
8789       "show ipv6 mbgp community (AA:NN|local-AS|no-advertise|no-export) exact-match",
8790       SHOW_STR
8791       IPV6_STR
8792       MBGP_STR
8793       "Display routes matching the communities\n"
8794       "community number\n"
8795       "Do not send outside local AS (well-known community)\n"
8796       "Do not advertise to any peer (well-known community)\n"
8797       "Do not export to next AS (well-known community)\n"
8798       "Exact match of the communities")
8799{
8800  return bgp_show_community (vty, NULL, argc, argv, 1, AFI_IP6, SAFI_MULTICAST);
8801}
8802
8803/* old command */
8804ALIAS (show_ipv6_mbgp_community_exact,
8805       show_ipv6_mbgp_community2_exact_cmd,
8806       "show ipv6 mbgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) exact-match",
8807       SHOW_STR
8808       IPV6_STR
8809       MBGP_STR
8810       "Display routes matching the communities\n"
8811       "community number\n"
8812       "Do not send outside local AS (well-known community)\n"
8813       "Do not advertise to any peer (well-known community)\n"
8814       "Do not export to next AS (well-known community)\n"
8815       "community number\n"
8816       "Do not send outside local AS (well-known community)\n"
8817       "Do not advertise to any peer (well-known community)\n"
8818       "Do not export to next AS (well-known community)\n"
8819       "Exact match of the communities")
8820
8821/* old command */
8822ALIAS (show_ipv6_mbgp_community_exact,
8823       show_ipv6_mbgp_community3_exact_cmd,
8824       "show ipv6 mbgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) exact-match",
8825       SHOW_STR
8826       IPV6_STR
8827       MBGP_STR
8828       "Display routes matching the communities\n"
8829       "community number\n"
8830       "Do not send outside local AS (well-known community)\n"
8831       "Do not advertise to any peer (well-known community)\n"
8832       "Do not export to next AS (well-known community)\n"
8833       "community number\n"
8834       "Do not send outside local AS (well-known community)\n"
8835       "Do not advertise to any peer (well-known community)\n"
8836       "Do not export to next AS (well-known community)\n"
8837       "community number\n"
8838       "Do not send outside local AS (well-known community)\n"
8839       "Do not advertise to any peer (well-known community)\n"
8840       "Do not export to next AS (well-known community)\n"
8841       "Exact match of the communities")
8842
8843/* old command */
8844ALIAS (show_ipv6_mbgp_community_exact,
8845       show_ipv6_mbgp_community4_exact_cmd,
8846       "show ipv6 mbgp community (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) (AA:NN|local-AS|no-advertise|no-export) exact-match",
8847       SHOW_STR
8848       IPV6_STR
8849       MBGP_STR
8850       "Display routes matching the communities\n"
8851       "community number\n"
8852       "Do not send outside local AS (well-known community)\n"
8853       "Do not advertise to any peer (well-known community)\n"
8854       "Do not export to next AS (well-known community)\n"
8855       "community number\n"
8856       "Do not send outside local AS (well-known community)\n"
8857       "Do not advertise to any peer (well-known community)\n"
8858       "Do not export to next AS (well-known community)\n"
8859       "community number\n"
8860       "Do not send outside local AS (well-known community)\n"
8861       "Do not advertise to any peer (well-known community)\n"
8862       "Do not export to next AS (well-known community)\n"
8863       "community number\n"
8864       "Do not send outside local AS (well-known community)\n"
8865       "Do not advertise to any peer (well-known community)\n"
8866       "Do not export to next AS (well-known community)\n"
8867       "Exact match of the communities")
8868#endif /* HAVE_IPV6 */
8869
8870static int
8871bgp_show_community_list (struct vty *vty, const char *com, int exact,
8872			 afi_t afi, safi_t safi)
8873{
8874  struct community_list *list;
8875
8876  list = community_list_lookup (bgp_clist, com, COMMUNITY_LIST_MASTER);
8877  if (list == NULL)
8878    {
8879      vty_out (vty, "%% %s is not a valid community-list name%s", com,
8880	       VTY_NEWLINE);
8881      return CMD_WARNING;
8882    }
8883
8884  return bgp_show (vty, NULL, afi, safi,
8885                   (exact ? bgp_show_type_community_list_exact :
8886		            bgp_show_type_community_list), list);
8887}
8888
8889DEFUN (show_ip_bgp_community_list,
8890       show_ip_bgp_community_list_cmd,
8891       "show ip bgp community-list (<1-500>|WORD)",
8892       SHOW_STR
8893       IP_STR
8894       BGP_STR
8895       "Display routes matching the community-list\n"
8896       "community-list number\n"
8897       "community-list name\n")
8898{
8899  return bgp_show_community_list (vty, argv[0], 0, AFI_IP, SAFI_UNICAST);
8900}
8901
8902DEFUN (show_ip_bgp_ipv4_community_list,
8903       show_ip_bgp_ipv4_community_list_cmd,
8904       "show ip bgp ipv4 (unicast|multicast) community-list (<1-500>|WORD)",
8905       SHOW_STR
8906       IP_STR
8907       BGP_STR
8908       "Address family\n"
8909       "Address Family modifier\n"
8910       "Address Family modifier\n"
8911       "Display routes matching the community-list\n"
8912       "community-list number\n"
8913       "community-list name\n")
8914{
8915  if (strncmp (argv[0], "m", 1) == 0)
8916    return bgp_show_community_list (vty, argv[1], 0, AFI_IP, SAFI_MULTICAST);
8917
8918  return bgp_show_community_list (vty, argv[1], 0, AFI_IP, SAFI_UNICAST);
8919}
8920
8921DEFUN (show_ip_bgp_community_list_exact,
8922       show_ip_bgp_community_list_exact_cmd,
8923       "show ip bgp community-list (<1-500>|WORD) exact-match",
8924       SHOW_STR
8925       IP_STR
8926       BGP_STR
8927       "Display routes matching the community-list\n"
8928       "community-list number\n"
8929       "community-list name\n"
8930       "Exact match of the communities\n")
8931{
8932  return bgp_show_community_list (vty, argv[0], 1, AFI_IP, SAFI_UNICAST);
8933}
8934
8935DEFUN (show_ip_bgp_ipv4_community_list_exact,
8936       show_ip_bgp_ipv4_community_list_exact_cmd,
8937       "show ip bgp ipv4 (unicast|multicast) community-list (<1-500>|WORD) exact-match",
8938       SHOW_STR
8939       IP_STR
8940       BGP_STR
8941       "Address family\n"
8942       "Address Family modifier\n"
8943       "Address Family modifier\n"
8944       "Display routes matching the community-list\n"
8945       "community-list number\n"
8946       "community-list name\n"
8947       "Exact match of the communities\n")
8948{
8949  if (strncmp (argv[0], "m", 1) == 0)
8950    return bgp_show_community_list (vty, argv[1], 1, AFI_IP, SAFI_MULTICAST);
8951
8952  return bgp_show_community_list (vty, argv[1], 1, AFI_IP, SAFI_UNICAST);
8953}
8954
8955#ifdef HAVE_IPV6
8956DEFUN (show_bgp_community_list,
8957       show_bgp_community_list_cmd,
8958       "show bgp community-list (<1-500>|WORD)",
8959       SHOW_STR
8960       BGP_STR
8961       "Display routes matching the community-list\n"
8962       "community-list number\n"
8963       "community-list name\n")
8964{
8965  return bgp_show_community_list (vty, argv[0], 0, AFI_IP6, SAFI_UNICAST);
8966}
8967
8968ALIAS (show_bgp_community_list,
8969       show_bgp_ipv6_community_list_cmd,
8970       "show bgp ipv6 community-list (<1-500>|WORD)",
8971       SHOW_STR
8972       BGP_STR
8973       "Address family\n"
8974       "Display routes matching the community-list\n"
8975       "community-list number\n"
8976       "community-list name\n")
8977
8978/* old command */
8979DEFUN (show_ipv6_bgp_community_list,
8980       show_ipv6_bgp_community_list_cmd,
8981       "show ipv6 bgp community-list WORD",
8982       SHOW_STR
8983       IPV6_STR
8984       BGP_STR
8985       "Display routes matching the community-list\n"
8986       "community-list name\n")
8987{
8988  return bgp_show_community_list (vty, argv[0], 0, AFI_IP6, SAFI_UNICAST);
8989}
8990
8991/* old command */
8992DEFUN (show_ipv6_mbgp_community_list,
8993       show_ipv6_mbgp_community_list_cmd,
8994       "show ipv6 mbgp community-list WORD",
8995       SHOW_STR
8996       IPV6_STR
8997       MBGP_STR
8998       "Display routes matching the community-list\n"
8999       "community-list name\n")
9000{
9001  return bgp_show_community_list (vty, argv[0], 0, AFI_IP6, SAFI_MULTICAST);
9002}
9003
9004DEFUN (show_bgp_community_list_exact,
9005       show_bgp_community_list_exact_cmd,
9006       "show bgp community-list (<1-500>|WORD) exact-match",
9007       SHOW_STR
9008       BGP_STR
9009       "Display routes matching the community-list\n"
9010       "community-list number\n"
9011       "community-list name\n"
9012       "Exact match of the communities\n")
9013{
9014  return bgp_show_community_list (vty, argv[0], 1, AFI_IP6, SAFI_UNICAST);
9015}
9016
9017ALIAS (show_bgp_community_list_exact,
9018       show_bgp_ipv6_community_list_exact_cmd,
9019       "show bgp ipv6 community-list (<1-500>|WORD) exact-match",
9020       SHOW_STR
9021       BGP_STR
9022       "Address family\n"
9023       "Display routes matching the community-list\n"
9024       "community-list number\n"
9025       "community-list name\n"
9026       "Exact match of the communities\n")
9027
9028/* old command */
9029DEFUN (show_ipv6_bgp_community_list_exact,
9030       show_ipv6_bgp_community_list_exact_cmd,
9031       "show ipv6 bgp community-list WORD exact-match",
9032       SHOW_STR
9033       IPV6_STR
9034       BGP_STR
9035       "Display routes matching the community-list\n"
9036       "community-list name\n"
9037       "Exact match of the communities\n")
9038{
9039  return bgp_show_community_list (vty, argv[0], 1, AFI_IP6, SAFI_UNICAST);
9040}
9041
9042/* old command */
9043DEFUN (show_ipv6_mbgp_community_list_exact,
9044       show_ipv6_mbgp_community_list_exact_cmd,
9045       "show ipv6 mbgp community-list WORD exact-match",
9046       SHOW_STR
9047       IPV6_STR
9048       MBGP_STR
9049       "Display routes matching the community-list\n"
9050       "community-list name\n"
9051       "Exact match of the communities\n")
9052{
9053  return bgp_show_community_list (vty, argv[0], 1, AFI_IP6, SAFI_MULTICAST);
9054}
9055#endif /* HAVE_IPV6 */
9056
9057static int
9058bgp_show_prefix_longer (struct vty *vty, const char *prefix, afi_t afi,
9059			safi_t safi, enum bgp_show_type type)
9060{
9061  int ret;
9062  struct prefix *p;
9063
9064  p = prefix_new();
9065
9066  ret = str2prefix (prefix, p);
9067  if (! ret)
9068    {
9069      vty_out (vty, "%% Malformed Prefix%s", VTY_NEWLINE);
9070      return CMD_WARNING;
9071    }
9072
9073  ret = bgp_show (vty, NULL, afi, safi, type, p);
9074  prefix_free(p);
9075  return ret;
9076}
9077
9078DEFUN (show_ip_bgp_prefix_longer,
9079       show_ip_bgp_prefix_longer_cmd,
9080       "show ip bgp A.B.C.D/M longer-prefixes",
9081       SHOW_STR
9082       IP_STR
9083       BGP_STR
9084       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
9085       "Display route and more specific routes\n")
9086{
9087  return bgp_show_prefix_longer (vty, argv[0], AFI_IP, SAFI_UNICAST,
9088				 bgp_show_type_prefix_longer);
9089}
9090
9091DEFUN (show_ip_bgp_flap_prefix_longer,
9092       show_ip_bgp_flap_prefix_longer_cmd,
9093       "show ip bgp flap-statistics A.B.C.D/M longer-prefixes",
9094       SHOW_STR
9095       IP_STR
9096       BGP_STR
9097       "Display flap statistics of routes\n"
9098       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
9099       "Display route and more specific routes\n")
9100{
9101  return bgp_show_prefix_longer (vty, argv[0], AFI_IP, SAFI_UNICAST,
9102				 bgp_show_type_flap_prefix_longer);
9103}
9104
9105DEFUN (show_ip_bgp_ipv4_prefix_longer,
9106       show_ip_bgp_ipv4_prefix_longer_cmd,
9107       "show ip bgp ipv4 (unicast|multicast) A.B.C.D/M longer-prefixes",
9108       SHOW_STR
9109       IP_STR
9110       BGP_STR
9111       "Address family\n"
9112       "Address Family modifier\n"
9113       "Address Family modifier\n"
9114       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
9115       "Display route and more specific routes\n")
9116{
9117  if (strncmp (argv[0], "m", 1) == 0)
9118    return bgp_show_prefix_longer (vty, argv[1], AFI_IP, SAFI_MULTICAST,
9119				   bgp_show_type_prefix_longer);
9120
9121  return bgp_show_prefix_longer (vty, argv[1], AFI_IP, SAFI_UNICAST,
9122				 bgp_show_type_prefix_longer);
9123}
9124
9125DEFUN (show_ip_bgp_flap_address,
9126       show_ip_bgp_flap_address_cmd,
9127       "show ip bgp flap-statistics A.B.C.D",
9128       SHOW_STR
9129       IP_STR
9130       BGP_STR
9131       "Display flap statistics of routes\n"
9132       "Network in the BGP routing table to display\n")
9133{
9134  return bgp_show_prefix_longer (vty, argv[0], AFI_IP, SAFI_UNICAST,
9135				 bgp_show_type_flap_address);
9136}
9137
9138DEFUN (show_ip_bgp_flap_prefix,
9139       show_ip_bgp_flap_prefix_cmd,
9140       "show ip bgp flap-statistics A.B.C.D/M",
9141       SHOW_STR
9142       IP_STR
9143       BGP_STR
9144       "Display flap statistics of routes\n"
9145       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
9146{
9147  return bgp_show_prefix_longer (vty, argv[0], AFI_IP, SAFI_UNICAST,
9148				 bgp_show_type_flap_prefix);
9149}
9150#ifdef HAVE_IPV6
9151DEFUN (show_bgp_prefix_longer,
9152       show_bgp_prefix_longer_cmd,
9153       "show bgp X:X::X:X/M longer-prefixes",
9154       SHOW_STR
9155       BGP_STR
9156       "IPv6 prefix <network>/<length>\n"
9157       "Display route and more specific routes\n")
9158{
9159  return bgp_show_prefix_longer (vty, argv[0], AFI_IP6, SAFI_UNICAST,
9160				 bgp_show_type_prefix_longer);
9161}
9162
9163ALIAS (show_bgp_prefix_longer,
9164       show_bgp_ipv6_prefix_longer_cmd,
9165       "show bgp ipv6 X:X::X:X/M longer-prefixes",
9166       SHOW_STR
9167       BGP_STR
9168       "Address family\n"
9169       "IPv6 prefix <network>/<length>\n"
9170       "Display route and more specific routes\n")
9171
9172/* old command */
9173DEFUN (show_ipv6_bgp_prefix_longer,
9174       show_ipv6_bgp_prefix_longer_cmd,
9175       "show ipv6 bgp X:X::X:X/M longer-prefixes",
9176       SHOW_STR
9177       IPV6_STR
9178       BGP_STR
9179       "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n"
9180       "Display route and more specific routes\n")
9181{
9182  return bgp_show_prefix_longer (vty, argv[0], AFI_IP6, SAFI_UNICAST,
9183				 bgp_show_type_prefix_longer);
9184}
9185
9186/* old command */
9187DEFUN (show_ipv6_mbgp_prefix_longer,
9188       show_ipv6_mbgp_prefix_longer_cmd,
9189       "show ipv6 mbgp X:X::X:X/M longer-prefixes",
9190       SHOW_STR
9191       IPV6_STR
9192       MBGP_STR
9193       "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n"
9194       "Display route and more specific routes\n")
9195{
9196  return bgp_show_prefix_longer (vty, argv[0], AFI_IP6, SAFI_MULTICAST,
9197				 bgp_show_type_prefix_longer);
9198}
9199#endif /* HAVE_IPV6 */
9200
9201static struct peer *
9202peer_lookup_in_view (struct vty *vty, const char *view_name,
9203                     const char *ip_str)
9204{
9205  int ret;
9206  struct bgp *bgp;
9207  struct peer *peer;
9208  union sockunion su;
9209
9210  /* BGP structure lookup. */
9211  if (view_name)
9212    {
9213      bgp = bgp_lookup_by_name (view_name);
9214      if (! bgp)
9215        {
9216          vty_out (vty, "Can't find BGP view %s%s", view_name, VTY_NEWLINE);
9217          return NULL;
9218        }
9219    }
9220  else
9221    {
9222      bgp = bgp_get_default ();
9223      if (! bgp)
9224        {
9225          vty_out (vty, "No BGP process is configured%s", VTY_NEWLINE);
9226          return NULL;
9227        }
9228    }
9229
9230  /* Get peer sockunion. */
9231  ret = str2sockunion (ip_str, &su);
9232  if (ret < 0)
9233    {
9234      vty_out (vty, "Malformed address: %s%s", ip_str, VTY_NEWLINE);
9235      return NULL;
9236    }
9237
9238  /* Peer structure lookup. */
9239  peer = peer_lookup (bgp, &su);
9240  if (! peer)
9241    {
9242      vty_out (vty, "No such neighbor%s", VTY_NEWLINE);
9243      return NULL;
9244    }
9245
9246  return peer;
9247}
9248
9249enum bgp_stats
9250{
9251  BGP_STATS_MAXBITLEN = 0,
9252  BGP_STATS_RIB,
9253  BGP_STATS_PREFIXES,
9254  BGP_STATS_TOTPLEN,
9255  BGP_STATS_UNAGGREGATEABLE,
9256  BGP_STATS_MAX_AGGREGATEABLE,
9257  BGP_STATS_AGGREGATES,
9258  BGP_STATS_SPACE,
9259  BGP_STATS_ASPATH_COUNT,
9260  BGP_STATS_ASPATH_MAXHOPS,
9261  BGP_STATS_ASPATH_TOTHOPS,
9262  BGP_STATS_ASPATH_MAXSIZE,
9263  BGP_STATS_ASPATH_TOTSIZE,
9264  BGP_STATS_ASN_HIGHEST,
9265  BGP_STATS_MAX,
9266};
9267
9268static const char *table_stats_strs[] =
9269{
9270  [BGP_STATS_PREFIXES]            = "Total Prefixes",
9271  [BGP_STATS_TOTPLEN]             = "Average prefix length",
9272  [BGP_STATS_RIB]                 = "Total Advertisements",
9273  [BGP_STATS_UNAGGREGATEABLE]     = "Unaggregateable prefixes",
9274  [BGP_STATS_MAX_AGGREGATEABLE]   = "Maximum aggregateable prefixes",
9275  [BGP_STATS_AGGREGATES]          = "BGP Aggregate advertisements",
9276  [BGP_STATS_SPACE]               = "Address space advertised",
9277  [BGP_STATS_ASPATH_COUNT]        = "Advertisements with paths",
9278  [BGP_STATS_ASPATH_MAXHOPS]      = "Longest AS-Path (hops)",
9279  [BGP_STATS_ASPATH_MAXSIZE]      = "Largest AS-Path (bytes)",
9280  [BGP_STATS_ASPATH_TOTHOPS]      = "Average AS-Path length (hops)",
9281  [BGP_STATS_ASPATH_TOTSIZE]      = "Average AS-Path size (bytes)",
9282  [BGP_STATS_ASN_HIGHEST]         = "Highest public ASN",
9283  [BGP_STATS_MAX] = NULL,
9284};
9285
9286struct bgp_table_stats
9287{
9288  struct bgp_table *table;
9289  unsigned long long counts[BGP_STATS_MAX];
9290};
9291
9292#if 0
9293#define TALLY_SIGFIG 100000
9294static unsigned long
9295ravg_tally (unsigned long count, unsigned long oldavg, unsigned long newval)
9296{
9297  unsigned long newtot = (count-1) * oldavg + (newval * TALLY_SIGFIG);
9298  unsigned long res = (newtot * TALLY_SIGFIG) / count;
9299  unsigned long ret = newtot / count;
9300
9301  if ((res % TALLY_SIGFIG) > (TALLY_SIGFIG/2))
9302    return ret + 1;
9303  else
9304    return ret;
9305}
9306#endif
9307
9308static int
9309bgp_table_stats_walker (struct thread *t)
9310{
9311  struct bgp_node *rn;
9312  struct bgp_node *top;
9313  struct bgp_table_stats *ts = THREAD_ARG (t);
9314  unsigned int space = 0;
9315
9316  if (!(top = bgp_table_top (ts->table)))
9317    return 0;
9318
9319  switch (top->p.family)
9320    {
9321      case AF_INET:
9322        space = IPV4_MAX_BITLEN;
9323        break;
9324      case AF_INET6:
9325        space = IPV6_MAX_BITLEN;
9326        break;
9327    }
9328
9329  ts->counts[BGP_STATS_MAXBITLEN] = space;
9330
9331  for (rn = top; rn; rn = bgp_route_next (rn))
9332    {
9333      struct bgp_info *ri;
9334      struct bgp_node *prn = bgp_node_parent_nolock (rn);
9335      unsigned int rinum = 0;
9336
9337      if (rn == top)
9338        continue;
9339
9340      if (!rn->info)
9341        continue;
9342
9343      ts->counts[BGP_STATS_PREFIXES]++;
9344      ts->counts[BGP_STATS_TOTPLEN] += rn->p.prefixlen;
9345
9346#if 0
9347      ts->counts[BGP_STATS_AVGPLEN]
9348        = ravg_tally (ts->counts[BGP_STATS_PREFIXES],
9349                      ts->counts[BGP_STATS_AVGPLEN],
9350                      rn->p.prefixlen);
9351#endif
9352
9353      /* check if the prefix is included by any other announcements */
9354      while (prn && !prn->info)
9355        prn = bgp_node_parent_nolock (prn);
9356
9357      if (prn == NULL || prn == top)
9358        {
9359          ts->counts[BGP_STATS_UNAGGREGATEABLE]++;
9360          /* announced address space */
9361          if (space)
9362            ts->counts[BGP_STATS_SPACE] += 1 << (space - rn->p.prefixlen);
9363        }
9364      else if (prn->info)
9365        ts->counts[BGP_STATS_MAX_AGGREGATEABLE]++;
9366
9367      for (ri = rn->info; ri; ri = ri->next)
9368        {
9369          rinum++;
9370          ts->counts[BGP_STATS_RIB]++;
9371
9372          if (ri->attr &&
9373              (CHECK_FLAG (ri->attr->flag,
9374                           ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE))))
9375            ts->counts[BGP_STATS_AGGREGATES]++;
9376
9377          /* as-path stats */
9378          if (ri->attr && ri->attr->aspath)
9379            {
9380              unsigned int hops = aspath_count_hops (ri->attr->aspath);
9381              unsigned int size = aspath_size (ri->attr->aspath);
9382              as_t highest = aspath_highest (ri->attr->aspath);
9383
9384              ts->counts[BGP_STATS_ASPATH_COUNT]++;
9385
9386              if (hops > ts->counts[BGP_STATS_ASPATH_MAXHOPS])
9387                ts->counts[BGP_STATS_ASPATH_MAXHOPS] = hops;
9388
9389              if (size > ts->counts[BGP_STATS_ASPATH_MAXSIZE])
9390                ts->counts[BGP_STATS_ASPATH_MAXSIZE] = size;
9391
9392              ts->counts[BGP_STATS_ASPATH_TOTHOPS] += hops;
9393              ts->counts[BGP_STATS_ASPATH_TOTSIZE] += size;
9394#if 0
9395              ts->counts[BGP_STATS_ASPATH_AVGHOPS]
9396                = ravg_tally (ts->counts[BGP_STATS_ASPATH_COUNT],
9397                              ts->counts[BGP_STATS_ASPATH_AVGHOPS],
9398                              hops);
9399              ts->counts[BGP_STATS_ASPATH_AVGSIZE]
9400                = ravg_tally (ts->counts[BGP_STATS_ASPATH_COUNT],
9401                              ts->counts[BGP_STATS_ASPATH_AVGSIZE],
9402                              size);
9403#endif
9404              if (highest > ts->counts[BGP_STATS_ASN_HIGHEST])
9405                ts->counts[BGP_STATS_ASN_HIGHEST] = highest;
9406            }
9407        }
9408    }
9409  return 0;
9410}
9411
9412static int
9413bgp_table_stats (struct vty *vty, struct bgp *bgp, afi_t afi, safi_t safi)
9414{
9415  struct bgp_table_stats ts;
9416  unsigned int i;
9417
9418  if (!bgp->rib[afi][safi])
9419    {
9420      vty_out (vty, "%% No RIB exist for the AFI/SAFI%s", VTY_NEWLINE);
9421      return CMD_WARNING;
9422    }
9423
9424  memset (&ts, 0, sizeof (ts));
9425  ts.table = bgp->rib[afi][safi];
9426  thread_execute (bm->master, bgp_table_stats_walker, &ts, 0);
9427
9428  vty_out (vty, "BGP %s RIB statistics%s%s",
9429           afi_safi_print (afi, safi), VTY_NEWLINE, VTY_NEWLINE);
9430
9431  for (i = 0; i < BGP_STATS_MAX; i++)
9432    {
9433      if (!table_stats_strs[i])
9434        continue;
9435
9436      switch (i)
9437        {
9438#if 0
9439          case BGP_STATS_ASPATH_AVGHOPS:
9440          case BGP_STATS_ASPATH_AVGSIZE:
9441          case BGP_STATS_AVGPLEN:
9442            vty_out (vty, "%-30s: ", table_stats_strs[i]);
9443            vty_out (vty, "%12.2f",
9444                     (float)ts.counts[i] / (float)TALLY_SIGFIG);
9445            break;
9446#endif
9447          case BGP_STATS_ASPATH_TOTHOPS:
9448          case BGP_STATS_ASPATH_TOTSIZE:
9449            vty_out (vty, "%-30s: ", table_stats_strs[i]);
9450            vty_out (vty, "%12.2f",
9451                     ts.counts[i] ?
9452                     (float)ts.counts[i] /
9453                      (float)ts.counts[BGP_STATS_ASPATH_COUNT]
9454                     : 0);
9455            break;
9456          case BGP_STATS_TOTPLEN:
9457            vty_out (vty, "%-30s: ", table_stats_strs[i]);
9458            vty_out (vty, "%12.2f",
9459                     ts.counts[i] ?
9460                     (float)ts.counts[i] /
9461                      (float)ts.counts[BGP_STATS_PREFIXES]
9462                     : 0);
9463            break;
9464          case BGP_STATS_SPACE:
9465            vty_out (vty, "%-30s: ", table_stats_strs[i]);
9466            vty_out (vty, "%12llu%s", ts.counts[i], VTY_NEWLINE);
9467            if (ts.counts[BGP_STATS_MAXBITLEN] < 9)
9468              break;
9469            vty_out (vty, "%30s: ", "%% announced ");
9470            vty_out (vty, "%12.2f%s",
9471                     100 * (float)ts.counts[BGP_STATS_SPACE] /
9472                       (float)((uint64_t)1UL << ts.counts[BGP_STATS_MAXBITLEN]),
9473                       VTY_NEWLINE);
9474            vty_out (vty, "%30s: ", "/8 equivalent ");
9475            vty_out (vty, "%12.2f%s",
9476                     (float)ts.counts[BGP_STATS_SPACE] /
9477                       (float)(1UL << (ts.counts[BGP_STATS_MAXBITLEN] - 8)),
9478                     VTY_NEWLINE);
9479            if (ts.counts[BGP_STATS_MAXBITLEN] < 25)
9480              break;
9481            vty_out (vty, "%30s: ", "/24 equivalent ");
9482            vty_out (vty, "%12.2f",
9483                     (float)ts.counts[BGP_STATS_SPACE] /
9484                       (float)(1UL << (ts.counts[BGP_STATS_MAXBITLEN] - 24)));
9485            break;
9486          default:
9487            vty_out (vty, "%-30s: ", table_stats_strs[i]);
9488            vty_out (vty, "%12llu", ts.counts[i]);
9489        }
9490
9491      vty_out (vty, "%s", VTY_NEWLINE);
9492    }
9493  return CMD_SUCCESS;
9494}
9495
9496static int
9497bgp_table_stats_vty (struct vty *vty, const char *name,
9498                     const char *afi_str, const char *safi_str)
9499{
9500  struct bgp *bgp;
9501  afi_t afi;
9502  safi_t safi;
9503
9504 if (name)
9505    bgp = bgp_lookup_by_name (name);
9506  else
9507    bgp = bgp_get_default ();
9508
9509  if (!bgp)
9510    {
9511      vty_out (vty, "%% No such BGP instance exist%s", VTY_NEWLINE);
9512      return CMD_WARNING;
9513    }
9514  if (strncmp (afi_str, "ipv", 3) == 0)
9515    {
9516      if (strncmp (afi_str, "ipv4", 4) == 0)
9517        afi = AFI_IP;
9518      else if (strncmp (afi_str, "ipv6", 4) == 0)
9519        afi = AFI_IP6;
9520      else
9521        {
9522          vty_out (vty, "%% Invalid address family %s%s",
9523                   afi_str, VTY_NEWLINE);
9524          return CMD_WARNING;
9525        }
9526      if (strncmp (safi_str, "m", 1) == 0)
9527        safi = SAFI_MULTICAST;
9528      else if (strncmp (safi_str, "u", 1) == 0)
9529        safi = SAFI_UNICAST;
9530      else if (strncmp (safi_str, "vpnv4", 5) == 0 || strncmp (safi_str, "vpnv6", 5) == 0)
9531        safi = SAFI_MPLS_LABELED_VPN;
9532      else
9533        {
9534          vty_out (vty, "%% Invalid subsequent address family %s%s",
9535                   safi_str, VTY_NEWLINE);
9536          return CMD_WARNING;
9537        }
9538    }
9539  else
9540    {
9541      vty_out (vty, "%% Invalid address family %s%s",
9542               afi_str, VTY_NEWLINE);
9543      return CMD_WARNING;
9544    }
9545
9546  return bgp_table_stats (vty, bgp, afi, safi);
9547}
9548
9549DEFUN (show_bgp_statistics,
9550       show_bgp_statistics_cmd,
9551       "show bgp (ipv4|ipv6) (unicast|multicast) statistics",
9552       SHOW_STR
9553       BGP_STR
9554       "Address family\n"
9555       "Address family\n"
9556       "Address Family modifier\n"
9557       "Address Family modifier\n"
9558       "BGP RIB advertisement statistics\n")
9559{
9560  return bgp_table_stats_vty (vty, NULL, argv[0], argv[1]);
9561}
9562
9563ALIAS (show_bgp_statistics,
9564       show_bgp_statistics_vpnv4_cmd,
9565       "show bgp (ipv4) (vpnv4) statistics",
9566       SHOW_STR
9567       BGP_STR
9568       "Address family\n"
9569       "Address Family modifier\n"
9570       "BGP RIB advertisement statistics\n")
9571
9572DEFUN (show_bgp_statistics_view,
9573       show_bgp_statistics_view_cmd,
9574       "show bgp view WORD (ipv4|ipv6) (unicast|multicast) statistics",
9575       SHOW_STR
9576       BGP_STR
9577       "BGP view\n"
9578       "Address family\n"
9579       "Address family\n"
9580       "Address Family modifier\n"
9581       "Address Family modifier\n"
9582       "BGP RIB advertisement statistics\n")
9583{
9584  return bgp_table_stats_vty (vty, NULL, argv[0], argv[1]);
9585}
9586
9587ALIAS (show_bgp_statistics_view,
9588       show_bgp_statistics_view_vpnv4_cmd,
9589       "show bgp view WORD (ipv4) (vpnv4) statistics",
9590       SHOW_STR
9591       BGP_STR
9592       "BGP view\n"
9593       "Address family\n"
9594       "Address Family modifier\n"
9595       "BGP RIB advertisement statistics\n")
9596
9597enum bgp_pcounts
9598{
9599  PCOUNT_ADJ_IN = 0,
9600  PCOUNT_DAMPED,
9601  PCOUNT_REMOVED,
9602  PCOUNT_HISTORY,
9603  PCOUNT_STALE,
9604  PCOUNT_VALID,
9605  PCOUNT_ALL,
9606  PCOUNT_COUNTED,
9607  PCOUNT_PFCNT, /* the figure we display to users */
9608  PCOUNT_MAX,
9609};
9610
9611static const char *pcount_strs[] =
9612{
9613  [PCOUNT_ADJ_IN]  = "Adj-in",
9614  [PCOUNT_DAMPED]  = "Damped",
9615  [PCOUNT_REMOVED] = "Removed",
9616  [PCOUNT_HISTORY] = "History",
9617  [PCOUNT_STALE]   = "Stale",
9618  [PCOUNT_VALID]   = "Valid",
9619  [PCOUNT_ALL]     = "All RIB",
9620  [PCOUNT_COUNTED] = "PfxCt counted",
9621  [PCOUNT_PFCNT]   = "Useable",
9622  [PCOUNT_MAX]     = NULL,
9623};
9624
9625struct peer_pcounts
9626{
9627  unsigned int count[PCOUNT_MAX];
9628  const struct peer *peer;
9629  const struct bgp_table *table;
9630};
9631
9632static int
9633bgp_peer_count_walker (struct thread *t)
9634{
9635  struct bgp_node *rn;
9636  struct peer_pcounts *pc = THREAD_ARG (t);
9637  const struct peer *peer = pc->peer;
9638
9639  for (rn = bgp_table_top (pc->table); rn; rn = bgp_route_next (rn))
9640    {
9641      struct bgp_adj_in *ain;
9642      struct bgp_info *ri;
9643
9644      for (ain = rn->adj_in; ain; ain = ain->next)
9645        if (ain->peer == peer)
9646          pc->count[PCOUNT_ADJ_IN]++;
9647
9648      for (ri = rn->info; ri; ri = ri->next)
9649        {
9650          char buf[SU_ADDRSTRLEN];
9651
9652          if (ri->peer != peer)
9653            continue;
9654
9655          pc->count[PCOUNT_ALL]++;
9656
9657          if (CHECK_FLAG (ri->flags, BGP_INFO_DAMPED))
9658            pc->count[PCOUNT_DAMPED]++;
9659          if (CHECK_FLAG (ri->flags, BGP_INFO_HISTORY))
9660            pc->count[PCOUNT_HISTORY]++;
9661          if (CHECK_FLAG (ri->flags, BGP_INFO_REMOVED))
9662            pc->count[PCOUNT_REMOVED]++;
9663          if (CHECK_FLAG (ri->flags, BGP_INFO_STALE))
9664            pc->count[PCOUNT_STALE]++;
9665          if (CHECK_FLAG (ri->flags, BGP_INFO_VALID))
9666            pc->count[PCOUNT_VALID]++;
9667          if (!CHECK_FLAG (ri->flags, BGP_INFO_UNUSEABLE))
9668            pc->count[PCOUNT_PFCNT]++;
9669
9670          if (CHECK_FLAG (ri->flags, BGP_INFO_COUNTED))
9671            {
9672              pc->count[PCOUNT_COUNTED]++;
9673              if (CHECK_FLAG (ri->flags, BGP_INFO_UNUSEABLE))
9674                plog_warn (peer->log,
9675                           "%s [pcount] %s/%d is counted but flags 0x%x",
9676                           peer->host,
9677                           inet_ntop(rn->p.family, &rn->p.u.prefix,
9678                                     buf, SU_ADDRSTRLEN),
9679                           rn->p.prefixlen,
9680                           ri->flags);
9681            }
9682          else
9683            {
9684              if (!CHECK_FLAG (ri->flags, BGP_INFO_UNUSEABLE))
9685                plog_warn (peer->log,
9686                           "%s [pcount] %s/%d not counted but flags 0x%x",
9687                           peer->host,
9688                           inet_ntop(rn->p.family, &rn->p.u.prefix,
9689                                     buf, SU_ADDRSTRLEN),
9690                           rn->p.prefixlen,
9691                           ri->flags);
9692            }
9693        }
9694    }
9695  return 0;
9696}
9697
9698static int
9699bgp_peer_counts (struct vty *vty, struct peer *peer, afi_t afi, safi_t safi)
9700{
9701  struct peer_pcounts pcounts = { .peer = peer };
9702  unsigned int i;
9703
9704  if (!peer || !peer->bgp || !peer->afc[afi][safi]
9705      || !peer->bgp->rib[afi][safi])
9706    {
9707      vty_out (vty, "%% No such neighbor or address family%s", VTY_NEWLINE);
9708      return CMD_WARNING;
9709    }
9710
9711  memset (&pcounts, 0, sizeof(pcounts));
9712  pcounts.peer = peer;
9713  pcounts.table = peer->bgp->rib[afi][safi];
9714
9715  /* in-place call via thread subsystem so as to record execution time
9716   * stats for the thread-walk (i.e. ensure this can't be blamed on
9717   * on just vty_read()).
9718   */
9719  thread_execute (bm->master, bgp_peer_count_walker, &pcounts, 0);
9720
9721  vty_out (vty, "Prefix counts for %s, %s%s",
9722           peer->host, afi_safi_print (afi, safi), VTY_NEWLINE);
9723  vty_out (vty, "PfxCt: %ld%s", peer->pcount[afi][safi], VTY_NEWLINE);
9724  vty_out (vty, "%sCounts from RIB table walk:%s%s",
9725           VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
9726
9727  for (i = 0; i < PCOUNT_MAX; i++)
9728      vty_out (vty, "%20s: %-10d%s",
9729               pcount_strs[i], pcounts.count[i], VTY_NEWLINE);
9730
9731  if (pcounts.count[PCOUNT_PFCNT] != peer->pcount[afi][safi])
9732    {
9733      vty_out (vty, "%s [pcount] PfxCt drift!%s",
9734               peer->host, VTY_NEWLINE);
9735      vty_out (vty, "Please report this bug, with the above command output%s",
9736              VTY_NEWLINE);
9737    }
9738
9739  return CMD_SUCCESS;
9740}
9741
9742DEFUN (show_ip_bgp_neighbor_prefix_counts,
9743       show_ip_bgp_neighbor_prefix_counts_cmd,
9744       "show ip bgp neighbors (A.B.C.D|X:X::X:X) prefix-counts",
9745       SHOW_STR
9746       IP_STR
9747       BGP_STR
9748       "Detailed information on TCP and BGP neighbor connections\n"
9749       "Neighbor to display information about\n"
9750       "Neighbor to display information about\n"
9751       "Display detailed prefix count information\n")
9752{
9753  struct peer *peer;
9754
9755  peer = peer_lookup_in_view (vty, NULL, argv[0]);
9756  if (! peer)
9757    return CMD_WARNING;
9758
9759  return bgp_peer_counts (vty, peer, AFI_IP, SAFI_UNICAST);
9760}
9761
9762DEFUN (show_bgp_ipv6_neighbor_prefix_counts,
9763       show_bgp_ipv6_neighbor_prefix_counts_cmd,
9764       "show bgp ipv6 neighbors (A.B.C.D|X:X::X:X) prefix-counts",
9765       SHOW_STR
9766       BGP_STR
9767       "Address family\n"
9768       "Detailed information on TCP and BGP neighbor connections\n"
9769       "Neighbor to display information about\n"
9770       "Neighbor to display information about\n"
9771       "Display detailed prefix count information\n")
9772{
9773  struct peer *peer;
9774
9775  peer = peer_lookup_in_view (vty, NULL, argv[0]);
9776  if (! peer)
9777    return CMD_WARNING;
9778
9779  return bgp_peer_counts (vty, peer, AFI_IP6, SAFI_UNICAST);
9780}
9781
9782DEFUN (show_ip_bgp_ipv4_neighbor_prefix_counts,
9783       show_ip_bgp_ipv4_neighbor_prefix_counts_cmd,
9784       "show ip bgp ipv4 (unicast|multicast) neighbors (A.B.C.D|X:X::X:X) prefix-counts",
9785       SHOW_STR
9786       IP_STR
9787       BGP_STR
9788       "Address family\n"
9789       "Address Family modifier\n"
9790       "Address Family modifier\n"
9791       "Detailed information on TCP and BGP neighbor connections\n"
9792       "Neighbor to display information about\n"
9793       "Neighbor to display information about\n"
9794       "Display detailed prefix count information\n")
9795{
9796  struct peer *peer;
9797
9798  peer = peer_lookup_in_view (vty, NULL, argv[1]);
9799  if (! peer)
9800    return CMD_WARNING;
9801
9802  if (strncmp (argv[0], "m", 1) == 0)
9803    return bgp_peer_counts (vty, peer, AFI_IP, SAFI_MULTICAST);
9804
9805  return bgp_peer_counts (vty, peer, AFI_IP, SAFI_UNICAST);
9806}
9807
9808DEFUN (show_ip_bgp_vpnv4_neighbor_prefix_counts,
9809       show_ip_bgp_vpnv4_neighbor_prefix_counts_cmd,
9810       "show ip bgp vpnv4 all neighbors (A.B.C.D|X:X::X:X) prefix-counts",
9811       SHOW_STR
9812       IP_STR
9813       BGP_STR
9814       "Address family\n"
9815       "Address Family modifier\n"
9816       "Address Family modifier\n"
9817       "Detailed information on TCP and BGP neighbor connections\n"
9818       "Neighbor to display information about\n"
9819       "Neighbor to display information about\n"
9820       "Display detailed prefix count information\n")
9821{
9822  struct peer *peer;
9823
9824  peer = peer_lookup_in_view (vty, NULL, argv[0]);
9825  if (! peer)
9826    return CMD_WARNING;
9827
9828  return bgp_peer_counts (vty, peer, AFI_IP, SAFI_MPLS_VPN);
9829}
9830
9831
9832static void
9833show_adj_route (struct vty *vty, struct peer *peer, afi_t afi, safi_t safi,
9834		int in)
9835{
9836  struct bgp_table *table;
9837  struct bgp_adj_in *ain;
9838  struct bgp_adj_out *adj;
9839  unsigned long output_count;
9840  struct bgp_node *rn;
9841  int header1 = 1;
9842  struct bgp *bgp;
9843  int header2 = 1;
9844
9845  bgp = peer->bgp;
9846
9847  if (! bgp)
9848    return;
9849
9850  table = bgp->rib[afi][safi];
9851
9852  output_count = 0;
9853
9854  if (! in && CHECK_FLAG (peer->af_sflags[afi][safi],
9855			  PEER_STATUS_DEFAULT_ORIGINATE))
9856    {
9857      vty_out (vty, "BGP table version is 0, local router ID is %s%s", inet_ntoa (bgp->router_id), VTY_NEWLINE);
9858      vty_out (vty, BGP_SHOW_SCODE_HEADER, VTY_NEWLINE, VTY_NEWLINE);
9859      vty_out (vty, BGP_SHOW_OCODE_HEADER, VTY_NEWLINE, VTY_NEWLINE);
9860
9861      vty_out (vty, "Originating default network 0.0.0.0%s%s",
9862	       VTY_NEWLINE, VTY_NEWLINE);
9863      header1 = 0;
9864    }
9865
9866  for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
9867    if (in)
9868      {
9869	for (ain = rn->adj_in; ain; ain = ain->next)
9870	  if (ain->peer == peer)
9871	    {
9872	      if (header1)
9873		{
9874		  vty_out (vty, "BGP table version is 0, local router ID is %s%s", inet_ntoa (bgp->router_id), VTY_NEWLINE);
9875		  vty_out (vty, BGP_SHOW_SCODE_HEADER, VTY_NEWLINE, VTY_NEWLINE);
9876		  vty_out (vty, BGP_SHOW_OCODE_HEADER, VTY_NEWLINE, VTY_NEWLINE);
9877		  header1 = 0;
9878		}
9879	      if (header2)
9880		{
9881		  vty_out (vty, BGP_SHOW_HEADER, VTY_NEWLINE);
9882		  header2 = 0;
9883		}
9884	      if (ain->attr)
9885		{
9886		  route_vty_out_tmp (vty, &rn->p, ain->attr, safi);
9887		  output_count++;
9888		}
9889	    }
9890      }
9891    else
9892      {
9893	for (adj = rn->adj_out; adj; adj = adj->next)
9894	  if (adj->peer == peer)
9895	    {
9896	      if (header1)
9897		{
9898		  vty_out (vty, "BGP table version is 0, local router ID is %s%s", inet_ntoa (bgp->router_id), VTY_NEWLINE);
9899		  vty_out (vty, BGP_SHOW_SCODE_HEADER, VTY_NEWLINE, VTY_NEWLINE);
9900		  vty_out (vty, BGP_SHOW_OCODE_HEADER, VTY_NEWLINE, VTY_NEWLINE);
9901		  header1 = 0;
9902		}
9903	      if (header2)
9904		{
9905		  vty_out (vty, BGP_SHOW_HEADER, VTY_NEWLINE);
9906		  header2 = 0;
9907		}
9908	      if (adj->attr)
9909		{
9910		  route_vty_out_tmp (vty, &rn->p, adj->attr, safi);
9911		  output_count++;
9912		}
9913	    }
9914      }
9915
9916  if (output_count != 0)
9917    vty_out (vty, "%sTotal number of prefixes %ld%s",
9918	     VTY_NEWLINE, output_count, VTY_NEWLINE);
9919}
9920
9921static int
9922peer_adj_routes (struct vty *vty, struct peer *peer, afi_t afi, safi_t safi, int in)
9923{
9924  if (! peer || ! peer->afc[afi][safi])
9925    {
9926      vty_out (vty, "%% No such neighbor or address family%s", VTY_NEWLINE);
9927      return CMD_WARNING;
9928    }
9929
9930  if (in && ! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SOFT_RECONFIG))
9931    {
9932      vty_out (vty, "%% Inbound soft reconfiguration not enabled%s",
9933	       VTY_NEWLINE);
9934      return CMD_WARNING;
9935    }
9936
9937  show_adj_route (vty, peer, afi, safi, in);
9938
9939  return CMD_SUCCESS;
9940}
9941
9942DEFUN (show_ip_bgp_view_neighbor_advertised_route,
9943       show_ip_bgp_view_neighbor_advertised_route_cmd,
9944       "show ip bgp view WORD neighbors (A.B.C.D|X:X::X:X) advertised-routes",
9945       SHOW_STR
9946       IP_STR
9947       BGP_STR
9948       "BGP view\n"
9949       "View name\n"
9950       "Detailed information on TCP and BGP neighbor connections\n"
9951       "Neighbor to display information about\n"
9952       "Neighbor to display information about\n"
9953       "Display the routes advertised to a BGP neighbor\n")
9954{
9955  struct peer *peer;
9956
9957  if (argc == 2)
9958    peer = peer_lookup_in_view (vty, argv[0], argv[1]);
9959  else
9960    peer = peer_lookup_in_view (vty, NULL, argv[0]);
9961
9962  if (! peer)
9963    return CMD_WARNING;
9964
9965  return peer_adj_routes (vty, peer, AFI_IP, SAFI_UNICAST, 0);
9966}
9967
9968ALIAS (show_ip_bgp_view_neighbor_advertised_route,
9969       show_ip_bgp_neighbor_advertised_route_cmd,
9970       "show ip bgp neighbors (A.B.C.D|X:X::X:X) advertised-routes",
9971       SHOW_STR
9972       IP_STR
9973       BGP_STR
9974       "Detailed information on TCP and BGP neighbor connections\n"
9975       "Neighbor to display information about\n"
9976       "Neighbor to display information about\n"
9977       "Display the routes advertised to a BGP neighbor\n")
9978
9979DEFUN (show_ip_bgp_ipv4_neighbor_advertised_route,
9980       show_ip_bgp_ipv4_neighbor_advertised_route_cmd,
9981       "show ip bgp ipv4 (unicast|multicast) neighbors (A.B.C.D|X:X::X:X) advertised-routes",
9982       SHOW_STR
9983       IP_STR
9984       BGP_STR
9985       "Address family\n"
9986       "Address Family modifier\n"
9987       "Address Family modifier\n"
9988       "Detailed information on TCP and BGP neighbor connections\n"
9989       "Neighbor to display information about\n"
9990       "Neighbor to display information about\n"
9991       "Display the routes advertised to a BGP neighbor\n")
9992{
9993  struct peer *peer;
9994
9995  peer = peer_lookup_in_view (vty, NULL, argv[1]);
9996  if (! peer)
9997    return CMD_WARNING;
9998
9999  if (strncmp (argv[0], "m", 1) == 0)
10000    return peer_adj_routes (vty, peer, AFI_IP, SAFI_MULTICAST, 0);
10001
10002  return peer_adj_routes (vty, peer, AFI_IP, SAFI_UNICAST, 0);
10003}
10004
10005#ifdef HAVE_IPV6
10006DEFUN (show_bgp_view_neighbor_advertised_route,
10007       show_bgp_view_neighbor_advertised_route_cmd,
10008       "show bgp view WORD neighbors (A.B.C.D|X:X::X:X) advertised-routes",
10009       SHOW_STR
10010       BGP_STR
10011       "BGP view\n"
10012       "View name\n"
10013       "Detailed information on TCP and BGP neighbor connections\n"
10014       "Neighbor to display information about\n"
10015       "Neighbor to display information about\n"
10016       "Display the routes advertised to a BGP neighbor\n")
10017{
10018  struct peer *peer;
10019
10020  if (argc == 2)
10021    peer = peer_lookup_in_view (vty, argv[0], argv[1]);
10022  else
10023    peer = peer_lookup_in_view (vty, NULL, argv[0]);
10024
10025  if (! peer)
10026    return CMD_WARNING;
10027
10028  return peer_adj_routes (vty, peer, AFI_IP6, SAFI_UNICAST, 0);
10029}
10030
10031ALIAS (show_bgp_view_neighbor_advertised_route,
10032       show_bgp_view_ipv6_neighbor_advertised_route_cmd,
10033       "show bgp view WORD ipv6 neighbors (A.B.C.D|X:X::X:X) advertised-routes",
10034       SHOW_STR
10035       BGP_STR
10036       "BGP view\n"
10037       "View name\n"
10038       "Address family\n"
10039       "Detailed information on TCP and BGP neighbor connections\n"
10040       "Neighbor to display information about\n"
10041       "Neighbor to display information about\n"
10042       "Display the routes advertised to a BGP neighbor\n")
10043
10044DEFUN (show_bgp_view_neighbor_received_routes,
10045       show_bgp_view_neighbor_received_routes_cmd,
10046       "show bgp view WORD neighbors (A.B.C.D|X:X::X:X) received-routes",
10047       SHOW_STR
10048       BGP_STR
10049       "BGP view\n"
10050       "View name\n"
10051       "Detailed information on TCP and BGP neighbor connections\n"
10052       "Neighbor to display information about\n"
10053       "Neighbor to display information about\n"
10054       "Display the received routes from neighbor\n")
10055{
10056  struct peer *peer;
10057
10058  if (argc == 2)
10059    peer = peer_lookup_in_view (vty, argv[0], argv[1]);
10060  else
10061    peer = peer_lookup_in_view (vty, NULL, argv[0]);
10062
10063  if (! peer)
10064    return CMD_WARNING;
10065
10066  return peer_adj_routes (vty, peer, AFI_IP6, SAFI_UNICAST, 1);
10067}
10068
10069ALIAS (show_bgp_view_neighbor_received_routes,
10070       show_bgp_view_ipv6_neighbor_received_routes_cmd,
10071       "show bgp view WORD ipv6 neighbors (A.B.C.D|X:X::X:X) received-routes",
10072       SHOW_STR
10073       BGP_STR
10074       "BGP view\n"
10075       "View name\n"
10076       "Address family\n"
10077       "Detailed information on TCP and BGP neighbor connections\n"
10078       "Neighbor to display information about\n"
10079       "Neighbor to display information about\n"
10080       "Display the received routes from neighbor\n")
10081
10082ALIAS (show_bgp_view_neighbor_advertised_route,
10083       show_bgp_neighbor_advertised_route_cmd,
10084       "show bgp neighbors (A.B.C.D|X:X::X:X) advertised-routes",
10085       SHOW_STR
10086       BGP_STR
10087       "Detailed information on TCP and BGP neighbor connections\n"
10088       "Neighbor to display information about\n"
10089       "Neighbor to display information about\n"
10090       "Display the routes advertised to a BGP neighbor\n")
10091
10092ALIAS (show_bgp_view_neighbor_advertised_route,
10093       show_bgp_ipv6_neighbor_advertised_route_cmd,
10094       "show bgp ipv6 neighbors (A.B.C.D|X:X::X:X) advertised-routes",
10095       SHOW_STR
10096       BGP_STR
10097       "Address family\n"
10098       "Detailed information on TCP and BGP neighbor connections\n"
10099       "Neighbor to display information about\n"
10100       "Neighbor to display information about\n"
10101       "Display the routes advertised to a BGP neighbor\n")
10102
10103/* old command */
10104ALIAS (show_bgp_view_neighbor_advertised_route,
10105       ipv6_bgp_neighbor_advertised_route_cmd,
10106       "show ipv6 bgp neighbors (A.B.C.D|X:X::X:X) advertised-routes",
10107       SHOW_STR
10108       IPV6_STR
10109       BGP_STR
10110       "Detailed information on TCP and BGP neighbor connections\n"
10111       "Neighbor to display information about\n"
10112       "Neighbor to display information about\n"
10113       "Display the routes advertised to a BGP neighbor\n")
10114
10115/* old command */
10116DEFUN (ipv6_mbgp_neighbor_advertised_route,
10117       ipv6_mbgp_neighbor_advertised_route_cmd,
10118       "show ipv6 mbgp neighbors (A.B.C.D|X:X::X:X) advertised-routes",
10119       SHOW_STR
10120       IPV6_STR
10121       MBGP_STR
10122       "Detailed information on TCP and BGP neighbor connections\n"
10123       "Neighbor to display information about\n"
10124       "Neighbor to display information about\n"
10125       "Display the routes advertised to a BGP neighbor\n")
10126{
10127  struct peer *peer;
10128
10129  peer = peer_lookup_in_view (vty, NULL, argv[0]);
10130  if (! peer)
10131    return CMD_WARNING;
10132
10133  return peer_adj_routes (vty, peer, AFI_IP6, SAFI_MULTICAST, 0);
10134}
10135#endif /* HAVE_IPV6 */
10136
10137DEFUN (show_ip_bgp_view_neighbor_received_routes,
10138       show_ip_bgp_view_neighbor_received_routes_cmd,
10139       "show ip bgp view WORD neighbors (A.B.C.D|X:X::X:X) received-routes",
10140       SHOW_STR
10141       IP_STR
10142       BGP_STR
10143       "BGP view\n"
10144       "View name\n"
10145       "Detailed information on TCP and BGP neighbor connections\n"
10146       "Neighbor to display information about\n"
10147       "Neighbor to display information about\n"
10148       "Display the received routes from neighbor\n")
10149{
10150  struct peer *peer;
10151
10152  if (argc == 2)
10153    peer = peer_lookup_in_view (vty, argv[0], argv[1]);
10154  else
10155    peer = peer_lookup_in_view (vty, NULL, argv[0]);
10156
10157  if (! peer)
10158    return CMD_WARNING;
10159
10160  return peer_adj_routes (vty, peer, AFI_IP, SAFI_UNICAST, 1);
10161}
10162
10163ALIAS (show_ip_bgp_view_neighbor_received_routes,
10164       show_ip_bgp_neighbor_received_routes_cmd,
10165       "show ip bgp neighbors (A.B.C.D|X:X::X:X) received-routes",
10166       SHOW_STR
10167       IP_STR
10168       BGP_STR
10169       "Detailed information on TCP and BGP neighbor connections\n"
10170       "Neighbor to display information about\n"
10171       "Neighbor to display information about\n"
10172       "Display the received routes from neighbor\n")
10173
10174DEFUN (show_ip_bgp_ipv4_neighbor_received_routes,
10175       show_ip_bgp_ipv4_neighbor_received_routes_cmd,
10176       "show ip bgp ipv4 (unicast|multicast) neighbors (A.B.C.D|X:X::X:X) received-routes",
10177       SHOW_STR
10178       IP_STR
10179       BGP_STR
10180       "Address family\n"
10181       "Address Family modifier\n"
10182       "Address Family modifier\n"
10183       "Detailed information on TCP and BGP neighbor connections\n"
10184       "Neighbor to display information about\n"
10185       "Neighbor to display information about\n"
10186       "Display the received routes from neighbor\n")
10187{
10188  struct peer *peer;
10189
10190  peer = peer_lookup_in_view (vty, NULL, argv[1]);
10191  if (! peer)
10192    return CMD_WARNING;
10193
10194  if (strncmp (argv[0], "m", 1) == 0)
10195    return peer_adj_routes (vty, peer, AFI_IP, SAFI_MULTICAST, 1);
10196
10197  return peer_adj_routes (vty, peer, AFI_IP, SAFI_UNICAST, 1);
10198}
10199
10200DEFUN (show_bgp_view_afi_safi_neighbor_adv_recd_routes,
10201       show_bgp_view_afi_safi_neighbor_adv_recd_routes_cmd,
10202#ifdef HAVE_IPV6
10203       "show bgp view WORD (ipv4|ipv6) (unicast|multicast) neighbors (A.B.C.D|X:X::X:X) (advertised-routes|received-routes)",
10204#else
10205       "show bgp view WORD ipv4 (unicast|multicast) neighbors (A.B.C.D|X:X::X:X) (advertised-routes|received-routes)",
10206#endif
10207       SHOW_STR
10208       BGP_STR
10209       "BGP view\n"
10210       "View name\n"
10211       "Address family\n"
10212#ifdef HAVE_IPV6
10213       "Address family\n"
10214#endif
10215       "Address family modifier\n"
10216       "Address family modifier\n"
10217       "Detailed information on TCP and BGP neighbor connections\n"
10218       "Neighbor to display information about\n"
10219       "Neighbor to display information about\n"
10220       "Display the advertised routes to neighbor\n"
10221       "Display the received routes from neighbor\n")
10222{
10223  int afi;
10224  int safi;
10225  int in;
10226  struct peer *peer;
10227
10228#ifdef HAVE_IPV6
10229    peer = peer_lookup_in_view (vty, argv[0], argv[3]);
10230#else
10231    peer = peer_lookup_in_view (vty, argv[0], argv[2]);
10232#endif
10233
10234  if (! peer)
10235    return CMD_WARNING;
10236
10237#ifdef HAVE_IPV6
10238  afi = (strncmp (argv[1], "ipv6", 4) == 0) ? AFI_IP6 : AFI_IP;
10239  safi = (strncmp (argv[2], "m", 1) == 0) ? SAFI_MULTICAST : SAFI_UNICAST;
10240  in = (strncmp (argv[4], "r", 1) == 0) ? 1 : 0;
10241#else
10242  afi = AFI_IP;
10243  safi = (strncmp (argv[1], "m", 1) == 0) ? SAFI_MULTICAST : SAFI_UNICAST;
10244  in = (strncmp (argv[3], "r", 1) == 0) ? 1 : 0;
10245#endif
10246
10247  return peer_adj_routes (vty, peer, afi, safi, in);
10248}
10249
10250DEFUN (show_ip_bgp_neighbor_received_prefix_filter,
10251       show_ip_bgp_neighbor_received_prefix_filter_cmd,
10252       "show ip bgp neighbors (A.B.C.D|X:X::X:X) received prefix-filter",
10253       SHOW_STR
10254       IP_STR
10255       BGP_STR
10256       "Detailed information on TCP and BGP neighbor connections\n"
10257       "Neighbor to display information about\n"
10258       "Neighbor to display information about\n"
10259       "Display information received from a BGP neighbor\n"
10260       "Display the prefixlist filter\n")
10261{
10262  char name[BUFSIZ];
10263  union sockunion su;
10264  struct peer *peer;
10265  int count, ret;
10266
10267  ret = str2sockunion (argv[0], &su);
10268  if (ret < 0)
10269    {
10270      vty_out (vty, "Malformed address: %s%s", argv[0], VTY_NEWLINE);
10271      return CMD_WARNING;
10272    }
10273
10274  peer = peer_lookup (NULL, &su);
10275  if (! peer)
10276    return CMD_WARNING;
10277
10278  sprintf (name, "%s.%d.%d", peer->host, AFI_IP, SAFI_UNICAST);
10279  count =  prefix_bgp_show_prefix_list (NULL, AFI_IP, name);
10280  if (count)
10281    {
10282      vty_out (vty, "Address family: IPv4 Unicast%s", VTY_NEWLINE);
10283      prefix_bgp_show_prefix_list (vty, AFI_IP, name);
10284    }
10285
10286  return CMD_SUCCESS;
10287}
10288
10289DEFUN (show_ip_bgp_ipv4_neighbor_received_prefix_filter,
10290       show_ip_bgp_ipv4_neighbor_received_prefix_filter_cmd,
10291       "show ip bgp ipv4 (unicast|multicast) neighbors (A.B.C.D|X:X::X:X) received prefix-filter",
10292       SHOW_STR
10293       IP_STR
10294       BGP_STR
10295       "Address family\n"
10296       "Address Family modifier\n"
10297       "Address Family modifier\n"
10298       "Detailed information on TCP and BGP neighbor connections\n"
10299       "Neighbor to display information about\n"
10300       "Neighbor to display information about\n"
10301       "Display information received from a BGP neighbor\n"
10302       "Display the prefixlist filter\n")
10303{
10304  char name[BUFSIZ];
10305  union sockunion su;
10306  struct peer *peer;
10307  int count, ret;
10308
10309  ret = str2sockunion (argv[1], &su);
10310  if (ret < 0)
10311    {
10312      vty_out (vty, "Malformed address: %s%s", argv[1], VTY_NEWLINE);
10313      return CMD_WARNING;
10314    }
10315
10316  peer = peer_lookup (NULL, &su);
10317  if (! peer)
10318    return CMD_WARNING;
10319
10320  if (strncmp (argv[0], "m", 1) == 0)
10321    {
10322      sprintf (name, "%s.%d.%d", peer->host, AFI_IP, SAFI_MULTICAST);
10323      count =  prefix_bgp_show_prefix_list (NULL, AFI_IP, name);
10324      if (count)
10325	{
10326	  vty_out (vty, "Address family: IPv4 Multicast%s", VTY_NEWLINE);
10327	  prefix_bgp_show_prefix_list (vty, AFI_IP, name);
10328	}
10329    }
10330  else
10331    {
10332      sprintf (name, "%s.%d.%d", peer->host, AFI_IP, SAFI_UNICAST);
10333      count =  prefix_bgp_show_prefix_list (NULL, AFI_IP, name);
10334      if (count)
10335	{
10336	  vty_out (vty, "Address family: IPv4 Unicast%s", VTY_NEWLINE);
10337	  prefix_bgp_show_prefix_list (vty, AFI_IP, name);
10338	}
10339    }
10340
10341  return CMD_SUCCESS;
10342}
10343
10344
10345#ifdef HAVE_IPV6
10346ALIAS (show_bgp_view_neighbor_received_routes,
10347       show_bgp_neighbor_received_routes_cmd,
10348       "show bgp neighbors (A.B.C.D|X:X::X:X) received-routes",
10349       SHOW_STR
10350       BGP_STR
10351       "Detailed information on TCP and BGP neighbor connections\n"
10352       "Neighbor to display information about\n"
10353       "Neighbor to display information about\n"
10354       "Display the received routes from neighbor\n")
10355
10356ALIAS (show_bgp_view_neighbor_received_routes,
10357       show_bgp_ipv6_neighbor_received_routes_cmd,
10358       "show bgp ipv6 neighbors (A.B.C.D|X:X::X:X) received-routes",
10359       SHOW_STR
10360       BGP_STR
10361       "Address family\n"
10362       "Detailed information on TCP and BGP neighbor connections\n"
10363       "Neighbor to display information about\n"
10364       "Neighbor to display information about\n"
10365       "Display the received routes from neighbor\n")
10366
10367DEFUN (show_bgp_neighbor_received_prefix_filter,
10368       show_bgp_neighbor_received_prefix_filter_cmd,
10369       "show bgp neighbors (A.B.C.D|X:X::X:X) received prefix-filter",
10370       SHOW_STR
10371       BGP_STR
10372       "Detailed information on TCP and BGP neighbor connections\n"
10373       "Neighbor to display information about\n"
10374       "Neighbor to display information about\n"
10375       "Display information received from a BGP neighbor\n"
10376       "Display the prefixlist filter\n")
10377{
10378  char name[BUFSIZ];
10379  union sockunion su;
10380  struct peer *peer;
10381  int count, ret;
10382
10383  ret = str2sockunion (argv[0], &su);
10384  if (ret < 0)
10385    {
10386      vty_out (vty, "Malformed address: %s%s", argv[0], VTY_NEWLINE);
10387      return CMD_WARNING;
10388    }
10389
10390  peer = peer_lookup (NULL, &su);
10391  if (! peer)
10392    return CMD_WARNING;
10393
10394  sprintf (name, "%s.%d.%d", peer->host, AFI_IP6, SAFI_UNICAST);
10395  count =  prefix_bgp_show_prefix_list (NULL, AFI_IP6, name);
10396  if (count)
10397    {
10398      vty_out (vty, "Address family: IPv6 Unicast%s", VTY_NEWLINE);
10399      prefix_bgp_show_prefix_list (vty, AFI_IP6, name);
10400    }
10401
10402  return CMD_SUCCESS;
10403}
10404
10405ALIAS (show_bgp_neighbor_received_prefix_filter,
10406       show_bgp_ipv6_neighbor_received_prefix_filter_cmd,
10407       "show bgp ipv6 neighbors (A.B.C.D|X:X::X:X) received prefix-filter",
10408       SHOW_STR
10409       BGP_STR
10410       "Address family\n"
10411       "Detailed information on TCP and BGP neighbor connections\n"
10412       "Neighbor to display information about\n"
10413       "Neighbor to display information about\n"
10414       "Display information received from a BGP neighbor\n"
10415       "Display the prefixlist filter\n")
10416
10417/* old command */
10418ALIAS (show_bgp_view_neighbor_received_routes,
10419       ipv6_bgp_neighbor_received_routes_cmd,
10420       "show ipv6 bgp neighbors (A.B.C.D|X:X::X:X) received-routes",
10421       SHOW_STR
10422       IPV6_STR
10423       BGP_STR
10424       "Detailed information on TCP and BGP neighbor connections\n"
10425       "Neighbor to display information about\n"
10426       "Neighbor to display information about\n"
10427       "Display the received routes from neighbor\n")
10428
10429/* old command */
10430DEFUN (ipv6_mbgp_neighbor_received_routes,
10431       ipv6_mbgp_neighbor_received_routes_cmd,
10432       "show ipv6 mbgp neighbors (A.B.C.D|X:X::X:X) received-routes",
10433       SHOW_STR
10434       IPV6_STR
10435       MBGP_STR
10436       "Detailed information on TCP and BGP neighbor connections\n"
10437       "Neighbor to display information about\n"
10438       "Neighbor to display information about\n"
10439       "Display the received routes from neighbor\n")
10440{
10441  struct peer *peer;
10442
10443  peer = peer_lookup_in_view (vty, NULL, argv[0]);
10444  if (! peer)
10445    return CMD_WARNING;
10446
10447  return peer_adj_routes (vty, peer, AFI_IP6, SAFI_MULTICAST, 1);
10448}
10449
10450DEFUN (show_bgp_view_neighbor_received_prefix_filter,
10451       show_bgp_view_neighbor_received_prefix_filter_cmd,
10452       "show bgp view WORD neighbors (A.B.C.D|X:X::X:X) received prefix-filter",
10453       SHOW_STR
10454       BGP_STR
10455       "BGP view\n"
10456       "View name\n"
10457       "Detailed information on TCP and BGP neighbor connections\n"
10458       "Neighbor to display information about\n"
10459       "Neighbor to display information about\n"
10460       "Display information received from a BGP neighbor\n"
10461       "Display the prefixlist filter\n")
10462{
10463  char name[BUFSIZ];
10464  union sockunion su;
10465  struct peer *peer;
10466  struct bgp *bgp;
10467  int count, ret;
10468
10469  /* BGP structure lookup. */
10470  bgp = bgp_lookup_by_name (argv[0]);
10471  if (bgp == NULL)
10472  {
10473	  vty_out (vty, "Can't find BGP view %s%s", argv[0], VTY_NEWLINE);
10474	  return CMD_WARNING;
10475	}
10476
10477  ret = str2sockunion (argv[1], &su);
10478  if (ret < 0)
10479    {
10480      vty_out (vty, "Malformed address: %s%s", argv[1], VTY_NEWLINE);
10481      return CMD_WARNING;
10482    }
10483
10484  peer = peer_lookup (bgp, &su);
10485  if (! peer)
10486    return CMD_WARNING;
10487
10488  sprintf (name, "%s.%d.%d", peer->host, AFI_IP6, SAFI_UNICAST);
10489  count =  prefix_bgp_show_prefix_list (NULL, AFI_IP6, name);
10490  if (count)
10491    {
10492      vty_out (vty, "Address family: IPv6 Unicast%s", VTY_NEWLINE);
10493      prefix_bgp_show_prefix_list (vty, AFI_IP6, name);
10494    }
10495
10496  return CMD_SUCCESS;
10497}
10498
10499ALIAS (show_bgp_view_neighbor_received_prefix_filter,
10500       show_bgp_view_ipv6_neighbor_received_prefix_filter_cmd,
10501       "show bgp view WORD ipv6 neighbors (A.B.C.D|X:X::X:X) received prefix-filter",
10502       SHOW_STR
10503       BGP_STR
10504       "BGP view\n"
10505       "View name\n"
10506       "Address family\n"
10507       "Detailed information on TCP and BGP neighbor connections\n"
10508       "Neighbor to display information about\n"
10509       "Neighbor to display information about\n"
10510       "Display information received from a BGP neighbor\n"
10511       "Display the prefixlist filter\n")
10512#endif /* HAVE_IPV6 */
10513
10514static int
10515bgp_show_neighbor_route (struct vty *vty, struct peer *peer, afi_t afi,
10516			 safi_t safi, enum bgp_show_type type)
10517{
10518  if (! peer || ! peer->afc[afi][safi])
10519    {
10520      vty_out (vty, "%% No such neighbor or address family%s", VTY_NEWLINE);
10521      return CMD_WARNING;
10522    }
10523
10524  return bgp_show (vty, peer->bgp, afi, safi, type, &peer->su);
10525}
10526
10527DEFUN (show_ip_bgp_neighbor_routes,
10528       show_ip_bgp_neighbor_routes_cmd,
10529       "show ip bgp neighbors (A.B.C.D|X:X::X:X) routes",
10530       SHOW_STR
10531       IP_STR
10532       BGP_STR
10533       "Detailed information on TCP and BGP neighbor connections\n"
10534       "Neighbor to display information about\n"
10535       "Neighbor to display information about\n"
10536       "Display routes learned from neighbor\n")
10537{
10538  struct peer *peer;
10539
10540  peer = peer_lookup_in_view (vty, NULL, argv[0]);
10541  if (! peer)
10542    return CMD_WARNING;
10543
10544  return bgp_show_neighbor_route (vty, peer, AFI_IP, SAFI_UNICAST,
10545				  bgp_show_type_neighbor);
10546}
10547
10548DEFUN (show_ip_bgp_neighbor_flap,
10549       show_ip_bgp_neighbor_flap_cmd,
10550       "show ip bgp neighbors (A.B.C.D|X:X::X:X) flap-statistics",
10551       SHOW_STR
10552       IP_STR
10553       BGP_STR
10554       "Detailed information on TCP and BGP neighbor connections\n"
10555       "Neighbor to display information about\n"
10556       "Neighbor to display information about\n"
10557       "Display flap statistics of the routes learned from neighbor\n")
10558{
10559  struct peer *peer;
10560
10561  peer = peer_lookup_in_view (vty, NULL, argv[0]);
10562  if (! peer)
10563    return CMD_WARNING;
10564
10565  return bgp_show_neighbor_route (vty, peer, AFI_IP, SAFI_UNICAST,
10566				  bgp_show_type_flap_neighbor);
10567}
10568
10569DEFUN (show_ip_bgp_neighbor_damp,
10570       show_ip_bgp_neighbor_damp_cmd,
10571       "show ip bgp neighbors (A.B.C.D|X:X::X:X) dampened-routes",
10572       SHOW_STR
10573       IP_STR
10574       BGP_STR
10575       "Detailed information on TCP and BGP neighbor connections\n"
10576       "Neighbor to display information about\n"
10577       "Neighbor to display information about\n"
10578       "Display the dampened routes received from neighbor\n")
10579{
10580  struct peer *peer;
10581
10582  peer = peer_lookup_in_view (vty, NULL, argv[0]);
10583  if (! peer)
10584    return CMD_WARNING;
10585
10586  return bgp_show_neighbor_route (vty, peer, AFI_IP, SAFI_UNICAST,
10587				  bgp_show_type_damp_neighbor);
10588}
10589
10590DEFUN (show_ip_bgp_ipv4_neighbor_routes,
10591       show_ip_bgp_ipv4_neighbor_routes_cmd,
10592       "show ip bgp ipv4 (unicast|multicast) neighbors (A.B.C.D|X:X::X:X) routes",
10593       SHOW_STR
10594       IP_STR
10595       BGP_STR
10596       "Address family\n"
10597       "Address Family modifier\n"
10598       "Address Family modifier\n"
10599       "Detailed information on TCP and BGP neighbor connections\n"
10600       "Neighbor to display information about\n"
10601       "Neighbor to display information about\n"
10602       "Display routes learned from neighbor\n")
10603{
10604  struct peer *peer;
10605
10606  peer = peer_lookup_in_view (vty, NULL, argv[1]);
10607  if (! peer)
10608    return CMD_WARNING;
10609
10610  if (strncmp (argv[0], "m", 1) == 0)
10611    return bgp_show_neighbor_route (vty, peer, AFI_IP, SAFI_MULTICAST,
10612				    bgp_show_type_neighbor);
10613
10614  return bgp_show_neighbor_route (vty, peer, AFI_IP, SAFI_UNICAST,
10615				  bgp_show_type_neighbor);
10616}
10617
10618DEFUN (show_ip_bgp_view_rsclient,
10619       show_ip_bgp_view_rsclient_cmd,
10620       "show ip bgp view WORD rsclient (A.B.C.D|X:X::X:X)",
10621       SHOW_STR
10622       IP_STR
10623       BGP_STR
10624       "BGP view\n"
10625       "View name\n"
10626       "Information about Route Server Client\n"
10627       NEIGHBOR_ADDR_STR)
10628{
10629  struct bgp_table *table;
10630  struct peer *peer;
10631
10632  if (argc == 2)
10633    peer = peer_lookup_in_view (vty, argv[0], argv[1]);
10634  else
10635    peer = peer_lookup_in_view (vty, NULL, argv[0]);
10636
10637  if (! peer)
10638    return CMD_WARNING;
10639
10640  if (! peer->afc[AFI_IP][SAFI_UNICAST])
10641    {
10642      vty_out (vty, "%% Activate the neighbor for the address family first%s",
10643            VTY_NEWLINE);
10644      return CMD_WARNING;
10645    }
10646
10647  if ( ! CHECK_FLAG (peer->af_flags[AFI_IP][SAFI_UNICAST],
10648              PEER_FLAG_RSERVER_CLIENT))
10649    {
10650      vty_out (vty, "%% Neighbor is not a Route-Server client%s",
10651            VTY_NEWLINE);
10652      return CMD_WARNING;
10653    }
10654
10655  table = peer->rib[AFI_IP][SAFI_UNICAST];
10656
10657  return bgp_show_table (vty, table, &peer->remote_id, bgp_show_type_normal, NULL);
10658}
10659
10660ALIAS (show_ip_bgp_view_rsclient,
10661       show_ip_bgp_rsclient_cmd,
10662       "show ip bgp rsclient (A.B.C.D|X:X::X:X)",
10663       SHOW_STR
10664       IP_STR
10665       BGP_STR
10666       "Information about Route Server Client\n"
10667       NEIGHBOR_ADDR_STR)
10668
10669DEFUN (show_bgp_view_ipv4_safi_rsclient,
10670       show_bgp_view_ipv4_safi_rsclient_cmd,
10671       "show bgp view WORD ipv4 (unicast|multicast) rsclient (A.B.C.D|X:X::X:X)",
10672       SHOW_STR
10673       BGP_STR
10674       "BGP view\n"
10675       "View name\n"
10676       "Address family\n"
10677       "Address Family modifier\n"
10678       "Address Family modifier\n"
10679       "Information about Route Server Client\n"
10680       NEIGHBOR_ADDR_STR)
10681{
10682  struct bgp_table *table;
10683  struct peer *peer;
10684  safi_t safi;
10685
10686  if (argc == 3) {
10687    peer = peer_lookup_in_view (vty, argv[0], argv[2]);
10688    safi = (strncmp (argv[1], "m", 1) == 0) ? SAFI_MULTICAST : SAFI_UNICAST;
10689  } else {
10690    peer = peer_lookup_in_view (vty, NULL, argv[1]);
10691    safi = (strncmp (argv[0], "m", 1) == 0) ? SAFI_MULTICAST : SAFI_UNICAST;
10692  }
10693
10694  if (! peer)
10695    return CMD_WARNING;
10696
10697  if (! peer->afc[AFI_IP][safi])
10698    {
10699      vty_out (vty, "%% Activate the neighbor for the address family first%s",
10700            VTY_NEWLINE);
10701      return CMD_WARNING;
10702    }
10703
10704  if ( ! CHECK_FLAG (peer->af_flags[AFI_IP][safi],
10705              PEER_FLAG_RSERVER_CLIENT))
10706    {
10707      vty_out (vty, "%% Neighbor is not a Route-Server client%s",
10708            VTY_NEWLINE);
10709      return CMD_WARNING;
10710    }
10711
10712  table = peer->rib[AFI_IP][safi];
10713
10714  return bgp_show_table (vty, table, &peer->remote_id, bgp_show_type_normal, NULL);
10715}
10716
10717ALIAS (show_bgp_view_ipv4_safi_rsclient,
10718       show_bgp_ipv4_safi_rsclient_cmd,
10719       "show bgp ipv4 (unicast|multicast) rsclient (A.B.C.D|X:X::X:X)",
10720       SHOW_STR
10721       BGP_STR
10722       "Address family\n"
10723       "Address Family modifier\n"
10724       "Address Family modifier\n"
10725       "Information about Route Server Client\n"
10726       NEIGHBOR_ADDR_STR)
10727
10728DEFUN (show_ip_bgp_view_rsclient_route,
10729       show_ip_bgp_view_rsclient_route_cmd,
10730       "show ip bgp view WORD rsclient (A.B.C.D|X:X::X:X) A.B.C.D",
10731       SHOW_STR
10732       IP_STR
10733       BGP_STR
10734       "BGP view\n"
10735       "View name\n"
10736       "Information about Route Server Client\n"
10737       NEIGHBOR_ADDR_STR
10738       "Network in the BGP routing table to display\n")
10739{
10740  struct bgp *bgp;
10741  struct peer *peer;
10742
10743  /* BGP structure lookup. */
10744  if (argc == 3)
10745    {
10746      bgp = bgp_lookup_by_name (argv[0]);
10747      if (bgp == NULL)
10748	{
10749	  vty_out (vty, "Can't find BGP view %s%s", argv[0], VTY_NEWLINE);
10750	  return CMD_WARNING;
10751	}
10752    }
10753  else
10754    {
10755      bgp = bgp_get_default ();
10756      if (bgp == NULL)
10757	{
10758	  vty_out (vty, "No BGP process is configured%s", VTY_NEWLINE);
10759	  return CMD_WARNING;
10760	}
10761    }
10762
10763  if (argc == 3)
10764    peer = peer_lookup_in_view (vty, argv[0], argv[1]);
10765  else
10766    peer = peer_lookup_in_view (vty, NULL, argv[0]);
10767
10768  if (! peer)
10769    return CMD_WARNING;
10770
10771  if (! peer->afc[AFI_IP][SAFI_UNICAST])
10772    {
10773      vty_out (vty, "%% Activate the neighbor for the address family first%s",
10774            VTY_NEWLINE);
10775      return CMD_WARNING;
10776}
10777
10778  if ( ! CHECK_FLAG (peer->af_flags[AFI_IP][SAFI_UNICAST],
10779              PEER_FLAG_RSERVER_CLIENT))
10780    {
10781      vty_out (vty, "%% Neighbor is not a Route-Server client%s",
10782            VTY_NEWLINE);
10783      return CMD_WARNING;
10784    }
10785
10786  return bgp_show_route_in_table (vty, bgp, peer->rib[AFI_IP][SAFI_UNICAST],
10787                                  (argc == 3) ? argv[2] : argv[1],
10788                                  AFI_IP, SAFI_UNICAST, NULL, 0);
10789}
10790
10791ALIAS (show_ip_bgp_view_rsclient_route,
10792       show_ip_bgp_rsclient_route_cmd,
10793       "show ip bgp rsclient (A.B.C.D|X:X::X:X) A.B.C.D",
10794       SHOW_STR
10795       IP_STR
10796       BGP_STR
10797       "Information about Route Server Client\n"
10798       NEIGHBOR_ADDR_STR
10799       "Network in the BGP routing table to display\n")
10800
10801DEFUN (show_bgp_view_ipv4_safi_rsclient_route,
10802       show_bgp_view_ipv4_safi_rsclient_route_cmd,
10803       "show bgp view WORD ipv4 (unicast|multicast) rsclient (A.B.C.D|X:X::X:X) A.B.C.D",
10804       SHOW_STR
10805       BGP_STR
10806       "BGP view\n"
10807       "View name\n"
10808       "Address family\n"
10809       "Address Family modifier\n"
10810       "Address Family modifier\n"
10811       "Information about Route Server Client\n"
10812       NEIGHBOR_ADDR_STR
10813       "Network in the BGP routing table to display\n")
10814{
10815  struct bgp *bgp;
10816  struct peer *peer;
10817  safi_t safi;
10818
10819  /* BGP structure lookup. */
10820  if (argc == 4)
10821    {
10822      bgp = bgp_lookup_by_name (argv[0]);
10823      if (bgp == NULL)
10824	{
10825	  vty_out (vty, "Can't find BGP view %s%s", argv[0], VTY_NEWLINE);
10826	  return CMD_WARNING;
10827	}
10828    }
10829  else
10830    {
10831      bgp = bgp_get_default ();
10832      if (bgp == NULL)
10833	{
10834	  vty_out (vty, "No BGP process is configured%s", VTY_NEWLINE);
10835	  return CMD_WARNING;
10836	}
10837    }
10838
10839  if (argc == 4) {
10840    peer = peer_lookup_in_view (vty, argv[0], argv[2]);
10841    safi = (strncmp (argv[1], "m", 1) == 0) ? SAFI_MULTICAST : SAFI_UNICAST;
10842  } else {
10843    peer = peer_lookup_in_view (vty, NULL, argv[1]);
10844    safi = (strncmp (argv[0], "m", 1) == 0) ? SAFI_MULTICAST : SAFI_UNICAST;
10845  }
10846
10847  if (! peer)
10848    return CMD_WARNING;
10849
10850  if (! peer->afc[AFI_IP][safi])
10851    {
10852      vty_out (vty, "%% Activate the neighbor for the address family first%s",
10853            VTY_NEWLINE);
10854      return CMD_WARNING;
10855}
10856
10857  if ( ! CHECK_FLAG (peer->af_flags[AFI_IP][safi],
10858              PEER_FLAG_RSERVER_CLIENT))
10859    {
10860      vty_out (vty, "%% Neighbor is not a Route-Server client%s",
10861            VTY_NEWLINE);
10862      return CMD_WARNING;
10863    }
10864
10865  return bgp_show_route_in_table (vty, bgp, peer->rib[AFI_IP][safi],
10866                                  (argc == 4) ? argv[3] : argv[2],
10867                                  AFI_IP, safi, NULL, 0);
10868}
10869
10870ALIAS (show_bgp_view_ipv4_safi_rsclient_route,
10871       show_bgp_ipv4_safi_rsclient_route_cmd,
10872       "show bgp ipv4 (unicast|multicast) rsclient (A.B.C.D|X:X::X:X) A.B.C.D",
10873       SHOW_STR
10874       BGP_STR
10875       "Address family\n"
10876       "Address Family modifier\n"
10877       "Address Family modifier\n"
10878       "Information about Route Server Client\n"
10879       NEIGHBOR_ADDR_STR
10880       "Network in the BGP routing table to display\n")
10881
10882DEFUN (show_ip_bgp_view_rsclient_prefix,
10883       show_ip_bgp_view_rsclient_prefix_cmd,
10884       "show ip bgp view WORD rsclient (A.B.C.D|X:X::X:X) A.B.C.D/M",
10885       SHOW_STR
10886       IP_STR
10887       BGP_STR
10888       "BGP view\n"
10889       "View name\n"
10890       "Information about Route Server Client\n"
10891       NEIGHBOR_ADDR_STR
10892       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
10893{
10894  struct bgp *bgp;
10895  struct peer *peer;
10896
10897  /* BGP structure lookup. */
10898  if (argc == 3)
10899    {
10900      bgp = bgp_lookup_by_name (argv[0]);
10901      if (bgp == NULL)
10902	{
10903	  vty_out (vty, "Can't find BGP view %s%s", argv[0], VTY_NEWLINE);
10904	  return CMD_WARNING;
10905	}
10906    }
10907  else
10908    {
10909      bgp = bgp_get_default ();
10910      if (bgp == NULL)
10911	{
10912	  vty_out (vty, "No BGP process is configured%s", VTY_NEWLINE);
10913	  return CMD_WARNING;
10914	}
10915    }
10916
10917  if (argc == 3)
10918    peer = peer_lookup_in_view (vty, argv[0], argv[1]);
10919  else
10920  peer = peer_lookup_in_view (vty, NULL, argv[0]);
10921
10922  if (! peer)
10923    return CMD_WARNING;
10924
10925  if (! peer->afc[AFI_IP][SAFI_UNICAST])
10926    {
10927      vty_out (vty, "%% Activate the neighbor for the address family first%s",
10928            VTY_NEWLINE);
10929      return CMD_WARNING;
10930}
10931
10932  if ( ! CHECK_FLAG (peer->af_flags[AFI_IP][SAFI_UNICAST],
10933              PEER_FLAG_RSERVER_CLIENT))
10934{
10935      vty_out (vty, "%% Neighbor is not a Route-Server client%s",
10936            VTY_NEWLINE);
10937    return CMD_WARNING;
10938    }
10939
10940  return bgp_show_route_in_table (vty, bgp, peer->rib[AFI_IP][SAFI_UNICAST],
10941                                  (argc == 3) ? argv[2] : argv[1],
10942                                  AFI_IP, SAFI_UNICAST, NULL, 1);
10943}
10944
10945ALIAS (show_ip_bgp_view_rsclient_prefix,
10946       show_ip_bgp_rsclient_prefix_cmd,
10947       "show ip bgp rsclient (A.B.C.D|X:X::X:X) A.B.C.D/M",
10948       SHOW_STR
10949       IP_STR
10950       BGP_STR
10951       "Information about Route Server Client\n"
10952       NEIGHBOR_ADDR_STR
10953       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
10954
10955DEFUN (show_bgp_view_ipv4_safi_rsclient_prefix,
10956       show_bgp_view_ipv4_safi_rsclient_prefix_cmd,
10957       "show bgp view WORD ipv4 (unicast|multicast) rsclient (A.B.C.D|X:X::X:X) A.B.C.D/M",
10958       SHOW_STR
10959       BGP_STR
10960       "BGP view\n"
10961       "View name\n"
10962       "Address family\n"
10963       "Address Family modifier\n"
10964       "Address Family modifier\n"
10965       "Information about Route Server Client\n"
10966       NEIGHBOR_ADDR_STR
10967       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
10968{
10969  struct bgp *bgp;
10970  struct peer *peer;
10971  safi_t safi;
10972
10973  /* BGP structure lookup. */
10974  if (argc == 4)
10975    {
10976      bgp = bgp_lookup_by_name (argv[0]);
10977      if (bgp == NULL)
10978	{
10979	  vty_out (vty, "Can't find BGP view %s%s", argv[0], VTY_NEWLINE);
10980	  return CMD_WARNING;
10981	}
10982    }
10983  else
10984    {
10985      bgp = bgp_get_default ();
10986      if (bgp == NULL)
10987	{
10988	  vty_out (vty, "No BGP process is configured%s", VTY_NEWLINE);
10989	  return CMD_WARNING;
10990	}
10991    }
10992
10993  if (argc == 4) {
10994    peer = peer_lookup_in_view (vty, argv[0], argv[2]);
10995    safi = (strncmp (argv[1], "m", 1) == 0) ? SAFI_MULTICAST : SAFI_UNICAST;
10996  } else {
10997    peer = peer_lookup_in_view (vty, NULL, argv[1]);
10998    safi = (strncmp (argv[0], "m", 1) == 0) ? SAFI_MULTICAST : SAFI_UNICAST;
10999  }
11000
11001  if (! peer)
11002    return CMD_WARNING;
11003
11004  if (! peer->afc[AFI_IP][safi])
11005    {
11006      vty_out (vty, "%% Activate the neighbor for the address family first%s",
11007            VTY_NEWLINE);
11008      return CMD_WARNING;
11009}
11010
11011  if ( ! CHECK_FLAG (peer->af_flags[AFI_IP][safi],
11012              PEER_FLAG_RSERVER_CLIENT))
11013{
11014      vty_out (vty, "%% Neighbor is not a Route-Server client%s",
11015            VTY_NEWLINE);
11016    return CMD_WARNING;
11017    }
11018
11019  return bgp_show_route_in_table (vty, bgp, peer->rib[AFI_IP][safi],
11020                                  (argc == 4) ? argv[3] : argv[2],
11021                                  AFI_IP, safi, NULL, 1);
11022}
11023
11024ALIAS (show_bgp_view_ipv4_safi_rsclient_prefix,
11025       show_bgp_ipv4_safi_rsclient_prefix_cmd,
11026       "show bgp ipv4 (unicast|multicast) rsclient (A.B.C.D|X:X::X:X) A.B.C.D/M",
11027       SHOW_STR
11028       BGP_STR
11029       "Address family\n"
11030       "Address Family modifier\n"
11031       "Address Family modifier\n"
11032       "Information about Route Server Client\n"
11033       NEIGHBOR_ADDR_STR
11034       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
11035
11036#ifdef HAVE_IPV6
11037DEFUN (show_bgp_view_neighbor_routes,
11038       show_bgp_view_neighbor_routes_cmd,
11039       "show bgp view WORD neighbors (A.B.C.D|X:X::X:X) routes",
11040       SHOW_STR
11041       BGP_STR
11042       "BGP view\n"
11043       "View name\n"
11044       "Detailed information on TCP and BGP neighbor connections\n"
11045       "Neighbor to display information about\n"
11046       "Neighbor to display information about\n"
11047       "Display routes learned from neighbor\n")
11048{
11049  struct peer *peer;
11050
11051  if (argc == 2)
11052    peer = peer_lookup_in_view (vty, argv[0], argv[1]);
11053  else
11054    peer = peer_lookup_in_view (vty, NULL, argv[0]);
11055
11056  if (! peer)
11057    return CMD_WARNING;
11058
11059  return bgp_show_neighbor_route (vty, peer, AFI_IP6, SAFI_UNICAST,
11060				  bgp_show_type_neighbor);
11061}
11062
11063ALIAS (show_bgp_view_neighbor_routes,
11064       show_bgp_view_ipv6_neighbor_routes_cmd,
11065       "show bgp view WORD ipv6 neighbors (A.B.C.D|X:X::X:X) routes",
11066       SHOW_STR
11067       BGP_STR
11068       "BGP view\n"
11069       "View name\n"
11070       "Address family\n"
11071       "Detailed information on TCP and BGP neighbor connections\n"
11072       "Neighbor to display information about\n"
11073       "Neighbor to display information about\n"
11074       "Display routes learned from neighbor\n")
11075
11076DEFUN (show_bgp_view_neighbor_damp,
11077       show_bgp_view_neighbor_damp_cmd,
11078       "show bgp view WORD neighbors (A.B.C.D|X:X::X:X) dampened-routes",
11079       SHOW_STR
11080       BGP_STR
11081       "BGP view\n"
11082       "View name\n"
11083       "Detailed information on TCP and BGP neighbor connections\n"
11084       "Neighbor to display information about\n"
11085       "Neighbor to display information about\n"
11086       "Display the dampened routes received from neighbor\n")
11087{
11088  struct peer *peer;
11089
11090  if (argc == 2)
11091    peer = peer_lookup_in_view (vty, argv[0], argv[1]);
11092  else
11093    peer = peer_lookup_in_view (vty, NULL, argv[0]);
11094
11095  if (! peer)
11096    return CMD_WARNING;
11097
11098  return bgp_show_neighbor_route (vty, peer, AFI_IP6, SAFI_UNICAST,
11099				  bgp_show_type_damp_neighbor);
11100}
11101
11102ALIAS (show_bgp_view_neighbor_damp,
11103       show_bgp_view_ipv6_neighbor_damp_cmd,
11104       "show bgp view WORD ipv6 neighbors (A.B.C.D|X:X::X:X) dampened-routes",
11105       SHOW_STR
11106       BGP_STR
11107       "BGP view\n"
11108       "View name\n"
11109       "Address family\n"
11110       "Detailed information on TCP and BGP neighbor connections\n"
11111       "Neighbor to display information about\n"
11112       "Neighbor to display information about\n"
11113       "Display the dampened routes received from neighbor\n")
11114
11115DEFUN (show_bgp_view_neighbor_flap,
11116       show_bgp_view_neighbor_flap_cmd,
11117       "show bgp view WORD neighbors (A.B.C.D|X:X::X:X) flap-statistics",
11118       SHOW_STR
11119       BGP_STR
11120       "BGP view\n"
11121       "View name\n"
11122       "Detailed information on TCP and BGP neighbor connections\n"
11123       "Neighbor to display information about\n"
11124       "Neighbor to display information about\n"
11125       "Display flap statistics of the routes learned from neighbor\n")
11126{
11127  struct peer *peer;
11128
11129  if (argc == 2)
11130    peer = peer_lookup_in_view (vty, argv[0], argv[1]);
11131  else
11132    peer = peer_lookup_in_view (vty, NULL, argv[0]);
11133
11134  if (! peer)
11135    return CMD_WARNING;
11136
11137  return bgp_show_neighbor_route (vty, peer, AFI_IP6, SAFI_UNICAST,
11138				  bgp_show_type_flap_neighbor);
11139}
11140
11141ALIAS (show_bgp_view_neighbor_flap,
11142       show_bgp_view_ipv6_neighbor_flap_cmd,
11143       "show bgp view WORD ipv6 neighbors (A.B.C.D|X:X::X:X) flap-statistics",
11144       SHOW_STR
11145       BGP_STR
11146       "BGP view\n"
11147       "View name\n"
11148       "Address family\n"
11149       "Detailed information on TCP and BGP neighbor connections\n"
11150       "Neighbor to display information about\n"
11151       "Neighbor to display information about\n"
11152       "Display flap statistics of the routes learned from neighbor\n")
11153
11154ALIAS (show_bgp_view_neighbor_routes,
11155       show_bgp_neighbor_routes_cmd,
11156       "show bgp neighbors (A.B.C.D|X:X::X:X) routes",
11157       SHOW_STR
11158       BGP_STR
11159       "Detailed information on TCP and BGP neighbor connections\n"
11160       "Neighbor to display information about\n"
11161       "Neighbor to display information about\n"
11162       "Display routes learned from neighbor\n")
11163
11164
11165ALIAS (show_bgp_view_neighbor_routes,
11166       show_bgp_ipv6_neighbor_routes_cmd,
11167       "show bgp ipv6 neighbors (A.B.C.D|X:X::X:X) routes",
11168       SHOW_STR
11169       BGP_STR
11170       "Address family\n"
11171       "Detailed information on TCP and BGP neighbor connections\n"
11172       "Neighbor to display information about\n"
11173       "Neighbor to display information about\n"
11174       "Display routes learned from neighbor\n")
11175
11176/* old command */
11177ALIAS (show_bgp_view_neighbor_routes,
11178       ipv6_bgp_neighbor_routes_cmd,
11179       "show ipv6 bgp neighbors (A.B.C.D|X:X::X:X) routes",
11180       SHOW_STR
11181       IPV6_STR
11182       BGP_STR
11183       "Detailed information on TCP and BGP neighbor connections\n"
11184       "Neighbor to display information about\n"
11185       "Neighbor to display information about\n"
11186       "Display routes learned from neighbor\n")
11187
11188/* old command */
11189DEFUN (ipv6_mbgp_neighbor_routes,
11190       ipv6_mbgp_neighbor_routes_cmd,
11191       "show ipv6 mbgp neighbors (A.B.C.D|X:X::X:X) routes",
11192       SHOW_STR
11193       IPV6_STR
11194       MBGP_STR
11195       "Detailed information on TCP and BGP neighbor connections\n"
11196       "Neighbor to display information about\n"
11197       "Neighbor to display information about\n"
11198       "Display routes learned from neighbor\n")
11199{
11200  struct peer *peer;
11201
11202  peer = peer_lookup_in_view (vty, NULL, argv[0]);
11203  if (! peer)
11204    return CMD_WARNING;
11205
11206  return bgp_show_neighbor_route (vty, peer, AFI_IP6, SAFI_MULTICAST,
11207				  bgp_show_type_neighbor);
11208}
11209
11210ALIAS (show_bgp_view_neighbor_flap,
11211       show_bgp_neighbor_flap_cmd,
11212       "show bgp neighbors (A.B.C.D|X:X::X:X) flap-statistics",
11213       SHOW_STR
11214       BGP_STR
11215       "Detailed information on TCP and BGP neighbor connections\n"
11216       "Neighbor to display information about\n"
11217       "Neighbor to display information about\n"
11218       "Display flap statistics of the routes learned from neighbor\n")
11219
11220ALIAS (show_bgp_view_neighbor_flap,
11221       show_bgp_ipv6_neighbor_flap_cmd,
11222       "show bgp ipv6 neighbors (A.B.C.D|X:X::X:X) flap-statistics",
11223       SHOW_STR
11224       BGP_STR
11225       "Address family\n"
11226       "Detailed information on TCP and BGP neighbor connections\n"
11227       "Neighbor to display information about\n"
11228       "Neighbor to display information about\n"
11229       "Display flap statistics of the routes learned from neighbor\n")
11230
11231ALIAS (show_bgp_view_neighbor_damp,
11232       show_bgp_neighbor_damp_cmd,
11233       "show bgp neighbors (A.B.C.D|X:X::X:X) dampened-routes",
11234       SHOW_STR
11235       BGP_STR
11236       "Detailed information on TCP and BGP neighbor connections\n"
11237       "Neighbor to display information about\n"
11238       "Neighbor to display information about\n"
11239       "Display the dampened routes received from neighbor\n")
11240
11241ALIAS (show_bgp_view_neighbor_damp,
11242       show_bgp_ipv6_neighbor_damp_cmd,
11243       "show bgp ipv6 neighbors (A.B.C.D|X:X::X:X) dampened-routes",
11244       SHOW_STR
11245       BGP_STR
11246       "Address family\n"
11247       "Detailed information on TCP and BGP neighbor connections\n"
11248       "Neighbor to display information about\n"
11249       "Neighbor to display information about\n"
11250       "Display the dampened routes received from neighbor\n")
11251
11252DEFUN (show_bgp_view_rsclient,
11253       show_bgp_view_rsclient_cmd,
11254       "show bgp view WORD rsclient (A.B.C.D|X:X::X:X)",
11255       SHOW_STR
11256       BGP_STR
11257       "BGP view\n"
11258       "View name\n"
11259       "Information about Route Server Client\n"
11260       NEIGHBOR_ADDR_STR)
11261{
11262  struct bgp_table *table;
11263  struct peer *peer;
11264
11265  if (argc == 2)
11266    peer = peer_lookup_in_view (vty, argv[0], argv[1]);
11267  else
11268    peer = peer_lookup_in_view (vty, NULL, argv[0]);
11269
11270  if (! peer)
11271    return CMD_WARNING;
11272
11273  if (! peer->afc[AFI_IP6][SAFI_UNICAST])
11274    {
11275      vty_out (vty, "%% Activate the neighbor for the address family first%s",
11276            VTY_NEWLINE);
11277      return CMD_WARNING;
11278    }
11279
11280  if ( ! CHECK_FLAG (peer->af_flags[AFI_IP6][SAFI_UNICAST],
11281              PEER_FLAG_RSERVER_CLIENT))
11282    {
11283      vty_out (vty, "%% Neighbor is not a Route-Server client%s",
11284            VTY_NEWLINE);
11285      return CMD_WARNING;
11286    }
11287
11288  table = peer->rib[AFI_IP6][SAFI_UNICAST];
11289
11290  return bgp_show_table (vty, table, &peer->remote_id, bgp_show_type_normal, NULL);
11291}
11292
11293ALIAS (show_bgp_view_rsclient,
11294       show_bgp_rsclient_cmd,
11295       "show bgp rsclient (A.B.C.D|X:X::X:X)",
11296       SHOW_STR
11297       BGP_STR
11298       "Information about Route Server Client\n"
11299       NEIGHBOR_ADDR_STR)
11300
11301DEFUN (show_bgp_view_ipv6_safi_rsclient,
11302       show_bgp_view_ipv6_safi_rsclient_cmd,
11303       "show bgp view WORD ipv6 (unicast|multicast) rsclient (A.B.C.D|X:X::X:X)",
11304       SHOW_STR
11305       BGP_STR
11306       "BGP view\n"
11307       "View name\n"
11308       "Address family\n"
11309       "Address Family modifier\n"
11310       "Address Family modifier\n"
11311       "Information about Route Server Client\n"
11312       NEIGHBOR_ADDR_STR)
11313{
11314  struct bgp_table *table;
11315  struct peer *peer;
11316  safi_t safi;
11317
11318  if (argc == 3) {
11319    peer = peer_lookup_in_view (vty, argv[0], argv[2]);
11320    safi = (strncmp (argv[1], "m", 1) == 0) ? SAFI_MULTICAST : SAFI_UNICAST;
11321  } else {
11322    peer = peer_lookup_in_view (vty, NULL, argv[1]);
11323    safi = (strncmp (argv[0], "m", 1) == 0) ? SAFI_MULTICAST : SAFI_UNICAST;
11324  }
11325
11326  if (! peer)
11327    return CMD_WARNING;
11328
11329  if (! peer->afc[AFI_IP6][safi])
11330    {
11331      vty_out (vty, "%% Activate the neighbor for the address family first%s",
11332            VTY_NEWLINE);
11333      return CMD_WARNING;
11334    }
11335
11336  if ( ! CHECK_FLAG (peer->af_flags[AFI_IP6][safi],
11337              PEER_FLAG_RSERVER_CLIENT))
11338    {
11339      vty_out (vty, "%% Neighbor is not a Route-Server client%s",
11340            VTY_NEWLINE);
11341      return CMD_WARNING;
11342    }
11343
11344  table = peer->rib[AFI_IP6][safi];
11345
11346  return bgp_show_table (vty, table, &peer->remote_id, bgp_show_type_normal, NULL);
11347}
11348
11349ALIAS (show_bgp_view_ipv6_safi_rsclient,
11350       show_bgp_ipv6_safi_rsclient_cmd,
11351       "show bgp ipv6 (unicast|multicast) rsclient (A.B.C.D|X:X::X:X)",
11352       SHOW_STR
11353       BGP_STR
11354       "Address family\n"
11355       "Address Family modifier\n"
11356       "Address Family modifier\n"
11357       "Information about Route Server Client\n"
11358       NEIGHBOR_ADDR_STR)
11359
11360DEFUN (show_bgp_view_rsclient_route,
11361       show_bgp_view_rsclient_route_cmd,
11362       "show bgp view WORD rsclient (A.B.C.D|X:X::X:X) X:X::X:X",
11363       SHOW_STR
11364       BGP_STR
11365       "BGP view\n"
11366       "View name\n"
11367       "Information about Route Server Client\n"
11368       NEIGHBOR_ADDR_STR
11369       "Network in the BGP routing table to display\n")
11370{
11371  struct bgp *bgp;
11372  struct peer *peer;
11373
11374  /* BGP structure lookup. */
11375  if (argc == 3)
11376    {
11377      bgp = bgp_lookup_by_name (argv[0]);
11378      if (bgp == NULL)
11379        {
11380          vty_out (vty, "Can't find BGP view %s%s", argv[0], VTY_NEWLINE);
11381          return CMD_WARNING;
11382        }
11383    }
11384  else
11385    {
11386      bgp = bgp_get_default ();
11387      if (bgp == NULL)
11388        {
11389          vty_out (vty, "No BGP process is configured%s", VTY_NEWLINE);
11390          return CMD_WARNING;
11391        }
11392    }
11393
11394  if (argc == 3)
11395    peer = peer_lookup_in_view (vty, argv[0], argv[1]);
11396  else
11397    peer = peer_lookup_in_view (vty, NULL, argv[0]);
11398
11399  if (! peer)
11400    return CMD_WARNING;
11401
11402  if (! peer->afc[AFI_IP6][SAFI_UNICAST])
11403    {
11404      vty_out (vty, "%% Activate the neighbor for the address family first%s",
11405            VTY_NEWLINE);
11406      return CMD_WARNING;
11407    }
11408
11409  if ( ! CHECK_FLAG (peer->af_flags[AFI_IP6][SAFI_UNICAST],
11410              PEER_FLAG_RSERVER_CLIENT))
11411    {
11412      vty_out (vty, "%% Neighbor is not a Route-Server client%s",
11413            VTY_NEWLINE);
11414      return CMD_WARNING;
11415    }
11416
11417  return bgp_show_route_in_table (vty, bgp, peer->rib[AFI_IP6][SAFI_UNICAST],
11418                                  (argc == 3) ? argv[2] : argv[1],
11419                                  AFI_IP6, SAFI_UNICAST, NULL, 0);
11420}
11421
11422ALIAS (show_bgp_view_rsclient_route,
11423       show_bgp_rsclient_route_cmd,
11424       "show bgp rsclient (A.B.C.D|X:X::X:X) X:X::X:X",
11425       SHOW_STR
11426       BGP_STR
11427       "Information about Route Server Client\n"
11428       NEIGHBOR_ADDR_STR
11429       "Network in the BGP routing table to display\n")
11430
11431DEFUN (show_bgp_view_ipv6_safi_rsclient_route,
11432       show_bgp_view_ipv6_safi_rsclient_route_cmd,
11433       "show bgp view WORD ipv6 (unicast|multicast) rsclient (A.B.C.D|X:X::X:X) X:X::X:X",
11434       SHOW_STR
11435       BGP_STR
11436       "BGP view\n"
11437       "View name\n"
11438       "Address family\n"
11439       "Address Family modifier\n"
11440       "Address Family modifier\n"
11441       "Information about Route Server Client\n"
11442       NEIGHBOR_ADDR_STR
11443       "Network in the BGP routing table to display\n")
11444{
11445  struct bgp *bgp;
11446  struct peer *peer;
11447  safi_t safi;
11448
11449  /* BGP structure lookup. */
11450  if (argc == 4)
11451    {
11452      bgp = bgp_lookup_by_name (argv[0]);
11453      if (bgp == NULL)
11454	{
11455	  vty_out (vty, "Can't find BGP view %s%s", argv[0], VTY_NEWLINE);
11456	  return CMD_WARNING;
11457	}
11458    }
11459  else
11460    {
11461      bgp = bgp_get_default ();
11462      if (bgp == NULL)
11463	{
11464	  vty_out (vty, "No BGP process is configured%s", VTY_NEWLINE);
11465	  return CMD_WARNING;
11466	}
11467    }
11468
11469  if (argc == 4) {
11470    peer = peer_lookup_in_view (vty, argv[0], argv[2]);
11471    safi = (strncmp (argv[1], "m", 1) == 0) ? SAFI_MULTICAST : SAFI_UNICAST;
11472  } else {
11473    peer = peer_lookup_in_view (vty, NULL, argv[1]);
11474    safi = (strncmp (argv[0], "m", 1) == 0) ? SAFI_MULTICAST : SAFI_UNICAST;
11475  }
11476
11477  if (! peer)
11478    return CMD_WARNING;
11479
11480  if (! peer->afc[AFI_IP6][safi])
11481    {
11482      vty_out (vty, "%% Activate the neighbor for the address family first%s",
11483            VTY_NEWLINE);
11484      return CMD_WARNING;
11485}
11486
11487  if ( ! CHECK_FLAG (peer->af_flags[AFI_IP6][safi],
11488              PEER_FLAG_RSERVER_CLIENT))
11489    {
11490      vty_out (vty, "%% Neighbor is not a Route-Server client%s",
11491            VTY_NEWLINE);
11492      return CMD_WARNING;
11493    }
11494
11495  return bgp_show_route_in_table (vty, bgp, peer->rib[AFI_IP6][safi],
11496                                  (argc == 4) ? argv[3] : argv[2],
11497                                  AFI_IP6, safi, NULL, 0);
11498}
11499
11500ALIAS (show_bgp_view_ipv6_safi_rsclient_route,
11501       show_bgp_ipv6_safi_rsclient_route_cmd,
11502       "show bgp ipv6 (unicast|multicast) rsclient (A.B.C.D|X:X::X:X) X:X::X:X",
11503       SHOW_STR
11504       BGP_STR
11505       "Address family\n"
11506       "Address Family modifier\n"
11507       "Address Family modifier\n"
11508       "Information about Route Server Client\n"
11509       NEIGHBOR_ADDR_STR
11510       "Network in the BGP routing table to display\n")
11511
11512DEFUN (show_bgp_view_rsclient_prefix,
11513       show_bgp_view_rsclient_prefix_cmd,
11514       "show bgp view WORD rsclient (A.B.C.D|X:X::X:X) X:X::X:X/M",
11515       SHOW_STR
11516       BGP_STR
11517       "BGP view\n"
11518       "View name\n"
11519       "Information about Route Server Client\n"
11520       NEIGHBOR_ADDR_STR
11521       "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n")
11522{
11523  struct bgp *bgp;
11524  struct peer *peer;
11525
11526  /* BGP structure lookup. */
11527  if (argc == 3)
11528    {
11529      bgp = bgp_lookup_by_name (argv[0]);
11530      if (bgp == NULL)
11531        {
11532          vty_out (vty, "Can't find BGP view %s%s", argv[0], VTY_NEWLINE);
11533          return CMD_WARNING;
11534        }
11535    }
11536  else
11537    {
11538      bgp = bgp_get_default ();
11539      if (bgp == NULL)
11540        {
11541          vty_out (vty, "No BGP process is configured%s", VTY_NEWLINE);
11542          return CMD_WARNING;
11543        }
11544    }
11545
11546  if (argc == 3)
11547    peer = peer_lookup_in_view (vty, argv[0], argv[1]);
11548  else
11549    peer = peer_lookup_in_view (vty, NULL, argv[0]);
11550
11551  if (! peer)
11552    return CMD_WARNING;
11553
11554  if (! peer->afc[AFI_IP6][SAFI_UNICAST])
11555    {
11556      vty_out (vty, "%% Activate the neighbor for the address family first%s",
11557            VTY_NEWLINE);
11558      return CMD_WARNING;
11559    }
11560
11561  if ( ! CHECK_FLAG (peer->af_flags[AFI_IP6][SAFI_UNICAST],
11562              PEER_FLAG_RSERVER_CLIENT))
11563    {
11564      vty_out (vty, "%% Neighbor is not a Route-Server client%s",
11565            VTY_NEWLINE);
11566      return CMD_WARNING;
11567    }
11568
11569  return bgp_show_route_in_table (vty, bgp, peer->rib[AFI_IP6][SAFI_UNICAST],
11570                                  (argc == 3) ? argv[2] : argv[1],
11571                                  AFI_IP6, SAFI_UNICAST, NULL, 1);
11572}
11573
11574ALIAS (show_bgp_view_rsclient_prefix,
11575       show_bgp_rsclient_prefix_cmd,
11576       "show bgp rsclient (A.B.C.D|X:X::X:X) X:X::X:X/M",
11577       SHOW_STR
11578       BGP_STR
11579       "Information about Route Server Client\n"
11580       NEIGHBOR_ADDR_STR
11581       "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n")
11582
11583DEFUN (show_bgp_view_ipv6_safi_rsclient_prefix,
11584       show_bgp_view_ipv6_safi_rsclient_prefix_cmd,
11585       "show bgp view WORD ipv6 (unicast|multicast) rsclient (A.B.C.D|X:X::X:X) X:X::X:X/M",
11586       SHOW_STR
11587       BGP_STR
11588       "BGP view\n"
11589       "View name\n"
11590       "Address family\n"
11591       "Address Family modifier\n"
11592       "Address Family modifier\n"
11593       "Information about Route Server Client\n"
11594       NEIGHBOR_ADDR_STR
11595       "IP prefix <network>/<length>, e.g., 3ffe::/16\n")
11596{
11597  struct bgp *bgp;
11598  struct peer *peer;
11599  safi_t safi;
11600
11601  /* BGP structure lookup. */
11602  if (argc == 4)
11603    {
11604      bgp = bgp_lookup_by_name (argv[0]);
11605      if (bgp == NULL)
11606	{
11607	  vty_out (vty, "Can't find BGP view %s%s", argv[0], VTY_NEWLINE);
11608	  return CMD_WARNING;
11609	}
11610    }
11611  else
11612    {
11613      bgp = bgp_get_default ();
11614      if (bgp == NULL)
11615	{
11616	  vty_out (vty, "No BGP process is configured%s", VTY_NEWLINE);
11617	  return CMD_WARNING;
11618	}
11619    }
11620
11621  if (argc == 4) {
11622    peer = peer_lookup_in_view (vty, argv[0], argv[2]);
11623    safi = (strncmp (argv[1], "m", 1) == 0) ? SAFI_MULTICAST : SAFI_UNICAST;
11624  } else {
11625    peer = peer_lookup_in_view (vty, NULL, argv[1]);
11626    safi = (strncmp (argv[0], "m", 1) == 0) ? SAFI_MULTICAST : SAFI_UNICAST;
11627  }
11628
11629  if (! peer)
11630    return CMD_WARNING;
11631
11632  if (! peer->afc[AFI_IP6][safi])
11633    {
11634      vty_out (vty, "%% Activate the neighbor for the address family first%s",
11635            VTY_NEWLINE);
11636      return CMD_WARNING;
11637}
11638
11639  if ( ! CHECK_FLAG (peer->af_flags[AFI_IP6][safi],
11640              PEER_FLAG_RSERVER_CLIENT))
11641{
11642      vty_out (vty, "%% Neighbor is not a Route-Server client%s",
11643            VTY_NEWLINE);
11644    return CMD_WARNING;
11645    }
11646
11647  return bgp_show_route_in_table (vty, bgp, peer->rib[AFI_IP6][safi],
11648                                  (argc == 4) ? argv[3] : argv[2],
11649                                  AFI_IP6, safi, NULL, 1);
11650}
11651
11652ALIAS (show_bgp_view_ipv6_safi_rsclient_prefix,
11653       show_bgp_ipv6_safi_rsclient_prefix_cmd,
11654       "show bgp ipv6 (unicast|multicast) rsclient (A.B.C.D|X:X::X:X) X:X::X:X/M",
11655       SHOW_STR
11656       BGP_STR
11657       "Address family\n"
11658       "Address Family modifier\n"
11659       "Address Family modifier\n"
11660       "Information about Route Server Client\n"
11661       NEIGHBOR_ADDR_STR
11662       "IP prefix <network>/<length>, e.g., 3ffe::/16\n")
11663
11664#endif /* HAVE_IPV6 */
11665
11666struct bgp_table *bgp_distance_table;
11667
11668struct bgp_distance
11669{
11670  /* Distance value for the IP source prefix. */
11671  u_char distance;
11672
11673  /* Name of the access-list to be matched. */
11674  char *access_list;
11675};
11676
11677static struct bgp_distance *
11678bgp_distance_new (void)
11679{
11680  return XCALLOC (MTYPE_BGP_DISTANCE, sizeof (struct bgp_distance));
11681}
11682
11683static void
11684bgp_distance_free (struct bgp_distance *bdistance)
11685{
11686  XFREE (MTYPE_BGP_DISTANCE, bdistance);
11687}
11688
11689static int
11690bgp_distance_set (struct vty *vty, const char *distance_str,
11691                  const char *ip_str, const char *access_list_str)
11692{
11693  int ret;
11694  struct prefix_ipv4 p;
11695  u_char distance;
11696  struct bgp_node *rn;
11697  struct bgp_distance *bdistance;
11698
11699  ret = str2prefix_ipv4 (ip_str, &p);
11700  if (ret == 0)
11701    {
11702      vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
11703      return CMD_WARNING;
11704    }
11705
11706  distance = atoi (distance_str);
11707
11708  /* Get BGP distance node. */
11709  rn = bgp_node_get (bgp_distance_table, (struct prefix *) &p);
11710  if (rn->info)
11711    {
11712      bdistance = rn->info;
11713      bgp_unlock_node (rn);
11714    }
11715  else
11716    {
11717      bdistance = bgp_distance_new ();
11718      rn->info = bdistance;
11719    }
11720
11721  /* Set distance value. */
11722  bdistance->distance = distance;
11723
11724  /* Reset access-list configuration. */
11725  if (bdistance->access_list)
11726    {
11727      free (bdistance->access_list);
11728      bdistance->access_list = NULL;
11729    }
11730  if (access_list_str)
11731    bdistance->access_list = strdup (access_list_str);
11732
11733  return CMD_SUCCESS;
11734}
11735
11736static int
11737bgp_distance_unset (struct vty *vty, const char *distance_str,
11738                    const char *ip_str, const char *access_list_str)
11739{
11740  int ret;
11741  struct prefix_ipv4 p;
11742  u_char distance;
11743  struct bgp_node *rn;
11744  struct bgp_distance *bdistance;
11745
11746  ret = str2prefix_ipv4 (ip_str, &p);
11747  if (ret == 0)
11748    {
11749      vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
11750      return CMD_WARNING;
11751    }
11752
11753  distance = atoi (distance_str);
11754
11755  rn = bgp_node_lookup (bgp_distance_table, (struct prefix *)&p);
11756  if (! rn)
11757    {
11758      vty_out (vty, "Can't find specified prefix%s", VTY_NEWLINE);
11759      return CMD_WARNING;
11760    }
11761
11762  bdistance = rn->info;
11763
11764  if (bdistance->distance != distance)
11765    {
11766       vty_out (vty, "Distance does not match configured%s", VTY_NEWLINE);
11767       return CMD_WARNING;
11768    }
11769
11770  if (bdistance->access_list)
11771    free (bdistance->access_list);
11772  bgp_distance_free (bdistance);
11773
11774  rn->info = NULL;
11775  bgp_unlock_node (rn);
11776  bgp_unlock_node (rn);
11777
11778  return CMD_SUCCESS;
11779}
11780
11781/* Apply BGP information to distance method. */
11782u_char
11783bgp_distance_apply (struct prefix *p, struct bgp_info *rinfo, struct bgp *bgp)
11784{
11785  struct bgp_node *rn;
11786  struct prefix_ipv4 q;
11787  struct peer *peer;
11788  struct bgp_distance *bdistance;
11789  struct access_list *alist;
11790  struct bgp_static *bgp_static;
11791
11792  if (! bgp)
11793    return 0;
11794
11795  if (p->family != AF_INET)
11796    return 0;
11797
11798  peer = rinfo->peer;
11799
11800  if (peer->su.sa.sa_family != AF_INET)
11801    return 0;
11802
11803  memset (&q, 0, sizeof (struct prefix_ipv4));
11804  q.family = AF_INET;
11805  q.prefix = peer->su.sin.sin_addr;
11806  q.prefixlen = IPV4_MAX_BITLEN;
11807
11808  /* Check source address. */
11809  rn = bgp_node_match (bgp_distance_table, (struct prefix *) &q);
11810  if (rn)
11811    {
11812      bdistance = rn->info;
11813      bgp_unlock_node (rn);
11814
11815      if (bdistance->access_list)
11816	{
11817	  alist = access_list_lookup (AFI_IP, bdistance->access_list);
11818	  if (alist && access_list_apply (alist, p) == FILTER_PERMIT)
11819	    return bdistance->distance;
11820	}
11821      else
11822	return bdistance->distance;
11823    }
11824
11825  /* Backdoor check. */
11826  rn = bgp_node_lookup (bgp->route[AFI_IP][SAFI_UNICAST], p);
11827  if (rn)
11828    {
11829      bgp_static = rn->info;
11830      bgp_unlock_node (rn);
11831
11832      if (bgp_static->backdoor)
11833	{
11834	  if (bgp->distance_local)
11835	    return bgp->distance_local;
11836	  else
11837	    return ZEBRA_IBGP_DISTANCE_DEFAULT;
11838	}
11839    }
11840
11841  if (peer->sort == BGP_PEER_EBGP)
11842    {
11843      if (bgp->distance_ebgp)
11844	return bgp->distance_ebgp;
11845      return ZEBRA_EBGP_DISTANCE_DEFAULT;
11846    }
11847  else
11848    {
11849      if (bgp->distance_ibgp)
11850	return bgp->distance_ibgp;
11851      return ZEBRA_IBGP_DISTANCE_DEFAULT;
11852    }
11853}
11854
11855DEFUN (bgp_distance,
11856       bgp_distance_cmd,
11857       "distance bgp <1-255> <1-255> <1-255>",
11858       "Define an administrative distance\n"
11859       "BGP distance\n"
11860       "Distance for routes external to the AS\n"
11861       "Distance for routes internal to the AS\n"
11862       "Distance for local routes\n")
11863{
11864  struct bgp *bgp;
11865
11866  bgp = vty->index;
11867
11868  bgp->distance_ebgp = atoi (argv[0]);
11869  bgp->distance_ibgp = atoi (argv[1]);
11870  bgp->distance_local = atoi (argv[2]);
11871  return CMD_SUCCESS;
11872}
11873
11874DEFUN (no_bgp_distance,
11875       no_bgp_distance_cmd,
11876       "no distance bgp <1-255> <1-255> <1-255>",
11877       NO_STR
11878       "Define an administrative distance\n"
11879       "BGP distance\n"
11880       "Distance for routes external to the AS\n"
11881       "Distance for routes internal to the AS\n"
11882       "Distance for local routes\n")
11883{
11884  struct bgp *bgp;
11885
11886  bgp = vty->index;
11887
11888  bgp->distance_ebgp= 0;
11889  bgp->distance_ibgp = 0;
11890  bgp->distance_local = 0;
11891  return CMD_SUCCESS;
11892}
11893
11894ALIAS (no_bgp_distance,
11895       no_bgp_distance2_cmd,
11896       "no distance bgp",
11897       NO_STR
11898       "Define an administrative distance\n"
11899       "BGP distance\n")
11900
11901DEFUN (bgp_distance_source,
11902       bgp_distance_source_cmd,
11903       "distance <1-255> A.B.C.D/M",
11904       "Define an administrative distance\n"
11905       "Administrative distance\n"
11906       "IP source prefix\n")
11907{
11908  bgp_distance_set (vty, argv[0], argv[1], NULL);
11909  return CMD_SUCCESS;
11910}
11911
11912DEFUN (no_bgp_distance_source,
11913       no_bgp_distance_source_cmd,
11914       "no distance <1-255> A.B.C.D/M",
11915       NO_STR
11916       "Define an administrative distance\n"
11917       "Administrative distance\n"
11918       "IP source prefix\n")
11919{
11920  bgp_distance_unset (vty, argv[0], argv[1], NULL);
11921  return CMD_SUCCESS;
11922}
11923
11924DEFUN (bgp_distance_source_access_list,
11925       bgp_distance_source_access_list_cmd,
11926       "distance <1-255> A.B.C.D/M WORD",
11927       "Define an administrative distance\n"
11928       "Administrative distance\n"
11929       "IP source prefix\n"
11930       "Access list name\n")
11931{
11932  bgp_distance_set (vty, argv[0], argv[1], argv[2]);
11933  return CMD_SUCCESS;
11934}
11935
11936DEFUN (no_bgp_distance_source_access_list,
11937       no_bgp_distance_source_access_list_cmd,
11938       "no distance <1-255> A.B.C.D/M WORD",
11939       NO_STR
11940       "Define an administrative distance\n"
11941       "Administrative distance\n"
11942       "IP source prefix\n"
11943       "Access list name\n")
11944{
11945  bgp_distance_unset (vty, argv[0], argv[1], argv[2]);
11946  return CMD_SUCCESS;
11947}
11948
11949DEFUN (bgp_damp_set,
11950       bgp_damp_set_cmd,
11951       "bgp dampening <1-45> <1-20000> <1-20000> <1-255>",
11952       "BGP Specific commands\n"
11953       "Enable route-flap dampening\n"
11954       "Half-life time for the penalty\n"
11955       "Value to start reusing a route\n"
11956       "Value to start suppressing a route\n"
11957       "Maximum duration to suppress a stable route\n")
11958{
11959  struct bgp *bgp;
11960  int half = DEFAULT_HALF_LIFE * 60;
11961  int reuse = DEFAULT_REUSE;
11962  int suppress = DEFAULT_SUPPRESS;
11963  int max = 4 * half;
11964
11965  if (argc == 4)
11966    {
11967      half = atoi (argv[0]) * 60;
11968      reuse = atoi (argv[1]);
11969      suppress = atoi (argv[2]);
11970      max = atoi (argv[3]) * 60;
11971    }
11972  else if (argc == 1)
11973    {
11974      half = atoi (argv[0]) * 60;
11975      max = 4 * half;
11976    }
11977
11978  bgp = vty->index;
11979  return bgp_damp_enable (bgp, bgp_node_afi (vty), bgp_node_safi (vty),
11980			  half, reuse, suppress, max);
11981}
11982
11983ALIAS (bgp_damp_set,
11984       bgp_damp_set2_cmd,
11985       "bgp dampening <1-45>",
11986       "BGP Specific commands\n"
11987       "Enable route-flap dampening\n"
11988       "Half-life time for the penalty\n")
11989
11990ALIAS (bgp_damp_set,
11991       bgp_damp_set3_cmd,
11992       "bgp dampening",
11993       "BGP Specific commands\n"
11994       "Enable route-flap dampening\n")
11995
11996DEFUN (bgp_damp_unset,
11997       bgp_damp_unset_cmd,
11998       "no bgp dampening",
11999       NO_STR
12000       "BGP Specific commands\n"
12001       "Enable route-flap dampening\n")
12002{
12003  struct bgp *bgp;
12004
12005  bgp = vty->index;
12006  return bgp_damp_disable (bgp, bgp_node_afi (vty), bgp_node_safi (vty));
12007}
12008
12009ALIAS (bgp_damp_unset,
12010       bgp_damp_unset2_cmd,
12011       "no bgp dampening <1-45> <1-20000> <1-20000> <1-255>",
12012       NO_STR
12013       "BGP Specific commands\n"
12014       "Enable route-flap dampening\n"
12015       "Half-life time for the penalty\n"
12016       "Value to start reusing a route\n"
12017       "Value to start suppressing a route\n"
12018       "Maximum duration to suppress a stable route\n")
12019
12020DEFUN (show_ip_bgp_dampened_paths,
12021       show_ip_bgp_dampened_paths_cmd,
12022       "show ip bgp dampened-paths",
12023       SHOW_STR
12024       IP_STR
12025       BGP_STR
12026       "Display paths suppressed due to dampening\n")
12027{
12028  return bgp_show (vty, NULL, AFI_IP, SAFI_UNICAST, bgp_show_type_dampend_paths,
12029                   NULL);
12030}
12031
12032DEFUN (show_ip_bgp_flap_statistics,
12033       show_ip_bgp_flap_statistics_cmd,
12034       "show ip bgp flap-statistics",
12035       SHOW_STR
12036       IP_STR
12037       BGP_STR
12038       "Display flap statistics of routes\n")
12039{
12040  return bgp_show (vty, NULL, AFI_IP, SAFI_UNICAST,
12041                   bgp_show_type_flap_statistics, NULL);
12042}
12043
12044/* Display specified route of BGP table. */
12045static int
12046bgp_clear_damp_route (struct vty *vty, const char *view_name,
12047                      const char *ip_str, afi_t afi, safi_t safi,
12048                      struct prefix_rd *prd, int prefix_check)
12049{
12050  int ret;
12051  struct prefix match;
12052  struct bgp_node *rn;
12053  struct bgp_node *rm;
12054  struct bgp_info *ri;
12055  struct bgp_info *ri_temp;
12056  struct bgp *bgp;
12057  struct bgp_table *table;
12058
12059  /* BGP structure lookup. */
12060  if (view_name)
12061    {
12062      bgp = bgp_lookup_by_name (view_name);
12063      if (bgp == NULL)
12064	{
12065	  vty_out (vty, "%% Can't find BGP view %s%s", view_name, VTY_NEWLINE);
12066	  return CMD_WARNING;
12067	}
12068    }
12069  else
12070    {
12071      bgp = bgp_get_default ();
12072      if (bgp == NULL)
12073	{
12074	  vty_out (vty, "%% No BGP process is configured%s", VTY_NEWLINE);
12075	  return CMD_WARNING;
12076	}
12077    }
12078
12079  /* Check IP address argument. */
12080  ret = str2prefix (ip_str, &match);
12081  if (! ret)
12082    {
12083      vty_out (vty, "%% address is malformed%s", VTY_NEWLINE);
12084      return CMD_WARNING;
12085    }
12086
12087  match.family = afi2family (afi);
12088
12089  if (safi == SAFI_MPLS_VPN)
12090    {
12091      for (rn = bgp_table_top (bgp->rib[AFI_IP][SAFI_MPLS_VPN]); rn; rn = bgp_route_next (rn))
12092        {
12093          if (prd && memcmp (rn->p.u.val, prd->val, 8) != 0)
12094            continue;
12095
12096	  if ((table = rn->info) != NULL)
12097	    if ((rm = bgp_node_match (table, &match)) != NULL)
12098              {
12099                if (! prefix_check || rm->p.prefixlen == match.prefixlen)
12100                  {
12101                    ri = rm->info;
12102                    while (ri)
12103                      {
12104                        if (ri->extra && ri->extra->damp_info)
12105                          {
12106                            ri_temp = ri->next;
12107                            bgp_damp_info_free (ri->extra->damp_info, 1);
12108                            ri = ri_temp;
12109                          }
12110                        else
12111                          ri = ri->next;
12112                      }
12113                  }
12114
12115                bgp_unlock_node (rm);
12116              }
12117        }
12118    }
12119  else
12120    {
12121      if ((rn = bgp_node_match (bgp->rib[afi][safi], &match)) != NULL)
12122        {
12123          if (! prefix_check || rn->p.prefixlen == match.prefixlen)
12124            {
12125              ri = rn->info;
12126              while (ri)
12127                {
12128                  if (ri->extra && ri->extra->damp_info)
12129                    {
12130                      ri_temp = ri->next;
12131                      bgp_damp_info_free (ri->extra->damp_info, 1);
12132                      ri = ri_temp;
12133                    }
12134                  else
12135                    ri = ri->next;
12136                }
12137            }
12138
12139          bgp_unlock_node (rn);
12140        }
12141    }
12142
12143  return CMD_SUCCESS;
12144}
12145
12146DEFUN (clear_ip_bgp_dampening,
12147       clear_ip_bgp_dampening_cmd,
12148       "clear ip bgp dampening",
12149       CLEAR_STR
12150       IP_STR
12151       BGP_STR
12152       "Clear route flap dampening information\n")
12153{
12154  bgp_damp_info_clean ();
12155  return CMD_SUCCESS;
12156}
12157
12158DEFUN (clear_ip_bgp_dampening_prefix,
12159       clear_ip_bgp_dampening_prefix_cmd,
12160       "clear ip bgp dampening A.B.C.D/M",
12161       CLEAR_STR
12162       IP_STR
12163       BGP_STR
12164       "Clear route flap dampening information\n"
12165       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
12166{
12167  return bgp_clear_damp_route (vty, NULL, argv[0], AFI_IP,
12168			       SAFI_UNICAST, NULL, 1);
12169}
12170
12171DEFUN (clear_ip_bgp_dampening_address,
12172       clear_ip_bgp_dampening_address_cmd,
12173       "clear ip bgp dampening A.B.C.D",
12174       CLEAR_STR
12175       IP_STR
12176       BGP_STR
12177       "Clear route flap dampening information\n"
12178       "Network to clear damping information\n")
12179{
12180  return bgp_clear_damp_route (vty, NULL, argv[0], AFI_IP,
12181			       SAFI_UNICAST, NULL, 0);
12182}
12183
12184DEFUN (clear_ip_bgp_dampening_address_mask,
12185       clear_ip_bgp_dampening_address_mask_cmd,
12186       "clear ip bgp dampening A.B.C.D A.B.C.D",
12187       CLEAR_STR
12188       IP_STR
12189       BGP_STR
12190       "Clear route flap dampening information\n"
12191       "Network to clear damping information\n"
12192       "Network mask\n")
12193{
12194  int ret;
12195  char prefix_str[BUFSIZ];
12196
12197  ret = netmask_str2prefix_str (argv[0], argv[1], prefix_str);
12198  if (! ret)
12199    {
12200      vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
12201      return CMD_WARNING;
12202    }
12203
12204  return bgp_clear_damp_route (vty, NULL, prefix_str, AFI_IP,
12205			       SAFI_UNICAST, NULL, 0);
12206}
12207
12208static int
12209bgp_config_write_network_vpnv4 (struct vty *vty, struct bgp *bgp,
12210				afi_t afi, safi_t safi, int *write)
12211{
12212  struct bgp_node *prn;
12213  struct bgp_node *rn;
12214  struct bgp_table *table;
12215  struct prefix *p;
12216  struct prefix_rd *prd;
12217  struct bgp_static *bgp_static;
12218  u_int32_t label;
12219  char buf[SU_ADDRSTRLEN];
12220  char rdbuf[RD_ADDRSTRLEN];
12221
12222  /* Network configuration. */
12223  for (prn = bgp_table_top (bgp->route[afi][safi]); prn; prn = bgp_route_next (prn))
12224    if ((table = prn->info) != NULL)
12225      for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
12226	if ((bgp_static = rn->info) != NULL)
12227	  {
12228	    p = &rn->p;
12229	    prd = (struct prefix_rd *) &prn->p;
12230
12231	    /* "address-family" display.  */
12232	    bgp_config_write_family_header (vty, afi, safi, write);
12233
12234	    /* "network" configuration display.  */
12235	    prefix_rd2str (prd, rdbuf, RD_ADDRSTRLEN);
12236	    label = decode_label (bgp_static->tag);
12237
12238	    vty_out (vty, " network %s/%d rd %s tag %d",
12239		     inet_ntop (p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
12240		     p->prefixlen,
12241		     rdbuf, label);
12242	    vty_out (vty, "%s", VTY_NEWLINE);
12243	  }
12244  return 0;
12245}
12246
12247/* Configuration of static route announcement and aggregate
12248   information. */
12249int
12250bgp_config_write_network (struct vty *vty, struct bgp *bgp,
12251			  afi_t afi, safi_t safi, int *write)
12252{
12253  struct bgp_node *rn;
12254  struct prefix *p;
12255  struct bgp_static *bgp_static;
12256  struct bgp_aggregate *bgp_aggregate;
12257  char buf[SU_ADDRSTRLEN];
12258
12259  if (afi == AFI_IP && safi == SAFI_MPLS_VPN)
12260    return bgp_config_write_network_vpnv4 (vty, bgp, afi, safi, write);
12261
12262  /* Network configuration. */
12263  for (rn = bgp_table_top (bgp->route[afi][safi]); rn; rn = bgp_route_next (rn))
12264    if ((bgp_static = rn->info) != NULL)
12265      {
12266	p = &rn->p;
12267
12268	/* "address-family" display.  */
12269	bgp_config_write_family_header (vty, afi, safi, write);
12270
12271	/* "network" configuration display.  */
12272	if (bgp_option_check (BGP_OPT_CONFIG_CISCO) && afi == AFI_IP)
12273	  {
12274	    u_int32_t destination;
12275	    struct in_addr netmask;
12276
12277	    destination = ntohl (p->u.prefix4.s_addr);
12278	    masklen2ip (p->prefixlen, &netmask);
12279	    vty_out (vty, " network %s",
12280		     inet_ntop (p->family, &p->u.prefix, buf, SU_ADDRSTRLEN));
12281
12282	    if ((IN_CLASSC (destination) && p->prefixlen == 24)
12283		|| (IN_CLASSB (destination) && p->prefixlen == 16)
12284		|| (IN_CLASSA (destination) && p->prefixlen == 8)
12285		|| p->u.prefix4.s_addr == 0)
12286	      {
12287		/* Natural mask is not display. */
12288	      }
12289	    else
12290	      vty_out (vty, " mask %s", inet_ntoa (netmask));
12291	  }
12292	else
12293	  {
12294	    vty_out (vty, " network %s/%d",
12295		     inet_ntop (p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
12296		     p->prefixlen);
12297	  }
12298
12299	if (bgp_static->rmap.name)
12300	  vty_out (vty, " route-map %s", bgp_static->rmap.name);
12301	else
12302	  {
12303	    if (bgp_static->backdoor)
12304	      vty_out (vty, " backdoor");
12305          }
12306
12307	vty_out (vty, "%s", VTY_NEWLINE);
12308      }
12309
12310  /* Aggregate-address configuration. */
12311  for (rn = bgp_table_top (bgp->aggregate[afi][safi]); rn; rn = bgp_route_next (rn))
12312    if ((bgp_aggregate = rn->info) != NULL)
12313      {
12314	p = &rn->p;
12315
12316	/* "address-family" display.  */
12317	bgp_config_write_family_header (vty, afi, safi, write);
12318
12319	if (bgp_option_check (BGP_OPT_CONFIG_CISCO) && afi == AFI_IP)
12320	  {
12321	    struct in_addr netmask;
12322
12323	    masklen2ip (p->prefixlen, &netmask);
12324	    vty_out (vty, " aggregate-address %s %s",
12325		     inet_ntop (p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
12326		     inet_ntoa (netmask));
12327	  }
12328	else
12329	  {
12330	    vty_out (vty, " aggregate-address %s/%d",
12331		     inet_ntop (p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
12332		     p->prefixlen);
12333	  }
12334
12335	if (bgp_aggregate->as_set)
12336	  vty_out (vty, " as-set");
12337
12338	if (bgp_aggregate->summary_only)
12339	  vty_out (vty, " summary-only");
12340
12341	vty_out (vty, "%s", VTY_NEWLINE);
12342      }
12343
12344  return 0;
12345}
12346
12347int
12348bgp_config_write_distance (struct vty *vty, struct bgp *bgp)
12349{
12350  struct bgp_node *rn;
12351  struct bgp_distance *bdistance;
12352
12353  /* Distance configuration. */
12354  if (bgp->distance_ebgp
12355      && bgp->distance_ibgp
12356      && bgp->distance_local
12357      && (bgp->distance_ebgp != ZEBRA_EBGP_DISTANCE_DEFAULT
12358	  || bgp->distance_ibgp != ZEBRA_IBGP_DISTANCE_DEFAULT
12359	  || bgp->distance_local != ZEBRA_IBGP_DISTANCE_DEFAULT))
12360    vty_out (vty, " distance bgp %d %d %d%s",
12361	     bgp->distance_ebgp, bgp->distance_ibgp, bgp->distance_local,
12362	     VTY_NEWLINE);
12363
12364  for (rn = bgp_table_top (bgp_distance_table); rn; rn = bgp_route_next (rn))
12365    if ((bdistance = rn->info) != NULL)
12366      {
12367	vty_out (vty, " distance %d %s/%d %s%s", bdistance->distance,
12368		 inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen,
12369		 bdistance->access_list ? bdistance->access_list : "",
12370		 VTY_NEWLINE);
12371      }
12372
12373  return 0;
12374}
12375
12376/* Allocate routing table structure and install commands. */
12377void
12378bgp_route_init (void)
12379{
12380  /* Init BGP distance table. */
12381  bgp_distance_table = bgp_table_init (AFI_IP, SAFI_UNICAST);
12382
12383  /* IPv4 BGP commands. */
12384  install_element (BGP_NODE, &bgp_network_cmd);
12385  install_element (BGP_NODE, &bgp_network_mask_cmd);
12386  install_element (BGP_NODE, &bgp_network_mask_natural_cmd);
12387  install_element (BGP_NODE, &bgp_network_route_map_cmd);
12388  install_element (BGP_NODE, &bgp_network_mask_route_map_cmd);
12389  install_element (BGP_NODE, &bgp_network_mask_natural_route_map_cmd);
12390  install_element (BGP_NODE, &bgp_network_backdoor_cmd);
12391  install_element (BGP_NODE, &bgp_network_mask_backdoor_cmd);
12392  install_element (BGP_NODE, &bgp_network_mask_natural_backdoor_cmd);
12393  install_element (BGP_NODE, &no_bgp_network_cmd);
12394  install_element (BGP_NODE, &no_bgp_network_mask_cmd);
12395  install_element (BGP_NODE, &no_bgp_network_mask_natural_cmd);
12396  install_element (BGP_NODE, &no_bgp_network_route_map_cmd);
12397  install_element (BGP_NODE, &no_bgp_network_mask_route_map_cmd);
12398  install_element (BGP_NODE, &no_bgp_network_mask_natural_route_map_cmd);
12399  install_element (BGP_NODE, &no_bgp_network_backdoor_cmd);
12400  install_element (BGP_NODE, &no_bgp_network_mask_backdoor_cmd);
12401  install_element (BGP_NODE, &no_bgp_network_mask_natural_backdoor_cmd);
12402
12403  install_element (BGP_NODE, &aggregate_address_cmd);
12404  install_element (BGP_NODE, &aggregate_address_mask_cmd);
12405  install_element (BGP_NODE, &aggregate_address_summary_only_cmd);
12406  install_element (BGP_NODE, &aggregate_address_mask_summary_only_cmd);
12407  install_element (BGP_NODE, &aggregate_address_as_set_cmd);
12408  install_element (BGP_NODE, &aggregate_address_mask_as_set_cmd);
12409  install_element (BGP_NODE, &aggregate_address_as_set_summary_cmd);
12410  install_element (BGP_NODE, &aggregate_address_mask_as_set_summary_cmd);
12411  install_element (BGP_NODE, &aggregate_address_summary_as_set_cmd);
12412  install_element (BGP_NODE, &aggregate_address_mask_summary_as_set_cmd);
12413  install_element (BGP_NODE, &no_aggregate_address_cmd);
12414  install_element (BGP_NODE, &no_aggregate_address_summary_only_cmd);
12415  install_element (BGP_NODE, &no_aggregate_address_as_set_cmd);
12416  install_element (BGP_NODE, &no_aggregate_address_as_set_summary_cmd);
12417  install_element (BGP_NODE, &no_aggregate_address_summary_as_set_cmd);
12418  install_element (BGP_NODE, &no_aggregate_address_mask_cmd);
12419  install_element (BGP_NODE, &no_aggregate_address_mask_summary_only_cmd);
12420  install_element (BGP_NODE, &no_aggregate_address_mask_as_set_cmd);
12421  install_element (BGP_NODE, &no_aggregate_address_mask_as_set_summary_cmd);
12422  install_element (BGP_NODE, &no_aggregate_address_mask_summary_as_set_cmd);
12423
12424  /* IPv4 unicast configuration.  */
12425  install_element (BGP_IPV4_NODE, &bgp_network_cmd);
12426  install_element (BGP_IPV4_NODE, &bgp_network_mask_cmd);
12427  install_element (BGP_IPV4_NODE, &bgp_network_mask_natural_cmd);
12428  install_element (BGP_IPV4_NODE, &bgp_network_route_map_cmd);
12429  install_element (BGP_IPV4_NODE, &bgp_network_mask_route_map_cmd);
12430  install_element (BGP_IPV4_NODE, &bgp_network_mask_natural_route_map_cmd);
12431  install_element (BGP_IPV4_NODE, &no_bgp_network_cmd);
12432  install_element (BGP_IPV4_NODE, &no_bgp_network_mask_cmd);
12433  install_element (BGP_IPV4_NODE, &no_bgp_network_mask_natural_cmd);
12434  install_element (BGP_IPV4_NODE, &no_bgp_network_route_map_cmd);
12435  install_element (BGP_IPV4_NODE, &no_bgp_network_mask_route_map_cmd);
12436  install_element (BGP_IPV4_NODE, &no_bgp_network_mask_natural_route_map_cmd);
12437
12438  install_element (BGP_IPV4_NODE, &aggregate_address_cmd);
12439  install_element (BGP_IPV4_NODE, &aggregate_address_mask_cmd);
12440  install_element (BGP_IPV4_NODE, &aggregate_address_summary_only_cmd);
12441  install_element (BGP_IPV4_NODE, &aggregate_address_mask_summary_only_cmd);
12442  install_element (BGP_IPV4_NODE, &aggregate_address_as_set_cmd);
12443  install_element (BGP_IPV4_NODE, &aggregate_address_mask_as_set_cmd);
12444  install_element (BGP_IPV4_NODE, &aggregate_address_as_set_summary_cmd);
12445  install_element (BGP_IPV4_NODE, &aggregate_address_mask_as_set_summary_cmd);
12446  install_element (BGP_IPV4_NODE, &aggregate_address_summary_as_set_cmd);
12447  install_element (BGP_IPV4_NODE, &aggregate_address_mask_summary_as_set_cmd);
12448  install_element (BGP_IPV4_NODE, &no_aggregate_address_cmd);
12449  install_element (BGP_IPV4_NODE, &no_aggregate_address_summary_only_cmd);
12450  install_element (BGP_IPV4_NODE, &no_aggregate_address_as_set_cmd);
12451  install_element (BGP_IPV4_NODE, &no_aggregate_address_as_set_summary_cmd);
12452  install_element (BGP_IPV4_NODE, &no_aggregate_address_summary_as_set_cmd);
12453  install_element (BGP_IPV4_NODE, &no_aggregate_address_mask_cmd);
12454  install_element (BGP_IPV4_NODE, &no_aggregate_address_mask_summary_only_cmd);
12455  install_element (BGP_IPV4_NODE, &no_aggregate_address_mask_as_set_cmd);
12456  install_element (BGP_IPV4_NODE, &no_aggregate_address_mask_as_set_summary_cmd);
12457  install_element (BGP_IPV4_NODE, &no_aggregate_address_mask_summary_as_set_cmd);
12458
12459  /* IPv4 multicast configuration.  */
12460  install_element (BGP_IPV4M_NODE, &bgp_network_cmd);
12461  install_element (BGP_IPV4M_NODE, &bgp_network_mask_cmd);
12462  install_element (BGP_IPV4M_NODE, &bgp_network_mask_natural_cmd);
12463  install_element (BGP_IPV4M_NODE, &bgp_network_route_map_cmd);
12464  install_element (BGP_IPV4M_NODE, &bgp_network_mask_route_map_cmd);
12465  install_element (BGP_IPV4M_NODE, &bgp_network_mask_natural_route_map_cmd);
12466  install_element (BGP_IPV4M_NODE, &no_bgp_network_cmd);
12467  install_element (BGP_IPV4M_NODE, &no_bgp_network_mask_cmd);
12468  install_element (BGP_IPV4M_NODE, &no_bgp_network_mask_natural_cmd);
12469  install_element (BGP_IPV4M_NODE, &no_bgp_network_route_map_cmd);
12470  install_element (BGP_IPV4M_NODE, &no_bgp_network_mask_route_map_cmd);
12471  install_element (BGP_IPV4M_NODE, &no_bgp_network_mask_natural_route_map_cmd);
12472  install_element (BGP_IPV4M_NODE, &aggregate_address_cmd);
12473  install_element (BGP_IPV4M_NODE, &aggregate_address_mask_cmd);
12474  install_element (BGP_IPV4M_NODE, &aggregate_address_summary_only_cmd);
12475  install_element (BGP_IPV4M_NODE, &aggregate_address_mask_summary_only_cmd);
12476  install_element (BGP_IPV4M_NODE, &aggregate_address_as_set_cmd);
12477  install_element (BGP_IPV4M_NODE, &aggregate_address_mask_as_set_cmd);
12478  install_element (BGP_IPV4M_NODE, &aggregate_address_as_set_summary_cmd);
12479  install_element (BGP_IPV4M_NODE, &aggregate_address_mask_as_set_summary_cmd);
12480  install_element (BGP_IPV4M_NODE, &aggregate_address_summary_as_set_cmd);
12481  install_element (BGP_IPV4M_NODE, &aggregate_address_mask_summary_as_set_cmd);
12482  install_element (BGP_IPV4M_NODE, &no_aggregate_address_cmd);
12483  install_element (BGP_IPV4M_NODE, &no_aggregate_address_summary_only_cmd);
12484  install_element (BGP_IPV4M_NODE, &no_aggregate_address_as_set_cmd);
12485  install_element (BGP_IPV4M_NODE, &no_aggregate_address_as_set_summary_cmd);
12486  install_element (BGP_IPV4M_NODE, &no_aggregate_address_summary_as_set_cmd);
12487  install_element (BGP_IPV4M_NODE, &no_aggregate_address_mask_cmd);
12488  install_element (BGP_IPV4M_NODE, &no_aggregate_address_mask_summary_only_cmd);
12489  install_element (BGP_IPV4M_NODE, &no_aggregate_address_mask_as_set_cmd);
12490  install_element (BGP_IPV4M_NODE, &no_aggregate_address_mask_as_set_summary_cmd);
12491  install_element (BGP_IPV4M_NODE, &no_aggregate_address_mask_summary_as_set_cmd);
12492
12493  install_element (VIEW_NODE, &show_ip_bgp_cmd);
12494  install_element (VIEW_NODE, &show_ip_bgp_ipv4_cmd);
12495  install_element (VIEW_NODE, &show_bgp_ipv4_safi_cmd);
12496  install_element (VIEW_NODE, &show_ip_bgp_route_cmd);
12497  install_element (VIEW_NODE, &show_ip_bgp_ipv4_route_cmd);
12498  install_element (VIEW_NODE, &show_bgp_ipv4_safi_route_cmd);
12499  install_element (VIEW_NODE, &show_ip_bgp_vpnv4_all_route_cmd);
12500  install_element (VIEW_NODE, &show_ip_bgp_vpnv4_rd_route_cmd);
12501  install_element (VIEW_NODE, &show_ip_bgp_prefix_cmd);
12502  install_element (VIEW_NODE, &show_ip_bgp_ipv4_prefix_cmd);
12503  install_element (VIEW_NODE, &show_bgp_ipv4_safi_prefix_cmd);
12504  install_element (VIEW_NODE, &show_ip_bgp_vpnv4_all_prefix_cmd);
12505  install_element (VIEW_NODE, &show_ip_bgp_vpnv4_rd_prefix_cmd);
12506  install_element (VIEW_NODE, &show_ip_bgp_view_cmd);
12507  install_element (VIEW_NODE, &show_ip_bgp_view_route_cmd);
12508  install_element (VIEW_NODE, &show_ip_bgp_view_prefix_cmd);
12509  install_element (VIEW_NODE, &show_ip_bgp_regexp_cmd);
12510  install_element (VIEW_NODE, &show_ip_bgp_ipv4_regexp_cmd);
12511  install_element (VIEW_NODE, &show_ip_bgp_prefix_list_cmd);
12512  install_element (VIEW_NODE, &show_ip_bgp_ipv4_prefix_list_cmd);
12513  install_element (VIEW_NODE, &show_ip_bgp_filter_list_cmd);
12514  install_element (VIEW_NODE, &show_ip_bgp_ipv4_filter_list_cmd);
12515  install_element (VIEW_NODE, &show_ip_bgp_route_map_cmd);
12516  install_element (VIEW_NODE, &show_ip_bgp_ipv4_route_map_cmd);
12517  install_element (VIEW_NODE, &show_ip_bgp_cidr_only_cmd);
12518  install_element (VIEW_NODE, &show_ip_bgp_ipv4_cidr_only_cmd);
12519  install_element (VIEW_NODE, &show_ip_bgp_community_all_cmd);
12520  install_element (VIEW_NODE, &show_ip_bgp_ipv4_community_all_cmd);
12521  install_element (VIEW_NODE, &show_ip_bgp_community_cmd);
12522  install_element (VIEW_NODE, &show_ip_bgp_community2_cmd);
12523  install_element (VIEW_NODE, &show_ip_bgp_community3_cmd);
12524  install_element (VIEW_NODE, &show_ip_bgp_community4_cmd);
12525  install_element (VIEW_NODE, &show_ip_bgp_ipv4_community_cmd);
12526  install_element (VIEW_NODE, &show_ip_bgp_ipv4_community2_cmd);
12527  install_element (VIEW_NODE, &show_ip_bgp_ipv4_community3_cmd);
12528  install_element (VIEW_NODE, &show_ip_bgp_ipv4_community4_cmd);
12529  install_element (VIEW_NODE, &show_bgp_view_afi_safi_community_all_cmd);
12530  install_element (VIEW_NODE, &show_bgp_view_afi_safi_community_cmd);
12531  install_element (VIEW_NODE, &show_bgp_view_afi_safi_community2_cmd);
12532  install_element (VIEW_NODE, &show_bgp_view_afi_safi_community3_cmd);
12533  install_element (VIEW_NODE, &show_bgp_view_afi_safi_community4_cmd);
12534  install_element (VIEW_NODE, &show_ip_bgp_community_exact_cmd);
12535  install_element (VIEW_NODE, &show_ip_bgp_community2_exact_cmd);
12536  install_element (VIEW_NODE, &show_ip_bgp_community3_exact_cmd);
12537  install_element (VIEW_NODE, &show_ip_bgp_community4_exact_cmd);
12538  install_element (VIEW_NODE, &show_ip_bgp_ipv4_community_exact_cmd);
12539  install_element (VIEW_NODE, &show_ip_bgp_ipv4_community2_exact_cmd);
12540  install_element (VIEW_NODE, &show_ip_bgp_ipv4_community3_exact_cmd);
12541  install_element (VIEW_NODE, &show_ip_bgp_ipv4_community4_exact_cmd);
12542  install_element (VIEW_NODE, &show_ip_bgp_community_list_cmd);
12543  install_element (VIEW_NODE, &show_ip_bgp_ipv4_community_list_cmd);
12544  install_element (VIEW_NODE, &show_ip_bgp_community_list_exact_cmd);
12545  install_element (VIEW_NODE, &show_ip_bgp_ipv4_community_list_exact_cmd);
12546  install_element (VIEW_NODE, &show_ip_bgp_prefix_longer_cmd);
12547  install_element (VIEW_NODE, &show_ip_bgp_ipv4_prefix_longer_cmd);
12548  install_element (VIEW_NODE, &show_ip_bgp_neighbor_advertised_route_cmd);
12549  install_element (VIEW_NODE, &show_ip_bgp_ipv4_neighbor_advertised_route_cmd);
12550  install_element (VIEW_NODE, &show_ip_bgp_neighbor_received_routes_cmd);
12551  install_element (VIEW_NODE, &show_ip_bgp_ipv4_neighbor_received_routes_cmd);
12552  install_element (VIEW_NODE, &show_bgp_view_afi_safi_neighbor_adv_recd_routes_cmd);
12553  install_element (VIEW_NODE, &show_ip_bgp_neighbor_routes_cmd);
12554  install_element (VIEW_NODE, &show_ip_bgp_ipv4_neighbor_routes_cmd);
12555  install_element (VIEW_NODE, &show_ip_bgp_neighbor_received_prefix_filter_cmd);
12556  install_element (VIEW_NODE, &show_ip_bgp_ipv4_neighbor_received_prefix_filter_cmd);
12557  install_element (VIEW_NODE, &show_ip_bgp_dampened_paths_cmd);
12558  install_element (VIEW_NODE, &show_ip_bgp_flap_statistics_cmd);
12559  install_element (VIEW_NODE, &show_ip_bgp_flap_address_cmd);
12560  install_element (VIEW_NODE, &show_ip_bgp_flap_prefix_cmd);
12561  install_element (VIEW_NODE, &show_ip_bgp_flap_cidr_only_cmd);
12562  install_element (VIEW_NODE, &show_ip_bgp_flap_regexp_cmd);
12563  install_element (VIEW_NODE, &show_ip_bgp_flap_filter_list_cmd);
12564  install_element (VIEW_NODE, &show_ip_bgp_flap_prefix_list_cmd);
12565  install_element (VIEW_NODE, &show_ip_bgp_flap_prefix_longer_cmd);
12566  install_element (VIEW_NODE, &show_ip_bgp_flap_route_map_cmd);
12567  install_element (VIEW_NODE, &show_ip_bgp_neighbor_flap_cmd);
12568  install_element (VIEW_NODE, &show_ip_bgp_neighbor_damp_cmd);
12569  install_element (VIEW_NODE, &show_ip_bgp_rsclient_cmd);
12570  install_element (VIEW_NODE, &show_bgp_ipv4_safi_rsclient_cmd);
12571  install_element (VIEW_NODE, &show_ip_bgp_rsclient_route_cmd);
12572  install_element (VIEW_NODE, &show_bgp_ipv4_safi_rsclient_route_cmd);
12573  install_element (VIEW_NODE, &show_ip_bgp_rsclient_prefix_cmd);
12574  install_element (VIEW_NODE, &show_bgp_ipv4_safi_rsclient_prefix_cmd);
12575  install_element (VIEW_NODE, &show_ip_bgp_view_neighbor_advertised_route_cmd);
12576  install_element (VIEW_NODE, &show_ip_bgp_view_neighbor_received_routes_cmd);
12577  install_element (VIEW_NODE, &show_ip_bgp_view_rsclient_cmd);
12578  install_element (VIEW_NODE, &show_bgp_view_ipv4_safi_rsclient_cmd);
12579  install_element (VIEW_NODE, &show_ip_bgp_view_rsclient_route_cmd);
12580  install_element (VIEW_NODE, &show_bgp_view_ipv4_safi_rsclient_route_cmd);
12581  install_element (VIEW_NODE, &show_ip_bgp_view_rsclient_prefix_cmd);
12582  install_element (VIEW_NODE, &show_bgp_view_ipv4_safi_rsclient_prefix_cmd);
12583
12584  /* Restricted node: VIEW_NODE - (set of dangerous commands) */
12585  install_element (RESTRICTED_NODE, &show_ip_bgp_route_cmd);
12586  install_element (RESTRICTED_NODE, &show_ip_bgp_ipv4_route_cmd);
12587  install_element (RESTRICTED_NODE, &show_bgp_ipv4_safi_route_cmd);
12588  install_element (RESTRICTED_NODE, &show_ip_bgp_vpnv4_rd_route_cmd);
12589  install_element (RESTRICTED_NODE, &show_ip_bgp_prefix_cmd);
12590  install_element (RESTRICTED_NODE, &show_ip_bgp_ipv4_prefix_cmd);
12591  install_element (RESTRICTED_NODE, &show_bgp_ipv4_safi_prefix_cmd);
12592  install_element (RESTRICTED_NODE, &show_ip_bgp_vpnv4_all_prefix_cmd);
12593  install_element (RESTRICTED_NODE, &show_ip_bgp_vpnv4_rd_prefix_cmd);
12594  install_element (RESTRICTED_NODE, &show_ip_bgp_view_route_cmd);
12595  install_element (RESTRICTED_NODE, &show_ip_bgp_view_prefix_cmd);
12596  install_element (RESTRICTED_NODE, &show_ip_bgp_community_cmd);
12597  install_element (RESTRICTED_NODE, &show_ip_bgp_community2_cmd);
12598  install_element (RESTRICTED_NODE, &show_ip_bgp_community3_cmd);
12599  install_element (RESTRICTED_NODE, &show_ip_bgp_community4_cmd);
12600  install_element (RESTRICTED_NODE, &show_ip_bgp_ipv4_community_cmd);
12601  install_element (RESTRICTED_NODE, &show_ip_bgp_ipv4_community2_cmd);
12602  install_element (RESTRICTED_NODE, &show_ip_bgp_ipv4_community3_cmd);
12603  install_element (RESTRICTED_NODE, &show_ip_bgp_ipv4_community4_cmd);
12604  install_element (RESTRICTED_NODE, &show_bgp_view_afi_safi_community_all_cmd);
12605  install_element (RESTRICTED_NODE, &show_bgp_view_afi_safi_community_cmd);
12606  install_element (RESTRICTED_NODE, &show_bgp_view_afi_safi_community2_cmd);
12607  install_element (RESTRICTED_NODE, &show_bgp_view_afi_safi_community3_cmd);
12608  install_element (RESTRICTED_NODE, &show_bgp_view_afi_safi_community4_cmd);
12609  install_element (RESTRICTED_NODE, &show_ip_bgp_community_exact_cmd);
12610  install_element (RESTRICTED_NODE, &show_ip_bgp_community2_exact_cmd);
12611  install_element (RESTRICTED_NODE, &show_ip_bgp_community3_exact_cmd);
12612  install_element (RESTRICTED_NODE, &show_ip_bgp_community4_exact_cmd);
12613  install_element (RESTRICTED_NODE, &show_ip_bgp_ipv4_community_exact_cmd);
12614  install_element (RESTRICTED_NODE, &show_ip_bgp_ipv4_community2_exact_cmd);
12615  install_element (RESTRICTED_NODE, &show_ip_bgp_ipv4_community3_exact_cmd);
12616  install_element (RESTRICTED_NODE, &show_ip_bgp_ipv4_community4_exact_cmd);
12617  install_element (RESTRICTED_NODE, &show_ip_bgp_rsclient_route_cmd);
12618  install_element (RESTRICTED_NODE, &show_bgp_ipv4_safi_rsclient_route_cmd);
12619  install_element (RESTRICTED_NODE, &show_ip_bgp_rsclient_prefix_cmd);
12620  install_element (RESTRICTED_NODE, &show_bgp_ipv4_safi_rsclient_prefix_cmd);
12621  install_element (RESTRICTED_NODE, &show_ip_bgp_view_rsclient_route_cmd);
12622  install_element (RESTRICTED_NODE, &show_bgp_view_ipv4_safi_rsclient_route_cmd);
12623  install_element (RESTRICTED_NODE, &show_ip_bgp_view_rsclient_prefix_cmd);
12624  install_element (RESTRICTED_NODE, &show_bgp_view_ipv4_safi_rsclient_prefix_cmd);
12625
12626  install_element (ENABLE_NODE, &show_ip_bgp_cmd);
12627  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_cmd);
12628  install_element (ENABLE_NODE, &show_bgp_ipv4_safi_cmd);
12629  install_element (ENABLE_NODE, &show_ip_bgp_route_cmd);
12630  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_route_cmd);
12631  install_element (ENABLE_NODE, &show_bgp_ipv4_safi_route_cmd);
12632  install_element (ENABLE_NODE, &show_ip_bgp_vpnv4_all_route_cmd);
12633  install_element (ENABLE_NODE, &show_ip_bgp_vpnv4_rd_route_cmd);
12634  install_element (ENABLE_NODE, &show_ip_bgp_prefix_cmd);
12635  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_prefix_cmd);
12636  install_element (ENABLE_NODE, &show_bgp_ipv4_safi_prefix_cmd);
12637  install_element (ENABLE_NODE, &show_ip_bgp_vpnv4_all_prefix_cmd);
12638  install_element (ENABLE_NODE, &show_ip_bgp_vpnv4_rd_prefix_cmd);
12639  install_element (ENABLE_NODE, &show_ip_bgp_view_cmd);
12640  install_element (ENABLE_NODE, &show_ip_bgp_view_route_cmd);
12641  install_element (ENABLE_NODE, &show_ip_bgp_view_prefix_cmd);
12642  install_element (ENABLE_NODE, &show_ip_bgp_regexp_cmd);
12643  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_regexp_cmd);
12644  install_element (ENABLE_NODE, &show_ip_bgp_prefix_list_cmd);
12645  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_prefix_list_cmd);
12646  install_element (ENABLE_NODE, &show_ip_bgp_filter_list_cmd);
12647  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_filter_list_cmd);
12648  install_element (ENABLE_NODE, &show_ip_bgp_route_map_cmd);
12649  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_route_map_cmd);
12650  install_element (ENABLE_NODE, &show_ip_bgp_cidr_only_cmd);
12651  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_cidr_only_cmd);
12652  install_element (ENABLE_NODE, &show_ip_bgp_community_all_cmd);
12653  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_community_all_cmd);
12654  install_element (ENABLE_NODE, &show_ip_bgp_community_cmd);
12655  install_element (ENABLE_NODE, &show_ip_bgp_community2_cmd);
12656  install_element (ENABLE_NODE, &show_ip_bgp_community3_cmd);
12657  install_element (ENABLE_NODE, &show_ip_bgp_community4_cmd);
12658  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_community_cmd);
12659  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_community2_cmd);
12660  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_community3_cmd);
12661  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_community4_cmd);
12662  install_element (ENABLE_NODE, &show_bgp_view_afi_safi_community_all_cmd);
12663  install_element (ENABLE_NODE, &show_bgp_view_afi_safi_community_cmd);
12664  install_element (ENABLE_NODE, &show_bgp_view_afi_safi_community2_cmd);
12665  install_element (ENABLE_NODE, &show_bgp_view_afi_safi_community3_cmd);
12666  install_element (ENABLE_NODE, &show_bgp_view_afi_safi_community4_cmd);
12667  install_element (ENABLE_NODE, &show_ip_bgp_community_exact_cmd);
12668  install_element (ENABLE_NODE, &show_ip_bgp_community2_exact_cmd);
12669  install_element (ENABLE_NODE, &show_ip_bgp_community3_exact_cmd);
12670  install_element (ENABLE_NODE, &show_ip_bgp_community4_exact_cmd);
12671  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_community_exact_cmd);
12672  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_community2_exact_cmd);
12673  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_community3_exact_cmd);
12674  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_community4_exact_cmd);
12675  install_element (ENABLE_NODE, &show_ip_bgp_community_list_cmd);
12676  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_community_list_cmd);
12677  install_element (ENABLE_NODE, &show_ip_bgp_community_list_exact_cmd);
12678  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_community_list_exact_cmd);
12679  install_element (ENABLE_NODE, &show_ip_bgp_prefix_longer_cmd);
12680  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_prefix_longer_cmd);
12681  install_element (ENABLE_NODE, &show_ip_bgp_neighbor_advertised_route_cmd);
12682  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_neighbor_advertised_route_cmd);
12683  install_element (ENABLE_NODE, &show_ip_bgp_neighbor_received_routes_cmd);
12684  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_neighbor_received_routes_cmd);
12685  install_element (ENABLE_NODE, &show_bgp_view_afi_safi_neighbor_adv_recd_routes_cmd);
12686  install_element (ENABLE_NODE, &show_ip_bgp_neighbor_routes_cmd);
12687  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_neighbor_routes_cmd);
12688  install_element (ENABLE_NODE, &show_ip_bgp_neighbor_received_prefix_filter_cmd);
12689  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_neighbor_received_prefix_filter_cmd);
12690  install_element (ENABLE_NODE, &show_ip_bgp_dampened_paths_cmd);
12691  install_element (ENABLE_NODE, &show_ip_bgp_flap_statistics_cmd);
12692  install_element (ENABLE_NODE, &show_ip_bgp_flap_address_cmd);
12693  install_element (ENABLE_NODE, &show_ip_bgp_flap_prefix_cmd);
12694  install_element (ENABLE_NODE, &show_ip_bgp_flap_cidr_only_cmd);
12695  install_element (ENABLE_NODE, &show_ip_bgp_flap_regexp_cmd);
12696  install_element (ENABLE_NODE, &show_ip_bgp_flap_filter_list_cmd);
12697  install_element (ENABLE_NODE, &show_ip_bgp_flap_prefix_list_cmd);
12698  install_element (ENABLE_NODE, &show_ip_bgp_flap_prefix_longer_cmd);
12699  install_element (ENABLE_NODE, &show_ip_bgp_flap_route_map_cmd);
12700  install_element (ENABLE_NODE, &show_ip_bgp_neighbor_flap_cmd);
12701  install_element (ENABLE_NODE, &show_ip_bgp_neighbor_damp_cmd);
12702  install_element (ENABLE_NODE, &show_ip_bgp_rsclient_cmd);
12703  install_element (ENABLE_NODE, &show_bgp_ipv4_safi_rsclient_cmd);
12704  install_element (ENABLE_NODE, &show_ip_bgp_rsclient_route_cmd);
12705  install_element (ENABLE_NODE, &show_bgp_ipv4_safi_rsclient_route_cmd);
12706  install_element (ENABLE_NODE, &show_ip_bgp_rsclient_prefix_cmd);
12707  install_element (ENABLE_NODE, &show_bgp_ipv4_safi_rsclient_prefix_cmd);
12708  install_element (ENABLE_NODE, &show_ip_bgp_view_neighbor_advertised_route_cmd);
12709  install_element (ENABLE_NODE, &show_ip_bgp_view_neighbor_received_routes_cmd);
12710  install_element (ENABLE_NODE, &show_ip_bgp_view_rsclient_cmd);
12711  install_element (ENABLE_NODE, &show_bgp_view_ipv4_safi_rsclient_cmd);
12712  install_element (ENABLE_NODE, &show_ip_bgp_view_rsclient_route_cmd);
12713  install_element (ENABLE_NODE, &show_bgp_view_ipv4_safi_rsclient_route_cmd);
12714  install_element (ENABLE_NODE, &show_ip_bgp_view_rsclient_prefix_cmd);
12715  install_element (ENABLE_NODE, &show_bgp_view_ipv4_safi_rsclient_prefix_cmd);
12716
12717 /* BGP dampening clear commands */
12718  install_element (ENABLE_NODE, &clear_ip_bgp_dampening_cmd);
12719  install_element (ENABLE_NODE, &clear_ip_bgp_dampening_prefix_cmd);
12720  install_element (ENABLE_NODE, &clear_ip_bgp_dampening_address_cmd);
12721  install_element (ENABLE_NODE, &clear_ip_bgp_dampening_address_mask_cmd);
12722
12723  /* prefix count */
12724  install_element (ENABLE_NODE, &show_ip_bgp_neighbor_prefix_counts_cmd);
12725  install_element (ENABLE_NODE, &show_ip_bgp_ipv4_neighbor_prefix_counts_cmd);
12726  install_element (ENABLE_NODE, &show_ip_bgp_vpnv4_neighbor_prefix_counts_cmd);
12727#ifdef HAVE_IPV6
12728  install_element (ENABLE_NODE, &show_bgp_ipv6_neighbor_prefix_counts_cmd);
12729
12730  /* New config IPv6 BGP commands.  */
12731  install_element (BGP_IPV6_NODE, &ipv6_bgp_network_cmd);
12732  install_element (BGP_IPV6_NODE, &ipv6_bgp_network_route_map_cmd);
12733  install_element (BGP_IPV6_NODE, &no_ipv6_bgp_network_cmd);
12734  install_element (BGP_IPV6_NODE, &no_ipv6_bgp_network_route_map_cmd);
12735
12736  install_element (BGP_IPV6_NODE, &ipv6_aggregate_address_cmd);
12737  install_element (BGP_IPV6_NODE, &ipv6_aggregate_address_summary_only_cmd);
12738  install_element (BGP_IPV6_NODE, &no_ipv6_aggregate_address_cmd);
12739  install_element (BGP_IPV6_NODE, &no_ipv6_aggregate_address_summary_only_cmd);
12740
12741  install_element (BGP_IPV6M_NODE, &ipv6_bgp_network_cmd);
12742  install_element (BGP_IPV6M_NODE, &no_ipv6_bgp_network_cmd);
12743
12744  /* Old config IPv6 BGP commands.  */
12745  install_element (BGP_NODE, &old_ipv6_bgp_network_cmd);
12746  install_element (BGP_NODE, &old_no_ipv6_bgp_network_cmd);
12747
12748  install_element (BGP_NODE, &old_ipv6_aggregate_address_cmd);
12749  install_element (BGP_NODE, &old_ipv6_aggregate_address_summary_only_cmd);
12750  install_element (BGP_NODE, &old_no_ipv6_aggregate_address_cmd);
12751  install_element (BGP_NODE, &old_no_ipv6_aggregate_address_summary_only_cmd);
12752
12753  install_element (VIEW_NODE, &show_bgp_cmd);
12754  install_element (VIEW_NODE, &show_bgp_ipv6_cmd);
12755  install_element (VIEW_NODE, &show_bgp_ipv6_safi_cmd);
12756  install_element (VIEW_NODE, &show_bgp_route_cmd);
12757  install_element (VIEW_NODE, &show_bgp_ipv6_route_cmd);
12758  install_element (VIEW_NODE, &show_bgp_ipv6_safi_route_cmd);
12759  install_element (VIEW_NODE, &show_bgp_prefix_cmd);
12760  install_element (VIEW_NODE, &show_bgp_ipv6_prefix_cmd);
12761  install_element (VIEW_NODE, &show_bgp_ipv6_safi_prefix_cmd);
12762  install_element (VIEW_NODE, &show_bgp_regexp_cmd);
12763  install_element (VIEW_NODE, &show_bgp_ipv6_regexp_cmd);
12764  install_element (VIEW_NODE, &show_bgp_prefix_list_cmd);
12765  install_element (VIEW_NODE, &show_bgp_ipv6_prefix_list_cmd);
12766  install_element (VIEW_NODE, &show_bgp_filter_list_cmd);
12767  install_element (VIEW_NODE, &show_bgp_ipv6_filter_list_cmd);
12768  install_element (VIEW_NODE, &show_bgp_route_map_cmd);
12769  install_element (VIEW_NODE, &show_bgp_ipv6_route_map_cmd);
12770  install_element (VIEW_NODE, &show_bgp_community_all_cmd);
12771  install_element (VIEW_NODE, &show_bgp_ipv6_community_all_cmd);
12772  install_element (VIEW_NODE, &show_bgp_community_cmd);
12773  install_element (VIEW_NODE, &show_bgp_ipv6_community_cmd);
12774  install_element (VIEW_NODE, &show_bgp_community2_cmd);
12775  install_element (VIEW_NODE, &show_bgp_ipv6_community2_cmd);
12776  install_element (VIEW_NODE, &show_bgp_community3_cmd);
12777  install_element (VIEW_NODE, &show_bgp_ipv6_community3_cmd);
12778  install_element (VIEW_NODE, &show_bgp_community4_cmd);
12779  install_element (VIEW_NODE, &show_bgp_ipv6_community4_cmd);
12780  install_element (VIEW_NODE, &show_bgp_community_exact_cmd);
12781  install_element (VIEW_NODE, &show_bgp_ipv6_community_exact_cmd);
12782  install_element (VIEW_NODE, &show_bgp_community2_exact_cmd);
12783  install_element (VIEW_NODE, &show_bgp_ipv6_community2_exact_cmd);
12784  install_element (VIEW_NODE, &show_bgp_community3_exact_cmd);
12785  install_element (VIEW_NODE, &show_bgp_ipv6_community3_exact_cmd);
12786  install_element (VIEW_NODE, &show_bgp_community4_exact_cmd);
12787  install_element (VIEW_NODE, &show_bgp_ipv6_community4_exact_cmd);
12788  install_element (VIEW_NODE, &show_bgp_community_list_cmd);
12789  install_element (VIEW_NODE, &show_bgp_ipv6_community_list_cmd);
12790  install_element (VIEW_NODE, &show_bgp_community_list_exact_cmd);
12791  install_element (VIEW_NODE, &show_bgp_ipv6_community_list_exact_cmd);
12792  install_element (VIEW_NODE, &show_bgp_prefix_longer_cmd);
12793  install_element (VIEW_NODE, &show_bgp_ipv6_prefix_longer_cmd);
12794  install_element (VIEW_NODE, &show_bgp_neighbor_advertised_route_cmd);
12795  install_element (VIEW_NODE, &show_bgp_ipv6_neighbor_advertised_route_cmd);
12796  install_element (VIEW_NODE, &show_bgp_neighbor_received_routes_cmd);
12797  install_element (VIEW_NODE, &show_bgp_ipv6_neighbor_received_routes_cmd);
12798  install_element (VIEW_NODE, &show_bgp_neighbor_routes_cmd);
12799  install_element (VIEW_NODE, &show_bgp_ipv6_neighbor_routes_cmd);
12800  install_element (VIEW_NODE, &show_bgp_neighbor_received_prefix_filter_cmd);
12801  install_element (VIEW_NODE, &show_bgp_ipv6_neighbor_received_prefix_filter_cmd);
12802  install_element (VIEW_NODE, &show_bgp_neighbor_flap_cmd);
12803  install_element (VIEW_NODE, &show_bgp_ipv6_neighbor_flap_cmd);
12804  install_element (VIEW_NODE, &show_bgp_neighbor_damp_cmd);
12805  install_element (VIEW_NODE, &show_bgp_ipv6_neighbor_damp_cmd);
12806  install_element (VIEW_NODE, &show_bgp_rsclient_cmd);
12807  install_element (VIEW_NODE, &show_bgp_ipv6_safi_rsclient_cmd);
12808  install_element (VIEW_NODE, &show_bgp_rsclient_route_cmd);
12809  install_element (VIEW_NODE, &show_bgp_ipv6_safi_rsclient_route_cmd);
12810  install_element (VIEW_NODE, &show_bgp_rsclient_prefix_cmd);
12811  install_element (VIEW_NODE, &show_bgp_ipv6_safi_rsclient_prefix_cmd);
12812  install_element (VIEW_NODE, &show_bgp_view_cmd);
12813  install_element (VIEW_NODE, &show_bgp_view_ipv6_cmd);
12814  install_element (VIEW_NODE, &show_bgp_view_route_cmd);
12815  install_element (VIEW_NODE, &show_bgp_view_ipv6_route_cmd);
12816  install_element (VIEW_NODE, &show_bgp_view_prefix_cmd);
12817  install_element (VIEW_NODE, &show_bgp_view_ipv6_prefix_cmd);
12818  install_element (VIEW_NODE, &show_bgp_view_neighbor_advertised_route_cmd);
12819  install_element (VIEW_NODE, &show_bgp_view_ipv6_neighbor_advertised_route_cmd);
12820  install_element (VIEW_NODE, &show_bgp_view_neighbor_received_routes_cmd);
12821  install_element (VIEW_NODE, &show_bgp_view_ipv6_neighbor_received_routes_cmd);
12822  install_element (VIEW_NODE, &show_bgp_view_neighbor_routes_cmd);
12823  install_element (VIEW_NODE, &show_bgp_view_ipv6_neighbor_routes_cmd);
12824  install_element (VIEW_NODE, &show_bgp_view_neighbor_received_prefix_filter_cmd);
12825  install_element (VIEW_NODE, &show_bgp_view_ipv6_neighbor_received_prefix_filter_cmd);
12826  install_element (VIEW_NODE, &show_bgp_view_neighbor_flap_cmd);
12827  install_element (VIEW_NODE, &show_bgp_view_ipv6_neighbor_flap_cmd);
12828  install_element (VIEW_NODE, &show_bgp_view_neighbor_damp_cmd);
12829  install_element (VIEW_NODE, &show_bgp_view_ipv6_neighbor_damp_cmd);
12830  install_element (VIEW_NODE, &show_bgp_view_rsclient_cmd);
12831  install_element (VIEW_NODE, &show_bgp_view_ipv6_safi_rsclient_cmd);
12832  install_element (VIEW_NODE, &show_bgp_view_rsclient_route_cmd);
12833  install_element (VIEW_NODE, &show_bgp_view_ipv6_safi_rsclient_route_cmd);
12834  install_element (VIEW_NODE, &show_bgp_view_rsclient_prefix_cmd);
12835  install_element (VIEW_NODE, &show_bgp_view_ipv6_safi_rsclient_prefix_cmd);
12836
12837  /* Restricted:
12838   * VIEW_NODE - (set of dangerous commands) - (commands dependent on prev)
12839   */
12840  install_element (RESTRICTED_NODE, &show_bgp_route_cmd);
12841  install_element (RESTRICTED_NODE, &show_bgp_ipv6_route_cmd);
12842  install_element (RESTRICTED_NODE, &show_bgp_ipv6_safi_route_cmd);
12843  install_element (RESTRICTED_NODE, &show_bgp_prefix_cmd);
12844  install_element (RESTRICTED_NODE, &show_bgp_ipv6_prefix_cmd);
12845  install_element (RESTRICTED_NODE, &show_bgp_ipv6_safi_prefix_cmd);
12846  install_element (RESTRICTED_NODE, &show_bgp_community_cmd);
12847  install_element (RESTRICTED_NODE, &show_bgp_ipv6_community_cmd);
12848  install_element (RESTRICTED_NODE, &show_bgp_community2_cmd);
12849  install_element (RESTRICTED_NODE, &show_bgp_ipv6_community2_cmd);
12850  install_element (RESTRICTED_NODE, &show_bgp_community3_cmd);
12851  install_element (RESTRICTED_NODE, &show_bgp_ipv6_community3_cmd);
12852  install_element (RESTRICTED_NODE, &show_bgp_community4_cmd);
12853  install_element (RESTRICTED_NODE, &show_bgp_ipv6_community4_cmd);
12854  install_element (RESTRICTED_NODE, &show_bgp_community_exact_cmd);
12855  install_element (RESTRICTED_NODE, &show_bgp_ipv6_community_exact_cmd);
12856  install_element (RESTRICTED_NODE, &show_bgp_community2_exact_cmd);
12857  install_element (RESTRICTED_NODE, &show_bgp_ipv6_community2_exact_cmd);
12858  install_element (RESTRICTED_NODE, &show_bgp_community3_exact_cmd);
12859  install_element (RESTRICTED_NODE, &show_bgp_ipv6_community3_exact_cmd);
12860  install_element (RESTRICTED_NODE, &show_bgp_community4_exact_cmd);
12861  install_element (RESTRICTED_NODE, &show_bgp_ipv6_community4_exact_cmd);
12862  install_element (RESTRICTED_NODE, &show_bgp_rsclient_route_cmd);
12863  install_element (RESTRICTED_NODE, &show_bgp_ipv6_safi_rsclient_route_cmd);
12864  install_element (RESTRICTED_NODE, &show_bgp_rsclient_prefix_cmd);
12865  install_element (RESTRICTED_NODE, &show_bgp_ipv6_safi_rsclient_prefix_cmd);
12866  install_element (RESTRICTED_NODE, &show_bgp_view_route_cmd);
12867  install_element (RESTRICTED_NODE, &show_bgp_view_ipv6_route_cmd);
12868  install_element (RESTRICTED_NODE, &show_bgp_view_prefix_cmd);
12869  install_element (RESTRICTED_NODE, &show_bgp_view_ipv6_prefix_cmd);
12870  install_element (RESTRICTED_NODE, &show_bgp_view_neighbor_received_prefix_filter_cmd);
12871  install_element (RESTRICTED_NODE, &show_bgp_view_ipv6_neighbor_received_prefix_filter_cmd);
12872  install_element (RESTRICTED_NODE, &show_bgp_view_rsclient_route_cmd);
12873  install_element (RESTRICTED_NODE, &show_bgp_view_ipv6_safi_rsclient_route_cmd);
12874  install_element (RESTRICTED_NODE, &show_bgp_view_rsclient_prefix_cmd);
12875  install_element (RESTRICTED_NODE, &show_bgp_view_ipv6_safi_rsclient_prefix_cmd);
12876
12877  install_element (ENABLE_NODE, &show_bgp_cmd);
12878  install_element (ENABLE_NODE, &show_bgp_ipv6_cmd);
12879  install_element (ENABLE_NODE, &show_bgp_ipv6_safi_cmd);
12880  install_element (ENABLE_NODE, &show_bgp_route_cmd);
12881  install_element (ENABLE_NODE, &show_bgp_ipv6_route_cmd);
12882  install_element (ENABLE_NODE, &show_bgp_ipv6_safi_route_cmd);
12883  install_element (ENABLE_NODE, &show_bgp_prefix_cmd);
12884  install_element (ENABLE_NODE, &show_bgp_ipv6_prefix_cmd);
12885  install_element (ENABLE_NODE, &show_bgp_ipv6_safi_prefix_cmd);
12886  install_element (ENABLE_NODE, &show_bgp_regexp_cmd);
12887  install_element (ENABLE_NODE, &show_bgp_ipv6_regexp_cmd);
12888  install_element (ENABLE_NODE, &show_bgp_prefix_list_cmd);
12889  install_element (ENABLE_NODE, &show_bgp_ipv6_prefix_list_cmd);
12890  install_element (ENABLE_NODE, &show_bgp_filter_list_cmd);
12891  install_element (ENABLE_NODE, &show_bgp_ipv6_filter_list_cmd);
12892  install_element (ENABLE_NODE, &show_bgp_route_map_cmd);
12893  install_element (ENABLE_NODE, &show_bgp_ipv6_route_map_cmd);
12894  install_element (ENABLE_NODE, &show_bgp_community_all_cmd);
12895  install_element (ENABLE_NODE, &show_bgp_ipv6_community_all_cmd);
12896  install_element (ENABLE_NODE, &show_bgp_community_cmd);
12897  install_element (ENABLE_NODE, &show_bgp_ipv6_community_cmd);
12898  install_element (ENABLE_NODE, &show_bgp_community2_cmd);
12899  install_element (ENABLE_NODE, &show_bgp_ipv6_community2_cmd);
12900  install_element (ENABLE_NODE, &show_bgp_community3_cmd);
12901  install_element (ENABLE_NODE, &show_bgp_ipv6_community3_cmd);
12902  install_element (ENABLE_NODE, &show_bgp_community4_cmd);
12903  install_element (ENABLE_NODE, &show_bgp_ipv6_community4_cmd);
12904  install_element (ENABLE_NODE, &show_bgp_community_exact_cmd);
12905  install_element (ENABLE_NODE, &show_bgp_ipv6_community_exact_cmd);
12906  install_element (ENABLE_NODE, &show_bgp_community2_exact_cmd);
12907  install_element (ENABLE_NODE, &show_bgp_ipv6_community2_exact_cmd);
12908  install_element (ENABLE_NODE, &show_bgp_community3_exact_cmd);
12909  install_element (ENABLE_NODE, &show_bgp_ipv6_community3_exact_cmd);
12910  install_element (ENABLE_NODE, &show_bgp_community4_exact_cmd);
12911  install_element (ENABLE_NODE, &show_bgp_ipv6_community4_exact_cmd);
12912  install_element (ENABLE_NODE, &show_bgp_community_list_cmd);
12913  install_element (ENABLE_NODE, &show_bgp_ipv6_community_list_cmd);
12914  install_element (ENABLE_NODE, &show_bgp_community_list_exact_cmd);
12915  install_element (ENABLE_NODE, &show_bgp_ipv6_community_list_exact_cmd);
12916  install_element (ENABLE_NODE, &show_bgp_prefix_longer_cmd);
12917  install_element (ENABLE_NODE, &show_bgp_ipv6_prefix_longer_cmd);
12918  install_element (ENABLE_NODE, &show_bgp_neighbor_advertised_route_cmd);
12919  install_element (ENABLE_NODE, &show_bgp_ipv6_neighbor_advertised_route_cmd);
12920  install_element (ENABLE_NODE, &show_bgp_neighbor_received_routes_cmd);
12921  install_element (ENABLE_NODE, &show_bgp_ipv6_neighbor_received_routes_cmd);
12922  install_element (ENABLE_NODE, &show_bgp_neighbor_routes_cmd);
12923  install_element (ENABLE_NODE, &show_bgp_ipv6_neighbor_routes_cmd);
12924  install_element (ENABLE_NODE, &show_bgp_neighbor_received_prefix_filter_cmd);
12925  install_element (ENABLE_NODE, &show_bgp_ipv6_neighbor_received_prefix_filter_cmd);
12926  install_element (ENABLE_NODE, &show_bgp_neighbor_flap_cmd);
12927  install_element (ENABLE_NODE, &show_bgp_ipv6_neighbor_flap_cmd);
12928  install_element (ENABLE_NODE, &show_bgp_neighbor_damp_cmd);
12929  install_element (ENABLE_NODE, &show_bgp_ipv6_neighbor_damp_cmd);
12930  install_element (ENABLE_NODE, &show_bgp_rsclient_cmd);
12931  install_element (ENABLE_NODE, &show_bgp_ipv6_safi_rsclient_cmd);
12932  install_element (ENABLE_NODE, &show_bgp_rsclient_route_cmd);
12933  install_element (ENABLE_NODE, &show_bgp_ipv6_safi_rsclient_route_cmd);
12934  install_element (ENABLE_NODE, &show_bgp_rsclient_prefix_cmd);
12935  install_element (ENABLE_NODE, &show_bgp_ipv6_safi_rsclient_prefix_cmd);
12936  install_element (ENABLE_NODE, &show_bgp_view_cmd);
12937  install_element (ENABLE_NODE, &show_bgp_view_ipv6_cmd);
12938  install_element (ENABLE_NODE, &show_bgp_view_route_cmd);
12939  install_element (ENABLE_NODE, &show_bgp_view_ipv6_route_cmd);
12940  install_element (ENABLE_NODE, &show_bgp_view_prefix_cmd);
12941  install_element (ENABLE_NODE, &show_bgp_view_ipv6_prefix_cmd);
12942  install_element (ENABLE_NODE, &show_bgp_view_neighbor_advertised_route_cmd);
12943  install_element (ENABLE_NODE, &show_bgp_view_ipv6_neighbor_advertised_route_cmd);
12944  install_element (ENABLE_NODE, &show_bgp_view_neighbor_received_routes_cmd);
12945  install_element (ENABLE_NODE, &show_bgp_view_ipv6_neighbor_received_routes_cmd);
12946  install_element (ENABLE_NODE, &show_bgp_view_neighbor_routes_cmd);
12947  install_element (ENABLE_NODE, &show_bgp_view_ipv6_neighbor_routes_cmd);
12948  install_element (ENABLE_NODE, &show_bgp_view_neighbor_received_prefix_filter_cmd);
12949  install_element (ENABLE_NODE, &show_bgp_view_ipv6_neighbor_received_prefix_filter_cmd);
12950  install_element (ENABLE_NODE, &show_bgp_view_neighbor_flap_cmd);
12951  install_element (ENABLE_NODE, &show_bgp_view_ipv6_neighbor_flap_cmd);
12952  install_element (ENABLE_NODE, &show_bgp_view_neighbor_damp_cmd);
12953  install_element (ENABLE_NODE, &show_bgp_view_ipv6_neighbor_damp_cmd);
12954  install_element (ENABLE_NODE, &show_bgp_view_rsclient_cmd);
12955  install_element (ENABLE_NODE, &show_bgp_view_ipv6_safi_rsclient_cmd);
12956  install_element (ENABLE_NODE, &show_bgp_view_rsclient_route_cmd);
12957  install_element (ENABLE_NODE, &show_bgp_view_ipv6_safi_rsclient_route_cmd);
12958  install_element (ENABLE_NODE, &show_bgp_view_rsclient_prefix_cmd);
12959  install_element (ENABLE_NODE, &show_bgp_view_ipv6_safi_rsclient_prefix_cmd);
12960
12961  /* Statistics */
12962  install_element (ENABLE_NODE, &show_bgp_statistics_cmd);
12963  install_element (ENABLE_NODE, &show_bgp_statistics_vpnv4_cmd);
12964  install_element (ENABLE_NODE, &show_bgp_statistics_view_cmd);
12965  install_element (ENABLE_NODE, &show_bgp_statistics_view_vpnv4_cmd);
12966
12967  /* old command */
12968  install_element (VIEW_NODE, &show_ipv6_bgp_cmd);
12969  install_element (VIEW_NODE, &show_ipv6_bgp_route_cmd);
12970  install_element (VIEW_NODE, &show_ipv6_bgp_prefix_cmd);
12971  install_element (VIEW_NODE, &show_ipv6_bgp_regexp_cmd);
12972  install_element (VIEW_NODE, &show_ipv6_bgp_prefix_list_cmd);
12973  install_element (VIEW_NODE, &show_ipv6_bgp_filter_list_cmd);
12974  install_element (VIEW_NODE, &show_ipv6_bgp_community_all_cmd);
12975  install_element (VIEW_NODE, &show_ipv6_bgp_community_cmd);
12976  install_element (VIEW_NODE, &show_ipv6_bgp_community2_cmd);
12977  install_element (VIEW_NODE, &show_ipv6_bgp_community3_cmd);
12978  install_element (VIEW_NODE, &show_ipv6_bgp_community4_cmd);
12979  install_element (VIEW_NODE, &show_ipv6_bgp_community_exact_cmd);
12980  install_element (VIEW_NODE, &show_ipv6_bgp_community2_exact_cmd);
12981  install_element (VIEW_NODE, &show_ipv6_bgp_community3_exact_cmd);
12982  install_element (VIEW_NODE, &show_ipv6_bgp_community4_exact_cmd);
12983  install_element (VIEW_NODE, &show_ipv6_bgp_community_list_cmd);
12984  install_element (VIEW_NODE, &show_ipv6_bgp_community_list_exact_cmd);
12985  install_element (VIEW_NODE, &show_ipv6_bgp_prefix_longer_cmd);
12986  install_element (VIEW_NODE, &show_ipv6_mbgp_cmd);
12987  install_element (VIEW_NODE, &show_ipv6_mbgp_route_cmd);
12988  install_element (VIEW_NODE, &show_ipv6_mbgp_prefix_cmd);
12989  install_element (VIEW_NODE, &show_ipv6_mbgp_regexp_cmd);
12990  install_element (VIEW_NODE, &show_ipv6_mbgp_prefix_list_cmd);
12991  install_element (VIEW_NODE, &show_ipv6_mbgp_filter_list_cmd);
12992  install_element (VIEW_NODE, &show_ipv6_mbgp_community_all_cmd);
12993  install_element (VIEW_NODE, &show_ipv6_mbgp_community_cmd);
12994  install_element (VIEW_NODE, &show_ipv6_mbgp_community2_cmd);
12995  install_element (VIEW_NODE, &show_ipv6_mbgp_community3_cmd);
12996  install_element (VIEW_NODE, &show_ipv6_mbgp_community4_cmd);
12997  install_element (VIEW_NODE, &show_ipv6_mbgp_community_exact_cmd);
12998  install_element (VIEW_NODE, &show_ipv6_mbgp_community2_exact_cmd);
12999  install_element (VIEW_NODE, &show_ipv6_mbgp_community3_exact_cmd);
13000  install_element (VIEW_NODE, &show_ipv6_mbgp_community4_exact_cmd);
13001  install_element (VIEW_NODE, &show_ipv6_mbgp_community_list_cmd);
13002  install_element (VIEW_NODE, &show_ipv6_mbgp_community_list_exact_cmd);
13003  install_element (VIEW_NODE, &show_ipv6_mbgp_prefix_longer_cmd);
13004
13005  /* old command */
13006  install_element (ENABLE_NODE, &show_ipv6_bgp_cmd);
13007  install_element (ENABLE_NODE, &show_ipv6_bgp_route_cmd);
13008  install_element (ENABLE_NODE, &show_ipv6_bgp_prefix_cmd);
13009  install_element (ENABLE_NODE, &show_ipv6_bgp_regexp_cmd);
13010  install_element (ENABLE_NODE, &show_ipv6_bgp_prefix_list_cmd);
13011  install_element (ENABLE_NODE, &show_ipv6_bgp_filter_list_cmd);
13012  install_element (ENABLE_NODE, &show_ipv6_bgp_community_all_cmd);
13013  install_element (ENABLE_NODE, &show_ipv6_bgp_community_cmd);
13014  install_element (ENABLE_NODE, &show_ipv6_bgp_community2_cmd);
13015  install_element (ENABLE_NODE, &show_ipv6_bgp_community3_cmd);
13016  install_element (ENABLE_NODE, &show_ipv6_bgp_community4_cmd);
13017  install_element (ENABLE_NODE, &show_ipv6_bgp_community_exact_cmd);
13018  install_element (ENABLE_NODE, &show_ipv6_bgp_community2_exact_cmd);
13019  install_element (ENABLE_NODE, &show_ipv6_bgp_community3_exact_cmd);
13020  install_element (ENABLE_NODE, &show_ipv6_bgp_community4_exact_cmd);
13021  install_element (ENABLE_NODE, &show_ipv6_bgp_community_list_cmd);
13022  install_element (ENABLE_NODE, &show_ipv6_bgp_community_list_exact_cmd);
13023  install_element (ENABLE_NODE, &show_ipv6_bgp_prefix_longer_cmd);
13024  install_element (ENABLE_NODE, &show_ipv6_mbgp_cmd);
13025  install_element (ENABLE_NODE, &show_ipv6_mbgp_route_cmd);
13026  install_element (ENABLE_NODE, &show_ipv6_mbgp_prefix_cmd);
13027  install_element (ENABLE_NODE, &show_ipv6_mbgp_regexp_cmd);
13028  install_element (ENABLE_NODE, &show_ipv6_mbgp_prefix_list_cmd);
13029  install_element (ENABLE_NODE, &show_ipv6_mbgp_filter_list_cmd);
13030  install_element (ENABLE_NODE, &show_ipv6_mbgp_community_all_cmd);
13031  install_element (ENABLE_NODE, &show_ipv6_mbgp_community_cmd);
13032  install_element (ENABLE_NODE, &show_ipv6_mbgp_community2_cmd);
13033  install_element (ENABLE_NODE, &show_ipv6_mbgp_community3_cmd);
13034  install_element (ENABLE_NODE, &show_ipv6_mbgp_community4_cmd);
13035  install_element (ENABLE_NODE, &show_ipv6_mbgp_community_exact_cmd);
13036  install_element (ENABLE_NODE, &show_ipv6_mbgp_community2_exact_cmd);
13037  install_element (ENABLE_NODE, &show_ipv6_mbgp_community3_exact_cmd);
13038  install_element (ENABLE_NODE, &show_ipv6_mbgp_community4_exact_cmd);
13039  install_element (ENABLE_NODE, &show_ipv6_mbgp_community_list_cmd);
13040  install_element (ENABLE_NODE, &show_ipv6_mbgp_community_list_exact_cmd);
13041  install_element (ENABLE_NODE, &show_ipv6_mbgp_prefix_longer_cmd);
13042
13043  /* old command */
13044  install_element (VIEW_NODE, &ipv6_bgp_neighbor_advertised_route_cmd);
13045  install_element (ENABLE_NODE, &ipv6_bgp_neighbor_advertised_route_cmd);
13046  install_element (VIEW_NODE, &ipv6_mbgp_neighbor_advertised_route_cmd);
13047  install_element (ENABLE_NODE, &ipv6_mbgp_neighbor_advertised_route_cmd);
13048
13049  /* old command */
13050  install_element (VIEW_NODE, &ipv6_bgp_neighbor_received_routes_cmd);
13051  install_element (ENABLE_NODE, &ipv6_bgp_neighbor_received_routes_cmd);
13052  install_element (VIEW_NODE, &ipv6_mbgp_neighbor_received_routes_cmd);
13053  install_element (ENABLE_NODE, &ipv6_mbgp_neighbor_received_routes_cmd);
13054
13055  /* old command */
13056  install_element (VIEW_NODE, &ipv6_bgp_neighbor_routes_cmd);
13057  install_element (ENABLE_NODE, &ipv6_bgp_neighbor_routes_cmd);
13058  install_element (VIEW_NODE, &ipv6_mbgp_neighbor_routes_cmd);
13059  install_element (ENABLE_NODE, &ipv6_mbgp_neighbor_routes_cmd);
13060#endif /* HAVE_IPV6 */
13061
13062  install_element (BGP_NODE, &bgp_distance_cmd);
13063  install_element (BGP_NODE, &no_bgp_distance_cmd);
13064  install_element (BGP_NODE, &no_bgp_distance2_cmd);
13065  install_element (BGP_NODE, &bgp_distance_source_cmd);
13066  install_element (BGP_NODE, &no_bgp_distance_source_cmd);
13067  install_element (BGP_NODE, &bgp_distance_source_access_list_cmd);
13068  install_element (BGP_NODE, &no_bgp_distance_source_access_list_cmd);
13069
13070  install_element (BGP_NODE, &bgp_damp_set_cmd);
13071  install_element (BGP_NODE, &bgp_damp_set2_cmd);
13072  install_element (BGP_NODE, &bgp_damp_set3_cmd);
13073  install_element (BGP_NODE, &bgp_damp_unset_cmd);
13074  install_element (BGP_NODE, &bgp_damp_unset2_cmd);
13075  install_element (BGP_IPV4_NODE, &bgp_damp_set_cmd);
13076  install_element (BGP_IPV4_NODE, &bgp_damp_set2_cmd);
13077  install_element (BGP_IPV4_NODE, &bgp_damp_set3_cmd);
13078  install_element (BGP_IPV4_NODE, &bgp_damp_unset_cmd);
13079  install_element (BGP_IPV4_NODE, &bgp_damp_unset2_cmd);
13080
13081  /* Deprecated AS-Pathlimit commands */
13082  install_element (BGP_NODE, &bgp_network_ttl_cmd);
13083  install_element (BGP_NODE, &bgp_network_mask_ttl_cmd);
13084  install_element (BGP_NODE, &bgp_network_mask_natural_ttl_cmd);
13085  install_element (BGP_NODE, &bgp_network_backdoor_ttl_cmd);
13086  install_element (BGP_NODE, &bgp_network_mask_backdoor_ttl_cmd);
13087  install_element (BGP_NODE, &bgp_network_mask_natural_backdoor_ttl_cmd);
13088
13089  install_element (BGP_NODE, &no_bgp_network_ttl_cmd);
13090  install_element (BGP_NODE, &no_bgp_network_mask_ttl_cmd);
13091  install_element (BGP_NODE, &no_bgp_network_mask_natural_ttl_cmd);
13092  install_element (BGP_NODE, &no_bgp_network_backdoor_ttl_cmd);
13093  install_element (BGP_NODE, &no_bgp_network_mask_backdoor_ttl_cmd);
13094  install_element (BGP_NODE, &no_bgp_network_mask_natural_backdoor_ttl_cmd);
13095
13096  install_element (BGP_IPV4_NODE, &bgp_network_ttl_cmd);
13097  install_element (BGP_IPV4_NODE, &bgp_network_mask_ttl_cmd);
13098  install_element (BGP_IPV4_NODE, &bgp_network_mask_natural_ttl_cmd);
13099  install_element (BGP_IPV4_NODE, &bgp_network_backdoor_ttl_cmd);
13100  install_element (BGP_IPV4_NODE, &bgp_network_mask_backdoor_ttl_cmd);
13101  install_element (BGP_IPV4_NODE, &bgp_network_mask_natural_backdoor_ttl_cmd);
13102
13103  install_element (BGP_IPV4_NODE, &no_bgp_network_ttl_cmd);
13104  install_element (BGP_IPV4_NODE, &no_bgp_network_mask_ttl_cmd);
13105  install_element (BGP_IPV4_NODE, &no_bgp_network_mask_natural_ttl_cmd);
13106  install_element (BGP_IPV4_NODE, &no_bgp_network_backdoor_ttl_cmd);
13107  install_element (BGP_IPV4_NODE, &no_bgp_network_mask_backdoor_ttl_cmd);
13108  install_element (BGP_IPV4_NODE, &no_bgp_network_mask_natural_backdoor_ttl_cmd);
13109
13110  install_element (BGP_IPV4M_NODE, &bgp_network_ttl_cmd);
13111  install_element (BGP_IPV4M_NODE, &bgp_network_mask_ttl_cmd);
13112  install_element (BGP_IPV4M_NODE, &bgp_network_mask_natural_ttl_cmd);
13113  install_element (BGP_IPV4M_NODE, &bgp_network_backdoor_ttl_cmd);
13114  install_element (BGP_IPV4M_NODE, &bgp_network_mask_backdoor_ttl_cmd);
13115  install_element (BGP_IPV4M_NODE, &bgp_network_mask_natural_backdoor_ttl_cmd);
13116
13117  install_element (BGP_IPV4M_NODE, &no_bgp_network_ttl_cmd);
13118  install_element (BGP_IPV4M_NODE, &no_bgp_network_mask_ttl_cmd);
13119  install_element (BGP_IPV4M_NODE, &no_bgp_network_mask_natural_ttl_cmd);
13120  install_element (BGP_IPV4M_NODE, &no_bgp_network_backdoor_ttl_cmd);
13121  install_element (BGP_IPV4M_NODE, &no_bgp_network_mask_backdoor_ttl_cmd);
13122  install_element (BGP_IPV4M_NODE, &no_bgp_network_mask_natural_backdoor_ttl_cmd);
13123
13124#ifdef HAVE_IPV6
13125  install_element (BGP_IPV6_NODE, &ipv6_bgp_network_ttl_cmd);
13126  install_element (BGP_IPV6_NODE, &no_ipv6_bgp_network_ttl_cmd);
13127#endif
13128}
13129
13130void
13131bgp_route_finish (void)
13132{
13133  bgp_table_unlock (bgp_distance_table);
13134  bgp_distance_table = NULL;
13135}
13136