]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_nexthop.c
BGP: Update commands for VRF support
[mirror_frr.git] / bgpd / bgp_nexthop.c
CommitLineData
718e3744 1/* BGP nexthop scan
2 Copyright (C) 2000 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 "command.h"
24#include "thread.h"
25#include "prefix.h"
26#include "zclient.h"
27#include "stream.h"
28#include "network.h"
29#include "log.h"
30#include "memory.h"
10f9bf3f
JBD
31#include "hash.h"
32#include "jhash.h"
fb018d25 33#include "nexthop.h"
3f9c7369 34#include "queue.h"
718e3744 35
36#include "bgpd/bgpd.h"
37#include "bgpd/bgp_table.h"
38#include "bgpd/bgp_route.h"
39#include "bgpd/bgp_attr.h"
40#include "bgpd/bgp_nexthop.h"
fb018d25 41#include "bgpd/bgp_nht.h"
718e3744 42#include "bgpd/bgp_debug.h"
43#include "bgpd/bgp_damp.h"
8ffedcea 44#include "bgpd/bgp_fsm.h"
718e3744 45#include "zebra/rib.h"
46#include "zebra/zserv.h" /* For ZEBRA_SERV_PATH. */
47
fb018d25 48
718e3744 49
fb018d25
DS
50char *
51bnc_str (struct bgp_nexthop_cache *bnc, char *buf, int size)
52{
53 prefix2str(&(bnc->node->p), buf, size);
54 return buf;
55}
56
fb018d25 57void
718e3744 58bnc_nexthop_free (struct bgp_nexthop_cache *bnc)
59{
60 struct nexthop *nexthop;
61 struct nexthop *next = NULL;
62
63 for (nexthop = bnc->nexthop; nexthop; nexthop = next)
64 {
65 next = nexthop->next;
66 XFREE (MTYPE_NEXTHOP, nexthop);
67 }
68}
69
fb018d25 70struct bgp_nexthop_cache *
ffd0c037 71bnc_new (void)
718e3744 72{
fb018d25
DS
73 struct bgp_nexthop_cache *bnc;
74
75 bnc = XCALLOC (MTYPE_BGP_NEXTHOP_CACHE, sizeof (struct bgp_nexthop_cache));
76 LIST_INIT(&(bnc->paths));
77 return bnc;
718e3744 78}
79
fb018d25 80void
718e3744 81bnc_free (struct bgp_nexthop_cache *bnc)
82{
83 bnc_nexthop_free (bnc);
84 XFREE (MTYPE_BGP_NEXTHOP_CACHE, bnc);
85}
6b0655a2 86
718e3744 87/* Reset and free all BGP nexthop cache. */
94f2b392 88static void
718e3744 89bgp_nexthop_cache_reset (struct bgp_table *table)
90{
91 struct bgp_node *rn;
92 struct bgp_nexthop_cache *bnc;
93
94 for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
95 if ((bnc = rn->info) != NULL)
96 {
97 bnc_free (bnc);
98 rn->info = NULL;
99 bgp_unlock_node (rn);
100 }
101}
102
10f9bf3f
JBD
103/* BGP own address structure */
104struct bgp_addr
105{
106 struct in_addr addr;
107 int refcnt;
108};
109
10f9bf3f
JBD
110static void *
111bgp_address_hash_alloc (void *p)
112{
ffd0c037 113 const struct in_addr *val = (const struct in_addr *)p;
10f9bf3f
JBD
114 struct bgp_addr *addr;
115
116 addr = XMALLOC (MTYPE_BGP_ADDR, sizeof (struct bgp_addr));
117 addr->refcnt = 0;
118 addr->addr.s_addr = val->s_addr;
119
120 return addr;
121}
122
123static unsigned int
124bgp_address_hash_key_make (void *p)
125{
126 const struct bgp_addr *addr = p;
127
128 return jhash_1word(addr->addr.s_addr, 0);
129}
130
131static int
132bgp_address_hash_cmp (const void *p1, const void *p2)
133{
134 const struct bgp_addr *addr1 = p1;
135 const struct bgp_addr *addr2 = p2;
136
137 return addr1->addr.s_addr == addr2->addr.s_addr;
138}
139
140void
6aeb9e78 141bgp_address_init (struct bgp *bgp)
10f9bf3f 142{
6aeb9e78 143 bgp->address_hash = hash_create (bgp_address_hash_key_make,
10f9bf3f
JBD
144 bgp_address_hash_cmp);
145}
146
147static void
6aeb9e78 148bgp_address_add (struct bgp *bgp, struct prefix *p)
10f9bf3f
JBD
149{
150 struct bgp_addr tmp;
151 struct bgp_addr *addr;
152
153 tmp.addr = p->u.prefix4;
154
6aeb9e78 155 addr = hash_get (bgp->address_hash, &tmp, bgp_address_hash_alloc);
5ce10e92
DS
156 if (!addr)
157 return;
158
10f9bf3f
JBD
159 addr->refcnt++;
160}
161
162static void
6aeb9e78 163bgp_address_del (struct bgp *bgp, struct prefix *p)
10f9bf3f
JBD
164{
165 struct bgp_addr tmp;
166 struct bgp_addr *addr;
167
168 tmp.addr = p->u.prefix4;
169
6aeb9e78 170 addr = hash_lookup (bgp->address_hash, &tmp);
9e47abd8
RG
171 /* may have been deleted earlier by bgp_interface_down() */
172 if (addr == NULL)
173 return;
174
10f9bf3f
JBD
175 addr->refcnt--;
176
177 if (addr->refcnt == 0)
178 {
6aeb9e78 179 hash_release (bgp->address_hash, addr);
10f9bf3f
JBD
180 XFREE (MTYPE_BGP_ADDR, addr);
181 }
182}
183
6b0655a2 184
5932020b 185struct bgp_connected_ref
718e3744 186{
187 unsigned int refcnt;
188};
189
190void
6aeb9e78 191bgp_connected_add (struct bgp *bgp, struct connected *ifc)
718e3744 192{
193 struct prefix p;
194 struct prefix *addr;
718e3744 195 struct bgp_node *rn;
5932020b 196 struct bgp_connected_ref *bc;
6aeb9e78 197 struct listnode *node, *nnode;
8ffedcea 198 struct peer *peer;
718e3744 199
718e3744 200 addr = ifc->address;
718e3744 201
4608cb43 202 p = *(CONNECTED_PREFIX(ifc));
718e3744 203 if (addr->family == AF_INET)
204 {
718e3744 205 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
206
207 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
208 return;
209
6aeb9e78 210 bgp_address_add (bgp, addr);
10f9bf3f 211
6aeb9e78 212 rn = bgp_node_get (bgp->connected_table[AFI_IP], (struct prefix *) &p);
718e3744 213 if (rn->info)
214 {
215 bc = rn->info;
216 bc->refcnt++;
217 }
218 else
219 {
6c88b44d 220 bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref));
718e3744 221 bc->refcnt = 1;
222 rn->info = bc;
223 }
8ffedcea 224
6aeb9e78
DS
225 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
226 {
227 if (peer->conf_if && (strcmp (peer->conf_if, ifc->ifp->name) == 0) &&
228 !CHECK_FLAG(peer->flags, PEER_FLAG_IFPEER_V6ONLY))
229 {
230 if (peer_active(peer))
231 BGP_EVENT_ADD (peer, BGP_Stop);
232 BGP_EVENT_ADD (peer, BGP_Start);
233 }
234 }
718e3744 235 }
236#ifdef HAVE_IPV6
e4529636 237 else if (addr->family == AF_INET6)
718e3744 238 {
718e3744 239 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
240
241 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
242 return;
243
244 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
245 return;
246
6aeb9e78 247 rn = bgp_node_get (bgp->connected_table[AFI_IP6], (struct prefix *) &p);
718e3744 248 if (rn->info)
249 {
250 bc = rn->info;
251 bc->refcnt++;
252 }
253 else
254 {
6c88b44d 255 bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref));
718e3744 256 bc->refcnt = 1;
257 rn->info = bc;
258 }
259 }
260#endif /* HAVE_IPV6 */
261}
262
263void
6aeb9e78 264bgp_connected_delete (struct bgp *bgp, struct connected *ifc)
718e3744 265{
266 struct prefix p;
267 struct prefix *addr;
718e3744 268 struct bgp_node *rn;
5932020b 269 struct bgp_connected_ref *bc;
718e3744 270
718e3744 271 addr = ifc->address;
718e3744 272
4608cb43 273 p = *(CONNECTED_PREFIX(ifc));
718e3744 274 if (addr->family == AF_INET)
275 {
718e3744 276 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
277
278 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
279 return;
280
6aeb9e78 281 bgp_address_del (bgp, addr);
10f9bf3f 282
6aeb9e78 283 rn = bgp_node_lookup (bgp->connected_table[AFI_IP], &p);
718e3744 284 if (! rn)
285 return;
286
287 bc = rn->info;
288 bc->refcnt--;
289 if (bc->refcnt == 0)
290 {
6c88b44d 291 XFREE (MTYPE_BGP_CONN, bc);
718e3744 292 rn->info = NULL;
293 }
294 bgp_unlock_node (rn);
295 bgp_unlock_node (rn);
296 }
297#ifdef HAVE_IPV6
298 else if (addr->family == AF_INET6)
299 {
718e3744 300 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
301
302 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
303 return;
304
305 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
306 return;
307
6aeb9e78 308 rn = bgp_node_lookup (bgp->connected_table[AFI_IP6], (struct prefix *) &p);
718e3744 309 if (! rn)
310 return;
311
312 bc = rn->info;
313 bc->refcnt--;
314 if (bc->refcnt == 0)
315 {
6c88b44d 316 XFREE (MTYPE_BGP_CONN, bc);
718e3744 317 rn->info = NULL;
318 }
319 bgp_unlock_node (rn);
320 bgp_unlock_node (rn);
321 }
322#endif /* HAVE_IPV6 */
323}
324
325int
6aeb9e78 326bgp_nexthop_self (struct bgp *bgp, struct attr *attr)
718e3744 327{
10f9bf3f 328 struct bgp_addr tmp, *addr;
718e3744 329
10f9bf3f
JBD
330 tmp.addr = attr->nexthop;
331
6aeb9e78 332 addr = hash_lookup (bgp->address_hash, &tmp);
10f9bf3f
JBD
333 if (addr)
334 return 1;
718e3744 335
718e3744 336 return 0;
337}
6b0655a2 338
718e3744 339int
fc9a856f 340bgp_multiaccess_check_v4 (struct in_addr nexthop, struct peer *peer)
718e3744 341{
342 struct bgp_node *rn1;
343 struct bgp_node *rn2;
fc9a856f 344 struct prefix p;
718e3744 345 int ret;
346
fc9a856f
DS
347 p.family = AF_INET;
348 p.prefixlen = IPV4_MAX_BITLEN;
349 p.u.prefix4 = nexthop;
718e3744 350
6aeb9e78 351 rn1 = bgp_node_match (peer->bgp->connected_table[AFI_IP], &p);
fc9a856f 352 if (!rn1)
718e3744 353 return 0;
718e3744 354
fc9a856f
DS
355 p.family = AF_INET;
356 p.prefixlen = IPV4_MAX_BITLEN;
357 p.u.prefix4 = peer->su.sin.sin_addr;
718e3744 358
6aeb9e78 359 rn2 = bgp_node_match (peer->bgp->connected_table[AFI_IP], &p);
fc9a856f 360 if (!rn2)
718e3744 361 {
fc9a856f
DS
362 bgp_unlock_node(rn1);
363 return 0;
718e3744 364 }
365
fc9a856f 366 ret = (rn1 == rn2) ? 1 : 0;
718e3744 367
fc9a856f
DS
368 bgp_unlock_node(rn1);
369 bgp_unlock_node(rn2);
718e3744 370
fc9a856f 371 return (ret);
718e3744 372}
373
fb018d25 374static int
6aeb9e78 375show_ip_bgp_nexthop_table (struct vty *vty, const char *name, int detail)
fb018d25
DS
376{
377 struct bgp_node *rn;
378 struct bgp_nexthop_cache *bnc;
da11a696 379 char buf[PREFIX2STR_BUFFER];
77217fd4 380 struct nexthop *nexthop;
fb018d25 381 time_t tbuf;
da11a696 382 afi_t afi;
6aeb9e78
DS
383 struct bgp *bgp;
384
385 if (name)
386 bgp = bgp_lookup_by_name (name);
387 else
388 bgp = bgp_get_default ();
389 if (!bgp)
390 {
391 vty_out (vty, "%% No such BGP instance exist%s", VTY_NEWLINE);
392 return CMD_WARNING;
393 }
fb018d25
DS
394
395 vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE);
da11a696
DS
396 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
397 {
6aeb9e78 398 for (rn = bgp_table_top (bgp->nexthop_cache_table[afi]); rn; rn = bgp_route_next (rn))
fb018d25 399 {
da11a696
DS
400 if ((bnc = rn->info) != NULL)
401 {
402 if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID))
77217fd4 403 {
da11a696
DS
404 vty_out (vty, " %s valid [IGP metric %d], #paths %d%s",
405 inet_ntop (rn->p.family, &rn->p.u.prefix, buf, sizeof (buf)),
406 bnc->metric, bnc->path_count, VTY_NEWLINE);
407 if (detail)
408 for (nexthop = bnc->nexthop; nexthop; nexthop = nexthop->next)
409 switch (nexthop->type)
410 {
411 case NEXTHOP_TYPE_IPV6:
412 vty_out (vty, " gate %s%s",
413 inet_ntop (AF_INET6, &nexthop->gate.ipv6,
414 buf, sizeof (buf)), VTY_NEWLINE);
415 break;
416 case NEXTHOP_TYPE_IPV6_IFINDEX:
417 vty_out(vty, " gate %s, if %s%s",
418 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf,
419 sizeof (buf)),
420 ifindex2ifname(nexthop->ifindex),
421 VTY_NEWLINE);
422 break;
423 case NEXTHOP_TYPE_IPV4:
424 vty_out (vty, " gate %s%s",
425 inet_ntop (AF_INET, &nexthop->gate.ipv4, buf,
426 sizeof (buf)), VTY_NEWLINE);
427 break;
428 case NEXTHOP_TYPE_IFINDEX:
429 vty_out (vty, " if %s%s",
430 ifindex2ifname(nexthop->ifindex), VTY_NEWLINE);
431 break;
432 case NEXTHOP_TYPE_IPV4_IFINDEX:
433 vty_out (vty, " gate %s, if %s%s",
434 inet_ntop(AF_INET, &nexthop->gate.ipv4, buf,
435 sizeof (buf)),
436 ifindex2ifname(nexthop->ifindex), VTY_NEWLINE);
437 break;
438 default:
439 vty_out (vty, " invalid nexthop type %u%s",
440 nexthop->type, VTY_NEWLINE);
441 }
442 }
443 else
444 {
445 vty_out (vty, " %s invalid%s",
446 inet_ntop (rn->p.family, &rn->p.u.prefix,
447 buf, sizeof (buf)), VTY_NEWLINE);
448 if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED))
449 vty_out (vty, " Must be Connected%s", VTY_NEWLINE);
77217fd4 450 }
fb018d25 451#ifdef HAVE_CLOCK_MONOTONIC
da11a696
DS
452 tbuf = time(NULL) - (bgp_clock() - bnc->last_update);
453 vty_out (vty, " Last update: %s", ctime(&tbuf));
fb018d25 454#else
da11a696 455 vty_out (vty, " Last update: %s", ctime(&bnc->uptime));
fb018d25 456#endif /* HAVE_CLOCK_MONOTONIC */
da11a696
DS
457 vty_out(vty, "%s", VTY_NEWLINE);
458 }
fb018d25 459 }
da11a696 460 }
fb018d25
DS
461 return CMD_SUCCESS;
462}
463
fb018d25
DS
464DEFUN (show_ip_bgp_nexthop,
465 show_ip_bgp_nexthop_cmd,
466 "show ip bgp nexthop",
467 SHOW_STR
468 IP_STR
469 BGP_STR
470 "BGP nexthop table\n")
471{
6aeb9e78 472 return show_ip_bgp_nexthop_table (vty, NULL, 0);
fb018d25
DS
473}
474
475DEFUN (show_ip_bgp_nexthop_detail,
476 show_ip_bgp_nexthop_detail_cmd,
477 "show ip bgp nexthop detail",
478 SHOW_STR
479 IP_STR
480 BGP_STR
481 "BGP nexthop table\n")
482{
6aeb9e78 483 return show_ip_bgp_nexthop_table (vty, NULL, 1);
fb018d25
DS
484}
485
50ef26d4 486DEFUN (show_ip_bgp_view_nexthop,
487 show_ip_bgp_view_nexthop_cmd,
488 "show ip bgp (view|vrf) WORD nexthop",
489 SHOW_STR
490 IP_STR
491 BGP_STR
492 "BGP view\nBGP VRF\n"
493 "View/VRF name\n"
494 "BGP nexthop table\n")
495{
496 return show_ip_bgp_nexthop_table (vty, argv[1], 0);
497}
498
499DEFUN (show_ip_bgp_view_nexthop_detail,
500 show_ip_bgp_view_nexthop_detail_cmd,
501 "show ip bgp (view|vrf) WORD nexthop detail",
502 SHOW_STR
503 IP_STR
504 BGP_STR
505 "BGP view\nBGP VRF\n"
506 "View/VRF name\n"
507 "BGP nexthop table\n")
508{
509 return show_ip_bgp_nexthop_table (vty, argv[1], 1);
510}
511
718e3744 512void
6aeb9e78 513bgp_scan_init (struct bgp *bgp)
718e3744 514{
6aeb9e78
DS
515 bgp->nexthop_cache_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
516 bgp->connected_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
517 bgp->import_check_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
718e3744 518
519#ifdef HAVE_IPV6
6aeb9e78
DS
520 bgp->nexthop_cache_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
521 bgp->connected_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
522 bgp->import_check_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
718e3744 523#endif /* HAVE_IPV6 */
524
fc9a856f
DS
525}
526
527void
ffd0c037 528bgp_scan_vty_init (void)
fc9a856f
DS
529{
530 install_element (ENABLE_NODE, &show_ip_bgp_nexthop_cmd);
fb018d25
DS
531 install_element (VIEW_NODE, &show_ip_bgp_nexthop_cmd);
532 install_element (VIEW_NODE, &show_ip_bgp_nexthop_detail_cmd);
fb018d25 533 install_element (ENABLE_NODE, &show_ip_bgp_nexthop_detail_cmd);
50ef26d4 534 install_element (ENABLE_NODE, &show_ip_bgp_view_nexthop_cmd);
535 install_element (VIEW_NODE, &show_ip_bgp_view_nexthop_cmd);
536 install_element (VIEW_NODE, &show_ip_bgp_view_nexthop_detail_cmd);
537 install_element (ENABLE_NODE, &show_ip_bgp_view_nexthop_detail_cmd);
718e3744 538}
228da428
CC
539
540void
6aeb9e78 541bgp_scan_finish (struct bgp *bgp)
228da428 542{
6c88b44d 543 /* Only the current one needs to be reset. */
6aeb9e78 544 bgp_nexthop_cache_reset (bgp->nexthop_cache_table[AFI_IP]);
6c88b44d 545
6aeb9e78
DS
546 bgp_table_unlock (bgp->nexthop_cache_table[AFI_IP]);
547 bgp->nexthop_cache_table[AFI_IP] = NULL;
228da428 548
6aeb9e78
DS
549 bgp_table_unlock (bgp->connected_table[AFI_IP]);
550 bgp->connected_table[AFI_IP] = NULL;
228da428 551
6aeb9e78
DS
552 bgp_table_unlock (bgp->import_check_table[AFI_IP]);
553 bgp->import_check_table[AFI_IP] = NULL;
078430f6 554
228da428 555#ifdef HAVE_IPV6
6c88b44d 556 /* Only the current one needs to be reset. */
6aeb9e78 557 bgp_nexthop_cache_reset (bgp->nexthop_cache_table[AFI_IP6]);
6c88b44d 558
6aeb9e78
DS
559 bgp_table_unlock (bgp->nexthop_cache_table[AFI_IP6]);
560 bgp->nexthop_cache_table[AFI_IP6] = NULL;
228da428 561
6aeb9e78
DS
562 bgp_table_unlock (bgp->connected_table[AFI_IP6]);
563 bgp->connected_table[AFI_IP6] = NULL;
078430f6 564
6aeb9e78
DS
565 bgp_table_unlock (bgp->import_check_table[AFI_IP6]);
566 bgp->import_check_table[AFI_IP6] = NULL;
228da428
CC
567#endif /* HAVE_IPV6 */
568}