build: Add QuaggaId to README.NetBSD
[jleu-quagga.git] / bgpd / bgp_nexthop.c
blob0cde665eb380e4d2585d3d8595d7a41b249d30ca
1 /* BGP nexthop scan
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
9 later version.
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
19 02111-1307, USA. */
21 #include <zebra.h>
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"
32 #include "bgpd/bgpd.h"
33 #include "bgpd/bgp_table.h"
34 #include "bgpd/bgp_route.h"
35 #include "bgpd/bgp_attr.h"
36 #include "bgpd/bgp_nexthop.h"
37 #include "bgpd/bgp_debug.h"
38 #include "bgpd/bgp_damp.h"
39 #include "zebra/rib.h"
40 #include "zebra/zserv.h" /* For ZEBRA_SERV_PATH. */
42 struct bgp_nexthop_cache *zlookup_query (struct in_addr);
43 #ifdef HAVE_IPV6
44 struct bgp_nexthop_cache *zlookup_query_ipv6 (struct in6_addr *);
45 #endif /* HAVE_IPV6 */
47 /* Only one BGP scan thread are activated at the same time. */
48 static struct thread *bgp_scan_thread = NULL;
50 /* BGP import thread */
51 static struct thread *bgp_import_thread = NULL;
53 /* BGP scan interval. */
54 static int bgp_scan_interval;
56 /* BGP import interval. */
57 static int bgp_import_interval;
59 /* Route table for next-hop lookup cache. */
60 static struct bgp_table *bgp_nexthop_cache_table[AFI_MAX];
61 static struct bgp_table *cache1_table[AFI_MAX];
62 static struct bgp_table *cache2_table[AFI_MAX];
64 /* Route table for connected route. */
65 static struct bgp_table *bgp_connected_table[AFI_MAX];
67 /* BGP nexthop lookup query client. */
68 struct zclient *zlookup = NULL;
70 /* Add nexthop to the end of the list. */
71 static void
72 bnc_nexthop_add (struct bgp_nexthop_cache *bnc, struct nexthop *nexthop)
74 struct nexthop *last;
76 for (last = bnc->nexthop; last && last->next; last = last->next)
78 if (last)
79 last->next = nexthop;
80 else
81 bnc->nexthop = nexthop;
82 nexthop->prev = last;
85 static void
86 bnc_nexthop_free (struct bgp_nexthop_cache *bnc)
88 struct nexthop *nexthop;
89 struct nexthop *next = NULL;
91 for (nexthop = bnc->nexthop; nexthop; nexthop = next)
93 next = nexthop->next;
94 XFREE (MTYPE_NEXTHOP, nexthop);
98 static struct bgp_nexthop_cache *
99 bnc_new (void)
101 return XCALLOC (MTYPE_BGP_NEXTHOP_CACHE, sizeof (struct bgp_nexthop_cache));
104 static void
105 bnc_free (struct bgp_nexthop_cache *bnc)
107 bnc_nexthop_free (bnc);
108 XFREE (MTYPE_BGP_NEXTHOP_CACHE, bnc);
111 static int
112 bgp_nexthop_same (struct nexthop *next1, struct nexthop *next2)
114 if (next1->type != next2->type)
115 return 0;
117 switch (next1->type)
119 case ZEBRA_NEXTHOP_IPV4:
120 if (! IPV4_ADDR_SAME (&next1->gate.ipv4, &next2->gate.ipv4))
121 return 0;
122 break;
123 case ZEBRA_NEXTHOP_IFINDEX:
124 case ZEBRA_NEXTHOP_IFNAME:
125 if (next1->ifindex != next2->ifindex)
126 return 0;
127 break;
128 #ifdef HAVE_IPV6
129 case ZEBRA_NEXTHOP_IPV6:
130 if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
131 return 0;
132 break;
133 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
134 case ZEBRA_NEXTHOP_IPV6_IFNAME:
135 if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
136 return 0;
137 if (next1->ifindex != next2->ifindex)
138 return 0;
139 break;
140 #endif /* HAVE_IPV6 */
141 default:
142 /* do nothing */
143 break;
145 return 1;
148 static int
149 bgp_nexthop_cache_changed (struct bgp_nexthop_cache *bnc1,
150 struct bgp_nexthop_cache *bnc2)
152 int i;
153 struct nexthop *next1, *next2;
155 if (bnc1->nexthop_num != bnc2->nexthop_num)
156 return 1;
158 next1 = bnc1->nexthop;
159 next2 = bnc2->nexthop;
161 for (i = 0; i < bnc1->nexthop_num; i++)
163 if (! bgp_nexthop_same (next1, next2))
164 return 1;
166 next1 = next1->next;
167 next2 = next2->next;
169 return 0;
172 /* If nexthop exists on connected network return 1. */
174 bgp_nexthop_check_ebgp (afi_t afi, struct attr *attr)
176 struct bgp_node *rn;
178 /* If zebra is not enabled return */
179 if (zlookup->sock < 0)
180 return 1;
182 /* Lookup the address is onlink or not. */
183 if (afi == AFI_IP)
185 rn = bgp_node_match_ipv4 (bgp_connected_table[AFI_IP], &attr->nexthop);
186 if (rn)
188 bgp_unlock_node (rn);
189 return 1;
192 #ifdef HAVE_IPV6
193 else if (afi == AFI_IP6)
195 if (attr->extra->mp_nexthop_len == 32)
196 return 1;
197 else if (attr->extra->mp_nexthop_len == 16)
199 if (IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
200 return 1;
202 rn = bgp_node_match_ipv6 (bgp_connected_table[AFI_IP6],
203 &attr->extra->mp_nexthop_global);
204 if (rn)
206 bgp_unlock_node (rn);
207 return 1;
211 #endif /* HAVE_IPV6 */
212 return 0;
215 #ifdef HAVE_IPV6
216 /* Check specified next-hop is reachable or not. */
217 static int
218 bgp_nexthop_lookup_ipv6 (struct peer *peer, struct bgp_info *ri, int *changed,
219 int *metricchanged)
221 struct bgp_node *rn;
222 struct prefix p;
223 struct bgp_nexthop_cache *bnc;
224 struct attr *attr;
226 /* If lookup is not enabled, return valid. */
227 if (zlookup->sock < 0)
229 if (ri->extra)
230 ri->extra->igpmetric = 0;
231 return 1;
234 /* Only check IPv6 global address only nexthop. */
235 attr = ri->attr;
237 if (attr->extra->mp_nexthop_len != 16
238 || IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
239 return 1;
241 memset (&p, 0, sizeof (struct prefix));
242 p.family = AF_INET6;
243 p.prefixlen = IPV6_MAX_BITLEN;
244 p.u.prefix6 = attr->extra->mp_nexthop_global;
246 /* IBGP or ebgp-multihop */
247 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP6], &p);
249 if (rn->info)
251 bnc = rn->info;
252 bgp_unlock_node (rn);
254 else
256 bnc = zlookup_query_ipv6 (&attr->extra->mp_nexthop_global);
257 if (bnc)
259 struct bgp_table *old;
260 struct bgp_node *oldrn;
261 struct bgp_nexthop_cache *oldbnc;
263 if (changed)
265 if (bgp_nexthop_cache_table[AFI_IP6] == cache1_table[AFI_IP6])
266 old = cache2_table[AFI_IP6];
267 else
268 old = cache1_table[AFI_IP6];
270 oldrn = bgp_node_lookup (old, &p);
271 if (oldrn)
273 oldbnc = oldrn->info;
275 bnc->changed = bgp_nexthop_cache_changed (bnc, oldbnc);
277 if (bnc->metric != oldbnc->metric)
278 bnc->metricchanged = 1;
282 else
284 bnc = bnc_new ();
285 bnc->valid = 0;
287 rn->info = bnc;
290 if (changed)
291 *changed = bnc->changed;
293 if (metricchanged)
294 *metricchanged = bnc->metricchanged;
296 if (bnc->valid && bnc->metric)
297 (bgp_info_extra_get (ri))->igpmetric = bnc->metric;
298 else if (ri->extra)
299 ri->extra->igpmetric = 0;
301 return bnc->valid;
303 #endif /* HAVE_IPV6 */
305 /* Check specified next-hop is reachable or not. */
307 bgp_nexthop_lookup (afi_t afi, struct peer *peer, struct bgp_info *ri,
308 int *changed, int *metricchanged)
310 struct bgp_node *rn;
311 struct prefix p;
312 struct bgp_nexthop_cache *bnc;
313 struct in_addr addr;
315 /* If lookup is not enabled, return valid. */
316 if (zlookup->sock < 0)
318 if (ri->extra)
319 ri->extra->igpmetric = 0;
320 return 1;
323 #ifdef HAVE_IPV6
324 if (afi == AFI_IP6)
325 return bgp_nexthop_lookup_ipv6 (peer, ri, changed, metricchanged);
326 #endif /* HAVE_IPV6 */
328 addr = ri->attr->nexthop;
330 memset (&p, 0, sizeof (struct prefix));
331 p.family = AF_INET;
332 p.prefixlen = IPV4_MAX_BITLEN;
333 p.u.prefix4 = addr;
335 /* IBGP or ebgp-multihop */
336 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP], &p);
338 if (rn->info)
340 bnc = rn->info;
341 bgp_unlock_node (rn);
343 else
345 bnc = zlookup_query (addr);
346 if (bnc)
348 struct bgp_table *old;
349 struct bgp_node *oldrn;
350 struct bgp_nexthop_cache *oldbnc;
352 if (changed)
354 if (bgp_nexthop_cache_table[AFI_IP] == cache1_table[AFI_IP])
355 old = cache2_table[AFI_IP];
356 else
357 old = cache1_table[AFI_IP];
359 oldrn = bgp_node_lookup (old, &p);
360 if (oldrn)
362 oldbnc = oldrn->info;
364 bnc->changed = bgp_nexthop_cache_changed (bnc, oldbnc);
366 if (bnc->metric != oldbnc->metric)
367 bnc->metricchanged = 1;
371 else
373 bnc = bnc_new ();
374 bnc->valid = 0;
376 rn->info = bnc;
379 if (changed)
380 *changed = bnc->changed;
382 if (metricchanged)
383 *metricchanged = bnc->metricchanged;
385 if (bnc->valid && bnc->metric)
386 (bgp_info_extra_get(ri))->igpmetric = bnc->metric;
387 else if (ri->extra)
388 ri->extra->igpmetric = 0;
390 return bnc->valid;
393 /* Reset and free all BGP nexthop cache. */
394 static void
395 bgp_nexthop_cache_reset (struct bgp_table *table)
397 struct bgp_node *rn;
398 struct bgp_nexthop_cache *bnc;
400 for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
401 if ((bnc = rn->info) != NULL)
403 bnc_free (bnc);
404 rn->info = NULL;
405 bgp_unlock_node (rn);
409 static void
410 bgp_scan (afi_t afi, safi_t safi)
412 struct bgp_node *rn;
413 struct bgp *bgp;
414 struct bgp_info *bi;
415 struct bgp_info *next;
416 struct peer *peer;
417 struct listnode *node, *nnode;
418 int valid;
419 int current;
420 int changed;
421 int metricchanged;
423 /* Change cache. */
424 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
425 bgp_nexthop_cache_table[afi] = cache2_table[afi];
426 else
427 bgp_nexthop_cache_table[afi] = cache1_table[afi];
429 /* Get default bgp. */
430 bgp = bgp_get_default ();
431 if (bgp == NULL)
432 return;
434 /* Maximum prefix check */
435 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
437 if (peer->status != Established)
438 continue;
440 if (peer->afc[afi][SAFI_UNICAST])
441 bgp_maximum_prefix_overflow (peer, afi, SAFI_UNICAST, 1);
442 if (peer->afc[afi][SAFI_MULTICAST])
443 bgp_maximum_prefix_overflow (peer, afi, SAFI_MULTICAST, 1);
444 if (peer->afc[afi][SAFI_MPLS_VPN])
445 bgp_maximum_prefix_overflow (peer, afi, SAFI_MPLS_VPN, 1);
448 for (rn = bgp_table_top (bgp->rib[afi][SAFI_UNICAST]); rn;
449 rn = bgp_route_next (rn))
451 for (bi = rn->info; bi; bi = next)
453 next = bi->next;
455 if (bi->type == ZEBRA_ROUTE_BGP && bi->sub_type == BGP_ROUTE_NORMAL)
457 changed = 0;
458 metricchanged = 0;
460 if (peer_sort (bi->peer) == BGP_PEER_EBGP && bi->peer->ttl == 1)
461 valid = bgp_nexthop_check_ebgp (afi, bi->attr);
462 else
463 valid = bgp_nexthop_lookup (afi, bi->peer, bi,
464 &changed, &metricchanged);
466 current = CHECK_FLAG (bi->flags, BGP_INFO_VALID) ? 1 : 0;
468 if (changed)
469 SET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
470 else
471 UNSET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
473 if (valid != current)
475 if (CHECK_FLAG (bi->flags, BGP_INFO_VALID))
477 bgp_aggregate_decrement (bgp, &rn->p, bi,
478 afi, SAFI_UNICAST);
479 bgp_info_unset_flag (rn, bi, BGP_INFO_VALID);
481 else
483 bgp_info_set_flag (rn, bi, BGP_INFO_VALID);
484 bgp_aggregate_increment (bgp, &rn->p, bi,
485 afi, SAFI_UNICAST);
489 if (CHECK_FLAG (bgp->af_flags[afi][SAFI_UNICAST],
490 BGP_CONFIG_DAMPENING)
491 && bi->extra && bi->extra->damp_info )
492 if (bgp_damp_scan (bi, afi, SAFI_UNICAST))
493 bgp_aggregate_increment (bgp, &rn->p, bi,
494 afi, SAFI_UNICAST);
497 bgp_process (bgp, rn, afi, SAFI_UNICAST);
500 /* Flash old cache. */
501 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
502 bgp_nexthop_cache_reset (cache2_table[afi]);
503 else
504 bgp_nexthop_cache_reset (cache1_table[afi]);
506 if (BGP_DEBUG (events, EVENTS))
508 if (afi == AFI_IP)
509 zlog_debug ("scanning IPv4 Unicast routing tables");
510 else if (afi == AFI_IP6)
511 zlog_debug ("scanning IPv6 Unicast routing tables");
515 /* BGP scan thread. This thread check nexthop reachability. */
516 static int
517 bgp_scan_timer (struct thread *t)
519 bgp_scan_thread =
520 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
522 if (BGP_DEBUG (events, EVENTS))
523 zlog_debug ("Performing BGP general scanning");
525 bgp_scan (AFI_IP, SAFI_UNICAST);
527 #ifdef HAVE_IPV6
528 bgp_scan (AFI_IP6, SAFI_UNICAST);
529 #endif /* HAVE_IPV6 */
531 return 0;
534 struct bgp_connected_ref
536 unsigned int refcnt;
539 void
540 bgp_connected_add (struct connected *ifc)
542 struct prefix p;
543 struct prefix *addr;
544 struct interface *ifp;
545 struct bgp_node *rn;
546 struct bgp_connected_ref *bc;
548 ifp = ifc->ifp;
550 if (! ifp)
551 return;
553 if (if_is_loopback (ifp))
554 return;
556 addr = ifc->address;
558 if (addr->family == AF_INET)
560 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
561 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
563 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
564 return;
566 rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p);
567 if (rn->info)
569 bc = rn->info;
570 bc->refcnt++;
572 else
574 bc = XCALLOC (0, sizeof (struct bgp_connected_ref));
575 bc->refcnt = 1;
576 rn->info = bc;
579 #ifdef HAVE_IPV6
580 else if (addr->family == AF_INET6)
582 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
583 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
585 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
586 return;
588 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
589 return;
591 rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
592 if (rn->info)
594 bc = rn->info;
595 bc->refcnt++;
597 else
599 bc = XCALLOC (0, sizeof (struct bgp_connected_ref));
600 bc->refcnt = 1;
601 rn->info = bc;
604 #endif /* HAVE_IPV6 */
607 void
608 bgp_connected_delete (struct connected *ifc)
610 struct prefix p;
611 struct prefix *addr;
612 struct interface *ifp;
613 struct bgp_node *rn;
614 struct bgp_connected_ref *bc;
616 ifp = ifc->ifp;
618 if (if_is_loopback (ifp))
619 return;
621 addr = ifc->address;
623 if (addr->family == AF_INET)
625 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
626 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
628 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
629 return;
631 rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p);
632 if (! rn)
633 return;
635 bc = rn->info;
636 bc->refcnt--;
637 if (bc->refcnt == 0)
639 XFREE (0, bc);
640 rn->info = NULL;
642 bgp_unlock_node (rn);
643 bgp_unlock_node (rn);
645 #ifdef HAVE_IPV6
646 else if (addr->family == AF_INET6)
648 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
649 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
651 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
652 return;
654 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
655 return;
657 rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
658 if (! rn)
659 return;
661 bc = rn->info;
662 bc->refcnt--;
663 if (bc->refcnt == 0)
665 XFREE (0, bc);
666 rn->info = NULL;
668 bgp_unlock_node (rn);
669 bgp_unlock_node (rn);
671 #endif /* HAVE_IPV6 */
675 bgp_nexthop_self (afi_t afi, struct attr *attr)
677 struct listnode *node;
678 struct listnode *node2;
679 struct interface *ifp;
680 struct connected *ifc;
681 struct prefix *p;
683 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
685 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
687 p = ifc->address;
689 if (p && p->family == AF_INET
690 && IPV4_ADDR_SAME (&p->u.prefix4, &attr->nexthop))
691 return 1;
694 return 0;
697 static struct bgp_nexthop_cache *
698 zlookup_read (void)
700 struct stream *s;
701 uint16_t length;
702 u_char marker;
703 u_char version;
704 uint16_t command;
705 int nbytes;
706 struct in_addr raddr;
707 uint32_t metric;
708 int i;
709 u_char nexthop_num;
710 struct nexthop *nexthop;
711 struct bgp_nexthop_cache *bnc;
713 s = zlookup->ibuf;
714 stream_reset (s);
716 nbytes = stream_read (s, zlookup->sock, 2);
717 length = stream_getw (s);
719 nbytes = stream_read (s, zlookup->sock, length - 2);
720 marker = stream_getc (s);
721 version = stream_getc (s);
723 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
725 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
726 __func__, zlookup->sock, marker, version);
727 return NULL;
730 command = stream_getw (s);
732 raddr.s_addr = stream_get_ipv4 (s);
733 metric = stream_getl (s);
734 nexthop_num = stream_getc (s);
736 if (nexthop_num)
738 bnc = bnc_new ();
739 bnc->valid = 1;
740 bnc->metric = metric;
741 bnc->nexthop_num = nexthop_num;
743 for (i = 0; i < nexthop_num; i++)
745 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
746 nexthop->type = stream_getc (s);
747 switch (nexthop->type)
749 case ZEBRA_NEXTHOP_IPV4:
750 nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s);
751 break;
752 case ZEBRA_NEXTHOP_IFINDEX:
753 case ZEBRA_NEXTHOP_IFNAME:
754 nexthop->ifindex = stream_getl (s);
755 break;
756 default:
757 /* do nothing */
758 break;
760 bnc_nexthop_add (bnc, nexthop);
763 else
764 return NULL;
766 return bnc;
769 struct bgp_nexthop_cache *
770 zlookup_query (struct in_addr addr)
772 int ret;
773 struct stream *s;
775 /* Check socket. */
776 if (zlookup->sock < 0)
777 return NULL;
779 s = zlookup->obuf;
780 stream_reset (s);
781 zclient_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
782 stream_put_in_addr (s, &addr);
784 stream_putw_at (s, 0, stream_get_endp (s));
786 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
787 if (ret < 0)
789 zlog_err ("can't write to zlookup->sock");
790 close (zlookup->sock);
791 zlookup->sock = -1;
792 return NULL;
794 if (ret == 0)
796 zlog_err ("zlookup->sock connection closed");
797 close (zlookup->sock);
798 zlookup->sock = -1;
799 return NULL;
802 return zlookup_read ();
805 #ifdef HAVE_IPV6
806 static struct bgp_nexthop_cache *
807 zlookup_read_ipv6 (void)
809 struct stream *s;
810 uint16_t length;
811 u_char version, marker;
812 uint16_t command;
813 int nbytes;
814 struct in6_addr raddr;
815 uint32_t metric;
816 int i;
817 u_char nexthop_num;
818 struct nexthop *nexthop;
819 struct bgp_nexthop_cache *bnc;
821 s = zlookup->ibuf;
822 stream_reset (s);
824 nbytes = stream_read (s, zlookup->sock, 2);
825 length = stream_getw (s);
827 nbytes = stream_read (s, zlookup->sock, length - 2);
828 marker = stream_getc (s);
829 version = stream_getc (s);
831 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
833 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
834 __func__, zlookup->sock, marker, version);
835 return NULL;
838 command = stream_getw (s);
840 stream_get (&raddr, s, 16);
842 metric = stream_getl (s);
843 nexthop_num = stream_getc (s);
845 if (nexthop_num)
847 bnc = bnc_new ();
848 bnc->valid = 1;
849 bnc->metric = metric;
850 bnc->nexthop_num = nexthop_num;
852 for (i = 0; i < nexthop_num; i++)
854 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
855 nexthop->type = stream_getc (s);
856 switch (nexthop->type)
858 case ZEBRA_NEXTHOP_IPV6:
859 stream_get (&nexthop->gate.ipv6, s, 16);
860 break;
861 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
862 case ZEBRA_NEXTHOP_IPV6_IFNAME:
863 stream_get (&nexthop->gate.ipv6, s, 16);
864 nexthop->ifindex = stream_getl (s);
865 break;
866 case ZEBRA_NEXTHOP_IFINDEX:
867 case ZEBRA_NEXTHOP_IFNAME:
868 nexthop->ifindex = stream_getl (s);
869 break;
870 default:
871 /* do nothing */
872 break;
874 bnc_nexthop_add (bnc, nexthop);
877 else
878 return NULL;
880 return bnc;
883 struct bgp_nexthop_cache *
884 zlookup_query_ipv6 (struct in6_addr *addr)
886 int ret;
887 struct stream *s;
889 /* Check socket. */
890 if (zlookup->sock < 0)
891 return NULL;
893 s = zlookup->obuf;
894 stream_reset (s);
895 zclient_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
896 stream_put (s, addr, 16);
897 stream_putw_at (s, 0, stream_get_endp (s));
899 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
900 if (ret < 0)
902 zlog_err ("can't write to zlookup->sock");
903 close (zlookup->sock);
904 zlookup->sock = -1;
905 return NULL;
907 if (ret == 0)
909 zlog_err ("zlookup->sock connection closed");
910 close (zlookup->sock);
911 zlookup->sock = -1;
912 return NULL;
915 return zlookup_read_ipv6 ();
917 #endif /* HAVE_IPV6 */
919 static int
920 bgp_import_check (struct prefix *p, u_int32_t *igpmetric,
921 struct in_addr *igpnexthop)
923 struct stream *s;
924 int ret;
925 u_int16_t length, command;
926 u_char version, marker;
927 int nbytes;
928 struct in_addr addr;
929 struct in_addr nexthop;
930 u_int32_t metric = 0;
931 u_char nexthop_num;
932 u_char nexthop_type;
934 /* If lookup connection is not available return valid. */
935 if (zlookup->sock < 0)
937 if (igpmetric)
938 *igpmetric = 0;
939 return 1;
942 /* Send query to the lookup connection */
943 s = zlookup->obuf;
944 stream_reset (s);
945 zclient_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP);
947 stream_putc (s, p->prefixlen);
948 stream_put_in_addr (s, &p->u.prefix4);
950 stream_putw_at (s, 0, stream_get_endp (s));
952 /* Write the packet. */
953 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
955 if (ret < 0)
957 zlog_err ("can't write to zlookup->sock");
958 close (zlookup->sock);
959 zlookup->sock = -1;
960 return 1;
962 if (ret == 0)
964 zlog_err ("zlookup->sock connection closed");
965 close (zlookup->sock);
966 zlookup->sock = -1;
967 return 1;
970 /* Get result. */
971 stream_reset (s);
973 /* Fetch length. */
974 nbytes = stream_read (s, zlookup->sock, 2);
975 length = stream_getw (s);
977 /* Fetch whole data. */
978 nbytes = stream_read (s, zlookup->sock, length - 2);
979 marker = stream_getc (s);
980 version = stream_getc (s);
982 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
984 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
985 __func__, zlookup->sock, marker, version);
986 return 0;
989 command = stream_getw (s);
991 addr.s_addr = stream_get_ipv4 (s);
992 metric = stream_getl (s);
993 nexthop_num = stream_getc (s);
995 /* Set IGP metric value. */
996 if (igpmetric)
997 *igpmetric = metric;
999 /* If there is nexthop then this is active route. */
1000 if (nexthop_num)
1002 nexthop.s_addr = 0;
1003 nexthop_type = stream_getc (s);
1004 if (nexthop_type == ZEBRA_NEXTHOP_IPV4)
1006 nexthop.s_addr = stream_get_ipv4 (s);
1007 if (igpnexthop)
1008 *igpnexthop = nexthop;
1010 else
1011 *igpnexthop = nexthop;
1013 return 1;
1015 else
1016 return 0;
1019 /* Scan all configured BGP route then check the route exists in IGP or
1020 not. */
1021 static int
1022 bgp_import (struct thread *t)
1024 struct bgp *bgp;
1025 struct bgp_node *rn;
1026 struct bgp_static *bgp_static;
1027 struct listnode *node, *nnode;
1028 int valid;
1029 u_int32_t metric;
1030 struct in_addr nexthop;
1031 afi_t afi;
1032 safi_t safi;
1034 bgp_import_thread =
1035 thread_add_timer (master, bgp_import, NULL, bgp_import_interval);
1037 if (BGP_DEBUG (events, EVENTS))
1038 zlog_debug ("Import timer expired.");
1040 for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
1042 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1043 for (safi = SAFI_UNICAST; safi < SAFI_MPLS_VPN; safi++)
1044 for (rn = bgp_table_top (bgp->route[afi][safi]); rn;
1045 rn = bgp_route_next (rn))
1046 if ((bgp_static = rn->info) != NULL)
1048 if (bgp_static->backdoor)
1049 continue;
1051 valid = bgp_static->valid;
1052 metric = bgp_static->igpmetric;
1053 nexthop = bgp_static->igpnexthop;
1055 if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK)
1056 && afi == AFI_IP && safi == SAFI_UNICAST)
1057 bgp_static->valid = bgp_import_check (&rn->p, &bgp_static->igpmetric,
1058 &bgp_static->igpnexthop);
1059 else
1061 bgp_static->valid = 1;
1062 bgp_static->igpmetric = 0;
1063 bgp_static->igpnexthop.s_addr = 0;
1066 if (bgp_static->valid != valid)
1068 if (bgp_static->valid)
1069 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1070 else
1071 bgp_static_withdraw (bgp, &rn->p, afi, safi);
1073 else if (bgp_static->valid)
1075 if (bgp_static->igpmetric != metric
1076 || bgp_static->igpnexthop.s_addr != nexthop.s_addr
1077 || bgp_static->rmap.name)
1078 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1082 return 0;
1085 /* Connect to zebra for nexthop lookup. */
1086 static int
1087 zlookup_connect (struct thread *t)
1089 struct zclient *zlookup;
1091 zlookup = THREAD_ARG (t);
1092 zlookup->t_connect = NULL;
1094 if (zlookup->sock != -1)
1095 return 0;
1097 #ifdef HAVE_TCP_ZEBRA
1098 zlookup->sock = zclient_socket ();
1099 #else
1100 zlookup->sock = zclient_socket_un (ZEBRA_SERV_PATH);
1101 #endif /* HAVE_TCP_ZEBRA */
1102 if (zlookup->sock < 0)
1103 return -1;
1105 return 0;
1108 /* Check specified multiaccess next-hop. */
1110 bgp_multiaccess_check_v4 (struct in_addr nexthop, char *peer)
1112 struct bgp_node *rn1;
1113 struct bgp_node *rn2;
1114 struct prefix p1;
1115 struct prefix p2;
1116 struct in_addr addr;
1117 int ret;
1119 ret = inet_aton (peer, &addr);
1120 if (! ret)
1121 return 0;
1123 memset (&p1, 0, sizeof (struct prefix));
1124 p1.family = AF_INET;
1125 p1.prefixlen = IPV4_MAX_BITLEN;
1126 p1.u.prefix4 = nexthop;
1127 memset (&p2, 0, sizeof (struct prefix));
1128 p2.family = AF_INET;
1129 p2.prefixlen = IPV4_MAX_BITLEN;
1130 p2.u.prefix4 = addr;
1132 /* If bgp scan is not enabled, return invalid. */
1133 if (zlookup->sock < 0)
1134 return 0;
1136 rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p1);
1137 if (! rn1)
1138 return 0;
1140 rn2 = bgp_node_match (bgp_connected_table[AFI_IP], &p2);
1141 if (! rn2)
1142 return 0;
1144 if (rn1 == rn2)
1145 return 1;
1147 return 0;
1150 DEFUN (bgp_scan_time,
1151 bgp_scan_time_cmd,
1152 "bgp scan-time <5-60>",
1153 "BGP specific commands\n"
1154 "Configure background scanner interval\n"
1155 "Scanner interval (seconds)\n")
1157 bgp_scan_interval = atoi (argv[0]);
1159 if (bgp_scan_thread)
1161 thread_cancel (bgp_scan_thread);
1162 bgp_scan_thread =
1163 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
1166 return CMD_SUCCESS;
1169 DEFUN (no_bgp_scan_time,
1170 no_bgp_scan_time_cmd,
1171 "no bgp scan-time",
1172 NO_STR
1173 "BGP specific commands\n"
1174 "Configure background scanner interval\n")
1176 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1178 if (bgp_scan_thread)
1180 thread_cancel (bgp_scan_thread);
1181 bgp_scan_thread =
1182 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
1185 return CMD_SUCCESS;
1188 ALIAS (no_bgp_scan_time,
1189 no_bgp_scan_time_val_cmd,
1190 "no bgp scan-time <5-60>",
1191 NO_STR
1192 "BGP specific commands\n"
1193 "Configure background scanner interval\n"
1194 "Scanner interval (seconds)\n")
1196 DEFUN (show_ip_bgp_scan,
1197 show_ip_bgp_scan_cmd,
1198 "show ip bgp scan",
1199 SHOW_STR
1200 IP_STR
1201 BGP_STR
1202 "BGP scan status\n")
1204 struct bgp_node *rn;
1205 struct bgp_nexthop_cache *bnc;
1207 if (bgp_scan_thread)
1208 vty_out (vty, "BGP scan is running%s", VTY_NEWLINE);
1209 else
1210 vty_out (vty, "BGP scan is not running%s", VTY_NEWLINE);
1211 vty_out (vty, "BGP scan interval is %d%s", bgp_scan_interval, VTY_NEWLINE);
1213 vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE);
1214 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP]); rn; rn = bgp_route_next (rn))
1215 if ((bnc = rn->info) != NULL)
1217 if (bnc->valid)
1218 vty_out (vty, " %s valid [IGP metric %d]%s",
1219 inet_ntoa (rn->p.u.prefix4), bnc->metric, VTY_NEWLINE);
1220 else
1221 vty_out (vty, " %s invalid%s",
1222 inet_ntoa (rn->p.u.prefix4), VTY_NEWLINE);
1225 #ifdef HAVE_IPV6
1227 char buf[BUFSIZ];
1228 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]);
1229 rn;
1230 rn = bgp_route_next (rn))
1231 if ((bnc = rn->info) != NULL)
1233 if (bnc->valid)
1234 vty_out (vty, " %s valid [IGP metric %d]%s",
1235 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
1236 bnc->metric, VTY_NEWLINE);
1237 else
1238 vty_out (vty, " %s invalid%s",
1239 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
1240 VTY_NEWLINE);
1243 #endif /* HAVE_IPV6 */
1245 vty_out (vty, "BGP connected route:%s", VTY_NEWLINE);
1246 for (rn = bgp_table_top (bgp_connected_table[AFI_IP]);
1247 rn;
1248 rn = bgp_route_next (rn))
1249 if (rn->info != NULL)
1250 vty_out (vty, " %s/%d%s", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen,
1251 VTY_NEWLINE);
1253 #ifdef HAVE_IPV6
1255 char buf[BUFSIZ];
1257 for (rn = bgp_table_top (bgp_connected_table[AFI_IP6]);
1258 rn;
1259 rn = bgp_route_next (rn))
1260 if (rn->info != NULL)
1261 vty_out (vty, " %s/%d%s",
1262 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
1263 rn->p.prefixlen,
1264 VTY_NEWLINE);
1266 #endif /* HAVE_IPV6 */
1268 return CMD_SUCCESS;
1272 bgp_config_write_scan_time (struct vty *vty)
1274 if (bgp_scan_interval != BGP_SCAN_INTERVAL_DEFAULT)
1275 vty_out (vty, " bgp scan-time %d%s", bgp_scan_interval, VTY_NEWLINE);
1276 return CMD_SUCCESS;
1279 void
1280 bgp_scan_init (void)
1282 zlookup = zclient_new ();
1283 zlookup->sock = -1;
1284 zlookup->t_connect = thread_add_event (master, zlookup_connect, zlookup, 0);
1286 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1287 bgp_import_interval = BGP_IMPORT_INTERVAL_DEFAULT;
1289 cache1_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1290 cache2_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1291 bgp_nexthop_cache_table[AFI_IP] = cache1_table[AFI_IP];
1293 bgp_connected_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1295 #ifdef HAVE_IPV6
1296 cache1_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1297 cache2_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1298 bgp_nexthop_cache_table[AFI_IP6] = cache1_table[AFI_IP6];
1299 bgp_connected_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1300 #endif /* HAVE_IPV6 */
1302 /* Make BGP scan thread. */
1303 bgp_scan_thread = thread_add_timer (master, bgp_scan_timer,
1304 NULL, bgp_scan_interval);
1305 /* Make BGP import there. */
1306 bgp_import_thread = thread_add_timer (master, bgp_import, NULL, 0);
1308 install_element (BGP_NODE, &bgp_scan_time_cmd);
1309 install_element (BGP_NODE, &no_bgp_scan_time_cmd);
1310 install_element (BGP_NODE, &no_bgp_scan_time_val_cmd);
1311 install_element (VIEW_NODE, &show_ip_bgp_scan_cmd);
1312 install_element (RESTRICTED_NODE, &show_ip_bgp_scan_cmd);
1313 install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd);
1316 void
1317 bgp_scan_finish (void)
1319 bgp_table_unlock (cache1_table[AFI_IP]);
1320 cache1_table[AFI_IP] = NULL;
1322 bgp_table_unlock (cache2_table[AFI_IP]);
1323 cache2_table[AFI_IP] = NULL;
1325 bgp_table_unlock (bgp_connected_table[AFI_IP]);
1326 bgp_connected_table[AFI_IP] = NULL;
1328 #ifdef HAVE_IPV6
1329 bgp_table_unlock (cache1_table[AFI_IP6]);
1330 cache1_table[AFI_IP6] = NULL;
1332 bgp_table_unlock (cache2_table[AFI_IP6]);
1333 cache2_table[AFI_IP6] = NULL;
1335 bgp_table_unlock (bgp_connected_table[AFI_IP6]);
1336 bgp_connected_table[AFI_IP6] = NULL;
1337 #endif /* HAVE_IPV6 */