[bgpd] bug #352: IPv6/Multicast address-family config not written out
[jleu-quagga.git] / bgpd / bgp_nexthop.c
blob50ae74e45582d263efda1e441eeff6ee9e3f46cc
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 static 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 ()
101 struct bgp_nexthop_cache *new;
103 new = XMALLOC (MTYPE_BGP_NEXTHOP_CACHE, sizeof (struct bgp_nexthop_cache));
104 memset (new, 0, sizeof (struct bgp_nexthop_cache));
105 return new;
108 static void
109 bnc_free (struct bgp_nexthop_cache *bnc)
111 bnc_nexthop_free (bnc);
112 XFREE (MTYPE_BGP_NEXTHOP_CACHE, bnc);
115 static int
116 bgp_nexthop_same (struct nexthop *next1, struct nexthop *next2)
118 if (next1->type != next2->type)
119 return 0;
121 switch (next1->type)
123 case ZEBRA_NEXTHOP_IPV4:
124 if (! IPV4_ADDR_SAME (&next1->gate.ipv4, &next2->gate.ipv4))
125 return 0;
126 break;
127 case ZEBRA_NEXTHOP_IFINDEX:
128 case ZEBRA_NEXTHOP_IFNAME:
129 if (next1->ifindex != next2->ifindex)
130 return 0;
131 break;
132 #ifdef HAVE_IPV6
133 case ZEBRA_NEXTHOP_IPV6:
134 if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
135 return 0;
136 break;
137 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
138 case ZEBRA_NEXTHOP_IPV6_IFNAME:
139 if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
140 return 0;
141 if (next1->ifindex != next2->ifindex)
142 return 0;
143 break;
144 #endif /* HAVE_IPV6 */
145 default:
146 /* do nothing */
147 break;
149 return 1;
152 static int
153 bgp_nexthop_cache_changed (struct bgp_nexthop_cache *bnc1,
154 struct bgp_nexthop_cache *bnc2)
156 int i;
157 struct nexthop *next1, *next2;
159 if (bnc1->nexthop_num != bnc2->nexthop_num)
160 return 1;
162 next1 = bnc1->nexthop;
163 next2 = bnc2->nexthop;
165 for (i = 0; i < bnc1->nexthop_num; i++)
167 if (! bgp_nexthop_same (next1, next2))
168 return 1;
170 next1 = next1->next;
171 next2 = next2->next;
173 return 0;
176 /* If nexthop exists on connected network return 1. */
178 bgp_nexthop_check_ebgp (afi_t afi, struct attr *attr)
180 struct bgp_node *rn;
182 /* If zebra is not enabled return */
183 if (zlookup->sock < 0)
184 return 1;
186 /* Lookup the address is onlink or not. */
187 if (afi == AFI_IP)
189 rn = bgp_node_match_ipv4 (bgp_connected_table[AFI_IP], &attr->nexthop);
190 if (rn)
192 bgp_unlock_node (rn);
193 return 1;
196 #ifdef HAVE_IPV6
197 else if (afi == AFI_IP6)
199 if (attr->mp_nexthop_len == 32)
200 return 1;
201 else if (attr->mp_nexthop_len == 16)
203 if (IN6_IS_ADDR_LINKLOCAL (&attr->mp_nexthop_global))
204 return 1;
206 rn = bgp_node_match_ipv6 (bgp_connected_table[AFI_IP6],
207 &attr->mp_nexthop_global);
208 if (rn)
210 bgp_unlock_node (rn);
211 return 1;
215 #endif /* HAVE_IPV6 */
216 return 0;
219 #ifdef HAVE_IPV6
220 /* Check specified next-hop is reachable or not. */
221 static int
222 bgp_nexthop_lookup_ipv6 (struct peer *peer, struct bgp_info *ri, int *changed,
223 int *metricchanged)
225 struct bgp_node *rn;
226 struct prefix p;
227 struct bgp_nexthop_cache *bnc;
228 struct attr *attr;
230 /* If lookup is not enabled, return valid. */
231 if (zlookup->sock < 0)
233 ri->igpmetric = 0;
234 return 1;
237 /* Only check IPv6 global address only nexthop. */
238 attr = ri->attr;
240 if (attr->mp_nexthop_len != 16
241 || IN6_IS_ADDR_LINKLOCAL (&attr->mp_nexthop_global))
242 return 1;
244 memset (&p, 0, sizeof (struct prefix));
245 p.family = AF_INET6;
246 p.prefixlen = IPV6_MAX_BITLEN;
247 p.u.prefix6 = attr->mp_nexthop_global;
249 /* IBGP or ebgp-multihop */
250 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP6], &p);
252 if (rn->info)
254 bnc = rn->info;
255 bgp_unlock_node (rn);
257 else
259 bnc = zlookup_query_ipv6 (&attr->mp_nexthop_global);
260 if (bnc)
262 struct bgp_table *old;
263 struct bgp_node *oldrn;
264 struct bgp_nexthop_cache *oldbnc;
266 if (changed)
268 if (bgp_nexthop_cache_table[AFI_IP6] == cache1_table[AFI_IP6])
269 old = cache2_table[AFI_IP6];
270 else
271 old = cache1_table[AFI_IP6];
273 oldrn = bgp_node_lookup (old, &p);
274 if (oldrn)
276 oldbnc = oldrn->info;
278 bnc->changed = bgp_nexthop_cache_changed (bnc, oldbnc);
280 if (bnc->metric != oldbnc->metric)
281 bnc->metricchanged = 1;
285 else
287 bnc = bnc_new ();
288 bnc->valid = 0;
290 rn->info = bnc;
293 if (changed)
294 *changed = bnc->changed;
296 if (metricchanged)
297 *metricchanged = bnc->metricchanged;
299 if (bnc->valid)
300 ri->igpmetric = bnc->metric;
301 else
302 ri->igpmetric = 0;
304 return bnc->valid;
306 #endif /* HAVE_IPV6 */
308 /* Check specified next-hop is reachable or not. */
310 bgp_nexthop_lookup (afi_t afi, struct peer *peer, struct bgp_info *ri,
311 int *changed, int *metricchanged)
313 struct bgp_node *rn;
314 struct prefix p;
315 struct bgp_nexthop_cache *bnc;
316 struct in_addr addr;
318 /* If lookup is not enabled, return valid. */
319 if (zlookup->sock < 0)
321 ri->igpmetric = 0;
322 return 1;
325 #ifdef HAVE_IPV6
326 if (afi == AFI_IP6)
327 return bgp_nexthop_lookup_ipv6 (peer, ri, changed, metricchanged);
328 #endif /* HAVE_IPV6 */
330 addr = ri->attr->nexthop;
332 memset (&p, 0, sizeof (struct prefix));
333 p.family = AF_INET;
334 p.prefixlen = IPV4_MAX_BITLEN;
335 p.u.prefix4 = addr;
337 /* IBGP or ebgp-multihop */
338 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP], &p);
340 if (rn->info)
342 bnc = rn->info;
343 bgp_unlock_node (rn);
345 else
347 bnc = zlookup_query (addr);
348 if (bnc)
350 struct bgp_table *old;
351 struct bgp_node *oldrn;
352 struct bgp_nexthop_cache *oldbnc;
354 if (changed)
356 if (bgp_nexthop_cache_table[AFI_IP] == cache1_table[AFI_IP])
357 old = cache2_table[AFI_IP];
358 else
359 old = cache1_table[AFI_IP];
361 oldrn = bgp_node_lookup (old, &p);
362 if (oldrn)
364 oldbnc = oldrn->info;
366 bnc->changed = bgp_nexthop_cache_changed (bnc, oldbnc);
368 if (bnc->metric != oldbnc->metric)
369 bnc->metricchanged = 1;
373 else
375 bnc = bnc_new ();
376 bnc->valid = 0;
378 rn->info = bnc;
381 if (changed)
382 *changed = bnc->changed;
384 if (metricchanged)
385 *metricchanged = bnc->metricchanged;
387 if (bnc->valid)
388 ri->igpmetric = bnc->metric;
389 else
390 ri->igpmetric = 0;
392 return bnc->valid;
395 /* Reset and free all BGP nexthop cache. */
396 static void
397 bgp_nexthop_cache_reset (struct bgp_table *table)
399 struct bgp_node *rn;
400 struct bgp_nexthop_cache *bnc;
402 for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
403 if ((bnc = rn->info) != NULL)
405 bnc_free (bnc);
406 rn->info = NULL;
407 bgp_unlock_node (rn);
411 static void
412 bgp_scan (afi_t afi, safi_t safi)
414 struct bgp_node *rn;
415 struct bgp *bgp;
416 struct bgp_info *bi;
417 struct bgp_info *next;
418 struct peer *peer;
419 struct listnode *node, *nnode;
420 int valid;
421 int current;
422 int changed;
423 int metricchanged;
425 /* Change cache. */
426 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
427 bgp_nexthop_cache_table[afi] = cache2_table[afi];
428 else
429 bgp_nexthop_cache_table[afi] = cache1_table[afi];
431 /* Get default bgp. */
432 bgp = bgp_get_default ();
433 if (bgp == NULL)
434 return;
436 /* Maximum prefix check */
437 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
439 if (peer->status != Established)
440 continue;
442 if (peer->afc[afi][SAFI_UNICAST])
443 bgp_maximum_prefix_overflow (peer, afi, SAFI_UNICAST, 1);
444 if (peer->afc[afi][SAFI_MULTICAST])
445 bgp_maximum_prefix_overflow (peer, afi, SAFI_MULTICAST, 1);
446 if (peer->afc[afi][SAFI_MPLS_VPN])
447 bgp_maximum_prefix_overflow (peer, afi, SAFI_MPLS_VPN, 1);
450 for (rn = bgp_table_top (bgp->rib[afi][SAFI_UNICAST]); rn;
451 rn = bgp_route_next (rn))
453 for (bi = rn->info; bi; bi = next)
455 next = bi->next;
457 if (bi->type == ZEBRA_ROUTE_BGP && bi->sub_type == BGP_ROUTE_NORMAL)
459 changed = 0;
460 metricchanged = 0;
462 if (peer_sort (bi->peer) == BGP_PEER_EBGP && bi->peer->ttl == 1)
463 valid = bgp_nexthop_check_ebgp (afi, bi->attr);
464 else
465 valid = bgp_nexthop_lookup (afi, bi->peer, bi,
466 &changed, &metricchanged);
468 current = CHECK_FLAG (bi->flags, BGP_INFO_VALID) ? 1 : 0;
470 if (changed)
471 SET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
472 else
473 UNSET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
475 if (valid != current)
477 if (CHECK_FLAG (bi->flags, BGP_INFO_VALID))
479 bgp_aggregate_decrement (bgp, &rn->p, bi,
480 afi, SAFI_UNICAST);
481 bgp_info_unset_flag (rn, bi, BGP_INFO_VALID);
483 else
485 bgp_info_set_flag (rn, bi, BGP_INFO_VALID);
486 bgp_aggregate_increment (bgp, &rn->p, bi,
487 afi, SAFI_UNICAST);
491 if (CHECK_FLAG (bgp->af_flags[afi][SAFI_UNICAST],
492 BGP_CONFIG_DAMPENING)
493 && bi->damp_info )
494 if (bgp_damp_scan (bi, afi, SAFI_UNICAST))
495 bgp_aggregate_increment (bgp, &rn->p, bi,
496 afi, SAFI_UNICAST);
499 bgp_process (bgp, rn, afi, SAFI_UNICAST);
502 /* Flash old cache. */
503 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
504 bgp_nexthop_cache_reset (cache2_table[afi]);
505 else
506 bgp_nexthop_cache_reset (cache1_table[afi]);
508 if (BGP_DEBUG (events, EVENTS))
510 if (afi == AFI_IP)
511 zlog_debug ("scanning IPv4 Unicast routing tables");
512 else if (afi == AFI_IP6)
513 zlog_debug ("scanning IPv6 Unicast routing tables");
517 /* BGP scan thread. This thread check nexthop reachability. */
518 static int
519 bgp_scan_timer (struct thread *t)
521 bgp_scan_thread =
522 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
524 if (BGP_DEBUG (events, EVENTS))
525 zlog_debug ("Performing BGP general scanning");
527 bgp_scan (AFI_IP, SAFI_UNICAST);
529 #ifdef HAVE_IPV6
530 bgp_scan (AFI_IP6, SAFI_UNICAST);
531 #endif /* HAVE_IPV6 */
533 return 0;
536 struct bgp_connected_ref
538 unsigned int refcnt;
541 void
542 bgp_connected_add (struct connected *ifc)
544 struct prefix p;
545 struct prefix *addr;
546 struct interface *ifp;
547 struct bgp_node *rn;
548 struct bgp_connected_ref *bc;
550 ifp = ifc->ifp;
552 if (! ifp)
553 return;
555 if (if_is_loopback (ifp))
556 return;
558 addr = ifc->address;
560 if (addr->family == AF_INET)
562 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
563 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
565 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
566 return;
568 rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p);
569 if (rn->info)
571 bc = rn->info;
572 bc->refcnt++;
574 else
576 bc = XMALLOC (0, sizeof (struct bgp_connected_ref));
577 memset (bc, 0, sizeof (struct bgp_connected_ref));
578 bc->refcnt = 1;
579 rn->info = bc;
582 #ifdef HAVE_IPV6
583 else if (addr->family == AF_INET6)
585 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
586 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
588 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
589 return;
591 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
592 return;
594 rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
595 if (rn->info)
597 bc = rn->info;
598 bc->refcnt++;
600 else
602 bc = XMALLOC (0, sizeof (struct bgp_connected_ref));
603 memset (bc, 0, sizeof (struct bgp_connected_ref));
604 bc->refcnt = 1;
605 rn->info = bc;
608 #endif /* HAVE_IPV6 */
611 void
612 bgp_connected_delete (struct connected *ifc)
614 struct prefix p;
615 struct prefix *addr;
616 struct interface *ifp;
617 struct bgp_node *rn;
618 struct bgp_connected_ref *bc;
620 ifp = ifc->ifp;
622 if (if_is_loopback (ifp))
623 return;
625 addr = ifc->address;
627 if (addr->family == AF_INET)
629 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
630 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
632 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
633 return;
635 rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p);
636 if (! rn)
637 return;
639 bc = rn->info;
640 bc->refcnt--;
641 if (bc->refcnt == 0)
643 XFREE (0, bc);
644 rn->info = NULL;
646 bgp_unlock_node (rn);
647 bgp_unlock_node (rn);
649 #ifdef HAVE_IPV6
650 else if (addr->family == AF_INET6)
652 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
653 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
655 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
656 return;
658 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
659 return;
661 rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
662 if (! rn)
663 return;
665 bc = rn->info;
666 bc->refcnt--;
667 if (bc->refcnt == 0)
669 XFREE (0, bc);
670 rn->info = NULL;
672 bgp_unlock_node (rn);
673 bgp_unlock_node (rn);
675 #endif /* HAVE_IPV6 */
679 bgp_nexthop_self (afi_t afi, struct attr *attr)
681 struct listnode *node;
682 struct listnode *node2;
683 struct interface *ifp;
684 struct connected *ifc;
685 struct prefix *p;
687 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
689 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
691 p = ifc->address;
693 if (p && p->family == AF_INET
694 && IPV4_ADDR_SAME (&p->u.prefix4, &attr->nexthop))
695 return 1;
698 return 0;
701 static struct bgp_nexthop_cache *
702 zlookup_read ()
704 struct stream *s;
705 uint16_t length;
706 u_char marker;
707 u_char version;
708 uint16_t command;
709 int nbytes;
710 struct in_addr raddr;
711 uint32_t metric;
712 int i;
713 u_char nexthop_num;
714 struct nexthop *nexthop;
715 struct bgp_nexthop_cache *bnc;
717 s = zlookup->ibuf;
718 stream_reset (s);
720 nbytes = stream_read (s, zlookup->sock, 2);
721 length = stream_getw (s);
723 nbytes = stream_read (s, zlookup->sock, length - 2);
724 marker = stream_getc (s);
725 version = stream_getc (s);
727 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
729 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
730 __func__, zlookup->sock, marker, version);
731 return NULL;
734 command = stream_getw (s);
736 raddr.s_addr = stream_get_ipv4 (s);
737 metric = stream_getl (s);
738 nexthop_num = stream_getc (s);
740 if (nexthop_num)
742 bnc = bnc_new ();
743 bnc->valid = 1;
744 bnc->metric = metric;
745 bnc->nexthop_num = nexthop_num;
747 for (i = 0; i < nexthop_num; i++)
749 nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
750 memset (nexthop, 0, sizeof (struct nexthop));
751 nexthop->type = stream_getc (s);
752 switch (nexthop->type)
754 case ZEBRA_NEXTHOP_IPV4:
755 nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s);
756 break;
757 case ZEBRA_NEXTHOP_IFINDEX:
758 case ZEBRA_NEXTHOP_IFNAME:
759 nexthop->ifindex = stream_getl (s);
760 break;
761 default:
762 /* do nothing */
763 break;
765 bnc_nexthop_add (bnc, nexthop);
768 else
769 return NULL;
771 return bnc;
774 struct bgp_nexthop_cache *
775 zlookup_query (struct in_addr addr)
777 int ret;
778 struct stream *s;
780 /* Check socket. */
781 if (zlookup->sock < 0)
782 return NULL;
784 s = zlookup->obuf;
785 stream_reset (s);
786 zclient_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
787 stream_put_in_addr (s, &addr);
789 stream_putw_at (s, 0, stream_get_endp (s));
791 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
792 if (ret < 0)
794 zlog_err ("can't write to zlookup->sock");
795 close (zlookup->sock);
796 zlookup->sock = -1;
797 return NULL;
799 if (ret == 0)
801 zlog_err ("zlookup->sock connection closed");
802 close (zlookup->sock);
803 zlookup->sock = -1;
804 return NULL;
807 return zlookup_read ();
810 #ifdef HAVE_IPV6
811 static struct bgp_nexthop_cache *
812 zlookup_read_ipv6 ()
814 struct stream *s;
815 uint16_t length;
816 u_char version, marker;
817 uint16_t command;
818 int nbytes;
819 struct in6_addr raddr;
820 uint32_t metric;
821 int i;
822 u_char nexthop_num;
823 struct nexthop *nexthop;
824 struct bgp_nexthop_cache *bnc;
826 s = zlookup->ibuf;
827 stream_reset (s);
829 nbytes = stream_read (s, zlookup->sock, 2);
830 length = stream_getw (s);
832 nbytes = stream_read (s, zlookup->sock, length - 2);
833 marker = stream_getc (s);
834 version = stream_getc (s);
836 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
838 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
839 __func__, zlookup->sock, marker, version);
840 return NULL;
843 command = stream_getw (s);
845 stream_get (&raddr, s, 16);
847 metric = stream_getl (s);
848 nexthop_num = stream_getc (s);
850 if (nexthop_num)
852 bnc = bnc_new ();
853 bnc->valid = 1;
854 bnc->metric = metric;
855 bnc->nexthop_num = nexthop_num;
857 for (i = 0; i < nexthop_num; i++)
859 nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
860 memset (nexthop, 0, sizeof (struct nexthop));
861 nexthop->type = stream_getc (s);
862 switch (nexthop->type)
864 case ZEBRA_NEXTHOP_IPV6:
865 stream_get (&nexthop->gate.ipv6, s, 16);
866 break;
867 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
868 case ZEBRA_NEXTHOP_IPV6_IFNAME:
869 stream_get (&nexthop->gate.ipv6, s, 16);
870 nexthop->ifindex = stream_getl (s);
871 break;
872 case ZEBRA_NEXTHOP_IFINDEX:
873 case ZEBRA_NEXTHOP_IFNAME:
874 nexthop->ifindex = stream_getl (s);
875 break;
876 default:
877 /* do nothing */
878 break;
880 bnc_nexthop_add (bnc, nexthop);
883 else
884 return NULL;
886 return bnc;
889 struct bgp_nexthop_cache *
890 zlookup_query_ipv6 (struct in6_addr *addr)
892 int ret;
893 struct stream *s;
895 /* Check socket. */
896 if (zlookup->sock < 0)
897 return NULL;
899 s = zlookup->obuf;
900 stream_reset (s);
901 zclient_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
902 stream_put (s, addr, 16);
903 stream_putw_at (s, 0, stream_get_endp (s));
905 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
906 if (ret < 0)
908 zlog_err ("can't write to zlookup->sock");
909 close (zlookup->sock);
910 zlookup->sock = -1;
911 return NULL;
913 if (ret == 0)
915 zlog_err ("zlookup->sock connection closed");
916 close (zlookup->sock);
917 zlookup->sock = -1;
918 return NULL;
921 return zlookup_read_ipv6 ();
923 #endif /* HAVE_IPV6 */
925 static int
926 bgp_import_check (struct prefix *p, u_int32_t *igpmetric,
927 struct in_addr *igpnexthop)
929 struct stream *s;
930 int ret;
931 u_int16_t length, command;
932 u_char version, marker;
933 int nbytes;
934 struct in_addr addr;
935 struct in_addr nexthop;
936 u_int32_t metric = 0;
937 u_char nexthop_num;
938 u_char nexthop_type;
940 /* If lookup connection is not available return valid. */
941 if (zlookup->sock < 0)
943 if (igpmetric)
944 *igpmetric = 0;
945 return 1;
948 /* Send query to the lookup connection */
949 s = zlookup->obuf;
950 stream_reset (s);
951 zclient_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP);
953 stream_putc (s, p->prefixlen);
954 stream_put_in_addr (s, &p->u.prefix4);
956 stream_putw_at (s, 0, stream_get_endp (s));
958 /* Write the packet. */
959 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
961 if (ret < 0)
963 zlog_err ("can't write to zlookup->sock");
964 close (zlookup->sock);
965 zlookup->sock = -1;
966 return 1;
968 if (ret == 0)
970 zlog_err ("zlookup->sock connection closed");
971 close (zlookup->sock);
972 zlookup->sock = -1;
973 return 1;
976 /* Get result. */
977 stream_reset (s);
979 /* Fetch length. */
980 nbytes = stream_read (s, zlookup->sock, 2);
981 length = stream_getw (s);
983 /* Fetch whole data. */
984 nbytes = stream_read (s, zlookup->sock, length - 2);
985 marker = stream_getc (s);
986 version = stream_getc (s);
988 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
990 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
991 __func__, zlookup->sock, marker, version);
992 return 0;
995 command = stream_getw (s);
997 addr.s_addr = stream_get_ipv4 (s);
998 metric = stream_getl (s);
999 nexthop_num = stream_getc (s);
1001 /* Set IGP metric value. */
1002 if (igpmetric)
1003 *igpmetric = metric;
1005 /* If there is nexthop then this is active route. */
1006 if (nexthop_num)
1008 nexthop.s_addr = 0;
1009 nexthop_type = stream_getc (s);
1010 if (nexthop_type == ZEBRA_NEXTHOP_IPV4)
1012 nexthop.s_addr = stream_get_ipv4 (s);
1013 if (igpnexthop)
1014 *igpnexthop = nexthop;
1016 else
1017 *igpnexthop = nexthop;
1019 return 1;
1021 else
1022 return 0;
1025 /* Scan all configured BGP route then check the route exists in IGP or
1026 not. */
1027 static int
1028 bgp_import (struct thread *t)
1030 struct bgp *bgp;
1031 struct bgp_node *rn;
1032 struct bgp_static *bgp_static;
1033 struct listnode *node, *nnode;
1034 int valid;
1035 u_int32_t metric;
1036 struct in_addr nexthop;
1037 afi_t afi;
1038 safi_t safi;
1040 bgp_import_thread =
1041 thread_add_timer (master, bgp_import, NULL, bgp_import_interval);
1043 if (BGP_DEBUG (events, EVENTS))
1044 zlog_debug ("Import timer expired.");
1046 for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
1048 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1049 for (safi = SAFI_UNICAST; safi < SAFI_MPLS_VPN; safi++)
1050 for (rn = bgp_table_top (bgp->route[afi][safi]); rn;
1051 rn = bgp_route_next (rn))
1052 if ((bgp_static = rn->info) != NULL)
1054 if (bgp_static->backdoor)
1055 continue;
1057 valid = bgp_static->valid;
1058 metric = bgp_static->igpmetric;
1059 nexthop = bgp_static->igpnexthop;
1061 if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK)
1062 && afi == AFI_IP && safi == SAFI_UNICAST)
1063 bgp_static->valid = bgp_import_check (&rn->p, &bgp_static->igpmetric,
1064 &bgp_static->igpnexthop);
1065 else
1067 bgp_static->valid = 1;
1068 bgp_static->igpmetric = 0;
1069 bgp_static->igpnexthop.s_addr = 0;
1072 if (bgp_static->valid != valid)
1074 if (bgp_static->valid)
1075 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1076 else
1077 bgp_static_withdraw (bgp, &rn->p, afi, safi);
1079 else if (bgp_static->valid)
1081 if (bgp_static->igpmetric != metric
1082 || bgp_static->igpnexthop.s_addr != nexthop.s_addr
1083 || bgp_static->rmap.name)
1084 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1088 return 0;
1091 /* Connect to zebra for nexthop lookup. */
1092 static int
1093 zlookup_connect (struct thread *t)
1095 struct zclient *zlookup;
1097 zlookup = THREAD_ARG (t);
1098 zlookup->t_connect = NULL;
1100 if (zlookup->sock != -1)
1101 return 0;
1103 #ifdef HAVE_TCP_ZEBRA
1104 zlookup->sock = zclient_socket ();
1105 #else
1106 zlookup->sock = zclient_socket_un (ZEBRA_SERV_PATH);
1107 #endif /* HAVE_TCP_ZEBRA */
1108 if (zlookup->sock < 0)
1109 return -1;
1111 return 0;
1114 /* Check specified multiaccess next-hop. */
1116 bgp_multiaccess_check_v4 (struct in_addr nexthop, char *peer)
1118 struct bgp_node *rn1;
1119 struct bgp_node *rn2;
1120 struct prefix p1;
1121 struct prefix p2;
1122 struct in_addr addr;
1123 int ret;
1125 ret = inet_aton (peer, &addr);
1126 if (! ret)
1127 return 0;
1129 memset (&p1, 0, sizeof (struct prefix));
1130 p1.family = AF_INET;
1131 p1.prefixlen = IPV4_MAX_BITLEN;
1132 p1.u.prefix4 = nexthop;
1133 memset (&p2, 0, sizeof (struct prefix));
1134 p2.family = AF_INET;
1135 p2.prefixlen = IPV4_MAX_BITLEN;
1136 p2.u.prefix4 = addr;
1138 /* If bgp scan is not enabled, return invalid. */
1139 if (zlookup->sock < 0)
1140 return 0;
1142 rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p1);
1143 if (! rn1)
1144 return 0;
1146 rn2 = bgp_node_match (bgp_connected_table[AFI_IP], &p2);
1147 if (! rn2)
1148 return 0;
1150 if (rn1 == rn2)
1151 return 1;
1153 return 0;
1156 DEFUN (bgp_scan_time,
1157 bgp_scan_time_cmd,
1158 "bgp scan-time <5-60>",
1159 "BGP specific commands\n"
1160 "Configure background scanner interval\n"
1161 "Scanner interval (seconds)\n")
1163 bgp_scan_interval = atoi (argv[0]);
1165 if (bgp_scan_thread)
1167 thread_cancel (bgp_scan_thread);
1168 bgp_scan_thread =
1169 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
1172 return CMD_SUCCESS;
1175 DEFUN (no_bgp_scan_time,
1176 no_bgp_scan_time_cmd,
1177 "no bgp scan-time",
1178 NO_STR
1179 "BGP specific commands\n"
1180 "Configure background scanner interval\n")
1182 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1184 if (bgp_scan_thread)
1186 thread_cancel (bgp_scan_thread);
1187 bgp_scan_thread =
1188 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
1191 return CMD_SUCCESS;
1194 ALIAS (no_bgp_scan_time,
1195 no_bgp_scan_time_val_cmd,
1196 "no bgp scan-time <5-60>",
1197 NO_STR
1198 "BGP specific commands\n"
1199 "Configure background scanner interval\n"
1200 "Scanner interval (seconds)\n")
1202 DEFUN (show_ip_bgp_scan,
1203 show_ip_bgp_scan_cmd,
1204 "show ip bgp scan",
1205 SHOW_STR
1206 IP_STR
1207 BGP_STR
1208 "BGP scan status\n")
1210 struct bgp_node *rn;
1211 struct bgp_nexthop_cache *bnc;
1213 if (bgp_scan_thread)
1214 vty_out (vty, "BGP scan is running%s", VTY_NEWLINE);
1215 else
1216 vty_out (vty, "BGP scan is not running%s", VTY_NEWLINE);
1217 vty_out (vty, "BGP scan interval is %d%s", bgp_scan_interval, VTY_NEWLINE);
1219 vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE);
1220 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP]); rn; rn = bgp_route_next (rn))
1221 if ((bnc = rn->info) != NULL)
1223 if (bnc->valid)
1224 vty_out (vty, " %s valid [IGP metric %d]%s",
1225 inet_ntoa (rn->p.u.prefix4), bnc->metric, VTY_NEWLINE);
1226 else
1227 vty_out (vty, " %s invalid%s",
1228 inet_ntoa (rn->p.u.prefix4), VTY_NEWLINE);
1231 #ifdef HAVE_IPV6
1233 char buf[BUFSIZ];
1234 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]);
1235 rn;
1236 rn = bgp_route_next (rn))
1237 if ((bnc = rn->info) != NULL)
1239 if (bnc->valid)
1240 vty_out (vty, " %s valid [IGP metric %d]%s",
1241 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
1242 bnc->metric, VTY_NEWLINE);
1243 else
1244 vty_out (vty, " %s invalid%s",
1245 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
1246 VTY_NEWLINE);
1249 #endif /* HAVE_IPV6 */
1251 vty_out (vty, "BGP connected route:%s", VTY_NEWLINE);
1252 for (rn = bgp_table_top (bgp_connected_table[AFI_IP]);
1253 rn;
1254 rn = bgp_route_next (rn))
1255 if (rn->info != NULL)
1256 vty_out (vty, " %s/%d%s", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen,
1257 VTY_NEWLINE);
1259 #ifdef HAVE_IPV6
1261 char buf[BUFSIZ];
1263 for (rn = bgp_table_top (bgp_connected_table[AFI_IP6]);
1264 rn;
1265 rn = bgp_route_next (rn))
1266 if (rn->info != NULL)
1267 vty_out (vty, " %s/%d%s",
1268 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
1269 rn->p.prefixlen,
1270 VTY_NEWLINE);
1272 #endif /* HAVE_IPV6 */
1274 return CMD_SUCCESS;
1278 bgp_config_write_scan_time (struct vty *vty)
1280 if (bgp_scan_interval != BGP_SCAN_INTERVAL_DEFAULT)
1281 vty_out (vty, " bgp scan-time %d%s", bgp_scan_interval, VTY_NEWLINE);
1282 return CMD_SUCCESS;
1285 void
1286 bgp_scan_init ()
1288 zlookup = zclient_new ();
1289 zlookup->sock = -1;
1290 zlookup->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1291 zlookup->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1292 zlookup->t_connect = thread_add_event (master, zlookup_connect, zlookup, 0);
1294 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1295 bgp_import_interval = BGP_IMPORT_INTERVAL_DEFAULT;
1297 cache1_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1298 cache2_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1299 bgp_nexthop_cache_table[AFI_IP] = cache1_table[AFI_IP];
1301 bgp_connected_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1303 #ifdef HAVE_IPV6
1304 cache1_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1305 cache2_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1306 bgp_nexthop_cache_table[AFI_IP6] = cache1_table[AFI_IP6];
1307 bgp_connected_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1308 #endif /* HAVE_IPV6 */
1310 /* Make BGP scan thread. */
1311 bgp_scan_thread = thread_add_timer (master, bgp_scan_timer,
1312 NULL, bgp_scan_interval);
1313 /* Make BGP import there. */
1314 bgp_import_thread = thread_add_timer (master, bgp_import, NULL, 0);
1316 install_element (BGP_NODE, &bgp_scan_time_cmd);
1317 install_element (BGP_NODE, &no_bgp_scan_time_cmd);
1318 install_element (BGP_NODE, &no_bgp_scan_time_val_cmd);
1319 install_element (VIEW_NODE, &show_ip_bgp_scan_cmd);
1320 install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd);