2 Copyright (C) 2000 Kunihiro Ishiguro
4 This file is part of GNU Zebra.
6 GNU Zebra is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 GNU Zebra is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Zebra; see the file COPYING. If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
37 #include "bgpd/bgpd.h"
38 #include "bgpd/bgp_table.h"
39 #include "bgpd/bgp_route.h"
40 #include "bgpd/bgp_attr.h"
41 #include "bgpd/bgp_nexthop.h"
42 #include "bgpd/bgp_nht.h"
43 #include "bgpd/bgp_debug.h"
44 #include "bgpd/bgp_damp.h"
45 #include "bgpd/bgp_fsm.h"
46 #include "bgpd/bgp_vty.h"
47 #include "zebra/rib.h"
48 #include "zebra/zserv.h" /* For ZEBRA_SERV_PATH. */
51 bnc_str (struct bgp_nexthop_cache
*bnc
, char *buf
, int size
)
53 prefix2str(&(bnc
->node
->p
), buf
, size
);
58 bnc_nexthop_free (struct bgp_nexthop_cache
*bnc
)
60 nexthops_free(bnc
->nexthop
);
63 struct bgp_nexthop_cache
*
66 struct bgp_nexthop_cache
*bnc
;
68 bnc
= XCALLOC (MTYPE_BGP_NEXTHOP_CACHE
, sizeof (struct bgp_nexthop_cache
));
69 LIST_INIT(&(bnc
->paths
));
74 bnc_free (struct bgp_nexthop_cache
*bnc
)
76 bnc_nexthop_free (bnc
);
77 XFREE (MTYPE_BGP_NEXTHOP_CACHE
, bnc
);
80 /* Reset and free all BGP nexthop cache. */
82 bgp_nexthop_cache_reset (struct bgp_table
*table
)
85 struct bgp_nexthop_cache
*bnc
;
87 for (rn
= bgp_table_top (table
); rn
; rn
= bgp_route_next (rn
))
88 if ((bnc
= rn
->info
) != NULL
)
96 /* BGP own address structure */
104 bgp_address_hash_alloc (void *p
)
106 const struct in_addr
*val
= (const struct in_addr
*)p
;
107 struct bgp_addr
*addr
;
109 addr
= XMALLOC (MTYPE_BGP_ADDR
, sizeof (struct bgp_addr
));
111 addr
->addr
.s_addr
= val
->s_addr
;
117 bgp_address_hash_free (void *addr
)
119 XFREE (MTYPE_BGP_ADDR
, addr
);
123 bgp_address_hash_key_make (void *p
)
125 const struct bgp_addr
*addr
= p
;
127 return jhash_1word(addr
->addr
.s_addr
, 0);
131 bgp_address_hash_cmp (const void *p1
, const void *p2
)
133 const struct bgp_addr
*addr1
= p1
;
134 const struct bgp_addr
*addr2
= p2
;
136 return addr1
->addr
.s_addr
== addr2
->addr
.s_addr
;
140 bgp_address_init (struct bgp
*bgp
)
142 bgp
->address_hash
= hash_create (bgp_address_hash_key_make
,
143 bgp_address_hash_cmp
);
147 bgp_address_destroy (struct bgp
*bgp
)
149 if (bgp
->address_hash
== NULL
)
151 hash_clean(bgp
->address_hash
, bgp_address_hash_free
);
152 hash_free(bgp
->address_hash
);
153 bgp
->address_hash
= NULL
;
157 bgp_address_add (struct bgp
*bgp
, struct prefix
*p
)
160 struct bgp_addr
*addr
;
162 tmp
.addr
= p
->u
.prefix4
;
164 addr
= hash_get (bgp
->address_hash
, &tmp
, bgp_address_hash_alloc
);
172 bgp_address_del (struct bgp
*bgp
, struct prefix
*p
)
175 struct bgp_addr
*addr
;
177 tmp
.addr
= p
->u
.prefix4
;
179 addr
= hash_lookup (bgp
->address_hash
, &tmp
);
180 /* may have been deleted earlier by bgp_interface_down() */
186 if (addr
->refcnt
== 0)
188 hash_release (bgp
->address_hash
, addr
);
189 XFREE (MTYPE_BGP_ADDR
, addr
);
194 struct bgp_connected_ref
200 bgp_connected_add (struct bgp
*bgp
, struct connected
*ifc
)
205 struct bgp_connected_ref
*bc
;
206 struct listnode
*node
, *nnode
;
211 p
= *(CONNECTED_PREFIX(ifc
));
212 if (addr
->family
== AF_INET
)
214 apply_mask_ipv4 ((struct prefix_ipv4
*) &p
);
216 if (prefix_ipv4_any ((struct prefix_ipv4
*) &p
))
219 bgp_address_add (bgp
, addr
);
221 rn
= bgp_node_get (bgp
->connected_table
[AFI_IP
], (struct prefix
*) &p
);
229 bc
= XCALLOC (MTYPE_BGP_CONN
, sizeof (struct bgp_connected_ref
));
234 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
236 if (peer
->conf_if
&& (strcmp (peer
->conf_if
, ifc
->ifp
->name
) == 0) &&
237 peer
->status
!= Established
&&
238 !CHECK_FLAG(peer
->flags
, PEER_FLAG_IFPEER_V6ONLY
))
240 if (peer_active(peer
))
241 BGP_EVENT_ADD (peer
, BGP_Stop
);
242 BGP_EVENT_ADD (peer
, BGP_Start
);
247 else if (addr
->family
== AF_INET6
)
249 apply_mask_ipv6 ((struct prefix_ipv6
*) &p
);
251 if (IN6_IS_ADDR_UNSPECIFIED (&p
.u
.prefix6
))
254 if (IN6_IS_ADDR_LINKLOCAL (&p
.u
.prefix6
))
257 rn
= bgp_node_get (bgp
->connected_table
[AFI_IP6
], (struct prefix
*) &p
);
265 bc
= XCALLOC (MTYPE_BGP_CONN
, sizeof (struct bgp_connected_ref
));
270 #endif /* HAVE_IPV6 */
274 bgp_connected_delete (struct bgp
*bgp
, struct connected
*ifc
)
279 struct bgp_connected_ref
*bc
;
283 p
= *(CONNECTED_PREFIX(ifc
));
284 if (addr
->family
== AF_INET
)
286 apply_mask_ipv4 ((struct prefix_ipv4
*) &p
);
288 if (prefix_ipv4_any ((struct prefix_ipv4
*) &p
))
291 bgp_address_del (bgp
, addr
);
293 rn
= bgp_node_lookup (bgp
->connected_table
[AFI_IP
], &p
);
301 XFREE (MTYPE_BGP_CONN
, bc
);
304 bgp_unlock_node (rn
);
305 bgp_unlock_node (rn
);
308 else if (addr
->family
== AF_INET6
)
310 apply_mask_ipv6 ((struct prefix_ipv6
*) &p
);
312 if (IN6_IS_ADDR_UNSPECIFIED (&p
.u
.prefix6
))
315 if (IN6_IS_ADDR_LINKLOCAL (&p
.u
.prefix6
))
318 rn
= bgp_node_lookup (bgp
->connected_table
[AFI_IP6
], (struct prefix
*) &p
);
326 XFREE (MTYPE_BGP_CONN
, bc
);
329 bgp_unlock_node (rn
);
330 bgp_unlock_node (rn
);
332 #endif /* HAVE_IPV6 */
336 bgp_nexthop_self (struct bgp
*bgp
, struct attr
*attr
)
338 struct bgp_addr tmp
, *addr
;
340 tmp
.addr
= attr
->nexthop
;
342 addr
= hash_lookup (bgp
->address_hash
, &tmp
);
350 bgp_multiaccess_check_v4 (struct in_addr nexthop
, struct peer
*peer
)
352 struct bgp_node
*rn1
;
353 struct bgp_node
*rn2
;
358 p
.prefixlen
= IPV4_MAX_BITLEN
;
359 p
.u
.prefix4
= nexthop
;
361 rn1
= bgp_node_match (peer
->bgp
->connected_table
[AFI_IP
], &p
);
366 p
.prefixlen
= IPV4_MAX_BITLEN
;
367 p
.u
.prefix4
= peer
->su
.sin
.sin_addr
;
369 rn2
= bgp_node_match (peer
->bgp
->connected_table
[AFI_IP
], &p
);
372 bgp_unlock_node(rn1
);
376 ret
= (rn1
== rn2
) ? 1 : 0;
378 bgp_unlock_node(rn1
);
379 bgp_unlock_node(rn2
);
385 bgp_show_nexthops (struct vty
*vty
, struct bgp
*bgp
, int detail
)
388 struct bgp_nexthop_cache
*bnc
;
389 char buf
[PREFIX2STR_BUFFER
];
390 struct nexthop
*nexthop
;
394 vty_out (vty
, "Current BGP nexthop cache:%s", VTY_NEWLINE
);
395 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
397 if (!bgp
->nexthop_cache_table
[afi
])
400 for (rn
= bgp_table_top (bgp
->nexthop_cache_table
[afi
]); rn
; rn
= bgp_route_next (rn
))
402 if ((bnc
= rn
->info
) != NULL
)
404 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
))
406 vty_out (vty
, " %s valid [IGP metric %d], #paths %d%s",
407 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, sizeof (buf
)),
408 bnc
->metric
, bnc
->path_count
, VTY_NEWLINE
);
410 for (nexthop
= bnc
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
411 switch (nexthop
->type
)
413 case NEXTHOP_TYPE_IPV6
:
414 vty_out (vty
, " gate %s%s",
415 inet_ntop (AF_INET6
, &nexthop
->gate
.ipv6
,
416 buf
, sizeof (buf
)), VTY_NEWLINE
);
418 case NEXTHOP_TYPE_IPV6_IFINDEX
:
419 vty_out(vty
, " gate %s, if %s%s",
420 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
,
422 ifindex2ifname(nexthop
->ifindex
),
425 case NEXTHOP_TYPE_IPV4
:
426 vty_out (vty
, " gate %s%s",
427 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
, buf
,
428 sizeof (buf
)), VTY_NEWLINE
);
430 case NEXTHOP_TYPE_IFINDEX
:
431 vty_out (vty
, " if %s%s",
432 ifindex2ifname(nexthop
->ifindex
), VTY_NEWLINE
);
434 case NEXTHOP_TYPE_IPV4_IFINDEX
:
435 vty_out (vty
, " gate %s, if %s%s",
436 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
,
438 ifindex2ifname(nexthop
->ifindex
), VTY_NEWLINE
);
441 vty_out (vty
, " invalid nexthop type %u%s",
442 nexthop
->type
, VTY_NEWLINE
);
447 vty_out (vty
, " %s invalid%s",
448 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
,
449 buf
, sizeof (buf
)), VTY_NEWLINE
);
450 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
))
451 vty_out (vty
, " Must be Connected%s", VTY_NEWLINE
);
453 tbuf
= time(NULL
) - (bgp_clock() - bnc
->last_update
);
454 vty_out (vty
, " Last update: %s", ctime(&tbuf
));
455 vty_out(vty
, "%s", VTY_NEWLINE
);
462 show_ip_bgp_nexthop_table (struct vty
*vty
, const char *name
, int detail
)
467 bgp
= bgp_lookup_by_name (name
);
469 bgp
= bgp_get_default ();
472 vty_out (vty
, "%% No such BGP instance exist%s", VTY_NEWLINE
);
476 bgp_show_nexthops (vty
, bgp
, detail
);
482 bgp_show_all_instances_nexthops_vty (struct vty
*vty
)
484 struct listnode
*node
, *nnode
;
487 for (ALL_LIST_ELEMENTS (bm
->bgp
, node
, nnode
, bgp
))
489 vty_out (vty
, "%sInstance %s:%s",
491 (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) ? "Default" : bgp
->name
,
493 bgp_show_nexthops (vty
, bgp
, 0);
497 DEFUN (show_ip_bgp_nexthop
,
498 show_ip_bgp_nexthop_cmd
,
499 "show [ip] bgp [<view|vrf> VRFNAME] nexthop [detail]",
503 BGP_INSTANCE_HELP_STR
504 "BGP nexthop table\n"
505 "Show detailed information\n")
508 char *vrf
= argv_find (argv
, argc
, "VRFNAME", &idx
) ? argv
[idx
]->arg
: NULL
;
509 int detail
= argv_find (argv
, argc
, "detail", &idx
) ? 1 : 0;
510 return show_ip_bgp_nexthop_table (vty
, vrf
, detail
);
513 DEFUN (show_ip_bgp_instance_all_nexthop
,
514 show_ip_bgp_instance_all_nexthop_cmd
,
515 "show [ip] bgp <view|vrf> all nexthop",
519 BGP_INSTANCE_ALL_HELP_STR
520 "BGP nexthop table\n")
522 bgp_show_all_instances_nexthops_vty (vty
);
527 bgp_scan_init (struct bgp
*bgp
)
531 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
533 bgp
->nexthop_cache_table
[afi
] = bgp_table_init (afi
, SAFI_UNICAST
);
534 bgp
->connected_table
[afi
] = bgp_table_init (afi
, SAFI_UNICAST
);
535 bgp
->import_check_table
[afi
] = bgp_table_init (afi
, SAFI_UNICAST
);
540 bgp_scan_vty_init (void)
542 install_element (VIEW_NODE
, &show_ip_bgp_nexthop_cmd
);
543 install_element (VIEW_NODE
, &show_ip_bgp_instance_all_nexthop_cmd
);
547 bgp_scan_finish (struct bgp
*bgp
)
551 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
553 /* Only the current one needs to be reset. */
554 bgp_nexthop_cache_reset (bgp
->nexthop_cache_table
[afi
]);
555 bgp_table_unlock (bgp
->nexthop_cache_table
[afi
]);
556 bgp
->nexthop_cache_table
[afi
] = NULL
;
558 bgp_table_unlock (bgp
->connected_table
[afi
]);
559 bgp
->connected_table
[afi
] = NULL
;
561 bgp_table_unlock (bgp
->import_check_table
[afi
]);
562 bgp
->import_check_table
[afi
] = NULL
;