]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_nexthop.c
Merge pull request #2503 from pacovn/Coverity_1469898_Uninitialized_scalar_variable
[mirror_frr.git] / bgpd / bgp_nexthop.c
1 /* BGP nexthop scan
2 * Copyright (C) 2000 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
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"
31 #include "hash.h"
32 #include "jhash.h"
33 #include "nexthop.h"
34 #include "queue.h"
35 #include "filter.h"
36
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. */
49
50 char *bnc_str(struct bgp_nexthop_cache *bnc, char *buf, int size)
51 {
52 prefix2str(&(bnc->node->p), buf, size);
53 return buf;
54 }
55
56 void bnc_nexthop_free(struct bgp_nexthop_cache *bnc)
57 {
58 nexthops_free(bnc->nexthop);
59 }
60
61 struct bgp_nexthop_cache *bnc_new(void)
62 {
63 struct bgp_nexthop_cache *bnc;
64
65 bnc = XCALLOC(MTYPE_BGP_NEXTHOP_CACHE,
66 sizeof(struct bgp_nexthop_cache));
67 LIST_INIT(&(bnc->paths));
68 return bnc;
69 }
70
71 void bnc_free(struct bgp_nexthop_cache *bnc)
72 {
73 bnc_nexthop_free(bnc);
74 XFREE(MTYPE_BGP_NEXTHOP_CACHE, bnc);
75 }
76
77 /* Reset and free all BGP nexthop cache. */
78 static void bgp_nexthop_cache_reset(struct bgp_table *table)
79 {
80 struct bgp_node *rn;
81 struct bgp_nexthop_cache *bnc;
82
83 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn))
84 if ((bnc = rn->info) != NULL) {
85 bnc_free(bnc);
86 rn->info = NULL;
87 bgp_unlock_node(rn);
88 }
89 }
90
91 static void *bgp_tip_hash_alloc(void *p)
92 {
93 const struct in_addr *val = (const struct in_addr *)p;
94 struct tip_addr *addr;
95
96 addr = XMALLOC(MTYPE_TIP_ADDR, sizeof(struct tip_addr));
97 addr->refcnt = 0;
98 addr->addr.s_addr = val->s_addr;
99
100 return addr;
101 }
102
103 static void bgp_tip_hash_free(void *addr)
104 {
105 XFREE(MTYPE_TIP_ADDR, addr);
106 }
107
108 static unsigned int bgp_tip_hash_key_make(void *p)
109 {
110 const struct tip_addr *addr = p;
111
112 return jhash_1word(addr->addr.s_addr, 0);
113 }
114
115 static int bgp_tip_hash_cmp(const void *p1, const void *p2)
116 {
117 const struct tip_addr *addr1 = p1;
118 const struct tip_addr *addr2 = p2;
119
120 return addr1->addr.s_addr == addr2->addr.s_addr;
121 }
122
123 void bgp_tip_hash_init(struct bgp *bgp)
124 {
125 bgp->tip_hash = hash_create(bgp_tip_hash_key_make, bgp_tip_hash_cmp,
126 "BGP TIP hash");
127 }
128
129 void bgp_tip_hash_destroy(struct bgp *bgp)
130 {
131 if (bgp->tip_hash == NULL)
132 return;
133 hash_clean(bgp->tip_hash, bgp_tip_hash_free);
134 hash_free(bgp->tip_hash);
135 bgp->tip_hash = NULL;
136 }
137
138 void bgp_tip_add(struct bgp *bgp, struct in_addr *tip)
139 {
140 struct tip_addr tmp;
141 struct tip_addr *addr;
142
143 tmp.addr = *tip;
144
145 addr = hash_get(bgp->tip_hash, &tmp, bgp_tip_hash_alloc);
146 if (!addr)
147 return;
148
149 addr->refcnt++;
150 }
151
152 void bgp_tip_del(struct bgp *bgp, struct in_addr *tip)
153 {
154 struct tip_addr tmp;
155 struct tip_addr *addr;
156
157 tmp.addr = *tip;
158
159 addr = hash_lookup(bgp->tip_hash, &tmp);
160 /* may have been deleted earlier by bgp_interface_down() */
161 if (addr == NULL)
162 return;
163
164 addr->refcnt--;
165
166 if (addr->refcnt == 0) {
167 hash_release(bgp->tip_hash, addr);
168 XFREE(MTYPE_TIP_ADDR, addr);
169 }
170 }
171
172 static void *bgp_address_hash_alloc(void *p)
173 {
174 const struct in_addr *val = (const struct in_addr *)p;
175 struct bgp_addr *addr;
176
177 addr = XMALLOC(MTYPE_BGP_ADDR, sizeof(struct bgp_addr));
178 addr->refcnt = 0;
179 addr->addr.s_addr = val->s_addr;
180
181 return addr;
182 }
183
184 static void bgp_address_hash_free(void *addr)
185 {
186 XFREE(MTYPE_BGP_ADDR, addr);
187 }
188
189 static unsigned int bgp_address_hash_key_make(void *p)
190 {
191 const struct bgp_addr *addr = p;
192
193 return jhash_1word(addr->addr.s_addr, 0);
194 }
195
196 static int bgp_address_hash_cmp(const void *p1, const void *p2)
197 {
198 const struct bgp_addr *addr1 = p1;
199 const struct bgp_addr *addr2 = p2;
200
201 return addr1->addr.s_addr == addr2->addr.s_addr;
202 }
203
204 void bgp_address_init(struct bgp *bgp)
205 {
206 bgp->address_hash =
207 hash_create(bgp_address_hash_key_make, bgp_address_hash_cmp,
208 "BGP Address Hash");
209 }
210
211 void bgp_address_destroy(struct bgp *bgp)
212 {
213 if (bgp->address_hash == NULL)
214 return;
215 hash_clean(bgp->address_hash, bgp_address_hash_free);
216 hash_free(bgp->address_hash);
217 bgp->address_hash = NULL;
218 }
219
220 static void bgp_address_add(struct bgp *bgp, struct prefix *p)
221 {
222 struct bgp_addr tmp;
223 struct bgp_addr *addr;
224
225 tmp.addr = p->u.prefix4;
226
227 addr = hash_get(bgp->address_hash, &tmp, bgp_address_hash_alloc);
228 if (!addr)
229 return;
230
231 addr->refcnt++;
232 }
233
234 static void bgp_address_del(struct bgp *bgp, struct prefix *p)
235 {
236 struct bgp_addr tmp;
237 struct bgp_addr *addr;
238
239 tmp.addr = p->u.prefix4;
240
241 addr = hash_lookup(bgp->address_hash, &tmp);
242 /* may have been deleted earlier by bgp_interface_down() */
243 if (addr == NULL)
244 return;
245
246 addr->refcnt--;
247
248 if (addr->refcnt == 0) {
249 hash_release(bgp->address_hash, addr);
250 XFREE(MTYPE_BGP_ADDR, addr);
251 }
252 }
253
254
255 struct bgp_connected_ref {
256 unsigned int refcnt;
257 };
258
259 void bgp_connected_add(struct bgp *bgp, struct connected *ifc)
260 {
261 struct prefix p;
262 struct prefix *addr;
263 struct bgp_node *rn;
264 struct bgp_connected_ref *bc;
265 struct listnode *node, *nnode;
266 struct peer *peer;
267
268 addr = ifc->address;
269
270 p = *(CONNECTED_PREFIX(ifc));
271 if (addr->family == AF_INET) {
272 apply_mask_ipv4((struct prefix_ipv4 *)&p);
273
274 if (prefix_ipv4_any((struct prefix_ipv4 *)&p))
275 return;
276
277 bgp_address_add(bgp, addr);
278
279 rn = bgp_node_get(bgp->connected_table[AFI_IP],
280 (struct prefix *)&p);
281 if (rn->info) {
282 bc = rn->info;
283 bc->refcnt++;
284 } else {
285 bc = XCALLOC(MTYPE_BGP_CONN,
286 sizeof(struct bgp_connected_ref));
287 bc->refcnt = 1;
288 rn->info = bc;
289 }
290
291 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
292 if (peer->conf_if
293 && (strcmp(peer->conf_if, ifc->ifp->name) == 0)
294 && peer->status != Established
295 && !CHECK_FLAG(peer->flags,
296 PEER_FLAG_IFPEER_V6ONLY)) {
297 if (peer_active(peer))
298 BGP_EVENT_ADD(peer, BGP_Stop);
299 BGP_EVENT_ADD(peer, BGP_Start);
300 }
301 }
302 } else if (addr->family == AF_INET6) {
303 apply_mask_ipv6((struct prefix_ipv6 *)&p);
304
305 if (IN6_IS_ADDR_UNSPECIFIED(&p.u.prefix6))
306 return;
307
308 if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6))
309 return;
310
311 rn = bgp_node_get(bgp->connected_table[AFI_IP6],
312 (struct prefix *)&p);
313 if (rn->info) {
314 bc = rn->info;
315 bc->refcnt++;
316 } else {
317 bc = XCALLOC(MTYPE_BGP_CONN,
318 sizeof(struct bgp_connected_ref));
319 bc->refcnt = 1;
320 rn->info = bc;
321 }
322 }
323 }
324
325 void bgp_connected_delete(struct bgp *bgp, struct connected *ifc)
326 {
327 struct prefix p;
328 struct prefix *addr;
329 struct bgp_node *rn = NULL;
330 struct bgp_connected_ref *bc;
331
332 addr = ifc->address;
333
334 p = *(CONNECTED_PREFIX(ifc));
335 apply_mask(&p);
336 if (addr->family == AF_INET) {
337 if (prefix_ipv4_any((struct prefix_ipv4 *)&p))
338 return;
339
340 bgp_address_del(bgp, addr);
341
342 rn = bgp_node_lookup(bgp->connected_table[AFI_IP], &p);
343 } else if (addr->family == AF_INET6) {
344 if (IN6_IS_ADDR_UNSPECIFIED(&p.u.prefix6))
345 return;
346
347 if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6))
348 return;
349
350 rn = bgp_node_lookup(bgp->connected_table[AFI_IP6],
351 (struct prefix *)&p);
352 }
353
354 if (!rn)
355 return;
356
357 bc = rn->info;
358 bc->refcnt--;
359 if (bc->refcnt == 0) {
360 XFREE(MTYPE_BGP_CONN, bc);
361 rn->info = NULL;
362 }
363 bgp_unlock_node(rn);
364 bgp_unlock_node(rn);
365 }
366
367 static void bgp_connected_cleanup(struct route_table *table,
368 struct route_node *rn)
369 {
370 struct bgp_connected_ref *bc;
371
372 bc = rn->info;
373 if (!bc)
374 return;
375
376 bc->refcnt--;
377 if (bc->refcnt == 0) {
378 XFREE(MTYPE_BGP_CONN, bc);
379 rn->info = NULL;
380 }
381 }
382
383 int bgp_nexthop_self(struct bgp *bgp, struct in_addr nh_addr)
384 {
385 struct bgp_addr tmp, *addr;
386 struct tip_addr tmp_tip, *tip;
387
388 tmp.addr = nh_addr;
389
390 addr = hash_lookup(bgp->address_hash, &tmp);
391 if (addr)
392 return 1;
393
394 tmp_tip.addr = nh_addr;
395 tip = hash_lookup(bgp->tip_hash, &tmp_tip);
396 if (tip)
397 return 1;
398
399 return 0;
400 }
401
402 int bgp_multiaccess_check_v4(struct in_addr nexthop, struct peer *peer)
403 {
404 struct bgp_node *rn1;
405 struct bgp_node *rn2;
406 struct prefix p;
407 int ret;
408
409 p.family = AF_INET;
410 p.prefixlen = IPV4_MAX_BITLEN;
411 p.u.prefix4 = nexthop;
412
413 rn1 = bgp_node_match(peer->bgp->connected_table[AFI_IP], &p);
414 if (!rn1)
415 return 0;
416
417 p.family = AF_INET;
418 p.prefixlen = IPV4_MAX_BITLEN;
419 p.u.prefix4 = peer->su.sin.sin_addr;
420
421 rn2 = bgp_node_match(peer->bgp->connected_table[AFI_IP], &p);
422 if (!rn2) {
423 bgp_unlock_node(rn1);
424 return 0;
425 }
426
427 ret = (rn1 == rn2) ? 1 : 0;
428
429 bgp_unlock_node(rn1);
430 bgp_unlock_node(rn2);
431
432 return (ret);
433 }
434
435 int bgp_subgrp_multiaccess_check_v4(struct in_addr nexthop,
436 struct update_subgroup *subgrp)
437 {
438 struct bgp_node *rn1, *rn2;
439 struct peer_af *paf;
440 struct prefix p, np;
441 struct bgp *bgp;
442
443 np.family = AF_INET;
444 np.prefixlen = IPV4_MAX_BITLEN;
445 np.u.prefix4 = nexthop;
446
447 p.family = AF_INET;
448 p.prefixlen = IPV4_MAX_BITLEN;
449
450 rn2 = NULL;
451
452 bgp = SUBGRP_INST(subgrp);
453 rn1 = bgp_node_match(bgp->connected_table[AFI_IP], &np);
454 if (!rn1)
455 return 0;
456
457 SUBGRP_FOREACH_PEER (subgrp, paf) {
458 p.u.prefix4 = paf->peer->su.sin.sin_addr;
459
460 rn2 = bgp_node_match(bgp->connected_table[AFI_IP], &p);
461 if (rn1 == rn2) {
462 bgp_unlock_node(rn1);
463 bgp_unlock_node(rn2);
464 return 1;
465 }
466
467 if (rn2)
468 bgp_unlock_node(rn2);
469 }
470
471 bgp_unlock_node(rn1);
472 return 0;
473 }
474
475 static void bgp_show_nexthops_detail(struct vty *vty, struct bgp *bgp,
476 struct bgp_nexthop_cache *bnc)
477 {
478 char buf[PREFIX2STR_BUFFER];
479 struct nexthop *nexthop;
480
481 for (nexthop = bnc->nexthop; nexthop; nexthop = nexthop->next)
482 switch (nexthop->type) {
483 case NEXTHOP_TYPE_IPV6:
484 vty_out(vty, " gate %s\n",
485 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf,
486 sizeof(buf)));
487 break;
488 case NEXTHOP_TYPE_IPV6_IFINDEX:
489 vty_out(vty, " gate %s, if %s\n",
490 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf,
491 sizeof(buf)),
492 ifindex2ifname(nexthop->ifindex, bgp->vrf_id));
493 break;
494 case NEXTHOP_TYPE_IPV4:
495 vty_out(vty, " gate %s\n",
496 inet_ntop(AF_INET, &nexthop->gate.ipv4, buf,
497 sizeof(buf)));
498 break;
499 case NEXTHOP_TYPE_IFINDEX:
500 vty_out(vty, " if %s\n",
501 ifindex2ifname(nexthop->ifindex, bgp->vrf_id));
502 break;
503 case NEXTHOP_TYPE_IPV4_IFINDEX:
504 vty_out(vty, " gate %s, if %s\n",
505 inet_ntop(AF_INET, &nexthop->gate.ipv4, buf,
506 sizeof(buf)),
507 ifindex2ifname(nexthop->ifindex, bgp->vrf_id));
508 break;
509 case NEXTHOP_TYPE_BLACKHOLE:
510 vty_out(vty, " blackhole\n");
511 break;
512 default:
513 vty_out(vty, " invalid nexthop type %u\n",
514 nexthop->type);
515 }
516 }
517
518 static void bgp_show_nexthops(struct vty *vty, struct bgp *bgp, int detail)
519 {
520 struct bgp_node *rn;
521 struct bgp_nexthop_cache *bnc;
522 char buf[PREFIX2STR_BUFFER];
523 time_t tbuf;
524 afi_t afi;
525
526 vty_out(vty, "Current BGP nexthop cache:\n");
527 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
528 if (!bgp->nexthop_cache_table[afi])
529 continue;
530
531 for (rn = bgp_table_top(bgp->nexthop_cache_table[afi]); rn;
532 rn = bgp_route_next(rn)) {
533 if ((bnc = rn->info) != NULL) {
534 if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID)) {
535 vty_out(vty,
536 " %s valid [IGP metric %d], #paths %d\n",
537 inet_ntop(rn->p.family,
538 &rn->p.u.prefix, buf,
539 sizeof(buf)),
540 bnc->metric, bnc->path_count);
541
542 if (!detail)
543 continue;
544
545 bgp_show_nexthops_detail(vty, bgp, bnc);
546
547 } else {
548 vty_out(vty, " %s invalid\n",
549 inet_ntop(rn->p.family,
550 &rn->p.u.prefix, buf,
551 sizeof(buf)));
552 if (CHECK_FLAG(bnc->flags,
553 BGP_NEXTHOP_CONNECTED))
554 vty_out(vty,
555 " Must be Connected\n");
556 }
557 tbuf = time(NULL)
558 - (bgp_clock() - bnc->last_update);
559 vty_out(vty, " Last update: %s", ctime(&tbuf));
560 vty_out(vty, "\n");
561 }
562 }
563 }
564 }
565
566 static int show_ip_bgp_nexthop_table(struct vty *vty, const char *name,
567 int detail)
568 {
569 struct bgp *bgp;
570
571 if (name)
572 bgp = bgp_lookup_by_name(name);
573 else
574 bgp = bgp_get_default();
575 if (!bgp) {
576 vty_out(vty, "%% No such BGP instance exist\n");
577 return CMD_WARNING;
578 }
579
580 bgp_show_nexthops(vty, bgp, detail);
581
582 return CMD_SUCCESS;
583 }
584
585 static void bgp_show_all_instances_nexthops_vty(struct vty *vty)
586 {
587 struct listnode *node, *nnode;
588 struct bgp *bgp;
589
590 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
591 vty_out(vty, "\nInstance %s:\n",
592 (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
593 ? "Default"
594 : bgp->name);
595 bgp_show_nexthops(vty, bgp, 0);
596 }
597 }
598
599 DEFUN (show_ip_bgp_nexthop,
600 show_ip_bgp_nexthop_cmd,
601 "show [ip] bgp [<view|vrf> VIEWVRFNAME] nexthop [detail]",
602 SHOW_STR
603 IP_STR
604 BGP_STR
605 BGP_INSTANCE_HELP_STR
606 "BGP nexthop table\n"
607 "Show detailed information\n")
608 {
609 int idx = 0;
610 char *vrf = NULL;
611
612 if (argv_find(argv, argc, "view", &idx)
613 || argv_find(argv, argc, "vrf", &idx))
614 vrf = argv[++idx]->arg;
615 int detail = argv_find(argv, argc, "detail", &idx) ? 1 : 0;
616 return show_ip_bgp_nexthop_table(vty, vrf, detail);
617 }
618
619 DEFUN (show_ip_bgp_instance_all_nexthop,
620 show_ip_bgp_instance_all_nexthop_cmd,
621 "show [ip] bgp <view|vrf> all nexthop",
622 SHOW_STR
623 IP_STR
624 BGP_STR
625 BGP_INSTANCE_ALL_HELP_STR
626 "BGP nexthop table\n")
627 {
628 bgp_show_all_instances_nexthops_vty(vty);
629 return CMD_SUCCESS;
630 }
631
632 void bgp_scan_init(struct bgp *bgp)
633 {
634 afi_t afi;
635
636 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
637 bgp->nexthop_cache_table[afi] =
638 bgp_table_init(bgp, afi, SAFI_UNICAST);
639 bgp->connected_table[afi] = bgp_table_init(bgp, afi,
640 SAFI_UNICAST);
641 bgp->import_check_table[afi] =
642 bgp_table_init(bgp, afi, SAFI_UNICAST);
643 }
644 }
645
646 void bgp_scan_vty_init(void)
647 {
648 install_element(VIEW_NODE, &show_ip_bgp_nexthop_cmd);
649 install_element(VIEW_NODE, &show_ip_bgp_instance_all_nexthop_cmd);
650 }
651
652 void bgp_scan_finish(struct bgp *bgp)
653 {
654 afi_t afi;
655
656 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
657 /* Only the current one needs to be reset. */
658 bgp_nexthop_cache_reset(bgp->nexthop_cache_table[afi]);
659 bgp_table_unlock(bgp->nexthop_cache_table[afi]);
660 bgp->nexthop_cache_table[afi] = NULL;
661
662 bgp->connected_table[afi]->route_table->cleanup =
663 bgp_connected_cleanup;
664 bgp_table_unlock(bgp->connected_table[afi]);
665 bgp->connected_table[afi] = NULL;
666
667 bgp_table_unlock(bgp->import_check_table[afi]);
668 bgp->import_check_table[afi] = NULL;
669 }
670 }